-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
72 lines (64 loc) · 2.91 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
<?php
/*
* This is a script for storing and retrieving intermediate data on a server. You need an HTTP server with PHP and
* SQLITE3 support to make this work. On Ubuntu Linux you can simply use the packages apache2, libapache2-mod-php and
* php-sqlite3 to achieve that. You may want to enable mod_headers and set "Header set Access-Control-Allow-Origin *"
* to allow direct communication between the client and a server even if they are installed on different machines.
*
* Alternatively, a modern PHP IDE may also suffice. For example, PhpStorm (a commercial one) can be set up
* to use a php-cgi interpreter (e.g. the package php-cgi in Ubuntu), and then, after installing SQLITE3
* (the package php-sqlite3 in Ubuntu) and enabling it (in /etc/php/.../cgi/php.ini by uncommenting the
* setting for this extension), and finally restarting PhpStorm the internal PHP-CGI Server will take over
* all requirements.
*
* Please note that this script writes to the file $dbfile defined at the beginning of the script (see below).
* You can define a different path for that. The database schema will be created on the first run automatically.
*
* The server side accepts HTTP POST requests for registering, updating, getting heartbeat information
* and providing information to continue a job. These are handled on the client side when the URL of the
* script is given by the startup GET parameter z=..., in this mode RegularNGons will regularly restart
* itself and append the GET parameter c=... to the startup URL to retrieve data from the previous runs.
*
* A restart can be initiated directly as well, by using the same method, that is, by adding the GET
* parameter c=... directly to the startup URL.
*/
$dbfile = "/tmp/RegularNGons.sqlite";
if (!file_exists($dbfile)) {
$f = fopen($dbfile, "w");
fclose($f);
$db = new SQLite3($dbfile);
$db->exec("CREATE TABLE clients (id text, parameters text, ggb text, log text, changes text)");
} else {
$db = new SQLite3($dbfile);
}
$action = $_POST['action'];
if ($action == "register") {
$id = substr(md5(rand()), 0, 7);
echo $id;
$parameters = $_POST['parameters'];
$db->exec("insert into clients (id, parameters, log) values ('$id', '$parameters', '')");
die();
}
if ($action == "update") {
$id = $_POST['id'];
$text = $_POST['update'] . "\n";
$ggb = $_POST['ggb'];
$db->exec("update clients set log = log || '$text', ggb = '$ggb' where id = '$id'");
die();
}
if ($action == "heartbeat") {
$id = $_POST['id'];
$changes = $_POST['changes'] . "\n";
$db->exec("update clients set changes = '$changes' where id = '$id'");
die();
}
if ($action == "continue") {
$id = $_POST['id'];
$results = $db->query("SELECT changes || ';ggb=\"' || ggb || '\";fulllog=\"' || " .
"replace(log,'\n','\\n') || '\";' FROM clients where id = '$id'");
while ($row = $results->fetchArray()) {
echo $row[0];
}
die();
}
?>