]> git.mxchange.org Git - friendica.git/blob - mod/dirfind.php
Correct content type
[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(defaults($_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::ACTIVITYPUB, 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                         $objresult->pcid = $contact["zid"];
89
90                         $j->results[] = $objresult;
91
92                         // Add the contact to the global contacts if it isn't already in our system
93                         if (($contact["cid"] == 0) && ($contact["zid"] == 0) && ($contact["gid"] == 0)) {
94                                 GContact::update($user_data);
95                         }
96                 } elseif ($local) {
97
98                         if ($community)
99                                 $extra_sql = " AND `community`";
100                         else
101                                 $extra_sql = "";
102
103                         $perpage = 80;
104                         $startrec = (($a->pager['page']) * $perpage) - $perpage;
105
106                         if (Config::get('system','diaspora_enabled')) {
107                                 $diaspora = Protocol::DIASPORA;
108                         } else {
109                                 $diaspora = Protocol::DFRN;
110                         }
111
112                         if (!Config::get('system','ostatus_disabled')) {
113                                 $ostatus = Protocol::OSTATUS;
114                         } else {
115                                 $ostatus = Protocol::DFRN;
116                         }
117
118                         $search2 = "%".$search."%";
119
120                         /// @TODO These 2 SELECTs are not checked on validity with DBA::isResult()
121                         $count = q("SELECT count(*) AS `total` FROM `gcontact`
122                                         WHERE NOT `hide` AND `network` IN ('%s', '%s', '%s') AND
123                                                 ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`)) AND
124                                                 (`url` LIKE '%s' OR `name` LIKE '%s' OR `location` LIKE '%s' OR
125                                                 `addr` LIKE '%s' OR `about` LIKE '%s' OR `keywords` LIKE '%s') $extra_sql",
126                                         DBA::escape(Protocol::DFRN), DBA::escape($ostatus), DBA::escape($diaspora),
127                                         DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)),
128                                         DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)));
129
130                         $results = q("SELECT `nurl`
131                                         FROM `gcontact`
132                                         WHERE NOT `hide` AND `network` IN ('%s', '%s', '%s') AND
133                                                 ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`)) AND
134                                                 (`url` LIKE '%s' OR `name` LIKE '%s' OR `location` LIKE '%s' OR
135                                                 `addr` LIKE '%s' OR `about` LIKE '%s' OR `keywords` LIKE '%s') $extra_sql
136                                                 GROUP BY `nurl`
137                                                 ORDER BY `updated` DESC LIMIT %d, %d",
138                                         DBA::escape(Protocol::DFRN), DBA::escape($ostatus), DBA::escape($diaspora),
139                                         DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)),
140                                         DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)), DBA::escape(escape_tags($search2)),
141                                         intval($startrec), intval($perpage));
142                         $j = new stdClass();
143                         $j->total = $count[0]["total"];
144                         $j->items_page = $perpage;
145                         $j->page = $a->pager['page'];
146                         foreach ($results AS $result) {
147                                 if (PortableContact::alternateOStatusUrl($result["nurl"])) {
148                                         continue;
149                                 }
150
151                                 $urlparts = parse_url($result["nurl"]);
152
153                                 // Ignore results that look strange.
154                                 // For historic reasons the gcontact table does contain some garbage.
155                                 if (!empty($urlparts['query']) || !empty($urlparts['fragment'])) {
156                                         continue;
157                                 }
158
159                                 $result = Contact::getDetailsByURL($result["nurl"], local_user());
160
161                                 if ($result["name"] == "") {
162                                         $result["name"] = end(explode("/", $urlparts["path"]));
163                                 }
164
165                                 $objresult = new stdClass();
166                                 $objresult->cid = $result["cid"];
167                                 $objresult->pcid = $result["zid"];
168                                 $objresult->name = $result["name"];
169                                 $objresult->addr = $result["addr"];
170                                 $objresult->url = $result["url"];
171                                 $objresult->photo = $result["photo"];
172                                 $objresult->tags = $result["keywords"];
173                                 $objresult->network = $result["network"];
174
175                                 $j->results[] = $objresult;
176                         }
177
178                         // Add found profiles from the global directory to the local directory
179                         Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
180                 } else {
181
182                         $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
183
184                         if(strlen(Config::get('system','directory')))
185                                 $x = Network::fetchUrl(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
186
187                         $j = json_decode($x);
188                 }
189
190                 if ($j->total) {
191                         $a->set_pager_total($j->total);
192                         $a->set_pager_itemspage($j->items_page);
193                 }
194
195                 if (!empty($j->results)) {
196
197                         $id = 0;
198
199                         foreach ($j->results as $jj) {
200
201                                 $alt_text = "";
202
203                                 $contact_details = Contact::getDetailsByURL($jj->url, local_user());
204
205                                 $itemurl = (($contact_details["addr"] != "") ? $contact_details["addr"] : $jj->url);
206
207                                 // If We already know this contact then don't show the "connect" button
208                                 if ($jj->cid > 0) {
209                                         $connlnk = "";
210                                         $conntxt = "";
211                                         $contact = DBA::selectFirst('contact', [], ['id' => $jj->cid]);
212                                         if (DBA::isResult($contact)) {
213                                                 $photo_menu = Contact::photoMenu($contact);
214                                                 $details = _contact_detail_for_template($contact);
215                                                 $alt_text = $details['alt_text'];
216                                         } else {
217                                                 $photo_menu = [];
218                                         }
219                                 } else {
220                                         $connlnk = System::baseUrl().'/follow/?url='.(!empty($jj->connect) ? $jj->connect : $jj->url);
221                                         $conntxt = L10n::t('Connect');
222
223                                         $contact = DBA::selectFirst('contact', [], ['id' => $jj->pcid]);
224                                         if (DBA::isResult($contact)) {
225                                                 $photo_menu = Contact::photoMenu($contact);
226                                         } else {
227                                                 $photo_menu = [];
228                                         }
229
230                                         $photo_menu['profile'] = [L10n::t("View Profile"), Contact::magicLink($jj->url)];
231                                         $photo_menu['follow'] = [L10n::t("Connect/Follow"), $connlnk];
232                                 }
233
234                                 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
235
236                                 $entry = [
237                                         'alt_text' => $alt_text,
238                                         'url' => Contact::magicLink($jj->url),
239                                         'itemurl' => $itemurl,
240                                         'name' => htmlentities($jj->name),
241                                         'thumb' => ProxyUtils::proxifyUrl($jj->photo, false, ProxyUtils::SIZE_THUMB),
242                                         'img_hover' => $jj->tags,
243                                         'conntxt' => $conntxt,
244                                         'connlnk' => $connlnk,
245                                         'photo_menu' => $photo_menu,
246                                         'details'       => $contact_details['location'],
247                                         'tags'          => $contact_details['keywords'],
248                                         'about'         => $contact_details['about'],
249                                         'account_type'  => Contact::getAccountType($contact_details),
250                                         'network' => ContactSelector::networkToName($jj->network, $jj->url),
251                                         'id' => ++$id,
252                                 ];
253                                 $entries[] = $entry;
254                         }
255
256                 $tpl = get_markup_template('viewcontact_template.tpl');
257
258                 $o .= replace_macros($tpl,[
259                         'title' => $header,
260                         '$contacts' => $entries,
261                         '$paginate' => paginate($a),
262                 ]);
263
264                 } else {
265                         info(L10n::t('No matches') . EOL);
266                 }
267
268         }
269
270         return $o;
271 }