/
class.apns.server.php
215 lines (187 loc) · 6.61 KB
/
class.apns.server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/*
* apns server 使用php进行服务推送
*
* @created by www.rockywu.com 2015-07-23
* @author rockywu wjl19890427@hotmail.com
*/
class apns_server
{
private $timeout = 60;
private $handle_log = null;
private $max_length = 30;
private $count = 0;
private $handle = null;
private $category = "";
private $certificate = null;
private $passphrase = null; //pushchat
private $sandbox_configure = array(
'feedback' => 'ssl://feedback.sandbox.push.apple.com:2196',
'gateway' => 'ssl://gateway.sandbox.push.apple.com:2195',
);
private $production_configure = array(
'feedback' => 'ssl://feedback.push.apple.com:2196',
'gateway' => 'ssl://gateway.push.apple.com:2195',
);
private $configure = array();
private $logger = null;
private $callback = "message";
private $invalid_tokens = array();
private $new_interface = false;
/**
* 构造函数 __construct
* @param string $environment //开发环境 : production or sandbox
* 生产环境(production) or 沙盒环境(sandbox)
* @param string $category // 接口类型 : gateway or feedback
* 推送接口(gateway) or 反馈接口(feedback)
* @param string $certificate //证书签名 (文件所在地址)
* @param string $passphrase //签名密钥
* @param object $logger //输出日志对象(可选)
* @param string $callback //输出日志回调方法(可选)
* @author rockywu
*/
public function __construct($environment, $category, $certificate, $passphrase, $logger=null, $callback="") {
if($environment != "sandbox" && $environment != "production") {
die("apple apns : environment is error");
}
if($category != 'gateway' &&$category != 'feedback') {
die("apple apns : category is error");
}
// 设置环境
$this-> set_environment($environment);
// 设置服务类型
$this-> category = $category;
// 设置日志生成器
$this-> set_logger($logger, $callback);
//设置签名密钥
$this-> set_passphrase($passphrase);
//设置签名文件
$this-> set_certificate($certificate);
//创建链接
$this-> handle_create();
}
public function use_new_interface($is_user = false) {
if($is_user) {
$this-> new_interface = true;
} else {
$this-> new_interface = false;
}
}
public function set_max_length($length) {
$length > 0 ? $this-> length : '';
}
public function log($message) {
$message = $message . PHP_EOL;
if($this-> logger) {
call_user_method_array($this-> callback, $this-> logger, array($message));
} else {
$this-> message($message);
}
}
public function message($message) {
printf("Anjuke APNS -- %s", $message);
}
public function set_logger($logger, $callback) {
if(method_exists($logger, $callback)) {
$this-> logger = $logger;
$this-> callback = $callback;
}
}
public function set_environment($type="sandbox") {
$this-> configure = $type == 'sandbox' ? $this-> sandbox_configure : $this-> production_configure;
}
private function set_passphrase($passphrase) {
if($passphrase && is_string($passphrase)) {
$this-> passphrase = $passphrase;
} else {
die("apple apns : passphrase is not exist");
}
}
private function set_certificate($certificate) {
if(is_readable($certificate)) {
$this-> certificate = $certificate;
} else {
die("apple apns : certificate is not exist");
}
}
private function handle_create() {
$this-> handle_close();
$this-> log("handle create");
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $this-> certificate);
stream_context_set_option($ctx, 'ssl', 'passphrase', $this-> passphrase);
if($this-> category == 'feedback') {
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
$this-> log("create feedback handle");
} else {
$this-> log("create gateway handle");
}
$this-> handle = stream_socket_client(
$this-> configure[$this->category],
$error,
$errorString,
$this-> timeout,
(STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT),
$ctx
);
if(!$this-> handle) {
$this->log("handle is error");
}
}
private function handle_close() {
if($this-> handle) {
$this-> log("handle close");
fclose($this-> handle);
} else {
$this-> handle = null;
}
}
/*
* 发送推送消息
*
* @param string $token
* @param string $message
* @param string $mark //标签
* @return boolean
*/
public function send_notification($token, $message, $mark = "markId") {
if(is_array($message)) {
$this-> log("\$message is array change to string");
$message = json_encode($message);
}
if(!is_string($message)) {
$this-> log("\$message is not string");
return false;
}
if($this-> new_interface) {
$expiry = time() + 120;
$msg = chr(1).pack("N", $mark).pack("N",$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;
}
if($this-> count > $this-> max_length) {
$this-> count = 0;
$this-> log("handle reopen");
$this-> handle_create();
}
$result = fwrite($this-> handle, $msg, strlen($msg));
$this-> count++;
$this-> log("send : $result | interface is" . ($this-> new_interface ? 'V 2.0' : 'V 1.0'));
return $result ? true : false;
}
public function get_invalid_tokens() {
while ($devcon = fread($this-> handle, 38)){
$arr = unpack("H*", $devcon);
$rawhex = trim(implode("", $arr));
$token = substr($rawhex, 12, 64);
if(!empty($token)){
$this-> invalid_tokens[] = $token;
}
}
$count = count($this-> invalid_tokens);
if($count) {
$this-> log("count : $count | tokens : " . json_encode($this-> invalid_tokens));
}
return $this-> invalid_tokens;
}
}