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\Database\DBA;
30 use Friendica\Util\Strings;
31 use Friendica\Util\XML;
33 function poco_init(App $a) {
34 if (intval(DI::config()->get('system', 'block_public')) || (DI::config()->get('system', 'block_local_dir'))) {
35 throw new \Friendica\Network\HTTPException\ForbiddenException();
38 if (DI::args()->getArgc() > 1) {
39 // Only the system mode is supported
40 throw new \Friendica\Network\HTTPException\NotFoundException();
43 $format = ($_GET['format'] ?? '') ?: 'json';
45 $totalResults = DBA::count('profile', ['net-publish' => true]);
46 if ($totalResults == 0) {
47 throw new \Friendica\Network\HTTPException\ForbiddenException();
50 if (!empty($_GET['startIndex'])) {
51 $startIndex = intval($_GET['startIndex']);
55 $itemsPerPage = (!empty($_GET['count']) ? intval($_GET['count']) : $totalResults);
57 Logger::info("Start system mode query");
58 $contacts = DBA::selectToArray('owner-view', [], ['net-publish' => true], ['limit' => [$startIndex, $itemsPerPage]]);
60 Logger::info("Query done");
63 if (!empty($_GET['sorted'])) {
64 $ret['sorted'] = false;
66 if (!empty($_GET['filtered'])) {
67 $ret['filtered'] = false;
69 if (!empty($_GET['updatedSince'])) {
70 $ret['updatedSince'] = false;
72 $ret['startIndex'] = (int) $startIndex;
73 $ret['itemsPerPage'] = (int) $itemsPerPage;
74 $ret['totalResults'] = (int) $totalResults;
79 'displayName' => false,
82 'preferredUsername' => false,
85 'currentLocation' => false,
89 'contactType' => false,
93 if (empty($_GET['fields'])) {
94 foreach ($fields_ret as $k => $v) {
95 $fields_ret[$k] = true;
98 $fields_req = explode(',', $_GET['fields']);
99 foreach ($fields_req as $f) {
100 $fields_ret[trim($f)] = true;
104 if (!is_array($contacts)) {
105 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
108 if (DBA::isResult($contacts)) {
109 foreach ($contacts as $contact) {
110 if (!isset($contact['updated'])) {
111 $contact['updated'] = '';
114 if (! isset($contact['generation'])) {
115 $contact['generation'] = 1;
118 if (($contact['keywords'] == "") && isset($contact['pub_keywords'])) {
119 $contact['keywords'] = $contact['pub_keywords'];
121 if (isset($contact['account-type'])) {
122 $contact['contact-type'] = $contact['account-type'];
124 $about = DI::cache()->get("about:" . $contact['updated'] . ":" . $contact['nurl']);
125 if (is_null($about)) {
126 $about = BBCode::convertForUriId($contact['uri-id'], $contact['about']);
127 DI::cache()->set("about:" . $contact['updated'] . ":" . $contact['nurl'], $about);
130 // Non connected persons can only see the keywords of a Diaspora account
131 if ($contact['network'] == Protocol::DIASPORA) {
132 $contact['location'] = "";
137 if ($fields_ret['id']) {
138 $entry['id'] = (int)$contact['id'];
140 if ($fields_ret['displayName']) {
141 $entry['displayName'] = $contact['name'];
143 if ($fields_ret['aboutMe']) {
144 $entry['aboutMe'] = $about;
146 if ($fields_ret['currentLocation']) {
147 $entry['currentLocation'] = $contact['location'];
149 if ($fields_ret['generation']) {
150 $entry['generation'] = (int)$contact['generation'];
152 if ($fields_ret['urls']) {
153 $entry['urls'] = [['value' => $contact['url'], 'type' => 'profile']];
154 if ($contact['addr'] && ($contact['network'] !== Protocol::MAIL)) {
155 $entry['urls'][] = ['value' => 'acct:' . $contact['addr'], 'type' => 'webfinger'];
158 if ($fields_ret['preferredUsername']) {
159 $entry['preferredUsername'] = $contact['nick'];
161 if ($fields_ret['updated']) {
162 $entry['updated'] = $contact['success_update'];
164 if ($contact['name-date'] > $entry['updated']) {
165 $entry['updated'] = $contact['name-date'];
167 if ($contact['uri-date'] > $entry['updated']) {
168 $entry['updated'] = $contact['uri-date'];
170 if ($contact['avatar-date'] > $entry['updated']) {
171 $entry['updated'] = $contact['avatar-date'];
173 $entry['updated'] = date("c", strtotime($entry['updated']));
175 if ($fields_ret['photos']) {
176 $entry['photos'] = [['value' => $contact['photo'], 'type' => 'profile']];
178 if ($fields_ret['network']) {
179 $entry['network'] = $contact['network'];
180 if ($entry['network'] == Protocol::STATUSNET) {
181 $entry['network'] = Protocol::OSTATUS;
183 if (($entry['network'] == "") && ($contact['self'])) {
184 $entry['network'] = Protocol::DFRN;
187 if ($fields_ret['tags']) {
188 $tags = str_replace(",", " ", $contact['keywords']);
189 $tags = explode(" ", $tags);
192 foreach ($tags as $tag) {
193 $tag = trim(strtolower($tag));
199 $entry['tags'] = [$cleaned];
201 if ($fields_ret['address']) {
202 $entry['address'] = [];
204 if (isset($contact['locality'])) {
205 $entry['address']['locality'] = $contact['locality'];
208 if (isset($contact['region'])) {
209 $entry['address']['region'] = $contact['region'];
212 if (isset($contact['country'])) {
213 $entry['address']['country'] = $contact['country'];
217 if ($fields_ret['contactType']) {
218 $entry['contactType'] = intval($contact['contact-type']);
220 $ret['entry'][] = $entry;
223 $ret['entry'][] = [];
226 Logger::info("End of poco");
228 if ($format === 'xml') {
229 header('Content-type: text/xml');
230 echo Renderer::replaceMacros(Renderer::getMarkupTemplate('poco_xml.tpl'), XML::arrayEscape(['$response' => $ret]));
233 if ($format === 'json') {
234 header('Content-type: application/json');
235 echo json_encode($ret);
238 throw new \Friendica\Network\HTTPException\InternalServerErrorException();