]> git.mxchange.org Git - friendica.git/blob - mod/poco.php
Merge pull request #3380 from fabrixxm/feature/frio/fixedaside2
[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 use Friendica\Core\Config;
8
9 function poco_init(App $a) {
10         $system_mode = false;
11
12         if (intval(Config::get('system', 'block_public')) || (Config::get('system', 'block_local_dir'))) {
13                 http_status_exit(401);
14         }
15
16         if ($a->argc > 1) {
17                 $user = notags(trim($a->argv[1]));
18         }
19         if (! x($user)) {
20                 $c = q("SELECT * FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1");
21                 if (! dbm::is_result($c)) {
22                         http_status_exit(401);
23                 }
24                 $system_mode = true;
25         }
26
27         $format = (($_GET['format']) ? $_GET['format'] : 'json');
28
29         $justme = false;
30         $global = false;
31
32         if ($a->argc > 1 && $a->argv[1] === '@server') {
33                 require_once 'include/socgraph.php';
34                 // List of all servers that this server knows
35                 $ret = poco_serverlist();
36                 header('Content-type: application/json');
37                 echo json_encode($ret);
38                 killme();
39         }
40         if ($a->argc > 1 && $a->argv[1] === '@global') {
41                 // List of all profiles that this server recently had data from
42                 $global = true;
43                 $update_limit = date("Y-m-d H:i:s", time() - 30 * 86400);
44         }
45         if ($a->argc > 2 && $a->argv[2] === '@me') {
46                 $justme = true;
47         }
48         if ($a->argc > 3 && $a->argv[3] === '@all') {
49                 $justme = false;
50         }
51         if ($a->argc > 3 && $a->argv[3] === '@self') {
52                 $justme = true;
53         }
54         if ($a->argc > 4 && intval($a->argv[4]) && $justme == false) {
55                 $cid = intval($a->argv[4]);
56         }
57
58         if (! $system_mode AND ! $global) {
59                 $users = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
60                         where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
61                         dbesc($user)
62                 );
63                 if (! dbm::is_result($users) || $users[0]['hidewall'] || $users[0]['hide-friends']) {
64                         http_status_exit(404);
65                 }
66
67                 $user = $users[0];
68         }
69
70         if ($justme) {
71                 $sql_extra = " AND `contact`.`self` = 1 ";
72         }
73 //      else
74 //              $sql_extra = " AND `contact`.`self` = 0 ";
75
76         if ($cid) {
77                 $sql_extra = sprintf(" AND `contact`.`id` = %d ", intval($cid));
78         }
79         if (x($_GET, 'updatedSince')) {
80                 $update_limit = date("Y-m-d H:i:s", strtotime($_GET['updatedSince']));
81         }
82         if ($global) {
83                 $contacts = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure` AND NOT `hide` AND `network` IN ('%s', '%s', '%s')",
84                         dbesc($update_limit),
85                         dbesc(NETWORK_DFRN),
86                         dbesc(NETWORK_DIASPORA),
87                         dbesc(NETWORK_OSTATUS)
88                 );
89         } elseif ($system_mode) {
90                 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
91                         AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
92         } else {
93                 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
94                         AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
95                         AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
96                         intval($user['uid']),
97                         dbesc(NETWORK_DFRN),
98                         dbesc(NETWORK_DIASPORA),
99                         dbesc(NETWORK_OSTATUS),
100                         dbesc(NETWORK_STATUSNET)
101                 );
102         }
103         if (dbm::is_result($contacts)) {
104                 $totalResults = intval($contacts[0]['total']);
105         } else {
106                 $totalResults = 0;
107         }
108         $startIndex = intval($_GET['startIndex']);
109         if (! $startIndex) {
110                 $startIndex = 0;
111         }
112         $itemsPerPage = ((x($_GET, 'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
113
114         if ($global) {
115                 logger("Start global query", LOGGER_DEBUG);
116                 $contacts = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
117                         ORDER BY `updated` DESC LIMIT %d, %d",
118                         dbesc($update_limit),
119                         dbesc(NETWORK_DFRN),
120                         dbesc(NETWORK_DIASPORA),
121                         dbesc(NETWORK_OSTATUS),
122                         intval($startIndex),
123                         intval($itemsPerPage)
124                 );
125         } elseif ($system_mode) {
126                 logger("Start system mode query", LOGGER_DEBUG);
127                 $contacts = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`,
128                                 `profile`.`gender` AS `pgender`, `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`,
129                                 `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`, `user`.`account-type`
130                         FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
131                                 INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
132                         WHERE `self` = 1 AND `profile`.`is-default`
133                         AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
134                         intval($startIndex),
135                         intval($itemsPerPage)
136                 );
137         } else {
138                 logger("Start query for user " . $user['nickname'], LOGGER_DEBUG);
139                 $contacts = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
140                         AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
141                         AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
142                         intval($user['uid']),
143                         dbesc(NETWORK_DFRN),
144                         dbesc(NETWORK_DIASPORA),
145                         dbesc(NETWORK_OSTATUS),
146                         dbesc(NETWORK_STATUSNET),
147                         intval($startIndex),
148                         intval($itemsPerPage)
149                 );
150         }
151         logger("Query done", LOGGER_DEBUG);
152
153         $ret = array();
154         if (x($_GET, 'sorted')) {
155                 $ret['sorted'] = false;
156         }
157         if (x($_GET, 'filtered')) {
158                 $ret['filtered'] = false;
159         }
160         if (x($_GET, 'updatedSince') AND ! $global) {
161                 $ret['updatedSince'] = false;
162         }
163         $ret['startIndex']   = (int) $startIndex;
164         $ret['itemsPerPage'] = (int) $itemsPerPage;
165         $ret['totalResults'] = (int) $totalResults;
166         $ret['entry']        = array();
167
168
169         $fields_ret = array(
170                 'id' => false,
171                 'displayName' => false,
172                 'urls' => false,
173                 'updated' => false,
174                 'preferredUsername' => false,
175                 'photos' => false,
176                 'aboutMe' => false,
177                 'currentLocation' => false,
178                 'network' => false,
179                 'gender' => false,
180                 'tags' => false,
181                 'address' => false,
182                 'contactType' => false,
183                 'generation' => false
184         );
185
186         if ((! x($_GET, 'fields')) || ($_GET['fields'] === '@all')) {
187                 foreach ($fields_ret as $k => $v) {
188                         $fields_ret[$k] = true;
189                 }
190         } else {
191                 $fields_req = explode(',', $_GET['fields']);
192                 foreach ($fields_req as $f) {
193                         $fields_ret[trim($f)] = true;
194                 }
195         }
196
197         if (is_array($contacts)) {
198                 if (dbm::is_result($contacts)) {
199                         foreach ($contacts as $contact) {
200                                 if (! isset($contact['generation'])) {
201                                         if ($global) {
202                                                 $contact['generation'] = 3;
203                                         } elseif ($system_mode) {
204                                                 $contact['generation'] = 1;
205                                         } else {
206                                                 $contact['generation'] = 2;
207                                         }
208                                 }
209
210                                 if (($contact['about'] == "") AND isset($contact['pabout'])) {
211                                         $contact['about'] = $contact['pabout'];
212                                 }
213                                 if ($contact['location'] == "") {
214                                         if (isset($contact['plocation'])) {
215                                                 $contact['location'] = $contact['plocation'];
216                                         }
217                                         if (isset($contact['pregion']) AND ( $contact['pregion'] != "")) {
218                                                 if ($contact['location'] != "") {
219                                                         $contact['location'] .= ", ";
220                                                 }
221                                                 $contact['location'] .= $contact['pregion'];
222                                         }
223
224                                         if (isset($contact['pcountry']) AND ( $contact['pcountry'] != "")) {
225                                                 if ($contact['location'] != "") {
226                                                         $contact['location'] .= ", ";
227                                                 }
228                                                 $contact['location'] .= $contact['pcountry'];
229                                         }
230                                 }
231
232                                 if (($contact['gender'] == "") AND isset($contact['pgender'])) {
233                                         $contact['gender'] = $contact['pgender'];
234                                 }
235                                 if (($contact['keywords'] == "") AND isset($contact['pub_keywords'])) {
236                                         $contact['keywords'] = $contact['pub_keywords'];
237                                 }
238                                 if (isset($contact['account-type'])) {
239                                         $contact['contact-type'] = $contact['account-type'];
240                                 }
241                                 $about = Cache::get("about:" . $contact['updated'] . ":" . $contact['nurl']);
242                                 if (is_null($about)) {
243                                         require_once 'include/bbcode.php';
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 }