IT translations 100% THX Sylke Vicious
[friendica.git/.git] / src / Module / NoScrape.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Profile;
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         public static function rawContent(array $parameters = [])
40         {
41                 $a = DI::app();
42
43                 if (isset($parameters['nick'])) {
44                         // Get infos about a specific nick (public)
45                         $which = $parameters['nick'];
46                 } elseif (local_user() && isset($parameters['profile']) && DI::args()->get(2) == 'view') {
47                         // view infos about a known profile (needs a login)
48                         $which = $a->user['nickname'];
49                 } else {
50                         System::jsonError(403, 'Authentication required');
51                 }
52
53                 Profile::load($a, $which);
54
55                 if (empty($a->profile['uid'])) {
56                         System::jsonError(404, 'Profile not found');
57                 }
58
59                 $json_info = [
60                         'addr'         => $a->profile['addr'],
61                         'nick'         => $which,
62                         'guid'         => $a->profile['guid'],
63                         'key'          => $a->profile['upubkey'],
64                         'homepage'     => DI::baseUrl() . "/profile/{$which}",
65                         'comm'         => ($a->profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY),
66                         'account-type' => $a->profile['account-type'],
67                 ];
68
69                 $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
70                 foreach ($dfrn_pages as $dfrn) {
71                         $json_info["dfrn-{$dfrn}"] = DI::baseUrl() . "/dfrn_{$dfrn}/{$which}";
72                 }
73
74                 if (!$a->profile['net-publish']) {
75                         $json_info['hide'] = true;
76                         System::jsonExit($json_info);
77                 }
78
79                 $keywords = $a->profile['pub_keywords'] ?? '';
80                 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], $keywords);
81                 $keywords = explode(',', $keywords);
82
83                 $contactPhoto = DBA::selectFirst('contact', ['photo'], ['self' => true, 'uid' => $a->profile['uid']]);
84
85                 $json_info['fn']       = $a->profile['name'];
86                 $json_info['photo']    = $contactPhoto["photo"];
87                 $json_info['tags']     = $keywords;
88                 $json_info['language'] = $a->profile['language'];
89
90                 if (!empty($a->profile['last-item'])) {
91                         $json_info['updated'] = date("c", strtotime($a->profile['last-item']));
92                 }
93
94                 if (!($a->profile['hide-friends'] ?? false)) {
95                         $json_info['contacts'] = DBA::count('contact',
96                                 [
97                                         'uid'     => $a->profile['uid'],
98                                         'self'    => 0,
99                                         'blocked' => 0,
100                                         'pending' => 0,
101                                         'hidden'  => 0,
102                                         'archive' => 0,
103                                         'network' => [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]
104                                 ]);
105                 }
106
107                 // We display the last activity (post or login), reduced to year and week number
108                 $last_active = 0;
109                 $condition   = ['uid' => $a->profile['uid'], 'self' => true];
110                 $contact     = DBA::selectFirst('contact', ['last-item'], $condition);
111                 if (DBA::isResult($contact)) {
112                         $last_active = strtotime($contact['last-item']);
113                 }
114
115                 $condition = ['uid' => $a->profile['uid']];
116                 $user      = DBA::selectFirst('user', ['login_date'], $condition);
117                 if (DBA::isResult($user)) {
118                         if ($last_active < strtotime($user['login_date'])) {
119                                 $last_active = strtotime($user['login_date']);
120                         }
121                 }
122                 $json_info['last-activity'] = date('o-W', $last_active);
123
124                 //These are optional fields.
125                 $profile_fields = ['about', 'locality', 'region', 'postal-code', 'country-name'];
126                 foreach ($profile_fields as $field) {
127                         if (!empty($a->profile[$field])) {
128                                 $json_info["$field"] = $a->profile[$field];
129                         }
130                 }
131
132                 System::jsonExit($json_info);
133         }
134 }