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

$okdef='10,20,30,40';
$serverremoteaddr='';

function server_remote_addr() {
    global $argc, $argv;
    if (php_sapi_name() === 'cli') {
      if ($argc > 1) {
        return explode('.py',explode('piechartpython_', $argv[-1 + $argc])[-1 + sizeof(explode('piechartpython_', $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 . 'piechartpython_' . server_remote_addr() . '.py ' . dirname(__FILE__) . DIRECTORY_SEPARATOR; // Your command
} else if (file_exists('/bin/python3')) {
$command = '/bin/python3 ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'piechartpython_' . server_remote_addr() . '.py ' . dirname(__FILE__) . DIRECTORY_SEPARATOR; // Your command
} else if (file_exists('/bin/python')) {
$command = '/bin/python ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'piechartpython_' . 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 . 'piechartpython_' . server_remote_addr() . '.py ' . dirname(__FILE__) . DIRECTORY_SEPARATOR; // Your command
} else {
$command = 'python ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'piechartpython_' . 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 . 'piechartpython_' . 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('?','',$_SERVER['QUERY_STRING']), '&') === false) {
$serverremoteaddr='_' . server_remote_addr();
$waspy=file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pie_chart_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);
    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));
      file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'piechartpython_' . server_remote_addr() . '.py', str_replace("nums = []","nums = [" . str_replace('+',' ',urldecode($_GET['array'])) . "]",$waspy));
    } else {
      echo "<html>
      <body>
      <h1>Pie Chart</h1>
      <h2>Using Python Matplotlib</h2>
      <h3>RJM Programming - September, 2026</h3>
      <form method=GET target='_self' action='./pie_chart_python.php' onsubmit=\" if (document.getElementById('array').value.trim() == '') { return false; } document.body.style.cursor='progress'; return true;\">
      <input type=text id=array name=array value='10,20,30,40'></input><br><br>
      <input style=\"background-color:yellow;color:purple;\" type=submit value=\"Display Pie Chart\"></input>
      </form>
      </body>
      </html>
      ";
      exit;
    }
}
//exec('php ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pie_chart_python.php > /dev/null 2> /dev/null');
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 ./pie_chart_python.php > /dev/null 2> /dev/null ; open ./plot.png');
} else {
//file_put_contents('xx.xx','cd ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . ' ; ksh -c "' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pie_chart_python.ksh ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'piechartpython_' . server_remote_addr() . '.py"');
exec('cd ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . ' ; ksh -c "' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pie_chart_python.ksh ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pie_chart_python.php ' . '_' . server_remote_addr() . ' ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'piechartpython_' . server_remote_addr() . '.py"');
exec('chmod 777 ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'piechartpython_' . server_remote_addr() . '.py');
}
while (file_exists(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'piechartpython_' . server_remote_addr() . '.py')) {
 sleep(3);
}
}
echo "<html><body style=\"background:url(./plot" . $serverremoteaddr . ".png);background-repeat:no-repeat;background-size:contain;\"><h1>Pie Chart</h1>
      <h2>Using Python Matplotlib</h2>
      <h3>RJM Programming - September, 2026</h3>
      <form method=GET target='_self' action='./pie_chart_python.php' onsubmit=\" if (document.getElementById('array').value.trim() == '') { return false; } document.body.style.cursor='progress'; return true;\">
      <input type=text id=array name=array value='" . $okdef . "'></input><br><br>
      <input type=submit value=\"Display Pie Chart\"></input>
      </form>
</body></html>";
?>
