-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.php
125 lines (110 loc) · 2.78 KB
/
file.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
<?
//probably useless and overly paranoid file open routine.
function openfile($filename, $filemode)
{
$counter = 0;
$handle = fopen($filename, $filemode);
while (!$handle)
{
sleep(1);
$handle = fopen($filename, $filemode);
$counter++;
if ($counter > 5)
{
return FALSE;
}
}
return $handle;
}
//append the message to the file, erasing the log if it's older than today
function writeMessage($message)
{
$filename = 'log.txt';
$filemode = 'a';
if (file_exists($filename))
{
$modded = date("dmY", filemtime($filename));
$today = date("dmY");
if ($today != $modded)
{
$filemode = 'w';
}
}
$fp = openfile($filename, $filemode);
fwrite($fp, $message);
fwrite($fp, "\n");
fclose($fp);
}
//first compare the currentTime against the modified time, and only if it's different then continue processing.
//if there are more lines than the specified current line count, then return only the new lines.
function getMessage($currentTime, $currentLineCount)
{
$filename = 'log.txt';
$fileTime = filemtime($filename);
$counter = 0;
$contents = '';
$lineCount = 0;
$text = '';
if ($fileTime != $currentTime)
{
$contents = file_get_contents($filename);
}
if ($contents != '')
{
$lines = explode("\n", trim($contents));
$lineCount = count($lines);
for ($index = $currentLineCount; $index < $lineCount; $index++)
{
$text = $text . $lines[$index] . "\n";
}
}
$response = array();
$response['text'] = $text;
$response['fileTime'] = $fileTime;
$response['lineCount'] = $lineCount;
echo json_encode($response);
flush();
}
session_start();
$name = isset($_SESSION['name']) ? $_SESSION['name'] : '';
$message = isset($_POST['text']) ? $_POST['text'] : '';
$fileTime = isset($_POST['fileTime']) ? $_POST['fileTime'] : '';
$lineCount = isset($_POST['lineCount']) ? $_POST['lineCount'] : '';
//this is some server-side debug stuff.
if ($_SERVER['argc'] > 1)
{
$name = 'noshbar';
for ($commandLineIndex=1; $commandLineIndex < $_SERVER['argc']; $commandLineIndex++)
{
$commandLineArgument = explode('=', $_SERVER['argv'][$commandLineIndex]);
$commandLineArgumentKey = array_shift($commandLineArgument);
$commandLineArgumentValue = implode('=', $commandLineArgument);
switch($commandLineArgumentKey)
{
case '-text' :
$message = $commandLineArgumentValue;
break;
case '-fileTime' :
$fileTime = $commandLineArgumentValue;
break;
case '-lineCount' :
$lineCount = $commandLineArgumentValue;
break;
}
}
}
//if we don't have a valid session, don't do anything.
if ($name == '')
{
die();
}
if ($message != '')
{
writeMessage($message);
die();
}
if ($fileTime != '' && $lineCount != '')
{
getMessage(intval($fileTime), intval($lineCount));
}
?>