Orion PHP  0.11.12
The PHP5.3 framework
restful.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /**
00004  * Orion RESTful controller base class.
00005  *
00006  * Extend this class to create a new REST controller.
00007  *
00008  * @author Thibaut Despoulain
00009  * @license BSD 4-clauses
00010  * @version 0.11.12
00011  */
00012 
00013 namespace Orion\Core\Controller;
00014 
00015 use \Orion\Core;
00016 
00017 abstract class Restful extends Core\Controller
00018 {
00019     const CLASS_NAME = 'OrionControllerRestful';
00020 
00021     const E_LOGIN_ERROR = 2;
00022     const E_LOGIN_DISALLOW = 4;
00023     const E_ROUTE_NO = 8;
00024     const E_FUNCTION_NO = 16;
00025 
00026     const DELETE = 'DELETE';
00027     const GET = 'GET';
00028     const POST = 'POST';
00029     const PUT = 'PUT';
00030 
00031     /**
00032      * Main module function, executed right after module loading by Orion.
00033      * Handles route parsing and function callbacks.
00034      */
00035     public function load()
00036     {
00037         if ( $this->route == null )
00038             $this->sendError( self::E_ROUTE_NO );
00039 
00040         $function = $this->route->decode();
00041 
00042         if ( Core\Tools::startWith( $function->getName(), '__' ) )
00043             $this->sendError( self::E_FUNCTION_NO );
00044 
00045         if ( Core\Tools::startWith( $function->getName(), self::FUNCTION_PREFIX ) )
00046             $this->sendError( self::E_FUNCTION_NO );
00047 
00048         if ( !is_callable( array( $this, self::FUNCTION_PREFIX . $function->getName() ) ) )
00049             $this->sendError( self::E_FUNCTION_NO );
00050 
00051         Core\Tools::callClassMethod( $this, self::FUNCTION_PREFIX . $function->getName(), $function->getArgs() );
00052     }
00053 
00054     /**
00055      * Allows access only to logged users that have a level equal to or less than provided role. If permission is not granted, it will send a JSON error object.
00056      * <p><b>Note that while it's doing all login/auth/redirection work automatically, you still have to create the corresponding user table in your database in addition to provide the login module into orion's module directory.</b></p>
00057      * @see OrionAuth
00058      *      MainConfig
00059      *      LoginModule
00060      * @param string $slug the role identifier (ie: 'administrator', 'member', etc.). See your configuration file for a liste of roles and their permission level.
00061      */
00062     public function allow( $slug )
00063     {
00064         try
00065         {
00066             if ( !Core\Auth::login( true ) )
00067             {
00068                 $this->sendError( self::E_LOGIN_DISALLOW );
00069             }
00070             if ( !Core\Auth::allow( $slug ) )
00071             {// this exception prevents any redirection defect or hack
00072                 $this->sendError( self::E_LOGIN_DISALLOW );
00073             }
00074         }
00075         catch ( Core\Exception $e )
00076         {
00077             throw $e;
00078         }
00079     }
00080 
00081     /**
00082      * Gets REST PUT data
00083      */
00084     public function getPutData()
00085     {
00086         $data = null;
00087         parse_str( file_get_contents( "php://input" ), $data );
00088         return $data;
00089     }
00090 
00091     /**
00092      * Test method used to access the resource
00093      * @param $method POST|GET|PUT|DELETE
00094      */
00095     public function isMethod( $method )
00096     {
00097         return ($_SERVER[ 'REQUEST_METHOD' ] == $method);
00098     }
00099 
00100     /**
00101      * Encodes $array to JSON format and sends it.
00102      * @param array $array
00103      * @param boolean $exit Exit after response ?
00104      */
00105     public function send( $array, $exit=true, $code=null )
00106     {
00107         $this->respond( json_encode( $array ), $exit, $code );
00108     }
00109 
00110     /**
00111      * Sends a standard {"error":X} JSON Object
00112      * @param int $e Error code
00113      */
00114     public function sendError( $e, $code=401 )
00115     {
00116         Core\Context::setHeaderCode( $code );
00117         $this->send( array( 'error' => $e ) );
00118         exit();
00119     }
00120 
00121 }
00122 
00123 ?>