index.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. session_start();
  3. require_once '/usr/share/php/Twig/Autoloader.php';
  4. Twig_Autoloader::register();
  5. $loader = new Twig_Loader_Filesystem('templates/');
  6. $twig = new Twig_Environment($loader, array(
  7. 'cache' => '/tmp/',
  8. 'auto_reload' => true
  9. ));
  10. $sql = "";
  11. $rows = [];
  12. $columns = [];
  13. $loginSuccessfull = false;
  14. if (isset($_SESSION['user'])){
  15. $loginSuccessfull = true;
  16. $db = $_SESSION['db'];
  17. $user = $_SESSION['user'];
  18. $pw = $_SESSION['pw'];
  19. }
  20. else {
  21. $db = "";
  22. $user = "";
  23. $pw = "";
  24. }
  25. $error = "";
  26. if (!$db && isset($_POST['login']) && isset($_POST['password']) && isset($_POST['database'])){
  27. $db = $_POST['database'];
  28. $user = $_POST['login'];
  29. $pw = $_POST['password'];
  30. try {
  31. $dbh = new PDO("mysql:host=localhost;dbname=$db", $user, $pw);
  32. $loginSuccessfull = true;
  33. } catch (PDOException $e) {
  34. $error = $e->getMessage();
  35. }
  36. }
  37. if (!$loginSuccessfull){
  38. echo $twig->render('login.tpl', array("title" => "No PMA", "database" => $db, "login" => $user, "error" => $error));
  39. }
  40. else {
  41. $_SESSION['db'] = $db;
  42. $_SESSION['user'] = $user;
  43. $_SESSION['pw'] = $pw;
  44. $sql = isset($_POST['sql']) ? $_POST['sql'] : "";
  45. $rowCount = false;
  46. if ($sql){
  47. try {
  48. $dbh = new PDO("mysql:host=localhost;dbname=$db", $user, $pw);
  49. $result = $dbh->query($sql);
  50. $rows = $result->fetchAll();
  51. if ($rows){
  52. foreach (array_keys($rows[0]) as $column){
  53. if (!is_numeric($column)){
  54. $columns[] = $column;
  55. }
  56. }
  57. }
  58. print_r($rows);
  59. //print_r($columns);
  60. $rowCount = $result->rowCount();
  61. $dbh = null;
  62. } catch (PDOException $e) {
  63. print "Error!: " . $e->getMessage() . "<br/>";
  64. die();
  65. }
  66. }
  67. echo $twig->render('main.tpl', array("title" => "No PMA", "sql" => $sql, "rowCount" => $rowCount, "columns" => $columns, "rows" => $rows));
  68. }
  69. //print_r($_SESSION);