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