Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
tidy_plupload | |
0.00% |
0 / 21 |
|
0.00% |
0 / 4 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
run | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
30 | |||
is_runnable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
should_run | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * |
4 | * This file is part of the phpBB Forum Software package. |
5 | * |
6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
7 | * @license GNU General Public License, version 2 (GPL-2.0) |
8 | * |
9 | * For full copyright and license information, please see |
10 | * the docs/CREDITS.txt file. |
11 | * |
12 | */ |
13 | |
14 | namespace phpbb\cron\task\core; |
15 | |
16 | /** |
17 | * Cron task for cleaning plupload's temporary upload directory. |
18 | */ |
19 | class tidy_plupload extends \phpbb\cron\task\base |
20 | { |
21 | /** |
22 | * How old a file must be (in seconds) before it is deleted. |
23 | * @var int |
24 | */ |
25 | protected $max_file_age = 86400; |
26 | |
27 | /** |
28 | * How often we run the cron (in seconds). |
29 | * @var int |
30 | */ |
31 | protected $cron_frequency = 86400; |
32 | |
33 | /** |
34 | * phpBB root path |
35 | * @var string |
36 | */ |
37 | protected $phpbb_root_path; |
38 | |
39 | /** |
40 | * Config object |
41 | * @var \phpbb\config\config |
42 | */ |
43 | protected $config; |
44 | |
45 | /** @var \phpbb\log\log_interface */ |
46 | protected $log; |
47 | |
48 | /** @var \phpbb\user */ |
49 | protected $user; |
50 | |
51 | /** |
52 | * Directory where plupload stores temporary files. |
53 | * @var string |
54 | */ |
55 | protected $plupload_upload_path; |
56 | |
57 | /** |
58 | * Constructor. |
59 | * |
60 | * @param string $phpbb_root_path The root path |
61 | * @param \phpbb\config\config $config The config |
62 | * @param \phpbb\log\log_interface $log Log |
63 | * @param \phpbb\user $user User object |
64 | */ |
65 | public function __construct($phpbb_root_path, \phpbb\config\config $config, \phpbb\log\log_interface $log, \phpbb\user $user) |
66 | { |
67 | $this->phpbb_root_path = $phpbb_root_path; |
68 | $this->config = $config; |
69 | $this->log = $log; |
70 | $this->user = $user; |
71 | |
72 | $this->plupload_upload_path = $this->phpbb_root_path . $this->config['upload_path'] . '/plupload'; |
73 | } |
74 | |
75 | /** |
76 | * {@inheritDoc} |
77 | */ |
78 | public function run() |
79 | { |
80 | // Remove old temporary file (perhaps failed uploads?) |
81 | $last_valid_timestamp = time() - $this->max_file_age; |
82 | try |
83 | { |
84 | $iterator = new \DirectoryIterator($this->plupload_upload_path); |
85 | foreach ($iterator as $file) |
86 | { |
87 | if (strpos($file->getBasename(), $this->config['plupload_salt']) !== 0) |
88 | { |
89 | // Skip over any non-plupload files. |
90 | continue; |
91 | } |
92 | |
93 | if ($file->getMTime() < $last_valid_timestamp) |
94 | { |
95 | @unlink($file->getPathname()); |
96 | } |
97 | } |
98 | } |
99 | catch (\UnexpectedValueException $e) |
100 | { |
101 | $this->log->add('critical', $this->user->data['user_id'], $this->user->ip, 'LOG_PLUPLOAD_TIDY_FAILED', false, array( |
102 | $this->plupload_upload_path, |
103 | $e->getMessage(), |
104 | $e->getTraceAsString() |
105 | )); |
106 | } |
107 | |
108 | $this->config->set('plupload_last_gc', time()); |
109 | } |
110 | |
111 | /** |
112 | * {@inheritDoc} |
113 | */ |
114 | public function is_runnable() |
115 | { |
116 | return !empty($this->config['plupload_salt']) && is_dir($this->plupload_upload_path); |
117 | } |
118 | |
119 | /** |
120 | * {@inheritDoc} |
121 | */ |
122 | public function should_run() |
123 | { |
124 | return $this->config['plupload_last_gc'] < time() - $this->cron_frequency; |
125 | } |
126 | } |