3 if(! function_exists('poco_init')) {
4 function poco_init(&$a) {
5 require_once("include/bbcode.php");
9 if(intval(get_config('system','block_public')) || (get_config('system','block_local_dir')))
10 http_status_exit(401);
14 $user = notags(trim($a->argv[1]));
17 $c = q("SELECT * FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1");
19 http_status_exit(401);
23 $format = (($_GET['format']) ? $_GET['format'] : 'json');
28 if($a->argc > 1 && $a->argv[1] === '@global') {
30 $update_limit = date("Y-m-d H:i:s", time() - 30 * 86400);
32 if($a->argc > 2 && $a->argv[2] === '@me')
34 if($a->argc > 3 && $a->argv[3] === '@all')
36 if($a->argc > 3 && $a->argv[3] === '@self')
38 if($a->argc > 4 && intval($a->argv[4]) && $justme == false)
39 $cid = intval($a->argv[4]);
42 if(!$system_mode AND !$global) {
43 $r = q("SELECT `user`.*,`profile`.`hide-friends` from user left join profile on `user`.`uid` = `profile`.`uid`
44 where `user`.`nickname` = '%s' and `profile`.`is-default` = 1 limit 1",
47 if(! count($r) || $r[0]['hidewall'] || $r[0]['hide-friends'])
48 http_status_exit(404);
54 $sql_extra = " AND `contact`.`self` = 1 ";
56 // $sql_extra = " AND `contact`.`self` = 0 ";
59 $sql_extra = sprintf(" AND `contact`.`id` = %d ",intval($cid));
61 if(x($_GET,'updatedSince'))
62 $update_limit = date("Y-m-d H:i:s",strtotime($_GET['updatedSince']));
65 $r = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `updated` >= '%s' AND `updated` >= `last_failure` AND NOT `hide` AND `network` IN ('%s', '%s', '%s')",
68 dbesc(NETWORK_DIASPORA),
69 dbesc(NETWORK_OSTATUS)
71 } elseif($system_mode) {
72 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `self` = 1
73 AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ");
75 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
76 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
77 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
80 dbesc(NETWORK_DIASPORA),
81 dbesc(NETWORK_OSTATUS),
82 dbesc(NETWORK_STATUSNET)
86 $totalResults = intval($r[0]['total']);
90 $startIndex = intval($_GET['startIndex']);
93 $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
96 logger("Start global query", LOGGER_DEBUG);
97 $r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND NOT `hide` AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
98 ORDER BY `updated` DESC LIMIT %d, %d",
101 dbesc(NETWORK_DIASPORA),
102 dbesc(NETWORK_OSTATUS),
104 intval($itemsPerPage)
106 } elseif($system_mode) {
107 logger("Start system mode query", LOGGER_DEBUG);
108 $r = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`, `profile`.`gender` AS `pgender`,
109 `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`, `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`
110 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
111 WHERE `self` = 1 AND `profile`.`is-default`
112 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
114 intval($itemsPerPage)
117 logger("Start query for user ".$user['nickname'], LOGGER_DEBUG);
118 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
119 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
120 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
121 intval($user['uid']),
123 dbesc(NETWORK_DIASPORA),
124 dbesc(NETWORK_OSTATUS),
125 dbesc(NETWORK_STATUSNET),
127 intval($itemsPerPage)
130 logger("Query done", LOGGER_DEBUG);
133 if(x($_GET,'sorted'))
134 $ret['sorted'] = false;
135 if(x($_GET,'filtered'))
136 $ret['filtered'] = false;
137 if(x($_GET,'updatedSince') AND !$global)
138 $ret['updatedSince'] = false;
140 $ret['startIndex'] = (int) $startIndex;
141 $ret['itemsPerPage'] = (int) $itemsPerPage;
142 $ret['totalResults'] = (int) $totalResults;
143 $ret['entry'] = array();
148 'displayName' => false,
151 'preferredUsername' => false,
154 'currentLocation' => false,
159 'generation' => false
162 if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
163 foreach($fields_ret as $k => $v)
164 $fields_ret[$k] = true;
166 $fields_req = explode(',',$_GET['fields']);
167 foreach($fields_req as $f)
168 $fields_ret[trim($f)] = true;
174 if (!isset($rr['generation'])) {
176 $rr['generation'] = 3;
177 elseif ($system_mode)
178 $rr['generation'] = 1;
180 $rr['generation'] = 2;
183 if (($rr['about'] == "") AND isset($rr['pabout']))
184 $rr['about'] = $rr['pabout'];
186 if ($rr['location'] == "") {
187 if (isset($rr['plocation']))
188 $rr['location'] = $rr['plocation'];
190 if (isset($rr['pregion']) AND ($rr['pregion'] != "")) {
191 if ($rr['location'] != "")
192 $rr['location'] .= ", ";
194 $rr['location'] .= $rr['pregion'];
197 if (isset($rr['pcountry']) AND ($rr['pcountry'] != "")) {
198 if ($rr['location'] != "")
199 $rr['location'] .= ", ";
201 $rr['location'] .= $rr['pcountry'];
205 if (($rr['gender'] == "") AND isset($rr['pgender']))
206 $rr['gender'] = $rr['pgender'];
208 if (($rr['keywords'] == "") AND isset($rr['pub_keywords']))
209 $rr['keywords'] = $rr['pub_keywords'];
211 $about = Cache::get("about:".$rr['updated'].":".$rr['nurl']);
212 if (is_null($about)) {
213 $about = bbcode($rr['about'], false, false);
214 Cache::set("about:".$rr['updated'].":".$rr['nurl'],$about);
217 // Non connected persons can only see the keywords of a Diaspora account
218 if ($rr['network'] == NETWORK_DIASPORA) {
219 $rr['location'] = "";
225 if($fields_ret['id'])
226 $entry['id'] = (int)$rr['id'];
227 if($fields_ret['displayName'])
228 $entry['displayName'] = $rr['name'];
229 if($fields_ret['aboutMe'])
230 $entry['aboutMe'] = $about;
231 if($fields_ret['currentLocation'])
232 $entry['currentLocation'] = $rr['location'];
233 if($fields_ret['gender'])
234 $entry['gender'] = $rr['gender'];
235 if($fields_ret['generation'])
236 $entry['generation'] = (int)$rr['generation'];
237 if($fields_ret['urls']) {
238 $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
239 if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
240 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
242 if($fields_ret['preferredUsername'])
243 $entry['preferredUsername'] = $rr['nick'];
244 if($fields_ret['updated']) {
246 $entry['updated'] = $rr['success_update'];
248 if ($rr['name-date'] > $entry['updated'])
249 $entry['updated'] = $rr['name-date'];
251 if ($rr['uri-date'] > $entry['updated'])
252 $entry['updated'] = $rr['uri-date'];
254 if ($rr['avatar-date'] > $entry['updated'])
255 $entry['updated'] = $rr['avatar-date'];
257 $entry['updated'] = $rr['updated'];
259 $entry['updated'] = date("c", strtotime($entry['updated']));
261 if($fields_ret['photos'])
262 $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
263 if($fields_ret['network']) {
264 $entry['network'] = $rr['network'];
265 if ($entry['network'] == NETWORK_STATUSNET)
266 $entry['network'] = NETWORK_OSTATUS;
267 if (($entry['network'] == "") AND ($rr['self']))
268 $entry['network'] = NETWORK_DFRN;
270 if($fields_ret['tags']) {
271 $tags = str_replace(","," ",$rr['keywords']);
272 $tags = explode(" ", $tags);
275 foreach ($tags as $tag) {
276 $tag = trim(strtolower($tag));
281 $entry['tags'] = array($cleaned);
283 if($fields_ret['address']) {
284 $entry['address'] = array();
286 // Deactivated. It just reveals too much data. (Although its from the default profile)
287 //if (isset($rr['paddress']))
288 // $entry['address']['streetAddress'] = $rr['paddress'];
290 if (isset($rr['plocation']))
291 $entry['address']['locality'] = $rr['plocation'];
293 if (isset($rr['pregion']))
294 $entry['address']['region'] = $rr['pregion'];
297 //if (isset($rr['ppostalcode']))
298 // $entry['address']['postalCode'] = $rr['ppostalcode'];
300 if (isset($rr['pcountry']))
301 $entry['address']['country'] = $rr['pcountry'];
304 $ret['entry'][] = $entry;
308 $ret['entry'][] = array();
311 http_status_exit(500);
313 logger("End of poco", LOGGER_DEBUG);
315 if($format === 'xml') {
316 header('Content-type: text/xml');
317 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
320 if($format === 'json') {
321 header('Content-type: application/json');
322 echo json_encode($ret);
326 http_status_exit(500);