Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
command.php
Go to the documentation of this file.
1 <?php
7 if (isset($_POST['command'])) {
8  $command = urldecode($_POST['command']);
9  if (array_key_exists("arguments", $_POST)) {
10  $arguments = urldecode($_POST['arguments']);
11  } else {
12  $arguments = null;
13  }
14  $php = PHP_BINDIR ? PHP_BINDIR . '/php' : 'php';
15  $valid = validateCommand($command);
16  if ($valid) {
17  exec(
18  escapeCommand($php . ' -f ../../../../bin/magento ' . $command) . " $arguments" ." 2>&1",
19  $output,
20  $exitCode
21  );
22  if ($exitCode == 0) {
23  http_response_code(202);
24  } else {
25  http_response_code(500);
26  }
27  echo implode("\n", $output);
28  } else {
29  http_response_code(403);
30  echo "Given command not found valid in Magento CLI Command list.";
31  }
32 } else {
33  http_response_code(412);
34  echo("Command parameter is not set.");
35 }
36 
43 function escapeCommand($command)
44 {
45  $escapeExceptions = [
46  '> /dev/null &' => '--dev-null-amp--'
47  ];
48 
49  $command = escapeshellcmd(
50  str_replace(array_keys($escapeExceptions), array_values($escapeExceptions), $command)
51  );
52 
53  return str_replace(array_values($escapeExceptions), array_keys($escapeExceptions), $command);
54 }
55 
61 function validateCommand($command)
62 {
63  $php = PHP_BINDIR ? PHP_BINDIR . '/php' : 'php';
64  exec($php . ' -f ../../../../bin/magento list', $commandList);
65  // Trim list of commands after first whitespace
66  $commandList = array_map("trimAfterWhitespace", $commandList);
67  return in_array(trimAfterWhitespace($command), $commandList);
68 }
69 
75 function trimAfterWhitespace($string)
76 {
77  return strtok($string, ' ');
78 }
escapeCommand($command)
Definition: command.php:43
exec($command, array &$output=null, &$return_var=null)
$arguments
validateCommand($command)
Definition: command.php:61
trimAfterWhitespace($string)
Definition: command.php:75