]> git.mxchange.org Git - friendica.git/blob - mod/poco.php
Bugfix: The user id wasn't fetched correctly.
[friendica.git] / mod / poco.php
1 <?php
2
3 function poco_init(&$a) {
4         require_once("include/bbcode.php");
5
6         $system_mode = false;
7
8         if(intval(get_config('system','block_public')) || (get_config('system','block_local_dir')))
9                 http_status_exit(401);
10
11
12         if($a->argc > 1) {
13                 $user = notags(trim($a->argv[1]));
14         }
15         if(! x($user)) {
16                 $c = q("SELECT * FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1");
17                 if(! count($c))
18                         http_status_exit(401);
19                 $system_mode = true;
20         }
21
22         $format = (($_GET['format']) ? $_GET['format'] : 'json');
23
24         $justme = false;
25         $global = false;
26
27         if($a->argc > 1 && $a->argv[1] === '@global') {
28                 $global = true;
29                 $update_limit = date("Y-m-d H:i:s", time() - 30 * 86400);
30         }
31         if($a->argc > 2 && $a->argv[2] === '@me')
32                 $justme = true;
33         if($a->argc > 3 && $a->argv[3] === '@all')
34                 $justme = false;
35         if($a->argc > 3 && $a->argv[3] === '@self')
36                 $justme = true;
37         if($a->argc > 4 && intval($a->argv[4]) && $justme == false)
38                 $cid = intval($a->argv[4]);
39
40
41         if(!$system_mode AND !$global) {
42                 $r = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
43                         where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
44                         dbesc($user)
45                 );
46                 if(! count($r) || $r[0]['hidewall'] || $r[0]['hide-friends'])
47                         http_status_exit(404);
48
49                 $user = $r[0];
50         }
51
52         if($justme)
53                 $sql_extra = " AND `contact`.`self` = 1 ";
54 //      else
55 //              $sql_extra = " AND `contact`.`self` = 0 ";
56
57         if($cid)
58                 $sql_extra = sprintf(" AND `contact`.`id` = %d ",intval($cid));
59
60         if(x($_GET,'updatedSince'))
61                 $update_limit =  date("Y-m-d H:i:s",strtotime($_GET['updatedSince']));
62
63         if ($global) {
64                 $r = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `network` IN ('%s')",
65                         dbesc($update_limit),
66                         dbesc(NETWORK_DFRN)
67                 );
68         } elseif($system_mode) {
69                 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s', '')
70                         AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ",
71                         dbesc(NETWORK_DFRN),
72                         dbesc(NETWORK_DIASPORA),
73                         dbesc(NETWORK_OSTATUS),
74                         dbesc(NETWORK_STATUSNET)
75                         );
76         } else {
77                 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
78                         AND `network` IN ('%s', '%s', '%s', '%s', '') $sql_extra",
79                         intval($user['uid']),
80                         dbesc(NETWORK_DFRN),
81                         dbesc(NETWORK_DIASPORA),
82                         dbesc(NETWORK_OSTATUS),
83                         dbesc(NETWORK_STATUSNET)
84                 );
85         }
86         if(count($r))
87                 $totalResults = intval($r[0]['total']);
88         else
89                 $totalResults = 0;
90
91         $startIndex = intval($_GET['startIndex']);
92         if(! $startIndex)
93                 $startIndex = 0;
94         $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
95
96
97         if ($global) {
98                 $r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND `network` IN ('%s') LIMIT %d, %d",
99                         dbesc($update_limit),
100                         dbesc(NETWORK_DFRN),
101                         intval($startIndex),
102                         intval($itemsPerPage)
103                 );
104         } elseif($system_mode) {
105                 $r = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`, `profile`.`gender` AS `pgender`,
106                         `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`, `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`
107                         FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
108                         WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s', '') AND `profile`.`is-default`
109                         AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
110                         dbesc(NETWORK_DFRN),
111                         dbesc(NETWORK_DIASPORA),
112                         dbesc(NETWORK_OSTATUS),
113                         dbesc(NETWORK_STATUSNET),
114                         intval($startIndex),
115                         intval($itemsPerPage)
116                 );
117         } else {
118                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
119                         AND `network` IN ('%s', '%s', '%s', '%s', '') $sql_extra LIMIT %d, %d",
120                         intval($user['uid']),
121                         dbesc(NETWORK_DFRN),
122                         dbesc(NETWORK_DIASPORA),
123                         dbesc(NETWORK_OSTATUS),
124                         dbesc(NETWORK_STATUSNET),
125                         intval($startIndex),
126                         intval($itemsPerPage)
127                 );
128         }
129
130         $ret = array();
131         if(x($_GET,'sorted'))
132                 $ret['sorted'] = false;
133         if(x($_GET,'filtered'))
134                 $ret['filtered'] = false;
135         if(x($_GET,'updatedSince') AND !$global)
136                 $ret['updatedSince'] = false;
137
138         $ret['startIndex']   = (int) $startIndex;
139         $ret['itemsPerPage'] = (int) $itemsPerPage;
140         $ret['totalResults'] = (int) $totalResults;
141         $ret['entry']        = array();
142
143
144         $fields_ret = array(
145                 'id' => false,
146                 'displayName' => false,
147                 'urls' => false,
148                 'updated' => false,
149                 'preferredUsername' => false,
150                 'photos' => false,
151                 'aboutMe' => false,
152                 'currentLocation' => false,
153                 'network' => false,
154                 'gender' => false,
155                 'tags' => false,
156                 'address' => false,
157                 'generation' => false
158         );
159
160         if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
161                 foreach($fields_ret as $k => $v)
162                         $fields_ret[$k] = true;
163         else {
164                 $fields_req = explode(',',$_GET['fields']);
165                 foreach($fields_req as $f)
166                         $fields_ret[trim($f)] = true;
167         }
168
169         if(is_array($r)) {
170                 if(count($r)) {
171                         foreach($r as $rr) {
172                                 if (!isset($rr['generation'])) {
173                                         if ($global)
174                                                 $rr['generation'] = 3;
175                                         elseif ($system_mode)
176                                                 $rr['generation'] = 1;
177                                         else
178                                                 $rr['generation'] = 2;
179                                 }
180
181                                 if (($rr['about'] == "") AND isset($rr['pabout']))
182                                         $rr['about'] = $rr['pabout'];
183
184                                 if ($rr['location'] == "") {
185                                         if (isset($rr['plocation']))
186                                                 $rr['location'] = $rr['plocation'];
187
188                                         if (isset($rr['pregion']) AND ($rr['pregion'] != "")) {
189                                                 if ($rr['location'] != "")
190                                                         $rr['location'] .= ", ";
191
192                                                 $rr['location'] .= $rr['pregion'];
193                                         }
194
195                                         if (isset($rr['pcountry']) AND ($rr['pcountry'] != "")) {
196                                                 if ($rr['location'] != "")
197                                                         $rr['location'] .= ", ";
198
199                                                 $rr['location'] .= $rr['pcountry'];
200                                         }
201                                 }
202
203                                 if (($rr['gender'] == "") AND isset($rr['pgender']))
204                                         $rr['gender'] = $rr['pgender'];
205
206                                 if (($rr['keywords'] == "") AND isset($rr['pub_keywords']))
207                                         $rr['keywords'] = $rr['pub_keywords'];
208
209                                 $entry = array();
210                                 if($fields_ret['id'])
211                                         $entry['id'] = (int)$rr['id'];
212                                 if($fields_ret['displayName'])
213                                         $entry['displayName'] = $rr['name'];
214                                 if($fields_ret['aboutMe'])
215                                         $entry['aboutMe'] = bbcode($rr['about'], false, false);
216                                 if($fields_ret['currentLocation'])
217                                         $entry['currentLocation'] = $rr['location'];
218                                 if($fields_ret['gender'])
219                                         $entry['gender'] = $rr['gender'];
220                                 if($fields_ret['generation'])
221                                         $entry['generation'] = (int)$rr['generation'];
222                                 if($fields_ret['urls']) {
223                                         $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
224                                         if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
225                                                 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
226                                 }
227                                 if($fields_ret['preferredUsername'])
228                                         $entry['preferredUsername'] = $rr['nick'];
229                                 if($fields_ret['updated']) {
230                                         if (!$global) {
231                                                 $entry['updated'] = $rr['success_update'];
232
233                                                 if ($rr['name-date'] > $entry['updated'])
234                                                         $entry['updated'] = $rr['name-date'];
235
236                                                 if ($rr['uri-date'] > $entry['updated'])
237                                                         $entry['updated'] = $rr['uri-date'];
238
239                                                 if ($rr['avatar-date'] > $entry['updated'])
240                                                         $entry['updated'] = $rr['avatar-date'];
241                                         } else
242                                                 $entry['updated'] = $rr['updated'];
243
244                                         $entry['updated'] = date("c", strtotime($entry['updated']));
245                                 }
246                                 if($fields_ret['photos'])
247                                         $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
248                                 if($fields_ret['network']) {
249                                         $entry['network'] = $rr['network'];
250                                         if ($entry['network'] == NETWORK_STATUSNET)
251                                                 $entry['network'] = NETWORK_OSTATUS;
252                                         if (($entry['network'] == "") AND ($rr['self']))
253                                                 $entry['network'] = NETWORK_DFRN;
254                                 }
255                                 if($fields_ret['tags']) {
256                                         $tags = str_replace(","," ",$rr['keywords']);
257                                         $tags = explode(" ", $tags);
258
259                                         $cleaned = array();
260                                         foreach ($tags as $tag) {
261                                                 $tag = trim(strtolower($tag));
262                                                 if ($tag != "")
263                                                         $cleaned[] = $tag;
264                                         }
265
266                                         $entry['tags'] = array($cleaned);
267                                 }
268                                 if($fields_ret['address']) {
269                                         $entry['address'] = array();
270
271                                         // Deactivated. It just reveals too much data. (Although its from the default profile)
272                                         //if (isset($rr['paddress']))
273                                         //       $entry['address']['streetAddress'] = $rr['paddress'];
274
275                                         if (isset($rr['plocation']))
276                                                  $entry['address']['locality'] = $rr['plocation'];
277
278                                         if (isset($rr['pregion']))
279                                                  $entry['address']['region'] = $rr['pregion'];
280
281                                         // See above
282                                         //if (isset($rr['ppostalcode']))
283                                         //       $entry['address']['postalCode'] = $rr['ppostalcode'];
284
285                                         if (isset($rr['pcountry']))
286                                                  $entry['address']['country'] = $rr['pcountry'];
287                                 }
288
289                                 $ret['entry'][] = $entry;
290                         }
291                 }
292                 else
293                         $ret['entry'][] = array();
294         }
295         else
296                 http_status_exit(500);
297
298         if($format === 'xml') {
299                 header('Content-type: text/xml');
300                 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
301                 killme();
302         }
303         if($format === 'json') {
304                 header('Content-type: application/json');
305                 echo json_encode($ret);
306                 killme();
307         }
308         else
309                 http_status_exit(500);
310
311
312 }