Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00036 include_once 'PEAR.php';
00037 include_once 'DB.php';
00038
00039 class ilCronClients extends PEAR
00040 {
00041 var $fp;
00042
00043
00044 function ilCronClients()
00045 {
00046 define('INI_FILE_PATH','../ilias.ini.php');
00047 define('CRON_DEBUG',1);
00048
00049 $this->__createLock();
00050 $this->__readClients();
00051 register_shutdown_function(array($this,'__ilCronClients'));
00052 }
00053
00054
00055 function &_getInstance()
00056 {
00057 if(ilCronClients::_lockExists())
00058 {
00059 die('Instance already created');
00060 }
00061 return new ilCronClients();
00062 }
00063
00064 function __createLock()
00065 {
00066 $this->fp = @fopen('cron.lock','wb');
00067 fwrite($this->fp,(string) time(),strlen((string) time()));
00068
00069 return true;
00070 }
00071
00072 function _lockExists()
00073 {
00074 if(@file_exists('cron.lock'))
00075 {
00076 $fp = fopen('cron.lock','r');
00077
00078 (int) $timest = fread($fp,filesize('cron.lock'));
00079
00080 if(!CRON_DEBUG and ($timest > time() - 60 * 60 * 12))
00081 {
00082 return true;
00083 }
00084 unlink('cron.lock');
00085 }
00086 return false;
00087 }
00088
00089 function __readClients()
00090 {
00091 include_once '../classes/class.ilIniFile.php';
00092
00093 $ini_file_obj =& new ilIniFile(INI_FILE_PATH);
00094
00095 $ini_file_obj->read();
00096
00097 $this->log['enabled'] = $ini_file_obj->readVariable('log','enabled');
00098 $this->log['path'] = $ini_file_obj->readVariable('log','path');
00099 $this->log['file'] = $ini_file_obj->readVariable('log','file');
00100
00101 $this->web_enabled = $ini_file_obj->readVariable('cron','web_enabled');
00102 $this->web_pass = $ini_file_obj->readVariable('cron','web_pass');
00103 $this->__checkAccess();
00104
00105
00106 $this->client_data = $ini_file_obj->readGroup('clients');
00107 unset($ini_file_obj);
00108
00109
00110
00111
00112
00113 $this->client_ini = array();
00114 $dp = opendir('../'.$this->client_data['path']);
00115 while(($file = readdir($dp)) !== false)
00116 {
00117 if($file == '.' or $file == '..' or $file == 'CVS')
00118 {
00119 continue;
00120 }
00121 if(@file_exists('../'.$this->client_data['path'].'/'.$file.'/'.$this->client_data['inifile']))
00122 {
00123 $tmp_data['path'] = '../'.$this->client_data['path'].'/'.$file.'/'.$this->client_data['inifile'];
00124 $tmp_data['name'] = $file;
00125
00126 $this->client_ini[] = $tmp_data;
00127 unset($tmp_data);
00128 }
00129 }
00130
00131 $this->__startChecks();
00132 }
00133
00134 function __startChecks()
00135 {
00136 foreach($this->client_ini as $client_data)
00137 {
00138 include_once '../classes/class.ilIniFile.php';
00139
00140 $ini_file_obj =& new ilIniFile($client_data['path']);
00141
00142 $ini_file_obj->read();
00143 $this->db_data = $ini_file_obj->readGroup('db');
00144
00145 $this->__readFileDBVersion();
00146
00147 if($this->__openDb())
00148 {
00149 include_once './classes/class.ilCron.php';
00150
00151 $cron_obj =& new ilCron($this->db);
00152 if($this->log['enabled'])
00153 {
00154 $cron_obj->initLog($this->log['path'],$this->log['file'],$client_data['name']);
00155 }
00156
00157 if($this->__checkDBVersion())
00158 {
00159 $cron_obj->start();
00160 }
00161 else
00162 {
00163 include_once '../classes/class.ilLog.php';
00164
00165 $log =& new ilLog($this->log['path'],$this->log['file']);
00166 $log->write('Cron: Database not up to date. Aborting');
00167 }
00168 $this->db->disconnect();
00169 }
00170 }
00171 }
00172
00173 function __openDb()
00174 {
00175 $dsn = $this->db_data['type']."://".
00176 $this->db_data['user'].":".
00177 $this->db_data['pass']."@".
00178 $this->db_data['host']."/".
00179 $this->db_data['name'];
00180
00181 $this->db = DB::connect($dsn,true);
00182
00183 if (DB::isError($this->db))
00184 {
00185 return false;
00186 }
00187 return true;
00188 }
00189
00190 function __checkDBVersion()
00191 {
00192 $query = "SELECT value FROM settings ".
00193 "WHERE keyword = 'db_version'";
00194
00195 $res = $this->db->query($query);
00196 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00197 {
00198 $db_version = $row->value;
00199 }
00200
00201 return $db_version == $this->file_version;
00202 }
00203
00204 function __readFileDBVersion()
00205 {
00206 $this->db_version = 99999;
00207
00208
00209 if(!$content = file('../sql/dbupdate.php'))
00210 {
00211 echo 'Cannot open ../sql/dbupdate.php';
00212 return false;
00213 }
00214 foreach($content as $row)
00215 {
00216 if(preg_match('/^<#([0-9]+)>/',$row,$matches))
00217 {
00218 $this->file_version = $matches[1];
00219 }
00220 }
00221 }
00222
00223 function __checkAccess()
00224 {
00225 if($_SERVER['REQUEST_URI'])
00226 {
00227 if(!$this->web_enabled or ($_GET['web_pass'] !== $this->web_pass))
00228 {
00229 if($this->log['enabled'])
00230 {
00231 include_once '../classes/class.ilLog.php';
00232
00233 $this->log =& new ilLog($this->log['path'],$this->log['file']);
00234
00235 $this->log->write('Cron: __checkAccess() failed');
00236
00237 exit;
00238 }
00239 }
00240 }
00241 return true;
00242 }
00243
00244
00245
00246 function __ilCronClients()
00247 {
00248 fclose($this->fp);
00249 }
00250 }
00251