-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
140 lines (114 loc) · 3.07 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
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
<?php
/*
Original script is from http://www.jezra.net/blog/proof_of_concept_webserver_written_in_PHP
Modified for working with PFA by Aryes
*/
error_reporting(E_ALL);
ini_set('display_errors', 'On');
set_time_limit(0);
ob_implicit_flush();
date_default_timezone_set('UTC');
require_once("Response.php");
require_once("Request.php");
require_once("Android.php");
define('SITE_PATH', dirname($_SERVER['PHP_SELF']));
interface IHandler {
public function get ();
}
class Start implements IHandler {
public function get() {
$file = SITE_PATH . '/gui.html';
ob_start();
include $file;
$output = ob_get_contents();
ob_end_clean();
return $output;
}
}
class Stop implements IHandler {
public function get() {
global $running;
$running = false;
return "Daemon stopping.";
}
}
/*
function p1($args) {
global $droid;
$droid->vibrate(30);
return 'Button pushed';
}
function p2($args) {
global $droid;
$droid->vibrate(30);
return 'Button pushed';
}
function p3($args) {
global $droid;
$droid->vibrate(30);
return 'Button pushed';
}
function r1($args) {
global $droid;
$droid->vibrate(30);
return 'Button released';
}
function r2($args) {
global $droid;
$droid->vibrate(30);
return 'Button released';
}
function r3($args) {
global $droid;
$droid->vibrate(30);
return 'Button released';
}
*/
$droid = new Android();
$droid->notify("Starting", "Starting server...");
$bind_address = '127.0.0.1';
$port = 8000;
$server_socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("socket_create()failed.");
socket_bind($server_socket, $bind_address, $port) or die("socket_bind()failed");
socket_listen($server_socket, 5) or die("socket_listen()failed");
// Stop socket from handing when killed
socket_set_block($server_socket);
socket_set_option($server_socket, SOL_SOCKET, SO_LINGER, array('l_onoff' => 1, 'l_linger' => 0));
$droid->notify("Running", "Server is up and running ....");
$droid->view("http://localhost:$port/start", "text/html");
$running = true;
do {
if (($socket = socket_accept($server_socket)) < 0) {
echo "socket_accept() failed \r\n";
break;
}
$request = new Request();
$request->setSocket($socket);
$request->getBuffer();
$request->parseBuffer();
$response = new Response();
$response->setRequest($request);
if(class_exists($request->getUri())) {
$handler_name = $request->getUri();
$handler = new $handler_name();
$response->setHandler($handler);
} else {
// TODO: 404
echo "cannot find class " . $request->getUri();
}
$response->sendData();
unset($response);
} while ($running);
socket_shutdown($server_socket);
socket_close($server_socket);
try {
$droid->exit();
die("Script exited normally\r\n");
} catch (Exception $e) {
$droid->vibrate(30);
echo "Exception:" . $e->getMessage();
$droid->notify('Exception', $e->getMessage());
$droid->exit();
die("script exited with an exception");
}
?>