]> git.mxchange.org Git - friendica.git/blob - mod/directory.php
c32cf5dbb8f93e204f7db42f80551a166dd05c19
[friendica.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         Nav::setSelected('directory');
49
50         if (!empty($a->data['search'])) {
51                 $search = Strings::escapeTags(trim($a->data['search']));
52         } else {
53                 $search = (!empty($_GET['search']) ? Strings::escapeTags(trim(rawurldecode($_GET['search']))) : '');
54         }
55
56         $gdirpath = '';
57         $dirurl = Config::get('system', 'directory');
58         if (strlen($dirurl)) {
59                 $gdirpath = Profile::zrl($dirurl, true);
60         }
61
62         if ($search) {
63                 $search = DBA::escape($search);
64
65                 $sql_extra = " AND ((`profile`.`name` LIKE '%$search%') OR
66                                 (`user`.`nickname` LIKE '%$search%') OR
67                                 (`profile`.`pdesc` LIKE '%$search%') OR
68                                 (`profile`.`locality` LIKE '%$search%') OR
69                                 (`profile`.`region` LIKE '%$search%') OR
70                                 (`profile`.`country-name` LIKE '%$search%') OR
71                                 (`profile`.`gender` LIKE '%$search%') OR
72                                 (`profile`.`marital` LIKE '%$search%') OR
73                                 (`profile`.`sexual` LIKE '%$search%') OR
74                                 (`profile`.`about` LIKE '%$search%') OR
75                                 (`profile`.`romance` LIKE '%$search%') OR
76                                 (`profile`.`work` LIKE '%$search%') OR
77                                 (`profile`.`education` LIKE '%$search%') OR
78                                 (`profile`.`pub_keywords` LIKE '%$search%') OR
79                                 (`profile`.`prv_keywords` LIKE '%$search%'))";
80         } else {
81                 $sql_extra = '';
82         }
83
84         $publish = (Config::get('system', 'publish_all') ? '' : " AND `publish` = 1 " );
85
86
87         $total = 0;
88         $cnt = DBA::fetchFirst("SELECT COUNT(*) AS `total` FROM `profile`
89                                 LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
90                                 WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed` $sql_extra");
91         if (DBA::isResult($cnt)) {
92                 $total = $cnt['total'];
93         }
94         $pager = new Pager($a->query_string, 60);
95
96         $order = " ORDER BY `name` ASC ";
97
98         $limit = $pager->getStart()."," . $pager->getItemsPerPage();
99
100         $r = DBA::p("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`, `user`.`timezone` , `user`.`page-flags`,
101                         `contact`.`addr`, `contact`.`url` AS profile_url FROM `profile`
102                         LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
103                         LEFT JOIN `contact` ON `contact`.`uid` = `user`.`uid`
104                         WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed` AND `contact`.`self`
105                         $sql_extra $order LIMIT $limit"
106         );
107         if (DBA::isResult($r)) {
108                 if (in_array('small', $a->argv)) {
109                         $photo = 'thumb';
110                 } else {
111                         $photo = 'photo';
112                 }
113
114                 while ($rr = DBA::fetch($r)) {
115
116                         $itemurl = (($rr['addr'] != "") ? $rr['addr'] : $rr['profile_url']);
117
118                         $profile_link = $rr['profile_url'];
119
120                         $pdesc = (($rr['pdesc']) ? $rr['pdesc'] . '<br />' : '');
121
122                         $details = '';
123                         if (strlen($rr['locality'])) {
124                                 $details .= $rr['locality'];
125                         }
126                         if (strlen($rr['region'])) {
127                                 if (strlen($rr['locality'])) {
128                                         $details .= ', ';
129                                 }
130                                 $details .= $rr['region'];
131                         }
132                         if (strlen($rr['country-name'])) {
133                                 if (strlen($details)) {
134                                         $details .= ', ';
135                                 }
136                                 $details .= $rr['country-name'];
137                         }
138 //                      if(strlen($rr['dob'])) {
139 //                              if(($years = age($rr['dob'],$rr['timezone'],'')) != 0)
140 //                                      $details .= '<br />' . L10n::t('Age: ') . $years;
141 //                      }
142 //                      if(strlen($rr['gender']))
143 //                              $details .= '<br />' . L10n::t('Gender: ') . $rr['gender'];
144
145                         $profile = $rr;
146
147                         if (!empty($profile['address'])
148                                 || !empty($profile['locality'])
149                                 || !empty($profile['region'])
150                                 || !empty($profile['postal-code'])
151                                 || !empty($profile['country-name'])
152                         ) {
153                                 $location = L10n::t('Location:');
154                         } else {
155                                 $location = '';
156                         }
157
158                         $gender   = (!empty($profile['gender']) ? L10n::t('Gender:')   : false);
159                         $marital  = (!empty($profile['marital']) ? L10n::t('Status:')   : false);
160                         $homepage = (!empty($profile['homepage']) ? L10n::t('Homepage:') : false);
161                         $about    = (!empty($profile['about']) ? L10n::t('About:')    : false);
162
163                         $location_e = $location;
164
165                         $photo_menu = [
166                                 'profile' => [L10n::t("View Profile"), Contact::magicLink($profile_link)]
167                         ];
168
169                         $entry = [
170                                 'id'           => $rr['id'],
171                                 'url'          => Contact::magicLInk($profile_link),
172                                 'itemurl'      => $itemurl,
173                                 'thumb'        => ProxyUtils::proxifyUrl($rr[$photo], false, ProxyUtils::SIZE_THUMB),
174                                 'img_hover'    => $rr['name'],
175                                 'name'         => $rr['name'],
176                                 'details'      => $details,
177                                 'account_type' => Contact::getAccountType($rr),
178                                 'profile'      => $profile,
179                                 'location'     => $location_e,
180                                 'tags'         => $rr['pub_keywords'],
181                                 'gender'       => $gender,
182                                 'pdesc'        => $pdesc,
183                                 'marital'      => $marital,
184                                 'homepage'     => $homepage,
185                                 'about'        => $about,
186                                 'photo_menu'   => $photo_menu,
187
188                         ];
189
190                         $arr = ['contact' => $rr, 'entry' => $entry];
191
192                         Hook::callAll('directory_item', $arr);
193
194                         unset($profile);
195                         unset($location);
196
197                         if (!$arr['entry']) {
198                                 continue;
199                         }
200
201                         $entries[] = $arr['entry'];
202                 }
203                 DBA::close($r);
204
205                 $tpl = Renderer::getMarkupTemplate('directory_header.tpl');
206
207                 $o .= Renderer::replaceMacros($tpl, [
208                         '$search'    => $search,
209                         '$globaldir' => L10n::t('Global Directory'),
210                         '$gdirpath'  => $gdirpath,
211                         '$desc'      => L10n::t('Find on this site'),
212                         '$contacts'  => $entries,
213                         '$finding'   => L10n::t('Results for:'),
214                         '$findterm'  => (strlen($search) ? $search : ""),
215                         '$title'     => L10n::t('Site Directory'),
216                         '$submit'    => L10n::t('Find'),
217                         '$paginate'  => $pager->renderFull($total),
218                 ]);
219         } else {
220                 info(L10n::t("No entries \x28some entries may be hidden\x29.") . EOL);
221         }
222
223         return $o;
224 }