• 防篡改php文件校验程序


    <?php
    /**
     * 校验线上源文件是否和本地的一致
     * User: Administrator
     * Date: 2015/11/26
     * Time: 9:30
     */
    include_once 'functions.php';
    
    class SrcVerifier
    {
        var $md5_files = array();
        var $total = 0;
    
        public function scan($dir, $prefix_len, $excepts=array())
        {
            if ( $handle = opendir($dir) ) {
                while ( ($file = readdir($handle)) !== false ) {
                    if ( $file != ".." && $file != "." && $file != ".svn" && $file!=".buildpath" && $file!=".htaccess" && $file!=".idea" && $file!=".project" && $file!=".settings") {
                        $real_location = $dir . "/" . $file;
                        $relative_location = substr($real_location, $prefix_len);
                        if ( is_dir($real_location) ) {
                            echo "[dir] - $relative_location [end]
    ";
                            //$files[$file] = scandir($file_location);
                            if(!in_array($relative_location, $excepts)) {
                                $this->scan($real_location, $prefix_len, $excepts);
                            }
                        }else {
                            $this->md5_files[$relative_location] = md5_file($real_location);
                            echo "[file] - $relative_location , ".$this->md5_files[$relative_location]." 
    ";
                            $this->total++;
                        }
                    }
                }
                closedir($handle);
            }
        }
    
        public function list_file($dir, $except_dirs = array())
        {
            $list = scandir($dir); // 得到该文件下的所有文件和文件夹
            foreach ($list as $file) {//遍历
                $file_location = $dir . "/" . $file;//生成路径
                if (is_dir($file_location) && $file != "." && $file != ".." && $file != ".svn") { //判断是不是文件夹
                    echo "[dir] - $file_location 
    ";
                    $this->list_file($file_location); //继续遍历
                } elseif (is_file($file_location)) {
                    echo "[file] - $file_location 
    ";
                    $this->total++;
                }
    
            }
        }
    
        public function total_files(){
            return $this->total;
        }
    }
    
    $root_path = 'd:/projects/php/test';
    $start_time = microtime_float();
    
    //step_1,扫描校验本地源文件,生成文件路径与其md5值之间的映射文件
    $verifier = new SrcVerifier();
    $verifier->scan($root_path, strlen($root_path), array('/data/files','/temp'));
    
    file_put_contents('md5_files.php', '<?php return '.var_export($verifier->md5_files,true));//校验结果写入本地文件
    
    //统计数据
    $scan_local_time = microtime_float() - $start_time;
    echo "scan files: ".$verifier->total_files()."
    ";
    echo "total cost: {$scan_local_time} secs.
    ";
    
    
    //step_2,遍历刚取下的源文件,并和本地的线上源文件进行md5校验,如果存在不一致的,则写入日志
    
    
    //step_3,输出校验结果
    

      

  • 相关阅读:
    oracle 添加用户
    oracle---存储结构及其之间的关系和创建数据库
    oracle---临时表空间
    对字符串进行加密解密知识
    面向对象---函数重复---原型
    Ajax
    sqlHelper+app.config
    sqlHelper ---转载
    web.config中的连接字符串 ---关于 providerName 特性---转载
    SQL中的cast()函数--转载
  • 原文地址:https://www.cnblogs.com/jenqz/p/4999999.html
Copyright © 2020-2023  润新知