PHP递归检测文件系统更改

时间:2013-05-15 00:30:47

标签: php security ftp

我一直在寻找一种检测文件系统更改的实用方法。我从“Jonathan Franzone”找到了这个非常简单的剧本。但我的问题是,它不扫描子文件夹。由于我只是PHP的新手,我想在这里要求提供强大的解决方案。

注意:我在写作之前在网站上进行了扩展搜索。许多问题询问了这个安全网站的方法。但根本没有完整的答复。

<?php
      /**
       * File : ftpMonitor.php
       * Monitors a remote directory via FTP and emails a list of changes if any are
       * found.
       *
       * @version June 4, 2008
       * @author <a href="http://www.franzone.com">Jonathan Franzone</a>
       */

      // Configuration ///////////////////////////////////////////////////////////////
      $host = 'ftp.domain.com';
      $port = 21;
      $user = 'username';
      $pass = 'password';
      $remote_dir = '/public_html';
      $cache_file = 'ftp_cache';
      $email_notify = 'your.email@gmail.com';
      $email_from = 'email.from@gmail.com';

      // Main Run Program ////////////////////////////////////////////////////////////

      // Connect to FTP Host
      $conn = ftp_connect($host, $port) or die("Could not connect to {$host}\n");

      // Login
      if(ftp_login($conn, $user, $pass)) {

        // Retrieve File List
        $files = ftp_nlist($conn, $remote_dir);

        // Filter out . and .. listings
        $ftpFiles = array();
        foreach($files as $file)
        {
          $thisFile = basename($file);
          if($thisFile != '.' && $thisFile != '..') {
            $ftpFiles[] = $thisFile;
          }
        }

        // Retrieve the current listing from the cache file
        $currentFiles = array();
        if(file_exists($cache_file))
        {
          // Read contents of file
          $handle = fopen($cache_file, "r");
          if($handle)
          {
            $contents = fread($handle, filesize($cache_file));
            fclose($handle);

            // Unserialize the contents
            $currentFiles = unserialize($contents);
          }
        }

        // Sort arrays before comparison
        sort($currentFiles, SORT_STRING);
        sort($ftpFiles, SORT_STRING);

        // Perform an array diff to see if there are changes
        $diff = array_diff($ftpFiles, $currentFiles);
        if(count($diff) > 0)
        {
          // Email the changes
          $msg = "<html><head><title>ftpMonitor Changes</title></head><body>" .
            "<h1>ftpMonitor Found Changes:</h1><ul>";
          foreach($diff as $file)
          {
            $msg .= "<li>{$file}</li>";
          }
          $msg .= "</ul>";
          $msg .= '<em>Script by <a href="http://www.franzone.com">Jonathan Franzone</a></em>';
          $msg .= "</body></html>";

          $headers = "MIME-Version: 1.0\r\n";
          $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
          $headers .= "To: {$email_notify}\r\n";
          $headers .= "From: {$email_from}\r\n";
          $headers .= "X-Mailer: PHP/" . phpversion();

          mail($email_notify, "ftpMonitor Changes Found", $msg, $headers);
        }

        // Write new file list out to cache
        $handle = fopen($cache_file, "w");
        fwrite($handle, serialize($ftpFiles));
        fflush($handle);
        fclose($handle);
      }
      else {
        echo "Could not login to {$host}\n";
      }

      // Close Connection
      ftp_close($conn);
      ?>

感谢任何人提供解决方案或至少尝试提供帮助。

编辑:其实我愿意自动要求删除,但是,我不想要求太多。如果它不会是一种痛苦,为什么不呢。

0 个答案:

没有答案