me@helium 8 years ago
parent
commit
644122d7f1
4 changed files with 144 additions and 0 deletions
  1. 76 0
      nopma/index.php
  2. 32 0
      nopma/templates/layout.tpl
  3. 9 0
      nopma/templates/login.tpl
  4. 27 0
      nopma/templates/main.tpl

+ 76 - 0
nopma/index.php

@@ -0,0 +1,76 @@
+<?php
+session_start();
+require_once '/usr/share/php/Twig/Autoloader.php';
+Twig_Autoloader::register();
+$loader = new Twig_Loader_Filesystem('templates/');
+$twig = new Twig_Environment($loader, array(
+            'cache' => '/tmp/',
+            'auto_reload' => true
+            ));
+
+$sql = "";
+$rows    = [];
+$columns = [];
+$loginSuccessfull = false;
+if (isset($_SESSION['user'])){
+    $loginSuccessfull = true;
+    $db   = $_SESSION['db'];
+    $user = $_SESSION['user'];
+    $pw   = $_SESSION['pw'];
+}
+else {
+    $db   = "";
+    $user = "";
+    $pw   = "";
+}
+$error = "";
+
+if (!$db && isset($_POST['login']) && isset($_POST['password']) && isset($_POST['database'])){
+    $db   = $_POST['database'];
+    $user = $_POST['login'];
+    $pw   = $_POST['password'];
+    try {
+        $dbh = new PDO("mysql:host=localhost;dbname=$db", $user, $pw);
+        $loginSuccessfull = true;
+    } catch (PDOException $e) {
+        $error = $e->getMessage();
+    }
+}
+if (!$loginSuccessfull){
+    echo $twig->render('login.tpl', array("title" => "No PMA", "database" => $db, "login" => $user, "error" => $error));
+}
+else {
+    $_SESSION['db'] = $db;
+    $_SESSION['user'] = $user;
+    $_SESSION['pw'] = $pw;
+
+    $sql = isset($_POST['sql']) ? $_POST['sql'] : "";
+    $rowCount = false;
+
+    if ($sql){
+        try {
+            $dbh = new PDO("mysql:host=localhost;dbname=$db", $user, $pw);
+            $result = $dbh->query($sql);
+            $rows   = $result->fetchAll();
+            if ($rows){
+                foreach (array_keys($rows[0]) as $column){
+                    if (!is_numeric($column)){
+                        $columns[] = $column;
+                    }
+                }
+            }
+            print_r($rows);
+            //print_r($columns);
+            $rowCount = $result->rowCount();
+            $dbh = null;
+        } catch (PDOException $e) {
+            print "Error!: " . $e->getMessage() . "<br/>";
+            die();
+        }
+    }
+
+    echo $twig->render('main.tpl', array("title" => "No PMA", "sql" => $sql, "rowCount" => $rowCount, "columns" => $columns, "rows" => $rows));
+}
+
+
+//print_r($_SESSION);

+ 32 - 0
nopma/templates/layout.tpl

@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>{{ title }}</title>
+<meta name="viewport" content="width=device-width, initial-scale=1.0">
+
+<!-- Bootstrap core CSS -->
+<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" media="screen">
+
+<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
+<!--[if lt IE 9]>
+<script src="http://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.js"></script>
+<script src="http://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js"></script>
+<![endif]-->
+</head>
+<body>
+
+{% if error %}
+<div class='alert alert-danger' role="alert">
+    {{ error }}
+</div>
+{% endif %}
+{% block content %}
+<h1>Hello, Bootstrappers!</h1>
+{% endblock %}
+
+<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
+<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
+<!-- Include all compiled plugins (below), or include individual files as needed -->
+<script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
+</body>
+</html>

+ 9 - 0
nopma/templates/login.tpl

@@ -0,0 +1,9 @@
+{% extends "layout.tpl" %}
+{% block content %}
+<form method="POST">
+    <input type='text' name='login' value = "{{ login }}" placeholder="login">
+    <input type='text' name='database' value = "{{ database }}" placeholder="database">
+    <input type='password' name='password' placeholder="password">
+    <input type='submit' value='Login'>
+</form>
+{% endblock %}

+ 27 - 0
nopma/templates/main.tpl

@@ -0,0 +1,27 @@
+{% extends "layout.tpl" %}
+{% block content %}
+<form method="POST">
+    <textarea name='sql' rows=10 style='width: 90%'>{{ sql }}</textarea><br/>
+    <input type='submit' value='Run..'>
+</form>
+{% if rowCount %}
+<div class='alert alert-info' role="alert">
+    Row Count Affected: {{ rowCount }}
+</div>
+{% endif %}
+
+<table style="width: 100%" border="1">
+    <tr>
+    {% for column in columns %}
+    <th> {{ column }} </th>
+    {% endfor %}
+    </tr>
+    {% for row in rows %}
+        <tr>
+            {% for column in columns %}
+                <td> {{ row[column] }} </td>
+            {% endfor %}
+        </tr>
+    {% endfor %}
+</table>
+{% endblock %}