Abstract.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. * Constructor
  98. *
  99. * You must pass the account and the Rackspace authentication key.
  100. * Optional: the authentication url (default is US)
  101. *
  102. * @param string $user
  103. * @param string $key
  104. * @param string $authUrl
  105. */
  106. public function __construct($user, $key, $authUrl=self::US_AUTH_URL)
  107. {
  108. if (!isset($user)) {
  109. require_once 'Zend/Service/Rackspace/Exception.php';
  110. throw new Zend_Service_Rackspace_Exception("The user cannot be empty");
  111. }
  112. if (!isset($key)) {
  113. require_once 'Zend/Service/Rackspace/Exception.php';
  114. throw new Zend_Service_Rackspace_Exception("The key cannot be empty");
  115. }
  116. if (!in_array($authUrl, array(self::US_AUTH_URL, self::UK_AUTH_URL))) {
  117. require_once 'Zend/Service/Rackspace/Exception.php';
  118. throw new Zend_Service_Rackspace_Exception("The authentication URL should be valid");
  119. }
  120. $this->setUser($user);
  121. $this->setKey($key);
  122. $this->setAuthUrl($authUrl);
  123. }
  124. /**
  125. * Get User account
  126. *
  127. * @return string
  128. */
  129. public function getUser()
  130. {
  131. return $this->user;
  132. }
  133. /**
  134. * Get user key
  135. *
  136. * @return string
  137. */
  138. public function getKey()
  139. {
  140. return $this->key;
  141. }
  142. /**
  143. * Get authentication URL
  144. *
  145. * @return string
  146. */
  147. public function getAuthUrl()
  148. {
  149. return $this->authUrl;
  150. }
  151. /**
  152. * Get the storage URL
  153. *
  154. * @return string|boolean
  155. */
  156. public function getStorageUrl()
  157. {
  158. if (empty($this->storageUrl)) {
  159. if (!$this->authenticate()) {
  160. return false;
  161. }
  162. }
  163. return $this->storageUrl;
  164. }
  165. /**
  166. * Get the CDN URL
  167. *
  168. * @return string|boolean
  169. */
  170. public function getCdnUrl()
  171. {
  172. if (empty($this->cdnUrl)) {
  173. if (!$this->authenticate()) {
  174. return false;
  175. }
  176. }
  177. return $this->cdnUrl;
  178. }
  179. /**
  180. * Get the management server URL
  181. *
  182. * @return string|boolean
  183. */
  184. public function getManagementUrl()
  185. {
  186. if (empty($this->managementUrl)) {
  187. if (!$this->authenticate()) {
  188. return false;
  189. }
  190. }
  191. return $this->managementUrl;
  192. }
  193. /**
  194. * Set the user account
  195. *
  196. * @param string $user
  197. * @return void
  198. */
  199. public function setUser($user)
  200. {
  201. if (!empty($user)) {
  202. $this->user = $user;
  203. }
  204. }
  205. /**
  206. * Set the authentication key
  207. *
  208. * @param string $key
  209. * @return void
  210. */
  211. public function setKey($key)
  212. {
  213. if (!empty($key)) {
  214. $this->key = $key;
  215. }
  216. }
  217. /**
  218. * Set the Authentication URL
  219. *
  220. * @param string $url
  221. * @return void
  222. */
  223. public function setAuthUrl($url)
  224. {
  225. if (!empty($url) && in_array($url, array(self::US_AUTH_URL, self::UK_AUTH_URL))) {
  226. $this->authUrl = $url;
  227. } else {
  228. require_once 'Zend/Service/Rackspace/Exception.php';
  229. throw new Zend_Service_Rackspace_Exception("The authentication URL is not valid");
  230. }
  231. }
  232. /**
  233. * Get the authentication token
  234. *
  235. * @return string
  236. */
  237. public function getToken()
  238. {
  239. if (empty($this->token)) {
  240. if (!$this->authenticate()) {
  241. return false;
  242. }
  243. }
  244. return $this->token;
  245. }
  246. /**
  247. * Get the error msg of the last HTTP call
  248. *
  249. * @return string
  250. */
  251. public function getErrorMsg()
  252. {
  253. return $this->errorMsg;
  254. }
  255. /**
  256. * Get the error code of the last HTTP call
  257. *
  258. * @return strig
  259. */
  260. public function getErrorCode()
  261. {
  262. return $this->errorCode;
  263. }
  264. /**
  265. * get the HttpClient instance
  266. *
  267. * @return Zend_Http_Client
  268. */
  269. public function getHttpClient()
  270. {
  271. if (empty($this->httpClient)) {
  272. $this->httpClient = new Zend_Http_Client();
  273. }
  274. return $this->httpClient;
  275. }
  276. /**
  277. * Return true is the last call was successful
  278. *
  279. * @return boolean
  280. */
  281. public function isSuccessful()
  282. {
  283. return ($this->errorMsg=='');
  284. }
  285. /**
  286. * HTTP call
  287. *
  288. * @param string $url
  289. * @param string $method
  290. * @param array $headers
  291. * @param array $get
  292. * @param string $body
  293. * @return Zend_Http_Response
  294. */
  295. protected function httpCall($url,$method,$headers=array(),$data=array(),$body=null)
  296. {
  297. $client = $this->getHttpClient();
  298. $client->resetParameters(true);
  299. if (empty($headers[self::AUTHUSER_HEADER])) {
  300. $headers[self::AUTHTOKEN]= $this->getToken();
  301. }
  302. $client->setMethod($method);
  303. if (empty($data['format'])) {
  304. $data['format']= self::API_FORMAT;
  305. }
  306. $client->setParameterGet($data);
  307. if (!empty($body)) {
  308. $client->setRawData($body);
  309. if (!isset($headers['Content-Type'])) {
  310. $headers['Content-Type']= 'application/json';
  311. }
  312. }
  313. $client->setHeaders($headers);
  314. $client->setUri($url);
  315. $this->errorMsg='';
  316. $this->errorCode='';
  317. return $client->request();
  318. }
  319. /**
  320. * Authentication
  321. *
  322. * @return boolean
  323. */
  324. public function authenticate()
  325. {
  326. $headers = array (
  327. self::AUTHUSER_HEADER => $this->user,
  328. self::AUTHKEY_HEADER => $this->key
  329. );
  330. $result = $this->httpCall($this->authUrl.'/'.self::VERSION,'GET', $headers);
  331. if ($result->getStatus()==204) {
  332. $this->token = $result->getHeader(self::AUTHTOKEN);
  333. $this->storageUrl = $result->getHeader(self::STORAGE_URL);
  334. $this->cdnUrl = $result->getHeader(self::CDNM_URL);
  335. $this->managementUrl = $result->getHeader(self::MANAGEMENT_URL);
  336. return true;
  337. }
  338. $this->errorMsg = $result->getBody();
  339. $this->errorCode = $result->getStatus();
  340. return false;
  341. }
  342. }