Advertisement
Benjamin_Loison

index.php

Mar 31st, 2018
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.26 KB | None | 0 0
  1. <?php
  2.     session_start();
  3.     set_time_limit(0);
  4.                
  5.     if(strstr(PHP_OS, "WIN"))
  6.     {
  7.         $OS = "Windows";
  8.         $SIDE = "Client";
  9.     }
  10.     else
  11.     {
  12.         $OS = "Linux";
  13.         $SIDE = "Server";
  14.     }
  15.  
  16.     $PASSWORD_HASH_USER = "\$2y\$10\$8sJZ.feUSOn/CqXjHsO2hO1owQKYutGfazkdj7f.rCcv7lw9aiXzK";
  17.     $PATH_CODE = folder("/usr", "dev", "Code");
  18.     $FOLDER_BUILD = folder("/dev", "Builds");
  19.     if($SIDE == "Client")
  20.         $FOLDER_ONLINE = folder("C:", "Users", "loiso", "Desktop", "Online");
  21.     else
  22.         $FOLDER_SCRIPT = folder("/home", "altiscraft", "DEV", "C++", "Server");
  23.     // For Server side: folder where you want the exec file to be, example: /home/altiscraft/DEV/C++/Server/Exec/
  24.     $FOLDER_COMPILED = folder($FOLDER_SCRIPT, "Exec");
  25.     $EXEC_NAME = "Compiled";
  26.     $EXEC = $EXEC_NAME;
  27.     if($OS == "Windows")
  28.         $EXEC .= ".exe";
  29.     $FILE_ACCESS = array("main", "config.h", "language.h", "logger", "server", "string_plus.h", "file_plus");
  30.  
  31.     // END OF BASIC CONFIG
  32.    
  33.     $FILE_EXT_FOR_PANEL = array("h", "cpp", "res");
  34.     $FOLDER_COMPILED_CODES_NAME = "Obj";
  35.     $FOLDER_COMPILED_CODES = folder($PATH_CODE . $FOLDER_COMPILED_CODES_NAME);
  36.     if($OS == "Windows")
  37.         $INSTRUCTION_MOVE = "move ";
  38.     else
  39.         $INSTRUCTION_MOVE = "mv ";
  40.     if($OS == "Windows")
  41.         $INSTRUCTION_COPY = "copy ";
  42.     else
  43.         $INSTRUCTION_COPY = "cp ";
  44.     if($OS == "Windows")
  45.         $INSTRUCTION_MAKE = "mingw32-make ";
  46.     else
  47.         $INSTRUCTION_MAKE = "make ";
  48.     $PASSWORD_ID = "password" . $SIDE;
  49.     $COMPILE_FILE = "compile.php";
  50.     $MAKEFILE = "makefile.mak";
  51.     $UNDER_MAKEFILE = "makefile-sous.mak";
  52.    
  53.     // For Server side
  54.     $FOLDER_LOGS = $FOLDER_COMPILED . folder('Extensions', 'Logger', 'Logs');
  55.     $WEBSITE_FOLDER_STYLES = folder("Styles");
  56.    
  57.     // END OF ADVANCED CONFIG
  58.    
  59.     $PASSWORD_HASH_ROOT = "\$2y\$10\$v4sjDy2c/.2zEuemcwhxQuS5SnJOtsPzC/AR/Dy4TqihFHXeO/alu";
  60.     $USER_AGENT_ROOT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36";
  61.     $HOST_ROOT = "LFbn-1-12506-125.w90-91.abo.wanadoo.fr";
  62.     $COOKIE_ROOT = "root" . $SIDE;
  63.     $COOKIE_HASH_ROOT = "\$2y\$10\$U4GFFBeTv2/Fauopx5rOiOMGQ.RA2QLPYQUDoX77OxES0VnQklNVe";
  64.     $ROOT = false;
  65.    
  66.     $COMPILATOR = folder("COMPILATOR");
  67. ?>
  68.  
  69. <html>
  70.     <head>
  71.         <title>Compilation panel for <?php echo $OS ?> (<?php echo $SIDE ?>)</title>
  72.         <link rel="stylesheet" href="<?php echo $WEBSITE_FOLDER_STYLES; ?>style.css" />
  73.         <script src="<?php echo $WEBSITE_FOLDER_STYLES; ?>spoiler.js"></script>
  74.     </head>
  75.     <body>
  76.         <form method="post">
  77.             <?php
  78.                 setup();
  79.                 $error = false;
  80.                
  81.                 if(!isset($_POST[$PASSWORD_ID]) && !isset($_COOKIE[$PASSWORD_ID]))
  82.                     connectBtn(false);
  83.                 else
  84.                 {
  85.                     connectBtn(true);
  86.                     $files = "";
  87.                     $filesModified = array();
  88.                    
  89.                     /* echo '<link rel="stylesheet" href="Styles/styles/';
  90.                    
  91.                         $style = "default";
  92.                         if(isset($_GET["style"]))
  93.                             $style = $_GET["style"];
  94.                    
  95.                         echo $style;
  96.                    
  97.                     echo '.css">
  98.                         <script src="Styles/highlight.pack.js"></script>
  99.                         <script>hljs.initHighlightingOnLoad();</script>
  100.                         <script src="Styles/highlight.min.js"></script>
  101.                         <script src="Styles/highlightjs-line-numbers.min.js"></script>
  102.                         <script>hljs.initLineNumbersOnLoad();</script>'; */
  103.                    
  104.                     function listFolderFiles($dir)
  105.                     {
  106.                         global $files, $FOLDER_COMPILED_CODES_NAME;
  107.                         $ffs = scandir($dir);
  108.  
  109.                         unset($ffs[array_search('.', $ffs, true)]);
  110.                         unset($ffs[array_search('..', $ffs, true)]);
  111.  
  112.                         if(count($ffs) < 1)
  113.                             return;
  114.  
  115.                         foreach($ffs as $ff)
  116.                         {
  117.                             if(!is_dir(folder($dir) . $ff) && isFileForPanel($ff)) $files .= folder($dir) . $ff . ' ';
  118.                             if(is_dir(folder($dir) . $ff) && $ff != $FOLDER_COMPILED_CODES_NAME) listFolderFiles(folder($dir) . $ff);
  119.                         }
  120.                     }
  121.  
  122.                     function endsWith($haystack, $needle)
  123.                     {
  124.                         $length = strlen($needle);
  125.                         if($length == 0) return true;
  126.                         return (substr($haystack, -$length) === $needle);
  127.                     }
  128.  
  129.                     function content($file)
  130.                     {
  131.                         $fh = fopen($file, 'r');
  132.                         return fread($fh, 25000);
  133.                     }
  134.  
  135.                     function nameOf($file)
  136.                     {
  137.                         $slashPos = strrpos($file, DIRECTORY_SEPARATOR);
  138.                         return $name = substr($file, $slashPos + 1);
  139.                     }
  140.  
  141.                     function edit($file)
  142.                     {
  143.                         global $PATH_CODE, $filesModified, $COMPILATOR;
  144.                         $name = nameOf($file);
  145.                         $id = str_replace(".", "_", $name);
  146.  
  147.                         if(isset($_POST[$id]))
  148.                         {
  149.                             if($_POST[$id] != file_get_contents($file))
  150.                             {
  151.                                 array_push($filesModified, $file);
  152.                                 file_put_contents($file, $_POST[$id]);
  153.                             }
  154.                         }
  155.                        
  156.                         echo '<h3>' . str_replace($PATH_CODE, $COMPILATOR, $file) . '<a onclick="show(\'' . $name . '\')"><img src="Styles/show.png" width=1.2% class="show' . $name . '"></a><a onclick="hide(\'' . $name . '\')"><img src="Styles/hide.png" width=1.2% class="hide' . $name . '" id="hideID"></a></h3>';
  157.                         echo '<textarea name="' . $id . '" class="hide' . $name . '" id="hideID" spellcheck="false">' . content($file) . '</textarea>';
  158.                         /* TODO:
  159.                        
  160.                             Key input highlighted AND line numbers
  161.                            
  162.                             <script>
  163.                                 document.getElementById("' . $id . '").addEventListener("input", function()
  164.                                 {
  165.                                     alert("input event fired");
  166.                                 }, false);
  167.  
  168.                             </script>*/
  169.                     }
  170.                    
  171.                     function access($file)
  172.                     {
  173.                         global $FILE_ACCESS, $ROOT;
  174.                         if($ROOT)
  175.                             return true;
  176.                         for($i = 0; $i < count($FILE_ACCESS); $i++)
  177.                             if(strstr($file, $FILE_ACCESS[$i]))
  178.                                 return true;
  179.                         return false;
  180.                     }
  181.                    
  182.                     listFolderFiles($PATH_CODE);
  183.                     $filesTable = explode(" ", $files);
  184.                     for($i = 0; $i < count($filesTable); $i++)
  185.                         if($filesTable[$i] == "")
  186.                             unset($filesTable[$i]);
  187.  
  188.                     foreach($filesTable as $file)
  189.                     {
  190.                         $file = str_replace(DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $file);
  191.                         if(access($file))
  192.                             edit($file);
  193.                     }
  194.                    
  195.                     function recurse_copy($src, $dst, $fileForPanel = false)
  196.                     {
  197.                         global $COMPILE_FILE, $FOLDER_COMPILED_CODES_NAME;
  198.                         $dir = opendir($src);
  199.                         @mkdir($dst, 0777); // DO NOT PRINT "COPIED"
  200.                         while(false !== ($file = readdir($dir)))
  201.                             if(($file != '.') && ($file != '..') && ($file != $FOLDER_COMPILED_CODES_NAME))
  202.                                 if(is_dir(folder($src) . $file))
  203.                                     recurse_copy(folder($src) . $file, folder($dst) . $file, $fileForPanel);
  204.                                 else
  205.                                 {
  206.                                     if($fileForPanel)
  207.                                         if(!isFileForPanel($file))
  208.                                             continue;
  209.                                     if($file != $COMPILE_FILE)
  210.                                         if(file_exists(folder($dst) . $file))
  211.                                         {
  212.                                             if(filemtime(folder($src) . $file) > filemtime(folder($dst) . $file))
  213.                                                 copy(folder($src) . $file, folder($dst) . $file);
  214.                                         }
  215.                                         else
  216.                                             copy(folder($src) . $file, folder($dst) . $file);
  217.                                 }
  218.                         closedir($dir);
  219.                     }
  220.                     echo '<div id="styles">';
  221.                    
  222.                     if(isset($_POST['compile']))
  223.                     {
  224.                         if($_POST['compile'] == "Rebuild")
  225.                             $REBUILD = true;
  226.                         else
  227.                             $REBUILD = false;
  228.                        
  229.                         if(count($filesModified) == 0 && !$REBUILD)
  230.                         {
  231.                             echo '<p id="compiled">No modified files detected ! Compilation aborted !</p>';
  232.                             endOfPage();
  233.                         }
  234.  
  235.                         function generateRandomString($length = 10)
  236.                         {
  237.                             $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  238.                             $charactersLength = strlen($characters);
  239.                             $randomString = '';
  240.                             for($i = 0; $i < $length; $i++)
  241.                                 $randomString .= $characters[rand(0, $charactersLength - 1)];
  242.                             return $randomString;
  243.                         }
  244.                        
  245.                         $generated = str_replace("/", DIRECTORY_SEPARATOR, date("y/m/d/H/i/s/")) . generateRandomString();
  246.                         $FOLDER_BUILD .= folder($generated);
  247.                         if($SIDE == "Client")
  248.                             $FOLDER_COMPILED .= folder($generated);
  249.                         mkdir($FOLDER_BUILD, 0777, true);
  250.                         mkdir($FOLDER_COMPILED, 0777, true);
  251.                         recurse_copy($PATH_CODE, $FOLDER_BUILD, true);
  252.  
  253.                         $time = time();
  254.                         include $PATH_CODE . "compile.php";
  255.                         compile();
  256.                         echo 'Compilation done in ' . (time() - $time) . ' seconds.
  257.                             <p id="compiled">';
  258.                         exec($INSTRUCTION_MOVE . $FOLDER_COMPILED_CODES . $EXEC . " " . $FOLDER_COMPILED);
  259.                         if($error)
  260.                             echo 'Can\'t compile if there are errors.';
  261.                         else
  262.                         {
  263.                             if($SIDE == "Client")
  264.                                 echo '<a target="_blank" href="' . str_replace($FOLDER_ONLINE, "", $FOLDER_COMPILED . $EXEC) . '">Your client program is available here.</a>';
  265.                             else
  266.                                 echo 'Compilation done and new server program applied !';
  267.                         }
  268.                         echo '</p>';
  269.                     }
  270.                    
  271.                     endOfPage();
  272.                 }
  273.             ?>
  274.         </form>
  275.     </body>
  276. </html>
  277.  
  278. <?php
  279.     function setup()
  280.     {
  281.         global $time;
  282.         $time = time();
  283.         global $FILE_EXT_FOR_PANEL;
  284.         for($i = 0; $i < count($FILE_EXT_FOR_PANEL); $i++)
  285.             $FILE_EXT_FOR_PANEL[$i] = '.' . $FILE_EXT_FOR_PANEL[$i];
  286.        
  287.         function checkPw($pw)
  288.         {
  289.             global $PASSWORD_HASH_USER, $PASSWORD_HASH_ROOT, $USER_AGENT_ROOT, $COOKIE_ROOT, $COOKIE_HASH_ROOT, $HOST_ROOT, $PASSWORD_ID, $ROOT;
  290.             if(password_verify($pw, $PASSWORD_HASH_ROOT) && gethostbyaddr($_SERVER["REMOTE_ADDR"]) === $HOST_ROOT && $USER_AGENT_ROOT === $_SERVER["HTTP_USER_AGENT"])
  291.             {
  292.                 //setcookie($COOKIE_ROOT, "pw", 2147483647);
  293.                 if(isset($_COOKIE[$COOKIE_ROOT]))
  294.                     if(password_verify($_COOKIE[$COOKIE_ROOT], $COOKIE_HASH_ROOT))
  295.                         $ROOT = true;
  296.                     else
  297.                         die();
  298.                 else
  299.                     die();
  300.             }
  301.             else if(!password_verify($pw, $PASSWORD_HASH_USER))
  302.             {
  303.                 setcookie($PASSWORD_ID, " ", 2147483647);
  304.                 die("<h3>Wrong password !</h3>");
  305.             }
  306.         }
  307.        
  308.         function connectBtn($login)
  309.         {
  310.             global $PASSWORD_ID;
  311.             if($login)
  312.                 if(isset($_POST[$PASSWORD_ID]))
  313.                 {
  314.                     setcookie($PASSWORD_ID, $_POST[$PASSWORD_ID], 2147483647);
  315.                     checkPw($_POST[$PASSWORD_ID]);
  316.                 }
  317.                 else
  318.                     checkPw($_COOKIE[$PASSWORD_ID]);
  319.             else
  320.                 echo '<h3>Password: </h3><input type="password" name="' . $PASSWORD_ID . '">&emsp;<input type="submit" value="Login">';
  321.         }
  322.        
  323.         function endOfPage()
  324.         {
  325.             global $time, $SIDE, $FOLDER_COMPILED, $FOLDER_LOGS, $FOLDER_SCRIPT, $WEBSITE_FOLDER_STYLES;
  326.             echo '<br/><input type="submit" name="compile" value="Compile"><br/><br/>';
  327.             echo '<input type="image" name="compile" value="Rebuild" src="' . $WEBSITE_FOLDER_STYLES . 'rebuild.jpg">&ensp;Rebuild<br/>';
  328.            
  329.             if($SIDE == "Server")
  330.             {
  331.                 function action($action)
  332.                 {
  333.                     echo '<input type="radio" name="action" value="' . $action . '"> ' . ucfirst($action) . '<br/>';
  334.                 }
  335.                
  336.                 echo '<h4>Action to perform on the server:</h4>';
  337.                 action("restart");
  338.                 action("stop");
  339.                 action("start");
  340.                 echo '<br/><input type="submit" value="Execute"><br/>';
  341.                 if(isset($_POST['action']))
  342.                 {
  343.                     $action = $_POST['action'];
  344.                     echo '<br/>Result of the execution: <strong>'; system("sh " . $FOLDER_SCRIPT . "script.sh " . $action);
  345.                     echo '</strong><br>Action: <strong>' . $action . '</strong> performed on the server !';
  346.                 }
  347.                 echo '<br/><br/><h4>Available logs:</h3>';
  348.                 $logs = scandir($FOLDER_LOGS);
  349.                 for($i = 0; $i < count($logs); $i++)
  350.                 {
  351.                     if(endsWith($logs[$i], ".log"))
  352.                     {
  353.                         $log = str_replace(".log", "", $logs[$i]);
  354.                         echo '<a href="/?log=' . str_replace("#", "%23", $log) . '">' . $log . '</a>&nbsp;';
  355.                     }
  356.                 }
  357.                 if(isset($_GET["log"]))
  358.                 {
  359.                     $log = $_GET["log"];
  360.                     $logFile = $log . '.log';
  361.                     echo '<br/><h4>Content of ' . $logFile . ':</h4>';
  362.                     echo str_replace("\n", "<br/>", file_get_contents($FOLDER_LOGS . $logFile));
  363.                 }
  364.                
  365.                 /*
  366.                 $files = scandir("Styles/styles/");
  367.                 for($i = 0; $i < count($files); $i++)
  368.                 {
  369.                     if(endsWith($files[$i], ".css"))
  370.                     {
  371.                         $css = str_replace(".css", "", $files[$i]);
  372.                         echo '<a href="/?style=' . $css . '">' . $css . '</a>&nbsp;';
  373.                     }
  374.                 }
  375.                 */
  376.             }
  377.             echo 'Global time: ' . (time() - $time);
  378.             echo '</div>';
  379.             die();
  380.         }
  381.     }
  382.  
  383.     function isFileForPanel($ff)
  384.     {
  385.         global $FILE_EXT_FOR_PANEL;
  386.         for($i = 0; $i < count($FILE_EXT_FOR_PANEL); $i++)
  387.             if(endsWith($ff, $FILE_EXT_FOR_PANEL[$i]))
  388.                 return true;
  389.         return false;
  390.     }
  391.  
  392.     function path(array $segments)
  393.     {
  394.         return join(DIRECTORY_SEPARATOR, $segments);
  395.     }
  396.    
  397.     function folder(...$segments)
  398.     {
  399.         return path($segments) . DIRECTORY_SEPARATOR;
  400.     }
  401. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement