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");
20 http_status_exit(401);
24 $format = (($_GET['format']) ? $_GET['format'] : 'json');
29 if($a->argc > 1 && $a->argv[1] === '@global') {
31 $update_limit = date("Y-m-d H:i:s", time() - 30 * 86400);
33 if($a->argc > 2 && $a->argv[2] === '@me')
35 if($a->argc > 3 && $a->argv[3] === '@all')
37 if($a->argc > 3 && $a->argv[3] === '@self')
39 if($a->argc > 4 && intval($a->argv[4]) && $justme == false)
40 $cid = intval($a->argv[4]);
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",
48 if(! dbm::is_result($r) || $r[0]['hidewall'] || $r[0]['hide-friends'])
49 http_status_exit(404);
55 $sql_extra = " AND `contact`.`self` = 1 ";
57 // $sql_extra = " AND `contact`.`self` = 0 ";
60 $sql_extra = sprintf(" AND `contact`.`id` = %d ",intval($cid));
62 if(x($_GET,'updatedSince'))
63 $update_limit = date("Y-m-d H:i:s",strtotime($_GET['updatedSince']));
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')",
69 dbesc(NETWORK_DIASPORA),
70 dbesc(NETWORK_OSTATUS)
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) ");
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",
81 dbesc(NETWORK_DIASPORA),
82 dbesc(NETWORK_OSTATUS),
83 dbesc(NETWORK_STATUSNET)
86 if (dbm::is_result($r))
87 $totalResults = intval($r[0]['total']);
91 $startIndex = intval($_GET['startIndex']);
94 $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
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),
102 dbesc(NETWORK_DIASPORA),
103 dbesc(NETWORK_OSTATUS),
105 intval($itemsPerPage)
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",
117 intval($itemsPerPage)
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']),
126 dbesc(NETWORK_DIASPORA),
127 dbesc(NETWORK_OSTATUS),
128 dbesc(NETWORK_STATUSNET),
130 intval($itemsPerPage)
133 logger("Query done", LOGGER_DEBUG);
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;
143 $ret['startIndex'] = (int) $startIndex;
144 $ret['itemsPerPage'] = (int) $itemsPerPage;
145 $ret['totalResults'] = (int) $totalResults;
146 $ret['entry'] = array();
151 'displayName' => false,
154 'preferredUsername' => false,
157 'currentLocation' => false,
162 'contactType' => false,
163 'generation' => false
166 if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
167 foreach($fields_ret as $k => $v)
168 $fields_ret[$k] = true;
170 $fields_req = explode(',',$_GET['fields']);
171 foreach($fields_req as $f)
172 $fields_ret[trim($f)] = true;
176 if (dbm::is_result($r)) {
178 if (!isset($rr['generation'])) {
180 $rr['generation'] = 3;
181 elseif ($system_mode)
182 $rr['generation'] = 1;
184 $rr['generation'] = 2;
187 if (($rr['about'] == "") AND isset($rr['pabout']))
188 $rr['about'] = $rr['pabout'];
190 if ($rr['location'] == "") {
191 if (isset($rr['plocation']))
192 $rr['location'] = $rr['plocation'];
194 if (isset($rr['pregion']) AND ($rr['pregion'] != "")) {
195 if ($rr['location'] != "")
196 $rr['location'] .= ", ";
198 $rr['location'] .= $rr['pregion'];
201 if (isset($rr['pcountry']) AND ($rr['pcountry'] != "")) {
202 if ($rr['location'] != "")
203 $rr['location'] .= ", ";
205 $rr['location'] .= $rr['pcountry'];
209 if (($rr['gender'] == "") AND isset($rr['pgender']))
210 $rr['gender'] = $rr['pgender'];
212 if (($rr['keywords'] == "") AND isset($rr['pub_keywords']))
213 $rr['keywords'] = $rr['pub_keywords'];
215 if (isset($rr['account-type']))
216 $rr['contact-type'] = $rr['account-type'];
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);
224 // Non connected persons can only see the keywords of a Diaspora account
225 if ($rr['network'] == NETWORK_DIASPORA) {
226 $rr['location'] = "";
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');
249 if($fields_ret['preferredUsername'])
250 $entry['preferredUsername'] = $rr['nick'];
251 if($fields_ret['updated']) {
253 $entry['updated'] = $rr['success_update'];
255 if ($rr['name-date'] > $entry['updated'])
256 $entry['updated'] = $rr['name-date'];
258 if ($rr['uri-date'] > $entry['updated'])
259 $entry['updated'] = $rr['uri-date'];
261 if ($rr['avatar-date'] > $entry['updated'])
262 $entry['updated'] = $rr['avatar-date'];
264 $entry['updated'] = $rr['updated'];
266 $entry['updated'] = date("c", strtotime($entry['updated']));
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;
277 if($fields_ret['tags']) {
278 $tags = str_replace(","," ",$rr['keywords']);
279 $tags = explode(" ", $tags);
282 foreach ($tags as $tag) {
283 $tag = trim(strtolower($tag));
288 $entry['tags'] = array($cleaned);
290 if($fields_ret['address']) {
291 $entry['address'] = array();
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'];
297 if (isset($rr['plocation']))
298 $entry['address']['locality'] = $rr['plocation'];
300 if (isset($rr['pregion']))
301 $entry['address']['region'] = $rr['pregion'];
304 //if (isset($rr['ppostalcode']))
305 // $entry['address']['postalCode'] = $rr['ppostalcode'];
307 if (isset($rr['pcountry']))
308 $entry['address']['country'] = $rr['pcountry'];
311 if($fields_ret['contactType'])
312 $entry['contactType'] = intval($rr['contact-type']);
314 $ret['entry'][] = $entry;
318 $ret['entry'][] = array();
321 http_status_exit(500);
323 logger("End of poco", LOGGER_DEBUG);
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)));
330 if($format === 'json') {
331 header('Content-type: application/json');
332 echo json_encode($ret);
336 http_status_exit(500);