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