/inc/class.scan.php
PHP | 412 lines | 333 code | 35 blank | 44 comment | 28 complexity | a98095c8363909bbecb86054b456d086 MD5 | raw file
- <?php
- if(!defined('in_main')) exit;
- class SCAN
- {
- private $is_locked;
- private $cfg;
- var $updated_files = array();
- var $storage_dir = './storage/';
- var $runlog_dir = './run_logs/';
- var $dupelog_dir = './dupe_logs/';
- var $datafile = 'data.txt';
- var $download_files = array();
- var $total_dupes = 0;
- private $caught_users;
- private $account_serial = array();
- private $account_name = array();
- private $character_serial = array();
- private $character_name = array();
- // ------------------
- // Initalize the SCAN class
- // Usage:
- // $scan = new SCAN('storage_path');
- // -------------------
- public function SCAN($cfg, $storage_dir = '', $datafile = '')
- {
- $this->cfg = $cfg;
- $this->download_files = (is_array($cfg['download_files']) && !empty($cfg['download_files'])) ? $cfg['download_files'] : $this->download_files;
- $this->storage_dir = ($storage_dir != '') ? $storage_dir : $this->storage_dir;
- $this->datafile = ($datafile != '') ? $datafile : $this->datafile;
- // Quick checks on our folders and etc
- if(!file_exists($this->storage_dir.$this->datafile)) die('Data file does not exist');
- if(!is_writable($this->storage_dir.$this->datafile)) die('Data file is not writable');
- if(!is_writable($this->runlog_dir)) die('Not enough permissions for the run log folder');
- if(!is_writable($this->dupelog_dir)) die('Not enough permissions for the dupe log folder');
- // Check locked process
- $this->is_locked = $this->check_lock_process();
- if($this->is_locked === true) {
- die('Process is locked, please wait...');
- }
- // Okay, now lets download our latest info
- $this->downloadLatest();
- // Get data info
- $datainfo = $this->get_datafile();
- # Run through any of the latest files
- foreach($this->updated_files as $file)
- {
- // Get the data of our latest file (old/new sizes and name)
- $file_data = $datainfo[$file];
- // Open a file handle for the file on our server
- $handle = fopen($this->storage_dir.$file, "r");
- if($file_data['new_size'] > $file_data['old_size'])
- {
- fseek($handle, $file_data['old_size']);
- }
- $contents = fread($handle, filesize($this->storage_dir.$file));
- // Close the on-server file handle
- fclose($handle);
- // Now we will just unleash dupe checks here
- $checks_dir = './inc/checks/';
- if($dh = opendir($checks_dir)) {
- # DEBUG
- if($cfg['enable_debug']) echo "Opend directory: ".$checks_dir."\r\n";
- while(false !== ($check_file = readdir($dh))) {
- # DEBUG
- if($cfg['enable_debug']) echo "Checking file: ".$check_file."\r\n";
- if(preg_match("/check\.([a-zA-Z_\-]*)\.php/", $check_file, $custom))
- {
- # DEBUG
- if($cfg['enable_debug']) echo "Accepted file: ".$custom[0]."\r\n";
- if(include_once $checks_dir.$custom[0]) {
- $function_name = "check_".$custom[1];
- if(is_callable($function_name)) {
- # DEBUG
- if($cfg['enable_debug']) echo "Called function: ".$function_name."\r\n";
- call_user_func_array($function_name, array($file, $contents, $this));
- } else {
- die("Unable to call function: $function_name (from: ".$custom[0].")");
- }
- }
- }
- }
- }
- closedir($dh);
- }
- }
- // ------------------
- // Downloads the latest data via ftp
- // Usage:
- // $this->downloadLatest()
- // -------------------
- private function downloadLatest()
- {
- // set up basic connection
- $conn_id = ftp_connect($this->cfg['ftp_host'], $this->cfg['ftp_port'], 5) or die('Could not connect to ftp server');
- // login with username and password
- $login_result = ftp_login($conn_id, $this->cfg['ftp_user'], $this->cfg['ftp_pass']);
- ftp_pasv ($conn_id, true);
- // Change directory
- if(!ftp_chdir($conn_id, $this->cfg['ftp_logs_dir'])) {
- die('Could not change FTP directory');
- }
- // Now lock the process, do not want anything else going on right now!
- $this->lock_process();
- // Loop through our dupe files
- foreach($this->download_files as $check_files)
- {
- $split = preg_split("/\//", $check_files, -1, PREG_SPLIT_NO_EMPTY);
- $dir = $split[0];
- $regex_file = $split[1];
- # Get a list of files
- $list_files = ftp_nlist($conn_id, $dir);
- # Narrow down the files to those we only want
- $list_files = preg_grep("/$regex_file/", $list_files);
- # Run through these files
- foreach($list_files as $file)
- {
- # Full file path
- $filepath = $dir.'/'.$file;
- // First, check if we have the file downloaded
- // If we do, we'll see if we need to re-download it
- if(file_exists($this->storage_dir.$file))
- {
- // If this file exists then we do some checks to see
- // if we need to update our local file
- // get the size of $file
- $res = ftp_size($conn_id, $filepath);
- // Get current file size
- $cur_size = filesize($this->storage_dir.$file);
- if ($res != -1) {
- if($res > $cur_size) {
- echo "Change in size of $file, $res bytes\r\n";
- // New file? Awsome, pretty simple
- // We download it and shove it in our latest_data file
- $handle = fopen($this->storage_dir.$file, 'a');
- // try to download $remote_file and save it to $handle
- if (ftp_fget($conn_id, $handle, $filepath, FTP_ASCII, $cur_size)) {
- echo "Successfully written to ".$this->storage_dir." $file\r\n";
- } else {
- echo "There was a problem while downloading $remote_file to $local_file\r\n";
- }
- // Set the latest files
- $this->updated_files[] = $file;
- // Update latest data
- $this->update_datafile($file,$cur_size,$res);
- // close the file
- fclose($handle);
- }
- } else {
- echo "couldn't get the size";
- }
- }
- else
- {
- // New file? Awsome, pretty simple
- // We download it and shove it in our latest_data file
- $handle = fopen($this->storage_dir.$file, 'w');
- // try to download $remote_file and save it to $handle
- if (ftp_fget($conn_id, $handle, $filepath, FTP_ASCII, 0)) {
- echo "Successfully written to ".$this->storage_dir." $file\r\n";
- } else {
- echo "There was a problem while downloading $remote_file to $local_file\r\n";
- }
- // Set the latest files
- $this->updated_files[] = $file;
- // Get filesize
- $cur_size = filesize($this->storage_dir.$file);
- // Update latest data
- $this->update_datafile($file,$cur_size,$cur_size);
- // close the file
- fclose($handle);
- }
- }
- }
- // and unlock it
- $this->unlock_process();
- // close the connection and the ftp server
- ftp_close($conn_id);
- }
- // ------------------
- // Updates our latest data file
- // Usage:
- // $this->update_datafile('datafile.txt', 0,0)
- // -------------------
- private function update_datafile($filename, $oldsize, $filesize)
- {
- $data_file = $this->storage_dir.$this->datafile;
- $handle = fopen($data_file, "rw+");
- $contents = @fread($handle, @filesize($data_file));
- rewind($handle);
- // We search the content for this file and update its size if it exists
- // pattern is: filename,filesize\n
- if(preg_match_all("/$filename,[0-9]*,[0-9]*\n/", $contents, $matches)) {
- $contents = preg_replace("/".$matches[0][0]."/", "$filename,$oldsize,$filesize\r\n", $contents);
- } else {
- $contents .= "$filename,$oldsize,$filesize\r\n";
- }
- fwrite($handle, $contents);
- fclose($handle);
- }
- // ------------------
- // Gets the information from the 'latest data' file
- // Usage:
- // $this->get_latestdata
- // -------------------
- private function get_datafile()
- {
- $return = array();
- $data_file = $this->storage_dir.$this->datafile;
- $handle = fopen($data_file, "r");
- $contents = fread($handle, filesize($data_file));
- $files = explode("\r\n",$contents);
- foreach($files as $file)
- {
- if($file != "") {
- $param = explode(",", $file);
- $return[$param[0]] = array("old_size" => $param[1], "new_size" => $param[2]);
- }
- }
- fclose($handle);
- return $return;
- }
- // ------------------
- // Adds to the $this->caught_users, making sure
- // there are no duplicates
- // Usage:
- // $this->add_caughtuser(...all required...)
- // -------------------
- public function add_caughtuser($dupe_type, $log, $account_serial, $account_name, $character_serial, $character_name, $time)
- {
- // Incriment total dupes, even if its not added to the caught users
- $this->total_dupes++;
-
- // Make sure to skip if we already have caught this user!
- if(!$this->add_to_array($account_serial, $this->account_serial)) return;
- if(!$this->add_to_array($account_name, $this->account_name)) return;
- if(!$this->add_to_array($character_serial, $this->character_serial)) return;
- if(!$this->add_to_array($character_name, $this->character_name)) return;
-
- // New user? Add it to the catch
- $this->caught_users[] = array(
- 'dupe_type' => $dupe_type,
- 'log' => $log,
- 'account_serial' => $account_serial,
- 'account_name' => $account_name,
- 'character_serial' => $character_serial,
- 'character_name' => $character_name,
- 'time' => strtotime($time)
- );
- return true;
- }
- // ------------------
- // Adds to an array only if uni uw
- // Usage:
- // $this->add_to_array(...all required...)
- // -------------------
- private function add_to_array($needle, &$haystack)
- {
- if($needle != 0 && $needle != '') {
- if(in_array($needle, $haystack)) {
- return false;
- } else {
- $haystack[] = $needle;
- return true;
- }
- } else {
- return true;
- }
- }
- // ------------------
- // Returns the array of caught users
- // Usage:
- // $this->list_caught_users()
- // -------------------
- public function list_caught_users()
- {
- return $this->caught_users;
- }
- // ------------------
- // Writes run data to a log file
- // Usage:
- // $this->lock_process
- // -------------------
- public function runLog($content)
- {
- $date = date("d-m-y_G-i");
- $fp = fopen($this->runlog_dir."$date.log", 'w');
- fwrite($fp, $content);
- fclose($fp);
- return true;
- }
- // ------------------
- // Dupe Log files
- // Usage:
- // $this->dupeLog('reward','asdoasd')
- // -------------------
- public function dupeLog($type, $content)
- {
- $date = date("d-m-y_G-i");
- $fp = fopen($this->dupelog_dir."[$date]$type.log", 'a+');
- fwrite($fp, $content);
- fwrite($fp, "\r\n");
- fclose($fp);
- return true;
- }
- // ------------------
- // Creates a locked file
- // Usage:
- // $this->lock_process
- // -------------------
- public function lock_process()
- {
- $fp = fopen('.lock', 'w');
- fwrite($fp, '1');
- fclose($fp);
- return true;
- }
- // ------------------
- // Deletes the lock file
- // Usage:
- // $this->unlock_process
- // -------------------
- public function unlock_process()
- {
- unlink('.lock');
- return true;
- }
- // ------------------
- // Returns boolean for the lock process
- // Usage:
- // $this->check_lock_process
- // -------------------
- public function check_lock_process()
- {
- if(file_exists('.lock')) {
- return true;
- } else {
- return false;
- }
- }
- }
- ?>