]> git.mxchange.org Git - friendica.git/blob - mod/poco.php
53c7f5b0b7c89bdf7992dcfa070775f799e8de7e
[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 ((`last_contact` >= `last_failure`) OR (`updated` >= `last_failure`))  AND `network` IN ('%s')",
65                 $r = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure`  AND `network` IN ('%s', '%s', '%s')",
66                         dbesc($update_limit),
67                         dbesc(NETWORK_DFRN),
68                         dbesc(NETWORK_DIASPORA),
69                         dbesc(NETWORK_OSTATUS)
70                 );
71         } elseif($system_mode) {
72                 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s', '')
73                         AND `success_update` >= `failure_update`
74                         AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ",
75                         dbesc(NETWORK_DFRN),
76                         dbesc(NETWORK_DIASPORA),
77                         dbesc(NETWORK_OSTATUS),
78                         dbesc(NETWORK_STATUSNET)
79                         );
80         } else {
81                 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
82                         AND `success_update` >= `failure_update` AND `network` IN ('%s', '%s', '%s', '%s', '') $sql_extra",
83                         intval($user['uid']),
84                         dbesc(NETWORK_DFRN),
85                         dbesc(NETWORK_DIASPORA),
86                         dbesc(NETWORK_OSTATUS),
87                         dbesc(NETWORK_STATUSNET)
88                 );
89         }
90         if(count($r))
91                 $totalResults = intval($r[0]['total']);
92         else
93                 $totalResults = 0;
94
95         $startIndex = intval($_GET['startIndex']);
96         if(! $startIndex)
97                 $startIndex = 0;
98         $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
99
100
101         if ($global) {
102                 //$r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND `network` IN ('%s') AND ((`last_contact` >= `last_failure`) OR (`updated` > `last_failure`)) LIMIT %d, %d",
103                 $r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND `network` IN ('%s', '%1', '%1') and `updated` > `last_failure` LIMIT %d, %d",
104                         dbesc($update_limit),
105                         dbesc(NETWORK_DFRN),
106                         dbesc(NETWORK_DIASPORA),
107                         dbesc(NETWORK_OSTATUS),
108                         intval($startIndex),
109                         intval($itemsPerPage)
110                 );
111         } elseif($system_mode) {
112                 $r = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`, `profile`.`gender` AS `pgender`,
113                         `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`, `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`
114                         FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
115                         WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s', '') AND `profile`.`is-default`
116                         AND ((`contact`.`success_update` >= `contact`.`failure_update`) OR (`contact`.`last-item` >= `contact`.`failure_update`))
117                         AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
118                         dbesc(NETWORK_DFRN),
119                         dbesc(NETWORK_DIASPORA),
120                         dbesc(NETWORK_OSTATUS),
121                         dbesc(NETWORK_STATUSNET),
122                         intval($startIndex),
123                         intval($itemsPerPage)
124                 );
125         } else {
126                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
127                         AND `success_update` >= `failure_update` AND `network` IN ('%s', '%s', '%s', '%s', '') $sql_extra LIMIT %d, %d",
128                         intval($user['uid']),
129                         dbesc(NETWORK_DFRN),
130                         dbesc(NETWORK_DIASPORA),
131                         dbesc(NETWORK_OSTATUS),
132                         dbesc(NETWORK_STATUSNET),
133                         intval($startIndex),
134                         intval($itemsPerPage)
135                 );
136         }
137
138         $ret = array();
139         if(x($_GET,'sorted'))
140                 $ret['sorted'] = false;
141         if(x($_GET,'filtered'))
142                 $ret['filtered'] = false;
143         if(x($_GET,'updatedSince') AND !$global)
144                 $ret['updatedSince'] = false;
145
146         $ret['startIndex']   = (int) $startIndex;
147         $ret['itemsPerPage'] = (int) $itemsPerPage;
148         $ret['totalResults'] = (int) $totalResults;
149         $ret['entry']        = array();
150
151
152         $fields_ret = array(
153                 'id' => false,
154                 'displayName' => false,
155                 'urls' => false,
156                 'updated' => false,
157                 'preferredUsername' => false,
158                 'photos' => false,
159                 'aboutMe' => false,
160                 'currentLocation' => false,
161                 'network' => false,
162                 'gender' => false,
163                 'tags' => false,
164                 'address' => false,
165                 'generation' => false
166         );
167
168         if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
169                 foreach($fields_ret as $k => $v)
170                         $fields_ret[$k] = true;
171         else {
172                 $fields_req = explode(',',$_GET['fields']);
173                 foreach($fields_req as $f)
174                         $fields_ret[trim($f)] = true;
175         }
176
177         if(is_array($r)) {
178                 if(count($r)) {
179                         foreach($r as $rr) {
180                                 if (!isset($rr['generation'])) {
181                                         if ($global)
182                                                 $rr['generation'] = 3;
183                                         elseif ($system_mode)
184                                                 $rr['generation'] = 1;
185                                         else
186                                                 $rr['generation'] = 2;
187                                 }
188
189                                 if (($rr['about'] == "") AND isset($rr['pabout']))
190                                         $rr['about'] = $rr['pabout'];
191
192                                 if ($rr['location'] == "") {
193                                         if (isset($rr['plocation']))
194                                                 $rr['location'] = $rr['plocation'];
195
196                                         if (isset($rr['pregion']) AND ($rr['pregion'] != "")) {
197                                                 if ($rr['location'] != "")
198                                                         $rr['location'] .= ", ";
199
200                                                 $rr['location'] .= $rr['pregion'];
201                                         }
202
203                                         if (isset($rr['pcountry']) AND ($rr['pcountry'] != "")) {
204                                                 if ($rr['location'] != "")
205                                                         $rr['location'] .= ", ";
206
207                                                 $rr['location'] .= $rr['pcountry'];
208                                         }
209                                 }
210
211                                 if (($rr['gender'] == "") AND isset($rr['pgender']))
212                                         $rr['gender'] = $rr['pgender'];
213
214                                 if (($rr['keywords'] == "") AND isset($rr['pub_keywords']))
215                                         $rr['keywords'] = $rr['pub_keywords'];
216
217                                 $entry = array();
218                                 if($fields_ret['id'])
219                                         $entry['id'] = (int)$rr['id'];
220                                 if($fields_ret['displayName'])
221                                         $entry['displayName'] = $rr['name'];
222                                 if($fields_ret['aboutMe'])
223                                         $entry['aboutMe'] = bbcode($rr['about'], false, false);
224                                 if($fields_ret['currentLocation'])
225                                         $entry['currentLocation'] = $rr['location'];
226                                 if($fields_ret['gender'])
227                                         $entry['gender'] = $rr['gender'];
228                                 if($fields_ret['generation'])
229                                         $entry['generation'] = (int)$rr['generation'];
230                                 if($fields_ret['urls']) {
231                                         $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
232                                         if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
233                                                 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
234                                 }
235                                 if($fields_ret['preferredUsername'])
236                                         $entry['preferredUsername'] = $rr['nick'];
237                                 if($fields_ret['updated']) {
238                                         if (!$global) {
239                                                 $entry['updated'] = $rr['success_update'];
240
241                                                 if ($rr['name-date'] > $entry['updated'])
242                                                         $entry['updated'] = $rr['name-date'];
243
244                                                 if ($rr['uri-date'] > $entry['updated'])
245                                                         $entry['updated'] = $rr['uri-date'];
246
247                                                 if ($rr['avatar-date'] > $entry['updated'])
248                                                         $entry['updated'] = $rr['avatar-date'];
249                                         } else
250                                                 $entry['updated'] = $rr['updated'];
251
252                                         $entry['updated'] = date("c", strtotime($entry['updated']));
253                                 }
254                                 if($fields_ret['photos'])
255                                         $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
256                                 if($fields_ret['network']) {
257                                         $entry['network'] = $rr['network'];
258                                         if ($entry['network'] == NETWORK_STATUSNET)
259                                                 $entry['network'] = NETWORK_OSTATUS;
260                                         if (($entry['network'] == "") AND ($rr['self']))
261                                                 $entry['network'] = NETWORK_DFRN;
262                                 }
263                                 if($fields_ret['tags']) {
264                                         $tags = str_replace(","," ",$rr['keywords']);
265                                         $tags = explode(" ", $tags);
266
267                                         $cleaned = array();
268                                         foreach ($tags as $tag) {
269                                                 $tag = trim(strtolower($tag));
270                                                 if ($tag != "")
271                                                         $cleaned[] = $tag;
272                                         }
273
274                                         $entry['tags'] = array($cleaned);
275                                 }
276                                 if($fields_ret['address']) {
277                                         $entry['address'] = array();
278
279                                         // Deactivated. It just reveals too much data. (Although its from the default profile)
280                                         //if (isset($rr['paddress']))
281                                         //       $entry['address']['streetAddress'] = $rr['paddress'];
282
283                                         if (isset($rr['plocation']))
284                                                  $entry['address']['locality'] = $rr['plocation'];
285
286                                         if (isset($rr['pregion']))
287                                                  $entry['address']['region'] = $rr['pregion'];
288
289                                         // See above
290                                         //if (isset($rr['ppostalcode']))
291                                         //       $entry['address']['postalCode'] = $rr['ppostalcode'];
292
293                                         if (isset($rr['pcountry']))
294                                                  $entry['address']['country'] = $rr['pcountry'];
295                                 }
296
297                                 $ret['entry'][] = $entry;
298                         }
299                 }
300                 else
301                         $ret['entry'][] = array();
302         }
303         else
304                 http_status_exit(500);
305
306         if($format === 'xml') {
307                 header('Content-type: text/xml');
308                 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
309                 killme();
310         }
311         if($format === 'json') {
312                 header('Content-type: application/json');
313                 echo json_encode($ret);
314                 killme();
315         }
316         else
317                 http_status_exit(500);
318
319
320 }