forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_archive.php
200 lines (166 loc) · 5.56 KB
/
file_archive.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Abstraction of general file archives.
*
* @package moodlecore
* @subpackage file-packer
* @copyright 2008 Petr Skoda (http://skodak.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class file_archive implements Iterator {
/** Open archive if exists, fail if does not exist. */
const OPEN = 1;
/** Open archive if exists, create if does not. */
const CREATE = 2;
/** Always create new archive */
const OVERWRITE = 4;
/** Encoding of file names - windows usually expects DOS single-byte charset*/
protected $encoding = 'utf-8';
/**
* Open or create archive (depending on $mode)
* @param string $archivepathname
* @param int $mode OPEN, CREATE or OVERWRITE constant
* @param string $encoding archive local paths encoding
* @return bool success
*/
public abstract function open($archivepathname, $mode=file_archive::CREATE, $encoding='utf-8');
/**
* Close archive
* @return bool success
*/
public abstract function close();
/**
* Returns file stream for reading of content
* @param int $index of file
* @return stream or false if error
*/
public abstract function get_stream($index);
/**
* Returns file information
* @param int $index of file
* @return info object or false if error
*/
public abstract function get_info($index);
/**
* Returns array of info about all files in archive
* @return array of file infos
*/
public abstract function list_files();
/**
* Returns number of files in archive
* @return int number of files
*/
public abstract function count();
/**
* Add file into archive
* @param string $localname name of file in archive
* @param string $pathname location of file
* @return bool success
*/
public abstract function add_file_from_pathname($localname, $pathname);
/**
* Add content of string into archive
* @param string $localname name of file in archive
* @param string $contents
* @return bool success
*/
public abstract function add_file_from_string($localname, $contents);
/**
* Add empty directory into archive
* @param string $local
* @return bool success
*/
public abstract function add_directory($localname);
/**
* Tries to convert $localname into another encoding,
* please note that it may fail really badly.
* @param strin $localname in utf-8 encoding
* @return string
*/
protected function mangle_pathname($localname) {
if ($this->encoding === 'utf-8') {
return $localname;
}
$textlib = textlib_get_instance();
$converted = $textlib->convert($localname, 'utf-8', $this->encoding);
$original = $textlib->convert($converted, $this->encoding, 'utf-8');
if ($original === $localname) {
$result = $converted;
} else {
// try ascci conversion
$converted2 = $textlib->specialtoascii($localname);
$converted2 = $textlib->convert($converted2, 'utf-8', $this->encoding);
$original2 = $textlib->convert($converted, $this->encoding, 'utf-8');
if ($original2 === $localname) {
//this looks much better
$result = $converted2;
} else {
//bad luck - the file name may not be usable at all
$result = $converted;
}
}
$result = preg_replace('/\.\.+/', '', $result);
$result = ltrim($result); // no leadin /
if ($result === '.') {
$result = '';
}
return $result;
}
/**
* Tries to convert $localname into utf-8
* please note that it may fail really badly.
* The resulting file name is cleaned.
*
* @param strin $localname in another encoding
* @return string in utf-8
*/
protected function unmangle_pathname($localname) {
if ($this->encoding === 'utf-8') {
return $localname;
}
$textlib = textlib_get_instance();
$result = $textlib->convert($localname, $this->encoding, 'utf-8');
$result = clean_param($result, PARAM_PATH);
$result = ltrim($result); // no leadin /
return $result;
}
/**
* Returns current file info
* @return object
*/
//public abstract function current();
/**
* Returns the index of current file
* @return int current file index
*/
//public abstract function key();
/**
* Moves forward to next file
* @return void
*/
//public abstract function next();
/**
* Rewinds back to the first file
* @return void
*/
//public abstract function rewind();
/**
* Did we reach the end?
* @return boolean
*/
//public abstract function valid();
}