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

$islocal=false;
$posthone='';
$posthlist='';
$thisoptih='';
$serverremoteaddr='';
$thingo=" sector";
$sper=" percentages";
$stle=" titles";
$chartname="Pie Chart";
$chartprefix='';
$chartnoblanks='';
$okdef='10,20,30,40';
$okldef='"Label1","Label2","Label3","Label4"';
$labldelim="'";
$labtdelim="'";
$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'])) {
  foreach (glob(dirname(__FILE__) . DIRECTORY_SEPARATOR . '*_python.py') as $filename) {
    $bname=explode(".py", basename($filename))[0];
    if (strpos(',' . $posthlist . ',', ',' . str_replace('_','',$bname) . ',') === false) {
      if ($posthone == '') {
        $thisoptih='';
        for ($jj=0; $jj<sizeof(explode(' ',str_replace('_',' ',$bname))); $jj++) {
          if ($thisoptih == '') {
            $thisoptih=strtoupper(substr(explode(' ',str_replace('_',' ',$bname))[$jj],0,1)) . strtolower(substr(explode(' ',str_replace('_',' ',$bname))[$jj],1));
          } else {
            $thisoptih.=' ' . strtoupper(substr(explode(' ',str_replace('_',' ',$bname))[$jj],0,1)) . strtolower(substr(explode(' ',str_replace('_',' ',$bname))[$jj],1));
          }
        }
        $pothlist=str_replace('_','',$bname);
        $posthone="&nbsp;&nbsp;<sup><select onchange=\"if (this.value.trim().length != 0) { location.href='./' + this.value + '.php?rand=' + Math.floor(Math.random() * 1989786);  }\"><option value=''>++</option><option value='" . $bname . "'>" . str_replace(' Python','',$thisoptih) . "</option></select></sup>";
      } else {
        $thisoptih='';
        for ($jj=0; $jj<sizeof(explode(' ',str_replace('_',' ',$bname))); $jj++) {
          if ($thisoptih == '') {
            $thisoptih=strtoupper(substr(explode(' ',str_replace('_',' ',$bname))[$jj],0,1)) . strtolower(substr(explode(' ',str_replace('_',' ',$bname))[$jj],1));
          } else {
            $thisoptih.=' ' . strtoupper(substr(explode(' ',str_replace('_',' ',$bname))[$jj],0,1)) . strtolower(substr(explode(' ',str_replace('_',' ',$bname))[$jj],1));
          }
        }
        $pothlist.=',' . str_replace('_','',$bname);
        $posthone=str_replace("</select>", "<option value='" . $bname . "'>" . str_replace(' Python','',$thisoptih) . "</option></select>", $posthone);
      }
    }
  }
  if (strpos($pothlist, ',') === false) {
    $posthone="";
  }
  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('&rand=','',str_replace('&title=','',str_replace('&labels=','',str_replace('?','',$_SERVER['QUERY_STRING'])))), '&') === false) {
$serverremoteaddr='_' . server_remote_addr();
$waspy=file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . (1 == 11 ? $chartprefix : '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);
    $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 (strpos($oktdef, "'") !== false) {
           $labtdelim='"';
         }
         if (strpos($oktdef, $labtdelim) !== false) {
           $oktdef=str_replace($labtdelim, "`", $oktdef);
         }
      }
      if (isset($_GET['labels'])) {
      $getlb=str_replace('+',' ',urldecode($_GET['labels']));
      $otherlb='';
      if (strpos($getlb, ",") !== false) {
       if (strpos($getlb, "','") === false && strpos($getlb, '","') === false) {
         $getlb=('"' . str_replace(',','","',str_replace('"','`',$getlb)) . '"');
         $wds=explode('","', substr($getlb,1,(-2 + strlen($getlb))));
         for ($ii=0; $ii<sizeof($wds); $ii++) {
           if (strpos($wds[$ii], '"') !== false || strpos($wds[$ii], $labldelim) !== false) { $getlb=str_replace($wds[$ii], str_replace($labldelim,'`',$wds[$ii]), str_replace($wds[$ii], str_replace('"','`',$wds[$ii]), $getlb));   }
         }
       } else if (strpos($getlb, "','") !== false) {
         $labldelim='"';
         $wds=explode("','", substr($getlb,1,(-2 + strlen($getlb))));
         for ($ii=0; $ii<sizeof($wds); $ii++) {
           if (strpos($wds[$ii], "'") !== false || strpos($wds[$ii], $labldelim) !== false) { $getlb=str_replace($wds[$ii], str_replace($labldelim,'`',$wds[$ii]), str_replace($wds[$ii], str_replace("'","`",$wds[$ii]), $getlb));   }
         }
       } else {
         $wds=explode('","', substr($getlb,1,(-2 + strlen($getlb))));
         for ($ii=0; $ii<sizeof($wds); $ii++) {
           if (strpos($wds[$ii], '"') !== false || strpos($wds[$ii], $labldelim) !== false) { $getlb=str_replace($wds[$ii], str_replace($labldelim,'`',$wds[$ii]), str_replace($wds[$ii], str_replace('"','`',$wds[$ii]), $getlb));   }
         }
       }
      }
      if ($otherlb != '') {
       $okldef=$otherlb;
      } else {
       $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 {

if (strpos("~" . $chartname, "~Column") !== false) {
 $thingo=" axis";
 $sper=" Y values";
 $stle=" X labels";
} else if (strpos("~" . $chartname, "~Bar") !== false) {
 $thingo=" axis";
 $sper=" X values";
 $stle=" Y labels";
}

      echo "<html>
      <head><title>" . $chartname . " - Python Matplotlib - RJM Programming - September, 2026</title>
      <style>
      input[type=submit]:focus { border: 3px dotted green;  }
      </style>
      </head>
      <body>
      <h1>" . $chartname . $posthone . "</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 . $thingo . $sper . "\" placeholder=\"" . $chartname . $thingo . $sper . "\" type=text id=array name=array value='10,20,30,40'></input><br><br>
      <input style=\"width:50%;\" title=\"" . $chartname . $thingo . $stle . "\" placeholder=\"" . $chartname . $thingo . $stle . "\" type=text id=labels name=labels value=" . $labldelim . $okldef . $labldelim . "></input><br><br>
      <input style=\"width:50%;\" title=\"" . $chartname . " title\" placeholder=\"" . $chartname . " title\" type=text id=mytitle name=title value=" . $labtdelim . $oktdef . $labtdelim . "></input><br><br>
      <input type=hidden name=rand value='" . rand(0,5645342) . "'></input>
      <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);
 }
}
}

if (strpos("~" . $chartname, "~Column") !== false) {
 $thingo=" axis";
 $sper=" Y values";
 $stle=" X labels";
} else if (strpos("~" . $chartname, "~Bar") !== false) {
 $thingo=" axis";
 $sper=" X values";
 $stle=" Y labels";
}

echo "<html>
      <head><title>" . $chartname . " - Python Matplotlib - RJM Programming - September, 2026</title>
      <style>
      input[type=submit]:focus { border: 3px dotted green;  }
      </style>
      </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 . $posthone . "</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 . $thingo . $sper . "\" placeholder=\"" . $chartname . $thingo . $sper . "\" type=text id=array name=array value='" . $okdef . "'></input><br><br>
      <input title=\"" . $chartname . $thingo . $stle . "\" placeholder=\"" . $chartname . $thingo . $stle . "\" type=text id=labels name=labels value=" . $labldelim . $okldef . $labldelim . "></input><br><br>
      <input title=\"" . $chartname . " title\" placeholder=\"" . $chartname . " title\" type=text id=mytitle name=title value=" . $labtdelim . $oktdef . $labtdelim . "></input><br><br>
      <input type=hidden name=rand value='" . rand(0,5645342) . "'></input>
      <input style=\"background-color:yellow;color:purple;\" type=submit value=\"Display " . $chartname . "\"></input>
      </form></td></tr></table>
</body></html>";
?>
