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\Logger;
12 use Friendica\Core\Protocol;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\System;
15 use Friendica\Database\DBA;
16 use Friendica\Protocol\PortableContact;
17 use Friendica\Util\DateTimeFormat;
18 use Friendica\Util\Strings;
19 use Friendica\Util\XML;
21 function poco_init(App $a) {
24 if (intval(Config::get('system', 'block_public')) || (Config::get('system', 'block_local_dir'))) {
25 System::httpExit(401);
29 $nickname = Strings::escapeTags(trim($a->argv[1]));
31 if (empty($nickname)) {
32 $c = q("SELECT * FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1");
33 if (!DBA::isResult($c)) {
34 System::httpExit(401);
39 $format = defaults($_GET, 'format', 'json');
44 if ($a->argc > 1 && $a->argv[1] === '@server') {
45 // List of all servers that this server knows
46 $ret = PortableContact::serverlist();
47 header('Content-type: application/json');
48 echo json_encode($ret);
52 if ($a->argc > 1 && $a->argv[1] === '@global') {
53 // List of all profiles that this server recently had data from
55 $update_limit = date(DateTimeFormat::MYSQL, time() - 30 * 86400);
57 if ($a->argc > 2 && $a->argv[2] === '@me') {
60 if ($a->argc > 3 && $a->argv[3] === '@all') {
63 if ($a->argc > 3 && $a->argv[3] === '@self') {
66 if ($a->argc > 4 && intval($a->argv[4]) && $justme == false) {
67 $cid = intval($a->argv[4]);
70 if (! $system_mode && ! $global) {
71 $users = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
72 where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
73 DBA::escape($nickname)
75 if (! DBA::isResult($users) || $users[0]['hidewall'] || $users[0]['hide-friends']) {
76 System::httpExit(404);
83 $sql_extra = " AND `contact`.`self` = 1 ";
89 $sql_extra = sprintf(" AND `contact`.`id` = %d ", intval($cid));
91 if (!empty($_GET['updatedSince'])) {
92 $update_limit = date(DateTimeFormat::MYSQL, strtotime($_GET['updatedSince']));
95 $contacts = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure` AND NOT `hide` AND `network` IN ('%s', '%s', '%s')",
96 DBA::escape($update_limit),
97 DBA::escape(Protocol::DFRN),
98 DBA::escape(Protocol::DIASPORA),
99 DBA::escape(Protocol::OSTATUS)
101 } elseif ($system_mode) {
102 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
103 AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
105 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
106 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
107 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
108 intval($user['uid']),
109 DBA::escape(Protocol::DFRN),
110 DBA::escape(Protocol::DIASPORA),
111 DBA::escape(Protocol::OSTATUS),
112 DBA::escape(Protocol::STATUSNET)
115 if (DBA::isResult($contacts)) {
116 $totalResults = intval($contacts[0]['total']);
120 if (!empty($_GET['startIndex'])) {
121 $startIndex = intval($_GET['startIndex']);
125 $itemsPerPage = ((!empty($_GET['count'])) ? intval($_GET['count']) : $totalResults);
128 Logger::log("Start global query", Logger::DEBUG);
129 $contacts = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
130 ORDER BY `updated` DESC LIMIT %d, %d",
131 DBA::escape($update_limit),
132 DBA::escape(Protocol::DFRN),
133 DBA::escape(Protocol::DIASPORA),
134 DBA::escape(Protocol::OSTATUS),
136 intval($itemsPerPage)
138 } elseif ($system_mode) {
139 Logger::log("Start system mode query", Logger::DEBUG);
140 $contacts = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`,
141 `profile`.`gender` AS `pgender`, `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`,
142 `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`, `user`.`account-type`
143 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
144 INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
145 WHERE `self` = 1 AND `profile`.`is-default`
146 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
148 intval($itemsPerPage)
151 Logger::log("Start query for user " . $user['nickname'], Logger::DEBUG);
152 $contacts = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
153 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
154 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
155 intval($user['uid']),
156 DBA::escape(Protocol::DFRN),
157 DBA::escape(Protocol::DIASPORA),
158 DBA::escape(Protocol::OSTATUS),
159 DBA::escape(Protocol::STATUSNET),
161 intval($itemsPerPage)
164 Logger::log("Query done", Logger::DEBUG);
167 if (!empty($_GET['sorted'])) {
168 $ret['sorted'] = false;
170 if (!empty($_GET['filtered'])) {
171 $ret['filtered'] = false;
173 if (!empty($_GET['updatedSince']) && ! $global) {
174 $ret['updatedSince'] = false;
176 $ret['startIndex'] = (int) $startIndex;
177 $ret['itemsPerPage'] = (int) $itemsPerPage;
178 $ret['totalResults'] = (int) $totalResults;
184 'displayName' => false,
187 'preferredUsername' => false,
190 'currentLocation' => false,
195 'contactType' => false,
196 'generation' => false
199 if (empty($_GET['fields']) || ($_GET['fields'] === '@all')) {
200 foreach ($fields_ret as $k => $v) {
201 $fields_ret[$k] = true;
204 $fields_req = explode(',', $_GET['fields']);
205 foreach ($fields_req as $f) {
206 $fields_ret[trim($f)] = true;
210 if (is_array($contacts)) {
211 if (DBA::isResult($contacts)) {
212 foreach ($contacts as $contact) {
213 if (!isset($contact['updated'])) {
214 $contact['updated'] = '';
217 if (! isset($contact['generation'])) {
219 $contact['generation'] = 3;
220 } elseif ($system_mode) {
221 $contact['generation'] = 1;
223 $contact['generation'] = 2;
227 if (($contact['about'] == "") && isset($contact['pabout'])) {
228 $contact['about'] = $contact['pabout'];
230 if ($contact['location'] == "") {
231 if (isset($contact['plocation'])) {
232 $contact['location'] = $contact['plocation'];
234 if (isset($contact['pregion']) && ( $contact['pregion'] != "")) {
235 if ($contact['location'] != "") {
236 $contact['location'] .= ", ";
238 $contact['location'] .= $contact['pregion'];
241 if (isset($contact['pcountry']) && ( $contact['pcountry'] != "")) {
242 if ($contact['location'] != "") {
243 $contact['location'] .= ", ";
245 $contact['location'] .= $contact['pcountry'];
249 if (($contact['gender'] == "") && isset($contact['pgender'])) {
250 $contact['gender'] = $contact['pgender'];
252 if (($contact['keywords'] == "") && isset($contact['pub_keywords'])) {
253 $contact['keywords'] = $contact['pub_keywords'];
255 if (isset($contact['account-type'])) {
256 $contact['contact-type'] = $contact['account-type'];
258 $about = Cache::get("about:" . $contact['updated'] . ":" . $contact['nurl']);
259 if (is_null($about)) {
260 $about = BBCode::convert($contact['about'], false);
261 Cache::set("about:" . $contact['updated'] . ":" . $contact['nurl'], $about);
264 // Non connected persons can only see the keywords of a Diaspora account
265 if ($contact['network'] == Protocol::DIASPORA) {
266 $contact['location'] = "";
268 $contact['gender'] = "";
272 if ($fields_ret['id']) {
273 $entry['id'] = (int)$contact['id'];
275 if ($fields_ret['displayName']) {
276 $entry['displayName'] = $contact['name'];
278 if ($fields_ret['aboutMe']) {
279 $entry['aboutMe'] = $about;
281 if ($fields_ret['currentLocation']) {
282 $entry['currentLocation'] = $contact['location'];
284 if ($fields_ret['gender']) {
285 $entry['gender'] = $contact['gender'];
287 if ($fields_ret['generation']) {
288 $entry['generation'] = (int)$contact['generation'];
290 if ($fields_ret['urls']) {
291 $entry['urls'] = [['value' => $contact['url'], 'type' => 'profile']];
292 if ($contact['addr'] && ($contact['network'] !== Protocol::MAIL)) {
293 $entry['urls'][] = ['value' => 'acct:' . $contact['addr'], 'type' => 'webfinger'];
296 if ($fields_ret['preferredUsername']) {
297 $entry['preferredUsername'] = $contact['nick'];
299 if ($fields_ret['updated']) {
301 $entry['updated'] = $contact['success_update'];
303 if ($contact['name-date'] > $entry['updated']) {
304 $entry['updated'] = $contact['name-date'];
306 if ($contact['uri-date'] > $entry['updated']) {
307 $entry['updated'] = $contact['uri-date'];
309 if ($contact['avatar-date'] > $entry['updated']) {
310 $entry['updated'] = $contact['avatar-date'];
313 $entry['updated'] = $contact['updated'];
315 $entry['updated'] = date("c", strtotime($entry['updated']));
317 if ($fields_ret['photos']) {
318 $entry['photos'] = [['value' => $contact['photo'], 'type' => 'profile']];
320 if ($fields_ret['network']) {
321 $entry['network'] = $contact['network'];
322 if ($entry['network'] == Protocol::STATUSNET) {
323 $entry['network'] = Protocol::OSTATUS;
325 if (($entry['network'] == "") && ($contact['self'])) {
326 $entry['network'] = Protocol::DFRN;
329 if ($fields_ret['tags']) {
330 $tags = str_replace(",", " ", $contact['keywords']);
331 $tags = explode(" ", $tags);
334 foreach ($tags as $tag) {
335 $tag = trim(strtolower($tag));
341 $entry['tags'] = [$cleaned];
343 if ($fields_ret['address']) {
344 $entry['address'] = [];
346 // Deactivated. It just reveals too much data. (Although its from the default profile)
347 //if (isset($rr['paddress']))
348 // $entry['address']['streetAddress'] = $rr['paddress'];
350 if (isset($contact['plocation'])) {
351 $entry['address']['locality'] = $contact['plocation'];
353 if (isset($contact['pregion'])) {
354 $entry['address']['region'] = $contact['pregion'];
357 //if (isset($rr['ppostalcode']))
358 // $entry['address']['postalCode'] = $rr['ppostalcode'];
360 if (isset($contact['pcountry'])) {
361 $entry['address']['country'] = $contact['pcountry'];
365 if ($fields_ret['contactType']) {
366 $entry['contactType'] = intval($contact['contact-type']);
368 $ret['entry'][] = $entry;
371 $ret['entry'][] = [];
374 System::httpExit(500);
376 Logger::log("End of poco", Logger::DEBUG);
378 if ($format === 'xml') {
379 header('Content-type: text/xml');
380 echo Renderer::replaceMacros(Renderer::getMarkupTemplate('poco_xml.tpl'), XML::arrayEscape(['$response' => $ret]));
383 if ($format === 'json') {
384 header('Content-type: application/json');
385 echo json_encode($ret);
388 System::httpExit(500);