&1 <config = $this->getModule('api_config')->getConfig('bconsole'); if(count($this->config) > 0) { $use_sudo = ((integer)$this->config['use_sudo'] === 1); $cmd_path = $this->config['bin_path']; $custom_cfg_path = self::getCfgPath(); $cfg_path = isset($custom_cfg_path) ? $custom_cfg_path : $this->config['cfg_path']; $this->setEnvironmentParams($cmd_path, $cfg_path, $use_sudo); } } public static function setCmdPath($path, $force = false) { // possible to set only once if (is_null(self::$cmd_path) || $force) { self::$cmd_path = $path; } } public static function getCmdPath() { return self::$cmd_path; } public static function setCfgPath($path, $force = false) { // possible to set only once if (is_null(self::$cfg_path) || $force) { self::$cfg_path = $path; } } public static function getCfgPath() { return self::$cfg_path; } public function setUseSudo($use_sudo, $force) { // possible to set only once if (is_null($this->use_sudo) || $force) { $this->use_sudo = $use_sudo; } } public function getUseSudo() { return $this->use_sudo; } private function setEnvironmentParams($cmd_path, $cfg_path, $use_sudo, $force = false) { self::setCmdPath($cmd_path, $force); self::setCfgPath($cfg_path, $force); $this->setUseSudo($use_sudo, $force); } private function isCommandValid($command) { $command = trim($command); return in_array($command, $this->allowed_commands); } private function prepareResult(array $output, $exitcode, $bconsole_command) { array_pop($output); // deleted 'quit' bconsole command for($i = 0; $i < count($output); $i++) { if(strstr($output[$i], $bconsole_command) == 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) { if (count($this->config) > 0 && $this->config['enabled'] !== '1') { throw new BConsoleException( BconsoleError::MSG_ERROR_BCONSOLE_DISABLED, BconsoleError::ERROR_BCONSOLE_DISABLED ); } $base_command = count($command) > 0 ? $command[0] : null; if($this->isCommandValid($base_command) === true) { $result = $this->execCommand($director, $command, $user); } else { throw new BConsoleException( BconsoleError::MSG_ERROR_INVALID_COMMAND, BconsoleError::ERROR_INVALID_COMMAND ); } return $result; } private function execCommand($director, array $command, $user) { $cmd = ''; $result = null; if(!is_null($director) && $this->isValidDirector($director) === false) { throw new BConsoleException( BconsoleError::MSG_ERROR_INVALID_DIRECTOR, BconsoleError::ERROR_INVALID_DIRECTOR ); } else { $dir = is_null($director) ? '': '-D ' . $director; $sudo = ($this->getUseSudo() === true) ? self::SUDO . ' ' : ''; $bconsole_command = implode(' ', $command); $cmd = sprintf( self::BCONSOLE_COMMAND_PATTERN, $sudo, self::getCmdPath(), self::getCfgPath(), $dir, $bconsole_command ); exec($cmd, $output, $exitcode); if($exitcode != 0) { $emsg = ' Output=>' . implode("\n", $output) . ', Exitcode=>' . $exitcode; throw new BConsoleException( BconsoleError::MSG_ERROR_BCONSOLE_CONNECTION_PROBLEM . $emsg, BconsoleError::ERROR_BCONSOLE_CONNECTION_PROBLEM ); } else { $result = $this->prepareResult($output, $exitcode, $bconsole_command); } } $this->Application->getModule('logging')->log( $cmd, $output, Logging::CATEGORY_EXECUTE, __FILE__, __LINE__ ); return $result; } public function getDirectors() { $sudo = ($this->getUseSudo() === true) ? self::SUDO . ' ' : ''; $cmd = sprintf( self::BCONSOLE_DIRECTORS_PATTERN, $sudo, self::getCmdPath(), self::getCfgPath() ); exec($cmd, $output, $exitcode); if($exitcode != 0) { $emsg = ' Output=>' . implode("\n", $output) . ', Exitcode=>' . $exitcode; throw new BConsoleException( BconsoleError::MSG_ERROR_BCONSOLE_CONNECTION_PROBLEM . $emsg, BconsoleError::ERROR_BCONSOLE_CONNECTION_PROBLEM ); } $result = (object)array('output' => $output, 'exitcode' => $exitcode); return $result; } private function isValidDirector($director) { return in_array($director, $this->getDirectors()->output); } public function testBconsoleCommand(array $command, $cmd_path, $cfg_path, $use_sudo) { $this->setEnvironmentParams($cmd_path, $cfg_path, $use_sudo, true); $director = ''; $result = null; try { $director = array_shift($this->getDirectors()->output); $result = $this->bconsoleCommand($director, $command); } catch (BException $e) { $result = (object)array( 'output' => $e->getErrorMessage(), 'exitcode' => $e->getErrorCode() ); } return $result; } } ?>