-
Notifications
You must be signed in to change notification settings - Fork 52
/
Pushover.php
554 lines (496 loc) · 13.1 KB
/
Pushover.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<?php
/**
* php-pushover
*
* PHP service wrapper for the pushover.net API: https://pushover.net/api
*
* @author Chris Schalenborgh <[email protected]>
* @version 0.2
* @package php-pushover
* @example test.php
* @link https://pushover.net/api
* @license MIT License
*/
class Pushover
{
// api url
const API_URL = 'https://api.pushover.net/1/messages.xml';
//validation url
const VALIDATION_URL = 'https://api.pushover.net/1/users/validate.json';
//sounds URL
const SOUNDS_URL = 'https://api.pushover.net/1/sounds.json?token=%s';
/**
* Application API token
*
* @var string
*/
private $_token;
/**
* User API token
*
* @var string
*/
private $_user;
/**
* Turn on/off debug mode
*
* @var bool
*/
private $_debug = false;
/**
* Title of the message
*
* @var string
*/
private $_title;
/**
* The message itself (up to 512 characters)
*
* @var string
*/
private $_message;
/**
* To enable HTML formatting, include an html parameter set to 1.
* The normal message content in your message parameter will then be displayed as HTML
* @var int
*/
private $_html;
/**
* Timestamp in Unix timestamp format
*
* @var int
*/
private $_timestamp;
/**
* User's device (user specific)
*
* @var string
*/
private $_device;
/**
* Priority of the message. Can be 0, 1 or 2. High-priority messages (1) override a user's "quiet hours" setting and will always be delivered any time they are received. High priority messages are highlighted in red in the Android and iOS clients. Emergency Priority (2) messages work similar to High-Priority messages, but they are repeated until the message is acknowledged by the user.
*
* @var string
*/
private $_priority = 0;
/**
* Include a Supplementary URL (up to 200 characters)
*
* @var string
*/
private $_url;
/**
* Title of the included URL (up to 50 characters)
*
* @var string
*/
private $_url_title;
/**
* The Retry parameter is only used when the Priority is set to 2 (or emergency-priority), and specifies how often (in seconds) the Pushover servers will send the same notification to the user. In a situation where your user might be in a noisy environment or sleeping, retrying the notification (with sound and vibration) will help get his or her attention. This parameter must have a value of at least 30 seconds between retries.
*
* @var int
*/
private $_retry;
/**
* The expire parameter is only used when the Priority is set to 2 (or emergency-priority), and specifies how many seconds your notification will continue to be retried for. If the notification has not been acknowledged in expire seconds, it will be marked as expired and will stop being sent to the user. This parameter must have a maximum value of at most 86400 seconds (24 hours).
*
* @var int
*/
private $_expire;
/**
* The optional callback parameter may be supplied with a publicly-accessible URL that our servers will send a request to when the user has acknowledged your notification.
*
* @var string
*/
private $_callback;
/**
* The sound parameter. Get an up-to-date sound list from https://api.pushover.net/1/sounds.json?token=
*
* @var int
*/
private $_sound;
/**
* The receipt parameter. The receipt of the last send message
*
* @var int
*/
private $_receipt;
/**
* Default constructor
*/
public function __construct () {
}
/**
* Set API token
*
* @param string $token Your app API key.
*
* @return void
*/
public function setToken ($token) {
$this->_token = (string)$token;
}
/**
* Get API token
*
* @return string
*/
public function getToken () {
return $this->_token;
}
/**
* Set API user
*
* @param string $user The user's API key.
*
* @return void
*/
public function setUser ($user) {
$this->_user = (string)$user;
}
/**
* Validate the user using Pushover API
*
* @return bool
*/
public function validateUser() {
if(!Empty($this->_token)&&!Empty($this->_user)){
$c = curl_init();
curl_setopt($c,CURLOPT_URL,self::VALIDATION_URL);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_POSTFIELDS,array(
"token" => $this->_token,
"user" => $this->_user
));
$response = curl_exec($c);
$json = json_decode($response);
return $json->status == 1;
} else {
return null;
}
}
/**
* Get API user
*
* @return string
*/
public function getUser () {
return $this->_user;
}
/**
* Set message title
*
* @param string $title Title of push notification.
*
* @return void
*/
public function setTitle ($title) {
$this->_title = (string)$title;
}
/**
* Get message title
*
* @return string
*/
public function getTitle () {
return $this->_title;
}
/**
* Set html attribute
*
* @param int $html Enable or disable html messages.
*
* @return void
*/
public function setHtml ($html) {
$this->_html = (int)$html;
}
/**
* Get html attribute
*
* @return int
*/
public function getHtml () {
return $this->_html;
}
/**
* Set Retry Time
*
* @param int $retry The retry time (in seconds). Must have a value of at least 30 seconds.
*/
public function setRetry ($retry) {
$this->_retry = (int)$retry;
}
/**
* Get Retry Time
*
* @return int
*/
public function getRetry() {
return $this->_retry;
}
/**
* Set Expire Time
*
* @param int $expire The expiry time (in seconds). Must have a maximum value of at most, 86400 seconds.
*/
public function setExpire ($expire) {
$this->_expire = (int)$expire;
}
/**
* Get Expire Time
*
* @return string
*/
public function getExpire () {
return $this->_expire;
}
/**
* Set Callback URL
*
* @param string $callback a publically-accessible URL that Pushover sends a request to when the user has acknowledged your notification.
*/
public function setCallback ($callback) {
$this->_callback = $callback;
}
/**
* Get Callback URL
*
* @return int
*/
public function getCallback() {
return $this->_callback;
}
/**
* Set message
*
* @param string $msg Message of push notification.
*
* @return void
*/
public function setMessage ($msg) {
$this->_message = (string)$msg;
}
/**
* Get message
*
* @return string
*/
public function getMessage () {
return $this->_message;
}
/**
* Set device
*
* @param string $device Leave this empty if you want to send to all user's devices. This can be user specific!
*
* @return void
*/
public function setDevice ($device) {
$this->_device = (string)$device;
}
/**
* Get device
*
* @return string
*/
public function getDevice () {
return $this->_device;
}
/**
* Set timestamp
*
* Messages are stored on the Pushover servers with a timestamp of when they were initially received through the API. This timestamp is sent to and shown on client devices, and messages are listed in order of these timestamps. In most cases, this default timestamp is acceptable. This is not for scheduling!
*
* @param int $time dispaly time on device
*
* @return void
*/
public function setTimestamp ($time) {
$this->_timestamp = (int)$time;
}
/**
* Get timestamp
*
* @return int
*/
public function getTimestamp () {
return $this->_timestamp;
}
/**
* Set priority (-1, 0 or 1)
*
* -1 Low priority notifications.
* 0 Default.
* 1 triggers a high-priority alert that always generates sound and vibration.
* 2 triggers the same high-priority alert that #1 does; but is repeated until the notification is acknowledged by the user.
*
* @param int $priority priority level.
*
* @return void
*/
public function setPriority ($priority) {
$this->_priority = (int)$priority;
}
/**
* Get priority
*
* @return int
*/
public function getPriority () {
return $this->_priority;
}
/**
* Set url
*
* @param string $url Add an url to your notification.
*
* @return void
*/
public function setUrl ($url) {
$this->_url = (string)$url;
}
/**
* Get url
*
* @return string
*/
public function getUrl () {
return $this->_url;
}
/**
* Set url title
*
* @param string $url_title A title if you want to show a text instead of the actual url.
*
* @return void
*/
public function setUrlTitle ($url_title) {
$this->_url_title = (string)$url_title;
}
/**
* Get url title
*
* @return string
*/
public function getUrlTitle () {
return $this->_url_title;
}
/**
* Set receipt mode
*
* @param string $receipt Set receipt string
*
* @return void
*/
public function setReceipt ($receipt) {
$this->_receipt = (string)$receipt;
}
/**
* Get last receipt
*
* @return string
*/
public function getReceipt () {
return $this->_receipt;
}
/**
* Set debug mode
*
* @param bool $debug Enable this to receive detailed input and output info.
*
* @return void
*/
public function setDebug ($debug) {
$this->_debug = (boolean)$debug;
}
/**
* Get debug mode
*
* @return bool
*/
public function getDebug () {
return $this->_debug;
}
/**
* Set sound
*
* @param string $sound If no sound parameter is specified, the user's default tone will play. If the user has not chosen a custom sound, the standard Pushover sound will play.
*
* @return void
*/
public function setSound ($sound) {
$this->_sound = (string)$sound;
}
/**
* Get sound
*
* @return string
*/
public function getSound () {
return $this->_sound;
}
/**
* Gets an array of notification sounds that can be used with Pushover.
*
* @return array
*/
public function getSoundsList() {
if(!empty($this->_token)){
$c = curl_init();
curl_setopt($c, CURLOPT_URL, sprintf(self::SOUNDS_URL,$this->_token));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($c);
$decoded = json_decode($response,true);
return $decoded['sounds'];
}
return null;
}
/**
* Send message to Pushover API
*
* @return bool
*/
public function send() {
if(!Empty($this->_token) && !Empty($this->_user) && !Empty($this->_message)) {
if(!isset($this->_timestamp)) $this->setTimestamp(time());
$c = curl_init();
curl_setopt($c, CURLOPT_URL, self::API_URL);
curl_setopt($c, CURLOPT_HEADER, false);
/*
if possible, set CURLOPT_SSL_VERIFYPEER to true..
- http://www.tehuber.com/phps/cabundlegen.phps
*/
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_POSTFIELDS, array(
'token' => $this->getToken(),
'user' => $this->getUser(),
'title' => $this->getTitle(),
'message' => $this->getMessage(),
'html' => $this->getHtml(),
'device' => $this->getDevice(),
'priority' => $this->getPriority(),
'timestamp' => $this->getTimestamp(),
'expire' => $this->getExpire(),
'retry' => $this->getRetry(),
'callback' => $this->getCallback(),
'url' => $this->getUrl(),
'sound' => $this->getSound(),
'url_title' => $this->getUrlTitle()
));
$response = curl_exec($c);
$xml = simplexml_load_string($response);
# Store notification-receipt
$this->setReceipt( $xml->receipt );
if($this->getDebug()) {
return array('output' => $xml, 'input' => $this);
}
else {
return ($xml->status == 1) ? true : false;
}
}
}
}
?>