1
1
<?php
2
2
// Initializing
3
+ if (!isset ($ NO_LOGIN )) $ NO_LOGIN = false ;
3
4
if (!isset ($ ACCOUNTS )) $ ACCOUNTS = array ();
4
5
if (isset ($ USER ) && isset ($ PASSWORD ) && $ USER && $ PASSWORD ) $ ACCOUNTS [$ USER ] = $ PASSWORD ;
5
6
if (!isset ($ PASSWORD_HASH_ALGORITHM )) $ PASSWORD_HASH_ALGORITHM = '' ;
6
- if (!isset ($ NO_LOGIN )) $ NO_LOGIN = false ;
7
7
if (!isset ($ HOME_DIRECTORY )) $ HOME_DIRECTORY = '' ;
8
8
$ IS_CONFIGURED = ($ NO_LOGIN || count ($ ACCOUNTS ) >= 1 ) ? true : false ;
9
9
10
+ // Utilities
11
+ function is_empty_string ($ string ) {
12
+ return strlen ($ string ) <= 0 ;
13
+ }
14
+
15
+ function is_equal_strings ($ string1 , $ string2 ) {
16
+ return strcmp ($ string1 , $ string2 ) == 0 ;
17
+ }
18
+
19
+ function get_hash ($ algorithm , $ string ) {
20
+ return hash ($ algorithm , trim ((string ) $ string ));
21
+ }
22
+
10
23
// Command execution
11
24
function execute_command ($ command ) {
12
25
$ descriptors = array (
@@ -37,7 +50,7 @@ function execute_command($command) {
37
50
function parse_command ($ command ) {
38
51
$ value = ltrim ((string ) $ command );
39
52
40
- if ($ value && ! empty ($ value )) {
53
+ if (! is_empty_string ($ value )) {
41
54
$ values = explode (' ' , $ value );
42
55
$ values_total = count ($ values );
43
56
@@ -47,8 +60,7 @@ function parse_command($command) {
47
60
for ($ index = $ values_total - 2 ; $ index >= 0 ; $ index --) {
48
61
$ value_item = $ values [$ index ];
49
62
50
- if (substr ($ value_item , -1 ) == '\\' )
51
- $ value = $ value_item . ' ' . $ value ;
63
+ if (substr ($ value_item , -1 ) == '\\' ) $ value = $ value_item . ' ' . $ value ;
52
64
else break ;
53
65
}
54
66
}
@@ -66,23 +78,18 @@ private function error($message) {
66
78
}
67
79
68
80
// Authentication
69
- private function password_hash ($ algorithm , $ password ) {
70
- return hash ($ algorithm , trim ((string ) $ password ));
71
- }
72
-
73
81
private function authenticate_user ($ user , $ password ) {
74
82
$ user = trim ((string ) $ user );
75
83
$ password = trim ((string ) $ password );
76
84
77
85
if ($ user && $ password ) {
78
86
global $ ACCOUNTS , $ PASSWORD_HASH_ALGORITHM ;
79
87
80
- if (!empty ($ ACCOUNTS [$ user ])) {
81
- if ($ PASSWORD_HASH_ALGORITHM )
82
- $ password = $ this ->password_hash ($ PASSWORD_HASH_ALGORITHM , $ password );
88
+ if (isset ($ ACCOUNTS [$ user ]) && !is_empty_string ($ ACCOUNTS [$ user ])) {
89
+ if ($ PASSWORD_HASH_ALGORITHM ) $ password = get_hash ($ PASSWORD_HASH_ALGORITHM , $ password );
83
90
84
- if (strcmp ($ password , $ ACCOUNTS [$ user ]) == 0 )
85
- return $ user . ': ' . $ this -> password_hash ('sha256 ' , $ password );
91
+ if (is_equal_strings ($ password , $ ACCOUNTS [$ user ]))
92
+ return $ user . ': ' . get_hash ('sha256 ' , $ password );
86
93
}
87
94
}
88
95
@@ -103,28 +110,26 @@ private function authenticate_token($token) {
103
110
if ($ user && $ password_hash ) {
104
111
global $ ACCOUNTS ;
105
112
106
- if (!empty ($ ACCOUNTS [$ user ])) {
107
- $ real_password_hash = $ this ->password_hash ('sha256 ' , $ ACCOUNTS [$ user ]);
108
-
109
- if (strcmp ($ password_hash , $ real_password_hash ) == 0 )
110
- return $ user ;
113
+ if (isset ($ ACCOUNTS [$ user ]) && !is_empty_string ($ ACCOUNTS [$ user ])) {
114
+ $ real_password_hash = get_hash ('sha256 ' , $ ACCOUNTS [$ user ]);
115
+ if (is_equal_strings ($ password_hash , $ real_password_hash )) return $ user ;
111
116
}
112
117
}
113
118
}
114
119
115
120
throw new Exception ("Incorrect user or password " );
116
121
}
117
122
118
- private function get_home_directory ($ user, $ default ) {
123
+ private function get_home_directory ($ user ) {
119
124
global $ HOME_DIRECTORY ;
120
125
121
- if (!empty ($ HOME_DIRECTORY )) {
122
- if (is_string ($ HOME_DIRECTORY )) return $ HOME_DIRECTORY ;
123
- else if (!empty ($ user ) && is_string ($ user ) && !empty ($ HOME_DIRECTORY [$ user ]))
124
- return $ HOME_DIRECTORY [$ user ];
126
+ if (is_string ($ HOME_DIRECTORY )) {
127
+ if (!is_empty_string ($ HOME_DIRECTORY )) return $ HOME_DIRECTORY ;
125
128
}
129
+ else if (is_string ($ user ) && !is_empty_string ($ user ) && isset ($ HOME_DIRECTORY [$ user ]) && !is_empty_string ($ HOME_DIRECTORY [$ user ]))
130
+ return $ HOME_DIRECTORY [$ user ];
126
131
127
- return $ default ? getcwd () : '' ;
132
+ return getcwd ();
128
133
}
129
134
130
135
// Environment
@@ -135,9 +140,9 @@ private function get_environment() {
135
140
136
141
private function set_environment ($ environment ) {
137
142
$ environment = !empty ($ environment ) ? (array ) $ environment : array ();
138
- $ path = ! empty ($ environment ['path ' ]) ? $ environment ['path ' ] : $ this ->home_directory ;
143
+ $ path = ( isset ( $ environment [ ' path ' ]) && ! is_empty_string ($ environment ['path ' ]) ) ? $ environment ['path ' ] : $ this ->home_directory ;
139
144
140
- if (!empty ($ path )) {
145
+ if (!is_empty_string ($ path )) {
141
146
if (is_dir ($ path )) {
142
147
if (!@chdir ($ path )) return array ('output ' => "Unable to change directory to current working directory, updating current directory " ,
143
148
'environment ' => $ this ->get_environment ());
@@ -150,7 +155,7 @@ private function set_environment($environment) {
150
155
// Initialization
151
156
private function initialize ($ token , $ environment ) {
152
157
$ user = $ this ->authenticate_token ($ token );
153
- $ this ->home_directory = $ this ->get_home_directory ($ user, true );
158
+ $ this ->home_directory = $ this ->get_home_directory ($ user );
154
159
$ result = $ this ->set_environment ($ environment );
155
160
156
161
if ($ result ) return $ result ;
@@ -161,8 +166,8 @@ public function login($user, $password) {
161
166
$ result = array ('token ' => $ this ->authenticate_user ($ user , $ password ),
162
167
'environment ' => $ this ->get_environment ());
163
168
164
- $ home_directory = $ this ->get_home_directory ($ user, false );
165
- if (!empty ($ home_directory )) {
169
+ $ home_directory = $ this ->get_home_directory ($ user );
170
+ if (!is_empty_string ($ home_directory )) {
166
171
if (is_dir ($ home_directory )) $ result ['environment ' ]['path ' ] = $ home_directory ;
167
172
else $ result ['output ' ] = "Home directory not found: " . $ home_directory ;
168
173
}
@@ -175,9 +180,9 @@ public function cd($token, $environment, $path) {
175
180
if ($ result ) return $ result ;
176
181
177
182
$ path = trim ((string ) $ path );
178
- if (! isset ( $ path ) || ! strlen ($ path )) $ path = $ this ->home_directory ;
183
+ if (is_empty_string ($ path )) $ path = $ this ->home_directory ;
179
184
180
- if (isset ( $ path ) && strlen ($ path )) {
185
+ if (! is_empty_string ($ path )) {
181
186
if (is_dir ($ path )) {
182
187
if (!@chdir ($ path )) return array ('output ' => "cd: " . $ path . ": Unable to change directory " );
183
188
}
@@ -240,7 +245,7 @@ public function run($token, $environment, $command) {
240
245
$ result = $ this ->initialize ($ token , $ environment );
241
246
if ($ result ) return $ result ;
242
247
243
- $ output = ($ command && !empty ($ command )) ? execute_command ($ command ) : '' ;
248
+ $ output = ($ command && !is_empty_string ($ command )) ? execute_command ($ command ) : '' ;
244
249
if ($ output && substr ($ output , -1 ) == "\n" ) $ output = substr ($ output , 0 , -1 );
245
250
246
251
return array ('output ' => $ output );
0 commit comments