ILIAS  Release_4_0_x_branch Revision 61816
 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-2009 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 
32 include_once 'PEAR.php';
33 include_once 'MDB2.php';
34 
35 class ilCronClients extends PEAR
36 {
37  var $fp;
38 
39  // PRIVATE CONTRUCTOR
40  function ilCronClients()
41  {
42  define('INI_FILE_PATH','../ilias.ini.php');
43  define('CRON_DEBUG',1);
44 
45  $this->__createLock();
46  $this->__readClients();
47  register_shutdown_function(array($this,'__ilCronClients'));
48  }
49 
50 
51  function &_getInstance()
52  {
54  {
55  die('Instance already created');
56  }
57  return new ilCronClients();
58  }
59 
60  function __createLock()
61  {
62  $this->fp = @fopen('cron.lock','wb');
63  fwrite($this->fp,(string) time(),strlen((string) time()));
64 
65  return true;
66  }
67 
68  function _lockExists()
69  {
70  if(@file_exists('cron.lock'))
71  {
72  $fp = fopen('cron.lock','r');
73 
74  (int) $timest = fread($fp,filesize('cron.lock'));
75 
76  if(!CRON_DEBUG and ($timest > time() - 60 * 60 * 12))
77  {
78  return true;
79  }
80  unlink('cron.lock');
81  }
82  return false;
83  }
84 
85  function __readClients()
86  {
87  include_once './classes/class.ilIniFile.php';
88 
89  $ini_file_obj =& new ilIniFile(INI_FILE_PATH);
90 
91  $ini_file_obj->read();
92 
93  $this->log['enabled'] = $ini_file_obj->readVariable('log','enabled');
94  $this->log['path'] = $ini_file_obj->readVariable('log','path');
95  $this->log['file'] = $ini_file_obj->readVariable('log','file');
96 
97  $this->web_enabled = $ini_file_obj->readVariable('cron','web_enabled');
98  $this->web_pass = $ini_file_obj->readVariable('cron','web_pass');
99  $this->__checkAccess();
100 
101 
102  $this->client_data = $ini_file_obj->readGroup('clients');
103  unset($ini_file_obj);
104 
105  // open client.ini.php
106 
107  // set path to directory where clients reside
108 
109  $this->client_ini = array();
110  $dp = opendir('../'.$this->client_data['path']);
111  while(($file = readdir($dp)) !== false)
112  {
113  if($file == '.' or $file == '..' or $file == 'CVS')
114  {
115  continue;
116  }
117  if(@file_exists('../'.$this->client_data['path'].'/'.$file.'/'.$this->client_data['inifile']))
118  {
119  $tmp_data['path'] = '../'.$this->client_data['path'].'/'.$file.'/'.$this->client_data['inifile'];
120  $tmp_data['name'] = $file;
121 
122  $this->client_ini[] = $tmp_data;
123  unset($tmp_data);
124  }
125  }
126 
127  $this->__startChecks();
128  }
129 
130  function __startChecks()
131  {
132  foreach($this->client_ini as $client_data)
133  {
134  include_once './classes/class.ilIniFile.php';
135 
136  $ini_file_obj =& new ilIniFile($client_data['path']);
137 
138  $ini_file_obj->read();
139  $this->db_data = $ini_file_obj->readGroup('db');
140 
141  $this->__readFileDBVersion();
142 
143  if($this->__openDb())
144  {
145  include_once './setup/classes/class.ilCron.php';
146 
147  $cron_obj =& new ilCron($this->db);
148  if($this->log['enabled'])
149  {
150  $cron_obj->initLog($this->log['path'],$this->log['file'],$client_data['name']);
151  }
152 
153  if($this->__checkDBVersion())
154  {
155  $cron_obj->start();
156  }
157  else
158  {
159  include_once './Services/Logging/classes/class.ilLog.php';
160 
161  $log =& new ilLog($this->log['path'],$this->log['file']);
162  $log->write('Cron: Database not up to date. Aborting');
163  }
164  $this->db->disconnect();
165  }
166  }
167  }
168 
169  function __openDb()
170  {
171  $dsn = $this->db_data['type']."://".
172  $this->db_data['user'].":".
173  $this->db_data['pass']."@".
174  $this->db_data['host']."/".
175  $this->db_data['name'];
176 
177  $this->db = MDB2::connect($dsn,true);
178 
179  if (MDB2::isError($this->db))
180  {
181  return false;
182  }
183  return true;
184  }
185 
186  function __checkDBVersion()
187  {
188  $GLOBALS["ilDB"] = $this->db;
189  include_once './Services/Administration/classes/class.ilSetting.php';
190  $setting = new ilSetting();
191 
192  /*$query = "SELECT value FROM sett ings ".
193  "WHERE keyword = 'db_version'";
194 
195  $res = $this->db->query($query);
196  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
197  {
198  $db_version = $row->value;
199  }*/
200  $db_version = $setting->get("db_version");
201 
202  return $db_version == $this->file_version;
203  }
204 
206  {
207  $this->db_version = 99999;
208 
209  // GET FILE VERSION
210  if(!$content = file('./setup/sql/dbupdate_02.php'))
211  {
212  echo 'Cannot open ./setup/sql/dbupdate_02.php';
213  return false;
214  }
215  foreach($content as $row)
216  {
217  if(preg_match('/^<#([0-9]+)>/',$row,$matches))
218  {
219  $this->file_version = $matches[1];
220  }
221  }
222  }
223 
224  function __checkAccess()
225  {
226  if($_SERVER['REQUEST_URI'])
227  {
228  if(!$this->web_enabled or ($_GET['web_pass'] !== $this->web_pass))
229  {
230  if($this->log['enabled'])
231  {
232  include_once './Services/Logging/classes/class.ilLog.php';
233 
234  $this->log =& new ilLog($this->log['path'],$this->log['file']);
235 
236  $this->log->write('Cron: __checkAccess() failed');
237 
238  exit;
239  }
240  }
241  }
242  return true;
243  }
244 
245 
246 
247  function __ilCronClients()
248  {
249  fclose($this->fp);
250  }
251 }
252