command.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. require_once __DIR__ . '/../../../../app/bootstrap.php';
  7. if (!empty($_POST['token']) && !empty($_POST['command'])) {
  8. $magentoObjectManagerFactory = \Magento\Framework\App\Bootstrap::createObjectManagerFactory(BP, $_SERVER);
  9. $magentoObjectManager = $magentoObjectManagerFactory->create($_SERVER);
  10. $tokenModel = $magentoObjectManager->get(\Magento\Integration\Model\Oauth\Token::class);
  11. $tokenPassedIn = urldecode($_POST['token']);
  12. $command = urldecode($_POST['command']);
  13. if (!empty($_POST['arguments'])) {
  14. $arguments = urldecode($_POST['arguments']);
  15. } else {
  16. $arguments = null;
  17. }
  18. // Token returned will be null if the token we passed in is invalid
  19. $tokenFromMagento = $tokenModel->loadByToken($tokenPassedIn)->getToken();
  20. if (!empty($tokenFromMagento) && ($tokenFromMagento == $tokenPassedIn)) {
  21. $php = PHP_BINDIR ? PHP_BINDIR . '/php' : 'php';
  22. $magentoBinary = $php . ' -f ../../../../bin/magento';
  23. $valid = validateCommand($magentoBinary, $command);
  24. if ($valid) {
  25. $process = new Symfony\Component\Process\Process($magentoBinary . " $command" . " $arguments");
  26. $process->setIdleTimeout(60);
  27. $process->setTimeout(0);
  28. $idleTimeout = false;
  29. try {
  30. $process->run();
  31. $output = $process->getOutput();
  32. if (!$process->isSuccessful()) {
  33. $output = $process->getErrorOutput();
  34. }
  35. if (empty($output)) {
  36. $output = "CLI did not return output.";
  37. }
  38. } catch (Symfony\Component\Process\Exception\ProcessTimedOutException $exception) {
  39. $output = "CLI command timed out, no output available.";
  40. $idleTimeout = true;
  41. }
  42. $exitCode = $process->getExitCode();
  43. if ($exitCode == 0 || $idleTimeout) {
  44. http_response_code(202);
  45. } else {
  46. http_response_code(500);
  47. }
  48. echo $output;
  49. } else {
  50. http_response_code(403);
  51. echo "Given command not found valid in Magento CLI Command list.";
  52. }
  53. } else {
  54. http_response_code(401);
  55. echo("Command not unauthorized.");
  56. }
  57. } else {
  58. http_response_code(412);
  59. echo("Required parameters are not set.");
  60. }
  61. /**
  62. * Returns escaped command.
  63. *
  64. * @param string $command
  65. * @return string
  66. */
  67. function escapeCommand($command)
  68. {
  69. $escapeExceptions = [
  70. '> /dev/null &' => '--dev-null-amp--'
  71. ];
  72. $command = escapeshellcmd(
  73. str_replace(array_keys($escapeExceptions), array_values($escapeExceptions), $command)
  74. );
  75. return str_replace(array_values($escapeExceptions), array_keys($escapeExceptions), $command);
  76. }
  77. /**
  78. * Checks magento list of CLI commands for given $command. Does not check command parameters, just base command.
  79. * @param string $magentoBinary
  80. * @param string $command
  81. * @return bool
  82. */
  83. function validateCommand($magentoBinary, $command)
  84. {
  85. exec($magentoBinary . ' list', $commandList);
  86. // Trim list of commands after first whitespace
  87. $commandList = array_map("trimAfterWhitespace", $commandList);
  88. return in_array(trimAfterWhitespace($command), $commandList);
  89. }
  90. /**
  91. * Returns given string trimmed of everything after the first found whitespace.
  92. * @param string $string
  93. * @return string
  94. */
  95. function trimAfterWhitespace($string)
  96. {
  97. return strtok($string, ' ');
  98. }