1
1
<?php
2
- include ( dirname (__FILE__ ). "/headers.php " ) ;
3
- include ( dirname (__FILE__ ). "/settings.php " ) ;
2
+ include dirname (__FILE__ ) . "/headers.php " ;
3
+ include dirname (__FILE__ ) . "/settings.php " ;
4
4
5
+ // Set some common aliases
6
+ $ aliases = array (
7
+ 'la ' => 'ls -la ' ,
8
+ 'll ' => 'ls -lvhF ' ,
9
+ );
5
10
6
- function proc_open_enabled () {
7
- $ disabled = explode ( ' , ' , ini_get ( ' disable_functions ' ));
8
- return ! in_array ( ' proc_open ' , $ disabled );
11
+ // If we have a current working dir in session, change to that dir
12
+ if ( true === isset ( $ _SESSION [ ' cwd ' ])) {
13
+ chdir ( $ _SESSION [ ' cwd ' ] );
9
14
}
10
15
11
- if (! proc_open_enabled ()) {
12
- exit ( " <span style= \" color: #fff \" >Sorry but you can't use this terminal if your proc_open is disabled</span> \n\n" );
13
- }
16
+ // Get current user and cwd
17
+ $ user = str_replace ( "\n" , "" , shell_exec ( " whoami " ) );
18
+ $ cwd = str_replace ( "\n" , "" , shell_exec ( " pwd " ));
14
19
15
- $ aliases = array (
16
- 'la ' => 'ls -la ' ,
17
- 'll ' => 'ls -lvhF ' ,
18
- );
20
+ // Check if we have proc_open_enabled
21
+ // (Used later to handle commands)
22
+ function proc_open_enabled () {
23
+ $ disabled = explode (', ' , ini_get ('disable_functions ' ));
24
+ return false === in_array ('proc_open ' , $ disabled );
25
+ }
19
26
20
- // Get current working dir
21
- $ user = str_replace ("\n" ,"" ,shell_exec ("whoami " ));
22
- $ cwd = getcwd ();
27
+ // Return HTML prompt plus the command the user provided last
28
+ function returnHTMLPromptCommand ($ cmd ) {
29
+ global $ user , $ cwd ;
30
+ // Begin output with prompt and user command
31
+ return '<div class="commandLine"><div class="user"> ' . $ user . ' </div> ' .
32
+ '<div class="cwd"> ' . $ cwd . ' </div> : ' . date ("H:m:s " ) .
33
+ '<br> ' .
34
+ '<div class="promptVLine"></div><div class="promptHLine">─<div class="promptArrow">▶</div></div> ' . $ cmd . '</div></div><br> ' ;
35
+ }
23
36
24
- // If we have a command
25
- if (!empty ($ _REQUEST ['command ' ])) {
26
- // Strip any slashes from it
27
- if (get_magic_quotes_gpc ()) {
28
- $ _REQUEST ['command ' ] = stripslashes ($ _REQUEST ['command ' ]);
29
- }
37
+ // If proc_open isn't enabled, display prompt, command and a message re needing this enabled
38
+ if (false === proc_open_enabled ()) {
39
+ echo json_encode ([
40
+ "output " => returnHTMLPromptCommand ($ _REQUEST ['command ' ] . "<br><br>Sorry but you can't use this terminal if your proc_open is disabled " ),
41
+ "user " => $ user ,
42
+ "cwd " => $ cwd
43
+ ]);
44
+ exit ;
45
+ }
30
46
31
- // Begin output with prompt and user command
32
- $ output = '<div class="commandLine"><div class="user"> ' .$ user .' </div> ' .
33
- '<div class="path"> ' .$ cwd .' </div> : ' .date ("H:m:s " ).
34
- '<br> ' .
35
- '<div class="promptVLine"></div><div class="promptHLine">─<div class="promptArrow">▶</div></div> ' .$ _REQUEST ['command ' ].'</div><br><br> ' ;
47
+ // If in demo mode, display message and go no further
48
+ if (true === $ demoMode ) {
49
+ echo json_encode ([
50
+ "output " => returnHTMLPromptCommand ($ _REQUEST ['command ' ] . "<br><br>Sorry, shell usage not enabled in demo mode " ),
51
+ "user " => $ user ,
52
+ "cwd " => $ cwd
53
+ ]);
54
+ exit ;
36
55
}
37
56
38
57
// If in demo mode, display message and go no further
39
- if ($ demoMode ) {
40
- $ output .= "Sorry, shell usage not enabled in demo mode \n\n" ;
41
- echo $ output ;
42
- exit ;
58
+ if (false === isset ($ _REQUEST ['command ' ])) {
59
+ echo json_encode ([
60
+ "output " => returnHTMLPromptCommand ($ _REQUEST ['command ' ] . "<br><br>Sorry, no command received " ),
61
+ "user " => $ user ,
62
+ "cwd " => $ cwd
63
+ ]);
64
+ exit ;
43
65
}
44
66
67
+ // Strip any slashes from command
68
+ $ _REQUEST ['command ' ] = stripslashes ($ _REQUEST ['command ' ]);
69
+
70
+ // Start output with the prompt and command they provided last
71
+ $ output = returnHTMLPromptCommand ($ _REQUEST ['command ' ]);
72
+
45
73
// If command contains cd but no dir
46
- if (preg_match ('/^[[:blank:]]*cd[[:blank:]]*$/ ' , @$ _REQUEST ['command ' ])) {
47
- $ _SESSION ['cwd ' ] = getcwd (); //dirname(__FILE__);
74
+ if (preg_match ('/^[[:blank:]]*cd[[:blank:]]*$/ ' , $ _REQUEST ['command ' ])) {
75
+ $ _SESSION ['cwd ' ] = $ cwd ;
76
+ $ output .= returnHTMLPromptCommand ("cd " );
48
77
// Else cd to a dir
49
- } elseif (preg_match ('/^[[:blank:]]*cd[[:blank:]]+([^;]+)$/ ' , @ $ _REQUEST ['command ' ], $ regs )) {
78
+ } elseif (preg_match ('/^[[:blank:]]*cd[[:blank:]]+([^;]+)$/ ' , $ _REQUEST ['command ' ], $ regs )) {
50
79
// The current command is 'cd', which we have to handle as an internal shell command
51
- // Absolute/relative path ?
52
- ($ regs [1 ][0 ] == '/ ' ) ? $ newDir = $ regs [1 ] : $ newDir = $ _SESSION ['cwd ' ].'/ ' .$ regs [1 ];
80
+ $ newDir = "/ " === $ regs [1 ][0 ] ? $ regs [1 ] : $ _SESSION ['cwd ' ] . "/ " . $ regs [1 ];
53
81
54
82
// Tidy up appearance on /./
55
- while (strpos ($ newDir , '/./ ' ) !== false ) {
83
+ while (false !== strpos ($ newDir , '/./ ' )) {
56
84
$ newDir = str_replace ('/./ ' , '/ ' , $ newDir );
57
85
}
58
86
// Tidy up appearance on //
59
- while (strpos ($ newDir , '// ' ) !== false ) {
87
+ while (false !== strpos ($ newDir , '// ' )) {
60
88
$ newDir = str_replace ('// ' , '/ ' , $ newDir );
61
89
}
62
90
// Tidy up appearance on other variations
63
- while (preg_match ('|/\ .\.(?!\.)| ' , $ newDir )) {
64
- $ newDir = preg_replace ('|/ ?[^/]+/\.\.(?!\.)| ' , '' , $ newDir );
91
+ while (preg_match ('/\/\ .\.(?!\.)/ ' , $ newDir )) {
92
+ $ newDir = preg_replace ('/\/ ?[^\ /]+\ /\.\.(?!\.)/ ' , '' , $ newDir );
65
93
}
66
94
67
95
// Empty dir
@@ -70,36 +98,35 @@ function proc_open_enabled() {
70
98
}
71
99
72
100
// Test if we could change to that dir, else display error
73
- (@chdir ($ newDir )) ? $ _SESSION ['cwd ' ] = $ newDir : $ output .= "\n\n Could not change to: $ newDir \n\n" ;
101
+ (@chdir ($ newDir )) ? $ _SESSION ['cwd ' ] = $ newDir : $ output .= "Could not change to: $ newDir \n\n" ;
74
102
} else {
75
- // The command is not a 'cd' command, so we execute it after
76
- // changing the directory and save the output.
77
- chdir ($ _SESSION ['cwd ' ]);
103
+ // The command is not a 'cd' command
78
104
79
105
// Alias expansion
80
- $ length = strcspn (@ $ _REQUEST ['command ' ], " \t" );
81
- $ token = substr (@ $ _REQUEST ['command ' ], 0 , $ length );
82
- if (isset ($ aliases [$ token ])) {
83
- $ _REQUEST ['command ' ] = $ aliases [$ token ]. substr ($ _REQUEST ['command ' ], $ length );
106
+ $ length = strcspn ($ _REQUEST ['command ' ], " \t" );
107
+ $ token = substr ($ _REQUEST ['command ' ], 0 , $ length );
108
+ if (true === isset ($ aliases [$ token ])) {
109
+ $ _REQUEST ['command ' ] = $ aliases [$ token ] . substr ($ _REQUEST ['command ' ], $ length );
84
110
}
85
111
86
112
// Open a proc with array and $io return
87
113
$ p = proc_open (
88
- @ $ _REQUEST ['command ' ],
114
+ $ _REQUEST ['command ' ],
89
115
array (
90
116
1 => array ('pipe ' , 'w ' ),
91
117
2 => array ('pipe ' , 'w ' )
92
118
),
93
119
$ io
94
120
);
95
-
121
+
96
122
// Read output sent to stdout
97
- while (!feof ($ io [1 ])) { /// this will return always false ... and will loop forever until "fork: retry: no child processes" will show if proc_open is disabled;
98
- $ output .= htmlspecialchars (fgets ($ io [1 ]),ENT_COMPAT , 'UTF-8 ' );
123
+ while (false === feof ($ io [1 ])) {
124
+ // this will return always false ... and will loop forever until "fork: retry: no child processes" will show if proc_open is disabled;
125
+ $ output .= htmlspecialchars (fgets ($ io [1 ]), ENT_COMPAT , 'UTF-8 ' );
99
126
}
100
127
// Read output sent to stderr
101
- while (! feof ($ io [2 ])) {
102
- $ output .= htmlspecialchars (fgets ($ io [2 ]),ENT_COMPAT , 'UTF-8 ' );
128
+ while (false === feof ($ io [2 ])) {
129
+ $ output .= htmlspecialchars (fgets ($ io [2 ]), ENT_COMPAT , 'UTF-8 ' );
103
130
}
104
131
$ output .= "\n" ;
105
132
@@ -109,6 +136,16 @@ function proc_open_enabled() {
109
136
proc_close ($ p );
110
137
}
111
138
112
- // Finally, output our string
113
- echo $ output ;
139
+ // Change to the cwd in session
140
+ chdir ($ _SESSION ['cwd ' ]);
141
+ // and again ask for current user and working dir
142
+ $ user = str_replace ("\n" ,"" ,shell_exec ("whoami " ));
143
+ $ cwd = str_replace ("\n" ,"" ,shell_exec ("pwd " ));
144
+
145
+ // Finally, output our JSON data
146
+ echo json_encode ([
147
+ "output " => $ output ,
148
+ "user " => $ user ,
149
+ "cwd " => $ cwd
150
+ ]);
114
151
0 commit comments