Example #1
0
 /** ----------------------------------------
     /**  Show search results
     /** ----------------------------------------*/
 function search_results()
 {
     global $IN, $DB, $TMPL, $LANG, $FNS, $OUT, $LOC, $PREFS, $REGX;
     /** ----------------------------------------
         /**  Fetch the search language file
         /** ----------------------------------------*/
     $LANG->fetch_language_file('search');
     /** ----------------------------------------
         /**  Check search ID number
         /** ----------------------------------------*/
     // If the QSTR variable is less than 32 characters long we
     // don't have a valid search ID number
     if (strlen($IN->QSTR) < 32) {
         return $OUT->show_user_error('off', array($LANG->line('search_no_result')), $LANG->line('search_result_heading'));
     }
     /** ----------------------------------------
         /**  Clear old search results
         /** ----------------------------------------*/
     $expire = time() - $this->cache_expire * 3600;
     $DB->query("DELETE FROM exp_search WHERE site_id = '" . $DB->escape_str($PREFS->ini('site_id')) . "' AND search_date < '{$expire}'");
     /** ----------------------------------------
         /**  Fetch ID number and page number
         /** ----------------------------------------*/
     // We cleverly disguise the page number in the ID hash string
     $cur_page = 0;
     if (strlen($IN->QSTR) == 32) {
         $search_id = $IN->QSTR;
     } else {
         $search_id = substr($IN->QSTR, 0, 32);
         $cur_page = substr($IN->QSTR, 32);
     }
     /** ----------------------------------------
         /**  Fetch the cached search query
         /** ----------------------------------------*/
     $query = $DB->query("SELECT * FROM exp_search WHERE search_id = '" . $DB->escape_str($search_id) . "'");
     if ($query->num_rows == 0 or $query->row['total_results'] == 0) {
         return $OUT->show_user_error('off', array($LANG->line('search_no_result')), $LANG->line('search_result_heading'));
     }
     $fields = $query->row['custom_fields'] == '' ? array() : unserialize(stripslashes($query->row['custom_fields']));
     $sql = unserialize(stripslashes($query->row['query']));
     $sql = str_replace('MDBMPREFIX', 'exp_', $sql);
     $per_page = $query->row['per_page'];
     $res_page = $query->row['result_page'];
     /** ----------------------------------------
         /**  Run the search query
         /** ----------------------------------------*/
     $query = $DB->query(preg_replace("/SELECT(.*?)\\s+FROM\\s+/is", 'SELECT COUNT(*) AS count FROM ', $sql));
     if ($query->row['count'] == 0) {
         return $OUT->show_user_error('off', array($LANG->line('search_no_result')), $LANG->line('search_result_heading'));
     }
     /** ----------------------------------------
         /**  Calculate total number of pages
         /** ----------------------------------------*/
     $current_page = $cur_page / $per_page + 1;
     $total_pages = intval($query->row['count'] / $per_page);
     if ($query->row['count'] % $per_page) {
         $total_pages++;
     }
     $page_count = $LANG->line('page') . ' ' . $current_page . ' ' . $LANG->line('of') . ' ' . $total_pages;
     /** -----------------------------
        	/**  Do we need pagination?
        	/** -----------------------------*/
     // If so, we'll add the LIMIT clause to the SQL statement and run the query again
     $pager = '';
     if ($query->row['count'] > $per_page) {
         if (!class_exists('Paginate')) {
             require PATH_CORE . 'core.paginate' . EXT;
         }
         $PGR = new Paginate();
         $PGR->path = $FNS->create_url($res_page . '/' . $search_id, 0, 0);
         $PGR->total_count = $query->row['count'];
         $PGR->per_page = $per_page;
         $PGR->cur_page = $cur_page;
         $pager = $PGR->show_links();
         $sql .= " LIMIT " . $cur_page . ", " . $per_page;
     }
     $query = $DB->query($sql);
     $output = '';
     if (!class_exists('Weblog')) {
         require PATH_MOD . '/weblog/mod.weblog' . EXT;
     }
     unset($TMPL->var_single['auto_path']);
     unset($TMPL->var_single['excerpt']);
     unset($TMPL->var_single['id_auto_path']);
     unset($TMPL->var_single['full_text']);
     unset($TMPL->var_single['switch']);
     foreach ($TMPL->var_single as $key => $value) {
         if (substr($key, 0, strlen('member_path')) == 'member_path') {
             unset($TMPL->var_single[$key]);
         }
     }
     $weblog = new Weblog();
     // This allows the weblog {absolute_count} variable to work
     $weblog->p_page = $per_page * $current_page - $per_page;
     $weblog->fetch_custom_weblog_fields();
     $weblog->fetch_custom_member_fields();
     $weblog->query = $DB->query($sql);
     if ($weblog->query->num_rows == 0) {
         return $TMPL->no_results();
     }
     if (!class_exists('Typography')) {
         require PATH_CORE . 'core.typography' . EXT;
     }
     $weblog->TYPE = new Typography();
     $weblog->TYPE->convert_curly = FALSE;
     $weblog->TYPE->encode_email = FALSE;
     $weblog->fetch_categories();
     $weblog->parse_weblog_entries();
     $tagdata = $TMPL->tagdata;
     // Does the tag contain "related entries" that we need to parse out?
     if (count($TMPL->related_data) > 0 and count($weblog->related_entries) > 0) {
         $weblog->parse_related_entries();
     }
     if (count($TMPL->reverse_related_data) > 0 and count($weblog->reverse_related_entries) > 0) {
         $weblog->parse_reverse_related_entries();
     }
     $output = $weblog->return_data;
     $TMPL->tagdata = $tagdata;
     /** -----------------------------
     		/**  Fetch member path variable
     		/** -----------------------------*/
     // We do it here in case it's used in multiple places.
     $m_paths = array();
     if (preg_match_all("/" . LD . "member_path(\\s*=.*?)" . RD . "/s", $TMPL->tagdata, $matches)) {
         for ($j = 0; $j < count($matches['0']); $j++) {
             $m_paths[] = array($matches['0'][$j], $FNS->extract_path($matches['1'][$j]));
         }
     }
     /** -----------------------------
     		/**  Fetch switch param
     		/** -----------------------------*/
     $switch1 = '';
     $switch2 = '';
     if ($switch = $TMPL->fetch_param('switch')) {
         if (strpos($switch, '|') !== FALSE) {
             $x = explode("|", $switch);
             $switch1 = $x['0'];
             $switch2 = $x['1'];
         } else {
             $switch1 = $switch;
         }
     }
     /** -----------------------------
     		/**  Result Loop - Legacy!
     		/** -----------------------------*/
     $i = 0;
     foreach ($query->result as $row) {
         if (isset($row['field_id_' . $row['search_excerpt']]) and $row['field_id_' . $row['search_excerpt']]) {
             $format = !isset($row['field_ft_' . $row['search_excerpt']]) ? 'xhtml' : $row['field_ft_' . $row['search_excerpt']];
             $full_text = $weblog->TYPE->parse_type(strip_tags($row['field_id_' . $row['search_excerpt']]), array('text_format' => $format, 'html_format' => 'safe', 'auto_links' => 'y', 'allow_img_url' => 'n'));
             $excerpt = strip_tags($full_text);
             $excerpt = trim(preg_replace("/(\r\n)|(\r)|(\n)/", " ", $excerpt));
             $excerpt = $FNS->word_limiter($excerpt, 50);
         } else {
             $excerpt = '';
             $full_text = '';
         }
         // Parse permalink path
         $url = $row['search_results_url'] != '' ? $row['search_results_url'] : $row['blog_url'];
         $path = $FNS->remove_double_slashes($REGX->prep_query_string($url) . '/' . $row['url_title'] . '/');
         $idpath = $FNS->remove_double_slashes($REGX->prep_query_string($url) . '/' . $row['entry_id'] . '/');
         $switch = $i++ % 2 ? $switch1 : $switch2;
         $output = preg_replace("/" . LD . 'switch' . RD . "/", $switch, $output, sizeof(explode(LD . 'switch' . RD, $TMPL->tagdata)) - 1);
         $output = preg_replace("/" . LD . 'auto_path' . RD . "/", $path, $output, sizeof(explode(LD . 'auto_path' . RD, $TMPL->tagdata)) - 1);
         $output = preg_replace("/" . LD . 'id_auto_path' . RD . "/", $idpath, $output, sizeof(explode(LD . 'id_auto_path' . RD, $TMPL->tagdata)) - 1);
         $output = preg_replace("/" . LD . 'excerpt' . RD . "/", preg_quote($excerpt), $output, sizeof(explode(LD . 'excerpt' . RD, $TMPL->tagdata)) - 1);
         $output = preg_replace("/" . LD . 'full_text' . RD . "/", preg_quote($full_text), $output, sizeof(explode(LD . 'full_text' . RD, $TMPL->tagdata)) - 1);
         // Parse member_path
         if (count($m_paths) > 0) {
             foreach ($m_paths as $val) {
                 $output = preg_replace("/" . $val['0'] . "/", $FNS->create_url($val['1'] . '/' . $row['member_id']), $output, 1);
             }
         }
     }
     $TMPL->tagdata = $output;
     /** ----------------------------------------
     		/**  Parse variables
     		/** ----------------------------------------*/
     $swap = array('lang:total_search_results' => $LANG->line('search_total_results'), 'lang:search_engine' => $LANG->line('search_engine'), 'lang:search_results' => $LANG->line('search_results'), 'lang:search' => $LANG->line('search'), 'lang:title' => $LANG->line('search_title'), 'lang:weblog' => $LANG->line('search_weblog'), 'lang:excerpt' => $LANG->line('search_excerpt'), 'lang:author' => $LANG->line('search_author'), 'lang:date' => $LANG->line('search_date'), 'lang:total_comments' => $LANG->line('search_total_comments'), 'lang:recent_comments' => $LANG->line('search_recent_comment_date'), 'lang:keywords' => $LANG->line('search_keywords'));
     $TMPL->template = $FNS->var_swap($TMPL->template, $swap);
     /** ----------------------------------------
     		/**  Add Pagination
     		/** ----------------------------------------*/
     if ($pager == '') {
         $TMPL->template = preg_replace("/" . LD . "if paginate" . RD . ".*?" . LD . "&#47;if" . RD . "/s", '', $TMPL->template);
     } else {
         $TMPL->template = preg_replace("/" . LD . "if paginate" . RD . "(.*?)" . LD . "&#47;if" . RD . "/s", "\\1", $TMPL->template);
     }
     $TMPL->template = str_replace(LD . 'paginate' . RD, $pager, $TMPL->template);
     $TMPL->template = str_replace(LD . 'page_count' . RD, $page_count, $TMPL->template);
     return stripslashes($TMPL->tagdata);
 }