Skip to content

Commit

Permalink
"cd" fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nickola committed Nov 5, 2016
1 parent 72cbe11 commit 5f80bbf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "WebConsole",
"version": "0.9.6",
"version": "0.9.7",
"devDependencies": {
"grunt": "~0.4.2",
"grunt-contrib-concat": "~0.3.0",
Expand Down
69 changes: 37 additions & 32 deletions src/webconsole.main.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
<?php
// Initializing
if (!isset($NO_LOGIN)) $NO_LOGIN = false;
if (!isset($ACCOUNTS)) $ACCOUNTS = array();
if (isset($USER) && isset($PASSWORD) && $USER && $PASSWORD) $ACCOUNTS[$USER] = $PASSWORD;
if (!isset($PASSWORD_HASH_ALGORITHM)) $PASSWORD_HASH_ALGORITHM = '';
if (!isset($NO_LOGIN)) $NO_LOGIN = false;
if (!isset($HOME_DIRECTORY)) $HOME_DIRECTORY = '';
$IS_CONFIGURED = ($NO_LOGIN || count($ACCOUNTS) >= 1) ? true : false;

// Utilities
function is_empty_string($string) {
return strlen($string) <= 0;
}

function is_equal_strings($string1, $string2) {
return strcmp($string1, $string2) == 0;
}

function get_hash($algorithm, $string) {
return hash($algorithm, trim((string) $string));
}

// Command execution
function execute_command($command) {
$descriptors = array(
Expand Down Expand Up @@ -37,7 +50,7 @@ function execute_command($command) {
function parse_command($command) {
$value = ltrim((string) $command);

if ($value && !empty($value)) {
if (!is_empty_string($value)) {
$values = explode(' ', $value);
$values_total = count($values);

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

if (substr($value_item, -1) == '\\')
$value = $value_item . ' ' . $value;
if (substr($value_item, -1) == '\\') $value = $value_item . ' ' . $value;
else break;
}
}
Expand All @@ -66,23 +78,18 @@ private function error($message) {
}

// Authentication
private function password_hash($algorithm, $password) {
return hash($algorithm, trim((string) $password));
}

private function authenticate_user($user, $password) {
$user = trim((string) $user);
$password = trim((string) $password);

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

if (!empty($ACCOUNTS[$user])) {
if ($PASSWORD_HASH_ALGORITHM)
$password = $this->password_hash($PASSWORD_HASH_ALGORITHM, $password);
if (isset($ACCOUNTS[$user]) && !is_empty_string($ACCOUNTS[$user])) {
if ($PASSWORD_HASH_ALGORITHM) $password = get_hash($PASSWORD_HASH_ALGORITHM, $password);

if (strcmp($password, $ACCOUNTS[$user]) == 0)
return $user . ':' . $this->password_hash('sha256', $password);
if (is_equal_strings($password, $ACCOUNTS[$user]))
return $user . ':' . get_hash('sha256', $password);
}
}

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

if (!empty($ACCOUNTS[$user])) {
$real_password_hash = $this->password_hash('sha256', $ACCOUNTS[$user]);

if (strcmp($password_hash, $real_password_hash) == 0)
return $user;
if (isset($ACCOUNTS[$user]) && !is_empty_string($ACCOUNTS[$user])) {
$real_password_hash = get_hash('sha256', $ACCOUNTS[$user]);
if (is_equal_strings($password_hash, $real_password_hash)) return $user;
}
}
}

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

private function get_home_directory($user, $default) {
private function get_home_directory($user) {
global $HOME_DIRECTORY;

if (!empty($HOME_DIRECTORY)) {
if (is_string($HOME_DIRECTORY)) return $HOME_DIRECTORY;
else if (!empty($user) && is_string($user) && !empty($HOME_DIRECTORY[$user]))
return $HOME_DIRECTORY[$user];
if (is_string($HOME_DIRECTORY)) {
if (!is_empty_string($HOME_DIRECTORY)) return $HOME_DIRECTORY;
}
else if (is_string($user) && !is_empty_string($user) && isset($HOME_DIRECTORY[$user]) && !is_empty_string($HOME_DIRECTORY[$user]))
return $HOME_DIRECTORY[$user];

return $default ? getcwd() : '';
return getcwd();
}

// Environment
Expand All @@ -135,9 +140,9 @@ private function get_environment() {

private function set_environment($environment) {
$environment = !empty($environment) ? (array) $environment : array();
$path = !empty($environment['path']) ? $environment['path'] : $this->home_directory;
$path = (isset($environment['path']) && !is_empty_string($environment['path'])) ? $environment['path'] : $this->home_directory;

if (!empty($path)) {
if (!is_empty_string($path)) {
if (is_dir($path)) {
if (!@chdir($path)) return array('output' => "Unable to change directory to current working directory, updating current directory",
'environment' => $this->get_environment());
Expand All @@ -150,7 +155,7 @@ private function set_environment($environment) {
// Initialization
private function initialize($token, $environment) {
$user = $this->authenticate_token($token);
$this->home_directory = $this->get_home_directory($user, true);
$this->home_directory = $this->get_home_directory($user);
$result = $this->set_environment($environment);

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

$home_directory = $this->get_home_directory($user, false);
if (!empty($home_directory)) {
$home_directory = $this->get_home_directory($user);
if (!is_empty_string($home_directory)) {
if (is_dir($home_directory)) $result['environment']['path'] = $home_directory;
else $result['output'] = "Home directory not found: ". $home_directory;
}
Expand All @@ -175,9 +180,9 @@ public function cd($token, $environment, $path) {
if ($result) return $result;

$path = trim((string) $path);
if (!isset($path) || !strlen($path)) $path = $this->home_directory;
if (is_empty_string($path)) $path = $this->home_directory;

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

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

return array('output' => $output);
Expand Down

0 comments on commit 5f80bbf

Please sign in to comment.