File indexing completed on 2025-01-26 05:25:27
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Service 0017 * @subpackage Rackspace 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 */ 0021 0022 // require_once 'Zend/Http/Client.php'; 0023 0024 abstract class Zend_Service_Rackspace_Abstract 0025 { 0026 const VERSION = 'v1.0'; 0027 const US_AUTH_URL = 'https://auth.api.rackspacecloud.com'; 0028 const UK_AUTH_URL = 'https://lon.auth.api.rackspacecloud.com'; 0029 const API_FORMAT = 'json'; 0030 const USER_AGENT = 'Zend_Service_Rackspace'; 0031 const STORAGE_URL = "X-Storage-Url"; 0032 const AUTHTOKEN = "X-Auth-Token"; 0033 const AUTHUSER_HEADER = "X-Auth-User"; 0034 const AUTHKEY_HEADER = "X-Auth-Key"; 0035 const AUTHUSER_HEADER_LEGACY = "X-Storage-User"; 0036 const AUTHKEY_HEADER_LEGACY = "X-Storage-Pass"; 0037 const AUTHTOKEN_LEGACY = "X-Storage-Token"; 0038 const CDNM_URL = "X-CDN-Management-Url"; 0039 const MANAGEMENT_URL = "X-Server-Management-Url"; 0040 /** 0041 * Rackspace Key 0042 * 0043 * @var string 0044 */ 0045 protected $key; 0046 /** 0047 * Rackspace account name 0048 * 0049 * @var string 0050 */ 0051 protected $user; 0052 /** 0053 * Token of authentication 0054 * 0055 * @var string 0056 */ 0057 protected $token; 0058 /** 0059 * Authentication URL 0060 * 0061 * @var string 0062 */ 0063 protected $authUrl; 0064 /** 0065 * @var Zend_Http_Client 0066 */ 0067 protected $httpClient; 0068 /** 0069 * Error Msg 0070 * 0071 * @var string 0072 */ 0073 protected $errorMsg; 0074 /** 0075 * HTTP error code 0076 * 0077 * @var string 0078 */ 0079 protected $errorCode; 0080 /** 0081 * Storage URL 0082 * 0083 * @var string 0084 */ 0085 protected $storageUrl; 0086 /** 0087 * CDN URL 0088 * 0089 * @var string 0090 */ 0091 protected $cdnUrl; 0092 /** 0093 * Server management URL 0094 * 0095 * @var string 0096 */ 0097 protected $managementUrl; 0098 /** 0099 * Do we use ServiceNet? 0100 * 0101 * @var boolean 0102 */ 0103 protected $useServiceNet = false; 0104 /** 0105 * Constructor 0106 * 0107 * You must pass the account and the Rackspace authentication key. 0108 * Optional: the authentication url (default is US) 0109 * 0110 * @param string $user 0111 * @param string $key 0112 * @param string $authUrl 0113 */ 0114 public function __construct($user, $key, $authUrl=self::US_AUTH_URL) 0115 { 0116 if (!isset($user)) { 0117 // require_once 'Zend/Service/Rackspace/Exception.php'; 0118 throw new Zend_Service_Rackspace_Exception("The user cannot be empty"); 0119 } 0120 if (!isset($key)) { 0121 // require_once 'Zend/Service/Rackspace/Exception.php'; 0122 throw new Zend_Service_Rackspace_Exception("The key cannot be empty"); 0123 } 0124 if (!in_array($authUrl, array(self::US_AUTH_URL, self::UK_AUTH_URL))) { 0125 // require_once 'Zend/Service/Rackspace/Exception.php'; 0126 throw new Zend_Service_Rackspace_Exception("The authentication URL should be valid"); 0127 } 0128 $this->setUser($user); 0129 $this->setKey($key); 0130 $this->setAuthUrl($authUrl); 0131 } 0132 /** 0133 * Get User account 0134 * 0135 * @return string 0136 */ 0137 public function getUser() 0138 { 0139 return $this->user; 0140 } 0141 /** 0142 * Get user key 0143 * 0144 * @return string 0145 */ 0146 public function getKey() 0147 { 0148 return $this->key; 0149 } 0150 /** 0151 * Get authentication URL 0152 * 0153 * @return string 0154 */ 0155 public function getAuthUrl() 0156 { 0157 return $this->authUrl; 0158 } 0159 /** 0160 * Get the storage URL 0161 * 0162 * @return string|boolean 0163 */ 0164 public function getStorageUrl() 0165 { 0166 if (empty($this->storageUrl)) { 0167 if (!$this->authenticate()) { 0168 return false; 0169 } 0170 } 0171 return $this->storageUrl; 0172 } 0173 /** 0174 * Get the CDN URL 0175 * 0176 * @return string|boolean 0177 */ 0178 public function getCdnUrl() 0179 { 0180 if (empty($this->cdnUrl)) { 0181 if (!$this->authenticate()) { 0182 return false; 0183 } 0184 } 0185 return $this->cdnUrl; 0186 } 0187 /** 0188 * Get the management server URL 0189 * 0190 * @return string|boolean 0191 */ 0192 public function getManagementUrl() 0193 { 0194 if (empty($this->managementUrl)) { 0195 if (!$this->authenticate()) { 0196 return false; 0197 } 0198 } 0199 return $this->managementUrl; 0200 } 0201 /** 0202 * Set the user account 0203 * 0204 * @param string $user 0205 * @return void 0206 */ 0207 public function setUser($user) 0208 { 0209 if (!empty($user)) { 0210 $this->user = $user; 0211 } 0212 } 0213 /** 0214 * Set the authentication key 0215 * 0216 * @param string $key 0217 * @return void 0218 */ 0219 public function setKey($key) 0220 { 0221 if (!empty($key)) { 0222 $this->key = $key; 0223 } 0224 } 0225 /** 0226 * Set the Authentication URL 0227 * 0228 * @param string $url 0229 * @return void 0230 */ 0231 public function setAuthUrl($url) 0232 { 0233 if (!empty($url) && in_array($url, array(self::US_AUTH_URL, self::UK_AUTH_URL))) { 0234 $this->authUrl = $url; 0235 } else { 0236 // require_once 'Zend/Service/Rackspace/Exception.php'; 0237 throw new Zend_Service_Rackspace_Exception("The authentication URL is not valid"); 0238 } 0239 } 0240 0241 /** 0242 * Sets whether to use ServiceNet 0243 * 0244 * ServiceNet is Rackspace's internal network. Bandwidth on ServiceNet is 0245 * not charged. 0246 * 0247 * @param boolean $useServiceNet 0248 */ 0249 public function setServiceNet($useServiceNet = true) 0250 { 0251 $this->useServiceNet = $useServiceNet; 0252 return $this; 0253 } 0254 0255 /** 0256 * Get whether we're using ServiceNet 0257 * 0258 * @return boolean 0259 */ 0260 public function getServiceNet() 0261 { 0262 return $this->useServiceNet; 0263 } 0264 0265 /** 0266 * Get the authentication token 0267 * 0268 * @return string 0269 */ 0270 public function getToken() 0271 { 0272 if (empty($this->token)) { 0273 if (!$this->authenticate()) { 0274 return false; 0275 } 0276 } 0277 return $this->token; 0278 } 0279 /** 0280 * Get the error msg of the last HTTP call 0281 * 0282 * @return string 0283 */ 0284 public function getErrorMsg() 0285 { 0286 return $this->errorMsg; 0287 } 0288 /** 0289 * Get the error code of the last HTTP call 0290 * 0291 * @return strig 0292 */ 0293 public function getErrorCode() 0294 { 0295 return $this->errorCode; 0296 } 0297 /** 0298 * get the HttpClient instance 0299 * 0300 * @return Zend_Http_Client 0301 */ 0302 public function getHttpClient() 0303 { 0304 if (empty($this->httpClient)) { 0305 $this->httpClient = new Zend_Http_Client(); 0306 } 0307 return $this->httpClient; 0308 } 0309 /** 0310 * Return true is the last call was successful 0311 * 0312 * @return boolean 0313 */ 0314 public function isSuccessful() 0315 { 0316 return ($this->errorMsg==''); 0317 } 0318 /** 0319 * HTTP call 0320 * 0321 * @param string $url 0322 * @param string $method 0323 * @param array $headers 0324 * @param array $get 0325 * @param string $body 0326 * @return Zend_Http_Response 0327 */ 0328 protected function httpCall($url,$method,$headers=array(),$data=array(),$body=null) 0329 { 0330 $client = $this->getHttpClient(); 0331 $client->resetParameters(true); 0332 if ($method == 'PUT' && empty($body)) { 0333 // if left at NULL a PUT request will always have 0334 // Content-Type: x-url-form-encoded, which breaks copyObject() 0335 $client->setEncType(''); 0336 } 0337 if (empty($headers[self::AUTHUSER_HEADER])) { 0338 $headers[self::AUTHTOKEN]= $this->getToken(); 0339 } 0340 $client->setMethod($method); 0341 if (empty($data['format'])) { 0342 $data['format']= self::API_FORMAT; 0343 } 0344 $client->setParameterGet($data); 0345 if (!empty($body)) { 0346 $client->setRawData($body); 0347 if (!isset($headers['Content-Type'])) { 0348 $headers['Content-Type']= 'application/json'; 0349 } 0350 } 0351 $client->setHeaders($headers); 0352 $client->setUri($url); 0353 $this->errorMsg=''; 0354 $this->errorCode=''; 0355 return $client->request(); 0356 } 0357 /** 0358 * Authentication 0359 * 0360 * @return boolean 0361 */ 0362 public function authenticate() 0363 { 0364 if (empty($this->user)) { 0365 /** 0366 * @see Zend_Service_Rackspace_Exception 0367 */ 0368 // require_once 'Zend/Service/Rackspace/Exception.php'; 0369 throw new Zend_Service_Rackspace_Exception("User has not been set"); 0370 } 0371 0372 $headers = array ( 0373 self::AUTHUSER_HEADER => $this->user, 0374 self::AUTHKEY_HEADER => $this->key 0375 ); 0376 $result = $this->httpCall($this->authUrl.'/'.self::VERSION,'GET', $headers); 0377 if ($result->getStatus()==204) { 0378 $this->token = $result->getHeader(self::AUTHTOKEN); 0379 $this->cdnUrl = $result->getHeader(self::CDNM_URL); 0380 $this->managementUrl = $result->getHeader(self::MANAGEMENT_URL); 0381 $storageUrl = $result->getHeader(self::STORAGE_URL); 0382 if ($this->useServiceNet) { 0383 $storageUrl = preg_replace('|(.*)://([^/]*)(.*)|', '$1://snet-$2$3', $storageUrl); 0384 } 0385 $this->storageUrl = $storageUrl; 0386 return true; 0387 } 0388 $this->errorMsg = $result->getBody(); 0389 $this->errorCode = $result->getStatus(); 0390 return false; 0391 } 0392 }