PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/application/helpers/upload.php

http://github.com/ushahidi/Ushahidi_Web
PHP | 318 lines | 201 code | 34 blank | 83 comment | 36 complexity | 10a9191e38607e27c11ece93e5b9dc12 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Upload helper class for working with the global $_FILES
  4. * array and Validation library.
  5. *
  6. * $Id: upload.php 3264 2008-09-23 19:03:14Z David Kobia $
  7. *
  8. * @package Core
  9. * @author Kohana Team
  10. * @copyright (c) 2007-2008 Kohana Team
  11. * @license http://kohanaphp.com/license.html
  12. */
  13. class upload_Core {
  14. /**
  15. * Save an uploaded file to a new location.
  16. *
  17. * @param mixed name of $_FILE input or array of upload data
  18. * @param string new filename
  19. * @param string new directory
  20. * @param integer chmod mask
  21. * @return string full path to new file
  22. */
  23. public static function save($file, $filename = NULL, $directory = NULL, $chmod = 0644)
  24. {
  25. // Load file data from FILES if not passed as array
  26. $file = is_array($file) ? $file : $_FILES[$file];
  27. if ($filename === NULL)
  28. {
  29. // Use the default filename, with a timestamp pre-pended
  30. $filename = time().(is_array($file['name']) ? $file['name'][0] : $file['name']);
  31. }
  32. if (Kohana::config('upload.remove_spaces') === TRUE)
  33. {
  34. // Remove spaces from the filename
  35. $filename = preg_replace('/\s+/', '_', $filename);
  36. }
  37. if ($directory === NULL)
  38. {
  39. // Use the pre-configured upload directory
  40. $directory = Kohana::config('upload.directory', TRUE);
  41. }
  42. // Make sure the directory ends with a slash
  43. $directory = rtrim($directory, '/').'/';
  44. if ( ! is_dir($directory) AND Kohana::config('upload.create_directories') === TRUE)
  45. {
  46. // Create the upload directory
  47. mkdir($directory, 0777, TRUE);
  48. }
  49. if ( ! is_writable($directory))
  50. throw new Kohana_Exception('upload.not_writable', $directory);
  51. // loop through if tmp_name returns an array
  52. if( is_array( $file['tmp_name'] ) ) {
  53. $i = 0;
  54. $filenames = array();
  55. foreach( $file['tmp_name'] as $tmp_name ) {
  56. if (is_uploaded_file($tmp_name ) AND
  57. move_uploaded_file($tmp_name, $filename =
  58. $directory.$file['name'][$i] ) )
  59. {
  60. if ($chmod !== FALSE)
  61. {
  62. // Set permissions on filename
  63. chmod( $filename, $chmod );
  64. }
  65. // Add $filename to $filenames array
  66. $filenames[] = $filename;
  67. }
  68. $i++;
  69. }
  70. // Return new file path array
  71. return $filenames;
  72. }
  73. else
  74. {
  75. if (is_uploaded_file($file['tmp_name']) AND move_uploaded_file($file['tmp_name'], $filename = $directory.$filename))
  76. {
  77. if ($chmod !== FALSE)
  78. {
  79. // Set permissions on filename
  80. chmod($filename, $chmod);
  81. }
  82. // Return new file path
  83. return $filename;
  84. }
  85. }
  86. return FALSE;
  87. }
  88. /* Validation Rules */
  89. /**
  90. * Tests if input data is valid file type, even if no upload is present.
  91. *
  92. * @param array $_FILES item
  93. * @return bool
  94. */
  95. public static function valid($file)
  96. {
  97. if (is_array($file))
  98. {
  99. // Is this a multi-upload array?
  100. if (is_array($file['name']))
  101. {
  102. $filesCount = count($file['name'])-1;
  103. for ($i=0; $i <= $filesCount ; $i++)
  104. {
  105. if (isset($file['error'][$i])
  106. AND isset($file['name'][$i])
  107. AND isset($file['type'][$i])
  108. AND isset($file['tmp_name'][$i])
  109. AND isset($file['size'][$i]))
  110. {
  111. if($filesCount == $i)
  112. return true;
  113. }
  114. else
  115. {
  116. return false;
  117. }
  118. }
  119. }
  120. // No - this is a single upload
  121. else
  122. {
  123. return (isset($file['error'])
  124. AND isset($file['name'])
  125. AND isset($file['type'])
  126. AND isset($file['tmp_name'])
  127. AND isset($file['size']));
  128. }
  129. }
  130. else
  131. {
  132. return false;
  133. }
  134. }
  135. /**
  136. * Tests if input data has valid upload data.
  137. *
  138. * @param array $_FILES item
  139. * @return bool
  140. */
  141. public static function required(array $file)
  142. {
  143. if (is_array($file['name']))
  144. {
  145. for ($i=0; $i <= count($file['name']) ; $i++)
  146. {
  147. if (isset($file['tmp_name'][$i])
  148. AND isset($file['error'][$i])
  149. AND is_uploaded_file($file['tmp_name'][$i])
  150. AND (int) $file['error'][$i] === UPLOAD_ERR_OK)
  151. {
  152. return true;
  153. }
  154. else
  155. {
  156. return false;
  157. }
  158. }
  159. }
  160. // This is a single upload
  161. else
  162. {
  163. return (isset($file['tmp_name'])
  164. AND isset($file['error'])
  165. AND is_uploaded_file($file['tmp_name'])
  166. AND (int) $file['error'] === UPLOAD_ERR_OK);
  167. }
  168. }
  169. /**
  170. * Validation rule to test if an uploaded file is allowed by extension.
  171. *
  172. * @param array $_FILES item
  173. * @param array allowed file extensions
  174. * @return bool
  175. */
  176. public static function type(array $file, array $allowed_types)
  177. {
  178. if (is_array($file['name']))
  179. {
  180. $filesCount = count($file['name'])-1;
  181. for ($i=0; $i <= $filesCount ; $i++)
  182. {
  183. if ((int) $file['error'][$i] !== UPLOAD_ERR_OK)
  184. {
  185. return TRUE;
  186. }
  187. // Get the default extension of the file
  188. $extension = strtolower(pathinfo($file['name'][$i], PATHINFO_EXTENSION));
  189. // Get the mime types for the extension
  190. $mime_types = Kohana::config('mimes.'.$extension);
  191. // Make sure there is an extension, that the extension is allowed, and that mime types exist
  192. if ( ! empty($extension) AND in_array($extension, $allowed_types) AND is_array($mime_types))
  193. {
  194. if($filesCount == $i)
  195. return TRUE;
  196. }
  197. else
  198. {
  199. return false;
  200. }
  201. }
  202. }
  203. // This is a single upload
  204. else
  205. {
  206. if ((int) $file['error'] !== UPLOAD_ERR_OK)
  207. return TRUE;
  208. // Get the default extension of the file
  209. $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
  210. // Get the mime types for the extension
  211. $mime_types = Kohana::config('mimes.'.$extension);
  212. // Make sure there is an extension, that the extension is allowed, and that mime types exist
  213. return ( ! empty($extension) AND in_array($extension, $allowed_types) AND is_array($mime_types));
  214. }
  215. }
  216. /**
  217. * Validation rule to test if an uploaded file is allowed by file size.
  218. * File sizes are defined as: SB, where S is the size (1, 15, 300, etc) and
  219. * B is the byte modifier: (B)ytes, (K)ilobytes, (M)egabytes, (G)igabytes.
  220. * Eg: to limit the size to 1MB or less, you would use "1M".
  221. *
  222. * @param array $_FILES item
  223. * @param array maximum file size
  224. * @return bool
  225. */
  226. public static function size(array $file, array $size)
  227. {
  228. if (is_array($file['name']))
  229. {
  230. // Only one size is allowed
  231. $size = strtoupper($size[0]);
  232. /*if ( ! preg_match('/[0-9]++[BKMG]/', $size))
  233. {
  234. return FALSE;
  235. }*/
  236. // Make the size into a power of 1024
  237. switch (substr($size, -1))
  238. {
  239. case 'G': $size = intval($size) * pow(1024, 3); break;
  240. case 'M': $size = intval($size) * pow(1024, 2); break;
  241. case 'K': $size = intval($size) * pow(1024, 1); break;
  242. default: $size = intval($size); break;
  243. }
  244. $filesCount = count($file['name'])-1;
  245. for ($i=0; $i <= $filesCount ; $i++)
  246. {
  247. if ((int) $file['error'][$i] !== UPLOAD_ERR_OK)
  248. {
  249. return TRUE;
  250. }
  251. // Test that the file is under or equal to the max size
  252. if ($file['size'][$i] <= $size)
  253. {
  254. if($filesCount == $i)
  255. return true;
  256. }
  257. else
  258. {
  259. return false;
  260. }
  261. }
  262. }
  263. // This is a single upload
  264. else
  265. {
  266. if ((int) $file['error'] !== UPLOAD_ERR_OK)
  267. return TRUE;
  268. // Only one size is allowed
  269. $size = strtoupper($size[0]);
  270. if ( ! preg_match('/[0-9]++[BKMG]/', $size))
  271. return FALSE;
  272. // Make the size into a power of 1024
  273. switch (substr($size, -1))
  274. {
  275. case 'G': $size = intval($size) * pow(1024, 3); break;
  276. case 'M': $size = intval($size) * pow(1024, 2); break;
  277. case 'K': $size = intval($size) * pow(1024, 1); break;
  278. default: $size = intval($size); break;
  279. }
  280. // Test that the file is under or equal to the max size
  281. return ($file['size'] <= $size);
  282. }
  283. }
  284. } // End upload