示例#1
0
 public static function connectFeedback($all_count, $cronjob = false)
 {
     global $wpdb;
     self::$cronSendOperation = $cronjob;
     if ($cronjob === true) {
         smpush_helper::$returnValue = 'cronjob';
     }
     $fail = $androidfail = 0;
     $feedbacks = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "push_feedback");
     foreach ($feedbacks as $feedback) {
         if ($feedback->device_type == 'ios_invalid') {
             self::$pushdb->query(self::parse_query("UPDATE {tbname} SET {active_name}='0' WHERE {token_name}='" . $feedback->tokens . "'"));
             self::updateStats('iosfail', 1);
         } elseif ($feedback->device_type == 'ios') {
             if (!empty($_GET['feedback_open'])) {
                 self::jsonPrint(4, '<p>Start connection and reading with Apple feedback server, Maybe takes some time</p>');
             }
             $ctx = stream_context_create();
             stream_context_set_option($ctx, 'ssl', 'local_cert', self::$apisetting['apple_cert_path']);
             stream_context_set_option($ctx, 'ssl', 'passphrase', self::$apisetting['apple_passphrase']);
             if (self::$apisetting['apple_sandbox'] == 1) {
                 $appleserver = 'tls://feedback.sandbox.push.apple.com:2196';
             } else {
                 $appleserver = 'tls://feedback.push.apple.com:2196';
             }
             @($fpssl = stream_socket_client($appleserver, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx));
             if (!$fpssl) {
                 if (empty($errstr)) {
                     $errstr = 'Apple certification error or problem with Password phrase';
                 }
                 if ($err == 111) {
                     $errstr .= ' - Contact your host to enable outgoing ports';
                 }
                 self::jsonPrint(0, '<p class="error">Could not establish connection with SSL server: ' . $errstr . '</p>');
             }
             $nFeedbackTupleLen = self::TIME_BINARY_SIZE + self::TOKEN_LENGTH_BINARY_SIZE + self::DEVICE_BINARY_SIZE;
             $sBuffer = '';
             while (!feof($fpssl)) {
                 $sBuffer .= $sCurrBuffer = fread($fpssl, 8192);
                 $nCurrBufferLen = strlen($sCurrBuffer);
                 unset($sCurrBuffer, $nCurrBufferLen);
                 $nBufferLen = strlen($sBuffer);
                 if ($nBufferLen >= $nFeedbackTupleLen) {
                     $nFeedbackTuples = floor($nBufferLen / $nFeedbackTupleLen);
                     for ($i = 0; $i < $nFeedbackTuples; $i++) {
                         $sFeedbackTuple = substr($sBuffer, 0, $nFeedbackTupleLen);
                         $sBuffer = substr($sBuffer, $nFeedbackTupleLen);
                         $aFeedback = self::_parseBinaryTuple($sFeedbackTuple);
                         self::$pushdb->query(self::parse_query("UPDATE {tbname} SET {active_name}='0' WHERE {token_name}='{$aFeedback['deviceToken']}'"));
                         $fail++;
                         unset($aFeedback);
                     }
                 }
                 $read = array($fpssl);
                 $null = NULL;
                 $nChangedStreams = stream_select($read, $null, $null, 0, 1000000);
                 if ($nChangedStreams === false) {
                     break;
                 }
             }
             self::updateStats('iosfail', $fail);
             if ($fail > 0) {
                 self::jsonPrint(2, array('message' => '<p>Reading from Apple feedback is finised, try to read again for more</p>', 'all_count' => $all_count));
             }
         } else {
             if (!empty($_GET['feedback_google'])) {
                 self::jsonPrint(5, '<p>Start processing Google feedback queries</p>');
             }
             $tokens = unserialize($feedback->tokens);
             $result = json_decode($feedback->feedback);
             foreach ($result->results as $key => $status) {
                 if (isset($status->error)) {
                     if ($status->error == 'InvalidRegistration' || $status->error == 'NotRegistered' || $status->error == 'MismatchSenderId') {
                         self::$pushdb->query(self::parse_query("UPDATE {tbname} SET {active_name}='0' WHERE {token_name}='{$tokens[$key]}'"));
                         $androidfail++;
                     }
                 }
             }
             self::updateStats('androidfail', $androidfail);
         }
         $wpdb->query("DELETE FROM " . $wpdb->prefix . "push_feedback WHERE id='" . $feedback->id . "'");
     }
 }