<?php
// subgraph_example.php
// RJM Programming
// September, 2024
// Trying out Python GraphViz package ... thanks to https://graphviz.readthedocs.io/en/stable/
 
 $ourtmp=$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR;
  
 foreach ( $_GET as $key => $value ) {  $_POST[$key] = $value; } 
  $abl=['round-table','King Arthur','Sir Bedevere the Wise','Sir Lancelot the Brave'];
  $newabl=['solar-system-planets','Sun','Earth','Moon'];
  $prefix='';
  $suffix='';
  $yes='n';
  
function server_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    return str_replace('.','_',$rma);
}

  if (!isset($_GET['one']) && !isset($_GET['two']) && !isset($_GET['three']) && !isset($_GET['original'])) {
    $yes='y';
    $_GET['one']=$newabl[1];
    $_GET['two']=$newabl[2];
    $_GET['three']=$newabl[3];
  }

  if (isset($_GET['title'])) {
  $newabl[0]=str_replace('+',' ',urldecode($_GET['title']));
  } else if (!isset($_GET['original'])) {
  $_GET['title']=$newabl[0];
  }
   
  if (isset($_GET['one']) && isset($_GET['two']) && isset($_GET['three'])) {
  $prefix=$_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR; //'/tmp/';
  $newabl[1]=str_replace('+',' ',urldecode($_GET['one']));
  $newabl[2]=str_replace('+',' ',urldecode($_GET['two']));
  $newabl[3]=str_replace('+',' ',urldecode($_GET['three']));
  $pycont=file_get_contents('./subgraph_example.py');
  $pycont=str_replace($abl[0], str_replace("\n", "' + \"\\n\" + '",str_replace("'", "' + \"'\" + '",str_replace('+',' ',urldecode(str_replace("%0D","",$_GET['title']))))), $pycont);
  $pycont=str_replace($abl[1], str_replace("\n", "' + \"\\n\" + '",str_replace("'", "' + \"'\" + '",str_replace('+',' ',urldecode(str_replace("%0D","",$_GET['one']))))), $pycont);
  $pycont=str_replace($abl[2], str_replace("\n", "' + \"\\n\" + '",str_replace("'", "' + \"'\" + '",str_replace('+',' ',urldecode(str_replace("%0D","",$_GET['two']))))), $pycont);
  $pycont=str_replace($abl[3], str_replace("\n", "' + \"\\n\" + '",str_replace("'", "' + \"'\" + '",str_replace('+',' ',urldecode(str_replace("%0D","",$_GET['three']))))), $pycont);
  $pycont=str_replace('doctest-output', $prefix . 'doctest-output', $pycont);
  if ($yes != 'y') { // use crontab 
  $suffix='_' . server_remote_addr();
  file_put_contents($prefix . 'subgraph_example_' . $suffix . '.py', $pycont);
  file_put_contents($prefix . 'subgraph_example_' . $suffix . '.ksh', "#!/bin/ksh\n/usr/bin/python3 " . $prefix . "subgraph_example_" . $suffix . ".py >> " . $prefix . "subgraph_example.good 2>> " . $prefix . "subgraph_example.bad \nln -s " . $prefix . "doctest-output" . DIRECTORY_SEPARATOR . $newabl[0] . ".gv.pdf " . $_SERVER['DOCUMENT_ROOT'] .  DIRECTORY_SEPARATOR . "doctest-output" . DIRECTORY_SEPARATOR . $newabl[0] . ".gv.pdf \nrm -f " . $prefix . "subgraph_example_" . $suffix . ".py \nrm -f " . $prefix . "subgraph_example_" . $suffix . ".ksh \nexit");
  exec('chmod 777 ' . $prefix . 'subgraph_example_' . $suffix . '.ksh');
  exec('ksh ' . $prefix . 'subgraph_example_' . $suffix . '.ksh');
  exec('chown root ' . $prefix . 'subgraph_example_' . $suffix . '.ksh');
  exec('chgrp root ' . $prefix . 'subgraph_example_' . $suffix . '.ksh');
  while (file_exists($prefix . 'subgraph_example_' . $suffix . '.ksh')) {
    sleep(10);
  }
  } else {
  file_put_contents($ourtmp . 'subgraph_example.py', $pycont);
  exec('chmod 777 ' . $ourtmp . 'subgraph_example.py');
  exec('chown rjmprogr ' . $ourtmp . 'subgraph_example.py');
  exec('chgrp rjmprogr ' . $ourtmp . 'subgraph_example.py');
  //if (file_exists('doctest-output' . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.pdf')) {
    //exec('rm -f ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.pdf');
    //exec('rm -f ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $newabl[0] . '.gv');
    //unlink('doctest-output' . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.pdf');
    //unlink('doctest-output' . DIRECTORY_SEPARATOR . $newabl[0] . '.gv');
  //}
  exec('/usr/bin/python3 ' . $ourtmp . 'subgraph_example.py >  ' . $ourtmp . 'subgraph_example.good 2>  ' . $ourtmp . 'subgraph_example.bad');
  sleep(8);
  if (file_exists($prefix . 'doctest-output' . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.pdf')) {
  exec('ln -s ' . $prefix . 'doctest-output' . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.pdf ' . $_SERVER['DOCUMENT_ROOT'] .  DIRECTORY_SEPARATOR . 'doctest-output' . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.pdf');
  }
  }
  } else {
  exec('/usr/bin/python3 subgraph_example.py');
  sleep(8);
  }
    
  if (file_exists('doctest-output' . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.pdf')) {
     exec('chmod 777 ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.pdf');
     exec('chmod 777 ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $newabl[0] . '.gv');
     exec('chown rjmprogr ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.pdf');
     exec('chown rjmprogr ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $newabl[0] . '.gv');
     exec('chgrp rjmprogr ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $newabl[0] . '.gv.pdf');
     exec('chgrp rjmprogr ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $newabl[0] . '.gv');
     echo "<html>
<head>
<title>Trying out Python GraphViz hosted in PHP - RJM Programming - September, 2024 - Triangular Relationship</title>
</head>
<body>
<h1>Trying out Python GraphViz hosted in PHP - Triangular Relationship<sup id=\"supgws\" style=\"vertical-align: top;\">&nbsp;<details title=\"Open to reveal other GraphViz web applications you can navigate to.\" style=\"display: inline-block;\" id=\"gvdtls\"><summary style=\"color: #e162bf;list-style-type:'\\01F4C8'\"></summary><select size=\"8\" id=\"gvwapps\" onchange=\"if (this.value != '') { this.style.cursor='progress'; location.href=this.value; }\"><option value=\"\">Optionally select a GraphViz web application below ...</option><option value=\"/dot_colour_wheel.php\">Colour Wheel</option><option value=\"/family_tree.php\">Family Tree</option><option value=\"/hierarchy_organization.php\">Organization Hierarchy</option><option value=\"/html_table_basis.php\">HTML Table</option><option value=\"/subgraph_example.php\">Triangular Relationship</option><option value=\"/twopi_vs_circo_example.php\">Circular Layout</option><option value=\"/venn_diagram_basis.php\">Venn Diagram</option></select></details></sup></h1>
<h3>RJM Programming - September, 2024</h3>
<h4>Thanks to <a target=_blank title='Python GraphViz package information' href='//graphviz.readthedocs.io/en/stable/'>https://graphviz.readthedocs.io/en/stable/</a></h4>
<table style=width:98%;><tr><td>
<iframe style='width:65%;height:400px;' type=application/pdf src='/doctest-output" . DIRECTORY_SEPARATOR . $newabl[0] . ".gv.pdf?rand=" . rand(13456,6789056) . "'></iframe>
</td><td style=background-color:#f0f0f0;>
<form onsubmit=\"if (document.getElementById('ititle').value.replace('solar-system-planets','').trim() == '') { alert('Please enter a different title, then click/tap Draw again.');  return false; } document.body.style.cursor='progress'; return true;\" method=GET action=./subgraph_example.php>
<p>Title ...</p><br>
<input type=text id=ititle name=title placeholder='" . $newabl[0] . "' value=''></input><br><br>
<p>Triangular Relationship Names ...</p><br>
<textarea name=one value=''>" . $newabl[1] . "</textarea><br>
<textarea name=two value=''>" . $newabl[2] . "</textarea><br>
<textarea name=three value=''>" . $newabl[3] . "</textarea><br><br>
<input type=submit value=Draw style=background-color:pink;></input>
</form>
</td></tr></table>
</body>
</html>";
  } else {
     echo "<html>
<head>
<title>Trying out Python GraphViz hosted in PHP - RJM Programming - September, 2024 - Triangular Relationship</title>
</head>
<body>
<h1>Trying out Python GraphViz hosted in PHP - Triangular Relationship<sup id=\"supgws\" style=\"vertical-align: top;\">&nbsp;<details title=\"Open to reveal other GraphViz web applications you can navigate to.\" style=\"display: inline-block;\" id=\"gvdtls\"><summary style=\"color: #e162bf;list-style-type:'\\01F4C8'\"></summary><select size=\"8\" id=\"gvwapps\" onchange=\"if (this.value != '') { this.style.cursor='progress'; location.href=this.value; }\"><option value=\"\">Optionally select a GraphViz web application below ...</option><option value=\"/dot_colour_wheel.php\">Colour Wheel</option><option value=\"/family_tree.php\">Family Tree</option><option value=\"/hierarchy_organization.php\">Organization Hierarchy</option><option value=\"/html_table_basis.php\">HTML Table</option><option value=\"/subgraph_example.php\">Triangular Relationship</option><option value=\"/twopi_vs_circo_example.php\">Circular Layout</option><option value=\"/venn_diagram_basis.php\">Venn Diagram</option></select></details></sup></h1>
<h3>RJM Programming - September, 2024</h3>
<h4>Thanks to <a target=_blank title='Python GraphViz package information' href='//graphviz.readthedocs.io/en/stable/'>https://graphviz.readthedocs.io/en/stable/</a></h4>
<table style=width:98%;><tr><td>
<iframe style='width:65%;height:400px;' type=application/pdf src='/doctest-output" . DIRECTORY_SEPARATOR . $newabl[0] . ".gv.pdf?rand=" . rand(13456,6789056) . "'></iframe>
</td><td style=background-color:#f0f0f0;>
<form onsubmit=\"if (document.getElementById('ititle').value.replace('solar-system-planets','').trim() == '') { alert('Please enter a different title, then click/tap Draw again.');  return false; } document.body.style.cursor='progress';  return true;\" method=GET action=./subgraph_example.php>
<p>Title ...</p><br>
<input type=text id=ititle name=title placeholder='" . $newabl[0] . " value=''></input><br><br>
<p>Triangular Relationship Names ...</p><br>
<textarea name=one value=''>" . $newabl[1] . "</textarea><br>
<textarea name=two value=''>" . $newabl[2] . "</textarea><br>
<textarea name=three value=''>" . $newabl[3] . "</textarea><br><br>
<input type=submit value=Draw style=background-color:pink;></input>
</form>
</td></tr></table>
</body>
</html>";
  }

?>
