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