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