]> git.mxchange.org Git - friendica.git/blob - mod/dirfind.php
Move fetch_url
[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\Model\Profile;
15 use Friendica\Network\Probe;
16 use Friendica\Protocol\PortableContact;
17 use Friendica\Util\Network;
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) && validate_email($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                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
117                                                 AND `contact`.`network` = `gcontact`.`network`
118                                                 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
119                                                 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
120                                         WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
121                                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
122                                         (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
123                                                 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql",
124                                         intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
125                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
126                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
127                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)));
128
129                         $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`network`, `gcontact`.`keywords`, `gcontact`.`addr`
130                                         FROM `gcontact`
131                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
132                                                 AND `contact`.`network` = `gcontact`.`network`
133                                                 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
134                                                 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
135                                         WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
136                                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
137                                         (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
138                                                 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql
139                                                 GROUP BY `gcontact`.`nurl`
140                                                 ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d",
141                                         intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
142                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
143                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
144                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
145                                         intval($startrec), intval($perpage));
146                         $j = new stdClass();
147                         $j->total = $count[0]["total"];
148                         $j->items_page = $perpage;
149                         $j->page = $a->pager['page'];
150                         foreach ($results AS $result) {
151                                 if (PortableContact::alternateOStatusUrl($result["url"])) {
152                                         continue;
153                                 }
154
155                                 $result = Contact::getDetailsByURL($result["url"], local_user(), $result);
156
157                                 if ($result["name"] == "") {
158                                         $urlparts = parse_url($result["url"]);
159                                         $result["name"] = end(explode("/", $urlparts["path"]));
160                                 }
161
162                                 $objresult = new stdClass();
163                                 $objresult->cid = $result["cid"];
164                                 $objresult->name = $result["name"];
165                                 $objresult->addr = $result["addr"];
166                                 $objresult->url = $result["url"];
167                                 $objresult->photo = $result["photo"];
168                                 $objresult->tags = $result["keywords"];
169                                 $objresult->network = $result["network"];
170
171                                 $j->results[] = $objresult;
172                         }
173
174                         // Add found profiles from the global directory to the local directory
175                         Worker::add(PRIORITY_LOW, 'DiscoverPoCo', "dirsearch", urlencode($search));
176                 } else {
177
178                         $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
179
180                         if(strlen(Config::get('system','directory')))
181                                 $x = Network::fetchURL(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
182
183                         $j = json_decode($x);
184                 }
185
186                 if ($j->total) {
187                         $a->set_pager_total($j->total);
188                         $a->set_pager_itemspage($j->items_page);
189                 }
190
191                 if (count($j->results)) {
192
193                         $id = 0;
194
195                         foreach ($j->results as $jj) {
196
197                                 $alt_text = "";
198
199                                 $contact_details = 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 = q("SELECT * FROM `contact` WHERE `id` = %d",
208                                                         intval($jj->cid));
209                                         if ($contact) {
210                                                 $photo_menu = Contact::photoMenu($contact[0]);
211                                                 $details = _contact_detail_for_template($contact[0]);
212                                                 $alt_text = $details['alt_text'];
213                                         } else {
214                                                 $photo_menu = [];
215                                         }
216                                 } else {
217                                         $connlnk = System::baseUrl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
218                                         $conntxt = L10n::t('Connect');
219                                         $photo_menu = [
220                                                 'profile' => [L10n::t("View Profile"), Profile::zrl($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' => Profile::zrl($jj->url),
230                                         'itemurl' => $itemurl,
231                                         'name' => htmlentities($jj->name),
232                                         'thumb' => proxy_url($jj->photo, false, PROXY_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 }