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