PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes.php

https://gitlab.com/simcript/Uploader
PHP | 195 lines | 167 code | 22 blank | 6 comment | 30 complexity | 729f6be069d1fb041b7e4694ff2a8074 MD5 | raw file
  1. <?php
  2. class SS_Upload {
  3. protected $_uploaded = array(); // for Array super global variable $_FILES
  4. protected $_destination; // directory default for move files
  5. protected $_maxSize = 1048576; // maximum size file uploaded default is 1048576 Byte or 1 MB
  6. protected $_messages = array(); // messages status upload
  7. protected $_permitted = array(); // types MIME files allowed
  8. protected $_renamed = false; // status renamed file
  9. protected $_filenames = array();
  10. protected $_minRand = 3078; //minimum number for random number default is 3078
  11. protected $_maxRand = 9730514; //maximum number for random number default is 9730514
  12. protected $_sizeFiles = array();
  13. public function __construct($path) {
  14. if (!is_dir($path) || !is_writable($path)) {
  15. throw new Exception("Error! $path is not available or not writable!");
  16. }
  17. $this->_destination = $path;
  18. $this->_uploaded = $_FILES;
  19. }
  20. public function getMaxSize() {
  21. return number_format($this->_maxSize/1024, 1) . 'kB';
  22. }
  23. public function setMaxSize($num) {
  24. if (!is_numeric($num)) {
  25. throw new Exception("Maximum size must be a number.");
  26. }
  27. $this->_maxSize = (int) $num;
  28. }
  29. public function move($overwrite = false) {
  30. $field = current($this->_uploaded);
  31. if (is_array($field['name'])) {
  32. foreach ($field['name'] as $number => $filename) {
  33. // process multiple upload
  34. $this->_renamed = false;
  35. $this->processFile($filename, $field['error'][$number], $field['size'][$number], $field['type'][$number], $field['tmp_name'][$number], $overwrite);
  36. }
  37. } else {
  38. $this->processFile($field['name'], $field['error'], $field['size'], $field['type'], $field['tmp_name'], $overwrite);
  39. }
  40. }
  41. public function getMessages() {
  42. return $this->_messages;
  43. }
  44. public function getFileSize($filename){
  45. return $this->_sizeFiles["$filename"];
  46. }
  47. protected function checkError($filename, $error) {
  48. switch ($error) {
  49. case 0:
  50. return true;
  51. case 1:
  52. case 2:
  53. $this->_messages[] = "$filename exceeds maximum size: " . $this->getMaxSize();
  54. return true;
  55. case 3:
  56. $this->_messages[] = "Error uploading $filename. Please try again.";
  57. return false;
  58. case 4:
  59. $this->_messages[] = 'No file selected.';
  60. return false;
  61. default:
  62. $this->_messages[] = "System error uploading $filename. Contact webmaster.";
  63. return false;
  64. }
  65. }
  66. protected function checkSize($filename, $size) {
  67. if ($size == 0) {
  68. return false;
  69. } elseif ($size > $this->_maxSize) {
  70. $this->_messages[] = "$filename exceeds maximum size: " . $this->getMaxSize();
  71. return false;
  72. } else {
  73. return true;
  74. }
  75. }
  76. protected function checkType($filename, $type) {
  77. if (empty($type)) {
  78. return false;
  79. } elseif (!in_array($type, $this->_permitted)) {
  80. $this->_messages[] = "$filename is not a permitted type of file.";
  81. return false;
  82. } else {
  83. return true;
  84. }
  85. }
  86. public function setPermittedTypes($types){
  87. $types = explode(",",$types);
  88. $this->isValidMime($types);
  89. $this->_permitted = $types;
  90. }
  91. public function addPermittedTypes($types) {
  92. $types = (array) $types;
  93. $this->isValidMime($types);
  94. $this->_permitted = array_merge($this->_permitted, $types);
  95. }
  96. public function getFilenames() {
  97. return $this->_filenames;
  98. }
  99. public function setMinMaxRand($min,$max) { //set minimum and maximum number for generate a random number
  100. if (($min > 0) && ($max > 0) && ($min < $max)) {
  101. $this->_minRand = $min;
  102. $this->_maxRand = $max;
  103. } else {
  104. $this->_messages[] = "Minimum and maximum numbers for rename file are unknown";
  105. }
  106. }
  107. protected function getRandom() { //get a random number between $_minRand and $_maxRand
  108. return (int) mt_rand($this->_minRand,$this->_maxRand);
  109. }
  110. protected function isValidMime($types) {
  111. $alsoValid = array('application/envoy','application/fractals','application/futuresplash','application/hta','application/internet-property-stream','application/mac-binhex40','application/msword','application/msword','application/octet-stream','application/octet-stream','application/octet-stream','application/octet-stream','application/octet-stream','application/octet-stream','application/octet-stream','application/oda','application/olescript','application/pdf','application/pics-rules','application/pkcs10','application/pkix-crl','application/postscript','application/postscript','application/postscript','application/rtf','application/set-payment-initiation','application/set-registration-initiation','application/vnd.ms-excel','application/vnd.ms-excel','application/vnd.ms-excel','application/vnd.ms-excel','application/vnd.ms-excel','application/vnd.ms-excel','application/vnd.ms-outlook','application/vnd.ms-pkicertstore','application/vnd.ms-pkiseccat','application/vnd.ms-pkistl','application/vnd.ms-powerpoint','application/vnd.ms-powerpoint','application/vnd.ms-powerpoint','application/vnd.ms-project','application/vnd.ms-works','application/vnd.ms-works','application/vnd.ms-works','application/vnd.ms-works','application/winhlp','application/x-bcpio','application/x-cdf','application/x-compress','application/x-compressed','application/x-cpio','application/x-csh','application/x-director','application/x-director','application/x-director','application/x-dvi','application/x-gtar','application/x-gzip','application/x-hdf','application/x-internet-signup','application/x-internet-signup','application/x-iphone','application/x-javascript','application/x-latex','application/x-msaccess','application/x-mscardfile','application/x-msclip','application/x-msdownload','application/x-msmediaview','application/x-msmediaview','application/x-msmediaview','application/x-msmetafile','application/x-msmoney','application/x-mspublisher','application/x-msschedule','application/x-msterminal','application/x-mswrite','application/x-netcdf','application/x-netcdf','application/x-perfmon','application/x-perfmon','application/x-perfmon','application/x-perfmon','application/x-perfmon','application/x-pkcs12','application/x-pkcs12','application/x-pkcs7-certificates','application/x-pkcs7-certificates','application/x-pkcs7-certreqresp','application/x-pkcs7-mime','application/x-pkcs7-mime','application/x-pkcs7-signature','application/x-sh','application/x-shar','application/x-shockwave-flash','application/x-stuffit','application/x-sv4cpio','application/x-sv4crc','application/x-tar','application/x-tcl','application/x-tex','application/x-texinfo','application/x-texinfo','application/x-troff','application/x-troff','application/x-troff','application/x-troff-man','application/x-troff-me','application/x-troff-ms','application/x-ustar','application/x-wais-source','application/x-x509-ca-cert','application/x-x509-ca-cert','application/x-x509-ca-cert','application/ynd.ms-pkipko','application/zip','audio/basic','audio/basic','audio/mid','audio/mid','audio/mpeg','audio/x-aiff','audio/x-aiff','audio/x-aiff','audio/x-mpegurl','audio/x-pn-realaudio','audio/x-pn-realaudio','audio/x-wav','image/bmp','image/cis-cod','image/gif','image/ief','image/jpeg','image/jpeg','image/png','image/jpeg','image/pipeg','image/svg+xml','image/tiff','image/tiff','image/x-cmu-raster','image/x-cmx','image/x-icon','image/x-portable-anymap','image/x-portable-bitmap','image/x-portable-graymap','image/x-portable-pixmap','image/x-rgb','image/x-xbitmap','image/x-xpixmap','image/x-xwindowdump','message/rfc822','message/rfc822','message/rfc822','text/css','text/h323','text/html','text/html','text/html','text/iuls','text/plain','text/plain','text/plain','text/plain','text/richtext','text/scriptlet','text/tab-separated-values','text/webviewhtml','text/x-component','text/x-setext','text/x-vcard','video/mpeg','video/mpeg','video/mpeg','video/mpeg','video/mpeg','video/mpeg','video/quicktime','video/quicktime','video/x-la-asf','video/x-la-asf','video/x-ms-asf','video/x-ms-asf','video/x-ms-asf','video/x-msvideo','video/x-sgi-movie','x-world/x-vrml','x-world/x-vrml','x-world/x-vrml','x-world/x-vrml','x-world/x-vrml','x-world/x-vrml');
  112. $valid = array_merge($this->_permitted, $alsoValid);
  113. foreach ($types as $type) {
  114. if (!in_array($type, $valid)) {
  115. throw new Exception("$type is not a permitted MIME type");
  116. }
  117. }
  118. }
  119. protected function checkName($name, $overwrite) {
  120. $nospaces = str_replace(' ', '_', $name);
  121. if ($nospaces != $name) {
  122. $this->_renamed = true;
  123. }
  124. if (!$overwrite) {
  125. $existing = scandir($this->_destination);
  126. if (in_array($nospaces, $existing)) {
  127. $dot = strrpos($nospaces, '.');
  128. if ($dot) {
  129. $base = substr($nospaces, 0, $dot);
  130. $extension = substr($nospaces, $dot);
  131. } else {
  132. $base = $nospaces;
  133. $extension = '';
  134. }
  135. do {
  136. $i = $this->getRandom();
  137. $nospaces = $base . '_' . $i . $extension;
  138. } while (in_array($nospaces, $existing));
  139. $this->_renamed = true;
  140. }
  141. }
  142. return $nospaces;
  143. }
  144. protected function processFile($filename, $error, $size, $type, $tmp_name, $overwrite) {
  145. $OK = $this->checkError($filename, $error);
  146. if ($OK) {
  147. $sizeOK = $this->checkSize($filename, $size);
  148. $typeOK = $this->checkType($filename, $type);
  149. if ($sizeOK && $typeOK) {
  150. $name = $this->checkName($filename, $overwrite);
  151. $this->_sizeFiles[$name] = $size; ///SIZE FILE IS SAVE
  152. // move and rename file to upload directory
  153. $success = move_uploaded_file($tmp_name, $this->_destination . $name);
  154. if ($success) {
  155. // add the amended filename to the array of filenames
  156. $this->_filenames[] = $name;
  157. $message = "$filename uploaded successfully";
  158. if ($this->_renamed) {
  159. $message .= " and renamed $name";
  160. }
  161. $this->_messages[] = $message;
  162. } else {
  163. $this->_messages[] = "Could not upload $filename";
  164. }
  165. }
  166. }
  167. }
  168. }
  169. ###############################################
  170. ############### Class Messaging ###############
  171. ###############################################
  172. class SS_Messaging {
  173. }