forked from TinyWebDB/tinywebdb-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
168 lines (158 loc) · 5.01 KB
/
index.php
File metadata and controls
168 lines (158 loc) · 5.01 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
if (isset($_SERVER['REQUEST_URI'])) {
$request = $_SERVER['REQUEST_URI'];
} else {
$request = substr($_SERVER['PHP_SELF'], 1);
if (isset($_SERVER['QUERY_STRING']) AND $_SERVER['QUERY_STRING'] != '') {
$request .= '?' . $_SERVER['QUERY_STRING'];
}
}
if (isset($_POST['action'])) {
$request = $_POST['action'] . '/';
}
{
header("HTTP/1.1 200 OK");
$path = parse_url ($request, PHP_URL_PATH);
$action = basename( $path );
switch ($action) {
case "getvalue": // this action enable from v 0.1.x
// JSON_API , Post Parameters : tag
$tagName = $_REQUEST['tag'];
$tagValue = '';
is_file($tagName . ".txt") && ($tagValue = file_get_contents($tagName . ".txt"));
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo json_encode(array(
"VALUE",
$tagName,
$tagValue
));
exit;
break;
case "storeavalue": // this action will enable from v 0.2.x
// JSON_API , Post Parameters : tag,value
$tagName = $_POST['tag'];
$tagValue = $_POST['value'];
$apiKey = ''; // $_POST['apikey'];
$log_message = sprintf("%s:%s\n", date('Y-m-d H:i:s'), "storeavalue: ($apiKey) $tagName -- $tagValue");
$file_name = 'tinywebdb_' . date('Y-m-d') . '.log';
error_log($log_message, 3, $file_name);
$setting_apikey = '';
if ($apiKey == $setting_apikey) {
if(strlen($tagValue) == 0) {
unlink($tagName . ".txt");
echo "Removed tagName: " . $tagName;
exit;
}
$fh = fopen($tagName . ".txt", "w") or die("check file write permission.");
fwrite($fh, $tagValue);
fclose($fh);
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo json_encode(array(
"STORED",
$tagName,
$tagValue
));
} else {
echo "check api key.";
}
exit;
break;
default:
break;
}
}
$listLog = array();
$listTxt = array();
if ($handler = opendir("./")) {
while (($sub = readdir($handler)) !== FALSE) {
if (substr($sub, -4, 4) == ".txt") {
$listTxt[] = $sub;
} elseif (substr($sub, 0, 10) == "tinywebdb_") {
$listLog[] = $sub;
}
}
closedir($handler);
}
include_once("main.html");
if (file_exists('tags.php')) echo "<h3>TinyWebDB <a href=tags.php>Tags</a></h3>";
else echo "<h3>TinyWebDB Tags</h3>";
echo "<table border=1>";
echo "<thead><tr>";
echo "<th> Tag </th>";
echo "<th> Value </th>";
echo "<th> Time </th>";
echo "<th> Size </th>";
echo "</tr></thead>\n";
if ($listTxt) {
sort($listTxt);
foreach ($listTxt as $sub) {
echo "<tr>";
echo "<td><a href=getvalue?tag=" . substr($sub, 0, -4) . ">" .substr($sub, 0, -4) . "</a></td>\n";
echo "<td>" ; readfile("./" . $sub) ; echo "</td>\n";
echo "<td>" . date('Y-m-d H:i:s',filemtime("./" . $sub)) . "</td>\n";
echo "<td>" . filesize("./" . $sub) . "</td>\n";
echo "</tr>";
}
}
echo "</table>";
echo "<h3>TinyWebDB Log Tail</h3>";
echo "<table border=1>";
echo "<thead><tr>";
echo "<th> Log Name </th>";
echo "<th> Size </th>";
echo "</tr></thead>\n";
if ($listLog) {
sort($listLog);
foreach ($listLog as $sub) {
echo "<tr>";
echo "<td><a href=?logfile=" . $sub . ">$sub</a></td>\n";
echo "<td>" . filesize("./" . $sub) . "</td>\n";
echo "</tr>";
}
}
echo "</table>";
if (isset($_GET['logfile'])) {
$logfile = substr($_GET['logfile'], 0, 24);
echo "<h2>Log file : " . $logfile . "</h2>";
if (file_exists($logfile)) {
if (file_exists('draw.php')) echo "<p><img src = 'draw.php?logfile=$logfile'></p>";
$lines = wp_tinywebdb_api_read_tail($logfile, 20);
foreach ($lines as $line) {
echo $line . "<br>";
}
}
}
exit; // this stops rest steps
function wp_tinywebdb_api_read_tail($file, $lines)
{
//global $fsize;
$handle = fopen($file, "r");
$linecounter = $lines;
$pos = -2;
$beginning = false;
$text = array();
while ($linecounter > 0) {
$t = " ";
while ($t != "\n") {
if (fseek($handle, $pos, SEEK_END) == -1) {
$beginning = true;
break;
}
$t = fgetc($handle);
$pos--;
}
$linecounter--;
if ($beginning) {
rewind($handle);
}
$text[$lines - $linecounter - 1] = fgets($handle);
if ($beginning)
break;
}
fclose($handle);
return array_reverse($text);
}