]> git.mxchange.org Git - friendica.git/blob - mod/poco.php
hmmm - why won't poco xml work
[friendica.git] / mod / poco.php
1 <?php
2
3 function poco_init(&$a) {
4
5         if($a->argc > 1) {
6                 $user = notags(trim($a->argv[1]));
7         }
8         if(! x($user) || get_config('system','block_public'))
9                 http_status_exit(401);
10
11         $format = (($_GET['format']) ? $_GET['format'] : 'json');
12
13         $justme = false;
14
15         if($a->argc > 2 && $a->argv[2] === '@me')
16                 $justme = true;
17         if($a->argc > 3 && $a->argv[3] === '@all')
18                 $justme = false;
19         if($a->argc > 3 && $a->argv[3] === '@self')
20                 $justme = true;
21         if($a->argc > 4 && intval($a->argv[4]) && $justme == false)
22                 $cid = intval($a->argv[4]);
23                 
24
25         $r = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
26                 where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
27                 dbesc($user)
28         );
29         if(! count($r) || $r[0]['hidewall'] || $r[0]['hide-friends'])
30                 http_status_exit(404);
31
32         $user = $r[0];
33
34         if($justme)
35                 $sql_extra = " and `contact`.`self` = 1 ";
36
37         if($cid)
38                 $sql_extra = sprintf(" and `contact`.`id` = %d ",intval($cid));
39
40         $r = q("SELECT count(*) as `total` from `contact` where `uid` = %d and blocked = 0 and pending = 0
41                 $sql_extra ",
42                 intval($user['uid'])
43         );
44         if(count($r))
45                 $totalResults = intval($r[0]['total']);
46         else
47                 $totalResults = 0;
48
49         $startIndex = intval($_GET['startIndex']);
50         if(! $startIndex)
51                 $startIndex = 0;
52         $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
53
54         $r = q("SELECT * from `contact` where `uid` = %d and blocked = 0 and pending = 0
55                 $sql_extra LIMIT %d, %d",
56                 intval($user['uid']),
57                 intval($startIndex),
58                 intval($itemsPerPage)
59         );
60
61         $ret = array();
62         if(x($_GET,'sorted'))
63                 $ret['sorted'] = 'false';
64         if(x($_GET,'filtered'))
65                 $ret['filtered'] = 'false';
66         if(x($_GET,'updatedSince'))
67                 $ret['updateSince'] = 'false';
68
69         $ret['startIndex']   = (string) $startIndex;
70         $ret['itemsPerPage'] = (string) $itemsPerPage;
71         $ret['totalResults'] = (string) $totalResults;
72         $ret['entry']        = array();
73
74
75         $fields_ret = array(
76                 'id' => false,
77                 'displayName' => false,
78                 'urls' => false,
79                 'preferredName' => false,
80                 'photos' => false
81         );
82
83         if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
84                 foreach($fields_ret as $k => $v)
85                         $fields_ret[$k] = true;
86         else {
87                 $fields_req = explode(',',$_GET['fields']);
88                 foreach($fields_req as $f)
89                         $fields_ret[trim($f)] = true;
90         }
91
92         if(is_array($r)) {
93                 if(count($r)) {
94                         foreach($r as $rr) {
95                                 $entry = array();
96                                 if($fields_ret['id'])
97                                         $entry['id'] = $rr['id'];
98                                 if($fields_ret['displayName'])
99                                         $entry['displayName'] = $rr['name'];
100                                 if($fields_ret['urls'])
101                                         $entry['urls'] = array('value' => $rr['url'], 'type' => 'profile');
102                                 if($fields_ret['preferredUsername'])
103                                         $entry['preferredUsername'] = $rr['nick'];
104                                 if($fields_ret['photos'])
105                                         $entry['photos'] = array('value' => $rr['photo'], 'type' => 'profile');
106                                 $ret['entry'][] = $entry;
107                         }
108                 }
109                 else
110                         $ret['entry'][] = array();
111         }
112         else
113                 http_status_exit(500);
114
115         if($format === 'xml') {
116                 header('Content-type: text/xml');
117                 echo replace_macros(get_markup_template('poco_xml.tpl',array_xmlify(array('$response' => $ret))));
118                 http_status_exit(500);
119         }
120         if($format === 'json') {
121                 header('Content-type: application/json');
122                 echo json_encode($ret);
123                 killme();       
124         }
125         else
126                 http_status_exit(500);
127
128
129 }