View source
<?php
backup_migrate_include('crud');
function backup_migrate_get_location_subtypes() {
backup_migrate_include('crud', 'destinations', 'sources');
return backup_migrate_crud_subtypes('destination') + backup_migrate_crud_subtypes('source');
}
function backup_migrate_backup_migrate_locations() {
$out = array();
backup_migrate_include('filters');
$out += backup_migrate_filters_invoke_all('locations');
return $out;
}
function backup_migrate_get_locations($op = 'all') {
static $locations = NULL;
if ($locations === NULL) {
$locations = backup_migrate_crud_get_items('location');
}
if ($op == 'all') {
return $locations;
}
$out = array();
if ($locations) {
foreach ($locations as $key => $location) {
if ($location
->op($op)) {
$out[$key] = $location;
}
}
}
return $out;
}
function backup_migrate_get_location($id) {
$locations = backup_migrate_get_locations('all');
return empty($locations[$id]) ? NULL : $locations[$id];
}
class backup_migrate_location extends backup_migrate_item {
var $db_table = "backup_migrate_destinations";
var $type_name = "location";
var $default_values = array(
'settings' => array(),
);
var $singular = 'location';
var $plural = 'locations';
var $title_plural = 'Locations';
var $title_singular = 'Location';
var $subtype = "";
var $supported_ops = array();
function strings() {
t('location');
t('locations');
t('Location');
t('Locations');
}
function ops() {
return $this->supported_ops;
}
function op($op) {
$ops = (array) $this
->ops();
return in_array($op, $ops);
}
function remove_op($op) {
$key = array_search($op, $this->supported_ops);
if ($key !== FALSE) {
unset($this->supported_ops[$key]);
}
}
function get_name() {
return @$this->name;
}
function set_name($name) {
return $this->name = $name;
}
function set_location($location) {
$this->location = $location;
}
function get_location() {
return @$this->location;
}
function get_display_location() {
return $this
->get_location();
}
function settings($key = NULL) {
$out = $this->settings;
if ($key) {
$out = isset($out[$key]) ? $out[$key] : NULL;
}
return $out;
}
function get_subtype_name() {
if ($type = $this
->get('subtype')) {
$types = $this
->location_types();
return isset($types[$type]['type_name']) ? $types[$type]['type_name'] : $type;
}
}
function edit_form() {
if (!empty($this->supported_ops)) {
$form = parent::edit_form();
$form['subtype'] = array(
"#type" => "value",
"#default_value" => $this
->get('subtype'),
);
}
else {
$types = $this
->location_types();
$items = array();
foreach ($types as $key => $type) {
if (@$type['can_create']) {
$type_url_str = str_replace('_', '-', $key);
$out = '<dt>' . l($type['type_name'], BACKUP_MIGRATE_MENU_PATH . "/settings/{$this->type_name}/add/{$type_url_str}", array(
'attributes' => array(
'title' => t('Add a new @s location.', array(
'@s' => $type['type_name'],
)),
),
)) . '</dt>';
$out .= '<dd>' . filter_xss_admin($type['description']) . '</dd>';
$items[] = $out;
}
}
if (count($items)) {
$output = t('Choose the type of location you would like to create:') . '<dl>' . implode('', $items) . '</dl>';
}
else {
$output = t('No types available.');
}
$form['select_type'] = array(
'#type' => 'markup',
'#markup' => $output,
);
}
return $form;
}
function location_types() {
return backup_migrate_get_location_subtypes();
}
function delete_confirm_message() {
return t('Are you sure you want to delete the %name?', array(
'%name' => $this
->get_name(),
));
}
function get_list_column_info() {
$out = parent::get_list_column_info();
$out = array(
'name' => array(
'title' => t('Name'),
),
'subtype_name' => array(
'title' => t('Type'),
),
'display_location' => array(
'title' => t('Location'),
),
) + $out;
return $out;
}
function get_list_row() {
$out = parent::get_list_row();
if (empty($out['actions'])) {
return NULL;
}
return $out;
}
function get_action_links() {
$out = parent::get_action_links();
$item_id = $this
->get_id();
if ($this
->op('list files') && user_access("access backup files")) {
$out = array(
'list files' => l(t("list files"), BACKUP_MIGRATE_MENU_PATH . "/{$this->type_name}/list/files/" . $item_id),
) + $out;
}
if (!$this
->op('configure') || !user_access('administer backup and migrate')) {
unset($out['edit']);
}
return $out;
}
function can_read_file($file_id) {
return $this
->op('restore');
}
function settings_default() {
return array();
}
function settings_form($form) {
return $form;
}
function settings_form_validate($form_values) {
}
function settings_form_submit($form_values) {
return $form_values;
}
function backup_settings_default() {
return array();
}
function backup_settings_form($settings) {
return array();
}
function backup_settings_form_validate($form, &$form_state) {
}
function backup_settings_form_submit($form, &$form_state) {
}
function restore_settings_default() {
return array();
}
function restore_settings_form($settings) {
return array();
}
function restore_settings_form_validate($form_values) {
}
function restore_settings_form_submit($form_values) {
return $form_values;
}
function create($params = array()) {
$out = NULL;
$types = backup_migrate_get_location_subtypes();
$location_type = !empty($params['subtype']) ? $params['subtype'] : NULL;
if ($location_type && ($type = @$types[$location_type])) {
if (!empty($type['file'])) {
require_once './' . $type['file'];
}
$out = new $type['class']($params + array(
'subtype' => $location_type,
));
}
if (empty($out)) {
$out = parent::create($params);
}
return $out;
}
function url($hide_password = TRUE) {
return $this
->glue_url($this->dest_url, $hide_password);
}
function glue_url($parts, $hide_password = TRUE) {
$parts['pass'] = $hide_password ? "" : $parts['pass'];
$out = "";
$out .= $parts['scheme'] . '://';
$out .= $parts['user'] ? urlencode($parts['user']) : '';
$out .= $parts['user'] && $parts['pass'] ? ":" . urlencode($parts['pass']) : '';
$out .= $parts['user'] || $parts['pass'] ? "@" : "";
$out .= $parts['host'];
$out .= !empty($parts['port']) ? ':' . $parts['port'] : '';
$out .= "/" . $parts['path'];
return $out;
}
function set_url($url) {
$parts = (array) parse_url($url);
$parts['user'] = urldecode(@$parts['user']);
$parts['pass'] = urldecode(@$parts['pass']);
$parts['path'] = urldecode(@$parts['path']);
$parts['path'] = ltrim(@$parts['path'], "/");
$this->dest_url = $parts;
}
function file_types() {
return array();
}
}
class backup_migrate_location_remote extends backup_migrate_location {
function get_location() {
return $this
->url(FALSE);
}
function get_display_location() {
return $this
->url(TRUE);
}
function set_location($location) {
$this->location = $location;
$this
->set_url($location);
}
function url($hide_password = TRUE) {
return $this
->glue_url($this->dest_url, $hide_password);
}
function glue_url($parts, $hide_password = TRUE) {
$parts['pass'] = $hide_password ? "" : $parts['pass'];
$out = "";
$out .= $parts['scheme'] . '://';
$out .= $parts['user'] ? urlencode($parts['user']) : '';
$out .= $parts['user'] && $parts['pass'] ? ":" . urlencode($parts['pass']) : '';
$out .= $parts['user'] || $parts['pass'] ? "@" : "";
$out .= $parts['host'];
$out .= !empty($parts['port']) ? ':' . $parts['port'] : '';
$out .= "/" . $parts['path'];
return $out;
}
function set_url($url) {
$parts = (array) parse_url($url);
$parts['user'] = urldecode(@$parts['user']);
$parts['pass'] = urldecode(@$parts['pass']);
$parts['path'] = urldecode(@$parts['path']);
$parts['path'] = ltrim(@$parts['path'], "/");
$this->dest_url = $parts;
}
function edit_form() {
$form = parent::edit_form();
$form['scheme'] = array(
"#type" => "select",
"#title" => t("Scheme"),
"#default_value" => @$this->dest_url['scheme'] ? $this->dest_url['scheme'] : 'mysql',
"#required" => TRUE,
"#options" => array(
$GLOBALS['db_type'] => $GLOBALS['db_type'],
),
"#weight" => 0,
);
$form['host'] = array(
"#type" => "textfield",
"#title" => t("Host"),
"#default_value" => @$this->dest_url['host'] ? $this->dest_url['host'] : 'localhost',
"#required" => TRUE,
"#weight" => 10,
);
$form['path'] = array(
"#type" => "textfield",
"#title" => t("Path"),
"#default_value" => @$this->dest_url['path'],
"#required" => TRUE,
"#weight" => 20,
);
$form['user'] = array(
"#type" => "textfield",
"#title" => t("Username"),
"#default_value" => @$this->dest_url['user'],
"#required" => TRUE,
"#weight" => 30,
);
$form['pass'] = array(
"#type" => "password",
"#title" => t("Password"),
"#default_value" => @$this->dest_url['pass'],
'#description' => '',
"#weight" => 40,
);
if (@$this->dest_url['pass']) {
$form['old_password'] = array(
"#type" => "value",
"#value" => @$this->dest_url['pass'],
);
$form['pass']["#description"] .= t(' You do not need to enter a password unless you wish to change the currently saved password.');
}
return $form;
}
function edit_form_submit($form, &$form_state) {
$form_state['values']['pass'] = $form_state['values']['pass'] ? $form_state['values']['pass'] : $form_state['values']['old_password'];
$form_state['values']['location'] = $this
->glue_url($form_state['values'], FALSE);
parent::edit_form_submit($form, $form_state);
}
}