forked from connorlacombe/Safari-Push-Notifications
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdesktop.php
206 lines (192 loc) · 7.57 KB
/
desktop.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
<?php
// http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
if($_REQUEST['test'] && $_REQUEST['token']) {
$data = array(
"title" => strip_tags($_REQUEST['title']),
"body" => strip_tags($_REQUEST['body']),
"button" => strip_tags($_REQUEST['button']),
"urlargs" => "/",
"auth" => AUTHORISATION_CODE
);
$pushurl = WEBSERVICE_URL."/v1/push/".strip_tags($_REQUEST['token']);
echo do_post_request($pushurl, http_build_query($data));
exit();
}
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Safari Push Notification Service</title>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
<style type="text/css">
body {
margin: 0;
padding: 0;
background: #EDEDED;
font-family: "Avenir Next", "Helvetica Neue", Helvetica, sans-serif;
}
.box {
border-radius: 8px;
background: #fff;
width: 600px;
height: 300px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -300px;
margin-top: -150px;
border: 1px solid #CECECE;
box-shadow: rgba(255,255,255,0.7) 0 1px 0, inset rgba(0,0,0,0.1) 0 1px 2px;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script type="text/javascript">
var token = "";
var id = "<?php echo $id; ?>";
window.onload = function() {
var ua = window.navigator.userAgent,
safari = ua.indexOf ( "Safari" ),
chrome = ua.indexOf ( "Chrome" ),
version = ua.substring(0,safari).substring(ua.substring(0,safari).lastIndexOf("/")+1);
if(chrome ==-1 && safari > 0 && parseInt(version, 10) >=7) {
checkPerms();
}
else {
document.getElementById("old").style.display = "";
}
};
function checkPerms() {
document.getElementById("reqperm").style.display = "none";
document.getElementById("granted").style.display = "none";
document.getElementById("denied").style.display = "none";
var pResult = window.safari.pushNotification.permission('<?php echo WEBSITE_UID; ?>');
if(pResult.permission === 'default') {
//request permission
document.getElementById("reqperm").style.display = "";
requestPermissions();
}
else if(pResult.permission === 'granted') {
document.getElementById("granted").style.display = "";
token = pResult.deviceToken;
}
else if(pResult.permission === 'denied') {
document.getElementById("denied").style.display = "";
}
}
function requestPermissions() {
window.safari.pushNotification.requestPermission('<?php echo WEBSERVICE_URL; ?>', '<?php echo WEBSITE_UID; ?>', {}, function(c) {
if(c.permission === 'granted') {
document.getElementById("reqperm").style.display = "none";
document.getElementById("granted").style.display = "";
token = c.deviceToken;
}
else if(c.permission === 'denied') {
document.getElementById("reqperm").style.display = "none";
document.getElementById("denied").style.display = "";
}
});
}
function do_push() {
var checksOut = true;
$("#form input").each(function(index, element) {
$(this).parents(".control-group").removeClass("error");
if(element.value == "") {
$(this).parents(".control-group").addClass("error");
checksOut = false;
}
});
p = window.safari.pushNotification.permission('<?php echo WEBSITE_UID; ?>');
if(p.permission != 'granted') checksOut = false;
if(checksOut == true) {
var data = {"test":1, "token":p.deviceToken, "title": document.getElementById("not_title").value, "body": document.getElementById("not_body").value, "button": document.getElementById("not_button").value};
var jqxhr = $.post("/", data)
.fail(function(){alert("The server responded with an error")})
.always(function(){console.log(data)});
$("#form input").val("");
$("#modal_scrim").fadeOut(300);
}
}
</script>
</head>
<body>
<div style="background: rgba(0,0,0,0.8); position: fixed; top: 0; left: 0; width: 100%; height: 100%; display: none; z-index: 899;" id="modal_scrim">
<div class="modal">
<div class="modal-header">
<button type="button" class="close" onClick="$('#modal_scrim').fadeOut(300);" aria-hidden="true">×</button>
<h3>Create Push Notification</h3>
</div>
<div class="modal-body form-horizontal" id="form">
<div class="control-group">
<label class="control-label" for="not_title">Title</label>
<div class="controls">
<input type="text" id="not_title">
</div>
</div>
<div class="control-group">
<label class="control-label" for="not_title">Body</label>
<div class="controls">
<input type="text" id="not_body">
</div>
</div>
<div class="control-group">
<label class="control-label" for="not_title">Button Label</label>
<div class="controls">
<input type="text" id="not_button">
</div>
</div>
</div>
<div class="modal-footer">
<div class="btn btn-primary btn-small" onClick="do_push();">Push!</div>
</div>
</div>
</div>
<div class="box">
<div style="font-weight: 500; font-size: 20px; margin: 10px;">Safari Push Notification Service for <?php echo WEBSITE_NAME; ?></div>
<!-- old safari lolz -->
<div style="margin-top: 100px; text-align: center; display: none;" id="old">
You need Safari 7.0+ on Mac OS 10.9+ to view your notification status.
</div>
<!-- checking permissions -->
<div style="margin-top: 100px; text-align: center; display: none;" id="reqperm">
<img src="loader.gif">
<div>Requesting permission...</div>
</div>
<!-- denied permissions -->
<div style="margin-top: 100px; text-align: center; display: none;" id="denied">
<div>You have denied this website permission to send push notifications.</div>
<div class="btn btn-primary btn-small" onClick="checkPerms();">I've changed my mind...</div>
</div>
<!-- granted permissions -->
<div style="margin-top: 20px; text-align: center; display: none;" id="granted">
<div>You have granted this website permission to send push notifications.</div>
<div class="btn btn-primary" onClick="$('#modal_scrim').fadeIn(300); document.getElementById('not_title').focus();">Send Yourself a Push Notification</div>
</div>
</div>
<div style="position: absolute; top: 50%; margin-top: 170px; left: 50%; width: 600px; text-align: center; margin-left: -300px; text-shadow: #fff 0 1px 0;">
Reference Safari Push Notification Service by <a href="http://www.surrealroad.com">Surreal Road</a>, based on <a href="https://github.com/connorlacombe/Safari-Push-Notifications">Safari Push Notifications</a> by Connor LaCombe, available on <a href="https://github.com/surrealroad/Safari-Push-Notifications">GitHub</a>
</div>
</div>
</body>
</html>