]> git.mxchange.org Git - friendica.git/blob - src/Module/NoScrape.php
Merge pull request #13093 from annando/emoji-comments
[friendica.git] / src / Module / NoScrape.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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  */
21
22 namespace Friendica\Module;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\System;
26 use Friendica\DI;
27 use Friendica\Model\APContact;
28 use Friendica\Model\User;
29
30 /**
31  * Endpoint for getting current user infos
32  *
33  * @see Contact::updateFromNoScrape() for usage
34  */
35 class NoScrape extends BaseModule
36 {
37         protected function rawContent(array $request = [])
38         {
39                 $a = DI::app();
40
41                 if (isset($this->parameters['nick'])) {
42                         // Get infos about a specific nick (public)
43                         $which = $this->parameters['nick'];
44                 } elseif (DI::userSession()->getLocalUserId() && isset($this->parameters['profile']) && DI::args()->get(2) == 'view') {
45                         // view infos about a known profile (needs a login)
46                         $which = $a->getLoggedInUserNickname();
47                 } else {
48                         System::jsonError(403, 'Authentication required');
49                 }
50
51                 $owner = User::getOwnerDataByNick($which);
52
53                 if (empty($owner['uid'])) {
54                         System::jsonError(404, 'Profile not found');
55                 }
56
57                 $json_info = [
58                         'addr'         => $owner['addr'],
59                         'nick'         => $which,
60                         'guid'         => $owner['guid'],
61                         'key'          => $owner['upubkey'],
62                         'homepage'     => DI::baseUrl() . '/profile/' . $which,
63                         'comm'         => ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY),
64                         'account-type' => $owner['account-type'],
65                 ];
66
67                 $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
68                 foreach ($dfrn_pages as $dfrn) {
69                         $json_info["dfrn-{$dfrn}"] = DI::baseUrl() . "/dfrn_{$dfrn}/{$which}";
70                 }
71
72                 if (!$owner['net-publish']) {
73                         $json_info['hide'] = true;
74                         System::jsonExit($json_info);
75                 }
76
77                 $keywords = $owner['pub_keywords'] ?? '';
78                 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], $keywords);
79                 $keywords = explode(',', $keywords);
80
81                 $json_info['fn']       = $owner['name'];
82                 $json_info['photo']    = User::getAvatarUrl($owner);
83                 $json_info['tags']     = $keywords;
84                 $json_info['language'] = $owner['language'];
85
86                 if (!empty($owner['last-item'])) {
87                         $json_info['updated'] = date("c", strtotime($owner['last-item']));
88                 }
89
90                 if (!($owner['hide-friends'] ?? false)) {
91                         $apcontact = APContact::getByURL($owner['url']);
92                         $json_info['contacts'] = max($apcontact['following_count'], $apcontact['followers_count']);
93                 }
94
95                 // We display the last activity (post or login), reduced to year and week number
96                 $last_active = strtotime($owner['last-item']);
97                 if ($owner['last-activity'] && $last_active < strtotime($owner['last-activity'])) {
98                         $last_active = strtotime($owner['last-activity']);
99                 }
100                 $json_info['last-activity'] = date('o-W', $last_active);
101
102                 //These are optional fields.
103                 $profile_fields = ['about', 'locality', 'region', 'postal-code', 'country-name', 'xmpp', 'matrix'];
104                 foreach ($profile_fields as $field) {
105                         if (!empty($owner[$field])) {
106                                 $json_info[$field] = $owner[$field];
107                         }
108                 }
109
110                 System::jsonExit($json_info);
111         }
112 }