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` AND `network` IN ('%s')",
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 `success_update` >= `failure_update`
71 AND `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ",
73 dbesc(NETWORK_DIASPORA),
74 dbesc(NETWORK_OSTATUS),
75 dbesc(NETWORK_STATUSNET)
78 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
79 AND `success_update` >= `failure_update` AND `network` IN ('%s', '%s', '%s', '%s', '') $sql_extra",
82 dbesc(NETWORK_DIASPORA),
83 dbesc(NETWORK_OSTATUS),
84 dbesc(NETWORK_STATUSNET)
88 $totalResults = intval($r[0]['total']);
92 $startIndex = intval($_GET['startIndex']);
95 $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
99 $r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND `network` IN ('%s') AND `last_contact` >= `last_failure` LIMIT %d, %d",
100 dbesc($update_limit),
103 intval($itemsPerPage)
105 } elseif($system_mode) {
106 $r = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`, `profile`.`gender` AS `pgender`,
107 `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`, `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`
108 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
109 WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s', '') AND `profile`.`is-default`
110 AND `contact`.`success_update` >= `contact`.`failure_update`
111 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
113 dbesc(NETWORK_DIASPORA),
114 dbesc(NETWORK_OSTATUS),
115 dbesc(NETWORK_STATUSNET),
117 intval($itemsPerPage)
120 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
121 AND `success_update` >= `failure_update` AND `network` IN ('%s', '%s', '%s', '%s', '') $sql_extra LIMIT %d, %d",
122 intval($user['uid']),
124 dbesc(NETWORK_DIASPORA),
125 dbesc(NETWORK_OSTATUS),
126 dbesc(NETWORK_STATUSNET),
128 intval($itemsPerPage)
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'];
212 if($fields_ret['id'])
213 $entry['id'] = (int)$rr['id'];
214 if($fields_ret['displayName'])
215 $entry['displayName'] = $rr['name'];
216 if($fields_ret['aboutMe'])
217 $entry['aboutMe'] = bbcode($rr['about'], false, false);
218 if($fields_ret['currentLocation'])
219 $entry['currentLocation'] = $rr['location'];
220 if($fields_ret['gender'])
221 $entry['gender'] = $rr['gender'];
222 if($fields_ret['generation'])
223 $entry['generation'] = (int)$rr['generation'];
224 if($fields_ret['urls']) {
225 $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
226 if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
227 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
229 if($fields_ret['preferredUsername'])
230 $entry['preferredUsername'] = $rr['nick'];
231 if($fields_ret['updated']) {
233 $entry['updated'] = $rr['success_update'];
235 if ($rr['name-date'] > $entry['updated'])
236 $entry['updated'] = $rr['name-date'];
238 if ($rr['uri-date'] > $entry['updated'])
239 $entry['updated'] = $rr['uri-date'];
241 if ($rr['avatar-date'] > $entry['updated'])
242 $entry['updated'] = $rr['avatar-date'];
244 $entry['updated'] = $rr['updated'];
246 $entry['updated'] = date("c", strtotime($entry['updated']));
248 if($fields_ret['photos'])
249 $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
250 if($fields_ret['network']) {
251 $entry['network'] = $rr['network'];
252 if ($entry['network'] == NETWORK_STATUSNET)
253 $entry['network'] = NETWORK_OSTATUS;
254 if (($entry['network'] == "") AND ($rr['self']))
255 $entry['network'] = NETWORK_DFRN;
257 if($fields_ret['tags']) {
258 $tags = str_replace(","," ",$rr['keywords']);
259 $tags = explode(" ", $tags);
262 foreach ($tags as $tag) {
263 $tag = trim(strtolower($tag));
268 $entry['tags'] = array($cleaned);
270 if($fields_ret['address']) {
271 $entry['address'] = array();
273 // Deactivated. It just reveals too much data. (Although its from the default profile)
274 //if (isset($rr['paddress']))
275 // $entry['address']['streetAddress'] = $rr['paddress'];
277 if (isset($rr['plocation']))
278 $entry['address']['locality'] = $rr['plocation'];
280 if (isset($rr['pregion']))
281 $entry['address']['region'] = $rr['pregion'];
284 //if (isset($rr['ppostalcode']))
285 // $entry['address']['postalCode'] = $rr['ppostalcode'];
287 if (isset($rr['pcountry']))
288 $entry['address']['country'] = $rr['pcountry'];
291 $ret['entry'][] = $entry;
295 $ret['entry'][] = array();
298 http_status_exit(500);
300 if($format === 'xml') {
301 header('Content-type: text/xml');
302 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
305 if($format === 'json') {
306 header('Content-type: application/json');
307 echo json_encode($ret);
311 http_status_exit(500);