Destructor(); } include_once('class.errHandler.php'); class FileServerManager extends errHandler { var $_restart_required = false; var $_destructor_registered = false; /** * @return bool * @param string $action * @param array $params * @param string $which * @desc call to fileservermng */ function _call($action, $params = array(), $which = 'msg') { $args = array('--' . $action); foreach ($params AS $param_name => $param_value) { if (!is_bool($param_value)) $args[] = '--' . $param_name . '=' . $param_value; elseif (true === $param_value) $args[] = '--' . $param_name; } if (false === ($ret = mod_util_exec('fileserver', 'filesrvmng', $args, $which))) { global $plesk_errormsg; $this->setError_('filesrvmng failed: ' . $plesk_errormsg); } return $ret; } /** * @return bool * @param string $name * @param string $sys_name * @param string $password * @desc add new user */ function add_user($name, $sys_name, $password, $without_restart = true) { $params = array( 'smbuser-name' => $name, 'user-name' => $sys_name, 'without-restart' => $without_restart, ); if ($without_restart) $this->restart_required(); putenv('PSA_PASSWORD=' . $password); if (false === $this->_call('add-smbuser', $params)) { putenv('PSA_PASSWORD='); $this->setError_('Unable to add user: ' . $this->getErrorMsg()); return false; } putenv('PSA_PASSWORD='); return true; } /** * @return bool * @param string $name * @param string $password * @desc change password of existing user */ function set_user_password($name, $sys_name, $password, $without_restart = true) { $params = array( 'smbuser-name' => $name, 'user-name' => $sys_name, 'without-restart' => $without_restart, ); if ($without_restart) $this->restart_required(); putenv('PSA_PASSWORD=' . $password); if (false === $this->_call('set-smbuser-password', $params)) { putenv('PSA_PASSWORD='); $this->setError_('Unable to change password: ' . $this->getErrorMsg()); return false; } putenv('PSA_PASSWORD='); return true; } /** * @return bool * @param string $old_name * @param string $new_name * @desc rename existing user */ function rename_user($old_name, $new_name, $sys_name, $without_restart = true) { $params = array( 'old-smbuser-name' => $old_name, 'new-smbuser-name' => $new_name, 'user-name' => $sys_name, 'without-restart' => $without_restart, ); if ($without_restart) $this->restart_required(); if (false === $this->_call('rename-smbuser', $params)) { $this->setError_('Unable to rename user: ' . $this->getErrorMsg()); return false; } return true; } /** * @return bool * @param string $name * @desc delete existing user */ function del_user($name, $sys_name, $without_restart = true) { $params = array( 'smbuser-name' => $name, 'user-name' => $sys_name, 'without-restart' => $without_restart, ); if ($without_restart) $this->restart_required(); if (false === $this->_call('del-smbuser', $params)) { $this->setError_('Unable to del user: ' . $this->getErrorMsg()); return false; } return true; } /** * @return array() * @desc returns associative (by user name) array of FileServerUsers */ function &get_users() { $users = array(); $query = 'SELECT id FROM ' . FILESERVER_TABLE_USERS; if (false === ($res = mysql_query($query))) { $this->setError_('mysql_query() failed: ' . db_error()); return false; } while(list($uid) = mysql_fetch_row($res)) { include_once('modules/fileserver/FileServerUser.php'); $myFileServerUser =& objectMaker('FileServerUser', $uid); if ($myFileServerUser->error()) { $this->setError_('Unable to create FileServerUser objetc: ' . $this->getErrorMsg()); return false; } $users[$uid] =& $myFileServerUser; } return $users; } /** * @return bool * @param string $share_name * @param string $path * @param bool $writable * @param bool $guest_ok * @param string $description * @param array $users * @param bool $without_restart * @desc add new share */ function add_share($share_name, $path, $writable, $guest_ok, $description, $users = false, $without_restart = true) { $params = array( 'without-restart' => $without_restart, ); if ($without_restart) $this->restart_required(); if (false === $this->_call('reconfigure-all', $params)) { $this->setError_('Unable to add share: ' . $this->getErrorMsg()); return false; } return true; } /** * @return bool * @param string $share_name * @param string $path * @param bool $writable * @param bool $guest_ok * @param string $description * @param array $users * @param bool $without_restart * @desc update existing share preferences */ function update_share($share_name, $path, $writable, $guest_ok, $description, $users = false, $without_restart = true) { $params = array( 'without-restart' => $without_restart, ); if ($without_restart) $this->restart_required(); if (false === $this->_call('reconfigure-all', $params)) { $this->setError_('Unable to add share: ' . $this->getErrorMsg()); return false; } return true; } /** * @return bool * @param string $old_share_name * @param string $new_share_name * @param bool $without_restart * @desc rename existing share */ function rename_share($old_share_name, $new_share_name, $without_restart = true) { $params = array( 'without-restart' => $without_restart, ); if ($without_restart) $this->restart_required(); if (false === $this->_call('reconfigure-all', $params)) { $this->setError_('Unable to add share: ' . $this->getErrorMsg()); return false; } return true; } /** * @return bool * @param string $share_name * @param bool $without_restart * @desc delete existing share */ function del_share($share_name, $without_restart = true) { $params = array( 'share-name' => $share_name, 'without-restart' => $without_restart, ); if ($without_restart) $this->restart_required(); if (false === $this->_call('del-share', $params)) { $this->setError_('Unable to del share: ' . $this->getErrorMsg()); return false; } return true; } /** * @return bool * @param array $preferences * @desc setup fileserver preferences */ function set_preferences($preferences = array(), $without_restart = true) { $params = array( 'without-restart' => $without_restart, ); if ($without_restart) $this->restart_required(); if (false === $this->_call('reconfigure-all', $params)) { $this->setError_('Unable to update fileserver preferences: ' . $this->getErrorMsg()); return false; } return true; } /** * @return string * @desc get fileserver status */ function status() { if (false === ($ret = $this->_call('status', array(), 'lst'))) { $this->setError_('Unable to retrive fileserver status: ' . $this->getErrorMsg()); return false; } return $ret[sizeof($ret) - 1]; } /** * @return bool * @desc stop fileserver */ function stop() { if (false === $this->_call('stop')) { $this->setError_('Unable to stop fileserver: ' . $this->getErrorMsg()); return false; } return true; } /** * @return bool * @desc start fileserver */ function start() { if (false === $this->_call('start')) { $this->setError_('Unable to start fileserver: ' . $this->getErrorMsg()); return false; } return true; } /** * @return bool * @desc restart fileserver */ function restart() { if (false === $this->_call('restart')) { $this->setError_('Unable to restart fileserver: ' . $this->getErrorMsg()); return false; } $this->_restart_required = false; return true; } /** * @return $string * @desc return usage of server */ function usage_status() { if (false === ($ret = $this->_call('usage-status', array(), 'lst'))) { $this->setError_('Unable to retrive usage status of the fileserver: ' . $this->getErrorMsg()); return false; } return $ret; } function restart_required() { static $status; if (!isset($status)) $status = $this->status(); if ('is running' != $status) return true; $this->_restart_required = true; if (!$this->_destructor_registered) register_shutdown_function('FileServerManagerDestructor'); return true; } function Destructor() { if ($this->_restart_required) return $this->restart(); } } ?>