Passed
Push — master ( bac51f...28a360 )
by Mihail
05:04
created

Main::actionSessions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 8

Duplication

Lines 7
Ratio 36.84 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 0
dl 7
loc 19
rs 9.4285
1
<?php
2
3
namespace Apps\Controller\Admin;
4
5
use Apps\ActiveRecord\Session;
6
use Apps\Model\Admin\Main\EntityDeleteRoute;
7
use Apps\Model\Admin\Main\FormAddRoute;
8
use Apps\Model\Admin\Main\FormSettings;
9
use Apps\Model\Install\Main\EntityCheck;
10
use Extend\Core\Arch\AdminController;
11
use Ffcms\Core\App;
12
use Ffcms\Core\Exception\SyntaxException;
13
use Ffcms\Core\Helper\Environment;
14
use Ffcms\Core\Helper\FileSystem\Directory;
15
use Ffcms\Core\Helper\FileSystem\File;
16
use Ffcms\Core\Helper\Type\Arr;
17
use Ffcms\Core\Helper\Type\Integer;
18
use Ffcms\Core\Helper\Type\Str;
19
20
class Main extends AdminController
21
{
22
    public $type = 'app';
23
24
    public function __construct()
25
    {
26
        parent::__construct(false);
27
    }
28
29
    /**
30
     * Index page of admin dashboard
31
     * @return string
32
     * @throws \Ffcms\Core\Exception\SyntaxException
33
     * @throws \Ffcms\Core\Exception\NativeException
34
     */
35
    public function actionIndex()
36
    {
37
        // cache some data
38
        $rootSize = App::$Cache->get('root.size');
39
        if ($rootSize === null) {
40
            $rootSize = round(Directory::getSize('/') / (1024*1000), 2) . ' mb';
41
            App::$Cache->set('root.size', $rootSize, 86400); // 24 hours caching = 60 * 60 * 24
42
        }
43
        $loadAvg = App::$Cache->get('load.average');
44
        if ($loadAvg === null) {
45
            $loadAvg = Environment::loadAverage();
46
            App::$Cache->set('load.average', $loadAvg, 60*5); // 5 min cache
47
        }
48
49
        // prepare system statistic
50
        $stats = [
51
            'ff_version' => App::$Properties->version['num'] . ' (' . App::$Properties->version['date'] . ')',
52
            'php_version' => Environment::phpVersion() . ' (' . Environment::phpSAPI() . ')',
53
            'os_name' => Environment::osName(),
54
            'database_name' => App::$Database->connection()->getDatabaseName() . ' (' . App::$Database->connection()->getDriverName() . ')',
55
            'file_size' => $rootSize,
56
            'load_avg' => $loadAvg
57
        ];
58
        // check directory chmods and other environment features
59
        $model = new EntityCheck();
60
61
        // render view output
62
        return App::$View->render('index', [
63
            'stats' => $stats,
64
            'check' => $model
65
        ]);
66
    }
67
68
    /**
69
     * Manage settings in web
70
     * @return string
71
     * @throws \Ffcms\Core\Exception\SyntaxException
72
     * @throws \Ffcms\Core\Exception\NativeException
73
     */
74 View Code Duplication
    public function actionSettings()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
75
    {
76
        // init settings model and process post send
77
        $model = new FormSettings();
78
        if ($model->send()) {
79
            if ($model->validate()) {
80
                if ($model->makeSave()) {
81
                    // show message about successful save and take system some time ;)
82
                    return App::$View->render('settings_save');
83
                } else {
84
                    App::$Session->getFlashBag()->add('error', __('Configuration file is not writable! Check /Private/Config/ dir and files'));
85
                }
86
            } else {
87
                App::$Session->getFlashBag()->add('error', __('Validation of form data is failed!'));
88
            }
89
        }
90
91
        // render output view
92
        return App::$View->render('settings', [
93
            'model' => $model->filter()
94
        ]);
95
    }
96
97
    /**
98
     * Manage files via elFinder
99
     * @return string
100
     * @throws \Ffcms\Core\Exception\SyntaxException
101
     * @throws \Ffcms\Core\Exception\NativeException
102
     */
103
    public function actionFiles()
104
    {
105
        return App::$View->render('files', [
106
            'connector' => App::$Alias->scriptUrl . '/api/main/files?lang=' . App::$Request->getLanguage()
107
        ]);
108
    }
109
110
    /**
111
     * Show antivirus view
112
     * @return string
113
     * @throws \Ffcms\Core\Exception\SyntaxException
114
     * @throws \Ffcms\Core\Exception\NativeException
115
     */
116
    public function actionAntivirus()
117
    {
118
        return App::$View->render('antivirus');
119
    }
120
121
    /**
122
     * Set debugging cookie to current user session
123
     */
124
    public function actionDebugcookie()
125
    {
126
        $cookieProperty = App::$Properties->get('debug');
127
        //App::$Request->cookies->add([$cookieProperty['cookie']['key'] => $cookieProperty['cookie']['value']]); todo: fix me
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
128
        setcookie($cookieProperty['cookie']['key'], $cookieProperty['cookie']['value'], Integer::MAX, '/', null, null, true);
129
        App::$Response->redirect('/');
130
    }
131
132
    /**
133
     * List available routes
134
     * @return string
135
     * @throws \Ffcms\Core\Exception\SyntaxException
136
     * @throws \Ffcms\Core\Exception\NativeException
137
     */
138
    public function actionRouting()
139
    {
140
        $routingMap = App::$Properties->getAll('Routing');
141
142
        return App::$View->render('routing', [
143
            'routes' => $routingMap
144
        ]);
145
    }
146
147
    /**
148
     * Show add form for routing
149
     * @return string
150
     * @throws \Ffcms\Core\Exception\SyntaxException
151
     * @throws \Ffcms\Core\Exception\NativeException
152
     */
153
    public function actionAddroute()
154
    {
155
        $model = new FormAddRoute();
156
157
        if (!File::exist('/Private/Config/Routing.php') || !File::writable('/Private/Config/Routing.php')) {
158
            App::$Session->getFlashBag()->add('error', __('Routing configuration file is not allowed to write: /Private/Config/Routing.php'));
159
        } elseif ($model->send() && $model->validate()) {
160
            $model->save();
161
            return App::$View->render('add_route_save');
162
        }
163
164
        return App::$View->render('add_route', [
165
            'model' => $model->filter()
166
        ]);
167
    }
168
169
    /**
170
     * Delete scheme route
171
     * @throws SyntaxException
172
     * @return string
173
     * @throws \Ffcms\Core\Exception\NativeException
174
     */
175
    public function actionDeleteroute()
176
    {
177
        $type = (string)App::$Request->query->get('type');
178
        $loader = (string)App::$Request->query->get('loader');
179
        $source = Str::lowerCase((string)App::$Request->query->get('path'));
180
181
        $model = new EntityDeleteRoute($type, $loader, $source);
182
        if ($model->send()) {
183
            $model->make();
184
            return App::$View->render('delete_route_save');
185
        }
186
187
        return App::$View->render('delete_route', [
188
            'model' => $model->filter()
189
        ]);
190
    }
191
192
    /**
193
     * Clear cached data
194
     * @return string
195
     * @throws \Ffcms\Core\Exception\NativeException
196
     * @throws SyntaxException
197
     */
198
    public function actionCache()
199
    {
200
        $stats = App::$Cache->stats();
201
        // get size in mb from cache stats
202
        $size = round((int)$stats['size'] / (1024*1024), 2);
203
204
        // check if submited
205 View Code Duplication
        if (App::$Request->request->get('clearcache', false)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
206
            // clear cache
207
            App::$Cache->clean();
208
            // add notification & redirect
209
            App::$Session->getFlashBag()->add('success', __('Cache cleared successfully'));
210
            App::$Response->redirect('/');
211
        }
212
213
        // render output view
214
        return App::$View->render('clear_cache', [
215
            'size' => $size
216
        ]);
217
    }
218
219
    /**
220
     * Clear all sessions data
221
     * @return string
222
     * @throws \Ffcms\Core\Exception\NativeException
223
     * @throws SyntaxException
224
     */
225
    public function actionSessions()
226
    {
227
        // get all sessions data
228
        $sessions = Session::all();
229
230
        // check if action is submited
231 View Code Duplication
        if (App::$Request->request->get('clearsessions', false)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
232
            // truncate table
233
            App::$Database->table('sessions')->truncate();
234
            // add notification and make redirect to main
235
            App::$Session->getFlashBag()->add('success', __('Sessions cleared successfully'));
236
            App::$Response->redirect('/');
237
        }
238
239
        // render output view
240
        return App::$View->render('clear_sessions', [
241
            'count' => $sessions->count()
242
        ]);
243
    }
244
}