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