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 $ret = poco_serverlist();
32 header('Content-type: application/json');
33 echo json_encode($ret);
36 if($a->argc > 1 && $a->argv[1] === '@global') {
38 $update_limit = date("Y-m-d H:i:s", time() - 30 * 86400);
40 if($a->argc > 2 && $a->argv[2] === '@me')
42 if($a->argc > 3 && $a->argv[3] === '@all')
44 if($a->argc > 3 && $a->argv[3] === '@self')
46 if($a->argc > 4 && intval($a->argv[4]) && $justme == false)
47 $cid = intval($a->argv[4]);
50 if(!$system_mode AND !$global) {
51 $r = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
52 where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
55 if(! dbm::is_result($r) || $r[0]['hidewall'] || $r[0]['hide-friends'])
56 http_status_exit(404);
62 $sql_extra = " AND `contact`.`self` = 1 ";
64 // $sql_extra = " AND `contact`.`self` = 0 ";
67 $sql_extra = sprintf(" AND `contact`.`id` = %d ",intval($cid));
69 if(x($_GET,'updatedSince'))
70 $update_limit = date("Y-m-d H:i:s",strtotime($_GET['updatedSince']));
73 $r = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure` AND NOT `hide` AND `network` IN ('%s', '%s', '%s')",
76 dbesc(NETWORK_DIASPORA),
77 dbesc(NETWORK_OSTATUS)
79 } elseif($system_mode) {
80 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
81 AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
83 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
84 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
85 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
88 dbesc(NETWORK_DIASPORA),
89 dbesc(NETWORK_OSTATUS),
90 dbesc(NETWORK_STATUSNET)
93 if (dbm::is_result($r))
94 $totalResults = intval($r[0]['total']);
98 $startIndex = intval($_GET['startIndex']);
101 $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
104 logger("Start global query", LOGGER_DEBUG);
105 $r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
106 ORDER BY `updated` DESC LIMIT %d, %d",
107 dbesc($update_limit),
109 dbesc(NETWORK_DIASPORA),
110 dbesc(NETWORK_OSTATUS),
112 intval($itemsPerPage)
114 } elseif($system_mode) {
115 logger("Start system mode query", LOGGER_DEBUG);
116 $r = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`,
117 `profile`.`gender` AS `pgender`, `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`,
118 `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`, `user`.`account-type`
119 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
120 INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
121 WHERE `self` = 1 AND `profile`.`is-default`
122 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
124 intval($itemsPerPage)
127 logger("Start query for user ".$user['nickname'], LOGGER_DEBUG);
128 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
129 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
130 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
131 intval($user['uid']),
133 dbesc(NETWORK_DIASPORA),
134 dbesc(NETWORK_OSTATUS),
135 dbesc(NETWORK_STATUSNET),
137 intval($itemsPerPage)
140 logger("Query done", LOGGER_DEBUG);
143 if(x($_GET,'sorted'))
144 $ret['sorted'] = false;
145 if(x($_GET,'filtered'))
146 $ret['filtered'] = false;
147 if(x($_GET,'updatedSince') AND !$global)
148 $ret['updatedSince'] = false;
150 $ret['startIndex'] = (int) $startIndex;
151 $ret['itemsPerPage'] = (int) $itemsPerPage;
152 $ret['totalResults'] = (int) $totalResults;
153 $ret['entry'] = array();
158 'displayName' => false,
161 'preferredUsername' => false,
164 'currentLocation' => false,
169 'contactType' => false,
170 'generation' => false
173 if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
174 foreach($fields_ret as $k => $v)
175 $fields_ret[$k] = true;
177 $fields_req = explode(',',$_GET['fields']);
178 foreach($fields_req as $f)
179 $fields_ret[trim($f)] = true;
183 if (dbm::is_result($r)) {
184 foreach ($r as $rr) {
185 if (!isset($rr['generation'])) {
187 $rr['generation'] = 3;
188 elseif ($system_mode)
189 $rr['generation'] = 1;
191 $rr['generation'] = 2;
194 if (($rr['about'] == "") AND isset($rr['pabout']))
195 $rr['about'] = $rr['pabout'];
197 if ($rr['location'] == "") {
198 if (isset($rr['plocation']))
199 $rr['location'] = $rr['plocation'];
201 if (isset($rr['pregion']) AND ($rr['pregion'] != "")) {
202 if ($rr['location'] != "")
203 $rr['location'] .= ", ";
205 $rr['location'] .= $rr['pregion'];
208 if (isset($rr['pcountry']) AND ($rr['pcountry'] != "")) {
209 if ($rr['location'] != "")
210 $rr['location'] .= ", ";
212 $rr['location'] .= $rr['pcountry'];
216 if (($rr['gender'] == "") AND isset($rr['pgender']))
217 $rr['gender'] = $rr['pgender'];
219 if (($rr['keywords'] == "") AND isset($rr['pub_keywords']))
220 $rr['keywords'] = $rr['pub_keywords'];
222 if (isset($rr['account-type']))
223 $rr['contact-type'] = $rr['account-type'];
225 $about = Cache::get("about:".$rr['updated'].":".$rr['nurl']);
226 if (is_null($about)) {
227 $about = bbcode($rr['about'], false, false);
228 Cache::set("about:".$rr['updated'].":".$rr['nurl'],$about);
231 // Non connected persons can only see the keywords of a Diaspora account
232 if ($rr['network'] == NETWORK_DIASPORA) {
233 $rr['location'] = "";
239 if($fields_ret['id'])
240 $entry['id'] = (int)$rr['id'];
241 if($fields_ret['displayName'])
242 $entry['displayName'] = $rr['name'];
243 if($fields_ret['aboutMe'])
244 $entry['aboutMe'] = $about;
245 if($fields_ret['currentLocation'])
246 $entry['currentLocation'] = $rr['location'];
247 if($fields_ret['gender'])
248 $entry['gender'] = $rr['gender'];
249 if($fields_ret['generation'])
250 $entry['generation'] = (int)$rr['generation'];
251 if($fields_ret['urls']) {
252 $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
253 if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
254 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
256 if($fields_ret['preferredUsername'])
257 $entry['preferredUsername'] = $rr['nick'];
258 if($fields_ret['updated']) {
260 $entry['updated'] = $rr['success_update'];
262 if ($rr['name-date'] > $entry['updated'])
263 $entry['updated'] = $rr['name-date'];
265 if ($rr['uri-date'] > $entry['updated'])
266 $entry['updated'] = $rr['uri-date'];
268 if ($rr['avatar-date'] > $entry['updated'])
269 $entry['updated'] = $rr['avatar-date'];
271 $entry['updated'] = $rr['updated'];
273 $entry['updated'] = date("c", strtotime($entry['updated']));
275 if($fields_ret['photos'])
276 $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
277 if($fields_ret['network']) {
278 $entry['network'] = $rr['network'];
279 if ($entry['network'] == NETWORK_STATUSNET)
280 $entry['network'] = NETWORK_OSTATUS;
281 if (($entry['network'] == "") AND ($rr['self']))
282 $entry['network'] = NETWORK_DFRN;
284 if($fields_ret['tags']) {
285 $tags = str_replace(","," ",$rr['keywords']);
286 $tags = explode(" ", $tags);
289 foreach ($tags as $tag) {
290 $tag = trim(strtolower($tag));
295 $entry['tags'] = array($cleaned);
297 if($fields_ret['address']) {
298 $entry['address'] = array();
300 // Deactivated. It just reveals too much data. (Although its from the default profile)
301 //if (isset($rr['paddress']))
302 // $entry['address']['streetAddress'] = $rr['paddress'];
304 if (isset($rr['plocation']))
305 $entry['address']['locality'] = $rr['plocation'];
307 if (isset($rr['pregion']))
308 $entry['address']['region'] = $rr['pregion'];
311 //if (isset($rr['ppostalcode']))
312 // $entry['address']['postalCode'] = $rr['ppostalcode'];
314 if (isset($rr['pcountry']))
315 $entry['address']['country'] = $rr['pcountry'];
318 if($fields_ret['contactType'])
319 $entry['contactType'] = intval($rr['contact-type']);
321 $ret['entry'][] = $entry;
324 $ret['entry'][] = array();
326 http_status_exit(500);
328 logger("End of poco", LOGGER_DEBUG);
330 if($format === 'xml') {
331 header('Content-type: text/xml');
332 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
335 if($format === 'json') {
336 header('Content-type: application/json');
337 echo json_encode($ret);
340 http_status_exit(500);