Beispiel #1
0
 /**
  * Perform archiving file from file path.
  *
  * @param zip_archive $ziparch zip archive instance
  * @param string $archivepath file path to archive
  * @param string $file path name of the file
  * @param file_progress $progress Progress indicator callback or null if not required
  * @return bool success
  */
 private function archive_pathname($ziparch, $archivepath, $file, file_progress $progress = null)
 {
     // Record progress each time this function is called.
     if ($progress) {
         $progress->progress();
     }
     if (!file_exists($file)) {
         return false;
     }
     if (is_file($file)) {
         if (!is_readable($file)) {
             return false;
         }
         return $ziparch->add_file_from_pathname($archivepath, $file);
     }
     if (is_dir($file)) {
         if ($archivepath !== '') {
             $ziparch->add_directory($archivepath);
         }
         $files = new DirectoryIterator($file);
         foreach ($files as $file) {
             if ($file->isDot()) {
                 continue;
             }
             $newpath = $archivepath . '/' . $file->getFilename();
             $this->archive_pathname($ziparch, $newpath, $file->getPathname(), $progress);
         }
         unset($files);
         // Release file handles.
         return true;
     }
 }
 public function test_close_archive()
 {
     global $CFG;
     $this->resetAfterTest(true);
     $archive = "{$CFG->tempdir}/archive.zip";
     $textfile = "{$CFG->tempdir}/textfile.txt";
     touch($textfile);
     $this->assertFileNotExists($archive);
     $this->assertFileExists($textfile);
     // Create archive and close it without files.
     // (returns true, without any warning).
     $zip_archive = new zip_archive();
     $result = $zip_archive->open($archive, file_archive::CREATE);
     $this->assertTrue($result);
     $result = $zip_archive->close();
     $this->assertTrue($result);
     unlink($archive);
     // Create archive and close it with files.
     // (returns true, without any warning).
     $zip_archive = new zip_archive();
     $result = $zip_archive->open($archive, file_archive::CREATE);
     $this->assertTrue($result);
     $result = $zip_archive->add_file_from_string('test.txt', 'test');
     $this->assertTrue($result);
     $result = $zip_archive->add_file_from_pathname('test2.txt', $textfile);
     $result = $zip_archive->close();
     $this->assertTrue($result);
     unlink($archive);
     // Create archive and close if forcing error.
     // (returns true for old PHP versions and
     // false with warnings for new PHP versions). MDL-51863.
     $zip_archive = new zip_archive();
     $result = $zip_archive->open($archive, file_archive::CREATE);
     $this->assertTrue($result);
     $result = $zip_archive->add_file_from_string('test.txt', 'test');
     $this->assertTrue($result);
     $result = $zip_archive->add_file_from_pathname('test2.txt', $textfile);
     $this->assertTrue($result);
     // Delete the file before closing does force close() to fail.
     unlink($textfile);
     // Behavior is different between old PHP versions and new ones. Let's detect it.
     $result = false;
     try {
         // Old PHP versions were not printing any warning.
         $result = $zip_archive->close();
     } catch (Exception $e) {
         // New PHP versions print PHP Warning.
         $this->assertInstanceOf('PHPUnit_Framework_Error_Warning', $e);
         $this->assertContains('ZipArchive::close', $e->getMessage());
     }
     // This is crazy, but it shows how some PHP versions do return true.
     try {
         // And some PHP versions do return correctly false (5.4.25, 5.6.14...)
         $this->assertFalse($result);
     } catch (Exception $e) {
         // But others do insist into returning true (5.6.13...). Only can accept them.
         $this->assertInstanceOf('PHPUnit_Framework_ExpectationFailedException', $e);
         $this->assertTrue($result);
     }
     $this->assertFileNotExists($archive);
 }
 /**
  * @depends test_extract_to_storage
  */
 public function test_add_files()
 {
     global $CFG;
     $this->resetAfterTest(false);
     $packer = get_file_packer('application/zip');
     $archive = "{$CFG->tempdir}/archive.zip";
     $this->assertFileNotExists($archive);
     $packer->archive_to_pathname(array(), $archive);
     $this->assertFileExists($archive);
     $zip_archive = new zip_archive();
     $zip_archive->open($archive, file_archive::OPEN);
     $this->assertEquals(0, $zip_archive->count());
     $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->add_directory('test2');
     $zip_archive->close();
     $zip_archive->open($archive, file_archive::OPEN);
     $files = $zip_archive->list_files();
     $this->assertCount(2, $files);
     $this->assertEquals('test.txt', $files[0]->pathname);
     $this->assertEquals('test2/', $files[1]->pathname);
     $result = $zip_archive->add_file_from_pathname('test.txt', __DIR__ . '/nonexistent/file.txt');
     $this->assertFalse($result);
     $zip_archive->close();
     $zip_archive->open($archive, file_archive::OPEN);
     $this->assertEquals(2, $zip_archive->count());
     $zip_archive->close();
     unlink($archive);
 }
Beispiel #4
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;
}
Beispiel #5
0
 /**
  * Perform archiving file from file path
  *
  * @param zip_archive $ziparch zip archive instance
  * @param string $archivepath file path to archive
  * @param string $file path name of the file
  * @return bool success
  */
 private function archive_pathname($ziparch, $archivepath, $file)
 {
     if (!file_exists($file)) {
         return false;
     }
     if (is_file($file)) {
         if (!is_readable($file)) {
             return false;
         }
         return $ziparch->add_file_from_pathname($archivepath, $file);
     }
     if (is_dir($file)) {
         if ($archivepath !== '') {
             $ziparch->add_directory($archivepath);
         }
         $files = new DirectoryIterator($file);
         foreach ($files as $file) {
             if ($file->isDot()) {
                 continue;
             }
             $newpath = $archivepath . '/' . $file->getFilename();
             $this->archive_pathname($ziparch, $newpath, $file->getPathname());
         }
         unset($files);
         //release file handles
         return true;
     }
 }