Veno-File-Manager (VFM4)云盘,增加用户只读游客无权限公共目录的方法
解决方法:
1.新增一个用户只读公共目录(user_read_dirs)数组配置
打开 根目录/vfm-admin/config-master.php 文件,新添一个 'user_read_dirs'数组配置参数,代码如下:
- 'user_read_dirs' =>
- array (
- 0 => 'user_public',
- 1 => 'user_temp',
- ),
复制代码
2.为用户只读公共目录(user_read_dirs)设置相应类中成员方法
打开 根目录/vfm-admin/class/class.location.php 文件,参考类中成员方法(用户私有可读写目录) checkUserDir 和 editAllowed() 代码,
新添用户只读公共目录 userPublicDir() 和userReadtAllowed() 成员方法,代码如下:- /**
- * 检查用户是否允许读取当前目录,
- * based on configuration settings
- *
- * @param string $relative relative path to index.php
- *
- * @return true/false
- */
- public function userReadtAllowed($relative = false)
- {
- global $setUp;
- $totdirs = count($this->path);
- $father = $this->getDir(false, true, false, $totdirs -1);
- $hidden_dirs = $setUp->getConfig('hidden_dirs');
- if (!$hidden_dirs) {
- return false;
- }
- if (in_array(basename($father), $hidden_dirs)) {
- return false;
- }
- if ($this->userPublicDir($relative) === true) {
- return true;
- }
- return false;
- }
- /**
- * 检查目录是否可供用户只读使用
- *
- * @param string $relative relative path to index.php
- *
- * @return true/false
- */
- public function userPublicDir($relative = false)
- {
- global $gateKeeper;
- global $setUp;
- $thispath = $this->getDir(true, false, false, 0, $relative);
- if (!is_dir(realpath($thispath))) {
- return false;
- }
-
- $startdir = $setUp->getConfig('starting_dir');
- $publicdirsarray = $setUp->getConfig('user_read_dirs') !== null ? $setUp->getConfig('user_read_dirs') : array();
- $thiscleanpath = ltrim($thispath, './');
- $cleanstartdir = rtrim(ltrim($startdir, './'), '/');
- $thispatharray = explode('/', $thiscleanpath);
- $checkpath = $thispatharray[0] === $cleanstartdir && strlen($cleanstartdir) ? $thispatharray[1] : $thispatharray[0];
- $pathcounter = $thispatharray[0] === $cleanstartdir && strlen($cleanstartdir) ? (int)2 : (int)1;
-
- //检查分配的多个公共目录'user_read_dirs'文件夹
- foreach ($publicdirsarray as $value) {
- //检查是否分配了子/子文件夹
- $userdirarray = explode('/', $value);
- $usersubs = count($userdirarray) - 1;
- if ($usersubs > 0) {
- $subscounter = $usersubs + $pathcounter;
- for ($i = $pathcounter; $i < $subscounter; $i++) {
- $checkpath .= '/'.$thispatharray[$i];
- }
- }
- //最后,检查用户是否可以访问该位置
- if ($value === $checkpath) {
- return true;
- }
- }
- return false;
- }
复制代码
新添游客只读公共目录 guestPublicDir和guestReadtAllowed() 成员方法,代码如下:
- /**
- * 检查游客是否允许读取当前目录,
- * based on configuration settings
- *
- * @param string $relative relative path to index.php
- *
- * @return true/false
- */
- public function guestReadtAllowed($relative = false)
- {
- global $setUp;
- $totdirs = count($this->path);
- $father = $this->getDir(false, true, false, $totdirs -1);
- $hidden_dirs = $setUp->getConfig('hidden_dirs');
- if (!$hidden_dirs) {
- return false;
- }
- if (in_array(basename($father), $hidden_dirs)) {
- return false;
- }
- if ($this->guestPublicDir($relative) === true) {
- return true;
- }
- return false;
- }
- /**
- * 检查目录是否可供游客使用
- *
- * @param string $relative relative path to index.php
- *
- * @return true/false
- */
- public function guestPublicDir($relative = false)
- {
- global $gateKeeper;
- global $setUp;
- $thispath = $this->getDir(true, false, false, 0, $relative);
- if (!is_dir(realpath($thispath))) {
- return false;
- }
-
- $startdir = $setUp->getConfig('starting_dir');
- $publicdirsarray = $setUp->getConfig('public_dirs') !== null ? $setUp->getConfig('public_dirs') : array();
- $thiscleanpath = ltrim($thispath, './');
- $cleanstartdir = rtrim(ltrim($startdir, './'), '/');
- $thispatharray = explode('/', $thiscleanpath);
- $checkpath = $thispatharray[0] === $cleanstartdir && strlen($cleanstartdir) ? $thispatharray[1] : $thispatharray[0];
- $pathcounter = $thispatharray[0] === $cleanstartdir && strlen($cleanstartdir) ? (int)2 : (int)1;
-
- //检查分配的多个公共目录'public_dirs'文件夹
- foreach ($publicdirsarray as $value) {
- //检查是否分配了子/子文件夹
- $userdirarray = explode('/', $value);
- $usersubs = count($userdirarray) - 1;
- if ($usersubs > 0) {
- $subscounter = $usersubs + $pathcounter;
- for ($i = $pathcounter; $i < $subscounter; $i++) {
- $checkpath .= '/'.$thispatharray[$i];
- }
- }
- //最后,检查用户是否可以访问该位置
- if ($value === $checkpath) {
- return true;
- }
- }
-
- return false;
- }
复制代码
3.修改文件夹类,把用户只读目录(user_read_dirs)添加进用户目录
打开 根目录/vfm-admin/class/class.dirs.php 文件,查找数组变量 $userpatharray ,代码如下:- $userpatharray = $gateKeeper->getUserInfo('dir') !== null ? json_decode($gateKeeper->getUserInfo('dir'), true) : false;
复制代码
把公共目录(public_dir)和用户只读目录(user_read_dirs)添加到 $userpatharray数组中,修改后代码为:- $userpatharray = $gateKeeper->getUserInfo('dir') !== null ? json_decode($gateKeeper->getUserInfo('dir'), true) : false;
- $public_dirs = $setUp->getConfig('public_dirs');
- $user_read_dirs = $setUp->getConfig('user_read_dirs');
- @$userpatharray = array_merge($userpatharray,$public_dirs);
- if($gateKeeper->isUserLoggedIn() ){
- @$userpatharray = array_merge($userpatharray,$public_dirs,$user_read_dirs);
- }
复制代码
再查找仅获取用户分配的文件夹(如果有)遍历语句,添加 !$this->location->guestReadtAllowed($relative) 和 !$this->location->userReadtAllowed($relative) 两个条件,修改后如下:- if (is_array($content)) {
- foreach ($content as $item) {
- if (is_dir($item)) {
- $mbitem = Utils::mbPathinfo($item);
- $item_basename = $mbitem['basename'];
- // get only users' assigned folders if any
- if ($userpatharray
- && !in_array($item_basename, $userpatharray)
- && !$this->location->editAllowed($relative)
- && !$this->location->guestReadtAllowed($relative)
- && !$this->location->userReadtAllowed($relative)) {
- continue;
- }
-
- // Skip /vfm-admin/ if the main uploads dir is the root
- if (!$hidefiles || ($hidefiles && !in_array($item_basename, $hidden_dirs))) {
- $this->dirs[] = new Dir($item_basename, $this->location, $relative);
- }
- }
- }
- }
复制代码
4.修改前端显示UI代码,让游客和登录用户显示公共目录(public_dirs)、用户只读目录(user_read_dirs)和根目录(starting_dir)中的文件
打开根目录 vfm-admin/template/list-files.php 文件,把条件判断语句改为如下格式:- if ($gateKeeper->isAccessAllowed()){
- if( $location->editAllowed()
- || $location->guestReadtAllowed()
- || $location->userReadtAllowed() && $gateKeeper->isUserLoggedIn()
- || $location->getCleanPath() ==="") {
- if ($gateKeeper->isAllowed('view_enable')) {
- 列表显示文件代码 }
- 显示上传文件功能区块代码 }
- }
复制代码 存在问题,当以上代码中添加 guestReadtAllowed()、userReadtAllowed() 、getCleanPath() ==="" 三条判断语句后,会出现一个新的bug,即编辑(editor)用户打开首页、游客公共目录(public_dirs)或用户只读公共目录(user_read_dirs),一直显示加载状态,打不开网页。
解决方法:
打开根目录 vfm-admin/include/load-js.php文件,查找与更名 (rename_enable) 相关代码,把显示条件判断语句改为如下格式:
单选操作(右侧):更名
- if ($gateKeeper->isAllowed('rename_enable') && $location->editAllowed())
复制代码
解决浏览公共目录(user_read_dirs)下文件时时,“多选操作”菜单中仍显示移动、复制和删除按钮的问题:
大概在65行左右,添加条件判断语句,使“多选操作”菜单中不显示移动、复制和删除按钮,改后代码如下:
- if ($gateKeeper->isAllowed('move_enable') && $location->checkUserDir()) { ?>
- <li>
- <a class="multimove dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#archive-map-move" data-action="move">
- <i class="bi bi-arrow-right"></i>
- <?php echo $setUp->getString("move"); ?>
- </a>
- </li>
- <?php
- }
- if ($gateKeeper->isAllowed('copy_enable') && $location->checkUserDir()) { ?>
- <li>
- <a class="multicopy dropdown-item" href="#" data-bs-toggle="modal" data-bs-target="#archive-map-copy" data-action="copy">
- <i class="bi bi-clipboard-check"></i>
- <?php echo $setUp->getString("copy"); ?>
- </a>
- </li>
- <?php
- }
- if ($gateKeeper->isAllowed('delete_enable') && $location->checkUserDir()) { ?>
- <li><a class="multic dropdown-item" href="#">
- <i class="bi bi-trash"></i>
- <?php echo $setUp->getString("delete"); ?>
- </a>
- </li>
- <?php
- } ?>
复制代码
也可以把条件语句改为如下代码:
- if ($gateKeeper->isAllowed('move_enable') && !$location->guestReadtAllowed() && !$location->userReadtAllowed() && $location->getCleanPath() !=="")
复制代码
解决PC列表模式下显示公共目录(user_read_dirs)下文件时时,表格仍显示重命名和删除列的问题:
大概在137行左右,添加条件判断语句,使表格不显示重命名和删除列,改后代码如下:
- if ($gateKeeper->isAllowed('rename_enable') && $location->checkUserDir()) { ?>
- <td class="small text-center gridview-hidden d-none d-md-table-cell">
- <i class="bi bi-pencil"></i>
- </td>
- <?php
- } ?>
- <td class="small text-center gridview-hidden">
- <?php
- if ($gateKeeper->isAllowed('delete_enable') && $location->checkUserDir()) { ?>
- <i class="bi bi-trash d-none d-md-block"></i>
- <?php
- } ?>
复制代码
5.修改ajax请求代码,使游客和登录用户能获取到公共目录(public_dirs)、用户只读目录(user_read_dirs)和根目录(starting_dir)中的文件
打开根目录 /vfm-admin/ajax/get-files.php 文件,查找如下代码:- if ($gateKeeper->isAccessAllowed() && $location->editAllowed('../../') && $gateKeeper->isAllowed('view_enable'))
- {
- $fullpath = $location->getFullPath();
- ...
- }
复制代码
改为如下代码:
- $cleanlocdir = rtrim(ltrim($locdir, './'), '/');
- $startdir = './'.$setUp->getConfig('starting_dir');
- $cleanstartdir = rtrim(ltrim($startdir, './'), '/');
- if ($cleanlocdir === $cleanstartdir
- || $gateKeeper->isAccessAllowed()
- && $location->guestReadtAllowed('../../')
- || $location->editAllowed('../../')
- || $location->userReadtAllowed('../../')
- && $gateKeeper->isAllowed('view_enable')) {
- $fullpath = $location->getFullPath();
- ...
- }
复制代码
解决平铺视图模式下显示公共目录(user_read_dirs)下文件时时,在文件右上角显示重命名和删除按钮问题:
大概在271行左右,添加条件判断语句,使平铺视图文件右上角不显示重命名和删除按钮,改后代码如下:- if ($gateKeeper->isAllowed('rename_enable') && $location->editAllowed('../../')) {
- $data['icon'] .= '<div class="icon text-center minibtn">
- <button class="round-btn rename" data-thisdir="'.$thisdir.'" data-thisext="'.$ext.'" data-thisname="'.$normalizedName.'">
- <i class="bi bi-pencil-square"></i>
- </button>
- </div>';
- }
- if ($gateKeeper->isAllowed('delete_enable') && $location->editAllowed('../../')) {
- $data['icon'] .= '<div class="minibtn">
- <button class="round-btn del" data-name="'.$thisfile.'" data-link="'.$thisdel.'&h='.$cash.'">
- <i class="bi bi-trash"></i>
- </button>
- </div>';
- }
复制代码
解决PC浏览器打开公共目录(user_read_dirs)时,在文件列表右侧显示重命名和删除按钮问题:
大概在326行左右,添加条件判断语句,使文件列表右侧不显示重命名和删除按钮,改后代码如下:- $data['delete'] = '';
- if ($gateKeeper->isAllowed('delete_enable') && $location->editAllowed('../../')) {
- $data['delete'] .= '<div class="d-none d-md-block"><button class="round-btn btn-mini del" data-name="'.$thisfile.'" data-link="'.$thisdel.'&h='.$cash.'"><i class="bi bi-x-lg"></i></button></div>';
- }
复制代码
解决手机端打开公共目录(user_read_dirs)时,在文件列表右侧显示重命名和删除按钮问题:
大概在342行左右,添加条件判断语句,使文件列表右侧不显示重命名和删除按钮,改后代码如下码:- if ($gateKeeper->isAllowed('rename_enable') && $location->editAllowed('../../')) {
- $data['delete'] .= '<li>
- <a class="rename dropdown-item" data-thisdir="'.$thisdir.'" data-thisext="'.$ext.'" data-thisname="'.$normalizedName.'" href="javascript:void(0)">
- <i class="bi bi-pencil-square"></i> '.$setUp->getString("rename").'</a></li>';
- }
- if ($gateKeeper->isAllowed('delete_enable') && $location->editAllowed('../../')) {
- $data['delete'] .= '<li>
- <a class="del dropdown-item" href="javascript:void(0)" data-link="'.$thisdel.'&h='.$cash.'" data-name="'.$thisfile.'"><i class="bi bi-trash"></i> '.$setUp->getString("delete").'</a></li>';
- }
复制代码
6.修改ajax请求代码,使游客无权获取用户只读公共目录(user_read_dirs)内子目录。
打开根目录 /vfm-admin/ajax/get-dirs.php 文件,查找如下代码:
- if ($gateKeeper->isAccessAllowed() && $gateKeeper->isAllowed('viewdirs_enable')) {
- $fullpath = $location->getFullPath();
- ...
- } // end allowed
复制代码
再此基础上再添加多条判断语句,修改后代码如下:- $cleanlocdir = rtrim(ltrim($locdir, './'), '/');
- $startdir = './'.$setUp->getConfig('starting_dir');
- $cleanstartdir = rtrim(ltrim($startdir, './'), '/');
- if ($cleanlocdir === $cleanstartdir
- && $gateKeeper->isAccessAllowed()
- && $gateKeeper->isAllowed('viewdirs_enable')
- || $location->guestReadtAllowed('../../')
- || $location->editAllowed('../../')
- || $location->userReadtAllowed('../../')
- && $gateKeeper->isUserLoggedIn()) {
- $fullpath = $location->getFullPath();
- ...
- } // end allowed
复制代码
实现此功能相关文件:
根目录/vfm-admin/config-master.php //配置用户只读目录(user_read_dirs)
根目录/vfm-admin/class/class.location.php //检查当前目录的用户权限
根目录/vfm-admin/class/class.dirs.php //遍历所有用户私有文件夹下的子目录,使可以列表显示
根目录/vfm-admin/template/list-files.php //列表显示文件UI模板
根目录/vfm-admin/template/list-folders.php //列表显示文件夹模板
根目录 /vfm-admin/ajax/get-files.php //获取并显示文件名称
根目录 /vfm-admin/ajax/get-dirs.php //获取并显示文件夹名称
根目录vfm-admin/include/load-js.php //单选操作(右侧):更名
|
|