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