]> git.mxchange.org Git - friendica.git/blob - mod/poco.php
Merge pull request #1274 from annando/issue-1218
[friendica.git] / mod / poco.php
1 <?php
2
3 function poco_init(&$a) {
4
5         $system_mode = false;
6
7         if(intval(get_config('system','block_public')))
8                 http_status_exit(401);
9
10
11         if($a->argc > 1) {
12                 $user = notags(trim($a->argv[1]));
13         }
14         if(! x($user)) {
15                 $c = q("SELECT * FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1");
16                 if(! count($c))
17                         http_status_exit(401);
18                 $system_mode = true;
19         }
20
21         $format = (($_GET['format']) ? $_GET['format'] : 'json');
22
23         $justme = false;
24
25         if($a->argc > 2 && $a->argv[2] === '@me')
26                 $justme = true;
27         if($a->argc > 3 && $a->argv[3] === '@all')
28                 $justme = false;
29         if($a->argc > 3 && $a->argv[3] === '@self')
30                 $justme = true;
31         if($a->argc > 4 && intval($a->argv[4]) && $justme == false)
32                 $cid = intval($a->argv[4]);
33
34
35         if(! $system_mode) {
36                 $r = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
37                         where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
38                         dbesc($user)
39                 );
40                 if(! count($r) || $r[0]['hidewall'] || $r[0]['hide-friends'])
41                         http_status_exit(404);
42
43                 $user = $r[0];
44         }
45
46         if($justme)
47                 $sql_extra = " AND `contact`.`self` = 1 ";
48         else
49                 $sql_extra = " AND `contact`.`self` = 0 ";
50
51         if($cid)
52                 $sql_extra = sprintf(" AND `contact`.`id` = %d ",intval($cid));
53
54         if($system_mode) {
55                 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s', '')
56                         AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ",
57                         dbesc(NETWORK_DFRN),
58                         dbesc(NETWORK_DIASPORA),
59                         dbesc(NETWORK_OSTATUS),
60                         dbesc(NETWORK_STATUSNET)
61                         );
62         }
63         else {
64                 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
65                         AND `network` IN ('%s', '%s', '%s', '%s', '') $sql_extra",
66                         intval($user['uid']),
67                         dbesc(NETWORK_DFRN),
68                         dbesc(NETWORK_DIASPORA),
69                         dbesc(NETWORK_OSTATUS),
70                         dbesc(NETWORK_STATUSNET)
71                 );
72         }
73         if(count($r))
74                 $totalResults = intval($r[0]['total']);
75         else
76                 $totalResults = 0;
77
78         $startIndex = intval($_GET['startIndex']);
79         if(! $startIndex)
80                 $startIndex = 0;
81         $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
82
83
84         if($system_mode) {
85                 $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s', '')
86                         AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
87                         dbesc(NETWORK_DFRN),
88                         dbesc(NETWORK_DIASPORA),
89                         dbesc(NETWORK_OSTATUS),
90                         dbesc(NETWORK_STATUSNET),
91                         intval($startIndex),
92                         intval($itemsPerPage)
93                 );
94         }
95         else {
96                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
97                         AND `network` IN ('%s', '%s', '%s', '%s', '') $sql_extra LIMIT %d, %d",
98                         intval($user['uid']),
99                         dbesc(NETWORK_DFRN),
100                         dbesc(NETWORK_DIASPORA),
101                         dbesc(NETWORK_OSTATUS),
102                         dbesc(NETWORK_STATUSNET),
103                         intval($startIndex),
104                         intval($itemsPerPage)
105                 );
106         }
107         $ret = array();
108         if(x($_GET,'sorted'))
109                 $ret['sorted'] = 'false';
110         if(x($_GET,'filtered'))
111                 $ret['filtered'] = 'false';
112         if(x($_GET,'updatedSince'))
113                 $ret['updateSince'] = 'false';
114
115         $ret['startIndex']   = (string) $startIndex;
116         $ret['itemsPerPage'] = (string) $itemsPerPage;
117         $ret['totalResults'] = (string) $totalResults;
118         $ret['entry']        = array();
119
120
121         $fields_ret = array(
122                 'id' => false,
123                 'displayName' => false,
124                 'urls' => false,
125                 'updated' => false,
126                 'preferredUsername' => false,
127                 'photos' => false,
128                 'network' => false
129         );
130
131         if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
132                 foreach($fields_ret as $k => $v)
133                         $fields_ret[$k] = true;
134         else {
135                 $fields_req = explode(',',$_GET['fields']);
136                 foreach($fields_req as $f)
137                         $fields_ret[trim($f)] = true;
138         }
139
140         if(is_array($r)) {
141                 if(count($r)) {
142                         foreach($r as $rr) {
143                                 $entry = array();
144                                 if($fields_ret['id'])
145                                         $entry['id'] = $rr['id'];
146                                 if($fields_ret['displayName'])
147                                         $entry['displayName'] = $rr['name'];
148                                 if($fields_ret['urls']) {
149                                         $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
150                                         if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
151                                                 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
152                                 }
153                                 if($fields_ret['preferredUsername'])
154                                         $entry['preferredUsername'] = $rr['nick'];
155                                 if($fields_ret['updated']) {
156                                         $entry['updated'] = $rr['success_update'];
157
158                                         if ($rr['name-date'] > $entry['updated'])
159                                                 $entry['updated'] = $rr['name-date'];
160
161                                         if ($rr['uri-date'] > $entry['updated'])
162                                                 $entry['updated'] = $rr['uri-date'];
163
164                                         if ($rr['avatar-date'] > $entry['updated'])
165                                                 $entry['updated'] = $rr['avatar-date'];
166
167                                         $entry['updated'] = date("c", strtotime($entry['updated']));
168                                 }
169                                 if($fields_ret['photos'])
170                                         $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
171                                 if($fields_ret['network']) {
172                                         $entry['network'] = $rr['network'];
173                                         if ($entry['network'] == NETWORK_STATUSNET)
174                                                 $entry['network'] = NETWORK_OSTATUS;
175                                 }
176                                 $ret['entry'][] = $entry;
177                         }
178                 }
179                 else
180                         $ret['entry'][] = array();
181         }
182         else
183                 http_status_exit(500);
184
185         if($format === 'xml') {
186                 header('Content-type: text/xml');
187                 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
188                 killme();
189         }
190         if($format === 'json') {
191                 header('Content-type: application/json');
192                 echo json_encode($ret);
193                 killme();
194         }
195         else
196                 http_status_exit(500);
197
198
199 }