]> git.mxchange.org Git - friendica.git/blob - mod/poco.php
Add missing require for poco_serverlist
[friendica.git] / mod / poco.php
1 <?php
2
3 // See here for a documentation for portable contacts:
4 // https://web.archive.org/web/20160405005550/http://portablecontacts.net/draft-spec.html
5
6 use Friendica\App;
7
8 function poco_init(App $a) {
9         require_once 'include/bbcode.php';
10         require_once 'include/socgraph.php';
11
12         $system_mode = false;
13
14         if (intval(get_config('system', 'block_public')) || (get_config('system', 'block_local_dir'))) {
15                 http_status_exit(401);
16         }
17
18         if ($a->argc > 1) {
19                 $user = notags(trim($a->argv[1]));
20         }
21         if (! x($user)) {
22                 $c = q("SELECT * FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1");
23                 if (! dbm::is_result($c)) {
24                         http_status_exit(401);
25                 }
26                 $system_mode = true;
27         }
28
29         $format = (($_GET['format']) ? $_GET['format'] : 'json');
30
31         $justme = false;
32         $global = false;
33
34         if ($a->argc > 1 && $a->argv[1] === '@server') {
35                 // List of all servers that this server knows
36                 $ret = poco_serverlist();
37                 header('Content-type: application/json');
38                 echo json_encode($ret);
39                 killme();
40         }
41         if ($a->argc > 1 && $a->argv[1] === '@global') {
42                 // List of all profiles that this server recently had data from
43                 $global = true;
44                 $update_limit = date("Y-m-d H:i:s", time() - 30 * 86400);
45         }
46         if ($a->argc > 2 && $a->argv[2] === '@me') {
47                 $justme = true;
48         }
49         if ($a->argc > 3 && $a->argv[3] === '@all') {
50                 $justme = false;
51         }
52         if ($a->argc > 3 && $a->argv[3] === '@self') {
53                 $justme = true;
54         }
55         if ($a->argc > 4 && intval($a->argv[4]) && $justme == false) {
56                 $cid = intval($a->argv[4]);
57         }
58
59         if (! $system_mode AND ! $global) {
60                 $contacts = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
61                         where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
62                         dbesc($user)
63                 );
64                 if (! dbm::is_result($contacts) || $contacts[0]['hidewall'] || $contacts[0]['hide-friends']) {
65                         http_status_exit(404);
66                 }
67
68                 $user = $contacts[0];
69         }
70
71         if ($justme) {
72                 $sql_extra = " AND `contact`.`self` = 1 ";
73         }
74 //      else
75 //              $sql_extra = " AND `contact`.`self` = 0 ";
76
77         if ($cid) {
78                 $sql_extra = sprintf(" AND `contact`.`id` = %d ", intval($cid));
79         }
80         if (x($_GET, 'updatedSince')) {
81                 $update_limit = date("Y-m-d H:i:s", strtotime($_GET['updatedSince']));
82         }
83         if ($global) {
84                 $contacts = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure` AND NOT `hide` AND `network` IN ('%s', '%s', '%s')",
85                         dbesc($update_limit),
86                         dbesc(NETWORK_DFRN),
87                         dbesc(NETWORK_DIASPORA),
88                         dbesc(NETWORK_OSTATUS)
89                 );
90         } elseif ($system_mode) {
91                 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
92                         AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
93         } else {
94                 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
95                         AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
96                         AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
97                         intval($user['uid']),
98                         dbesc(NETWORK_DFRN),
99                         dbesc(NETWORK_DIASPORA),
100                         dbesc(NETWORK_OSTATUS),
101                         dbesc(NETWORK_STATUSNET)
102                 );
103         }
104         if (dbm::is_result($contacts)) {
105                 $totalResults = intval($contacts[0]['total']);
106         } else {
107                 $totalResults = 0;
108         }
109         $startIndex = intval($_GET['startIndex']);
110         if (! $startIndex) {
111                 $startIndex = 0;
112         }
113         $itemsPerPage = ((x($_GET, 'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
114
115         if ($global) {
116                 logger("Start global query", LOGGER_DEBUG);
117                 $contacts = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
118                         ORDER BY `updated` DESC LIMIT %d, %d",
119                         dbesc($update_limit),
120                         dbesc(NETWORK_DFRN),
121                         dbesc(NETWORK_DIASPORA),
122                         dbesc(NETWORK_OSTATUS),
123                         intval($startIndex),
124                         intval($itemsPerPage)
125                 );
126         } elseif ($system_mode) {
127                 logger("Start system mode query", LOGGER_DEBUG);
128                 $contacts = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`,
129                                 `profile`.`gender` AS `pgender`, `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`,
130                                 `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`, `user`.`account-type`
131                         FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
132                                 INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
133                         WHERE `self` = 1 AND `profile`.`is-default`
134                         AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
135                         intval($startIndex),
136                         intval($itemsPerPage)
137                 );
138         } else {
139                 logger("Start query for user " . $user['nickname'], LOGGER_DEBUG);
140                 $contacts = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
141                         AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
142                         AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
143                         intval($user['uid']),
144                         dbesc(NETWORK_DFRN),
145                         dbesc(NETWORK_DIASPORA),
146                         dbesc(NETWORK_OSTATUS),
147                         dbesc(NETWORK_STATUSNET),
148                         intval($startIndex),
149                         intval($itemsPerPage)
150                 );
151         }
152         logger("Query done", LOGGER_DEBUG);
153
154         $ret = array();
155         if (x($_GET, 'sorted')) {
156                 $ret['sorted'] = false;
157         }
158         if (x($_GET, 'filtered')) {
159                 $ret['filtered'] = false;
160         }
161         if (x($_GET, 'updatedSince') AND ! $global) {
162                 $ret['updatedSince'] = false;
163         }
164         $ret['startIndex']   = (int) $startIndex;
165         $ret['itemsPerPage'] = (int) $itemsPerPage;
166         $ret['totalResults'] = (int) $totalResults;
167         $ret['entry']        = array();
168
169
170         $fields_ret = array(
171                 'id' => false,
172                 'displayName' => false,
173                 'urls' => false,
174                 'updated' => false,
175                 'preferredUsername' => false,
176                 'photos' => false,
177                 'aboutMe' => false,
178                 'currentLocation' => false,
179                 'network' => false,
180                 'gender' => false,
181                 'tags' => false,
182                 'address' => false,
183                 'contactType' => false,
184                 'generation' => false
185         );
186
187         if ((! x($_GET, 'fields')) || ($_GET['fields'] === '@all')) {
188                 foreach ($fields_ret as $k => $v) {
189                         $fields_ret[$k] = true;
190                 }
191         } else {
192                 $fields_req = explode(',', $_GET['fields']);
193                 foreach ($fields_req as $f) {
194                         $fields_ret[trim($f)] = true;
195                 }
196         }
197
198         if (is_array($contacts)) {
199                 if (dbm::is_result($contacts)) {
200                         foreach ($contacts as $contact) {
201                                 if (! isset($contact['generation'])) {
202                                         if ($global) {
203                                                 $contact['generation'] = 3;
204                                         } elseif ($system_mode) {
205                                                 $contact['generation'] = 1;
206                                         } else {
207                                                 $contact['generation'] = 2;
208                                         }
209                                 }
210
211                                 if (($contact['about'] == "") AND isset($contact['pabout'])) {
212                                         $contact['about'] = $contact['pabout'];
213                                 }
214                                 if ($contact['location'] == "") {
215                                         if (isset($contact['plocation'])) {
216                                                 $contact['location'] = $contact['plocation'];
217                                         }
218                                         if (isset($contact['pregion']) AND ( $contact['pregion'] != "")) {
219                                                 if ($contact['location'] != "") {
220                                                         $contact['location'] .= ", ";
221                                                 }
222                                                 $contact['location'] .= $contact['pregion'];
223                                         }
224
225                                         if (isset($contact['pcountry']) AND ( $contact['pcountry'] != "")) {
226                                                 if ($contact['location'] != "") {
227                                                         $contact['location'] .= ", ";
228                                                 }
229                                                 $contact['location'] .= $contact['pcountry'];
230                                         }
231                                 }
232
233                                 if (($contact['gender'] == "") AND isset($contact['pgender'])) {
234                                         $contact['gender'] = $contact['pgender'];
235                                 }
236                                 if (($contact['keywords'] == "") AND isset($contact['pub_keywords'])) {
237                                         $contact['keywords'] = $contact['pub_keywords'];
238                                 }
239                                 if (isset($contact['account-type'])) {
240                                         $contact['contact-type'] = $contact['account-type'];
241                                 }
242                                 $about = Cache::get("about:" . $contact['updated'] . ":" . $contact['nurl']);
243                                 if (is_null($about)) {
244                                         $about = bbcode($contact['about'], false, false);
245                                         Cache::set("about:" . $contact['updated'] . ":" . $contact['nurl'], $about);
246                                 }
247
248                                 // Non connected persons can only see the keywords of a Diaspora account
249                                 if ($contact['network'] == NETWORK_DIASPORA) {
250                                         $contact['location'] = "";
251                                         $about = "";
252                                         $contact['gender'] = "";
253                                 }
254
255                                 $entry = array();
256                                 if ($fields_ret['id']) {
257                                         $entry['id'] = (int)$contact['id'];
258                                 }
259                                 if ($fields_ret['displayName']) {
260                                         $entry['displayName'] = $contact['name'];
261                                 }
262                                 if ($fields_ret['aboutMe']) {
263                                         $entry['aboutMe'] = $about;
264                                 }
265                                 if ($fields_ret['currentLocation']) {
266                                         $entry['currentLocation'] = $contact['location'];
267                                 }
268                                 if ($fields_ret['gender']) {
269                                         $entry['gender'] = $contact['gender'];
270                                 }
271                                 if ($fields_ret['generation']) {
272                                         $entry['generation'] = (int)$contact['generation'];
273                                 }
274                                 if ($fields_ret['urls']) {
275                                         $entry['urls'] = array(array('value' => $contact['url'], 'type' => 'profile'));
276                                         if ($contact['addr'] && ($contact['network'] !== NETWORK_MAIL)) {
277                                                 $entry['urls'][] = array('value' => 'acct:' . $contact['addr'], 'type' => 'webfinger');
278                                         }
279                                 }
280                                 if ($fields_ret['preferredUsername']) {
281                                         $entry['preferredUsername'] = $contact['nick'];
282                                 }
283                                 if ($fields_ret['updated']) {
284                                         if (! $global) {
285                                                 $entry['updated'] = $contact['success_update'];
286
287                                                 if ($contact['name-date'] > $entry['updated']) {
288                                                         $entry['updated'] = $contact['name-date'];
289                                                 }
290                                                 if ($contact['uri-date'] > $entry['updated']) {
291                                                         $entry['updated'] = $contact['uri-date'];
292                                                 }
293                                                 if ($contact['avatar-date'] > $entry['updated']) {
294                                                         $entry['updated'] = $contact['avatar-date'];
295                                                 }
296                                         } else {
297                                                 $entry['updated'] = $contact['updated'];
298                                         }
299                                         $entry['updated'] = date("c", strtotime($entry['updated']));
300                                 }
301                                 if ($fields_ret['photos']) {
302                                         $entry['photos'] = array(array('value' => $contact['photo'], 'type' => 'profile'));
303                                 }
304                                 if ($fields_ret['network']) {
305                                         $entry['network'] = $contact['network'];
306                                         if ($entry['network'] == NETWORK_STATUSNET) {
307                                                 $entry['network'] = NETWORK_OSTATUS;
308                                         }
309                                         if (($entry['network'] == "") AND ($contact['self'])) {
310                                                 $entry['network'] = NETWORK_DFRN;
311                                         }
312                                 }
313                                 if ($fields_ret['tags']) {
314                                         $tags = str_replace(",", " ", $contact['keywords']);
315                                         $tags = explode(" ", $tags);
316
317                                         $cleaned = array();
318                                         foreach ($tags as $tag) {
319                                                 $tag = trim(strtolower($tag));
320                                                 if ($tag != "") {
321                                                         $cleaned[] = $tag;
322                                                 }
323                                         }
324
325                                         $entry['tags'] = array($cleaned);
326                                 }
327                                 if ($fields_ret['address']) {
328                                         $entry['address'] = array();
329
330                                         // Deactivated. It just reveals too much data. (Although its from the default profile)
331                                         //if (isset($rr['paddress']))
332                                         //       $entry['address']['streetAddress'] = $rr['paddress'];
333
334                                         if (isset($contact['plocation'])) {
335                                                 $entry['address']['locality'] = $contact['plocation'];
336                                         }
337                                         if (isset($contact['pregion'])) {
338                                                 $entry['address']['region'] = $contact['pregion'];
339                                         }
340                                         // See above
341                                         //if (isset($rr['ppostalcode']))
342                                         //       $entry['address']['postalCode'] = $rr['ppostalcode'];
343
344                                         if (isset($contact['pcountry'])) {
345                                                 $entry['address']['country'] = $contact['pcountry'];
346                                         }
347                                 }
348
349                                 if ($fields_ret['contactType']) {
350                                         $entry['contactType'] = intval($contact['contact-type']);
351                                 }
352                                 $ret['entry'][] = $entry;
353                         }
354                 } else {
355                         $ret['entry'][] = array();
356                 }
357         } else {
358                 http_status_exit(500);
359         }
360         logger("End of poco", LOGGER_DEBUG);
361
362         if ($format === 'xml') {
363                 header('Content-type: text/xml');
364                 echo replace_macros(get_markup_template('poco_xml.tpl'), array_xmlify(array('$response' => $ret)));
365                 killme();
366         }
367         if ($format === 'json') {
368                 header('Content-type: application/json');
369                 echo json_encode($ret);
370                 killme();
371         } else {
372                 http_status_exit(500);
373         }
374 }