PageRenderTime 106ms CodeModel.GetById 60ms app.highlight 17ms RepoModel.GetById 23ms app.codeStats 1ms

/Zend/Service/Amazon/Sqs.php

https://bitbucket.org/simukti/zf1
PHP | 542 lines | 256 code | 69 blank | 217 comment | 43 complexity | ed732c2e8a378cd34dacbf129eaddfd1 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 Amazon_Sqs
 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 * @version    $Id: Sqs.php 25024 2012-07-30 15:08:15Z rob $
 21 */
 22
 23/**
 24 * @see Zend_Service_Amazon_Abstract
 25 */
 26require_once 'Zend/Service/Amazon/Abstract.php';
 27
 28/**
 29 * @see Zend_Crypt_Hmac
 30 */
 31require_once 'Zend/Crypt/Hmac.php';
 32
 33/**
 34 * Class for connecting to the Amazon Simple Queue Service (SQS)
 35 *
 36 * @category   Zend
 37 * @package    Zend_Service
 38 * @subpackage Amazon_Sqs
 39 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 40 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 41 * @see        http://aws.amazon.com/sqs/ Amazon Simple Queue Service
 42 */
 43class Zend_Service_Amazon_Sqs extends Zend_Service_Amazon_Abstract
 44{
 45    /**
 46     * Default timeout for createQueue() function
 47     */
 48    const CREATE_TIMEOUT_DEFAULT = 30;
 49
 50    /**
 51     * HTTP end point for the Amazon SQS service
 52     */
 53    protected $_sqsEndpoint = 'queue.amazonaws.com';
 54
 55    /**
 56     * The API version to use
 57     */
 58    protected $_sqsApiVersion = '2009-02-01';
 59
 60    /**
 61     * Signature Version
 62     */
 63    protected $_sqsSignatureVersion = '2';
 64
 65    /**
 66     * Signature Encoding Method
 67     */
 68    protected $_sqsSignatureMethod = 'HmacSHA256';
 69
 70    protected $_sqsEndpoints = array('us-east-1' => 'sqs.us-east-1.amazonaws.com',
 71                                     'us-west-1' => 'sqs.us-west-1.amazonaws.com',
 72                                     'eu-west-1' => 'sqs.eu-west-1.amazonaws.com',
 73                                     'ap-southeast-1' => 'sqs.ap-southeast-1.amazonaws.com',
 74                                     'ap-northeast-1' => 'sqs.ap-northeast-1.amazonaws.com');
 75    /**
 76     * Constructor
 77     *
 78     * The default region is us-east-1. Use the region to set it to one of the regions that is build-in into ZF.
 79     * To add a new AWS region use the setEndpoint() method.
 80     *
 81     * @param string $accessKey
 82     * @param string $secretKey
 83     * @param string $region
 84     */
 85    public function __construct($accessKey = null, $secretKey = null, $region = null)
 86    {
 87        parent::__construct($accessKey, $secretKey, $region);
 88        
 89        if (null !== $region) {
 90            $this->_setEndpoint($region);
 91        }
 92    }
 93
 94    /**
 95     * Set SQS endpoint
 96     *
 97     * Checks and sets endpoint if region exists in $_sqsEndpoints. If a new SQS region is added by amazon,
 98     * please use the setEndpoint function to set it.
 99     *
100     * @param  string  $region region
101     * @throws Zend_Service_Amazon_Sqs_Exception
102     */
103    protected function _setEndpoint($region)
104    {
105        if (array_key_exists($region, $this->_sqsEndpoints)) {
106            $this->_sqsEndpoint = $this->_sqsEndpoints[$region];
107        } else {
108            throw new Zend_Service_Amazon_Sqs_Exception('Invalid SQS region specified.');
109        }
110    }
111    
112    /**
113     * Set SQS endpoint
114     *
115     * You can set SQS to on of the build-in regions. If the region does not exsist it will be added.
116     *
117     * @param  string  $region region
118     * @throws Zend_Service_Amazon_Sqs_Exception
119     */
120    public function setEndpoint($region)
121    {
122        if (!empty($region)) {
123            if (array_key_exists($region, $this->_sqsEndpoints)) {
124                $this->_sqsEndpoint = $this->_sqsEndpoints[$region];
125            } else {
126                $this->_sqsEndpoints[$region] = "sqs.$region.amazonaws.com";
127                $this->_sqsEndpoint = $this->_sqsEndpoints[$region];
128            }
129        } else {
130            throw new Zend_Service_Amazon_Sqs_Exception('Empty region specified.');
131        }
132    }
133
134    /**
135     * Get the SQS endpoint
136     * 
137     * @return string 
138     */
139    public function getEndpoint()
140    {
141        return $this->_sqsEndpoint;
142    }
143
144    /**
145     * Get possible SQS endpoints
146     *
147     * Since there is not an SQS webserive to get all possible endpoints, a hardcoded list is available.
148     * For the actual region list please check:
149     * http://docs.amazonwebservices.com/AWSSimpleQueueService/2009-02-01/APIReference/index.html?QueueServiceWsdlArticle.html
150     *
151     * @param  string  $region region
152     * @return array
153     */
154    public function getEndpoints()
155    {
156        return $this->_sqsEndpoints;
157    }
158    
159    /**
160     * Create a new queue
161     *
162     * Visibility timeout is how long a message is left in the queue "invisible"
163     * to other readers.  If the message is acknowleged (deleted) before the
164     * timeout, then the message is deleted.  However, if the timeout expires
165     * then the message will be made available to other queue readers.
166     *
167     * @param  string  $queue_name queue name
168     * @param  integer $timeout    default visibility timeout
169     * @return string|boolean
170     * @throws Zend_Service_Amazon_Sqs_Exception
171     */
172    public function create($queue_name, $timeout = null)
173    {
174        $params = array();
175        $params['QueueName'] = $queue_name;
176        $timeout = ($timeout === null) ? self::CREATE_TIMEOUT_DEFAULT : (int)$timeout;
177        $params['DefaultVisibilityTimeout'] = $timeout;
178
179        $retry_count = 0;
180
181        do {
182            $retry  = false;
183            $result = $this->_makeRequest(null, 'CreateQueue', $params);
184
185            if (!isset($result->CreateQueueResult->QueueUrl)
186                || empty($result->CreateQueueResult->QueueUrl)
187            ) {
188                if ($result->Error->Code == 'AWS.SimpleQueueService.QueueNameExists') {
189                    return false;
190                } elseif ($result->Error->Code == 'AWS.SimpleQueueService.QueueDeletedRecently') {
191                    // Must sleep for 60 seconds, then try re-creating the queue
192                    sleep(60);
193                    $retry = true;
194                    $retry_count++;
195                } else {
196                    require_once 'Zend/Service/Amazon/Sqs/Exception.php';
197                    throw new Zend_Service_Amazon_Sqs_Exception($result->Error->Code);
198                }
199            } else {
200                return (string) $result->CreateQueueResult->QueueUrl;
201            }
202
203        } while ($retry);
204
205        return false;
206    }
207
208    /**
209     * Delete a queue and all of it's messages
210     *
211     * Returns false if the queue is not found, true if the queue exists
212     *
213     * @param  string  $queue_url queue URL
214     * @return boolean
215     * @throws Zend_Service_Amazon_Sqs_Exception
216     */
217    public function delete($queue_url)
218    {
219        $result = $this->_makeRequest($queue_url, 'DeleteQueue');
220
221        if ($result->Error->Code !== null) {
222            require_once 'Zend/Service/Amazon/Sqs/Exception.php';
223            throw new Zend_Service_Amazon_Sqs_Exception($result->Error->Code);
224        }
225
226        return true;
227    }
228
229    /**
230     * Get an array of all available queues
231     *
232     * @return array
233     * @throws Zend_Service_Amazon_Sqs_Exception
234     */
235    public function getQueues()
236    {
237        $result = $this->_makeRequest(null, 'ListQueues');
238
239        if (isset($result->Error)) {
240            require_once 'Zend/Service/Amazon/Sqs/Exception.php';
241            throw new Zend_Service_Amazon_Sqs_Exception($result->Error->Code);
242        }
243
244        if (!isset($result->ListQueuesResult->QueueUrl)
245            || empty($result->ListQueuesResult->QueueUrl)
246        ) {
247            return array();
248        }
249
250        $queues = array();
251        foreach ($result->ListQueuesResult->QueueUrl as $queue_url) {
252            $queues[] = (string)$queue_url;
253        }
254
255        return $queues;
256    }
257
258    /**
259     * Return the approximate number of messages in the queue
260     *
261     * @param  string  $queue_url Queue URL
262     * @return integer
263     * @throws Zend_Service_Amazon_Sqs_Exception
264     */
265    public function count($queue_url)
266    {
267        return (int)$this->getAttribute($queue_url, 'ApproximateNumberOfMessages');
268    }
269
270    /**
271     * Send a message to the queue
272     *
273     * @param  string $queue_url Queue URL
274     * @param  string $message   Message to send to the queue
275     * @return string            Message ID
276     * @throws Zend_Service_Amazon_Sqs_Exception
277     */
278    public function send($queue_url, $message)
279    {
280        $params = array();
281        $params['MessageBody'] = urlencode($message);
282
283        $checksum = md5($params['MessageBody']);
284
285        $result = $this->_makeRequest($queue_url, 'SendMessage', $params);
286
287        if (!isset($result->SendMessageResult->MessageId)
288            || empty($result->SendMessageResult->MessageId)
289        ) {
290            require_once 'Zend/Service/Amazon/Sqs/Exception.php';
291            throw new Zend_Service_Amazon_Sqs_Exception($result->Error->Code);
292        } else if ((string) $result->SendMessageResult->MD5OfMessageBody != $checksum) {
293            require_once 'Zend/Service/Amazon/Sqs/Exception.php';
294            throw new Zend_Service_Amazon_Sqs_Exception('MD5 of body does not match message sent');
295        }
296
297        return (string) $result->SendMessageResult->MessageId;
298    }
299
300    /**
301     * Get messages in the queue
302     *
303     * @param  string  $queue_url    Queue name
304     * @param  integer $max_messages Maximum number of messages to return
305     * @param  integer $timeout      Visibility timeout for these messages
306     * @return array
307     * @throws Zend_Service_Amazon_Sqs_Exception
308     */
309    public function receive($queue_url, $max_messages = null, $timeout = null)
310    {
311        $params = array();
312
313        // If not set, the visibility timeout on the queue is used
314        if ($timeout !== null) {
315            $params['VisibilityTimeout'] = (int)$timeout;
316        }
317
318        // SQS will default to only returning one message
319        if ($max_messages !== null) {
320            $params['MaxNumberOfMessages'] = (int)$max_messages;
321        }
322
323        $result = $this->_makeRequest($queue_url, 'ReceiveMessage', $params);
324
325        if (isset($result->Error)) {
326            require_once 'Zend/Service/Amazon/Sqs/Exception.php';
327            throw new Zend_Service_Amazon_Sqs_Exception($result->Error->Code);
328        }
329
330        if (!isset($result->ReceiveMessageResult->Message)
331            || empty($result->ReceiveMessageResult->Message)
332        ) {
333            // no messages found
334            return array();
335        }
336
337        $data = array();
338        foreach ($result->ReceiveMessageResult->Message as $message) {
339            $data[] = array(
340                'message_id' => (string)$message->MessageId,
341                'handle'     => (string)$message->ReceiptHandle,
342                'md5'        => (string)$message->MD5OfBody,
343                'body'       => urldecode((string)$message->Body),
344            );
345        }
346
347        return $data;
348    }
349
350    /**
351     * Delete a message from the queue
352     *
353     * Returns true if the message is deleted, false if the deletion is
354     * unsuccessful.
355     *
356     * @param  string $queue_url  Queue URL
357     * @param  string $handle     Message handle as returned by SQS
358     * @return boolean
359     * @throws Zend_Service_Amazon_Sqs_Exception
360     */
361    public function deleteMessage($queue_url, $handle)
362    {
363        $params = array();
364        $params['ReceiptHandle'] = (string)$handle;
365
366        $result = $this->_makeRequest($queue_url, 'DeleteMessage', $params);
367
368        if (isset($result->Error->Code)
369            && !empty($result->Error->Code)
370        ) {
371            return false;
372        }
373
374        // Will always return true unless ReceiptHandle is malformed
375        return true;
376    }
377
378    /**
379     * Get the attributes for the queue
380     *
381     * @param  string $queue_url  Queue URL
382     * @param  string $attribute
383     * @return string
384     * @throws Zend_Service_Amazon_Sqs_Exception
385     */
386    public function getAttribute($queue_url, $attribute = 'All')
387    {
388        $params = array();
389        $params['AttributeName'] = $attribute;
390
391        $result = $this->_makeRequest($queue_url, 'GetQueueAttributes', $params);
392
393        if (!isset($result->GetQueueAttributesResult->Attribute)
394            || empty($result->GetQueueAttributesResult->Attribute)
395        ) {
396            require_once 'Zend/Service/Amazon/Sqs/Exception.php';
397            throw new Zend_Service_Amazon_Sqs_Exception($result->Error->Code);
398        }
399
400        if(count($result->GetQueueAttributesResult->Attribute) > 1) {
401            $attr_result = array();
402            foreach($result->GetQueueAttributesResult->Attribute as $attribute) {
403                $attr_result[(string)$attribute->Name] = (string)$attribute->Value;
404            }
405            return $attr_result;
406        } else {
407            return (string) $result->GetQueueAttributesResult->Attribute->Value;
408        }
409    }
410
411    /**
412     * Make a request to Amazon SQS
413     *
414     * @param  string           $queue  Queue Name
415     * @param  string           $action SQS action
416     * @param  array            $params
417     * @return SimpleXMLElement
418     */
419    private function _makeRequest($queue_url, $action, $params = array())
420    {
421        $params['Action'] = $action;
422        $params = $this->addRequiredParameters($queue_url, $params);
423
424        if ($queue_url === null) {
425            $queue_url = '/';
426        }
427
428        $client = self::getHttpClient();
429
430        switch ($action) {
431            case 'ListQueues':
432            case 'CreateQueue':
433                $client->setUri('http://'.$this->_sqsEndpoint);
434                break;
435            default:
436                $client->setUri($queue_url);
437                break;
438        }
439
440        $retry_count = 0;
441
442        do {
443            $retry = false;
444
445            $client->resetParameters();
446            $client->setParameterGet($params);
447
448            $response = $client->request('GET');
449
450            $response_code = $response->getStatus();
451
452            // Some 5xx errors are expected, so retry automatically
453            if ($response_code >= 500 && $response_code < 600 && $retry_count <= 5) {
454                $retry = true;
455                $retry_count++;
456                sleep($retry_count / 4 * $retry_count);
457            }
458        } while ($retry);
459
460        unset($client);
461
462        return new SimpleXMLElement($response->getBody());
463    }
464
465    /**
466     * Adds required authentication and version parameters to an array of
467     * parameters
468     *
469     * The required parameters are:
470     * - AWSAccessKey
471     * - SignatureVersion
472     * - Timestamp
473     * - Version and
474     * - Signature
475     *
476     * If a required parameter is already set in the <tt>$parameters</tt> array,
477     * it is overwritten.
478     *
479     * @param  string $queue_url  Queue URL
480     * @param  array  $parameters the array to which to add the required
481     *                            parameters.
482     * @return array
483     */
484    protected function addRequiredParameters($queue_url, array $parameters)
485    {
486        $parameters['AWSAccessKeyId']   = $this->_getAccessKey();
487        $parameters['SignatureVersion'] = $this->_sqsSignatureVersion;
488        $parameters['Timestamp']        = gmdate('Y-m-d\TH:i:s\Z', time()+10);
489        $parameters['Version']          = $this->_sqsApiVersion;
490        $parameters['SignatureMethod']  = $this->_sqsSignatureMethod;
491        $parameters['Signature']        = $this->_signParameters($queue_url, $parameters);
492
493        return $parameters;
494    }
495
496    /**
497     * Computes the RFC 2104-compliant HMAC signature for request parameters
498     *
499     * This implements the Amazon Web Services signature, as per the following
500     * specification:
501     *
502     * 1. Sort all request parameters (including <tt>SignatureVersion</tt> and
503     *    excluding <tt>Signature</tt>, the value of which is being created),
504     *    ignoring case.
505     *
506     * 2. Iterate over the sorted list and append the parameter name (in its
507     *    original case) and then its value. Do not URL-encode the parameter
508     *    values before constructing this string. Do not use any separator
509     *    characters when appending strings.
510     *
511     * @param  string $queue_url  Queue URL
512     * @param  array  $parameters the parameters for which to get the signature.
513     *
514     * @return string the signed data.
515     */
516    protected function _signParameters($queue_url, array $paramaters)
517    {
518        $data = "GET\n";
519        $data .= $this->_sqsEndpoint . "\n";
520        if ($queue_url !== null) {
521            $data .= parse_url($queue_url, PHP_URL_PATH);
522        }
523        else {
524            $data .= '/';
525        }
526        $data .= "\n";
527
528        uksort($paramaters, 'strcmp');
529        unset($paramaters['Signature']);
530
531        $arrData = array();
532        foreach($paramaters as $key => $value) {
533            $arrData[] = $key . '=' . str_replace('%7E', '~', urlencode($value));
534        }
535
536        $data .= implode('&', $arrData);
537
538        $hmac = Zend_Crypt_Hmac::compute($this->_getSecretKey(), 'SHA256', $data, Zend_Crypt_Hmac::BINARY);
539
540        return base64_encode($hmac);
541    }
542}