]> git.mxchange.org Git - friendica.git/blob - mod/msearch.php
Issue 10720: Use different path scheme for user avatars
[friendica.git] / mod / msearch.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Database\DBA;
24 use Friendica\DI;
25 use Friendica\Model\User;
26 use Friendica\Util\Proxy;
27
28 function msearch_post(App $a)
29 {
30         $search = $_POST['s'] ?? '';
31         $perpage  = intval(($_POST['n'] ?? 0) ?: 80);
32         $page     = intval(($_POST['p'] ?? 0) ?: 1);
33         $startrec = ($page - 1) * $perpage;
34
35         $total = 0;
36         $results = [];
37
38         if (!strlen($search)) {
39                 $output = ['total' => 0, 'items_page' => $perpage, 'page' => $page, 'results' => $results];
40                 echo json_encode($output);
41                 exit();
42         }
43
44         $total = 0;
45
46         $count_stmt = DBA::p(
47                 "SELECT COUNT(*) AS `total`
48                         FROM `profile`
49                         JOIN `user` ON `user`.`uid` = `profile`.`uid`
50                         WHERE `profile`.`net-publish`
51                         AND MATCH(`pub_keywords`) AGAINST (?)",
52                 $search
53         );
54         if (DBA::isResult($count_stmt)) {
55                 $row = DBA::fetch($count_stmt);
56                 $total = $row['total'];
57         }
58
59         DBA::close($count_stmt);
60
61         $search_stmt = DBA::p(
62                 "SELECT `pub_keywords`, `username`, `nickname`, `user`.`uid`
63                         FROM `user`
64                         JOIN `profile` ON `user`.`uid` = `profile`.`uid`
65                         WHERE `profile`.`net-publish`
66                         AND MATCH(`pub_keywords`) AGAINST (?)
67                         LIMIT ?, ?",
68                 $search,
69                 $startrec,
70                 $perpage
71         );
72
73         while ($search_result = DBA::fetch($search_stmt)) {
74                 $results[] = [
75                         'name'  => $search_result['name'],
76                         'url'   => DI::baseUrl() . '/profile/' . $search_result['nickname'],
77                         'photo' => User::getAvatarUrlForId($search_result['uid'], Proxy::SIZE_THUMB),
78                         'tags'  => str_replace([',', '  '], [' ', ' '], $search_result['pub_keywords'])
79                 ];
80         }
81
82         DBA::close($search_stmt);
83
84         $output = ['total' => $total, 'items_page' => $perpage, 'page' => $page, 'results' => $results];
85
86         echo json_encode($output);
87
88         exit();
89 }