public static function connectPush($message, $device_token, $device_type, $options, $showerror = true, $all_count = 0, $cronjob = false) { global $wpdb; self::$cronSendOperation = $cronjob; if ($cronjob === true) { smpush_helper::$returnValue = 'cronjob'; } $message = str_replace('"', '', trim(stripslashes($message))); self::$sendoptions = $options; if ($device_type == 'ios') { $payload = self::getPayload($message); $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://gateway.sandbox.push.apple.com:2195'; } else { $appleserver = 'tls://gateway.push.apple.com:2195'; } @($fpssl = stream_socket_client($appleserver, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx)); if (!$fpssl && $showerror) { if (empty($errstr)) { $errstr = 'Apple Certification error or problem with Password phrase'; } if ($err == 111) { $errstr .= ' - Contact your host to enable outgoing ports'; } elseif ($errstr == 'Connection timed out') { @fclose($fpssl); sleep(10); return self::jsonPrint(2, array('message' => '<p class="error">Connection timed out or your host blocked the outgoing port 2195...System trying reconnect now</p>', 'all_count' => $all_count)); } self::jsonPrint(0, '<p class="error">Could not establish connection with SSL server: ' . $errstr . '</p>'); } elseif (!$fpssl) { return; } elseif (!empty($_GET['firstrun'])) { $_SESSION['smpush_firstrun'] = 1; @fclose($fpssl); self::jsonPrint(2, array('message' => '<p>Connection With Apple server established successfully</p>', 'all_count' => $all_count)); } if (self::$sendoptions['expire'] > 0) { $expiry = current_time('timestamp', 1) + self::$sendoptions['expire'] * 3600; } else { $expiry = 0; } foreach ($device_token as $key => $sDevice) { $sDevice['token'] = str_replace(array(' ', '-'), '', $sDevice['token']); if (isset($sDevice['id']) && $cronjob === false) { $wpdb->query("DELETE FROM " . $wpdb->prefix . "push_queue WHERE id='" . $sDevice['id'] . "'"); } unset($device_token[$key]); if (preg_match('~^[a-f0-9]{64}$~i', $sDevice['token'])) { if ($expiry > 0) { @($sslwrite = chr(1) . pack("N", $sDevice['id']) . pack("N", $expiry) . pack("n", 32) . pack('H*', $sDevice['token']) . pack("n", strlen($payload)) . $payload); } else { @($sslwrite = chr(0) . pack('n', 32) . pack('H*', $sDevice['token']) . pack('n', strlen($payload)) . $payload); } $sslwriteLen = strlen($sslwrite); if ($sslwriteLen !== (int) @fwrite($fpssl, $sslwrite)) { @fclose($fpssl); sleep(3); return self::jsonPrint(2, array('message' => '', 'all_count' => $all_count)); } if (!empty($_SESSION['smpush_firstrun']) or self::$sendoptions['iostestmode'] == 1 and $cronjob === false) { stream_set_blocking($fpssl, 0); stream_set_write_buffer($fpssl, 0); $read = array($fpssl); $null = NULL; $nChangedStreams = stream_select($read, $null, $null, 0, 1000000); if ($nChangedStreams !== false && $nChangedStreams > 0) { $status = @ord(fread($fpssl, 1)); if (in_array($status, array(3, 4, 6, 7))) { @fclose($fpssl); self::jsonPrint(0, '<p class="error">Apple server error: ' . self::$_aErrorResponseMessages[$status] . '</p>'); } if ($status == 8) { $wpdb->insert($wpdb->prefix . 'push_feedback', array('tokens' => $sDevice['token'], 'device_type' => 'ios_invalid')); @fclose($fpssl); if (self::$sendoptions['iostestmode'] == 1) { $_SESSION['smpush_firstrun'] = 0; self::jsonPrint(2, array('message' => '<p>Apple server accepts the payload and start working</p>', 'all_count' => $all_count)); } else { self::connectPush($message, $device_token, $device_type, $options, true, $all_count, $cronjob); } } } } if (!empty($_SESSION['smpush_firstrun'])) { $_SESSION['smpush_firstrun'] = 0; @fclose($fpssl); self::jsonPrint(2, array('message' => '<p>Apple server accepts the payload and start working</p>', 'all_count' => $all_count)); } } else { $wpdb->insert($wpdb->prefix . 'push_feedback', array('tokens' => $sDevice['token'], 'device_type' => 'ios_invalid')); } } fclose($fpssl); } else { $baseurl = 'https://android.googleapis.com/gcm/send'; if (self::$apisetting['android_titanium_payload'] == 1) { $data = array(); $data['android']['alert'] = $message; } else { $data = array('message' => $message); } if (!empty(self::$sendoptions['and_extravalue'])) { if (self::$sendoptions['and_extra_type'] == 'normal') { $data['relatedvalue'] = stripslashes(self::$sendoptions['and_extravalue']); } else { $extravalue = json_decode(self::$sendoptions['and_extravalue']); if ($extravalue) { foreach ($extravalue as $key => $value) { $data[$key] = stripslashes($value); } } } } elseif (!empty(self::$sendoptions['extravalue'])) { if (self::$sendoptions['extra_type'] == 'normal') { $data['relatedvalue'] = stripslashes(self::$sendoptions['extravalue']); } else { $extravalue = json_decode(self::$sendoptions['extravalue']); if ($extravalue) { foreach ($extravalue as $key => $value) { $data[$key] = stripslashes($value); } } } } $fields = array('registration_ids' => $device_token['token'], 'data' => $data); if (self::$sendoptions['expire'] > 0) { $fields['time_to_live'] = self::$sendoptions['expire'] * 3600; } $headers = array('Authorization: key=' . self::$apisetting['google_apikey'], 'Content-Type: application/json'); if (!function_exists('curl_init') && $showerror) { self::jsonPrint(0, '<p class="error">Google: CURL Library is not support in your host</p>'); } elseif (!function_exists('curl_init')) { return; } $chandle = curl_init(); curl_setopt($chandle, CURLOPT_URL, $baseurl); curl_setopt($chandle, CURLOPT_POST, true); curl_setopt($chandle, CURLOPT_HTTPHEADER, $headers); curl_setopt($chandle, CURLOPT_RETURNTRANSFER, true); curl_setopt($chandle, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($chandle, CURLOPT_POSTFIELDS, json_encode($fields)); $result = curl_exec($chandle); $httpcode = curl_getinfo($chandle, CURLINFO_HTTP_CODE); if ($result === FALSE && $showerror) { self::jsonPrint(0, '<p class="error">Google push notification server not responding</p>'); } elseif ($httpcode == 503 && $showerror) { self::jsonPrint(0, '<p class="error">Google push notification server not responding</p>'); } elseif ($httpcode == 401 && $showerror) { $result = json_decode($result); if (!empty($result->results[0]->error)) { self::jsonPrint(0, '<p class="error">' . $result->results[0]->error . '</p>'); } else { self::jsonPrint(0, '<p class="error">Invalid Google API key</p>'); } } if (isset($device_token['id'])) { self::updateStats('androidsend', count($device_token['id']), $cronjob); if ($cronjob === false) { $ids = implode(',', $device_token['id']); $wpdb->query("DELETE FROM " . $wpdb->prefix . "push_queue WHERE id IN({$ids})"); } if (self::$sendoptions['feedback'] == 1) { $wpdb->insert($wpdb->prefix . 'push_feedback', array('tokens' => serialize($device_token['token']), 'feedback' => $result, 'device_type' => 'android')); } } curl_close($chandle); if (!empty($_GET['google_notify'])) { self::jsonPrint(3, array('message' => '<p>Connection With Google server established successfully</p><p>Google server accepts the payload and start working</p>', 'all_count' => $all_count)); } } }