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