<?php
// figcaption.php
// Use HTML figcaption tag to create a gallery
// RJM Programming
// April,2019

$stfle="";
$prestfle="";
$extras="";
$ismulti=[];
$ismultidatauri=[];
$icnt=1;
$moreh1="";
$imagefig="";
$imagefigandthen="";
$agtext="";
$url="";


function jserver_remote_addr() {
    global $stfle;
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
    return str_replace(":", "_", $rma);
}

function server_remote_addr() {
    global $stfle;
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
    if (isset($_GET['outfile'])) {
      if (urldecode($_GET['outfile']) != "") {
        $stfle='<input type=hidden name=outfile id=outfile value="' . urldecode($_GET['outfile']) . '"></input> ';
        return urldecode($_GET['outfile']);
      }
    } else if (isset($_POST['outfile'])) {
      if (urldecode($_POST['outfile']) != "") {
        $stfle='<input type=hidden name=outfile id=outfile value="' . urldecode($_POST['outfile']) . '"></input> ';
        return urldecode($_POST['outfile']);
      }
    }
    return str_replace(":", "_", $rma);
}


if (isset($_POST['stitle']) && isset($_POST['slideshow'])) {

$owidth="";
if (isset($_POST['owidth'])){
  $owidth=htmlspecialchars(urldecode($_POST['owidth']));
  if (strpos($owidth, "%") !== false || strpos(strtolower($owidth), "vw") !== false) { 
    $owidth=" style='width:" . $owidth . "'";  
  } else if ($owidth != "") { 
    $owidth=" style='width:" . str_replace("px","",strtolower($owidth)) . "px'";  
  }
}

$oururl=htmlspecialchars(urldecode($_POST['slideshow']));
$ourcaption="";
if (strpos($oururl, ";") !== false) {
  $ourcaption=explode(";", $oururl)[1];
  $oururl=explode(";", $oururl)[0];
}
$ourtitle=htmlspecialchars(urldecode($_POST['stitle']));
$oursubtitle="";
if (strpos($ourtitle, ";") !== false) {
  $oursubtitle=explode(";", $ourtitle)[1];
  $ourtitle=explode(";", $ourtitle)[0];
}

$tablesofar="<table border=5><tr><td><figure><img" . $owidth . " onclick=\"window.open('" . $oururl . "','_blank');\" src='" . $oururl . "' title='" . $ourcaption . "' alt='" . $ourcaption . "' style='width:100%'><figcaption>" . $ourcaption . "</figcaption></figure></td></tr></table>";

$ij=2;
while (isset($_POST['slideshow' . $ij])) {
  $oururl=htmlspecialchars(urldecode($_POST['slideshow' . $ij]));
  if ($oururl != "") {
  $ourcaption="";
  if (strpos($oururl, ";") !== false) {
    $ourcaption=explode(";", $oururl)[1];
    $oururl=explode(";", $oururl)[0];
  }
  if (strpos($tablesofar, " src=''") !== false) {
  $tablesofar="<table border=5><tr><td><figure><img" . $owidth . " onclick=\"window.open('" . $oururl . "','_blank');\" src='" . $oururl . "' title='" . $ourcaption . "' alt='" . $ourcaption . "' style='width:100%'><figcaption>" . $ourcaption . "</figcaption></figure></td></tr></table>";
  } else {
  $tablesofar=str_replace("</tr>", "<td><figure><img" . $owidth . " onclick=\"window.open('" . $oururl . "','_blank');\" src='" . $oururl . "' title='" . $ourcaption . "' alt='" . $ourcaption . "' style='width:100%'><figcaption>" . $ourcaption . "</figcaption></figure></td></tr>", $tablesofar);
  }
  }
  $ij++;
}

echo "
<!DOCTYPE html>
<html>
<body>

<h2>" . $ourtitle . "</h2>

<p>" . $oursubtitle . "</p>

" . $tablesofar . "

</body>
</html>
";

} else {
   echo "<!doctype html>
<html>
<head>
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<title>Use HTML figcaption tag to create a gallery</title>
<script type='text/javascript'>
var icnt=1;
function isimage(tvl) {
  if (tvl.toLowerCase().indexOf('.jp') != -1) return true;
  if (tvl.toLowerCase().indexOf('.gif') != -1) return true;
  if (tvl.toLowerCase().indexOf('.png') != -1) return true;
  if (tvl.toLowerCase().indexOf('.bmp') != -1) return true;
  if (tvl.toLowerCase().indexOf('.tif') != -1) return true;
  return false;
}
function and_then() {
  document.getElementById('mysubmit').click();
}
function maybemore(tval, tid) {
  var newi=null, fo=null;
  if (isimage(tval)) {
   fo=document.getElementById('fdiv' + icnt);
   icnt++;
   newi=document.getElementById('slideshow' + icnt);
   if (fo != null && newi == null) {
    fo.innerHTML+=\"Image URL \" + icnt + \" <input onblur='maybemore(this.value, this.id);' style='width:70%;' type='text' id='slideshow\" + icnt + \"' name='slideshow\" + icnt + \"' value=''></input><br><br><div id='fdiv\" + icnt + \"'></div>\";
   } 
  }
}
function andthen() {
" . $imagefigandthen . "
}
</script>
<style>
a { padding: 5px 5px 5px 5px; margin: 5px 5px 5px 5px; border: 1px solid red; background-color: #f0f0f0; } 
</style>
</head>
<body style='background-color:lightgreen;' onload=' if (top.document.getElementById(\"theimgnamegoeshere\")) { top.document.getElementById(\"theimgnamegoeshere\").value=\"//www.rjmprogramming.com.au/PHP/animegif/" . jserver_remote_addr() . ".gif\";  } setTimeout(andthen, 8000);  '>
<h1 id='myh1' align='center'>Use HTML figcaption tag to create a gallery" . $moreh1 . "</h1>
<div align='center'>
<form id='myform' action='./gallery_via_figcaption.php' method='POST'>" . $prestfle . $stfle . "
Gallery Title <input placeholder=\"Optionally separate the main title from an optional subtitle via a semicolon ;\"  style='width:70%;' type='text' id='stitle' name='stitle' value='" . $agtext . "'></input><br><br>
Gallery Image Width <input placeholder=\"Optional set image width in pixels\" style='width:70%;' type='text' id='owidth' name='owidth' value=''></input><br><br>
Image URL <input placeholder=\"Optionally separate the URL from an optional caption via a semicolon ;\" onblur=\"maybemore(this.value, 'slideshow1');\" style='width:70%;' type='text' id='slideshow' name='slideshow' value='" . $url . "'></input><br><br>
<div id='fdiv1'></div><input id=\"mysubmit\" type=\"submit\" title='Use HTML figcaption tag to create a gallery' value='Use HTML figcaption tag to create a gallery'></input>" . $extras . "
</form><div id='idiv' align='center' style='background-color:pink;'>" . $imagefig . "</div>
</div>
<input type=text style='position:absolute;top:-200px;left:-200px;' value=''></input>
</body>
</html>";


}

?>

