<?php
// pie_chart_python.php
// RJM Programming - September, 2026

$islocal=false;
$serverremoteaddr='';
$chartname="Pie Chart";
$chartprefix='';
$chartnoblanks='';
$okdef='10,20,30,40';
$okldef='"Label1","Label2","Label3","Label4"';
$oktdef='';
if (php_sapi_name() === 'cli') {
  if ($argc > 1) {
   if (strpos($argv[0], '_python') !== false) {
    $chartprefix=explode('/', explode('_python', $argv[0])[0])[-1 + sizeof(explode('/', explode('_python', $argv[0])[0]))];
   }
  }
} else if (isset($_SERVER['REQUEST_URI'])) {
  if (strpos($_SERVER['REQUEST_URI'], '_python') !== false) {
    if (strpos($_SERVER['SERVER_NAME'], 'localhost') !== false) {
     $islocal=true;
    }
    $chartprefix=explode('/', explode('_python', $_SERVER['REQUEST_URI'])[0])[-1 + sizeof(explode('/', explode('_python', $_SERVER['REQUEST_URI'])[0]))];
  }
}
if ($chartprefix != '') {
  $words=explode('_', $chartprefix);
  $chartname=strtoupper(substr($words[0],0,1)) . strtolower(substr($words[0],1));
  for ($iw=1; $iw<sizeof($words); $iw++) {
    $chartname.=' ' . strtoupper(substr($words[$iw],0,1)) . strtolower(substr($words[$iw],1));
  }
} else {
  $charprefix=str_replace(' ','_',strtolower(str_replace('+',' ',urldecode($chartname))));
}
$chartnoblanks=str_replace('_','',$chartprefix) . 'python';

function server_remote_addr() {
    global $argc, $argv, $chartnoblanks;
    if (php_sapi_name() === 'cli') {
      if ($argc > 1) {
        return explode('.py',explode($chartnoblanks . '_', $argv[-1 + $argc])[-1 + sizeof(explode($chartnoblanks . '_', $argv[-1 + $argc]))])[0];
      }
    }
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
    if (isset($_GET['outfile'])) {
      if (urldecode($_GET['outfile']) != "") {
        $stfle='<input type=hidden name=outfile id=outfile value="' . urldecode($_GET['outfile']) . '"></input> ';
        return urldecode($_GET['outfile']);
      }
    } else if (isset($_POST['outfile'])) {
      $notpost='POST'; 
      if (urldecode($_POST['outfile']) != "") {
        $stfle='<input type=hidden name=outfile id=outfile value="' . urldecode($_POST['outfile']) . '"></input> ';
        return urldecode($_POST['outfile']);
      }
    }
     if (isset($_GET['parentipaddr'])) { 
      return str_replace('+',' ',urldecode($_GET['parentipaddr']));
    }
   return str_replace(":", "_", $rma);
}

if (php_sapi_name() === 'cli') {
if (file_exists('/usr/local/bin/python3')) {
$command = '/usr/local/bin/python3 ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py ' . dirname(__FILE__) . DIRECTORY_SEPARATOR; // Your command
} else if (file_exists('/bin/python3')) {
$command = '/bin/python3 ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py ' . dirname(__FILE__) . DIRECTORY_SEPARATOR; // Your command
} else if (file_exists('/bin/python')) {
$command = '/bin/python ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py ' . dirname(__FILE__) . DIRECTORY_SEPARATOR; // Your command
} else if (file_exists('/usr/local/bin/python')) {
$command = '/usr/local/bin/python ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py ' . dirname(__FILE__) . DIRECTORY_SEPARATOR; // Your command
} else {
$command = 'python ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py ' . dirname(__FILE__) . DIRECTORY_SEPARATOR; // Your command
}
if (1 == 1) {
//file_put_contents('xxxxx.xxxxx', server_remote_addr());
//file_put_contents('xxxx.xxxx', $command);
exec($command); // . '; rm -f ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py');
} else {
// Open the process pointer for reading
$handle = popen($command, 'r');

if ($handle) {
    // Read and output the buffer until the end of the file
    while (!feof($handle)) {
        echo fread($handle, 2048);
        flush(); // Send it to the browser immediately
    }
    pclose($handle);
}
}
exit;
} else if (strpos(str_replace('&title=','',str_replace('&labels=','',str_replace('?','',$_SERVER['QUERY_STRING']))), '&') === false) {
$serverremoteaddr='_' . server_remote_addr();
$waspy=file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartprefix . '_python.py');
      //file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'xxxxxx.xxxxxx', $waspy);
if (strpos($waspy, "nums = []") !== false) {
      //file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'xxxxxx.xxxxxx2', $waspy);
    $waspy=str_replace("img.load()","#img.load()",$waspy);
    $waspy=str_replace("img.show()","#img.show()",$waspy);
    $waspy=str_replace("with Image.open(","#with Image.open(",$waspy);
    if (isset($_GET['array'])) {
      $serverremoteaddr='_' . server_remote_addr();
      $okdef= str_replace('+',' ',urldecode($_GET['array']));
      //file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'xxxxxx.xxxxxx3', str_replace("nums = []","nums = [" . str_replace('+',' ',urldecode($_GET['array'])) . "]",$waspy));
      if (isset($_GET['title'])) {
         $oktdef=str_replace('+',' ',urldecode($_GET['title']));
         $waspy=str_replace('mytitle = ""','mytitle = "' . str_replace('"','`',$oktdef) . '"',$waspy);
      }
      if (isset($_GET['labels'])) {
      $getlb=str_replace('+',' ',urldecode($_GET['labels']));
      if (strpos($getlb, ",") !== false) {
       if (strpos($getlb, "','") === false && strpos($getlb, '","') === false) {
         $getlb=('"' . str_replace(',','","',$getlb) . '"');
       }
      }
      $okldef=$getlb;
      $waspy=str_replace("nums = []","nums = [" . str_replace('+',' ',urldecode($_GET['array'])) . "]",$waspy);
      $waspy=str_replace("lbls = []","lbls = [" . $getlb . "]",$waspy);
      file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py', $waspy);
      } else {
      $waspy=str_replace("nums = []","nums = [" . str_replace('+',' ',urldecode($_GET['array'])) . "]",$waspy);
      file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py', $waspy);
      }
      if ($islocal) {
if (file_exists('/usr/local/bin/python3')) {
$command = '/usr/local/bin/python3 ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py ' . basename(dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py') . ' ' . dirname(__FILE__) . DIRECTORY_SEPARATOR; // Your command
} else if (file_exists('/bin/python3')) {
$command = '/bin/python3 ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py ' . basename(dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py') . ' '  . dirname(__FILE__) . DIRECTORY_SEPARATOR; // Your command
} else if (file_exists('/bin/python')) {
$command = '/bin/python ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py ' . basename(dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py') . ' '  . dirname(__FILE__) . DIRECTORY_SEPARATOR; // Your command
} else if (file_exists('/usr/local/bin/python')) {
$command = '/usr/local/bin/python ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py ' . basename(dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py') . ' '  . dirname(__FILE__) . DIRECTORY_SEPARATOR; // Your command
} else {
$command = 'python ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py ' . basename(dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py') . ' '  . dirname(__FILE__) . DIRECTORY_SEPARATOR; // Your command
}
exec($command);
      }
    } else {
      echo "<html>
      <head><title>" . $chartname . " - Python Matplotlib - RJM Programming - September, 2026</title></head>
      <body>
      <h1>" . $chartname . "</h1>
      <h2>Using Python Matplotlib</h2>
      <h3>RJM Programming - September, 2026</h3>
      <form method=GET target='_self' action='./" . $chartprefix . "_python.php' onsubmit=\" if (document.getElementById('array').value.trim() == '') { return false; } document.body.style.cursor='progress'; return true;\">
      <input style=\"width:50%;\" title=\"" . $chartname . " sector percentages\" placeholder=\"" . $chartname . " sector percentages\" type=text id=array name=array value='10,20,30,40'></input><br><br>
      <input style=\"width:50%;\" title=\"" . $chartname . " sector labels\" placeholder=\"" . $chartname . " sector labels\" type=text id=labels name=labels value='" . $okldef . "'></input><br><br>
      <input style=\"width:50%;\" title=\"" . $chartname . " title\" placeholder=\"" . $chartname . " title\" type=text id=mytitle name=title value='" . $oktdef . "'></input><br><br>
      <input style=\"background-color:yellow;color:purple;\" type=submit value=\"Display " . $chartname . "\"></input>
      </form>
      </body>
      </html>
      ";
      exit;
    }
}
if (file_exists('/Applications/MAMP/bin/php/php7.4.33/bin/php')) {
exec('cd ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . ' ; /Applications/MAMP/bin/php/php7.4.33/bin/php -n ./' . $chartprefix . '_python.php > /dev/null 2> /dev/null ; open ./plot' . $serverremoteaddr . '.png');
} else {
exec('cd ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . ' ; ksh -c "' . dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartprefix . '_python.ksh ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartprefix . '_python.php ' . '_' . server_remote_addr() . ' ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py"');
exec('chmod 777 ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py');
}
if ($islocal) {
 sleep(3);
} else {
 while (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . $chartnoblanks . '_' . server_remote_addr() . '.py')) {
  sleep(3);
 }
}
}
echo "<html>
      <head><title>" . $chartname . " - Python Matplotlib - RJM Programming - September, 2026</title></head>
      <body data-style=\"background:url(./plot" . $serverremoteaddr . ".png);background-repeat:no-repeat;background-size:contain;\">
      <table><tr><td style=vertical-align:top;padding-right:20px;><img src=./plot" . $serverremoteaddr . ".png></img></td><td style=vertical-align:top;padding-left:20px;><h1>" . $chartname . "</h1>
      <h2>Using Python Matplotlib</h2>
      <h3>RJM Programming - <br>September, 2026</h3>
      <form method=GET target='_self' action='./" . $chartprefix . "_python.php' onsubmit=\" if (document.getElementById('array').value.trim() == '') { return false; } document.body.style.cursor='progress'; return true;\">
      <input title=\"" . $chartname . " sector percentages\" placeholder=\"" . $chartname . " sector percentages\" type=text id=array name=array value='" . $okdef . "'></input><br><br>
      <input title=\"" . $chartname . " sector labels\" placeholder=\"" . $chartname . " sector labels\" type=text id=labels name=labels value='" . $okldef . "'></input><br><br>
      <input title=\"" . $chartname . " title\" placeholder=\"" . $chartname . " title\" type=text id=mytitle name=title value='" . $oktdef . "'></input><br><br>
      <input style=\"background-color:yellow;color:purple;\" type=submit value=\"Display " . $chartname . "\"></input>
      </form></td></tr></table>
</body></html>";
?>
