<?php
// quarter_hour_timer.php
// RJM Programming
// December, 2021
set_time_limit(300);

// iptc_make_tag() function by Thies C. Arntzen
function iptc_make_tag($rec, $data, $value) {
    $length = strlen($value);
    $retval = chr(0x1C) . chr($rec) . chr($data);

    if($length < 0x8000)
    {
        $retval .= chr($length >> 8) .  chr($length & 0xFF);
    }
    else
    {
        $retval .= chr(0x80) . 
                   chr(0x04) . 
                   chr(($length >> 24) & 0xFF) . 
                   chr(($length >> 16) & 0xFF) . 
                   chr(($length >> 8) & 0xFF) . 
                   chr($length & 0xFF);
    }

    return $retval . $value;
}

if (isset($_GET['yourta'])) {
 $dru="http://" . $_SERVER['SERVER_NAME'] . "" . str_replace("~","",str_replace(":443~","",str_replace(":80~","",(":" . $_SERVER['SERVER_PORT'] . "~")))) . "/";
 $cet="";
 if (strlen($_GET['yourta']) != 0) { $cet=" contenteditable=true onblur=repostit(this); onfocus=wopen(event,false); "; }
 $prepath=$_SERVER['DOCUMENT_ROOT'];
 if (strpos($dru, "rjmprogramming.com.au") !== false) {
   $prepath="HTTP://localhost:8888";
   $dru="HTTP://localhost:8888/";
 }
 $contis=str_replace('+',' ',urldecode($_GET['yourta']));
 //file_put_contents('xx.xx', $contis);
 $htmlis='';
 $myf='';
 $backi='';
 $cali='';
 $taback='';
 $bcontis="'" . str_replace(chr(34), "' + String.fromCharCode(34) + '", str_replace("\n", "' + String.fromCharCode(10) + '", str_replace("\r\n", "' + String.fromCharCode(10) + '", str_replace("'", "' + String.fromCharCode(39) + '", $contis)))) . "'";
 if (substr(($bcontis . '   '),0,3) == "' + ") {  $bcontis=$bcontis.substr(3); }
 if (substr(('   ' . $bcontis),-3,3) == " + '") {  $bcontis=$bcontis.substr(0,(-3 + strlen($bcontis))); }
 foreach ($_GET as $name=>$val) {
   if ($name != 'yourta') { // && $val == '') {
   //echo $name;
     $orig=$name;
     $myf=$name;
     for ($imh=12; $imh>=1; $imh--) {
     for ($idd=31; $idd>=1; $idd--) {
     //file_put_contents('xz.xz',$orig . ' Is ' . substr('00' . $imh,-2,2) . substr('00' . $idd,-2,2) . ' less than ' . $val);
     if ((substr('00' . $imh,-2,2) . substr('00' . $idd,-2,2)) < $val) {
     for ($ihh=0; $ihh<=23; $ihh++) {
     for ($imm=0; $imm<=45; $imm+=15) {
     //if ($ihh != 0 || $imm != 0) {
       $myf=str_replace("0101_", substr('00' . $imh,-2,2) . substr('00' . $idd,-2,2) . "_",$orig); 
       $myf=str_replace("_0000", "_" . substr('00' . $ihh,-2,2) . substr('00' . $imm,-2,2),$myf); 
     //}
     $taback=str_replace('-','_',str_replace('itd','tatd',$myf));
     $backi=str_replace('-','_',str_replace('screen-','itd_',$myf));
     $cali=str_replace('-','',str_replace('screen-','td',$myf));
     $idcali=explode('-',explode('_',str_replace('screen-','td',str_replace('itd_','td',$myf)))[0])[0];

// Thanks to https://doc.bccnsoft.com/docs/php-docs-7-en/function.iptcembed.html

// Path to jpeg file
$path = $prepath . DIRECTORY_SEPARATOR . str_replace('_','-',str_replace('itd_','screen-',$myf)) . '.jpg';
//file_put_contents('x.x',$path);
if (file_exists($path) || strpos(strtoupper($prepath),"HTTP") !== false) {
$capt = "" . str_replace('+',' ',urldecode($_GET['yourta']));  // Thanks to https://www.php.net/manual/en/function.iptcparse.php
$size = getimagesize( $path, $info );
if ($size) {
if (isset($info["APP13"])) {
//file_put_contents('xx.xx',$path);
    if ($iptc = iptcparse( $info["APP13"] ) ) {
//file_put_contents('xxx.xxx',$path);
        $capt = str_replace( "\000", "", $iptc["2#120"][0] );
//file_put_contents('xxxx.xxxx',$capt);
    }
}
//file_put_contents('xxz.xxz',file_get_contents('xxz.xxz') . "\n" . $path . " " . $capt);
 $bcontis="" . str_replace(chr(34), "' + String.fromCharCode(34) + '", str_replace("\n", "' + String.fromCharCode(10) + '", str_replace("\r\n", "' + String.fromCharCode(10) + '", str_replace("'", "' + String.fromCharCode(39) + '", $capt)))) . "";
 $ccpre="<p" . $cet . " onclick=this.innerHTML+=String.fromCharCode(60)+String.fromCharCode(98)+String.fromCharCode(114)+String.fromCharCode(62); style=background-color:orange;background-repeat:no-repeat;background-size:contain;background-image:linear-gradient(rgba(255,255,255,0.3),rgba(255,255,255,0.3)),URL(" . $dru . str_replace('_','-',str_replace('itd_','screen-',$myf)) . ".jpg" . "); onmouseover=wopen(event,true); onmouseout=wopen(event,false);  title=" . substr(str_replace('td','p',$cali),-4,2) . ":" . substr(str_replace('td','p',$cali),-2,2) . "++ id=" . str_replace('td','p',$cali) . ">";
 $ccpost="</p>";
 $ccontis="<br><p style='background-color:orange; title='" . str_replace('td','p',$cali) . "' id=" . str_replace('td','p',$cali) . ">" . str_replace(chr(34), "' + String.fromCharCode(34) + '", str_replace("\n", "<br>", str_replace("\r\n", "<br>", str_replace("'", "' + String.fromCharCode(39) + '", $capt)))) . "</p>";
//file_put_contents('xxxxx.xxxxx',$bcontis);
//file_put_contents('xxxxxx.xxxxxx',$taback . ' ... ' . $backi);
//file_put_contents('xxxxxxx.xxxxxxx',"parent.document.getElementById('" . $backi . "').title='" . $bcontis . "';  parent.document.getElementById('" . $taback . "').value='" . $bcontis . "'; ");

  // }
   
 if ($bcontis != "''" && $bcontis != "") { 
   if ($htmlis == '') {
    $htmlis="<html><head><script type=text/javascript> var imois=null, iwo=null; 
     function repostit(ih) {
     var ihis=(ih.innerText || ih.contentWindow || ih.contentDocument);
     var pathpart=ih.id;
     if (ihis != '') {
   var xzhr = new XMLHttpRequest();
   var xform=new FormData();
   xform.append('myta',ihis);
   xform.append(pathpart.split('.')[0].replace('ip_','screen-').replace('_','-'),'');
   xzhr.open('post','./quarter_hour_timer.php',true);
   xzhr.send(xform);
     }
     }
     function wopen(event,overvsout) { if (!overvsout) { if (imois == event.target) { imois=null; } return;  } imois=event.target; setTimeout(postwopen, 2000); } function postwopen() { var pois=imois; if (pois.outerHTML.indexOf('URL(') != -1) { window.open(pois.outerHTML.split('URL(')[1].split(')')[0].replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),''),'_blank','top=50,left=50,width=600,height=600');    }  } function onl() {   var huhg='';  if (parent.document.getElementById('" . $idcali . "')) { huhg='" . $ccpre . $bcontis . $ccpost . "';  while (huhg.indexOf(String.fromCharCode(10)) != -1) { huhg=huhg.replace(String.fromCharCode(10),'<br>');  } parent.document.getElementById('" . $idcali . "').innerHTML+=huhg; }     }  </script></head><body onload=\"onl();\"></body></html>"; 
   } else if (strpos($htmlis, $myf) === false) {
// if (strpos($myf, "20211205") !== false) {
//file_put_contents('xxz.xxz',file_get_contents('xxz.xxz') . "\nhuhg='" . $ccpre . $bcontis . $ccpost . "';  while (huhg.indexOf(String.fromCharCode(10)) != -1) { huhg=huhg.replace(String.fromCharCode(10),'<br>');  } parent.document.getElementById('" . $idcali . "').innerHTML+=huhg; \n" . $htmlis);
// }
    $htmlis=str_replace("+=huhg; }", "+=huhg; \n huhg='" . $ccpre . $bcontis . $ccpost . "';  while (huhg.indexOf(String.fromCharCode(10)) != -1) { huhg=huhg.replace(String.fromCharCode(10),'<br>');  } parent.document.getElementById('" . $idcali . "').innerHTML+=huhg; }", $htmlis);
//   } else {
// if (strpos($myf, "20211205") !== false) {
//file_put_contents('xxz.xxz',file_get_contents('xxz.xxz') . "\nHuhg='" . $ccpre . $bcontis . $ccpost . "';  while (huhg.indexOf(String.fromCharCode(10)) != -1) { huhg=huhg.replace(String.fromCharCode(10),'<br>');  } parent.document.getElementById('" . $idcali . "').innerHTML+=huhg; ");
// }
   }
 }

   }
   }
    
 }
 }
 
 if ($htmlis != "") { echo $htmlis;  }
  
 }
 }
 }
 }
 }
 } else if (isset($_GET['myta'])) {
 $dru="http://" . $_SERVER['SERVER_NAME'] . "" . str_replace("~","",str_replace(":443~","",str_replace(":80~","",(":" . $_SERVER['SERVER_PORT'] . "~")))) . "/";
 $contis=str_replace('+',' ',urldecode($_GET['myta']));
 //file_put_contents('xx.xx', $contis);
 $myf='';
 $backi='';
 $cali='';
 $taback='';
 $bcontis="'" . str_replace(chr(34), "' + String.fromCharCode(34) + '", str_replace("\n", "' + String.fromCharCode(10) + '", str_replace("\r\n", "' + String.fromCharCode(10) + '", str_replace("'", "' + String.fromCharCode(39) + '", $contis)))) . "'";
 if (substr(($bcontis . '   '),0,3) == "' + ") {  $bcontis=$bcontis.substr(3); }
 if (substr(('   ' . $bcontis),-3,3) == " + '") {  $bcontis=$bcontis.substr(0,(-3 + strlen($bcontis))); }
 foreach ($_GET as $name=>$val) {
   if ($name != 'myta' && $val == '') {
   //echo $name;
     $myf=$name;
     $taback=str_replace('-','_',str_replace('itd','tatd',$myf));
     $backi=str_replace('-','_',str_replace('screen-','itd_',$myf));
     $cali=str_replace('-','',str_replace('screen-','td',$myf));
     $idcali=explode('-',explode('_',str_replace('screen-','td',str_replace('itd_','td',$myf)))[0])[0];

// Thanks to https://doc.bccnsoft.com/docs/php-docs-7-en/function.iptcembed.html

// Path to jpeg file
$path = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . str_replace('_','-',str_replace('itd_','screen-',$myf)) . '.jpg';
//file_put_contents('x.x',$path);

$capt = "";  // Thanks to https://www.php.net/manual/en/function.iptcparse.php
$size = getimagesize( $path, $info );
if (isset($info["APP13"])) {
//file_put_contents('xx.xx',$path);
    if ($iptc = iptcparse( $info["APP13"] ) ) {
//file_put_contents('xxx.xxx',$path);
        $capt = str_replace( "\000", "", $iptc["2#120"][0] );
//file_put_contents('xxxx.xxxx',$capt);
    }
}
 $bcontis="" . str_replace(chr(34), "' + String.fromCharCode(34) + '", str_replace("\n", "' + String.fromCharCode(10) + '", str_replace("\r\n", "' + String.fromCharCode(10) + '", str_replace("'", "' + String.fromCharCode(39) + '", $capt)))) . "";
 $ccpre="<p onclick=this.innerHTML+=String.fromCharCode(60)+String.fromCharCode(98)+String.fromCharCode(114)+String.fromCharCode(62); style=background-color:orange;background-repeat:no-repeat;background-size:contain;background-image:linear-gradient(rgba(255,255,255,0.3),rgba(255,255,255,0.3)),URL(" . $dru . str_replace('_','-',str_replace('itd_','screen-',$myf)) . ".jpg" . "); onmouseover=wopen(event,true); onmouseout=wopen(event,false);  title=" . substr(str_replace('td','p',$cali),-4,2) . ":" . substr(str_replace('td','p',$cali),-2,2) . "++ id=" . str_replace('td','p',$cali) . ">";
 $ccpost="</p>";
 $ccontis="<br><p style='background-color:orange; title='" . str_replace('td','p',$cali) . "' id=" . str_replace('td','p',$cali) . ">" . str_replace(chr(34), "' + String.fromCharCode(34) + '", str_replace("\n", "<br>", str_replace("\r\n", "<br>", str_replace("'", "' + String.fromCharCode(39) + '", $capt)))) . "</p>";
//file_put_contents('xxxxx.xxxxx',$bcontis);
//file_put_contents('xxxxxx.xxxxxx',$taback . ' ... ' . $backi);
//file_put_contents('xxxxxxx.xxxxxxx',"parent.document.getElementById('" . $backi . "').title='" . $bcontis . "';  parent.document.getElementById('" . $taback . "').value='" . $bcontis . "'; ");

   }
 if ($bcontis != "''") { echo "<html><head><script type=text/javascript> var iwo=null, imois=null; function wopen(event,overvsout) { imois=event.target; setTimeout(postwopen, 2000); } function postwopen() { var pois=imois; if (pois.outerHTML.indexOf('URL(') != -1) { if (iwo) { iwo.close(); iwo=null; } iwo=window.open(pois.outerHTML.split('URL(')[1].split(')')[0].replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),''),'_blank','top=50,left=50,width=600,height=600');    }  }</script></head><body onload=\" parent.document.getElementById('" . $backi . "').title='" . $bcontis . "';  parent.document.getElementById('" . $taback . "').value='" . $bcontis . "';   if (parent.document.getElementById('" . $idcali . "')) { var huhg='" . $ccpre . "' + parent.document.getElementById('" . $taback . "').value + '" . $ccpost . "';  while (huhg.indexOf(String.fromCharCode(10)) != -1) { huhg=huhg.replace(String.fromCharCode(10),'<br>');  } parent.document.getElementById('" . $idcali . "').innerHTML+=huhg; }  \"></body></html>"; } 
 }
 } else if (isset($_POST['myta'])) {
 $dru="http://" . $_SERVER['SERVER_NAME'] . "" . str_replace("~","",str_replace(":443~","",str_replace(":80~","",(":" . $_SERVER['SERVER_PORT'] . "~")))) . "/";
 $contis=str_replace('+',' ',urldecode($_POST['myta']));
 //file_put_contents('xx.xx', $contis);
 $myf='';
 $backi='';
 $cali='';
 $bcontis="'" . str_replace(chr(34), "' + String.fromCharCode(34) + '", str_replace("\n", "' + String.fromCharCode(10) + '", str_replace("\r\n", "' + String.fromCharCode(10) + '", str_replace("'", "' + String.fromCharCode(39) + '", $contis)))) . "'";
 
 $ccpre="<p onclick=this.innerHTML+=String.fromCharCode(60)+String.fromCharCode(98)+String.fromCharCode(114)+String.fromCharCode(62); style=background-color:orange;background-repeat:no-repeat;background-size:contain;background-image:linear-gradient(rgba(255,255,255,0.3),rgba(255,255,255,0.3)),URL(" . $dru . str_replace('_','-',str_replace('itd_','screen-',$myf)) . ".jpg" . "); onmouseover=wopen(event,true); onmouseout=wopen(event,false);  title=" . substr(str_replace('td','p',$cali),-4,2) . ":" . substr(str_replace('td','p',$cali),-2,2) . "++ id=" . str_replace('td','p',$cali) . ">";
 $ccpost="</p>";
 $ccontis="<br><p style='background-color:orange; title='" . str_replace('td','p',$cali) . "' id=" . str_replace('td','p',$cali) . ">" . str_replace(chr(34), "' + String.fromCharCode(34) + '", str_replace("\n", "<br>", str_replace("\r\n", "<br>", str_replace("'", "' + String.fromCharCode(39) + '", $capt)))) . "</p>";

 
 if (substr(($bcontis . '   '),0,3) == "' + ") {  $bcontis=$bcontis.substr(3); }
 if (substr(('   ' . $bcontis),-3,3) == " + '") {  $bcontis=$bcontis.substr(0,(-3 + strlen($bcontis))); }
 foreach ($_POST as $name=>$val) {
   if ($name != 'myta') {
     $myf=$name;
     $backi=str_replace('-','_',str_replace('screen-','itd_',$myf));
     $taback=str_replace('tatd','tatd_',substr(str_replace('-','',str_replace('_','',str_replace('itd','tatd',str_replace('screen','tatd',$myf)))),0,12)) . '_' . substr(str_replace('-','',str_replace('_','',str_replace('itd','tatd',str_replace('screen','tatd',$myf)))),-4,4);
     $pback=str_replace('tatd','ip',$taback);
     $cali=str_replace('-','',str_replace('screen-','td',$myf));
     $idcali=explode('-',explode('_',str_replace('screen-','td',str_replace('itd_','td',$myf)))[0])[0];

// Thanks to https://doc.bccnsoft.com/docs/php-docs-7-en/function.iptcembed.html

// Path to jpeg file
$path = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $myf . '.jpg';

//file_put_contents('x.x', $path);

// Set the IPTC tags
$iptc = array(
    '2#120' => $contis,
    '2#116' => 'Copyright 2021, RJM Programming'
);

// Convert the IPTC tags into binary code
$data = '';

foreach($iptc as $tag => $string) {
    $tag = substr($tag, 2);
    $data .= iptc_make_tag(2, $tag, $string);
}

// Embed the IPTC data
$content = iptcembed($data, $path);

// Write the new image data out to the file.
$fp = fopen($path, "wb");
fwrite($fp, $content);
fclose($fp);

   }
 echo "<html><head><script type=text/javascript> var iwo=null, imois=null; function wopen(event,overvsout) { imois=event.target; setTimeout(postwopen, 2000); } function postwopen() { var pois=imois; if (pois.outerHTML.indexOf('URL(') != -1) {  if (iwo) { iwo.close(); iwo=null; } iwo=window.open(pois.outerHTML.split('URL(')[1].split(')')[0].replace(String.fromCharCode(34),'').replace(String.fromCharCode(34),''),'_blank','top=50,left=50,width=600,height=600');    }  }</script></head><body onload=\" parent.document.getElementById('" . $backi . "').title=" . $bcontis . ";   if (parent.document.getElementById('" . $idcali . "')) { var huhg='" . $ccpre . "' + parent.document.getElementById('" . $taback . "').value + '" . $ccpost . "';  if (parent.document.getElementById('" . $pback . "')) { huhg=parent.document.getElementById('" . $taback . "').value; }  while (huhg.indexOf(String.fromCharCode(10)) != -1) { huhg=huhg.replace(String.fromCharCode(10),'<br>');  }    if (parent.document.getElementById('" . $pback . "')) {  parent.document.getElementById('" . $pback . "').innerHTML=huhg; } else { parent.document.getElementById('" . $idcali . "').innerHTML+=huhg; } }  \"></body></html>";
 }
 } else {
 exec("/usr/sbin/screencapture -Cd -tjpg /Applications/MAMP/htdocs/screen-`date +\"%Y%m%d-%H%M\"`.jpg");
 }
 exit;
?>
