| 1 |
<?php |
<?php |
| 2 |
/* $Id: search404.module,v 1.13.2.10 2008/12/10 11:48:51 zyxware Exp $ */ |
/* $Id: search404.module,v 1.13.2.11 2008/12/19 09:11:25 zyxware Exp $ */ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* Implementation of hook_menu(). |
* Implementation of hook_menu(). |
| 38 |
if (!empty($extensions) && preg_match("/\.($extensions)$/", $_REQUEST['destination'])) { |
if (!empty($extensions) && preg_match("/\.($extensions)$/", $_REQUEST['destination'])) { |
| 39 |
return false; |
return false; |
| 40 |
} |
} |
| 41 |
$keys = $_REQUEST['destination']; |
$keys = $_REQUEST['destination'] ? $_REQUEST['destination'] : $_REQUEST['q']; |
| 42 |
$misc_var = variable_get('search404_regex', ''); |
$regex_filter = variable_get('search404_regex', ''); |
| 43 |
if (!empty($misc_var)) { |
$keys_array[] = $keys; |
| 44 |
$keys = preg_grep($misc_var, $keys); |
if (!empty($regex_filter)) { |
| 45 |
$keys = $keys[0]; |
$keys = preg_replace("/".$regex_filter."/", '', $keys); |
| 46 |
} |
} |
| 47 |
// Ingore certain extensions from query |
// Ingore certain extensions from query |
| 48 |
$extensions = preg_split('/\s+/', variable_get('search404_ignore_extensions', 'htm html php')); |
$extensions = preg_split('/\s+/', variable_get('search404_ignore_extensions', 'htm html php')); |
| 64 |
} |
} |
| 65 |
|
|
| 66 |
/** |
/** |
| 67 |
* Detect search from search engine (BETA) |
* Detect search from search engine |
| 68 |
* WARNING: This function can potentially return dangerous |
* WARNING: This function can potentially return dangerous |
| 69 |
* potential SQL inject/XSS |
* potential SQL inject/XSS |
| 70 |
* data. Return must be sanatized before use. |
* data. Return must be sanatized before use. |
| 74 |
'altavista' => 'q', |
'altavista' => 'q', |
| 75 |
'aol' => 'query', |
'aol' => 'query', |
| 76 |
'google' => 'q', |
'google' => 'q', |
| 77 |
'live' => 'q', |
'bing' => 'q', |
| 78 |
'lycos' => 'query', |
'lycos' => 'query', |
|
'msn' => 'q', |
|
| 79 |
'yahoo' => 'p', |
'yahoo' => 'p', |
| 80 |
); |
); |
| 81 |
$parsed_url = parse_url($_SERVER['HTTP_REFERER']); |
$parsed_url = parse_url($_SERVER['HTTP_REFERER']); |
| 105 |
|
|
| 106 |
if (module_exists('search') && user_access('search content')) { |
if (module_exists('search') && user_access('search content')) { |
| 107 |
$keys = ""; |
$keys = ""; |
| 108 |
|
// if apachesolr_search is installed use apachesolr_search instead default node search |
| 109 |
|
$type_search = (module_exists('apachesolr_search')) ? 'apachesolr_search' : 'node'; |
| 110 |
if (variable_get('search404_use_search_engine', false)) { |
if (variable_get('search404_use_search_engine', false)) { |
| 111 |
$keys = search404_search_engine_query(); |
$keys = search404_search_engine_query(); |
| 112 |
} |
} |
| 115 |
} |
} |
| 116 |
if ($keys) { |
if ($keys) { |
| 117 |
// TODO: watchdog? |
// TODO: watchdog? |
| 118 |
$results = module_invoke('node', 'search', 'search', $keys); |
$results = module_invoke($type_search, 'search', 'search', $keys); |
| 119 |
if (isset($results) && is_array($results) && count($results) == 1 && variable_get('search404_jump', false)) { |
if (isset($results) && is_array($results) && count($results) == 1 && variable_get('search404_jump', false)) { |
| 120 |
// First, check to see if there is exactly 1 result |
// First, check to see if there is exactly 1 result |
| 121 |
drupal_set_message(t('The page you requested does not exist. A search for %keys resulted in this page.', array('%keys' => check_plain($keys))), 'status'); |
drupal_set_message(t('The page you requested does not exist. A search for %keys resulted in this page.', array('%keys' => check_plain($keys))), 'status'); |
| 137 |
$oldgetq = $_GET['q']; |
$oldgetq = $_GET['q']; |
| 138 |
$olddestination = $_REQUEST['destination']; |
$olddestination = $_REQUEST['destination']; |
| 139 |
unset($_REQUEST['destination']); |
unset($_REQUEST['destination']); |
| 140 |
$_GET['q'] = "search/node/$keys"; |
$_GET['q'] = "search/$type_search/$keys"; |
| 141 |
$results = theme('search_page', $results, 'node'); |
$results = theme('search_page', $results, 'node'); |
| 142 |
$_GET['q'] = $oldgetq; |
$_GET['q'] = $oldgetq; |
| 143 |
$_REQUEST['destination'] = $olddestination; |
$_REQUEST['destination'] = $olddestination; |
| 194 |
$form['advanced'] = array( |
$form['advanced'] = array( |
| 195 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 196 |
'#title' => t('Advanced settings'), |
'#title' => t('Advanced settings'), |
| 197 |
'#description' => t("WARNING. Some of these settings can mess up stuff, don't touch unless you know what you are doing."), |
//'#description' => t("WARNING. Some of these settings can mess up stuff, don't touch unless you know what you are doing."), |
| 198 |
'#collapsible' => TRUE, |
'#collapsible' => TRUE, |
| 199 |
'#collapsed' => TRUE, |
'#collapsed' => TRUE, |
| 200 |
); |
); |
| 205 |
); |
); |
| 206 |
$form['advanced']['search404_use_search_engine'] = array( |
$form['advanced']['search404_use_search_engine'] = array( |
| 207 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 208 |
'#title' => t('Use auto-detection of keywords from search engine referer. BETA! Not for production sites, use at your own risk.'), |
'#title' => t('Use auto-detection of keywords from search engine referer.'), |
| 209 |
|
'#description' => t('This feature will conduct a search based on the query string got from a search engine if the URL of the search result points to a 404 page in the current website. Currently supported search engines: Google, Yahoo, Altavista, Lycos, Bing and AOL'), |
| 210 |
'#default_value' => variable_get('search404_use_search_engine', false), |
'#default_value' => variable_get('search404_use_search_engine', false), |
| 211 |
); |
); |
| 212 |
$form['advanced']['search404_ignore'] = array( |
$form['advanced']['search404_ignore'] = array( |
| 218 |
$form['advanced']['search404_ignore_extensions'] = array( |
$form['advanced']['search404_ignore_extensions'] = array( |
| 219 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 220 |
'#title' => t('Extensions to ignore'), |
'#title' => t('Extensions to ignore'), |
| 221 |
'#description' => t('These extensions will be ignored from query. Separate extensions with a space, e.g.: "htm html php". Do not include leading dot.'), |
'#description' => t('These extensions will be ignored from the search query, e.g.: http://www.example.com/invalid/page.php will only search for "invalid page". Separate extensions with a space, e.g.: "htm html php". Do not include leading dot.'), |
| 222 |
'#default_value' => variable_get('search404_ignore_extensions', 'htm html php'), |
'#default_value' => variable_get('search404_ignore_extensions', 'htm html php'), |
| 223 |
); |
); |
| 224 |
$form['advanced']['search404_ignore_query'] = array( |
$form['advanced']['search404_ignore_query'] = array( |
| 229 |
); |
); |
| 230 |
$form['advanced']['search404_regex'] = array( |
$form['advanced']['search404_regex'] = array( |
| 231 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 232 |
'#title' => t('PCRE REGEX'), |
'#title' => t('PCRE filter'), |
| 233 |
'#description' => t('This regex will applied to all queries. It uses the code:<p>%code</p>Look directly at the source code to understand underlying syntax. See also <a href="http://php.net/pcre">PCRE pages in the PHP Manual</a>.', array('%code' => "\$keys = preg_grep(variable_get('search404_regex'), \$keys);\n\$keys = \$keys[0];", '%function' => 'search404_get_keys()')), |
'#description' => t('This regular expression will be applied to filter all queries. The parts of the path that match the expression will be EXCLUDED from the search. You do NOT have to enclose the regex in forward slashes when defining the PCRE. e.g.: use "[foo]bar" instead of "/[foo]bar/". On how to use a PCRE Regex please refer <a href="http://php.net/pcre">PCRE pages in the PHP Manual</a>.'), |
| 234 |
'#default_value' => variable_get('search404_regex',''), |
'#default_value' => variable_get('search404_regex',''), |
| 235 |
); |
); |
| 236 |
|
|