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

$stfle="";
$prestfle="";
$extras="";
$ismulti=[];
$ismultidatauri=[];
$icnt=1;
$moreh1="";
$imagefig="";
$imagefigandthen="";
$agtext="";
$url="";
$ij=2;
$ijm=2;
$andmore="";
$prevselbit="youllneverfindthis";
$one="1";
$perrow="0";
$rowsep="";
$capselsuff="";
$ext="";
$type="";
$starttag="<img";
$midsrc=" src=";
$endtag="</img>";
$noep="";

$types = array("audio/wav","audio/x-wav","audio/x-pn-realaudio","audio/x-mpegurl","audio/x-aiff","audio/mpeg","audio/mid","video/mp4",
      "audio/basic","audio/ogg","video/x-sgi-movie","video/x-msvideo","video/quicktime","audio/mp3","video/mp4","video/mpeg",
      "video/x-la-asf","video/ogg","video/webm","audio/mp4");  
$exts = array(".wav",".wav",".ram",".m3u",".aiff",".mp3",".rmi",".mp4",
      ".snd",".ogg",".movie",".avi",".mov",".mp3",".m4v",".mpeg",
      ".lsx",".ogv",".webm",".m4a");
      
function urltype($ourl) {
global $types, $exts, $type, $ext, $starttag, $midsrc, $endtag, $noep;
$starttag="<img";
$midsrc=" src=";
$endtag="</img>";
$ext="";
$noep="";
$type="image/jpeg";
if (strpos(explode("?", explode("#", $ourl)[0])[0], ".") !== false) {
  $type="image/" . str_replace("jpg", "jpeg", explode(".", explode("?", explode("#", strtolower($ourl))[0])[0])[-1 + sizeof(explode(".", explode("?", explode("#", strtolower($ourl))[0])[0]))]);
}
for ($it=0; $it<sizeof($types); $it++) {
  if (strpos((explode("?", explode("#", strtolower($ourl))[0])[0] . "~"), ($exts[$it] . "~")) !== false) {
    $type=$types[$it];
    $endtag="</source></" . explode("/", $type)[0] . ">";
    $starttag="<" . explode("/", $type)[0] . "";
    $midsrc=" controls><source type='" . $type ."' src=";
    $noep="if (7 == 9) ";
  }
}
return $ourl;
} 

function rearr($intbl) {
$joff=0;
$intbl=str_replace("</TR>", "</tr>", str_replace("<TR>", "<tr>", $intbl));
if (isset($_POST['operrow'])) { // operrow
  $perrow=htmlspecialchars(urldecode($_POST['operrow']));
  if ($perrow == "0") { 
    return $intbl;
  } else { 
    $pretr=explode("<tr>", $intbl)[0];
    $midrows=explode("</tr>", explode("</table>", substr($intbl, strlen($pretr)))[0]);
    if ($midrows[-1 + sizeof($midrows)] == "" || (sizeof($midrows) % 2) == 1) { $joff=1; }
    for ($ii=0; $ii<((sizeof($midrows) - $joff) / 2); $ii++) {
      $pretr.=$midrows[$ii] . "</tr>";
      $pretr.=$midrows[((sizeof($midrows) - $joff) / 2) + $ii] . "</tr>";
    }
    //file_put_contents("z.z", $pretr);
    return $pretr . "</table>";
  }
} else {
  return $intbl;
}
}


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'])) {

if (isset($_POST['operrow'])) { // operrow
  $perrow=htmlspecialchars(urldecode($_POST['operrow']));
  if ($perrow == "1") { $rowsep="<!--/tr><tr-->"; $perrow="0"; } // else { echo $perrow; exit; }
}

$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];
} else if (strpos($oururl, "#") !== false) {
  $ourcaption=explode("#", $oururl)[1];
  $oururl=explode("#", $oururl)[0];
}  
if (strpos($oururl, "//") === false && (strpos($oururl, "[") !== false || strpos($oururl, "*") !== false || strpos($oururl, "?") !== false)) {
   $specurl=$oururl;
   $oururl="";
   $andmore="";
   foreach (glob($specurl) as $filename) {
     if ($oururl == "") {
       $oururl=$filename;
       $capselsuff=" (" . $filename . ")";
     } else if ($andmore == "") {
       $andmore=$filename;
     } else {
       $andmore.="," . $filename;
     }
   }
}

$oururl=urltype($oururl);

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

$tablesofar="<table id='topt' border=5><TR><td><select onchange=\" if (this.value == 'slideshow') { moveto=0; moveby=1; setTimeout(move, 2000);  } else if (('' + this.value).trim().length != 0) { moveby=0; moveto=eval('' + this.value); location.href='#slideshow' + this.value; } else { moveto=1; moveby=0;  }\"><option value=''>Go to ...</option><option value=slideshow>Slideshow</option><option value=' '>Stop Slideshow</option><option value='-1'>Another Go</option><option value='1'>" . $ourcaption . $capselsuff . "</option></select></td></TR><tr><td><figure>" . $starttag . $owidth . " id='slideshow1' onclick=\"" . $noep . "window.open('" . $oururl . "','_blank');\" title='" . $ourcaption . "' alt='" . $ourcaption . "' style='width:100%'" . $midsrc . "'" . $oururl . "'>" . $endtag . "<figcaption onclick=\"location.href='#toph';\"  title='" . $capselsuff . "'>" . $ourcaption . "</figcaption></figure></td></tr></table>";
$capselsuff="";

while ($andmore != "") {
  if ($perrow != "0") {
    $rowsep="";
    if ($ijm % $perrow == 1) { $rowsep="<!--/tr><tr-->";  }
  } 
  $oururl=urltype(explode(",", $andmore)[0]);
  $capselsuff=str_replace(" ()", "", " (" . $oururl . ")");
  if (strpos($tablesofar, " src=''") !== false) {
  $tablesofar="<table id='topt' border=5><TR><td><select onchange=\"document.title='' + this.value.length; if (this.value == 'slideshow') { moveto=0; moveby=1; setTimeout(move, 2000);  } else if (('' + this.value).trim().length != 0) { moveby=0; moveto=eval('' + this.value); location.href='#slideshow' + this.value; } else { moveto=1; moveby=0;  }\"><option value=''>Go to ...</option><option value=slideshow>Slideshow</option><option value=' '>Stop Slideshow</option><option value='-1'>Another Go</option><option value=" . $ijm . ">" . $ourcaption . $capselsuff . "</option></select></td></TR><tr><td><figure>" . $starttag . $owidth . " id='slideshow" . $ijm . "' onclick=\"" . $noep . "window.open('" . $oururl . "','_blank');\" title='" . $ourcaption . "' alt='" . $ourcaption . "' style='width:100%'" . $midsrc . "'" . $oururl . "'>" . $endtag . "<figcaption onclick=\"location.href='#toph';\"  title='" . $capselsuff . "'>" . $ourcaption . "</figcaption></figure></td></tr></table>";
  $capselsuff="";
  } else {
  $tablesofar=str_replace("</select>", "<option value=" . $ijm . ">" . $ourcaption . $capselsuff . "</option></select>", str_replace("</tr>", $rowsep . "<td><figure>" . $starttag . $owidth . " id='slideshow" . $ijm . "' onclick=\"" . $noep . "window.open('" . $oururl . "','_blank');\" title='" . $ourcaption . "' alt='" . $ourcaption . "' style='width:100%'" . $midsrc ."'" . $oururl . "'>" . $endtag . "<figcaption onclick=\"location.href='#toph';\"  title='" . $capselsuff . "'>" . $ourcaption . "</figcaption></figure></td></tr>", $tablesofar));
  $selbit=explode("<select", explode("</select>", $tablesofar)[0])[-1 + sizeof(explode("<select", explode("</select>", $tablesofar)[0]))];
  $tablesofar=str_replace("<SELECT" . $prevselbit . "</SELECT>","<SELECT" . $selbit . "</SELECT>",str_replace("</td></TR>", "</td>" . $rowsep . "<td><SELECT" . $selbit . "</SELECT></td></TR>", $tablesofar));
  $prevselbit=$selbit;
  $capselsuff="";
  }
  if ($oururl == $andmore) {
    $andmore="";
  } else {
    $andmore=substr($andmore, (strlen($oururl) + 1));
    $capselsuff=str_replace(" ()", "", " (" . explode(",", $andmore)[0] . ")");
  }
  $ijm++;
}

while (isset($_POST['slideshow' . $ij])) {
  if ($perrow != "0") {
    $rowsep="";
    if ($ijm % $perrow == 1) { $rowsep="<!--/tr><tr-->";   }
  } 
  $oururl=htmlspecialchars(urldecode($_POST['slideshow' . $ij]));
  if ($oururl != "") {
  $ourcaption="";
  if (strpos($oururl, ";") !== false) {
    $ourcaption=explode(";", $oururl)[1];
    $oururl=explode(";", $oururl)[0];
  } else if (strpos($oururl, "#") !== false) {
    $ourcaption=explode("#", $oururl)[1];
    $oururl=explode("#", $oururl)[0];
  }
  if (strpos($oururl, "//") === false && (strpos($oururl, "[") !== false || strpos($oururl, "*") !== false || strpos($oururl, "?") !== false)) {
   $specurl=$oururl;
   $oururl="";
   $andmore="";
   foreach (glob($specurl) as $filename) {
     if ($oururl == "") {
       $oururl=$filename;
       $capselsuff=" (" . $filename . ")";
     } else if ($andmore == "") {
       $andmore=$filename;
     } else {
       $andmore.="," . $filename;
     }
   }
  }

  $oururl=urltype($oururl);

  if (strpos($tablesofar, " src=''") !== false) {
  $one="" . $ijm;
  $tablesofar="<table id='topt' border=5><TR><td><select onchange=\" if (this.value == 'slideshow') { moveto=0; moveby=1; setTimeout(move, 2000);  } else if (this.value.length != 0) { moveby=0; moveto=eval('' + this.value); location.href='#slideshow' + this.value; } else { moveto=1; moveby=0;   }\"><option value=''>Go to ...</option><option value=slideshow>Slideshow</option><option value='-1'>Another Go</option><option value='1'>" . $ourcaption . $capselsuff . "</option></select></td></TR><tr><td><figure>" . $starttag . $owidth . " id='slideshow" . $ijm . "' onclick=\"" . $noep . "window.open('" . $oururl . "','_blank');\" title='" . $ourcaption . "' alt='" . $ourcaption . "' style='width:100%'" . $midsrc . "'" . $oururl . "'>" . $endtag . "<figcaption onclick=\"location.href='#toph';\"  title='" . $capselsuff . "'>" . $ourcaption . "</figcaption></figure></td></tr></table>";
  $capselsuff="";
  } else {
  $tablesofar=str_replace("</select>", "<option value=" . $ijm . ">" . $ourcaption . $capselsuff . "</option></select>", str_replace("</tr>", $rowsep . "<td><figure>" . $starttag . $owidth . " id='slideshow" . $ijm . "' onclick=\"" . $noep . "window.open('" . $oururl . "','_blank');\" title='" . $ourcaption . "' alt='" . $ourcaption . "' style='width:100%'" . $midsrc . "'" . $oururl . "'>" . $endtag . "<figcaption onclick=\"location.href='#toph';\"  title='" . $capselsuff . "'>" . $ourcaption . "</figcaption></figure></td></tr>", $tablesofar));
  $selbit=explode("<select", explode("</select>", $tablesofar)[0])[-1 + sizeof(explode("<select", explode("</select>", $tablesofar)[0]))];
  $tablesofar=str_replace("<SELECT" . $prevselbit . "</SELECT>","<SELECT" . $selbit . "</SELECT>",str_replace("</td></TR>", "</td>" . $rowsep . "<td><SELECT" . $selbit . "</SELECT></td></TR>", $tablesofar));
  $prevselbit=$selbit;
  $capselsuff="";
  }
  }
  $ij++;
  $ijm++;
  while ($andmore != "") {
  if ($perrow != "0") {
    $rowsep="";
    if ($ijm % $perrow == 1) { $rowsep="<!--/tr><tr-->";   }
  } 
  $oururl=urltype(explode(",", $andmore)[0]);
  $capselsuff=str_replace(" ()", "", " (" . $oururl . ")");
  if (strpos($tablesofar, " src=''") !== false) {
  $one="" . $ijm;
  $tablesofar="<table id='topt' border=5><TR><td><select onchange=\"if (this.value == 'slideshow') { moveto=0; moveby=1; setTimeout(move, 2000);  } else if (('' + this.value).trim().length != 0) { moveby=0; moveto=eval('' + this.value); location.href='#slideshow' + this.value; } else { moveto=1; moveby=0;  }\"><option value=''>Go to ...</option><option value=slideshow>Slideshow</option><option value=' '>Stop Slideshow</option><option value='-1'>Another Go</option><option value=" . $ijm . ">" . $ourcaption . $capselsuff . "</option></select></td></TR><tr><td><figure>" . $starttag . $owidth . " id='slideshow" . $ijm . "' onclick=\"" . $noep . "window.open('" . $oururl . "','_blank');\" title='" . $ourcaption . "' alt='" . $ourcaption . "' style='width:100%'" . $midsrc . "'" . $oururl . "'>" . $endtag . "<figcaption onclick=\"location.href='#toph';\"  title='" . $capselsuff . "'>" . $ourcaption . "</figcaption></figure></td></tr></table>";
  $capselsuff="";
  } else {
  $tablesofar=str_replace("</select>", "<option value=" . $ijm . ">" . $ourcaption . $capselsuff . "</option></select>", str_replace("</tr>", $rowsep . "<td><figure>" . $starttag . $owidth . " id='slideshow" . $ijm . "' onclick=\"" . $noep . "window.open('" . $oururl . "','_blank');\" title='" . $ourcaption . "' alt='" . $ourcaption . "' style='width:100%'" . $midsrc . "'" . $oururl . "'>" . $endtag . "<figcaption onclick=\"location.href='#toph';\"  title='" . $capselsuff . "'>" . $ourcaption . "</figcaption></figure></td></tr>", $tablesofar));
  $selbit=explode("<select", explode("</select>", $tablesofar)[0])[-1 + sizeof(explode("<select", explode("</select>", $tablesofar)[0]))];
  $tablesofar=str_replace("</td></TR>", "</td>" . $rowsep . "<td><SELECT" . $selbit . "</SELECT></td></TR>", $tablesofar);
  $capselsuff="";
  }
  if ($oururl == $andmore) {
    $andmore="";
  } else {
    $andmore=substr($andmore, (strlen($oururl) + 1));
    $capselsuff=str_replace(" ()", "", " (" . explode(",", $andmore)[0] . ")");
  }
  $ijm++;
  }
}

//file_put_contents("x.x", $tablesofar);

echo "
<!DOCTYPE html>
<html>
<head>
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<style>
 td { vertical-align: top; }
</style>
<title>Use HTML figcaption tag to create a media gallery</title>
<script type='text/javascript'>
var icnt=1, moveby=0, moveto=1, one=" . $one . ";
function move() {
 moveto+=moveby;
 if (document.getElementById('slideshow' + moveto)) {  
   location.href='#slideshow' + moveto;  
   if (moveby != 0) { setTimeout(move, 5000); }
 } else if (moveby != 0) {
   moveto=one;
   location.href='#slideshow' + moveto;
   if (moveby != 0) { setTimeout(move, 5000); }
 }
}
</script>
</head>
<body>

<h2 id='toph' style='width:100%;text-align:center;'>" . $ourtitle . "</h2>

<p style='width:100%;text-align:center;'>" . $oursubtitle . "</p>

" . rearr(str_replace("<!--/tr><tr-->", "</tr><tr>", $tablesofar)) . "

<br>
<a id='slideshow-1' onclick=\"location.href='#toph';\" style='text-decoration:underline;cursor:pointer;'>Back to Gallery</a><br>
<iframe id='anothergo' src='./gallery_via_figcaption.php' style='width:100%;height:1000px;'></iframe>

</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 media gallery</title>
<script type='text/javascript'>
var icnt=1, moveby=0, moveto=1;
function move() {
 moveto+=moveby;
 if (document.getElementById('slideshow' + moveto)) {   
   location.href='#slideshow' + moveto; 
   if (moveby != 0) { setTimeout(move, 5000); }
 } else if (moveby != 0) {
   moveto=1;
   location.href='#slideshow' + moveto;
   if (moveby != 0) { setTimeout(move, 5000); }
 }
}
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:lightblue;' 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 media gallery" . $moreh1 . "</h1>
<div align='center'>
<form target='_top' 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 ; or hash #\"  style='width:70%;' type='text' id='stitle' name='stitle' value='" . $agtext . "'></input><br><br>
Gallery Media Width <input placeholder=\"Optional set media width in pixels\" style='width:70%;' type='text' id='owidth' name='owidth' value=''></input><br><br>
Slides per Row <input title=\"Optional if zero, no limit\" placeholder=\"Optional if zero, no limit\" style='width:70%;' type='number' id='operrow' name='operrow' value='0' min='0' step='1'></input><br><br>
Image/Video/Audio URL <input placeholder=\"Optionally separate the URL (if relative, can use * or [] or ?) from an optional caption via a semicolon ; or hash #\" onblur=\"maybemore(this.value, 'slideshow1');\" style='width:70%;' type='text' id='slideshow' name='slideshow' value='" . $url . "'></input><br><br>
<div id='fdiv1'></div><input style='background-color:yellow;' id=\"mysubmit\" type=\"submit\" title='Use HTML figcaption tag to create a media gallery' value='Use HTML figcaption tag to create a media 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>";


}

?>

