'Acquia Cloud API Settings', 'page callback' => 'drupal_get_form', 'page arguments' => array('acquiasdk_settings'), 'access arguments' => array('administer acquiasdk'), ); return $items; } /** * Implements hook_permission(). */ function acquiasdk_permission() { $perms = array(); $parms['administer acquiasdk'] = array( 'title' => t('Administer Acquia SDK'), 'description' => t('Perform administration functions for Acquia SDK module.'), ); return $perms; } /** * Acquia SDK module settings form. */ function acquiasdk_settings($form, &$form_state) { $form = array(); $account_link = l(t('Acquia account page'), 'https://accounts.acquia.com/account', array('attributes' => array('target' => '_blank'))); $form['cloudapi'] = array( '#type' => 'fieldset', '#title' => t('Cloud API'), '#description' => t('Obtain your Acquia Cloud API credentials from your !link.', array('!link' => $account_link)), ); $form['cloudapi']['acquiasdk_cloudapi_username'] = array( '#type' => 'textfield', '#title' => t('Username'), '#default_value' => variable_get('acquiasdk_cloudapi_username'), ); $form['cloudapi']['acquiasdk_cloudapi_password'] = array( '#type' => 'password', '#title' => t('Password'), '#description' => t('Leave this blank to keep the existing password.'), ); $network_link = l(t('Acquia Insight'), 'https://insight.acquia.com/', array('attributes' => array('target' => '_blank'))); $form['network'] = array( '#type' => 'fieldset', '#title' => t('Acquia Network'), '#description' => t('Obtain your Acquia Network credentials from !link.', array('!link' => $network_link)), ); $form['network']['acquiasdk_network_id'] = array( '#type' => 'textfield', '#title' => t('ID'), '#default_value' => variable_get('acquiasdk_network_id'), ); $form['network']['acquiasdk_network_key'] = array( '#type' => 'password', '#title' => t('Key'), '#description' => t('Leave this blank to keep the existing key.'), ); $form = system_settings_form($form); return $form; } /** * Validates Acquia SDK module settings form. */ function acquiasdk_settings_validate($form, &$form_state) { $password_fields = array( 'acquiasdk_cloudapi_password' => 'acquiasdk_cloudapi_username', 'acquiasdk_network_key' => 'acquiasdk_network_id', ); foreach ($password_fields as $password_field => $user_field) { $field =& $form_state['values'][$password_field]; if (!$field) { // Hack to preserve the existing value if no new value is set. unset($form_state['values'][$password_field]); continue; } if (!$form_state['values'][$user_field]) { form_set_error($user_field, t('Username is required when providing a password')); } if (module_exists('encrypt')) { $field = encrypt($field); } } } /** * Instantiates a Acquia Cloud API client object. * * @param string $username * (optional) Acquia Cloud API username. If NULL then global config is used. * @param string $password * (optional) Acquia Cloud API password. If NULL then global config is used. * * @return CloudApiClient * Acquia Cloud API client object. */ function acquiasdk_get_cloudapi_client($username = NULL, $password = NULL) { if (!$username) { $username = variable_get('acquiasdk_cloudapi_username'); } if (!$password) { $password = acquiasdk_variable_get('acquiasdk_cloudapi_password'); } if (!$username || !$password) { throw new Exception('Invalid Acquia Cloud API credentials.'); } $cloudapi = CloudApiClient::factory(array( 'username' => $username, 'password' => $password, )); return $cloudapi; } /** * Instantiates Acquia Network client object. * * @param string $network_id * Acquia Network id. If NULL then global config is used. (optional) * @param string $network_key * Acquia Network key. If NULL then global config is used. (optional) * * @return AcquiaNetworkClient * Acquia Network client object. */ function acquiasdk_get_network_client($network_id = NULL, $network_key = NULL) { if (!$network_id) { $network_id = variable_get('acquiasdk_network_id'); } if (!$password) { $network_key = acquiasdk_variable_get('acquiasdk_network_key'); } if (!$network_id || !$network_key) { throw new Exception('Invalid Acquia Network credentials.'); } $network = AcquiaNetworkClient::factory(array( 'network_id' => $network_id, 'network_key' => $network_key, )); return $network; } /** * Retrieves variable with transparent unencryption if supported. * * @param string $name * The name of the variable to return. * * @return mixed * The value of the variable or NULL if not found. */ function acquiasdk_variable_get($name) { $encrypted = &drupal_static(__FUNCTION__); if (NULL == $encrypted) { $encrypted = module_exists('encrypt'); } $var = variable_get($name); if (!$encrypted || 'a:3' != substr($var, 0, 3)) { return $var; } return decrypt($var); }