Пример #1
0
 function low_format($str = '', $format = 'html')
 {
     // Encode/decode chars specifically for EE params
     $code = array('"' => '"', ''' => "'", '{' => '{', '}' => '}');
     switch ($format) {
         case 'url':
             $str = urlencode($str);
             break;
         case 'html':
             $str = htmlspecialchars($str);
             $str = low_format($str, 'ee-encode');
             break;
         case 'clean':
             $str = low_clean_string($str);
             break;
         case 'ee-encode':
             $str = str_replace(array_values($code), array_keys($code), $str);
             break;
         case 'ee-decode':
             $str = str_replace(array_keys($code), array_values($code), $str);
             break;
     }
     return $str;
 }
Пример #2
0
 /**
  * Populate internal Terms array
  */
 private function _set_terms()
 {
     // --------------------------------------
     // Local cache of stop words
     // --------------------------------------
     static $stop_words;
     // --------------------------------------
     // Initiate fulltext mode to TRUE
     // --------------------------------------
     $this->_fulltext = TRUE;
     // --------------------------------------
     // If mode is auto and loose ends is left/both, no fulltext
     // --------------------------------------
     if (($mode = $this->params->get('search_mode')) != 'auto' && in_array($this->params->get('loose_ends'), array('left', 'both'))) {
         $this->_fulltext = FALSE;
     }
     // --------------------------------------
     // Reset terms
     // --------------------------------------
     $this->_terms = $this->_keywords = array();
     // --------------------------------------
     // Get raw keywords from parameters
     // --------------------------------------
     $words = (string) $this->params->get('keywords');
     // --------------------------------------
     // Replace pipes with spaces as if it were separate words
     // --------------------------------------
     $words = str_replace('|', ' ', $words);
     // --------------------------------------
     // Exact match?
     // --------------------------------------
     if ($mode == 'exact') {
         $words = '"' . str_replace('"', '', $words) . '"';
     }
     // --------------------------------------
     // Use tokens to get to each term,
     // including quoted ones
     // --------------------------------------
     for ($word = strtok($words, ' '); $word !== FALSE; $word = strtok(' ')) {
         // Create new Term Object
         $term = new Low_search_term();
         // Is term part of an OR group
         $group = FALSE;
         if ($mode == 'auto') {
             // Check if search term is part of an OR group
             if ($word == 'OR') {
                 // Grab the next one or bail
                 if (($word = strtok(' ')) === FALSE) {
                     break;
                 }
                 if ($total = count($this->_terms)) {
                     // Set the previous token to a term_group, if it's not already
                     $prev = $this->_terms[$total - 1];
                     if (!$prev instanceof Low_search_term_group) {
                         $prev = new Low_search_term_group(array($prev));
                         $this->_terms[$total - 1] = $prev;
                     }
                     $group = TRUE;
                 }
             }
             // Negation?
             if ($term->exclude = substr($word, 0, 1) == '-') {
                 $word = ltrim($word, '-');
             }
             // Loose ends on the left
             if ($term->loose_left = substr($word, 0, 1) == '*') {
                 $word = ltrim($word, '*');
                 $this->_fulltext = FALSE;
             }
             // Loose ends on the right
             if ($term->loose_right = substr($word, -1) == '*') {
                 $word = rtrim($word, '*');
             }
         }
         // Check for quoted terms
         if (substr($word, 0, 1) == '"') {
             // 1-word quote or not?
             $word = substr($word, -1) == '"' ? substr($word, 1, -1) : substr($word, 1) . ' ' . strtok('"');
             // Set exact marker to TRUE
             $term->exact = TRUE;
         }
         // Non-exact match, strip ignore words from term
         $ignore = $term->exact ? array() : ee()->low_search_settings->get('ignore_words');
         // Get the cleaned search term
         $term->term = low_clean_string($word, $ignore);
         // Skip the rest if nothing's left
         if (!$term->term) {
             continue;
         }
         // Add term object to previous group or general terms
         $group ? $prev->terms[] = $term : ($this->_terms[] = $term);
         // Add whole thing to the keywords array if we're not excluding
         if (!$term->exclude) {
             $this->_keywords[] = $term->term;
         }
         // Check if keywords are fulltext-worthy
         if ($this->_fulltext) {
             foreach (explode(' ', $term->term) as $word) {
                 // Get word length
                 $length = MB_ENABLED ? mb_strlen($word) : strlen($word);
                 if ($length < ee()->low_search_settings->get('min_word_length')) {
                     $this->_fulltext = FALSE;
                     continue;
                 }
                 // Make sure stop words are an array
                 if (!is_array($stop_words)) {
                     // Clean stopwords and convert into array
                     $stop_words = (array) array_unique(preg_split('/\\s+/', low_clean_string(ee()->low_search_settings->get('stop_words'))));
                 }
                 if (in_array($word, $stop_words)) {
                     $this->_fulltext = FALSE;
                     continue;
                 }
             }
         }
     }
     // Final ckeck to see if we're dealing with fulltext or not:
     // Empty keywords is sign that there's only negated keywords,
     // which cannot use fulltext...
     if ($this->_fulltext && empty($this->_keywords)) {
         $this->_fulltext = FALSE;
     }
 }