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