/** * Validate compression of log files into a zip file */ public function test_compresslogsforemail() { global $CFG, $DB; $logids = array(); for ($i = 1; $i <= 3; $i++) { // Create summary records. $filename = 'compress_log_' . $i . '.txt'; $oldpath = dirname(__FILE__) . '/other/' . $filename; $newpath = $CFG->dataroot . '/' . $filename; copy($oldpath, $newpath); $summarylog = new stdClass(); $summarylog->logpath = $newpath; $summarylog->plugin = 'dhimport_version1'; $summarylog->userid = 9999; $summarylog->targetstarttime = 0; $summarylog->starttime = 0; $summarylog->endtime = 0; $summarylog->filesuccesses = 0; $summarylog->filefailures = 0; $summarylog->storedsuccesses = 0; $summarylog->storedfailures = 0; $summarylog->statusmessage = ''; $summarylog->logpath = $newpath; $logids[] = $DB->insert_record(RLIP_LOG_TABLE, $summarylog); } $zipfilename = rlip_compress_logs_email('dhimport_version1', $logids); for ($i = 1; $i <= 3; $i++) { // Clean up copies. $filename = 'compress_log_' . $i . '.txt'; @unlink($CFG->dataroot . '/' . $filename); } // Open zip_archive and verify all logs included. $zip = new zip_archive(); $result = $zip->open($CFG->dataroot . '/' . $zipfilename, file_archive::OPEN); $this->assertTrue($result); $this->assertEquals(3, $zip->count()); $files = $zip->list_files(); // Validate zip contents. for ($i = 1; $i <= 3; $i++) { $filename = 'compress_log_' . $i . '.txt'; $found = false; foreach ($files as $file) { if ($file->pathname == $filename) { $found = true; break; } } $this->assertTrue($found); } $zip->close(); }
/** * Validate that log files are archived for a variety of import and * export plugins, for a variety of configured log paths * * @param string $plugintype One of 'import' or 'export' * @param string $plugin The import plugin to associate log files to * @param string $logfilelocation The logfilelocation setting value to use * @dataProvider importpluginprovider */ public function test_logfilesarchived($plugintype, $plugin, $logfilelocation) { global $CFG, $DB, $USER; require_once $CFG->dirroot . '/local/datahub/fileplugins/log/log.class.php'; require_once $CFG->libdir . '/filestorage/zip_archive.php'; // Clean-up any existing log & zip files. self::cleanup_log_files(); self::cleanup_zip_files(); // Set up the log path. set_config('logfilelocation', $logfilelocation, $plugin); $format = get_string('logfile_timestamp', 'local_datahub'); $USER->timezone = 99; // Create some log files to be zipped by the cron job. // Way earlier then any real existing files! $starttime = make_timestamp(1971, 1, 3); $filenames = array(); for ($i = 0; $i < 10; ++$i) { $filenames[$i] = rlip_log_file_name($plugintype, $plugin, $logfilelocation, 'user', false, $starttime + $i * 3600); // Write out a line to the logfile. $logfile = new rlip_fileplugin_log($filenames[$i]); $logfile->open(RLIP_FILE_WRITE); $logfile->write(array('test entry')); $logfile->close(); } // Call cron job that zips the specified day's log files. $zipfiles = rlip_compress_logs_cron('bogus', 0, $starttime); $this->assertTrue(!empty($zipfiles)); // Was a zip file created?. // Verify that the compressed file exists. $exists = file_exists($zipfiles[0]); $this->assertTrue($exists); // Open zip_archive and verify all logs included. $zip = new zip_archive(); $result = $zip->open($zipfiles[0]); $this->assertTrue($result); $this->assertEquals(10, $zip->count()); $zip->close(); // Verify that the log files created are gone.... for ($i = 0; $i < 10; ++$i) { $exists = file_exists($filenames[$i]); $this->assertFalse($exists); } // Validate that the zip file name corresponds to the plugin. // E.g. pugin is 'dhimport_version1' and file name starts with 'import_version1_'. $parts = explode('/', $zipfiles[0]); $this->assertStringStartsWith($plugin . '_', 'dh' . $parts[count($parts) - 1]); // Delete the test zip. @unlink($zipfiles[0]); }
/** * @depends test_add_files */ public function test_open_archive() { global $CFG; $this->resetAfterTest(true); $archive = "{$CFG->tempdir}/archive.zip"; $this->assertFileNotExists($archive); $zip_archive = new zip_archive(); $result = $zip_archive->open($archive, file_archive::OPEN); $this->assertFalse($result); $this->assertDebuggingCalled(); $zip_archive = new zip_archive(); $result = $zip_archive->open($archive, file_archive::CREATE); $this->assertTrue($result); $zip_archive->add_file_from_string('test.txt', 'test'); $zip_archive->close(); $zip_archive->open($archive, file_archive::OPEN); $this->assertEquals(1, $zip_archive->count()); $zip_archive = new zip_archive(); $result = $zip_archive->open($archive, file_archive::OVERWRITE); $this->assertTrue($result); $zip_archive->add_file_from_string('test2.txt', 'test'); $zip_archive->close(); $zip_archive->open($archive, file_archive::OPEN); $this->assertEquals(1, $zip_archive->count()); $zip_archive->close(); unlink($archive); $zip_archive = new zip_archive(); $result = $zip_archive->open($archive, file_archive::OVERWRITE); $this->assertTrue($result); $zip_archive->add_file_from_string('test2.txt', 'test'); $zip_archive->close(); $zip_archive->open($archive, file_archive::OPEN); $this->assertEquals(1, $zip_archive->count()); $zip_archive->close(); unlink($archive); }
protected function extract_files($moss = null) { global $DB; if ($moss == null) { $moss = $this->moss; } $sizelimit = $this->get_config('maxfilesize'); $fs = get_file_storage(); $contexti = $DB->get_record('context', array('instanceid' => $moss->cmid, 'contextlevel' => '70')); //$files = $fs->get_area_files(get_system_context()->id, 'plagiarism_moss', 'files', $moss->cmid, 'sortorder', false); $files = $fs->get_area_files($contexti->id, 'mod_workshop', 'submission_attachment', false, 'sortorder', false); foreach ($files as $file) { if ($file->get_filesize() > $sizelimit) { continue; } $content = $this->get_clear_utf8_content($file); if (empty($content)) { continue; } $path = $this->tempdir . $file->get_filepath() . $file->get_userid() . '/'; mkdir($path); $fullpath = $path . $file->get_filename(); $fullpath2 = $path . 'oo.java'; if (!check_dir_exists($path)) { throw new moodle_exception('errorcreatingdirectory', '', '', $path); } file_put_contents($fullpath, $content); //LKQ $filen = $file->get_filename(); $file_type = strtolower(substr($filen, strlen($filen) - 4, 4)); if ($file_type == '.zip') { $zp = new zip_archive(); $zp->open($fullpath, file_archive::OPEN); $filecount = $zp->count(); $content2 = ""; for ($i = 0; $i < $filecount; $i++) { $filename2 = $zp->get_info($i)->original_pathname; if (strtolower(substr($filename2, strlen($filename2) - 5, 5)) == '.java') { $tempfile = $zp->get_stream($i); $content2 = $content2 . fread($tempfile, 2 * 1024 * 1024); } } mtrace("\tfile " . $filecount . ' ' . strlen($content2)); file_put_contents($fullpath2, $content2); } } }