/**
 * Returns a search URL
 *
 * @param mixed $words the search words target
 * @param mixed $dates the dates that limit the search
 * @param mixed $fields the fields on which to search
 * NOTE: $words and $dates are mutually exclusive and $fields applies only to $words searches
 * @param int $page the page number for the URL
 * @param array $object_list the list of objects to search
 * @return string
 * @since 1.1.3
 */
function getSearchURL($words, $dates, $fields, $page, $object_list = NULL)
{
    $urls = array();
    $rewrite = false;
    if (MOD_REWRITE) {
        $rewrite = true;
        if (is_array($object_list)) {
            foreach ($object_list as $obj) {
                if ($obj) {
                    $rewrite = false;
                    break;
                }
            }
        }
    }
    if ($rewrite) {
        $url = SEO_WEBPATH . '/' . _SEARCH_ . '/';
    } else {
        $url = SEO_WEBPATH . "/index.php";
        $urls[] = 'p=search';
    }
    if ($words) {
        if (is_array($words)) {
            foreach ($words as $key => $word) {
                $words[$key] = search_quote($word);
            }
            $words = implode(',', $words);
        }
        $words = SearchEngine::encode($words);
        if ($rewrite) {
            $url .= $words . '/';
        } else {
            $urls[] = 'words=' . $words;
        }
        if (!empty($fields)) {
            if (!is_array($fields)) {
                $fields = explode(',', $fields);
            }
            $temp = $fields;
            if ($rewrite && count($fields) == 1 && array_shift($temp) == 'tags') {
                $url = SEO_WEBPATH . '/' . _TAGS_ . '/' . $words . '/';
            } else {
                $search = new SearchEngine();
                $urls[] = $search->getSearchFieldsText($fields, 'searchfields=');
            }
        }
    } else {
        //	dates
        if (is_array($dates)) {
            $dates = implode(',', $dates);
        }
        if ($rewrite) {
            $url = SEO_WEBPATH . '/' . _ARCHIVE_ . '/' . $dates . '/';
        } else {
            $urls[] = "date={$dates}";
        }
    }
    if ($page > 1) {
        if ($rewrite) {
            $url .= $page;
        } else {
            $urls[] = "page={$page}";
        }
    }
    if (is_array($object_list)) {
        foreach ($object_list as $key => $list) {
            if (!empty($list)) {
                if (is_array($list)) {
                    $list = implode(',', $list);
                }
                $urls[] = 'in' . $key . '=' . urlencode($list);
            }
        }
    }
    if (!empty($urls)) {
        $url .= '?' . implode('&', $urls);
    }
    return $url;
}
/**
 * Creates a "REWRITE" url given the query parameters that represent the link
 *
 * @param type $query
 * @return string
 */
function zpRewriteURL($query)
{
    $redirectURL = '';
    if (isset($query['p'])) {
        sanitize($query);
        switch ($query['p']) {
            case 'news':
                $redirectURL = _NEWS_;
                if (isset($query['category'])) {
                    $obj = newCategory(rtrim($query['category'], '/'), false);
                    if (!$obj->loaded) {
                        return '';
                    }
                    $redirectURL = $obj->getLink();
                    unset($query['category']);
                } else {
                    if (isset($query['date'])) {
                        $redirectURL = _NEWS_ARCHIVE_ . '/' . rtrim($query['date'], '/') . '/';
                        unset($query['date']);
                    }
                }
                if (isset($query['title'])) {
                    $obj = newArticle(rtrim($query['title'], '/'), false);
                    if (!$obj->loaded) {
                        return '';
                    }
                    $redirectURL = $obj->getLink();
                    unset($query['title']);
                }
                break;
            case 'pages':
                if (isset($query['title'])) {
                    $obj = newPage(rtrim($query['title'], '/'), false);
                    if (!$obj->loaded) {
                        return '';
                    }
                    $redirectURL = $obj->getLink();
                    unset($query['title']);
                }
                break;
            case 'search':
                $redirectURL = _SEARCH_;
                if (isset($query['date'])) {
                    $redirectURL = _ARCHIVE_ . '/' . rtrim($query['date'], '/') . '/';
                    unset($query['date']);
                } else {
                    if (isset($query['searchfields']) && $query['searchfields'] == 'tags') {
                        $redirectURL = _TAGS_;
                        unset($query['searchfields']);
                    }
                }
                if (isset($query['words'])) {
                    if (!preg_match('/^[0-9A-F]+\\.[0-9A-F]+$/i', $query['words'])) {
                        $query['words'] = SearchEngine::encode($query['words']);
                    }
                    $redirectURL .= '/' . $query['words'] . '/';
                    unset($query['words']);
                }
                break;
            default:
                $redirectURL = getCustomPageURL(rtrim($query['p'], '/'));
                break;
        }
        unset($query['p']);
        if (isset($query['page'])) {
            $redirectURL = rtrim($redirectURL, '/') . '/' . rtrim($query['page'], '/');
            unset($query['page']);
        }
    } else {
        if (isset($query['album'])) {
            if (isset($query['image'])) {
                $obj = newImage(array('folder' => $query['album'], 'filename' => $query['image']), NULL, true);
                unset($query['image']);
            } else {
                $obj = newAlbum($query['album'], NULL, true);
            }
            if (is_object($obj) && !$obj->exists) {
                return '';
            }
            unset($query['album']);
            $redirectURL = preg_replace('~^' . WEBPATH . '/~', '', $obj->getLink(@$query['page']));
            unset($query['page']);
        } else {
            if (isset($query['page'])) {
                //index page
                $redirectURL = _PAGE_ . '/' . rtrim($query['page'], '/');
                unset($query['page']);
            }
        }
    }
    if ($redirectURL && !empty($query)) {
        $redirectURL .= '?' . http_build_query($query);
    }
    return $redirectURL;
}