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