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