]> git.mxchange.org Git - friendica.git/blob - mod/noscrape.php
Just some more fixed notice
[friendica.git] / mod / noscrape.php
1 <?php
2 /**
3  * @file mod/noscrape.php
4  */
5
6 use Friendica\App;
7 use Friendica\Core\System;
8 use Friendica\Database\dba;
9 use Friendica\Database\DBM;
10 use Friendica\Model\Profile;
11
12 function noscrape_init(App $a)
13 {
14         if ($a->argc > 1) {
15                 $which = $a->argv[1];
16         } else {
17                 killme();
18         }
19
20         $profile = 0;
21         if ((local_user()) && ($a->argc > 2) && ($a->argv[2] === 'view')) {
22                 $which = $a->user['nickname'];
23                 $profile = $a->argv[1];
24         }
25
26         Profile::load($a, $which, $profile);
27
28         $json_info = [
29                 'addr'     => $a->profile['addr'],
30                 'nick'     => $which,
31                 'guid'     => $a->profile['guid'],
32                 'key'      => $a->profile['pubkey'],
33                 'homepage' => System::baseUrl()."/profile/{$which}",
34                 'comm'     => ($a->profile['account-type'] == ACCOUNT_TYPE_COMMUNITY),
35         ];
36
37         if (!$a->profile['net-publish'] || $a->profile['hidewall']) {
38                 header('Content-type: application/json; charset=utf-8');
39                 $json_info["hide"] = true;
40                 echo json_encode($json_info);
41                 exit;
42         }
43
44         $keywords = ((x($a->profile, 'pub_keywords')) ? $a->profile['pub_keywords'] : '');
45         $keywords = str_replace(['#',',',' ',',,'], ['',' ',',',','], $keywords);
46         $keywords = explode(',', $keywords);
47
48         $contactPhoto = dba::selectFirst('contact', ['photo'], ['self' => true, 'uid' => $a->profile['uid']]);
49
50         $json_info['fn'] = $a->profile['name'];
51         $json_info['photo'] = $contactPhoto["photo"];
52         $json_info['tags'] = $keywords;
53
54         if (is_array($a->profile) && !$a->profile['hide-friends']) {
55                 /// @todo What should this value tell us?
56                 $r = q("SELECT `gcontact`.`updated` FROM `contact` INNER JOIN `gcontact` WHERE `gcontact`.`nurl` = `contact`.`nurl` AND `self` AND `uid` = %d LIMIT 1",
57                         intval($a->profile['uid']));
58                 if (DBM::is_result($r)) {
59                         $json_info["updated"] =  date("c", strtotime($r[0]['updated']));
60                 }
61
62                 $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 AND `hidden` = 0 AND `archive` = 0
63                                 AND `network` IN ('%s', '%s', '%s', '')",
64                         intval($a->profile['uid']),
65                         dbesc(NETWORK_DFRN),
66                         dbesc(NETWORK_DIASPORA),
67                         dbesc(NETWORK_OSTATUS)
68                 );
69                 if (DBM::is_result($r)) {
70                         $json_info["contacts"] = intval($r[0]['total']);
71                 }
72         }
73
74         // We display the last activity (post or login), reduced to year and week number
75         $last_active = 0;
76         $condition = ['uid' => $a->profile['uid'], 'self' => true];
77         $contact = dba::selectFirst('contact', ['last-item'], $condition);
78         if (DBM::is_result($contact)) {
79                 $last_active = strtotime($contact['last-item']);
80         }
81
82         $condition = ['uid' => $a->profile['uid']];
83         $user = dba::selectFirst('user', ['login_date'], $condition);
84         if (DBM::is_result($user)) {
85                 if ($last_active < strtotime($user['login_date'])) {
86                         $last_active = strtotime($user['login_date']);
87                 }
88         }
89         $json_info["last-activity"] = date("o-W", $last_active);
90
91         //These are optional fields.
92         $profile_fields = ['pdesc', 'locality', 'region', 'postal-code', 'country-name', 'gender', 'marital', 'about'];
93         foreach ($profile_fields as $field) {
94                 if (!empty($a->profile[$field])) {
95                         $json_info["$field"] = $a->profile[$field];
96                 }
97         }
98
99         $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
100         foreach ($dfrn_pages as $dfrn) {
101                 $json_info["dfrn-{$dfrn}"] = System::baseUrl()."/dfrn_{$dfrn}/{$which}";
102         }
103
104         //Output all the JSON!
105         header('Content-type: application/json; charset=utf-8');
106         echo json_encode($json_info);
107         exit;
108 }