1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ExpressApi\V1\Rpc\ClearCache; |
4
|
|
|
|
5
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
6
|
|
|
use ZF\ApiProblem\ApiProblem; |
7
|
|
|
use ZF\ApiProblem\ApiProblemResponse; |
8
|
|
|
|
9
|
|
|
class ClearCacheController extends AbstractActionController { |
10
|
|
|
|
11
|
|
|
private $dirsWhiteList = array( |
12
|
|
|
'api', |
13
|
|
|
'module' |
14
|
|
|
); |
15
|
|
|
|
16
|
|
|
public function clearCacheAction() { |
17
|
|
|
$params = $this->bodyParams(); |
|
|
|
|
18
|
|
|
$dirs = array_key_exists('dir', $params) ? explode(';', $params['dir']) : array(); |
19
|
|
|
$result = array( |
20
|
|
|
'success' => true |
21
|
|
|
); |
22
|
|
|
if(is_array($dirs) && count($dirs)) { |
23
|
|
|
foreach($dirs as $dir) { |
24
|
|
|
if(in_array($dir, $this->dirsWhiteList)) { |
25
|
|
|
$result['success'] = $this->clearCacheDir($dir) && $result['success']; |
26
|
|
|
} |
27
|
|
|
else { |
28
|
|
|
return new ApiProblemResponse(new ApiProblem(400, "The dir ".$dir." is not allowed.")); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
return $result; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private function getCacheDir() { |
36
|
|
|
return dirname(__FILE__).'/../../../../../../../data/cache/'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function clearCacheDir($dir) { |
40
|
|
|
$cacheDir = $this->getCacheDir().$dir; |
41
|
|
|
if(is_dir($cacheDir)) { |
42
|
|
|
$dirContent = array_diff(scandir($cacheDir), array('.', '..')); |
43
|
|
|
foreach($dirContent as $cacheNode) { |
44
|
|
|
$cacheRealPath = $cacheDir.'/'.$cacheNode; |
45
|
|
|
if($this->canDeleteCacheFolder($cacheDir, $cacheNode)) { |
46
|
|
|
$this->delTree($cacheRealPath); |
47
|
|
|
} |
48
|
|
|
else if($this->canDeleteCacheFile($cacheDir, $cacheNode)) { |
49
|
|
|
unlink($cacheRealPath); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function canDeleteCacheFolder($path, $nodeCache) { |
57
|
|
|
return (is_dir($path.'/'.$nodeCache) && $nodeCache !== '.' && $nodeCache !== '..'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function canDeleteCacheFile($path, $nodeCache) { |
|
|
|
|
61
|
|
|
$ext = pathinfo($nodeCache, PATHINFO_EXTENSION); |
62
|
|
|
return ($ext === 'php'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function delTree($dir) { |
66
|
|
|
$files = array_diff(scandir($dir), array('.', '..')); |
67
|
|
|
foreach($files as $file) { |
68
|
|
|
(is_dir("$dir/$file") && !is_link($dir)) ? self::delTree("$dir/$file") : unlink("$dir/$file"); |
69
|
|
|
} |
70
|
|
|
return rmdir($dir); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.