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