Merge pull request #6694 from Quix0r/rewrites/added-missing-var-init
[friendica.git/.git] / mod / directory.php
1 <?php
2 /**
3  * @file mod/directory.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\Nav;
8 use Friendica\Content\Pager;
9 use Friendica\Content\Widget;
10 use Friendica\Core\Config;
11 use Friendica\Core\Hook;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Renderer;
14 use Friendica\Database\DBA;
15 use Friendica\Model\Contact;
16 use Friendica\Model\Profile;
17 use Friendica\Util\Proxy as ProxyUtils;
18 use Friendica\Util\Strings;
19
20 function directory_init(App $a)
21 {
22         if (local_user()) {
23                 $a->page['aside'] .= Widget::findPeople();
24                 $a->page['aside'] .= Widget::follow();
25         } else {
26                 unset($_SESSION['theme']);
27                 unset($_SESSION['mobile-theme']);
28         }
29 }
30
31 function directory_post(App $a)
32 {
33         if (!empty($_POST['search'])) {
34                 $a->data['search'] = $_POST['search'];
35         }
36 }
37
38 function directory_content(App $a)
39 {
40         if ((Config::get('system', 'block_public') && !local_user() && !remote_user())
41                 || (Config::get('system', 'block_local_dir') && !local_user() && !remote_user())
42         ) {
43                 notice(L10n::t('Public access denied.') . EOL);
44                 return;
45         }
46
47         $o = '';
48         $entries = [];
49
50         Nav::setSelected('directory');
51
52         if (!empty($a->data['search'])) {
53                 $search = Strings::escapeTags(trim($a->data['search']));
54         } else {
55                 $search = (!empty($_GET['search']) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : '');
56         }
57
58         $gdirpath = '';
59         $dirurl = Config::get('system', 'directory');
60         if (strlen($dirurl)) {
61                 $gdirpath = Profile::zrl($dirurl, true);
62         }
63
64         if ($search) {
65                 $search = DBA::escape($search);
66
67                 $sql_extra = " AND ((`profile`.`name` LIKE '%$search%') OR
68                                 (`user`.`nickname` LIKE '%$search%') OR
69                                 (`profile`.`pdesc` LIKE '%$search%') OR
70                                 (`profile`.`locality` LIKE '%$search%') OR
71                                 (`profile`.`region` LIKE '%$search%') OR
72                                 (`profile`.`country-name` LIKE '%$search%') OR
73                                 (`profile`.`gender` LIKE '%$search%') OR
74                                 (`profile`.`marital` LIKE '%$search%') OR
75                                 (`profile`.`sexual` LIKE '%$search%') OR
76                                 (`profile`.`about` LIKE '%$search%') OR
77                                 (`profile`.`romance` LIKE '%$search%') OR
78                                 (`profile`.`work` LIKE '%$search%') OR
79                                 (`profile`.`education` LIKE '%$search%') OR
80                                 (`profile`.`pub_keywords` LIKE '%$search%') OR
81                                 (`profile`.`prv_keywords` LIKE '%$search%'))";
82         } else {
83                 $sql_extra = '';
84         }
85
86         $publish = (Config::get('system', 'publish_all') ? '' : " AND `publish` = 1 " );
87
88
89         $total = 0;
90         $cnt = DBA::fetchFirst("SELECT COUNT(*) AS `total` FROM `profile`
91                                 LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
92                                 WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed` $sql_extra");
93         if (DBA::isResult($cnt)) {
94                 $total = $cnt['total'];
95         }
96         $pager = new Pager($a->query_string, 60);
97
98         $order = " ORDER BY `name` ASC ";
99
100         $limit = $pager->getStart()."," . $pager->getItemsPerPage();
101
102         $r = DBA::p("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`, `user`.`timezone` , `user`.`page-flags`,
103                         `contact`.`addr`, `contact`.`url` AS `profile_url` FROM `profile`
104                         LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
105                         LEFT JOIN `contact` ON `contact`.`uid` = `user`.`uid`
106                         WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed` AND `contact`.`self`
107                         $sql_extra $order LIMIT $limit"
108         );
109         if (DBA::isResult($r)) {
110                 if (in_array('small', $a->argv)) {
111                         $photo = 'thumb';
112                 } else {
113                         $photo = 'photo';
114                 }
115
116                 while ($rr = DBA::fetch($r)) {
117                         $entries[] = format_directory_entry($rr, $photo);
118                 }
119                 DBA::close($r);
120         } else {
121                 info(L10n::t("No entries \x28some entries may be hidden\x29.") . EOL);
122         }
123
124         $tpl = Renderer::getMarkupTemplate('directory_header.tpl');
125
126         $o .= Renderer::replaceMacros($tpl, [
127                 '$search'    => $search,
128                 '$globaldir' => L10n::t('Global Directory'),
129                 '$gdirpath'  => $gdirpath,
130                 '$desc'      => L10n::t('Find on this site'),
131                 '$contacts'  => $entries,
132                 '$finding'   => L10n::t('Results for:'),
133                 '$findterm'  => (strlen($search) ? $search : ""),
134                 '$title'     => L10n::t('Site Directory'),
135                 '$search_mod' => 'directory',
136                 '$submit'    => L10n::t('Find'),
137                 '$paginate'  => $pager->renderFull($total),
138         ]);
139
140         return $o;
141 }
142
143 /**
144  * Format contact/profile/user data from the database into an usable
145  * array for displaying directory entries.
146  * 
147  * @param array $arr The directory entry from the database.
148  * @param string $photo_size Avatar size (thumb, photo or micro).
149  * 
150  * @return array
151  */
152 function format_directory_entry(array $arr, $photo_size = 'photo')
153 {
154         $itemurl = (($arr['addr'] != "") ? $arr['addr'] : $arr['profile_url']);
155
156         $profile_link = $arr['profile_url'];
157
158         $pdesc = (($arr['pdesc']) ? $arr['pdesc'] . '<br />' : '');
159
160         $details = '';
161         if (strlen($arr['locality'])) {
162                 $details .= $arr['locality'];
163         }
164         if (strlen($arr['region'])) {
165                 if (strlen($arr['locality'])) {
166                         $details .= ', ';
167                 }
168                 $details .= $arr['region'];
169         }
170         if (strlen($arr['country-name'])) {
171                 if (strlen($details)) {
172                         $details .= ', ';
173                 }
174                 $details .= $arr['country-name'];
175         }
176
177         $profile = $arr;
178
179         if (!empty($profile['address'])
180                 || !empty($profile['locality'])
181                 || !empty($profile['region'])
182                 || !empty($profile['postal-code'])
183                 || !empty($profile['country-name'])
184         ) {
185                 $location = L10n::t('Location:');
186         } else {
187                 $location = '';
188         }
189
190         $gender   = (!empty($profile['gender'])   ? L10n::t('Gender:')   : false);
191         $marital  = (!empty($profile['marital'])  ? L10n::t('Status:')   : false);
192         $homepage = (!empty($profile['homepage']) ? L10n::t('Homepage:') : false);
193         $about    = (!empty($profile['about'])    ? L10n::t('About:')    : false);
194
195         $location_e = $location;
196
197         $photo_menu = [
198                 'profile' => [L10n::t("View Profile"), Contact::magicLink($profile_link)]
199         ];
200
201         $entry = [
202                 'id'           => $arr['id'],
203                 'url'          => Contact::magicLInk($profile_link),
204                 'itemurl'      => $itemurl,
205                 'thumb'        => ProxyUtils::proxifyUrl($arr[$photo_size], false, ProxyUtils::SIZE_THUMB),
206                 'img_hover'    => $arr['name'],
207                 'name'         => $arr['name'],
208                 'details'      => $details,
209                 'account_type' => Contact::getAccountType($arr),
210                 'profile'      => $profile,
211                 'location'     => $location_e,
212                 'tags'         => $arr['pub_keywords'],
213                 'gender'       => $gender,
214                 'pdesc'        => $pdesc,
215                 'marital'      => $marital,
216                 'homepage'     => $homepage,
217                 'about'        => $about,
218                 'photo_menu'   => $photo_menu,
219
220         ];
221
222         $hook = ['contact' => $arr, 'entry' => $entry];
223
224         Hook::callAll('directory_item', $hook);
225
226         unset($profile);
227         unset($location);
228
229         return $hook['entry'];
230 }