compile.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. include_once("../../../auth.php");
  3. include_once("../../../SystemAOB/functions/personalization/configIO.php");
  4. $configs = getConfig("encoding",true);
  5. putenv('LANG=en_US.UTF-8');
  6. //Check if the source file is valid
  7. if (!isset($_GET['source']) || !file_exists("../" . $_GET['source'])){
  8. if (file_exists($_GET['source'])){
  9. //File located in external storage
  10. die("Error. You cannot compile in external storage. " . $_GET['source'] . ' given.');
  11. }else{
  12. die("Error. Source file not defined or not found. " . $_GET['source'] . ' given.');
  13. }
  14. }
  15. //Check if the path is located inside AOR (Yes, AOR Only, not external media)
  16. if (!(strpos(realpath("../" . $_GET['source']),realpath($rootPath)) !== false)){
  17. die("Error. Script is not located within ArOZ Online Root.");
  18. }
  19. $filepath = realpath("../" . $_GET['source']);
  20. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  21. //use Tiny gcc for the compilation, output file will be the same name as the source filename
  22. $binaryPath = dirname($filepath) . "\\" . basename($filepath,pathinfo($filepath, PATHINFO_EXTENSION)) . "exe";
  23. $output = shell_exec('tcc\\tcc.exe "' . $filepath .'" -o "'. $binaryPath . '" 2>&1');
  24. if ($configs["winHostEncoding"][3] == "true"){
  25. $output = mb_convert_encoding($output, "UTF-8",$configs["forceEncodingType"][3]);
  26. }
  27. if (trim($output) == ""){
  28. if (file_exists($binaryPath)){
  29. if (isset($_GET['run'])){
  30. //Run the application after compile.
  31. $output = shell_exec($binaryPath . ' 2>&1');
  32. if ($configs["winHostEncoding"][3] == "true"){
  33. $output = mb_convert_encoding($output, "UTF-8",$configs["forceEncodingType"][3]);
  34. }
  35. echo $output;
  36. }else{
  37. //Compile only
  38. echo 'Compiled succeed. Exported file: ' . $binaryPath;
  39. }
  40. }else{
  41. die("Unknown Error. Compiler do not return anything.");
  42. }
  43. }else{
  44. echo "Error. <br>" . nl2br($output);
  45. }
  46. } else {
  47. //Linux, use build in gcc
  48. $binaryPath = dirname($filepath) . "/" . basename($filepath,pathinfo($filepath, PATHINFO_EXTENSION)) . "out";
  49. $output = shell_exec('gcc "' . $filepath .'" -o "'. $binaryPath . '" 2>&1');
  50. if (trim($output) == ""){
  51. if (file_exists($binaryPath)){
  52. if (isset($_GET['run'])){
  53. //Run the application after compile.
  54. $output = shell_exec($binaryPath . ' 2>&1');
  55. echo nl2br($output);
  56. }else{
  57. //Compile only
  58. echo 'Compiled succeed. Exported file: ' . $binaryPath;
  59. }
  60. }else{
  61. die("Unknown Error. Compiler do not return anything.");
  62. }
  63. }else{
  64. echo "Error. <br>" . nl2br($output);
  65. }
  66. }
  67. ?>