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\Core\Cache;
9 use Friendica\Core\Config;
10 use Friendica\Core\System;
11 use Friendica\Database\DBM;
12 use Friendica\Protocol\PortableContact;
13 use Friendica\Util\DateTimeFormat;
15 function poco_init(App $a) {
18 if (intval(Config::get('system', 'block_public')) || (Config::get('system', 'block_local_dir'))) {
19 System::httpExit(401);
23 $user = notags(trim($a->argv[1]));
26 $c = q("SELECT * FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1");
27 if (! DBM::is_result($c)) {
28 System::httpExit(401);
33 $format = (($_GET['format']) ? $_GET['format'] : 'json');
38 if ($a->argc > 1 && $a->argv[1] === '@server') {
39 // List of all servers that this server knows
40 $ret = PortableContact::serverlist();
41 header('Content-type: application/json');
42 echo json_encode($ret);
45 if ($a->argc > 1 && $a->argv[1] === '@global') {
46 // List of all profiles that this server recently had data from
48 $update_limit = date(DateTimeFormat::MYSQL, time() - 30 * 86400);
50 if ($a->argc > 2 && $a->argv[2] === '@me') {
53 if ($a->argc > 3 && $a->argv[3] === '@all') {
56 if ($a->argc > 3 && $a->argv[3] === '@self') {
59 if ($a->argc > 4 && intval($a->argv[4]) && $justme == false) {
60 $cid = intval($a->argv[4]);
63 if (! $system_mode && ! $global) {
64 $users = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
65 where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
68 if (! DBM::is_result($users) || $users[0]['hidewall'] || $users[0]['hide-friends']) {
69 System::httpExit(404);
76 $sql_extra = " AND `contact`.`self` = 1 ";
79 // $sql_extra = " AND `contact`.`self` = 0 ";
82 $sql_extra = sprintf(" AND `contact`.`id` = %d ", intval($cid));
84 if (x($_GET, 'updatedSince')) {
85 $update_limit = date(DateTimeFormat::MYSQL, strtotime($_GET['updatedSince']));
88 $contacts = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure` AND NOT `hide` AND `network` IN ('%s', '%s', '%s')",
91 dbesc(NETWORK_DIASPORA),
92 dbesc(NETWORK_OSTATUS)
94 } elseif ($system_mode) {
95 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
96 AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
98 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
99 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
100 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
101 intval($user['uid']),
103 dbesc(NETWORK_DIASPORA),
104 dbesc(NETWORK_OSTATUS),
105 dbesc(NETWORK_STATUSNET)
108 if (DBM::is_result($contacts)) {
109 $totalResults = intval($contacts[0]['total']);
113 $startIndex = intval($_GET['startIndex']);
117 $itemsPerPage = ((x($_GET, 'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
120 logger("Start global query", LOGGER_DEBUG);
121 $contacts = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
122 ORDER BY `updated` DESC LIMIT %d, %d",
123 dbesc($update_limit),
125 dbesc(NETWORK_DIASPORA),
126 dbesc(NETWORK_OSTATUS),
128 intval($itemsPerPage)
130 } elseif ($system_mode) {
131 logger("Start system mode query", LOGGER_DEBUG);
132 $contacts = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`,
133 `profile`.`gender` AS `pgender`, `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`,
134 `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`, `user`.`account-type`
135 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
136 INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
137 WHERE `self` = 1 AND `profile`.`is-default`
138 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
140 intval($itemsPerPage)
143 logger("Start query for user " . $user['nickname'], LOGGER_DEBUG);
144 $contacts = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
145 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
146 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
147 intval($user['uid']),
149 dbesc(NETWORK_DIASPORA),
150 dbesc(NETWORK_OSTATUS),
151 dbesc(NETWORK_STATUSNET),
153 intval($itemsPerPage)
156 logger("Query done", LOGGER_DEBUG);
159 if (x($_GET, 'sorted')) {
160 $ret['sorted'] = false;
162 if (x($_GET, 'filtered')) {
163 $ret['filtered'] = false;
165 if (x($_GET, 'updatedSince') && ! $global) {
166 $ret['updatedSince'] = false;
168 $ret['startIndex'] = (int) $startIndex;
169 $ret['itemsPerPage'] = (int) $itemsPerPage;
170 $ret['totalResults'] = (int) $totalResults;
176 'displayName' => false,
179 'preferredUsername' => false,
182 'currentLocation' => false,
187 'contactType' => false,
188 'generation' => false
191 if ((! x($_GET, 'fields')) || ($_GET['fields'] === '@all')) {
192 foreach ($fields_ret as $k => $v) {
193 $fields_ret[$k] = true;
196 $fields_req = explode(',', $_GET['fields']);
197 foreach ($fields_req as $f) {
198 $fields_ret[trim($f)] = true;
202 if (is_array($contacts)) {
203 if (DBM::is_result($contacts)) {
204 foreach ($contacts as $contact) {
205 if (! isset($contact['generation'])) {
207 $contact['generation'] = 3;
208 } elseif ($system_mode) {
209 $contact['generation'] = 1;
211 $contact['generation'] = 2;
215 if (($contact['about'] == "") && isset($contact['pabout'])) {
216 $contact['about'] = $contact['pabout'];
218 if ($contact['location'] == "") {
219 if (isset($contact['plocation'])) {
220 $contact['location'] = $contact['plocation'];
222 if (isset($contact['pregion']) && ( $contact['pregion'] != "")) {
223 if ($contact['location'] != "") {
224 $contact['location'] .= ", ";
226 $contact['location'] .= $contact['pregion'];
229 if (isset($contact['pcountry']) && ( $contact['pcountry'] != "")) {
230 if ($contact['location'] != "") {
231 $contact['location'] .= ", ";
233 $contact['location'] .= $contact['pcountry'];
237 if (($contact['gender'] == "") && isset($contact['pgender'])) {
238 $contact['gender'] = $contact['pgender'];
240 if (($contact['keywords'] == "") && isset($contact['pub_keywords'])) {
241 $contact['keywords'] = $contact['pub_keywords'];
243 if (isset($contact['account-type'])) {
244 $contact['contact-type'] = $contact['account-type'];
246 $about = Cache::get("about:" . $contact['updated'] . ":" . $contact['nurl']);
247 if (is_null($about)) {
248 require_once 'include/bbcode.php';
249 $about = bbcode($contact['about'], false, 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);