Passed
Push — master ( b690c3...d5460a )
by Florent
12:12
created

AdminController   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 233
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 107
dl 0
loc 233
rs 9.92
c 1
b 0
f 0
wmc 31

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 24 5
A adminSocial() 0 5 1
F save() 0 51 18
A adminSettings() 0 33 3
A adminIndex() 0 25 3
A logout() 0 9 1
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
     * @param array $args
76
     *
77
     * @return object $response
78
     */
79
    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

79
    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...
80
    {
81
        // Create array from config file
82
        $settings = $this->app->get('settings');
83
        $accountFile = $this->app->get('accountfile');
84
85
        $apiConfig = [
86
            "hiveNode" => $settings['api'],
87
            "debug" => false
88
        ];
89
        $api = new HiveCondenser($apiConfig);
90
91
        $cache_interval = 300;
92
93
        $current_time = time();
94
        if ((!file_exists($accountFile)) || ($current_time - filemtime($accountFile) > $cache_interval)) {
95
            $result = json_encode($api->getAccounts($settings['author']), JSON_PRETTY_PRINT);
96
            file_put_contents($accountFile, $result);
97
        }
98
99
        $account = json_decode(file_get_contents($accountFile), true);
100
101
        return $this->app->get('view')->render($response, '/admin/admin-index.html', [
102
            'settings' => $settings,
103
            'account' => $account[0]
104
        ]);
105
    }
106
    
107
    /**
108
     * Admin settings function
109
     *
110
     * This function display tthe settings page
111
     * This page contains every Superhive settings (not plugins settings)..
112
     *
113
     * @param object $request
114
     * @param object $response
115
     * @param array $args
116
     *
117
     * @return object $response
118
     */
119
    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

119
    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...
120
    {
121
        // Create array from config file
122
        $settings = $this->app->get('settings');
123
        $accountFile = $this->app->get('accountfile');
124
        $langFile = $this->app->get('basedir') . 'resources/languages.json';
125
        $nodesFile = $this->app->get('basedir') . 'resources/nodes.json';
126
127
        $apiConfig = [
128
            'hiveNode' => $settings['api'],
129
            'debug' => false
130
        ];
131
        $api = new HiveCondenser($apiConfig);
132
133
        $cache_interval = 300;
134
135
        $current_time = time();
136
        if ((!file_exists($accountFile)) || ($current_time - filemtime($accountFile) > $cache_interval)) {
137
            $result = json_encode($api->getAccounts($settings['author']), JSON_PRETTY_PRINT);
138
            file_put_contents($accountFile, $result);
139
        }
140
141
        $account = json_decode(file_get_contents($accountFile), true);
142
        $langs = json_decode(file_get_contents($langFile), true);
143
        $nodes = json_decode(file_get_contents($nodesFile), true);
144
145
        $themes = array_map('basename', glob($this->app->get('themesdir') . '*', GLOB_ONLYDIR));
146
        return $this->app->get('view')->render($response, '/admin/admin-settings.html', [
147
            'settings' => $settings,
148
            'account' => $account[0],
149
            'themes' => $themes,
150
            'languages' => $langs,
151
            'nodes' => $nodes
152
        ]);
153
    }
154
155
    /**
156
     * Admin social function
157
     *
158
     * This function display tthe social pagewith a form.
159
     * This page contains every social settings which can be modified.
160
     *
161
     * @param object $request
162
     * @param object $response
163
     * @param array $args
164
     *
165
     * @return object $response
166
     */
167
    public function adminSocial(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

167
    public function adminSocial(/** @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...
168
    {
169
        $settings = $this->app->get('settings');
170
        return $this->app->get('view')->render($response, '/admin/admin-social.html', [
171
                'settings' => $settings
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
     * @param array $args
183
     *
184
     * @return object $response
185
     */
186
    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

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