Example #1
0
function tep_parse_search_string($search_str = '', &$objects)
{
    $search_str = trim(tep_strtolower($search_str));
    // Break up $search_str on whitespace; quoted string will be reconstructed later
    $pieces = array();
    $temp_pieces = explode(' ', $search_str);
    reset($temp_pieces);
    while (list(, $temp_piece) = each($temp_pieces)) {
        $temp_piece = trim($temp_pieces);
        if (tep_not_null($temp_piece)) {
            $pieces[] = $temp_piece;
        }
    }
    $objects = array();
    $tmpstring = '';
    $flag = '';
    for ($k = 0; $k < count($pieces); $k++) {
        while (substr($pieces[$k], 0, 1) == '(') {
            $objects[] = '(';
            if (strlen($pieces[$k]) > 1) {
                $pieces[$k] = substr($pieces[$k], 1);
            } else {
                $pieces[$k] = '';
            }
        }
        $post_objects = array();
        while (substr($pieces[$k], -1) == ')') {
            $post_objects[] = ')';
            if (strlen($pieces[$k]) > 1) {
                $pieces[$k] = substr($pieces[$k], 0, -1);
            } else {
                $pieces[$k] = '';
            }
        }
        // Check individual words
        if (substr($pieces[$k], -1) != '"' && substr($pieces[$k], 0, 1) != '"') {
            $objects[] = trim($pieces[$k]);
            for ($j = 0; $j < count($post_objects); $j++) {
                $objects[] = $post_objects[$j];
            }
        } else {
            /* This means that the $piece is either the beginning or the end of a string.
               So, we'll slurp up the $pieces and stick them together until we get to the
               end of the string or run out of pieces.
            */
            // Add this word to the $tmpstring, starting the $tmpstring
            $tmpstring = trim(str_replace('"', ' ', $pieces[$k]));
            // Check for one possible exception to the rule. That there is a single quoted word.
            if (substr($pieces[$k], -1) == '"') {
                // Turn the flag off for future iterations
                $flag = 'off';
                $objects[] = trim($pieces[$k]);
                for ($j = 0; $j < count($post_objects); $j++) {
                    $objects[] = $post_objects[$j];
                }
                unset($tmpstring);
                // Stop looking for the end of the string and move onto the next word.
                continue;
            }
            // Otherwise, turn on the flag to indicate no quotes have been found attached to this word in the string.
            $flag = 'on';
            // Move on to the next word
            $k++;
            // Keep reading until the end of the string as long as the $flag is on
            while ($flag == 'on' && $k < count($pieces)) {
                while (substr($pieces[$k], -1) == ')') {
                    $post_objects[] = ')';
                    if (strlen($pieces[$k]) > 1) {
                        $pieces[$k] = substr($pieces[$k], 0, -1);
                    } else {
                        $pieces[$k] = '';
                    }
                }
                // If the word doesn't end in double quotes, append it to the $tmpstring.
                if (substr($pieces[$k], -1) != '"') {
                    // Tack this word onto the current string entity
                    $tmpstring .= ' ' . $pieces[$k];
                    // Move on to the next word
                    $k++;
                    continue;
                } else {
                    /* If the $piece ends in double quotes, strip the double quotes, tack the
                       $piece onto the tail of the string, push the $tmpstring onto the $haves,
                       kill the $tmpstring, turn the $flag "off", and return.
                    */
                    $tmpstring .= ' ' . trim(str_replace('"', ' ', $pieces[$k]));
                    // Push the $tmpstring onto the array of stuff to search for
                    $objects[] = trim($tmpstring);
                    for ($j = 0; $j < count($post_objects); $j++) {
                        $objects[] = $post_objects[$j];
                    }
                    unset($tmpstring);
                    // Turn off the flag to exit the loop
                    $flag = 'off';
                }
            }
        }
    }
    // add default logical operators if needed
    $temp = array();
    for ($i = 0; $i < count($objects) - 1; $i++) {
        $temp[] = $objects[$i];
        if ($objects[$i] != 'and' && $objects[$i] != 'or' && $objects[$i] != '(' && $objects[$i + 1] != 'and' && $objects[$i + 1] != 'or' && $objects[$i + 1] != ')') {
            $temp[] = 'and';
        }
    }
    $temp[] = $objects[$i];
    $objects = $temp;
    $keyword_count = 0;
    $operator_count = 0;
    $balance = 0;
    for ($i = 0; $i < count($objects); $i++) {
        if ($objects[$i] == '(') {
            $balance--;
        }
        if ($objects[$i] == ')') {
            $balance++;
        }
        if ($objects[$i] == 'and' || $objects[$i] == 'or') {
            $operator_count++;
        } elseif ($objects[$i] && $objects[$i] != '(' && $objects[$i] != ')') {
            $keyword_count++;
        }
    }
    if ($operator_count < $keyword_count && $balance == 0) {
        return true;
    } else {
        return false;
    }
}
            $search_params_array['products_price_to'] = str_replace(',', '.', $pto);
        }
    }
}
// только поиск по словам
if (tep_not_null($HTTP_GET_VARS['keywords']) || tep_not_null($HTTP_GET_VARS['detailed'])) {
    $keywords = '';
    if (tep_not_null($HTTP_GET_VARS['detailed'])) {
        $keywords .= ' ' . urldecode($HTTP_GET_VARS['detailed']);
    }
    if (tep_not_null($HTTP_GET_VARS['keywords'])) {
        $keywords .= ' ' . urldecode($HTTP_GET_VARS['keywords']);
    }
    $keywords = htmlspecialchars(stripslashes(trim(strip_tags($keywords))), ENT_QUOTES);
    $keywords_to_search = array();
    $keywords = strip_tags(tep_strtolower(html_entity_decode($keywords)));
    $keywords = str_replace(array('+', '"', '/', '.', ',', '(', ')', '{', '}', '[', ']', '!', '?', '*', ';', '\'', 'Ч'), ' ', $keywords);
    $keywords = preg_replace('/(\\D)\\-(\\D)/i', '$1 $2', $keywords);
    $keywords = preg_replace('/\\s+/', ' ', tep_db_input($keywords));
    $keywords_array = array_unique(explode(' ', $keywords));
    reset($keywords_array);
    while (list(, $keyword_to_search) = each($keywords_array)) {
        if (mb_strlen(trim($keyword_to_search), 'CP1251') > 1) {
            $keywords_to_search[] = $keyword_to_search;
        }
    }
    $k = 0;
    reset($keywords_to_search);
    while (list(, $keyword_to_search) = each($keywords_to_search)) {
        $keyword_query = tep_db_query("select search_keywords_id from " . TABLE_SEARCH_KEYWORDS . " where search_keywords_word = '" . $keyword_to_search . "'");
        $keyword_row = tep_db_fetch_array($keyword_query);
}
if (tep_not_null($HTTP_GET_VARS['manufacturers_id'])) {
    $manufacturer_query = tep_db_query("select manufacturers_name from " . TABLE_MANUFACTURERS_INFO . " where manufacturers_id = '" . (int) $HTTP_GET_VARS['manufacturers_id'] . "' and languages_id = '" . (int) $languages_id . "'");
    $manufacturer_array = tep_db_fetch_array($manufacturer_query);
    $request_string .= (tep_not_null($request_string) ? '; ' : '') . tep_strtolower(ENTRY_MANUFACTURER) . ' <strong>' . $manufacturer_array['manufacturers_name'] . '</strong>';
}
$temp_string = '';
if (tep_not_null($pfrom)) {
    $temp_string .= ' ' . TEXT_FROM . ' <strong>' . $currencies->format($pfrom) . '</strong>';
}
if (tep_not_null($pto)) {
    $temp_string .= ' ' . TEXT_TO . ' <strong>' . $currencies->format($pto) . '</strong>';
}
if (tep_not_null($temp_string)) {
    $request_string .= (tep_not_null($request_string) ? '; ' : '') . tep_strtolower(ENTRY_PRICE) . $temp_string;
}
$temp_string = '';
if (tep_not_null($year_from)) {
    $temp_string .= ' ' . TEXT_FROM . ' <strong>' . $year_from . '</strong>';
}
if (tep_not_null($year_to)) {
    $temp_string .= ' ' . TEXT_TO . ' <strong>' . $year_to . '</strong>';
}
if (tep_not_null($temp_string)) {
    $request_string .= (tep_not_null($request_string) ? '; ' : '') . tep_strtolower(ENTRY_YEAR) . $temp_string;
}
$advanced_search_page_query = tep_db_query("select pages_name from " . TABLE_PAGES . " where pages_filename = '" . tep_db_input(FILENAME_ADVANCED_SEARCH) . "' and language_id = '" . (int) $languages_id . "'");
$advanced_search_page = tep_db_fetch_array($advanced_search_page_query);
$breadcrumb->add($advanced_search_page['pages_name'], tep_href_link(FILENAME_ADVANCED_SEARCH, '', 'NONSSL'));
$breadcrumb->add($page['pages_name'], tep_href_link(FILENAME_ADVANCED_SEARCH_RESULT, tep_get_all_get_params(), 'NONSSL', true, false));
require DIR_WS_INCLUDES . 'application_bottom.php';