File indexing completed on 2024-06-16 05:30:15

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_Mobile
0017  * @subpackage Zend_Mobile_Push
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  * @version    $Id$
0021  */
0022 
0023 /** Zend_Http_Client **/
0024 // require_once 'Zend/Http/Client.php';
0025 
0026 /** Zend_Mobile_Push_Abstract **/
0027 // require_once 'Zend/Mobile/Push/Abstract.php';
0028 
0029 /** Zend_Mobile_Push_Message_Mpns **/
0030 // require_once 'Zend/Mobile/Push/Message/Mpns.php';
0031 
0032 /**
0033  * Mpns Push
0034  *
0035  * @category   Zend
0036  * @package    Zend_Mobile
0037  * @subpackage Zend_Mobile_Push
0038  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0039  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0040  * @version    $Id$
0041  */
0042 class Zend_Mobile_Push_Mpns extends Zend_Mobile_Push_Abstract
0043 {
0044     /**
0045      * Http Client
0046      *
0047      * @var Zend_Http_Client
0048      */
0049     protected $_httpClient;
0050 
0051     /**
0052      * Get Http Client
0053      *
0054      * @return Zend_Http_Client
0055      */
0056     public function getHttpClient()
0057     {
0058         if (!$this->_httpClient) {
0059             $this->_httpClient = new Zend_Http_Client();
0060             $this->_httpClient->setConfig(array(
0061                 'strictredirects' => true,
0062             ));
0063         }
0064         return $this->_httpClient;
0065     }
0066 
0067     /**
0068      * Set Http Client
0069      *
0070      * @return Zend_Mobile_Push_Mpns
0071      */
0072     public function setHttpClient(Zend_Http_Client $client)
0073     {
0074         $this->_httpClient = $client;
0075         return $this;
0076     }
0077 
0078     /**
0079      * Send Message
0080      *
0081      * @param  Zend_Mobile_Push_Message_Abstract $message
0082      * @throws Zend_Http_Client_Exception
0083      * @throws Zend_Mobile_Push_Exception
0084      * @throws Zend_Mobile_Push_Exception_DeviceQuotaExceeded
0085      * @throws Zend_Mobile_Push_Exception_InvalidPayload
0086      * @throws Zend_Mobile_Push_Exception_InvalidToken
0087      * @throws Zend_Mobile_Push_Exception_QuotaExceeded
0088      * @throws Zend_Mobile_Push_Exception_ServerUnavailable
0089      * @return boolean
0090      */
0091     public function send(Zend_Mobile_Push_Message_Abstract $message)
0092     {
0093         if (!$message->validate()) {
0094             throw new Zend_Mobile_Push_Exception('The message is not valid.');
0095         }
0096 
0097         $this->connect();
0098 
0099         $client = $this->getHttpClient();
0100         $client->setUri($message->getToken());
0101         $client->setHeaders(array(
0102             'Context-Type' => 'text/xml',
0103             'Accept' => 'application/*',
0104             'X-NotificationClass' => $message->getDelay()
0105         ));
0106         if ($message->getId()) {
0107             $client->setHeaders('X-MessageID', $message->getId());
0108         }
0109         if ($message->getNotificationType() != Zend_Mobile_Push_Message_Mpns::TYPE_RAW) {
0110             $client->setHeaders('X-WindowsPhone-Target', $message->getNotificationType());
0111         }
0112         $client->setRawData($message->getXmlPayload(), 'text/xml');
0113         $response = $client->request('POST');
0114         $this->close();
0115 
0116 
0117         switch ($response->getStatus())
0118         {
0119             case 200:
0120                 // check headers for response?  need to test how this actually works to correctly handle different states.
0121                 if ($response->getHeader('NotificationStatus') == 'QueueFull') {
0122                     // require_once 'Zend/Mobile/Push/Exception/DeviceQuotaExceeded.php';
0123                     throw new Zend_Mobile_Push_Exception_DeviceQuotaExceeded('The devices push notification queue is full, use exponential backoff');
0124                 }
0125                 break;
0126             case 400:
0127                 // require_once 'Zend/Mobile/Push/Exception/InvalidPayload.php';
0128                 throw new Zend_Mobile_Push_Exception_InvalidPayload('The message xml was invalid');
0129                 break;
0130             case 401:
0131                 // require_once 'Zend/Mobile/Push/Exception/InvalidToken.php';
0132                 throw new Zend_Mobile_Push_Exception_InvalidToken('The device token is not valid or there is a mismatch between certificates');
0133                 break;
0134             case 404:
0135                 // require_once 'Zend/Mobile/Push/Exception/InvalidToken.php';
0136                 throw new Zend_Mobile_Push_Exception_InvalidToken('The device subscription is invalid, stop sending notifications to this device');
0137                 break;
0138             case 405:
0139                 throw new Zend_Mobile_Push_Exception('Invalid method, only POST is allowed'); // will never be hit unless overwritten
0140                 break;
0141             case 406:
0142                 // require_once 'Zend/Mobile/Push/Exception/QuotaExceeded.php';
0143                 throw new Zend_Mobile_Push_Exception_QuotaExceeded('The unauthenticated web service has reached the per-day throttling limit');
0144                 break;
0145             case 412:
0146                 // require_once 'Zend/Mobile/Push/Exception/InvalidToken.php';
0147                 throw new Zend_Mobile_Push_Exception_InvalidToken('The device is in an inactive state.  You may retry once per hour');
0148                 break;
0149             case 503:
0150                 // require_once 'Zend/Mobile/Push/Exception/ServerUnavailable.php';
0151                 throw new Zend_Mobile_Push_Exception_ServerUnavailable('The server was unavailable.');
0152                 break;
0153             default:
0154                 break;
0155         }
0156         return true;
0157     }
0158 }