Application->getModule('configuration')->isApplicationConfig() === true) { $params = ConfigurationManager::getApplicationConfig(); $useSudo = ((integer)$params['bconsole']['use_sudo'] === 1); $bconsoleCmdPath = $params['bconsole']['bin_path']; $bconsoleCfgPath = $params['bconsole']['cfg_path']; $bconsoleCfgCustomPath = array_key_exists('cfg_custom_path', $params['bconsole']) ? $params['bconsole']['cfg_custom_path'] : null; $this->setEnvironmentParams($bconsoleCmdPath, $bconsoleCfgPath, $bconsoleCfgCustomPath, $useSudo); } } private function setEnvironmentParams($bconsoleCmdPath, $bconsoleCfgPath, $bconsoleCfgCustomPath, $useSudo) { $this->bconsoleCmdPath = $bconsoleCmdPath; $this->bconsoleCfgPath = $bconsoleCfgPath; $this->bconsoleCfgCustomPath = $bconsoleCfgCustomPath; $this->useSudo = $useSudo; } private function isCommandValid($command) { $command = trim($command); return in_array($command, $this->availableCommands); } private function prepareResult(array $output, $exitcode, $bconsoleCommand) { array_pop($output); // deleted 'quit' bconsole command for($i = 0; $i < count($output); $i++) { if(strstr($output[$i], $bconsoleCommand) == false) { unset($output[$i]); } else { break; } } $output = count($output) > 1 ? array_values($output) : array_shift($output); return (object)array('output' => $output, 'exitcode' => (integer)$exitcode); } public function bconsoleCommand($director, array $command, $user = null) { $baseCommand = count($command) > 0 ? $command[0] : null; if($this->isCommandValid($baseCommand) === true) { $result = $this->execCommand($director, $command, $user); } else { $result = $this->prepareResult(array(BconsoleError::MSG_ERROR_INVALID_COMMAND, ''), BconsoleError::ERROR_INVALID_COMMAND, ' '); } return $result; } private function execCommand($director, array $command, $user) { $cmd = ''; if(!is_null($director) && $this->isValidDirector($director) === false) { $output = array(BconsoleError::MSG_ERROR_INVALID_DIRECTOR, ''); $exitcode = BconsoleError::ERROR_INVALID_DIRECTOR; $result = $this->prepareResult($output, $exitcode, ' '); } else { $dir = is_null($director) ? '': '-D ' . $director; $sudo = ($this->useSudo === true) ? self::SUDO . ' ' : ''; $bconsoleCommand = implode(' ', $command); if(!is_null($this->bconsoleCfgCustomPath) && !is_null($user)) { $this->bconsoleCfgPath = str_replace(self::BCONSOLE_CFG_USER_KEYWORD, $user, $this->bconsoleCfgCustomPath); } $cmd = sprintf(self::BCONSOLE_COMMAND_PATTERN, $sudo, $this->bconsoleCmdPath, $this->bconsoleCfgPath, $dir, $bconsoleCommand); exec($cmd, $output, $exitcode); if($exitcode != 0) { $output = array(BconsoleError::MSG_ERROR_BCONSOLE_CONNECTION_PROBLEM, ''); $exitcode = BconsoleError::ERROR_BCONSOLE_CONNECTION_PROBLEM; $result = $this->prepareResult($output, $exitcode, ' '); } else { $result = $this->prepareResult($output, $exitcode, $bconsoleCommand); } } $this->Application->getModule('logging')->log($cmd, $result, Logging::CATEGORY_EXECUTE, __FILE__, __LINE__); return $result; } public function getDirectors() { $sudo = ($this->useSudo === true) ? self::SUDO . ' ' : ''; $cmd = sprintf(self::BCONSOLE_DIRECTORS_PATTERN, $sudo, $this->bconsoleCmdPath, $this->bconsoleCfgPath); exec($cmd, $output, $exitcode); if($exitcode != 0) { $output = array(BconsoleError::MSG_ERROR_BCONSOLE_CONNECTION_PROBLEM, ''); $exitcode = BconsoleError::ERROR_BCONSOLE_CONNECTION_PROBLEM; } $result = (object)array('output' => $output, 'exitcode' => $exitcode); return $result; } private function isValidDirector($director) { $ret = in_array($director, $this->getDirectors()->output); return $ret; } public function testBconsoleCommand(array $command, $bconsoleCmdPath, $bconsoleCfgPath, $useSudo) { $this->setEnvironmentParams($bconsoleCmdPath, $bconsoleCfgPath, null, $useSudo); $director = array_shift($this->getDirectors()->output); $result = $this->bconsoleCommand($director, $command); return $result; } } ?>