iT邦幫忙

0

透過PHP新增filezilla server使用者

  • 分享至 

  • xImage

請問如何透過PHP新增filezilla server的FTP使用者,並設定Home dir
目前試過透過編輯xml的方式

	public function create_user($user_name, $password, $ftpUserFolder, $createBackup = true, $reloadFTP = true)
	{
		$xmlfile = FTP_CONF_FILE;
		$xmlfolder = dirname(FTP_CONF_FILE);

		$xmlbackupfile = @date("Y-m-d-H-i-s") . '_FileZilla_Server.xml';

		$xml = simplexml_load_file($xmlfile);
		// Copy Config for backup before each change, too.

		$isvalid = $this->filezilla_ftp->isUserID($user_name);
		if (!$isvalid) {
			echo "Username $user_name is invalid\n";
			return false;
		}
		if ($this->filezilla_ftp->check_user_exists($xml, $user_name)) {
			echo "Username $user_name already exists...\n";
			return false;
		}
		if ($createBackup) {
			if (!copy($xmlfile, $xmlbackupfile)) {
				echo "Problem creating xml backup file";
				return false;
			}
		}


		$salt = "";
		while (strlen($salt) !== 64) {
			$byte = ord(openssl_random_pseudo_bytes(1));
			if ($byte > 31 && $byte < 127) {
				$salt .= chr($byte);
			}
		}

		$hashed_password = strtoupper(hash('SHA512', $password . $salt));

		$xml = simplexml_load_file($xmlfile);
		$Pass = $xml->xpath("//User[@Name='TheUserName']//Option[@Name='Pass']");
		$Salt = $xml->xpath("//User[@Name='TheUserName']//Option[@Name='Salt']");

		$Pass[0][0] = $hashed_password;
		$Salt[0][0] = $salt;

		$user = $xml->Users->addChild('User');
		$user->addAttribute('Name', $user_name);

		$this->filezilla_ftp->add_option($user, 'Pass', $hashed_password);
		$this->filezilla_ftp->add_option($user, 'Group', null);
		$this->filezilla_ftp->add_option($user, 'Bypass server userlimit', '0');
		$this->filezilla_ftp->add_option($user, 'User Limit', '0');
		$this->filezilla_ftp->add_option($user, 'IP Limit', '0');
		$this->filezilla_ftp->add_option($user, 'Enabled', '1');
		$this->filezilla_ftp->add_option($user, 'Comments', 'none');
		$this->filezilla_ftp->add_option($user, 'ForceSsl', '0');

		$filter = $user->addChild('IpFilter');
		$filter->addChild('Disallowed');
		$filter->addChild('Allowed');

		$permissions = $user->addChild('Permissions');
		$permission = $permissions->addChild('Permission');

		$permission->addAttribute('Dir', str_replace("/", "\\", $ftpUserFolder));

		$this->filezilla_ftp->add_option($permission, 'FileRead', '1');
		$this->filezilla_ftp->add_option($permission, 'FileWrite', '1');
		$this->filezilla_ftp->add_option($permission, 'FileDelete', '1');
		$this->filezilla_ftp->add_option($permission, 'FileAppend', '1');
		$this->filezilla_ftp->add_option($permission, 'DirCreate', '1');
		$this->filezilla_ftp->add_option($permission, 'DirDelete', '1');
		$this->filezilla_ftp->add_option($permission, 'DirList', '1');
		$this->filezilla_ftp->add_option($permission, 'DirSubdirs', '1');
		$this->filezilla_ftp->add_option($permission, 'IsHome', '1');
		$this->filezilla_ftp->add_option($permission, 'AutoCreate', '1');

		$speed = $user->addChild('SpeedLimits');
		$speed->addAttribute('DlType', '1');
		$speed->addAttribute('DlLimit', '10');
		$speed->addAttribute('ServerDlLimitBypass', '0');
		$speed->addAttribute('UlType', '1');
		$speed->addAttribute('UlLimit', '10');
		$speed->addAttribute('ServerUlLimitBypass', '0');
		$speed->addChild('Download');
		$speed->addChild('Upload');

		if (!$rv = $xml->asXML($xmlfile)) {
			echo('SimpleXML could not write file');
			return false;
		}
		//Change file encoding from UTF8 to ISO-8859-1
		$dom = new DOMDocument("1.0", "ISO-8859-1");
		$dom->preserveWhiteSpace = false;
		$dom->formatOutput = true;
		if (!$dom->load($xmlfile) || !$dom->save($xmlfile)) {
			echo('DOMDocument could not change file enconding from UTF8 to ISO-8859-1');
			return false;
		}
		if ($reloadFTP) {
			$ftpExecutable = '"' . FTP_EXE_FILE . '" /reload-config';
			$command = $ftpExecutable;
			system($command, $retval);
		}
		return true;
	}
	public function isUserID($username) {
		return preg_match('/^[A-Za-z0-9][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$/', $username);
	}
	public function isValid($str) {
		return !preg_match('/[^A-Za-z0-9\_\-]/', $str);
	}
	public function add_option($xmlobj, $name, $value) {
		$option = $xmlobj->addChild('Option', $value);
		$option->addAttribute('Name', $name);
	}
	public function check_user_exists($xml, $username) {
		$children = $xml->Users->children();
		foreach ($children as $child) {
			if ($child->getName() == 'User') {
				foreach ($child->attributes() as $attributes) {
					if (trim($attributes) == trim($username)) {
						echo "Username $username already exits... \n";
						return true;
					}
				}
			}
		}
		return false;
	}

(以上PHP使用codeigniter框架,放在model filezilla_ftp.php中)
但是卡在Passsalt這兩個欄位

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
gemmalyly
iT邦見習生 ‧ 2024-01-24 15:39:21

The password connections is stored as an MD5 hash, as FileZilla Server typically stores passwords in this format. Adjust this based on your server configuration.

0
carinamon
iT邦見習生 ‧ 2024-05-03 10:15:44

With each new update, each new feature, and each new player who joins the fold, the universe of infinite craft continues to expand, offering new adventures and opportunities for players to discover, create, and connect.

我要發表回答

立即登入回答