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