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\DBM;
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 (! DBM::is_result($c)) {
29 System::httpExit(401);
34 $format = (($_GET['format']) ? $_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);
46 if ($a->argc > 1 && $a->argv[1] === '@global') {
47 // List of all profiles that this server recently had data from
49 $update_limit = date(DateTimeFormat::MYSQL, time() - 30 * 86400);
51 if ($a->argc > 2 && $a->argv[2] === '@me') {
54 if ($a->argc > 3 && $a->argv[3] === '@all') {
57 if ($a->argc > 3 && $a->argv[3] === '@self') {
60 if ($a->argc > 4 && intval($a->argv[4]) && $justme == false) {
61 $cid = intval($a->argv[4]);
64 if (! $system_mode && ! $global) {
65 $users = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
66 where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
69 if (! DBM::is_result($users) || $users[0]['hidewall'] || $users[0]['hide-friends']) {
70 System::httpExit(404);
77 $sql_extra = " AND `contact`.`self` = 1 ";
80 // $sql_extra = " AND `contact`.`self` = 0 ";
83 $sql_extra = sprintf(" AND `contact`.`id` = %d ", intval($cid));
85 if (x($_GET, 'updatedSince')) {
86 $update_limit = date(DateTimeFormat::MYSQL, strtotime($_GET['updatedSince']));
89 $contacts = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure` AND NOT `hide` AND `network` IN ('%s', '%s', '%s')",
92 dbesc(NETWORK_DIASPORA),
93 dbesc(NETWORK_OSTATUS)
95 } elseif ($system_mode) {
96 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
97 AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
99 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
100 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
101 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
102 intval($user['uid']),
104 dbesc(NETWORK_DIASPORA),
105 dbesc(NETWORK_OSTATUS),
106 dbesc(NETWORK_STATUSNET)
109 if (DBM::is_result($contacts)) {
110 $totalResults = intval($contacts[0]['total']);
114 $startIndex = intval($_GET['startIndex']);
118 $itemsPerPage = ((x($_GET, 'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
121 logger("Start global query", LOGGER_DEBUG);
122 $contacts = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
123 ORDER BY `updated` DESC LIMIT %d, %d",
124 dbesc($update_limit),
126 dbesc(NETWORK_DIASPORA),
127 dbesc(NETWORK_OSTATUS),
129 intval($itemsPerPage)
131 } elseif ($system_mode) {
132 logger("Start system mode query", LOGGER_DEBUG);
133 $contacts = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`,
134 `profile`.`gender` AS `pgender`, `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`,
135 `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`, `user`.`account-type`
136 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
137 INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
138 WHERE `self` = 1 AND `profile`.`is-default`
139 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
141 intval($itemsPerPage)
144 logger("Start query for user " . $user['nickname'], LOGGER_DEBUG);
145 $contacts = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
146 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
147 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
148 intval($user['uid']),
150 dbesc(NETWORK_DIASPORA),
151 dbesc(NETWORK_OSTATUS),
152 dbesc(NETWORK_STATUSNET),
154 intval($itemsPerPage)
157 logger("Query done", LOGGER_DEBUG);
160 if (x($_GET, 'sorted')) {
161 $ret['sorted'] = false;
163 if (x($_GET, 'filtered')) {
164 $ret['filtered'] = false;
166 if (x($_GET, 'updatedSince') && ! $global) {
167 $ret['updatedSince'] = false;
169 $ret['startIndex'] = (int) $startIndex;
170 $ret['itemsPerPage'] = (int) $itemsPerPage;
171 $ret['totalResults'] = (int) $totalResults;
177 'displayName' => false,
180 'preferredUsername' => false,
183 'currentLocation' => false,
188 'contactType' => false,
189 'generation' => false
192 if ((! x($_GET, 'fields')) || ($_GET['fields'] === '@all')) {
193 foreach ($fields_ret as $k => $v) {
194 $fields_ret[$k] = true;
197 $fields_req = explode(',', $_GET['fields']);
198 foreach ($fields_req as $f) {
199 $fields_ret[trim($f)] = true;
203 if (is_array($contacts)) {
204 if (DBM::is_result($contacts)) {
205 foreach ($contacts as $contact) {
206 if (! isset($contact['generation'])) {
208 $contact['generation'] = 3;
209 } elseif ($system_mode) {
210 $contact['generation'] = 1;
212 $contact['generation'] = 2;
216 if (($contact['about'] == "") && isset($contact['pabout'])) {
217 $contact['about'] = $contact['pabout'];
219 if ($contact['location'] == "") {
220 if (isset($contact['plocation'])) {
221 $contact['location'] = $contact['plocation'];
223 if (isset($contact['pregion']) && ( $contact['pregion'] != "")) {
224 if ($contact['location'] != "") {
225 $contact['location'] .= ", ";
227 $contact['location'] .= $contact['pregion'];
230 if (isset($contact['pcountry']) && ( $contact['pcountry'] != "")) {
231 if ($contact['location'] != "") {
232 $contact['location'] .= ", ";
234 $contact['location'] .= $contact['pcountry'];
238 if (($contact['gender'] == "") && isset($contact['pgender'])) {
239 $contact['gender'] = $contact['pgender'];
241 if (($contact['keywords'] == "") && isset($contact['pub_keywords'])) {
242 $contact['keywords'] = $contact['pub_keywords'];
244 if (isset($contact['account-type'])) {
245 $contact['contact-type'] = $contact['account-type'];
247 $about = Cache::get("about:" . $contact['updated'] . ":" . $contact['nurl']);
248 if (is_null($about)) {
249 $about = BBCode::convert($contact['about'], false);
250 Cache::set("about:" . $contact['updated'] . ":" . $contact['nurl'], $about);
253 // Non connected persons can only see the keywords of a Diaspora account
254 if ($contact['network'] == NETWORK_DIASPORA) {
255 $contact['location'] = "";
257 $contact['gender'] = "";
261 if ($fields_ret['id']) {
262 $entry['id'] = (int)$contact['id'];
264 if ($fields_ret['displayName']) {
265 $entry['displayName'] = $contact['name'];
267 if ($fields_ret['aboutMe']) {
268 $entry['aboutMe'] = $about;
270 if ($fields_ret['currentLocation']) {
271 $entry['currentLocation'] = $contact['location'];
273 if ($fields_ret['gender']) {
274 $entry['gender'] = $contact['gender'];
276 if ($fields_ret['generation']) {
277 $entry['generation'] = (int)$contact['generation'];
279 if ($fields_ret['urls']) {
280 $entry['urls'] = [['value' => $contact['url'], 'type' => 'profile']];
281 if ($contact['addr'] && ($contact['network'] !== NETWORK_MAIL)) {
282 $entry['urls'][] = ['value' => 'acct:' . $contact['addr'], 'type' => 'webfinger'];
285 if ($fields_ret['preferredUsername']) {
286 $entry['preferredUsername'] = $contact['nick'];
288 if ($fields_ret['updated']) {
290 $entry['updated'] = $contact['success_update'];
292 if ($contact['name-date'] > $entry['updated']) {
293 $entry['updated'] = $contact['name-date'];
295 if ($contact['uri-date'] > $entry['updated']) {
296 $entry['updated'] = $contact['uri-date'];
298 if ($contact['avatar-date'] > $entry['updated']) {
299 $entry['updated'] = $contact['avatar-date'];
302 $entry['updated'] = $contact['updated'];
304 $entry['updated'] = date("c", strtotime($entry['updated']));
306 if ($fields_ret['photos']) {
307 $entry['photos'] = [['value' => $contact['photo'], 'type' => 'profile']];
309 if ($fields_ret['network']) {
310 $entry['network'] = $contact['network'];
311 if ($entry['network'] == NETWORK_STATUSNET) {
312 $entry['network'] = NETWORK_OSTATUS;
314 if (($entry['network'] == "") && ($contact['self'])) {
315 $entry['network'] = NETWORK_DFRN;
318 if ($fields_ret['tags']) {
319 $tags = str_replace(",", " ", $contact['keywords']);
320 $tags = explode(" ", $tags);
323 foreach ($tags as $tag) {
324 $tag = trim(strtolower($tag));
330 $entry['tags'] = [$cleaned];
332 if ($fields_ret['address']) {
333 $entry['address'] = [];
335 // Deactivated. It just reveals too much data. (Although its from the default profile)
336 //if (isset($rr['paddress']))
337 // $entry['address']['streetAddress'] = $rr['paddress'];
339 if (isset($contact['plocation'])) {
340 $entry['address']['locality'] = $contact['plocation'];
342 if (isset($contact['pregion'])) {
343 $entry['address']['region'] = $contact['pregion'];
346 //if (isset($rr['ppostalcode']))
347 // $entry['address']['postalCode'] = $rr['ppostalcode'];
349 if (isset($contact['pcountry'])) {
350 $entry['address']['country'] = $contact['pcountry'];
354 if ($fields_ret['contactType']) {
355 $entry['contactType'] = intval($contact['contact-type']);
357 $ret['entry'][] = $entry;
360 $ret['entry'][] = [];
363 System::httpExit(500);
365 logger("End of poco", LOGGER_DEBUG);
367 if ($format === 'xml') {
368 header('Content-type: text/xml');
369 echo replace_macros(get_markup_template('poco_xml.tpl'), array_xmlify(['$response' => $ret]));
372 if ($format === 'json') {
373 header('Content-type: application/json');
374 echo json_encode($ret);
377 System::httpExit(500);