]> git.pld-linux.org Git - packages/php-pear-HTTP_Upload.git/blob - php-pear-HTTP_Upload-cvs.patch
- tabs in preamble
[packages/php-pear-HTTP_Upload.git] / php-pear-HTTP_Upload-cvs.patch
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
3
4 --- Upload.php  2004/08/08 09:37:50     1.42
5 +++ Upload.php  2006/02/11 17:45:50     1.53
6 @@ -280,6 +311,13 @@
7      var $files = array();
8      
9      /**
10 +     * Whether the files array has already been built or not
11 +     * @var int
12 +     * @access private
13 +     */
14 +    var $is_built = false;
15 +
16 +    /**
17       * Contains the desired chmod for uploaded files
18       * @var int
19       * @access private
20 @@ -287,6 +325,19 @@
21      var $_chmod = HTTP_UPLOAD_DEFAULT_CHMOD;
22  
23      /**
24 +     * Specially used if the naming mode is 'seq'
25 +        * Contains file naming information
26 +     * 
27 +     * @var array
28 +     * @access private
29 +     */
30 +    var $_modeNameSeq = array(
31 +        'flag' => false,
32 +        'prepend' => '',
33 +        'append' => '',
34 +    ); 
35 +
36 +    /**
37       * Constructor
38       *
39       * @param string $lang Language to use for reporting errors
40 @@ -328,22 +379,22 @@
41       */
42      function &getFiles($file = null)
43      {
44 -        static $is_built = false;
45          //build only once for multiple calls
46 -        if (!$is_built) {
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(
54                                                         '_error', null,
55                                                         null, null,
56 -                                                       null, $files->getCode(),
57 +                                                       null, $files_code,
58                                                         $this->lang, $this->_chmod);
59              } else {
60                  $this->files = $files;
61              }
62 -            $is_built = true;
63 +            $this->is_built = true;
64          }
65          if ($file !== null) {
66              if (is_int($file)) {
67 @@ -362,11 +413,12 @@
68              } else {
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(
72 -                                           '_error', null,
73 -                                           null, null,
74 -                                           null, 'DEV_NO_DEF_FILE',
75 -                                           $this->lang);
76 +                $huf =&  new HTTP_Upload_File(
77 +                                             '_error', null,
78 +                                             null, null,
79 +                                             null, 'DEV_NO_DEF_FILE',
80 +                                             $this->lang);
81 +                return $huf;
82              }
83          }
84          return $this->files;
85 @@ -383,7 +435,8 @@
86          if (!isset($this->content_type) ||
87              strpos($this->content_type, 'multipart/form-data') !== 0)
88          {
89 -            return $this->raiseError('BAD_FORM');
90 +            $error =& $this->raiseError('BAD_FORM');
91 +            return $error;
92          }
93          // In 4.1 $_FILES isn't initialized when no uploads
94          // XXX (cox) afaik, in >= 4.1 and <= 4.3 only
95 @@ -457,11 +510,14 @@
96      function isMissing()
97      {
98          if (count($this->post_files) < 1) {
99 -            return $this->raiseError('NO_USER_FILE');
100 +            $error =& $this->raiseError('NO_USER_FILE');
101 +            return $error;
102          }
103          //we also check if at least one file has more than 0 bytes :)
104          $files = array();
105          $size = 0;
106 +        $error = null;
107 +
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 @@
112                  }
113              } else {  //one file
114                  $size = $value['size'];
115 +                $error = $value['error'];
116              }
117          }
118 -        if ($size == 0) {
119 -            $this->raiseError('NO_USER_FILE');
120 +        if ($error != 2 && $size == 0) {
121 +            $error =& $this->raiseError('NO_USER_FILE');
122 +            return $error;
123          }
124          return false;
125      }
126 @@ -525,14 +583,14 @@
127       * @access private
128       * @see HTTP_Upload_File::setValidExtensions()
129       */
130 -    var $_extensions_check = array('php', 'phtm', 'phtml', 'php3', 'inc');
131 +    var $_extensionsCheck = array('php', 'phtm', 'phtml', 'php3', 'inc');
132  
133      /**
134       * @see HTTP_Upload_File::setValidExtensions()
135       * @var string
136       * @access private
137       */
138 -    var $_extensions_mode  = 'deny';
139 +    var $_extensionsMode  = 'deny';
140  
141      /**
142       * Contains the desired chmod for uploaded files
143 @@ -560,7 +618,7 @@
144          $this->HTTP_Upload_Error($lang);
145          $ext = null;
146  
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';
152 @@ -602,7 +660,7 @@
153      /**
154       * Sets the name of the destination file
155       *
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
160       *
161 @@ -628,6 +686,11 @@
162              case 'real':
163                  $name = $this->upload['real'];
164                  break;
165 +            case 'seq':
166 +                $this->_modeNameSeq['flag'] = true;
167 +                $this->_modeNameSeq['prepend'] = $prepend;
168 +                $this->_modeNameSeq['append'] = $append;
169 +                break;
170              default:
171                  $name = $mode;
172          }
173 @@ -637,6 +700,46 @@
174      }
175  
176      /**
177 +     * Sequence file names in the form: userGuide[1].pdf, userGuide[2].pdf ...
178 +     *
179 +     * @param string $dir  Destination directory
180 +     */
181 +    function nameToSeq($dir)
182 +    {
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'];
187 +        } else {
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 );
192 +
193 +            //here's the pattern we're looking for
194 +            $pattern = '(\[)([[:digit:]]+)(\])$';
195 +
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);
199 +               
200 +            /*
201 +             * attempt to find a unique sequence file name
202 +             */
203 +            $i = 1;
204 +               
205 +            while (true) {
206 +                $filename = $basename . '[' . $i . '].' . $this->upload['ext'];
207 +                $check = $dir . DIRECTORY_SEPARATOR . $filename;
208 +                if (!@is_file($check)) {
209 +                    return $filename;
210 +                }
211 +                $i++;
212 +            }
213 +        }
214 +    }
215 +
216 +    /**
217       * Unique file names in the form: 9022210413b75410c28bef.html
218       * @see HTTP_Upload_File::setName()
219       */
220 @@ -660,8 +763,8 @@
221       */
222      function nameToSafe($name, $maxlen=250)
223      {
224 -        $noalpha = 'ÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÀÈÌÒÙàèìòùÄËÏÖÜäëïöüÿÃãÕõÅåÑñÇç@°ºª';
225 -        $alpha   = 'AEIOUYaeiouyAEIOUaeiouAEIOUaeiouAEIOUaeiouyAaOoAaNnCcaooa';
226 +        $noalpha = 'ÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÀÈÌÒÙàèìòùÄËÏÖÜäëïöüÿÃãÕõÅåÑñÇç@°ºªÞþÆæ';
227 +        $alpha   = 'AEIOUYaeiouyAEIOUaeiouAEIOUaeiouAEIOUaeiouyAaOoAaNnCcaooaTtAa';
228  
229          $name = substr($name, 0, $maxlen);
230          $name = strtr($name, $noalpha, $alpha);
231 @@ -715,47 +818,59 @@
232      /**
233       * Moves the uploaded file to its destination directory.
234       *
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
241       * @access public
242       */
243 -    function moveTo($dir_dest, $overwrite = true)
244 +    function moveTo($dir, $overwrite = true)
245      {
246          if (!$this->isValid()) {
247 -            return $this->raiseError($this->upload['error']);
248 +            $error =& $this->raiseError($this->upload['error']);
249 +            return $error;
250          }
251  
252          //Valid extensions check
253          if (!$this->_evalValidExtensions()) {
254 -            return $this->raiseError('NOT_ALLOWED_EXTENSION');
255 +            $error =& $this->raiseError('NOT_ALLOWED_EXTENSION');
256 +            return $error;
257          }
258  
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);
264 +            return $error;
265          }
266          // Use 'safe' mode by default if no other was selected
267          if (!$this->mode_name_selected) {
268              $this->setName('safe');
269          }
270  
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'];
275 +        }
276 +
277 +        $name = $dir . DIRECTORY_SEPARATOR . $this->upload['name'];
278  
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');
286 +                return $error;
287 +            } elseif (!is_writable($name)) {
288 +                $error =& $this->raiseError('CANNOT_OVERWRITE');
289 +                return $error;
290              }
291          }
292  
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');
296 -        }
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');
301 +            return $error; 
302 +        }
303 +        @chmod($name, $this->_chmod);
304 +        $prop =& $this->getProp('name');
305 +        return $prop;
306      }
307  
308      /**
309 @@ -764,15 +879,15 @@
310       * @param    string  $dir_dest Destination dir
311       * @return   mixed   False on no errors or error code on error
312       */
313 -    function _chk_dir_dest($dir_dest)
314 +    function _chkDirDest($dir_dest)
315      {
316          if (!$dir_dest) {
317              return 'MISSING_DIR';
318          }
319 -        if (!@is_dir ($dir_dest)) {
320 +        if (!@is_dir($dir_dest)) {
321              return 'IS_NOT_DIR';
322          }
323 -        if (!is_writeable ($dir_dest)) {
324 +        if (!is_writeable($dir_dest)) {
325              return 'NO_WRITE_PERMS';
326          }
327          return false;
328 @@ -826,8 +941,8 @@
329       */
330      function setValidExtensions($exts, $mode = 'deny')
331      {
332 -        $this->_extensions_check = $exts;
333 -        $this->_extensions_mode  = $mode;
334 +        $this->_extensionsCheck = $exts;
335 +        $this->_extensionsMode  = $mode;
336      }
337  
338      /**
339 @@ -838,9 +953,9 @@
340       */
341      function _evalValidExtensions()
342      {
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)) {
349                  return false;
350              }
This page took 0.052175 seconds and 3 git commands to generate.