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