[ Index ]
 

Code source de WordPress 2.1.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/wp-includes/ -> rewrite.php (source)

   1  <?php
   2  
   3  /* WP_Rewrite API
   4  *******************************************************************************/
   5  
   6  //Add a straight rewrite rule
   7  function add_rewrite_rule($regex, $redirect) {
   8      global $wp_rewrite;
   9      $wp_rewrite->add_rule($regex, $redirect);
  10  }
  11  
  12  //Add a new tag (like %postname%)
  13  //warning: you must call this on init or earlier, otherwise the query var addition stuff won't work
  14  function add_rewrite_tag($tagname, $regex) {
  15      //validation
  16      if (strlen($tagname) < 3 || $tagname{0} != '%' || $tagname{strlen($tagname)-1} != '%') {
  17          return;
  18      }
  19  
  20      $qv = trim($tagname, '%');
  21  
  22      global $wp_rewrite, $wp;
  23      $wp->add_query_var($qv);
  24      $wp_rewrite->add_rewrite_tag($tagname, $regex, $qv . '=');
  25  }
  26  
  27  //Add a new feed type like /atom1/
  28  function add_feed($feedname, $function) {
  29      global $wp_rewrite;
  30      if (!in_array($feedname, $wp_rewrite->feeds)) { //override the file if it is
  31          $wp_rewrite->feeds[] = $feedname;
  32      }
  33      $hook = 'do_feed_' . $feedname;
  34      remove_action($hook, $function, 10, 1);
  35      add_action($hook, $function, 10, 1);
  36      return $hook;
  37  }
  38  
  39  define('EP_PERMALINK',  1   );
  40  define('EP_ATTACHMENT', 2   );
  41  define('EP_DATE',       4   );
  42  define('EP_YEAR',       8   );
  43  define('EP_MONTH',      16  );
  44  define('EP_DAY',        32  );
  45  define('EP_ROOT',       64  );
  46  define('EP_COMMENTS',   128 );
  47  define('EP_SEARCH',     256 );
  48  define('EP_CATEGORIES', 512 );
  49  define('EP_AUTHORS',    1024);
  50  define('EP_PAGES',      2048);
  51  //pseudo-places
  52  define('EP_NONE',       0  );
  53  define('EP_ALL',        255);
  54  
  55  //and an endpoint, like /trackback/
  56  function add_rewrite_endpoint($name, $places) {
  57      global $wp_rewrite;
  58      $wp_rewrite->add_endpoint($name, $places);
  59  }
  60  
  61  // examine a url (supposedly from this blog) and try to
  62  // determine the post ID it represents.
  63  function url_to_postid($url) {
  64      global $wp_rewrite;
  65  
  66      // First, check to see if there is a 'p=N' or 'page_id=N' to match against
  67      preg_match('#[?&](p|page_id)=(\d+)#', $url, $values);
  68      $id = intval($values[2]);
  69      if ( $id ) return $id;
  70  
  71      // Check to see if we are using rewrite rules
  72      $rewrite = $wp_rewrite->wp_rewrite_rules();
  73  
  74      // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
  75      if ( empty($rewrite) )
  76          return 0;
  77  
  78      // $url cleanup by Mark Jaquith
  79      // This fixes things like #anchors, ?query=strings, missing 'www.',
  80      // added 'www.', or added 'index.php/' that will mess up our WP_Query
  81      // and return a false negative
  82  
  83      // Get rid of the #anchor
  84      $url_split = explode('#', $url);
  85      $url = $url_split[0];
  86  
  87      // Get rid of URL ?query=string
  88      $url_split = explode('?', $url);
  89      $url = $url_split[0];
  90  
  91      // Add 'www.' if it is absent and should be there
  92      if ( false !== strpos(get_option('home'), '://www.') && false === strpos($url, '://www.') )
  93          $url = str_replace('://', '://www.', $url);
  94  
  95      // Strip 'www.' if it is present and shouldn't be
  96      if ( false === strpos(get_option('home'), '://www.') )
  97          $url = str_replace('://www.', '://', $url);
  98  
  99      // Strip 'index.php/' if we're not using path info permalinks
 100      if ( !$wp_rewrite->using_index_permalinks() )
 101          $url = str_replace('index.php/', '', $url);
 102  
 103      if ( false !== strpos($url, get_option('home')) ) {
 104          // Chop off http://domain.com
 105          $url = str_replace(get_option('home'), '', $url);
 106      } else {
 107          // Chop off /path/to/blog
 108          $home_path = parse_url(get_option('home'));
 109          $home_path = $home_path['path'];
 110          $url = str_replace($home_path, '', $url);
 111      }
 112  
 113      // Trim leading and lagging slashes
 114      $url = trim($url, '/');
 115  
 116      $request = $url;
 117  
 118      // Done with cleanup
 119  
 120      // Look for matches.
 121      $request_match = $request;
 122      foreach ($rewrite as $match => $query) {
 123          // If the requesting file is the anchor of the match, prepend it
 124          // to the path info.
 125          if ( (! empty($url)) && (strpos($match, $url) === 0) ) {
 126              $request_match = $url . '/' . $request;
 127          }
 128  
 129          if ( preg_match("!^$match!", $request_match, $matches) ) {
 130              // Got a match.
 131              // Trim the query of everything up to the '?'.
 132              $query = preg_replace("!^.+\?!", '', $query);
 133  
 134              // Substitute the substring matches into the query.
 135              eval("\$query = \"$query\";");
 136              $query = new WP_Query($query);
 137              if ( $query->is_single || $query->is_page )
 138                  return $query->post->ID;
 139              else
 140                  return 0;
 141          }
 142      }
 143      return 0;
 144  }
 145  
 146  /* WP_Rewrite class
 147  *******************************************************************************/
 148  
 149  class WP_Rewrite {
 150      var $permalink_structure;
 151      var $category_base;
 152      var $category_structure;
 153      var $author_base = 'author';
 154      var $author_structure;
 155      var $date_structure;
 156      var $page_structure;
 157      var $search_base = 'search';
 158      var $search_structure;
 159      var $comments_base = 'comments';
 160      var $feed_base = 'feed';
 161      var $comments_feed_structure;
 162      var $feed_structure;
 163      var $front;
 164      var $root = '';
 165      var $index = 'index.php';
 166      var $matches = '';
 167      var $rules;
 168      var $extra_rules; //those not generated by the class, see add_rewrite_rule()
 169      var $non_wp_rules; //rules that don't redirect to WP's index.php
 170      var $endpoints;
 171      var $use_verbose_rules = false;
 172      var $rewritecode =
 173          array(
 174                      '%year%',
 175                      '%monthnum%',
 176                      '%day%',
 177                      '%hour%',
 178                      '%minute%',
 179                      '%second%',
 180                      '%postname%',
 181                      '%post_id%',
 182                      '%category%',
 183                      '%author%',
 184                      '%pagename%',
 185                      '%search%'
 186                      );
 187  
 188      var $rewritereplace =
 189          array(
 190                      '([0-9]{4})',
 191                      '([0-9]{1,2})',
 192                      '([0-9]{1,2})',
 193                      '([0-9]{1,2})',
 194                      '([0-9]{1,2})',
 195                      '([0-9]{1,2})',
 196                      '([^/]+)',
 197                      '([0-9]+)',
 198                      '(.+?)',
 199                      '([^/]+)',
 200                      '([^/]+)',
 201                      '(.+)'
 202                      );
 203  
 204      var $queryreplace =
 205          array (
 206                      'year=',
 207                      'monthnum=',
 208                      'day=',
 209                      'hour=',
 210                      'minute=',
 211                      'second=',
 212                      'name=',
 213                      'p=',
 214                      'category_name=',
 215                      'author_name=',
 216                      'pagename=',
 217                      's='
 218                      );
 219  
 220      var $feeds = array ( 'feed', 'rdf', 'rss', 'rss2', 'atom' );
 221  
 222  	function using_permalinks() {
 223          if (empty($this->permalink_structure))
 224              return false;
 225          else
 226              return true;
 227      }
 228  
 229  	function using_index_permalinks() {
 230          if (empty($this->permalink_structure)) {
 231              return false;
 232          }
 233  
 234          // If the index is not in the permalink, we're using mod_rewrite.
 235          if (preg_match('#^/*' . $this->index . '#', $this->permalink_structure)) {
 236              return true;
 237          }
 238  
 239          return false;
 240      }
 241  
 242      function using_mod_rewrite_permalinks() {
 243          if ( $this->using_permalinks() && ! $this->using_index_permalinks())
 244              return true;
 245          else
 246              return false;
 247      }
 248  
 249      function preg_index($number) {
 250          $match_prefix = '$';
 251          $match_suffix = '';
 252  
 253          if (! empty($this->matches)) {
 254              $match_prefix = '$' . $this->matches . '[';
 255              $match_suffix = ']';
 256          }
 257  
 258          return "$match_prefix$number$match_suffix";
 259      }
 260  
 261      function page_rewrite_rules() {
 262          $uris = get_option('page_uris');
 263          $attachment_uris = get_option('page_attachment_uris');
 264  
 265          $rewrite_rules = array();
 266          $page_structure = $this->get_page_permastruct();
 267          if( is_array( $attachment_uris ) ) {
 268              foreach ($attachment_uris as $uri => $pagename) {
 269                  $this->add_rewrite_tag('%pagename%', "($uri)", 'attachment=');
 270                  $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
 271              }
 272          }
 273          if( is_array( $uris ) ) {
 274              foreach ($uris as $uri => $pagename) {
 275                  $this->add_rewrite_tag('%pagename%', "($uri)", 'pagename=');
 276                  $rewrite_rules = array_merge($rewrite_rules, $this->generate_rewrite_rules($page_structure, EP_PAGES));
 277              }
 278          }
 279  
 280          return $rewrite_rules;
 281      }
 282  
 283      function get_date_permastruct() {
 284          if (isset($this->date_structure)) {
 285              return $this->date_structure;
 286          }
 287  
 288          if (empty($this->permalink_structure)) {
 289              $this->date_structure = '';
 290              return false;
 291          }
 292  
 293          // The date permalink must have year, month, and day separated by slashes.
 294          $endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%');
 295  
 296          $this->date_structure = '';
 297          $date_endian = '';
 298  
 299          foreach ($endians as $endian) {
 300              if (false !== strpos($this->permalink_structure, $endian)) {
 301                  $date_endian= $endian;
 302                  break;
 303              }
 304          }
 305  
 306          if ( empty($date_endian) )
 307              $date_endian = '%year%/%monthnum%/%day%';
 308  
 309          // Do not allow the date tags and %post_id% to overlap in the permalink
 310          // structure. If they do, move the date tags to $front/date/.
 311          $front = $this->front;
 312          preg_match_all('/%.+?%/', $this->permalink_structure, $tokens);
 313          $tok_index = 1;
 314          foreach ($tokens[0] as $token) {
 315              if ( ($token == '%post_id%') && ($tok_index <= 3) ) {
 316                  $front = $front . 'date/';
 317                  break;
 318              }
 319              $tok_index++;
 320          }
 321  
 322          $this->date_structure = $front . $date_endian;
 323  
 324          return $this->date_structure;
 325      }
 326  
 327      function get_year_permastruct() {
 328          $structure = $this->get_date_permastruct($this->permalink_structure);
 329  
 330          if (empty($structure)) {
 331              return false;
 332          }
 333  
 334          $structure = str_replace('%monthnum%', '', $structure);
 335          $structure = str_replace('%day%', '', $structure);
 336  
 337          $structure = preg_replace('#/+#', '/', $structure);
 338  
 339          return $structure;
 340      }
 341  
 342      function get_month_permastruct() {
 343          $structure = $this->get_date_permastruct($this->permalink_structure);
 344  
 345          if (empty($structure)) {
 346              return false;
 347          }
 348  
 349          $structure = str_replace('%day%', '', $structure);
 350  
 351          $structure = preg_replace('#/+#', '/', $structure);
 352  
 353          return $structure;
 354      }
 355  
 356      function get_day_permastruct() {
 357          return $this->get_date_permastruct($this->permalink_structure);
 358      }
 359  
 360      function get_category_permastruct() {
 361          if (isset($this->category_structure)) {
 362              return $this->category_structure;
 363          }
 364  
 365          if (empty($this->permalink_structure)) {
 366              $this->category_structure = '';
 367              return false;
 368          }
 369  
 370          if (empty($this->category_base))
 371              $this->category_structure = $this->front . 'category/';
 372          else
 373              $this->category_structure = $this->category_base . '/';
 374  
 375          $this->category_structure .= '%category%';
 376  
 377          return $this->category_structure;
 378      }
 379  
 380      function get_author_permastruct() {
 381          if (isset($this->author_structure)) {
 382              return $this->author_structure;
 383          }
 384  
 385          if (empty($this->permalink_structure)) {
 386              $this->author_structure = '';
 387              return false;
 388          }
 389  
 390          $this->author_structure = $this->front . $this->author_base . '/%author%';
 391  
 392          return $this->author_structure;
 393      }
 394  
 395      function get_search_permastruct() {
 396          if (isset($this->search_structure)) {
 397              return $this->search_structure;
 398          }
 399  
 400          if (empty($this->permalink_structure)) {
 401              $this->search_structure = '';
 402              return false;
 403          }
 404  
 405          $this->search_structure = $this->root . $this->search_base . '/%search%';
 406  
 407          return $this->search_structure;
 408      }
 409  
 410      function get_page_permastruct() {
 411          if (isset($this->page_structure)) {
 412              return $this->page_structure;
 413          }
 414  
 415          if (empty($this->permalink_structure)) {
 416              $this->page_structure = '';
 417              return false;
 418          }
 419  
 420          $this->page_structure = $this->root . '%pagename%';
 421  
 422          return $this->page_structure;
 423      }
 424  
 425      function get_feed_permastruct() {
 426          if (isset($this->feed_structure)) {
 427              return $this->feed_structure;
 428          }
 429  
 430          if (empty($this->permalink_structure)) {
 431              $this->feed_structure = '';
 432              return false;
 433          }
 434  
 435          $this->feed_structure = $this->root . $this->feed_base . '/%feed%';
 436  
 437          return $this->feed_structure;
 438      }
 439  
 440      function get_comment_feed_permastruct() {
 441          if (isset($this->comment_feed_structure)) {
 442              return $this->comment_feed_structure;
 443          }
 444  
 445          if (empty($this->permalink_structure)) {
 446              $this->comment_feed_structure = '';
 447              return false;
 448          }
 449  
 450          $this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%';
 451  
 452          return $this->comment_feed_structure;
 453      }
 454  
 455      function add_rewrite_tag($tag, $pattern, $query) {
 456          // If the tag already exists, replace the existing pattern and query for
 457          // that tag, otherwise add the new tag, pattern, and query to the end of
 458          // the arrays.
 459          $position = array_search($tag, $this->rewritecode);
 460          if (FALSE !== $position && NULL !== $position) {
 461              $this->rewritereplace[$position] = $pattern;
 462              $this->queryreplace[$position] = $query;
 463          } else {
 464              $this->rewritecode[] = $tag;
 465              $this->rewritereplace[] = $pattern;
 466              $this->queryreplace[] = $query;
 467          }
 468      }
 469  
 470      //the main WP_Rewrite function. generate the rules from permalink structure
 471      function generate_rewrite_rules($permalink_structure, $ep_mask = EP_NONE, $paged = true, $feed = true, $forcomments = false, $walk_dirs = true, $endpoints = true) {
 472          //build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/?
 473          $feedregex2 = '';
 474          foreach ($this->feeds as $feed_name) {
 475              $feedregex2 .= $feed_name . '|';
 476          }
 477          $feedregex2 = '(' . trim($feedregex2, '|') .  ')/?$';
 478          //$feedregex is identical but with /feed/ added on as well, so URLs like <permalink>/feed/atom
 479          //and <permalink>/atom are both possible
 480          $feedregex = $this->feed_base  . '/' . $feedregex2;
 481  
 482          //build a regex to match the trackback and page/xx parts of URLs
 483          $trackbackregex = 'trackback/?$';
 484          $pageregex = 'page/?([0-9]{1,})/?$';
 485  
 486          //build up an array of endpoint regexes to append => queries to append
 487          if ($endpoints) {
 488              $ep_query_append = array ();
 489              foreach ($this->endpoints as $endpoint) {
 490                  //match everything after the endpoint name, but allow for nothing to appear there
 491                  $epmatch = $endpoint[1] . '(/(.*))?/?$';
 492                  //this will be appended on to the rest of the query for each dir
 493                  $epquery = '&' . $endpoint[1] . '=';
 494                  $ep_query_append[$epmatch] = array ( $endpoint[0], $epquery );
 495              }
 496          }
 497  
 498          //get everything up to the first rewrite tag
 499          $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
 500          //build an array of the tags (note that said array ends up being in $tokens[0])
 501          preg_match_all('/%.+?%/', $permalink_structure, $tokens);
 502  
 503          $num_tokens = count($tokens[0]);
 504  
 505          $index = $this->index; //probably 'index.php'
 506          $feedindex = $index;
 507          $trackbackindex = $index;
 508          //build a list from the rewritecode and queryreplace arrays, that will look something like
 509          //tagname=$matches[i] where i is the current $i
 510          for ($i = 0; $i < $num_tokens; ++$i) {
 511              if (0 < $i) {
 512                  $queries[$i] = $queries[$i - 1] . '&';
 513              }
 514  
 515              $query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1);
 516              $queries[$i] .= $query_token;
 517          }
 518  
 519          //get the structure, minus any cruft (stuff that isn't tags) at the front
 520          $structure = $permalink_structure;
 521          if ($front != '/') {
 522              $structure = str_replace($front, '', $structure);
 523          }
 524          //create a list of dirs to walk over, making rewrite rules for each level
 525          //so for example, a $structure of /%year%/%month%/%postname% would create
 526          //rewrite rules for /%year%/, /%year%/%month%/ and /%year%/%month%/%postname%
 527          $structure = trim($structure, '/');
 528          if ($walk_dirs) {
 529              $dirs = explode('/', $structure);
 530          } else {
 531              $dirs[] = $structure;
 532          }
 533          $num_dirs = count($dirs);
 534  
 535          //strip slashes from the front of $front
 536          $front = preg_replace('|^/+|', '', $front);
 537  
 538          //the main workhorse loop
 539          $post_rewrite = array();
 540          $struct = $front;
 541          for ($j = 0; $j < $num_dirs; ++$j) {
 542              //get the struct for this dir, and trim slashes off the front
 543              $struct .= $dirs[$j] . '/'; //accumulate. see comment near explode('/', $structure) above
 544              $struct = ltrim($struct, '/');
 545              //replace tags with regexes
 546              $match = str_replace($this->rewritecode, $this->rewritereplace, $struct);
 547              //make a list of tags, and store how many there are in $num_toks
 548              $num_toks = preg_match_all('/%.+?%/', $struct, $toks);
 549              //get the 'tagname=$matches[i]'
 550              $query = $queries[$num_toks - 1];
 551  
 552              //set up $ep_mask_specific which is used to match more specific URL types
 553              switch ($dirs[$j]) {
 554                  case '%year%': $ep_mask_specific = EP_YEAR; break;
 555                  case '%monthnum%': $ep_mask_specific = EP_MONTH; break;
 556                  case '%day%': $ep_mask_specific = EP_DAY; break;
 557              }
 558  
 559              //create query for /page/xx
 560              $pagematch = $match . $pageregex;
 561              $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1);
 562  
 563              //create query for /feed/(feed|atom|rss|rss2|rdf)
 564              $feedmatch = $match . $feedregex;
 565              $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
 566  
 567              //create query for /(feed|atom|rss|rss2|rdf) (see comment near creation of $feedregex)
 568              $feedmatch2 = $match . $feedregex2;
 569              $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
 570  
 571              //if asked to, turn the feed queries into comment feed ones
 572              if ($forcomments) {
 573                  $feedquery .= '&withcomments=1';
 574                  $feedquery2 .= '&withcomments=1';
 575              }
 576  
 577              //start creating the array of rewrites for this dir
 578              $rewrite = array();
 579              if ($feed) //...adding on /feed/ regexes => queries
 580                  $rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2);
 581              if ($paged) //...and /page/xx ones
 582                  $rewrite = array_merge($rewrite, array($pagematch => $pagequery));
 583  
 584              //if we've got some tags in this dir
 585              if ($num_toks) {
 586                  $post = false;
 587                  $page = false;
 588  
 589                  //check to see if this dir is permalink-level: i.e. the structure specifies an
 590                  //individual post. Do this by checking it contains at least one of 1) post name,
 591                  //2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and
 592                  //minute all present). Set these flags now as we need them for the endpoints.
 593                  if (strstr($struct, '%postname%') || strstr($struct, '%post_id%')
 594                          || strstr($struct, '%pagename%')
 595                          || (strstr($struct, '%year%') &&  strstr($struct, '%monthnum%') && strstr($struct, '%day%') && strstr($struct, '%hour%') && strstr($struct, '%minute') && strstr($struct, '%second%'))) {
 596                      $post = true;
 597                      if  ( strstr($struct, '%pagename%') )
 598                          $page = true;
 599                  }
 600  
 601                  //do endpoints
 602                  if ($endpoints) {
 603                      foreach ($ep_query_append as $regex => $ep) {
 604                          //add the endpoints on if the mask fits
 605                          if ($ep[0] & $ep_mask || $ep[0] & $ep_mask_specific) {
 606                              $rewrite[$match . $regex] = $index . '?' . $query . $ep[1] . $this->preg_index($num_toks + 2);
 607                          }
 608                      }
 609                  }
 610  
 611                  //if we're creating rules for a permalink, do all the endpoints like attachments etc
 612                  if ($post) {
 613                      $post = true;
 614                      //create query and regex for trackback
 615                      $trackbackmatch = $match . $trackbackregex;
 616                      $trackbackquery = $trackbackindex . '?' . $query . '&tb=1';
 617                      //trim slashes from the end of the regex for this dir
 618                      $match = rtrim($match, '/');
 619                      //get rid of brackets
 620                      $submatchbase = str_replace(array('(',')'),'',$match);
 621  
 622                      //add a rule for at attachments, which take the form of <permalink>/some-text
 623                      $sub1 = $submatchbase . '/([^/]+)/';
 624                      $sub1tb = $sub1 . $trackbackregex; //add trackback regex <permalink>/trackback/...
 625                      $sub1feed = $sub1 . $feedregex; //and <permalink>/feed/(atom|...)
 626                      $sub1feed2 = $sub1 . $feedregex2; //and <permalink>/(feed|atom...)
 627                      //add an ? as we don't have to match that last slash, and finally a $ so we
 628                      //match to the end of the URL
 629  
 630                      //add another rule to match attachments in the explicit form:
 631                      //<permalink>/attachment/some-text
 632                      $sub2 = $submatchbase . '/attachment/([^/]+)/';
 633                      $sub2tb = $sub2 . $trackbackregex; //and add trackbacks <permalink>/attachment/trackback
 634                      $sub2feed = $sub2 . $feedregex;    //feeds, <permalink>/attachment/feed/(atom|...)
 635                      $sub2feed2 = $sub2 . $feedregex2;  //and feeds again on to this <permalink>/attachment/(feed|atom...)
 636  
 637                      //create queries for these extra tag-ons we've just dealt with
 638                      $subquery = $index . '?attachment=' . $this->preg_index(1);
 639                      $subtbquery = $subquery . '&tb=1';
 640                      $subfeedquery = $subquery . '&feed=' . $this->preg_index(2);
 641  
 642                      //do endpoints for attachments
 643                      if ($endpoint) { foreach ($ep_query_append as $regex => $ep) {
 644                          if ($ep[0] & EP_ATTACHMENT) {
 645                              $rewrite[$sub1 . $regex] = $subquery . '?' . $ep[1] . $this->preg_index(2);
 646                              $rewrite[$sub2 . $regex] = $subquery . '?' . $ep[1] . $this->preg_index(2);
 647                          }
 648                      } }
 649  
 650                      //now we've finished with endpoints, finish off the $sub1 and $sub2 matches
 651                      $sub1 .= '?$';
 652                      $sub2 .= '?$';
 653  
 654                      //allow URLs like <permalink>/2 for <permalink>/page/2
 655                      $match = $match . '(/[0-9]+)?/?$';
 656                      $query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1);
 657                  } else { //not matching a permalink so this is a lot simpler
 658                      //close the match and finalise the query
 659                      $match .= '?$';
 660                      $query = $index . '?' . $query;
 661                  }
 662  
 663                  //create the final array for this dir by joining the $rewrite array (which currently
 664                  //only contains rules/queries for trackback, pages etc) to the main regex/query for
 665                  //this dir
 666                  $rewrite = array_merge($rewrite, array($match => $query));
 667  
 668                  //if we're matching a permalink, add those extras (attachments etc) on
 669                  if ($post) {
 670                      //add trackback
 671                      $rewrite = array_merge(array($trackbackmatch => $trackbackquery), $rewrite);
 672  
 673                      //add regexes/queries for attachments, attachment trackbacks and so on
 674                      if ( ! $page ) //require <permalink>/attachment/stuff form for pages because of confusion with subpages
 675                          $rewrite = array_merge($rewrite, array($sub1 => $subquery, $sub1tb => $subtbquery, $sub1feed => $subfeedquery, $sub1feed2 => $subfeedquery));
 676                      $rewrite = array_merge($rewrite, array($sub2 => $subquery, $sub2tb => $subtbquery, $sub2feed => $subfeedquery, $sub2feed2 => $subfeedquery));
 677                  }
 678              } //if($num_toks)
 679              //add the rules for this dir to the accumulating $post_rewrite
 680              $post_rewrite = array_merge($rewrite, $post_rewrite);
 681          } //foreach ($dir)
 682          return $post_rewrite; //the finished rules. phew!
 683      }
 684  
 685      function generate_rewrite_rule($permalink_structure, $walk_dirs = false) {
 686          return $this->generate_rewrite_rules($permalink_structure, EP_NONE, false, false, false, $walk_dirs);
 687      }
 688  
 689      /* rewrite_rules
 690       * Construct rewrite matches and queries from permalink structure.
 691       * Returns an associate array of matches and queries.
 692       */
 693  	function rewrite_rules() {
 694          $rewrite = array();
 695  
 696          if (empty($this->permalink_structure)) {
 697              return $rewrite;
 698          }
 699  
 700          // robots.txt
 701          $robots_rewrite = array('robots.txt$' => $this->index . '?robots=1');
 702  
 703          //Default Feed rules - These are require to allow for the direct access files to work with permalink structure starting with %category%
 704          $default_feeds = array(    'wp-atom.php$'    =>    $this->index .'?feed=atom',
 705                                  'wp-rdf.php$'    =>    $this->index .'?feed=rdf',
 706                                  'wp-rss.php$'    =>    $this->index .'?feed=rss',
 707                                  'wp-rss2.php$'    =>    $this->index .'?feed=rss2',
 708                                  'wp-feed.php$'    =>    $this->index .'?feed=feed',
 709                                  'wp-commentsrss2.php$'    =>    $this->index . '?feed=rss2&withcomments=1');
 710  
 711          // Post
 712          $post_rewrite = $this->generate_rewrite_rules($this->permalink_structure, EP_PERMALINK);
 713          $post_rewrite = apply_filters('post_rewrite_rules', $post_rewrite);
 714  
 715          // Date
 716          $date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct(), EP_DATE);
 717          $date_rewrite = apply_filters('date_rewrite_rules', $date_rewrite);
 718  
 719          // Root
 720          $root_rewrite = $this->generate_rewrite_rules($this->root . '/', EP_ROOT);
 721          $root_rewrite = apply_filters('root_rewrite_rules', $root_rewrite);
 722  
 723          // Comments
 724          $comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, EP_COMMENTS, true, true, true, false);
 725          $comments_rewrite = apply_filters('comments_rewrite_rules', $comments_rewrite);
 726  
 727          // Search
 728          $search_structure = $this->get_search_permastruct();
 729          $search_rewrite = $this->generate_rewrite_rules($search_structure, EP_SEARCH);
 730          $search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite);
 731  
 732          // Categories
 733          $category_rewrite = $this->generate_rewrite_rules($this->get_category_permastruct(), EP_CATEGORIES);
 734          $category_rewrite = apply_filters('category_rewrite_rules', $category_rewrite);
 735  
 736          // Authors
 737          $author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct(), EP_AUTHORS);
 738          $author_rewrite = apply_filters('author_rewrite_rules', $author_rewrite);
 739  
 740          // Pages
 741          $page_rewrite = $this->page_rewrite_rules();
 742          $page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite);
 743  
 744          // Put them together.
 745          $this->rules = array_merge($robots_rewrite, $default_feeds, $page_rewrite, $root_rewrite, $comments_rewrite, $search_rewrite, $category_rewrite, $author_rewrite, $date_rewrite, $post_rewrite, $this->extra_rules);
 746  
 747          do_action_ref_array('generate_rewrite_rules', array(&$this));
 748          $this->rules = apply_filters('rewrite_rules_array', $this->rules);
 749  
 750          return $this->rules;
 751      }
 752  
 753  	function wp_rewrite_rules() {
 754          $this->rules = get_option('rewrite_rules');
 755          if ( empty($this->rules) ) {
 756              $this->matches = 'matches';
 757              $this->rewrite_rules();
 758              update_option('rewrite_rules', $this->rules);
 759          }
 760  
 761          return $this->rules;
 762      }
 763  
 764  	function mod_rewrite_rules() {
 765          if ( ! $this->using_permalinks()) {
 766              return '';
 767          }
 768  
 769          $site_root = parse_url(get_option('siteurl'));
 770          $site_root = trailingslashit($site_root['path']);
 771  
 772          $home_root = parse_url(get_option('home'));
 773          $home_root = trailingslashit($home_root['path']);
 774  
 775          $rules = "<IfModule mod_rewrite.c>\n";
 776          $rules .= "RewriteEngine On\n";
 777          $rules .= "RewriteBase $home_root\n";
 778  
 779          //add in the rules that don't redirect to WP's index.php (and thus shouldn't be handled by WP at all)
 780          foreach ($this->non_wp_rules as $match => $query) {
 781              // Apache 1.3 does not support the reluctant (non-greedy) modifier.
 782              $match = str_replace('.+?', '.+', $match);
 783  
 784              // If the match is unanchored and greedy, prepend rewrite conditions
 785              // to avoid infinite redirects and eclipsing of real files.
 786              if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
 787                  //nada.
 788              }
 789  
 790              $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
 791          }
 792  
 793          if ($this->use_verbose_rules) {
 794              $this->matches = '';
 795              $rewrite = $this->rewrite_rules();
 796              $num_rules = count($rewrite);
 797              $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" .
 798                  "RewriteCond %{REQUEST_FILENAME} -d\n" .
 799                  "RewriteRule ^.*$ - [S=$num_rules]\n";
 800  
 801              foreach ($rewrite as $match => $query) {
 802                  // Apache 1.3 does not support the reluctant (non-greedy) modifier.
 803                  $match = str_replace('.+?', '.+', $match);
 804  
 805                  // If the match is unanchored and greedy, prepend rewrite conditions
 806                  // to avoid infinite redirects and eclipsing of real files.
 807                  if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
 808                      //nada.
 809                  }
 810  
 811                  if (strstr($query, $this->index)) {
 812                      $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
 813                  } else {
 814                      $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n";
 815                  }
 816              }
 817          } else {
 818              $rules .= "RewriteCond %{REQUEST_FILENAME} !-f\n" .
 819                  "RewriteCond %{REQUEST_FILENAME} !-d\n" .
 820                  "RewriteRule . {$home_root}{$this->index} [L]\n";
 821          }
 822  
 823          $rules .= "</IfModule>\n";
 824  
 825          $rules = apply_filters('mod_rewrite_rules', $rules);
 826          $rules = apply_filters('rewrite_rules', $rules);  // Deprecated
 827  
 828          return $rules;
 829      }
 830  
 831      //Add a straight rewrite rule
 832  	function add_rule($regex, $redirect) {
 833          //get everything up to the first ?
 834          $index = (strpos($redirect, '?') == false ? strlen($redirect) : strpos($redirect, '?'));
 835          $front = substr($redirect, 0, $index);
 836          if ($front != $this->index) { //it doesn't redirect to WP's index.php
 837              $this->add_external_rule($regex, $redirect);
 838          } else {
 839              $this->extra_rules[$regex] = $redirect;
 840          }
 841      }
 842  
 843      //add a rule that doesn't redirect to index.php
 844  	function add_external_rule($regex, $redirect) {
 845          $this->non_wp_rules[$regex] = $redirect;
 846      }
 847  
 848      //add an endpoint, like /trackback/, to be inserted after certain URL types (specified in $places)
 849  	function add_endpoint($name, $places) {
 850          global $wp;
 851          $this->endpoints[] = array ( $places, $name );
 852          $wp->add_query_var($name);
 853      }
 854  
 855  	function flush_rules() {
 856          generate_page_uri_index();
 857          delete_option('rewrite_rules');
 858          $this->wp_rewrite_rules();
 859          if ( function_exists('save_mod_rewrite_rules') )
 860              save_mod_rewrite_rules();
 861      }
 862  
 863  	function init() {
 864          $this->extra_rules = $this->non_wp_rules = $this->endpoints = array();
 865          $this->permalink_structure = get_option('permalink_structure');
 866          $this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%'));
 867          $this->root = '';
 868          if ($this->using_index_permalinks()) {
 869              $this->root = $this->index . '/';
 870          }
 871          $this->category_base = get_option('category_base');
 872          unset($this->category_structure);
 873          unset($this->author_structure);
 874          unset($this->date_structure);
 875          unset($this->page_structure);
 876          unset($this->search_structure);
 877          unset($this->feed_structure);
 878          unset($this->comment_feed_structure);
 879      }
 880  
 881  	function set_permalink_structure($permalink_structure) {
 882          if ($permalink_structure != $this->permalink_structure) {
 883              update_option('permalink_structure', $permalink_structure);
 884              $this->init();
 885          }
 886      }
 887  
 888  	function set_category_base($category_base) {
 889          if ($category_base != $this->category_base) {
 890              update_option('category_base', $category_base);
 891              $this->init();
 892          }
 893      }
 894  
 895  	function WP_Rewrite() {
 896          $this->init();
 897      }
 898  }
 899  
 900  ?>


Généré le : Fri Mar 30 19:41:27 2007 par Balluche grâce à PHPXref 0.7