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