-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnewfi.php
More file actions
147 lines (117 loc) · 3.38 KB
/
newfi.php
File metadata and controls
147 lines (117 loc) · 3.38 KB
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
<?php
/* For simulating DreamHost crappiness */
/*
if (rand(1,100) > 33 ) { // && isset($_REQUEST['s'])) {
sleep( 2 );
header($_SERVER['SERVER_PROTOCOL'] . " 504 Ain't nobody got time for that.", true, 504);
exit;
}
*/
$start_time = time();
date_default_timezone_set('America/Anchorage');
header('Content-type: text/html');
//$datafile = dirname($_SERVER{'SCRIPT_FILENAME'}) . '/newfi';
$datafile = './newfi';
$max_post_seconds = 10;
$retry_microseconds = 1000;
$maxtries = $max_post_seconds * 1000 * 1000 / $retry_microseconds;
$tries = 0;
if (isset($_REQUEST['s'])) {
$timeday = array();
$timeday = gettimeofday();
$say = strftime("%m-%d %H:%M") . ": " . $_REQUEST['s'] . "\r\n";
$file = false;
do {
$file = @fopen($datafile,"a");
// fseek($file,0,SEEK_END);
if (!$file) usleep($retry_microseconds);
}
while (!$file && $tries <= $maxtries);
if ($file) {
if (flock($file, LOCK_EX)) {
fwrite($file,$say,strlen($say));
flock($file, LOCK_UN);
fclose($file);
exit;
}
else {
die( "COULD NOT FLOCK $datafile");
}
}
else {
echo "COULD NOT OPEN $datafile FOR APPENDING after $tries tries!";
exit;
}
}
function pretty($string)
{
$newstring = array();
$newchars = 0;
for($i = 0 ; $i < strlen($string) ; $i++) {
// ascii delete or backspace
if ($string[$i] == "\x7f" || $string[$i] == "\x08") {
$newchars--;
$newstring[$newchars] = '';
}
else {
$newstring[$newchars++] = $string[$i];
}
}
// $string = preg_replace('/.\x7f/','',$string);
// $string = preg_replace('/\n/','<br>',$string);
return implode('',$newstring);
}
/*
However, the timeout value has to be chosen carefully; indeed,
problems can occur if this value is set too high (e.g., the client
might receive a 408 Request Timeout answer from the server or a 504
Gateway Timeout answer from a proxy). The default timeout value in a
browser is 300 seconds, but most network infrastructures include
proxies and servers whose timeout is not that long.
-- http://tools.ietf.org/html/draft-loreto-http-bidirectional-07#section-5.5
*/
$max_poll_seconds = 20;
$retry_microseconds = 100000;
$maxtries = $max_poll_seconds * (1000000 / $retry_microseconds);
$tries = 0;
$max_bytes_fistory = 8192;
if (isset($_REQUEST['g'])) {
$get = intval($_REQUEST['g']);
$skip_first_partial_line = false;
if ($get == 0) {
$stat = stat($datafile);
$get = max($stat['size'] - $max_bytes_fistory, 0);
if ( $get > 0 ) {
$skip_first_partial_line = true;
}
}
do {
usleep($retry_microseconds);
clearstatcache();
$stat = stat($datafile);
$seek_position = $stat['size'];
} while ($seek_position <= $get && $tries++ <= $maxtries && time() - $start_time < $max_poll_seconds - 1 ) ;
$file = fopen($datafile,"r");
if ($file) {
if (flock($file, LOCK_SH)) {
fseek($file,$get,0);
// stream_select($read,$write,$except,1);
$data = fread($file, $max_bytes_fistory);
$seek_position = ftell($file);
flock($file, LOCK_UN);
}
else {
die("COULD NOT aquire shared lock.");
}
fclose($file);
$data = pretty($data);
if ($skip_first_partial_line) {
$data= preg_replace('/^[^\n]+/s', '', $data);
}
echo $seek_position . ";" . $data;
exit;
}
else {
die("ERRAR. Could not open $datafile for reading.");
}
}