]> git.mxchange.org Git - friendica.git/blob - mod/dirfind.php
a5b77312f35d03ba078a8700b67220a7acfea2ef
[friendica.git] / mod / dirfind.php
1 <?php
2 /**
3  * @file mod/dirfind.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\ContactSelector;
8 use Friendica\Content\Pager;
9 use Friendica\Content\Widget;
10 use Friendica\Core\Config;
11 use Friendica\Core\L10n;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\System;
15 use Friendica\Core\Worker;
16 use Friendica\Database\DBA;
17 use Friendica\Model;
18 use Friendica\Module;
19 use Friendica\Network\Probe;
20 use Friendica\Protocol\PortableContact;
21 use Friendica\Util\Network;
22 use Friendica\Util\Proxy as ProxyUtils;
23 use Friendica\Util\Strings;
24
25
26 function dirfind_init(App $a) {
27
28         if (! local_user()) {
29                 notice(L10n::t('Permission denied.') . EOL );
30                 return;
31         }
32
33         if (empty($a->page['aside'])) {
34                 $a->page['aside'] = '';
35         }
36
37         $a->page['aside'] .= Widget::findPeople();
38
39         $a->page['aside'] .= Widget::follow();
40 }
41
42 function dirfind_content(App $a, $prefix = "") {
43
44         $community = false;
45         $discover_user = false;
46
47         $local = Config::get('system','poco_local_search');
48
49         $search = $prefix.Strings::escapeTags(trim(defaults($_REQUEST, 'search', '')));
50
51         $header = '';
52
53         if (strpos($search,'@') === 0) {
54                 $search = substr($search,1);
55                 $header = L10n::t('People Search - %s', $search);
56                 if ((filter_var($search, FILTER_VALIDATE_EMAIL) && Network::isEmailDomainValid($search)) ||
57                         (substr(Strings::normaliseLink($search), 0, 7) == "http://")) {
58                         $user_data = Probe::uri($search);
59                         $discover_user = (in_array($user_data["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::DIASPORA]));
60                 }
61         }
62
63         if (strpos($search,'!') === 0) {
64                 $search = substr($search,1);
65                 $community = true;
66                 $header = L10n::t('Forum Search - %s', $search);
67         }
68
69         $o = '';
70
71         if ($search) {
72                 $pager = new Pager($a->query_string);
73
74                 if ($discover_user) {
75                         $j = new stdClass();
76                         $j->total = 1;
77                         $j->items_page = 1;
78                         $j->page = $pager->getPage();
79
80                         $objresult = new stdClass();
81                         $objresult->cid = 0;
82                         $objresult->name = $user_data["name"];
83                         $objresult->addr = $user_data["addr"];
84                         $objresult->url = $user_data["url"];
85                         $objresult->photo = $user_data["photo"];
86                         $objresult->tags = "";
87                         $objresult->network = $user_data["network"];
88
89                         $contact = Model\Contact::getDetailsByURL($user_data["url"], local_user());
90                         $objresult->cid = $contact["cid"];
91                         $objresult->pcid = $contact["zid"];
92
93                         $j->results[] = $objresult;
94
95                         // Add the contact to the global contacts if it isn't already in our system
96                         if (($contact["cid"] == 0) && ($contact["zid"] == 0) && ($contact["gid"] == 0)) {
97                                 Model\GContact::update($user_data);
98                         }
99                 } elseif ($local) {
100                         if ($community) {
101                                 $extra_sql = " AND `community`";
102                         } else {
103                                 $extra_sql = "";
104                         }
105
106                         $pager->setItemsPerPage(80);
107
108                         if (Config::get('system','diaspora_enabled')) {
109                                 $diaspora = Protocol::DIASPORA;
110                         } else {
111                                 $diaspora = Protocol::DFRN;
112                         }
113
114                         if (!Config::get('system','ostatus_disabled')) {
115                                 $ostatus = Protocol::OSTATUS;
116                         } else {
117                                 $ostatus = Protocol::DFRN;
118                         }
119
120                         $search2 = "%".$search."%";
121
122                         /// @TODO These 2 SELECTs are not checked on validity with DBA::isResult()
123                         $count = q("SELECT count(*) AS `total` FROM `gcontact`
124                                         WHERE NOT `hide` AND `network` IN ('%s', '%s', '%s', '%s') AND
125                                                 ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`)) AND
126                                                 (`url` LIKE '%s' OR `name` LIKE '%s' OR `location` LIKE '%s' OR
127                                                 `addr` LIKE '%s' OR `about` LIKE '%s' OR `keywords` LIKE '%s') $extra_sql",
128                                         DBA::escape(Protocol::ACTIVITYPUB), DBA::escape(Protocol::DFRN), DBA::escape($ostatus), DBA::escape($diaspora),
129                                         DBA::escape(Strings::escapeHtml($search2)), DBA::escape(Strings::escapeHtml($search2)), DBA::escape(Strings::escapeHtml($search2)),
130                                         DBA::escape(Strings::escapeHtml($search2)), DBA::escape(Strings::escapeHtml($search2)), DBA::escape(Strings::escapeHtml($search2)));
131
132                         $results = q("SELECT `nurl`
133                                         FROM `gcontact`
134                                         WHERE NOT `hide` AND `network` IN ('%s', '%s', '%s', '%s') AND
135                                                 ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`)) AND
136                                                 (`url` LIKE '%s' OR `name` LIKE '%s' OR `location` LIKE '%s' OR
137                                                 `addr` LIKE '%s' OR `about` LIKE '%s' OR `keywords` LIKE '%s') $extra_sql
138                                                 GROUP BY `nurl`
139                                                 ORDER BY `updated` DESC LIMIT %d, %d",
140                                         DBA::escape(Protocol::ACTIVITYPUB), DBA::escape(Protocol::DFRN), DBA::escape($ostatus), DBA::escape($diaspora),
141                                         DBA::escape(Strings::escapeHtml($search2)), DBA::escape(Strings::escapeHtml($search2)), DBA::escape(Strings::escapeHtml($search2)),
142                                         DBA::escape(Strings::escapeHtml($search2)), DBA::escape(Strings::escapeHtml($search2)), DBA::escape(Strings::escapeHtml($search2)),
143                                         $pager->getStart(), $pager->getItemsPerPage());
144                         $j = new stdClass();
145                         $j->total = $count[0]["total"];
146                         $j->items_page = $pager->getItemsPerPage();
147                         $j->page = $pager->getPage();
148                         foreach ($results AS $result) {
149                                 if (PortableContact::alternateOStatusUrl($result["nurl"])) {
150                                         continue;
151                                 }
152
153                                 $urlparts = parse_url($result["nurl"]);
154
155                                 // Ignore results that look strange.
156                                 // For historic reasons the gcontact table does contain some garbage.
157                                 if (!empty($urlparts['query']) || !empty($urlparts['fragment'])) {
158                                         continue;
159                                 }
160
161                                 $result = Model\Contact::getDetailsByURL($result["nurl"], local_user());
162
163                                 if ($result["name"] == "") {
164                                         $result["name"] = end(explode("/", $urlparts["path"]));
165                                 }
166
167                                 $objresult = new stdClass();
168                                 $objresult->cid = $result["cid"];
169                                 $objresult->pcid = $result["zid"];
170                                 $objresult->name = $result["name"];
171                                 $objresult->addr = $result["addr"];
172                                 $objresult->url = $result["url"];
173                                 $objresult->photo = $result["photo"];
174                                 $objresult->tags = $result["keywords"];
175                                 $objresult->network = $result["network"];
176
177                                 $j->results[] = $objresult;
178                         }
179
180                         // Add found profiles from the global directory to the local directory
181                         Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
182                 } elseif (strlen(Config::get('system','directory'))) {
183                         $p = (($pager->getPage() != 1) ? '&p=' . $pager->getPage() : '');
184
185                         $x = Network::fetchUrl(get_server() . '/lsearch?f=' . $p .  '&search=' . urlencode($search));
186
187                         $j = json_decode($x);
188                         $pager->setItemsPerPage($j->items_page);
189                 }
190
191                 if (!empty($j->results)) {
192                         $id = 0;
193
194                         $entries = [];
195                         foreach ($j->results as $jj) {
196
197                                 $alt_text = "";
198
199                                 $contact_details = Model\Contact::getDetailsByURL($jj->url, local_user());
200
201                                 $itemurl = (($contact_details["addr"] != "") ? $contact_details["addr"] : $jj->url);
202
203                                 // If We already know this contact then don't show the "connect" button
204                                 if ($jj->cid > 0) {
205                                         $connlnk = "";
206                                         $conntxt = "";
207                                         $contact = DBA::selectFirst('contact', [], ['id' => $jj->cid]);
208                                         if (DBA::isResult($contact)) {
209                                                 $photo_menu = Model\Contact::photoMenu($contact);
210                                                 $details = Module\Contact::getContactTemplateVars($contact);
211                                                 $alt_text = $details['alt_text'];
212                                         } else {
213                                                 $photo_menu = [];
214                                         }
215                                 } else {
216                                         $connlnk = System::baseUrl().'/follow/?url='.(!empty($jj->connect) ? $jj->connect : $jj->url);
217                                         $conntxt = L10n::t('Connect');
218
219                                         $contact = DBA::selectFirst('contact', [], ['id' => $jj->pcid]);
220                                         if (DBA::isResult($contact)) {
221                                                 $photo_menu = Model\Contact::photoMenu($contact);
222                                         } else {
223                                                 $photo_menu = [];
224                                         }
225
226                                         $photo_menu['profile'] = [L10n::t("View Profile"), Model\Contact::magicLink($jj->url)];
227                                         $photo_menu['follow'] = [L10n::t("Connect/Follow"), $connlnk];
228                                 }
229
230                                 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
231
232                                 $entry = [
233                                         'alt_text' => $alt_text,
234                                         'url' => Model\Contact::magicLink($jj->url),
235                                         'itemurl' => $itemurl,
236                                         'name' => $jj->name,
237                                         'thumb' => ProxyUtils::proxifyUrl($jj->photo, false, ProxyUtils::SIZE_THUMB),
238                                         'img_hover' => $jj->tags,
239                                         'conntxt' => $conntxt,
240                                         'connlnk' => $connlnk,
241                                         'photo_menu' => $photo_menu,
242                                         'details'       => $contact_details['location'],
243                                         'tags'          => $contact_details['keywords'],
244                                         'about'         => $contact_details['about'],
245                                         'account_type'  => Model\Contact::getAccountType($contact_details),
246                                         'network' => ContactSelector::networkToName($jj->network, $jj->url),
247                                         'id' => ++$id,
248                                 ];
249                                 $entries[] = $entry;
250                         }
251
252                         $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
253                         $o .= Renderer::replaceMacros($tpl,[
254                                 'title' => $header,
255                                 '$contacts' => $entries,
256                                 '$paginate' => $pager->renderFull($j->total),
257                         ]);
258                 } else {
259                         info(L10n::t('No matches') . EOL);
260                 }
261
262         }
263
264         return $o;
265 }