<?php
// subgraph_example.php
// RJM Programming
// September, 2024
// Trying out Python GraphViz package ... thanks to https://graphviz.readthedocs.io/en/stable/

  $abl=['round-table','King Arthur','Sir Bedevere the Wise','Sir Lancelot the Brave'];
  $newabl=['solar-system-planets','Sun','Earth','Moon'];
  $prefix='';

  if (!isset($_GET['one']) && !isset($_GET['two']) && !isset($_GET['three']) && !isset($_GET['original'])) {
    $_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='/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('+',' ',urldecode($_GET['title'])), $pycont);
  $pycont=str_replace($abl[1], str_replace('+',' ',urldecode($_GET['one'])), $pycont);
  $pycont=str_replace($abl[2], str_replace('+',' ',urldecode($_GET['two'])), $pycont);
  $pycont=str_replace($abl[3], str_replace('+',' ',urldecode($_GET['three'])), $pycont);
  $pycont=str_replace('doctest-output', $prefix . 'doctest-output', $pycont);
  file_put_contents('/tmp/subgraph_example.py', $pycont);
  exec('chmod 777 ' . '/tmp/subgraph_example.py');
  exec('chown rjmprogr ' .'/tmp/subgraph_example.py');
  exec('chgrp rjmprogr ' . '/tmp/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 /tmp/subgraph_example.py > subgraph_example.good 2> 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</title>
</head>
<body>
<h1>Trying out Python GraphViz hosted in PHP</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 method=GET action=./subgraph_example.php>
<p>Title ...</p><br>
<input type=text name=title value='" . $newabl[0] . "'></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>";
  }

?>