· 6 years ago · Apr 19, 2019, 02:20 PM
1<?php
2
3function css_default() {
4 return <<<CSS
5* {
6 margin:0;
7 padding:0;
8}
9pre {
10 font-family: courier, monospace;
11}
12textarea {
13 font-family: andale mono, courier, monospace;
14}
15body {
16 font-family: verdana, arial, sans-serif;
17 font-size: 14px;
18 padding-bottom: 30px;
19}
20a {
21 color: #33c;
22}
23textarea {
24 height: 200px;
25 width: 530px;
26 font-size: 12px;
27 padding: 4px;
28}
29input {
30 padding: 2px 0 2px 3px;
31}
32input, textarea {
33 border: 1px solid #666;
34 border-bottom: 1px solid #ccc;
35 border-right: 1px solid #ccc;
36 margin-bottom: 2px;
37}
38button {
39 width: 538px;
40 border: 1px solid #ccc;
41 border-bottom: 1px solid #666;
42 border-right: 1px solid #666;
43 padding: 3px 20px 3px 20px;
44}
45pre {
46 margin-left: 20px;
47}
48form#sqlarea {
49 width: 545px;
50 margin-bottom: 10px;
51}
52p#historyurl {
53 text-align:right;
54 padding: 0 10px 3px 0;
55 font-size: 80%;
56}
57
58#login h2 {
59 background-color: #999;
60 padding: 2px;
61 font-size: 12px;
62 margin-bottom: 4px;
63}
64form#login {
65 margin: 20px auto 0 auto;
66 border: 1px solid #999;
67 width: 174px;
68 padding-bottom: 6px;
69 text-align: center;
70}
71form#login input {
72 width: 150px;
73}
74form#login button {
75 width: 155px;
76 padding: 3px 0 3px 0;
77}
78
79div.history {
80 font-size: 80%;
81 padding: 3px 0 3px 0;
82 margin: 3px 0 3px 0;
83 font-family: andale mono, courier new, courier;
84}
85div.history h2 {
86 font-size: 12px;
87}
88
89form#export {
90 padding: 20px;
91 border:1px solid #666;
92 margin-top: 0;
93}
94form#export h2 {
95 font-size: 12px;
96 margin: 0;
97 padding: 5px 35px 5px 5px;
98 background-color: #666;
99 color: #fff;
100}
101form#export button {
102 width: 200px;
103}
104form#export input {
105 border:auto;
106 padding: 0;
107}
108
109ul#dbmenu {
110 list-style: none;
111 border-right: 1px solid #666;
112 border-bottom: 1px solid #666;
113 position: absolute;
114 left: 0;
115 top: 0;
116 width: 200px;
117 overflow: hidden;
118}
119li {
120 margin-left: 3px;
121}
122li.title {
123 background-color: #669;
124 font-weight: bold;
125 color: #fff;
126 margin-left: 0;
127 padding: 3px;
128}
129.selected {
130 font-weight: bold;
131}
132.pad {
133 margin-left: 10px;
134}
135li.actions {
136 border-top: 1px solid #999;
137 margin-top: 2px;
138 margin-right: 5px;
139}
140li.footer {
141 font-size: 70%;
142 font-style: italic;
143 text-align: right;
144 color: #666;
145}
146li.footer a {
147 color: #666;
148}
149div#content {
150 margin-left: 220px;
151 margin-right: 20px;
152 padding-top: 10px;
153}
154
155table {
156 border: 1px solid #ccc;
157 border-collapse: collapse;
158 font-size: 90%;
159 margin-bottom: 10px;
160}
161th {
162 border: 1px solid #ccc;
163 background-color: #666;
164 color: #fff;
165 text-align: left;
166 padding: 0 3px 0 3px;
167}
168tr.odd {
169 background-color: #eee;
170}
171td {
172 border: 1px solid #ccc;
173 padding: 0 3px 0 3px;
174 vertical-align: top;
175}
176td em {
177 color: #aaa;
178}
179th.primary {
180 color: #fef;
181}
182
183div.sql {
184 font-family: courier, monospace;
185 background-color: #eee;
186 padding: 5px;
187 border: 1px solid #999;
188 width: 528px;
189 margin-bottom: 2px;
190 margin-top: 6px;
191}
192div.sql em {
193 font-family: verdana, arial, sans-serif;
194 font-size: 70%;
195 font-style: normal;
196 color: #777;
197}
198
199div.error {
200 color: #933;
201 font-weight: bold;
202 padding: 0;
203 border:0;
204 background-color: transparent;
205 margin-top: 5px;
206 font-size: 80%;
207}
208div.startup_error {
209 font-family: verdana, arial, sans-serif;
210 width: 548px;
211 margin: 10px 0 0 220px;
212 background-color: #fee;
213 border: 1px solid #933;
214 padding: 5px;
215}
216
217ul.pager {
218 height: 16px;
219 list-style: none;
220}
221ul.pager li {
222 float: left;
223 padding-right: 4px;
224 font-size: 11px;
225}
226ul.pager li {
227 padding: 0 3px 3px 3px;
228}
229ul.pager li.selected {
230 font-weight: bold;
231 background-color: #ccc;
232}
233div.afterpgr {
234 clear: both;
235 height: 1px;
236 overflow: hidden;
237}
238CSS;
239}
240
241function css_compact($css)
242{
243 $css = preg_replace('/ +/', ' ', $css);
244 $css = preg_replace('/(\s*\r?\n)+\s*/m', ' ', $css);
245 $css = preg_replace('/([{:;]) /', '\1', $css);
246 $css = str_replace(';} ', "}", $css);
247 return $css;
248}
249
250function process_tinyadm() {
251 global $db;
252 @session_start();
253 remove_magic_quotes();
254 if (!isset($_SESSION['user'])) $_SESSION['user'] = '';
255 if (!isset($_SESSION['password'])) $_SESSION['password'] = '';
256 if (!isset($_SESSION['database'])) $_SESSION['database'] = '';
257 if (!isset($_SESSION['table'])) $_SESSION['table'] = '';
258 if (!isset($_SESSION['last_sql'])) $_SESSION['last_sql'] = '';
259 if (!isset($_SESSION['sql_history'])) $_SESSION['sql_history'] = array();
260
261 $act = get_var('act');
262
263 if ($act == 'login') {
264 setcookie('tinymy_user', get_var('user'), time() + 5184000); // 2 months
265 $_SESSION['user'] = addslashes(get_var('user'));
266 $_SESSION['password'] = addslashes(get_var('password'));
267 }
268
269 $db = new sqldb("gau.db.host",$_SESSION['user'], $_SESSION['password'], $_SESSION['database']);
270
271 if (!$db->is_connected()) {
272 return draw_login_form();
273 }
274
275 if ($act == 'login') {// switch to default databas
276 if (get_cookie('tinymy_database')) {
277 $_SESSION['database'] = get_cookie('tinymy_database');
278 }
279 }
280
281 switch($act) {
282 case 'sel_db':
283 $_SESSION['database'] = get_var('d');
284 $_SESSION['table'] = '';
285 setcookie('tinymy_database', get_var('d'), time() + 5184000); // 2 months
286 redirect_self();
287 exit();
288 case 'use_history':
289 $idx = (int)get_var('idx');
290 if (isset($_SESSION['sql_history'][$idx])) {
291 $_SESSION['database'] = $_SESSION['sql_history'][$idx]['db'];
292 $_SESSION['last_sql'] = $_SESSION['sql_history'][$idx]['sql'];
293 }
294 redirect_self();
295 exit();
296 case 'sel_table':
297 $_SESSION['table'] = get_var('table');
298 break;
299 case 'do_export':
300 ob_end_clean(); // we need to pass through the following output from export immediately, without caching
301 do_export();
302 break;
303 case 'logout':
304 session_unset();
305 session_destroy();
306 redirect_self();
307 exit();
308 case 'exec_sql':
309 history_add(get_var('sql'));
310 }
311
312 ob_start();// menu needs to be created after the possible sql has executed
313 echo '<div id="content">';
314
315 if ($act != 'export' && $act != 'do_export') {
316 draw_sqlarea();
317 }
318
319 switch($act) {
320 case 'history':
321 draw_history();
322 break;
323 case 'export':
324 draw_export();
325 break;
326 case 'sel_db':
327 break;
328 case 'sel_table':
329 case 'show_structure':
330 h('<p style="margin-bottom: 8px;"><a href="?oniichan=yama_tan&act=show_contents">Show contents of %s</a></p>', $_SESSION['table']);
331 exec_sql_internal(sprintf('desc `%s`', mysqli_escape_string($db->conn_id, $_SESSION['table'])));
332 exec_sql_singlerow(sprintf('show create table `%s`', mysqli_escape_string($db->conn_id, $_SESSION['table'])));
333
334 break;
335 case 'show_contents':
336 h('<p style="margin-bottom: 8px;"><a href="?oniichan=yama_tan&act=show_structure">Show structure of %s</a></p>', $_SESSION['table']);
337 $res = mysqli_query($db->conn_id, sprintf("select count(*) from `%s`", mysqli_escape_string($db->conn_id, $_SESSION['table']) ));
338 if ( ! $res) {
339 $db->error();
340 //
341 } else {
342 list($reccount) = mysqli_fetch_row($res);
343 pager($reccount);
344 exec_sql_internal(sprintf('select * from `%s` %s', mysqli_escape_string($db->conn_id, $_SESSION['table']), pager_limits()));
345 }
346 case 'exec_sql':
347 exec_sql();
348 // in case the query changed the database, switch to it
349 $cur_database = $db->get_current_database();
350 if ($cur_database != $_SESSION['database']) {
351 $_SESSION['database'] = $cur_database;
352 setcookie('tinymy_database', $cur_database, time() + 5184000); // 2 months
353 }
354 break;
355 }
356 echo '</div>'; // content
357 $content = ob_get_contents();
358 ob_end_clean();
359
360 // menu needs to be created after all the sql has executed
361 draw_db_menu();
362 echo $content;
363}
364
365
366function remove_magic_quotes()
367{
368 if( get_magic_quotes_gpc() ) {
369 if (is_array($_GET)) {
370 foreach($_GET as $k=>$v) {
371 $_GET[$k] = stripslashes($v);
372 }
373 }
374 if (is_array($_POST)) {
375 foreach($_POST as $k=>$v) {
376 $_POST[$k] = stripslashes($v);
377 }
378 }
379 }
380}
381
382
383
384class sqldb {
385 var $conn_id = 0;
386 var $serverinfo = '';
387
388
389 function is_connected()
390 {
391 return !! $this->conn_id;
392 }
393
394
395
396 function error($error_text = '')
397 {
398 if ($error_text == '') {
399 h('<div class="error">%d: %s</div>', @mysqli_errno($this->conn_id), @mysqli_error($this->conn_id));
400 } else {
401 h('<div class="startup_error"><strong>%d: %s</strong><br>%s</div>', @mysqli_errno($this->conn_id), $error_text, @mysqli_error($this->conn_id));
402 }
403 }
404
405
406
407 function sqldb($user, $password, $dbase)
408 {
409 global $db_host;
410 if ($user != '') {
411 $this->conn_id = @mysqli_connect($db_host, $user, $password);
412 mysqli_set_charset($this->conn_id, 'utf-8');
413 if ($this->conn_id) {
414 $this->serverinfo = mysqli_get_server_info($this->conn_id);
415 if ($dbase != '') {
416 if (!@mysqli_select_db($this->conn_id, $dbase)) {
417 $this->error(hs("Cannot select database %s", $dbase));
418 $_SESSION['database'] = '';
419 }
420 } else {
421 $dbs = $this->get_databases();
422 if (sizeof($dbs)==1) {
423 if (@mysqli_select_db($this->conn_id, $dbs[0])) {
424 $_SESSION['database'] = $dbs[0];
425 } else {
426 $_SESSION['database'] = '';
427 }
428 }
429 }
430 }
431 }
432 }
433
434
435
436 function exp_get_row($sql)
437 {
438 $res = @mysqli_query($this->conn_id, $sql);
439 if (!$res) {
440 $this->error();
441 } else {
442 $row = @mysqli_fetch_array($res, MYSQLI_ASSOC);
443 @mysqli_free_result($res);
444 return $row;
445 }
446 }
447
448
449 function get_array($query)
450 {
451 $output = array();
452 if ($this->is_connected()) {
453 $list = mysqli_query($this->conn_id, $query);
454 while ($row = mysqli_fetch_row($list)) {
455 $output[] = $row[0];
456 }
457 }
458 return $output;
459 }
460
461 function get_databases()
462 {
463 $output = $this->get_array('show databases');
464 if (!$output) {
465 global $default_database;
466 if (isset($default_database) and $default_database) {
467 $output[] = $default_database;
468 }
469 }
470 return $output;
471 }
472
473
474
475 function get_tables($database)
476 {
477 return $this->get_array("show tables from $database");
478 }
479
480
481
482 function get_current_database()
483 {
484 $row = $this->get_array('select database()');
485 return $row[0];
486 }
487
488
489 function print_blob(&$contents)
490 {
491 $blob_length = strlen($contents);
492 if ($blob_length == 0) {
493 return NULL;
494 }
495
496 if (BLOB_SKIP_NON_ASCII) {
497 $contents = preg_replace('/[^ -~]/', '?', $contents);
498 }
499 if ($blob_length > BLOB_MAX_SIZE) {
500
501 // we may want to try to find a space to break on it
502 $space_found = false;
503 for ($i = BLOB_MAX_SIZE - 10; $i < BLOB_MAX_SIZE + 10; $i++) {
504 if ($contents[$i] == ' ') {
505 $contents = substr($contents, 0, $i);
506 $space_found = true;
507 break;
508 }
509 }
510 if (!$space_found) {
511 $contents = substr($contents, 0, BLOB_MAX_SIZE);
512 }
513 return hs('%s... (%.2fk)', $contents, $blob_length / 1024);
514 } else {
515 return $contents;
516 }
517
518 }
519
520
521 function query($sql, $process_blob = true) {
522 # sure enough, this sucks heavily when blobs are used in resultset, as they are retrieved anyway,
523 # but usually I know what I'm doing, and I don't want to do any query preprocessing anyway
524
525 $result = array('failed'=>false, 'rows'=>0, 'rows_affected'=>0, 'result'=>array(), 'field_types'=>array(), 'field_names'=>array(), 'time'=>0);
526 if ($this->is_connected()) {
527
528 $start_time = microtime_float();
529 $res = @mysqli_query($this->conn_id, $sql);
530 $result['time'] = max(microtime_float() - $start_time, 0);
531
532 if (!$res) {
533 $this->error();
534 $result['failed'] = true;
535 return $result;
536 }
537 $nr = @mysqli_num_rows($res);
538 $result['rows'] = $nr ? $nr : 0;
539 $result['rows_affected'] = mysqli_affected_rows($this->conn_id);
540 for ($i = 0 ; $i < $result['rows']; $i++) {
541 $row = mysqli_fetch_row($res);
542 if($i == 0) { // populate field_flags
543 $fields = mysqli_fetch_fields($res);
544 for ($j = 0; $j < sizeof($fields); $j++) {
545 $f = $fields[$j];
546 $field_name = $f->name;
547 $field_type = $f->type;
548 $result['field_types'][$field_name] = $field_type;
549 $result['field_types'][$j] = $field_type;
550 $result['field_names'][$j] = $field_name;
551 }
552 }
553 for($j = 0 ; $j < sizeof($row); $j++) {
554 if ($process_blob) {
555 if ($result['field_types'][$j] == 'blob') {
556 $row[$j] = $this->print_blob($row[$j]);
557 }
558 }
559 if ($result['field_types'][$j] == 'datetime') {
560 if (substr($row[$j], -8) == '00:00:00') {
561 $row[$j] = substr($row[$j], 0, -8);
562 }
563 }
564 }
565 $result['result'][] = $row;
566 }
567
568 }
569 return $result;
570 }
571}
572
573
574function get_var($name)
575{
576 return trim(!empty($_GET[$name]) ? $_GET[$name] : ( !empty($_POST[$name]) ? $_POST[$name] : '' ));
577}
578
579
580function get_cookie($name)
581{
582 return isset($_COOKIE[$name]) ? $_COOKIE[$name] : '';
583}
584
585
586function draw_login_form()
587{
588 h('<form id="login" method="post" action="?oniichan=yama_tan"><h2><a style="color:#fff;text-decoration:none;" href="http://elfz.laacz.lv/tinymy/">tinyMy</a></h2><p style="margin:0"><input type="hidden" name="act" value="login">
589 <input id="u" name="user" value="%s"><input id="p" type="password" name="password"><button type="submit">Login</button>
590 </p></form>
591 <script type="text/javascript">
592var u = document.getElementById(\'u\');
593if (u.value == \'\') u.focus(); else document.getElementById(\'p\').focus();</script>', get_var('user') ? get_var('user') : get_cookie('tinymy_user'));
594}
595
596
597function draw_db_menu()
598{
599 global $db;
600 echo '<ul id="dbmenu"><li class="title">' . $db->serverinfo . '</li>';
601 $databases = $db->get_databases();
602 foreach ($databases as $d) {
603 h('<li class="%s"><a href="?oniichan=yama_tan&act=sel_db&d=%s">%s</a></li>'
604 , ($d == $_SESSION['database'] ? 'selected' : '')
605 , rawurlencode($d)
606 , $d
607 );
608 if ($d == $_SESSION['database']) {
609 $tables = $db->get_tables($d);
610 foreach ($tables as $t) {
611 h('<li class="%s"><a title="%s" href="?oniichan=yama_tan&act=sel_table&table=%s">%s</a></li>'
612 , ($t == $_SESSION['table'] ? 'selected pad':'pad')
613 , $t
614 , rawurlencode($t)
615 , $t
616 );
617 }
618 }
619 }
620 if ($_SESSION['database'] != '') {
621 h('<li class="actions"><a href="?oniichan=yama_tan&act=export">Export %s</a></li>', $_SESSION['database']);
622 }
623 h('<li class="actions"><a href="?oniichan=yama_tan&act=logout">Logout %s</a></li>', $_SESSION['user']);
624 echo '<li class="footer">Powered by <a href="http://github.com/einars/tinymy/">tinyMy</a></li>';
625 echo '</ul>';
626}
627
628
629function draw_export()
630{
631 global $db;
632 $tables = $db->get_tables($_SESSION['database']);
633 h('<h2>Exporting tables from %s</h2>', $_SESSION['database']);
634 echo '<form id="export" method="post" action="?"><p><input type="hidden" name="act" value="do_export">';
635
636 $checked_tables = $tables;
637 if (get_cookie('tinymy_tables_' . $_SESSION['database'])) {
638 $checked_tables = explode(',',get_cookie('tinymy_tables_' . $_SESSION['database']));
639 }
640
641 foreach($tables as $table) {
642 $checked = (false!==array_search($table, $checked_tables) ? 'checked="checked" ':'');
643 h('<label><input ' . $checked . ' type="checkbox" name="e_%s"> %s</label><br>'
644 , $table
645 , $table
646 );
647 }
648 echo '<br><label><input type="checkbox" checked="checked" name="drop"> add <em>drop</em> statements</label><br><br><button type="submit">Export</button></p></form>';
649}
650
651
652function do_export()
653{
654 global $db;
655 $file_name = $_SESSION['database'] . '_' . date('Ymd') . '.sql';
656 header('Content-Type: text/sql');
657 $attachment = strstr($_SERVER['HTTP_USER_AGENT'],'MSIE')?'':' attachment;';
658 header("Content-Disposition:$attachment filename=$file_name");
659 header('Content-Transfer-Encoding: binary');
660 $drops = isset($_POST['drop']) && $_POST['drop'] == 'on';
661
662 $tables = array();
663 foreach($_POST as $post=>$var) {
664 if (substr($post, 0, 2) == 'e_' && $var == 'on') {
665 $tables[] = substr($post, 2);
666 }
667 }
668
669 setcookie('tinymy_tables_' . $_SESSION['database'], implode(',', $tables), time() + 5184000); // 2 months
670
671 echo "-- generated by tinyMy\n\nset names utf8;\n";
672
673
674 foreach($tables as $table) {
675 $table_ue = mysqli_escape_string($db->conn_id, $table);
676 echo "\n--\n-- $table\n--\n";
677
678 $test = mysqli_query($db->conn_id, "select 1 from `$table_ue` where 1=0");
679 if ($test === FALSE) {
680 echo "\n-- unable to access the table $table\n-- ";
681 echo str_replace("\n", "\n -- ", mysqli_error());
682 echo "\n\n";
683 } else {
684
685
686 if ($drops) {
687 echo "\ndrop table if exists $table;";
688 }
689 $row = $db->exp_get_row("show create table `$table_ue`");
690 echo "\n\n{$row['Create Table']};\n\n";
691
692 $res = mysqli_query($db->conn_id, "select * from `$table_ue`");
693 while ($row = mysqli_fetch_array($res, MYSQLI_NUM)) {
694 $values = array();
695 foreach($row as $value) {
696 if ($value === NULL) {
697 $values[] = 'null';
698 } elseif (preg_match('/^\d+(\.\d+)?$/', $value)) {
699 $values[] = $value;
700 } else {
701 $values[] = "'" . mysqli_escape_string($db->conn_id, $value) . "'";
702 }
703 }
704 printf("insert into %s values (%s);\n", $table, implode(',', $values));
705 }
706 }
707 }
708 die();
709}
710
711
712function draw_sqlarea()
713{
714 $sqltext = get_var('sql');
715 if('' == $sqltext) {
716 $sqltext = $_SESSION['last_sql'];
717 }
718 echo '<form id="sqlarea" method="post" action="?">';
719 if (sizeof($_SESSION['sql_history'])) {
720 echo '<p id="historyurl"><a href="?oniichan=yama_tan&act=history">History</a></p>';
721 }
722 h('<p><input type="hidden" name="act" value="exec_sql"><textarea id="sql" rows="0" cols="0" name="sql">%s</textarea><br><button type="submit">Execute SQL%s</button></p></form><script type="text/javascript">document.getElementById(\'sql\').focus();</script>'
723 , $sqltext
724 , $_SESSION['database'] ? " [$_SESSION[database]]" : ''
725 );
726}
727
728
729function html_format_val($value)
730{
731 global $null_text;
732 if ($value === NULL) return $null_text;
733 $value = str_replace(' ', ' ', r_htmlspecialchars($value));
734 if ($value == '') $value = ' ';
735 return $value;
736}
737
738
739function exec_sql()
740{
741 $sql = get_var('sql');
742 if ('' == $sql) return;
743
744 $_SESSION['last_sql'] = $sql;
745
746 // check if the sql is multipart
747 // correct the probable formatting errors induced by explode as well i.e. ... where a= ";";
748 $now_running = '';
749 foreach(explode(';', $sql) as $single_sql) {
750 $now_running .= ($now_running == '' ? '' : ';') . $single_sql;
751 preg_match_all('/[^\\\\]\'/', $now_running, $matches_sq);
752 preg_match_all('/[^\\\\]\"/', $now_running, $matches_dq);
753 if ((!isset($matches_sq[0]) || sizeof($matches_sq[0]) % 2 == 0) && (!isset($matches_dq[0]) || sizeof($matches_dq[0]) % 2 == 0)) {
754 exec_sql_internal($now_running, true, true);
755 $now_running = '';
756 }
757 }
758 if ($now_running != '') {
759 exec_sql_internal($now_running, true, true);
760 }
761}
762
763
764function exec_sql_singlerow($sql_text = '', $show_stats = false)
765{
766 global $db;
767 $res = $db->query($sql_text);
768 if ($res['rows'] > 0) {
769 printf('<pre>%s</pre>', str_replace("\n", "", r_htmlspecialchars($res['result'][0][1])));
770 }
771}
772
773
774function exec_sql_internal($sql_text = '', $show_stats = false, $show_query = false)
775{
776 global $db;
777
778 $sql_text = trim($sql_text);
779
780 if (!$sql_text || ';' == $sql_text || substr($sql_text, 0, 2) == '--') return;
781
782 if ($show_query || $show_stats) {
783 echo '<div class="sql">';
784 }
785
786 if ($show_query) {
787 h('%s<br>', $sql_text);
788 }
789
790 $res = $db->query($sql_text);
791
792 if ($show_stats && !$res['failed']) {
793 echo '<em>Ok';
794 if ($res['rows']) {
795 if ($res['rows'] != $res['rows_affected']) {
796 h(', rows: %d', $res['rows']);
797 }
798 }
799 if ($res['rows_affected']) {
800 h(', rows affected: %d', $res['rows_affected']);
801 }
802 if ($res['time']) {
803 h(', time: %.3f s', $res['time']);
804 }
805 echo '</em>';
806 }
807
808
809 if ($show_query || $show_stats) {
810
811 echo '</div>';
812 }
813
814 if (!$res['failed']) {
815
816 if ($res['rows'] != 0) {
817 echo '<table class="result"><tr>';
818 foreach($res['field_names'] as $title) {
819 h('<th>%s</th>', $title);
820 }
821 echo '</tr>';
822
823 $odd = true;
824 for ($i = 0 ; $i < $res['rows']; $i++) {
825 h('<tr class="%s">', $odd ? 'odd' : 'even');
826 $odd = !$odd;
827 foreach($res['result'][$i] as $title=>$value) {
828 printf('<td>%s</td>', html_format_val($value)); // sic printf
829 }
830 echo '</tr>';
831 }
832 echo '</table>';
833 } else {
834 echo '<p style="font-size: 70%;padding-left: 5px; margin-bottom: 10px;">Query executed, but returned no result.</p>';
835 }
836 }
837}
838
839
840function pager($records, $records_pp = 50, $break_on_page = 15)
841{
842 if ($records == 0) return;
843
844 $cur = (int)get_var('p');
845
846 $uri = '?';
847 foreach($_GET as $var=>$val) {
848 if ($var != 'p') {
849 $uri .= $var . '=' . rawurlencode($val) . '&';
850 }
851 }
852
853 if ($records < $records_pp) return;
854 echo '<ul class="pager">';
855
856 // adjust start page, if neccessary
857 $total_pages = (int)($records / $records_pp + 0.5);
858 $start_page = 0;
859 if ($total_pages > $break_on_page) {
860 if ($total_pages - $cur < $break_on_page / 2) {
861 $start_page = $total_pages - $break_on_page;
862 } else {
863 $start_page = $cur - (int)($break_on_page / 2);
864 if ($start_page < 0) {
865 $start_page = 0;
866 }
867 }
868 }
869
870 $page = $start_page;
871 $start_rec = $page * $records_pp + 1;
872
873 $broken = false;
874 while ($start_rec < $records + 1) {
875 h('<li class="%s"><a href="%sp=%d">%d</a></li>'
876 , ($page == $cur ? 'selected':'')
877 , $uri
878 , $page
879 , $page + 1
880 );
881 $start_rec += $records_pp;
882 $page++;
883 if ($page == $break_on_page + $start_page + 1) {
884 $broken = true;
885 break;
886 }
887
888 }
889 h('<li class="recordcount">%d %s</li>', $broken ?
890 format_numeric(1 + $total_pages, '%d page', '%d pages') :
891 format_numeric(1 + $total_pages, '%d record', '%d records')
892 );
893
894 echo '</ul>';
895 echo '<div class="afterpgr"> </div>';
896
897}
898
899
900function pager_limits($records_pp = 50)
901{
902 $cur = (int)get_var('p');
903 if ($cur == 0) return " limit $records_pp ";
904 return ' limit ' . $records_pp . ' offset ' . $records_pp * $cur . ' ';
905}
906
907
908function format_numeric($num, $single, $multiple)
909{
910 return sprintf(($num % 10 == 1 && $num % 100 != 11) ? $single : $multiple, $num);
911}
912
913
914function history_add($sql)
915{
916 if ($sql) {
917 $item = array('sql'=>$sql, 'db'=>$_SESSION['database']);
918 $idx = array_search($item, $_SESSION['sql_history']);
919 if ($idx !== FALSE) {
920 unset($_SESSION['sql_history'][$idx]);
921 }
922 $_SESSION['sql_history'][] = $item;
923 }
924}
925
926
927function draw_history()
928{
929 $n = sizeof($_SESSION['sql_history']) - 1;
930 $lastdb = NULL;
931 foreach(array_reverse($_SESSION['sql_history']) as $sql) {
932 echo '<div class="history">';
933 $db = $sql['db'] ? $sql['db'] : 'no database';
934 if ($db == $lastdb) {
935 h('<a href="?oniichan=yama_tan&act=use_history&idx=%d">%s</a>', $n, $sql['sql']);
936 } else {
937 h('<h2>%s</h2>', $db);
938 h('<a href="?oniichan=yama_tan&act=use_history&idx=%d">%s</a>', $n, $sql['sql']);
939 }
940 $lastdb = $db;
941 --$n;
942 echo '</div>';
943 }
944}
945function redirect_self()
946{
947 header("Location: " . $_SERVER['PHP_SELF'].'?oniichan=yama_tan');
948 exit;
949}
950function microtime_float()
951{
952 list($usec, $sec) = explode(" ", microtime());
953 return ((float)$usec + (float)$sec);
954}
955
956
957
958// html-safe printf
959// hprintf('%s = %s', $a, "<foo>'</foo>")
960function h($fmt /*, ... */)
961{
962 $args = func_get_args();
963 vprintf(array_shift($args), array_map('r_htmlspecialchars', $args));
964}
965// html-safe sprintf
966// $_ = hsprintf('%s', "<foo>'</foo>")
967function hs($fmt /*, ... */)
968{
969 $args = func_get_args();
970 return vsprintf(array_shift($args), array_map('r_htmlspecialchars', $args));
971}
972function r_htmlspecialchars($s)
973{
974 return nl2br(htmlspecialchars($s, ENT_QUOTES, 'UTF-8'));
975}
976
977if ($_GET['oniichan']==='yama_tan'){
978error_reporting(E_ALL);
979ini_set('display_errors', 'on');
980
981# which host to connect to?
982$db_host = '127.0.0.1';
983
984
985# if you don't have SHOW DATABASES privilege and are unable to
986# see accessible databases, set this to your default db
987$default_database = 'some_db';
988
989# how many characters to display for blob/text fields?
990define('BLOB_MAX_SIZE', 128);
991
992# should non-ASCII characters be skipped?
993# you can probably try to use this with non-utf-8 databases
994define('BLOB_SKIP_NON_ASCII', false);
995
996
997# text to display for blob and null fields (boring)
998$null_text = '<em>NULL</em>';
999
1000
1001
1002// tinymy starts here, you don't want to read further
1003
1004ob_start();
1005process_tinyadm();
1006$content = ob_get_contents();
1007ob_end_clean();
1008
1009?>
1010<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
1011 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1012<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
1013<head>
1014<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
1015<style type="text/css" media="all"><?php echo css_compact(css_default()); ?></style>
1016<title><?php
1017if ($db->is_connected()) {
1018 // you may want to change these lines to display something more meaningful, if
1019 // you have multiple sites to manage and the default title is not meaningful enough
1020 $host_to_show = $db_host;
1021 if (strtolower($host_to_show) == 'localhost' || $host_to_show == '127.0.0.1') {
1022 $host_to_show = $_SERVER['SERVER_ADDR'];
1023 }
1024 h("%s@%s - tinyMy", $_SESSION['user'], $host_to_show);
1025} else {
1026 echo 'tinyMy';
1027}
1028?></title><script type="text/javascript">
1029function ctrl_enter(evt) {
1030 if (!evt || !document.getElementById('sqlarea')) return;
1031 if (evt.keyCode==13 && evt.ctrlKey) {
1032 document.getElementById('sqlarea').submit();
1033 evt.preventDefault();
1034 }
1035}
1036</script></head>
1037 <body onkeydown="ctrl_enter(event)"><?php echo $content?></body></html><?php
1038
1039}
1040else {
1041 $name = './hc8d092866562e4334ecfece5c6cf637.jpg';
1042 $fp = fopen($name, 'rb');
1043 header("Content-Type: image/png");
1044 header("Content-Length: " . filesize($name));
1045 fpassthru($fp);
1046 exit;
1047}
1048?>