]> git.mxchange.org Git - friendica.git/blob - mod/noscrape.php
Merge branch '2018.08-rc'
[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         ];
37
38         if (!$a->profile['net-publish'] || $a->profile['hidewall']) {
39                 header('Content-type: application/json; charset=utf-8');
40                 $json_info["hide"] = true;
41                 echo json_encode($json_info);
42                 exit;
43         }
44
45         $keywords = ((x($a->profile, 'pub_keywords')) ? $a->profile['pub_keywords'] : '');
46         $keywords = str_replace(['#',',',' ',',,'], ['',' ',',',','], $keywords);
47         $keywords = explode(',', $keywords);
48
49         $contactPhoto = DBA::selectFirst('contact', ['photo'], ['self' => true, 'uid' => $a->profile['uid']]);
50
51         $json_info['fn'] = $a->profile['name'];
52         $json_info['photo'] = $contactPhoto["photo"];
53         $json_info['tags'] = $keywords;
54
55         if (is_array($a->profile) && !$a->profile['hide-friends']) {
56                 /// @todo What should this value tell us?
57                 $r = q("SELECT `gcontact`.`updated` FROM `contact` INNER JOIN `gcontact` WHERE `gcontact`.`nurl` = `contact`.`nurl` AND `self` AND `uid` = %d LIMIT 1",
58                         intval($a->profile['uid']));
59                 if (DBA::isResult($r)) {
60                         $json_info["updated"] =  date("c", strtotime($r[0]['updated']));
61                 }
62
63                 $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
64                                 AND `network` IN ('%s', '%s', '%s', '')",
65                         intval($a->profile['uid']),
66                         DBA::escape(Protocol::DFRN),
67                         DBA::escape(Protocol::DIASPORA),
68                         DBA::escape(Protocol::OSTATUS)
69                 );
70                 if (DBA::isResult($r)) {
71                         $json_info["contacts"] = intval($r[0]['total']);
72                 }
73         }
74
75         // We display the last activity (post or login), reduced to year and week number
76         $last_active = 0;
77         $condition = ['uid' => $a->profile['uid'], 'self' => true];
78         $contact = DBA::selectFirst('contact', ['last-item'], $condition);
79         if (DBA::isResult($contact)) {
80                 $last_active = strtotime($contact['last-item']);
81         }
82
83         $condition = ['uid' => $a->profile['uid']];
84         $user = DBA::selectFirst('user', ['login_date'], $condition);
85         if (DBA::isResult($user)) {
86                 if ($last_active < strtotime($user['login_date'])) {
87                         $last_active = strtotime($user['login_date']);
88                 }
89         }
90         $json_info["last-activity"] = date("o-W", $last_active);
91
92         //These are optional fields.
93         $profile_fields = ['pdesc', 'locality', 'region', 'postal-code', 'country-name', 'gender', 'marital', 'about'];
94         foreach ($profile_fields as $field) {
95                 if (!empty($a->profile[$field])) {
96                         $json_info["$field"] = $a->profile[$field];
97                 }
98         }
99
100         $dfrn_pages = ['request', 'confirm', 'notify', 'poll'];
101         foreach ($dfrn_pages as $dfrn) {
102                 $json_info["dfrn-{$dfrn}"] = System::baseUrl()."/dfrn_{$dfrn}/{$which}";
103         }
104
105         //Output all the JSON!
106         header('Content-type: application/json; charset=utf-8');
107         echo json_encode($json_info);
108         exit;
109 }