-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.php
55 lines (48 loc) · 1.26 KB
/
server.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
<?php
// Fix file/dir owners on startup
exec("/bin/cchown");
// Start Server
$sock = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
if( socket_bind( $sock, '0.0.0.0', '80' ) ){
if( socket_listen( $sock , 15 ) ){
while( true ){
// wait for connection and handle it
handle_connection( socket_accept( $sock ) );
}
}
else{
die('Unable to listen to Socket.');
}
}
else{
die('Unable to bind to Socket.');
}
// handle connection
function handle_connection( $socket ){
// read message
$data = socket_read( $socket, 1024, PHP_BINARY_READ );
// echo $data;
mail_telegram($data);
// log
$ipo = socket_getpeername( $socket, $ip );
echo "Request from " . ( $ipo ? $ip : '??' ) . " at " . date('r') . PHP_EOL;
// The answer
$message = 'OK' . PHP_EOL;
// Prepare Headers
$answer = 'HTTP/1.1 200 OK' . PHP_EOL .
'Date: ' . date('r') . PHP_EOL .
'Content-Type: text/plain;charset=utf-8' . PHP_EOL .
'Content-Length: ' . strlen( $message ) . PHP_EOL .
'Last-Modified: ' . date( 'r', filemtime( __DIR__ . '/server.php' ) ) . PHP_EOL .
'Connection: close'. PHP_EOL .
PHP_EOL .
$message;
// send answert
socket_write( $socket, $answer );
socket_close( $socket );
}
//do the mail => telegram
function mail_telegram($d){
require( __DIR__ . '/cron.php' );
}
?>