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;
13 function poco_init(App $a) {
16 if (intval(Config::get('system', 'block_public')) || (Config::get('system', 'block_local_dir'))) {
17 System::httpExit(401);
21 $user = notags(trim($a->argv[1]));
24 $c = q("SELECT * FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1");
25 if (! DBM::is_result($c)) {
26 System::httpExit(401);
31 $format = (($_GET['format']) ? $_GET['format'] : 'json');
36 if ($a->argc > 1 && $a->argv[1] === '@server') {
37 // List of all servers that this server knows
38 $ret = PortableContact::serverlist();
39 header('Content-type: application/json');
40 echo json_encode($ret);
43 if ($a->argc > 1 && $a->argv[1] === '@global') {
44 // List of all profiles that this server recently had data from
46 $update_limit = date("Y-m-d H:i:s", time() - 30 * 86400);
48 if ($a->argc > 2 && $a->argv[2] === '@me') {
51 if ($a->argc > 3 && $a->argv[3] === '@all') {
54 if ($a->argc > 3 && $a->argv[3] === '@self') {
57 if ($a->argc > 4 && intval($a->argv[4]) && $justme == false) {
58 $cid = intval($a->argv[4]);
61 if (! $system_mode && ! $global) {
62 $users = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
63 where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
66 if (! DBM::is_result($users) || $users[0]['hidewall'] || $users[0]['hide-friends']) {
67 System::httpExit(404);
74 $sql_extra = " AND `contact`.`self` = 1 ";
77 // $sql_extra = " AND `contact`.`self` = 0 ";
80 $sql_extra = sprintf(" AND `contact`.`id` = %d ", intval($cid));
82 if (x($_GET, 'updatedSince')) {
83 $update_limit = date("Y-m-d H:i:s", strtotime($_GET['updatedSince']));
86 $contacts = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure` AND NOT `hide` AND `network` IN ('%s', '%s', '%s')",
89 dbesc(NETWORK_DIASPORA),
90 dbesc(NETWORK_OSTATUS)
92 } elseif ($system_mode) {
93 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
94 AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
96 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
97 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
98 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
101 dbesc(NETWORK_DIASPORA),
102 dbesc(NETWORK_OSTATUS),
103 dbesc(NETWORK_STATUSNET)
106 if (DBM::is_result($contacts)) {
107 $totalResults = intval($contacts[0]['total']);
111 $startIndex = intval($_GET['startIndex']);
115 $itemsPerPage = ((x($_GET, 'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
118 logger("Start global query", LOGGER_DEBUG);
119 $contacts = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
120 ORDER BY `updated` DESC LIMIT %d, %d",
121 dbesc($update_limit),
123 dbesc(NETWORK_DIASPORA),
124 dbesc(NETWORK_OSTATUS),
126 intval($itemsPerPage)
128 } elseif ($system_mode) {
129 logger("Start system mode query", LOGGER_DEBUG);
130 $contacts = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`,
131 `profile`.`gender` AS `pgender`, `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`,
132 `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`, `user`.`account-type`
133 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
134 INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
135 WHERE `self` = 1 AND `profile`.`is-default`
136 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
138 intval($itemsPerPage)
141 logger("Start query for user " . $user['nickname'], LOGGER_DEBUG);
142 $contacts = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
143 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
144 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
145 intval($user['uid']),
147 dbesc(NETWORK_DIASPORA),
148 dbesc(NETWORK_OSTATUS),
149 dbesc(NETWORK_STATUSNET),
151 intval($itemsPerPage)
154 logger("Query done", LOGGER_DEBUG);
157 if (x($_GET, 'sorted')) {
158 $ret['sorted'] = false;
160 if (x($_GET, 'filtered')) {
161 $ret['filtered'] = false;
163 if (x($_GET, 'updatedSince') && ! $global) {
164 $ret['updatedSince'] = false;
166 $ret['startIndex'] = (int) $startIndex;
167 $ret['itemsPerPage'] = (int) $itemsPerPage;
168 $ret['totalResults'] = (int) $totalResults;
174 'displayName' => false,
177 'preferredUsername' => false,
180 'currentLocation' => false,
185 'contactType' => false,
186 'generation' => false
189 if ((! x($_GET, 'fields')) || ($_GET['fields'] === '@all')) {
190 foreach ($fields_ret as $k => $v) {
191 $fields_ret[$k] = true;
194 $fields_req = explode(',', $_GET['fields']);
195 foreach ($fields_req as $f) {
196 $fields_ret[trim($f)] = true;
200 if (is_array($contacts)) {
201 if (DBM::is_result($contacts)) {
202 foreach ($contacts as $contact) {
203 if (! isset($contact['generation'])) {
205 $contact['generation'] = 3;
206 } elseif ($system_mode) {
207 $contact['generation'] = 1;
209 $contact['generation'] = 2;
213 if (($contact['about'] == "") && isset($contact['pabout'])) {
214 $contact['about'] = $contact['pabout'];
216 if ($contact['location'] == "") {
217 if (isset($contact['plocation'])) {
218 $contact['location'] = $contact['plocation'];
220 if (isset($contact['pregion']) && ( $contact['pregion'] != "")) {
221 if ($contact['location'] != "") {
222 $contact['location'] .= ", ";
224 $contact['location'] .= $contact['pregion'];
227 if (isset($contact['pcountry']) && ( $contact['pcountry'] != "")) {
228 if ($contact['location'] != "") {
229 $contact['location'] .= ", ";
231 $contact['location'] .= $contact['pcountry'];
235 if (($contact['gender'] == "") && isset($contact['pgender'])) {
236 $contact['gender'] = $contact['pgender'];
238 if (($contact['keywords'] == "") && isset($contact['pub_keywords'])) {
239 $contact['keywords'] = $contact['pub_keywords'];
241 if (isset($contact['account-type'])) {
242 $contact['contact-type'] = $contact['account-type'];
244 $about = Cache::get("about:" . $contact['updated'] . ":" . $contact['nurl']);
245 if (is_null($about)) {
246 require_once 'include/bbcode.php';
247 $about = bbcode($contact['about'], false, false);
248 Cache::set("about:" . $contact['updated'] . ":" . $contact['nurl'], $about);
251 // Non connected persons can only see the keywords of a Diaspora account
252 if ($contact['network'] == NETWORK_DIASPORA) {
253 $contact['location'] = "";
255 $contact['gender'] = "";
259 if ($fields_ret['id']) {
260 $entry['id'] = (int)$contact['id'];
262 if ($fields_ret['displayName']) {
263 $entry['displayName'] = $contact['name'];
265 if ($fields_ret['aboutMe']) {
266 $entry['aboutMe'] = $about;
268 if ($fields_ret['currentLocation']) {
269 $entry['currentLocation'] = $contact['location'];
271 if ($fields_ret['gender']) {
272 $entry['gender'] = $contact['gender'];
274 if ($fields_ret['generation']) {
275 $entry['generation'] = (int)$contact['generation'];
277 if ($fields_ret['urls']) {
278 $entry['urls'] = [['value' => $contact['url'], 'type' => 'profile']];
279 if ($contact['addr'] && ($contact['network'] !== NETWORK_MAIL)) {
280 $entry['urls'][] = ['value' => 'acct:' . $contact['addr'], 'type' => 'webfinger'];
283 if ($fields_ret['preferredUsername']) {
284 $entry['preferredUsername'] = $contact['nick'];
286 if ($fields_ret['updated']) {
288 $entry['updated'] = $contact['success_update'];
290 if ($contact['name-date'] > $entry['updated']) {
291 $entry['updated'] = $contact['name-date'];
293 if ($contact['uri-date'] > $entry['updated']) {
294 $entry['updated'] = $contact['uri-date'];
296 if ($contact['avatar-date'] > $entry['updated']) {
297 $entry['updated'] = $contact['avatar-date'];
300 $entry['updated'] = $contact['updated'];
302 $entry['updated'] = date("c", strtotime($entry['updated']));
304 if ($fields_ret['photos']) {
305 $entry['photos'] = [['value' => $contact['photo'], 'type' => 'profile']];
307 if ($fields_ret['network']) {
308 $entry['network'] = $contact['network'];
309 if ($entry['network'] == NETWORK_STATUSNET) {
310 $entry['network'] = NETWORK_OSTATUS;
312 if (($entry['network'] == "") && ($contact['self'])) {
313 $entry['network'] = NETWORK_DFRN;
316 if ($fields_ret['tags']) {
317 $tags = str_replace(",", " ", $contact['keywords']);
318 $tags = explode(" ", $tags);
321 foreach ($tags as $tag) {
322 $tag = trim(strtolower($tag));
328 $entry['tags'] = [$cleaned];
330 if ($fields_ret['address']) {
331 $entry['address'] = [];
333 // Deactivated. It just reveals too much data. (Although its from the default profile)
334 //if (isset($rr['paddress']))
335 // $entry['address']['streetAddress'] = $rr['paddress'];
337 if (isset($contact['plocation'])) {
338 $entry['address']['locality'] = $contact['plocation'];
340 if (isset($contact['pregion'])) {
341 $entry['address']['region'] = $contact['pregion'];
344 //if (isset($rr['ppostalcode']))
345 // $entry['address']['postalCode'] = $rr['ppostalcode'];
347 if (isset($contact['pcountry'])) {
348 $entry['address']['country'] = $contact['pcountry'];
352 if ($fields_ret['contactType']) {
353 $entry['contactType'] = intval($contact['contact-type']);
355 $ret['entry'][] = $entry;
358 $ret['entry'][] = [];
361 System::httpExit(500);
363 logger("End of poco", LOGGER_DEBUG);
365 if ($format === 'xml') {
366 header('Content-type: text/xml');
367 echo replace_macros(get_markup_template('poco_xml.tpl'), array_xmlify(['$response' => $ret]));
370 if ($format === 'json') {
371 header('Content-type: application/json');
372 echo json_encode($ret);
375 System::httpExit(500);