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