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