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