forked from nerdzeu/nerdz.eu
/
push.php
executable file
·84 lines (60 loc) · 2.26 KB
/
push.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
<?php
ob_start('ob_gzhandler');
require_once $_SERVER['DOCUMENT_ROOT'].'/class/autoload.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/class/vendor/autoload.php';
use MCilloni\Pushed\Pushed;
use MCilloni\Pushed\PushedException;
use NERDZ\Core\User;
use NERDZ\Core\Config;
use NERDZ\Core\Utils;
$user = new User();
try {
if(!$user->isLogged()) {
die(Utils::jsonResponse(['ERROR' => 'Not logged']));
}
if(!isset($_GET['action'])) {
die(Utils::jsonResponse(['ERROR' => 'Action not set']));
}
$thisUser = $user->getId();
if(!NERDZ\Core\Security::floodPushRegControl()) {
die(Utils::jsonResponse(['ERROR' => 'NO SPAM']));
}
$pushed = Pushed::connectIp(Config\PUSHED_PORT,Config\PUSHED_IP6);
$resp = [];
switch($_GET['action']) {
case 'subscribe':
if (!isset($_POST['service']) || !isset($_POST['deviceId'])) {
die(Utils::jsonResponse(['ERROR' => 'Field not set']));
}
$user->setPush($thisUser, true);
if(!$pushed->exists($thisUser)){
if($pushed->addUser($thisUser)[0] !== Pushed::$ACCEPTED) {
die(Utils::jsonResponse(['ERROR' => 'Request rejected']));
}
}
if($pushed->subscribe($thisUser, $_POST['service'], $_POST['deviceId'])[0] !== Pushed::$ACCEPTED) {
die(Utils::jsonResponse(['ERROR' => 'Request rejected']));
}
$resp = ['ACCEPTED' => 'Ok'];
break;
case 'unsubscribe': {
if (!isset($_POST['service']) || !isset($_POST['deviceId'])) {
die(Utils::jsonResponse(['ERROR' => 'Field not set']));
}
$user->setPush($thisUser, true);
if(!$pushed->exists($thisUser)){
die(Utils::jsonResponse(['ERROR' => 'No push for this user']));
}
if($pushed->unsubscribe($thisUser, $_POST['service'], $_POST['deviceId'])[0] !== Pushed::$ACCEPTED) {
die(Utils::jsonResponse(['ERROR' => 'Request rejected']));
}
$resp = ['ACCEPTED' => 'Ok'];
break;
}
default:
die(Utils::jsonResponse(['ERROR' => "Unknown request: '".addslashes($_GET['action'])."'"]));
}
} catch (PushedException $e) {
$resp = ['ERROR' => 'Internal Server Error'];
}
die(Utils::jsonResponse($resp));