[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/action/ -> resourceserveraction.class.php (source)

   1  <?php
   2  
   3      // the three different modes that a resource can be requested
   4      define( 'RESOURCE_VIEW_MODE_DEFAULT', '' );
   5      define( 'RESOURCE_VIEW_MODE_PREVIEW', 'preview' );
   6      define( 'RESOURCE_VIEW_MODE_MEDIUM', 'medium' );
   7  
   8      lt_include( PLOG_CLASS_PATH."class/action/action.class.php" );
   9      lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );
  10      lt_include( PLOG_CLASS_PATH."class/view/redirectview.class.php" );
  11      lt_include( PLOG_CLASS_PATH."class/net/http/subdomains.class.php" );
  12      lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
  13      lt_include( PLOG_CLASS_PATH."class/gallery/dao/galleryresources.class.php" );    
  14      lt_include( PLOG_CLASS_PATH."class/security/pipeline.class.php" );
  15      lt_include( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );
  16      lt_include( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );    
  17  
  18      /**
  19       * \ingroup Action
  20       * @private
  21       */    
  22      class ResourceServerAction extends Action
  23      {
  24          var $_mode;
  25          var $_resource;
  26          var $_resId;
  27          var $_album;
  28          var $_config;
  29          var $_blogInfo;
  30      
  31  		function ResourceServerAction( $actionInfo, $request )
  32          {
  33              $this->Action( $actionInfo, $request );
  34              
  35              // keep the session for later use
  36              $session = HttpVars::getSession();
  37              $this->_session = $session['SessionInfo'];            
  38              
  39              $this->_config =& Config::getConfig();
  40              
  41              $this->registerFieldValidator( "resource", new StringValidator(), true );
  42              $this->registerFieldValidator( "resId", new IntegerValidator(), true );
  43              $this->registerFieldValidator( "albumId", new IntegerValidator(), true );
  44              $this->registerFieldValidator( "albumName", new StringValidator(), true );
  45              $this->registerFieldValidator( "blogId", new IntegerValidator(), true );
  46              $this->registerFieldValidator( "blogName", new StringValidator(), true );
  47              $this->registerFieldValidator( "userId", new IntegerValidator(), true );
  48              $this->registerFieldValidator( "blogUserName", new StringValidator(), true );            
  49              
  50              // since this class does not return HTML code but files, we cannot
  51              // return HTML so let's return 404 status code with a custom error message
  52              $view = new View();
  53              $view->addHeaderResponse( "HTTP/1.1 404 Not Found" );
  54              $view->addHeaderResponse( "Status: 404 Not Found" );
  55              $view->addHeaderResponse( "X-LifeType-Error: Invalid parameters" );
  56              $this->setValidationErrorView( $view );
  57          }
  58          
  59          /**
  60           * Fetches the information for this blog from the database since we are going to need it
  61           * almost everywhere.
  62           */
  63          function _getBlogInfo()
  64          {            
  65              // see if we're using subdomains
  66              $config =& Config::getConfig();
  67              if( $config->getValue( "subdomains_enabled" )) {
  68                  $subdomainInfo = Subdomains::getSubdomainInfoFromRequest();
  69  
  70                  if( !empty($subdomainInfo["blogdomain"]) && $this->_request->getValue( 'blogDomain' ) == "" ) {
  71                      $this->_request->setValue( 'blogDomain', $subdomainInfo["blogdomain"] );
  72                  }
  73                  if( !empty($subdomainInfo["username"]) && $this->_request->getValue( 'blogUserName' ) == "" ) {
  74                      $this->_request->setValue( 'blogUserName', $subdomainInfo["username"] );
  75                  }
  76                  if( !empty($subdomainInfo["blogname"]) && $this->_request->getValue( 'blogName' ) == "" ) {
  77                      $this->_request->setValue( 'blogName', $subdomainInfo["blogname"] );
  78                  }
  79              }
  80  
  81              $blogId = $this->_request->getValue( 'blogId' );
  82              $blogName = $this->_request->getValue( 'blogName' );
  83              $userId = $this->_request->getValue( 'userId' );
  84              $userName = $this->_request->getValue( 'blogUserName' );
  85              $blogDomain = $this->_request->getValue( 'blogDomain' );
  86              
  87              // if there is a "blogId" parameter, it takes precedence over the
  88              // "user" parameter.
  89              if( !$blogId && !$blogName && !$blogDomain) {
  90                  // check if there was a user parameter
  91                  if( !empty($userName) ) {
  92                      // if so, check to which blogs the user belongs
  93                      lt_include( PLOG_CLASS_PATH."class/dao/users.class.php" );
  94                      $users = new Users();
  95                       $userInfo = $users->getUserInfoFromUsername( $userName );
  96                      // if the user exists and is valid...
  97                      if( $userInfo ) {
  98                          $userBlogs = $users->getUsersBlogs( $userInfo->getId(), BLOG_STATUS_ACTIVE );
  99                          // check if he or she belogs to any blog. If he or she does, simply
 100                          // get the first one (any better rule for this?)
 101                          if( !empty($userBlogs)) {                        
 102                              $blogId = $userBlogs[0]->getId();
 103                          } else{
 104                              $blogId = $this->_config->getValue('default_blog_id');
 105                          }
 106                      } else{
 107                          $blogId = $this->_config->getValue('default_blog_id');
 108                      }
 109                  }
 110                  else {
 111                      // if there is no user parameter, we take the blogId from the session
 112                      if( $this->_session->getValue('blogId') != '' ) {
 113                          $blogId = $this->_session->getValue('blogId');
 114                      }
 115                      else {
 116                          // get the default blog id from the database
 117                          $blogId = $this->_config->getValue('default_blog_id');                        
 118                      }
 119                  }
 120              }
 121              
 122              // fetch the BlogInfo object
 123              lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" );            
 124              $blogs = new Blogs();
 125              if( $blogId ) {
 126                  $this->_blogInfo = $blogs->getBlogInfo( $blogId );
 127              }
 128              else if($blogName){
 129                  $this->_blogInfo = $blogs->getBlogInfoByName( $blogName );
 130              }
 131              else if($blogDomain){
 132                  $this->_blogInfo = $blogs->getBlogInfoByDomain( $blogDomain );
 133              }
 134              else{
 135                  $this->_blogInfo = false;
 136              }
 137          }
 138          
 139  		function validate()
 140          {
 141              if( !parent::validate())
 142                  return false;
 143              
 144              // before we do anything, let's find out the blogId and if there isn't any, quit
 145              $this->_getBlogInfo();
 146              if( $this->_blogInfo == false ) {
 147                  // return 404 not found because the blog id is not correct!
 148                  $this->_view = new View();
 149                  $this->_view->addHeaderResponse( "HTTP/1.1 404 Not Found" );
 150                  $this->_view->addHeaderResponse( "Status: 404 Not Found" );
 151                  $this->_view->addHeaderResponse( "X-LifeType-Error: Blog $resId is not correct" );
 152                  
 153                  return false;            
 154              }
 155  
 156              // now if the blog id was correct, then we can proceed to get the rest of the parameters
 157              $this->_resName = $this->_request->getValue( "resource" );
 158              $this->_resId = $this->_request->getValue( "resId" );
 159              $this->_albumId = $this->_request->getValue( "albumId" );
 160              $this->_albumName = $this->_request->getValue( "albumName" );
 161              $this->_mode = $this->_request->getValue( "mode" );
 162              
 163              // check if we need to load the album to figure out the correct album id
 164              // because we got an album name instead of an album id
 165              if( !empty($this->_albumId) || !empty($this->_albumName)) {
 166                  if( $this->_albumName ) {
 167                      lt_include( PLOG_CLASS_PATH."class/gallery/dao/galleryalbums.class.php" );
 168                      $albums = new GalleryAlbums();
 169                      $album = $albums->getAlbumByName( $this->_albumName );
 170                      if( !$album ) {
 171                          $this->_view = new View();
 172                          $this->_view->addHeaderResponse( "HTTP/1.1 404 Not Found" );
 173                          $this->_view->addHeaderResponse( "Status: 404 Not Found" );
 174                          $this->_view->addHeaderResponse( "X-LifeType-Error: Album $albumId not found" );
 175                          return false;
 176                      }
 177                      $this->_albumId = $album->getId();
 178                  }
 179              }
 180              
 181              return true;
 182          }
 183          
 184  		function perform()
 185          {
 186              // and fetch the resource
 187              $resources = new GalleryResources();
 188              if( $this->_resName ) {
 189                  $resource = $resources->getResourceFile( $this->_blogInfo->getId(), $this->_resName );
 190              }
 191              else {
 192                  $resource = $resources->getResource( $this->_resId, $this->_blogInfo->getId());
 193              }
 194  
 195              if( !$resource ) {
 196                  // return 404 not found because the resource wasn't found
 197                  $this->_view = new View();
 198                  $this->_view->addHeaderResponse( "HTTP/1.1 404 Not Found" );
 199                  $this->_view->addHeaderResponse( "Status: 404 Not Found" );
 200                  $this->_view->addHeaderResponse( "X-LifeType-Error: Resource $this->_resId not found" );        
 201                  
 202                  return false;
 203              }
 204              
 205              $url = $this->_blogInfo->getBlogRequestGenerator();
 206              switch( $this->_mode ) {
 207                  case RESOURCE_VIEW_MODE_PREVIEW:
 208                      $redirectUrl = $url->resourcePreviewLink( $resource );
 209                      break;
 210                  case RESOURCE_VIEW_MODE_MEDIUM:
 211                      $redirectUrl = $url->resourceMediumSizePreviewLink( $resource );
 212                      break;
 213                  default:
 214                      $redirectUrl = $url->resourceDownloadLink( $resource );
 215                      break;
 216              }
 217              
 218              // generate the correct view with the resource data...            
 219              $this->_view = new RedirectView( $redirectUrl, $this->_mode );
 220              
 221              return true;
 222          }
 223      }
 224  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics