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