Exemple #1
0
 /**
  * @depends test_get_packer
  */
 public function test_list_files()
 {
     $this->resetAfterTest(false);
     $files = array(__DIR__ . '/fixtures/test_moodle_22.zip', __DIR__ . '/fixtures/test_moodle.zip', __DIR__ . '/fixtures/test_tc_8.zip', __DIR__ . '/fixtures/test_7zip_927.zip', __DIR__ . '/fixtures/test_winzip_165.zip', __DIR__ . '/fixtures/test_winrar_421.zip');
     if (function_exists('normalizer_normalize')) {
         // Unfortunately there is no way to standardise UTF-8 strings without INTL extension
         $files[] = __DIR__ . '/fixtures/test_infozip_3.zip';
         $files[] = __DIR__ . '/fixtures/test_osx_1074.zip';
     }
     $packer = get_file_packer('application/zip');
     foreach ($files as $archive) {
         $archivefiles = $packer->list_files($archive);
         $this->assertTrue(is_array($archivefiles), "Archive not extracted properly: " . basename($archive) . ' ');
         $this->assertTrue(count($this->files) === count($archivefiles) or count($this->files) === count($archivefiles) - 1);
         // Some zippers create empty dirs.
         foreach ($archivefiles as $file) {
             if ($file->pathname === 'Žluťoučký/') {
                 // Some zippers create empty dirs.
                 continue;
             }
             $this->assertArrayHasKey($file->pathname, $this->files, "File {$file->pathname} not extracted properly: " . basename($archive) . ' ');
         }
     }
     // Windows packer supports only DOS encoding
     $archive = __DIR__ . '/fixtures/test_win8_de.zip';
     $archivefiles = $packer->list_files($archive);
     $this->assertTrue(is_array($archivefiles), "Archive not extracted properly: " . basename($archive) . ' ');
     $this->assertEquals(2, count($archivefiles));
     foreach ($archivefiles as $file) {
         $this->assertTrue($file->pathname === 'Prüfung.txt' or $file->pathname === 'test.test');
     }
     $zip_archive = new zip_archive();
     $zip_archive->open(__DIR__ . '/fixtures/test_win8_cz.zip', file_archive::OPEN, 'cp852');
     $archivefiles = $zip_archive->list_files();
     $this->assertTrue(is_array($archivefiles), "Archive not extracted properly: " . basename($archive) . ' ');
     $this->assertEquals(3, count($archivefiles));
     foreach ($archivefiles as $file) {
         $this->assertTrue($file->pathname === 'Žluťoučký/Koníček.txt' or $file->pathname === 'testíček.txt' or $file->pathname === 'test.test');
     }
     $zip_archive->close();
 }
Exemple #2
0
 /**
  * Returns array of info about all files in archive.
  *
  * @param string|file_archive $archivefile
  * @return array of file infos
  */
 public function list_files($archivefile)
 {
     if (!is_string($archivefile)) {
         return $archivefile->list_files();
     }
     $ziparch = new zip_archive();
     if (!$ziparch->open($archivefile, file_archive::OPEN)) {
         return false;
     }
     $list = $ziparch->list_files();
     $ziparch->close();
     return $list;
 }
 /**
  * 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();
 }
 /**
  * @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);
 }
 /**
  * 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]);
 }
Exemple #6
0
/**
 * Compress logs for emailing
 *
 * @param string $plugin The plugin for which we are sending logs
 * @param array $logids The list of database record ids pointing to log files
 * @param boolean $manual True if manual, false if scheduled
 * @return string The name of the appropriate zip file
 */
function rlip_compress_logs_email($plugin, $logids, $manual = false)
{
    global $CFG, $DB;
    require_once $CFG->libdir . '/filestorage/zip_archive.php';
    if (empty($logids)) {
        // Nothing to compress.
        return false;
    }
    // Set up the archive.
    $archive_name = rlip_email_archive_name($plugin, 0, $manual);
    $path = $CFG->dataroot . '/' . $archive_name;
    $archive = new zip_archive();
    $result = $archive->open($path, file_archive::CREATE);
    // SQL fragments to get the logs.
    list($sql, $params) = $DB->get_in_or_equal($logids);
    $select = "id {$sql}";
    // Add files from log records, tracking whether a valid log path was found.
    $found = false;
    if ($records = $DB->get_records_select(RLIP_LOG_TABLE, $select, $params)) {
        foreach ($records as $record) {
            if ($record->logpath != NULL) {
                $archive->add_file_from_pathname(basename($record->logpath), $record->logpath);
                // Have at least one file in the zip.
                $found = true;
            }
        }
    }
    $archive->close();
    if (!$found) {
        // No logs, so delete the empty archive file and signal that we don't need to send the email.
        if (file_exists($path)) {
            @unlink($path);
        }
        return false;
    }
    return $archive_name;
}
Exemple #7
0
$centralend = zip_archive::zip_get_central_end($fp, $filesize);
if ($centralend === false) {
    cli_error("This is not a ZIP archive: {$archive}");
}
if ($centralend['disk'] !== 0 or $centralend['disk_start'] !== 0) {
    cli_error("Multi-disk archives are not supported: {$archive}");
}
if ($centralend['offset'] === 4294967295.0) {
    cli_error("ZIP64 archives are not supported: {$archive}");
}
fseek($fp, $centralend['offset']);
$data = fread($fp, $centralend['size']);
$pos = 0;
$files = array();
for ($i = 0; $i < $centralend['entries']; $i++) {
    $file = zip_archive::zip_parse_file_header($data, $centralend, $pos);
    if ($file === false) {
        cli_error('Invalid Zip file header structure: ' . $archive);
    }
    // Read local file header.
    fseek($fp, $file['local_offset']);
    $localfile = unpack('Vsig/vversion_req/vgeneral/vmethod/Vmodified/Vcrc/Vsize_compressed/Vsize/vname_length/vextra_length', fread($fp, 30));
    if ($localfile['sig'] !== 0x4034b50) {
        // Borked file!
        $file['error'] = 'Invalid local file signature';
        $files[] = $file;
        continue;
    }
    if ($localfile['name_length']) {
        $localfile['name'] = fread($fp, $localfile['name_length']);
    } else {
Exemple #8
0
 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);
         }
     }
 }