]> git.mxchange.org Git - friendica.git/blob - src/Module/NoScrape.php
Use rawContent for Special Options to avoid a protected options() method
[friendica.git] / src / Module / NoScrape.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  */
21
22 namespace Friendica\Module;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\User;
30
31 /**
32  * Endpoint for getting current user infos
33  *
34  * @see Contact::updateFromNoScrape() for usage
35  */
36 class NoScrape extends BaseModule
37 {
38         protected function rawContent(array $request = [])
39         {
40                 $a = DI::app();
41
42                 if (isset($this->parameters['nick'])) {
43                         // Get infos about a specific nick (public)
44                         $which = $this->parameters['nick'];
45                 } elseif (local_user() && isset($this->parameters['profile']) && DI::args()->get(2) == 'view') {
46                         // view infos about a known profile (needs a login)
47                         $which = $a->getLoggedInUserNickname();
48                 } else {
49                         System::jsonError(403, 'Authentication required');
50                 }
51
52                 $owner = User::getOwnerDataByNick($which);
53
54                 if (empty($owner['uid'])) {
55                         System::jsonError(404, 'Profile not found');
56                 }
57
58                 $json_info = [
59                         'addr'         => $owner['addr'],
60                         'nick'         => $which,
61                         'guid'         => $owner['guid'],
62                         'key'          => $owner['upubkey'],
63                         'homepage'     => DI::baseUrl() . '/profile/' . $which,
64                         'comm'         => ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY),
65                         'account-type' => $owner['account-type'],
66                 ];
67
68                 $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
69                 foreach ($dfrn_pages as $dfrn) {
70                         $json_info["dfrn-{$dfrn}"] = DI::baseUrl() . "/dfrn_{$dfrn}/{$which}";
71                 }
72
73                 if (!$owner['net-publish']) {
74                         $json_info['hide'] = true;
75                         System::jsonExit($json_info);
76                 }
77
78                 $keywords = $owner['pub_keywords'] ?? '';
79                 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], $keywords);
80                 $keywords = explode(',', $keywords);
81
82                 $json_info['fn']       = $owner['name'];
83                 $json_info['photo']    = User::getAvatarUrl($owner);
84                 $json_info['tags']     = $keywords;
85                 $json_info['language'] = $owner['language'];
86
87                 if (!empty($owner['last-item'])) {
88                         $json_info['updated'] = date("c", strtotime($owner['last-item']));
89                 }
90
91                 if (!($owner['hide-friends'] ?? false)) {
92                         $json_info['contacts'] = DBA::count('contact',
93                                 [
94                                         'uid'     => $owner['uid'],
95                                         'self'    => 0,
96                                         'blocked' => 0,
97                                         'pending' => 0,
98                                         'hidden'  => 0,
99                                         'archive' => 0,
100                                         'network' => [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]
101                                 ]);
102                 }
103
104                 // We display the last activity (post or login), reduced to year and week number
105                 $last_active = 0;
106                 $condition   = ['uid' => $owner['uid'], 'self' => true];
107                 $contact     = DBA::selectFirst('contact', ['last-item'], $condition);
108                 if (DBA::isResult($contact)) {
109                         $last_active = strtotime($contact['last-item']);
110                 }
111
112                 $condition = ['uid' => $owner['uid']];
113                 $user      = DBA::selectFirst('user', ['login_date'], $condition);
114                 if (DBA::isResult($user)) {
115                         if ($last_active < strtotime($user['login_date'])) {
116                                 $last_active = strtotime($user['login_date']);
117                         }
118                 }
119                 $json_info['last-activity'] = date('o-W', $last_active);
120
121                 //These are optional fields.
122                 $profile_fields = ['about', 'locality', 'region', 'postal-code', 'country-name', 'xmpp', 'matrix'];
123                 foreach ($profile_fields as $field) {
124                         if (!empty($owner[$field])) {
125                                 $json_info["$field"] = $owner[$field];
126                         }
127                 }
128
129                 System::jsonExit($json_info);
130         }
131 }