<?php
// agtoslides.php
// RJM Programming
// May, 2024
// Animated GIF to a slide via ... ksh agtoslides.ksh [animatedGIFfilename] [slideNumber]
ini_set('max_execution_time', 60000); 

$outp='';
$infl='';
$infill='';
$preoutp='';
$postoutp='';

if (file_exists('/tmp/agtoslides')) {
if (file_exists(rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'agtoslides.ksh')) {
   if (isset($argc) && !isset($_GET['agname']) && !isset($_POST['agname'])) {
     $infl=str_replace('+',' ',$argv[1]);
     if (strpos($infl, 'data:image/') !== false) {
      $infl=str_replace(' ','+',urldecode($argv[1]));
      file_put_contents('/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_xx.' . str_replace('jpeg','jpg',explode(';',explode('/',  $infl )[1])[0]), base64_decode(explode(';base64,',  $infl )[1] ));       
      $infl='/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_xx.' . str_replace('jpeg','jpg',explode(';',explode('/',  $infl )[1])[0]);
     } else if (!file_exists($infl) || strpos($infl, DIRECTORY_SEPARATOR) === false) {
      $infl=rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $infl;
     }
     if ($argc > 2) {
      $outp=shell_exec('ksh ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'agtoslides.ksh ' . $infl . ' ' . str_replace('+',' ',urldecode($argv[2]))) . ' ' . $infill;
     } else {
      $outp=shell_exec('ksh ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'agtoslides.ksh ' . $infl . ' 1' . ' ' . $infill);
     }
   } else {
     if (isset($_GET['random']) && !isset($_GET['agname'])) {
      echo "<html>
      <head>
      <scrip" . "t type=text/javascript>
      var jjjxhr=null, jjjform=null, astr='', stats=0;
      
      function joneslidedu() {
  if (jjjxhr.readyState == 4) {
    if (jjjxhr.status == 200) {
       parent.window.opener.timingshashpush((new Date()).getTime()); //     document.body.innerHTML=jjjxhr.responseText.split(jjjxhr.responseText.substring(1).split('>')[0])[1].split('</body>')[0];
       stats=jjjxhr.status;
    }
  }
      }

      function ajaxit() {
      if (1 == 1) {
      parent.setstats(1);
      jjjform = new FormData();
      jjjxhr = new XMLHttpRequest();
      jjjform.append('agname', decodeURIComponent(('' + location.hash).split('agname=')[1].split('&')[0].split('#')[0]));
      jjjform.append('random', decodeURIComponent('" . $_GET['random'] . "'));
      jjjform.append('slidenumber', decodeURIComponent('" . $_GET['slidenumber'] . "'));
      jjjxhr.open('post', './agtoslides.php', true);
      jjjxhr.onreadystatechange = joneslidedu;
      jjjxhr.send(jjjform);
      if (parent.window.opener.setstats) {
      parent.window.opener.setstats(1);
      }
      parent.window.opener.document.getElementById('spareagname').value=decodeURIComponent(('' + location.hash).split('agname=')[1].split('&')[0].split('#')[0]);
      //alert(parent.window.opener.document.getElementById('sparemyform').outerHTML.length);
      if (eval('' + parent.window.opener.document.getElementById('sparemyform').outerHTML.length) < 1000) {
         parent.window.opener.document.getElementById('sparemyform').method='GET';
      } else {
         parent.window.opener.document.getElementById('sparemyform').method='POST';
      }
      parent.window.opener.document.getElementById('sparemyform').action=('' + parent.document.getElementById('sparemyform').action).split('#')[0];
      parent.window.opener.document.getElementById('sparemysub').click();
      if (1 == 11) {
      //parent.getto().document.title='yes';
      while (('' + parent.getto().getstats()) != '200') {
         if (astr == '' || eval('' + astr.length) > 800) {
           astr=' ';
         } else {
           astr+=' ';
         }
      }
      }
      } else {
      jjjform = new FormData();
      jjjxhr = new XMLHttpRequest();
      jjjform.append('agname', decodeURIComponent(('' + location.hash).split('agname=')[1].split('&')[0].split('#')[0]));
      jjjform.append('random', decodeURIComponent('" . $_GET['random'] . "'));
      jjjform.append('slidenumber', decodeURIComponent('" . $_GET['slidenumber'] . "'));
      jjjxhr.open('post', './agtoslides.php', true);
      jjjxhr.onreadystatechange = joneslidedu;
      jjjxhr.send(jjjform);
      while (stats != 200) {
         if (astr == '' || eval('' + astr.length) > 800) {
           astr=' ';
         } else {
           astr+=' ';
         }
      }
      }
      return true;
      }
      </scr" . "ipt>
      </head>
      <body onload='return ajaxit();' id=xbody></body></html>
      ";
      exit;
     }
   if (isset($_GET['agname'])) {
     if (isset($_GET['random'])) {
       $infill=trim(str_replace('+',' ',urldecode($_GET['random']))) . ' tidyhere';
     }
     $infl=str_replace(' ','+',urldecode($_GET['agname']));
     if (strpos($infl, 'data:image/') !== false) {
      $infl=str_replace(' ','+',urldecode($_GET['agname']));
      file_put_contents('/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_xx.' . str_replace('jpeg','jpg',explode(';',explode('/',  $infl )[1])[0]), base64_decode(explode(';base64,',  $infl )[1] ));       
      $infl='/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_xx.' . str_replace('jpeg','jpg',explode(';',explode('/',  $infl )[1])[0]);
     } else if (strpos($infl, $_SERVER['SERVER_NAME']) === false && strpos($infl, 'http') !== false) {
      $infl=str_replace(' ','+',urldecode($_GET['agname']));
      file_put_contents('/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_xx.' . str_replace('jpeg','jpg',explode(';',explode('/',  $infl )[1])[0]), file_get_contents(str_replace('https:','http:',$infl)) );       
      $infl='/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_xx.' . str_replace('jpeg','jpg',explode(';',explode('/',  $infl )[1])[0]);
     }
     if ((!file_exists($infl) || strpos($infl, DIRECTORY_SEPARATOR) === false) && strpos($infl, 'data:image/') === false) {
      $infl=rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $infl;
     }
     if (isset($_GET['slidenumber'])) {
  //echo 'ksh ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'agtoslides.ksh ' . $infl . ' ' . str_replace('+',' ',urldecode($_GET['slidenumber']));
  //exit;
  //file_put_contents('xget.xxx', 'ksh ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'agtoslides.ksh ' . $infl . ' ' . str_replace('+',' ',urldecode($_GET['slidenumber'])) . ' ' . $infill . ' 2> ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'agtoslides.err');
      $outp=shell_exec('ksh ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'agtoslides.ksh ' . $infl . ' ' . str_replace('+',' ',urldecode($_GET['slidenumber'])) . ' ' . $infill . ' 2> ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'agtoslides.err');
      //echo ' --- ' . $outp;
      //exit;
     } else {
  //file_put_contents('xget.zzz', 'ksh ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'agtoslides.ksh ' . $infl . ' 1' . ' ' . $infill);
      $outp=shell_exec('ksh ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'agtoslides.ksh ' . $infl . ' 1' . ' ' . $infill);
     }
   } else if (isset($_POST['agname'])) { 
     if (isset($_POST['random'])) {
       $infill=trim(str_replace('+',' ',urldecode($_POST['random']))) . ' tidyhere';
     }
     $infl=str_replace(' ','+',urldecode($_POST['agname']));
     if (strpos($infl, 'data:image/') !== false) {
      $infl=str_replace(' ','+',urldecode($_POST['agname']));
      file_put_contents('/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_xx.' . str_replace('jpeg','jpg',explode(';',explode('/',  $infl )[1])[0]), base64_decode(explode(';base64,',  $infl )[1] ));       
      $infl='/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_xx.' . str_replace('jpeg','jpg',explode(';',explode('/',  $infl )[1])[0]);
//if (strlen($infl) != 0) {
//file_put_contents('x.xx', $infl . ' ' . shell_exec('ls -l /tmp/agtoslides/*.*'));
//}
     } else if (strpos($infl, $_SERVER['SERVER_NAME']) === false && strpos($infl, 'http') !== false) {
      $infl=str_replace(' ','+',urldecode($_POST['agname']));
      file_put_contents('/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_xx.' . str_replace('jpeg','jpg',explode(';',explode('/',  $infl )[1])[0]), file_get_contents(str_replace('https:','http:',$infl)) );       
      $infl='/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_xx.' . str_replace('jpeg','jpg',explode(';',explode('/',  $infl )[1])[0]);
//if (strlen($infl) != 0) {
//file_put_contents('x.xxxx', $infl . ' ' . shell_exec('ls -l /tmp/agtoslides/*.*'));
//}
//     } else {
//file_put_contents('x.xxxxx', $infl . ' ' . shell_exec('ls -l /tmp/agtoslides/*.*'));
     }
     if ((!file_exists($infl) || strpos($infl, DIRECTORY_SEPARATOR) === false) && strpos($infl, 'data:image/') === false) {
      $infl=rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $infl;
//if (strlen($infl) != 0) {
//file_put_contents('x.xxx', $infl . ' ' . shell_exec('ls -l /tmp/agtoslides/*.*'));
//}
     }
     if (isset($_POST['slidenumber'])) {
  //file_put_contents('xpost.xxx', 'ksh ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'agtoslides.ksh ' . $infl . ' ' . str_replace('+',' ',urldecode($_POST['slidenumber'])) . ' ' . $infill . ' 2> ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'agtoslides.err');
      $outp=shell_exec('ksh ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'agtoslides.ksh ' . $infl . ' ' . str_replace('+',' ',urldecode($_POST['slidenumber'])) . ' ' . $infill);
     } else {
  //file_put_contents('xpost.zzz', 'ksh ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'agtoslides.ksh ' . $infl . ' 1' . ' ' . $infill);
      $outp=shell_exec('ksh ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'agtoslides.ksh ' . $infl . ' 1' . ' ' . $infill);
     }
   } else {
$fp = fopen("php://input", "r");
$post = "" . file_get_contents("php://input");
fclose($fp);
if (strpos($post, "agname=") !== false && strpos($post, "random=") !== false && strpos($post, "slidenumber=") !== false) {
       $infill=explode('&', explode('random=', $post)[1])[0] . ' tidyhere';
       $sn=explode('&', explode('slidenumber=', $post)[1])[0];
       $infl=str_replace(' ','+',urldecode(explode('&', explode('agname=', $post)[1])[0]));
       if (strpos($infl, 'data:image/') !== false) {
       file_put_contents('/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_xx.' . str_replace('jpeg','jpg',explode(';',explode('/',  $infl )[1])[0]), base64_decode(explode(';base64,',  $infl )[1] ));       
       $infl='/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_xx.' . str_replace('jpeg','jpg',explode(';',explode('/',  $infl )[1])[0]);
       }
//file_put_contents('x.xxx', $infl . ' ' . shell_exec('ls -l /tmp/agtoslides/*.*'));
       $outp=shell_exec('ksh ' . rtrim(dirname(__FILE__), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'agtoslides.ksh ' . $infl . ' ' . $sn . ' ' . $infill);
}
   }
   }
}
}
$inn=0;
$ij=0;
$otherstuff="";
if (isset($_GET['delay'])) {
   $otherstuff.='delay=' . str_replace('+',' ',urldecode($_GET['delay'])) . '&';
}
if (isset($_POST['delay'])) {
   $otherstuff.='delay=' . str_replace('+',' ',urldecode($_POST['delay'])) . '&';
}
if (isset($_GET['title'])) {
   $otherstuff.='title=' . str_replace('+',' ',urldecode($_GET['title'])) . '&';
}
if (isset($_POST['title'])) {
   $otherstuff.='title=' . str_replace('+',' ',urldecode($_POST['title'])) . '&';
}
if (file_exists('/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_' . substr(('00000' . $inn),-5) . '.png')) {
while (file_exists('/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_' . substr(('00000' . $inn),-5) . '.png')) {
  $ij++;
  $inn++;
}
}
$inn=0;
while (file_exists('/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_' . substr(('00000' . $inn),-5) . '.png')) {
  if ($postoutp == '') {
    $postoutp='</body></html>';
    $preoutp="<html><body onload=\"  parent.agslideshow('slideshow','data:image/" . explode('#',str_replace('jpg','jpeg',strtolower(explode('.','/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_' . substr(('00000' . $inn),-5) . '.png')[-1 + sizeof(explode('.','/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_' . substr(('00000' . $inn),-5) . '.png'))])))[0] . ';base64,' . base64_encode(file_get_contents(explode('#','/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_' . substr(('00000' . $inn),-5) . '.png')[0])) . "');  ";
  }
  if ($postoutp != '') {
   if ($inn > 0) {
    $preoutp.=" parent.agslideshow('slideshow" . ('' . (1 + $inn)) . "','data:image/" . explode('#',str_replace('jpg','jpeg',strtolower(explode('.','/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_' . substr(('00000' . $inn),-5) . '.png')[-1 + sizeof(explode('.','/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_' . substr(('00000' . $inn),-5) . '.png'))])))[0] . ';base64,' . base64_encode(file_get_contents(explode('#','/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_' . substr(('00000' . $inn),-5) . '.png')[0])) . "'); ";
   }
  }
  unlink('/tmp/agtoslides/xx' . explode(' ',$infill)[0] . '_' . substr(('00000' . $inn),-5) . '.png');
  $inn++;
}
if ($postoutp != '') { $preoutp.=" parent.preagslideshow('/PHP/animegif/tutorial_to_animated_gif.php?" . $otherstuff . "numfillin=" . ('' . $ij) . "');  \">"; }
//file_put_contents('x.x', $preoutp . $outp . $postoutp);
echo $preoutp . $outp . $postoutp;
exit;
?>
