]> git.mxchange.org Git - friendica.git/blob - mod/poco.php
Test the license check
[friendica.git] / mod / poco.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  * @see https://web.archive.org/web/20160405005550/http://portablecontacts.net/draft-spec.html
21  */
22
23 use Friendica\App;
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;
30 use Friendica\DI;
31 use Friendica\Module\Response;
32 use Friendica\Util\DateTimeFormat;
33 use Friendica\Util\XML;
34
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();
38         }
39
40         if (DI::args()->getArgc() > 1) {
41                 // Only the system mode is supported 
42                 throw new \Friendica\Network\HTTPException\NotFoundException();
43         }
44
45         $format = ($_GET['format'] ?? '') ?: 'json';
46
47         $totalResults = DBA::count('profile', ['net-publish' => true]);
48         if ($totalResults == 0) {
49                 throw new \Friendica\Network\HTTPException\ForbiddenException();
50         }
51
52         if (!empty($_GET['startIndex'])) {
53                 $startIndex = intval($_GET['startIndex']);
54         } else {
55                 $startIndex = 0;
56         }
57         $itemsPerPage = (!empty($_GET['count']) ? intval($_GET['count']) : $totalResults);
58
59         Logger::info("Start system mode query");
60         $contacts = DBA::selectToArray('owner-view', [], ['net-publish' => true], ['limit' => [$startIndex, $itemsPerPage]]);
61
62         Logger::info("Query done");
63
64         $ret = [];
65         if (!empty($_GET['sorted'])) {
66                 $ret['sorted'] = false;
67         }
68         if (!empty($_GET['filtered'])) {
69                 $ret['filtered'] = false;
70         }
71         if (!empty($_GET['updatedSince'])) {
72                 $ret['updatedSince'] = false;
73         }
74         $ret['startIndex']   = (int) $startIndex;
75         $ret['itemsPerPage'] = (int) $itemsPerPage;
76         $ret['totalResults'] = (int) $totalResults;
77         $ret['entry']        = [];
78
79         $fields_ret = [
80                 'id' => false,
81                 'displayName' => false,
82                 'urls' => false,
83                 'updated' => false,
84                 'preferredUsername' => false,
85                 'photos' => false,
86                 'aboutMe' => false,
87                 'currentLocation' => false,
88                 'network' => false,
89                 'tags' => false,
90                 'address' => false,
91                 'contactType' => false,
92                 'generation' => false
93         ];
94
95         if (empty($_GET['fields'])) {
96                 foreach ($fields_ret as $k => $v) {
97                         $fields_ret[$k] = true;
98                 }
99         } else {
100                 $fields_req = explode(',', $_GET['fields']);
101                 foreach ($fields_req as $f) {
102                         $fields_ret[trim($f)] = true;
103                 }
104         }
105
106         if (!is_array($contacts)) {
107                 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
108         }
109
110         if (DBA::isResult($contacts)) {
111                 foreach ($contacts as $contact) {
112                         if (!isset($contact['updated'])) {
113                                 $contact['updated'] = '';
114                         }
115
116                         if (! isset($contact['generation'])) {
117                                 $contact['generation'] = 1;
118                         }
119
120                         if (($contact['keywords'] == "") && isset($contact['pub_keywords'])) {
121                                 $contact['keywords'] = $contact['pub_keywords'];
122                         }
123                         if (isset($contact['account-type'])) {
124                                 $contact['contact-type'] = $contact['account-type'];
125                         }
126
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);
132                         }
133
134                         // Non connected persons can only see the keywords of a Diaspora account
135                         if ($contact['network'] == Protocol::DIASPORA) {
136                                 $contact['location'] = "";
137                                 $about = "";
138                         }
139
140                         $entry = [];
141                         if ($fields_ret['id']) {
142                                 $entry['id'] = (int)$contact['id'];
143                         }
144                         if ($fields_ret['displayName']) {
145                                 $entry['displayName'] = $contact['name'];
146                         }
147                         if ($fields_ret['aboutMe']) {
148                                 $entry['aboutMe'] = $about;
149                         }
150                         if ($fields_ret['currentLocation']) {
151                                 $entry['currentLocation'] = $contact['location'];
152                         }
153                         if ($fields_ret['generation']) {
154                                 $entry['generation'] = (int)$contact['generation'];
155                         }
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'];
160                                 }
161                         }
162                         if ($fields_ret['preferredUsername']) {
163                                 $entry['preferredUsername'] = $contact['nick'];
164                         }
165                         if ($fields_ret['updated']) {
166                                 $entry['updated'] = $contact['success_update'];
167
168                                 if ($contact['name-date'] > $entry['updated']) {
169                                         $entry['updated'] = $contact['name-date'];
170                                 }
171                                 if ($contact['uri-date'] > $entry['updated']) {
172                                         $entry['updated'] = $contact['uri-date'];
173                                 }
174                                 if ($contact['avatar-date'] > $entry['updated']) {
175                                         $entry['updated'] = $contact['avatar-date'];
176                                 }
177                                 $entry['updated'] = date("c", strtotime($entry['updated']));
178                         }
179                         if ($fields_ret['photos']) {
180                                 $entry['photos'] = [['value' => $contact['photo'], 'type' => 'profile']];
181                         }
182                         if ($fields_ret['network']) {
183                                 $entry['network'] = $contact['network'];
184                                 if ($entry['network'] == Protocol::STATUSNET) {
185                                         $entry['network'] = Protocol::OSTATUS;
186                                 }
187                                 if (($entry['network'] == "") && ($contact['self'])) {
188                                         $entry['network'] = Protocol::DFRN;
189                                 }
190                         }
191                         if ($fields_ret['tags']) {
192                                 $tags = str_replace(",", " ", $contact['keywords']);
193                                 $tags = explode(" ", $tags);
194
195                                 $cleaned = [];
196                                 foreach ($tags as $tag) {
197                                         $tag = trim(strtolower($tag));
198                                         if ($tag != "") {
199                                                 $cleaned[] = $tag;
200                                         }
201                                 }
202
203                                 $entry['tags'] = [$cleaned];
204                         }
205                         if ($fields_ret['address']) {
206                                 $entry['address'] = [];
207
208                                 if (isset($contact['locality'])) {
209                                         $entry['address']['locality'] = $contact['locality'];
210                                 }
211
212                                 if (isset($contact['region'])) {
213                                         $entry['address']['region'] = $contact['region'];
214                                 }
215
216                                 if (isset($contact['country'])) {
217                                         $entry['address']['country'] = $contact['country'];
218                                 }
219                         }
220
221                         if ($fields_ret['contactType']) {
222                                 $entry['contactType'] = intval($contact['contact-type']);
223                         }
224                         $ret['entry'][] = $entry;
225                 }
226         } else {
227                 $ret['entry'][] = [];
228         }
229
230         Logger::info("End of poco");
231
232         if ($format === 'xml') {
233                 System::httpExit(Renderer::replaceMacros(Renderer::getMarkupTemplate('poco_xml.tpl'), XML::arrayEscape(['$response' => $ret])), Response::TYPE_XML);
234         }
235         if ($format === 'json') {
236                 System::jsonExit($ret);
237         } else {
238                 throw new \Friendica\Network\HTTPException\InternalServerErrorException();
239         }
240 }