home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / WordPress 1.5.1.dmg / wordpress / wp-includes / classes.php < prev    next >
Encoding:
PHP Script  |  2005-04-14  |  36.2 KB  |  1,299 lines

  1. <?php
  2.  
  3. class WP_Query {
  4.     var $query;
  5.     var $query_vars;
  6.     var $queried_object;
  7.     var $queried_object_id;
  8.  
  9.     var $posts;
  10.     var $post_count = 0;
  11.     var $current_post = -1;
  12.     var $post;
  13.  
  14.     var $is_single = false;
  15.     var $is_page = false;
  16.     var $is_archive = false;
  17.     var $is_date = false;
  18.     var $is_year = false;
  19.     var $is_month = false;
  20.     var $is_day = false;
  21.     var $is_time = false;
  22.     var $is_author = false;
  23.     var $is_category = false;
  24.     var $is_search = false;
  25.     var $is_feed = false;
  26.     var $is_trackback = false;
  27.     var $is_home = false;
  28.     var $is_404 = false;
  29.     var $is_comments_popup = false;
  30.     var $is_admin = false;
  31.  
  32.     function init () {
  33.         $this->is_single = false;
  34.         $this->is_page = false;
  35.         $this->is_archive = false;
  36.         $this->is_date = false;
  37.         $this->is_year = false;
  38.         $this->is_month = false;
  39.         $this->is_day = false;
  40.         $this->is_time = false;
  41.         $this->is_author = false;
  42.         $this->is_category = false;
  43.         $this->is_search = false;
  44.         $this->is_feed = false;
  45.         $this->is_trackback = false;
  46.         $this->is_home = false;
  47.         $this->is_404 = false;
  48.         $this->is_paged = false;
  49.         $this->is_admin = false;
  50.  
  51.         unset($this->posts);
  52.         unset($this->query);
  53.         unset($this->query_vars);
  54.         unset($this->queried_object);
  55.         unset($this->queried_object_id);
  56.         $this->post_count = 0;
  57.         $this->current_post = -1;
  58.     }
  59.  
  60.     // Reparse the query vars.
  61.     function parse_query_vars() {
  62.         $this->parse_query('');
  63.     }
  64.  
  65.     // Parse a query string and set query type booleans.
  66.     function parse_query ($query) {
  67.         if ( !empty($query) || !isset($this->query) ) {
  68.             $this->init();
  69.             parse_str($query, $qv);
  70.             $this->query = $query;
  71.             $this->query_vars = $qv;
  72.         }
  73.  
  74.         $qv['m'] =  (int) $qv['m'];
  75.         $qv['p'] =  (int) $qv['p'];
  76.  
  77.         if ('' != $qv['name']) {
  78.             $this->is_single = true;
  79.         } elseif ( $qv['p'] ) {
  80.             $this->is_single = true;
  81.         } elseif (('' != $qv['hour']) && ('' != $qv['minute']) &&('' != $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day'])) {
  82.             // If year, month, day, hour, minute, and second are set, a single 
  83.           // post is being queried.        
  84.             $this->is_single = true;
  85.         } elseif ('' != $qv['static'] || '' != $qv['pagename'] || '' != $qv['page_id']) {
  86.             $this->is_page = true;
  87.             $this->is_single = false;
  88.         } elseif (!empty($qv['s'])) {
  89.             $this->is_search = true;
  90.         } else {
  91.         // Look for archive queries.  Dates, categories, authors.
  92.  
  93.             if ( (int) $qv['second']) {
  94.                 $this->is_time = true;
  95.                 $this->is_date = true;
  96.             }
  97.  
  98.             if ( (int) $qv['minute']) {
  99.                 $this->is_time = true;
  100.                 $this->is_date = true;
  101.             }
  102.  
  103.             if ( (int) $qv['hour']) {
  104.                 $this->is_time = true;
  105.                 $this->is_date = true;
  106.             }
  107.  
  108.             if ( (int) $qv['day']) {
  109.                 if (! $this->is_date) {
  110.                     $this->is_day = true;
  111.                     $this->is_date = true;
  112.                 }
  113.             }
  114.  
  115.             if ( (int)  $qv['monthnum']) {
  116.                 if (! $this->is_date) {
  117.                     $this->is_month = true;
  118.                     $this->is_date = true;
  119.                 }
  120.             }
  121.  
  122.             if ( (int)  $qv['year']) {
  123.                 if (! $this->is_date) {
  124.                     $this->is_year = true;
  125.                     $this->is_date = true;
  126.                 }
  127.             }
  128.  
  129.             if ( (int)  $qv['m']) {
  130.                 $this->is_date = true;
  131.                 if (strlen($qv['m']) > 9) {
  132.                     $this->is_time = true;
  133.                 } else if (strlen($qv['m']) > 7) {
  134.                     $this->is_day = true;
  135.                 } else if (strlen($qv['m']) > 5) {
  136.                     $this->is_month = true;
  137.                 } else {
  138.                     $this->is_year = true;
  139.                 }
  140.             }
  141.  
  142.             if ('' != $qv['w']) {
  143.                 $this->is_date = true;
  144.             }
  145.  
  146.             if (empty($qv['cat']) || ($qv['cat'] == '0')) {
  147.                 $this->is_category = false;
  148.             } else {
  149.                 if (stristr($qv['cat'],'-')) {
  150.                     $this->is_category = false;
  151.                 } else {
  152.                     $this->is_category = true;
  153.                 }
  154.             }
  155.  
  156.             if ('' != $qv['category_name']) {
  157.                 $this->is_category = true;
  158.             }
  159.             
  160.             if ((empty($qv['author'])) || ($qv['author'] == '0')) {
  161.                 $this->is_author = false;
  162.             } else {
  163.                 $this->is_author = true;
  164.             }
  165.  
  166.             if ('' != $qv['author_name']) {
  167.                 $this->is_author = true;
  168.             }
  169.  
  170.             if ( ($this->is_date || $this->is_author || $this->is_category)) {
  171.                 $this->is_archive = true;
  172.             }
  173.         }
  174.  
  175.         if ('' != $qv['feed']) {
  176.             $this->is_feed = true;
  177.         }
  178.  
  179.         if ('' != $qv['tb']) {
  180.             $this->is_trackback = true;
  181.         }
  182.  
  183.         if ('404' == $qv['error']) {
  184.             $this->is_404 = true;
  185.         }
  186.  
  187.         if ('' != $qv['paged']) {
  188.             $this->is_paged = true;
  189.         }
  190.  
  191.         if ('' != $qv['comments_popup']) {
  192.             $this->is_comments_popup = true;
  193.         }
  194.  
  195.         if (strstr($_SERVER['PHP_SELF'], 'wp-admin/')) {
  196.             $this->is_admin = true;
  197.         }
  198.  
  199.         if ( ! ($this->is_archive || $this->is_single || $this->is_page || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup)) {
  200.             $this->is_home = true;
  201.         }
  202.  
  203.         if ( !empty($query) ) {
  204.             do_action('parse_query', array(&$this));
  205.         }
  206.     }
  207.  
  208.     function get($query_var) {
  209.         if (isset($this->query_vars[$query_var])) {
  210.             return $this->query_vars[$query_var];
  211.         }
  212.  
  213.         return '';
  214.     }
  215.  
  216.     function set($query_var, $value) {
  217.         $this->query_vars[$query_var] = $value;
  218.     }
  219.  
  220.     function &get_posts() {
  221.         global $wpdb, $pagenow, $request, $user_ID;
  222.  
  223.         // Shorthand.
  224.         $q = $this->query_vars;    
  225.  
  226.         // First let's clear some variables
  227.         $whichcat = '';
  228.         $whichauthor = '';
  229.         $result = '';
  230.         $where = '';
  231.         $limits = '';
  232.         $distinct = '';
  233.         $join = '';
  234.  
  235.         if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
  236.             $q['posts_per_page'] = get_settings('posts_per_page');
  237.         if ( !isset($q['what_to_show']) )
  238.             $q['what_to_show'] = get_settings('what_to_show');
  239.         if ( isset($q['showposts']) && $q['showposts'] ) {
  240.             $q['showposts'] = (int) $q['showposts'];
  241.             $q['posts_per_page'] = $q['showposts'];
  242.         }
  243.         if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
  244.             $q['posts_per_page'] = $q['posts_per_archive_page'];
  245.         if ( !isset($q['nopaging']) ) {
  246.             if ($q['posts_per_page'] == -1) {
  247.                 $q['nopaging'] = true;
  248.             } else {
  249.                 $q['nopaging'] = false;
  250.             }
  251.         }
  252.         if ( $this->is_feed ) {
  253.             $q['posts_per_page'] = get_settings('posts_per_rss');
  254.             $q['what_to_show'] = 'posts';
  255.         }
  256.  
  257.         if (isset($q['page'])) {
  258.             $q['page'] = trim($q['page'], '/');
  259.             $q['page'] = (int) $q['page'];
  260.         }
  261.     
  262.         $add_hours = intval(get_settings('gmt_offset'));
  263.         $add_minutes = intval(60 * (get_settings('gmt_offset') - $add_hours));
  264.         $wp_posts_post_date_field = "post_date"; // "DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)";
  265.  
  266.         // If a month is specified in the querystring, load that month
  267.         if ( (int) $q['m'] ) {
  268.             $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
  269.             $where .= ' AND YEAR(post_date)=' . substr($q['m'], 0, 4);
  270.             if (strlen($q['m'])>5)
  271.                 $where .= ' AND MONTH(post_date)=' . substr($q['m'], 4, 2);
  272.             if (strlen($q['m'])>7)
  273.                 $where .= ' AND DAYOFMONTH(post_date)=' . substr($q['m'], 6, 2);
  274.             if (strlen($q['m'])>9)
  275.                 $where .= ' AND HOUR(post_date)=' . substr($q['m'], 8, 2);
  276.             if (strlen($q['m'])>11)
  277.                 $where .= ' AND MINUTE(post_date)=' . substr($q['m'], 10, 2);
  278.             if (strlen($q['m'])>13)
  279.                 $where .= ' AND SECOND(post_date)=' . substr($q['m'], 12, 2);
  280.         }
  281.  
  282.         if ( (int) $q['hour'] ) {
  283.             $q['hour'] = '' . intval($q['hour']);
  284.             $where .= " AND HOUR(post_date)='" . $q['hour'] . "'";
  285.         }
  286.  
  287.         if ( (int) $q['minute'] ) {
  288.             $q['minute'] = '' . intval($q['minute']);
  289.             $where .= " AND MINUTE(post_date)='" . $q['minute'] . "'";
  290.         }
  291.  
  292.         if ( (int) $q['second'] ) {
  293.             $q['second'] = '' . intval($q['second']);
  294.             $where .= " AND SECOND(post_date)='" . $q['second'] . "'";
  295.         }
  296.  
  297.         if ( (int) $q['year'] ) {
  298.             $q['year'] = '' . intval($q['year']);
  299.             $where .= " AND YEAR(post_date)='" . $q['year'] . "'";
  300.         }
  301.  
  302.         if ( (int) $q['monthnum'] ) {
  303.             $q['monthnum'] = '' . intval($q['monthnum']);
  304.             $where .= " AND MONTH(post_date)='" . $q['monthnum'] . "'";
  305.         }
  306.  
  307.         if ( (int) $q['day'] ) {
  308.             $q['day'] = '' . intval($q['day']);
  309.             $where .= " AND DAYOFMONTH(post_date)='" . $q['day'] . "'";
  310.         }
  311.  
  312.         if ('' != $q['name']) {
  313.             $q['name'] = sanitize_title($q['name']);
  314.             $where .= " AND post_name = '" . $q['name'] . "'";
  315.         } else if ('' != $q['pagename']) {
  316.             $q['pagename'] = sanitize_title(basename(str_replace('%2F', '/', urlencode($q['pagename']))));
  317.             $q['name'] = $q['pagename'];
  318.             $where .= " AND post_name = '" . $q['pagename'] . "'";
  319.         }
  320.  
  321.  
  322.         if ( (int) $q['w'] ) {
  323.             $q['w'] = ''.intval($q['w']);
  324.             $where .= " AND WEEK(post_date, 1)='" . $q['w'] . "'";
  325.         }
  326.  
  327.         if ( intval($q['comments_popup']) )
  328.             $q['p'] = intval($q['comments_popup']);
  329.  
  330.         // If a post number is specified, load that post
  331.         if (($q['p'] != '') && intval($q['p']) != 0) {
  332.             $q['p'] =  (int) $q['p'];
  333.             $where = ' AND ID = ' . $q['p'];
  334.         }
  335.  
  336.         if (($q['page_id'] != '') && (intval($q['page_id']) != 0)) {
  337.             $q['page_id'] = intval($q['page_id']);
  338.             $q['p'] = $q['page_id'];
  339.             $where = ' AND ID = '.$q['page_id'];
  340.         }
  341.  
  342.         // If a search pattern is specified, load the posts that match
  343.         if (!empty($q['s'])) {
  344.             $q['s'] = addslashes_gpc($q['s']);
  345.             $search = ' AND (';
  346.             $q['s'] = preg_replace('/, +/', ' ', $q['s']);
  347.             $q['s'] = str_replace(',', ' ', $q['s']);
  348.             $q['s'] = str_replace('"', ' ', $q['s']);
  349.             $q['s'] = trim($q['s']);
  350.             if ($q['exact']) {
  351.                 $n = '';
  352.             } else {
  353.                 $n = '%';
  354.             }
  355.             if (!$q['sentence']) {
  356.                 $s_array = explode(' ',$q['s']);
  357.                 $q['search_terms'] = $s_array;
  358.                 $search .= '((post_title LIKE \''.$n.$s_array[0].$n.'\') OR (post_content LIKE \''.$n.$s_array[0].$n.'\'))';
  359.                 for ( $i = 1; $i < count($s_array); $i = $i + 1) {
  360.                     $search .= ' AND ((post_title LIKE \''.$n.$s_array[$i].$n.'\') OR (post_content LIKE \''.$n.$s_array[$i].$n.'\'))';
  361.                 }
  362.                 $search .= ' OR (post_title LIKE \''.$n.$q['s'].$n.'\') OR (post_content LIKE \''.$n.$q['s'].$n.'\')';
  363.                 $search .= ')';
  364.             } else {
  365.                 $search = ' AND ((post_title LIKE \''.$n.$q['s'].$n.'\') OR (post_content LIKE \''.$n.$q['s'].$n.'\'))';
  366.             }
  367.         }
  368.  
  369.         // Category stuff
  370.  
  371.         if ((empty($q['cat'])) || ($q['cat'] == '0') || 
  372.                 // Bypass cat checks if fetching specific posts
  373.                 ( $this->is_single || $this->is_page )) {
  374.             $whichcat='';
  375.         } else {
  376.             $q['cat'] = ''.urldecode($q['cat']).'';
  377.             $q['cat'] = addslashes_gpc($q['cat']);
  378.             if (stristr($q['cat'],'-')) {
  379.                 // Note: if we have a negative, we ignore all the positives. It must
  380.                 // always mean 'everything /except/ this one'. We should be able to do
  381.                 // multiple negatives but we don't :-(
  382.                 $eq = '!=';
  383.                 $andor = 'AND';
  384.                 $q['cat'] = explode('-',$q['cat']);
  385.                 $q['cat'] = intval($q['cat'][1]);
  386.             } else {
  387.                 $eq = '=';
  388.                 $andor = 'OR';
  389.             }
  390.             $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) ";
  391.             $cat_array = preg_split('/[,\s]+/', $q['cat']);
  392.             $whichcat .= ' AND (category_id '.$eq.' '.intval($cat_array[0]);
  393.             $whichcat .= get_category_children($cat_array[0], ' '.$andor.' category_id '.$eq.' ');
  394.             for ($i = 1; $i < (count($cat_array)); $i = $i + 1) {
  395.                 $whichcat .= ' '.$andor.' category_id '.$eq.' '.intval($cat_array[$i]);
  396.                 $whichcat .= get_category_children($cat_array[$i], ' '.$andor.' category_id '.$eq.' ');
  397.             }
  398.             $whichcat .= ')';
  399.             if ($eq == '!=') {
  400.                 $q['cat'] = '-'.$q['cat']; // Put back the knowledge that we are excluding a category.
  401.             }
  402.         }
  403.  
  404.         // Category stuff for nice URIs
  405.  
  406.         if ('' != $q['category_name']) {
  407.             if (stristr($q['category_name'],'/')) {
  408.                 $q['category_name'] = explode('/',$q['category_name']);
  409.                 if ($q['category_name'][count($q['category_name'])-1]) {
  410.                     $q['category_name'] = $q['category_name'][count($q['category_name'])-1]; // no trailing slash
  411.                 } else {
  412.                     $q['category_name'] = $q['category_name'][count($q['category_name'])-2]; // there was a trailling slash
  413.                 }
  414.             }
  415.             $q['category_name'] = sanitize_title($q['category_name']);
  416.             $tables = ", $wpdb->post2cat, $wpdb->categories";
  417.             $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) ";
  418.             $whichcat = " AND (category_nicename = '" . $q['category_name'] . "'";
  419.             $q['cat'] = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '" . $q['category_name'] . "'");
  420.             $whichcat .= get_category_children($q['cat'], " OR category_id = ");
  421.             $whichcat .= ")";
  422.         }
  423.  
  424.         // Author/user stuff
  425.  
  426.         if ((empty($q['author'])) || ($q['author'] == '0')) {
  427.             $whichauthor='';
  428.         } else {
  429.             $q['author'] = ''.urldecode($q['author']).'';
  430.             $q['author'] = addslashes_gpc($q['author']);
  431.             if (stristr($q['author'], '-')) {
  432.                 $eq = '!=';
  433.                 $andor = 'AND';
  434.                 $q['author'] = explode('-', $q['author']);
  435.                 $q['author'] = ''.intval($q['author'][1]);
  436.             } else {
  437.                 $eq = '=';
  438.                 $andor = 'OR';
  439.             }
  440.             $author_array = preg_split('/[,\s]+/', $q['author']);
  441.             $whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]);
  442.             for ($i = 1; $i < (count($author_array)); $i = $i + 1) {
  443.                 $whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]);
  444.             }
  445.             $whichauthor .= ')';
  446.         }
  447.  
  448.         // Author stuff for nice URIs
  449.  
  450.         if ('' != $q['author_name']) {
  451.             if (stristr($q['author_name'],'/')) {
  452.                 $q['author_name'] = explode('/',$q['author_name']);
  453.                 if ($q['author_name'][count($q['author_name'])-1]) {
  454.                     $q['author_name'] = $q['author_name'][count($q['author_name'])-1];#no trailing slash
  455.                 } else {
  456.                     $q['author_name'] = $q['author_name'][count($q['author_name'])-2];#there was a trailling slash
  457.                 }
  458.             }
  459.             $q['author_name'] = sanitize_title($q['author_name']);
  460.             $q['author'] = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_nicename='".$q['author_name']."'");
  461.             $whichauthor .= ' AND (post_author = '.intval($q['author']).')';
  462.         }
  463.  
  464.         $where .= $search.$whichcat.$whichauthor;
  465.  
  466.         if ((empty($q['order'])) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC'))) {
  467.             $q['order']='DESC';
  468.         }
  469.  
  470.         // Order by
  471.         if (empty($q['orderby'])) {
  472.             $q['orderby']='date '.$q['order'];
  473.         } else {
  474.             // Used to filter values
  475.             $allowed_keys = array('author','date','category','title');
  476.             $q['orderby'] = urldecode($q['orderby']);
  477.             $q['orderby'] = addslashes_gpc($q['orderby']);
  478.             $orderby_array = explode(' ',$q['orderby']);
  479.             if (!in_array($orderby_array[0],$allowed_keys)) {
  480.                 $orderby_array[0] = 'date';
  481.             }
  482.             $q['orderby'] = $orderby_array[0].' '.$q['order'];
  483.             if (count($orderby_array)>1) {
  484.                 for ($i = 1; $i < (count($orderby_array)); $i = $i + 1) {
  485.                     // Only allow certain values for safety
  486.                     if (in_array($orderby_array[$i],$allowed_keys)) {
  487.                         $q['orderby'] .= ',post_'.$orderby_array[$i].' '.$q['order'];
  488.                     }
  489.                 }
  490.             }
  491.         }
  492.  
  493.         $now = gmdate('Y-m-d H:i:59');
  494.  
  495.         if ($pagenow != 'post.php' && $pagenow != 'edit.php') {
  496.             $where .= " AND post_date_gmt <= '$now'";
  497.             $distinct = 'DISTINCT';
  498.         }
  499.  
  500.         if ($this->is_page) {
  501.             $where .= ' AND (post_status = "static")';
  502.         } elseif ($this->is_single) {
  503.             $where .= ' AND (post_status != "static")';
  504.         } else {
  505.             $where .= ' AND (post_status = "publish"';
  506.  
  507.             if (isset($user_ID) && ('' != intval($user_ID)))
  508.                 $where .= " OR post_author = $user_ID AND post_status != 'draft' AND post_status != 'static')";
  509.             else
  510.                 $where .= ')';                
  511.         }
  512.  
  513.         // Apply filters on where and join prior to paging so that any
  514.         // manipulations to them are reflected in the paging by day queries.
  515.         $where = apply_filters('posts_where', $where);
  516.         $join = apply_filters('posts_join', $join);
  517.  
  518.         // Paging
  519.         if (empty($q['nopaging']) && ! $this->is_single) {
  520.             $page = $q['paged'];
  521.             if (empty($page)) {
  522.                 $page = 1;
  523.             }
  524.  
  525.             if (($q['what_to_show'] == 'posts')) {
  526.                 $pgstrt = '';
  527.                 $pgstrt = (intval($page) -1) * $q['posts_per_page'] . ', ';
  528.                 $limits = 'LIMIT '.$pgstrt.$q['posts_per_page'];
  529.             } elseif ($q['what_to_show'] == 'days') {
  530.                 $startrow = $q['posts_per_page'] * (intval($page)-1);
  531.                 $start_date = $wpdb->get_var("SELECT max(post_date) FROM $wpdb->posts $join WHERE (1=1) $where GROUP BY year(post_date), month(post_date), dayofmonth(post_date) ORDER BY post_date DESC LIMIT $startrow,1");
  532.                 $endrow = $startrow + $q['posts_per_page'] - 1;
  533.                 $end_date = $wpdb->get_var("SELECT min(post_date) FROM $wpdb->posts $join WHERE (1=1) $where GROUP BY year(post_date), month(post_date), dayofmonth(post_date) ORDER BY post_date DESC LIMIT $endrow,1");
  534.  
  535.                 if ($page > 1) {
  536.                     $where .= " AND post_date >= '$end_date' AND post_date <= '$start_date'";
  537.                 } else {
  538.                     $where .= " AND post_date >= '$end_date'";
  539.                 }
  540.             }
  541.         }
  542.  
  543.         // Apply post-paging filters on where and join.  Only plugins that
  544.         // manipulate paging queries should use these hooks.
  545.         $where = apply_filters('posts_where_paged', $where);
  546.         $where .= " GROUP BY $wpdb->posts.ID";
  547.         $join = apply_filters('posts_join_paged', $join);
  548.         $orderby = "post_" . $q['orderby'];
  549.         $orderby = apply_filters('posts_orderby', $orderby); 
  550.         $request = " SELECT $distinct * FROM $wpdb->posts $join WHERE 1=1".$where." ORDER BY " . $orderby . " $limits";
  551.  
  552.         $this->posts = $wpdb->get_results($request);
  553.  
  554.         // Check post status to determine if post should be displayed.
  555.         if ($this->is_single) {
  556.             if ('publish' != $this->posts[0]->post_status) {
  557.                 if ( ! (isset($user_ID) && ('' != intval($user_ID))) ) {
  558.                     // User must be logged in to view unpublished posts.
  559.                     $this->posts = array();
  560.                 } else {
  561.                     if ('draft' == $this->posts[0]->post_status) {
  562.                         // User must have edit permissions on the draft to preview.
  563.                         if (! user_can_edit_post($user_ID, $this->posts[0]->ID))
  564.                             $this->posts = array();
  565.                     } elseif ('private' == $this->posts[0]->post_status) {
  566.                         if ($this->posts[0]->post_author != $user_ID)
  567.                             $this->posts = array();
  568.                     }
  569.                 }
  570.             }
  571.         }
  572.  
  573.         $this->posts = apply_filters('the_posts', $this->posts);
  574.         $this->post_count = count($this->posts);
  575.         if ($this->post_count > 0) {
  576.             $this->post = $this->posts[0];
  577.         }
  578.  
  579.         update_post_caches($this->posts);
  580.         
  581.         // Save any changes made to the query vars.
  582.         $this->query_vars = $q;
  583.         return $this->posts;
  584.     }
  585.  
  586.     function next_post() {
  587.         
  588.         $this->current_post++;
  589.  
  590.         $this->post = $this->posts[$this->current_post];
  591.         return $this->post;
  592.     }
  593.  
  594.     function the_post() {
  595.         global $post;
  596.         $post = $this->next_post();
  597.         setup_postdata($post);
  598.     }
  599.  
  600.     function have_posts() {
  601.         if ($this->current_post + 1 < $this->post_count) {
  602.             return true;
  603.         }
  604.  
  605.         return false;
  606.     }
  607.  
  608.     function rewind_posts() {
  609.         $this->current_post = -1;
  610.         if ($this->post_count > 0) {
  611.             $this->post = $this->posts[0];
  612.         }
  613.     }
  614.     
  615.     function &query($query) {
  616.         $this->parse_query($query);
  617.         return $this->get_posts();
  618.     }
  619.  
  620.     function get_queried_object() {
  621.         if (isset($this->queried_object)) {
  622.             return $this->queried_object;
  623.         }
  624.  
  625.         $this->queried_object = NULL;
  626.         $this->queried_object_id = 0;
  627.  
  628.         if ($this->is_category) {
  629.             $category = &get_category($this->get('cat'));
  630.             $this->queried_object = &$category;
  631.             $this->queried_object_id = $this->get('cat');
  632.         } else if ($this->is_single) {
  633.             $this->queried_object = $this->post;
  634.             $this->queried_object_id = $this->post->ID;
  635.         } else if ($this->is_page) {
  636.             $this->queried_object = $this->post;
  637.             $this->queried_object_id = $this->post->ID;
  638.         } else if ($this->is_author) {
  639.             global $cache_userdata;
  640.             if (isset($cache_userdata[$this->get('author')])) {
  641.                 $this->queried_object = $cache_userdata[$this->get('author')];
  642.                 $this->queried_object_id = $this->get('author');
  643.             }
  644.         }
  645.  
  646.         return $this->queried_object;
  647.     }
  648.  
  649.     function get_queried_object_id() {
  650.         $this->get_queried_object();
  651.  
  652.         if (isset($this->queried_object_id)) {
  653.             return $this->queried_object_id;
  654.         }
  655.  
  656.         return 0;
  657.     }
  658.  
  659.     function WP_Query ($query = '') {
  660.         if (! empty($query)) {
  661.             $this->query($query);
  662.         }
  663.     }
  664. }
  665.  
  666. // Make a global instance.
  667. if (! isset($wp_query)) {
  668.     $wp_query = new WP_Query();
  669. }
  670.  
  671. class retrospam_mgr {
  672.     var $spam_words;
  673.     var $comments_list;
  674.     var $found_comments;
  675.  
  676.     function retrospam_mgr() {
  677.         global $wpdb;
  678.  
  679.         $list = explode("\n", get_settings('moderation_keys') );
  680.         $list = array_unique( $list );
  681.         $this->spam_words = $list;
  682.  
  683.         $this->comment_list = $wpdb->get_results("SELECT comment_ID AS ID, comment_content AS text, comment_approved AS approved, comment_author_url AS url, comment_author_ip AS ip, comment_author_email AS email FROM $wpdb->comments ORDER BY comment_ID ASC");
  684.     }    // End of class constructor
  685.  
  686.     function move_spam( $id_list ) {
  687.         global $wpdb;
  688.         $cnt = 0;
  689.         $id_list = explode( ',', $id_list );
  690.  
  691.         foreach ( $id_list as $comment ) {
  692.             if ( $wpdb->query("update $wpdb->comments set comment_approved = '0' where comment_ID = '$comment'") ) {
  693.                 $cnt++;
  694.             }
  695.         }
  696.         echo "<div class='updated'><p>$cnt comment";
  697.         if ($cnt != 1 ) echo "s";
  698.         echo " moved to the moderation queue.</p></div>\n";
  699.     }    // End function move_spam
  700.  
  701.     function find_spam() {
  702.         $in_queue = 0;
  703.  
  704.         foreach( $this->comment_list as $comment ) {
  705.             if( $comment->approved == 1 ) {
  706.                 foreach( $this->spam_words as $word ) {
  707.                     if ( empty( $word ) )
  708.                         continue;
  709.                     $fulltext = strtolower($comment->email.' '.$comment->url.' '.$comment->ip.' '.$comment->text);
  710.                     if( strpos( $fulltext, strtolower(trim($word)) ) != FALSE ) {
  711.                         $this->found_comments[] = $comment->ID;
  712.                         break;
  713.                     }
  714.                 }
  715.             } else {
  716.                 $in_queue++;
  717.             }
  718.         }
  719.         return array( 'found' => $this->found_comments, 'in_queue' => $in_queue );
  720.     }    // End function find_spam
  721.  
  722.     function display_edit_form( $counters ) {
  723.         $numfound = count($counters[found]);
  724.         $numqueue = $counters[in_queue];
  725.  
  726.         $body = '<p>' . sprintf(__('Suspected spam comments: <strong>%s</strong>'), $numfound) . '</p>';
  727.  
  728.         if ( count($counters[found]) > 0 ) {
  729.             $id_list = implode( ',', $counters[found] );
  730.             $body .= '<p><a href="options-discussion.php?action=retrospam&move=true&ids='.$id_list.'">'. __('Move suspect comments to moderation queue »') . '</a></p>';
  731.  
  732.         }
  733.         $head = '<div class="wrap"><h2>' . __('Check Comments Results:') . '</h2>';
  734.  
  735.         $foot .= '<p><a href="options-discussion.php">' . __('« Return to Discussion Options page.') . '</a></p></div>';
  736.         
  737.         return $head . $body . $foot;
  738.     }     // End function display_edit_form
  739.  
  740. }
  741.  
  742. class WP_Rewrite {
  743.     var $permalink_structure;
  744.     var $category_base;
  745.     var $category_structure;
  746.     var $author_base = 'author';
  747.     var $author_structure;
  748.     var $date_structure;
  749.     var $page_structure;
  750.     var $search_base = 'search';
  751.     var $search_structure;
  752.     var $comments_base = 'comments';
  753.     var $feed_base = 'feed';
  754.     var $comments_feed_structure;
  755.     var $feed_structure;
  756.     var $front;
  757.     var $root = '';
  758.     var $index = 'index.php';
  759.     var $matches = '';
  760.     var $rules;
  761.     var $rewritecode = 
  762.         array(
  763.                     '%year%',
  764.                     '%monthnum%',
  765.                     '%day%',
  766.                     '%hour%',
  767.                     '%minute%',
  768.                     '%second%',
  769.                     '%postname%',
  770.                     '%post_id%',
  771.                     '%category%',
  772.                     '%author%',
  773.                     '%pagename%',
  774.                     '%search%'
  775.                     );
  776.  
  777.     var $rewritereplace = 
  778.         array(
  779.                     '([0-9]{4})',
  780.                     '([0-9]{1,2})',
  781.                     '([0-9]{1,2})',
  782.                     '([0-9]{1,2})',
  783.                     '([0-9]{1,2})',
  784.                     '([0-9]{1,2})',
  785.                     '([^/]+)',
  786.                     '([0-9]+)',
  787.                     '(.+?)',
  788.                     '([^/]+)',
  789.                     '([^/]+)',
  790.                     '(.+)'
  791.                     );
  792.  
  793.     var $queryreplace = 
  794.         array (
  795.                      'year=',
  796.                      'monthnum=',
  797.                      'day=',
  798.                      'hour=',
  799.                      'minute=',
  800.                      'second=',
  801.                      'name=',
  802.                      'p=',
  803.                      'category_name=',
  804.                      'author_name=',
  805.                      'pagename=',
  806.                      's='
  807.                      );
  808.  
  809.     var $feeds = array ('feed', 'rdf', 'rss', 'rss2', 'atom');
  810.  
  811.     function using_permalinks() {
  812.         if (empty($this->permalink_structure))
  813.             return false;
  814.         else
  815.             return true;
  816.     }                    
  817.  
  818.     function using_index_permalinks() {
  819.     if (empty($this->permalink_structure)) {
  820.             return false;
  821.     }
  822.  
  823.     // If the index is not in the permalink, we're using mod_rewrite.
  824.     if (preg_match('#^/*' . $this->index . '#', $this->permalink_structure)) {
  825.       return true;
  826.     }
  827.     
  828.     return false;
  829.     }
  830.  
  831.     function using_mod_rewrite_permalinks() {
  832.         if ( $this->using_permalinks() && ! $this->using_index_permalinks())
  833.             return true;
  834.         else
  835.             return false;
  836.     }                    
  837.  
  838.     function preg_index($number) {
  839.     $match_prefix = '$';
  840.     $match_suffix = '';
  841.     
  842.     if (! empty($this->matches)) {
  843.             $match_prefix = '$' . $this->matches . '['; 
  844.             $match_suffix = ']';
  845.     }        
  846.     
  847.     return "$match_prefix$number$match_suffix";        
  848.     }
  849.  
  850.     function page_rewrite_rules() {
  851.         $uris = get_settings('page_uris');
  852.  
  853.         $rewrite_rules = array();
  854.         $page_structure = $this->get_page_permastruct();
  855.         if( is_array( $uris ) )
  856.             {
  857.                 foreach ($uris as $uri => $pagename) {
  858.                     $this->add_rewrite_tag('%pagename%', "($uri)", 'pagename=');
  859.                     $rewrite_rules += $this->generate_rewrite_rules($page_structure);
  860.                 }
  861.             }
  862.  
  863.         return $rewrite_rules;
  864.     }
  865.  
  866.     function get_date_permastruct() {
  867.         if (isset($this->date_structure)) {
  868.             return $this->date_structure;
  869.         }
  870.  
  871.     if (empty($this->permalink_structure)) {
  872.             $this->date_structure = '';
  873.             return false;
  874.         }
  875.         
  876.         // The date permalink must have year, month, and day separated by slashes.
  877.         $endians = array('%year%/%monthnum%/%day%', '%day%/%monthnum%/%year%', '%monthnum%/%day%/%year%');
  878.  
  879.         $this->date_structure = '';
  880.         $date_endian = '';
  881.  
  882.         foreach ($endians as $endian) {
  883.             if (false !== strpos($this->permalink_structure, $endian)) {
  884.                 $date_endian= $endian;
  885.                 break;
  886.             }
  887.         } 
  888.  
  889.         if ( empty($date_endian) )
  890.             $date_endian = '%year%/%monthnum%/%day%';
  891.  
  892.         // Do not allow the date tags and %post_id% to overlap in the permalink
  893.         // structure. If they do, move the date tags to $front/date/.  
  894.         $front = $this->front;
  895.         preg_match_all('/%.+?%/', $this->permalink_structure, $tokens);
  896.         $tok_index = 1;
  897.         foreach ($tokens[0] as $token) {
  898.             if ( ($token == '%post_id%') && ($tok_index <= 3) ) {
  899.                 $front = $front . 'date/';
  900.                 break;
  901.             }
  902.         }
  903.  
  904.         $this->date_structure = $front . $date_endian;
  905.  
  906.         return $this->date_structure;
  907.     }
  908.  
  909.     function get_year_permastruct() {
  910.         $structure = $this->get_date_permastruct($permalink_structure);
  911.  
  912.         if (empty($structure)) {
  913.             return false;
  914.         }
  915.  
  916.         $structure = str_replace('%monthnum%', '', $structure);
  917.         $structure = str_replace('%day%', '', $structure);
  918.  
  919.         $structure = preg_replace('#/+#', '/', $structure);
  920.  
  921.         return $structure;
  922.     }
  923.  
  924.     function get_month_permastruct() {
  925.         $structure = $this->get_date_permastruct($permalink_structure);
  926.  
  927.         if (empty($structure)) {
  928.             return false;
  929.         }
  930.  
  931.         $structure = str_replace('%day%', '', $structure);
  932.  
  933.         $structure = preg_replace('#/+#', '/', $structure);
  934.  
  935.         return $structure;
  936.     }
  937.  
  938.     function get_day_permastruct() {
  939.         return $this->get_date_permastruct($permalink_structure);
  940.     }
  941.  
  942.     function get_category_permastruct() {
  943.         if (isset($this->category_structure)) {
  944.             return $this->category_structure;
  945.         }
  946.  
  947.     if (empty($this->permalink_structure)) {
  948.             $this->category_structure = '';
  949.             return false;
  950.         }
  951.  
  952.         if (empty($this->category_base))
  953.             $this->category_structure = $this->front . 'category/';
  954.         else
  955.             $this->category_structure = $this->category_base . '/';
  956.  
  957.         $this->category_structure .= '%category%';
  958.         
  959.         return $this->category_structure;
  960.     }
  961.  
  962.     function get_author_permastruct() {
  963.         if (isset($this->author_structure)) {
  964.             return $this->author_structure;
  965.         }
  966.  
  967.     if (empty($this->permalink_structure)) {
  968.             $this->author_structure = '';
  969.             return false;
  970.         }
  971.  
  972.         $this->author_structure = $this->front . $this->author_base . '/%author%';
  973.  
  974.         return $this->author_structure;
  975.     }
  976.  
  977.     function get_search_permastruct() {
  978.         if (isset($this->search_structure)) {
  979.             return $this->search_structure;
  980.         }
  981.  
  982.     if (empty($this->permalink_structure)) {
  983.             $this->search_structure = '';
  984.             return false;
  985.         }
  986.  
  987.         $this->search_structure = $this->root . $this->search_base . '/%search%';
  988.  
  989.         return $this->search_structure;
  990.     }
  991.  
  992.     function get_page_permastruct() {
  993.         if (isset($this->page_structure)) {
  994.             return $this->page_structure;
  995.         }
  996.  
  997.     if (empty($this->permalink_structure)) {
  998.             $this->page_structure = '';
  999.             return false;
  1000.         }
  1001.  
  1002.         $this->page_structure = $this->root . '%pagename%';
  1003.  
  1004.         return $this->page_structure;
  1005.     }
  1006.  
  1007.     function get_feed_permastruct() {
  1008.         if (isset($this->feed_structure)) {
  1009.             return $this->feed_structure;
  1010.         }
  1011.  
  1012.     if (empty($this->permalink_structure)) {
  1013.             $this->feed_structure = '';
  1014.             return false;
  1015.         }
  1016.  
  1017.         $this->feed_structure = $this->root . $this->feed_base . '/%feed%';
  1018.  
  1019.         return $this->feed_structure;
  1020.     }
  1021.  
  1022.     function get_comment_feed_permastruct() {
  1023.         if (isset($this->comment_feed_structure)) {
  1024.             return $this->comment_feed_structure;
  1025.         }
  1026.  
  1027.     if (empty($this->permalink_structure)) {
  1028.             $this->comment_feed_structure = '';
  1029.             return false;
  1030.         }
  1031.  
  1032.         $this->comment_feed_structure = $this->root . $this->comments_base . '/' . $this->feed_base . '/%feed%';
  1033.  
  1034.         return $this->comment_feed_structure;
  1035.     }
  1036.  
  1037.     function add_rewrite_tag($tag, $pattern, $query) {
  1038.         // If the tag already exists, replace the existing pattern and query for
  1039.         // that tag, otherwise add the new tag, pattern, and query to the end of
  1040.         // the arrays.
  1041.         $position = array_search($tag, $this->rewritecode);        
  1042.         if (FALSE !== $position && NULL !== $position) {
  1043.             $this->rewritereplace[$position] = $pattern;
  1044.             $this->queryreplace[$position] = $query;            
  1045.         } else {
  1046.             $this->rewritecode[] = $tag;
  1047.             $this->rewritereplace[] = $pattern;
  1048.             $this->queryreplace[] = $query;
  1049.         }
  1050.     }
  1051.  
  1052.     function generate_rewrite_rules($permalink_structure, $page = true, $feed = true, $forcomments = false, $walk_dirs = true) {
  1053.         $feedregex2 = '';
  1054.         foreach ($this->feeds as $feed_name) {
  1055.             $feedregex2 .= $feed_name . '|';
  1056.         }
  1057.         $feedregex2 = '(' . trim($feedregex2, '|') .  ')/?$';
  1058.         $feedregex = $this->feed_base  . '/' . $feedregex2;
  1059.  
  1060.         $trackbackregex = 'trackback/?$';
  1061.         $pageregex = 'page/?([0-9]{1,})/?$';
  1062.         
  1063.         $front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
  1064.         preg_match_all('/%.+?%/', $permalink_structure, $tokens);
  1065.  
  1066.         $num_tokens = count($tokens[0]);
  1067.  
  1068.         $index = $this->index;
  1069.         $feedindex = $index;
  1070.         $trackbackindex = $index;
  1071.         for ($i = 0; $i < $num_tokens; ++$i) {
  1072.             if (0 < $i) {
  1073.                 $queries[$i] = $queries[$i - 1] . '&';
  1074.             }
  1075.              
  1076.             $query_token = str_replace($this->rewritecode, $this->queryreplace, $tokens[0][$i]) . $this->preg_index($i+1);
  1077.             $queries[$i] .= $query_token;
  1078.         }
  1079.  
  1080.         $structure = $permalink_structure;
  1081.         if ($front != '/') {
  1082.             $structure = str_replace($front, '', $structure);
  1083.         }
  1084.         $structure = trim($structure, '/');
  1085.         if ($walk_dirs) {
  1086.             $dirs = explode('/', $structure);
  1087.         } else {
  1088.             $dirs[] = $structure;
  1089.         }
  1090.         $num_dirs = count($dirs);
  1091.  
  1092.         $front = preg_replace('|^/+|', '', $front);
  1093.  
  1094.         $post_rewrite = array();
  1095.         $struct = $front;
  1096.         for ($j = 0; $j < $num_dirs; ++$j) {
  1097.             $struct .= $dirs[$j] . '/';
  1098.             $struct = ltrim($struct, '/');
  1099.             $match = str_replace($this->rewritecode, $this->rewritereplace, $struct);
  1100.             $num_toks = preg_match_all('/%.+?%/', $struct, $toks);
  1101.             $query = $queries[$num_toks - 1];
  1102.  
  1103.             $pagematch = $match . $pageregex;
  1104.             $pagequery = $index . '?' . $query . '&paged=' . $this->preg_index($num_toks + 1);
  1105.  
  1106.             $feedmatch = $match . $feedregex;
  1107.             $feedquery = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
  1108.  
  1109.             $feedmatch2 = $match . $feedregex2;
  1110.             $feedquery2 = $feedindex . '?' . $query . '&feed=' . $this->preg_index($num_toks + 1);
  1111.  
  1112.             if ($forcomments) {
  1113.                 $feedquery .= '&withcomments=1';
  1114.                 $feedquery2 .= '&withcomments=1';
  1115.             }
  1116.  
  1117.             $rewrite = array();
  1118.             if ($feed) 
  1119.                 $rewrite = array($feedmatch => $feedquery, $feedmatch2 => $feedquery2);
  1120.             if ($page)
  1121.                 $rewrite = $rewrite + array($pagematch => $pagequery);
  1122.  
  1123.             if ($num_toks) {
  1124.                 $post = 0;
  1125.                 if (strstr($struct, '%postname%') || strstr($struct, '%post_id%')
  1126.                         || strstr($struct, '%pagename%')
  1127.                         || (strstr($struct, '%year%') &&  strstr($struct, '%monthnum%') && strstr($struct, '%day%') && strstr($struct, '%hour%') && strstr($struct, '%minute') && strstr($struct, '%second%'))) {
  1128.                     $post = 1;
  1129.                     $trackbackmatch = $match . $trackbackregex;
  1130.                     $trackbackquery = $trackbackindex . '?' . $query . '&tb=1';
  1131.                     $match = rtrim($match, '/');
  1132.                     $match = $match . '(/[0-9]+)?/?$';
  1133.                     $query = $index . '?' . $query . '&page=' . $this->preg_index($num_toks + 1);
  1134.                 } else {
  1135.                     $match .= '?$';
  1136.                     $query = $index . '?' . $query;
  1137.                 }
  1138.                         
  1139.                 $rewrite = $rewrite + array($match => $query);
  1140.  
  1141.                 if ($post) {
  1142.                     $rewrite = array($trackbackmatch => $trackbackquery) + $rewrite;
  1143.                 }
  1144.             }
  1145.  
  1146.             $post_rewrite = $rewrite + $post_rewrite;
  1147.         }
  1148.  
  1149.         return $post_rewrite;
  1150.     }
  1151.  
  1152.     function generate_rewrite_rule($permalink_structure, $walk_dirs = false) {
  1153.         return $this->generate_rewrite_rules($permalink_structure, false, false, false, $walk_dirs);
  1154.     }
  1155.  
  1156.     /* rewrite_rules
  1157.      * Construct rewrite matches and queries from permalink structure.
  1158.      * Returns an associate array of matches and queries.
  1159.      */
  1160.     function rewrite_rules() {
  1161.         $rewrite = array();
  1162.  
  1163.         if (empty($this->permalink_structure)) {
  1164.             return $rewrite;
  1165.         }
  1166.  
  1167.         // Post
  1168.         $post_rewrite = $this->generate_rewrite_rules($this->permalink_structure);
  1169.         $post_rewrite = apply_filters('post_rewrite_rules', $post_rewrite);
  1170.  
  1171.         // Date
  1172.         $date_rewrite = $this->generate_rewrite_rules($this->get_date_permastruct());
  1173.         $date_rewrite = apply_filters('date_rewrite_rules', $date_rewrite);
  1174.         
  1175.         // Root
  1176.         $root_rewrite = $this->generate_rewrite_rules($this->root . '/');
  1177.         $root_rewrite = apply_filters('root_rewrite_rules', $root_rewrite);
  1178.  
  1179.         // Comments
  1180.         $comments_rewrite = $this->generate_rewrite_rules($this->root . $this->comments_base, true, true, true);
  1181.         $comments_rewrite = apply_filters('comments_rewrite_rules', $comments_rewrite);
  1182.  
  1183.         // Search
  1184.         $search_structure = $this->get_search_permastruct();
  1185.         $search_rewrite = $this->generate_rewrite_rules($search_structure);
  1186.         $search_rewrite = apply_filters('search_rewrite_rules', $search_rewrite);
  1187.  
  1188.         // Categories
  1189.         $category_rewrite = $this->generate_rewrite_rules($this->get_category_permastruct());
  1190.         $category_rewrite = apply_filters('category_rewrite_rules', $category_rewrite);
  1191.  
  1192.         // Authors
  1193.         $author_rewrite = $this->generate_rewrite_rules($this->get_author_permastruct());
  1194.         $author_rewrite = apply_filters('author_rewrite_rules', $author_rewrite);
  1195.  
  1196.         // Pages
  1197.         $page_rewrite = $this->page_rewrite_rules();
  1198.         $page_rewrite = apply_filters('page_rewrite_rules', $page_rewrite);
  1199.  
  1200.         // Put them together.
  1201.         $this->rules = $page_rewrite + $root_rewrite + $comments_rewrite + $search_rewrite + $category_rewrite + $author_rewrite + $date_rewrite + $post_rewrite;
  1202.  
  1203.         do_action('generate_rewrite_rules', array(&$this));
  1204.         $this->rules = apply_filters('rewrite_rules_array', $this->rules);
  1205.         return $this->rules;
  1206.     }
  1207.  
  1208.     function wp_rewrite_rules() {
  1209.         $this->matches = 'matches';
  1210.         return $this->rewrite_rules();
  1211.     }
  1212.  
  1213.     function mod_rewrite_rules() {
  1214.         if ( ! $this->using_permalinks()) {
  1215.             return '';
  1216.         }
  1217.  
  1218.         $site_root = parse_url(get_settings('siteurl'));
  1219.         $site_root = trailingslashit($site_root['path']);
  1220.  
  1221.         $home_root = parse_url(get_settings('home'));
  1222.         $home_root = trailingslashit($home_root['path']);
  1223.     
  1224.         $rules = "<IfModule mod_rewrite.c>\n";
  1225.         $rules .= "RewriteEngine On\n";
  1226.         $rules .= "RewriteBase $home_root\n";
  1227.         $this->matches = '';
  1228.         $rewrite = $this->rewrite_rules();
  1229.         $num_rules = count($rewrite);
  1230.         $rules .= "RewriteCond %{REQUEST_FILENAME} -f [OR]\n" .
  1231.             "RewriteCond %{REQUEST_FILENAME} -d\n" .
  1232.             "RewriteRule ^.*$ - [S=$num_rules]\n";
  1233.  
  1234.         foreach ($rewrite as $match => $query) {
  1235.             // Apache 1.3 does not support the reluctant (non-greedy) modifier.
  1236.             $match = str_replace('.+?', '.+', $match);
  1237.  
  1238.             // If the match is unanchored and greedy, prepend rewrite conditions
  1239.             // to avoid infinite redirects and eclipsing of real files.
  1240.             if ($match == '(.+)/?$' || $match == '([^/]+)/?$' ) {
  1241.                 //nada.
  1242.             }
  1243.  
  1244.             if (strstr($query, $this->index)) {
  1245.                 $rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
  1246.             } else {
  1247.                 $rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n";
  1248.             }
  1249.         }
  1250.         $rules .= "</IfModule>\n";
  1251.  
  1252.         $rules = apply_filters('mod_rewrite_rules', $rules);
  1253.         $rules = apply_filters('rewrite_rules', $rules);  // Deprecated
  1254.  
  1255.         return $rules;
  1256.     }
  1257.  
  1258.     function init() {
  1259.         $this->permalink_structure = get_settings('permalink_structure');
  1260.         $this->front = substr($this->permalink_structure, 0, strpos($this->permalink_structure, '%'));        
  1261.         $this->root = '';
  1262.         if ($this->using_index_permalinks()) {
  1263.             $this->root = $this->index . '/';
  1264.         }
  1265.         $this->category_base = get_settings('category_base');
  1266.         unset($this->category_structure);
  1267.         unset($this->author_structure);
  1268.         unset($this->date_structure);
  1269.         unset($this->page_structure);
  1270.         unset($this->search_structure);
  1271.         unset($this->feed_structure);
  1272.         unset($this->comment_feed_structure);
  1273.     }
  1274.  
  1275.     function set_permalink_structure($permalink_structure) {
  1276.         if ($permalink_structure != $this->permalink_structure) {
  1277.             update_option('permalink_structure', $permalink_structure);
  1278.             $this->init();
  1279.         }
  1280.     }
  1281.  
  1282.     function set_category_base($category_base) {
  1283.         if ($category_base != $this->category_base) {
  1284.             update_option('category_base', $category_base);
  1285.             $this->init();
  1286.         }
  1287.     }
  1288.  
  1289.     function WP_Rewrite() {
  1290.         $this->init();
  1291.     }
  1292. }
  1293.  
  1294. // Make a global instance.
  1295. if (! isset($wp_rewrite)) {
  1296.     $wp_rewrite = new WP_Rewrite();
  1297. }
  1298.  
  1299. ?>