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\Config;
8 use Friendica\Database\DBM;
10 function poco_init(App $a) {
13 if (intval(Config::get('system', 'block_public')) || (Config::get('system', 'block_local_dir'))) {
14 http_status_exit(401);
18 $user = notags(trim($a->argv[1]));
21 $c = q("SELECT * FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1");
22 if (! DBM::is_result($c)) {
23 http_status_exit(401);
28 $format = (($_GET['format']) ? $_GET['format'] : 'json');
33 if ($a->argc > 1 && $a->argv[1] === '@server') {
34 require_once 'include/socgraph.php';
35 // List of all servers that this server knows
36 $ret = poco_serverlist();
37 header('Content-type: application/json');
38 echo json_encode($ret);
41 if ($a->argc > 1 && $a->argv[1] === '@global') {
42 // List of all profiles that this server recently had data from
44 $update_limit = date("Y-m-d H:i:s", time() - 30 * 86400);
46 if ($a->argc > 2 && $a->argv[2] === '@me') {
49 if ($a->argc > 3 && $a->argv[3] === '@all') {
52 if ($a->argc > 3 && $a->argv[3] === '@self') {
55 if ($a->argc > 4 && intval($a->argv[4]) && $justme == false) {
56 $cid = intval($a->argv[4]);
59 if (! $system_mode && ! $global) {
60 $users = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
61 where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
64 if (! DBM::is_result($users) || $users[0]['hidewall'] || $users[0]['hide-friends']) {
65 http_status_exit(404);
72 $sql_extra = " AND `contact`.`self` = 1 ";
75 // $sql_extra = " AND `contact`.`self` = 0 ";
78 $sql_extra = sprintf(" AND `contact`.`id` = %d ", intval($cid));
80 if (x($_GET, 'updatedSince')) {
81 $update_limit = date("Y-m-d H:i:s", strtotime($_GET['updatedSince']));
84 $contacts = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure` AND NOT `hide` AND `network` IN ('%s', '%s', '%s')",
87 dbesc(NETWORK_DIASPORA),
88 dbesc(NETWORK_OSTATUS)
90 } elseif ($system_mode) {
91 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
92 AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
94 $contacts = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
95 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
96 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
99 dbesc(NETWORK_DIASPORA),
100 dbesc(NETWORK_OSTATUS),
101 dbesc(NETWORK_STATUSNET)
104 if (DBM::is_result($contacts)) {
105 $totalResults = intval($contacts[0]['total']);
109 $startIndex = intval($_GET['startIndex']);
113 $itemsPerPage = ((x($_GET, 'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
116 logger("Start global query", LOGGER_DEBUG);
117 $contacts = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
118 ORDER BY `updated` DESC LIMIT %d, %d",
119 dbesc($update_limit),
121 dbesc(NETWORK_DIASPORA),
122 dbesc(NETWORK_OSTATUS),
124 intval($itemsPerPage)
126 } elseif ($system_mode) {
127 logger("Start system mode query", LOGGER_DEBUG);
128 $contacts = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`,
129 `profile`.`gender` AS `pgender`, `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`,
130 `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`, `user`.`account-type`
131 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
132 INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
133 WHERE `self` = 1 AND `profile`.`is-default`
134 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
136 intval($itemsPerPage)
139 logger("Start query for user " . $user['nickname'], LOGGER_DEBUG);
140 $contacts = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
141 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
142 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
143 intval($user['uid']),
145 dbesc(NETWORK_DIASPORA),
146 dbesc(NETWORK_OSTATUS),
147 dbesc(NETWORK_STATUSNET),
149 intval($itemsPerPage)
152 logger("Query done", LOGGER_DEBUG);
155 if (x($_GET, 'sorted')) {
156 $ret['sorted'] = false;
158 if (x($_GET, 'filtered')) {
159 $ret['filtered'] = false;
161 if (x($_GET, 'updatedSince') && ! $global) {
162 $ret['updatedSince'] = false;
164 $ret['startIndex'] = (int) $startIndex;
165 $ret['itemsPerPage'] = (int) $itemsPerPage;
166 $ret['totalResults'] = (int) $totalResults;
167 $ret['entry'] = array();
172 'displayName' => false,
175 'preferredUsername' => false,
178 'currentLocation' => false,
183 'contactType' => false,
184 'generation' => false
187 if ((! x($_GET, 'fields')) || ($_GET['fields'] === '@all')) {
188 foreach ($fields_ret as $k => $v) {
189 $fields_ret[$k] = true;
192 $fields_req = explode(',', $_GET['fields']);
193 foreach ($fields_req as $f) {
194 $fields_ret[trim($f)] = true;
198 if (is_array($contacts)) {
199 if (DBM::is_result($contacts)) {
200 foreach ($contacts as $contact) {
201 if (! isset($contact['generation'])) {
203 $contact['generation'] = 3;
204 } elseif ($system_mode) {
205 $contact['generation'] = 1;
207 $contact['generation'] = 2;
211 if (($contact['about'] == "") && isset($contact['pabout'])) {
212 $contact['about'] = $contact['pabout'];
214 if ($contact['location'] == "") {
215 if (isset($contact['plocation'])) {
216 $contact['location'] = $contact['plocation'];
218 if (isset($contact['pregion']) && ( $contact['pregion'] != "")) {
219 if ($contact['location'] != "") {
220 $contact['location'] .= ", ";
222 $contact['location'] .= $contact['pregion'];
225 if (isset($contact['pcountry']) && ( $contact['pcountry'] != "")) {
226 if ($contact['location'] != "") {
227 $contact['location'] .= ", ";
229 $contact['location'] .= $contact['pcountry'];
233 if (($contact['gender'] == "") && isset($contact['pgender'])) {
234 $contact['gender'] = $contact['pgender'];
236 if (($contact['keywords'] == "") && isset($contact['pub_keywords'])) {
237 $contact['keywords'] = $contact['pub_keywords'];
239 if (isset($contact['account-type'])) {
240 $contact['contact-type'] = $contact['account-type'];
242 $about = Cache::get("about:" . $contact['updated'] . ":" . $contact['nurl']);
243 if (is_null($about)) {
244 require_once 'include/bbcode.php';
245 $about = bbcode($contact['about'], false, false);
246 Cache::set("about:" . $contact['updated'] . ":" . $contact['nurl'], $about);
249 // Non connected persons can only see the keywords of a Diaspora account
250 if ($contact['network'] == NETWORK_DIASPORA) {
251 $contact['location'] = "";
253 $contact['gender'] = "";
257 if ($fields_ret['id']) {
258 $entry['id'] = (int)$contact['id'];
260 if ($fields_ret['displayName']) {
261 $entry['displayName'] = $contact['name'];
263 if ($fields_ret['aboutMe']) {
264 $entry['aboutMe'] = $about;
266 if ($fields_ret['currentLocation']) {
267 $entry['currentLocation'] = $contact['location'];
269 if ($fields_ret['gender']) {
270 $entry['gender'] = $contact['gender'];
272 if ($fields_ret['generation']) {
273 $entry['generation'] = (int)$contact['generation'];
275 if ($fields_ret['urls']) {
276 $entry['urls'] = array(array('value' => $contact['url'], 'type' => 'profile'));
277 if ($contact['addr'] && ($contact['network'] !== NETWORK_MAIL)) {
278 $entry['urls'][] = array('value' => 'acct:' . $contact['addr'], 'type' => 'webfinger');
281 if ($fields_ret['preferredUsername']) {
282 $entry['preferredUsername'] = $contact['nick'];
284 if ($fields_ret['updated']) {
286 $entry['updated'] = $contact['success_update'];
288 if ($contact['name-date'] > $entry['updated']) {
289 $entry['updated'] = $contact['name-date'];
291 if ($contact['uri-date'] > $entry['updated']) {
292 $entry['updated'] = $contact['uri-date'];
294 if ($contact['avatar-date'] > $entry['updated']) {
295 $entry['updated'] = $contact['avatar-date'];
298 $entry['updated'] = $contact['updated'];
300 $entry['updated'] = date("c", strtotime($entry['updated']));
302 if ($fields_ret['photos']) {
303 $entry['photos'] = array(array('value' => $contact['photo'], 'type' => 'profile'));
305 if ($fields_ret['network']) {
306 $entry['network'] = $contact['network'];
307 if ($entry['network'] == NETWORK_STATUSNET) {
308 $entry['network'] = NETWORK_OSTATUS;
310 if (($entry['network'] == "") && ($contact['self'])) {
311 $entry['network'] = NETWORK_DFRN;
314 if ($fields_ret['tags']) {
315 $tags = str_replace(",", " ", $contact['keywords']);
316 $tags = explode(" ", $tags);
319 foreach ($tags as $tag) {
320 $tag = trim(strtolower($tag));
326 $entry['tags'] = array($cleaned);
328 if ($fields_ret['address']) {
329 $entry['address'] = array();
331 // Deactivated. It just reveals too much data. (Although its from the default profile)
332 //if (isset($rr['paddress']))
333 // $entry['address']['streetAddress'] = $rr['paddress'];
335 if (isset($contact['plocation'])) {
336 $entry['address']['locality'] = $contact['plocation'];
338 if (isset($contact['pregion'])) {
339 $entry['address']['region'] = $contact['pregion'];
342 //if (isset($rr['ppostalcode']))
343 // $entry['address']['postalCode'] = $rr['ppostalcode'];
345 if (isset($contact['pcountry'])) {
346 $entry['address']['country'] = $contact['pcountry'];
350 if ($fields_ret['contactType']) {
351 $entry['contactType'] = intval($contact['contact-type']);
353 $ret['entry'][] = $entry;
356 $ret['entry'][] = array();
359 http_status_exit(500);
361 logger("End of poco", LOGGER_DEBUG);
363 if ($format === 'xml') {
364 header('Content-type: text/xml');
365 echo replace_macros(get_markup_template('poco_xml.tpl'), array_xmlify(array('$response' => $ret)));
368 if ($format === 'json') {
369 header('Content-type: application/json');
370 echo json_encode($ret);
373 http_status_exit(500);