SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Upload.php

Copyright (C) 2017 Karmabunny Pty Ltd.

This file is a part of SproutCMS.

SproutCMS is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.

For more information, visit <http://getsproutcms.com>.

This class was originally from Kohana 2.3.4
Copyright 2007-2008 Kohana Team
  1. <?php
  2. /**
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  *
  13.  * This class was originally from Kohana 2.3.4
  14.  * Copyright 2007-2008 Kohana Team
  15.  */
  16. namespace Sprout\Helpers;
  17.  
  18. use Kohana;
  19. use Kohana_Exception;
  20.  
  21.  
  22. /**
  23.  * Upload helper class for working with the global $_FILES array and Validation library.
  24.  * @deprecated This is old Kohana cruft
  25.  */
  26. class Upload
  27. {
  28.  
  29. /**
  30.   * Save an uploaded file to a new location.
  31.   *
  32.   * @param mixed name of $_FILE input or array of upload data
  33.   * @param string new filename
  34.   * @param string new directory
  35.   * @param integer chmod mask
  36.   * @return string full path to new file
  37.   */
  38. public static function save($file, $filename = NULL, $directory = NULL, $chmod = 0644)
  39. {
  40. // Load file data from FILES if not passed as array
  41. $file = is_array($file) ? $file : $_FILES[$file];
  42.  
  43. if ($filename === NULL)
  44. {
  45. // Use the default filename, with a timestamp pre-pended
  46. $filename = time().$file['name'];
  47. }
  48.  
  49. if (Kohana::config('upload.remove_spaces') === TRUE)
  50. {
  51. // Remove spaces from the filename
  52. $filename = preg_replace('/\s+/', '_', $filename);
  53. }
  54.  
  55. if ($directory === NULL)
  56. {
  57. // Use the pre-configured upload directory
  58. $directory = Kohana::config('upload.directory', TRUE);
  59. }
  60.  
  61. // Make sure the directory ends with a slash
  62. $directory = rtrim($directory, '/').'/';
  63.  
  64. if ( ! is_dir($directory) AND Kohana::config('upload.create_directories') === TRUE)
  65. {
  66. // Create the upload directory
  67. mkdir($directory, 0777, TRUE);
  68. }
  69.  
  70. if ( ! is_writable($directory))
  71. throw new Kohana_Exception('upload.not_writable', $directory);
  72.  
  73. $path = $directory . $filename;
  74. if (is_uploaded_file($file['tmp_name']) AND move_uploaded_file($file['tmp_name'], $path))
  75. {
  76. if ($chmod !== FALSE)
  77. {
  78. // Set permissions on filename
  79. chmod($path, $chmod);
  80. }
  81.  
  82. // Return new file path
  83. return $path;
  84. }
  85.  
  86. return FALSE;
  87. }
  88.  
  89. /* Validation Rules */
  90.  
  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. return (is_array($file)
  100. AND isset($file['error'])
  101. AND isset($file['name'])
  102. AND isset($file['type'])
  103. AND isset($file['tmp_name'])
  104. AND isset($file['size']));
  105. }
  106.  
  107. /**
  108.   * Tests if input data has valid upload data.
  109.   *
  110.   * @param array $_FILES item
  111.   * @return bool
  112.   */
  113. public static function required(array $file)
  114. {
  115. return (isset($file['tmp_name'])
  116. AND isset($file['error'])
  117. AND is_uploaded_file($file['tmp_name'])
  118. AND (int) $file['error'] === UPLOAD_ERR_OK);
  119. }
  120.  
  121. /**
  122.   * Validation rule to test if an uploaded file is allowed by extension.
  123.   *
  124.   * @param array $_FILES item
  125.   * @param array allowed file extensions
  126.   * @return bool
  127.   */
  128. public static function type(array $file, array $allowed_types)
  129. {
  130. if ((int) $file['error'] !== UPLOAD_ERR_OK)
  131. return TRUE;
  132.  
  133. // Get the default extension of the file
  134. $extension = strtolower(substr(strrchr($file['name'], '.'), 1));
  135.  
  136. // Get the mime types for the extension
  137. $mime_types = Kohana::config('mimes.'.$extension);
  138.  
  139. // Make sure there is an extension, that the extension is allowed, and that mime types exist
  140. return ( ! empty($extension) AND in_array($extension, $allowed_types) AND is_array($mime_types));
  141. }
  142.  
  143. /**
  144.   * Validation rule to test if an uploaded file is allowed by file size.
  145.   * File sizes are defined as: SB, where S is the size (1, 15, 300, etc) and
  146.   * B is the byte modifier: (B)ytes, (K)ilobytes, (M)egabytes, (G)igabytes.
  147.   * Eg: to limit the size to 1MB or less, you would use "1M".
  148.   *
  149.   * @param array $_FILES item
  150.   * @param array maximum file size
  151.   * @return bool
  152.   */
  153. public static function size(array $file, array $size)
  154. {
  155. if ((int) $file['error'] !== UPLOAD_ERR_OK)
  156. return TRUE;
  157.  
  158. // Only one size is allowed
  159. $size = strtoupper($size[0]);
  160.  
  161. if ( ! preg_match('/[0-9]++[BKMG]/', $size))
  162. return FALSE;
  163.  
  164. // Make the size into a power of 1024
  165. switch (substr($size, -1))
  166. {
  167. case 'G': $size = intval($size) * pow(1024, 3); break;
  168. case 'M': $size = intval($size) * pow(1024, 2); break;
  169. case 'K': $size = intval($size) * pow(1024, 1); break;
  170. default: $size = intval($size); break;
  171. }
  172.  
  173. // Test that the file is under or equal to the max size
  174. return ($file['size'] <= $size);
  175. }
  176.  
  177. } // End upload
  178.