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