PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/zan/classes/files.php

https://github.com/triartdesign/ZanPHP
PHP | 274 lines | 259 code | 1 blank | 14 comment | 1 complexity | 461a2e3bedb36d73cfd295f040db237c MD5 | raw file
  1. <?php
  2. /**
  3. * ZanPHP
  4. *
  5. * An open source agile and rapid development framework for PHP 5
  6. *
  7. * @package ZanPHP
  8. * @author MilkZoft Developer Team
  9. * @copyright Copyright (c) 2011, MilkZoft, Inc.
  10. * @license http://www.zanphp.com/documentation/en/license/
  11. * @link http://www.zanphp.com
  12. */
  13. /**
  14. * Access from index.php
  15. */
  16. if(!defined("_access")) {
  17. die("Error: You don't have permission to access here...");
  18. }
  19. /**
  20. * ZanPHP Files Class
  21. *
  22. * This class is used to upload files to server
  23. *
  24. * @package ZanPHP
  25. * @subpackage core
  26. * @category classes
  27. * @author MilkZoft Developer Team
  28. * @link http://www.zanphp.com/documentation/en/classes/files_class
  29. */
  30. class ZP_Files extends ZP_Load {
  31. /**
  32. * Contains the error if the file has one
  33. *
  34. * @var public $fileError
  35. */
  36. public $fileError = NULL;
  37. /**
  38. * Contains the name of the file
  39. *
  40. * @var public $filename
  41. */
  42. public $filename = NULL;
  43. /**
  44. * Contains the size in bytes of the file
  45. *
  46. * @var public $fileSize
  47. */
  48. public $fileSize = NULL;
  49. /**
  50. * Contains the temporal name of the file
  51. *
  52. * @var public $fileTmp
  53. */
  54. public $fileTmp = NULL;
  55. /**
  56. * Contains the mime type of the file
  57. *
  58. * @var public $fileType
  59. */
  60. public $fileType = NULL;
  61. public function __construct() {
  62. $this->helper("debugging");
  63. }
  64. public function getFileInformation($filename = FALSE) {
  65. if(!$this->filename and !$filename) {
  66. return FALSE;
  67. } else {
  68. $filename = ($this->filename) ? $this->filename : $filename;
  69. }
  70. $file["icon"] = NULL;
  71. $parts = explode(".", $filename);
  72. if(is_array($parts)) {
  73. $file["name"] = $parts[0];
  74. $file["extension"] = array_pop($parts);
  75. $audio = array("wav", "midi", "mid", "mp3", "wma");
  76. $codes = array("asp", "php", "c", "as", "html", "js", "css", "rb");
  77. $document = array("csv", "doc", "docx", "pdf", "ppt", "pptx", "txt", "xls", "xlsx");
  78. $image = array("jpg", "jpeg", "png", "gif", "bmp");
  79. $programs = array("7z", "ai", "cdr", "fla", "exe", "dmg", "pkg", "iso", "msi", "psd", "rar", "svg", "swf", "zip");
  80. $video = array("mpg", "mpeg", "avi", "wmv", "asf", "mp4", "flv", "mov");
  81. if(in_array(strtolower($file["extension"]), $audio)) {
  82. $file["type"] = "audio";
  83. } elseif(in_array(strtolower($file["extension"]), $codes)) {
  84. $file["type"] = "code";
  85. } elseif(in_array(strtolower($file["extension"]), $document)) {
  86. $file["type"] = "document";
  87. } elseif(in_array(strtolower($file["extension"]), $image)) {
  88. $file["type"] = "image";
  89. } elseif(in_array(strtolower($file["extension"]), $video)) {
  90. $file["type"] = "program";
  91. } elseif(in_array(strtolower($file["extension"]), $video)) {
  92. $file["type"] = "video";
  93. } else {
  94. $file["type"] = "unknown";
  95. }
  96. $icons = array(
  97. "txt" => array(get("webURL") ."www/lib/images/icons/files/text.png", __(_("Text File"))),
  98. "doc" => array(get("webURL") ."/www/lib/images/icons/files/doc.png", __(_("Document File"))),
  99. "docx" => array(get("webURL") ."/www/lib/images/icons/files/doc.png", __(_("Document File"))),
  100. "pdf" => array(get("webURL") ."/www/lib/images/icons/files/pdf.png", __(_("PDF File"))),
  101. "ppt" => array(get("webURL") ."/www/lib/images/icons/files/ppt.png", __(_("Power Point File"))),
  102. "pptx" => array(get("webURL") ."/www/lib/images/icons/files/ppt.png", __(_("Power Point File"))),
  103. "rar" => array(get("webURL") ."/www/lib/images/icons/files/rar.png", __(_("WinRAR File"))),
  104. "iso" => array(get("webURL") ."/www/lib/images/icons/files/rar.png", __(_("ISO File"))),
  105. "xls" => array(get("webURL") ."/www/lib/images/icons/files/xls.png", __(_("Excel File"))),
  106. "xlsx" => array(get("webURL") ."/www/lib/images/icons/files/xls.png", __(_("Excel File"))),
  107. "csv" => array(get("webURL") ."/www/lib/images/icons/files/xls.png", __(_("Excel File"))),
  108. "zip" => array(get("webURL") ."/www/lib/images/icons/files/zip.png", __(_("WinZIP File"))),
  109. "7z" => array(get("webURL") ."/www/lib/images/icons/files/7z.png", __(_("7z File"))),
  110. "ai" => array(get("webURL") ."/www/lib/images/icons/files/ai.png", __(_("Adobe Illustrator File"))),
  111. "svg" => array(get("webURL") ."/www/lib/images/icons/files/ai.png", __(_("Adobe Illustrator File"))),
  112. "cdr" => array(get("webURL") ."/www/lib/images/icons/files/cdr.png", __(_("Corel Draw File"))),
  113. "msi" => array(get("webURL") ."/www/lib/images/icons/files/exe.png", __(_("Executable File"))),
  114. "exe" => array(get("webURL") ."/www/lib/images/icons/files/exe.png", __(_("Executable File"))),
  115. "dmg" => array(get("webURL") ."/www/lib/images/icons/files/exe.png", __(_("Executable File"))),
  116. "pkg" => array(get("webURL") ."/www/lib/images/icons/files/exe.png", __(_("Executable File"))),
  117. );
  118. foreach($icons as $extension => $icon) {
  119. if($file["extension"] === $extension) {
  120. $file["icon"] = $icon;
  121. break;
  122. }
  123. }
  124. return $file;
  125. }
  126. return FALSE;
  127. }
  128. /**
  129. * Upload a file to specific path
  130. *
  131. * @param string $path = NULL
  132. * @param string $type = "Image"
  133. * @return string value
  134. */
  135. public function upload($path = NULL, $type = "image") {
  136. ini_set("post_max_size", 18388608);
  137. ini_set("upload_max_filesize", 18388608);
  138. ini_set("max_execution_time", "1000");
  139. ini_set("max_input_time", "1000");
  140. $file = $this->getFileInformation();
  141. if(!$file) {
  142. $error["upload"] = FALSE;
  143. $error["message"] = "A problem occurred when trying to upload file";
  144. return $error;
  145. }
  146. $filename = code(5, FALSE) ."_". slug($file["name"]) .".". $file["extension"];
  147. $URL = $path . $filename;
  148. if(file_exists($URL)) {
  149. $error["upload"] = FALSE;
  150. $error["message"] = "The file already exists";
  151. $error["filename"] = $filename;
  152. } elseif($this->fileSize > _fileSize) {
  153. $error["upload"] = FALSE;
  154. $error["message"] = "The file size exceed the permited limit";
  155. } elseif($this->fileError === 1) {
  156. $error["upload"] = FALSE;
  157. $error["message"] = "An error has ocurred";
  158. } elseif($file["type"] !== $type) {
  159. $error["upload"] = FALSE;
  160. $error["message"] = "The file type is not permited";
  161. } elseif(move_uploaded_file($this->fileTmp, $URL)) {
  162. chmod($URL, 0777);
  163. $error["upload"] = TRUE;
  164. $error["message"] = "The file has been upload correctly";
  165. $error["filename"] = $filename;
  166. } else {
  167. $error["upload"] = FALSE;
  168. $error["message"] = "A problem occurred when trying to upload file";
  169. }
  170. return $error;
  171. }
  172. /**
  173. * Upload and resize an image
  174. *
  175. * @param string $dir
  176. * @param string $name = "File"
  177. * @return array value
  178. */
  179. public function uploadImage($dir, $inputName = "file", $type = "resize", $small = TRUE, $medium = TRUE, $original = TRUE) {
  180. if(!is_dir($dir)) {
  181. @mkdir($dir, 0777);
  182. }
  183. if(FILES($inputName, "name")) {
  184. $this->filename = FILES($inputName, "name");
  185. $this->fileType = FILES($inputName, "type");
  186. $this->fileSize = FILES($inputName, "size");
  187. $this->fileError = FILES($inputName, "error");
  188. $this->fileTmp = FILES($inputName, "tmp_name");
  189. } else {
  190. return FALSE;
  191. }
  192. $upload = $this->upload($dir);
  193. if(!$upload["upload"]) {
  194. return FALSE;
  195. }
  196. $this->Images = $this->core("Images");
  197. $this->Images->load($dir . $upload["filename"]);
  198. if($type === "normal") {
  199. return $dir . $upload["filename"];
  200. } elseif($type === "resize") {
  201. if($small) {
  202. $size["small"] = $this->Images->getResize("small", $dir, $upload["filename"]);
  203. }
  204. if($medium) {
  205. $size["medium"] = $this->Images->getResize("medium", $dir, $upload["filename"], _minMedium, _maxMedium);
  206. }
  207. if($original) {
  208. $size["original"] = $this->Images->getResize("original", $dir, $upload["filename"], _minOriginal, _maxOriginal);
  209. }
  210. @unlink($dir . $upload["filename"]);
  211. return $size;
  212. } elseif($type === "mural") {
  213. if($this->Images->getWidth() !== _muralWidth and $this->Images->getHeight() !== _muralHeight) {
  214. unlink($dir . $upload["filename"]);
  215. $alert["alert"] = getAlert("The mural image is too big");
  216. return $alert;
  217. } else {
  218. return $dir . $upload["filename"];
  219. }
  220. }
  221. }
  222. public function resize($dir, $filename) {
  223. $this->Images = $this->core("Images");
  224. $size["miniature"] = $this->Images->getResize("miniature", $dir, $filename);
  225. $size["medium"] = $this->Images->getResize("medium", $dir, $filename);
  226. $size["large"] = $this->Images->getResize("large", $dir, $filename);
  227. $size["original"] = $this->Images->getResize("original", $dir, $filename);
  228. return $size;
  229. }
  230. }