Exemple #1
0
 /**
  * api
  * Perform like an api script
  * push request to queue
  *
  * @throws SPAPNS_Exception
  */
 public function api()
 {
     /*
     provider: string,
     user: string/optional,
     pass: string/optional,
     data: [
         {
             token: string,
             identifier: int/optional,
             expiry: int/optional,
             message: {
                 aps:{}
             }
         }
     ]
     */
     try {
         if (!isset($_GET['provider'])) {
             throw new SPAPNS_Exception('provider cannot be empty', 300001);
         }
         $provider = $_GET['provider'];
         $provider_config = $this->config->getProvider($provider);
         if (empty($provider_config)) {
             throw new SPAPNS_Exception('provider not available', 300002);
         }
         if ($provider_config['auth_user'] && (empty($_GET['user']) || $provider_config['auth_user'] != $_GET['user']) && ($provider_config['auth_pass'] && (empty($_GET['pass']) || $provider_config['auth_pass'] != md5($_GET['pass'])))) {
             throw new SPAPNS_Exception('auth failed', 300003);
         }
         if (empty($_POST['json']) || !($json = json_decode($_POST['json'], true)) || !is_array($json)) {
             throw new SPAPNS_Exception('json cannot be empty', 300004);
         }
         $ret_identities = array();
         foreach ($json as $array) {
             if (!isset($array['token']) || !isset($array['message']) || !isset($array['message']['aps']) || !spAPNSUtils::CheckToken($array['token'])) {
                 $ret_identities[] = null;
                 $this->log(LOG_INFO, ' [API] invalid message. provider=' . $provider, $array);
                 continue;
             }
             $array['provider'] = $provider;
             if (!isset($array['identifier']) || empty($array['identifier'])) {
                 $array['identifier'] = crc32(uniqid('apns_proxy', true));
             }
             $array['identifier'] = (int) $array['identifier'];
             $ret_identities[] = $array['identifier'];
             if (!isset($array['expiry'])) {
                 $array['expiry'] = 86400 * 3;
             }
             $array['expiry'] = (int) $array['expiry'];
             $this->q->push($array);
         }
         echo json_encode(array('code' => 0, 'message' => 'success', 'data' => $ret_identities));
     } catch (Exception $e) {
         echo json_encode(array('code' => $e->getCode(), 'message' => $e->getMessage()));
     }
 }
Exemple #2
0
 /**
  * batch push
  *
  * @param array $array
  * @return mixed
  * @throws SPAPNS_Exception
  */
 public function push(array $array)
 {
     $post_array = array();
     foreach ($array as $v) {
         if (!isset($v['token']) || !spAPNSUtils::CheckToken($v['token'])) {
             throw new SPAPNS_Exception('推送token不合法', 400101);
         }
         if (!isset($v['message'])) {
             throw new SPAPNS_Exception('推送消息不合法', 400102);
         }
         $msgobj = new spAPNSMessage($v['message']);
         $v['message'] = $msgobj->build(false);
         $post_array[] = $v;
     }
     return $this->do_push($post_array);
 }
Exemple #3
0
 /**
  * Build Payload
  * @param string $token
  * @return boolean|string
  */
 public function payload($token, $identifier = null, $expiry = null)
 {
     $token = str_replace(' ', '', $token);
     if (!spAPNSUtils::CheckToken($token)) {
         return false;
     }
     $message = $this->build(true);
     $current_time = time();
     if (empty($expiry) || $expiry < $current_time && $expiry > 86400 * 180) {
         $expiry = $current_time + self::TIMEOUT;
     } else {
         if ($expiry <= 86400 * 180) {
             $expiry = $current_time + $expiry;
         }
     }
     if (!empty($identifier)) {
         $msg = chr(1) . pack('N', (int) $identifier) . pack('N', (int) $expiry) . pack("n", 32) . pack('H*', $token) . pack("n", strlen($message)) . $message;
     } else {
         $msg = chr(0) . pack("n", 32) . pack('H*', $token) . pack("n", strlen($message)) . $message;
     }
     return $msg;
 }