]> git.mxchange.org Git - friendica.git/blob - mod/poco.php
Remove unused global cache constants
[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 (! x($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 = (($_GET['format']) ? $_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         if ($a->argc > 1 && $a->argv[1] === '@global') {
47                 // List of all profiles that this server recently had data from
48                 $global = true;
49                 $update_limit = date(DateTimeFormat::MYSQL, time() - 30 * 86400);
50         }
51         if ($a->argc > 2 && $a->argv[2] === '@me') {
52                 $justme = true;
53         }
54         if ($a->argc > 3 && $a->argv[3] === '@all') {
55                 $justme = false;
56         }
57         if ($a->argc > 3 && $a->argv[3] === '@self') {
58                 $justme = true;
59         }
60         if ($a->argc > 4 && intval($a->argv[4]) && $justme == false) {
61                 $cid = intval($a->argv[4]);
62         }
63
64         if (! $system_mode && ! $global) {
65                 $users = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
66                         where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
67                         dbesc($user)
68                 );
69                 if (! DBM::is_result($users) || $users[0]['hidewall'] || $users[0]['hide-friends']) {
70                         System::httpExit(404);
71                 }
72
73                 $user = $users[0];
74         }
75
76         if ($justme) {
77                 $sql_extra = " AND `contact`.`self` = 1 ";
78         }
79 //      else
80 //              $sql_extra = " AND `contact`.`self` = 0 ";
81
82         if ($cid) {
83                 $sql_extra = sprintf(" AND `contact`.`id` = %d ", intval($cid));
84         }
85         if (x($_GET, 'updatedSince')) {
86                 $update_limit = date(DateTimeFormat::MYSQL, strtotime($_GET['updatedSince']));
87         }
88         if ($global) {
89                 $contacts = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure` AND NOT `hide` AND `network` IN ('%s', '%s', '%s')",
90                         dbesc($update_limit),
91                         dbesc(NETWORK_DFRN),
92                         dbesc(NETWORK_DIASPORA),
93                         dbesc(NETWORK_OSTATUS)
94                 );
95         } elseif ($system_mode) {
96                 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
97                         AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
98         } else {
99                 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
100                         AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
101                         AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
102                         intval($user['uid']),
103                         dbesc(NETWORK_DFRN),
104                         dbesc(NETWORK_DIASPORA),
105                         dbesc(NETWORK_OSTATUS),
106                         dbesc(NETWORK_STATUSNET)
107                 );
108         }
109         if (DBM::is_result($contacts)) {
110                 $totalResults = intval($contacts[0]['total']);
111         } else {
112                 $totalResults = 0;
113         }
114         $startIndex = intval($_GET['startIndex']);
115         if (! $startIndex) {
116                 $startIndex = 0;
117         }
118         $itemsPerPage = ((x($_GET, 'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
119
120         if ($global) {
121                 logger("Start global query", LOGGER_DEBUG);
122                 $contacts = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
123                         ORDER BY `updated` DESC LIMIT %d, %d",
124                         dbesc($update_limit),
125                         dbesc(NETWORK_DFRN),
126                         dbesc(NETWORK_DIASPORA),
127                         dbesc(NETWORK_OSTATUS),
128                         intval($startIndex),
129                         intval($itemsPerPage)
130                 );
131         } elseif ($system_mode) {
132                 logger("Start system mode query", LOGGER_DEBUG);
133                 $contacts = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`,
134                                 `profile`.`gender` AS `pgender`, `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`,
135                                 `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`, `user`.`account-type`
136                         FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
137                                 INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
138                         WHERE `self` = 1 AND `profile`.`is-default`
139                         AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
140                         intval($startIndex),
141                         intval($itemsPerPage)
142                 );
143         } else {
144                 logger("Start query for user " . $user['nickname'], LOGGER_DEBUG);
145                 $contacts = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
146                         AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
147                         AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
148                         intval($user['uid']),
149                         dbesc(NETWORK_DFRN),
150                         dbesc(NETWORK_DIASPORA),
151                         dbesc(NETWORK_OSTATUS),
152                         dbesc(NETWORK_STATUSNET),
153                         intval($startIndex),
154                         intval($itemsPerPage)
155                 );
156         }
157         logger("Query done", LOGGER_DEBUG);
158
159         $ret = [];
160         if (x($_GET, 'sorted')) {
161                 $ret['sorted'] = false;
162         }
163         if (x($_GET, 'filtered')) {
164                 $ret['filtered'] = false;
165         }
166         if (x($_GET, 'updatedSince') && ! $global) {
167                 $ret['updatedSince'] = false;
168         }
169         $ret['startIndex']   = (int) $startIndex;
170         $ret['itemsPerPage'] = (int) $itemsPerPage;
171         $ret['totalResults'] = (int) $totalResults;
172         $ret['entry']        = [];
173
174
175         $fields_ret = [
176                 'id' => false,
177                 'displayName' => false,
178                 'urls' => false,
179                 'updated' => false,
180                 'preferredUsername' => false,
181                 'photos' => false,
182                 'aboutMe' => false,
183                 'currentLocation' => false,
184                 'network' => false,
185                 'gender' => false,
186                 'tags' => false,
187                 'address' => false,
188                 'contactType' => false,
189                 'generation' => false
190         ];
191
192         if ((! x($_GET, 'fields')) || ($_GET['fields'] === '@all')) {
193                 foreach ($fields_ret as $k => $v) {
194                         $fields_ret[$k] = true;
195                 }
196         } else {
197                 $fields_req = explode(',', $_GET['fields']);
198                 foreach ($fields_req as $f) {
199                         $fields_ret[trim($f)] = true;
200                 }
201         }
202
203         if (is_array($contacts)) {
204                 if (DBM::is_result($contacts)) {
205                         foreach ($contacts as $contact) {
206                                 if (! isset($contact['generation'])) {
207                                         if ($global) {
208                                                 $contact['generation'] = 3;
209                                         } elseif ($system_mode) {
210                                                 $contact['generation'] = 1;
211                                         } else {
212                                                 $contact['generation'] = 2;
213                                         }
214                                 }
215
216                                 if (($contact['about'] == "") && isset($contact['pabout'])) {
217                                         $contact['about'] = $contact['pabout'];
218                                 }
219                                 if ($contact['location'] == "") {
220                                         if (isset($contact['plocation'])) {
221                                                 $contact['location'] = $contact['plocation'];
222                                         }
223                                         if (isset($contact['pregion']) && ( $contact['pregion'] != "")) {
224                                                 if ($contact['location'] != "") {
225                                                         $contact['location'] .= ", ";
226                                                 }
227                                                 $contact['location'] .= $contact['pregion'];
228                                         }
229
230                                         if (isset($contact['pcountry']) && ( $contact['pcountry'] != "")) {
231                                                 if ($contact['location'] != "") {
232                                                         $contact['location'] .= ", ";
233                                                 }
234                                                 $contact['location'] .= $contact['pcountry'];
235                                         }
236                                 }
237
238                                 if (($contact['gender'] == "") && isset($contact['pgender'])) {
239                                         $contact['gender'] = $contact['pgender'];
240                                 }
241                                 if (($contact['keywords'] == "") && isset($contact['pub_keywords'])) {
242                                         $contact['keywords'] = $contact['pub_keywords'];
243                                 }
244                                 if (isset($contact['account-type'])) {
245                                         $contact['contact-type'] = $contact['account-type'];
246                                 }
247                                 $about = Cache::get("about:" . $contact['updated'] . ":" . $contact['nurl']);
248                                 if (is_null($about)) {
249                                         $about = BBCode::convert($contact['about'], 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 }