Passed
Push — master ( d5460a...3d5e2f )
by Florent
02:36
created

AdminController::adminSocial()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
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 logout function
157
     *
158
     * This function clear ther session, destroy it, and redirect to login page.
159
     *
160
     * @param object $request
161
     * @param object $response
162
     * @param array $args
163
     *
164
     * @return object $response
165
     */
166
    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

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