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