2 // See here for a documentation for portable contacts:
3 // https://web.archive.org/web/20160405005550/http://portablecontacts.net/draft-spec.html
7 function poco_init(App $a) {
8 require_once("include/bbcode.php");
12 if (intval(get_config('system','block_public')) || (get_config('system','block_local_dir'))) {
13 http_status_exit(401);
17 $user = notags(trim($a->argv[1]));
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);
27 $format = (($_GET['format']) ? $_GET['format'] : 'json');
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);
39 if ($a->argc > 1 && $a->argv[1] === '@global') {
40 // List of all profiles that this server recently had data from
42 $update_limit = date("Y-m-d H:i:s", time() - 30 * 86400);
44 if ($a->argc > 2 && $a->argv[2] === '@me') {
47 if ($a->argc > 3 && $a->argv[3] === '@all') {
50 if ($a->argc > 3 && $a->argv[3] === '@self') {
53 if ($a->argc > 4 && intval($a->argv[4]) && $justme == false) {
54 $cid = intval($a->argv[4]);
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",
62 if (! dbm::is_result($r) || $r[0]['hidewall'] || $r[0]['hide-friends']) {
63 http_status_exit(404);
70 $sql_extra = " AND `contact`.`self` = 1 ";
73 // $sql_extra = " AND `contact`.`self` = 0 ";
76 $sql_extra = sprintf(" AND `contact`.`id` = %d ",intval($cid));
78 if (x($_GET,'updatedSince')) {
79 $update_limit = date("Y-m-d H:i:s",strtotime($_GET['updatedSince']));
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')",
85 dbesc(NETWORK_DIASPORA),
86 dbesc(NETWORK_OSTATUS)
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) ");
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",
97 dbesc(NETWORK_DIASPORA),
98 dbesc(NETWORK_OSTATUS),
99 dbesc(NETWORK_STATUSNET)
102 if (dbm::is_result($r)) {
103 $totalResults = intval($r[0]['total']);
107 $startIndex = intval($_GET['startIndex']);
111 $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
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),
119 dbesc(NETWORK_DIASPORA),
120 dbesc(NETWORK_OSTATUS),
122 intval($itemsPerPage)
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",
134 intval($itemsPerPage)
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']),
143 dbesc(NETWORK_DIASPORA),
144 dbesc(NETWORK_OSTATUS),
145 dbesc(NETWORK_STATUSNET),
147 intval($itemsPerPage)
150 logger("Query done", LOGGER_DEBUG);
153 if (x($_GET,'sorted')) {
154 $ret['sorted'] = false;
156 if (x($_GET,'filtered')) {
157 $ret['filtered'] = false;
159 if (x($_GET,'updatedSince') AND !$global) {
160 $ret['updatedSince'] = false;
162 $ret['startIndex'] = (int) $startIndex;
163 $ret['itemsPerPage'] = (int) $itemsPerPage;
164 $ret['totalResults'] = (int) $totalResults;
165 $ret['entry'] = array();
170 'displayName' => false,
173 'preferredUsername' => false,
176 'currentLocation' => false,
181 'contactType' => false,
182 'generation' => false
185 if ((! x($_GET,'fields')) || ($_GET['fields'] === '@all')) {
186 foreach ($fields_ret as $k => $v) {
187 $fields_ret[$k] = true;
190 $fields_req = explode(',',$_GET['fields']);
191 foreach ($fields_req as $f) {
192 $fields_ret[trim($f)] = true;
197 if (dbm::is_result($r)) {
198 foreach ($r as $rr) {
199 if (!isset($rr['generation'])) {
201 $rr['generation'] = 3;
202 } elseif ($system_mode) {
203 $rr['generation'] = 1;
205 $rr['generation'] = 2;
209 if (($rr['about'] == "") AND isset($rr['pabout'])) {
210 $rr['about'] = $rr['pabout'];
212 if ($rr['location'] == "") {
213 if (isset($rr['plocation'])) {
214 $rr['location'] = $rr['plocation'];
216 if (isset($rr['pregion']) AND ($rr['pregion'] != "")) {
217 if ($rr['location'] != "") {
218 $rr['location'] .= ", ";
220 $rr['location'] .= $rr['pregion'];
223 if (isset($rr['pcountry']) AND ($rr['pcountry'] != "")) {
224 if ($rr['location'] != "") {
225 $rr['location'] .= ", ";
227 $rr['location'] .= $rr['pcountry'];
231 if (($rr['gender'] == "") AND isset($rr['pgender'])) {
232 $rr['gender'] = $rr['pgender'];
234 if (($rr['keywords'] == "") AND isset($rr['pub_keywords'])) {
235 $rr['keywords'] = $rr['pub_keywords'];
237 if (isset($rr['account-type'])) {
238 $rr['contact-type'] = $rr['account-type'];
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);
246 // Non connected persons can only see the keywords of a Diaspora account
247 if ($rr['network'] == NETWORK_DIASPORA) {
248 $rr['location'] = "";
254 if ($fields_ret['id']) {
255 $entry['id'] = (int)$rr['id'];
257 if ($fields_ret['displayName']) {
258 $entry['displayName'] = $rr['name'];
260 if ($fields_ret['aboutMe']) {
261 $entry['aboutMe'] = $about;
263 if ($fields_ret['currentLocation']) {
264 $entry['currentLocation'] = $rr['location'];
266 if ($fields_ret['gender']) {
267 $entry['gender'] = $rr['gender'];
269 if ($fields_ret['generation']) {
270 $entry['generation'] = (int)$rr['generation'];
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');
278 if ($fields_ret['preferredUsername']) {
279 $entry['preferredUsername'] = $rr['nick'];
281 if ($fields_ret['updated']) {
283 $entry['updated'] = $rr['success_update'];
285 if ($rr['name-date'] > $entry['updated']) {
286 $entry['updated'] = $rr['name-date'];
288 if ($rr['uri-date'] > $entry['updated']) {
289 $entry['updated'] = $rr['uri-date'];
291 if ($rr['avatar-date'] > $entry['updated']) {
292 $entry['updated'] = $rr['avatar-date'];
295 $entry['updated'] = $rr['updated'];
297 $entry['updated'] = date("c", strtotime($entry['updated']));
299 if ($fields_ret['photos']) {
300 $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
302 if ($fields_ret['network']) {
303 $entry['network'] = $rr['network'];
304 if ($entry['network'] == NETWORK_STATUSNET) {
305 $entry['network'] = NETWORK_OSTATUS;
307 if (($entry['network'] == "") AND ($rr['self'])) {
308 $entry['network'] = NETWORK_DFRN;
311 if ($fields_ret['tags']) {
312 $tags = str_replace(","," ",$rr['keywords']);
313 $tags = explode(" ", $tags);
316 foreach ($tags as $tag) {
317 $tag = trim(strtolower($tag));
323 $entry['tags'] = array($cleaned);
325 if ($fields_ret['address']) {
326 $entry['address'] = array();
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'];
332 if (isset($rr['plocation'])) {
333 $entry['address']['locality'] = $rr['plocation'];
335 if (isset($rr['pregion'])) {
336 $entry['address']['region'] = $rr['pregion'];
339 //if (isset($rr['ppostalcode']))
340 // $entry['address']['postalCode'] = $rr['ppostalcode'];
342 if (isset($rr['pcountry'])) {
343 $entry['address']['country'] = $rr['pcountry'];
347 if ($fields_ret['contactType']) {
348 $entry['contactType'] = intval($rr['contact-type']);
350 $ret['entry'][] = $entry;
353 $ret['entry'][] = array();
356 http_status_exit(500);
358 logger("End of poco", LOGGER_DEBUG);
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)));
365 if ($format === 'json') {
366 header('Content-type: application/json');
367 echo json_encode($ret);
370 http_status_exit(500);