]> git.mxchange.org Git - friendica.git/blob - mod/dirfind.php
Events: Now with guid.
[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                         $search2 = "%".$search."%";
98
99                         $count = q("SELECT count(*) AS `total` FROM `gcontact`
100                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
101                                                 AND `contact`.`uid` = %d AND NOT `contact`.`blocked`
102                                                 AND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')
103                                         WHERE (`contact`.`id` > 0 OR (NOT `gcontact`.`hide` AND `gcontact`.`network` IN ('%s', '%s', '%s') AND
104                                         ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)))) AND
105                                         (`gcontact`.`url` LIKE '%s' OR `gcontact`.`name` LIKE '%s' OR `gcontact`.`location` LIKE '%s' OR
106                                                 `gcontact`.`addr` LIKE '%s' OR `gcontact`.`about` LIKE '%s' OR `gcontact`.`keywords` LIKE '%s') $extra_sql",
107                                         intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
108                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
109                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
110                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)));
111
112                         $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`network`, `gcontact`.`keywords`, `gcontact`.`addr`
113                                         FROM `gcontact`
114                                         LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`
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                                                 GROUP BY `gcontact`.`nurl`
122                                                 ORDER BY `gcontact`.`updated` DESC LIMIT %d, %d",
123                                         intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND),
124                                         dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora),
125                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
126                                         dbesc(escape_tags($search2)), dbesc(escape_tags($search2)), dbesc(escape_tags($search2)),
127                                         intval($startrec), intval($perpage));
128                         $j = new stdClass();
129                         $j->total = $count[0]["total"];
130                         $j->items_page = $perpage;
131                         $j->page = $a->pager['page'];
132                         foreach ($results AS $result) {
133                                 if (poco_alternate_ostatus_url($result["url"]))
134                                          continue;
135
136                                 if ($result["name"] == "") {
137                                         $urlparts = parse_url($result["url"]);
138                                         $result["name"] = end(explode("/", $urlparts["path"]));
139                                 }
140
141                                 $objresult = new stdClass();
142                                 $objresult->cid = $result["cid"];
143                                 $objresult->name = $result["name"];
144                                 $objresult->addr = $result["addr"];
145                                 $objresult->url = $result["url"];
146                                 $objresult->photo = $result["photo"];
147                                 $objresult->tags = $result["keywords"];
148                                 $objresult->network = $result["network"];
149
150                                 $j->results[] = $objresult;
151                         }
152
153                         // Add found profiles from the global directory to the local directory
154                         proc_run('php','include/discover_poco.php', "dirsearch", urlencode($search));
155                 } else {
156
157                         $p = (($a->pager['page'] != 1) ? '&p=' . $a->pager['page'] : '');
158
159                         if(strlen(get_config('system','directory')))
160                                 $x = fetch_url(get_server().'/lsearch?f=' . $p .  '&search=' . urlencode($search));
161
162                         $j = json_decode($x);
163                 }
164
165                 if($j->total) {
166                         $a->set_pager_total($j->total);
167                         $a->set_pager_itemspage($j->items_page);
168                 }
169
170                 if(count($j->results)) {
171
172                         $id = 0;
173
174                         foreach($j->results as $jj) {
175
176                                 $alt_text = "";
177
178                                 $contact_details = get_contact_details_by_url($jj->url, local_user());
179
180                                 $itemurl = (($contact_details["addr"] != "") ? $contact_details["addr"] : $jj->url);
181
182                                 // If We already know this contact then don't show the "connect" button
183                                 if ($jj->cid > 0) {
184                                         $connlnk = "";
185                                         $conntxt = "";
186                                         $contact = q("SELECT * FROM `contact` WHERE `id` = %d",
187                                                         intval($jj->cid));
188                                         if ($contact) {
189                                                 $photo_menu = contact_photo_menu($contact[0]);
190                                                 $details = _contact_detail_for_template($contact[0]);
191                                                 $alt_text = $details['alt_text'];
192                                         } else
193                                                 $photo_menu = array();
194                                 } else {
195                                         $connlnk = $a->get_baseurl().'/follow/?url='.(($jj->connect) ? $jj->connect : $jj->url);
196                                         $conntxt = t('Connect');
197                                         $photo_menu = array(array(t("View Profile"), zrl($jj->url)));
198                                         $photo_menu[] = array(t("Connect/Follow"), $connlnk);
199                                 }
200
201                                 $jj->photo = str_replace("http:///photo/", get_server()."/photo/", $jj->photo);
202
203                                 $entry = array(
204                                         'alt_text' => $alt_text,
205                                         'url' => zrl($jj->url),
206                                         'itemurl' => $itemurl,
207                                         'name' => htmlentities($jj->name),
208                                         'thumb' => proxy_url($jj->photo, false, PROXY_SIZE_THUMB),
209                                         'img_hover' => $jj->tags,
210                                         'conntxt' => $conntxt,
211                                         'connlnk' => $connlnk,
212                                         'photo_menu' => $photo_menu,
213                                         'details'       => $contact_details['location'],
214                                         'tags'          => $contact_details['keywords'],
215                                         'about'         => $contact_details['about'],
216                                         'account_type'  => (($contact_details['community']) ? t('Forum') : ''),
217                                         'network' => network_to_name($jj->network, $jj->url),
218                                         'id' => ++$id,
219                                 );
220                                 $entries[] = $entry;
221                         }
222
223                 $tpl = get_markup_template('viewcontact_template.tpl');
224
225                 $o .= replace_macros($tpl,array(
226                         'title' => sprintf( t('People Search - %s'), $search),
227                         '$contacts' => $entries,
228                         '$paginate' => paginate($a),
229                 ));
230
231                 }
232                 else {
233                         info( t('No matches') . EOL);
234                 }
235
236         }
237
238         return $o;
239 }