1: <?php
2: /**
3: * Login system task for automated garbage collection tasks.
4: *
5: * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
6: *
7: * See the enclosed file COPYING for license information (LGPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
9: *
10: * @author Michael Slusarz <slusarz@horde.org>
11: * @category Horde
12: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
13: * @package Horde
14: */
15: class Horde_LoginTasks_SystemTask_GarbageCollection extends Horde_LoginTasks_SystemTask
16: {
17: /**
18: * The interval at which to run the task.
19: *
20: * @var integer
21: */
22: public $interval = Horde_LoginTasks::WEEKLY;
23:
24: /**
25: * Perform all functions for this task.
26: */
27: public function execute()
28: {
29: /* Clean out static cache files. Any user has a 10% chance of
30: * triggering this weekly - no need to have every user trigger
31: * this once weekly since these static files are shared among
32: * all users. */
33: if (rand(0, 9) === 0) {
34: foreach (array('cachecss', 'cachejs') as $val) {
35: if (!empty($GLOBALS['conf'][$val]) &&
36: (strcasecmp($GLOBALS['conf'][$val . 'params']['driver'], 'filesystem') === 0)) {
37: $this->_staticFilesGc($val);
38: }
39: }
40: }
41: }
42:
43: /**
44: * Do cleanup of static files directory.
45: */
46: protected function _staticFilesGc($type)
47: {
48: if (!($lifetime = $GLOBALS['conf'][$type . 'params']['lifetime'])) {
49: continue;
50: }
51:
52: /* Keep a file in the static directory that prevents us from doing
53: * garbage collection more than once a day. */
54: $curr_time = time();
55: $static_dir = $GLOBALS['registry']->get('fileroot', 'horde') . '/static';
56: $static_stat = $static_dir . '/gc_' . $type;
57: $next_run = null;
58:
59: if (file_exists($static_stat)) {
60: $next_run = $static_stat;
61: }
62:
63: if (is_null($next_run) || ($curr_time > $next_run)) {
64: file_put_contents($static_stat, $curr_time + 86400);
65: }
66:
67: if (is_null($next_run) || ($curr_time < $next_run)) {
68: return;
69: }
70:
71: $c_time = $curr_time - $lifetime;
72: foreach (glob($static_dir . '/*.' . substr($type, 5)) as $file) {
73: if ($c_time > filemtime($file)) {
74: @unlink($file);
75: }
76: }
77:
78: Horde::logMessage('Cleaned out static files for ' . $type, 'DEBUG');
79: }
80:
81: }
82: