View source
<?php
define('ACQUIA_SEARCH_ENVIRONMENT_ID', 'acquia_search_server_1');
function acquia_search_get_environment($conf = array()) {
if (!empty($conf['acquia_subscription_id'])) {
$identifier = $conf['acquia_subscription_id'];
}
else {
$identifier = acquia_agent_settings('acquia_identifier');
}
$environment = array(
'url' => variable_get('acquia_search_url', 'http://search.acquia.com/solr/' . $identifier),
'service_class' => 'AcquiaSearchService',
);
return $environment;
}
function acquia_search_enable() {
if (acquia_agent_subscription_is_active()) {
acquia_search_enable_acquia_solr_environment();
acquia_agent_check_subscription();
}
}
function acquia_search_help($path, $arg) {
switch ($path) {
case 'admin/config/search/apachesolr':
$env_id = $arg[5] ? $arg[5] : variable_get('apachesolr_default_environment');
$environment = apachesolr_environment_load($env_id);
if (acquia_search_environment_connected($environment) && acquia_agent_subscription_is_active()) {
$as_link = l(t('Acquia Search'), 'http://www.acquia.com/products-services/acquia-search');
return t("Search is being provided by the !as network service.", array(
'!as' => $as_link,
));
}
break;
}
}
function acquia_search_enable_acquia_solr_environment() {
$environment = apachesolr_environment_load(ACQUIA_SEARCH_ENVIRONMENT_ID);
if (!$environment) {
variable_set('apachesolr_default_environment', ACQUIA_SEARCH_ENVIRONMENT_ID);
$environment['conf'] = array();
}
$environment = array_merge(acquia_search_get_environment(), $environment);
$environment['env_id'] = ACQUIA_SEARCH_ENVIRONMENT_ID;
$environment['name'] = t('Acquia Search');
apachesolr_environment_save($environment);
}
function acquia_search_disable() {
apachesolr_environment_delete(ACQUIA_SEARCH_ENVIRONMENT_ID);
$environments = apachesolr_load_all_environments();
foreach ($environments as $environment) {
if (acquia_search_environment_connected($environment)) {
$environment['url'] = 'http://localhost:8983/solr';
if (variable_get('apachesolr_default_environment', '') == $environment['env_id']) {
variable_del('apachesolr_default_environment');
}
$environment['service_class'] = '';
apachesolr_environment_save($environment);
}
}
}
function acquia_search_menu_alter(&$menu) {
$delete_page = 'admin/config/search/apachesolr-env/%apachesolr_environment/delete';
if (isset($menu[$delete_page])) {
$menu[$delete_page]['access callback'] = 'acquia_search_environment_delete_access';
$menu[$delete_page]['access arguments'] = array(
4,
);
}
}
function acquia_search_cron() {
$version = variable_get('acquia_search_version', '7.x');
$info = system_get_info('module', 'acquia_search');
$new_version = isset($info['version']) ? (string) $info['version'] : (string) $info['core'];
if ($version != $new_version) {
variable_set('acquia_search_version', $new_version);
}
}
function acquia_search_environment_connected($environment) {
return $environment['service_class'] == 'AcquiaSearchService';
}
function acquia_search_environment_delete_access($environment) {
if (strstr($environment['env_id'], ACQUIA_SEARCH_ENVIRONMENT_ID)) {
return FALSE;
}
return user_access('administer search');
}
function acquia_search_form_apachesolr_settings_alter(&$form, $form_state) {
if (acquia_agent_subscription_is_active()) {
foreach ($form['apachesolr_host_settings']['table']['#rows'] as &$row) {
if (isset($row['data']['delete']['data']) && strpos($row['data']['delete']['data'], ACQUIA_SEARCH_ENVIRONMENT_ID . '/delete') !== FALSE) {
$row['data']['delete']['data'] = '';
break;
}
}
$form['advanced']['acquia_search_edismax_default'] = array(
'#type' => 'radios',
'#title' => t('Always allow advanced syntax for Acquia Search'),
'#default_value' => variable_get('acquia_search_edismax_default', 0),
'#options' => array(
0 => t('Disabled'),
1 => t('Enabled'),
),
'#description' => t('If enabled, all Acquia Search keyword searches may use advanced <a href="@url">Lucene syntax</a> such as wildcard searches, fuzzy searches, proximity searches, boolean operators and more via the Extended Dismax parser. If not enabled, this syntax wll only be used when needed to enable wildcard searches.', array(
'@url' => 'http://lucene.apache.org/java/2_9_3/queryparsersyntax.html',
)),
'#weight' => 10,
);
}
}
function acquia_search_form_apachesolr_environment_edit_form_alter(&$form, $form_state) {
$env_id = isset($form['env_id']['#default_value']) ? $form['env_id']['#default_value'] : '';
$environment = $env_id ? apachesolr_environment_load($env_id) : FALSE;
if ($environment && acquia_search_environment_connected($environment)) {
$form['url']['#disabled'] = TRUE;
$form['env_id']['#disabled'] = TRUE;
}
if ($env_id == ACQUIA_SEARCH_ENVIRONMENT_ID) {
$form['name']['#disabled'] = TRUE;
$form['actions']['delete']['#access'] = FALSE;
}
$form['actions']['save']['#validate'][] = 'acquia_search_environment_edit_form_validate';
}
function acquia_search_environment_edit_form_validate($form, &$form_state) {
if ($form_state['values']['env_id'] == ACQUIA_SEARCH_ENVIRONMENT_ID) {
$form_state['values'] = array_merge($form_state['values'], acquia_search_get_environment());
}
}
function acquia_search_acquia_subscription_status($active) {
if ($active) {
acquia_search_enable_acquia_solr_environment();
}
else {
acquia_search_disable();
}
}
function acquia_search_auth_cookie(&$url, $string = '', $derived_key = NULL, $env_id = NULL) {
$uri = parse_url($url);
if (in_array('ssl', stream_get_transports(), TRUE) && !defined('ACQUIA_DEVELOPMENT_NOSSL')) {
$scheme = 'https://';
$port = '';
}
else {
$scheme = 'http://';
$port = isset($uri['port']) && $uri['port'] != 80 ? ':' . $uri['port'] : '';
}
$path = isset($uri['path']) ? $uri['path'] : '/';
$query = isset($uri['query']) ? '?' . $uri['query'] : '';
$url = $scheme . $uri['host'] . $port . $path . $query;
$nonce = base64_encode(drupal_random_bytes(24));
if ($string) {
$auth_header = acquia_search_authenticator($string, $nonce, $derived_key, $env_id);
}
else {
$auth_header = acquia_search_authenticator($path . $query, $nonce, $derived_key, $env_id);
}
return array(
$auth_header,
$nonce,
);
}
function _acquia_search_derived_key($env_id = NULL) {
static $derived_key = array();
if (empty($env_id)) {
$env_id = 0;
}
if (!isset($derived_key[$env_id])) {
$identifier = acquia_agent_settings('acquia_identifier');
$key = acquia_agent_settings('acquia_key');
if ($env_id) {
if ($search_key = apachesolr_environment_variable_get($env_id, 'acquia_search_key')) {
$derived_key[$env_id] = $search_key;
}
}
$subscription = acquia_agent_settings('acquia_subscription_data');
if (empty($subscription['active']) || empty($key) || empty($identifier)) {
$derived_key[$env_id] = '';
}
elseif (!isset($derived_key[$env_id])) {
$salt = isset($subscription['derived_key_salt']) ? $subscription['derived_key_salt'] : '';
$derivation_string = $identifier . 'solr' . $salt;
$derived_key[$env_id] = hash_hmac('sha1', str_pad($derivation_string, 80, $derivation_string), $key);
}
}
return $derived_key[$env_id];
}
function acquia_search_authenticator($string, $nonce, $derived_key = NULL, $env_id = NULL) {
if (empty($derived_key)) {
$derived_key = _acquia_search_derived_key($env_id);
}
if (empty($derived_key)) {
return '';
}
else {
$time = REQUEST_TIME;
return 'acquia_solr_time=' . $time . '; acquia_solr_nonce=' . $nonce . '; acquia_solr_hmac=' . hash_hmac('sha1', $time . $nonce . $string, $derived_key) . ';';
}
}
function acquia_search_valid_response($hmac, $nonce, $string, $derived_key = NULL, $env_id = NULL) {
if (empty($derived_key)) {
$derived_key = _acquia_search_derived_key($env_id);
}
return $hmac == hash_hmac('sha1', $nonce . $string, $derived_key);
}
function acquia_search_extract_hmac($headers) {
$reg = array();
if (is_array($headers)) {
foreach ($headers as $name => $value) {
if (strtolower($name) == 'pragma' && preg_match("/hmac_digest=([^;]+);/i", $value, $reg)) {
return trim($reg[1]);
}
}
}
return '';
}
function acquia_search_apachesolr_query_alter($query) {
if (!acquia_search_environment_connected($query
->solr('getId')) || $query
->getParam('qt') || $query
->getParam('defType')) {
return;
}
$keys = $query
->getParam('q');
if ($keys && (($wildcard = preg_match('/\\S+[*?]/', $keys)) || variable_get('acquia_search_edismax_default', 0))) {
$query
->addParam('defType', 'edismax');
if ($wildcard) {
$keys = preg_replace_callback('/(\\S+[*?]\\S*)/', '_acquia_search_lower', $keys);
$query
->replaceParam('q', $keys);
}
}
}
function _acquia_search_lower($matches) {
return drupal_strtolower($matches[1]);
}