3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 * @see https://web.archive.org/web/20160405005550/http://portablecontacts.net/draft-spec.html
24 use Friendica\Content\Text\BBCode;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\System;
29 use Friendica\Database\DBA;
31 use Friendica\Module\Response;
32 use Friendica\Util\DateTimeFormat;
33 use Friendica\Util\XML;
35 function poco_init(App $a) {
36 if (intval(DI::config()->get('system', 'block_public')) || (DI::config()->get('system', 'block_local_dir'))) {
37 throw new \Friendica\Network\HTTPException\ForbiddenException();
40 if (DI::args()->getArgc() > 1) {
41 // Only the system mode is supported
42 throw new \Friendica\Network\HTTPException\NotFoundException();
45 $format = ($_GET['format'] ?? '') ?: 'json';
47 $totalResults = DBA::count('profile', ['net-publish' => true]);
48 if ($totalResults == 0) {
49 throw new \Friendica\Network\HTTPException\ForbiddenException();
52 if (!empty($_GET['startIndex'])) {
53 $startIndex = intval($_GET['startIndex']);
57 $itemsPerPage = (!empty($_GET['count']) ? intval($_GET['count']) : $totalResults);
59 Logger::info("Start system mode query");
60 $contacts = DBA::selectToArray('owner-view', [], ['net-publish' => true], ['limit' => [$startIndex, $itemsPerPage]]);
62 Logger::info("Query done");
65 if (!empty($_GET['sorted'])) {
66 $ret['sorted'] = false;
68 if (!empty($_GET['filtered'])) {
69 $ret['filtered'] = false;
71 if (!empty($_GET['updatedSince'])) {
72 $ret['updatedSince'] = false;
74 $ret['startIndex'] = (int) $startIndex;
75 $ret['itemsPerPage'] = (int) $itemsPerPage;
76 $ret['totalResults'] = (int) $totalResults;
81 'displayName' => false,
84 'preferredUsername' => false,
87 'currentLocation' => false,
91 'contactType' => false,
95 if (empty($_GET['fields'])) {
96 foreach ($fields_ret as $k => $v) {
97 $fields_ret[$k] = true;
100 $fields_req = explode(',', $_GET['fields']);
101 foreach ($fields_req as $f) {
102 $fields_ret[trim($f)] = true;
106 if (!is_array($contacts)) {
107 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
110 if (DBA::isResult($contacts)) {
111 foreach ($contacts as $contact) {
112 if (!isset($contact['updated'])) {
113 $contact['updated'] = '';
116 if (! isset($contact['generation'])) {
117 $contact['generation'] = 1;
120 if (($contact['keywords'] == "") && isset($contact['pub_keywords'])) {
121 $contact['keywords'] = $contact['pub_keywords'];
123 if (isset($contact['account-type'])) {
124 $contact['contact-type'] = $contact['account-type'];
127 $cacheKey = 'about:' . $contact['nick'] . ':' . DateTimeFormat::utc($contact['updated'], DateTimeFormat::ATOM);
128 $about = DI::cache()->get($cacheKey);
129 if (is_null($about)) {
130 $about = BBCode::convertForUriId($contact['uri-id'], $contact['about']);
131 DI::cache()->set($cacheKey, $about);
134 // Non connected persons can only see the keywords of a Diaspora account
135 if ($contact['network'] == Protocol::DIASPORA) {
136 $contact['location'] = "";
141 if ($fields_ret['id']) {
142 $entry['id'] = (int)$contact['id'];
144 if ($fields_ret['displayName']) {
145 $entry['displayName'] = $contact['name'];
147 if ($fields_ret['aboutMe']) {
148 $entry['aboutMe'] = $about;
150 if ($fields_ret['currentLocation']) {
151 $entry['currentLocation'] = $contact['location'];
153 if ($fields_ret['generation']) {
154 $entry['generation'] = (int)$contact['generation'];
156 if ($fields_ret['urls']) {
157 $entry['urls'] = [['value' => $contact['url'], 'type' => 'profile']];
158 if ($contact['addr'] && ($contact['network'] !== Protocol::MAIL)) {
159 $entry['urls'][] = ['value' => 'acct:' . $contact['addr'], 'type' => 'webfinger'];
162 if ($fields_ret['preferredUsername']) {
163 $entry['preferredUsername'] = $contact['nick'];
165 if ($fields_ret['updated']) {
166 $entry['updated'] = $contact['success_update'];
168 if ($contact['name-date'] > $entry['updated']) {
169 $entry['updated'] = $contact['name-date'];
171 if ($contact['uri-date'] > $entry['updated']) {
172 $entry['updated'] = $contact['uri-date'];
174 if ($contact['avatar-date'] > $entry['updated']) {
175 $entry['updated'] = $contact['avatar-date'];
177 $entry['updated'] = date("c", strtotime($entry['updated']));
179 if ($fields_ret['photos']) {
180 $entry['photos'] = [['value' => $contact['photo'], 'type' => 'profile']];
182 if ($fields_ret['network']) {
183 $entry['network'] = $contact['network'];
184 if ($entry['network'] == Protocol::STATUSNET) {
185 $entry['network'] = Protocol::OSTATUS;
187 if (($entry['network'] == "") && ($contact['self'])) {
188 $entry['network'] = Protocol::DFRN;
191 if ($fields_ret['tags']) {
192 $tags = str_replace(",", " ", $contact['keywords']);
193 $tags = explode(" ", $tags);
196 foreach ($tags as $tag) {
197 $tag = trim(strtolower($tag));
203 $entry['tags'] = [$cleaned];
205 if ($fields_ret['address']) {
206 $entry['address'] = [];
208 if (isset($contact['locality'])) {
209 $entry['address']['locality'] = $contact['locality'];
212 if (isset($contact['region'])) {
213 $entry['address']['region'] = $contact['region'];
216 if (isset($contact['country'])) {
217 $entry['address']['country'] = $contact['country'];
221 if ($fields_ret['contactType']) {
222 $entry['contactType'] = intval($contact['contact-type']);
224 $ret['entry'][] = $entry;
227 $ret['entry'][] = [];
230 Logger::info("End of poco");
232 if ($format === 'xml') {
233 System::httpExit(Renderer::replaceMacros(Renderer::getMarkupTemplate('poco_xml.tpl'), XML::arrayEscape(['$response' => $ret])), Response::TYPE_XML);
235 if ($format === 'json') {
236 System::jsonExit($ret);
238 throw new \Friendica\Network\HTTPException\InternalServerErrorException();