<?php
// bubble_sort_c.php
// Uses and compiles a user modified bubble_sort.c ... thanks to https://www.w3schools.com/dsa/trydsa.php?filename=demo_bubblesort

  $lastresult="";

  $ccont=file_get_contents('./bubble_sort.c');

  $prefix=explode(' = {', $ccont)[0] . ' = {';
  $midbit=explode('}', explode($prefix, $ccont)[1])[0];
  $suffix=explode($prefix . $midbit, $ccont)[1];
  
  $altmidbit=$midbit;

  $httpis=" http";
  $endaf="youllveverfindthis";
  $endat="youllveverfindthis";
  $hbits=explode(' http', $prefix);
  if (sizeof($hbits) > 1) {
    $endaf=' http' . explode(' ', $hbits[1])[0];
    $endat=' http' . explode(' ', $hbits[1])[0] . '</a>';
    $httpis=$endaf;
  }
  
  if (isset($_POST['myarr'])) {
     $altmidbit=str_replace('+',' ',urldecode($_POST['myarr']));
     file_put_contents('new_bubble_sort.c', $prefix . $altmidbit . $suffix);
     exec('gcc new_bubble_sort.c -o bubble_sort');
     $lastresult='$ gcc new_bubble_sort.c -o bubble_sort <br>$ ./bubble_sort<br>';
     $lastresult.=shell_exec('./bubble_sort');
     $midbit=$altmidbit;
  } else if (isset($_GET['myarr'])) {
     $altmidbit=str_replace('+',' ',urldecode($_GET['myarr']));
     file_put_contents('new_bubble_sort.c', $prefix . $altmidbit . $suffix);
     exec('gcc new_bubble_sort.c -o bubble_sort');
     $lastresult='$ gcc new_bubble_sort.c -o bubble_sort <br>$ ./bubble_sort<br>';
     $lastresult.=shell_exec('./bubble_sort');
     $midbit=$altmidbit;
  }
  
  echo "<html>
<head>
<title>Bubble Sort Recompiles bubble_sort.c - RJM Programming - November, 2024 ... thanks to https://www.w3schools.com/dsa/trydsa.php?filename=demo_bubblesort</title>
</head>
<body onload=\"setTimeout(function(){ document.getElementById('myarr').focus(); }, 3000);\">
<h1>Bubble Sort Recompiles bubble_sort.c</h1>
<h3>RJM Programming - November, 2024</h3>
<table style=width:99%;>
<tr><td>
<form method=POST action=./bubble_sort_c.php style=background-color:lightblue;>
<span id=sprefix>" . str_replace($httpis," Thanks to <a onclick=\"window.open(this.innerHTML,'_blank','top=100,left=100,width=700,height=700');\" style=cursor:pointer;text-decoration:underline;>" . trim($httpis), str_replace($endaf, $endat, str_replace("\n", "<br>", str_replace('>','&gt;',str_replace('<','&lt;',$prefix))))) . "</span><textarea onblur=\"document.getElementById('mysub').click();\" name=myarr id=myarr style=display:inline-block;background-color:pink; rows=1 cols=" . (2 + strlen($midbit)) . " value=\"" . $midbit . "\">" . $midbit . "</textarea><span id=ssuffix>" . str_replace("\n", "<br>", str_replace('>','&gt;',str_replace('<','&lt;',$suffix))) . "</span>
<input type=submit id=mysub value=Submit style=display:none;></input>
</form>
</td><td style=vertical-align:top;><div id=result style=background-color:yellow;>" . $lastresult . "</div><br></tr></table>
</body>
</html>";

?>
