1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114 |
- <?php
- /**
- * @file
- * Functions to handle the NodeSquirrel backup destination.
- */
- define('NODESQUIRREL_SECRET_KEY_PATTERN', '/^[0-9a-f]{32}\:?[0-9a-f]{32}$/');
- /**
- * Checks that a nodesquirel key is valid.
- */
- function backup_migrate_nodesquirrel_check_secret_key($secret_key) {
- if ($destination = backup_migrate_nodesquirrel_get_destination($secret_key)) {
- if ($destination->confirm_destination()) {
- return $destination;
- }
- }
- return FALSE;
- }
- /**
- * Gets the NS destination for the given key.
- */
- function backup_migrate_nodesquirrel_get_destination($secret_key) {
- if ($secret_key) {
- backup_migrate_include('destinations');
- $destination = backup_migrate_create_destination('nodesquirrel', array('machine_name' => 'nodesquirrel'));
- $destination->settings['secret_key'] = $secret_key;
- return $destination;
- }
- return NULL;
- }
- /**
- * Gets a helper link to activate a site and create a tree.
- */
- function backup_migrate_nodesquirrel_get_activate_help_text() {
- $activate_link = backup_migrate_nodesquirrel_get_activate_link();
- return array(
- '#type' => 'item',
- '#title' => t('Need a Secret Key?'),
- '#markup' => t('Visit !nodesquirrel.', array('!nodesquirrel' => $activate_link)),
- '#description' => t("Don't worry if you don't have an account yet. You can create one when you get there."),
- );
- }
- /**
- * Gets a helper link to activate a site and create a tree.
- */
- function backup_migrate_nodesquirrel_get_activate_link() {
- $activate_link = l('nodesquirrel.com/activate', variable_get('nodesquirrel_activate_url', 'https://manage.nodesquirrel.com/activate'), array(
- 'query' => array(
- 'url' => url('', array('absolute' => TRUE)),
- 'email' => variable_get('site_mail', ''),
- 'configure' => url($_GET['q'], array('absolute' => TRUE)),
- ),
- ));
- return $activate_link;
- }
- /**
- * Gets a helper link to activate a site and create a tree.
- */
- function backup_migrate_nodesquirrel_get_manage_link($destination) {
- $url = variable_get('nodesquirrel_manage_url', 'https://manage.nodesquirrel.com') . '/backups/' . $destination->_get_destination();
- return l($url, $url);
- }
- /**
- * Gets a helper link to activate a site and create a tree.
- */
- function backup_migrate_nodesquirrel_get_plan_link() {
- $url = variable_get('nodesquirrel_manage_url', 'https://manage.nodesquirrel.com') . '/plan';
- return l($url, $url);
- }
- /**
- * Gets the path to the nodesquirrel settings tab.
- */
- function backup_migrate_nodesquirrel_settings_path() {
- $path = BACKUP_MIGRATE_MENU_PATH . '/settings/destinations/nodesquirrel/edit';
- return $path;
- }
- /**
- * NodeSquirrel settings page callback.
- */
- function backup_migrate_nodesquirrel_settings() {
- return drupal_get_form('backup_migrate_nodesquirrel_settings_form');
- }
- /**
- * NodeSquirrel settings form.
- */
- function backup_migrate_nodesquirrel_credentials_settings_form($key = '', $status) {
- $collapse = !empty($status);
- $form['nodesquirrel_credentials'] = array(
- '#type' => 'fieldset',
- '#title' => t('NodeSquirrel Credentials'),
- '#collapsible' => $collapse,
- '#collapsed' => $collapse,
- );
- $form['nodesquirrel_credentials']['nodesquirrel_secret_key'] = array(
- '#type' => 'textfield',
- '#title' => t('Secret Key'),
- '#size' => 80,
- '#default_value' => $key,
- );
- if (empty($key)) {
- $form['nodesquirrel_credentials']['secret_key_help'] = backup_migrate_nodesquirrel_get_activate_help_text();
- }
- $form['nodesquirrel_credentials']['nodesquirrel_failover'] = array(
- '#type' => 'checkbox',
- '#title' => t('Connection failover by disabling SSL certification validation'),
- '#description' => t("If normal connections to NodeSquirrel fail due incompatibility between this server's PHP installation and the destination server, use this option to disable validating the destination's SSL certificate.<br />Note: <em>this has security implications</em> and should be the last option taken; it probably would be safer to update the server's PHP."),
- '#default_value' => variable_get('nodesquirrel_failover', FALSE),
- );
- return $form;
- }
- /**
- * Return a form element with some help text describing NodeSquirrel.
- */
- function backup_migrate_nodesquirrel_info_form() {
- $form = array();
- $form['nodesquirrel_info'] = array(
- '#type' => 'fieldset',
- '#title' => t('What is NodeSquirrel?'),
- );
- $form['nodesquirrel_info']['intro'] = array(
- '#type' => 'markup',
- '#markup' => t('<p>NodeSquirrel is the cloud backup service built by the maintainers of Backup and Migrate.</p></p>NodeSquirrel is now free for all Drupal sites, courtesy of Pantheon.</p><p>Create your free account at !signup</p>', array(
- '!nodesquirrel' => l('nodesquirrel.com', 'http://www.nodesquirrel.com'),
- '!signup' => backup_migrate_nodesquirrel_get_activate_link(),
- )),
- );
- return $form;
- }
- /**
- * Display the NodeSquirrel status on the configuration form.
- */
- function backup_migrate_nodesquirrel_status_form($key, $destination, $status) {
- $form = array();
- $form['nodesquirrel_status'] = array(
- '#type' => 'fieldset',
- '#title' => t('NodeSquirrel Status'),
- );
- $form['nodesquirrel_status']['status'] = array(
- '#type' => 'item',
- '#title' => t('NodeSquirrel Status'),
- '#markup' => t('Not Configured. Enter your Secret Key below to get started.'),
- );
- // Warn the user if the key they entered is invalid.
- if ($key && empty($status)) {
- $form['nodesquirrel_status']['status']['#markup'] = t('Your secret key does not seem to be valid. Please check that you entered it correctly or visit !ns to generate a new key.', array('!ns' => backup_migrate_nodesquirrel_get_activate_link()));
- }
- elseif (!empty($destination) && is_array($status)) {
- if (!empty($status['lifetime_backups_used']) && !empty($status['lifetime_backups']) && $status['lifetime_backups_used'] >= $status['lifetime_backups']) {
- $form['nodesquirrel_status']['status']['#markup'] = t('Your !num backup trial has expired. Visit !link to continue backing up.', array('!num' => $status['lifetime_backups'], '!link' => backup_migrate_nodesquirrel_get_plan_link()));
- }
- else {
- $form['nodesquirrel_status']['status']['#markup'] = t('Ready to Backup');
- if (user_access('perform backup')) {
- $form['nodesquirrel_status']['status']['#markup'] .= ' ' . l('(' . t('backup now') . ')', BACKUP_MIGRATE_MENU_PATH);
- }
- }
- if (!empty($status['plan_name'])) {
- $form['nodesquirrel_status']['plan_name'] = array(
- '#type' => 'item',
- '#title' => t('Current Plan'),
- '#markup' => check_plain($status['plan_name']),
- );
- if (!empty($status['lifetime_backups']) && isset($status['lifetime_backups_used'])) {
- $remains = $status['lifetime_backups'] - $status['lifetime_backups_used'];
- $remains = $remains > 0 ? $remains : t('none');
- $form['nodesquirrel_status']['plan_name']['#markup'] .= ' ' . t('(@remains remaining of @backups backup trial)', array('@backups' => $status['lifetime_backups'], '@remains' => $remains));
- }
- if (!empty($status['lifespan']) && isset($status['age']) && $status['lifespan'] > 0) {
- $remains = ceil(($status['lifespan'] - $status['age']) / 86400);
- if ($remains <= 0) {
- $form['nodesquirrel_status']['plan_name']['#markup'] .= ' ' . t('(Your !span day trial has expired.)', array('!span' => ceil($status['lifespan'] / 86400)));
- }
- else {
- $form['nodesquirrel_status']['plan_name']['#markup'] .= ' ' . format_plural($remains, '(1 day remaining)', '(!span days remaining)', array('!span' => ceil($remains)));
- }
- }
- }
- if (isset($status['backups_used'])) {
- $form['nodesquirrel_status']['backups_used'] = array(
- '#type' => 'item',
- '#title' => t('Number of Stored Backups'),
- '#markup' => $status['backups_used'] == 0 ? t('None') : number_format($status['backups_used']),
- );
- }
- if (isset($status['last_backup'])) {
- $form['nodesquirrel_status']['last_backup'] = array(
- '#type' => 'item',
- '#title' => t('Last Backup'),
- '#markup' => empty($status['last_backup']) ? t('Never') : t('!date (!ago ago)', array(
- '!date' => format_date($status['last_backup'], 'small'),
- '!ago' => format_interval(time() - $status['last_backup'], 1),
- )),
- );
- }
- if ($status['bytes_per_locker']) {
- if (isset($status['bytes_used'])) {
- $form['nodesquirrel_status']['space'] = array(
- '#type' => 'item',
- '#title' => t('Storage Space'),
- '#markup' => t('!used used of !total (!remaining remaining)', array(
- '!used' => backup_migrate_format_size($status['bytes_used']),
- '!total' => backup_migrate_format_size($status['bytes_per_locker']),
- '!remaining' => backup_migrate_format_size(max(0, $status['bytes_per_locker'] - $status['bytes_used'])),
- )),
- );
- }
- else {
- $form['nodesquirrel_status']['space'] = array(
- '#type' => 'item',
- '#title' => t('Total Storage Space'),
- '#markup' => t('!total', array('!total' => backup_migrate_format_size($status['bytes_per_locker']))),
- );
- }
- }
- $form['nodesquirrel_status']['manage'] = array(
- '#type' => 'item',
- '#title' => t('Management Console'),
- '#markup' => backup_migrate_nodesquirrel_get_manage_link($destination),
- '#description' => t('You can use the NodeSquirrel management console to add and edit your sites, reset your secret key, download and delete backups, and modify your NodeSquirrel account.'),
- );
- }
- return $form;
- }
- function backup_migrate_nodesquirrel_schedule_settings_form($destination, $status) {
- backup_migrate_include('sources', 'schedules', 'profiles');
- // If the schedule has been overriden it must be edited in the schedule tab.
- $schedule = backup_migrate_crud_get_item('schedule', 'nodesquirrel');
- $default = 60 * 60 * 24;
- $form = array();
- $form['nodesquirrel_schedule'] = array(
- '#type' => 'fieldset',
- '#title' => t('Backup Schedule'),
- '#description' => t('Set up a schedule to back up your site to NodeSquirrel. You can customize this schedule or add additional schedules in the !schedule.', array(
- '!schedule' => l(t('Schedules tab'), BACKUP_MIGRATE_MENU_PATH . '/schedule'),
- '!cron' => l(t('cron'), 'http://drupal.org/cron'),
- )),
- );
- $key = 'nodesquirrel_schedule';
- $form['nodesquirrel_schedule'][$key] = array();
- $defaults = array(
- 'period' => empty($schedule) ? variable_get('nodesquirrel_schedule', 60 * 60 * 24) : $schedule->get('period'),
- 'enabled' => empty($schedule) ? variable_get('nodesquirrel_schedule_enabled', TRUE) : $schedule->get('enabled'),
- 'source_id' => empty($schedule) ? variable_get('nodesquirrel_schedule_source_id', 'db') : $schedule->get('source_id'),
- 'keep' => empty($schedule) ? variable_get('nodesquirrel_schedule_keep', BACKUP_MIGRATE_SMART_DELETE) : $schedule->get('keep'),
- );
- $form['nodesquirrel_schedule'][$key]['nodesquirrel_schedule_enabled'] = array(
- '#type' => 'checkbox',
- '#title' => t('Automatically backup to NodeSquirrel'),
- '#default_value' => $defaults['enabled'],
- );
- $form['nodesquirrel_schedule'][$key]['settings'] = array(
- '#type' => 'backup_migrate_dependent',
- '#dependencies' => array(
- 'nodesquirrel_schedule_enabled' => TRUE,
- ),
- );
- $form['nodesquirrel_schedule'][$key]['settings']['nodesquirrel_schedule_source_id'] = _backup_migrate_get_source_pulldown($defaults['source_id']);
- $options = array(
- (60 * 60) => t('Once an hour'),
- (60 * 60 * 24) => t('Once a day'),
- (60 * 60 * 24 * 7) => t('Once a week'),
- );
- $period = $defaults['period'];
- if (!isset($options[$period])) {
- $options[$period] = empty($schedule) ? t('Custom') : $schedule->get('frequency_description');
- }
- $form['nodesquirrel_schedule'][$key]['settings']['nodesquirrel_schedule'] = array(
- '#type' => 'select',
- '#title' => t('Schedule Frequency'),
- '#options' => $options,
- '#default_value' => $period,
- );
- $form['nodesquirrel_schedule'][$key]['settings']['nodesquirrel_schedule_keep'] = array(
- '#type' => 'checkbox',
- '#title' => t('Automatically delete old backups'),
- '#return_value' => $defaults['keep'] == BACKUP_MIGRATE_KEEP_ALL ? BACKUP_MIGRATE_SMART_DELETE : $defaults['keep'],
- '#default_value' => $defaults['keep'],
- );
- return $form;
- }
- /**
- * NodeSquirrel settings form.
- */
- function backup_migrate_nodesquirrel_settings_form($form_state) {
- _backup_migrate_message_callback('_backup_migrate_message_browser');
- $form = array();
- $key = variable_get('nodesquirrel_secret_key', '');
- $status = array();
- if ($destination = backup_migrate_nodesquirrel_get_destination($key)) {
- $status = $destination->check_limits();
- }
- $form += backup_migrate_nodesquirrel_info_form();
- $form += backup_migrate_nodesquirrel_status_form($key, $destination, $status);
- $form += backup_migrate_nodesquirrel_credentials_settings_form($key, $status);
- $form += backup_migrate_nodesquirrel_schedule_settings_form($destination, $status);
- return system_settings_form($form);
- }
- /**
- * A destination for sending database backups to the NodeSquirel backup service.
- *
- * @ingroup backup_migrate_destinations
- */
- class backup_migrate_destination_nodesquirrel extends backup_migrate_destination {
- public $supported_ops = array(
- 'scheduled backup',
- 'manual backup',
- 'remote backup',
- 'restore',
- 'list files',
- 'configure',
- 'delete',
- );
- public $cache_files = TRUE;
- /**
- * Don't generate a metadata file as NodeSquirrel can save metadata natively.
- */
- public $save_metadata = FALSE;
- public function __construct($params = array()) {
- parent::__construct($params + array('name' => t('NodeSquirrel')));
- }
- /**
- * Gets the menu items for manipulating this type.
- */
- public function get_menu_items() {
- $items = array();
- $path = BACKUP_MIGRATE_MENU_PATH . '/nodesquirrel';
- $items[$path] = array(
- 'title' => t('NodeSquirrel'),
- 'page callback' => 'backup_migrate_menu_callback',
- 'page arguments' => array('destinations.nodesquirrel', 'backup_migrate_nodesquirrel_settings'),
- 'access arguments' => array('administer backup and migrate'),
- 'weight' => 10,
- 'type' => MENU_LOCAL_TASK,
- );
- return $items;
- }
- /**
- * Declares any MySQL databases defined in the settings.php file as a possible destination.
- */
- public function destinations() {
- $out = array();
- $out['nodesquirrel'] = backup_migrate_create_destination('nodesquirrel', array('machine_name' => 'nodesquirrel'));
- if ($secret_key = variable_get('nodesquirrel_secret_key', '')) {
- $out['nodesquirrel']->settings['secret_key'] = $secret_key;
- }
- else {
- $out['nodesquirrel']->supported_ops = array('manual backup', 'remote backup');
- }
- return $out;
- }
- /**
- * Returns a form to be completed before saving to the destination can be complete.
- */
- public function before_backup_form($settings) {
- $form = array();
- $key = $this->settings('secret_key');
- if (empty($key)) {
- $form += backup_migrate_nodesquirrel_info_form();
- $form += backup_migrate_nodesquirrel_credentials_settings_form();
- }
- return $form;
- }
- /**
- * Validates the before_backup_form form.
- */
- public function before_backup_form_validate($settings, $form, $form_state) {
- if (isset($form_state['values']['nodesquirrel_secret_key'])) {
- $key = trim($form_state['values']['nodesquirrel_secret_key']);
- if ($error = $this->vaidate_key($key)) {
- form_set_error('secret_key', $error);
- }
- }
- }
- /**
- * Submits the before_backup_form form.
- */
- public function before_backup_form_submit($settings, $form, $form_state) {
- if (isset($form_state['values']['nodesquirrel_secret_key'])) {
- $this->settings['secret_key'] = $form_state['values']['nodesquirrel_secret_key'];
- variable_set('nodesquirrel_secret_key', $this->settings['secret_key']);
- }
- }
- /**
- * Saves to the NodeSquirrel destination.
- */
- public function save_file($file, $settings) {
- if ($destination = $this->_get_destination()) {
- srand((double) microtime() * 1000000);
- $filename = $file->filename();
- $filesize = filesize($file->filepath());
- $ticket = $this->_xmlrpc('backups.getUploadTicket', array(
- $destination,
- $filename,
- $filesize,
- $file->file_info,
- ));
- if ($ticket) {
- $url = $ticket['url'];
- // If the ticket requires authentication add our username/password to
- // the url.
- if (!empty($ticket['auth']) && $ticket['auth'] = 'basic') {
- $parts = parse_url($ticket['url']);
- list($parts['user'], $parts['pass']) = $this->get_user_pass();
- $url = $this->glue_url($parts, FALSE);
- }
- $out = $this->_post_file($url, 'POST', $ticket['params'], $file);
- if ($out->code == 200) {
- // Confirm the upload.
- $confirm = $this->_xmlrpc('backups.confirmUpload', array(
- $destination,
- $filename,
- $filesize,
- ));
- if ($confirm['success']) {
- // Set a message with a link to the manage console.
- _backup_migrate_message('Your backup has been saved to your NodeSquirrel account. View it at !account', array('!account' => backup_migrate_nodesquirrel_get_manage_link($this)));
- return $file;
- }
- else {
- _backup_migrate_message('The backup file never made it to the NodeSquirrel backup server. There may have been a network problem. Please try again later');
- }
- }
- else {
- $error = !empty($out->headers['x-bams-error']) ? $out->headers['x-bams-error'] : $out->error;
- _backup_migrate_message('The NodeSquirrel server returned the following error: %err', array('%err' => $error), 'error');
- }
- }
- elseif ($err = xmlrpc_error()) {
- // XMLRPC errors are already handled by the server function below.
- }
- else {
- _backup_migrate_message('The NodeSquirrel server refused the backup but did not specify why. Maybe the server is down.');
- }
- }
- return NULL;
- }
- /**
- * Loads from the NodeSquirrel destination.
- */
- public function load_file($file_id) {
- if ($destination = $this->_get_destination()) {
- backup_migrate_include('files');
- $file = new backup_file(array('filename' => $file_id));
- $ticket = $this->_xmlrpc('backups.getDownloadTicket', array($destination, $file_id));
- if ($ticket && $url = $ticket['url']) {
- // If the ticket requires authentication add our username/password to
- // the url.
- if (!empty($ticket['auth']) && $ticket['auth'] = 'basic') {
- $parts = parse_url($ticket['url']);
- $parts['user'] = @$this->dest_url['user'];
- $parts['pass'] = @$this->dest_url['pass'];
- $url = $this->glue_url($parts, FALSE);
- }
- $out = drupal_http_request($url);
- if ($out->code == 200) {
- file_put_contents($file->filepath(), $out->data);
- return $file;
- }
- else {
- $error = !empty($out->headers['x-bams-error']) ? $out->headers['x-bams-error'] : $out->error;
- _backup_migrate_message('The server returned the following error: %err', array('%err' => $error), 'error');
- }
- }
- }
- return NULL;
- }
- /**
- * Deletes from the NodeSquirrel destination.
- */
- public function delete_file($file_id) {
- if ($destination = $this->_get_destination()) {
- $result = $this->_xmlrpc('backups.deleteFile', array($destination, $file_id));
- }
- }
- /**
- * Lists the files in the remote destination.
- */
- public function _list_files() {
- $files = array();
- backup_migrate_include('files');
- if ($destination = $this->_get_destination()) {
- $file_list = $this->_xmlrpc('backups.listFiles', array($destination));
- foreach ((array) $file_list as $file) {
- $files[$file['filename']] = new backup_file($file);
- }
- }
- return $files;
- }
- /**
- * Lists the files in the remote destination.
- */
- public function check_limits() {
- if (empty($this->limits)) {
- $this->limits = $this->_xmlrpc('backups.getLimits', array($this->_get_destination()));
- }
- return $this->limits;
- }
- /**
- * Checks that a destination is valid.
- */
- public function confirm_destination() {
- return $this->check_limits();
- }
- /**
- * Gets the form for the settings for this destination.
- */
- public function edit_form() {
- $form = parent::edit_form();
- // If this is a new destination but the default NS destination has not been
- // created yet, redirect to the NS config screen.
- if (!$this->get_id() && !variable_get('nodesquirrel_secret_key', '')) {
- drupal_goto(BACKUP_MIGRATE_MENU_PATH . '/nodesquirrel');
- }
- $form['settings'] = array('#tree' => TRUE);
- $activate_link = backup_migrate_nodesquirrel_get_activate_link();
- // Retrieve the key from the settings or get it from the get string if this
- // is an auto-config action.
- $key = $this->settings('secret_key');
- if (!empty($_GET['key']) && preg_match(NODESQUIRREL_SECRET_KEY_PATTERN, $_GET['key'])) {
- $key = $_GET['key'];
- }
- $form['settings']['secret_key'] = array(
- '#type' => 'textfield',
- '#title' => t('Secret Key'),
- '#default_value' => $key,
- );
- $form['settings']['location'] = array('#type' => 'value', '#value' => '');
- $form['settings']['secret_key_help'] = array(
- '#type' => 'item',
- '#title' => t('Need a Secret Key?'),
- '#markup' => t('Visit !nodesquirrel.', array('!nodesquirrel' => $activate_link)),
- );
- return $form;
- }
- /**
- * Submits the configuration form.
- *
- * Glue the url together and add the old password back if a new one was not
- * specified.
- */
- public function edit_form_validate($form, &$form_state) {
- $key = trim($form_state['values']['settings']['secret_key']);
- if ($error = $this->vaidate_key($key)) {
- form_set_error('secret_key', $error);
- }
- }
- /**
- * Validates a secret key.
- *
- * Returns error text if the key is invalid.
- */
- public function vaidate_key($key) {
- $error = FALSE;
- if ($key) {
- if (!preg_match(NODESQUIRREL_SECRET_KEY_PATTERN, $key)) {
- return 'The secret key you entered is not the right format. Please make sure you copy it exactly.';
- }
- $this->settings['secret_key'] = check_plain($key);
- $limits = $this->check_limits();
- if (!$limits) {
- $err = xmlrpc_error();
- if (!empty($err->code) && $err->code == '401') {
- return 'Could not log in to the NodeSquirrel server. Please check that your secret key is correct.';
- }
- else {
- return 'Your site could not be found on NodeSquirrel. Please check that your secret key is correct.';
- }
- }
- }
- else {
- return 'You must enter a NodeSquirrel secret key.';
- }
- return FALSE;
- }
- /**
- * Submits the configuration form.
- *
- * Glue the url together and add the old password back if a new one was not
- * specified.
- */
- public function edit_form_submit($form, &$form_state) {
- $form_state['values']['secret_key'] = check_plain($form_state['values']['settings']['secret_key']);
- parent::edit_form_submit($form, $form_state);
- }
- /**
- * Gets the destination id or warn the user that it has not been set.
- */
- public function _get_destination($warn = TRUE) {
- list($id, $key) = $this->get_user_pass();
- return $id;
- }
- /**
- * Gets the destination id or warn the user that it has not been set.
- */
- public function _get_private_key($warn = TRUE) {
- list($id, $key) = $this->get_user_pass();
- return $key;
- }
- /**
- * Breaks the secret key into the public/private key (user/pass).
- */
- public function get_user_pass() {
- $key = $this->settings('secret_key');
- // The username is the first 32 chars.
- $user = substr($key, 0, 32);
- // The pass is the last 32 chars. There may be a separating character.
- $pass = substr($key, strlen($key) - 32);
- return array($user, $pass);
- }
- public function get_display_location() {
- return t('NodeSquirrel.com');
- }
- public function add_scheme($url) {
- return 'https://' . $url;
- }
- /**
- * Gets the form for the settings for this destination.
- */
- public function _xmlrpc($method, $args = array()) {
- // Retrieve the severs or read them from a stored variable.
- $servers = $this->_get_endpoints();
- // Do the actual call.
- return $this->_xmlrpc_request($method, $args, $servers);
- }
- /**
- * Gets the form for the settings for this destination.
- */
- public function _xmlrpc_request($method, $args, $servers, $retry = 3) {
- if ($servers && --$retry > 0) {
- // Add the key authentication arguments if we can.
- if ($this->_sign_request($args)) {
- $url = reset($servers);
- // Try each available server in order.
- while ($url) {
- $url = $this->add_scheme($url);
- $options = array();
- // Optionally disable SSL certification validation. This may be
- // necessary if the server's SSL certificates are out of date because
- // and might be incompatible with PHP 5.6's OpenSSL implementation.
- // @see http://php.net/manual/en/migration56.openssl.php
- // @see http://php.net/manual/en/context.ssl.php#context.ssl.verify-peer
- if (variable_get('nodesquirrel_failover', FALSE)) {
- $opts = array(
- 'ssl' => array(
- 'verify_peer' => FALSE,
- ),
- );
- $options = array(
- 'context' => stream_context_create($opts),
- );
- }
- $out = xmlrpc($url, array($method => $args), $options);
- // Check for errors.
- $err = xmlrpc_error();
- if ($err && $err->is_error) {
- switch ($err->code) {
- case '500':
- case '503':
- case '404':
- // Some sort of server error. Try the next one.
- $url = next($servers);
- // If we're at the end of the line then try refetching the urls.
- if (!$url) {
- $servers = $this->_get_endpoints(TRUE, $retry);
- return $this->_xmlrpc_request($method, $args, $servers, $retry);
- }
- break;
- case '300':
- // 'Multiple Choices' means that the existing server list needs
- // to be refreshed.
- $servers = $this->_get_endpoints(TRUE, $retry);
- return $this->_xmlrpc_request($method, $args, $servers, $retry);
- case '401':
- case '403':
- // Authentication failed.
- _backup_migrate_message('Couldn\'t log in to NodeSquirrel. The server error was: %err', array('%err' => $err->message), 'error');
- return FALSE;
- default:
- // Some sort of client error. Don't try the next server because
- // it'll probably say the same thing.
- _backup_migrate_message('The NodeSquirrel server returned the following error: %err', array('%err' => $err->message), 'error');
- return FALSE;
- }
- }
- // No error, return the result.
- else {
- return $out;
- }
- }
- }
- }
- }
- /**
- * Genrates a hash with a given secret key, timestamp and random value.
- */
- public function _get_hash($time, $nonce) {
- if ($private_key = $this->_get_private_key()) {
- $message = $time . ':' . $nonce . ':' . $private_key;
- // Use HMAC-SHA1 to authenticate the call.
- $hash = base64_encode(
- pack('H*', sha1((str_pad($private_key, 64, chr(0x00)) ^ (str_repeat(chr(0x5c), 64))) .
- pack('H*', sha1((str_pad($private_key, 64, chr(0x00)) ^ (str_repeat(chr(0x36), 64))) .
- $message))))
- );
- return $hash;
- }
- _backup_migrate_message('You must enter a valid secret key to use NodeSquirrel.', array(), 'error');
- return FALSE;
- }
- /**
- * Genrates a hash with a given secret key, timestamp and random value.
- */
- public function _sign_request(&$args) {
- $nonce = md5(mt_rand());
- $time = time();
- $hash = $this->_get_hash($time, $nonce);
- if ($hash) {
- array_unshift($args, $nonce);
- array_unshift($args, $time);
- array_unshift($args, $hash);
- return TRUE;
- }
- else {
- return FALSE;
- }
- }
- /**
- * Retrieves the list of servers.
- */
- public function _get_endpoints($refresh = FALSE, $retry = 3) {
- $servers = (array) variable_get('nodesquirrel_endpoint_urls', array());
- // No servers saved or a force refreshr required.
- if ($refresh || empty($servers)) {
- $servers = array_unique(array_merge($servers, variable_get('nodesquirrel_default_endpoint_urls', array('api.nodesquirrel.com/services/xmlrpc'))));
- // Call the get endpoints method but use the default or previous servers
- // to avoid infinite loops.
- $new_servers = $this->_xmlrpc_request('backups.getEndpoints', array($this->_get_destination(), 'xmlrpc'), $servers, $retry);
- if ($new_servers) {
- variable_set('nodesquirrel_endpoint_urls', $new_servers);
- $servers = $new_servers;
- }
- }
- return $servers;
- }
- /**
- * Posts a file via HTTP.
- *
- * This looks a lot like a clone of drupal_http_request() but it can post a
- * large file without reading the whole file into memory.
- */
- public function _post_file($url, $method = 'GET', $params = array(), $file = NULL, $retry = 3) {
- global $db_prefix;
- $result = new stdClass();
- // Parse the URL and make sure we can handle the schema.
- $uri = parse_url($url);
- if ($uri == FALSE) {
- $result->error = 'unable to parse URL';
- $result->code = -1001;
- return $result;
- }
- if (!isset($uri['scheme'])) {
- $result->error = 'missing schema';
- $result->code = -1002;
- return $result;
- }
- // Avoid some "undefined variable" errors.
- $errno = $errstr = NULL;
- switch ($uri['scheme']) {
- case 'http':
- case 'feed':
- $port = isset($uri['port']) ? $uri['port'] : 80;
- $host = $uri['host'] . ($port != 80 ? ':' . $port : '');
- $fp = @fsockopen($uri['host'], $port, $errno, $errstr, 15);
- break;
- case 'https':
- // Note: Only works for PHP 4.3+ compiled with OpenSSL.
- $port = isset($uri['port']) ? $uri['port'] : 443;
- $host = $uri['host'] . ($port != 443 ? ':' . $port : '');
- $fp = @fsockopen('ssl://' . $uri['host'], $port, $errno, $errstr, 20);
- // If fsockopen() doesn't work, try again after disabling SSL.
- // Optionally disable SSL certification validation. This may be
- // necessary if the server's SSL certificates are out of date and/or
- // might be incompatible with PHP 5.6's OpenSSL implementation.
- // @see http://php.net/manual/en/migration56.openssl.php
- // @see http://php.net/manual/en/context.ssl.php#context.ssl.verify-peer
- if (!$fp && variable_get('nodesquirrel_failover', FALSE)) {
- $opts = array(
- 'ssl' => array(
- 'verify_peer' => FALSE,
- ),
- );
- $context = stream_context_create($opts);
- $fp = stream_socket_client('ssl://' . $uri['host'] . ':' . $port, $errno, $errstr, 20, STREAM_CLIENT_CONNECT, $context);
- }
- break;
- default:
- $result->error = 'invalid schema ' . $uri['scheme'];
- $result->code = -1003;
- return $result;
- }
- // Make sure the socket opened properly.
- if (!$fp) {
- // When a network error occurs, we use a negative number so it does not
- // clash with the HTTP status codes.
- $result->code = -$errno;
- $result->error = trim($errstr);
- // Mark that this request failed. This will trigger a check of the web
- // server's ability to make outgoing HTTP requests the next time that
- // requirements checking is performed.
- // @see system_requirements()
- variable_set('drupal_http_request_fails', TRUE);
- return $result;
- }
- // Construct the path to act on.
- $path = isset($uri['path']) ? $uri['path'] : '/';
- if (isset($uri['query'])) {
- $path .= '?' . $uri['query'];
- }
- // Prepare the data payload.
- $boundary = '---------------------------' . substr(md5(rand(0, 32000)), 0, 10);
- $data_footer = "\r\n--$boundary--\r\n";
- $data_header = '';
- foreach ($params as $key => $value) {
- $data_header .= "--$boundary\r\n";
- $data_header .= "Content-Disposition: form-data; name=\"" . $key . "\"\r\n";
- $data_header .= "\r\n" . $value . "\r\n";
- $data_header .= "--$boundary\r\n";
- }
- // Add the file header to the post payload.
- $data_header .= "--$boundary\r\n";
- $data_header .= "Content-Disposition: form-data; name=\"file\"; filename=\"" . $file->filename() . "\"\r\n";
- $data_header .= "Content-Type: application/octet-stream;\r\n";
- $data_header .= "\r\n";
- // Calculate the content length.
- $content_length = strlen($data_header . $data_footer) + filesize($file->filepath());
- // file_get_contents($file->filepath()));
- // Create HTTP request.
- $defaults = array(
- // RFC 2616: "non-standard ports MUST, default ports MAY be included".
- // We don't add the port to prevent from breaking rewrite rules checking
- // the host that do not take into account the port number.
- 'Host' => "Host: $host",
- 'Content-type' => "Content-type: multipart/form-data, boundary=$boundary",
- 'User-Agent' => 'User-Agent: NodeSquirrel Client/1.x (+http://www.nodesquirrel.com) (Drupal ' . VERSION . '; Backup and Migrate 2.x)',
- 'Content-Length' => 'Content-Length: ' . $content_length,
- );
- // If the server url has a user then attempt to use basic authentication.
- if (isset($uri['user'])) {
- $defaults['Authorization'] = 'Authorization: Basic ' . base64_encode($uri['user'] . (!empty($uri['pass']) ? ":" . $uri['pass'] : ''));
- }
- $request = $method . ' ' . $path . " HTTP/1.0\r\n";
- $request .= implode("\r\n", $defaults);
- $request .= "\r\n\r\n";
- $result->request = $request;
- $out = FALSE;
- // Write the headers and start of the headers
- if (($fp_in = fopen($file->filepath(), 'rb')) && fwrite($fp, $request) && fwrite($fp, $data_header)) {
- // Copy the file 1M at a time.
- do {
- $data = fread($fp_in, 1024 * 1024);
- $size = strlen($data);
- $sent = 0;
- while ($sent < $size && ($out = fwrite($fp, substr($data, $sent)))) {
- $sent += $out;
- }
- } while ($out && !feof($fp_in));
- // Finish the write.
- if ($out) {
- $out = fwrite($fp, $data_footer);
- }
- }
- @fclose($fp_in);
- // Check for errors.
- if (!$out) {
- watchdog('backup_migrate', 'NodeSquirrel: network write failed', NULL, WATCHDOG_ERROR);
- $result->code = -$errno;
- $result->error = trim($errstr);
- return $result;
- }
- // Fetch response.
- $response = '';
- while (!feof($fp) && $chunk = fread($fp, 1024)) {
- $response .= $chunk;
- }
- fclose($fp);
- if (variable_get('debug_http_request', FALSE)) {
- drupal_debug(date('r'));
- drupal_debug($request);
- drupal_debug($response);
- }
- // Parse response.
- list($split, $result->data) = explode("\r\n\r\n", $response, 2);
- $split = preg_split("/\r\n|\n|\r/", $split);
- list($protocol, $code, $status_message) = explode(' ', trim(array_shift($split)), 3);
- $result->protocol = $protocol;
- $result->status_message = $status_message;
- $result->headers = array();
- // Parse headers.
- while ($line = trim(array_shift($split))) {
- list($header, $value) = explode(':', $line, 2);
- if (isset($result->headers[$header]) && $header == 'Set-Cookie') {
- // RFC 2109: the Set-Cookie response header comprises the token Set-
- // Cookie:, followed by a comma-separated list of one or more cookies.
- $result->headers[$header] .= ',' . trim($value);
- }
- else {
- $result->headers[$header] = trim($value);
- }
- }
- $responses = array(
- 100 => 'Continue',
- 101 => 'Switching Protocols',
- 200 => 'OK',
- 201 => 'Created',
- 202 => 'Accepted',
- 203 => 'Non-Authoritative Information',
- 204 => 'No Content',
- 205 => 'Reset Content',
- 206 => 'Partial Content',
- 300 => 'Multiple Choices',
- 301 => 'Moved Permanently',
- 302 => 'Found',
- 303 => 'See Other',
- 304 => 'Not Modified',
- 305 => 'Use Proxy',
- 307 => 'Temporary Redirect',
- 400 => 'Bad Request',
- 401 => 'Unauthorized',
- 402 => 'Payment Required',
- 403 => 'Forbidden',
- 404 => 'Not Found',
- 405 => 'Method Not Allowed',
- 406 => 'Not Acceptable',
- 407 => 'Proxy Authentication Required',
- 408 => 'Request Time-out',
- 409 => 'Conflict',
- 410 => 'Gone',
- 411 => 'Length Required',
- 412 => 'Precondition Failed',
- 413 => 'Request Entity Too Large',
- 414 => 'Request-URI Too Large',
- 415 => 'Unsupported Media Type',
- 416 => 'Requested range not satisfiable',
- 417 => 'Expectation Failed',
- 500 => 'Internal Server Error',
- 501 => 'Not Implemented',
- 502 => 'Bad Gateway',
- 503 => 'Service Unavailable',
- 504 => 'Gateway Time-out',
- 505 => 'HTTP Version not supported',
- );
- // RFC 2616 states that all unknown HTTP codes must be treated the same as
- // the base code in their class.
- if (!isset($responses[$code])) {
- $code = floor($code / 100) * 100;
- }
- switch ($code) {
- case 200:
- // OK.
- case 304:
- // Not modified.
- break;
- case 301:
- // Moved permanently.
- case 302:
- // Moved temporarily.
- case 307:
- // Moved temporarily.
- $location = $result->headers['Location'];
- if ($retry) {
- $result = drupal_http_request($result->headers['Location'], $headers, $method, $data, --$retry);
- $result->redirect_code = $result->code;
- }
- $result->redirect_url = $location;
- break;
- default:
- $result->error = $status_message;
- }
- $result->code = $code;
- return $result;
- }
- }
|