NewsFeaturesDownloadsDevelopmentSupportAbout Us

lifetype/branches/lifetype-1.0.6/class/action/resourceserveraction.class.php

Go to the documentation of this file.
00001 <?php
00002 
00003         include_once( PLOG_CLASS_PATH."class/action/action.class.php" );
00004         include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
00005         include_once( PLOG_CLASS_PATH."class/view/resourceserverview.class.php" );
00006         include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
00007         include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
00008     include_once( PLOG_CLASS_PATH."class/gallery/dao/galleryresources.class.php" );     
00009         include_once( PLOG_CLASS_PATH."class/net/url.class.php" );
00010     include_once( PLOG_CLASS_PATH."class/security/pipeline.class.php" );
00011     include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );     
00012         include_once( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );
00013         include_once( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );      
00014 
00019         class ResourceServerAction extends Action
00020         {
00021                 var $_mode;
00022                 var $_resource;
00023                 var $_resId;
00024                 var $_album;
00025                 var $_config;
00026                 var $_blogInfo;
00027         
00028                 function ResourceServerAction( $actionInfo, $request )
00029                 {
00030                         $this->Action( $actionInfo, $request );
00031                         
00032                         // keep the session for later use
00033             $session = HttpVars::getSession();
00034                 $this->_session = $session['SessionInfo'];                      
00035                         
00036                         $this->_config =& Config::getConfig();
00037                         
00038                         $this->registerFieldValidator( "resource", new StringValidator(), true );
00039                         $this->registerFieldValidator( "resId", new IntegerValidator(), true );
00040                         $this->registerFieldValidator( "albumId", new IntegerValidator(), true );
00041                         $this->registerFieldValidator( "albumName", new StringValidator(), true );
00042                         $this->registerFieldValidator( "blogId", new IntegerValidator(), true );
00043                         $this->registerFieldValidator( "blogName", new StringValidator(), true );
00044                         $this->registerFieldValidator( "userId", new IntegerValidator(), true );
00045                         $this->registerFieldValidator( "blogUserName", new StringValidator(), true );                   
00046                         
00047                         // since this class does not return HTML code but files, we cannot
00048                         // return HTML so let's return 404 status code with a custom error message
00049                         $view = new ResourceServerView();
00050                         $view->addHeaderResponse( "HTTP/1.1 404 Not Found" );
00051                         $view->addHeaderResponse( "Status: 404 Not Found" );
00052                         $view->addHeaderResponse( "X-LifeType-Error: Invalid parameters" );
00053                         $this->setValidationErrorView( $view );
00054                 }
00055                 
00060         function _getBlogInfo()
00061         {                       
00062                         // see if we're using subdomains
00063                         $config =& Config::getConfig();
00064                         if( $config->getValue( "subdomains_enabled" )) {
00065                                 $subdomainInfo = Subdomains::getSubdomainInfoFromRequest();
00066 
00067                                 if( $subdomainInfo["username"] != "" && $this->_request->getValue( 'blogUserName' ) == "" )
00068                                         $this->_request->setValue( 'blogUserName', $subdomainInfo["username"] );
00069                                 if( $subdomainInfo["blogname"] != "" && $this->_request->getValue( 'blogName' ) == "" ) 
00070                                         $this->_request->setValue( 'blogName', $subdomainInfo["blogname"] );                            
00071                         }
00072 
00073                 $blogId = $this->_request->getValue( 'blogId' );
00074                 $blogName = $this->_request->getValue( 'blogName' );
00075                 $userId = $this->_request->getValue( 'userId' );
00076                 $userName = $this->_request->getValue( 'blogUserName' );
00077                         
00078             // if there is a "blogId" parameter, it takes precedence over the
00079             // "user" parameter.
00080             if( !$blogId && !$blogName ) {
00081                 // check if there was a user parameter
00082                 if( !empty($userName) ) {
00083                         // if so, check to which blogs the user belongs
00084                         $users = new Users();
00085                         $userInfo = $users->getUserInfoFromUsername( $userName );
00086                     // if the user exists and is valid...
00087                         if( $userInfo ) {
00088                         $userBlogs = $users->getUsersBlogs( $userInfo->getId(), BLOG_STATUS_ACTIVE );
00089                         // check if he or she belogs to any blog. If he or she does, simply
00090                         // get the first one (any better rule for this?)
00091                         if( !empty($userBlogs)) {                                               
00092                                         $blogId = $userBlogs[0]->getId();
00093                         } else{
00094                                 $blogId = $this->_config->getValue('default_blog_id');
00095                         }
00096                     } else{
00097                         $blogId = $this->_config->getValue('default_blog_id');
00098                     }
00099                 }
00100                 else {
00101                     // if there is no user parameter, we take the blogId from the session
00102                     if( $this->_session->getValue('blogId') != '' ) {
00103                         $blogId = $this->_session->getValue('blogId');
00104                     }
00105                     else {
00106                         // get the default blog id from the database
00107                         $blogId = $this->_config->getValue('default_blog_id');                        
00108                     }
00109                 }
00110             }
00111                         
00112             // fetch the BlogInfo object
00113             $blogs = new Blogs();
00114             if( $blogId )
00115                 $this->_blogInfo = $blogs->getBlogInfo( $blogId );
00116             else
00117                 $this->_blogInfo = $blogs->getBlogInfoByName( $blogName );
00118         }
00119                 
00120                 function validate()
00121                 {
00122                         if( !parent::validate())
00123                                 return false;
00124                         
00125                         // before we do anything, let's find out the blogId and if there isn't any, quit
00126                         $this->_getBlogInfo();
00127                         if( $this->_blogInfo == false ) {
00128                                 // return 404 not found because the blog id is not correct!
00129                                 $this->_view = new ResourceServerView();
00130                                 $this->_view->addHeaderResponse( "HTTP/1.1 404 Not Found" );
00131                                 $this->_view->addHeaderResponse( "Status: 404 Not Found" );
00132                                 $this->_view->addHeaderResponse( "X-LifeType-Error: Blog $resId is not correct" );
00133                                 
00134                                 return false;                   
00135                         }
00136 
00137                         //
00138             // security stuff
00139                         //
00140                         // :KLUDGE: the problem with the security Pipeline and the PluginManager in this
00141                         // action is that we need to reimplement the whole stuff because... this action does not
00142                         // inherit from BlogAction and therefore nobody is doing these things for us! I don't really
00143                         // like to do it like this but while I think of a better way, let's leave like it
00144                         //
00145             $pipeline = new Pipeline( $this->_request, $this->_blogInfo );
00146             $result = $pipeline->process();
00147                         
00148             // if the pipeline blocked the request, then we have to let the user know and quit
00149                         // processing
00150             if( !$result->isValid()) {
00151                                 $this->_view = new ResourceServerView();
00152                                 $this->_view->addHeaderResponse( "HTTP/1.1 403 Forbidden" );
00153                                 $this->_view->addHeaderResponse( "Status: 403 Forbidden" );
00154                                 $this->_view->addHeaderResponse( "X-LifeType-Error: Access is blocked" );
00155                         
00156                                 return false;
00157             }
00158                                                 
00159                         // now if the blog id was correct, then we can proceed to get the rest of the parameters
00160                         $this->_resName = $this->_request->getValue( "resource" );
00161                         $this->_resId = $this->_request->getValue( "resId" );
00162                         $this->_albumId = $this->_request->getValue( "albumId" );
00163                         $this->_albumName = $this->_request->getValue( "albumName" );
00164                         $this->_mode = $this->_request->getValue( "mode" );
00165                         
00166                         // check if we need to load the album to figure out the correct album id
00167                         // because we got an album name instead of an album id
00168                         if( !empty($this->_albumId) || !empty($this->_albumName)) {
00169                                 if( $this->_albumName ) {
00170                                         $albums = new GalleryAlbums();
00171                                         $album = $albums->getAlbumByName( $this->_albumName );
00172                                         if( !$album ) {
00173                                                 $this->_view = new ResourceServerView();
00174                                                 $this->_view->addHeaderResponse( "HTTP/1.1 404 Not Found" );
00175                                                 $this->_view->addHeaderResponse( "Status: 404 Not Found" );
00176                                                 $this->_view->addHeaderResponse( "X-LifeType-Error: Album $albumId not found" );
00177                                                 return false;
00178                                         }
00179                                         $this->_albumId = $album->getId();
00180                                 }
00181                         }
00182                         
00183                         return true;
00184                 }
00185                 
00191                 function notifyEvent( $eventType, $params = Array())
00192                 {
00193                         $params[ 'from' ] = $this->_actionInfo->getActionParamValue();
00194                         $params[ 'request' ] = $this->_request;
00195                                         
00196                         return $this->_pm->notifyEvent( $eventType, $params );
00197                 }               
00198                 
00199                 function perform()
00200                 {
00201                         // initialize the plugin manager, needed to inform plugins of the EVENT_RESOURCE_LOADED
00202                         // event, in case any of them is waiting for it! This obviously slows things down but
00203                         // hey, what can I do? Users ask and I deliver...
00204             $this->_pm =& PluginManager::getPluginManager();
00205             $this->_pm->setBlogInfo( $this->_blogInfo );
00206                         $this->_userInfo = $this->_blogInfo->getOwnerInfo();
00207             $this->_pm->setUserInfo( $this->_userInfo );
00208 
00209                         // and fetch the resource
00210                         $resources = new GalleryResources();
00211                         if( $this->_resName ) {
00212                                 $resource = $resources->getResourceFile( $this->_blogInfo->getId(), $this->_resName );
00213                         }
00214                         else {
00215                                 $resource = $resources->getResource( $this->_resId, $this->_blogInfo->getId());
00216                         }
00217 
00218                         if( !$resource ) {
00219                                 // return 404 not found because the resource wasn't found
00220                                 $this->_view = new ResourceServerView();
00221                                 $this->_view->addHeaderResponse( "HTTP/1.1 404 Not Found" );
00222                                 $this->_view->addHeaderResponse( "Status: 404 Not Found" );
00223                                 $this->_view->addHeaderResponse( "X-LifeType-Error: Resource $this->_resId not found" );                
00224                                 
00225                                 return false;
00226                         }
00227                         
00228                         // we need to let plugins know that we have successfully loaded a resource
00229                         $this->notifyEvent( EVENT_RESOURCE_LOADED, Array( "resource" => &$resource ));
00230                         
00231                         // generate the correct view with the resource data...
00232                         $this->_view = new ResourceServerView( $resource, $this->_mode );
00233                         
00234                         return true;
00235                 }
00236         }
00237 ?>