forked from n8v/newfi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newfi.php
115 lines (93 loc) · 2.38 KB
/
newfi.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
<?php
date_default_timezone_set('America/Anchorage');
header('Content-type: text/html');
//$datafile = dirname($_SERVER{'SCRIPT_FILENAME'}) . '/newfi';
$datafile = './newfi';
$maxtries = 9;
$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(1000); // microseconds
}
while (!$file && $tries <= $maxtries);
if ($file) {
if (flock($file, LOCK_EX)) {
fwrite($file,$say,strlen($say));
flock($file, LOCK_UN);
fclose($file);
}
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);
}
$maxtries = 900; // about 90 seconds
$tries = 0;
if (isset($_REQUEST['g'])) {
$get = intval($_REQUEST['g']);
$skip_first_partial_line = false;
if ($get == 0) {
$stat = stat($datafile);
$get = max($stat['size'] - 8192, 0);
if ( $get > 0 ) {
$skip_first_partial_line = true;
}
}
$file = fopen($datafile,"r");
if ($file) {
do {
// $read = array($file);
// $write = NULL;
// $except = NULL;
if (flock($file, LOCK_SH)) {
fseek($file,$get,0);
// stream_select($read,$write,$except,1);
$data = fread($file,1000000);
$newsize = ftell($file);
flock($file, LOCK_UN);
}
else {
die("COULD NOT aquire shared lock.");
}
if ($newsize == $get) usleep(100000);
$tries++;
} while($newsize == $get && $tries <= $maxtries);
fclose($file);
$data = pretty($data);
if ($skip_first_partial_line) {
$data= preg_replace('/^[^\n]+/s', '', $data);
}
echo $newsize . ";" . $data;
}
else {
die("ERRAR. Could not open $datafile for reading.");
}
}