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