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