PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/core/helpers/upload.php

http://rapyd-framework.googlecode.com/
PHP | 175 lines | 99 code | 36 blank | 40 comment | 21 complexity | 0cf6ae69b7a8a65bee35faa19074c539 MD5 | raw file
  1. <?php if (!defined('RAPYD_PATH')) exit('No direct script access allowed');
  2. /**
  3. * upload helper
  4. * widely inspired by kohana http://kohanaphp.com
  5. **/
  6. class rpd_upload_helper {
  7. /**
  8. * @param mixed name of $_FILE input or array of upload data
  9. * @param string new directory
  10. * @param string new filename
  11. * @param integer chmod mask
  12. * @return string full path to new file
  13. */
  14. public static function save($file, $directory, $filename = NULL, $chmod = 0644)
  15. {
  16. // Load file data from FILES if not passed as array
  17. $file = is_array($file) ? $file : $_FILES[$file];
  18. if ($filename === NULL)
  19. {
  20. $filename = $file['name'];
  21. }
  22. $directory = rtrim($directory, '/').'/';
  23. if (!is_dir($directory))
  24. {
  25. return FALSE;
  26. }
  27. $filename = preg_replace('/\s+/', '_', $filename);
  28. $filename = preg_replace('/[^a-zA-Z0-9\._-]/', '', $filename);
  29. $extension = strtolower(substr(strrchr($filename, '.'), 1));
  30. $name = rtrim($filename, strrchr($filename, '.'));
  31. $i = 0;
  32. $finalname = $name;
  33. while (file_exists($directory . $finalname. '.'.$extension))
  34. {
  35. $i++;
  36. $finalname = $name . (string)$i;
  37. }
  38. $filename = $finalname. '.'.$extension;
  39. if (is_uploaded_file($file['tmp_name']) AND move_uploaded_file($file['tmp_name'], $directory.$filename))
  40. {
  41. if ($chmod !== FALSE)
  42. {
  43. // Set permissions on filename
  44. chmod($directory.$filename, $chmod);
  45. }
  46. // Return new filename
  47. return $filename;
  48. }
  49. return FALSE;
  50. }
  51. // --------------------------------------------------------------------
  52. public static function type(array $file, array $allowed_types)
  53. {
  54. if ((int) $file['error'] !== UPLOAD_ERR_OK)
  55. return TRUE;
  56. $extension = strtolower(substr(strrchr($file['name'], '.'), 1));
  57. return ( ! empty($extension) AND in_array($extension, $allowed_types) );
  58. }
  59. // --------------------------------------------------------------------
  60. /**
  61. * Validation rule to test if an uploaded file is allowed by file size.
  62. * File sizes are defined as: SB, where S is the size (1, 15, 300, etc) and
  63. * B is the byte modifier: (B)ytes, (K)ilobytes, (M)egabytes, (G)igabytes.
  64. * Eg: to limit the size to 1MB or less, you would use "1M".
  65. *
  66. * @param array $_FILES item
  67. * @param string size string (IE: 1M, 200K, 100B)
  68. * @return bool
  69. */
  70. public static function size(array $file, $size)
  71. {
  72. if ((int) $file['error'] !== UPLOAD_ERR_OK)
  73. return TRUE;
  74. // Only one size is allowed
  75. $size = strtoupper($size);
  76. if ( ! preg_match('/[0-9]++[BKMG]/', $size))
  77. return FALSE;
  78. // Make the size into a power of 1024
  79. switch (substr($size, -1))
  80. {
  81. case 'G': $size = intval($size) * pow(1024, 3); break;
  82. case 'M': $size = intval($size) * pow(1024, 2); break;
  83. case 'K': $size = intval($size) * pow(1024, 1); break;
  84. default: $size = intval($size); break;
  85. }
  86. // Test that the file is under or equal to the max size
  87. return ($file['size'] <= $size);
  88. }
  89. public static function fullperm($path)
  90. {
  91. if (!is_file($path) && !is_dir($path))
  92. return '-';
  93. $perms = fileperms($path);
  94. if (($perms & 0xC000) == 0xC000) {
  95. // Socket
  96. $info = 's';
  97. } elseif (($perms & 0xA000) == 0xA000) {
  98. // Symbolic Link
  99. $info = 'l';
  100. } elseif (($perms & 0x8000) == 0x8000) {
  101. // Regular
  102. $info = '-';
  103. } elseif (($perms & 0x6000) == 0x6000) {
  104. // Block special
  105. $info = 'b';
  106. } elseif (($perms & 0x4000) == 0x4000) {
  107. // Directory
  108. $info = 'd';
  109. } elseif (($perms & 0x2000) == 0x2000) {
  110. // Character special
  111. $info = 'c';
  112. } elseif (($perms & 0x1000) == 0x1000) {
  113. // FIFO pipe
  114. $info = 'p';
  115. } else {
  116. // Unknown
  117. $info = 'u';
  118. }
  119. // Owner
  120. $info .= (($perms & 0x0100) ? 'r' : '-');
  121. $info .= (($perms & 0x0080) ? 'w' : '-');
  122. $info .= (($perms & 0x0040) ?
  123. (($perms & 0x0800) ? 's' : 'x' ) :
  124. (($perms & 0x0800) ? 'S' : '-'));
  125. // Group
  126. $info .= (($perms & 0x0020) ? 'r' : '-');
  127. $info .= (($perms & 0x0010) ? 'w' : '-');
  128. $info .= (($perms & 0x0008) ?
  129. (($perms & 0x0400) ? 's' : 'x' ) :
  130. (($perms & 0x0400) ? 'S' : '-'));
  131. // World
  132. $info .= (($perms & 0x0004) ? 'r' : '-');
  133. $info .= (($perms & 0x0002) ? 'w' : '-');
  134. $info .= (($perms & 0x0001) ?
  135. (($perms & 0x0200) ? 't' : 'x' ) :
  136. (($perms & 0x0200) ? 'T' : '-'));
  137. return $info;
  138. }
  139. }