今天安装了Zpanel-6.1.1后添加个ftp用户结果密码输入时不知道多按了个什么,怎么都不正确,就尝试修改密码,可是怎么都修改不成功。报错如下:Can't Open FileZilla configuration file! 以为是没有装好Zpanel,就又重新安装了一次,结果还是出错!很是失望!迫不得已只有看代码了,看看到底是什么原因!经过一番周折最后终于找到原因了。原来在添加ftp用户和删除ftp用户时,都有一个判断,判断服务器操作系统是否为windows,如果为windows时操作windows格式的密码文件(FileZilla Server.xml)否则操作linux格式的密码文件(zftpd.passwd),而修改密码时没有此判断,而是直接使用操作windows格式的密码文件(FileZilla Server.xml)找到原因后就好办了,直接编写代码用crypt函数生成的密码替换zftpd.passwd中的相应ftp用户即可。
原代码如下:
- # Edit a FileZilla FTP Account
- function zapi_ftpaccount_edit($filezilla_root, $username, $newpassword) {
- # Reset the account password for the FTP account!
- $filezilla_reload = "\"" . $filezilla_root . "FileZilla server.exe\" /reload-config";
- $fzpath = $filezilla_root . "FileZilla Server.xml";
- $fzfile = file_get_contents($fzpath) or die("Can't Open FileZilla configuration file!");
- $startpos = strpos($fzfile, "<User Name=\"" . $username . "\">");
- $endpos = strpos($fzfile, "</User>");
- $endposlength = "</User>";
- $endposlength = strlen($endposlength);
- $fzrecord = substr($fzfile, $startpos, ($endpos - $startpos) + $endposlength);
- $replacement = "<Option Name=\"Pass\">" . md5($newpassword) . "</Option>";
- $newfzrecord = preg_replace('/<Option Name=\"Pass\">.*?<\/Option>/', $replacement, $fzrecord);
- $fzfile = substr_replace($fzfile, $newfzrecord, $startpos, ($endpos - $startpos) + $endposlength);
- $filehandle = fopen($fzpath, 'w') or die("Can't Open FileZilla configuration file!");
- if (!fwrite($filehandle, $fzfile)) {
- return false;
- } else {
- return true;
- }
- fclose($filehandle);
- }
修改后如下:
- # Edit a FileZilla FTP Account
- function zapi_ftpaccount_edit($filezilla_root, $username, $newpassword) {
- if (ShowServerPlatform() == "Windows") {
- # Reset the account password for the FTP account!
- $filezilla_reload = "\"" . $filezilla_root . "FileZilla server.exe\" /reload-config";
- $fzpath = $filezilla_root . "FileZilla Server.xml";
- $fzfile = file_get_contents($fzpath) or die("Can't Open FileZilla configuration file!");
- $startpos = strpos($fzfile, "<User Name=\"" . $username . "\">");
- $endpos = strpos($fzfile, "</User>");
- $endposlength = "</User>";
- $endposlength = strlen($endposlength);
- $fzrecord = substr($fzfile, $startpos, ($endpos - $startpos) + $endposlength);
- $replacement = "<Option Name=\"Pass\">" . md5($newpassword) . "</Option>";
- $newfzrecord = preg_replace('/<Option Name=\"Pass\">.*?<\/Option>/', $replacement, $fzrecord);
- $fzfile = substr_replace($fzfile, $newfzrecord, $startpos, ($endpos - $startpos) + $endposlength);
- $filehandle = fopen($fzpath, 'w') or die("Can't Open FileZilla configuration file!");
- if (!fwrite($filehandle, $fzfile)) {
- return false;
- } else {
- return true;
- }
- fclose($filehandle);
- }else {
- $proftpd_config = "/etc/zpanel/conf/ftp/zftpd.passwd";
- $file = file_get_contents($proftpd_config) or die("Can't Open FileZilla configuration file!");
- preg_match("/".$username.":(.{12})([^:]*)(:.*)/i", $file, $matches);
- $newpath = crypt($newpassword, $matches[1]);
- $newfile = str_replace($matches[0], $username.':'.$newpath.$matches[3], $file);
- file_put_contents($proftpd_config,$newfile);
- }
- }
如非注明则为本站原创文章,欢迎转载。转载请注明转载自:moon's blog
我只懂一点的代码,呵呵,要潜心在这里看看文章了