4
4
* See COPYING.txt for license details.
5
5
*/
6
6
7
- if (isset ($ _POST ['command ' ])) {
7
+ if (isset ($ _POST ['baseUrl ' ]) && isset ($ _POST ['username ' ]) && isset ($ _POST ['password ' ]) && isset ($ _POST ['command ' ])) {
8
+ $ baseUrl = urldecode ($ _POST ['baseUrl ' ]);
9
+ $ username = urldecode ($ _POST ['username ' ]);
10
+ $ password = urldecode ($ _POST ['password ' ]);
8
11
$ command = urldecode ($ _POST ['command ' ]);
9
12
if (array_key_exists ("arguments " , $ _POST )) {
10
13
$ arguments = urldecode ($ _POST ['arguments ' ]);
11
14
} else {
12
15
$ arguments = null ;
13
16
}
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 );
17
+
18
+ if (isAuthenticated ($ baseUrl , $ username , $ password )) {
19
+ $ php = PHP_BINDIR ? PHP_BINDIR . '/php ' : 'php ' ;
20
+ $ magentoBinary = $ php . ' -f ../../../../bin/magento ' ;
21
+ $ valid = validateCommand ($ magentoBinary , $ command );
22
+ if ($ valid ) {
23
+ exec (
24
+ escapeCommand ($ magentoBinary . ' ' . $ command ) . " $ arguments " ." 2>&1 " ,
25
+ $ output ,
26
+ $ exitCode
27
+ );
28
+ if ($ exitCode == 0 ) {
29
+ http_response_code (202 );
30
+ } else {
31
+ http_response_code (500 );
32
+ }
33
+ echo implode ("\n" , $ output );
24
34
} else {
25
- http_response_code (500 );
35
+ http_response_code (403 );
36
+ echo "Given command not found valid in Magento CLI Command list. " ;
26
37
}
27
- echo implode ("\n" , $ output );
28
38
} else {
29
- http_response_code (403 );
30
- echo " Given command not found valid in Magento CLI Command list. " ;
39
+ http_response_code (401 );
40
+ echo ( " Command not unauthorized. " ) ;
31
41
}
32
42
} else {
33
43
http_response_code (412 );
34
- echo ("Command parameter is not set. " );
44
+ echo ("Required parameters are not set. " );
45
+ }
46
+
47
+ /**
48
+ * Returns if credentials are successfully authenticated.
49
+ *
50
+ * @param string $baseUrl
51
+ * @param string $username
52
+ * @param string $password
53
+ * @return bool
54
+ */
55
+ function isAuthenticated ($ baseUrl , $ username , $ password )
56
+ {
57
+ $ userData = [
58
+ "username " => $ username ,
59
+ "password " => $ password
60
+ ];
61
+ $ ch = curl_init ($ baseUrl . "/index.php/rest/V1/integration/admin/token " );
62
+ curl_setopt ($ ch , CURLOPT_POST , true );
63
+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , json_encode ($ userData ));
64
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
65
+ curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , true );
66
+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , false );
67
+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , false );
68
+ curl_setopt ($ ch , CURLOPT_COOKIEFILE , '' );
69
+ curl_setopt (
70
+ $ ch ,
71
+ CURLOPT_HTTPHEADER ,
72
+ array ("Content-Type: application/json " , "Content-Lenght: " . strlen (json_encode ($ userData )))
73
+ );
74
+
75
+ $ token = curl_exec ($ ch );
76
+
77
+ if (!empty ($ token ) && curl_getinfo ($ ch , CURLINFO_HTTP_CODE ) === 200 ) {
78
+ curl_close ($ ch );
79
+ return true ;
80
+ } else {
81
+ echo "Authentication error. " ;
82
+ curl_close ($ ch );
83
+ return false ;
84
+ }
35
85
}
36
86
37
87
/**
@@ -55,13 +105,13 @@ function escapeCommand($command)
55
105
56
106
/**
57
107
* Checks magento list of CLI commands for given $command. Does not check command parameters, just base command.
108
+ * @param string $magentoBinary
58
109
* @param string $command
59
110
* @return bool
60
111
*/
61
- function validateCommand ($ command )
112
+ function validateCommand ($ magentoBinary , $ command )
62
113
{
63
- $ php = PHP_BINDIR ? PHP_BINDIR . '/php ' : 'php ' ;
64
- exec ($ php . ' -f ../../../../bin/magento list ' , $ commandList );
114
+ exec ($ magentoBinary . ' list ' , $ commandList );
65
115
// Trim list of commands after first whitespace
66
116
$ commandList = array_map ("trimAfterWhitespace " , $ commandList );
67
117
return in_array (trimAfterWhitespace ($ command ), $ commandList );
0 commit comments