1 without locale messages addon from
2 http://cvs.php.net/viewcvs.cgi/pear/HTTP_Upload/Upload.php?r1=1.42&r2=1.53&view=patch
4 --- Upload.php 2004/08/08 09:37:50 1.42
5 +++ Upload.php 2006/02/11 17:45:50 1.53
10 + * Whether the files array has already been built or not
14 + var $is_built = false;
17 * Contains the desired chmod for uploaded files
21 var $_chmod = HTTP_UPLOAD_DEFAULT_CHMOD;
24 + * Specially used if the naming mode is 'seq'
25 + * Contains file naming information
30 + var $_modeNameSeq = array(
39 * @param string $lang Language to use for reporting errors
42 function &getFiles($file = null)
44 - static $is_built = false;
45 //build only once for multiple calls
47 + if (!$this->is_built) {
48 $files = &$this->_buildFiles();
49 if (PEAR::isError($files)) {
50 // there was an error with the form.
51 // Create a faked upload embedding the error
52 + $files_code = $files->getCode();
53 $this->files['_error'] = &new HTTP_Upload_File(
56 - null, $files->getCode(),
58 $this->lang, $this->_chmod);
60 $this->files = $files;
63 + $this->is_built = true;
69 // developer didn't specify this name in the form
70 // warn him about it with a faked upload
71 - return new HTTP_Upload_File(
74 - null, 'DEV_NO_DEF_FILE',
76 + $huf =& new HTTP_Upload_File(
79 + null, 'DEV_NO_DEF_FILE',
86 if (!isset($this->content_type) ||
87 strpos($this->content_type, 'multipart/form-data') !== 0)
89 - return $this->raiseError('BAD_FORM');
90 + $error =& $this->raiseError('BAD_FORM');
93 // In 4.1 $_FILES isn't initialized when no uploads
94 // XXX (cox) afaik, in >= 4.1 and <= 4.3 only
98 if (count($this->post_files) < 1) {
99 - return $this->raiseError('NO_USER_FILE');
100 + $error =& $this->raiseError('NO_USER_FILE');
103 //we also check if at least one file has more than 0 bytes :)
108 foreach ($this->post_files as $userfile => $value) {
109 if (is_array($value['name'])) {
110 foreach ($value['name'] as $key => $val) {
111 @@ -469,10 +525,12 @@
114 $size = $value['size'];
115 + $error = $value['error'];
119 - $this->raiseError('NO_USER_FILE');
120 + if ($error != 2 && $size == 0) {
121 + $error =& $this->raiseError('NO_USER_FILE');
126 @@ -525,14 +583,14 @@
128 * @see HTTP_Upload_File::setValidExtensions()
130 - var $_extensions_check = array('php', 'phtm', 'phtml', 'php3', 'inc');
131 + var $_extensionsCheck = array('php', 'phtm', 'phtml', 'php3', 'inc');
134 * @see HTTP_Upload_File::setValidExtensions()
138 - var $_extensions_mode = 'deny';
139 + var $_extensionsMode = 'deny';
142 * Contains the desired chmod for uploaded files
144 $this->HTTP_Upload_Error($lang);
147 - if (empty($name) || $size == 0) {
148 + if (empty($name) || ($error != 'TOO_LARGE' && $size == 0)) {
149 $error = 'NO_USER_FILE';
150 } elseif ($tmp == 'none') {
151 $error = 'TOO_LARGE';
154 * Sets the name of the destination file
156 - * @param string $mode A valid mode: 'uniq', 'safe' or 'real' or a file name
157 + * @param string $mode A valid mode: 'uniq', 'seq', 'safe' or 'real' or a file name
158 * @param string $prepend A string to prepend to the name
159 * @param string $append A string to append to the name
163 $name = $this->upload['real'];
166 + $this->_modeNameSeq['flag'] = true;
167 + $this->_modeNameSeq['prepend'] = $prepend;
168 + $this->_modeNameSeq['append'] = $append;
177 + * Sequence file names in the form: userGuide[1].pdf, userGuide[2].pdf ...
179 + * @param string $dir Destination directory
181 + function nameToSeq($dir)
183 + //Check if a file with the same name already exists
184 + $name = $dir . DIRECTORY_SEPARATOR . $this->upload['real'];
185 + if (!@is_file($name)) {
186 + return $this->upload['real'];
188 + //we need to strip out the extension and the '.' of the file
189 + //e.g 'userGuide.pdf' becomes 'userGuide'
190 + $baselength = strlen($this->upload['real']) - strlen($this->upload['ext']) - 1;
191 + $basename = substr( $this->upload['real'],0, $baselength );
193 + //here's the pattern we're looking for
194 + $pattern = '(\[)([[:digit:]]+)(\])$';
196 + //just incase the original filename had a sequence, we take it out
197 + // e.g: 'userGuide[3]' should become 'userGuide'
198 + $basename = ereg_replace($pattern, '', $basename);
201 + * attempt to find a unique sequence file name
206 + $filename = $basename . '[' . $i . '].' . $this->upload['ext'];
207 + $check = $dir . DIRECTORY_SEPARATOR . $filename;
208 + if (!@is_file($check)) {
217 * Unique file names in the form: 9022210413b75410c28bef.html
218 * @see HTTP_Upload_File::setName()
222 function nameToSafe($name, $maxlen=250)
224 - $noalpha = 'ÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÀÈÌÒÙàèìòùÄËÏÖÜäëïöüÿÃãÕõÅåÑñÇç@°ºª';
225 - $alpha = 'AEIOUYaeiouyAEIOUaeiouAEIOUaeiouAEIOUaeiouyAaOoAaNnCcaooa';
226 + $noalpha = 'ÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÀÈÌÒÙàèìòùÄËÏÖÜäëïöüÿÃãÕõÅåÑñÇç@°ºªÞþÆæ';
227 + $alpha = 'AEIOUYaeiouyAEIOUaeiouAEIOUaeiouAEIOUaeiouyAaOoAaNnCcaooaTtAa';
229 $name = substr($name, 0, $maxlen);
230 $name = strtr($name, $noalpha, $alpha);
231 @@ -715,47 +818,59 @@
233 * Moves the uploaded file to its destination directory.
235 - * @param string $dir_dest Destination directory
236 - * @param bool $overwrite Overwrite if destination file exists?
237 - * @return mixed True on success or Pear_Error object on error
238 + * @param string $dir Destination directory
239 + * @param bool $overwrite Overwrite if destination file exists?
240 + * @return mixed True on success or PEAR_Error object on error
243 - function moveTo($dir_dest, $overwrite = true)
244 + function moveTo($dir, $overwrite = true)
246 if (!$this->isValid()) {
247 - return $this->raiseError($this->upload['error']);
248 + $error =& $this->raiseError($this->upload['error']);
252 //Valid extensions check
253 if (!$this->_evalValidExtensions()) {
254 - return $this->raiseError('NOT_ALLOWED_EXTENSION');
255 + $error =& $this->raiseError('NOT_ALLOWED_EXTENSION');
259 - $err_code = $this->_chk_dir_dest($dir_dest);
260 + $err_code = $this->_chkDirDest($dir);
261 if ($err_code !== false) {
262 - return $this->raiseError($err_code);
263 + $error =& $this->raiseError($err_code);
266 // Use 'safe' mode by default if no other was selected
267 if (!$this->mode_name_selected) {
268 $this->setName('safe');
271 - $name_dest = $dir_dest . DIRECTORY_SEPARATOR . $this->upload['name'];
272 + //test to see if we're working with sequence naming mode
273 + if ($this->_modeNameSeq['flag'] === true) {
274 + $this->upload['name'] = $this->_modeNameSeq['prepend'] . $this->nameToSeq($dir) . $this->_modeNameSeq['append'];
277 + $name = $dir . DIRECTORY_SEPARATOR . $this->upload['name'];
279 - if (@is_file($name_dest)) {
280 + if (@is_file($name)) {
281 if ($overwrite !== true) {
282 - return $this->raiseError('FILE_EXISTS');
283 - } elseif (!is_writable($name_dest)) {
284 - return $this->raiseError('CANNOT_OVERWRITE');
285 + $error =& $this->raiseError('FILE_EXISTS');
287 + } elseif (!is_writable($name)) {
288 + $error =& $this->raiseError('CANNOT_OVERWRITE');
293 // copy the file and let php clean the tmp
294 - if (!@move_uploaded_file($this->upload['tmp_name'], $name_dest)) {
295 - return $this->raiseError('E_FAIL_MOVE');
297 - @chmod($name_dest, $this->_chmod);
298 - return $this->getProp('name');
299 + if (!@move_uploaded_file($this->upload['tmp_name'], $name)) {
300 + $error =& $this->raiseError('E_FAIL_MOVE');
303 + @chmod($name, $this->_chmod);
304 + $prop =& $this->getProp('name');
309 @@ -764,15 +879,15 @@
310 * @param string $dir_dest Destination dir
311 * @return mixed False on no errors or error code on error
313 - function _chk_dir_dest($dir_dest)
314 + function _chkDirDest($dir_dest)
317 return 'MISSING_DIR';
319 - if (!@is_dir ($dir_dest)) {
320 + if (!@is_dir($dir_dest)) {
323 - if (!is_writeable ($dir_dest)) {
324 + if (!is_writeable($dir_dest)) {
325 return 'NO_WRITE_PERMS';
330 function setValidExtensions($exts, $mode = 'deny')
332 - $this->_extensions_check = $exts;
333 - $this->_extensions_mode = $mode;
334 + $this->_extensionsCheck = $exts;
335 + $this->_extensionsMode = $mode;
341 function _evalValidExtensions()
343 - $exts = $this->_extensions_check;
344 + $exts = $this->_extensionsCheck;
345 settype($exts, 'array');
346 - if ($this->_extensions_mode == 'deny') {
347 + if ($this->_extensionsMode == 'deny') {
348 if (in_array($this->getProp('ext'), $exts)) {