Merge branch 'wip-mdl-48190-m27' of https://github.com/rajeshtaneja/moodle into MOODL...
[moodle.git] / lib / filestorage / mbz_packer.php
blobd9dfed97f197235367024bd9770f4202537ce36a
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.
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/>.
17 /**
18 * Implementation of .mbz packer.
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.
24 * Internally this packer works by wrapping the existing .zip/.tar.gz packers.
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.
30 * @package core_files
31 * @copyright 2013 The Open University
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 defined('MOODLE_INTERNAL') || die();
37 require_once("$CFG->libdir/filestorage/file_packer.php");
39 /**
40 * Utility class - handles all packing/unpacking of .mbz files.
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
47 class mbz_packer extends file_packer {
48 /**
49 * Archive files and store the result in file storage.
51 * Any existing file at that location will be overwritten.
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
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);
75 /**
76 * Archive files and store the result in an OS file.
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
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);
91 /**
92 * Extract file to given file path (real OS filesystem), existing files are overwritten.
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
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);
108 * Extract file to given file path (real OS filesystem), existing files are overwritten.
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
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);
130 * Returns array of info about all files in archive.
132 * @param string|stored_file $archivefile
133 * @return array of file infos
135 public function list_files($archivefile) {
136 return $this->get_packer_for_read_operation($archivefile)->list_files($archivefile);
140 * Selects appropriate packer for new archive depending on system option
141 * and whether required extension is available.
143 * @return file_packer Suitable packer
145 protected function get_packer_for_archive_operation() {
146 global $CFG;
147 require_once($CFG->dirroot . '/lib/filestorage/tgz_packer.php');
149 if ($CFG->enabletgzbackups) {
150 return get_file_packer('application/x-gzip');
151 } else {
152 return get_file_packer('application/zip');
157 * Selects appropriate packer for existing archive depending on file contents.
159 * @param string|stored_file $archivefile full pathname of zip file or stored_file instance
160 * @return file_packer Suitable packer
162 protected function get_packer_for_read_operation($archivefile) {
163 global $CFG;
164 require_once($CFG->dirroot . '/lib/filestorage/tgz_packer.php');
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');