Advertisement
Guest User

Untitled

a guest
Apr 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.13 KB | None | 0 0
  1. <?php
  2. $is_g6 = false;
  3.  
  4. /**
  5.  * Created by PhpStorm.
  6.  * User: herbe
  7.  * Date: 20.04.2017
  8.  * Time: 11:33
  9.  */
  10. class Column
  11. {
  12.     public $name;
  13.     public $type;
  14.  
  15.     /**
  16.      * Column constructor.
  17.      * @param $name
  18.      * @param $type
  19.      */
  20.     public function __construct($name, $type)
  21.     {
  22.         global $is_g6;
  23.         $this->name = $name;
  24.         $this->type = $type;
  25.         $this->is_bool = false;
  26.  
  27.         if (!$is_g6 && $this->name == "g6"){
  28.             $is_g6 = true;
  29.         }
  30.  
  31.         if($this->type == "bool"){
  32.             $this->is_bool = true;
  33.         }
  34.  
  35.     }
  36.  
  37.     public function getVal($row){
  38.         if($this->is_bool){
  39.             if($row[$this->name]){
  40.                 return "true";
  41.             }else{
  42.                 return "false";
  43.             }
  44.         }
  45.         return $row[$this->name];
  46.     }
  47.  
  48.  
  49. }
  50.  
  51. $pdo = new PDO('pgsql:dbname=graphs;user=guest;password=guest;host=localhost');
  52.  
  53. $cols = "*";
  54. if (isset($_GET['cols'])) {
  55.     $cols = $_GET['cols'];
  56. }
  57. $tail = "LIMIT 500";
  58. if (isset($_GET['tail'])) {
  59.     $tail = $_GET['tail'];
  60. }
  61.  
  62. $stmt = "SELECT " . $cols . " FROM graph " . $tail;
  63.  
  64. if (strpos($stmt, ';') !== false) {
  65.     die("No injections, please, you sneaky basterd!");
  66. }
  67.  
  68. $query = $pdo->query($stmt);
  69. $rows = $query->fetchAll();
  70. $columns = array();
  71.  
  72. for ($i = 0; $i < $query->columnCount(); $i++) {
  73.     $col = $query->getColumnMeta($i);
  74.     $columns[] = new Column($col['name'], $col['native_type']);
  75. }
  76.  
  77. //echo json_encode($result);
  78.  
  79. ?>
  80. <!DOCTYPE html>
  81. <html lang="en">
  82. <head>
  83.     <meta charset="UTF-8">
  84.     <title>Graphs collection</title>
  85. </head>
  86. <body>
  87. <h1>Graphs collection</h1>
  88. <h2>PG Query statement</h2>
  89. <form action="" method="get">
  90.     <label>SELECT
  91.         <input type="text" NAME="cols" style="width: 15em" value="<?= $cols ?>">
  92.     </label>
  93.     <label>FROM graph
  94.         <textarea name="tail" style="width: 100%"><?= $tail ?></textarea>
  95.     </label>
  96.     <input type="submit"/>
  97. </form>
  98. <h2>Results</h2>
  99. <table border="1" cellpadding="5" cellspacing="0">
  100.     <tr>
  101.         <?php
  102.         foreach ($columns as $column) {
  103.             ?>
  104.             <th><?= $column->name ?></th>
  105.             <?php
  106.         }
  107.         ?>
  108.         <?php if($is_g6){
  109.             echo "<th>render g6</th>";
  110.         }?>
  111.     </tr>
  112.     <tr>
  113.         <?php
  114.         foreach ($columns as $column) {
  115.             ?>
  116.             <th>
  117.                 <small><?= $column->type ?></small>
  118.             </th>
  119.             <?php
  120.         }
  121.         ?>
  122.         <?php if($is_g6){
  123.             echo "<th>-</th>";
  124.         }?>
  125.     </tr>
  126.     <?php foreach ($rows as $row) {
  127.         ?>
  128.         <tr>
  129.             <?php
  130.             foreach ($columns as $column) {
  131.                 ?>
  132.                 <td><?= $column->getVal($row) ?></td>
  133.                 <?php
  134.             }
  135.             ?>
  136.             <?php if($is_g6){?>
  137.                 <td>
  138.                     <a target="_blank" href="http://treedecompositions.com/#/graph/<?= urlencode($row["g6"]) ?>">SHOW</a>
  139.                 </td>
  140.             <?php
  141.             }?>
  142.         </tr>
  143.         <?php
  144.     } ?>
  145. </table>
  146. </body>
  147. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement