acquia_agent.module

Tracking 6.x-2.x branch
  1. drupal
    1. 6 contributions/acquia_connector/acquia_agent/acquia_agent.module

Acquia Agent securely sends information to Acquia Network.

Functions & methods

NameDescription
acquia_agent_admin_menuImplementation of hook_admin_menu().
acquia_agent_callPrepare and send a XML-RPC request to Acquia Network with an authenticator.
acquia_agent_check_subscriptionGet subscription status from the Acquia Network, and store the result.
acquia_agent_connection_error_messageReturns an error message for the most recent (failed) attempt to connect to the Acquia Network during the current page request. If there were no failed attempts, returns FALSE.
acquia_agent_cronImplementation of hook_cron().
acquia_agent_form_system_modules_alterImplementation of hook_form_[form_id]_alter()..
acquia_agent_get_subscriptionGet subscription status from the Acquia Network
acquia_agent_has_credentialsHelper function to check if an identifer and key exist.
acquia_agent_has_update_serviceReturns the stored subscription data if update service is enabled or FALSE otherwise.
acquia_agent_help
acquia_agent_initImplementation of hook_init().
acquia_agent_load_versionsAPI function used by others to ensure version information is loaded.
acquia_agent_manual_statusSubstituted menu callback for 'admin/reports/updates/check'.
acquia_agent_menuImplementation of hook_menu().
acquia_agent_menu_alterImplemetation of hook_menu_alter()
acquia_agent_menu_iconRender an icon to display in the Administration Menu.
acquia_agent_network_addressHelper function to build the xmlrpc target address.
acquia_agent_random_bytesReturns a string of highly randomized bytes (over the full 8-bit range).
acquia_agent_refresh_statusMenu callback for 'admin/settings/acquia-agent/refresh-status'.
acquia_agent_report_xmlrpc_error
acquia_agent_settingsHelper function so that we don't need to repeat defaults.
acquia_agent_subscription_is_activeHelper function to check if the site has an active subscription.
acquia_agent_system_info_alterImplementation of hook_system_info_alter()
acquia_agent_themeImplementation of hook_theme().
acquia_agent_theme_registry_alterImplementation of hook_theme_registry_alter().
acquia_agent_translated_menu_link_alterImplementation of hook_translated_menu_link_alter().
acquia_agent_update_status_alterImplementation of hook_update_status_alter().
acquia_agent_valid_credentialsValidate identifier/key pair via XML-RPC call to Acquia Network address.
acquia_agent_watchdogImplementation of hook_watchdog().

Constants

NameDescription
SUBSCRIPTION_EXPIRED
SUBSCRIPTION_KEY_MISMATCH
SUBSCRIPTION_KEY_NOT_FOUND
SUBSCRIPTION_MESSAGE_EXPIRED
SUBSCRIPTION_MESSAGE_FUTURE
SUBSCRIPTION_MESSAGE_INVALID
SUBSCRIPTION_MESSAGE_LIFETIMESubscription message lifetime defined by the Acquia Network.
SUBSCRIPTION_NOT_FOUND XML-RPC errors defined by the Acquia Network.
SUBSCRIPTION_PROVISION_ERROR
SUBSCRIPTION_REPLAY_ATTACK
SUBSCRIPTION_VALIDATION_ERROR

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Acquia Agent securely sends information to Acquia Network.
  5. */
  6. /**
  7. * XML-RPC errors defined by the Acquia Network.
  8. */
  9. define('SUBSCRIPTION_NOT_FOUND' , 1000);
  10. define('SUBSCRIPTION_KEY_MISMATCH' , 1100);
  11. define('SUBSCRIPTION_EXPIRED' , 1200);
  12. define('SUBSCRIPTION_REPLAY_ATTACK' , 1300);
  13. define('SUBSCRIPTION_KEY_NOT_FOUND' , 1400);
  14. define('SUBSCRIPTION_MESSAGE_FUTURE' , 1500);
  15. define('SUBSCRIPTION_MESSAGE_EXPIRED' , 1600);
  16. define('SUBSCRIPTION_MESSAGE_INVALID' , 1700);
  17. define('SUBSCRIPTION_VALIDATION_ERROR', 1800);
  18. define('SUBSCRIPTION_PROVISION_ERROR' , 9000);
  19. /*ss*/
  20. /**
  21. * Subscription message lifetime defined by the Acquia Network.
  22. */
  23. define('SUBSCRIPTION_MESSAGE_LIFETIME', 15*60);
  24. /**
  25. * Implementation of hook_menu().
  26. */
  27. function acquia_agent_menu() {
  28. $items['admin/settings/acquia-agent'] = array(
  29. 'title' => 'Acquia Network settings',
  30. 'description' => 'Connect your site to the Acquia Network.',
  31. 'page callback' => 'acquia_agent_settings_page',
  32. 'file' => 'acquia_agent.pages.inc',
  33. 'access arguments' => array('administer site configuration'),
  34. );
  35. $items['admin/settings/acquia-agent/setup'] = array(
  36. 'title' => 'Acquia Network automatic setup',
  37. 'description' => 'Connect your site to the Acquia Network.',
  38. 'page callback' => 'drupal_get_form',
  39. 'page arguments' => array('acquia_agent_automatic_setup_form'),
  40. 'file' => 'acquia_agent.pages.inc',
  41. 'access arguments' => array('administer site configuration'),
  42. 'type' => MENU_CALLBACK,
  43. );
  44. $items['admin/settings/acquia-agent/credentials'] = array(
  45. 'title' => 'Acquia Network credentials',
  46. 'description' => 'Connect your site to the Acquia Network.',
  47. 'page callback' => 'drupal_get_form',
  48. 'page arguments' => array('acquia_agent_settings_credentials'),
  49. 'file' => 'acquia_agent.pages.inc',
  50. 'access arguments' => array('administer site configuration'),
  51. 'type' => MENU_CALLBACK,
  52. );
  53. $items['admin/settings/acquia-agent/migrate'] = array(
  54. 'title' => 'Acquia Cloud Upload',
  55. 'description' => 'Migrate your site to Acquia Cloud.',
  56. 'page callback' => 'acquia_agent_migrate_page',
  57. 'file' => 'acquia_agent.pages.inc',
  58. 'access arguments' => array('administer site configuration'),
  59. 'type' => MENU_CALLBACK,
  60. );
  61. $items['system/acquia-migrate-check'] = array(
  62. 'title' => 'Migrate capable',
  63. 'description' => 'Check for Acquia Cloud migration capabilities',
  64. 'page callback' => 'acquia_agent_migrate_check',
  65. 'file' => 'acquia_agent.migrate.inc',
  66. 'access arguments' => array('access content'),
  67. 'type' => MENU_CALLBACK,
  68. );
  69. $items['admin/settings/acquia-agent/refresh-status'] = array(
  70. 'title' => 'Manual update check',
  71. 'page callback' => 'acquia_agent_refresh_status',
  72. 'access arguments' => array('administer site configuration'),
  73. 'type' => MENU_CALLBACK,
  74. );
  75. return $items;
  76. }
  77. /**
  78. * Implementation of hook_init().
  79. */
  80. function acquia_agent_init() {
  81. if (arg(2) != 'acquia-agent' && empty($_POST) &&
  82. user_access('administer site configuration') &&
  83. (!acquia_agent_has_credentials()) &&
  84. arg(0) !== 'filefield' && arg(1) != 'progress') {
  85. $text = 'Get a <a href="!acquia-free">free 30-day trial</a> of the Acquia Network which includes a <a href="!library" target="_blank">library</a> of expert knowledge, <a href="!services" target="_blank">tools and services</a> to make your site awesome, and <a href="!support" target="_blank">Drupal support</a> when you need it.<br/>If you have an Acquia Network subscription, <a href="!settings">connect now</a>. Otherwise, you can turn this message off by disabling the Acquia Network <a href="!admin-modules">modules</a>.';
  86. if (isset($_SERVER['AH_SITE_GROUP'])) {
  87. $text = '<a href="!settings">Connect your site to the Acquia Network now</a>.';
  88. }
  89. $message = t($text, array('!acquia-free' => url('admin/settings/acquia-agent'), '!library' => url('http://library.acquia.com'), '!settings' => url('admin/settings/acquia-agent/setup'), '!support' => url('http://www.acquia.com/drupal-support'), '!services' => url('http://www.acquia.com/products-services/acquia-network/cloud-services'), '!admin-modules' => url('admin/build/modules', array('fragment' => 'edit-modules-acquia-network-connector'))));
  90. drupal_set_message($message, 'warning', FALSE);
  91. }
  92. }
  93. /**
  94. * Implementation of hook_theme().
  95. */
  96. function acquia_agent_theme() {
  97. return array(
  98. 'acquia_agent_banner_form' => array(
  99. 'arguments' => array('form' => NULL),
  100. 'file' => 'acquia_agent.pages.inc',
  101. ),
  102. );
  103. }
  104. /**
  105. * Get subscription status from the Acquia Network, and store the result.
  106. *
  107. * This check also sends a heartbeat to the Acquia Network unless
  108. * $params['no_heartbeat'] == 1.
  109. */
  110. function acquia_agent_check_subscription($params = array()) {
  111. // Default return value is FALSE.
  112. $subscription = FALSE;
  113. $response_error = FALSE;
  114. $active = FALSE;
  115. if (!acquia_agent_has_credentials()) {
  116. // If there is not an identifier or key, delete any old subscription data.
  117. variable_del('acquia_subscription_data');
  118. }
  119. else {
  120. $subscription = acquia_agent_get_subscription($params);
  121. if (is_int($subscription)) {
  122. switch($subscription) {
  123. case SUBSCRIPTION_NOT_FOUND:
  124. case SUBSCRIPTION_EXPIRED:
  125. variable_del('acquia_subscription_data');
  126. break;
  127. }
  128. }
  129. // Everything checked out fine
  130. variable_set('acquia_subscription_data', $subscription);
  131. // Check if our main subscription is active
  132. if ($subscription) {
  133. $active = acquia_agent_subscription_is_active();
  134. }
  135. }
  136. module_invoke_all('acquia_subscription_status', $active, $subscription);
  137. return $subscription;
  138. }
  139. /**
  140. * Get subscription status from the Acquia Network
  141. *
  142. * This check also sends a heartbeat to the Acquia Network unless
  143. * $params['no_heartbeat'] == 1.
  144. */
  145. function acquia_agent_get_subscription($params = array(), $identifier = NULL, $key = NULL, $acquia_network_address = NULL) {
  146. // Default return value is FALSE.
  147. $response_error = FALSE;
  148. // There is an identifier and key, so attempt communication.
  149. $subscription = array();
  150. $subscription['timestamp'] = time();
  151. // Include version number information.
  152. acquia_agent_load_versions();
  153. if (IS_ACQUIA_DRUPAL) {
  154. $params['version'] = ACQUIA_DRUPAL_VERSION;
  155. $params['series'] = ACQUIA_DRUPAL_SERIES;
  156. $params['branch'] = ACQUIA_DRUPAL_BRANCH;
  157. $params['revision'] = ACQUIA_DRUPAL_REVISION;
  158. }
  159. // Include Acquia Search module version number.
  160. if (module_exists('acquia_search')) {
  161. if (defined('ACQUIA_SEARCH_VERSION')) {
  162. $params['search_version']['acquia_search'] = ACQUIA_SEARCH_VERSION;
  163. }
  164. else {
  165. $params['search_version']['acquia_search'] = variable_get('acquia_search_version', '6.x-3.x');
  166. }
  167. $info = db_result(db_query("SELECT info FROM {system} WHERE name = 'apachesolr'"));
  168. if ($info = unserialize($info)) {
  169. // Send the Drupal core version if the module version is blank
  170. $params['search_version']['apachesolr'] = isset($info['version']) ? (string)$info['version'] : $info['core'];
  171. }
  172. }
  173. // Retrieve our subscription details
  174. $data = acquia_agent_call('acquia.agent.subscription', $params, $identifier, $key, $acquia_network_address);
  175. // Check for errors
  176. if ($errno = xmlrpc_errno()) {
  177. return $errno;
  178. }
  179. elseif (acquia_agent_valid_response($data, $key)) {
  180. // Add the response to our subscription array
  181. $subscription += $data['result']['body'];
  182. }
  183. else {
  184. watchdog('acquia agent', 'HMAC validation error: <pre>@data</pre>', array('@data' => print_r($data, TRUE)), WATCHDOG_ERROR);
  185. return FALSE;
  186. }
  187. return $subscription;
  188. }
  189. function acquia_agent_report_xmlrpc_error() {
  190. drupal_set_message(t('Error: @message (@errno)', array('@message' => xmlrpc_error_msg(), '@errno' => xmlrpc_errno())), 'error');
  191. }
  192. /**
  193. * Implementation of hook_update_status_alter().
  194. *
  195. * This compares the array of computed information about projects that are
  196. * missing available updates with the saved settings. If the settings specify
  197. * that a particular project or release should be ignored, the status for that
  198. * project is altered to indicate it is ignored because of settings.
  199. *
  200. * @param $projects
  201. * Reference to an array of information about available updates to each
  202. * project installed on the system.
  203. *
  204. * @see update_calculate_project_data()
  205. */
  206. function acquia_agent_update_status_alter(&$projects) {
  207. if (!$subscription = acquia_agent_has_update_service()) {
  208. // Get subscription data or return if the service is not enabled.
  209. return;
  210. }
  211. foreach ($projects as $project => $project_info) {
  212. if ($project == 'drupal') {
  213. if (isset($subscription['update'])) {
  214. $projects[$project]['status'] = $subscription['update']['status'];
  215. $projects[$project]['releases'] = $subscription['update']['releases'];
  216. $projects[$project]['recommended'] = $subscription['update']['recommended'];
  217. $projects[$project]['latest_version'] = $subscription['update']['latest_version'];
  218. // Security updates are a separate piece of data. If we leave it, then core
  219. // security warnings from druapl.org will also be displayed on the update page.
  220. unset($projects[$project]['security updates']);
  221. }
  222. else {
  223. $projects[$project]['status'] = UPDATE_NOT_CHECKED;
  224. $projects[$project]['reason'] = t('No information available from the Acquia Network');
  225. unset($projects[$project]['releases']);
  226. unset($projects[$project]['recommended']);
  227. }
  228. $projects[$project]['link'] = 'http://acquia.com/products-services/acquia-drupal';
  229. $projects[$project]['title'] = 'Acquia Drupal';
  230. $projects[$project]['existing_version'] = ACQUIA_DRUPAL_VERSION;
  231. $projects[$project]['install_type'] = 'official';
  232. unset($projects[$project]['extra']);
  233. }
  234. elseif ($project_info['datestamp'] == 'acquia drupal') {
  235. $projects['drupal']['includes'][$project] = $project_info['title'];
  236. unset($projects[$project]);
  237. }
  238. }
  239. }
  240. /**
  241. * Implementation of hook_system_info_alter()
  242. */
  243. function acquia_agent_system_info_alter(&$info) {
  244. if (!$subscription = acquia_agent_has_update_service()) {
  245. // Get subscription data or return if the service is not enabled.
  246. return;
  247. }
  248. if (isset($info['acquia'])) {
  249. // Slight hack - the datestamp field is carried thourgh by update.module.
  250. $info['datestamp'] = 'acquia drupal';
  251. }
  252. }
  253. /**
  254. * Returns the stored subscription data if update service is enabled or FALSE otherwise.
  255. */
  256. function acquia_agent_has_update_service() {
  257. // Include version number information.
  258. acquia_agent_load_versions();
  259. $subscription = acquia_agent_settings('acquia_subscription_data');
  260. if (!IS_ACQUIA_DRUPAL || !$subscription['active'] || (isset($subscription['update_service']) && empty($subscription['update_service']))) {
  261. // We don't have update service if (1) this is not Acquia Drupal, (2) there
  262. // is no subscription or (3) the update service was disabled on acquia.com.
  263. // Requiring the update_service key and checking its value separately is
  264. // important for backwards compatibility. Isset & empty tells us
  265. // that the web service willingly told us to not do update notifications.
  266. return FALSE;
  267. }
  268. return $subscription;
  269. }
  270. /**
  271. * Implemetation of hook_menu_alter()
  272. */
  273. function acquia_agent_menu_alter(&$items) {
  274. if (isset($items['admin/reports/updates/check'])) {
  275. $items['admin/reports/updates/check']['page callback'] = 'acquia_agent_manual_status';
  276. }
  277. }
  278. function acquia_agent_help($path, $arg) {
  279. switch ($path) {
  280. case 'admin/help#acquia_agent':
  281. $output = '<h2>' . t('Acquia Network and Connector modules') . '</h2>';
  282. $output .= '<p>' . t("The Acquia Network Connector suite of modules allow you to connect your site to the Acquia Network and use its variety of services.") . '<p>';
  283. $output .= '<dl>';
  284. $output .= '<dt>Acquia Agent</dt>';
  285. $output .= '<dd>' . t('Enables secure communication between your Drupal sites and the Acquia Network.') . '</dt>';
  286. $output .= '<dt>Acquia SPI</dt>';
  287. $output .= '<dd>' . t('Automates the collection of site information. Required for use with the Acquia Insight service.') . '</dt>';
  288. $output .= '<dt>Acquia Search</dt>';
  289. $output .= '<dd>' . t('Provides authentication service to the Apache Solr Search Integration module to enable use of Acquia\'s hosted Solr search indexes.') . '</dt>';
  290. $output .= '</dl>';
  291. $output .= '<h3>' . t('Configuration settings') . '</h3>';
  292. $output .= '<dl>';
  293. $output .= '<dt>' . t('Data collection and examination') . '</dt>';
  294. $output .= '<dd>' . t('Upon cron (or if configured to run manually) information about your site will be sent and analyzed as part of the Acquia Insight service. You can optionally exclude information about admin privileges, content and user count, and watchdog logs.');
  295. $output .= '<dt>' . t('Source code analysis') . '</dt>';
  296. $output .= '<dd>' . t('If enhanced SSL security is enabled for your site and outside connections are allowed, Acquia Insight will examine the source code of your site to detect alterations and provide code diffs and update recommentations.');
  297. $output .= '<dt>' . t('Enhanced SSL security') . '</dt>';
  298. $output .= '<dd>' . t('The Acquia Connector will attempt to verify Acquia server identities before sending data if SSL is avaliable. May cause communication to fail, however, depending on your local configuration.');
  299. $ssl_available = (in_array('ssl', stream_get_transports(), TRUE) && !defined('ACQUIA_DEVELOPMENT_NOSSL'));
  300. if ($ssl_available) {
  301. $output .= ' <div class="ok">'. t('PHP has SSL support and may support this feature.') .'</div></dd>';
  302. }
  303. else {
  304. $output .= ' <div class="error">'. t('Your installation of PHP does not have SSL support. Please enable the SSL extension or compile PHP with SSL to use this feature, see: <a href="http://php.net/manual/en/book.openssl.php" target="_blank">http://php.net/manual/en/book.openssl.php</a>.') .'</div></dd>';
  305. }
  306. $output .= '<dt>' . t('Receive updates from Acquia Network') . '</dt>';
  307. $output .= '<dd>' . t('Receive dynamic updates on the Network Settings page from Acquia.com about your subscription and new features.') . '</dd>';
  308. $output .= '</dl>';
  309. return $output;
  310. }
  311. }
  312. /**
  313. * Menu callback for 'admin/settings/acquia-agent/refresh-status'.
  314. */
  315. function acquia_agent_refresh_status() {
  316. // Refresh subscription information, so we are sure about our update status.
  317. // We send a heartbeat here so that all of our status information gets
  318. // updated locally via the return data.
  319. acquia_agent_check_subscription();
  320. // Return to the setting page (or destination)
  321. drupal_goto('admin/settings/acquia-agent');
  322. }
  323. /**
  324. * Substituted menu callback for 'admin/reports/updates/check'.
  325. */
  326. function acquia_agent_manual_status() {
  327. // Refresh subscription information, so we are sure about our update status.
  328. // We send a heartbeat here so that all of our status information gets
  329. // updated locally via the return data.
  330. acquia_agent_check_subscription();
  331. // This callback will only ever be available if update module is active.
  332. update_manual_status();
  333. }
  334. /**
  335. * Implementation of hook_cron().
  336. */
  337. function acquia_agent_cron() {
  338. // Check subscription and send a heartbeat to Acquia Network via XML-RPC.
  339. acquia_agent_check_subscription();
  340. }
  341. /**
  342. * Implementation of hook_watchdog().
  343. */
  344. function acquia_agent_watchdog($log_entry) {
  345. // Make sure that even when cron failures prevent hook_cron() from being
  346. // called, we still send out a heartbeat.
  347. $cron_failure_messages = array(
  348. 'Cron has been running for more than an hour and is most likely stuck.',
  349. 'Attempting to re-run cron while it is already running.',
  350. );
  351. if (in_array($log_entry['message'], $cron_failure_messages, TRUE)) {
  352. acquia_agent_check_subscription();
  353. }
  354. }
  355. /**
  356. * @defgroup acquia_admin_menu Alter or add to the administration menu.
  357. * @{
  358. * The admin_menu module is enabled by default - we alter it to add our icon and
  359. * subscription information.
  360. */
  361. /**
  362. * Implementation of hook_admin_menu().
  363. */
  364. function acquia_agent_admin_menu() {
  365. // Add link to show current subscription status
  366. $links[] = array(
  367. 'title' => 'acquia_subscription_status',
  368. 'path' => 'http://acquia.com',
  369. 'weight' => -80,
  370. 'parent_path' => '<root>',
  371. 'options' => array('extra class' => 'admin-menu-action acquia-subscription-status', 'html' => TRUE),
  372. );
  373. return $links;
  374. }
  375. /**
  376. * Implementation of hook_translated_menu_link_alter().
  377. *
  378. * Here is where we make changes to links that need dynamic information such
  379. * as the current page path or the number of users.
  380. */
  381. function acquia_agent_translated_menu_link_alter(&$item, $map) {
  382. global $user;
  383. if (empty($user->uid) || ($item['module'] != 'admin_menu')) {
  384. return;
  385. }
  386. if ($item['title'] == 'acquia_subscription_status') {
  387. $subscription = acquia_agent_settings('acquia_subscription_data');
  388. if (empty($subscription['timestamp']) || (time() - $subscription['timestamp'] > 60*60*24)) {
  389. $subscription = acquia_agent_check_subscription(array('no_heartbeat' => 1));
  390. }
  391. if ($subscription['active']) {
  392. $icon = '<img src="'. base_path() . 'misc/watchdog-ok.png" height="10" alt="ok" />';
  393. $item['title'] = t("!icon Subscription active (expires @date)", array('!icon' => $icon, '@date' => format_date(strtotime($subscription['expiration_date']['value']), 'small')));
  394. $item['localized_options']['extra class'] .= " acquia-active-subscription";
  395. $item['localized_options']['attributes']['title'] = $subscription['product']['view'];
  396. $item['href'] = $subscription['href'];
  397. }
  398. else {
  399. $icon = '<img src="'. base_path() . 'misc/watchdog-error.png" height="10" alt="error" />';
  400. $item['title'] = t("!icon Subscription not active", array('!icon' => $icon));
  401. $item['localized_options']['extra class'] .= " acquia-inactive-subscription";
  402. $item['href'] = 'http://acquia.com/network';
  403. }
  404. }
  405. }
  406. /**
  407. * Implementation of hook_theme_registry_alter().
  408. */
  409. function acquia_agent_theme_registry_alter(&$theme_registry) {
  410. if (isset($theme_registry['admin_menu_icon'])) {
  411. $theme_registry['admin_menu_icon']['function'] = 'acquia_agent_menu_icon';
  412. }
  413. }
  414. /**
  415. * Render an icon to display in the Administration Menu.
  416. */
  417. function acquia_agent_menu_icon() {
  418. return '<img class="admin-menu-icon" src="'. base_path() . drupal_get_path('module', 'acquia_agent') . '/acquia.ico" height = "16" alt="" />';
  419. }
  420. /**
  421. * @} End of "acquia_admin_menu".
  422. */
  423. /**
  424. * Validate identifier/key pair via XML-RPC call to Acquia Network address.
  425. *
  426. * This is generaly only useful when actually entering the values in the form.
  427. * Normally, use acquia_agent_check_subscription() since it also validates
  428. * the response.
  429. */
  430. function acquia_agent_valid_credentials($identifier, $key, $acquia_network_address = NULL) {
  431. $data = acquia_agent_call('acquia.agent.validate', array(), $identifier, $key, $acquia_network_address);
  432. return (bool)$data['result'];
  433. }
  434. /**
  435. * Prepare and send a XML-RPC request to Acquia Network with an authenticator.
  436. *
  437. */
  438. function acquia_agent_call($method, $params, $identifier = NULL, $key = NULL, $acquia_network_address = NULL) {
  439. $path = drupal_get_path('module', 'acquia_agent');
  440. require_once $path .'/acquia_agent_streams.inc';
  441. $acquia_network_address = acquia_agent_network_address($acquia_network_address);
  442. $ip = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : '';
  443. $host = isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : '';
  444. $ssl = isset($_SERVER["HTTPS"]) ? TRUE : FALSE;
  445. $data = array(
  446. 'authenticator' => _acquia_agent_authenticator($params, $identifier, $key),
  447. 'ip' => $ip,
  448. 'host' => $host,
  449. 'ssl' => $ssl,
  450. 'body' => $params,
  451. );
  452. $data['result'] = _acquia_agent_request($acquia_network_address, $method, $data);
  453. return $data;
  454. }
  455. /* change */
  456. /**
  457. * Returns an error message for the most recent (failed) attempt to connect
  458. * to the Acquia Network during the current page request. If there were no
  459. * failed attempts, returns FALSE.
  460. *
  461. * This function assumes that the most recent XML-RPC error came from the
  462. * Acquia Network; otherwise, it will not work correctly.
  463. */
  464. function acquia_agent_connection_error_message() {
  465. $errno = xmlrpc_errno();
  466. if ($errno) {
  467. switch ($errno) {
  468. case SUBSCRIPTION_NOT_FOUND:
  469. return t('The identifier you have provided does not exist in the Acquia Network or is expired. Please make sure you have used the correct value and try again.');
  470. break;
  471. case SUBSCRIPTION_EXPIRED:
  472. return t('Your Acquia Network subscription has expired. Please renew your subscription so that you can resume using Acquia Network services.');
  473. break;
  474. case SUBSCRIPTION_MESSAGE_FUTURE:
  475. return t('Your server is unable to communicate with the Acquia Network due to a problem with your clock settings. For security reasons, we reject messages that are more than @time ahead of the actual time recorded by our servers. Please fix the clock on your server and try again.', array('@time' => format_interval(SUBSCRIPTION_MESSAGE_LIFETIME)));
  476. break;
  477. case SUBSCRIPTION_MESSAGE_EXPIRED:
  478. return t('Your server is unable to communicate with the Acquia Network due to a problem with your clock settings. For security reasons, we reject messages that are more than @time older than the actual time recorded by our servers. Please fix the clock on your server and try again.', array('@time' => format_interval(SUBSCRIPTION_MESSAGE_LIFETIME)));
  479. break;
  480. case SUBSCRIPTION_VALIDATION_ERROR:
  481. return t('The identifier and key you have provided for the Acquia Network do not match. Please make sure you have used the correct values and try again.');
  482. break;
  483. default:
  484. return t('There is an error communicating with the Acquia Network at this time. Please check your identifier and key and try again.');
  485. break;
  486. }
  487. }
  488. return FALSE;
  489. }
  490. /**
  491. * Helper function to build the xmlrpc target address.
  492. */
  493. function acquia_agent_network_address($acquia_network_address = NULL) {
  494. if (empty($acquia_network_address)) {
  495. $acquia_network_address = acquia_agent_settings('acquia_network_address');
  496. }
  497. // Strip protocol (scheme) from Network address
  498. $uri = parse_url($acquia_network_address);
  499. if (isset($uri['host'])) {
  500. $acquia_network_address = $uri['host'];
  501. }
  502. $acquia_network_address .= isset($uri['port']) ? ':' . $uri['port'] : '';
  503. $acquia_network_address .= (isset($uri['path']) && isset($uri['host'])) ? $uri['path'] : '';
  504. // Add a scheme based on PHP's capacity.
  505. if (in_array('ssl', stream_get_transports(), TRUE) && !defined('ACQUIA_DEVELOPMENT_NOSSL')) {
  506. // OpenSSL is available in PHP
  507. $acquia_network_address = 'https://' . $acquia_network_address;
  508. }
  509. else {
  510. $acquia_network_address = 'http://' . $acquia_network_address;
  511. }
  512. $acquia_network_address .= '/xmlrpc.php';
  513. return $acquia_network_address;
  514. }
  515. /**
  516. * Helper function to check if an identifer and key exist.
  517. */
  518. function acquia_agent_has_credentials() {
  519. return (bool)(variable_get('acquia_identifier', '') && variable_get('acquia_key', ''));
  520. }
  521. /**
  522. * Helper function to check if the site has an active subscription.
  523. */
  524. function acquia_agent_subscription_is_active() {
  525. $active = FALSE;
  526. // Subscription cannot be active if we have no credentials.
  527. if (acquia_agent_has_credentials()) {
  528. $subscription = acquia_agent_settings('acquia_subscription_data');
  529. $active = !empty($subscription['active']);
  530. }
  531. return $active;
  532. }
  533. /**
  534. * Helper function so that we don't need to repeat defaults.
  535. */
  536. function acquia_agent_settings($variable_name) {
  537. switch ($variable_name) {
  538. case 'acquia_identifier':
  539. return variable_get('acquia_identifier', '');
  540. case 'acquia_key':
  541. return variable_get('acquia_key', '');
  542. case 'acquia_network_address':
  543. return variable_get('acquia_network_address', 'https://rpc.acquia.com');
  544. case 'acquia_subscription_data':
  545. return variable_get('acquia_subscription_data', array('active' => FALSE));
  546. case 'acquia_subscription_name':
  547. return variable_get('acquia_subscription_name', '');
  548. }
  549. }
  550. /**
  551. * Returns a string of highly randomized bytes (over the full 8-bit range).
  552. *
  553. * This function is better than simply calling mt_rand() or any other built-in
  554. * PHP function because it can return a long string of bytes (compared to < 4
  555. * bytes normally from mt_rand)) and uses the best available pseudo-random source.
  556. *
  557. * @param $count
  558. * The number of characters (bytes) to return in the string.
  559. */
  560. function acquia_agent_random_bytes($count) {
  561. static $random_state;
  562. // We initialize with the somewhat random PHP process ID on the first call.
  563. if (empty($random_state)) {
  564. $random_state = getmypid();
  565. }
  566. $output = '';
  567. // /dev/urandom is available on many *nix systems and is considered the best
  568. // commonly available pseudo-random source.
  569. if ($fh = @fopen('/dev/urandom', 'rb')) {
  570. $output = fread($fh, $count);
  571. fclose($fh);
  572. }
  573. // If /dev/urandom is not available or returns no bytes, this loop will
  574. // generate a good set of pseudo-random bytes on any system.
  575. while (strlen($output) < $count) {
  576. $random_state = md5(microtime() . mt_rand() . $random_state);
  577. $output .= pack('H*', md5(mt_rand() . $random_state));
  578. }
  579. return substr($output, 0, $count);
  580. }
  581. /**
  582. * API function used by others to ensure version information is loaded.
  583. *
  584. * Saves us some cycles to not load it each time, when it is actually
  585. * not needed. We store this in a separate file, so that the Acquia
  586. * build process only needs to alter that file instead of the main
  587. * module file.
  588. */
  589. function acquia_agent_load_versions() {
  590. // Include version number information.
  591. include_once 'acquia_agent_drupal_version.inc';
  592. }
  593. /**
  594. * Implementation of hook_form_[form_id]_alter()..
  595. */
  596. function acquia_agent_form_system_modules_alter(&$form, &$form_state) {
  597. if (isset($form['description']['acquia_search'])) {
  598. $subscription = acquia_agent_settings('acquia_subscription_data');
  599. if (!module_exists('acquia_search') && empty($subscription['active'])) {
  600. $form['status']['#disabled_modules'][] = 'acquia_search';
  601. $text = 'Acquia Search requires an <a href="@network-url">Acquia Network subscription</a>';
  602. $message = t($text, array('@network-url' => 'http://acquia.com/products-services/acquia-search'));
  603. $form['description']['acquia_search']['#value'] = '<div style="padding-left:5px; margin:8px 0px" class="messages warning" id="acquia-agent-no-search">' . $message . '</div>' . $form['description']['acquia_search']['#value'];
  604. }
  605. }
  606. }