PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/upload.php

http://github.com/gallery/gallery3
PHP | 157 lines | 74 code | 22 blank | 61 comment | 11 complexity | d1d208b9eb7c096860476f63f047219b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * Upload helper class for working with the global $_FILES
  4. * array and Validation library.
  5. *
  6. * @package Kohana
  7. * @author Kohana Team
  8. * @copyright (c) 2007-2009 Kohana Team
  9. * @license http://kohanaphp.com/license
  10. */
  11. class upload_Core {
  12. /**
  13. * Save an uploaded file to a new location.
  14. *
  15. * @param mixed name of $_FILE input or array of upload data
  16. * @param string new filename
  17. * @param string new directory
  18. * @param integer chmod mask
  19. * @return string full path to new file
  20. */
  21. public static function save($file, $filename = NULL, $directory = NULL, $chmod = 0644)
  22. {
  23. // Load file data from FILES if not passed as array
  24. $file = is_array($file) ? $file : $_FILES[$file];
  25. if ($filename === NULL)
  26. {
  27. // Use the default filename, with a timestamp pre-pended
  28. $filename = time().$file['name'];
  29. }
  30. if (Kohana::config('upload.remove_spaces') === TRUE)
  31. {
  32. // Remove spaces from the filename
  33. $filename = preg_replace('/\s+/', '_', $filename);
  34. }
  35. if ($directory === NULL)
  36. {
  37. // Use the pre-configured upload directory
  38. $directory = Kohana::config('upload.directory', TRUE);
  39. }
  40. // Make sure the directory ends with a slash
  41. $directory = rtrim($directory, '/').'/';
  42. if ( ! is_dir($directory) AND Kohana::config('upload.create_directories') === TRUE)
  43. {
  44. // Create the upload directory
  45. mkdir($directory, 0777, TRUE);
  46. }
  47. if ( ! is_writable($directory))
  48. throw new Kohana_Exception('The upload destination folder, :dir:, does not appear to be writable.', array(':dir:' => $directory));
  49. if (is_uploaded_file($file['tmp_name']) AND move_uploaded_file($file['tmp_name'], $filename = $directory.$filename))
  50. {
  51. if ($chmod !== FALSE)
  52. {
  53. // Set permissions on filename
  54. chmod($filename, $chmod);
  55. }
  56. // Return new file path
  57. return $filename;
  58. }
  59. return FALSE;
  60. }
  61. /* Validation Rules */
  62. /**
  63. * Tests if input data is valid file type, even if no upload is present.
  64. *
  65. * @param array $_FILES item
  66. * @return bool
  67. */
  68. public static function valid($file)
  69. {
  70. return (is_array($file)
  71. AND isset($file['error'])
  72. AND isset($file['name'])
  73. AND isset($file['type'])
  74. AND isset($file['tmp_name'])
  75. AND isset($file['size']));
  76. }
  77. /**
  78. * Tests if input data has valid upload data.
  79. *
  80. * @param array $_FILES item
  81. * @return bool
  82. */
  83. public static function required(array $file)
  84. {
  85. return (isset($file['tmp_name'])
  86. AND isset($file['error'])
  87. AND is_uploaded_file($file['tmp_name'])
  88. AND (int) $file['error'] === UPLOAD_ERR_OK);
  89. }
  90. /**
  91. * Validation rule to test if an uploaded file is allowed by extension.
  92. *
  93. * @param array $_FILES item
  94. * @param array allowed file extensions
  95. * @return bool
  96. */
  97. public static function type(array $file, array $allowed_types)
  98. {
  99. if ((int) $file['error'] !== UPLOAD_ERR_OK)
  100. return TRUE;
  101. // Get the default extension of the file
  102. $extension = strtolower(substr(strrchr($file['name'], '.'), 1));
  103. // Make sure there is an extension and that the extension is allowed
  104. return ( ! empty($extension) AND in_array($extension, $allowed_types));
  105. }
  106. /**
  107. * Validation rule to test if an uploaded file is allowed by file size.
  108. * File sizes are defined as: SB, where S is the size (1, 15, 300, etc) and
  109. * B is the byte modifier: (B)ytes, (K)ilobytes, (M)egabytes, (G)igabytes.
  110. * Eg: to limit the size to 1MB or less, you would use "1M".
  111. *
  112. * @param array $_FILES item
  113. * @param array maximum file size
  114. * @return bool
  115. */
  116. public static function size(array $file, array $size)
  117. {
  118. if ((int) $file['error'] !== UPLOAD_ERR_OK)
  119. return TRUE;
  120. // Only one size is allowed
  121. $size = strtoupper($size[0]);
  122. if ( ! preg_match('/[0-9]++[BKMG]/', $size))
  123. return FALSE;
  124. // Make the size into a power of 1024
  125. switch (substr($size, -1))
  126. {
  127. case 'G': $size = intval($size) * pow(1024, 3); break;
  128. case 'M': $size = intval($size) * pow(1024, 2); break;
  129. case 'K': $size = intval($size) * pow(1024, 1); break;
  130. default: $size = intval($size); break;
  131. }
  132. // Test that the file is under or equal to the max size
  133. return ($file['size'] <= $size);
  134. }
  135. } // End upload