3 // See here for a documentation for portable contacts:
4 // https://web.archive.org/web/20160405005550/http://portablecontacts.net/draft-spec.html
7 use Friendica\Core\Config;
9 function poco_init(App $a) {
12 if (intval(Config::get('system', 'block_public')) || (Config::get('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 require_once 'include/socgraph.php';
34 // List of all servers that this server knows
35 $ret = poco_serverlist();
36 header('Content-type: application/json');
37 echo json_encode($ret);
40 if ($a->argc > 1 && $a->argv[1] === '@global') {
41 // List of all profiles that this server recently had data from
43 $update_limit = date("Y-m-d H:i:s", time() - 30 * 86400);
45 if ($a->argc > 2 && $a->argv[2] === '@me') {
48 if ($a->argc > 3 && $a->argv[3] === '@all') {
51 if ($a->argc > 3 && $a->argv[3] === '@self') {
54 if ($a->argc > 4 && intval($a->argv[4]) && $justme == false) {
55 $cid = intval($a->argv[4]);
58 if (! $system_mode && ! $global) {
59 $users = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
60 where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
63 if (! dbm::is_result($users) || $users[0]['hidewall'] || $users[0]['hide-friends']) {
64 http_status_exit(404);
71 $sql_extra = " AND `contact`.`self` = 1 ";
74 // $sql_extra = " AND `contact`.`self` = 0 ";
77 $sql_extra = sprintf(" AND `contact`.`id` = %d ", intval($cid));
79 if (x($_GET, 'updatedSince')) {
80 $update_limit = date("Y-m-d H:i:s", strtotime($_GET['updatedSince']));
83 $contacts = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure` AND NOT `hide` AND `network` IN ('%s', '%s', '%s')",
86 dbesc(NETWORK_DIASPORA),
87 dbesc(NETWORK_OSTATUS)
89 } elseif ($system_mode) {
90 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
91 AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
93 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
94 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
95 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
98 dbesc(NETWORK_DIASPORA),
99 dbesc(NETWORK_OSTATUS),
100 dbesc(NETWORK_STATUSNET)
103 if (dbm::is_result($contacts)) {
104 $totalResults = intval($contacts[0]['total']);
108 $startIndex = intval($_GET['startIndex']);
112 $itemsPerPage = ((x($_GET, 'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
115 logger("Start global query", LOGGER_DEBUG);
116 $contacts = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
117 ORDER BY `updated` DESC LIMIT %d, %d",
118 dbesc($update_limit),
120 dbesc(NETWORK_DIASPORA),
121 dbesc(NETWORK_OSTATUS),
123 intval($itemsPerPage)
125 } elseif ($system_mode) {
126 logger("Start system mode query", LOGGER_DEBUG);
127 $contacts = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`,
128 `profile`.`gender` AS `pgender`, `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`,
129 `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`, `user`.`account-type`
130 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
131 INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
132 WHERE `self` = 1 AND `profile`.`is-default`
133 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
135 intval($itemsPerPage)
138 logger("Start query for user " . $user['nickname'], LOGGER_DEBUG);
139 $contacts = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
140 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
141 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
142 intval($user['uid']),
144 dbesc(NETWORK_DIASPORA),
145 dbesc(NETWORK_OSTATUS),
146 dbesc(NETWORK_STATUSNET),
148 intval($itemsPerPage)
151 logger("Query done", LOGGER_DEBUG);
154 if (x($_GET, 'sorted')) {
155 $ret['sorted'] = false;
157 if (x($_GET, 'filtered')) {
158 $ret['filtered'] = false;
160 if (x($_GET, 'updatedSince') && ! $global) {
161 $ret['updatedSince'] = false;
163 $ret['startIndex'] = (int) $startIndex;
164 $ret['itemsPerPage'] = (int) $itemsPerPage;
165 $ret['totalResults'] = (int) $totalResults;
166 $ret['entry'] = array();
171 'displayName' => false,
174 'preferredUsername' => false,
177 'currentLocation' => false,
182 'contactType' => false,
183 'generation' => false
186 if ((! x($_GET, 'fields')) || ($_GET['fields'] === '@all')) {
187 foreach ($fields_ret as $k => $v) {
188 $fields_ret[$k] = true;
191 $fields_req = explode(',', $_GET['fields']);
192 foreach ($fields_req as $f) {
193 $fields_ret[trim($f)] = true;
197 if (is_array($contacts)) {
198 if (dbm::is_result($contacts)) {
199 foreach ($contacts as $contact) {
200 if (! isset($contact['generation'])) {
202 $contact['generation'] = 3;
203 } elseif ($system_mode) {
204 $contact['generation'] = 1;
206 $contact['generation'] = 2;
210 if (($contact['about'] == "") && isset($contact['pabout'])) {
211 $contact['about'] = $contact['pabout'];
213 if ($contact['location'] == "") {
214 if (isset($contact['plocation'])) {
215 $contact['location'] = $contact['plocation'];
217 if (isset($contact['pregion']) && ( $contact['pregion'] != "")) {
218 if ($contact['location'] != "") {
219 $contact['location'] .= ", ";
221 $contact['location'] .= $contact['pregion'];
224 if (isset($contact['pcountry']) && ( $contact['pcountry'] != "")) {
225 if ($contact['location'] != "") {
226 $contact['location'] .= ", ";
228 $contact['location'] .= $contact['pcountry'];
232 if (($contact['gender'] == "") && isset($contact['pgender'])) {
233 $contact['gender'] = $contact['pgender'];
235 if (($contact['keywords'] == "") && isset($contact['pub_keywords'])) {
236 $contact['keywords'] = $contact['pub_keywords'];
238 if (isset($contact['account-type'])) {
239 $contact['contact-type'] = $contact['account-type'];
241 $about = Cache::get("about:" . $contact['updated'] . ":" . $contact['nurl']);
242 if (is_null($about)) {
243 require_once 'include/bbcode.php';
244 $about = bbcode($contact['about'], false, false);
245 Cache::set("about:" . $contact['updated'] . ":" . $contact['nurl'], $about);
248 // Non connected persons can only see the keywords of a Diaspora account
249 if ($contact['network'] == NETWORK_DIASPORA) {
250 $contact['location'] = "";
252 $contact['gender'] = "";
256 if ($fields_ret['id']) {
257 $entry['id'] = (int)$contact['id'];
259 if ($fields_ret['displayName']) {
260 $entry['displayName'] = $contact['name'];
262 if ($fields_ret['aboutMe']) {
263 $entry['aboutMe'] = $about;
265 if ($fields_ret['currentLocation']) {
266 $entry['currentLocation'] = $contact['location'];
268 if ($fields_ret['gender']) {
269 $entry['gender'] = $contact['gender'];
271 if ($fields_ret['generation']) {
272 $entry['generation'] = (int)$contact['generation'];
274 if ($fields_ret['urls']) {
275 $entry['urls'] = array(array('value' => $contact['url'], 'type' => 'profile'));
276 if ($contact['addr'] && ($contact['network'] !== NETWORK_MAIL)) {
277 $entry['urls'][] = array('value' => 'acct:' . $contact['addr'], 'type' => 'webfinger');
280 if ($fields_ret['preferredUsername']) {
281 $entry['preferredUsername'] = $contact['nick'];
283 if ($fields_ret['updated']) {
285 $entry['updated'] = $contact['success_update'];
287 if ($contact['name-date'] > $entry['updated']) {
288 $entry['updated'] = $contact['name-date'];
290 if ($contact['uri-date'] > $entry['updated']) {
291 $entry['updated'] = $contact['uri-date'];
293 if ($contact['avatar-date'] > $entry['updated']) {
294 $entry['updated'] = $contact['avatar-date'];
297 $entry['updated'] = $contact['updated'];
299 $entry['updated'] = date("c", strtotime($entry['updated']));
301 if ($fields_ret['photos']) {
302 $entry['photos'] = array(array('value' => $contact['photo'], 'type' => 'profile'));
304 if ($fields_ret['network']) {
305 $entry['network'] = $contact['network'];
306 if ($entry['network'] == NETWORK_STATUSNET) {
307 $entry['network'] = NETWORK_OSTATUS;
309 if (($entry['network'] == "") && ($contact['self'])) {
310 $entry['network'] = NETWORK_DFRN;
313 if ($fields_ret['tags']) {
314 $tags = str_replace(",", " ", $contact['keywords']);
315 $tags = explode(" ", $tags);
318 foreach ($tags as $tag) {
319 $tag = trim(strtolower($tag));
325 $entry['tags'] = array($cleaned);
327 if ($fields_ret['address']) {
328 $entry['address'] = array();
330 // Deactivated. It just reveals too much data. (Although its from the default profile)
331 //if (isset($rr['paddress']))
332 // $entry['address']['streetAddress'] = $rr['paddress'];
334 if (isset($contact['plocation'])) {
335 $entry['address']['locality'] = $contact['plocation'];
337 if (isset($contact['pregion'])) {
338 $entry['address']['region'] = $contact['pregion'];
341 //if (isset($rr['ppostalcode']))
342 // $entry['address']['postalCode'] = $rr['ppostalcode'];
344 if (isset($contact['pcountry'])) {
345 $entry['address']['country'] = $contact['pcountry'];
349 if ($fields_ret['contactType']) {
350 $entry['contactType'] = intval($contact['contact-type']);
352 $ret['entry'][] = $entry;
355 $ret['entry'][] = array();
358 http_status_exit(500);
360 logger("End of poco", LOGGER_DEBUG);
362 if ($format === 'xml') {
363 header('Content-type: text/xml');
364 echo replace_macros(get_markup_template('poco_xml.tpl'), array_xmlify(array('$response' => $ret)));
367 if ($format === 'json') {
368 header('Content-type: application/json');
369 echo json_encode($ret);
372 http_status_exit(500);