PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/application/helpers/upload.php

https://github.com/fayazv/Taarifa_Web
PHP | 313 lines | 200 code | 33 blank | 80 comment | 32 complexity | fb1abc3c5ef97d8cd5d24480467aad72 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-3.0, BSD-3-Clause, 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. // Check if file is properly send.
  26. if (empty($_FILES[$file])) return;
  27. // Load file data from FILES if not passed as array
  28. $file = is_array($file) ? $file : $_FILES[$file];
  29. if ($filename === NULL)
  30. {
  31. // Use the default filename, with a timestamp pre-pended
  32. $filename = time().$file['name'];
  33. }
  34. if (Kohana::config('upload.remove_spaces') === TRUE)
  35. {
  36. // Remove spaces from the filename
  37. $filename = preg_replace('/\s+/', '_', $filename);
  38. }
  39. if ($directory === NULL)
  40. {
  41. // Use the pre-configured upload directory
  42. $directory = Kohana::config('upload.directory', TRUE);
  43. }
  44. // Make sure the directory ends with a slash
  45. $directory = rtrim($directory, '/').'/';
  46. if ( ! is_dir($directory) AND Kohana::config('upload.create_directories') === TRUE)
  47. {
  48. // Create the upload directory
  49. mkdir($directory, 0777, TRUE);
  50. }
  51. if ( ! is_writable($directory))
  52. throw new Kohana_Exception('upload.not_writable', $directory);
  53. // loop through if tmp_name returns an array
  54. if( is_array( $file['tmp_name'] ) ) {
  55. $i = 0;
  56. $filenames = array();
  57. foreach( $file['tmp_name'] as $tmp_name ) {
  58. if (is_uploaded_file($tmp_name ) AND
  59. move_uploaded_file($tmp_name, $filename =
  60. $directory.$file['name'][$i] ) )
  61. {
  62. if ($chmod !== FALSE)
  63. {
  64. // Set permissions on filename
  65. chmod( $filename, $chmod );
  66. }
  67. // Add $filename to $filenames array
  68. $filenames[] = $filename;
  69. }
  70. $i++;
  71. }
  72. // Return new file path array
  73. return $filenames;
  74. }
  75. else
  76. {
  77. if (is_uploaded_file($file['tmp_name']) AND move_uploaded_file($file['tmp_name'], $filename = $directory.$filename))
  78. {
  79. if ($chmod !== FALSE)
  80. {
  81. // Set permissions on filename
  82. chmod($filename, $chmod);
  83. }
  84. // Return new file path
  85. return $filename;
  86. }
  87. }
  88. return FALSE;
  89. }
  90. /* Validation Rules */
  91. /**
  92. * Tests if input data is valid file type, even if no upload is present.
  93. *
  94. * @param array $_FILES item
  95. * @return bool
  96. */
  97. public static function valid($file)
  98. {
  99. if (is_array($file))
  100. {
  101. // Is this a multi-upload array?
  102. if (is_array($file['name']))
  103. {
  104. for ($i=0; $i <= count($file['name']) ; $i++)
  105. {
  106. if (isset($file['error'][$i])
  107. AND isset($file['name'][$i])
  108. AND isset($file['type'][$i])
  109. AND isset($file['tmp_name'][$i])
  110. AND isset($file['size'][$i]))
  111. {
  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. for ($i=0; $i <= count($file['name']) ; $i++)
  181. {
  182. if ((int) $file['error'][$i] !== UPLOAD_ERR_OK)
  183. {
  184. return TRUE;
  185. }
  186. // Get the default extension of the file
  187. $extension = strtolower(substr(strrchr($file['name'][$i], '.'), 1));
  188. // Get the mime types for the extension
  189. $mime_types = Kohana::config('mimes.'.$extension);
  190. // Make sure there is an extension, that the extension is allowed, and that mime types exist
  191. if ( ! empty($extension) AND in_array($extension, $allowed_types) AND is_array($mime_types))
  192. {
  193. return TRUE;
  194. }
  195. else
  196. {
  197. return false;
  198. }
  199. }
  200. }
  201. // This is a single upload
  202. else
  203. {
  204. if ((int) $file['error'] !== UPLOAD_ERR_OK)
  205. return TRUE;
  206. // Get the default extension of the file
  207. $extension = strtolower(substr(strrchr($file['name'], '.'), 1));
  208. // Get the mime types for the extension
  209. $mime_types = Kohana::config('mimes.'.$extension);
  210. // Make sure there is an extension, that the extension is allowed, and that mime types exist
  211. return ( ! empty($extension) AND in_array($extension, $allowed_types) AND is_array($mime_types));
  212. }
  213. }
  214. /**
  215. * Validation rule to test if an uploaded file is allowed by file size.
  216. * File sizes are defined as: SB, where S is the size (1, 15, 300, etc) and
  217. * B is the byte modifier: (B)ytes, (K)ilobytes, (M)egabytes, (G)igabytes.
  218. * Eg: to limit the size to 1MB or less, you would use "1M".
  219. *
  220. * @param array $_FILES item
  221. * @param array maximum file size
  222. * @return bool
  223. */
  224. public static function size(array $file, array $size)
  225. {
  226. if (is_array($file['name']))
  227. {
  228. for ($i=0; $i <= count($file['name']) ; $i++)
  229. {
  230. if ((int) $file['error'][$i] !== UPLOAD_ERR_OK)
  231. {
  232. return TRUE;
  233. }
  234. // Only one size is allowed
  235. $size = strtoupper($size[0]);
  236. if ( ! preg_match('/[0-9]++[BKMG]/', $size))
  237. {
  238. return FALSE;
  239. }
  240. // Make the size into a power of 1024
  241. switch (substr($size, -1))
  242. {
  243. case 'G': $size = intval($size) * pow(1024, 3); break;
  244. case 'M': $size = intval($size) * pow(1024, 2); break;
  245. case 'K': $size = intval($size) * pow(1024, 1); break;
  246. default: $size = intval($size); break;
  247. }
  248. // Test that the file is under or equal to the max size
  249. if ($file['size'][$i] <= $size)
  250. {
  251. return true;
  252. }
  253. else
  254. {
  255. return false;
  256. }
  257. }
  258. }
  259. // This is a single upload
  260. else
  261. {
  262. if ((int) $file['error'] !== UPLOAD_ERR_OK)
  263. return TRUE;
  264. // Only one size is allowed
  265. $size = strtoupper($size[0]);
  266. if ( ! preg_match('/[0-9]++[BKMG]/', $size))
  267. return FALSE;
  268. // Make the size into a power of 1024
  269. switch (substr($size, -1))
  270. {
  271. case 'G': $size = intval($size) * pow(1024, 3); break;
  272. case 'M': $size = intval($size) * pow(1024, 2); break;
  273. case 'K': $size = intval($size) * pow(1024, 1); break;
  274. default: $size = intval($size); break;
  275. }
  276. // Test that the file is under or equal to the max size
  277. return ($file['size'] <= $size);
  278. }
  279. }
  280. } // End upload