44 * See COPYING.txt for license details.
55 */
66
7- if (isset ($ _POST ['command ' ])) {
7+ require_once __DIR__ . '/../../../../app/bootstrap.php ' ;
8+
9+ if (!empty ($ _POST ['token ' ]) && !empty ($ _POST ['command ' ])) {
10+ $ magentoObjectManagerFactory = \Magento \Framework \App \Bootstrap::createObjectManagerFactory (BP , $ _SERVER );
11+ $ magentoObjectManager = $ magentoObjectManagerFactory ->create ($ _SERVER );
12+ $ tokenModel = $ magentoObjectManager ->get (\Magento \Integration \Model \Oauth \Token::class);
13+
14+ $ tokenPassedIn = urldecode ($ _POST ['token ' ]);
815 $ command = urldecode ($ _POST ['command ' ]);
9- if (array_key_exists ("arguments " , $ _POST )) {
16+
17+ if (!empty ($ _POST ['arguments ' ])) {
1018 $ arguments = urldecode ($ _POST ['arguments ' ]);
1119 } else {
1220 $ arguments = null ;
1321 }
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 );
22+
23+ // Token returned will be null if the token we passed in is invalid
24+ $ tokenFromMagento = $ tokenModel ->loadByToken ($ tokenPassedIn )->getToken ();
25+ if (!empty ($ tokenFromMagento ) && ($ tokenFromMagento == $ tokenPassedIn )) {
26+ $ php = PHP_BINDIR ? PHP_BINDIR . '/php ' : 'php ' ;
27+ $ magentoBinary = $ php . ' -f ../../../../bin/magento ' ;
28+ $ valid = validateCommand ($ magentoBinary , $ command );
29+ if ($ valid ) {
30+ exec (
31+ escapeCommand ($ magentoBinary . " $ command " . " $ arguments " ) . " 2>&1 " ,
32+ $ output ,
33+ $ exitCode
34+ );
35+ if ($ exitCode == 0 ) {
36+ http_response_code (202 );
37+ } else {
38+ http_response_code (500 );
39+ }
40+ echo implode ("\n" , $ output );
2441 } else {
25- http_response_code (500 );
42+ http_response_code (403 );
43+ echo "Given command not found valid in Magento CLI Command list. " ;
2644 }
27- echo implode ("\n" , $ output );
2845 } else {
29- http_response_code (403 );
30- echo " Given command not found valid in Magento CLI Command list. " ;
46+ http_response_code (401 );
47+ echo ( " Command not unauthorized. " ) ;
3148 }
3249} else {
3350 http_response_code (412 );
34- echo ("Command parameter is not set. " );
51+ echo ("Required parameters are not set. " );
3552}
3653
3754/**
@@ -55,13 +72,13 @@ function escapeCommand($command)
5572
5673/**
5774 * Checks magento list of CLI commands for given $command. Does not check command parameters, just base command.
75+ * @param string $magentoBinary
5876 * @param string $command
5977 * @return bool
6078 */
61- function validateCommand ($ command )
79+ function validateCommand ($ magentoBinary , $ command )
6280{
63- $ php = PHP_BINDIR ? PHP_BINDIR . '/php ' : 'php ' ;
64- exec ($ php . ' -f ../../../../bin/magento list ' , $ commandList );
81+ exec ($ magentoBinary . ' list ' , $ commandList );
6582 // Trim list of commands after first whitespace
6683 $ commandList = array_map ("trimAfterWhitespace " , $ commandList );
6784 return in_array (trimAfterWhitespace ($ command ), $ commandList );
0 commit comments