View source
<?php
require_once dirname(__FILE__) . '/crud.inc';
function backup_migrate_get_location_subtypes() {
require_once dirname(__FILE__) . '/crud.inc';
require_once dirname(__FILE__) . '/destinations.inc';
require_once dirname(__FILE__) . '/sources.inc';
return backup_migrate_crud_subtypes('destination') + backup_migrate_crud_subtypes('source');
}
function backup_migrate_backup_migrate_locations() {
require_once dirname(__FILE__) . '/filters.inc';
$out = array();
$out += backup_migrate_filters_invoke_all('locations');
return $out;
}
function backup_migrate_get_locations($op = 'all') {
$locations =& drupal_static('backup_migrate_get_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 {
public $db_table = "backup_migrate_destinations";
public $type_name = "location";
public $default_values = array(
'settings' => array(),
);
public $singular = 'location';
public $plural = 'locations';
public $title_plural = 'Locations';
public $title_singular = 'Location';
public $subtype = "";
public $supported_ops = array();
public function strings() {
t('location');
t('locations');
t('Location');
t('Locations');
}
public function ops() {
return $this->supported_ops;
}
public function op($op) {
$ops = (array) $this
->ops();
return in_array($op, $ops);
}
public function remove_op($op) {
$key = array_search($op, $this->supported_ops);
if ($key !== FALSE) {
unset($this->supported_ops[$key]);
}
}
public function get_name() {
return @$this->name;
}
public function set_name($name) {
return $this->name = $name;
}
public function set_location($location) {
$this->location = $location;
}
public function get_location() {
return @$this->location;
}
public function get_display_location() {
return $this
->get_location();
}
public function settings($key = NULL) {
$out = $this->settings;
if ($key) {
$out = isset($out[$key]) ? $out[$key] : NULL;
}
return $out;
}
public function get_subtype_name() {
if ($type = $this
->get('subtype')) {
$types = $this
->location_types();
return isset($types[$type]['type_name']) ? $types[$type]['type_name'] : $type;
}
}
public 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;
}
public function location_types() {
return backup_migrate_get_location_subtypes();
}
public function delete_confirm_message() {
return t('Are you sure you want to delete the %name?', array(
'%name' => $this
->get_name(),
));
}
public 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;
}
public function get_list_row() {
$out = parent::get_list_row();
if (empty($out['actions'])) {
return NULL;
}
return $out;
}
public 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;
}
public function can_read_file($file_id) {
return $this
->op('restore');
}
public function settings_default() {
return array();
}
public function settings_form($form) {
return $form;
}
public function settings_form_validate($form_values) {
}
public function settings_form_submit($form_values) {
return $form_values;
}
public function backup_settings_default() {
return array();
}
public function backup_settings_form($settings) {
return array();
}
public function backup_settings_form_validate($form, &$form_state) {
}
public function backup_settings_form_submit($form, &$form_state) {
}
public function restore_settings_default() {
return array();
}
public function restore_settings_form($settings) {
return array();
}
public function restore_settings_form_validate($form_values) {
}
public function restore_settings_form_submit($form_values) {
return $form_values;
}
public 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;
}
public function url($hide_password = TRUE) {
return $this
->glue_url($this->dest_url, $hide_password);
}
public 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;
}
public 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;
}
public function file_types() {
return array();
}
}
class backup_migrate_location_remote extends backup_migrate_location {
public function get_location() {
return $this
->url(FALSE);
}
public function get_display_location() {
return $this
->url(TRUE);
}
public function set_location($location) {
$this->location = $location;
$this
->set_url($location);
}
public 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;
}
public 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);
}
}