]> git.mxchange.org Git - friendica.git/blob - src/Module/NoScrape.php
27bf7a95ccd5fd83caab36fecbc43d74ec4dd4fc
[friendica.git] / src / Module / NoScrape.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Friendica\BaseModule;
6 use Friendica\Core\Protocol;
7 use Friendica\Core\System;
8 use Friendica\Database\DBA;
9 use Friendica\DI;
10 use Friendica\Model\GContact;
11 use Friendica\Model\Profile;
12 use Friendica\Model\User;
13
14 /**
15  * Endpoint for getting current user infos
16  *
17  * @see GContact::updateFromNoScrape() for usage
18  */
19 class NoScrape extends BaseModule
20 {
21         public static function rawContent(array $parameters = [])
22         {
23                 $a = DI::app();
24
25                 if (isset($parameters['nick'])) {
26                         // Get infos about a specific nick (public)
27                         $which = $parameters['nick'];
28                         $profile = 0;
29                 } elseif (local_user() && isset($parameters['profile']) && DI::args()->get(2) == 'view') {
30                         // view infos about a known profile (needs a login)
31                         $which   = $a->user['nickname'];
32                         $profile = $parameters['profile'];
33                 } else {
34                         System::jsonError(403, 'Authentication required');
35                         exit();
36                 }
37
38                 Profile::load($a, $which, $profile);
39
40                 $json_info = [
41                         'addr'         => $a->profile['addr'],
42                         'nick'         => $which,
43                         'guid'         => $a->profile['guid'],
44                         'key'          => $a->profile['pubkey'],
45                         'homepage'     => DI::baseUrl() . "/profile/{$which}",
46                         'comm'         => ($a->profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY),
47                         'account-type' => $a->profile['account-type'],
48                 ];
49
50                 $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
51                 foreach ($dfrn_pages as $dfrn) {
52                         $json_info["dfrn-{$dfrn}"] = DI::baseUrl() . "/dfrn_{$dfrn}/{$which}";
53                 }
54
55                 if (!$a->profile['net-publish'] || $a->profile['hidewall']) {
56                         $json_info['hide'] = true;
57                         System::jsonExit($json_info);
58                 }
59
60                 $keywords = $a->profile['pub_keywords'] ?? '';
61                 $keywords = str_replace(['#', ',', ' ', ',,'], ['', ' ', ',', ','], $keywords);
62                 $keywords = explode(',', $keywords);
63
64                 $contactPhoto = DBA::selectFirst('contact', ['photo'], ['self' => true, 'uid' => $a->profile['uid']]);
65
66                 $json_info['fn']       = $a->profile['name'];
67                 $json_info['photo']    = $contactPhoto["photo"];
68                 $json_info['tags']     = $keywords;
69                 $json_info['language'] = $a->profile['language'];
70
71                 if (!($a->profile['hide-friends'] ?? false)) {
72                         $stmt = DBA::p(
73                                 "SELECT `gcontact`.`updated`
74                                 FROM `contact`
75                                 INNER JOIN `gcontact`
76                                 WHERE `gcontact`.`nurl` = `contact`.`nurl`
77                                   AND `self`
78                                   AND `uid` = ?
79                                 LIMIT 1",
80                                 intval($a->profile['uid'])
81                         );
82                         if ($gcontact = DBA::fetch($stmt)) {
83                                 $json_info["updated"] = date("c", strtotime($gcontact['updated']));
84                         }
85                         DBA::close($stmt);
86
87                         $json_info['contacts'] = DBA::count('contact',
88                                 [
89                                         'uid'     => $a->profile['uid'],
90                                         'self'    => 0,
91                                         'blocked' => 0,
92                                         'pending' => 0,
93                                         'hidden'  => 0,
94                                         'archive' => 0,
95                                         'network' => [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]
96                                 ]);
97                 }
98
99                 // We display the last activity (post or login), reduced to year and week number
100                 $last_active = 0;
101                 $condition   = ['uid' => $a->profile['uid'], 'self' => true];
102                 $contact     = DBA::selectFirst('contact', ['last-item'], $condition);
103                 if (DBA::isResult($contact)) {
104                         $last_active = strtotime($contact['last-item']);
105                 }
106
107                 $condition = ['uid' => $a->profile['uid']];
108                 $user      = DBA::selectFirst('user', ['login_date'], $condition);
109                 if (DBA::isResult($user)) {
110                         if ($last_active < strtotime($user['login_date'])) {
111                                 $last_active = strtotime($user['login_date']);
112                         }
113                 }
114                 $json_info['last-activity'] = date('o-W', $last_active);
115
116                 //These are optional fields.
117                 $profile_fields = ['pdesc', 'locality', 'region', 'postal-code', 'country-name', 'gender', 'marital', 'about'];
118                 foreach ($profile_fields as $field) {
119                         if (!empty($a->profile[$field])) {
120                                 $json_info["$field"] = $a->profile[$field];
121                         }
122                 }
123
124                 System::jsonExit($json_info);
125         }
126 }