]> git.mxchange.org Git - friendica.git/blob - mod/msearch.php
Some more "exit" replaced
[friendica.git] / mod / msearch.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Core\System;
24 use Friendica\Database\DBA;
25 use Friendica\DI;
26 use Friendica\Model\User;
27 use Friendica\Util\Proxy;
28
29 function msearch_post(App $a)
30 {
31         $search = $_POST['s'] ?? '';
32         $perpage  = intval(($_POST['n'] ?? 0) ?: 80);
33         $page     = intval(($_POST['p'] ?? 0) ?: 1);
34         $startrec = ($page - 1) * $perpage;
35
36         $total = 0;
37         $results = [];
38
39         if (!strlen($search)) {
40                 $output = ['total' => 0, 'items_page' => $perpage, 'page' => $page, 'results' => $results];
41                 System::jsonExit($output);
42         }
43
44         $total = 0;
45
46         $condition = ["`net-publish` AND MATCH(`pub_keywords`) AGAINST (?)", $search];
47         $total = DBA::count('owner-view', $condition);
48
49         $search_stmt = DBA::select('owner-view', ['pub_keywords', 'name', 'nickname', 'uid'], $condition, ['limit' => [$startrec, $perpage]]);
50         while ($search_result = DBA::fetch($search_stmt)) {
51                 $results[] = [
52                         'name'  => $search_result['name'],
53                         'url'   => DI::baseUrl() . '/profile/' . $search_result['nickname'],
54                         'photo' => User::getAvatarUrl($search_result, Proxy::SIZE_THUMB),
55                         'tags'  => str_replace([',', '  '], [' ', ' '], $search_result['pub_keywords'])
56                 ];
57         }
58
59         DBA::close($search_stmt);
60
61         $output = ['total' => $total, 'items_page' => $perpage, 'page' => $page, 'results' => $results];
62
63         System::jsonExit($output);
64 }