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