2 // See here for a documentation for portable contacts:
3 // https://web.archive.org/web/20160405005550/http://portablecontacts.net/draft-spec.html
5 function poco_init(App $a) {
6 require_once("include/bbcode.php");
10 if (intval(get_config('system','block_public')) || (get_config('system','block_local_dir'))) {
11 http_status_exit(401);
15 $user = notags(trim($a->argv[1]));
18 $c = q("SELECT * FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1");
19 if (! dbm::is_result($c)) {
20 http_status_exit(401);
25 $format = (($_GET['format']) ? $_GET['format'] : 'json');
30 if ($a->argc > 1 && $a->argv[1] === '@server') {
31 // List of all servers that this server knows
32 $ret = poco_serverlist();
33 header('Content-type: application/json');
34 echo json_encode($ret);
37 if ($a->argc > 1 && $a->argv[1] === '@global') {
38 // List of all profiles that this server recently had data from
40 $update_limit = date("Y-m-d H:i:s", time() - 30 * 86400);
42 if ($a->argc > 2 && $a->argv[2] === '@me') {
45 if ($a->argc > 3 && $a->argv[3] === '@all') {
48 if ($a->argc > 3 && $a->argv[3] === '@self') {
51 if ($a->argc > 4 && intval($a->argv[4]) && $justme == false) {
52 $cid = intval($a->argv[4]);
55 if (!$system_mode AND !$global) {
56 $r = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
57 where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
60 if (! dbm::is_result($r) || $r[0]['hidewall'] || $r[0]['hide-friends']) {
61 http_status_exit(404);
68 $sql_extra = " AND `contact`.`self` = 1 ";
71 // $sql_extra = " AND `contact`.`self` = 0 ";
74 $sql_extra = sprintf(" AND `contact`.`id` = %d ",intval($cid));
76 if (x($_GET,'updatedSince')) {
77 $update_limit = date("Y-m-d H:i:s",strtotime($_GET['updatedSince']));
80 $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(NETWORK_DIASPORA),
84 dbesc(NETWORK_OSTATUS)
86 } elseif ($system_mode) {
87 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
88 AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
90 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
91 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
92 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
95 dbesc(NETWORK_DIASPORA),
96 dbesc(NETWORK_OSTATUS),
97 dbesc(NETWORK_STATUSNET)
100 if (dbm::is_result($r)) {
101 $totalResults = intval($r[0]['total']);
105 $startIndex = intval($_GET['startIndex']);
109 $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
112 logger("Start global query", LOGGER_DEBUG);
113 $r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
114 ORDER BY `updated` DESC LIMIT %d, %d",
115 dbesc($update_limit),
117 dbesc(NETWORK_DIASPORA),
118 dbesc(NETWORK_OSTATUS),
120 intval($itemsPerPage)
122 } elseif ($system_mode) {
123 logger("Start system mode query", LOGGER_DEBUG);
124 $r = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`,
125 `profile`.`gender` AS `pgender`, `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`,
126 `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`, `user`.`account-type`
127 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
128 INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
129 WHERE `self` = 1 AND `profile`.`is-default`
130 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
132 intval($itemsPerPage)
135 logger("Start query for user ".$user['nickname'], LOGGER_DEBUG);
136 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
137 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
138 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
139 intval($user['uid']),
141 dbesc(NETWORK_DIASPORA),
142 dbesc(NETWORK_OSTATUS),
143 dbesc(NETWORK_STATUSNET),
145 intval($itemsPerPage)
148 logger("Query done", LOGGER_DEBUG);
151 if (x($_GET,'sorted')) {
152 $ret['sorted'] = false;
154 if (x($_GET,'filtered')) {
155 $ret['filtered'] = false;
157 if (x($_GET,'updatedSince') AND !$global) {
158 $ret['updatedSince'] = false;
160 $ret['startIndex'] = (int) $startIndex;
161 $ret['itemsPerPage'] = (int) $itemsPerPage;
162 $ret['totalResults'] = (int) $totalResults;
163 $ret['entry'] = array();
168 'displayName' => false,
171 'preferredUsername' => false,
174 'currentLocation' => false,
179 'contactType' => false,
180 'generation' => false
183 if ((! x($_GET,'fields')) || ($_GET['fields'] === '@all')) {
184 foreach ($fields_ret as $k => $v) {
185 $fields_ret[$k] = true;
188 $fields_req = explode(',',$_GET['fields']);
189 foreach ($fields_req as $f) {
190 $fields_ret[trim($f)] = true;
195 if (dbm::is_result($r)) {
196 foreach ($r as $rr) {
197 if (!isset($rr['generation'])) {
199 $rr['generation'] = 3;
200 } elseif ($system_mode) {
201 $rr['generation'] = 1;
203 $rr['generation'] = 2;
207 if (($rr['about'] == "") AND isset($rr['pabout'])) {
208 $rr['about'] = $rr['pabout'];
210 if ($rr['location'] == "") {
211 if (isset($rr['plocation'])) {
212 $rr['location'] = $rr['plocation'];
214 if (isset($rr['pregion']) AND ($rr['pregion'] != "")) {
215 if ($rr['location'] != "") {
216 $rr['location'] .= ", ";
218 $rr['location'] .= $rr['pregion'];
221 if (isset($rr['pcountry']) AND ($rr['pcountry'] != "")) {
222 if ($rr['location'] != "") {
223 $rr['location'] .= ", ";
225 $rr['location'] .= $rr['pcountry'];
229 if (($rr['gender'] == "") AND isset($rr['pgender'])) {
230 $rr['gender'] = $rr['pgender'];
232 if (($rr['keywords'] == "") AND isset($rr['pub_keywords'])) {
233 $rr['keywords'] = $rr['pub_keywords'];
235 if (isset($rr['account-type'])) {
236 $rr['contact-type'] = $rr['account-type'];
238 $about = Cache::get("about:".$rr['updated'].":".$rr['nurl']);
239 if (is_null($about)) {
240 $about = bbcode($rr['about'], false, false);
241 Cache::set("about:".$rr['updated'].":".$rr['nurl'],$about);
244 // Non connected persons can only see the keywords of a Diaspora account
245 if ($rr['network'] == NETWORK_DIASPORA) {
246 $rr['location'] = "";
252 if ($fields_ret['id']) {
253 $entry['id'] = (int)$rr['id'];
255 if ($fields_ret['displayName']) {
256 $entry['displayName'] = $rr['name'];
258 if ($fields_ret['aboutMe']) {
259 $entry['aboutMe'] = $about;
261 if ($fields_ret['currentLocation']) {
262 $entry['currentLocation'] = $rr['location'];
264 if ($fields_ret['gender']) {
265 $entry['gender'] = $rr['gender'];
267 if ($fields_ret['generation']) {
268 $entry['generation'] = (int)$rr['generation'];
270 if ($fields_ret['urls']) {
271 $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
272 if ($rr['addr'] && ($rr['network'] !== NETWORK_MAIL)) {
273 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
276 if ($fields_ret['preferredUsername']) {
277 $entry['preferredUsername'] = $rr['nick'];
279 if ($fields_ret['updated']) {
281 $entry['updated'] = $rr['success_update'];
283 if ($rr['name-date'] > $entry['updated']) {
284 $entry['updated'] = $rr['name-date'];
286 if ($rr['uri-date'] > $entry['updated']) {
287 $entry['updated'] = $rr['uri-date'];
289 if ($rr['avatar-date'] > $entry['updated']) {
290 $entry['updated'] = $rr['avatar-date'];
293 $entry['updated'] = $rr['updated'];
295 $entry['updated'] = date("c", strtotime($entry['updated']));
297 if ($fields_ret['photos']) {
298 $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
300 if ($fields_ret['network']) {
301 $entry['network'] = $rr['network'];
302 if ($entry['network'] == NETWORK_STATUSNET) {
303 $entry['network'] = NETWORK_OSTATUS;
305 if (($entry['network'] == "") AND ($rr['self'])) {
306 $entry['network'] = NETWORK_DFRN;
309 if ($fields_ret['tags']) {
310 $tags = str_replace(","," ",$rr['keywords']);
311 $tags = explode(" ", $tags);
314 foreach ($tags as $tag) {
315 $tag = trim(strtolower($tag));
321 $entry['tags'] = array($cleaned);
323 if ($fields_ret['address']) {
324 $entry['address'] = array();
326 // Deactivated. It just reveals too much data. (Although its from the default profile)
327 //if (isset($rr['paddress']))
328 // $entry['address']['streetAddress'] = $rr['paddress'];
330 if (isset($rr['plocation'])) {
331 $entry['address']['locality'] = $rr['plocation'];
333 if (isset($rr['pregion'])) {
334 $entry['address']['region'] = $rr['pregion'];
337 //if (isset($rr['ppostalcode']))
338 // $entry['address']['postalCode'] = $rr['ppostalcode'];
340 if (isset($rr['pcountry'])) {
341 $entry['address']['country'] = $rr['pcountry'];
345 if ($fields_ret['contactType']) {
346 $entry['contactType'] = intval($rr['contact-type']);
348 $ret['entry'][] = $entry;
351 $ret['entry'][] = array();
354 http_status_exit(500);
356 logger("End of poco", LOGGER_DEBUG);
358 if ($format === 'xml') {
359 header('Content-type: text/xml');
360 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
363 if ($format === 'json') {
364 header('Content-type: application/json');
365 echo json_encode($ret);
368 http_status_exit(500);