3 function poco_init(&$a) {
4 require_once("include/bbcode.php");
8 if(intval(get_config('system','block_public')) || (get_config('system','block_local_dir')))
13 $user = notags(trim($a->argv[1]));
16 $c = q("SELECT * FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1");
18 http_status_exit(401);
22 $format = (($_GET['format']) ? $_GET['format'] : 'json');
27 if($a->argc > 1 && $a->argv[1] === '@global') {
29 $update_limit = date("Y-m-d H:i:s", time() - 30 * 86400);
31 if($a->argc > 2 && $a->argv[2] === '@me')
33 if($a->argc > 3 && $a->argv[3] === '@all')
35 if($a->argc > 3 && $a->argv[3] === '@self')
37 if($a->argc > 4 && intval($a->argv[4]) && $justme == false)
38 $cid = intval($a->argv[4]);
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",
46 if(! count($r) || $r[0]['hidewall'] || $r[0]['hide-friends'])
47 http_status_exit(404);
53 $sql_extra = " AND `contact`.`self` = 1 ";
55 // $sql_extra = " AND `contact`.`self` = 0 ";
58 $sql_extra = sprintf(" AND `contact`.`id` = %d ",intval($cid));
60 if(x($_GET,'updatedSince'))
61 $update_limit = date("Y-m-d H:i:s",strtotime($_GET['updatedSince']));
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')",
68 dbesc(NETWORK_DIASPORA),
69 dbesc(NETWORK_OSTATUS)
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` OR `last-item` >= `failure_update`)
74 AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ",
76 dbesc(NETWORK_DIASPORA),
77 dbesc(NETWORK_OSTATUS),
78 dbesc(NETWORK_STATUSNET)
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` OR `last-item` >= `failure_update`)
83 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra",
86 dbesc(NETWORK_DIASPORA),
87 dbesc(NETWORK_OSTATUS),
88 dbesc(NETWORK_STATUSNET)
92 $totalResults = intval($r[0]['total']);
96 $startIndex = intval($_GET['startIndex']);
99 $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
102 logger("Start global query", LOGGER_DEBUG);
103 //$r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND `network` IN ('%s') AND ((`last_contact` >= `last_failure`) OR (`updated` > `last_failure`)) LIMIT %d, %d",
104 $r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND `network` IN ('%s', '%s', '%s') AND `updated` > `last_failure`
105 ORDER BY `updated` DESC LIMIT %d, %d",
106 dbesc($update_limit),
108 dbesc(NETWORK_DIASPORA),
109 dbesc(NETWORK_OSTATUS),
111 intval($itemsPerPage)
113 } elseif($system_mode) {
114 logger("Start system mode query", LOGGER_DEBUG);
115 $r = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`, `profile`.`gender` AS `pgender`,
116 `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`, `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`
117 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
118 WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s') AND `profile`.`is-default`
119 AND ((`contact`.`success_update` >= `contact`.`failure_update`) OR (`contact`.`last-item` >= `contact`.`failure_update`))
120 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
122 dbesc(NETWORK_DIASPORA),
123 dbesc(NETWORK_OSTATUS),
124 dbesc(NETWORK_STATUSNET),
126 intval($itemsPerPage)
129 logger("Start query for user ".$user['nickname'], LOGGER_DEBUG);
130 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
131 AND (`success_update` >= `failure_update` OR `last-item` >= `failure_update`)
132 AND `network` IN ('%s', '%s', '%s', '%s') $sql_extra LIMIT %d, %d",
133 intval($user['uid']),
135 dbesc(NETWORK_DIASPORA),
136 dbesc(NETWORK_OSTATUS),
137 dbesc(NETWORK_STATUSNET),
139 intval($itemsPerPage)
142 logger("Query done", LOGGER_DEBUG);
145 if(x($_GET,'sorted'))
146 $ret['sorted'] = false;
147 if(x($_GET,'filtered'))
148 $ret['filtered'] = false;
149 if(x($_GET,'updatedSince') AND !$global)
150 $ret['updatedSince'] = false;
152 $ret['startIndex'] = (int) $startIndex;
153 $ret['itemsPerPage'] = (int) $itemsPerPage;
154 $ret['totalResults'] = (int) $totalResults;
155 $ret['entry'] = array();
160 'displayName' => false,
163 'preferredUsername' => false,
166 'currentLocation' => false,
171 'generation' => false
174 if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
175 foreach($fields_ret as $k => $v)
176 $fields_ret[$k] = true;
178 $fields_req = explode(',',$_GET['fields']);
179 foreach($fields_req as $f)
180 $fields_ret[trim($f)] = true;
186 if (!isset($rr['generation'])) {
188 $rr['generation'] = 3;
189 elseif ($system_mode)
190 $rr['generation'] = 1;
192 $rr['generation'] = 2;
195 if (($rr['about'] == "") AND isset($rr['pabout']))
196 $rr['about'] = $rr['pabout'];
198 if ($rr['location'] == "") {
199 if (isset($rr['plocation']))
200 $rr['location'] = $rr['plocation'];
202 if (isset($rr['pregion']) AND ($rr['pregion'] != "")) {
203 if ($rr['location'] != "")
204 $rr['location'] .= ", ";
206 $rr['location'] .= $rr['pregion'];
209 if (isset($rr['pcountry']) AND ($rr['pcountry'] != "")) {
210 if ($rr['location'] != "")
211 $rr['location'] .= ", ";
213 $rr['location'] .= $rr['pcountry'];
217 if (($rr['gender'] == "") AND isset($rr['pgender']))
218 $rr['gender'] = $rr['pgender'];
220 if (($rr['keywords'] == "") AND isset($rr['pub_keywords']))
221 $rr['keywords'] = $rr['pub_keywords'];
223 $about = Cache::get("about:".$rr['updated'].":".$rr['nurl']);
224 if (is_null($about)) {
225 $about = bbcode($rr['about'], false, false);
226 Cache::set("about:".$rr['updated'].":".$rr['nurl'],$about);
230 if($fields_ret['id'])
231 $entry['id'] = (int)$rr['id'];
232 if($fields_ret['displayName'])
233 $entry['displayName'] = $rr['name'];
234 if($fields_ret['aboutMe'])
235 $entry['aboutMe'] = $about;
236 if($fields_ret['currentLocation'])
237 $entry['currentLocation'] = $rr['location'];
238 if($fields_ret['gender'])
239 $entry['gender'] = $rr['gender'];
240 if($fields_ret['generation'])
241 $entry['generation'] = (int)$rr['generation'];
242 if($fields_ret['urls']) {
243 $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
244 if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
245 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
247 if($fields_ret['preferredUsername'])
248 $entry['preferredUsername'] = $rr['nick'];
249 if($fields_ret['updated']) {
251 $entry['updated'] = $rr['success_update'];
253 if ($rr['name-date'] > $entry['updated'])
254 $entry['updated'] = $rr['name-date'];
256 if ($rr['uri-date'] > $entry['updated'])
257 $entry['updated'] = $rr['uri-date'];
259 if ($rr['avatar-date'] > $entry['updated'])
260 $entry['updated'] = $rr['avatar-date'];
262 $entry['updated'] = $rr['updated'];
264 $entry['updated'] = date("c", strtotime($entry['updated']));
266 if($fields_ret['photos'])
267 $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
268 if($fields_ret['network']) {
269 $entry['network'] = $rr['network'];
270 if ($entry['network'] == NETWORK_STATUSNET)
271 $entry['network'] = NETWORK_OSTATUS;
272 if (($entry['network'] == "") AND ($rr['self']))
273 $entry['network'] = NETWORK_DFRN;
275 if($fields_ret['tags']) {
276 $tags = str_replace(","," ",$rr['keywords']);
277 $tags = explode(" ", $tags);
280 foreach ($tags as $tag) {
281 $tag = trim(strtolower($tag));
286 $entry['tags'] = array($cleaned);
288 if($fields_ret['address']) {
289 $entry['address'] = array();
291 // Deactivated. It just reveals too much data. (Although its from the default profile)
292 //if (isset($rr['paddress']))
293 // $entry['address']['streetAddress'] = $rr['paddress'];
295 if (isset($rr['plocation']))
296 $entry['address']['locality'] = $rr['plocation'];
298 if (isset($rr['pregion']))
299 $entry['address']['region'] = $rr['pregion'];
302 //if (isset($rr['ppostalcode']))
303 // $entry['address']['postalCode'] = $rr['ppostalcode'];
305 if (isset($rr['pcountry']))
306 $entry['address']['country'] = $rr['pcountry'];
309 $ret['entry'][] = $entry;
313 $ret['entry'][] = array();
316 http_status_exit(500);
318 logger("End of poco", LOGGER_DEBUG);
320 if($format === 'xml') {
321 header('Content-type: text/xml');
322 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
325 if($format === 'json') {
326 header('Content-type: application/json');
327 echo json_encode($ret);
331 http_status_exit(500);