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\System;
28 use Friendica\Database\DBA;
30 use Friendica\Util\DateTimeFormat;
32 function poco_init(App $a) {
33 if (intval(DI::config()->get('system', 'block_public')) || (DI::config()->get('system', 'block_local_dir'))) {
34 throw new \Friendica\Network\HTTPException\ForbiddenException();
37 if (DI::args()->getArgc() > 1) {
38 // Only the system mode is supported
39 throw new \Friendica\Network\HTTPException\NotFoundException();
42 $format = ($_GET['format'] ?? '') ?: 'json';
44 $totalResults = DBA::count('profile', ['net-publish' => true]);
45 if ($totalResults == 0) {
46 throw new \Friendica\Network\HTTPException\ForbiddenException();
49 if (!empty($_GET['startIndex'])) {
50 $startIndex = intval($_GET['startIndex']);
54 $itemsPerPage = (!empty($_GET['count']) ? intval($_GET['count']) : $totalResults);
56 Logger::info("Start system mode query");
57 $contacts = DBA::selectToArray('owner-view', [], ['net-publish' => true], ['limit' => [$startIndex, $itemsPerPage]]);
59 Logger::info("Query done");
62 if (!empty($_GET['sorted'])) {
63 $ret['sorted'] = false;
65 if (!empty($_GET['filtered'])) {
66 $ret['filtered'] = false;
68 if (!empty($_GET['updatedSince'])) {
69 $ret['updatedSince'] = false;
71 $ret['startIndex'] = (int) $startIndex;
72 $ret['itemsPerPage'] = (int) $itemsPerPage;
73 $ret['totalResults'] = (int) $totalResults;
78 'displayName' => false,
81 'preferredUsername' => false,
84 'currentLocation' => false,
88 'contactType' => false,
92 if (empty($_GET['fields'])) {
93 foreach ($fields_ret as $k => $v) {
94 $fields_ret[$k] = true;
97 $fields_req = explode(',', $_GET['fields']);
98 foreach ($fields_req as $f) {
99 $fields_ret[trim($f)] = true;
103 if (!is_array($contacts)) {
104 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
107 if (DBA::isResult($contacts)) {
108 foreach ($contacts as $contact) {
109 if (!isset($contact['updated'])) {
110 $contact['updated'] = '';
113 if (! isset($contact['generation'])) {
114 $contact['generation'] = 1;
117 if (($contact['keywords'] == "") && isset($contact['pub_keywords'])) {
118 $contact['keywords'] = $contact['pub_keywords'];
120 if (isset($contact['account-type'])) {
121 $contact['contact-type'] = $contact['account-type'];
124 $cacheKey = 'about:' . $contact['nick'] . ':' . DateTimeFormat::utc($contact['updated'], DateTimeFormat::ATOM);
125 $about = DI::cache()->get($cacheKey);
126 if (is_null($about)) {
127 $about = BBCode::convertForUriId($contact['uri-id'], $contact['about']);
128 DI::cache()->set($cacheKey, $about);
131 // Non connected persons can only see the keywords of a Diaspora account
132 if ($contact['network'] == Protocol::DIASPORA) {
133 $contact['location'] = "";
138 if ($fields_ret['id']) {
139 $entry['id'] = (int)$contact['id'];
141 if ($fields_ret['displayName']) {
142 $entry['displayName'] = $contact['name'];
144 if ($fields_ret['aboutMe']) {
145 $entry['aboutMe'] = $about;
147 if ($fields_ret['currentLocation']) {
148 $entry['currentLocation'] = $contact['location'];
150 if ($fields_ret['generation']) {
151 $entry['generation'] = (int)$contact['generation'];
153 if ($fields_ret['urls']) {
154 $entry['urls'] = [['value' => $contact['url'], 'type' => 'profile']];
155 if ($contact['addr'] && ($contact['network'] !== Protocol::MAIL)) {
156 $entry['urls'][] = ['value' => 'acct:' . $contact['addr'], 'type' => 'webfinger'];
159 if ($fields_ret['preferredUsername']) {
160 $entry['preferredUsername'] = $contact['nick'];
162 if ($fields_ret['updated']) {
163 $entry['updated'] = $contact['success_update'];
165 if ($contact['name-date'] > $entry['updated']) {
166 $entry['updated'] = $contact['name-date'];
168 if ($contact['uri-date'] > $entry['updated']) {
169 $entry['updated'] = $contact['uri-date'];
171 if ($contact['avatar-date'] > $entry['updated']) {
172 $entry['updated'] = $contact['avatar-date'];
174 $entry['updated'] = date("c", strtotime($entry['updated']));
176 if ($fields_ret['photos']) {
177 $entry['photos'] = [['value' => $contact['photo'], 'type' => 'profile']];
179 if ($fields_ret['network']) {
180 $entry['network'] = $contact['network'];
181 if ($entry['network'] == Protocol::STATUSNET) {
182 $entry['network'] = Protocol::OSTATUS;
184 if (($entry['network'] == "") && ($contact['self'])) {
185 $entry['network'] = Protocol::DFRN;
188 if ($fields_ret['tags']) {
189 $tags = str_replace(",", " ", $contact['keywords']);
190 $tags = explode(" ", $tags);
193 foreach ($tags as $tag) {
194 $tag = trim(strtolower($tag));
200 $entry['tags'] = [$cleaned];
202 if ($fields_ret['address']) {
203 $entry['address'] = [];
205 if (isset($contact['locality'])) {
206 $entry['address']['locality'] = $contact['locality'];
209 if (isset($contact['region'])) {
210 $entry['address']['region'] = $contact['region'];
213 if (isset($contact['country'])) {
214 $entry['address']['country'] = $contact['country'];
218 if ($fields_ret['contactType']) {
219 $entry['contactType'] = intval($contact['contact-type']);
221 $ret['entry'][] = $entry;
224 $ret['entry'][] = [];
227 Logger::info("End of poco");
229 if ($format === 'json') {
230 System::jsonExit($ret);
232 throw new \Friendica\Network\HTTPException\UnsupportedMediaTypeException();