* Bacula(R) - The Network Backup Solution
* Baculum - Bacula web interface
*
- * Copyright (C) 2013-2015 Marcin Haba
+ * Copyright (C) 2013-2016 Marcin Haba
*
* The main author of Baculum is Marcin Haba.
* The original author of Bacula is Kern Sibbald, with contributions
/**
* Store configuration data from settings file
+ *
* @access protected
*/
- protected $appCfg;
+ protected $app_cfg;
/**
* These errors are allowed in API response and they do not cause
* disturb application working (no direction to error page)
+ *
* @access private
*/
- private $allowedErrors = array(
+ private $allowed_errors = array(
GenericError::ERROR_NO_ERRORS,
BconsoleError::ERROR_INVALID_COMMAND,
PoolError::ERROR_NO_VOLUMES_IN_POOL_TO_UPDATE
/**
* Get connection request handler.
* For data requests is used cURL interface.
+ *
* @access public
* @return resource connection handler on success, false on errors
*/
public function getConnection() {
$ch = curl_init();
- $userpwd = sprintf('%s:%s', $this->appCfg['baculum']['login'], $this->appCfg['baculum']['password']);
+ $userpwd = sprintf('%s:%s', $this->app_cfg['baculum']['login'], $this->app_cfg['baculum']['password']);
curl_setopt($ch, CURLOPT_USERPWD, $userpwd);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
/**
* Get API specific headers used in HTTP requests.
+ *
* @access private
* @return API specific headers
*/
/**
* Initializes API module (framework module constructor)
+ *
* @access public
* @param TXmlElement $config API module configuration
*/
public function init($config) {
$this->initSessionCache();
- $this->appCfg = $this->Application->getModule('configuration')->getApplicationConfig();
+ $this->app_cfg = $this->Application->getModule('configuration')->getApplicationConfig();
}
/**
* Get URL to use by internal API client's request.
+ *
* @access private
* @return string URL to internal API server
*/
/**
* Set URL parameters and prepare URL to request send.
+ *
* @access private
* @param string &$url reference to URL string variable
*/
/**
* Internal API GET request.
+ *
* @access public
* @param array $params GET params to send in request
* @param bool $use_cache if true then try to use session cache, if false then always use fresh data
/**
* Internal API SET request.
+ *
* @access public
* @param array $params GET params to send in request
* @param array $options POST params to send in request
/**
* Internal API CREATE request.
+ *
* @access public
* @param array $params GET params to send in request
* @param array $options POST params to send in request
/**
* Internal API REMOVE request.
+ *
* @access public
* @param array $params GET params to send in request
* @return object stdClass with request result as two properties: 'output' and 'error'
/**
* Initially parse and prepare every Internal API response.
* If a error occurs then redirect to appropriate error page.
+ *
* @access private
* @param string $result response output as JSON string (not object yet)
* @return object stdClass parsed response with two top level properties 'output' and 'error'
$error = null;
if(is_object($resource) && property_exists($resource, 'error')) {
- if(!in_array($resource->error, $this->allowedErrors)) {
+ if(!in_array($resource->error, $this->allowed_errors)) {
$error = $resource->error;
}
} else {
return $resource;
}
+ /**
+ * Initialize session cache.
+ *
+ * @access public
+ * @param bool $force if true then cache is force initialized
+ * @return none
+ */
public function initSessionCache($force = false) {
if (!isset($_SESSION) || !array_key_exists('cache', $_SESSION) || !is_array($_SESSION['cache']) || $force === true) {
$_SESSION['cache'] = array();
}
}
+ /**
+ * Get session cache value by params.
+ *
+ * @access private
+ * @param array $params command parameters as numeric array
+ * @return mixed if cache exists then returned is cached data, otherwise null
+ */
private function getSessionCache(array $params) {
$cached = null;
$key = $this->getSessionKey($params);
return $cached;
}
+ /**
+ * Save data to session cache.
+ *
+ * @access private
+ * @param array $params command parameters as numeric array
+ * @param mixed $value value to save in cache
+ * @return none
+ */
private function setSessionCache(array $params, $value) {
$key = $this->getSessionKey($params);
$_SESSION['cache'][$key] = $value;
}
+ /**
+ * Get session key by command parameters.
+ *
+ * @access private
+ * @param array $params command parameters as numeric array
+ * @return string session key for given command
+ */
private function getSessionKey(array $params) {
$key = implode(';', $params);
$key = base64_encode($key);
return $key;
}
+ /**
+ * Check if session key exists in session cache.
+ *
+ * @access private
+ * @param string $key session key
+ * @return bool true if session key exists, otherwise false
+ */
private function isSessionValue($key) {
$is_value = array_key_exists($key, $_SESSION['cache']);
return $is_value;
public function getDbNameByType($type) {
$type = (string) $type;
switch($type) {
- case self::PGSQL: $dbName = self::PGSQL_NAME; break;
- case self::MYSQL: $dbName = self::MYSQL_NAME; break;
- case self::SQLITE: $dbName = self::SQLITE_NAME; break;
- default: $dbName = null; break;
+ case self::PGSQL: $db_name = self::PGSQL_NAME; break;
+ case self::MYSQL: $db_name = self::MYSQL_NAME; break;
+ case self::SQLITE: $db_name = self::SQLITE_NAME; break;
+ default: $db_name = null; break;
}
- return $dbName;
+ return $db_name;
}
/**
* @return boolean true if config save is successfully, false if config save is failure
*/
public function setApplicationConfig(array $config) {
- $cfgFile = Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf');
- return ($this->Application->getModule('misc')->writeINIFile($cfgFile, $config) != false);
+ $cfg_file = Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf');
+ return ($this->Application->getModule('misc')->writeINIFile($cfg_file, $config) != false);
}
/**
* @return array application configuration
*/
public static function getApplicationConfig() {
- $cfgFile = Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf');
- return Miscellaneous::parseINIFile($cfgFile);
+ $cfg_file = Prado::getPathOfNamespace(self::CONFIG_FILE, '.conf');
+ return Miscellaneous::parseINIFile($cfg_file);
}
/**
* @access public
* @param string $user username
* @param string $password user's password
- * @param boolean $firstUsage determine if it is first saved user during first Baculum run
- * @param mixed $oldUser previous username before change
+ * @param boolean $first_usage determine if it is first saved user during first Baculum run
+ * @param mixed $old_user previous username before change
* @return boolean true if user saved successfully, otherwise false
*/
- public function setUsersConfig($user, $password, $firstUsage = false, $oldUser = null) {
- if ($firstUsage === true) {
+ public function setUsersConfig($user, $password, $first_usage = false, $old_user = null) {
+ if ($first_usage === true) {
$this->clearUsersConfig();
}
- $allUsers = $this->getAllUsers();
+ $all_users = $this->getAllUsers();
$password = $this->getCryptedPassword($password);
- $userExists = array_key_exists($user, $allUsers);
+ $userExists = array_key_exists($user, $all_users);
if ($userExists === true) {
// update user password;
- $allUsers[$user] = $password;
+ $all_users[$user] = $password;
}
- if (!is_null($oldUser) && $oldUser !== $user) {
+ if (!is_null($old_user) && $old_user !== $user) {
// delete old username with password from configuration file
- if (array_key_exists($oldUser, $allUsers)) {
- unset($allUsers[$oldUser]);
+ if (array_key_exists($old_user, $all_users)) {
+ unset($all_users[$old_user]);
}
}
// add new user if does not exist
if ($userExists === false) {
- $allUsers[$user] = $password;
+ $all_users[$user] = $password;
}
- $result = $this->saveUserConfig($allUsers);
+ $result = $this->saveUserConfig($all_users);
return $result;
}
* @return array users/passwords list
*/
public function getAllUsers() {
- $allUsers = array();
+ $all_users = array();
if ($this->isUsersConfig() === true) {
- $usersFile = Prado::getPathOfNamespace(self::USERS_FILE, '.users');
- $users = file($usersFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
+ $users_file = Prado::getPathOfNamespace(self::USERS_FILE, '.users');
+ $users = file($users_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
for($i = 0; $i < count($users); $i++) {
if (preg_match("/^(?P<user>\S+)\:(?P<hash>\S+)$/", $users[$i], $match) === 1) {
- $allUsers[$match['user']] = $match['hash'];
+ $all_users[$match['user']] = $match['hash'];
}
}
}
- return $allUsers;
+ return $all_users;
}
/**
* and encrypted passwords as values.
*
* @access public
- * @param array $allUsers users/passwords list
+ * @param array $all_users users/passwords list
* @return boolean true if users file saved successfully, otherwise false
*/
- public function saveUserConfig($allUsers) {
+ public function saveUserConfig($all_users) {
$users = array();
- foreach ($allUsers as $user => $pwd) {
+ foreach ($all_users as $user => $pwd) {
$users[] = "$user:$pwd";
}
- $usersFile = Prado::getPathOfNamespace(self::USERS_FILE, '.users');
+ $users_file = Prado::getPathOfNamespace(self::USERS_FILE, '.users');
$usersToFile = implode("\n", $users);
$old_umask = umask(0);
umask(0077);
- $result = file_put_contents($usersFile, $usersToFile) !== false;
+ $result = file_put_contents($users_file, $usersToFile) !== false;
umask($old_umask);
return $result;
}
*/
public function removeUser($username) {
$result = false;
- $allUsers = $this->getAllUsers();
- if (array_key_exists($username, $allUsers)) {
- unset($allUsers[$username]);
- $result = $this->saveUserConfig($allUsers);
+ $all_users = $this->getAllUsers();
+ if (array_key_exists($username, $all_users)) {
+ unset($all_users[$username]);
+ $result = $this->saveUserConfig($all_users);
}
return $result;
}
* @return boolean true if file cleared successfully, otherwise false
*/
public function clearUsersConfig() {
- $usersFile = Prado::getPathOfNamespace(self::USERS_FILE, '.users');
- $result = file_put_contents($usersFile, '') !== false;
+ $users_file = Prado::getPathOfNamespace(self::USERS_FILE, '.users');
+ $result = file_put_contents($users_file, '') !== false;
return $result;
}
* @return none
*/
public function switchToUser($http_protocol, $host, $port, $user, $password) {
- $urlPrefix = $this->Application->getModule('friendly-url')->getUrlPrefix();
- $location = sprintf("%s://%s:%s@%s:%d%s", $http_protocol, $user, $password, $host, $port, $urlPrefix);
- $refresh_url = sprintf("%s://%s:%d%s", $http_protocol, $host, $port, $urlPrefix);
+ $url_prefix = $this->Application->getModule('friendly-url')->getUrlPrefix();
+ $location = sprintf("%s://%s:%s@%s:%d%s", $http_protocol, $user, $password, $host, $port, $url_prefix);
+ $refresh_url = sprintf("%s://%s:%d%s", $http_protocol, $host, $port, $url_prefix);
/**
* Refresh page is required due to lack of auth data in $_SERVER superglobal array