| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('UPDRAFTPLUS_DIR')) die('No direct access allowed.'); |
| 4 |
|
| 5 |
# SDK uses namespacing - requires PHP 5.3 (actually the SDK states its requirements as 5.3.3) |
| 6 |
use OpenCloud\OpenStack; |
| 7 |
|
| 8 |
require_once(UPDRAFTPLUS_DIR.'/methods/openstack-base.php'); |
| 9 |
|
| 10 |
class UpdraftPlus_BackupModule_openstack extends UpdraftPlus_BackupModule_openstack_base { |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
# 4th parameter is a relative (to UPDRAFTPLUS_DIR) logo URL, which should begin with /, should we get approved for use of the OpenStack logo in future (have requested info) |
| 14 |
parent::__construct('openstack', 'OpenStack', 'OpenStack (Swift)', ''); |
| 15 |
} |
| 16 |
|
| 17 |
# $opts: 'tenant', 'user', 'password', 'authurl', (optional) 'region' |
| 18 |
public function get_service($opts, $useservercerts = false, $disablesslverify = null) { |
| 19 |
|
| 20 |
# 'tenant', 'user', 'password', 'authurl', 'path', (optional) 'region' |
| 21 |
extract($opts); |
| 22 |
|
| 23 |
if (null === $disablesslverify) $disablesslverify = UpdraftPlus_Options::get_updraft_option('updraft_ssl_disableverify'); |
| 24 |
|
| 25 |
if (empty($user) || empty($password) || empty($authurl)) throw new Exception(__('Authorisation failed (check your credentials)', 'updraftplus')); |
| 26 |
|
| 27 |
require_once(UPDRAFTPLUS_DIR.'/vendor/autoload.php'); |
| 28 |
global $updraftplus; |
| 29 |
$updraftplus->log("OpenStack authentication URL: ".$authurl); |
| 30 |
|
| 31 |
$client = new OpenStack($authurl, array( |
| 32 |
'username' => $user, |
| 33 |
'password' => $password, |
| 34 |
'tenantName' => $tenant |
| 35 |
)); |
| 36 |
$this->client = $client; |
| 37 |
|
| 38 |
if ($disablesslverify) { |
| 39 |
$client->setSslVerification(false); |
| 40 |
} else { |
| 41 |
if ($useservercerts) { |
| 42 |
$client->setConfig(array($client::SSL_CERT_AUTHORITY => false)); |
| 43 |
} else { |
| 44 |
$client->setSslVerification(UPDRAFTPLUS_DIR.'/includes/cacert.pem', true, 2); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
$client->authenticate(); |
| 49 |
|
| 50 |
if (empty($region)) { |
| 51 |
$catalog = $client->getCatalog(); |
| 52 |
if (!empty($catalog)) { |
| 53 |
$items = $catalog->getItems(); |
| 54 |
if (is_array($items)) { |
| 55 |
foreach ($items as $item) { |
| 56 |
$name = $item->getName(); |
| 57 |
$type = $item->getType(); |
| 58 |
if ('swift' != $name || 'object-store' != $type) continue; |
| 59 |
$eps = $item->getEndpoints(); |
| 60 |
if (!is_array($eps)) continue; |
| 61 |
foreach ($eps as $ep) { |
| 62 |
if (is_object($ep) && !empty($ep->region)) { |
| 63 |
$region = $ep->region; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
$this->region = $region; |
| 72 |
|
| 73 |
return $client->objectStoreService('swift', $region); |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
public function get_credentials() { |
| 78 |
return array('updraft_openstack'); |
| 79 |
} |
| 80 |
|
| 81 |
public function get_opts() { |
| 82 |
global $updraftplus; |
| 83 |
$opts = $updraftplus->get_job_option('updraft_openstack'); |
| 84 |
if (!is_array($opts)) $opts = array('user' => '', 'authurl' => '', 'password' => '', 'tenant' => '', 'path' => '', 'region' => ''); |
| 85 |
return $opts; |
| 86 |
} |
| 87 |
|
| 88 |
public function config_print_middlesection() { |
| 89 |
$opts = $this->get_opts(); |
| 90 |
?> |
| 91 |
<tr class="updraftplusmethod <?php echo $this->method;?>"> |
| 92 |
<th></th> |
| 93 |
<td> |
| 94 |
<p><?php _e('Get your access credentials from your OpenStack Swift provider, and then pick a container name to use for storage. This container will be created for you if it does not already exist.','updraftplus');?> <a href="https://updraftplus.com/faqs/there-appear-to-be-lots-of-extra-files-in-my-rackspace-cloud-files-container/"><?php _e('Also, you should read this important FAQ.', 'updraftplus'); ?></a></p> |
| 95 |
</td> |
| 96 |
</tr> |
| 97 |
|
| 98 |
<tr class="updraftplusmethod <?php echo $this->method;?>"> |
| 99 |
<th><?php echo ucfirst(__('authentication URI', 'updraftplus'));?>:</th> |
| 100 |
<td><input data-updraft_settings_test="authurl" type="text" autocomplete="off" style="width: 364px" id="updraft_openstack_authurl" name="updraft_openstack[authurl]" value="<?php echo htmlspecialchars($opts['authurl']) ?>" /> |
| 101 |
<br> |
| 102 |
<em><?php echo _x('This needs to be a v2 (Keystone) authentication URI; v1 (Swauth) is not supported.', 'Keystone and swauth are technical terms which cannot be translated', 'updraftplus');?></em> |
| 103 |
</td> |
| 104 |
</tr> |
| 105 |
|
| 106 |
<tr class="updraftplusmethod <?php echo $this->method;?>"> |
| 107 |
<th><a href="http://docs.openstack.org/openstack-ops/content/projects_users.html" title="<?php _e('Follow this link for more information', 'updraftplus');?>"><?php _e('Tenant', 'updraftplus');?></a>:</th> |
| 108 |
<td><input data-updraft_settings_test="tenant" type="text" autocomplete="off" style="width: 364px" id="updraft_openstack_tenant" name="updraft_openstack[tenant]" value="<?php echo htmlspecialchars($opts['tenant']) ?>" /> |
| 109 |
</td> |
| 110 |
</tr> |
| 111 |
|
| 112 |
<tr class="updraftplusmethod <?php echo $this->method;?>"> |
| 113 |
<th><?php _e('Region', 'updraftplus');?>:</th> |
| 114 |
<td><input data-updraft_settings_test="region" type="text" autocomplete="off" style="width: 364px" id="updraft_openstack_region" name="updraft_openstack[region]" value="<?php echo htmlspecialchars($opts['region']) ?>" /> |
| 115 |
<br> |
| 116 |
<em><?php _e('Leave this blank, and a default will be chosen.', 'updraftplus');?></em> |
| 117 |
</td> |
| 118 |
</tr> |
| 119 |
|
| 120 |
<tr class="updraftplusmethod <?php echo $this->method;?>"> |
| 121 |
<th><?php _e('Username', 'updraftplus');?>:</th> |
| 122 |
<td><input data-updraft_settings_test="user" type="text" autocomplete="off" style="width: 364px" id="updraft_openstack_user" name="updraft_openstack[user]" value="<?php echo htmlspecialchars($opts['user']) ?>" /> |
| 123 |
</td> |
| 124 |
</tr> |
| 125 |
|
| 126 |
<tr class="updraftplusmethod <?php echo $this->method;?>"> |
| 127 |
<th><?php _e('Password', 'updraftplus');?>:</th> |
| 128 |
<td><input data-updraft_settings_test="password" type="<?php echo apply_filters('updraftplus_admin_secret_field_type', 'password'); ?>" autocomplete="off" style="width: 364px" id="updraft_openstack_password" name="updraft_openstack[password]" value="<?php echo htmlspecialchars($opts['password']); ?>" /> |
| 129 |
</td> |
| 130 |
</tr> |
| 131 |
|
| 132 |
<tr class="updraftplusmethod <?php echo $this->method;?>"> |
| 133 |
<th><?php echo __('Container', 'updraftplus');?>:</th> |
| 134 |
<td><input data-updraft_settings_test="path" type="text" style="width: 364px" name="updraft_openstack[path]" id="updraft_openstack_path" value="<?php echo htmlspecialchars($opts['path']); ?>" /></td> |
| 135 |
</tr> |
| 136 |
<?php |
| 137 |
} |
| 138 |
|
| 139 |
public function credentials_test($posted_settings) { |
| 140 |
|
| 141 |
if (empty($posted_settings['user'])) { |
| 142 |
printf(__("Failure: No %s was given.",'updraftplus'), __('username','updraftplus')); |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
if (empty($posted_settings['password'])) { |
| 147 |
printf(__("Failure: No %s was given.",'updraftplus'), __('password','updraftplus')); |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
if (empty($posted_settings['tenant'])) { |
| 152 |
printf(__("Failure: No %s was given.",'updraftplus'), _x('tenant','"tenant" is a term used with OpenStack storage - Google for "OpenStack tenant" to get more help on its meaning', 'updraftplus')); |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
if (empty($posted_settings['authurl'])) { |
| 157 |
printf(__("Failure: No %s was given.",'updraftplus'), __('authentication URI', 'updraftplus')); |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
$opts = array( |
| 162 |
'user' => stripslashes($posted_settings['user']), |
| 163 |
'password' => stripslashes($posted_settings['password']), |
| 164 |
'authurl' => stripslashes($posted_settings['authurl']), |
| 165 |
'tenant' => stripslashes($posted_settings['tenant']), |
| 166 |
'region' => (!empty($posted_settings['region'])) ? $posted_settings['region'] : '', |
| 167 |
); |
| 168 |
|
| 169 |
$this->credentials_test_go($opts, stripslashes($posted_settings['path']), $posted_settings['useservercerts'], $posted_settings['disableverify']); |
| 170 |
} |
| 171 |
|
| 172 |
} |
| 173 |
|