[ 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/net/ -> customurlhandler.class.php (source)

   1  <?php
   2  
   3  
   4      
   5      lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );
   6      lt_include( PLOG_CLASS_PATH."class/net/linkformatmatcher.class.php" );
   7      lt_include( PLOG_CLASS_PATH."class/net/url.class.php" );    
   8  
   9      /**
  10       * \ingroup Net
  11       *
  12       * Parses incoming URLs when "custom URLs" are enabled. It uses the LinkFormatMatcher
  13       * to determine which *_link_format configuration setting matches the incoming URL, extracts
  14       * the information from the URL according to the specified format and puts it back to the
  15       * request so that the core classes can fetch the parameters.
  16       *
  17       * The use of this class is very specific and it should not be be called directly anyway.
  18       *
  19       * @see LinkFormatMatcher
  20       */    
  21      class CustomUrlHandler 
  22      {
  23      
  24          var $_formats;
  25          var $_params;
  26          var $_vars;
  27          var $_includeFile;
  28          var $_format;
  29          var $_indexName;
  30      
  31  		function CustomUrlHandler()
  32          {
  33              
  34              
  35              // we need to figure out the full server path so that we can generate proper
  36              // regexps for the url parsing. This should get rid of issues like
  37              // http://bugs.plogworld.net/view.php?id=369
  38              // The base url will be one or another depending
  39              $config =& Config::getConfig();
  40              if( $config->getValue( "subdomains_enabled" ))
  41                  $url = new Url( $config->getValue( "subdomains_base_url" ));
  42              else
  43                  $url = new Url( $config->getValue( "base_url" ));
  44              $path = $url->getPath();
  45              
  46              // intialize the array we're going to use
  47              $config =& Config::getConfig();
  48              $this->_indexName = $config->getValue("script_name");
  49              $this->_formats = Array( "permalink_format" => $path.$config->getValue( "permalink_format" ),
  50                                       "category_link_format" => $path.$config->getValue( "category_link_format" ),
  51                                       "blog_link_format" => $path.$config->getValue( "blog_link_format" ),
  52                                       "archive_link_format" => $path.$config->getValue( "archive_link_format" ),
  53                                       "user_posts_link_format" => $path.$config->getValue( "user_posts_link_format" ),
  54                                       "post_trackbacks_link_format" => $path.$config->getValue( "post_trackbacks_link_format" ),
  55                                       "template_link_format" => $path.$config->getValue( "template_link_format" ),
  56                                       "album_link_format" => $path.$config->getValue( "album_link_format" ),
  57                                       "resource_link_format" => $path.$config->getValue( "resource_link_format" ),
  58                                       "resource_download_link_format" => $path.$config->getValue( "resource_download_link_format" ),
  59                                       "resource_preview_link_format" => $path.$config->getValue( "resource_preview_link_format" ),
  60                                       "resource_medium_size_preview_link_format" => $path.$config->getValue( "resource_medium_size_preview_link_format" ));
  61              // if the url did not match any of the current settings, then let's try to parse it as an old
  62              // "search engine friendly" url
  63              $this->_fallback = Array( "permalink_format" => $path."/post/{blogid}/{postid}",
  64                                       "category_link_format" => $path."/category/{blogid}/{catid}",
  65                                       "blog_link_format" => $path."/{blogid}",
  66                                       "archive_link_format" => $path."/archives/{blogid}/{year}/{month}/{day}",
  67                                       "user_posts_link_format" => $path."/user/{blogid}/{userid}",
  68                                       "post_trackbacks_link_format" => $path."/trackbacks/{blogid}/{postid}",
  69                                       "template_link_format" => $path."/static/{blogid}/{templatename}",
  70                                       "album_link_format" => $path."/album/{blogid}/{albumid}",
  71                                       "resource_link_format" => $path."/resource/{blogid}/{resourceid}",
  72                                       "resource_download_link_format" => $path."/get/{blogid}/{resourceid}",
  73                                       "resource_preview_link_format" => "",  // this one does not exist
  74                                       "resource_medium_size_preview_link_format" => "" ); // this one does not exist either
  75          }
  76          
  77          /**
  78           * @private
  79           */
  80  		function getPageParameterValue( $requestUri )        
  81          {
  82              $config =& Config::getConfig();
  83              $pageSuffix = $config->getValue( "page_suffix_format" );
  84              $pageSuffixRegexp = str_replace( "{page}", "([0-9]*)", $pageSuffix );
  85              $pageSuffixRegexp = "/".str_replace( "/", "\/", $pageSuffixRegexp )."/";
  86              //print($pageSuffixRegexp."<br/>");            
  87              if( preg_match( $pageSuffixRegexp, $requestUri, $matches ))
  88                  $page = $matches[1];
  89              else
  90                  $page = "";
  91                  
  92              //print("page = $page<br/>");
  93              
  94              return( $page );
  95          }
  96          
  97          /**
  98           * @private
  99           * Given a request uri/url, remove the suffix which is used for the paging if any         
 100           */
 101  		function removePageSuffix( $requestUri )
 102          {
 103              $config =& Config::getConfig();
 104              $pageSuffix = $config->getValue( "page_suffix_format" );
 105              $pageSuffixRegexp = str_replace( "{page}", "[0-9]*", $pageSuffix );
 106              $pageSuffixRegexp = "/".str_replace( "/", "\/", $pageSuffixRegexp )."/";
 107              $requestUri = preg_replace( $pageSuffixRegexp, "", $requestUri );
 108              //print($pageSuffixRegexp." - ".$requestUri);
 109              
 110              return( $requestUri );        
 111          }
 112          
 113  		function process( $requestUri )
 114          {
 115              // decode the string, since it seems that php will not do it for us in this case...
 116              $requestUri = urldecode( $requestUri);
 117              // we should remove anything that comes after a '?' parameter, since we don't want to take
 118              // HTTP GET parameters into account                        
 119              if(( $pos = strpos( $requestUri, '?' ))) {
 120                  // if so, remove everything including the question mark
 121                  $requestUri = substr( $requestUri, 0, $pos );
 122              }
 123              
 124              // once we're done with everything else, let's get the value page parameter and save it            
 125              $page = $this->getPageParameterValue( $requestUri );
 126              
 127              // and now we can remove remove the page suffix so that it doesn't interfere with the 
 128              // url parser
 129              $requestUri = $this->removePageSuffix( $requestUri );                                          
 130              
 131              // guess which format we're using...
 132              $m = new LinkFormatMatcher( $requestUri, $this->_formats );
 133              $this->_format = $m->identify();
 134              $this->_params = $m->getParameters();
 135              
 136              
 137              // if it didn't work out the first time, let's try with an additional url format
 138              if( !$this->_fillRequestParameters()) {
 139                  $m = new LinkFormatMatcher( $requestUri, $this->_fallback );
 140                  $this->_format = $m->identify();
 141                  $this->_params = $m->getParameters();
 142                  $this->_fillRequestParameters();
 143              }
 144              
 145              // put the parameter back as a parameter
 146              $this->_params["page"] = $page;
 147                          
 148              return( true );
 149          }
 150          
 151          /**
 152           * @private
 153           */
 154  		function _fillRequestParameters()
 155          {
 156              // ...and then based on this, fill in the parameters in the request
 157              $matched = true;
 158              if( $this->_format == "permalink_format" ) {
 159                  $this->_includeFile = $this->_indexName;
 160                  if ( array_key_exists( "year", $this->_params ) )
 161                  {
 162                      $this->_params["date"] = $this->_params["year"];
 163                      if( array_key_exists( "month", $this->_params ) )
 164                      {
 165                          $this->_params["date"] .= $this->_params["month"];
 166                          if( array_key_exists( "day", $this->_params ) )
 167                          {
 168                              $this->_params["date"] .= $this->_params["day"];
 169                              if ( array_key_exists( "hours", $this->_params ) )
 170                              {
 171                                  $this->_params["date"] .= $this->_params["hours"];
 172                                  if( array_key_exists( "minutes", $this->_params ) )
 173                                      $this->_params["date"] .= $this->_params["minutes"];
 174                              }
 175                          }
 176                      }
 177                  }
 178                  $this->_params["op"] = "ViewArticle";
 179                  $this->_vars = Array( "postid" => "articleId",
 180                                 "postname" => "articleName",
 181                                 "blogid" => "blogId",
 182                                 "blogname" => "blogName",
 183                                 "userid" => "userId",
 184                                 "username" => "userName",
 185                                 "catid" => "postCategoryId",
 186                                 "catname" => "postCategoryName",
 187                                 "date" => "Date",
 188                                 "blogowner" => "blogUserName" );
 189              }
 190              elseif( $this->_format == "blog_link_format" ) {
 191                  $this->_includeFile = $this->_indexName;    
 192                  $this->_params["op"] = "Default";
 193                  $this->_vars = Array( "blogid" => "blogId",
 194                                        "blogname" => "blogName",
 195                                        "blogowner" => "blogUserName" );
 196              }
 197              elseif( $this->_format == "category_link_format" ) {
 198                  $this->_includeFile = $this->_indexName;    
 199                  $this->_params["op"] = "Default";
 200                  $this->_vars = Array( "blogid" => "blogId",
 201                                 "blogname" => "blogName",
 202                                 "blogowner" => "blogUserName",                               
 203                                 "catid" => "postCategoryId",
 204                                 "catname" => "postCategoryName" );
 205              }
 206              elseif( $this->_format == "archive_link_format" ) {
 207                  $this->_includeFile = $this->_indexName;    
 208                  if ( array_key_exists( "year", $this->_params ) )
 209                  {
 210                      $this->_params["date"] = $this->_params["year"];
 211                      if( array_key_exists( "month", $this->_params ) )
 212                      {
 213                          $this->_params["date"] .= $this->_params["month"];
 214                          if( array_key_exists( "day", $this->_params ) )
 215                          {
 216                              $this->_params["date"] .= $this->_params["day"];
 217                              if ( array_key_exists( "hours", $this->_params ) )
 218                              {
 219                                  $this->_params["date"] .= $this->_params["hours"];
 220                                  if( array_key_exists( "minutes", $this->_params ) )
 221                                      $this->_params["date"] .= $this->_params["minutes"];
 222                              }
 223                          }
 224                      }
 225                  }
 226                  $this->_params["op"] = "Default";
 227                  $this->_vars = Array( "blogid" => "blogId",
 228                                 "blogname" => "blogName",
 229                                 "blogowner" => "blogUserName",                               
 230                                 "date" => "Date" );
 231              }
 232              elseif( $this->_format == "user_posts_link_format" ) {
 233                  $this->_includeFile = $this->_indexName;    
 234                  if ( array_key_exists( "year", $this->_params ) )
 235                  {
 236                      $this->_params["date"] = $this->_params["year"];
 237                      if( array_key_exists( "month", $this->_params ) )
 238                      {
 239                          $this->_params["date"] .= $this->_params["month"];
 240                          if( array_key_exists( "day", $this->_params ) )
 241                          {
 242                              $this->_params["date"] .= $this->_params["day"];
 243                              if ( array_key_exists( "hours", $this->_params ) )
 244                              {
 245                                  $this->_params["date"] .= $this->_params["hours"];
 246                                  if( array_key_exists( "minutes", $this->_params ) )
 247                                      $this->_params["date"] .= $this->_params["minutes"];
 248                              }
 249                          }
 250                      }
 251                  }
 252                  $this->_params["op"] = "Default";
 253                  $this->_vars = Array( "blogid" => "blogId",
 254                                 "blogname" => "blogName",
 255                                 "blogowner" => "blogUserName",                               
 256                                 "date" => "Date",
 257                                 "userid" => "userId",
 258                                 "catid" => "postCategoryId",
 259                                 "catname" => "postCategoryName",
 260                                 "username" => "userName" );
 261              }
 262              elseif( $this->_format == "post_trackbacks_link_format" ) {
 263                  $this->_includeFile = $this->_indexName;    
 264                  $this->_params["op"] = "Trackbacks";
 265                  if ( array_key_exists( "year", $this->_params ) )
 266                  {
 267                      $this->_params["date"] = $this->_params["year"];
 268                      if( array_key_exists( "month", $this->_params ) )
 269                      {
 270                          $this->_params["date"] .= $this->_params["month"];
 271                          if( array_key_exists( "day", $this->_params ) )
 272                          {
 273                              $this->_params["date"] .= $this->_params["day"];
 274                              if ( array_key_exists( "hours", $this->_params ) )
 275                              {
 276                                  $this->_params["date"] .= $this->_params["hours"];
 277                                  if( array_key_exists( "minutes", $this->_params ) )
 278                                      $this->_params["date"] .= $this->_params["minutes"];
 279                              }
 280                          }
 281                      }
 282                  }
 283                  $this->_vars = Array( "blogid" => "blogId",
 284                                 "blogname" => "blogName",
 285                                 "blogowner" => "blogUserName",                               
 286                                 "postid" => "articleId",
 287                                 "postname" => "articleName",
 288                                 "userid" => "userId",
 289                                 "username" => "userName",
 290                                 "catid" => "postCategoryId",
 291                                 "catname" => "postCategoryName",
 292                                 "date" => "Date" );                       
 293              }
 294              elseif( $this->_format == "template_link_format" ) {
 295                  $this->_includeFile = $this->_indexName;    
 296                  $this->_params["op"] = "Template";
 297                  $this->_vars = Array( "templatename" => "show",
 298                                 "blogid" => "blogId",
 299                                 "blogowner" => "blogUserName",                               
 300                                 "blogname" => "blogName" );
 301              }
 302              elseif( $this->_format == "album_link_format" ) {
 303                  $this->_includeFile = $this->_indexName;    
 304                  $this->_params["op"] = "ViewAlbum";
 305                  $this->_vars = Array( "blogid" => "blogId",
 306                                 "blogname" => "blogName",
 307                                 "albumid" => "albumId",
 308                                 "blogowner" => "blogUserName",                               
 309                                 "albumname" => "albumName" );
 310              }
 311              elseif( $this->_format == "resource_link_format" ) {
 312                  $this->_includeFile = $this->_indexName;
 313                  $this->_params["op"] = "ViewResource";
 314                  $this->_vars = Array( "blogid" => "blogId",
 315                                 "blogname" => "blogName",
 316                                 "blogowner" => "blogUserName",                               
 317                                 "albumid" => "albumId",
 318                                 "albumname" => "albumName",
 319                                 "resourceid" => "resId",
 320                                 "resourcename" => "resource" );
 321              }
 322              elseif( $this->_format == "resource_download_link_format" ) {
 323                  $this->_includeFile = "resserver.php";
 324                  $this->_vars = Array( "blogid" => "blogId",
 325                                 "blogname" => "blogName",
 326                                 "blogowner" => "blogUserName",                               
 327                                 "albumid" => "albumId",
 328                                 "albumname" => "albumName",
 329                                 "resourceid" => "resId",
 330                                 "resourcename" => "resource" );
 331              }
 332              elseif( $this->_format == "resource_preview_link_format" ) {
 333                  $this->_includeFile = "resserver.php";
 334                  $this->_params["mode"] = "preview";
 335                  $this->_vars = Array( "blogid" => "blogId",
 336                                 "blogname" => "blogName",
 337                                 "blogowner" => "blogUserName",                               
 338                                 "albumid" => "albumId",
 339                                 "albumname" => "albumName",
 340                                 "resourceid" => "resId",
 341                                 "resourcename" => "resource",
 342                                 "mode" => "mode");
 343              }
 344              elseif( $this->_format == "resource_medium_size_preview_link_format" ) {
 345                  $this->_includeFile = "resserver.php";
 346                  $this->_params["mode"] = "medium";
 347                  $this->_vars = Array( "blogid" => "blogId",
 348                                 "blogname" => "blogName",
 349                                 "blogowner" => "blogUserName",                               
 350                                 "albumid" => "albumId",
 351                                 "albumname" => "albumName",
 352                                 "resourceid" => "resId",
 353                                 "resourcename" => "resource",
 354                                 "mode" => "mode");            
 355              }
 356              else {
 357                  $this->_includeFile = $this->_indexName;
 358                  $matched = false;
 359              }
 360              
 361              // this must be put in the _vars array so that client classes, when checking
 362              // for the parameters that were identified in the request, can also include
 363              // the page parameter
 364              $this->_vars["page"] = "page";
 365              
 366              return( $matched );
 367          }
 368          
 369  		function getVars()
 370          {
 371              return $this->_vars;
 372          }
 373          
 374  		function getParams()
 375          {
 376              return $this->_params;
 377          }
 378          
 379  		function getIncludeFile()
 380          {
 381              return $this->_includeFile;
 382          }
 383      }
 384  ?>


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