PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Service/Rackspace/Abstract.php

https://bitbucket.org/bigstylee/zend-framework
PHP | 379 lines | 191 code | 5 blank | 183 comment | 23 complexity | 17e24ae7236b5b51924cb93838e092b9 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage Rackspace
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. require_once 'Zend/Http/Client.php';
  22. abstract class Zend_Service_Rackspace_Abstract
  23. {
  24. const VERSION = 'v1.0';
  25. const US_AUTH_URL = 'https://auth.api.rackspacecloud.com';
  26. const UK_AUTH_URL = 'https://lon.auth.api.rackspacecloud.com';
  27. const API_FORMAT = 'json';
  28. const USER_AGENT = 'Zend_Service_Rackspace';
  29. const STORAGE_URL = "X-Storage-Url";
  30. const AUTHTOKEN = "X-Auth-Token";
  31. const AUTHUSER_HEADER = "X-Auth-User";
  32. const AUTHKEY_HEADER = "X-Auth-Key";
  33. const AUTHUSER_HEADER_LEGACY = "X-Storage-User";
  34. const AUTHKEY_HEADER_LEGACY = "X-Storage-Pass";
  35. const AUTHTOKEN_LEGACY = "X-Storage-Token";
  36. const CDNM_URL = "X-CDN-Management-Url";
  37. const MANAGEMENT_URL = "X-Server-Management-Url";
  38. /**
  39. * Rackspace Key
  40. *
  41. * @var string
  42. */
  43. protected $key;
  44. /**
  45. * Rackspace account name
  46. *
  47. * @var string
  48. */
  49. protected $user;
  50. /**
  51. * Token of authentication
  52. *
  53. * @var string
  54. */
  55. protected $token;
  56. /**
  57. * Authentication URL
  58. *
  59. * @var string
  60. */
  61. protected $authUrl;
  62. /**
  63. * @var Zend_Http_Client
  64. */
  65. protected $httpClient;
  66. /**
  67. * Error Msg
  68. *
  69. * @var string
  70. */
  71. protected $errorMsg;
  72. /**
  73. * HTTP error code
  74. *
  75. * @var string
  76. */
  77. protected $errorCode;
  78. /**
  79. * Storage URL
  80. *
  81. * @var string
  82. */
  83. protected $storageUrl;
  84. /**
  85. * CDN URL
  86. *
  87. * @var string
  88. */
  89. protected $cdnUrl;
  90. /**
  91. * Server management URL
  92. *
  93. * @var string
  94. */
  95. protected $managementUrl;
  96. /**
  97. * Do we use ServiceNet?
  98. *
  99. * @var boolean
  100. */
  101. protected $useServiceNet = false;
  102. /**
  103. * Constructor
  104. *
  105. * You must pass the account and the Rackspace authentication key.
  106. * Optional: the authentication url (default is US)
  107. *
  108. * @param string $user
  109. * @param string $key
  110. * @param string $authUrl
  111. */
  112. public function __construct($user, $key, $authUrl=self::US_AUTH_URL)
  113. {
  114. if (!isset($user)) {
  115. require_once 'Zend/Service/Rackspace/Exception.php';
  116. throw new Zend_Service_Rackspace_Exception("The user cannot be empty");
  117. }
  118. if (!isset($key)) {
  119. require_once 'Zend/Service/Rackspace/Exception.php';
  120. throw new Zend_Service_Rackspace_Exception("The key cannot be empty");
  121. }
  122. if (!in_array($authUrl, array(self::US_AUTH_URL, self::UK_AUTH_URL))) {
  123. require_once 'Zend/Service/Rackspace/Exception.php';
  124. throw new Zend_Service_Rackspace_Exception("The authentication URL should be valid");
  125. }
  126. $this->setUser($user);
  127. $this->setKey($key);
  128. $this->setAuthUrl($authUrl);
  129. }
  130. /**
  131. * Get User account
  132. *
  133. * @return string
  134. */
  135. public function getUser()
  136. {
  137. return $this->user;
  138. }
  139. /**
  140. * Get user key
  141. *
  142. * @return string
  143. */
  144. public function getKey()
  145. {
  146. return $this->key;
  147. }
  148. /**
  149. * Get authentication URL
  150. *
  151. * @return string
  152. */
  153. public function getAuthUrl()
  154. {
  155. return $this->authUrl;
  156. }
  157. /**
  158. * Get the storage URL
  159. *
  160. * @return string|boolean
  161. */
  162. public function getStorageUrl()
  163. {
  164. if (empty($this->storageUrl)) {
  165. if (!$this->authenticate()) {
  166. return false;
  167. }
  168. }
  169. return $this->storageUrl;
  170. }
  171. /**
  172. * Get the CDN URL
  173. *
  174. * @return string|boolean
  175. */
  176. public function getCdnUrl()
  177. {
  178. if (empty($this->cdnUrl)) {
  179. if (!$this->authenticate()) {
  180. return false;
  181. }
  182. }
  183. return $this->cdnUrl;
  184. }
  185. /**
  186. * Get the management server URL
  187. *
  188. * @return string|boolean
  189. */
  190. public function getManagementUrl()
  191. {
  192. if (empty($this->managementUrl)) {
  193. if (!$this->authenticate()) {
  194. return false;
  195. }
  196. }
  197. return $this->managementUrl;
  198. }
  199. /**
  200. * Set the user account
  201. *
  202. * @param string $user
  203. * @return void
  204. */
  205. public function setUser($user)
  206. {
  207. if (!empty($user)) {
  208. $this->user = $user;
  209. }
  210. }
  211. /**
  212. * Set the authentication key
  213. *
  214. * @param string $key
  215. * @return void
  216. */
  217. public function setKey($key)
  218. {
  219. if (!empty($key)) {
  220. $this->key = $key;
  221. }
  222. }
  223. /**
  224. * Set the Authentication URL
  225. *
  226. * @param string $url
  227. * @return void
  228. */
  229. public function setAuthUrl($url)
  230. {
  231. if (!empty($url) && in_array($url, array(self::US_AUTH_URL, self::UK_AUTH_URL))) {
  232. $this->authUrl = $url;
  233. } else {
  234. require_once 'Zend/Service/Rackspace/Exception.php';
  235. throw new Zend_Service_Rackspace_Exception("The authentication URL is not valid");
  236. }
  237. }
  238. /**
  239. * Sets whether to use ServiceNet
  240. *
  241. * ServiceNet is Rackspace's internal network. Bandwidth on ServiceNet is
  242. * not charged.
  243. *
  244. * @param boolean $useServiceNet
  245. */
  246. public function setServiceNet($useServiceNet = true)
  247. {
  248. $this->useServiceNet = $useServiceNet;
  249. return $this;
  250. }
  251. /**
  252. * Get whether we're using ServiceNet
  253. *
  254. * @return boolean
  255. */
  256. public function getServiceNet()
  257. {
  258. return $this->useServiceNet;
  259. }
  260. /**
  261. * Get the authentication token
  262. *
  263. * @return string
  264. */
  265. public function getToken()
  266. {
  267. if (empty($this->token)) {
  268. if (!$this->authenticate()) {
  269. return false;
  270. }
  271. }
  272. return $this->token;
  273. }
  274. /**
  275. * Get the error msg of the last HTTP call
  276. *
  277. * @return string
  278. */
  279. public function getErrorMsg()
  280. {
  281. return $this->errorMsg;
  282. }
  283. /**
  284. * Get the error code of the last HTTP call
  285. *
  286. * @return strig
  287. */
  288. public function getErrorCode()
  289. {
  290. return $this->errorCode;
  291. }
  292. /**
  293. * get the HttpClient instance
  294. *
  295. * @return Zend_Http_Client
  296. */
  297. public function getHttpClient()
  298. {
  299. if (empty($this->httpClient)) {
  300. $this->httpClient = new Zend_Http_Client();
  301. }
  302. return $this->httpClient;
  303. }
  304. /**
  305. * Return true is the last call was successful
  306. *
  307. * @return boolean
  308. */
  309. public function isSuccessful()
  310. {
  311. return ($this->errorMsg=='');
  312. }
  313. /**
  314. * HTTP call
  315. *
  316. * @param string $url
  317. * @param string $method
  318. * @param array $headers
  319. * @param array $get
  320. * @param string $body
  321. * @return Zend_Http_Response
  322. */
  323. protected function httpCall($url,$method,$headers=array(),$data=array(),$body=null)
  324. {
  325. $client = $this->getHttpClient();
  326. $client->resetParameters(true);
  327. if (empty($headers[self::AUTHUSER_HEADER])) {
  328. $headers[self::AUTHTOKEN]= $this->getToken();
  329. }
  330. $client->setMethod($method);
  331. if (empty($data['format'])) {
  332. $data['format']= self::API_FORMAT;
  333. }
  334. $client->setParameterGet($data);
  335. if (!empty($body)) {
  336. $client->setRawData($body);
  337. if (!isset($headers['Content-Type'])) {
  338. $headers['Content-Type']= 'application/json';
  339. }
  340. }
  341. $client->setHeaders($headers);
  342. $client->setUri($url);
  343. $this->errorMsg='';
  344. $this->errorCode='';
  345. return $client->request();
  346. }
  347. /**
  348. * Authentication
  349. *
  350. * @return boolean
  351. */
  352. public function authenticate()
  353. {
  354. $headers = array (
  355. self::AUTHUSER_HEADER => $this->user,
  356. self::AUTHKEY_HEADER => $this->key
  357. );
  358. $result = $this->httpCall($this->authUrl.'/'.self::VERSION,'GET', $headers);
  359. if ($result->getStatus()==204) {
  360. $this->token = $result->getHeader(self::AUTHTOKEN);
  361. $this->cdnUrl = $result->getHeader(self::CDNM_URL);
  362. $this->managementUrl = $result->getHeader(self::MANAGEMENT_URL);
  363. $storageUrl = $result->getHeader(self::STORAGE_URL);
  364. if ($this->useServiceNet) {
  365. $storageUrl = preg_replace('|(.*)://([^/]*)(.*)|', '$1://snet-$2$3', $storageUrl);
  366. }
  367. $this->storageUrl = $storageUrl;
  368. return true;
  369. }
  370. $this->errorMsg = $result->getBody();
  371. $this->errorCode = $result->getStatus();
  372. return false;
  373. }
  374. }