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\Database\DBM;
10 use Friendica\Protocol\PortableContact;
12 function poco_init(App $a) {
15 if (intval(Config::get('system', 'block_public')) || (Config::get('system', 'block_local_dir'))) {
16 http_status_exit(401);
20 $user = notags(trim($a->argv[1]));
23 $c = q("SELECT * FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1");
24 if (! DBM::is_result($c)) {
25 http_status_exit(401);
30 $format = (($_GET['format']) ? $_GET['format'] : 'json');
35 if ($a->argc > 1 && $a->argv[1] === '@server') {
36 // List of all servers that this server knows
37 $ret = PortableContact::serverlist();
38 header('Content-type: application/json');
39 echo json_encode($ret);
42 if ($a->argc > 1 && $a->argv[1] === '@global') {
43 // List of all profiles that this server recently had data from
45 $update_limit = date("Y-m-d H:i:s", time() - 30 * 86400);
47 if ($a->argc > 2 && $a->argv[2] === '@me') {
50 if ($a->argc > 3 && $a->argv[3] === '@all') {
53 if ($a->argc > 3 && $a->argv[3] === '@self') {
56 if ($a->argc > 4 && intval($a->argv[4]) && $justme == false) {
57 $cid = intval($a->argv[4]);
60 if (! $system_mode && ! $global) {
61 $users = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
62 where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
65 if (! DBM::is_result($users) || $users[0]['hidewall'] || $users[0]['hide-friends']) {
66 http_status_exit(404);
73 $sql_extra = " AND `contact`.`self` = 1 ";
76 // $sql_extra = " AND `contact`.`self` = 0 ";
79 $sql_extra = sprintf(" AND `contact`.`id` = %d ", intval($cid));
81 if (x($_GET, 'updatedSince')) {
82 $update_limit = date("Y-m-d H:i:s", strtotime($_GET['updatedSince']));
85 $contacts = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure` AND NOT `hide` AND `network` IN ('%s', '%s', '%s')",
88 dbesc(NETWORK_DIASPORA),
89 dbesc(NETWORK_OSTATUS)
91 } elseif ($system_mode) {
92 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
93 AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
95 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
96 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
97 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
100 dbesc(NETWORK_DIASPORA),
101 dbesc(NETWORK_OSTATUS),
102 dbesc(NETWORK_STATUSNET)
105 if (DBM::is_result($contacts)) {
106 $totalResults = intval($contacts[0]['total']);
110 $startIndex = intval($_GET['startIndex']);
114 $itemsPerPage = ((x($_GET, 'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
117 logger("Start global query", LOGGER_DEBUG);
118 $contacts = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
119 ORDER BY `updated` DESC LIMIT %d, %d",
120 dbesc($update_limit),
122 dbesc(NETWORK_DIASPORA),
123 dbesc(NETWORK_OSTATUS),
125 intval($itemsPerPage)
127 } elseif ($system_mode) {
128 logger("Start system mode query", LOGGER_DEBUG);
129 $contacts = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`,
130 `profile`.`gender` AS `pgender`, `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`,
131 `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`, `user`.`account-type`
132 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
133 INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
134 WHERE `self` = 1 AND `profile`.`is-default`
135 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
137 intval($itemsPerPage)
140 logger("Start query for user " . $user['nickname'], LOGGER_DEBUG);
141 $contacts = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
142 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
143 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
144 intval($user['uid']),
146 dbesc(NETWORK_DIASPORA),
147 dbesc(NETWORK_OSTATUS),
148 dbesc(NETWORK_STATUSNET),
150 intval($itemsPerPage)
153 logger("Query done", LOGGER_DEBUG);
156 if (x($_GET, 'sorted')) {
157 $ret['sorted'] = false;
159 if (x($_GET, 'filtered')) {
160 $ret['filtered'] = false;
162 if (x($_GET, 'updatedSince') && ! $global) {
163 $ret['updatedSince'] = false;
165 $ret['startIndex'] = (int) $startIndex;
166 $ret['itemsPerPage'] = (int) $itemsPerPage;
167 $ret['totalResults'] = (int) $totalResults;
168 $ret['entry'] = array();
173 'displayName' => false,
176 'preferredUsername' => false,
179 'currentLocation' => false,
184 'contactType' => false,
185 'generation' => false
188 if ((! x($_GET, 'fields')) || ($_GET['fields'] === '@all')) {
189 foreach ($fields_ret as $k => $v) {
190 $fields_ret[$k] = true;
193 $fields_req = explode(',', $_GET['fields']);
194 foreach ($fields_req as $f) {
195 $fields_ret[trim($f)] = true;
199 if (is_array($contacts)) {
200 if (DBM::is_result($contacts)) {
201 foreach ($contacts as $contact) {
202 if (! isset($contact['generation'])) {
204 $contact['generation'] = 3;
205 } elseif ($system_mode) {
206 $contact['generation'] = 1;
208 $contact['generation'] = 2;
212 if (($contact['about'] == "") && isset($contact['pabout'])) {
213 $contact['about'] = $contact['pabout'];
215 if ($contact['location'] == "") {
216 if (isset($contact['plocation'])) {
217 $contact['location'] = $contact['plocation'];
219 if (isset($contact['pregion']) && ( $contact['pregion'] != "")) {
220 if ($contact['location'] != "") {
221 $contact['location'] .= ", ";
223 $contact['location'] .= $contact['pregion'];
226 if (isset($contact['pcountry']) && ( $contact['pcountry'] != "")) {
227 if ($contact['location'] != "") {
228 $contact['location'] .= ", ";
230 $contact['location'] .= $contact['pcountry'];
234 if (($contact['gender'] == "") && isset($contact['pgender'])) {
235 $contact['gender'] = $contact['pgender'];
237 if (($contact['keywords'] == "") && isset($contact['pub_keywords'])) {
238 $contact['keywords'] = $contact['pub_keywords'];
240 if (isset($contact['account-type'])) {
241 $contact['contact-type'] = $contact['account-type'];
243 $about = Cache::get("about:" . $contact['updated'] . ":" . $contact['nurl']);
244 if (is_null($about)) {
245 require_once 'include/bbcode.php';
246 $about = bbcode($contact['about'], false, false);
247 Cache::set("about:" . $contact['updated'] . ":" . $contact['nurl'], $about);
250 // Non connected persons can only see the keywords of a Diaspora account
251 if ($contact['network'] == NETWORK_DIASPORA) {
252 $contact['location'] = "";
254 $contact['gender'] = "";
258 if ($fields_ret['id']) {
259 $entry['id'] = (int)$contact['id'];
261 if ($fields_ret['displayName']) {
262 $entry['displayName'] = $contact['name'];
264 if ($fields_ret['aboutMe']) {
265 $entry['aboutMe'] = $about;
267 if ($fields_ret['currentLocation']) {
268 $entry['currentLocation'] = $contact['location'];
270 if ($fields_ret['gender']) {
271 $entry['gender'] = $contact['gender'];
273 if ($fields_ret['generation']) {
274 $entry['generation'] = (int)$contact['generation'];
276 if ($fields_ret['urls']) {
277 $entry['urls'] = array(array('value' => $contact['url'], 'type' => 'profile'));
278 if ($contact['addr'] && ($contact['network'] !== NETWORK_MAIL)) {
279 $entry['urls'][] = array('value' => 'acct:' . $contact['addr'], 'type' => 'webfinger');
282 if ($fields_ret['preferredUsername']) {
283 $entry['preferredUsername'] = $contact['nick'];
285 if ($fields_ret['updated']) {
287 $entry['updated'] = $contact['success_update'];
289 if ($contact['name-date'] > $entry['updated']) {
290 $entry['updated'] = $contact['name-date'];
292 if ($contact['uri-date'] > $entry['updated']) {
293 $entry['updated'] = $contact['uri-date'];
295 if ($contact['avatar-date'] > $entry['updated']) {
296 $entry['updated'] = $contact['avatar-date'];
299 $entry['updated'] = $contact['updated'];
301 $entry['updated'] = date("c", strtotime($entry['updated']));
303 if ($fields_ret['photos']) {
304 $entry['photos'] = array(array('value' => $contact['photo'], 'type' => 'profile'));
306 if ($fields_ret['network']) {
307 $entry['network'] = $contact['network'];
308 if ($entry['network'] == NETWORK_STATUSNET) {
309 $entry['network'] = NETWORK_OSTATUS;
311 if (($entry['network'] == "") && ($contact['self'])) {
312 $entry['network'] = NETWORK_DFRN;
315 if ($fields_ret['tags']) {
316 $tags = str_replace(",", " ", $contact['keywords']);
317 $tags = explode(" ", $tags);
320 foreach ($tags as $tag) {
321 $tag = trim(strtolower($tag));
327 $entry['tags'] = array($cleaned);
329 if ($fields_ret['address']) {
330 $entry['address'] = array();
332 // Deactivated. It just reveals too much data. (Although its from the default profile)
333 //if (isset($rr['paddress']))
334 // $entry['address']['streetAddress'] = $rr['paddress'];
336 if (isset($contact['plocation'])) {
337 $entry['address']['locality'] = $contact['plocation'];
339 if (isset($contact['pregion'])) {
340 $entry['address']['region'] = $contact['pregion'];
343 //if (isset($rr['ppostalcode']))
344 // $entry['address']['postalCode'] = $rr['ppostalcode'];
346 if (isset($contact['pcountry'])) {
347 $entry['address']['country'] = $contact['pcountry'];
351 if ($fields_ret['contactType']) {
352 $entry['contactType'] = intval($contact['contact-type']);
354 $ret['entry'][] = $entry;
357 $ret['entry'][] = array();
360 http_status_exit(500);
362 logger("End of poco", LOGGER_DEBUG);
364 if ($format === 'xml') {
365 header('Content-type: text/xml');
366 echo replace_macros(get_markup_template('poco_xml.tpl'), array_xmlify(array('$response' => $ret)));
369 if ($format === 'json') {
370 header('Content-type: application/json');
371 echo json_encode($ret);
374 http_status_exit(500);