<?php
// map_area_polygon_mysql.php
// April, 2019
// RJM Programming
// Take HTML map tag polygons and add to a MySql database
/*

CREATE TABLE POLYGONS (
 polyid INT NOT NULL AUTO_INCREMENT, 
 mapid VARCHAR(20) NOT NULL, 
 onepolygon POLYGON NOT NULL, 
 PRIMARY KEY (polyid));


*/

  $foned = "block";
  $ftwod = "none";
  $polycontent = "";
  $exd = ' coords="';
  $exde = '"';
  $onl = "";
  $huha = [""];
  $suff = "";
  $two="6";
  $ourdatabase = "";
  $ourpassword = "";
  $insn = 0;
  $ouru = "";
  $dbi = "";
  $via = "";
  $suff = "";
  $moref = ""; // ",Minimum Bounding Rectangle,Geographicals Length";
  $mapidis = "(SELECT CAST(COUNT(*) as varchar(20)) FROM POLYGONS p)";
  $thismapidis = $mapidis;

  $defsp = "DROP TABLE IF EXISTS POLYGONS;  \n";
  $defsp .= "CREATE TABLE POLYGONS ( \n";
  $defsp .= " polyid INT NOT NULL AUTO_INCREMENT, \n";
  $defsp .= " mapid VARCHAR(20) NOT NULL, \n";
  $defsp .= " onepolygon POLYGON NOT NULL, \n";
  $defsp .= " PRIMARY KEY (polyid)); \n";
  $onedefqy = "INSERT INTO POLYGONS (`mapid`,`onepolygon`) values (" . $mapidis . ",ST_POLYGONFROMTEXT('POLYGON (())'));  \n";
  $defqy = "";
 
  if (isset($_GET['inhtml'])) {
   $via=' via ' . htmlspecialchars(urldecode($_GET['inhtml']));
   $content=file_get_contents(str_replace('https:','http:',htmlspecialchars(urldecode($_GET['inhtml']))));
   $mapoh=explode("<map", strtolower($content));
   $thismapidis = $mapidis;
   if (sizeof($mapoh) > 1) {
     $idmaybe=explode(">", $mapoh[1])[0];
     if (strpos($idmaybe, " id=") !== false) {
      if (strpos($idmaybe, " id='") !== false) {
       $thismapidis=explode("'", explode(" id='", " " . $idmaybe)[1])[0];
      } else if (strpos($idmaybe, ' id="') !== false) {
       $thismapidis=explode("'", explode(' id="', " " . $idmaybe)[1])[0];
      } else {
       $thismapidis=explode(">", explode(" ", explode(' id=', " " . $idmaybe)[1])[0])[0];
      }
     }
   }
   $huha=explode($exd, $content);
   if (sizeof($huha) == 1) {
     $exd = " coords='";
     $exde = "'";
     $huha=explode($exd, $content);
     if (sizeof($huha) == 1) {
      $exd = " coords=";
      $exde = " ";
      $huha=explode($exd, $content);
     }
   }
  } else if (isset($_POST['inhtml'])) {
   $via=' via ' . htmlspecialchars(urldecode($_POST['inhtml']));
   $content=file_get_contents(str_replace('https:','http:',htmlspecialchars(urldecode($_POST['inhtml']))));
   $mapoh=explode("<map", strtolower($content));
   $thismapidis = $mapidis;
   if (sizeof($mapoh) > 1) {
     $idmaybe=explode(">", $mapoh[1])[0];
     if (strpos($idmaybe, " id=") !== false) {
      if (strpos($idmaybe, " id='") !== false) {
       $thismapidis="'" . explode("'", explode(" id='", " " . $idmaybe)[1])[0] . "'";
      } else if (strpos($idmaybe, ' id="') !== false) {
       $thismapidis="'" . explode('"', explode(' id="', " " . $idmaybe)[1])[0] . "'";
      } else {
       $thismapidis="'" . explode(">", explode(" ", explode(' id=', " " . $idmaybe)[1])[0])[0] . "'";
      }
     }
   }
   $huha=explode($exd, $content);
   if (sizeof($huha) == 1) {
     $exd = " coords='";
     $exde = "'";
     $huha=explode($exd, $content);
     if (sizeof($huha) == 1) {
      $exd = " coords=";
      $exde = " ";
      $huha=explode($exd, $content);
     }
   }
  } // else {

  if (sizeof($huha) > 1) {
     $ftwod = "block";
     $foned = "none";
     for ($i=1; $i<sizeof($huha); $i++) {
       $thiscontent = explode(">", explode($exde, $huha[$i])[0])[0];
       $thisclist = explode(",", $thiscontent);
       if (sizeof($thisclist) == 4) {  // must be shape=rect
         $thiscontent = "" . $thisclist[0] . " " . $thisclist[1] . "," . $thisclist[0] . " " . $thisclist[3] . "," . $thisclist[2] . " " . $thisclist[3] . "," . $thisclist[0] . "" . $thisclist[3] . "," . $thisclist[0] . " " . $thisclist[1];
       } else {
         $thiscontent = "";
         $comma="";
         for ($j=0; $j<sizeof($thisclist); $j+=2) {
           $thiscontent.=$comma . $thisclist[$j] . " " . $thisclist[1 + $j];
           $comma=", ";
         }
         if (strpos(str_replace("(, ", "(", $thiscontent), $comma . $thisclist[0] . " " . $thisclist[1]) === false) {
           $thiscontent.=$comma . $thisclist[0] . " " . $thisclist[1];
         }
       }
       if ($polycontent == "") {
         $polycontent = $thiscontent;
         $defqy .= str_replace("(, ", "(", str_replace($mapidis, $thismapidis, str_replace("))'))", "" . $thiscontent . "))'))", $onedefqy)));
         $insn+=2;
       } else {
         $polycontent .= ";" . $thiscontent;
         $defqy .= str_replace("(, ", "(", str_replace($mapidis, $thismapidis, str_replace("))'))", "" . $thiscontent . "))'))", $onedefqy)));
         $insn+=2;
       }
     }
     
 }

 $ourdatabase = "";
 if (isset($_GET['host'])) {
   $ourhost=$_GET['host'];
 } else {
   $ourhost="localhost:8889";
 }
 if (isset($_GET['user'])) {
   $ouruser=$_GET['user'];
 } else {
   $ouruser="root";
 }
 if (isset($_GET['password'])) {
   $ourpassword=$_GET['password'];
 } else {
   $ourpassword="root";
 }
 if (isset($_GET['database'])) {
   $ourdatabase=$_GET['database'];
 } else {
   $ourdatabase="testdb";
 }

 $link=mysqli_connect($ourhost, $ouruser, $ourpassword);
 $ourhost='localhost:8889';
 $ouruser='root' . $suff;
 $ourpassword='root';
 if (mysqli_connect_errno()) {
  echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
  exit;
 }
 //$ourdb_table = mysql_list_dbs();
 $ourdb_table='';
 $od='';

// Thanks to https://stackoverflow.com/questions/4677279/php-mysqli-list-databases
if (!($result=mysqli_query($link,'SHOW DATABASES'))) {
        printf('Error: %s', mysqli_error($link));
    }

while( $row = mysqli_fetch_row( $result ) ){
        if (($row[0]!='information_schema') && ($row[0]!='mysql')) {
            if ($suff == "") {
            $ourdb_table.=$od . $row[0]; 
            $od=',';
            } else if ($row[0] == 'rjmprogr_frog') {
            $ourdb_table.=$od . $row[0]; 
            $od=',';
            }
        }
    }
 $ourdb_tables=explode(',', $ourdb_table);
 $selected = '';
 $selin = '';
    
 for ($ii = 0; $ii < sizeof($ourdb_tables); $ii++) { // mysqli_num_rows($ourdb_table); $ii++) {
  $dtis = $ourdb_tables[$ii]; //mysql_tablename($ourdb_table, $ii);
  if (strlen($ourdatabase) > 0) {
   if (strlen($ourdatabase) == strlen($dtis)) {
    $huh = stripos($dtis, $ourdatabase); 
    if ($huh !== false) {
     $selected = ' selected=selected>';
    }
   }
  }
  $selin .= '<option value=' . $dtis . ' ' . $selected . $dtis . '</option>';
  $selected = '>';
 }


   echo "<!doctype html>
<html>
<head>
<title>Take HTML map tag polygons and add to a MySql database - RJM Programming - April, 2019</title>
</head>
<body" . $onl . ">
<h1>Take HTML map tag polygons and add to a MySql database" . $via . "</h1>
<h3>RJM Programming - April, 2019</h3>
<form style='display:" . $foned . ";' id=fone action=./map_area_polygon_mysql.php method=POST>
<input style='width:50%;' name='inhtml' id='inhtml' type='text' value='' placeholder='Enter URL for HTML containing HTML map tag area elements'></input><br><br>
<input type=submit id=myshf value='Ready Proposed MySql Database SQL'></input>
</form>
<form onsubmit='return false;' style='display:" . $ftwod . ";' id=ftwo action=./ourmysqlstoredprocedure.php method=POST>
Select database for query:<br>
<select id=db name=db >" . $selin . "
</select><br><br><br>
<table><tr><td>
Please input SQL for (and before) Table Creation:<br>
<textarea name='spqy' id='spqy' cols=80 rows=" . $two . ">" . $defsp . "</textarea><br><br><br>
Please input SQL for polygon queries<br>
<textarea name='qy' id='qy' cols=180 rows=" . $insn . ">" . $defqy . "</textarea>
</td></tr></table>
<input name='polycontent' id='polycontent' type='hidden' value='" . $polycontent . "'></input>
<input name='moref' id='moref' type='hidden' value='" . $moref . "'></input>
<input name='host' id='host' type='hidden' value='" . $ourhost . "'></input>
<input name='user' id='user' type='hidden' value='" . $ouruser . "'></input>
<input name='password' id='password' type='hidden' value='" . $ourpassword . "'></input>
<input type=submit id=mys value='Add to MySql Database'></input>
</form>
<input type=text style='position:absolute;left:-200px;top:-200px;' value=''></input>
</body>
</html>";
//  }
  
  

?>   