Passed
Push — master ( 3d5e2f...33f4ab )
by Florent
03:26
created

AdminController::adminIndex()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 2
nop 2
dl 0
loc 25
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Admin controller
5
 *
6
 * The file contains all the functions used in all administration panel.
7
 * For admin posts function, please go to the Posts Controller
8
 * For admin pages function, please go to the Pages Controller
9
 *
10
 * @category   Controllers
11
 * @package    SuperHive
12
 * @author     Florent Kosmala <[email protected]>
13
 * @license    https://www.gnu.org/licenses/gpl-3.0.txt GPL-3.0
14
 */
15
16
namespace App\Controllers;
17
18
use DI\Container;
19
use Psr\Http\Message\ResponseInterface as Response;
20
use Psr\Http\Message\ServerRequestInterface as Request;
21
use Psr\Container\ContainerInterface;
22
use Slim\Factory\AppFactory;
23
use Slim\Routing\RouteContext;
24
use Hive\PhpLib\Hive\Condenser as HiveCondenser;
25
use App\Controllers\CommonController as Common;
26
27
final class AdminController
28
{
29
    private $app;
30
31
    /**
32
     * Admin part contructor
33
     *
34
     * This constructor is not the same as other controllers.
35
     * Administration need  to control if session exists with good account & encrypted key.
36
     *
37
     * @param object $app
38
     */
39
    public function __construct(ContainerInterface $app)
40
    {
41
        $this->app = $app;
42
        $genPosts = new Common($this->app);
43
        $genPosts->genPostsFile();
44
        
45
        /*
46
         *  Check security in session for admin functions
47
         */
48
        $settings = $this->app->get('settings');
49
        $session = $this->app->get('session');
50
        $cred = unserialize(file_get_contents($this->app->get('password')));
51
        $author = $settings['author'];
52
        $passwd = $cred[$author];
53
        
54
        /* If sessons keys are not set */
55
        if ((!isset($session['sh_author'])) || (!isset($session['sh_sign']))) {
56
            header("Location: /login");
57
            die();
58
        } else {
59
            /* If session keys are not good */
60
            if (($settings['author'] != $session['sh_author']) || ($passwd != $session['sh_sign'])) {
61
                header("Location: /login");
62
                die();
63
            }
64
        }
65
    }
66
67
    /**
68
     * Admin index function
69
     *
70
     * This function display the admin index with some settings ready to be changed.
71
     * It call the admin save() functionwhen the button is clicked.
72
     *
73
     * @param object $request
74
     * @param object $response
75
     *
76
     * @return object $response
77
     */
78
    public function adminIndex(Request $request, Response $response): Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

78
    public function adminIndex(/** @scrutinizer ignore-unused */ Request $request, Response $response): Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80
        // Create array from config file
81
        $settings = $this->app->get('settings');
82
        $accountFile = $this->app->get('accountfile');
83
84
        $apiConfig = [
85
            "hiveNode" => $settings['api'],
86
            "debug" => false
87
        ];
88
        $api = new HiveCondenser($apiConfig);
89
90
        $cache_interval = 300;
91
92
        $current_time = time();
93
        if ((!file_exists($accountFile)) || ($current_time - filemtime($accountFile) > $cache_interval)) {
94
            $result = json_encode($api->getAccounts($settings['author']), JSON_PRETTY_PRINT);
95
            file_put_contents($accountFile, $result);
96
        }
97
98
        $account = json_decode(file_get_contents($accountFile), true);
99
100
        return $this->app->get('view')->render($response, '/admin/admin-index.html', [
101
            'settings' => $settings,
102
            'account' => $account[0]
103
        ]);
104
    }
105
    
106
    /**
107
     * Admin settings function
108
     *
109
     * This function display tthe settings page
110
     * This page contains every Superhive settings (not plugins settings)..
111
     *
112
     * @param object $request
113
     * @param object $response
114
     *
115
     * @return object $response
116
     */
117
    public function adminSettings(Request $request, Response $response): Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

117
    public function adminSettings(/** @scrutinizer ignore-unused */ Request $request, Response $response): Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
    {
119
        // Create array from config file
120
        $settings = $this->app->get('settings');
121
        $accountFile = $this->app->get('accountfile');
122
        $langFile = $this->app->get('basedir') . 'resources/languages.json';
123
        $nodesFile = $this->app->get('basedir') . 'resources/nodes.json';
124
125
        $apiConfig = [
126
            'hiveNode' => $settings['api'],
127
            'debug' => false
128
        ];
129
        $api = new HiveCondenser($apiConfig);
130
131
        $cache_interval = 300;
132
133
        $current_time = time();
134
        if ((!file_exists($accountFile)) || ($current_time - filemtime($accountFile) > $cache_interval)) {
135
            $result = json_encode($api->getAccounts($settings['author']), JSON_PRETTY_PRINT);
136
            file_put_contents($accountFile, $result);
137
        }
138
139
        $account = json_decode(file_get_contents($accountFile), true);
140
        $langs = json_decode(file_get_contents($langFile), true);
141
        $nodes = json_decode(file_get_contents($nodesFile), true);
142
143
        $themes = array_map('basename', glob($this->app->get('themesdir') . '*', GLOB_ONLYDIR));
144
        return $this->app->get('view')->render($response, '/admin/admin-settings.html', [
145
            'settings' => $settings,
146
            'account' => $account[0],
147
            'themes' => $themes,
148
            'languages' => $langs,
149
            'nodes' => $nodes
150
        ]);
151
    }
152
    
153
    /**
154
     * Admin theme function
155
     *
156
     * This function is for the Theme page
157
     *
158
     * @param object $request
159
     * @param object $response
160
     *
161
     * @return object $response
162
     */
163
    public function adminThemes(Request $request, Response $response): Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

163
    public function adminThemes(/** @scrutinizer ignore-unused */ Request $request, Response $response): Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
164
    {
165
        // Create array from config file
166
        $settings = $this->app->get('settings');
167
168
        $themes = array_map('basename', glob($this->app->get('themesdir') . '*', GLOB_ONLYDIR));
169
        return $this->app->get('view')->render($response, '/admin/admin-themes.html', [
170
            'settings' => $settings,
171
            'themes' => $themes
172
        ]);
173
    }
174
    
175
    /**
176
     * Admin logout function
177
     *
178
     * This function clear ther session, destroy it, and redirect to login page.
179
     *
180
     * @param object $request
181
     * @param object $response
182
     *
183
     * @return object $response
184
     */
185
    public function logout(Request $request, Response $response): Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

185
    public function logout(/** @scrutinizer ignore-unused */ Request $request, Response $response): Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
186
    {
187
        $session = $this->app->get('session');
188
        
189
        $session->delete('sh_author');
190
        $session->delete('sh_sign');
191
        $session::destroy();
192
        
193
        return $response->withHeader('Location', '/login')->withStatus(302);
194
    }
195
    
196
    /**
197
     * Admin save function
198
     *
199
     * This function Take every fields in the form and convert the into a (human-readable))JSON file.
200
     * the generated file will be save in config folder.
201
     *
202
     * @param object $request
203
     * @param object $response
204
     *
205
     * @return object $response
206
     */
207
    public function save(Request $request, Response $response): Response
208
    {
209
        $data = $request->getParsedBody();
210
        $redirect = $data["redirect"];
211
        $settings = $this->app->get('settings');
212
        $crosspost = (!isset($data["cross"])) ? $settings["crosspost"] : (bool)$data["cross"];
213
        $devMode = (!isset($data["devel"])) ? $settings["devMode"] : (bool)$data["devel"];
214
        $api = (!isset($data["api"])) ? $settings["api"] : $data["api"];
215
        $displayedPosts = (!isset($data["displayedPosts"])) ? $settings["displayedPosts"] : (int)$data["displayedPosts"];
216
        $author = (!isset($data["author"])) ? $settings["author"] : $data["author"];
217
        $title = (!isset($data["title"])) ? $settings["title"] : $data["title"];
218
        $baseline = (!isset($data["baseline"])) ? $settings["baseline"] : $data["baseline"];
219
        $displayType = (!isset($data["displayTypes"])) ? $settings["displayType"]['type'] : $data["displayTypes"];
220
        $displayedTag = (!isset($data["tag"])) ? $settings["displayType"]['tag'] : $data["tag"];
221
        $socialDesc = (!isset($data["socialDesc"])) ? $settings["social"]["description"] : $data["socialDesc"];
222
        $socialImage = (!isset($data["socialImage"])) ? $settings["social"]["image"] : $data["socialImage"];
223
        $twitter = (!isset($data["twitter"])) ? $settings["social"]["twitter"] : $data["twitter"];
224
        $facebook = (!isset($data["facebook"])) ? $settings["social"]["facebook"] : $data["facebook"];
225
        $instagram = (!isset($data["instagram"])) ? $settings["social"]["instagram"] : $data["instagram"];
226
        $linkedin = (!isset($data["linkedin"])) ? $settings["social"]["linkedin"] : $data["linkedin"];
227
        $language = (!isset($data["lang"])) ? $settings["lang"] : $data["lang"];
228
        $newSettings = array(
229
            'author' => $author,
230
            'title' => $title,
231
            'baseline' => $baseline,
232
            'displayType' => array(
233
                'type' => $displayType,
234
                'tag' => $displayedTag,
235
            ),
236
            'social' => array(
237
                'description' => $socialDesc,
238
                'image' => $socialImage,
239
                'twitter' => $twitter,
240
                'facebook' => $facebook,
241
                'instagram' => $instagram,
242
                'linkedin' => $linkedin
243
            ),
244
            'theme' => $settings["theme"],
245
            'lang' => $language,
246
            'crosspost' => $crosspost,
247
            'api' => $api,
248
            'devMode' => $devMode,
249
            'displayedPosts' => (int)$displayedPosts
250
        );
251
        $file = json_encode($newSettings, JSON_PRETTY_PRINT);
252
        // Create array from config file
253
        file_put_contents($this->app->get('configfile'), $file);
254
        unlink($this->app->get('blogfile'));
255
256
        return $response->withHeader('Location', $redirect)->withStatus(302);
257
    }
258
    
259
    /**
260
     * Admin theme save function
261
     *
262
     * This function is for save the theme into the JSON config file
263
     *
264
     * @param object $request
265
     * @param object $response
266
     * @param array $args
267
     *
268
     * @return object $response
269
     */
270
    public function saveTheme(Request $request, Response $response, array $args): Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

270
    public function saveTheme(/** @scrutinizer ignore-unused */ Request $request, Response $response, array $args): Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
271
    {
272
        $settings = $this->app->get('settings');
273
        if (!isset($args['theme'])) {
274
            return $response->withHeader('Location', '/admin/themes')->withStatus(302);
275
        } else {
276
            $settings['theme'] = $args['theme'];
277
            $file = json_encode($settings, JSON_PRETTY_PRINT);
278
            file_put_contents($this->app->get('configfile'), $file);
279
            return $response->withHeader('Location', '/admin/themes')->withStatus(302);
280
        }
281
        
282
        
283
    }
284
}
285