[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/filestorage/ -> mbz_packer.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Implementation of .mbz packer.
  19   *
  20   * This packer supports .mbz files which can be either .zip or .tar.gz format
  21   * internally. A suitable format is chosen depending on system option when
  22   * creating new files.
  23   *
  24   * Internally this packer works by wrapping the existing .zip/.tar.gz packers.
  25   *
  26   * Backup filenames do not contain non-ASCII characters so packers that do not
  27   * support UTF-8 (like the current .tar.gz packer, and possibly external zip
  28   * software in some cases if used) can be used by this packer.
  29   *
  30   * @package core_files
  31   * @copyright 2013 The Open University
  32   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  
  35  defined('MOODLE_INTERNAL') || die();
  36  
  37  require_once("$CFG->libdir/filestorage/file_packer.php");
  38  
  39  /**
  40   * Utility class - handles all packing/unpacking of .mbz files.
  41   *
  42   * @package core_files
  43   * @category files
  44   * @copyright 2013 The Open University
  45   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   */
  47  class mbz_packer extends file_packer {
  48      /**
  49       * Archive files and store the result in file storage.
  50       *
  51       * Any existing file at that location will be overwritten.
  52       *
  53       * @param array $files array from archive path => pathname or stored_file
  54       * @param int $contextid context ID
  55       * @param string $component component
  56       * @param string $filearea file area
  57       * @param int $itemid item ID
  58       * @param string $filepath file path
  59       * @param string $filename file name
  60       * @param int $userid user ID
  61       * @param bool $ignoreinvalidfiles true means ignore missing or invalid files, false means abort on any error
  62       * @param file_progress $progress Progress indicator callback or null if not required
  63       * @return stored_file|bool false if error stored_file instance if ok
  64       * @throws file_exception If file operations fail
  65       * @throws coding_exception If any archive paths do not meet the restrictions
  66       */
  67      public function archive_to_storage(array $files, $contextid,
  68              $component, $filearea, $itemid, $filepath, $filename,
  69              $userid = null, $ignoreinvalidfiles = true, file_progress $progress = null) {
  70          return $this->get_packer_for_archive_operation()->archive_to_storage($files,
  71                  $contextid, $component, $filearea, $itemid, $filepath, $filename,
  72                  $userid, $ignoreinvalidfiles, $progress);
  73      }
  74  
  75      /**
  76       * Archive files and store the result in an OS file.
  77       *
  78       * @param array $files array from archive path => pathname or stored_file
  79       * @param string $archivefile path to target zip file
  80       * @param bool $ignoreinvalidfiles true means ignore missing or invalid files, false means abort on any error
  81       * @param file_progress $progress Progress indicator callback or null if not required
  82       * @return bool true if file created, false if not
  83       * @throws coding_exception If any archive paths do not meet the restrictions
  84       */
  85      public function archive_to_pathname(array $files, $archivefile,
  86              $ignoreinvalidfiles=true, file_progress $progress = null) {
  87          return $this->get_packer_for_archive_operation()->archive_to_pathname($files,
  88                  $archivefile, $ignoreinvalidfiles, $progress);
  89      }
  90  
  91      /**
  92       * Extract file to given file path (real OS filesystem), existing files are overwritten.
  93       *
  94       * @param stored_file|string $archivefile full pathname of zip file or stored_file instance
  95       * @param string $pathname target directory
  96       * @param array $onlyfiles only extract files present in the array
  97       * @param file_progress $progress Progress indicator callback or null if not required
  98       * @return array list of processed files (name=>true)
  99       * @throws moodle_exception If error
 100       */
 101      public function extract_to_pathname($archivefile, $pathname,
 102              array $onlyfiles = null, file_progress $progress = null) {
 103          return $this->get_packer_for_read_operation($archivefile)->extract_to_pathname(
 104                  $archivefile, $pathname, $onlyfiles, $progress);
 105      }
 106  
 107      /**
 108       * Extract file to given file path (real OS filesystem), existing files are overwritten.
 109       *
 110       * @param string|stored_file $archivefile full pathname of zip file or stored_file instance
 111       * @param int $contextid context ID
 112       * @param string $component component
 113       * @param string $filearea file area
 114       * @param int $itemid item ID
 115       * @param string $pathbase file path
 116       * @param int $userid user ID
 117       * @param file_progress $progress Progress indicator callback or null if not required
 118       * @return array list of processed files (name=>true)
 119       * @throws moodle_exception If error
 120       */
 121      public function extract_to_storage($archivefile, $contextid,
 122              $component, $filearea, $itemid, $pathbase, $userid = null,
 123              file_progress $progress = null) {
 124          return $this->get_packer_for_read_operation($archivefile)->extract_to_storage(
 125                  $archivefile, $contextid, $component, $filearea, $itemid, $pathbase,
 126                  $userid, $progress);
 127      }
 128  
 129      /**
 130       * Returns array of info about all files in archive.
 131       *
 132       * @param string|stored_file $archivefile
 133       * @return array of file infos
 134       */
 135      public function list_files($archivefile) {
 136          return $this->get_packer_for_read_operation($archivefile)->list_files($archivefile);
 137      }
 138  
 139      /**
 140       * Selects appropriate packer for new archive depending on system option
 141       * and whether required extension is available.
 142       *
 143       * @return file_packer Suitable packer
 144       */
 145      protected function get_packer_for_archive_operation() {
 146          global $CFG;
 147          require_once($CFG->dirroot . '/lib/filestorage/tgz_packer.php');
 148  
 149          if ($CFG->enabletgzbackups) {
 150              return get_file_packer('application/x-gzip');
 151          } else {
 152              return get_file_packer('application/zip');
 153          }
 154      }
 155  
 156      /**
 157       * Selects appropriate packer for existing archive depending on file contents.
 158       *
 159       * @param string|stored_file $archivefile full pathname of zip file or stored_file instance
 160       * @return file_packer Suitable packer
 161       */
 162      protected function get_packer_for_read_operation($archivefile) {
 163          global $CFG;
 164          require_once($CFG->dirroot . '/lib/filestorage/tgz_packer.php');
 165  
 166          if (tgz_packer::is_tgz_file($archivefile)) {
 167              return get_file_packer('application/x-gzip');
 168          } else {
 169              return get_file_packer('application/zip');
 170          }
 171      }
 172  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1