Example #1
0
 /**
  * This method validates the outputcontext called.
  *
  * It checks if the value passed is in the allowed array
  * of validOutputs.
  *
  * @see    self::$validOutputs
  * @return bool Valid or not.
  */
 public static function validateOutputType($type)
 {
     $outputs = Frapi_Output::getEnabledFormats();
     if (is_array($outputs) && !in_array(strtolower($type), $outputs)) {
         return false;
     }
     return true;
 }
Example #2
0
 /**
  * Detect and set the output format and mimetype
  *
  * This method bases it's detection on the ACCEPT header passed
  * to the API.
  *
  * @return array of mimetype, outputFormat and params
  */
 public function detectOutputFormat()
 {
     $mimetypes = Frapi_Output::getMimeTypeMap();
     if ($mimetypes) {
         $this->mimeMaps = $mimetypes;
     }
     $types = $this->parseAcceptHeader();
     $return = array('outputFormat' => false, 'mimetype' => false, 'params' => array());
     if (!empty($types)) {
         $mimetypes = $this->parseMimeTypes();
         foreach ($types as $type) {
             if (isset($this->mimeMaps[$type['mimetype']])) {
                 $return['outputFormat'] = $this->mimeMaps[$type['mimetype']];
                 $return['mimetype'] = $type['mimetype'];
                 $return['params'] = isset($type['params']) ? $type['params'] : array();
                 break;
             } else {
                 foreach ($mimetypes as $mimetype) {
                     $matches = array();
                     if (preg_match($mimetype['pattern'], $type['mimetype'], $matches)) {
                         $return['mimetype'] = $type['mimetype'];
                         $return['params'] = isset($type['params']) ? $type['params'] : array();
                         if ($mimetype['output_format'][0] == ':') {
                             $format = substr($mimetype['output_format'], 1);
                             if (!empty($matches[$format]) && Frapi_Rules::validateOutputType($matches[$format])) {
                                 $return['outputFormat'] = $matches[$format];
                             }
                         } else {
                             $return['outputFormat'] = $mimetype['output_format'];
                         }
                         foreach ($mimetype['params'] as $param) {
                             $return['params'][$param] = $matches[$param];
                         }
                         break 2;
                     }
                 }
             }
         }
     }
     // Final catch-all (for cases like Accept: */*)
     if (!$return['outputFormat']) {
         $return['outputFormat'] = $this->getDefaultFormatFromConfiguration();
         $return['mimetype'] = Frapi_Output::getMimeTypeByFormat($return['outputFormat']);
     }
     $this->setOutputFormat($return['outputFormat']);
     return $return;
 }
Example #3
0
 /**
  * reads the Content-Type header; sets request body format if a valid mime type exists
  *
  * @return void
  */
 public function setInputFormat()
 {
     $contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : null;
     $mimetypes = Frapi_Output::getMimeTypeMap();
     if ($mimetypes) {
         $this->mimeMaps = $mimetypes;
     }
     if (!empty($contentType) && isset($this->mimeMaps[$contentType]) && in_array($this->mimeMaps[$contentType], $this->allowedInputTypes)) {
         $this->inputFormat = $this->mimeMaps[$contentType];
     }
 }
Example #4
0
 /**
  * Get Output Instance
  *
  * Get an install of the context output by it's type.
  *
  * This method will use the Rules to make
  * sure that the type is an allowed type.
  *
  * @see    Frapi_Output::getInstance($type)
  * @param  string $type  The type of output to get.
  *
  * @return mixed         Output Instance of the OutputType
  *                       or false if the type is not a valid type.
  */
 protected function getOutputInstance($type)
 {
     $this->outputContext = Frapi_Output::getInstance($type);
     return $this->outputContext;
 }
Example #5
0
 /**
  * Constructor
  *
  * Upon invoking of the constructor, a few objects need to be created
  * in order to approve, authorize and secure the action contexts.
  *
  * This constructor sets up the Security, ErrorContainer, Authorization
  * and also sets the request parameters, files parameters, the format of the output
  * and of course the most important part which is the action/output contexts themselves.
  *
  * @warning IF-Clusterfuck.
  *
  * @see Security
  * @see ErrorContainer
  * @see Authorization
  * @params object $customAuthorization optional custom authorization extending Frapi_Authorization
  */
 public function __construct($customAuthorization = null)
 {
     try {
         $this->security = new Frapi_Security();
         $this->authorization = $customAuthorization instanceof Frapi_Authorization ? $customAuthorization : new Frapi_Authorization();
         $this->router = new Frapi_Router();
         $this->router->loadAndPrepareRoutes();
         $this->setInputFormat();
         $uri = $_SERVER['REQUEST_URI'];
         // For some reason, this is now a fatal
         // error in 5.3 and no longer a warning
         // in php (parse_url() with an http:// in the URL_PATH)...
         if (stristr($uri, '?') !== false) {
             $uri = substr($uri, 0, strpos($uri, '?'));
         }
         $query_path = parse_url($uri, PHP_URL_PATH);
         // @deprecated The use of extensions is considered deprecated
         //Query ending in .xxx may or may not be an output format
         $query_path_format = pathinfo($query_path, PATHINFO_EXTENSION);
         $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) {
                 $accept = Frapi_Output::getMimeTypeByFormat($extension);
                 if (isset($_SERVER['HTTP_ACCEPT'])) {
                     $_SERVER['HTTP_ACCEPT'] = $accept . ',' . $_SERVER['HTTP_ACCEPT'];
                 } else {
                     $_SERVER['HTTP_ACCEPT'] = $accept;
                 }
             }
             $query_path_format = $format = null;
         }
         if ($routed = $this->router->match($query_path)) {
             $_REQUEST = array_merge($_REQUEST, $routed['params']);
             $this->setAction(strtolower($routed['action']));
             $this->setRequest($_REQUEST);
         } else {
             $this->setRequest($_REQUEST);
             $this->setAction($this->getParam('action'));
         }
         // assign the files and params from the request
         $this->setFiles($_FILES)->setParams($this->request);
         $this->authorization->setAuthorizationParams($this->getParams());
     } catch (Frapi_Exception $e) {
         // Something RONG happened. Need to tell developers.
         throw $e;
     } catch (Exception $e) {
         // Something else? Silence! I Keeel you.
     }
 }
Example #6
0
 /**
  * Get Output Instance
  *
  * Get an install of the context output by it's type.
  *
  * This method will use the Rules to make
  * sure that the type is an allowed type.
  *
  * @see    Frapi_Output::getInstance($type)
  * @param  string $type  The type of output to get.
  *
  * @return mixed         Output Instance of the OutputType
  *                       or false if the type is not a valid type.
  */
 protected function getOutputInstance($type)
 {
     $options = $this->detectAndSetMimeType();
     $this->outputContext = Frapi_Output::getInstance($type, $options);
     return $this->outputContext;
 }