]> git.mxchange.org Git - friendica.git/blob - mod/poco.php
Merge remote-tracking branch 'upstream/develop' into 1501-enhanced-poco
[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 
56                         and uid in (select uid from pconfig where cat = 'system' and k = 'suggestme' and v = 1) ");
57         }
58         else {
59                 $r = q("SELECT count(*) as `total` from `contact` where `uid` = %d and blocked = 0 and pending = 0 and hidden = 0 and archive = 0
60                         $sql_extra ",
61                         intval($user['uid'])
62                 );
63         }
64         if(count($r))
65                 $totalResults = intval($r[0]['total']);
66         else
67                 $totalResults = 0;
68
69         $startIndex = intval($_GET['startIndex']);
70         if(! $startIndex)
71                 $startIndex = 0;
72         $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
73
74
75         if($system_mode) {
76                 $r = q("SELECT * from contact where self = 1 
77                         and uid in (select uid from pconfig where cat = 'system' and k = 'suggestme' and v = 1) limit %d, %d ",
78                         intval($startIndex),
79                         intval($itemsPerPage)
80                 );
81         }
82         else {
83
84                 $r = q("SELECT * from `contact` where `uid` = %d and blocked = 0 and pending = 0 and hidden = 0 and archive = 0
85                         $sql_extra LIMIT %d, %d",
86                         intval($user['uid']),
87                         intval($startIndex),
88                         intval($itemsPerPage)
89                 );
90         }
91         $ret = array();
92         if(x($_GET,'sorted'))
93                 $ret['sorted'] = 'false';
94         if(x($_GET,'filtered'))
95                 $ret['filtered'] = 'false';
96         if(x($_GET,'updatedSince'))
97                 $ret['updateSince'] = 'false';
98
99         $ret['startIndex']   = (string) $startIndex;
100         $ret['itemsPerPage'] = (string) $itemsPerPage;
101         $ret['totalResults'] = (string) $totalResults;
102         $ret['entry']        = array();
103
104
105         $fields_ret = array(
106                 'id' => false,
107                 'displayName' => false,
108                 'urls' => false,
109                 'updated' => false,
110                 'preferredUsername' => false,
111                 'photos' => false
112         );
113
114         if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
115                 foreach($fields_ret as $k => $v)
116                         $fields_ret[$k] = true;
117         else {
118                 $fields_req = explode(',',$_GET['fields']);
119                 foreach($fields_req as $f)
120                         $fields_ret[trim($f)] = true;
121         }
122
123         if(is_array($r)) {
124                 if(count($r)) {
125                         foreach($r as $rr) {
126                                 $entry = array();
127                                 if($fields_ret['id'])
128                                         $entry['id'] = $rr['id'];
129                                 if($fields_ret['displayName'])
130                                         $entry['displayName'] = $rr['name'];
131                                 if($fields_ret['urls']) {
132                                         $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
133                                         if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
134                                                 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
135                                 }
136                                 if($fields_ret['preferredUsername'])
137                                         $entry['preferredUsername'] = $rr['nick'];
138                                 if($fields_ret['updated']) {
139                                         $entry['updated'] = $rr['success_update'];
140
141                                         if ($rr['name-date'] > $entry['updated'])
142                                                 $entry['updated'] = $rr['name-date'];
143
144                                         if ($rr['uri-date'] > $entry['updated'])
145                                                 $entry['updated'] = $rr['uri-date'];
146
147                                         if ($rr['avatar-date'] > $entry['updated'])
148                                                 $entry['updated'] = $rr['avatar-date'];
149
150                                         $entry['updated'] = date("c", strtotime($entry['updated']));
151                                 }
152                                 if($fields_ret['photos'])
153                                         $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
154                                 $ret['entry'][] = $entry;
155                         }
156                 }
157                 else
158                         $ret['entry'][] = array();
159         }
160         else
161                 http_status_exit(500);
162
163         if($format === 'xml') {
164                 header('Content-type: text/xml');
165                 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
166                 killme();
167         }
168         if($format === 'json') {
169                 header('Content-type: application/json');
170                 echo json_encode($ret);
171                 killme();
172         }
173         else
174                 http_status_exit(500);
175
176
177 }