1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Api; |
4
|
|
|
|
5
|
|
|
use Apps\Model\Basic\Antivirus; |
6
|
|
|
use elFinder; |
7
|
|
|
use elFinderConnector; |
8
|
|
|
use Extend\Core\Arch\ApiController; |
9
|
|
|
use Ffcms\Core\App; |
10
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
11
|
|
|
use Ffcms\Core\Helper\FileSystem\File; |
12
|
|
|
use Ffcms\Core\Helper\Type\Str; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Main. Basic api features for ffcms |
16
|
|
|
* @package Apps\Controller\Api |
17
|
|
|
*/ |
18
|
|
|
class Main extends ApiController |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Test action |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
public function actionIndex(): ?string |
25
|
|
|
{ |
26
|
|
|
$this->setJsonHeader(); |
27
|
|
|
return json_encode(['status' => 1, 'value' => 'Welcome, man!']); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Elfinder injector file listing |
32
|
|
|
* @throws ForbiddenException |
33
|
|
|
*/ |
34
|
|
|
public function actionFiles() |
35
|
|
|
{ |
36
|
|
|
$user = App::$User->identity(); |
37
|
|
|
|
38
|
|
|
if (!$user || !$user->role->can('admin/main/files')) { |
39
|
|
|
throw new ForbiddenException('This action is not allowed!'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->setJsonHeader(); |
43
|
|
|
$connector = new elFinderConnector(new elFinder([ |
44
|
|
|
'locale' => '', |
45
|
|
|
'roots' => [ |
46
|
|
|
[ |
47
|
|
|
'driver' => 'LocalFileSystem', |
48
|
|
|
'path' => root . '/upload/', |
49
|
|
|
'URL' => App::$Alias->scriptUrl . '/upload/' |
50
|
|
|
] |
51
|
|
|
] |
52
|
|
|
])); |
53
|
|
|
|
54
|
|
|
$connector->run(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Make scan and display scan iteration data |
59
|
|
|
* @return string|null |
60
|
|
|
* @throws ForbiddenException |
61
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
62
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
63
|
|
|
*/ |
64
|
|
|
public function actionAntivirus(): ?string |
65
|
|
|
{ |
66
|
|
|
$user = App::$User->identity(); |
67
|
|
|
if (!$user || !$user->role->can('admin/main/antivirus')) { |
68
|
|
|
throw new ForbiddenException('This action is not allowed!'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$scanner = new Antivirus(); |
72
|
|
|
|
73
|
|
|
$this->setJsonHeader(); |
74
|
|
|
return json_encode($scanner->make()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Remove previous scan files |
79
|
|
|
* @return string |
80
|
|
|
* @throws ForbiddenException |
81
|
|
|
*/ |
82
|
|
|
public function actionAntivirusclear(): string |
83
|
|
|
{ |
84
|
|
|
$user = App::$User->identity(); |
85
|
|
|
if (!$user || !$user->role->can('admin/main/antivirus')) { |
86
|
|
|
throw new ForbiddenException('This action is not allowed!'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
File::remove('/Private/Antivirus/Infected.json'); |
90
|
|
|
File::remove('/Private/Antivirus/ScanFiles.json'); |
91
|
|
|
|
92
|
|
|
$this->setJsonHeader(); |
93
|
|
|
return json_encode(['status' => 1]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Show scan results |
98
|
|
|
* @return string |
99
|
|
|
* @throws ForbiddenException |
100
|
|
|
*/ |
101
|
|
|
public function actionAntivirusresults(): string |
102
|
|
|
{ |
103
|
|
|
$user = App::$User->identity(); |
104
|
|
|
if (!$user || !$user->role->can('admin/main/antivirus')) { |
105
|
|
|
throw new ForbiddenException('This action is not allowed!'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$response = null; |
109
|
|
|
if (!File::exist('/Private/Antivirus/Infected.json')) { |
110
|
|
|
$response = ['status' => 0]; |
111
|
|
|
} else { |
112
|
|
|
$data = json_decode(File::read('/Private/Antivirus/Infected.json')); |
|
|
|
|
113
|
|
|
$compile = []; |
114
|
|
|
foreach ($data as $file => $sign) { |
115
|
|
|
$file = Str::replace('\\', '/', Str::sub($file, strlen(root))); |
116
|
|
|
$compile[$file][] = $sign; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$response = ['status' => 1, 'data' => $compile]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->setJsonHeader(); |
123
|
|
|
return json_encode($response); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Download news from ffcms.org server and show it with caching & saving |
128
|
|
|
* @return string|null |
129
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
130
|
|
|
*/ |
131
|
|
|
public function actionNews(): ?string |
132
|
|
|
{ |
133
|
|
|
$this->setJsonHeader(); |
134
|
|
|
// get ffcms news if cache is not available |
135
|
|
|
$cache = App::$Cache->getItem('download.ffcms.api.news.' . $this->lang); |
136
|
|
|
if (!$cache->isHit()) { |
137
|
|
|
$cache->set(File::getFromUrl('https://ffcms.org/api/api/news?lang=' . $this->lang)) |
138
|
|
|
->expiresAfter(1440); |
139
|
|
|
} |
140
|
|
|
return $cache->get(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|