PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/system/classes/kohana/upload.php

https://bitbucket.org/seyar/ari100krat.local
PHP | 213 lines | 96 code | 23 blank | 94 comment | 12 complexity | 00e9bb441ab699098bb92126c6b779aa MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php defined('SYSPATH') or die('No direct access allowed.');
  2. /**
  3. * Upload helper class for working with uploaded files and [Validate].
  4. *
  5. * $array = Validate::factory($_FILES);
  6. *
  7. * [!!] Remember to define your form with "enctype=multipart/form-data" or file
  8. * uploading will not work!
  9. *
  10. * The following configuration properties can be set:
  11. *
  12. * - [Upload::$remove_spaces]
  13. * - [Upload::$default_directory]
  14. *
  15. * @package Kohana
  16. * @category Helpers
  17. * @author Kohana Team
  18. * @copyright (c) 2007-2010 Kohana Team
  19. * @license http://kohanaframework.org/license
  20. */
  21. class Kohana_Upload {
  22. /**
  23. * @var boolean remove spaces in uploaded files
  24. */
  25. public static $remove_spaces = TRUE;
  26. /**
  27. * @var string default upload directory
  28. */
  29. public static $default_directory = 'upload';
  30. /**
  31. * Save an uploaded file to a new location. If no filename is provided,
  32. * the original filename will be used, with a unique prefix added.
  33. *
  34. * This method should be used after validating the $_FILES array:
  35. *
  36. * if ($array->check())
  37. * {
  38. * // Upload is valid, save it
  39. * Upload::save($_FILES['file']);
  40. * }
  41. *
  42. * @param array uploaded file data
  43. * @param string new filename
  44. * @param string new directory
  45. * @param integer chmod mask
  46. * @return string on success, full path to new file
  47. * @return FALSE on failure
  48. */
  49. public static function save(array $file, $filename = NULL, $directory = NULL, $chmod = 0644)
  50. {
  51. if ( ! isset($file['tmp_name']) OR ! is_uploaded_file($file['tmp_name']))
  52. {
  53. // Ignore corrupted uploads
  54. return FALSE;
  55. }
  56. if ($filename === NULL)
  57. {
  58. // Use the default filename, with a timestamp pre-pended
  59. $filename = uniqid().$file['name'];
  60. }
  61. if (Upload::$remove_spaces === TRUE)
  62. {
  63. // Remove spaces from the filename
  64. $filename = preg_replace('/\s+/', '_', $filename);
  65. }
  66. if ($directory === NULL)
  67. {
  68. // Use the pre-configured upload directory
  69. $directory = Upload::$default_directory;
  70. }
  71. if ( ! is_dir($directory) OR ! is_writable(realpath($directory)))
  72. {
  73. throw new Kohana_Exception('Directory :dir must be writable',
  74. array(':dir' => Kohana::debug_path($directory)));
  75. }
  76. // Make the filename into a complete path
  77. $filename = realpath($directory).DIRECTORY_SEPARATOR.$filename;
  78. if (move_uploaded_file($file['tmp_name'], $filename))
  79. {
  80. if ($chmod !== FALSE)
  81. {
  82. // Set permissions on filename
  83. chmod($filename, $chmod);
  84. }
  85. // Return new file path
  86. return $filename;
  87. }
  88. return FALSE;
  89. }
  90. /**
  91. * Tests if upload data is valid, even if no file was uploaded. If you
  92. * _do_ require a file to be uploaded, add the [Upload::not_empty] rule
  93. * before this rule.
  94. *
  95. * $array->rule('file', 'Upload::valid')
  96. *
  97. * @param array $_FILES item
  98. * @return bool
  99. */
  100. public static function valid($file)
  101. {
  102. return (isset($file['error'])
  103. AND isset($file['name'])
  104. AND isset($file['type'])
  105. AND isset($file['tmp_name'])
  106. AND isset($file['size']));
  107. }
  108. /**
  109. * Tests if a successful upload has been made.
  110. *
  111. * $array->rule('file', 'Upload::not_empty');
  112. *
  113. * @param array $_FILES item
  114. * @return bool
  115. */
  116. public static function not_empty(array $file)
  117. {
  118. return (isset($file['error'])
  119. AND isset($file['tmp_name'])
  120. AND $file['error'] === UPLOAD_ERR_OK
  121. AND is_uploaded_file($file['tmp_name'])
  122. );
  123. }
  124. /**
  125. * Test if an uploaded file is an allowed file type, by extension.
  126. *
  127. * $array->rule('file', 'Upload::type', array(array('jpg', 'png', 'gif')));
  128. *
  129. * @param array $_FILES item
  130. * @param array allowed file extensions
  131. * @return bool
  132. */
  133. public static function type(array $file, array $allowed)
  134. {
  135. if ($file['error'] !== UPLOAD_ERR_OK)
  136. return TRUE;
  137. $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
  138. return in_array($ext, $allowed);
  139. }
  140. /**
  141. * Validation rule to test if an uploaded file is allowed by file size.
  142. * File sizes are defined as: SB, where S is the size (1, 15, 300, etc) and
  143. * B is the byte modifier: (B)ytes, (K)ilobytes, (M)egabytes, (G)igabytes.
  144. *
  145. * $array->rule('file', 'Upload::size', array('1M'))
  146. *
  147. * @param array $_FILES item
  148. * @param string maximum file size
  149. * @return bool
  150. */
  151. public static function size(array $file, $size)
  152. {
  153. if ($file['error'] === UPLOAD_ERR_INI_SIZE)
  154. {
  155. // Upload is larger than PHP allowed size
  156. return FALSE;
  157. }
  158. if ($file['error'] !== UPLOAD_ERR_OK)
  159. {
  160. // The upload failed, no size to check
  161. return TRUE;
  162. }
  163. // Only one size is allowed
  164. $size = strtoupper(trim($size));
  165. if ( ! preg_match('/^[0-9]++[BKMG]$/', $size))
  166. {
  167. throw new Kohana_Exception('Size does not contain a digit and a byte value: :size', array(
  168. ':size' => $size,
  169. ));
  170. }
  171. // Make the size into a power of 1024
  172. switch (substr($size, -1))
  173. {
  174. case 'G':
  175. $size = intval($size) * pow(1024, 3);
  176. break;
  177. case 'M':
  178. $size = intval($size) * pow(1024, 2);
  179. break;
  180. case 'K':
  181. $size = intval($size) * pow(1024, 1);
  182. break;
  183. default:
  184. $size = intval($size);
  185. break;
  186. }
  187. // Test that the file is under or equal to the max size
  188. return ($file['size'] <= $size);
  189. }
  190. } // End upload