]> git.mxchange.org Git - friendica.git/blob - mod/dirfind.php
Merge pull request #4193 from annando/item-unification
[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\Core\Config;
8 use Friendica\Core\System;
9 use Friendica\Core\Worker;
10 use Friendica\Model\Contact;
11 use Friendica\Model\GContact;
12 use Friendica\Network\Probe;
13 use Friendica\Protocol\PortableContact;
14
15 require_once 'include/contact_widgets.php';
16 require_once 'mod/contacts.php';
17
18 function dirfind_init(App $a) {
19
20         if (! local_user()) {
21                 notice( t('Permission denied.') . EOL );
22                 return;
23         }
24
25         if (! x($a->page,'aside')) {
26                 $a->page['aside'] = '';
27         }
28
29         $a->page['aside'] .= findpeople_widget();
30
31         $a->page['aside'] .= follow_widget();
32 }
33
34 function dirfind_content(App $a, $prefix = "") {
35
36         $community = false;
37         $discover_user = false;
38
39         $local = Config::get('system','poco_local_search');
40
41         $search = $prefix.notags(trim($_REQUEST['search']));
42
43         if (strpos($search,'@') === 0) {
44                 $search = substr($search,1);
45                 $header = sprintf( t('People Search - %s'), $search);
46                 if ((valid_email($search) && validate_email($search)) ||
47                         (substr(normalise_link($search), 0, 7) == "http://")) {
48                         $user_data = Probe::uri($search);
49                         $discover_user = (in_array($user_data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)));
50                 }
51         }
52
53         if (strpos($search,'!') === 0) {
54                 $search = substr($search,1);
55                 $community = true;
56                 $header = sprintf( t('Forum Search - %s'), $search);
57         }
58
59         $o = '';
60
61         if ($search) {
62
63                 if ($discover_user) {
64                         $j = new stdClass();
65                         $j->total = 1;
66                         $j->items_page = 1;
67                         $j->page = $a->pager['page'];
68
69                         $objresult = new stdClass();
70                         $objresult->cid = 0;
71                         $objresult->name = $user_data["name"];
72                         $objresult->addr = $user_data["addr"];
73                         $objresult->url = $user_data["url"];
74                         $objresult->photo = $user_data["photo"];
75                         $objresult->tags = "";
76                         $objresult->network = $user_data["network"];
77
78                         $contact = Contact::getDetailsByURL($user_data["url"], local_user());
79                         $objresult->cid = $contact["cid"];
80
81                         $j->results[] = $objresult;
82
83                         // Add the contact to the global contacts if it isn't already in our system
84                         if (($contact["cid"] == 0) && ($contact["zid"] == 0) && ($contact["gid"] == 0)) {
85                                 GContact::update($user_data);
86                         }
87                 } elseif ($local) {
88
89                         if ($community)
90                                 $extra_sql = " AND `community`";
91                         else
92                                 $extra_sql = "";
93
94                         $perpage = 80;
95                         $startrec = (($a->pager['page']) * $perpage) - $perpage;
96
97                         if (Config::get('system','diaspora_enabled')) {
98                                 $diaspora = NETWORK_DIASPORA;
99                         } else {
100                                 $diaspora = NETWORK_DFRN;
101                         }
102
103                         if (!Config::get('system','ostatus_disabled')) {
104                                 $ostatus = NETWORK_OSTATUS;
105                         } else {
106                                 $ostatus = NETWORK_DFRN;
107                         }
108
109                         $search2 = "%".$search."%";
110
111                         /// @TODO These 2 SELECTs are not checked on validity with DBM::is_result()
112                         $count = q("SELECT count(*) AS `total` FROM `gcontact`
113                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
114                                                 AND `contact`.`network` = `gcontact`.`network`
115                                                 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
116                                                 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
117                                         WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
118                                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
119                                         (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
120                                                 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql",
121                                         intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
122                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
123                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
124                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)));
125
126                         $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`network`, `gcontact`.`keywords`, `gcontact`.`addr`
127                                         FROM `gcontact`
128                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
129                                                 AND `contact`.`network` = `gcontact`.`network`
130                                                 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
131                                                 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
132                                         WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
133                                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
134                                         (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
135                                                 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql
136                                                 GROUP BY `gcontact`.`nurl`
137                                                 ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d",
138                                         intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
139                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
140                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
141                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
142                                         intval($startrec), intval($perpage));
143                         $j = new stdClass();
144                         $j->total = $count[0]["total"];
145                         $j->items_page = $perpage;
146                         $j->page = $a->pager['page'];
147                         foreach ($results AS $result) {
148                                 if (PortableContact::alternateOStatusUrl($result["url"])) {
149                                         continue;
150                                 }
151
152                                 $result = Contact::getDetailsByURL($result["url"], local_user(), $result);
153
154                                 if ($result["name"] == "") {
155                                         $urlparts = parse_url($result["url"]);
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 = fetch_url(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 = q("SELECT * FROM `contact` WHERE `id` = %d",
205                                                         intval($jj->cid));
206                                         if ($contact) {
207                                                 $photo_menu = Contact::photoMenu($contact[0]);
208                                                 $details = _contact_detail_for_template($contact[0]);
209                                                 $alt_text = $details['alt_text'];
210                                         } else {
211                                                 $photo_menu = array();
212                                         }
213                                 } else {
214                                         $connlnk = System::baseUrl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
215                                         $conntxt = t('Connect');
216                                         $photo_menu = array(
217                                                 'profile' => array(t("View Profile"), zrl($jj->url)),
218                                                 'follow' => array(t("Connect/Follow"), $connlnk)
219                                         );
220                                 }
221
222                                 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
223
224                                 $entry = array(
225                                         'alt_text' => $alt_text,
226                                         'url' => zrl($jj->url),
227                                         'itemurl' => $itemurl,
228                                         'name' => htmlentities($jj->name),
229                                         'thumb' => proxy_url($jj->photo, false, PROXY_SIZE_THUMB),
230                                         'img_hover' => $jj->tags,
231                                         'conntxt' => $conntxt,
232                                         'connlnk' => $connlnk,
233                                         'photo_menu' => $photo_menu,
234                                         'details'       => $contact_details['location'],
235                                         'tags'          => $contact_details['keywords'],
236                                         'about'         => $contact_details['about'],
237                                         'account_type'  => Contact::getAccountType($contact_details),
238                                         'network' => ContactSelector::networkToName($jj->network, $jj->url),
239                                         'id' => ++$id,
240                                 );
241                                 $entries[] = $entry;
242                         }
243
244                 $tpl = get_markup_template('viewcontact_template.tpl');
245
246                 $o .= replace_macros($tpl,array(
247                         'title' => $header,
248                         '$contacts' => $entries,
249                         '$paginate' => paginate($a),
250                 ));
251
252                 } else {
253                         info( t('No matches') . EOL);
254                 }
255
256         }
257
258         return $o;
259 }