\PHPZip\Zip\File\Zip::$temp = function() { return "./tempFile_" . rand(100000, 999999);}; $zip = new \PHPZip\Zip\File\Zip(); // $zip = new Zip(); // Archive comments don't really support utf-8. Some tools detect and read it though. $zip->setComment("Example Zip file.\nCreated on " . date('l jS \of F Y h:i:s A')); $zip->addFile("Hello World!", "hello.txt"); @$handle = opendir($fileDir); if ($handle) { /* This is the correct way to loop over the directory. */ while (false !== ($file = readdir($handle))) { if (strpos($file, ".php") !== false) { $pathData = pathinfo($fileDir . $file); $fileName = $pathData['filename']; $zip->addFile(file_get_contents($fileDir . $file), $file, filectime($fileDir . $file), NULL, TRUE, Zip::getFileExtAttr($file)); } } } // Uses my Lipsum generator from https://github.com/Grandt/PHPLipsumGenerator if(file_exists('./LipsumGenerator.php')) { require_once './LipsumGenerator.php'; $lg = new com\grandt\php\LipsumGenerator(); $zip->openStream("big one3.txt"); for ($i = 1 ; $i <= 20 ; $i++) { $zip->addStreamData("Chapter $i\r\n\r\n" . $lg->generate(300, 2500) . "\r\n"); } $zip->closeStream(); } $zip->sendZip("ZipExample3.zip", "application/zip", "ZipExample3.zip");
public function scanner($check_session = true, $show_results = true) { // Start scanning process error_reporting(0); ini_set('memory_limit', '256M'); if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { define(DIRSEP, '\\'); } else { define(DIRSEP, '/'); } // Skip the 2nd scan process $lockFile = $this->tmp_dir . 'scan.lock'; if (file_exists($lockFile) && time() - filemtime($lockFile) < 60 * 5) { $error_msg = 'Another Scanning Process in the memory. Exit.'; if (self::$debug) { self::DebugLog($error_msg); } exit; } register_shutdown_function('self::AntivirusFileLock'); $lockFp = fopen($lockFile, 'w'); // Register any shutdown of the script register_shutdown_function('self::AntivirusFinished'); $error_msg = 'Start Scan Process ver. ' . $this->antivirus_version . ' [scanner ver. ' . self::$scanner_version . ']'; if (self::$debug) { self::DebugLog($error_msg, true); } // Load extra settings if (file_exists($this->work_dir . 'settings.php')) { $error_msg = '=> Extra settings loaded'; if (self::$debug) { self::DebugLog($error_msg); } require_once $this->work_dir . 'settings.php'; if (count($avp_settings)) { foreach ($avp_settings as $k => $v) { $v_txt = $v; if ($v === false) { $v_txt = 'BOOL: false'; } if ($v === true) { $v_txt = 'BOOL: true'; } $error_msg = 'Setting Value: ' . strtoupper($k) . ' = ' . $v_txt; if (self::$debug) { self::DebugLog($error_msg); } if (strtolower($v) == 'false') { $v = false; } if (strtolower($v) == 'true') { $v = true; } define(strtoupper($k), $v); } } } // Analyze of exclude folders if (file_exists($this->work_dir . 'exclude_folders.php')) { $error_msg = '=> Exclude folders file loaded'; if (self::$debug) { self::DebugLog($error_msg); } require_once $this->work_dir . 'exclude_folders.php'; } $tmp_result = set_time_limit(7200); $error_msg = 'Change Time limit: ' . self::$bool_list[intval($tmp_result)] . ' , Value: ' . ini_get('max_execution_time'); if (self::$debug) { self::DebugLog($error_msg); } $error_msg = 'Current Memory limit: ' . ini_get('memory_limit'); if (self::$debug) { self::DebugLog($error_msg); } $error_msg = 'OS info: ' . PHP_OS . ' (' . php_uname() . ')'; if (self::$debug) { self::DebugLog($error_msg); } $error_msg = 'PHP ver: ' . PHP_VERSION; if (self::$debug) { self::DebugLog($error_msg); } unlink($this->tmp_dir . 'flag_terminated.tmp'); unlink($this->tmp_dir . 'filelist.txt'); /*if (!class_exists("HTTPClient")) { include_once($this->work_dir.'HttpClient.class.php'); } $HTTPClient = new HTTPClient();*/ // Some Init data $membership = $this->membership; $scan_path = $this->scan_path; $access_key = $this->access_key; $domain = $this->domain; $email = $this->email; $session_report_key = $this->session_report_key; // Some logs $error_msg = 'Domain: ' . $domain; if (self::$debug) { self::DebugLog($error_msg); } $error_msg = 'Scan path: ' . $scan_path; if (self::$debug) { self::DebugLog($error_msg); } $error_msg = 'Session report key: ' . $session_report_key; if (self::$debug) { self::DebugLog($error_msg); } $error_msg = 'Report URL: https://www.siteguarding.com/antivirus/viewreport?report_id=' . $session_report_key; if (self::$debug) { self::DebugLog($error_msg); } $error_msg = 'TMP folder: ' . $this->tmp_dir; if (self::$debug) { self::DebugLog($error_msg); } if (trim($domain) == '') { $error_msg = 'Domain is empty. Please contact SiteGuarding.com support.'; echo $error_msg; if (self::$debug) { self::DebugLog($error_msg); } exit; } if (trim($session_report_key) == '') { $error_msg = 'Session key is empty. Please contact SiteGuarding.com support.'; echo $error_msg; if (self::$debug) { self::DebugLog($error_msg); } exit; } if (trim($scan_path) == '') { $error_msg = 'Scan Path is empty. Please contact SiteGuarding.com support.'; echo $error_msg; if (self::$debug) { self::DebugLog($error_msg); } exit; } //session_start(); $current_task = 0; $total_tasks = 0; $total_tasks += 1; // Analyze what way to use for packing $total_tasks += 1; // Pack files $total_tasks += 1; // Send files $total_tasks += 1; // Get report /** * Analyze what way to use for packing */ $ssh_flag = false; if (function_exists('exec')) { // Pack files with ssh $ssh_flag = true; } if (defined('SETTINGS_ONLY_ZIP') && SETTINGS_ONLY_ZIP) { $ssh_flag = false; } // Update progress $current_task += 1; self::UpdateProgressValue($current_task, $total_tasks, 'Initialization.'); if (self::$debug) { self::DebugLog('line'); } $files_list = array(); if (defined('DEBUG_FILELIST') && DEBUG_FILELIST) { self::DebugFile($this->work_dir, true); } $error_msg = 'Collecting info about the files [METHOD 2]'; if (self::$debug) { self::DebugLog($error_msg); } $exclude_folders_real = array(); if (count($exclude_folders)) { foreach ($exclude_folders as $k => $ex_folder) { $ex_folder = $scan_path . trim($ex_folder); $exclude_folders_real[$k] = trim(str_replace(DIRSEP . DIRSEP, DIRSEP, $ex_folder)); } } else { $exclude_folders_real = array(); } $this->exclude_folders_real = $exclude_folders_real; $error_msg = 'Excluded Folders: ' . count($exclude_folders_real); if (self::$debug) { self::DebugLog($error_msg); } $error_msg = print_r($exclude_folders_real, true); if (self::$debug && count($exclude_folders_real) > 0) { self::DebugLog($error_msg); } $dirList = array(); $dirList[] = $scan_path; // Scan all dirs while (true) { $dirList = array_merge(self::ScanFolder(array_shift($dirList), $files_list), $dirList); if (count($dirList) < 1) { break; } } $error_msg = 'Save collected file_list'; if (self::$debug) { self::DebugLog($error_msg); } $collected_filelist = $this->tmp_dir . 'filelist.txt'; $fp = fopen($collected_filelist, 'w'); $status = fwrite($fp, implode("\n", $files_list)); fclose($fp); if ($status === false) { $error_msg = 'Cant save information about the collected files ' . $collected_filelist; if (self::$debug) { self::DebugLog($error_msg); } // Turn ZIP mode $ssh_flag = false; } $error_msg = 'Total files: ' . count($files_list); if (self::$debug) { self::DebugLog($error_msg); } if (self::$debug) { self::DebugLog('line'); } if ($ssh_flag) { // SSH way $error_msg = 'Start - Pack with SSH'; if (self::$debug) { self::DebugLog($error_msg); } $cmd = 'cd ' . $scan_path . '' . "\n" . 'tar -czf ' . $this->tmp_dir . 'pack.tar -T ' . $collected_filelist; $output = array(); $result = exec($cmd, $output); if (file_exists($this->tmp_dir . 'pack.tar') === false) { $ssh_flag = false; $error_msg = 'Change pack method from SSH to PHP (ZipArchive)'; if (self::$debug) { self::DebugLog($error_msg); } } } if (!$ssh_flag) { // PHP way $error_msg = 'Start - Pack with ZipArchive'; if (self::$debug) { self::DebugLog($error_msg); } $file_zip = $this->tmp_dir . 'pack.zip'; if (file_exists($file_zip)) { unlink($file_zip); } $pack_dir = $scan_path; if (class_exists('ZipArchive')) { // open archive $zip = new ZipArchive(); if ($zip->open($file_zip, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) === TRUE) { foreach ($files_list as $file_name) { $file_name = $this->scan_path . $file_name; if (strstr(realpath($file_name), "stark") == FALSE) { $short_key = str_replace($scan_path, "", $file_name); $s = $zip->addFile(realpath($file_name), $short_key); if (!$s) { $error_msg = 'Couldnt add file: ' . $file_name; if (self::$debug) { self::DebugLog($error_msg); } } } } // close and save archive $zip->close(); //$result['msg'][] = 'Archive created successfully'; } else { $error_msg = 'Error: Couldnt open ZIP archive.'; echo $error_msg; self::UpdateProgressValue($current_task, $total_tasks, $error_msg); if (self::$debug) { self::DebugLog($error_msg); } exit; } } else { $error_msg = 'Error: ZipArchive class is not exist.'; if (self::$debug) { self::DebugLog($error_msg); } } $error_msg = 'ZipArchive method - finished'; if (self::$debug) { self::DebugLog($error_msg); } // Check if zip file exists if (!file_exists($file_zip)) { $error_msg = 'Error: zip file is not exists. Use OwnZipClass'; if (self::$debug) { self::DebugLog($error_msg); } $error_msg = 'OwnZipClass method - started'; if (self::$debug) { self::DebugLog($error_msg); } $zip = new Zip(); $zip->setZipFile($file_zip); foreach ($files_list as $file_name_short) { $file_name = trim($this->scan_path . $file_name_short); $handle = fopen($file_name, "r"); if (filesize($file_name) > 0) { $zip->addFile(fread($handle, filesize($file_name)), $file_name_short, filectime($file_name), NULL, TRUE, Zip::getFileExtAttr($file_name)); } fclose($handle); } $zip->finalize(); $error_msg = 'OwnZipClass method - finished'; if (self::$debug) { self::DebugLog($error_msg); } $ssh_flag = false; } } // Update progress $current_task += 1; self::UpdateProgressValue($current_task, $total_tasks, 'Collecting information about the files.'); /** * Send files to SG server */ if ($ssh_flag) { $archive_filename = $this->tmp_dir . "pack.tar"; $archive_format = 'tar'; } else { $archive_filename = $this->tmp_dir . "pack.zip"; $archive_format = 'zip'; } $error_msg = 'Pack file: ' . $archive_filename; if (self::$debug) { self::DebugLog($error_msg); } // Check if pack file is exist if (file_exists($archive_filename) === false) { $error_msg = 'Error: Pack file is not exist. Probably not enough space on the server.'; if (self::$debug) { self::DebugLog($error_msg); } echo $error_msg; exit; } $tar_size = filesize($archive_filename); $error_msg = 'Pack file is ' . round($tar_size / 1024 / 1024, 2) . 'Mb'; if (self::$debug) { self::DebugLog($error_msg); } if (self::$debug) { self::DebugLog('line'); } $error_msg = 'Start - Send Packed files to SG server'; if (self::$debug) { self::DebugLog($error_msg); } $archive_file_url = "/" . str_replace($this->scan_path, "", $this->tmp_dir) . 'pack.' . $archive_format; $archive_file_url = str_replace("\\", "/", $archive_file_url); $error_msg = 'Pack URL: ' . $archive_file_url; if (self::$debug) { self::DebugLog($error_msg); } if ($tar_size < 32 * 1024 * 1024 || $membership == 'pro') { // Send file $post_data = base64_encode(json_encode(array('domain' => $domain, 'access_key' => $access_key, 'email' => $email, 'session_report_key' => $session_report_key, 'archive_format' => $archive_format, 'archive_file_url' => $archive_file_url))); $flag_CallBack = false; if (defined('CALLBACK_PACK_FILE') && CALLBACK_PACK_FILE) { // Callback option $flag_CallBack = true; } else { $result = self::UploadSingleFile($archive_filename, 'uploadfiles_ver2', $post_data); if ($result === false) { $error_msg = 'Can not upload pack file for analyze'; if (self::$debug) { self::DebugLog($error_msg); } $flag_CallBack = true; } else { $error_msg = 'Pack file sent for analyze - OK'; if (self::$debug) { self::DebugLog($error_msg); } $flag_CallBack = false; } } // CallBack method if ($flag_CallBack) { $error_msg = 'Start to use CallBack method'; if (self::$debug) { self::DebugLog($error_msg); } $post_data = base64_encode(json_encode(array('domain' => $domain, 'access_key' => $access_key, 'email' => $email, 'session_report_key' => $session_report_key, 'archive_format' => $archive_format, 'archive_file_url' => $archive_file_url))); $result = self::UploadSingleFile_Callback($post_data); if ($result === false) { $error_msg = 'CallBack method - failed'; if (self::$debug) { self::DebugLog($error_msg); } $error_msg = 'Can not upload pack file for analyze'; if (self::$debug) { self::DebugLog($error_msg); } echo $error_msg; exit; } else { $error_msg = 'CallBack method - OK'; if (self::$debug) { self::DebugLog($error_msg); } } } } else { $error_msg = 'Pack file is too big (' . $error_msg . '), please contact SiteGuarding.com support or upgrade to PRO version.'; if (self::$debug) { self::DebugLog($error_msg); } echo $error_msg; exit; } // Update progress $current_task += 1; self::UpdateProgressValue($current_task, $total_tasks, 'Analyzing the files. Preparing the report.'); if (self::$debug) { self::DebugLog('line'); } /** * Check and Get report from SG server */ $error_msg = 'Start - Report generating'; if (self::$debug) { self::DebugLog($error_msg); } for ($i = 1; $i <= 10 * 60; $i++) { sleep(5); /*$post_data = array( 'data'=> base64_encode(json_encode(array( 'domain' => $domain, 'access_key' => $access_key, 'session_report_key' => $session_report_key))) );*/ $post_data = base64_encode(json_encode(array('domain' => $domain, 'access_key' => $access_key, 'session_report_key' => $session_report_key))); //$result_json = $HTTPClient->post(self::$SITEGUARDING_SERVER.'?action=getreport_ver2', $post_data); $link = self::$SITEGUARDING_SERVER . '?action=getreport_ver2&data=' . $post_data; $result_json = file_get_contents($link); if ($result_json === false) { $error_msg = 'Report can not be generated. Please try again or contact support'; if (self::$debug) { self::DebugLog($error_msg); } echo $error_msg; exit; } $result_json = (array) json_decode($result_json, true); //if (self::$debug) self::DebugLog(print_r($result_json, true)); if ($result_json['status'] == 'ready') { echo $result_json['text']; // Update progress $current_task += 1; self::UpdateProgressValue($current_task, $total_tasks, 'Done. Sending your report.'); $error_msg = 'Done. Sending your report by email'; if (self::$debug) { self::DebugLog($error_msg); } // Send email to user with the report $email_result = self::SendEmail($email, $result_json['text']); if ($email_result) { $error_msg = 'Report Sent - OK'; } else { $error_msg = 'Report Sent - FAILED'; } if (self::$debug) { self::DebugLog($error_msg); } exit; } } $error_msg = 'Finished [Report is not sent]' . "\n"; if (self::$debug) { self::DebugLog($error_msg); } }