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