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