<?php
  // calendar_contenteditable.php
  // RJM Programming - September, 2026
  // Help out calendar_contenteditable.html
  error_reporting(E_ALL);
  
  function extended_jdtofrench($juliandate) { // thanks to https://www.php.net/manual/en/function.jdtofrench.php
    if ($juliandate > 2380945) {
        // jdtofrench () only accepts dates until september 1806
        $gregorian_date = jdtogregorian($juliandate);
        $temp = explode ('/', $gregorian_date);
        $year = $temp[2];
        $juliandate = gregoriantojd($temp[0], $temp[1], 1805);
        $republican_date = jdtofrench($juliandate);
        $republican_date = explode('/', $republican_date);
        $diff = $year - 1805;
        $republican_date[2] = $republican_date[2] + $diff;
    } else {
        $republican_date = jdtofrench($juliandate);
    }
    return $republican_date[1] . '/' . (1 + $republican_date[0]) . '/' . $republican_date[2];
  }
    
  function mdytodmy($inv) {
    $things=explode('/', $inv);
    if (sizeof($things) == 3) {
      return '' . $things[1] . '/' . (1 + $things[0]) . '/' . $things[2];
    }
    return $inv;
  }
  
  function mdytodmyj($inv) {
    $things=explode('/', $inv);
    if (sizeof($things) == 3) {
      return '' . $things[1] . '/' . (0 + $things[0]) . '/' . $things[2];
    }
    return $inv;
  }
  
  if (isset($_GET['docals']) && isset($_GET['monththe']) && isset($_GET['yearthe'])) {
    //data-gregorian='' data-julian='' data-jewish='' data-french=''
    $onl='';
    $jewishtsh='';
    $frenchts='';
    $gregdate='1';
    $gregmonth='' . $_GET['monththe'];
    $gregyear='' . $_GET['yearthe'];
    if (isset($_GET['datethe'])) {
      $gregdate='' . $_GET['datethe'];
    }
    $jdnum=gregoriantojd($gregmonth, $gregdate, $gregyear);
    $gregoriants='' . $gregmonth . '/' . $gregdate . '/' . $gregyear;
    $frenchts=extended_jdtofrench(($jdnum + 2));
    $jewishts=jdtojewish(($jdnum + 1));
    $juliants=jdtojulian($jdnum);
    $jewishtsz=jdtojewish(($jdnum + 1), true);
    $jewishtsh=iconv('WINDOWS-1255', 'UTF-8', $jewishtsz);
    $onl.=" var ispans=0, spans=parent.document.getElementsByTagName('span'); ";
    $onl.=" parent.document.getElementById('calcap').setAttribute('data-gregorian', '" . mdytodmy($gregoriants) . "'); spans[1].setAttribute('data-gregorian', '" . mdytodmy($gregoriants) . "');  ";
    $onl.=" parent.document.getElementById('calcap').setAttribute('data-julian', '" . mdytodmy($juliants) . "'); spans[1].setAttribute('data-julian', '" . mdytodmy($juliants) . "');  ";
    $onl.=" parent.document.getElementById('calcap').setAttribute('data-french', '" . $frenchts . "'); spans[1].setAttribute('data-french', '" . $frenchts . "'); ";
    $onl.=" parent.document.getElementById('calcap').setAttribute('data-jewish', '" . mdytodmyj($jewishts) . "'); spans[1].setAttribute('data-jewish', '" . mdytodmyj($jewishts) . "'); ";
    $onl.=" parent.document.getElementById('calcap').setAttribute('data-jewishy', '" . $jewishtsh . "'); spans[1].setAttribute('data-jewishy', '" . $jewishtsh . "');   ";
    
    
    if (5 == 5) {
    for ($ioff=1; $ioff<=31; $ioff++) {
      $jdnum++;
      $gregoriants='' . $gregmonth . '/' . (1 + $ioff) . '/' . $gregyear;
      $frenchts=extended_jdtofrench(($jdnum + 2));
      $jewishts=jdtojewish(($jdnum + 1));
      $juliants=jdtojulian($jdnum);
      $jewishtsz=jdtojewish(($jdnum + 1), true);
      $jewishtsh=iconv('WINDOWS-1255', 'UTF-8', $jewishtsz);
      $onl.=" if (eval('' + spans.length) > " . ($ioff + 1) . ") { ";
      $onl.=" spans[" . ($ioff + 1) . "].setAttribute('data-gregorian', '" . mdytodmy($gregoriants) . "');  ";
      $onl.=" spans[" . ($ioff + 1) . "].setAttribute('data-julian', '" . mdytodmy($juliants) . "');  ";
      $onl.=" spans[" . ($ioff + 1) . "].setAttribute('data-french', '" . $frenchts . "'); ";
      $onl.=" spans[" . ($ioff + 1) . "].setAttribute('data-jewish', '" . mdytodmyj($jewishts) . "'); ";
      $onl.=" spans[" . ($ioff + 1) . "].setAttribute('data-jewishy', '" . $jewishtsh . "');   ";
      $onl.=" } ";
    }
    }
    
    echo "<html><head><meta charset='UTF-8'/></head><body onload=\"" . $onl . "\"></body></html>";
    exit;
  } else if (isset($_POST['tihis'])) {
    if (isset($_POST['ticpc'])) {
    if (strpos(str_replace('+',' ',urldecode($_POST['ticpc'])) . '~$#', '.cpc~$#') !== false && strpos('~$#' . str_replace('+',' ',urldecode($_POST['ticpc'])), '~$#tmp' . DIRECTORY_SEPARATOR) !== false) {
    //file_put_contents('zx.zx9','' . str_replace('+',' ',urldecode($_POST['ticpc'])));
    file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . str_replace('+',' ',urldecode($_POST['ticpc'])), str_replace('+',' ',urldecode($_POST['tihis'])));
    }
    } else {
    file_put_contents('tihis.txt', str_replace('+',' ',urldecode($_POST['tihis'])));
    }
    exit;
  } else if (isset($_GET['tihis'])) {
   if (strpos(str_replace('+',' ',urldecode($_GET['tihis'])) . '~$#', '.cpc~$#') !== false && strpos('~$#' . str_replace('+',' ',urldecode($_GET['tihis'])), '~$#tmp' . DIRECTORY_SEPARATOR) !== false) {
   if (file_exists(str_replace('+',' ',urldecode($_GET['tihis'])))) {
    $newih=file_get_contents(str_replace('+',' ',urldecode($_GET['tihis'])));
    if (trim($newih) == $newih) {
    file_put_contents(str_replace('+',' ',urldecode($_GET['tihis'])),    file_get_contents(str_replace('+',' ',urldecode($_GET['tihis']))) . ' '  );
    } else if (3 == 4) {
    unlink(str_replace('+',' ',urldecode($_GET['tihis'])));
    }
    if (strpos($newih, '<cap' . 'tion ') !== false) {
      if (str_replace('?','',explode('tihis=', $_SERVER['QUERY_STRING'])[0]) != '') {
      $usualis=file_get_contents('http://www.rjmprogramming.com.au/calendar_contenteditable.html?' . str_replace('?','',explode('tihis=', $_SERVER['QUERY_STRING'])[0]));
      } else if (strpos($newih, '<cap' . 'tion data-durl="') !== false) {
      $usualis=file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . basename(explode('"',explode('<cap' . 'tion data-durl="', $newih)[1])[0]));
      } else {
      $usualis=file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'calendar_contenteditable.html');
      }
      $bittochange='<cap' . 'tion ' . explode('</table>', explode('<cap' . 'tion ', $usualis)[-1 + sizeof(explode('<cap' . 'tion ', $usualis))])[0];
      echo str_replace($bittochange, $newih, $usualis);
      exit;
    } else {
      header('Location: /calendar_contenteditable.html');
      exit;
    }
   } else {
    header('Location: /calendar_contenteditable.html');
    exit;
   }
   } else {
    header('Location: /calendar_contenteditable.html');
    exit;
   }
  } else if (file_exists('tihis.txt')) {
    $newih=file_get_contents('tihis.txt');
    unlink('tihis.txt');
    if (strpos($newih, '<cap' . 'tion ') !== false) {
      if (strpos($newih, '<cap' . 'tion data-durl="') !== false) {
      $usualis=file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . basename(explode('"',explode('<cap' . 'tion data-durl="', $newih)[1])[0]));
      } else {
      $usualis=file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'calendar_contenteditable.html');
      }
      $bittochange='<cap' . 'tion ' . explode('</table>', explode('<cap' . 'tion ', $usualis)[-1 + sizeof(explode('<cap' . 'tion ', $usualis))])[0];
      echo str_replace($bittochange, $newih, $usualis);
      exit;
    } else {
      header('Location: /calendar_contenteditable.html');
      exit;
    }
  } else {
    header('Location: /calendar_contenteditable.html');
    exit;
  }
?>
