Passed
Branch master (b40cdb)
by Florent
14:05
created

HomeController::search()   B

Complexity

Conditions 8
Paths 2

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 27
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 48
rs 8.4444
1
<?php
2
3
/**
4
 * Home controller
5
 *
6
 * The file contains every necessary functions for Home display.
7
 *
8
 * @category   Controllers
9
 * @package    SuperHive
10
 * @author     Florent Kosmala <[email protected]>
11
 * @license    https://www.gnu.org/licenses/gpl-3.0.txt GPL-3.0
12
 */
13
14
namespace App\Controllers;
15
16
use DI\Container;
17
use Psr\Http\Message\ResponseInterface as Response;
18
use Psr\Http\Message\ServerRequestInterface as Request;
19
use Psr\Container\ContainerInterface;
20
use Slim\Factory\AppFactory;
21
use Hive\PhpLib\Hive\Condenser as HiveCondenser;
22
use League\CommonMark\CommonMarkConverter;
23
use App\Controllers\CommonController as Common;
24
25
final class HomeController
26
{
27
    private $app;
28
29
    public function __construct(ContainerInterface $app)
30
    {
31
        $this->app = $app;
32
        $genPosts = new Common($this->app);
33
        $genPosts->genPostsFile();
34
    }
35
36
    /**
37
     * Index function
38
     *
39
     * This function display the index page with the list of posts.
40
     * It ocntians also the function to generate the blog.json file with all posts informations.
41
     * All the posts must be converted from MarkDown to HTML before display.
42
     *
43
     * @param object $request
44
     * @param object $response
45
     * @param array $args
46
     *
47
     * @return object $response
48
     */
49
    public function index(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

49
    public function index(/** @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...
50
    {
51
        $settings = $this->app->get('settings');
52
53
        // Hive API communication init
54
        $apiConfig = [
55
            "hiveNode" => $settings['api'],
56
            "debug" => false
57
        ];
58
        $api = new HiveCondenser($apiConfig);
0 ignored issues
show
Unused Code introduced by
The assignment to $api is dead and can be removed.
Loading history...
59
        
60
        // The file with the latest posts.
61
        $file = $this->app->get('blogfile');
62
63
        // Get the JSON
64
        $articles = json_decode(file_get_contents($file), true);
65
        
66
        //Get ready to parse the mardown
67
        $converter = new CommonMarkConverter();
68
        $parsedPosts = array();
69
        
70
        foreach ($articles as &$article) {
71
            // Create HTML from Markdown
72
            $article['body'] = $converter->convert($article['body']);
73
            $tags = '';
74
            
75
            //Get featured image
76
            $meta = json_decode($article['json_metadata'], true);
77
            
78
            $tags .= implode(",", $meta['tags']) . ',';
79
            if ((isset($meta['image'])) && (!empty($meta['image']))) {
80
                $featured = $meta['image'][0];
81
            } else {
82
                $featured = '/themes/' . $settings['theme'] . '/no-img.png';
83
            }
84
            $article['featured'] = $featured;
85
            
86
            $parsedPosts[] = $article;
87
        }
88
        
89
        if (!empty($tags)) {
90
            $tags = explode(',', $tags);
91
            $tagsArray = array_count_values($tags);
92
            array_multisort($tagsArray, SORT_DESC);
0 ignored issues
show
Bug introduced by
App\Controllers\SORT_DESC cannot be passed to array_multisort() as the parameter $rest expects a reference. ( Ignorable by Annotation )

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

92
            array_multisort($tagsArray, /** @scrutinizer ignore-type */ SORT_DESC);
Loading history...
93
            $mostUsedTags = array_slice($tagsArray, 0, 15);
94
        } else {
95
            $mostUsedTags = ['no', 'tags', 'found'];
96
        }
97
        
98
        // Return view with articles
99
        return $this->app->get('view')->render($response, $settings['theme'] . '/index.html', [
100
            'articles' => $parsedPosts,
101
            'tags' => $mostUsedTags,
102
            'settings' => $settings
103
        ]);
104
    }
105
    
106
    /**
107
     * Search function
108
     *
109
     * This function was called when search form is not empty and the form was send.
110
     * It displays the posts which contains the selected string.
111
     *
112
     * @param object $request
113
     * @param object $response
114
     * @param array $args
115
     *
116
     * @return object $response
117
     */
118
    public function search(Request $request, Response $response, $args): Response
0 ignored issues
show
Unused Code introduced by
The parameter $args 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

118
    public function search(Request $request, Response $response, /** @scrutinizer ignore-unused */ $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...
119
    {
120
        $data = $request->getParsedBody();
121
        $term = $data['term'];
122
        
123
        $settings = $this->app->get('settings');
124
        
125
        if ($term == '') {
126
            $result = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
127
        } else {
128
            $file = $this->app->get('blogfile');
129
            $articles = json_decode(file_get_contents($file), true);
130
            
131
            $matches = array();
132
            
133
            foreach ($articles as $article) {
134
                // Check in Title
135
                if (preg_match("/\b$term\b/i", $article['title'])) {
136
                    $matches[] = $article['title'];
137
                }
138
                
139
                // Check in Body
140
                if (preg_match("/\b$term\b/i", $article['body'])) {
141
                    $matches[] = $article['title'];
142
                }
143
                
144
                //Check in Tags
145
                $metadata = json_decode($article['json_metadata'], true);
146
                $tags = implode(",", $metadata['tags']);
147
                if (preg_match("/\b$term\b/i", $tags)) {
148
                    $matches[] = $article['title'];
149
                }
150
            }
151
            $result = array_unique($matches);
152
            
153
            $posts = array();
154
            
155
            foreach ($articles as $article) {
156
                if (in_array($article['title'], $result)) {
157
                    $posts[] = $article;
158
                }
159
            }
160
        }
161
162
        return $this->app->get('view')->render($response, $settings['theme'] . '/search.html', [
163
            'term' => $term,
164
            'posts' => $posts,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $posts does not seem to be defined for all execution paths leading up to this point.
Loading history...
165
            'settings' => $settings
166
        ]);
167
    }
168
    
169
    /**
170
     * Login function
171
     *
172
     * This function displays the login form.
173
     * It was called everytime an admin page is called without good session.
174
     *
175
     * @param object $request
176
     * @param object $response
177
     * @param array $args
178
     *
179
     * @return object $response
180
     */
181
    public function login(Request $request, Response $response, $args): Response
0 ignored issues
show
Unused Code introduced by
The parameter $args 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

181
    public function login(Request $request, Response $response, /** @scrutinizer ignore-unused */ $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...
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

181
    public function login(/** @scrutinizer ignore-unused */ Request $request, Response $response, $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...
182
    {
183
        $settings = $this->app->get('settings');
184
        $session = $this->app->get('session');
0 ignored issues
show
Unused Code introduced by
The assignment to $session is dead and can be removed.
Loading history...
185
        
186
        return $this->app->get('view')->render($response, 'login.html', [
187
            'settings' => $settings
188
        ]);
189
    }
190
    
191
    /**
192
     * Login post function
193
     *
194
     * Called when the login form is send.
195
     * just compare the entered login and the encrypted key (generated by HiveKeychain)
196
     * with the credentials in passwod file.
197
     *
198
     * @param object $request
199
     * @param object $response
200
     * @param array $args
201
     *
202
     * @return object $response
203
     */
204
    public function loginPost(Request $request, Response $response, $args): Response
0 ignored issues
show
Unused Code introduced by
The parameter $args 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

204
    public function loginPost(Request $request, Response $response, /** @scrutinizer ignore-unused */ $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...
205
    {
206
        $settings = $this->app->get('settings');
207
        $session = $this->app->get('session');
208
        $data = $request->getParsedBody();
209
        $author = $settings['author'];
210
        
211
        if ($author != $data['username']) {
212
            $session::destroy();
213
            $msg = "Not Ok";
214
        } else {
215
            $cred = unserialize(file_get_contents($this->app->get('password')));
216
            $passwd = $cred[$author];
217
            if ($data['passwd'] == $passwd) {
218
                $session['sh_author'] = $author;
219
                $session['sh_sign'] = $passwd;
220
                $msg = "OK";
221
            } else {
222
                $session::destroy();
223
                $msg = "Not Ok";
224
            }
225
        }
226
        $response->getBody()->write($msg);
227
        return $response;
228
    }
229
230
    /**
231
     * Feed function
232
     *
233
     * Generate the RSS feed of account's posts
234
     *
235
     * @param object $request
236
     * @param object $response
237
     * @param array $args
238
     *
239
     * @return object $response
240
     */
241
    public function feed(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

241
    public function feed(/** @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...
242
    {
243
        $settings = $this->app->get('settings');
244
245
        $file = $this->app->get('blogfile');
246
        $articles = json_decode(file_get_contents($file), true);
247
248
        header('Content-Type: text/xml');
249
        return $this->app->get('view')->render($response, '/feed.xml', [
250
            'articles' => $articles,
251
            'settings' => $settings
252
        ]);
253
    }
254
255
    /**
256
     * Sitemap function
257
     *
258
     * Generate the Sitemap with all posts links.
259
     *
260
     * @param object $request
261
     * @param object $response
262
     * @param array $args
263
     *
264
     * @return object $response
265
     */
266
    public function sitemap(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

266
    public function sitemap(/** @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...
267
    {
268
        $settings = $this->app->get('settings');
269
270
        $file = $this->app->get('blogfile');
271
        $articles = json_decode(file_get_contents($file), true);
272
273
        header('Content-Type: text/xml');
274
        return $this->app->get('view')->render($response, '/sitemap.xml', [
275
            'articles' => $articles,
276
            'settings' => $settings
277
        ]);
278
    }
279
    
280
    /**
281
     * About function
282
     *
283
     * Generate the about page with account information.
284
     *
285
     * @param object $request
286
     * @param object $response
287
     * @param array $args
288
     *
289
     * @return object $response
290
     */
291
    public function about(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

291
    public function about(/** @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...
292
    {
293
        $settings = $this->app->get('settings');
294
        $accountFile = $this->app->get('accountfile');
295
        $account = json_decode(file_get_contents($accountFile), true);
296
        
297
        $accountBio = json_decode($account[0]['posting_json_metadata'], true);
298
        
299
        return $this->app->get('view')->render($response, $settings['theme'] . '/about.html', [
300
            'settings' => $settings,
301
            'account' => $accountBio['profile']
302
        ]);
303
    }
304
}
305