index.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. // Your Token
  3. $toCheckToken = 'your_to_verify_token';
  4. // Your valid ip address
  5. $validIP = array('123.45.67.89');
  6. // Uncomment to set log
  7. //$logFile = '/tmp/deploy.log';
  8. $remoteIP = $_SERVER['REMOTE_ADDR'];
  9. $time = date("Y-m-d H:i:s");
  10. if ($logFile) {
  11. $log = true;
  12. $fs = fopen($logFile, 'a');
  13. fwrite($fs, "[start deploy] time:{$time}".PHP_EOL);
  14. }
  15. # Check IP
  16. if (!in_array($remoteIP, $validIP)) {
  17. $msg = "Invalid IP:".$remoteIP;
  18. echo $msg;
  19. $log && fwrite($fs, $msg.PHP_EOL);
  20. exit(0);
  21. }
  22. # Decode data
  23. $json = file_get_contents("php://input");
  24. $data = json_decode($json);
  25. if (!$data || empty($data)) {
  26. $data = (object) $_POST;
  27. }
  28. # Check token
  29. if ($data->token != $toCheckToken) {
  30. $msg = "Invalid token:".$data->token;
  31. echo $msg;
  32. $log && fwrite($fs, $msg.PHP_EOL);
  33. exit(0);
  34. }
  35. # Check function
  36. if (!function_exists('exec')) {
  37. echo "exec funciton is not avaiable!";
  38. exit(0);
  39. }
  40. # Exec update script
  41. exec("sh auto_deploy.sh",$output);
  42. foreach ($output as $val) {
  43. echo $val."\n";
  44. }
  45. # log
  46. $msg = "Finish auto deploy at ".$time;
  47. $log && fwrite($fs, $msg.PHP_EOL);
  48. $fs && fclose($fs);