ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCronClients.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
25 // include pear
26 //require_once("DB.php");
27 
35 include_once 'PEAR.php';
36 include_once 'MDB2.php';
37 
38 class ilCronClients extends PEAR
39 {
40  var $fp;
41 
42  // PRIVATE CONTRUCTOR
43  function ilCronClients()
44  {
45  define('INI_FILE_PATH','../ilias.ini.php');
46  define('CRON_DEBUG',1);
47 
48  $this->__createLock();
49  $this->__readClients();
50  register_shutdown_function(array($this,'__ilCronClients'));
51  }
52 
53 
54  function &_getInstance()
55  {
57  {
58  die('Instance already created');
59  }
60  return new ilCronClients();
61  }
62 
63  function __createLock()
64  {
65  $this->fp = @fopen('cron.lock','wb');
66  fwrite($this->fp,(string) time(),strlen((string) time()));
67 
68  return true;
69  }
70 
71  function _lockExists()
72  {
73  if(@file_exists('cron.lock'))
74  {
75  $fp = fopen('cron.lock','r');
76 
77  (int) $timest = fread($fp,filesize('cron.lock'));
78 
79  if(!CRON_DEBUG and ($timest > time() - 60 * 60 * 12))
80  {
81  return true;
82  }
83  unlink('cron.lock');
84  }
85  return false;
86  }
87 
88  function __readClients()
89  {
90  include_once '../classes/class.ilIniFile.php';
91 
92  $ini_file_obj =& new ilIniFile(INI_FILE_PATH);
93 
94  $ini_file_obj->read();
95 
96  $this->log['enabled'] = $ini_file_obj->readVariable('log','enabled');
97  $this->log['path'] = $ini_file_obj->readVariable('log','path');
98  $this->log['file'] = $ini_file_obj->readVariable('log','file');
99 
100  $this->web_enabled = $ini_file_obj->readVariable('cron','web_enabled');
101  $this->web_pass = $ini_file_obj->readVariable('cron','web_pass');
102  $this->__checkAccess();
103 
104 
105  $this->client_data = $ini_file_obj->readGroup('clients');
106  unset($ini_file_obj);
107 
108  // open client.ini.php
109 
110  // set path to directory where clients reside
111 
112  $this->client_ini = array();
113  $dp = opendir('../'.$this->client_data['path']);
114  while(($file = readdir($dp)) !== false)
115  {
116  if($file == '.' or $file == '..' or $file == 'CVS')
117  {
118  continue;
119  }
120  if(@file_exists('../'.$this->client_data['path'].'/'.$file.'/'.$this->client_data['inifile']))
121  {
122  $tmp_data['path'] = '../'.$this->client_data['path'].'/'.$file.'/'.$this->client_data['inifile'];
123  $tmp_data['name'] = $file;
124 
125  $this->client_ini[] = $tmp_data;
126  unset($tmp_data);
127  }
128  }
129 
130  $this->__startChecks();
131  }
132 
133  function __startChecks()
134  {
135  foreach($this->client_ini as $client_data)
136  {
137  include_once '../classes/class.ilIniFile.php';
138 
139  $ini_file_obj =& new ilIniFile($client_data['path']);
140 
141  $ini_file_obj->read();
142  $this->db_data = $ini_file_obj->readGroup('db');
143 
144  $this->__readFileDBVersion();
145 
146  if($this->__openDb())
147  {
148  include_once './classes/class.ilCron.php';
149 
150  $cron_obj =& new ilCron($this->db);
151  if($this->log['enabled'])
152  {
153  $cron_obj->initLog($this->log['path'],$this->log['file'],$client_data['name']);
154  }
155 
156  if($this->__checkDBVersion())
157  {
158  $cron_obj->start();
159  }
160  else
161  {
162  include_once '../Services/Logging/classes/class.ilLog.php';
163 
164  $log =& new ilLog($this->log['path'],$this->log['file']);
165  $log->write('Cron: Database not up to date. Aborting');
166  }
167  $this->db->disconnect();
168  }
169  }
170  }
171 
172  function __openDb()
173  {
174  $dsn = $this->db_data['type']."://".
175  $this->db_data['user'].":".
176  $this->db_data['pass']."@".
177  $this->db_data['host']."/".
178  $this->db_data['name'];
179 
180  $this->db = MDB2::connect($dsn,true);
181 
182  if (MDB2::isError($this->db))
183  {
184  return false;
185  }
186  return true;
187  }
188 
189  function __checkDBVersion()
190  {
191  $query = "SELECT value FROM settings ".
192  "WHERE keyword = 'db_version'";
193 
194  $res = $this->db->query($query);
195  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
196  {
197  $db_version = $row->value;
198  }
199 
200  return $db_version == $this->file_version;
201  }
202 
204  {
205  $this->db_version = 99999;
206 
207  // GET FILE VERSION
208  if(!$content = file('../setup/sql/dbupdate_02.php'))
209  {
210  echo 'Cannot open ../setup/sql/dbupdate_02.php';
211  return false;
212  }
213  foreach($content as $row)
214  {
215  if(preg_match('/^<#([0-9]+)>/',$row,$matches))
216  {
217  $this->file_version = $matches[1];
218  }
219  }
220  }
221 
222  function __checkAccess()
223  {
224  if($_SERVER['REQUEST_URI'])
225  {
226  if(!$this->web_enabled or ($_GET['web_pass'] !== $this->web_pass))
227  {
228  if($this->log['enabled'])
229  {
230  include_once '../Services/Logging/classes/class.ilLog.php';
231 
232  $this->log =& new ilLog($this->log['path'],$this->log['file']);
233 
234  $this->log->write('Cron: __checkAccess() failed');
235 
236  exit;
237  }
238  }
239  }
240  return true;
241  }
242 
243 
244 
245  function __ilCronClients()
246  {
247  fclose($this->fp);
248  }
249 }
250