more poco spec compliance

This commit is contained in:
Friendika 2011-10-27 00:57:19 -07:00
parent faa078b83f
commit 31502207bd
3 changed files with 89 additions and 21 deletions

View File

@ -7,7 +7,9 @@ function poco_init(&$a) {
$user = notags(trim($a->argv[1])); $user = notags(trim($a->argv[1]));
} }
if(! x($user) || get_config('system','block_public')) if(! x($user) || get_config('system','block_public'))
killme(); http_status_exit(401);
$format = (($_GET['format']) ? $_GET['format'] : 'json');
$justme = false; $justme = false;
@ -26,7 +28,7 @@ function poco_init(&$a) {
dbesc($user) dbesc($user)
); );
if(! count($r) || $r[0]['hidewall'] || $r[0]['hide-friends']) if(! count($r) || $r[0]['hidewall'] || $r[0]['hide-friends'])
killme(); http_status_exit(404);
$user = $r[0]; $user = $r[0];
@ -57,28 +59,73 @@ function poco_init(&$a) {
intval($itemsPerPage) intval($itemsPerPage)
); );
$ret = array( $ret = array();
'startIndex' => $startIndex, if(x($_GET,'sorted'))
'itemsPerPage' => $itemsPerPage, $ret['sorted'] = 'false';
'totalResults' => $totalResults, if(x($_GET,'filtered'))
'entry' => array() $ret['filtered'] = 'false';
if(x($_GET,'updatedSince'))
$ret['updateSince'] = 'false';
$ret['startIndex'] = $startIndex;
$ret['itemsPerPage'] = $itemsPerPage;
$ret['totalResults'] = $totalResults;
$ret['entry'] = array();
$fields_ret = array(
'id' => false,
'displayName' => false,
'urls' => false,
'preferredName' => false,
'photos' => false
); );
if(count($r)) { if((! x($_GET,'fields')) || ($_GET['fields'] === '@all'))
foreach($r as $rr) { foreach($fields_ret as $f)
$entry = array(); $f = true;
$entry['id'] = $rr['id']; else {
$entry['displayName'] = $rr['name']; $fields_req = explode(',',$_GET['fields']);
$entry['urls'] = array('value' => $rr['url'], 'type' => 'profile'); foreach($fields_req as $f)
$entry['preferredUsername'] = $rr['nick']; $fields_ret[trim($f)] = true;
$entry['photos'] = array('value' => $rr['photo'], 'type' => 'profile');
$ret['entry'][] = $entry;
}
} }
header('Content-type: application/json');
echo json_encode($ret);
killme();
if(is_array($r)) {
if(count($r)) {
foreach($r as $rr) {
$entry = array();
if($fields_ret['id'])
$entry['id'] = $rr['id'];
if($fields_ret['displayName'])
$entry['displayName'] = $rr['name'];
if($fields_ret['urls'])
$entry['urls'] = array('value' => $rr['url'], 'type' => 'profile');
if($fields_ret['preferredUsername'])
$entry['preferredUsername'] = $rr['nick'];
if($fields_ret['photos'])
$entry['photos'] = array('value' => $rr['photo'], 'type' => 'profile');
$ret['entry'][] = $entry;
}
}
else
$ret['entry'][] = array();
}
else
http_status_exit(500);
if($format === 'xml') {
header('Content-type: text/xml');
echo replace_macros(get_markup_template('poco.xml',array('response' => $ret)));
http_status_exit(500);
}
if($format === 'json') {
header('Content-type: application/json');
echo json_encode($ret);
killme();
}
else
http_status_exit(500);
} }

View File

@ -13,7 +13,7 @@ function tagger_content(&$a) {
$term = notags(trim($_GET['term'])); $term = notags(trim($_GET['term']));
// no commas allowed // no commas allowed
$term = str_replace(',','',$term); $term = str_replace(array(',',' '),array('','_'),$term);
if(! $term) if(! $term)
return; return;

21
view/poco_xml.tpl Normal file
View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<response>
{{ if $response.sorted }}<sorted>$response.sorted</sorted>{{ endif }}
{{ if $response.filtered }}<filtered>$response.filtered</filtered>{{ endif }}
{{ if $response.updatedSince }}<updatedSince>$response.updatedSince</updatedSince>{{ endif }}
{{ if $response.startIndex }}<startIndex>$response.startIndex</startIndex>{{ endif }}
{{ if $response.itemsPerPage }}<itemsPerPage>$response.itemsPerPage</itemsPerPage>{{ endif }}
{{ if $response.totalResults }}<totalResults>$response.totalResults</totalResults>{{ endif }}
{{ for $response.entry as $ent }}
<entry>
{{ if $ent.id }}<id>$ent.id</id>{{ endif }}
{{ if $ent.displayName }}<displayName>$ent.displayName</displayName>{{ endif }}
{{ if $ent.preferredName }}<preferredName>$ent.preferredName</preferredName>{{ endif }}
{{ if $ent.urls }}<urls><value>$ent.urls.value</value><type>$ent.urls.type</type></urls>{{ endif }}
{{ if $ent.photos }}<photos><value>$ent.photos.value</value><type><$ent.photos.type></type></photos>{{ endif }}
</entry>
{{ endfor }}
</response>