-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.php
543 lines (449 loc) · 13.8 KB
/
log.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<?php
/* 你好. Hello.
README:
log script to add log entries. Like a blog or a calendar or an idonethis or song lyrics.
2014-04-06
MIT license.
MIT 授權規定.
******
The MIT License (MIT)
Copyright (c) 2014 Morgan Jassen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******
*/
/*
* parts gotten from phpLiteAdmin v1.0
*/
// this php script expects a database log.sqlite in the same directory, with a table in it to hold the data.
// the query to create table in sqlite database:
// CREATE TABLE 'log' (description TEXT)
//Edit the following variables to suit your needs
date_default_timezone_set('America/New_York');
//an array of databases that you want to manage by path name relative to this file
$databases = array
(
"log.sqlite"
);
$table_name = 'log';
$home_url_name = "<a href='log.php'>log</a>";
//End of user-editable fields and beginning of user-unfriendly source code
ini_set("display_errors", 1);
error_reporting(E_STRICT | E_ALL);
session_start();
//build the basename of this file
$nameArr = explode("?", $_SERVER['PHP_SELF']);
$thisName = $nameArr[0];
$nameArr = explode("/", $thisName);
$thisName = $nameArr[sizeof($nameArr)-1];
define("PAGE", $thisName);
define("PROJECT", "phpLiteAdmin");
//the various possible error/warning messages (some are go-home errors, others are just warnings)
$errorMessages = array
(
"Error: Your version of PHP, ".doubleval(phpversion())." does not contain the necessary packages - either PDO, SQLite3, or SQLite. You may not continue until you enable/intall these packages.", // 0
"Error: The database file specified does not exist. You may not continue until you correctly reference the path to the database file.", // 1
"Error: The database file cannot be read or written-to. Use chmod to alter the permissions.", // 2
"Warning: The database file can be read but not written-to. Attempts to INSERT and UPDATE will cause errors.", // 3
"used to be a password error msg.", // 4
"also used to be a password error msg.", // 5
"Error: You have not specified any databases.", //6
"You should never see this message" //7
);
//loose functions
//added this function to provide consistent data quoting.
define("TICK", "'");
function quoteIt($s)
{
$t = TICK;
for($u = 0; $u < strlen($s); $u++)
{
if ($s[$u] == TICK) $t .= TICK;
$t .= $s[$u];
}
$t .= TICK;
return $t;
}
//don't know what this does but it's important
function endsIn64($s)
{
if (strlen($s) < 3) return false;
return substr($s, -2) === "64";
}
//error message function to show error on screen
function errorMsg($msg, $exit)
{
echo "<div class='confirm' style='margin:15px;'>";
echo $msg;
echo "</div>";
if($exit)
exit();
}
//
// Database class
// Generic database class to manage interaction with database
//
class Database
{
protected $db; //reference to the DB object
protected $type; //the extension for PHP that handles SQLite
protected $name; //the filename of the database
public function __construct($filename)
{
$this->name = $filename;
if(class_exists("PDO")) //first choice is PDO
{
$this->type = "PDO";
$this->db = new PDO("sqlite:".$filename) or die("Error connecting to database");
}
else if(class_exists("SQLite3")) //second choice is SQLite3
{
$this->type = "SQLite3";
$this->db = new SQLite3($filename) or die("Error connecting to database");
}
else if(class_exists("SQLite")) //third choice is SQLite, AKA SQlite2 - some features may be missing and cause problems, but it's better than nothing :/
{
$this->type = "SQLite";
$this->db = new SQLite($filename) or die("Error connecting to database");
}
else //none of the possible extensions are enabled/installed and thus, the application cannot work
{
die("Your installation of PHP does not include a valid SQLite3 or PDO extension required by the application.");
}
}
public function __destruct()
{
$this->close();
}
//get the exact PHP extension being used for SQLite
public function getType()
{
return $this->type;
}
//get the filename of the database
public function getName()
{
return $this->name;
}
public function close()
{
if($this->type=="PDO")
$this->db = NULL;
else if($this->type=="SQLite3")
$this->db->close();
else if($this->type=="SQLite")
sqlite_close($this->db);
}
public function beginTransaction()
{
$this->query("BEGIN");
}
public function commitTransaction()
{
$this->query("COMMIT");
}
public function rollbackTransaction()
{
$this->query("ROLLBACK");
}
//generic query wrapper
public function query($query)
{
if($this->type=="SQLite")
$result = sqlite_query($this->db, $query);
else
$result = $this->db->query($query);
return $result;
}
//wrapper for an INSERT and returns the ID of the inserted row
public function insert($query)
{
$result = $this->query($query);
if($this->type=="PDO")
return $this->db->lastInsertId();
else if($this->type=="SQLite3")
return $this->db->lastInsertRowID();
else if($this->type=="SQLite")
return sqlite_last_insert_rowid($this->db);
}
//returns an array for SELECT
public function select($query)
{
$result = $this->query($query);
if($this->type=="PDO")
return $result->fetch();
else if($this->type=="SQLite3")
return $result->fetchArray();
else if($this->type=="SQLite")
return sqlite_fetch_array($result);
}
//returns an array of arrays after doing a SELECT
public function selectArray($query)
{
$result = $this->query($query);
if($this->type=="PDO")
return $result->fetchAll();
else if($this->type=="SQLite3")
{
$arr = array();
$i = 0;
while($res = $result->fetchArray())
{
$arr[$i] = $res;
$i++;
}
return $arr;
}
else if($this->type=="SQLite")
return sqlite_fetch_all($result);
}
//get number of rows in table
public function numRows($table)
{
$result = $this->select("SELECT Count(*) FROM ".$table);
return $result[0];
}
}
//
// View class
// Various functions to visually represent the database
//
class View
{
protected $db;
public function __construct($db)
{
$this->db = $db;
}
//generate the insert row view
public function generateInsert($table)
{
$query = "PRAGMA table_info('".$table."')";
$result = $this->db->selectArray($query);
echo "<form action='".PAGE."?table=".$table."&insert=1' method='post'>";
echo "<table border='0' cellpadding='2' cellspacing='1'>";
echo "<tr>";
echo "<td class='tdheader'>Field</td>";
//echo "<td class='tdheader'>Type</td>";
echo "<td class='tdheader'>Value</td>";
echo "</tr>";
for($i=0; $i<sizeof($result); $i++)
{
if ($i == 0) //if its the first row which i want to have the datestamp in it
{
/*
$tdWithClass = "<td class='td" . ($i%2 ? "1" : "2") . "'>";
echo "<tr>";
//for ($k=1; $k<3; $k++)
for ($k=1; $k<2; $k++)
echo $tdWithClass . $result[$i][$k]."</td>";
echo $tdWithClass;
// -g-> Changed to allow for big amounts of text.
echo date('Y-m-d H:i:s')."<input type='hidden' name='".$result[$i][1]."' value='".date('Y-m-d H:i:s')."' style='width:100px;'/> ";
//date('YmdHis');
//echo '<textarea name="'.$result[$i][1].'" wrap="hard" rows="3" cols="15"></textarea>';
echo "</td>";
echo "</tr>";
*/
$tdWithClass = "<td class='td" . ($i%2 ? "1" : "2") . "'>";
echo "<tr>";
//for ($k=1; $k<3; $k++)
for ($k=1; $k<2; $k++)
echo $tdWithClass . $result[$i][$k]."</td>";
echo $tdWithClass;
// -g-> Changed to allow for big amounts of text.
//echo "<input type='text' name='".$result[$i][1]."' value='".date('Y-m-d ').(date('H')+1).':00'."' style='width:250px;'/> ";
echo '<textarea name="'.$result[$i][1].'" wrap="hard" rows="3" cols="15">'.date('Y-m-d H:i').'</textarea>';
echo "</td>";
echo "</tr>";
}
/*
//taking out the second row
else{ //else its not the first row which i want to have the datestamp in it
$tdWithClass = "<td class='td" . ($i%2 ? "1" : "2") . "'>";
echo "<tr>";
//for ($k=1; $k<3; $k++)
for ($k=1; $k<2; $k++)
echo $tdWithClass . $result[$i][$k]."</td>";
echo $tdWithClass;
// -g-> Changed to allow for big amounts of text.
echo "<input type='text' name='".$result[$i][1]."' style='width:250px;'/> ";
//echo '<textarea name="'.$result[$i][1].'" wrap="hard" rows="3" cols="15"></textarea>';
echo "</td>";
echo "</tr>";
}
//end taking out the second row
*/
}
echo "</table>";
echo "<input type='submit' value='Insert'/>";
echo "</form>";
}
//generate the view rows view
public function generateView($table, $numRows, $startRow, $sort, $order)
{
$_SESSION['numRows'] = $numRows;
$_SESSION['startRow'] = $startRow;
// -g-> We need to get the ROWID to be able to find the row again. I put it at the end of the list of fields so that the numbering remained unchanged.
$query = "SELECT *, ROWID FROM ".$table;
$queryDisp = "SELECT * FROM ".$table;
$queryAdd = "";
$queryAdd .= " ORDER BY description";
$queryAdd .= " DESC";
$queryAdd .= " LIMIT ".$startRow.", ".$numRows;
$query .= $queryAdd;
$queryDisp .= $queryAdd;
$startTime = microtime(true);
$arr = $this->db->selectArray($query);
$endTime = microtime(true);
$time = round(($endTime - $startTime), 4);
$total = $this->db->numRows($table);
//if(!(sizeof($arr)>0))
//{
// echo "<br/><br/>This table is empty.";
// return;
//}
if(sizeof($arr)>0)
{
echo "<br/>";
echo "Showing rows ".$startRow." - ".($startRow + sizeof($arr)-1)." of ".$total."<br/>";
echo "<br/>";
}
else
{
echo "<br/><br/>This table is empty.";
return;
}
//echo "<form action='".PAGE."?edit=1&table=".$table."' method='post' name='checkForm'>";
echo "<table border='0' cellpadding='2' cellspacing='1'>";
$query = "PRAGMA table_info('".$table."')";
$result = $this->db->selectArray($query);
$rowidColumn = sizeof($result);
echo "<tr>";
//echo "<td colspan='3'>";
echo "</td>";
for($i=0; $i<sizeof($result); $i++)
{
echo "<td class='tdheader'>";
echo "<a href='".PAGE."?table=".$table."&sort=".$result[$i][1];
$orderTag = ($sort==$result[$i][1] && $order=="ASC") ? "DESC" : "ASC";
echo "&order=".$orderTag;
echo "'>".$result[$i][1]."</a>";
if($sort==$result[$i][1])
echo (($order=="ASC") ? " <b>↑</b>" : " <b>↓</b>");
echo "</td>";
}
echo "</tr>";
for($i=0; $i<sizeof($arr); $i++)
{
// -g-> $pk will always be the last column in each row of the array because we are doing a "SELECT *, ROWID FROM ..."
$pk = $arr[$i][$rowidColumn];
$tdWithClass = "<td class='td".($i%2 ? "1" : "2")."'>";
echo "<tr>";
for($j=0; $j<sizeof($result); $j++)
{
echo $tdWithClass;
// -g-> although the inputs do not interpret HTML on the way "in", when we print the contents of the database the interpretation cannot be avoided.
$inBase64 = endsIn64($arr[$i][$j]);
echo $inBase64 ? base64_decode($arr[$i][$j]) : $arr[$i][$j];
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
}
}
// here begins the HTML.
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<title><?php echo PROJECT ?></title>
<style type="text/css">
#wrapper {
max-width: 400px;
}
</style>
<style type="text/css">
/* table rows */
.td2 { background-color:#ddd; }
/* odd rows */
.td1 { background-color:#eee; }
/* table header */
.tdheader { background-color:#bbb;}
</style>
</head>
<body>
<div id="wrapper">
<?php
if(sizeof($databases)>0)
$DBFilename = $databases[0];
else
errorMsg($errorMessages[6], true);
if(isset($_POST['database_switch']))
{
$_SESSION['DBFilename'] = $_POST['database_switch'];
$DBFilename = $_POST['database_switch'];
}
if(isset($_SESSION['DBFilename']))
$DBFilename = $_SESSION['DBFilename'];
//First, check that the right classes are available
if(!class_exists("PDO") && !class_exists("SQLite3") && !class_exists("SQLite"))
errorMsg($errorMessages[0], true);
$db = new Database($DBFilename); //create the Database object
$dbView = new View($db);
//Switch board for various operations a user could have requested
if(isset($_POST['createtable'])) //ver 1.1 bug fix - check for $_POST variables before $_GET variables
{
// Not sure what is happening here... gkf
}
else if(isset($_GET['insert'])) //insert record into table
{
$query = "INSERT INTO ".$_GET['table']." (";
$inBase64 = array();
$i = 0;
foreach($_POST as $vblname => $value)
{
$inBase64[$i++] = endsIn64($vblname);
$query .= $vblname.",";
}
$query = substr($query, 0, sizeof($query)-2);
$query .= ") VALUES (";
$i = 0;
foreach($_POST as $vblname => $value)
{
if($value=="")
$query .= "NULL,";
else
$query .= quoteIt($inBase64[$i++] ? base64_encode($value) : $value) . ",";
}
$query = substr($query, 0, sizeof($query)-2);
$query .= ")";
$db->query($query);
}
echo "<div id='container'>";
echo "<div id='content'>";
echo "<div id='contentInner'>";
$view = "browse";
$table = $table_name;
echo "<div id='main'>";
echo $home_url_name;
$dbView->generateInsert($table);
$_SESSION['startRow'] = 0;
$_SESSION['numRows'] = 45;
if(!isset($_GET['sort']))
$_GET['sort'] = NULL;
if(!isset($_GET['order']))
$_GET['order'] = NULL;
$dbView->generateView($table, $_SESSION['numRows'], 0, $_GET['sort'], $_GET['order']);
echo "</div>";
echo "</div>";
echo "<br/>";
echo "</div>";
echo "</div>";
$db->close(); //close the database
echo "</div></body>";
echo "</html>";