]> git.mxchange.org Git - friendica.git/blob - src/Module/NoScrape.php
Merge pull request #8297 from MrPetovan/task/8285-api-events
[friendica.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\GContact;
30 use Friendica\Model\Profile;
31 use Friendica\Model\User;
32
33 /**
34  * Endpoint for getting current user infos
35  *
36  * @see GContact::updateFromNoScrape() for usage
37  */
38 class NoScrape extends BaseModule
39 {
40         public static function rawContent(array $parameters = [])
41         {
42                 $a = DI::app();
43
44                 if (isset($parameters['nick'])) {
45                         // Get infos about a specific nick (public)
46                         $which = $parameters['nick'];
47                 } elseif (local_user() && isset($parameters['profile']) && DI::args()->get(2) == 'view') {
48                         // view infos about a known profile (needs a login)
49                         $which   = $a->user['nickname'];
50                 } else {
51                         System::jsonError(403, 'Authentication required');
52                         exit();
53                 }
54
55                 Profile::load($a, $which);
56
57                 $json_info = [
58                         'addr'         => $a->profile['addr'],
59                         'nick'         => $which,
60                         'guid'         => $a->profile['guid'],
61                         'key'          => $a->profile['pubkey'],
62                         'homepage'     => DI::baseUrl() . "/profile/{$which}",
63                         'comm'         => ($a->profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY),
64                         'account-type' => $a->profile['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 (!$a->profile['net-publish']) {
73                         $json_info['hide'] = true;
74                         System::jsonExit($json_info);
75                 }
76
77                 $keywords = $a->profile['pub_keywords'] ?? '';
78                 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], $keywords);
79                 $keywords = explode(',', $keywords);
80
81                 $contactPhoto = DBA::selectFirst('contact', ['photo'], ['self' => true, 'uid' => $a->profile['uid']]);
82
83                 $json_info['fn']       = $a->profile['name'];
84                 $json_info['photo']    = $contactPhoto["photo"];
85                 $json_info['tags']     = $keywords;
86                 $json_info['language'] = $a->profile['language'];
87
88                 if (!($a->profile['hide-friends'] ?? false)) {
89                         $stmt = DBA::p(
90                                 "SELECT `gcontact`.`updated`
91                                 FROM `contact`
92                                 INNER JOIN `gcontact`
93                                 WHERE `gcontact`.`nurl` = `contact`.`nurl`
94                                   AND `self`
95                                   AND `uid` = ?
96                                 LIMIT 1",
97                                 intval($a->profile['uid'])
98                         );
99                         if ($gcontact = DBA::fetch($stmt)) {
100                                 $json_info["updated"] = date("c", strtotime($gcontact['updated']));
101                         }
102                         DBA::close($stmt);
103
104                         $json_info['contacts'] = DBA::count('contact',
105                                 [
106                                         'uid'     => $a->profile['uid'],
107                                         'self'    => 0,
108                                         'blocked' => 0,
109                                         'pending' => 0,
110                                         'hidden'  => 0,
111                                         'archive' => 0,
112                                         'network' => [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]
113                                 ]);
114                 }
115
116                 // We display the last activity (post or login), reduced to year and week number
117                 $last_active = 0;
118                 $condition   = ['uid' => $a->profile['uid'], 'self' => true];
119                 $contact     = DBA::selectFirst('contact', ['last-item'], $condition);
120                 if (DBA::isResult($contact)) {
121                         $last_active = strtotime($contact['last-item']);
122                 }
123
124                 $condition = ['uid' => $a->profile['uid']];
125                 $user      = DBA::selectFirst('user', ['login_date'], $condition);
126                 if (DBA::isResult($user)) {
127                         if ($last_active < strtotime($user['login_date'])) {
128                                 $last_active = strtotime($user['login_date']);
129                         }
130                 }
131                 $json_info['last-activity'] = date('o-W', $last_active);
132
133                 //These are optional fields.
134                 $profile_fields = ['about', 'locality', 'region', 'postal-code', 'country-name'];
135                 foreach ($profile_fields as $field) {
136                         if (!empty($a->profile[$field])) {
137                                 $json_info["$field"] = $a->profile[$field];
138                         }
139                 }
140
141                 System::jsonExit($json_info);
142         }
143 }