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