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