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