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);
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 (! DBM::is_result($users) || $users[0]['hidewall'] || $users[0]['hide-friends']) {
71 System::httpExit(404);
78 $sql_extra = " AND `contact`.`self` = 1 ";
81 // $sql_extra = " AND `contact`.`self` = 0 ";
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 (DBM::is_result($contacts)) {
111 $totalResults = intval($contacts[0]['total']);
115 $startIndex = intval($_GET['startIndex']);
119 $itemsPerPage = ((x($_GET, 'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
122 logger("Start global query", LOGGER_DEBUG);
123 $contacts = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
124 ORDER BY `updated` DESC LIMIT %d, %d",
125 dbesc($update_limit),
127 dbesc(NETWORK_DIASPORA),
128 dbesc(NETWORK_OSTATUS),
130 intval($itemsPerPage)
132 } elseif ($system_mode) {
133 logger("Start system mode query", LOGGER_DEBUG);
134 $contacts = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`,
135 `profile`.`gender` AS `pgender`, `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`,
136 `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`, `user`.`account-type`
137 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
138 INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
139 WHERE `self` = 1 AND `profile`.`is-default`
140 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
142 intval($itemsPerPage)
145 logger("Start query for user " . $user['nickname'], LOGGER_DEBUG);
146 $contacts = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
147 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
148 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
149 intval($user['uid']),
151 dbesc(NETWORK_DIASPORA),
152 dbesc(NETWORK_OSTATUS),
153 dbesc(NETWORK_STATUSNET),
155 intval($itemsPerPage)
158 logger("Query done", LOGGER_DEBUG);
161 if (x($_GET, 'sorted')) {
162 $ret['sorted'] = false;
164 if (x($_GET, 'filtered')) {
165 $ret['filtered'] = false;
167 if (x($_GET, 'updatedSince') && ! $global) {
168 $ret['updatedSince'] = false;
170 $ret['startIndex'] = (int) $startIndex;
171 $ret['itemsPerPage'] = (int) $itemsPerPage;
172 $ret['totalResults'] = (int) $totalResults;
178 'displayName' => false,
181 'preferredUsername' => false,
184 'currentLocation' => false,
189 'contactType' => false,
190 'generation' => false
193 if ((! x($_GET, 'fields')) || ($_GET['fields'] === '@all')) {
194 foreach ($fields_ret as $k => $v) {
195 $fields_ret[$k] = true;
198 $fields_req = explode(',', $_GET['fields']);
199 foreach ($fields_req as $f) {
200 $fields_ret[trim($f)] = true;
204 if (is_array($contacts)) {
205 if (DBM::is_result($contacts)) {
206 foreach ($contacts as $contact) {
207 if (! isset($contact['generation'])) {
209 $contact['generation'] = 3;
210 } elseif ($system_mode) {
211 $contact['generation'] = 1;
213 $contact['generation'] = 2;
217 if (($contact['about'] == "") && isset($contact['pabout'])) {
218 $contact['about'] = $contact['pabout'];
220 if ($contact['location'] == "") {
221 if (isset($contact['plocation'])) {
222 $contact['location'] = $contact['plocation'];
224 if (isset($contact['pregion']) && ( $contact['pregion'] != "")) {
225 if ($contact['location'] != "") {
226 $contact['location'] .= ", ";
228 $contact['location'] .= $contact['pregion'];
231 if (isset($contact['pcountry']) && ( $contact['pcountry'] != "")) {
232 if ($contact['location'] != "") {
233 $contact['location'] .= ", ";
235 $contact['location'] .= $contact['pcountry'];
239 if (($contact['gender'] == "") && isset($contact['pgender'])) {
240 $contact['gender'] = $contact['pgender'];
242 if (($contact['keywords'] == "") && isset($contact['pub_keywords'])) {
243 $contact['keywords'] = $contact['pub_keywords'];
245 if (isset($contact['account-type'])) {
246 $contact['contact-type'] = $contact['account-type'];
248 $about = Cache::get("about:" . $contact['updated'] . ":" . $contact['nurl']);
249 if (is_null($about)) {
250 $about = BBCode::convert($contact['about'], false);
251 Cache::set("about:" . $contact['updated'] . ":" . $contact['nurl'], $about);
254 // Non connected persons can only see the keywords of a Diaspora account
255 if ($contact['network'] == NETWORK_DIASPORA) {
256 $contact['location'] = "";
258 $contact['gender'] = "";
262 if ($fields_ret['id']) {
263 $entry['id'] = (int)$contact['id'];
265 if ($fields_ret['displayName']) {
266 $entry['displayName'] = $contact['name'];
268 if ($fields_ret['aboutMe']) {
269 $entry['aboutMe'] = $about;
271 if ($fields_ret['currentLocation']) {
272 $entry['currentLocation'] = $contact['location'];
274 if ($fields_ret['gender']) {
275 $entry['gender'] = $contact['gender'];
277 if ($fields_ret['generation']) {
278 $entry['generation'] = (int)$contact['generation'];
280 if ($fields_ret['urls']) {
281 $entry['urls'] = [['value' => $contact['url'], 'type' => 'profile']];
282 if ($contact['addr'] && ($contact['network'] !== NETWORK_MAIL)) {
283 $entry['urls'][] = ['value' => 'acct:' . $contact['addr'], 'type' => 'webfinger'];
286 if ($fields_ret['preferredUsername']) {
287 $entry['preferredUsername'] = $contact['nick'];
289 if ($fields_ret['updated']) {
291 $entry['updated'] = $contact['success_update'];
293 if ($contact['name-date'] > $entry['updated']) {
294 $entry['updated'] = $contact['name-date'];
296 if ($contact['uri-date'] > $entry['updated']) {
297 $entry['updated'] = $contact['uri-date'];
299 if ($contact['avatar-date'] > $entry['updated']) {
300 $entry['updated'] = $contact['avatar-date'];
303 $entry['updated'] = $contact['updated'];
305 $entry['updated'] = date("c", strtotime($entry['updated']));
307 if ($fields_ret['photos']) {
308 $entry['photos'] = [['value' => $contact['photo'], 'type' => 'profile']];
310 if ($fields_ret['network']) {
311 $entry['network'] = $contact['network'];
312 if ($entry['network'] == NETWORK_STATUSNET) {
313 $entry['network'] = NETWORK_OSTATUS;
315 if (($entry['network'] == "") && ($contact['self'])) {
316 $entry['network'] = NETWORK_DFRN;
319 if ($fields_ret['tags']) {
320 $tags = str_replace(",", " ", $contact['keywords']);
321 $tags = explode(" ", $tags);
324 foreach ($tags as $tag) {
325 $tag = trim(strtolower($tag));
331 $entry['tags'] = [$cleaned];
333 if ($fields_ret['address']) {
334 $entry['address'] = [];
336 // Deactivated. It just reveals too much data. (Although its from the default profile)
337 //if (isset($rr['paddress']))
338 // $entry['address']['streetAddress'] = $rr['paddress'];
340 if (isset($contact['plocation'])) {
341 $entry['address']['locality'] = $contact['plocation'];
343 if (isset($contact['pregion'])) {
344 $entry['address']['region'] = $contact['pregion'];
347 //if (isset($rr['ppostalcode']))
348 // $entry['address']['postalCode'] = $rr['ppostalcode'];
350 if (isset($contact['pcountry'])) {
351 $entry['address']['country'] = $contact['pcountry'];
355 if ($fields_ret['contactType']) {
356 $entry['contactType'] = intval($contact['contact-type']);
358 $ret['entry'][] = $entry;
361 $ret['entry'][] = [];
364 System::httpExit(500);
366 logger("End of poco", LOGGER_DEBUG);
368 if ($format === 'xml') {
369 header('Content-type: text/xml');
370 echo replace_macros(get_markup_template('poco_xml.tpl'), array_xmlify(['$response' => $ret]));
373 if ($format === 'json') {
374 header('Content-type: application/json');
375 echo json_encode($ret);
378 System::httpExit(500);