View source
<?php
function evercurrent_run_update_check($key = NULL) {
$key = $key ?: _evercurrent_get_key();
if (!$key) {
_evercurrent_write_status(RMH_STATUS_WARNING, 'Update check not run. No key');
return FALSE;
}
if ($available = update_get_available(TRUE)) {
module_load_include('inc', 'update', 'update.compare');
$data = update_calculate_project_data($available);
}
else {
_evercurrent_write_status(RMH_STATUS_WARNING, 'Unable to get available updates');
drupal_set_message('Unable to get available updates');
return FALSE;
}
global $base_url;
$sender_data = array(
'send_url' => variable_get('evercurrent_target_address', RMH_ENV_URL),
'project_name' => _evercurrent_get_environment_url(),
'key' => $key,
'module_version' => '0',
'api_version' => '0',
'updates' => array(),
);
$status_list = array(
UPDATE_NOT_SECURE,
UPDATE_REVOKED,
UPDATE_NOT_SUPPORTED,
UPDATE_CURRENT,
UPDATE_NOT_CHECKED,
UPDATE_NOT_CURRENT,
);
foreach ($data as $module => $module_info) {
if (in_array($module_info['status'], $status_list, NULL)) {
$sender_data['updates'][$module] = $data[$module];
$module_path = str_replace('/' . $module, '', drupal_get_path('module', $module));
$sender_data['updates'][$module]['module_path'] = $module_path;
}
}
$sender_data['api_version'] = 2;
$versions = system_get_info('module', 'evercurrent');
$sender_data['module_version'] = $versions['version'];
$sender_data['enabled'] = module_list();
$themes = list_themes(TRUE);
foreach ($themes as $theme) {
$machine_name = $theme->name;
$sender_data['enabled'][$machine_name] = $machine_name;
}
$install_profile = variable_get('install_profile');
if ($install_profile && in_array($install_profile, array_keys($sender_data['updates']))) {
$sender_data['enabled'][$install_profile] = $install_profile;
}
drupal_alter('evercurrent_update_data', $sender_data);
$response = _evercurrent_send_updates($sender_data);
return $response;
}
function _evercurrent_get_key() {
$override = variable_get('evercurrent_override', FALSE);
$config_key = variable_get('evercurrent_environment_token', NULL);
$settings_key = variable_get('evercurrent_key', NULL);
return $config_key && !$override ? $config_key : $settings_key;
}
function _evercurrent_send_updates($sender_data) {
$path = $sender_data['send_url'] . RMH_URL;
$headers = array(
'Content-Type' => 'application/x-www-form-urlencoded',
);
$data = drupal_json_encode($sender_data);
$options = array(
'headers' => $headers,
'method' => 'POST',
'data' => http_build_query(array(
'data' => $data,
)),
);
$response = drupal_http_request($path, $options);
if ($response->code != 200) {
_evercurrent_write_status(RMH_STATUS_ERROR, 'Error code ' . $response->code . ' when trying to post to ' . $path);
}
else {
$response_data = json_decode($response->data);
if ($response_data) {
$saved = $response_data->saved;
if (!$saved) {
_evercurrent_write_status(RMH_STATUS_ERROR, check_plain($response_data->message));
return FALSE;
}
else {
_evercurrent_write_status(RMH_STATUS_OK, $response_data->message);
return TRUE;
}
}
}
}
function _evercurrent_write_status($severity, $message) {
$message = check_plain($message);
variable_set(RMH_STATUS, $severity);
variable_set(RMH_STATUS_MESSAGE, $message);
if ($severity == RMH_STATUS_ERROR) {
watchdog('RMH', $message, array(), WATCHDOG_ERROR);
}
if ($severity == RMH_STATUS_OK) {
variable_set(RMH_LAST_TRY, time());
}
}
function _evercurrent_get_environment_url() {
global $base_url;
$env_url = variable_get('evercurrent_environment_url');
$url = $env_url ? $env_url : $base_url;
return $url;
}