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