Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Main::actionFiles()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 21
rs 9.9
c 0
b 0
f 0
cc 3
nc 2
nop 0
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'));
0 ignored issues
show
Bug introduced by
It seems like Ffcms\Core\Helper\FileSy...tivirus/Infected.json') can also be of type false; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
            $data = json_decode(/** @scrutinizer ignore-type */ File::read('/Private/Antivirus/Infected.json'));
Loading history...
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