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