Skip to content

Commit 5f80bbf

Browse files
committed
"cd" fix
1 parent 72cbe11 commit 5f80bbf

File tree

2 files changed

+38
-33
lines changed

2 files changed

+38
-33
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "WebConsole",
3-
"version": "0.9.6",
3+
"version": "0.9.7",
44
"devDependencies": {
55
"grunt": "~0.4.2",
66
"grunt-contrib-concat": "~0.3.0",

src/webconsole.main.php

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
<?php
22
// Initializing
3+
if (!isset($NO_LOGIN)) $NO_LOGIN = false;
34
if (!isset($ACCOUNTS)) $ACCOUNTS = array();
45
if (isset($USER) && isset($PASSWORD) && $USER && $PASSWORD) $ACCOUNTS[$USER] = $PASSWORD;
56
if (!isset($PASSWORD_HASH_ALGORITHM)) $PASSWORD_HASH_ALGORITHM = '';
6-
if (!isset($NO_LOGIN)) $NO_LOGIN = false;
77
if (!isset($HOME_DIRECTORY)) $HOME_DIRECTORY = '';
88
$IS_CONFIGURED = ($NO_LOGIN || count($ACCOUNTS) >= 1) ? true : false;
99

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+
1023
// Command execution
1124
function execute_command($command) {
1225
$descriptors = array(
@@ -37,7 +50,7 @@ function execute_command($command) {
3750
function parse_command($command) {
3851
$value = ltrim((string) $command);
3952

40-
if ($value && !empty($value)) {
53+
if (!is_empty_string($value)) {
4154
$values = explode(' ', $value);
4255
$values_total = count($values);
4356

@@ -47,8 +60,7 @@ function parse_command($command) {
4760
for ($index = $values_total - 2; $index >= 0; $index--) {
4861
$value_item = $values[$index];
4962

50-
if (substr($value_item, -1) == '\\')
51-
$value = $value_item . ' ' . $value;
63+
if (substr($value_item, -1) == '\\') $value = $value_item . ' ' . $value;
5264
else break;
5365
}
5466
}
@@ -66,23 +78,18 @@ private function error($message) {
6678
}
6779

6880
// Authentication
69-
private function password_hash($algorithm, $password) {
70-
return hash($algorithm, trim((string) $password));
71-
}
72-
7381
private function authenticate_user($user, $password) {
7482
$user = trim((string) $user);
7583
$password = trim((string) $password);
7684

7785
if ($user && $password) {
7886
global $ACCOUNTS, $PASSWORD_HASH_ALGORITHM;
7987

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);
8390

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);
8693
}
8794
}
8895

@@ -103,28 +110,26 @@ private function authenticate_token($token) {
103110
if ($user && $password_hash) {
104111
global $ACCOUNTS;
105112

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;
111116
}
112117
}
113118
}
114119

115120
throw new Exception("Incorrect user or password");
116121
}
117122

118-
private function get_home_directory($user, $default) {
123+
private function get_home_directory($user) {
119124
global $HOME_DIRECTORY;
120125

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;
125128
}
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];
126131

127-
return $default ? getcwd() : '';
132+
return getcwd();
128133
}
129134

130135
// Environment
@@ -135,9 +140,9 @@ private function get_environment() {
135140

136141
private function set_environment($environment) {
137142
$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;
139144

140-
if (!empty($path)) {
145+
if (!is_empty_string($path)) {
141146
if (is_dir($path)) {
142147
if (!@chdir($path)) return array('output' => "Unable to change directory to current working directory, updating current directory",
143148
'environment' => $this->get_environment());
@@ -150,7 +155,7 @@ private function set_environment($environment) {
150155
// Initialization
151156
private function initialize($token, $environment) {
152157
$user = $this->authenticate_token($token);
153-
$this->home_directory = $this->get_home_directory($user, true);
158+
$this->home_directory = $this->get_home_directory($user);
154159
$result = $this->set_environment($environment);
155160

156161
if ($result) return $result;
@@ -161,8 +166,8 @@ public function login($user, $password) {
161166
$result = array('token' => $this->authenticate_user($user, $password),
162167
'environment' => $this->get_environment());
163168

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)) {
166171
if (is_dir($home_directory)) $result['environment']['path'] = $home_directory;
167172
else $result['output'] = "Home directory not found: ". $home_directory;
168173
}
@@ -175,9 +180,9 @@ public function cd($token, $environment, $path) {
175180
if ($result) return $result;
176181

177182
$path = trim((string) $path);
178-
if (!isset($path) || !strlen($path)) $path = $this->home_directory;
183+
if (is_empty_string($path)) $path = $this->home_directory;
179184

180-
if (isset($path) && strlen($path)) {
185+
if (!is_empty_string($path)) {
181186
if (is_dir($path)) {
182187
if (!@chdir($path)) return array('output' => "cd: ". $path . ": Unable to change directory");
183188
}
@@ -240,7 +245,7 @@ public function run($token, $environment, $command) {
240245
$result = $this->initialize($token, $environment);
241246
if ($result) return $result;
242247

243-
$output = ($command && !empty($command)) ? execute_command($command) : '';
248+
$output = ($command && !is_empty_string($command)) ? execute_command($command) : '';
244249
if ($output && substr($output, -1) == "\n") $output = substr($output, 0, -1);
245250

246251
return array('output' => $output);

0 commit comments

Comments
 (0)