3 function poco_init(&$a) {
4 require_once("include/bbcode.php");
8 if(intval(get_config('system','block_public')))
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 `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 `uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) ",
72 dbesc(NETWORK_DIASPORA),
73 dbesc(NETWORK_OSTATUS),
74 dbesc(NETWORK_STATUSNET)
77 $r = q("SELECT count(*) AS `total` FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
78 AND `network` IN ('%s', '%s', '%s', '%s', '') $sql_extra",
81 dbesc(NETWORK_DIASPORA),
82 dbesc(NETWORK_OSTATUS),
83 dbesc(NETWORK_STATUSNET)
87 $totalResults = intval($r[0]['total']);
91 $startIndex = intval($_GET['startIndex']);
94 $itemsPerPage = ((x($_GET,'count') && intval($_GET['count'])) ? intval($_GET['count']) : $totalResults);
98 $r = q("SELECT * FROM `gcontact` WHERE `updated` > '%s' AND `network` IN ('%s') LIMIT %d, %d",
102 intval($itemsPerPage)
104 } elseif($system_mode) {
105 $r = q("SELECT `contact`.*, `profile`.`about` AS `pabout`, `profile`.`locality` AS `plocation`, `profile`.`pub_keywords`, `profile`.`gender` AS `pgender`,
106 `profile`.`address` AS `paddress`, `profile`.`region` AS `pregion`, `profile`.`postal-code` AS `ppostalcode`, `profile`.`country-name` AS `pcountry`
107 FROM `contact` INNER JOIN `profile` ON `profile`.`uid` = `contact`.`uid`
108 WHERE `self` = 1 AND `network` IN ('%s', '%s', '%s', '%s', '') AND `profile`.`is-default`
109 AND `contact`.`uid` IN (SELECT `uid` FROM `pconfig` WHERE `cat` = 'system' AND `k` = 'suggestme' AND `v` = 1) LIMIT %d, %d",
111 dbesc(NETWORK_DIASPORA),
112 dbesc(NETWORK_OSTATUS),
113 dbesc(NETWORK_STATUSNET),
115 intval($itemsPerPage)
118 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0
119 AND `network` IN ('%s', '%s', '%s', '%s', '') $sql_extra LIMIT %d, %d",
120 intval($user['uid']),
122 dbesc(NETWORK_DIASPORA),
123 dbesc(NETWORK_OSTATUS),
124 dbesc(NETWORK_STATUSNET),
126 intval($itemsPerPage)
131 if(x($_GET,'sorted'))
132 $ret['sorted'] = false;
133 if(x($_GET,'filtered'))
134 $ret['filtered'] = false;
135 if(x($_GET,'updatedSince') AND !$global)
136 $ret['updatedSince'] = false;
138 $ret['startIndex'] = (string) $startIndex;
139 $ret['itemsPerPage'] = (string) $itemsPerPage;
140 $ret['totalResults'] = (string) $totalResults;
141 $ret['entry'] = array();
146 'displayName' => false,
149 'preferredUsername' => false,
152 'currentLocation' => false,
159 if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
160 foreach($fields_ret as $k => $v)
161 $fields_ret[$k] = true;
163 $fields_req = explode(',',$_GET['fields']);
164 foreach($fields_req as $f)
165 $fields_ret[trim($f)] = true;
171 if (($rr['about'] == "") AND isset($rr['pabout']))
172 $rr['about'] = $rr['pabout'];
174 if ($rr['location'] == "") {
175 if (isset($rr['plocation']))
176 $rr['location'] = $rr['plocation'];
178 if (isset($rr['pregion']) AND ($rr['pregion'] != "")) {
179 if ($rr['location'] != "")
180 $rr['location'] .= ", ";
182 $rr['location'] .= $rr['pregion'];
185 if (isset($rr['pcountry']) AND ($rr['pcountry'] != "")) {
186 if ($rr['location'] != "")
187 $rr['location'] .= ", ";
189 $rr['location'] .= $rr['pcountry'];
193 if (($rr['gender'] == "") AND isset($rr['pgender']))
194 $rr['gender'] = $rr['pgender'];
196 if (($rr['keywords'] == "") AND isset($rr['pub_keywords']))
197 $rr['keywords'] = $rr['pub_keywords'];
200 if($fields_ret['id'])
201 $entry['id'] = $rr['id'];
202 if($fields_ret['displayName'])
203 $entry['displayName'] = $rr['name'];
204 if($fields_ret['aboutMe'])
205 $entry['aboutMe'] = bbcode($rr['about'], false, false);
206 if($fields_ret['currentLocation'])
207 $entry['currentLocation'] = $rr['location'];
208 if($fields_ret['gender'])
209 $entry['gender'] = $rr['gender'];
210 if($fields_ret['urls']) {
211 $entry['urls'] = array(array('value' => $rr['url'], 'type' => 'profile'));
212 if($rr['addr'] && ($rr['network'] !== NETWORK_MAIL))
213 $entry['urls'][] = array('value' => 'acct:' . $rr['addr'], 'type' => 'webfinger');
215 if($fields_ret['preferredUsername'])
216 $entry['preferredUsername'] = $rr['nick'];
217 if($fields_ret['updated']) {
219 $entry['updated'] = $rr['success_update'];
221 if ($rr['name-date'] > $entry['updated'])
222 $entry['updated'] = $rr['name-date'];
224 if ($rr['uri-date'] > $entry['updated'])
225 $entry['updated'] = $rr['uri-date'];
227 if ($rr['avatar-date'] > $entry['updated'])
228 $entry['updated'] = $rr['avatar-date'];
230 $entry['updated'] = $rr['updated'];
232 $entry['updated'] = date("c", strtotime($entry['updated']));
234 if($fields_ret['photos'])
235 $entry['photos'] = array(array('value' => $rr['photo'], 'type' => 'profile'));
236 if($fields_ret['network']) {
237 $entry['network'] = $rr['network'];
238 if ($entry['network'] == NETWORK_STATUSNET)
239 $entry['network'] = NETWORK_OSTATUS;
240 if (($entry['network'] == "") AND ($rr['self']))
241 $entry['network'] = NETWORK_DFRN;
243 if($fields_ret['tags']) {
244 $tags = str_replace(","," ",$rr['keywords']);
245 $tags = explode(" ", $tags);
248 foreach ($tags as $tag) {
249 $tag = trim(strtolower($tag));
254 $entry['tags'] = array($cleaned);
256 if($fields_ret['address']) {
257 $entry['address'] = array();
259 // Deactivated. It just reveals too much data. (Although its from the default profile)
260 //if (isset($rr['paddress']))
261 // $entry['address']['streetAddress'] = $rr['paddress'];
263 if (isset($rr['plocation']))
264 $entry['address']['locality'] = $rr['plocation'];
266 if (isset($rr['pregion']))
267 $entry['address']['region'] = $rr['pregion'];
270 //if (isset($rr['ppostalcode']))
271 // $entry['address']['postalCode'] = $rr['ppostalcode'];
273 if (isset($rr['pcountry']))
274 $entry['address']['country'] = $rr['pcountry'];
277 $ret['entry'][] = $entry;
281 $ret['entry'][] = array();
284 http_status_exit(500);
286 if($format === 'xml') {
287 header('Content-type: text/xml');
288 echo replace_macros(get_markup_template('poco_xml.tpl'),array_xmlify(array('$response' => $ret)));
291 if($format === 'json') {
292 header('Content-type: application/json');
293 echo json_encode($ret);
297 http_status_exit(500);