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\Cache;
8 use Friendica\Core\Config;
9 use Friendica\Core\System;
10 use Friendica\Database\DBM;
11 use Friendica\Protocol\PortableContact;
12 use Friendica\Util\Temporal;
14 function poco_init(App $a) {
17 if (intval(Config::get('system', 'block_public')) || (Config::get('system', 'block_local_dir'))) {
18 System::httpExit(401);
22 $user = notags(trim($a->argv[1]));
25 $c = q("SELECT * FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1");
26 if (! DBM::is_result($c)) {
27 System::httpExit(401);
32 $format = (($_GET['format']) ? $_GET['format'] : 'json');
37 if ($a->argc > 1 && $a->argv[1] === '@server') {
38 // List of all servers that this server knows
39 $ret = PortableContact::serverlist();
40 header('Content-type: application/json');
41 echo json_encode($ret);
44 if ($a->argc > 1 && $a->argv[1] === '@global') {
45 // List of all profiles that this server recently had data from
47 $update_limit = date(Temporal::MYSQL, time() - 30 * 86400);
49 if ($a->argc > 2 && $a->argv[2] === '@me') {
52 if ($a->argc > 3 && $a->argv[3] === '@all') {
55 if ($a->argc > 3 && $a->argv[3] === '@self') {
58 if ($a->argc > 4 && intval($a->argv[4]) && $justme == false) {
59 $cid = intval($a->argv[4]);
62 if (! $system_mode && ! $global) {
63 $users = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
64 where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
67 if (! DBM::is_result($users) || $users[0]['hidewall'] || $users[0]['hide-friends']) {
68 System::httpExit(404);
75 $sql_extra = " AND `contact`.`self` = 1 ";
78 // $sql_extra = " AND `contact`.`self` = 0 ";
81 $sql_extra = sprintf(" AND `contact`.`id` = %d ", intval($cid));
83 if (x($_GET, 'updatedSince')) {
84 $update_limit = date(Temporal::MYSQL, strtotime($_GET['updatedSince']));
87 $contacts = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure` AND NOT `hide` AND `network` IN ('%s', '%s', '%s')",
90 dbesc(NETWORK_DIASPORA),
91 dbesc(NETWORK_OSTATUS)
93 } elseif ($system_mode) {
94 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
95 AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
97 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
98 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
99 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
100 intval($user['uid']),
102 dbesc(NETWORK_DIASPORA),
103 dbesc(NETWORK_OSTATUS),
104 dbesc(NETWORK_STATUSNET)
107 if (DBM::is_result($contacts)) {
108 $totalResults = intval($contacts[0]['total']);
112 $startIndex = intval($_GET['startIndex']);
116 $itemsPerPage = ((x($_GET, 'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
119 logger("Start global query", LOGGER_DEBUG);
120 $contacts = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
121 ORDER BY `updated` DESC LIMIT %d, %d",
122 dbesc($update_limit),
124 dbesc(NETWORK_DIASPORA),
125 dbesc(NETWORK_OSTATUS),
127 intval($itemsPerPage)
129 } elseif ($system_mode) {
130 logger("Start system mode query", LOGGER_DEBUG);
131 $contacts = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`,
132 `profile`.`gender` AS `pgender`, `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`,
133 `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`, `user`.`account-type`
134 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
135 INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
136 WHERE `self` = 1 AND `profile`.`is-default`
137 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
139 intval($itemsPerPage)
142 logger("Start query for user " . $user['nickname'], LOGGER_DEBUG);
143 $contacts = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
144 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
145 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
146 intval($user['uid']),
148 dbesc(NETWORK_DIASPORA),
149 dbesc(NETWORK_OSTATUS),
150 dbesc(NETWORK_STATUSNET),
152 intval($itemsPerPage)
155 logger("Query done", LOGGER_DEBUG);
158 if (x($_GET, 'sorted')) {
159 $ret['sorted'] = false;
161 if (x($_GET, 'filtered')) {
162 $ret['filtered'] = false;
164 if (x($_GET, 'updatedSince') && ! $global) {
165 $ret['updatedSince'] = false;
167 $ret['startIndex'] = (int) $startIndex;
168 $ret['itemsPerPage'] = (int) $itemsPerPage;
169 $ret['totalResults'] = (int) $totalResults;
175 'displayName' => false,
178 'preferredUsername' => false,
181 'currentLocation' => false,
186 'contactType' => false,
187 'generation' => false
190 if ((! x($_GET, 'fields')) || ($_GET['fields'] === '@all')) {
191 foreach ($fields_ret as $k => $v) {
192 $fields_ret[$k] = true;
195 $fields_req = explode(',', $_GET['fields']);
196 foreach ($fields_req as $f) {
197 $fields_ret[trim($f)] = true;
201 if (is_array($contacts)) {
202 if (DBM::is_result($contacts)) {
203 foreach ($contacts as $contact) {
204 if (! isset($contact['generation'])) {
206 $contact['generation'] = 3;
207 } elseif ($system_mode) {
208 $contact['generation'] = 1;
210 $contact['generation'] = 2;
214 if (($contact['about'] == "") && isset($contact['pabout'])) {
215 $contact['about'] = $contact['pabout'];
217 if ($contact['location'] == "") {
218 if (isset($contact['plocation'])) {
219 $contact['location'] = $contact['plocation'];
221 if (isset($contact['pregion']) && ( $contact['pregion'] != "")) {
222 if ($contact['location'] != "") {
223 $contact['location'] .= ", ";
225 $contact['location'] .= $contact['pregion'];
228 if (isset($contact['pcountry']) && ( $contact['pcountry'] != "")) {
229 if ($contact['location'] != "") {
230 $contact['location'] .= ", ";
232 $contact['location'] .= $contact['pcountry'];
236 if (($contact['gender'] == "") && isset($contact['pgender'])) {
237 $contact['gender'] = $contact['pgender'];
239 if (($contact['keywords'] == "") && isset($contact['pub_keywords'])) {
240 $contact['keywords'] = $contact['pub_keywords'];
242 if (isset($contact['account-type'])) {
243 $contact['contact-type'] = $contact['account-type'];
245 $about = Cache::get("about:" . $contact['updated'] . ":" . $contact['nurl']);
246 if (is_null($about)) {
247 require_once 'include/bbcode.php';
248 $about = bbcode($contact['about'], false, false);
249 Cache::set("about:" . $contact['updated'] . ":" . $contact['nurl'], $about);
252 // Non connected persons can only see the keywords of a Diaspora account
253 if ($contact['network'] == NETWORK_DIASPORA) {
254 $contact['location'] = "";
256 $contact['gender'] = "";
260 if ($fields_ret['id']) {
261 $entry['id'] = (int)$contact['id'];
263 if ($fields_ret['displayName']) {
264 $entry['displayName'] = $contact['name'];
266 if ($fields_ret['aboutMe']) {
267 $entry['aboutMe'] = $about;
269 if ($fields_ret['currentLocation']) {
270 $entry['currentLocation'] = $contact['location'];
272 if ($fields_ret['gender']) {
273 $entry['gender'] = $contact['gender'];
275 if ($fields_ret['generation']) {
276 $entry['generation'] = (int)$contact['generation'];
278 if ($fields_ret['urls']) {
279 $entry['urls'] = [['value' => $contact['url'], 'type' => 'profile']];
280 if ($contact['addr'] && ($contact['network'] !== NETWORK_MAIL)) {
281 $entry['urls'][] = ['value' => 'acct:' . $contact['addr'], 'type' => 'webfinger'];
284 if ($fields_ret['preferredUsername']) {
285 $entry['preferredUsername'] = $contact['nick'];
287 if ($fields_ret['updated']) {
289 $entry['updated'] = $contact['success_update'];
291 if ($contact['name-date'] > $entry['updated']) {
292 $entry['updated'] = $contact['name-date'];
294 if ($contact['uri-date'] > $entry['updated']) {
295 $entry['updated'] = $contact['uri-date'];
297 if ($contact['avatar-date'] > $entry['updated']) {
298 $entry['updated'] = $contact['avatar-date'];
301 $entry['updated'] = $contact['updated'];
303 $entry['updated'] = date("c", strtotime($entry['updated']));
305 if ($fields_ret['photos']) {
306 $entry['photos'] = [['value' => $contact['photo'], 'type' => 'profile']];
308 if ($fields_ret['network']) {
309 $entry['network'] = $contact['network'];
310 if ($entry['network'] == NETWORK_STATUSNET) {
311 $entry['network'] = NETWORK_OSTATUS;
313 if (($entry['network'] == "") && ($contact['self'])) {
314 $entry['network'] = NETWORK_DFRN;
317 if ($fields_ret['tags']) {
318 $tags = str_replace(",", " ", $contact['keywords']);
319 $tags = explode(" ", $tags);
322 foreach ($tags as $tag) {
323 $tag = trim(strtolower($tag));
329 $entry['tags'] = [$cleaned];
331 if ($fields_ret['address']) {
332 $entry['address'] = [];
334 // Deactivated. It just reveals too much data. (Although its from the default profile)
335 //if (isset($rr['paddress']))
336 // $entry['address']['streetAddress'] = $rr['paddress'];
338 if (isset($contact['plocation'])) {
339 $entry['address']['locality'] = $contact['plocation'];
341 if (isset($contact['pregion'])) {
342 $entry['address']['region'] = $contact['pregion'];
345 //if (isset($rr['ppostalcode']))
346 // $entry['address']['postalCode'] = $rr['ppostalcode'];
348 if (isset($contact['pcountry'])) {
349 $entry['address']['country'] = $contact['pcountry'];
353 if ($fields_ret['contactType']) {
354 $entry['contactType'] = intval($contact['contact-type']);
356 $ret['entry'][] = $entry;
359 $ret['entry'][] = [];
362 System::httpExit(500);
364 logger("End of poco", LOGGER_DEBUG);
366 if ($format === 'xml') {
367 header('Content-type: text/xml');
368 echo replace_macros(get_markup_template('poco_xml.tpl'), array_xmlify(['$response' => $ret]));
371 if ($format === 'json') {
372 header('Content-type: application/json');
373 echo json_encode($ret);
376 System::httpExit(500);