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] === '@global') {
32 $update_limit = date("Y-m-d H:i:s", time() - 30 * 86400);
34 if($a->argc > 2 && $a->argv[2] === '@me')
36 if($a->argc > 3 && $a->argv[3] === '@all')
38 if($a->argc > 3 && $a->argv[3] === '@self')
40 if($a->argc > 4 && intval($a->argv[4]) && $justme == false)
41 $cid = intval($a->argv[4]);
44 if(!$system_mode AND !$global) {
45 $r = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
46 where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
49 if(! dbm::is_result($r) || $r[0]['hidewall'] || $r[0]['hide-friends'])
50 http_status_exit(404);
56 $sql_extra = " AND `contact`.`self` = 1 ";
58 // $sql_extra = " AND `contact`.`self` = 0 ";
61 $sql_extra = sprintf(" AND `contact`.`id` = %d ",intval($cid));
63 if(x($_GET,'updatedSince'))
64 $update_limit = date("Y-m-d H:i:s",strtotime($_GET['updatedSince']));
67 $r = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure` AND NOT `hide` AND `network` IN ('%s', '%s', '%s')",
70 dbesc(NETWORK_DIASPORA),
71 dbesc(NETWORK_OSTATUS)
73 } elseif($system_mode) {
74 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
75 AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
77 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
78 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
79 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
82 dbesc(NETWORK_DIASPORA),
83 dbesc(NETWORK_OSTATUS),
84 dbesc(NETWORK_STATUSNET)
87 if (dbm::is_result($r))
88 $totalResults = intval($r[0]['total']);
92 $startIndex = intval($_GET['startIndex']);
95 $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
98 logger("Start global query", LOGGER_DEBUG);
99 $r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
100 ORDER BY `updated` DESC LIMIT %d, %d",
101 dbesc($update_limit),
103 dbesc(NETWORK_DIASPORA),
104 dbesc(NETWORK_OSTATUS),
106 intval($itemsPerPage)
108 } elseif($system_mode) {
109 logger("Start system mode query", LOGGER_DEBUG);
110 $r = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`,
111 `profile`.`gender` AS `pgender`, `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`,
112 `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`, `user`.`account-type`
113 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
114 INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
115 WHERE `self` = 1 AND `profile`.`is-default`
116 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
118 intval($itemsPerPage)
121 logger("Start query for user ".$user['nickname'], LOGGER_DEBUG);
122 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
123 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
124 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
125 intval($user['uid']),
127 dbesc(NETWORK_DIASPORA),
128 dbesc(NETWORK_OSTATUS),
129 dbesc(NETWORK_STATUSNET),
131 intval($itemsPerPage)
134 logger("Query done", LOGGER_DEBUG);
137 if(x($_GET,'sorted'))
138 $ret['sorted'] = false;
139 if(x($_GET,'filtered'))
140 $ret['filtered'] = false;
141 if(x($_GET,'updatedSince') AND !$global)
142 $ret['updatedSince'] = false;
144 $ret['startIndex'] = (int) $startIndex;
145 $ret['itemsPerPage'] = (int) $itemsPerPage;
146 $ret['totalResults'] = (int) $totalResults;
147 $ret['entry'] = array();
152 'displayName' => false,
155 'preferredUsername' => false,
158 'currentLocation' => false,
163 'contactType' => false,
164 'generation' => false
167 if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
168 foreach($fields_ret as $k => $v)
169 $fields_ret[$k] = true;
171 $fields_req = explode(',',$_GET['fields']);
172 foreach($fields_req as $f)
173 $fields_ret[trim($f)] = true;
177 if (dbm::is_result($r)) {
178 foreach ($r as $rr) {
179 if (!isset($rr['generation'])) {
181 $rr['generation'] = 3;
182 elseif ($system_mode)
183 $rr['generation'] = 1;
185 $rr['generation'] = 2;
188 if (($rr['about'] == "") AND isset($rr['pabout']))
189 $rr['about'] = $rr['pabout'];
191 if ($rr['location'] == "") {
192 if (isset($rr['plocation']))
193 $rr['location'] = $rr['plocation'];
195 if (isset($rr['pregion']) AND ($rr['pregion'] != "")) {
196 if ($rr['location'] != "")
197 $rr['location'] .= ", ";
199 $rr['location'] .= $rr['pregion'];
202 if (isset($rr['pcountry']) AND ($rr['pcountry'] != "")) {
203 if ($rr['location'] != "")
204 $rr['location'] .= ", ";
206 $rr['location'] .= $rr['pcountry'];
210 if (($rr['gender'] == "") AND isset($rr['pgender']))
211 $rr['gender'] = $rr['pgender'];
213 if (($rr['keywords'] == "") AND isset($rr['pub_keywords']))
214 $rr['keywords'] = $rr['pub_keywords'];
216 if (isset($rr['account-type']))
217 $rr['contact-type'] = $rr['account-type'];
219 $about = Cache::get("about:".$rr['updated'].":".$rr['nurl']);
220 if (is_null($about)) {
221 $about = bbcode($rr['about'], false, false);
222 Cache::set("about:".$rr['updated'].":".$rr['nurl'],$about);
225 // Non connected persons can only see the keywords of a Diaspora account
226 if ($rr['network'] == NETWORK_DIASPORA) {
227 $rr['location'] = "";
233 if($fields_ret['id'])
234 $entry['id'] = (int)$rr['id'];
235 if($fields_ret['displayName'])
236 $entry['displayName'] = $rr['name'];
237 if($fields_ret['aboutMe'])
238 $entry['aboutMe'] = $about;
239 if($fields_ret['currentLocation'])
240 $entry['currentLocation'] = $rr['location'];
241 if($fields_ret['gender'])
242 $entry['gender'] = $rr['gender'];
243 if($fields_ret['generation'])
244 $entry['generation'] = (int)$rr['generation'];
245 if($fields_ret['urls']) {
246 $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
247 if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
248 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
250 if($fields_ret['preferredUsername'])
251 $entry['preferredUsername'] = $rr['nick'];
252 if($fields_ret['updated']) {
254 $entry['updated'] = $rr['success_update'];
256 if ($rr['name-date'] > $entry['updated'])
257 $entry['updated'] = $rr['name-date'];
259 if ($rr['uri-date'] > $entry['updated'])
260 $entry['updated'] = $rr['uri-date'];
262 if ($rr['avatar-date'] > $entry['updated'])
263 $entry['updated'] = $rr['avatar-date'];
265 $entry['updated'] = $rr['updated'];
267 $entry['updated'] = date("c", strtotime($entry['updated']));
269 if($fields_ret['photos'])
270 $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
271 if($fields_ret['network']) {
272 $entry['network'] = $rr['network'];
273 if ($entry['network'] == NETWORK_STATUSNET)
274 $entry['network'] = NETWORK_OSTATUS;
275 if (($entry['network'] == "") AND ($rr['self']))
276 $entry['network'] = NETWORK_DFRN;
278 if($fields_ret['tags']) {
279 $tags = str_replace(","," ",$rr['keywords']);
280 $tags = explode(" ", $tags);
283 foreach ($tags as $tag) {
284 $tag = trim(strtolower($tag));
289 $entry['tags'] = array($cleaned);
291 if($fields_ret['address']) {
292 $entry['address'] = array();
294 // Deactivated. It just reveals too much data. (Although its from the default profile)
295 //if (isset($rr['paddress']))
296 // $entry['address']['streetAddress'] = $rr['paddress'];
298 if (isset($rr['plocation']))
299 $entry['address']['locality'] = $rr['plocation'];
301 if (isset($rr['pregion']))
302 $entry['address']['region'] = $rr['pregion'];
305 //if (isset($rr['ppostalcode']))
306 // $entry['address']['postalCode'] = $rr['ppostalcode'];
308 if (isset($rr['pcountry']))
309 $entry['address']['country'] = $rr['pcountry'];
312 if($fields_ret['contactType'])
313 $entry['contactType'] = intval($rr['contact-type']);
315 $ret['entry'][] = $entry;
319 $ret['entry'][] = array();
322 http_status_exit(500);
324 logger("End of poco", LOGGER_DEBUG);
326 if($format === 'xml') {
327 header('Content-type: text/xml');
328 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
331 if($format === 'json') {
332 header('Content-type: application/json');
333 echo json_encode($ret);
337 http_status_exit(500);