PageRenderTime 216ms CodeModel.GetById 90ms app.highlight 51ms RepoModel.GetById 70ms app.codeStats 0ms

/source/innomatic/core/classes/shared/maintenance/InnomaticLogsMaintenance.php

https://bitbucket.org/innoteam/innomatic
PHP | 457 lines | 375 code | 58 blank | 24 comment | 39 complexity | 9543dc27f8acb887e8eb12e31d8f2276 MD5 | raw file
  1<?php
  2/**
  3 * Innomatic
  4 *
  5 * LICENSE 
  6 * 
  7 * This source file is subject to the new BSD license that is bundled 
  8 * with this package in the file LICENSE.
  9 *
 10 * @copyright  1999-2012 Innoteam S.r.l.
 11 * @license    http://www.innomatic.org/license/   BSD License
 12 * @link       http://www.innomatic.org
 13 * @since      Class available since Release 5.0
 14*/
 15
 16require_once('innomatic/maintenance/MaintenanceTask.php');
 17
 18class InnomaticLogsMaintenance extends MaintenanceTask 
 19{
 20    public $mApplicationSettings;
 21
 22    public $mCleanRootLog;
 23    public $mCleanRootDbLog;
 24    public $mCleanAccessLog;
 25    public $mCleanWebServicesLog;
 26    public $mCleanPhpLog;
 27    public $mCleanDomainsLogs;
 28    public $mRotateRootLog;
 29    public $mRotateRootDbLog;
 30    public $mRotateAccessLog;
 31    public $mRotateWebServicesLog;
 32    public $mRotatePhpLog;
 33    public $mRotateDomainsLogs;
 34
 35    public function __construct()
 36    {
 37        require_once('innomatic/application/ApplicationSettings.php');
 38
 39        $this->mApplicationSettings = new ApplicationSettings(
 40            InnomaticContainer::instance('innomaticcontainer')->getDataAccess(),
 41            'innomatic'
 42           );
 43
 44        $this->mCleanRootLog = $this->mApplicationSettings->getKey('maintenance_cleanrootlog') == '1' ? true : false;
 45        $this->mCleanRootDbLog = $this->mApplicationSettings->getKey('maintenance_cleanrootdalog') == '1' ? true : false;
 46        $this->mCleanAccessLog = $this->mApplicationSettings->getKey('maintenance_cleanaccesslog') == '1' ? true : false;
 47        $this->mCleanWebServicesLog = $this->mApplicationSettings->getKey('maintenance_cleanwebserviceslog') == '1' ? true : false;
 48        $this->mCleanPhpLog = $this->mApplicationSettings->getKey('maintenance_cleanphplog') == '1' ? true : false;
 49        $this->mCleanDomainsLogs = $this->mApplicationSettings->getKey('maintenance_cleandomainslogs') == '1' ? true : false;
 50
 51        $this->mRotateRootLog = $this->mApplicationSettings->getKey('maintenance_rotaterootlog') == '1' ? true : false;
 52        $this->mRotateRootDbLog = $this->mApplicationSettings->getKey('maintenance_rotaterootdalog') == '1' ? true : false;
 53        $this->mRotateAccessLog = $this->mApplicationSettings->getKey('maintenance_rotateaccesslog') == '1' ? true : false;
 54        $this->mRotateWebServicesLog = $this->mApplicationSettings->getKey('maintenance_rotatewebserviceslog') == '1' ? true : false;
 55        $this->mRotatePhpLog = $this->mApplicationSettings->getKey('maintenance_rotatephplog') == '1' ? true : false;
 56        $this->mRotateDomainsLogs = $this->mApplicationSettings->getKey('maintenance_rotatedomainslogs') == '1' ? true : false;
 57    }
 58
 59
 60
 61    public function setCleanRootLog(
 62        $clean
 63    )
 64    {
 65        $this->mApplicationSettings->setKey(
 66            'maintenance_cleanrootlog',
 67            $clean ? '1' : '0'
 68           );
 69        $this->mCleanRootLog = $clean ? true : false;
 70    }
 71
 72    public function getCleanRootLog()
 73    {
 74        return $this->mCleanRootLog;
 75    }
 76
 77    public function setRotateRootLog(
 78        $clean
 79    )
 80    {
 81        $this->mApplicationSettings->setKey(
 82            'maintenance_rotaterootlog',
 83            $clean ? '1' : '0'
 84           );
 85        $this->mRotateRootLog = $clean ? true : false;
 86    }
 87
 88    public function getRotateRootLog()
 89    {
 90        return $this->mRotateRootLog;
 91    }
 92
 93    public function setCleanRootDbLog(
 94        $clean
 95    )
 96    {
 97        $this->mApplicationSettings->setKey(
 98            'maintenance_cleanrootdalog',
 99            $clean ? '1' : '0'
100           );
101        $this->mCleanRootDbLog = $clean ? true : false;
102    }
103
104    public function getCleanRootDbLog()
105    {
106        return $this->mCleanRootDbLog;
107    }
108
109    public function setRotateRootDbLog(
110        $clean
111    )
112    {
113        $this->mApplicationSettings->setKey(
114            'maintenance_rotaterootdalog',
115            $clean ? '1' : '0'
116           );
117        $this->mRotateRootDbLog = $clean ? true : false;
118    }
119
120    public function getRotateRootDbLog()
121    {
122        return $this->mRotateRootDbLog;
123    }
124
125
126
127    public function setCleanAccessLog(
128        $clean
129    )
130    {
131        $this->mApplicationSettings->setKey(
132            'maintenance_cleanaccesslog',
133            $clean ? '1' : '0'
134           );
135        $this->mCleanAccessLog = $clean ? true : false;
136    }
137
138    public function getCleanAccessLog()
139    {
140        return $this->mCleanAccessLog;
141    }
142
143    public function setRotateAccessLog(
144        $clean
145    )
146    {
147        $this->mApplicationSettings->setKey(
148            'maintenance_rotateaccesslog',
149            $clean ? '1' : '0'
150           );
151        $this->mRotateAccessLog = $clean ? true : false;
152    }
153
154    public function getRotateAccessLog()
155    {
156        return $this->mRotateAccessLog;
157    }
158
159    public function setCleanWebServicesLog(
160        $clean
161    )
162    {
163        $this->mApplicationSettings->setKey(
164            'maintenance_cleanwebserviceslog',
165            $clean ? '1' : '0'
166           );
167        $this->mCleanWebServicesLog = $clean ? true : false;
168    }
169
170    public function getCleanWebServicesLog()
171    {
172        return $this->mCleanWebServicesLog;
173    }
174
175    public function setRotateWebServicesLog(
176        $clean
177    )
178    {
179        $this->mApplicationSettings->setKey(
180            'maintenance_rotatewebserviceslog',
181            $clean ? '1' : '0'
182           );
183        $this->mRotateWebServicesLog = $clean ? true : false;
184    }
185
186    public function getRotateWebServicesLog()
187    {
188        return $this->mRotateWebServicesLog;
189    }
190
191    public function setCleanPhpLog(
192        $clean
193    )
194    {
195        $this->mApplicationSettings->setKey(
196            'maintenance_cleanphplog',
197            $clean ? '1' : '0'
198        );
199        $this->mCleanPhpLog = $clean ? true : false;
200    }
201
202    public function getCleanPhpLog()
203    {
204        return $this->mCleanPhpLog;
205    }
206
207    public function setRotatePhpLog(
208        $clean
209    )
210    {
211        $this->mApplicationSettings->setKey(
212            'maintenance_rotatephplog',
213            $clean ? '1' : '0'
214           );
215        $this->mRotatePhpLog = $clean ? true : false;
216    }
217
218    public function getRotatePhpLog()
219    {
220        return $this->mRotatePhpLog;
221    }
222
223    public function setCleanDomainsLogs(
224        $clean
225    )
226    {
227        $this->mApplicationSettings->setKey(
228            'maintenance_cleandomainslogs',
229            $clean ? '1' : '0'
230           );
231        $this->mCleanDomainsLogs = $clean ? true : false;
232    }
233
234    public function getCleanDomainsLogs()
235    {
236        return $this->mCleanDomainsLogs;
237    }
238
239    public function setRotateDomainsLogs(
240        $clean
241    )
242    {
243        $this->mApplicationSettings->setKey(
244            'maintenance_rotatedomainslogs',
245            $clean ? '1' : '0'
246           );
247        $this->mRotateDomainsLogs = $clean ? true : false;
248    }
249
250    public function getRotateDomainsLogs()
251    {
252        return $this->mRotateDomainsLogs;
253    }
254
255    // ----- Logs -----
256
257    public function getSystemLogsSize()
258    {
259        $total = 0;
260        require_once('innomatic/util/Registry.php');
261        $reg = Registry::instance();
262        if (file_exists(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/innomatic.log')) $total += filesize(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/innomatic.log');
263        if (file_exists(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/innomatic_root_db.log')) $total += filesize(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/innomatic_root_db.log');
264        if (file_exists(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/access.log')) $total += filesize(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/access.log');
265        if (file_exists(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/webservices.log')) $total += filesize(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/webservices.log');
266        if (file_exists(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/php.log')) $total += filesize(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/php.log');
267
268        return $total;
269    }
270
271    public function getDomainsLogsSize()
272    {
273        $total = 0;
274        $domains_query = InnomaticContainer::instance('innomaticcontainer')->getDataAccess()->execute(
275            'SELECT domainid '.
276            'FROM domains'
277           );
278
279        while (!$domains_query->eof)
280        {
281            $log_file = InnomaticContainer::instance('innomaticcontainer')->getHome().'core/domains/'.$domains_query->getFields('domainid').'/log/dataaccess.log';
282            if (file_exists($log_file)) $total += filesize($log_file);
283            if (file_exists(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/domains/'.$domains_query->getFields('domainid').'/log/domain.log'))
284                $total += filesize(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/domains/'.$domains_query->getFields('domainid').'/log/domain.log');
285
286            $domains_query->moveNext();
287        }
288
289        return $total;
290    }
291
292    public function cleanSystemLogs()
293    {
294        require_once('innomatic/util/Registry.php');
295        $reg = Registry::instance();
296        
297        if (
298            $this->mCleanRootLog
299            and file_exists(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/innomatic.log')
300           )
301        {
302            $log = new Logger(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/innomatic.log');
303            $log->Rotate(0);
304            unlink(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/innomatic.log');
305        }
306        if (
307            $this->mCleanRootDbLog
308            and file_exists(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/innomatic_root_db.log')
309           )
310        {
311            $log = new Logger(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/innomatic_root_db.log');
312            $log->Rotate(0);
313            unlink(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/innomatic_root_db.log');
314        }
315        if (
316            $this->mCleanPhpLog
317            and file_exists(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/php.log')
318           )
319        {
320            $log = new Logger(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/php.log');
321            $log->Rotate(0);
322            unlink(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/php.log');
323        }
324        if (
325            $this->mCleanWebServicesLog
326            and file_exists(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/webservices.log')
327           )
328        {
329            $log = new Logger(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/webservices.log');
330            $log->Rotate(0);
331            unlink(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/webservices.log');
332        }
333        if (
334            $this->mCleanAccessLog
335            and file_exists(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/access.log')
336           )
337        {
338            $log = new Logger(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/access.log');
339            $log->Rotate(0);
340            unlink(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/access.log');
341        }
342
343        if (
344            $this->mRotateRootLog
345           )
346        {
347            $log = new Logger(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/innomatic.log');
348            $log->Rotate(7);
349        }
350        if (
351            $this->mRotateRootDbLog
352           )
353        {
354            $log = new Logger(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/innomatic_root_db.log');
355            $log->Rotate(7);
356        }
357        if (
358            $this->mRotatePhpLog
359           )
360        {
361            $log = new Logger(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/php.log');
362            $log->Rotate(7);
363        }
364        if (
365            $this->mRotateWebServicesLog
366           )
367        {
368            $log = new Logger(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/webservices.log');
369            $log->Rotate(7);
370        }
371        if (
372            $this->mRotateAccessLog
373           )
374        {
375            $log = new Logger(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/log/access.log');
376            $log->Rotate(7);
377        }
378
379        return true;
380    }
381
382    public function CleanDomainsLogs()
383    {
384        $domains_query = InnomaticContainer::instance('innomaticcontainer')->getDataAccess()->execute(
385            'SELECT domainid '.
386            'FROM domains'
387           );
388
389        while (!$domains_query->eof)
390        {
391            $da_log_file = InnomaticContainer::instance('innomaticcontainer')->getHome().'core/domains/'.$domains_query->getFields('domainid').'/log/dataaccess.log';
392            if (file_exists($da_log_file))
393            {
394                if ($this->mCleanDomainsLogs)
395                {
396                    $log = new Logger($da_log_file);
397                    $log->Rotate(0);
398                    unlink($da_log_file);
399                }
400                else if ($this->mRotateDomainsLogs)
401                {
402                    $log = new Logger($da_log_file);
403                    $log->Rotate(7);
404                }
405            }
406
407            if (file_exists(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/domains/'.$domains_query->getFields('domainid').'/log/domain.log'))
408            {
409                if ($this->mCleanDomainsLogs)
410                {
411                    $log = new Logger(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/domains/'.$domains_query->getFields('domainid').'/log/domain.log');
412                    $log->Rotate(0);
413                    unlink(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/domains/'.$domains_query->getFields('domainid').'/log/domain.log');
414                }
415                else if ($this->mRotateDomainsLogs)
416                {
417                    $log = new Logger(InnomaticContainer::instance('innomaticcontainer')->getHome().'core/domains/'.$domains_query->getFields('domainid').'/log/domain.log');
418                    $log->Rotate(7);
419                }
420            }
421
422            $domains_query->moveNext();
423        }
424
425        return true;
426    }
427
428    /*
429    function RotateSystemLogs()
430    {
431    }
432
433    function RotateDomainsLogs()
434    {
435    }
436    */
437
438    // ----- Facilities -----
439
440    public function execute()
441    {
442        $this->CleanSystemLogs();
443        $this->CleanDomainsLogs();
444
445        return true;
446    }
447
448    public function getCleanableDiskSize()
449    {
450        $total = 0;
451
452        $total += $this->getSystemLogsSize();
453        $total += $this->getDomainsLogsSize();
454
455        return $total;
456    }
457}