]> git.mxchange.org Git - friendica.git/blob - mod/directory.php
Catch HTTPExceptions in App::runFrontend()
[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\Addon;
11 use Friendica\Core\Config;
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                         $itemurl= '';
116
117                         $itemurl = (($rr['addr'] != "") ? $rr['addr'] : $rr['profile_url']);
118
119                         $profile_link = $rr['profile_url'];
120
121                         $pdesc = (($rr['pdesc']) ? $rr['pdesc'] . '<br />' : '');
122
123                         $details = '';
124                         if (strlen($rr['locality'])) {
125                                 $details .= $rr['locality'];
126                         }
127                         if (strlen($rr['region'])) {
128                                 if (strlen($rr['locality'])) {
129                                         $details .= ', ';
130                                 }
131                                 $details .= $rr['region'];
132                         }
133                         if (strlen($rr['country-name'])) {
134                                 if (strlen($details)) {
135                                         $details .= ', ';
136                                 }
137                                 $details .= $rr['country-name'];
138                         }
139 //                      if(strlen($rr['dob'])) {
140 //                              if(($years = age($rr['dob'],$rr['timezone'],'')) != 0)
141 //                                      $details .= '<br />' . L10n::t('Age: ') . $years;
142 //                      }
143 //                      if(strlen($rr['gender']))
144 //                              $details .= '<br />' . L10n::t('Gender: ') . $rr['gender'];
145
146                         $profile = $rr;
147
148                         if (!empty($profile['address'])
149                                 || !empty($profile['locality'])
150                                 || !empty($profile['region'])
151                                 || !empty($profile['postal-code'])
152                                 || !empty($profile['country-name'])
153                         ) {
154                                 $location = L10n::t('Location:');
155                         } else {
156                                 $location = '';
157                         }
158
159                         $gender   = (!empty($profile['gender']) ? L10n::t('Gender:')   : false);
160                         $marital  = (!empty($profile['marital']) ? L10n::t('Status:')   : false);
161                         $homepage = (!empty($profile['homepage']) ? L10n::t('Homepage:') : false);
162                         $about    = (!empty($profile['about']) ? L10n::t('About:')    : false);
163
164                         $location_e = $location;
165
166                         $photo_menu = [
167                                 'profile' => [L10n::t("View Profile"), Contact::magicLink($profile_link)]
168                         ];
169
170                         $entry = [
171                                 'id'           => $rr['id'],
172                                 'url'          => Contact::magicLInk($profile_link),
173                                 'itemurl'      => $itemurl,
174                                 'thumb'        => ProxyUtils::proxifyUrl($rr[$photo], false, ProxyUtils::SIZE_THUMB),
175                                 'img_hover'    => $rr['name'],
176                                 'name'         => $rr['name'],
177                                 'details'      => $details,
178                                 'account_type' => Contact::getAccountType($rr),
179                                 'profile'      => $profile,
180                                 'location'     => $location_e,
181                                 'tags'         => $rr['pub_keywords'],
182                                 'gender'       => $gender,
183                                 'pdesc'        => $pdesc,
184                                 'marital'      => $marital,
185                                 'homepage'     => $homepage,
186                                 'about'        => $about,
187                                 'photo_menu'   => $photo_menu,
188
189                         ];
190
191                         $arr = ['contact' => $rr, 'entry' => $entry];
192
193                         Addon::callHooks('directory_item', $arr);
194
195                         unset($profile);
196                         unset($location);
197
198                         if (!$arr['entry']) {
199                                 continue;
200                         }
201
202                         $entries[] = $arr['entry'];
203                 }
204                 DBA::close($r);
205
206                 $tpl = Renderer::getMarkupTemplate('directory_header.tpl');
207
208                 $o .= Renderer::replaceMacros($tpl, [
209                         '$search'    => $search,
210                         '$globaldir' => L10n::t('Global Directory'),
211                         '$gdirpath'  => $gdirpath,
212                         '$desc'      => L10n::t('Find on this site'),
213                         '$contacts'  => $entries,
214                         '$finding'   => L10n::t('Results for:'),
215                         '$findterm'  => (strlen($search) ? $search : ""),
216                         '$title'     => L10n::t('Site Directory'),
217                         '$submit'    => L10n::t('Find'),
218                         '$paginate'  => $pager->renderFull($total),
219                 ]);
220         } else {
221                 info(L10n::t("No entries \x28some entries may be hidden\x29.") . EOL);
222         }
223
224         return $o;
225 }