|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 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_BINARY ?: (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 | + |
| 37 | +/** |
| 38 | + * Returns escaped command. |
| 39 | + * |
| 40 | + * @param string $command |
| 41 | + * @return string |
| 42 | + */ |
| 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 | + |
| 56 | +/** |
| 57 | + * Checks magento list of CLI commands for given $command. Does not check command parameters, just base command. |
| 58 | + * @param string $command |
| 59 | + * @return bool |
| 60 | + */ |
| 61 | +function validateCommand($command) |
| 62 | +{ |
| 63 | + $php = PHP_BINARY ?: (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 | + |
| 70 | +/** |
| 71 | + * Returns given string trimmed of everything after the first found whitespace. |
| 72 | + * @param string $string |
| 73 | + * @return string |
| 74 | + */ |
| 75 | +function trimAfterWhitespace($string) |
| 76 | +{ |
| 77 | + return strtok($string, ' '); |
| 78 | +} |
0 commit comments