-
Notifications
You must be signed in to change notification settings - Fork 1
/
hydna-push.php
157 lines (107 loc) · 3.4 KB
/
hydna-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
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
<?php
class HydnaUtil{
const MAX_PAYLOAD_SIZE = 0xFFFA;
const DEFAULT_PORT = 80;
public static function clean_payload($data){
if(empty($data)){
throw new Exception("Payload expected");
}
if(mb_strlen($data, "UTF-8") > self::MAX_PAYLOAD_SIZE){
throw new Exception("Payload exceeds maximum length allowed");
}
return $data;
}
public static function clean_token($token){
if(mb_strlen($token, "UTF-8") > self::MAX_PAYLOAD_SIZE){
throw new Exception("Token exceeds maximum length allowed");
}
return $token;
}
public static function clean_prio($prio){
if(!is_numeric($prio)){
throw new Exception("Priority needs to be a number 0-3");
}
if($prio > 3 | $prio < 0){
throw new Exception("Priority needs to be 0-3");
}
return $prio;
}
public static function parse_uri($uri){
if(strpos($uri, "http" ) === false){
$uri = sprintf("http://%s",$uri);
}
$components = HydnaUtil::get_url_parts($uri);
if(empty($components['scheme'])){
throw new Exception("No url scheme found");
}
$token = HydnaUtil::clean_token($components['query']);
return array(
"scheme" => $components['scheme'],
"host" => $components['host'],
"channel" => $components['path'],
"token" => $token,
"port" => $components['port']
);
}
public static function get_url_parts($uri){
$components = parse_url($uri);
if(!array_key_exists("path", $components)){
$components['path'] = "/";
}
if(!array_key_exists("query", $components)){
$components['query'] = "";
}
if(!array_key_exists("port", $components)){
$components['port'] = self::DEFAULT_PORT;
}
return $components;
}
};
class Hydna{
public $agent = "hydna-php-push";
public static $TIMEOUT = 5;
public function push($domain, $data, $prio=0, $ctoken=""){
$headers = array(
'Content-Type: text/plain',
sprintf('User-Agent: %s', $this->agent)
);
$prio = HydnaUtil::clean_prio($prio);
$headers[] = sprintf('X-Priority: %s', $prio);
return $this->send($domain, $headers, $data);
}
public function emit($domain, $signal, $ctoken=""){
$headers = array(
'X-Emit: yes',
'Content-Type: text/plain',
sprintf('User-Agent: %s',$this->agent)
);
return $this->send($domain, $headers, $signal);
}
private function send($url, $headers, $data){
if(!extension_loaded('curl')){
die('Sorry cURL is not installed!');
}
$uri = HydnaUtil::parse_uri($url);
$curl_handle = curl_init();
$conn = sprintf('%s://%s:%d%s', $uri['scheme'], $uri['host'],
$uri['port'], $uri['channel']);
if(!empty($uri['token'])){
$conn .= sprintf('?%s', $uri['token']);
}
curl_setopt($curl_handle, CURLOPT_URL, $conn);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, Hydna::$TIMEOUT);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
$data = HydnaUtil::clean_payload($data);
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl_handle);
$code = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
curl_close($curl_handle);
if($code != 200){
throw new Exception("Error, response code: ".$code);
}
return true;
}
}
?>