Development

/branches/1.3/lib/plugins/sfCompat10Plugin/lib/request/sfRequestCompat10.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/plugins/sfCompat10Plugin/lib/request/sfRequestCompat10.class.php

Revision 12222, 12.3 kB (checked in by fabien, 6 years ago)

[1.2] fixed sfRequestCompat10 (closes #4695)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 class sfRequestCompat10
4 {
5   /**
6    * Adds 1.0 compatibility methods to the request object.
7    *
8    * @param sfEvent $event The event
9    *
10    * @return Boolean true if the method has been found here, false otherwise
11    */
12   static public function call(sfEvent $event)
13   {
14     if (!in_array($event['method'], array(
15       'getError', 'getErrorNames', 'getErrors', 'hasError', 'hasErrors', 'removeError', 'setError', 'setErrors',
16       'getFile', 'getFileError', 'getFileName', 'getFileNames', 'getFilePath', 'getFileSize',
17       'getFileType', 'hasFile', 'hasFileError', 'hasFileErrors', 'hasFiles', 'getFileValue', 'getFileValues',
18       'getFileExtension', 'moveFile'
19     )))
20     {
21       return false;
22     }
23
24     $event->setReturnValue(call_user_func_array(array('sfRequestCompat10', $event['method']), array_merge(array($event->getSubject()), $event['arguments'])));
25
26     return true;
27   }
28
29   /**
30    * Retrieves an error message.
31    *
32    * @param  sfRequest $request A request object
33    * @param  string $name  An error name
34    *
35    * @return string An error message, if the error exists, otherwise null
36    */
37   static public function getError($request, $name)
38   {
39     return $request->getAttribute('errors['.$name.']');
40   }
41
42   /**
43    * Retrieves an array of error names.
44    *
45    * @param  sfRequest $request A request object
46    *
47    * @return array An indexed array of error names
48    */
49   static public function getErrorNames($request)
50   {
51     return array_keys($request->getAttribute('errors', array()));
52   }
53
54   /**
55    * Retrieves an array of errors.
56    *
57    * @param  sfRequest $request A request object
58    *
59    * @return array An associative array of errors
60    */
61   static public function getErrors($request)
62   {
63     return $request->getAttribute('errors', array());
64   }
65
66   /**
67    * Indicates whether or not an error exists.
68    *
69    * @param  sfRequest $request A request object
70    * @param  string $name  An error name
71    *
72    * @return bool true, if the error exists, otherwise false
73    */
74   static public function hasError($request, $name)
75   {
76     return $request->hasAttribute('errors['.$name.']');
77   }
78
79   /**
80    * Indicates whether or not any errors exist.
81    *
82    * @param  sfRequest $request A request object
83    *
84    * @return bool true, if any error exist, otherwise false
85    */
86   static public function hasErrors($request)
87   {
88     return count($request->getAttribute('errors', array())) > 0;
89   }
90
91   /**
92    * Removes an error.
93    *
94    * @param  sfRequest $request A request object
95    * @param  string $name  An error name
96    *
97    * @return string An error message, if the error was removed, otherwise null
98    */
99   static public function removeError($request, $name)
100   {
101     return $request->getAttributeHolder()->remove('errors['.$name.']');
102   }
103
104   /**
105    * Sets an error.
106    *
107    * @param  sfRequest $request A request object
108    * @param string $name     An error name
109    * @param string $message  An error message
110    *
111    */
112   static public function setError($request, $name, $message)
113   {
114     $errors = $request->getAttribute('errors', array());
115     $errors[$name] = $message;
116
117     $request->setAttribute('errors', $errors);
118   }
119
120   /**
121    * Sets an array of errors
122    *
123    * If an existing error name matches any of the keys in the supplied
124    * array, the associated message will be overridden.
125    *
126    * @param  sfRequest $request A request object
127    * @param array $erros An associative array of errors and their associated messages
128    *
129    */
130   static public function setErrors($request, $errors)
131   {
132     $request->setAttribute('errors', array_merge($request->getAttribute('errors', array()), $errors));
133   }
134
135   /**
136    * Retrieves an array of file information.
137    *
138    * @param  sfRequest $request A request object
139    * @param  string $name  A file name
140    *
141    * @return array An associative array of file information, if the file exists, otherwise null
142    */
143   static public function getFile($request, $name)
144   {
145     return self::hasFile($request, $name) ? self::getFileValues($request, $name) : null;
146   }
147
148   /**
149    * Retrieves a file error.
150    *
151    * @param  sfRequest $request A request object
152    * @param  string $name  A file name
153    *
154    * @return int One of the following error codes:
155    *
156    *             - <b>UPLOAD_ERR_OK</b>        (no error)
157    *             - <b>UPLOAD_ERR_INI_SIZE</b>  (the uploaded file exceeds the
158    *                                           upload_max_filesize directive
159    *                                           in php.ini)
160    *             - <b>UPLOAD_ERR_FORM_SIZE</b> (the uploaded file exceeds the
161    *                                           MAX_FILE_SIZE directive that
162    *                                           was specified in the HTML form)
163    *             - <b>UPLOAD_ERR_PARTIAL</b>   (the uploaded file was only
164    *                                           partially uploaded)
165    *             - <b>UPLOAD_ERR_NO_FILE</b>   (no file was uploaded)
166    */
167   static public function getFileError($request, $name)
168   {
169     return self::hasFile($request, $name) ? self::getFileValue($request, $name, 'error') : UPLOAD_ERR_NO_FILE;
170   }
171
172   /**
173    * Retrieves a file name.
174    *
175    * @param  sfRequest $request A request object
176    * @param  string $name  A file nam.
177    *
178    * @return string A file name, if the file exists, otherwise null
179    */
180   static public function getFileName($request, $name)
181   {
182     return self::hasFile($request, $name) ? self::getFileValue($request, $name, 'name') : null;
183   }
184
185   /**
186    * Retrieves an array of file names.
187    *
188    * @param  sfRequest $request A request object
189    *
190    * @return array An indexed array of file names
191    */
192   static public function getFileNames($request)
193   {
194     return array_keys($_FILES);
195   }
196
197   /**
198    * Retrieves a file path.
199    *
200    * @param  sfRequest $request A request object
201    * @param  string $name  A file name
202    *
203    * @return string A file path, if the file exists, otherwise null
204    */
205   static public function getFilePath($request, $name)
206   {
207     return self::hasFile($request, $name) ? self::getFileValue($request, $name, 'tmp_name') : null;
208   }
209
210   /**
211    * Retrieve a file size.
212    *
213    * @param  sfRequest $request A request object
214    * @param  string $name  A file name
215    *
216    * @return int A file size, if the file exists, otherwise null
217    */
218   static public function getFileSize($request, $name)
219   {
220     return self::hasFile($request, $name) ? self::getFileValue($request, $name, 'size') : null;
221   }
222
223   /**
224    * Retrieves a file type.
225    *
226    * This may not be accurate. This is the mime-type sent by the browser
227    * during the upload.
228    *
229    * @param  sfRequest $request A request object
230    * @param  string $name  A file name
231    *
232    * @return string A file type, if the file exists, otherwise null
233    */
234   static public function getFileType($request, $name)
235   {
236     return self::hasFile($request, $name) ? self::getFileValue($request, $name, 'type') : null;
237   }
238
239   /**
240    * Indicates whether or not a file exists.
241    *
242    * @param  sfRequest $request A request object
243    * @param  string $name  A file name
244    *
245    * @return bool true, if the file exists, otherwise false
246    */
247   static public function hasFile($request, $name)
248   {
249     if (preg_match('/^(.+?)\[(.+?)\]$/', $name, $match))
250     {
251       return isset($_FILES[$match[1]]['name'][$match[2]]);
252     }
253     else
254     {
255       return isset($_FILES[$name]);
256     }
257   }
258
259   /**
260    * Indicates whether or not a file error exists.
261    *
262    * @param  sfRequest $request A request object
263    * @param  string $name  A file name
264    *
265    * @return bool true, if the file error exists, otherwise false
266    */
267   static public function hasFileError($request, $name)
268   {
269     return self::hasFile($request, $name) ? (self::getFileValue($request, $name, 'error') != UPLOAD_ERR_OK) : false;
270   }
271
272   /**
273    * Indicates whether or not any file errors occured.
274    *
275    * @param  sfRequest $request A request object
276    *
277    * @return bool true, if any file errors occured, otherwise false
278    */
279   static public function hasFileErrors($request)
280   {
281     foreach (self::getFileNames($request) as $name)
282     {
283       if (self::hasFileError($request, $name) === true)
284       {
285         return true;
286       }
287     }
288
289     return false;
290   }
291
292   /**
293    * Indicates whether or not any files exist.
294    *
295    * @param  sfRequest $request A request object
296    *
297    * @return boolean true, if any files exist, otherwise false
298    */
299   static public function hasFiles($request)
300   {
301     return (count($_FILES) > 0);
302   }
303
304   /**
305    * Retrieves a file value.
306    *
307    * @param  sfRequest $request A request object
308    * @param string $name A file name
309    * @param string $key Value to search in the file
310    *
311    * @return string File value
312    */
313   static public function getFileValue($request, $name, $key)
314   {
315     if (preg_match('/^(.+?)\[(.+?)\]$/', $name, $match))
316     {
317       return $_FILES[$match[1]][$key][$match[2]];
318     }
319     else
320     {
321       return $_FILES[$name][$key];
322     }
323   }
324
325   /**
326    * Retrieves all the values from a file.
327    *
328    * @param  sfRequest $request A request object
329    * @param  string $name  A file name
330    *
331    * @return array Associative list of the file values
332    */
333   static public function getFileValues($request, $name)
334   {
335     if (preg_match('/^(.+?)\[(.+?)\]$/', $name, $match))
336     {
337       return array(
338         'name'     => $_FILES[$match[1]]['name'][$match[2]],
339         'type'     => $_FILES[$match[1]]['type'][$match[2]],
340         'tmp_name' => $_FILES[$match[1]]['tmp_name'][$match[2]],
341         'error'    => $_FILES[$match[1]]['error'][$match[2]],
342         'size'     => $_FILES[$match[1]]['size'][$match[2]],
343       );
344     }
345     else
346     {
347       return $_FILES[$name];
348     }
349   }
350
351   /**
352    * Retrieves an extension for a given file.
353    *
354    * @param  sfRequest $request A request object
355    * @param  string $name  A file name
356    *
357    * @return string Extension for the file
358    */
359   static public function getFileExtension($request, $name)
360   {
361     static $mimeTypes = null;
362
363     $fileType = self::getFileType($request, $name);
364
365     if (!$fileType)
366     {
367       return '.bin';
368     }
369
370     if (is_null($mimeTypes))
371     {
372       $mimeTypes = unserialize(file_get_contents(sfConfig::get('sf_symfony_lib_dir').'/plugins/sfCompat10Plugin/data/mime_types.dat'));
373     }
374
375     return isset($mimeTypes[$fileType]) ? '.'.$mimeTypes[$fileType] : '.bin';
376   }
377
378   /**
379    * Moves an uploaded file.
380    *
381    * @param  sfRequest $request A request object
382    * @param string $name      A file name
383    * @param string $file      An absolute filesystem path to where you would like the
384    *                          file moved. This includes the new filename as well, since
385    *                          uploaded files are stored with random names
386    * @param int    $fileMode  The octal mode to use for the new file
387    * @param bool   $create    Indicates that we should make the directory before moving the file
388    * @param int    $dirMode   The octal mode to use when creating the directory
389    *
390    * @return bool true, if the file was moved, otherwise false
391    *
392    * @throws <b>sfFileException</b> If a major error occurs while attempting to move the file
393    */
394   static public function moveFile($request, $name, $file, $fileMode = 0666, $create = true, $dirMode = 0777)
395   {
396     if (self::hasFile($request, $name) && self::getFileValue($request, $name, 'error') == UPLOAD_ERR_OK && self::getFileValue($request, $name, 'size') > 0)
397     {
398       // get our directory path from the destination filename
399       $directory = dirname($file);
400
401       if (!is_readable($directory))
402       {
403         $fmode = 0777;
404
405         if ($create && !@mkdir($directory, $dirMode, true))
406         {
407           // failed to create the directory
408           throw new sfFileException(sprintf('Failed to create file upload directory "%s".', $directory));
409         }
410
411         // chmod the directory since it doesn't seem to work on
412         // recursive paths
413         @chmod($directory, $dirMode);
414       }
415       else if (!is_dir($directory))
416       {
417         // the directory path exists but it's not a directory
418         throw new sfFileException(sprintf('File upload path "%s" exists, but is not a directory.', $directory));
419       }
420       else if (!is_writable($directory))
421       {
422         // the directory isn't writable
423         throw new sfFileException(sprintf('File upload path "%s" is not writable.', $directory));
424       }
425
426       if (@move_uploaded_file(self::getFileValue($request, $name, 'tmp_name'), $file))
427       {
428         // chmod our file
429         @chmod($file, $fileMode);
430
431         return true;
432       }
433     }
434
435     return false;
436   }
437 }
438
Note: See TracBrowser for help on using the browser.