|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* phpBB Media Embed PlugIn extension for the phpBB Forum Software package. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright (c) 2020 phpBB Limited <https://www.phpbb.com> |
|
7
|
|
|
* @license GNU General Public License, version 2 (GPL-2.0) |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace phpbb\mediaembed\cron; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* mediaembed cron task. |
|
15
|
|
|
*/ |
|
16
|
|
|
class mediaembed extends \phpbb\cron\task\base |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var \phpbb\config\config $config Config object */ |
|
19
|
|
|
protected $config; |
|
20
|
|
|
|
|
21
|
|
|
/** @var \phpbb\cache\driver\driver_interface */ |
|
22
|
|
|
protected $cache; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Constructor |
|
26
|
|
|
* |
|
27
|
|
|
* @param \phpbb\config\config $config Config object |
|
28
|
|
|
* @param \phpbb\cache\driver\driver_interface $cache Cache jbject |
|
29
|
|
|
* @access public |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(\phpbb\config\config $config, \phpbb\cache\driver\driver_interface $cache) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->config = $config; |
|
34
|
|
|
$this->cache = $cache; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritDoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function run() |
|
41
|
|
|
{ |
|
42
|
|
|
try |
|
43
|
|
|
{ |
|
44
|
|
|
$this->mediaembed_purge_cache(); |
|
45
|
|
|
} |
|
46
|
|
|
catch (\RuntimeException $e) |
|
47
|
|
|
{ |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritDoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function is_runnable() |
|
56
|
|
|
{ |
|
57
|
|
|
return (bool) $this->config['media_embed_enable_cache']; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritDoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function should_run() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->config['mediaembed_last_gc'] < strtotime('24 hours ago'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Purge all MediaEmbed cache files |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function mediaembed_purge_cache() |
|
72
|
|
|
{ |
|
73
|
|
|
try |
|
74
|
|
|
{ |
|
75
|
|
|
$iterator = new \DirectoryIterator($this->cache->cache_dir); |
|
76
|
|
|
} |
|
77
|
|
|
catch (\Exception $e) |
|
78
|
|
|
{ |
|
79
|
|
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
View Code Duplication |
foreach ($iterator as $fileInfo) |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
if ($fileInfo->isDot() || $fileInfo->isDir()) |
|
85
|
|
|
{ |
|
86
|
|
|
continue; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$filename = $fileInfo->getFilename(); |
|
90
|
|
|
if (strpos($filename, 'http.') === 0) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->cache->remove_file($fileInfo->getPathname()); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$this->config->set('mediaembed_last_gc', time(), false); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.