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