示例#1
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;
 }
示例#2
0
 /**
  * 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();
 }
示例#3
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);
 }
示例#4
0
 /**
  * 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]);
 }
示例#5
0
文件: lib.php 项目: jamesmcq/elis
/**
 * 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;
}
示例#6
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();
 }