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