]> git.mxchange.org Git - friendica.git/blob - mod/poco.php
rev update
[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
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
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                 'preferredUsername' => false,
110                 'photos' => false
111         );
112
113         if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
114                 foreach($fields_ret as $k => $v)
115                         $fields_ret[$k] = true;
116         else {
117                 $fields_req = explode(',',$_GET['fields']);
118                 foreach($fields_req as $f)
119                         $fields_ret[trim($f)] = true;
120         }
121
122         if(is_array($r)) {
123                 if(count($r)) {
124                         foreach($r as $rr) {
125                                 $entry = array();
126                                 if($fields_ret['id'])
127                                         $entry['id'] = $rr['id'];
128                                 if($fields_ret['displayName'])
129                                         $entry['displayName'] = $rr['name'];
130                                 if($fields_ret['urls']) {
131                                         $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
132                                         if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
133                                                 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');  
134                                 }
135                                 if($fields_ret['preferredUsername'])
136                                         $entry['preferredUsername'] = $rr['nick'];
137                                 if($fields_ret['photos'])
138                                         $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
139                                 $ret['entry'][] = $entry;
140                         }
141                 }
142                 else
143                         $ret['entry'][] = array();
144         }
145         else
146                 http_status_exit(500);
147
148         if($format === 'xml') {
149                 header('Content-type: text/xml');
150                 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
151                 http_status_exit(500);
152         }
153         if($format === 'json') {
154                 header('Content-type: application/json');
155                 echo json_encode($ret);
156                 killme();       
157         }
158         else
159                 http_status_exit(500);
160
161
162 }