3 // See here for a documentation for portable contacts:
4 // https://web.archive.org/web/20160405005550/http://portablecontacts.net/draft-spec.html
8 use Friendica\Content\Text\BBCode;
9 use Friendica\Core\Cache;
10 use Friendica\Core\Config;
11 use Friendica\Core\System;
12 use Friendica\Database\DBA;
13 use Friendica\Protocol\PortableContact;
14 use Friendica\Util\DateTimeFormat;
16 function poco_init(App $a) {
19 if (intval(Config::get('system', 'block_public')) || (Config::get('system', 'block_local_dir'))) {
20 System::httpExit(401);
24 $user = notags(trim($a->argv[1]));
27 $c = q("SELECT * FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1");
28 if (!DBA::isResult($c)) {
29 System::httpExit(401);
34 $format = defaults($_GET, 'format', 'json');
39 if ($a->argc > 1 && $a->argv[1] === '@server') {
40 // List of all servers that this server knows
41 $ret = PortableContact::serverlist();
42 header('Content-type: application/json');
43 echo json_encode($ret);
47 if ($a->argc > 1 && $a->argv[1] === '@global') {
48 // List of all profiles that this server recently had data from
50 $update_limit = date(DateTimeFormat::MYSQL, time() - 30 * 86400);
52 if ($a->argc > 2 && $a->argv[2] === '@me') {
55 if ($a->argc > 3 && $a->argv[3] === '@all') {
58 if ($a->argc > 3 && $a->argv[3] === '@self') {
61 if ($a->argc > 4 && intval($a->argv[4]) && $justme == false) {
62 $cid = intval($a->argv[4]);
65 if (! $system_mode && ! $global) {
66 $users = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
67 where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
70 if (! DBA::isResult($users) || $users[0]['hidewall'] || $users[0]['hide-friends']) {
71 System::httpExit(404);
78 $sql_extra = " AND `contact`.`self` = 1 ";
84 $sql_extra = sprintf(" AND `contact`.`id` = %d ", intval($cid));
86 if (x($_GET, 'updatedSince')) {
87 $update_limit = date(DateTimeFormat::MYSQL, strtotime($_GET['updatedSince']));
90 $contacts = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure` AND NOT `hide` AND `network` IN ('%s', '%s', '%s')",
93 dbesc(NETWORK_DIASPORA),
94 dbesc(NETWORK_OSTATUS)
96 } elseif ($system_mode) {
97 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
98 AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
100 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
101 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
102 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
103 intval($user['uid']),
105 dbesc(NETWORK_DIASPORA),
106 dbesc(NETWORK_OSTATUS),
107 dbesc(NETWORK_STATUSNET)
110 if (DBA::isResult($contacts)) {
111 $totalResults = intval($contacts[0]['total']);
115 if (!empty($_GET['startIndex'])) {
116 $startIndex = intval($_GET['startIndex']);
120 $itemsPerPage = ((x($_GET, 'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
123 logger("Start global query", LOGGER_DEBUG);
124 $contacts = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
125 ORDER BY `updated` DESC LIMIT %d, %d",
126 dbesc($update_limit),
128 dbesc(NETWORK_DIASPORA),
129 dbesc(NETWORK_OSTATUS),
131 intval($itemsPerPage)
133 } elseif ($system_mode) {
134 logger("Start system mode query", LOGGER_DEBUG);
135 $contacts = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`,
136 `profile`.`gender` AS `pgender`, `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`,
137 `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`, `user`.`account-type`
138 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
139 INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
140 WHERE `self` = 1 AND `profile`.`is-default`
141 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
143 intval($itemsPerPage)
146 logger("Start query for user " . $user['nickname'], LOGGER_DEBUG);
147 $contacts = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
148 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
149 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
150 intval($user['uid']),
152 dbesc(NETWORK_DIASPORA),
153 dbesc(NETWORK_OSTATUS),
154 dbesc(NETWORK_STATUSNET),
156 intval($itemsPerPage)
159 logger("Query done", LOGGER_DEBUG);
162 if (x($_GET, 'sorted')) {
163 $ret['sorted'] = false;
165 if (x($_GET, 'filtered')) {
166 $ret['filtered'] = false;
168 if (x($_GET, 'updatedSince') && ! $global) {
169 $ret['updatedSince'] = false;
171 $ret['startIndex'] = (int) $startIndex;
172 $ret['itemsPerPage'] = (int) $itemsPerPage;
173 $ret['totalResults'] = (int) $totalResults;
179 'displayName' => false,
182 'preferredUsername' => false,
185 'currentLocation' => false,
190 'contactType' => false,
191 'generation' => false
194 if ((! x($_GET, 'fields')) || ($_GET['fields'] === '@all')) {
195 foreach ($fields_ret as $k => $v) {
196 $fields_ret[$k] = true;
199 $fields_req = explode(',', $_GET['fields']);
200 foreach ($fields_req as $f) {
201 $fields_ret[trim($f)] = true;
205 if (is_array($contacts)) {
206 if (DBA::isResult($contacts)) {
207 foreach ($contacts as $contact) {
208 if (!isset($contact['updated'])) {
209 $contact['updated'] = '';
212 if (! isset($contact['generation'])) {
214 $contact['generation'] = 3;
215 } elseif ($system_mode) {
216 $contact['generation'] = 1;
218 $contact['generation'] = 2;
222 if (($contact['about'] == "") && isset($contact['pabout'])) {
223 $contact['about'] = $contact['pabout'];
225 if ($contact['location'] == "") {
226 if (isset($contact['plocation'])) {
227 $contact['location'] = $contact['plocation'];
229 if (isset($contact['pregion']) && ( $contact['pregion'] != "")) {
230 if ($contact['location'] != "") {
231 $contact['location'] .= ", ";
233 $contact['location'] .= $contact['pregion'];
236 if (isset($contact['pcountry']) && ( $contact['pcountry'] != "")) {
237 if ($contact['location'] != "") {
238 $contact['location'] .= ", ";
240 $contact['location'] .= $contact['pcountry'];
244 if (($contact['gender'] == "") && isset($contact['pgender'])) {
245 $contact['gender'] = $contact['pgender'];
247 if (($contact['keywords'] == "") && isset($contact['pub_keywords'])) {
248 $contact['keywords'] = $contact['pub_keywords'];
250 if (isset($contact['account-type'])) {
251 $contact['contact-type'] = $contact['account-type'];
253 $about = Cache::get("about:" . $contact['updated'] . ":" . $contact['nurl']);
254 if (is_null($about)) {
255 $about = BBCode::convert($contact['about'], false);
256 Cache::set("about:" . $contact['updated'] . ":" . $contact['nurl'], $about);
259 // Non connected persons can only see the keywords of a Diaspora account
260 if ($contact['network'] == NETWORK_DIASPORA) {
261 $contact['location'] = "";
263 $contact['gender'] = "";
267 if ($fields_ret['id']) {
268 $entry['id'] = (int)$contact['id'];
270 if ($fields_ret['displayName']) {
271 $entry['displayName'] = $contact['name'];
273 if ($fields_ret['aboutMe']) {
274 $entry['aboutMe'] = $about;
276 if ($fields_ret['currentLocation']) {
277 $entry['currentLocation'] = $contact['location'];
279 if ($fields_ret['gender']) {
280 $entry['gender'] = $contact['gender'];
282 if ($fields_ret['generation']) {
283 $entry['generation'] = (int)$contact['generation'];
285 if ($fields_ret['urls']) {
286 $entry['urls'] = [['value' => $contact['url'], 'type' => 'profile']];
287 if ($contact['addr'] && ($contact['network'] !== NETWORK_MAIL)) {
288 $entry['urls'][] = ['value' => 'acct:' . $contact['addr'], 'type' => 'webfinger'];
291 if ($fields_ret['preferredUsername']) {
292 $entry['preferredUsername'] = $contact['nick'];
294 if ($fields_ret['updated']) {
296 $entry['updated'] = $contact['success_update'];
298 if ($contact['name-date'] > $entry['updated']) {
299 $entry['updated'] = $contact['name-date'];
301 if ($contact['uri-date'] > $entry['updated']) {
302 $entry['updated'] = $contact['uri-date'];
304 if ($contact['avatar-date'] > $entry['updated']) {
305 $entry['updated'] = $contact['avatar-date'];
308 $entry['updated'] = $contact['updated'];
310 $entry['updated'] = date("c", strtotime($entry['updated']));
312 if ($fields_ret['photos']) {
313 $entry['photos'] = [['value' => $contact['photo'], 'type' => 'profile']];
315 if ($fields_ret['network']) {
316 $entry['network'] = $contact['network'];
317 if ($entry['network'] == NETWORK_STATUSNET) {
318 $entry['network'] = NETWORK_OSTATUS;
320 if (($entry['network'] == "") && ($contact['self'])) {
321 $entry['network'] = NETWORK_DFRN;
324 if ($fields_ret['tags']) {
325 $tags = str_replace(",", " ", $contact['keywords']);
326 $tags = explode(" ", $tags);
329 foreach ($tags as $tag) {
330 $tag = trim(strtolower($tag));
336 $entry['tags'] = [$cleaned];
338 if ($fields_ret['address']) {
339 $entry['address'] = [];
341 // Deactivated. It just reveals too much data. (Although its from the default profile)
342 //if (isset($rr['paddress']))
343 // $entry['address']['streetAddress'] = $rr['paddress'];
345 if (isset($contact['plocation'])) {
346 $entry['address']['locality'] = $contact['plocation'];
348 if (isset($contact['pregion'])) {
349 $entry['address']['region'] = $contact['pregion'];
352 //if (isset($rr['ppostalcode']))
353 // $entry['address']['postalCode'] = $rr['ppostalcode'];
355 if (isset($contact['pcountry'])) {
356 $entry['address']['country'] = $contact['pcountry'];
360 if ($fields_ret['contactType']) {
361 $entry['contactType'] = intval($contact['contact-type']);
363 $ret['entry'][] = $entry;
366 $ret['entry'][] = [];
369 System::httpExit(500);
371 logger("End of poco", LOGGER_DEBUG);
373 if ($format === 'xml') {
374 header('Content-type: text/xml');
375 echo replace_macros(get_markup_template('poco_xml.tpl'), array_xmlify(['$response' => $ret]));
378 if ($format === 'json') {
379 header('Content-type: application/json');
380 echo json_encode($ret);
383 System::httpExit(500);