]> git.mxchange.org Git - friendica.git/blob - src/Module/NoScrape.php
0d765f640380fd052ff4487df8ea61626135d7b6
[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                         /// @todo What should this value tell us?
73                         $result = DBA::p("SELECT `gcontact`.`updated` FROM `contact` INNER JOIN `gcontact` WHERE `gcontact`.`nurl` = `contact`.`nurl` AND `self` AND `uid` = %d LIMIT 1", $a->profile['uid']);
74                         if (DBA::isResult($result)) {
75                                 $json_info["updated"] = date("c", strtotime($result[0]['updated']));
76                         }
77
78                         $json_info['contacts'] = DBA::count('contact',
79                                 [
80                                         'uid'     => $a->profile['uid'],
81                                         'self'    => 0,
82                                         'blocked' => 0,
83                                         'pending' => 0,
84                                         'hidden'  => 0,
85                                         'archive' => 0,
86                                         'network' => [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]
87                                 ]);
88                 }
89
90                 // We display the last activity (post or login), reduced to year and week number
91                 $last_active = 0;
92                 $condition   = ['uid' => $a->profile['uid'], 'self' => true];
93                 $contact     = DBA::selectFirst('contact', ['last-item'], $condition);
94                 if (DBA::isResult($contact)) {
95                         $last_active = strtotime($contact['last-item']);
96                 }
97
98                 $condition = ['uid' => $a->profile['uid']];
99                 $user      = DBA::selectFirst('user', ['login_date'], $condition);
100                 if (DBA::isResult($user)) {
101                         if ($last_active < strtotime($user['login_date'])) {
102                                 $last_active = strtotime($user['login_date']);
103                         }
104                 }
105                 $json_info['last-activity'] = date('o-W', $last_active);
106
107                 //These are optional fields.
108                 $profile_fields = ['pdesc', 'locality', 'region', 'postal-code', 'country-name', 'gender', 'marital', 'about'];
109                 foreach ($profile_fields as $field) {
110                         if (!empty($a->profile[$field])) {
111                                 $json_info["$field"] = $a->profile[$field];
112                         }
113                 }
114
115                 System::jsonExit($json_info);
116         }
117 }