Skip to content

Commit

Permalink
Merge pull request #172 from trevormorse/issue_108
Browse files Browse the repository at this point in the history
Issue 108
  • Loading branch information
dshafik committed Jun 27, 2012
2 parents 7c9ef0e + 7a66d7a commit 5bce452
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
11 changes: 6 additions & 5 deletions src/frapi/library/Frapi/Controller/Main.php
Expand Up @@ -220,11 +220,12 @@ public function __construct($customAuthorization=null)
$format = $this->getParam('format');
if (is_string($query_path_format) && strlen($query_path_format) || $format) {
$extension = ($query_path_format) ? $query_path_format : $format;

if ($query_path_format) {
$query_path = substr($query_path, 0, (strlen($extension) + 1)*-1);
}

if (Frapi_Rules::validateOutputType($extension) === true) {
//Output format suffix is valid, remove from URL!
if ($query_path_format) {
$query_path = substr($query_path, 0, (strlen($extension) + 1)*-1);
}
$accept = Frapi_Output::getMimeTypeByFormat($extension);
if (isset($_SERVER['HTTP_ACCEPT'])) {
$_SERVER['HTTP_ACCEPT'] = $accept . ',' .$_SERVER['HTTP_ACCEPT'];
Expand Down Expand Up @@ -453,7 +454,7 @@ protected function setOutputFormat($format = false)
$format = strtolower($format);

if ($format) {
$typeValid = Frapi_Rules::validateOutputType($format);
$typeValid = Frapi_Rules::validateOutputType($format);
$this->format = $format;
} else {
throw new Frapi_Error (
Expand Down
25 changes: 25 additions & 0 deletions src/frapi/library/Frapi/Output.php
Expand Up @@ -209,6 +209,31 @@ public static function getMimeTypeMap()
return $map;
}

public static function getEnabledFormats()
{
try {
if ($formats = Frapi_Internal::getCached('Output.formats-enabled')) {
return $formats;
}

$cache = new Frapi_Internal();
$outputs = $cache->getConfiguration('outputs')->getAll('output');

$formats = array();
foreach ($outputs as $output) {
if($output['enabled'] == 1) {
$formats[] = strtolower($output['name']);
}
}
} catch (Exception $e) {
return false;
}

Frapi_Internal::setCached('Output.formats-enabled', $formats);

return $formats;
}

/**
* Return a mimetype for a given extension
*
Expand Down
11 changes: 2 additions & 9 deletions src/frapi/library/Frapi/Rules.php
Expand Up @@ -56,19 +56,12 @@ public static function validateActionType($type)
*/
public static function validateOutputType($type)
{
$outputs = Frapi_Internal::getCachedElseQueryConfigurationByKey(
'Output.formats-enabled',
array(
'type' => 'outputs',
'node' => 'output',
'key' => 'name'
)
);
$outputs = Frapi_Output::getEnabledFormats();

if (is_array($outputs) && !in_array(strtolower($type), $outputs)) {
return false;
}

return true;
}

Expand Down

0 comments on commit 5bce452

Please sign in to comment.