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