/core/Warecorp/File/Item.php

https://github.com/zanby/Core · PHP · 163 lines · 80 code · 8 blank · 75 comment · 24 complexity · d0f12be27609c5fb8c2984c64c489934 MD5 · raw file

  1. <?php
  2. /**
  3. * Zanby Enterprise Group Family System
  4. *
  5. * Copyright (C) 2005-2011 Zanby LLC. (http://www.zanby.com)
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * To contact Zanby LLC, send email to info@zanby.com. Our mailing
  21. * address is:
  22. *
  23. * Zanby LLC
  24. * 3611 Farmington Road
  25. * Minnetonka, MN 55305
  26. *
  27. * @category Zanby
  28. * @package
  29. * @copyright Copyright (c) 2005-2011 Zanby LLC. (http://www.zanby.com)
  30. * @license http://zanby.com/license/ GPL License
  31. * @version <this will be auto generated>
  32. */
  33. /**
  34. * Warecorp FRAMEWORK
  35. *
  36. * @package Warecorp_Group_Simple
  37. * @copyright Copyright (c) 2006
  38. * @author Halauniou Yauhen
  39. */
  40. /**
  41. * Class for File processing
  42. *
  43. */
  44. class Warecorp_File_Item
  45. {
  46. /**
  47. * check is uploaded file is image
  48. * @param $fileName - Uploaded file name
  49. * @param $filePath - path to uploaded file
  50. * @param bool true/false
  51. */
  52. static function isImage($fileName, $filePath)
  53. {
  54. //$valid_extension = array("jpg", "jpeg", "gif", "png");
  55. $valid_extension = Warecorp_File_Enum_Extensions::getInArrayMode(Warecorp_File_Enum_Extensions::IMAGES);
  56. if (file_exists($filePath)){
  57. $parts = explode(".", $fileName);
  58. $extension = strtolower($parts[count($parts)-1]);
  59. if (in_array($extension, $valid_extension)){
  60. @$sourceInfo = getimagesize($filePath);
  61. $format = strtolower(substr($sourceInfo["mime"], strpos($sourceInfo["mime"], '/')+1));
  62. if (in_array($format, $valid_extension)){
  63. return array($sourceInfo[0], $sourceInfo[1]);
  64. }
  65. }
  66. }
  67. return false;
  68. }
  69. /**
  70. * check is uploaded file is video
  71. * @param $fileName - Uploaded file name
  72. * @param $filePath - path to uploaded file
  73. * @param bool true/false
  74. */
  75. static function isVideo($fileName, $filePath)
  76. {
  77. $valid_extension = Warecorp_File_Enum_Extensions::getInArrayMode(Warecorp_File_Enum_Extensions::VIDEOS);
  78. if (file_exists($filePath)){
  79. $parts = explode(".", $fileName);
  80. $extension = strtolower($parts[count($parts)-1]);
  81. if (in_array($extension, $valid_extension)){
  82. if (new ffmpeg_movie($filePath)) return true;
  83. }
  84. }
  85. return false;
  86. }
  87. /**
  88. * Проверяет, существует ли файл (файлы), пришедшие для аплоада
  89. * @param string $nameRegexp - регуляр-соответствие имени
  90. * @return bool
  91. */
  92. static function filesIssetForUpload($nameRegexp = null)
  93. {
  94. $isset = false;
  95. if ( !isset($_FILES) || sizeof($_FILES) == 0 ) return false;
  96. foreach ( $_FILES as $_name => $_file ) {
  97. if ( $nameRegexp !== null ) {
  98. if ( preg_match($nameRegexp, $_name, $match) ) {
  99. if ( $_file['name'] != '' && $_file['tmp_name'] != '' ) $isset = true;
  100. }
  101. } else {
  102. if ( $_file['name'] != '' && $_file['tmp_name'] != '' ) $isset = true;
  103. }
  104. }
  105. return $isset;
  106. }
  107. /**
  108. * Upload file
  109. * @param string $from - full filepath
  110. * @param string $to - full filepath
  111. * @return bool - result
  112. */
  113. static function uploadFile($from, $to)
  114. {
  115. if ( is_uploaded_file($from) ) {
  116. if ( @move_uploaded_file($from, $to) ) return true;
  117. }
  118. return false;
  119. }
  120. /**
  121. * Return extension of file
  122. * @param string $filename - name or path of file
  123. * @return string
  124. */
  125. static function getFileExt($filename)
  126. {
  127. $filename = basename($filename);
  128. $ext = explode("\.", $filename);
  129. return ( sizeof($ext) > 1 ) ? strtolower($ext[sizeof($ext) - 1]) : '';
  130. }
  131. /**
  132. * Return max file size in bytes
  133. */
  134. static function getMaxFileSize()
  135. {
  136. $_maxFileSize = 0;
  137. if (preg_match('/^([0-9]+)([a-zA-Z]*)$/', ini_get('upload_max_filesize'), $matches)) {
  138. // see http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
  139. switch (strtoupper($matches['2'])) {
  140. case 'G':
  141. $_maxFileSize = $matches['1'] * 1073741824;
  142. break;
  143. case 'M':
  144. $_maxFileSize = $matches['1'] * 1048576;
  145. break;
  146. case 'K':
  147. $_maxFileSize = $matches['1'] * 1024;
  148. break;
  149. default:
  150. $_maxFileSize = $matches['1'];
  151. }
  152. }
  153. return $_maxFileSize;
  154. }
  155. }