Initial commit
[2ch-be.git] / dev-test / mb / simplepush.php.bak
blob46db9c722ff05b24e419e258a8bde74530b51b69
1 <?php
3 if(!isset($_GET['token']) ) { //&& !isset($_GET['unread'])) {
4         exit;
5 } else if(isset($_GET['token'])) {
6         $deviceToks = $_GET['token'];
7         //$unread = $_GET['unread'];
11 $cookie_val = $_COOKIE['DMDM'];
12         $cookie_val2 = $_COOKIE['MDMD'];
13         $opts = array(
14           'http'=>array(
15                 'method'=>"GET",
16                 'header'=>"Accept-language: en\r\n" .
17                                   "Cookie: DMDM=" . $cookie_val . "\r\n" .
18                                   "Cookie: MDMD=" . $cookie_val2
19           )
20         );
21         $context = stream_context_create($opts);
22         
23 include('../encrypt.php');
24 $email = Decrypt($_COOKIE['DMDM'],KEY);
25         
26 //$gotemail = file_get_contents('http://be.2ch.net/mb/ur.php', TRUE , $context);
29 $gotmessage = file_get_contents('https://be.2ch.net/mb/noti.php?un='.$email, FALSE , $context);
31 if($gotmessage >= 1) {
33 $deviceToken = $deviceToks;
34 // Put your device token here (without spaces):
35 //$deviceToken = str_replace(' ','', $deviceToks);
37 // Put your private key's passphrase here:
38 $passphrase = 'rqibemessenger';
40 // Put your alert message here:
42 /*if(!empty($unread)){
43         $countmessage = $unread . " unread messages!";
44 } else {
45         $countmessage = "";
48         $countmessage = "";
50 $message = 'あなたはBEmessengerから新しいメッセージを得た! ' . $countmessage ;
52 ////////////////////////////////////////////////////////////////////////////////
54 $ctx = stream_context_create();
56 //DISABLED THIS WHEN APP STORE AVAIL
57 stream_context_set_option($ctx, 'ssl', 'local_cert', '/home/auth/secure_html/ckDEV.pem');
59 //ENABLED THIS WHEN APP STORE AVAIL
60 //stream_context_set_option($ctx, 'ssl', 'local_cert', '/home/auth/secure_html/ckDis.pem');
63 stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
65 // Open a connection to the APNS server
66 $fp = stream_socket_client(
67         'ssl://gateway.sandbox.push.apple.com:2195', $err,
68         $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
70 if (!$fp)
71         exit("Failed to connect: $err $errstr" . PHP_EOL);
73 echo 'Connected to APNS' . PHP_EOL;
75 // Create the payload body
76 $body['aps'] = array(
77         'alert' => $message,
78         'sound' => 'default'
79         );
81 // Encode the payload as JSON
82 $payload = json_encode($body);
84 // Build the binary notification
85 $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
87 // Send it to the server
88 $result = fwrite($fp, $msg, strlen($msg));
90 if (!$result)
91         echo 'Message not delivered' . PHP_EOL;
92 else
93         echo 'Message successfully delivered' . PHP_EOL;
95 // Close the connection to the server
96 fclose($fp);
98 } else {
99         echo "No message.";