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

/upload/system/library/msfile.php

https://gitlab.com/chandanpasunoori/multimerch
PHP | 300 lines | 223 code | 65 blank | 12 comment | 71 complexity | 545016516bb45744fec67d10b2e8d03d MD5 | raw file
  1. <?php
  2. class MsFile extends Model {
  3. private function _isNewUpload($fileName) {
  4. return file_exists(DIR_IMAGE . $this->config->get('msconf_temp_image_path') . $fileName) || file_exists(DIR_DOWNLOAD . $this->config->get('msconf_temp_download_path') . $fileName);
  5. }
  6. // ***FUNCTION***: checks whether file already exists and proposes a new name for a file
  7. function _checkExistingFiles($path, $filename) {
  8. $newFilename = $filename;
  9. $i = 1;
  10. while (file_exists($path . '/' . $newFilename)) {
  11. $newFilename = substr($filename, 0, strrpos($filename, '.')) . "-" . $i++ . substr($filename, strrpos($filename, '.'));
  12. }
  13. return $newFilename;
  14. }
  15. function _checkExistingFilesSizes($path, $filename, $md5) {
  16. $newFilename = $filename;
  17. $i = 1;
  18. while ( file_exists($path . '/' . $newFilename) && ($filesize != filesize($path . '/' . $newFilename)) ) {
  19. $newFilename = substr($filename, 0, strrpos($filename, '.')) . "-" . $i++ . substr($filename, strrpos($filename, '.'));
  20. }
  21. return $newFilename;
  22. }
  23. function _checkExistingFilesMd5($path, $filename, $md5) {
  24. $newFilename = $filename;
  25. $i = 1;
  26. while ( file_exists($path . '/' . $newFilename) && ($md5 !== md5_file($path . '/' . $newFilename)) ) {
  27. $newFilename = substr($filename, 0, strrpos($filename, '.')) . "-" . $i++ . substr($filename, strrpos($filename, '.'));
  28. }
  29. return $newFilename;
  30. }
  31. public function checkPostMax($post, $files) {
  32. $errors = array();
  33. if (empty($post) || empty($files)) {
  34. $POST_MAX_SIZE = ini_get('post_max_size');
  35. $mul = substr($POST_MAX_SIZE, -1);
  36. $mul = ($mul == 'M' ? 1048576 : ($mul == 'K' ? 1024 : ($mul == 'G' ? 1073741824 : 1)));
  37. if ($_SERVER['CONTENT_LENGTH'] > $mul * (int)$POST_MAX_SIZE && $POST_MAX_SIZE) {
  38. $errors[] = $this->language->get('ms_error_file_size');
  39. } else {
  40. $errors[] = $this->language->get('ms_error_file_upload_error');
  41. }
  42. }
  43. return $errors;
  44. }
  45. public function checkFile($file, $allowed_filetypes) {
  46. $errors = array();
  47. $filetypes = explode(',', $allowed_filetypes);
  48. $filetypes = array_map('strtolower', $filetypes);
  49. $filetypes = array_map('trim', $filetypes);
  50. $ext = explode('.', $file['name']);
  51. $ext = end($ext);
  52. if (!in_array(strtolower($ext),$filetypes)) {
  53. $errors[] = $this->language->get('ms_error_file_extension');
  54. }
  55. if ($file["error"] != UPLOAD_ERR_OK) {
  56. if ($file["error"] == UPLOAD_ERR_INI_SIZE || $file["error"] == UPLOAD_ERR_FORM_SIZE) {
  57. $errors[] = $this->language->get('ms_error_file_size');
  58. } else {
  59. $errors[] = $this->language->get('ms_error_file_upload_error');
  60. }
  61. } else {
  62. // todo filename size
  63. if (mb_strlen($file['name']) > 150) {
  64. $errors[] = $this->language->get('ms_error_file_upload_error');
  65. }
  66. }
  67. return $errors;
  68. }
  69. public function checkDownload($file) {
  70. return $this->checkFile($file, $this->config->get('msconf_allowed_download_types'));
  71. }
  72. public function checkImage($file) {
  73. $errors = $this->checkFile($file, $this->config->get('msconf_allowed_image_types'));
  74. if (!$errors) {
  75. $size = getimagesize($file["tmp_name"]);
  76. list($width, $height, $type, $attr) = getimagesize($file["tmp_name"]);
  77. if (($this->config->get('msconf_min_uploaded_image_width') > 0 && $width < $this->config->get('msconf_min_uploaded_image_width')) || ($this->config->get('msconf_min_uploaded_image_height') > 0 && $height < $this->config->get('msconf_min_uploaded_image_height'))) {
  78. $errors[] = sprintf($this->language->get('ms_error_image_too_small'), $this->config->get('msconf_min_uploaded_image_width'), $this->config->get('msconf_min_uploaded_image_height'));
  79. } else if (($this->config->get('msconf_max_uploaded_image_width') > 0 && $width > $this->config->get('msconf_max_uploaded_image_width')) || ($this->config->get('msconf_max_uploaded_image_height') > 0 && $height > $this->config->get('msconf_max_uploaded_image_height'))) {
  80. $errors[] = sprintf($this->language->get('ms_error_image_too_big'), $this->config->get('msconf_max_uploaded_image_width'), $this->config->get('msconf_max_uploaded_image_height'));
  81. }
  82. //@TODO? Flash reports all files as octet-stream
  83. //if(!isset($size) || stripos($file['type'],'image/') === FALSE || stripos($size['mime'],'image/') === FALSE) {
  84. if(!isset($size)) {
  85. //var_dump('error');
  86. $errors[] = $this->language->get('ms_error_file_type');
  87. }
  88. }
  89. return $errors;
  90. }
  91. public function uploadImage($file) {
  92. $filename = time() . '_' . md5(rand()) . '.' . $file["name"];
  93. move_uploaded_file($file["tmp_name"], DIR_IMAGE . $this->config->get('msconf_temp_image_path') . $filename);
  94. if (!in_array($filename, $this->session->data['multiseller']['files'])) {
  95. $this->session->data['multiseller']['files'][] = $filename;
  96. }
  97. return $filename;
  98. }
  99. public function uploadDownload($file) {
  100. $filename = time() . '_' . md5(rand()) . '.' . $this->MsLoader->MsSeller->getNickname() . '_' . $file["name"];
  101. move_uploaded_file($file["tmp_name"], DIR_DOWNLOAD . $this->config->get('msconf_temp_download_path') . $filename);
  102. if (!in_array($filename, $this->session->data['multiseller']['files']))
  103. $this->session->data['multiseller']['files'][] = $filename;
  104. return array(
  105. 'fileName' => $filename,
  106. 'fileMask' => $file['name']
  107. );
  108. }
  109. public function checkFileAgainstSession($fileName) {
  110. if (array_search($fileName, $this->session->data['multiseller']['files']) === FALSE) {
  111. return FALSE;
  112. }
  113. return TRUE;
  114. }
  115. public function checkPredefinedAvatar($fileName) {
  116. return (strpos($fileName, $this->config->get('msconf_predefined_avatars_path'))===0 && file_exists(DIR_IMAGE . $fileName));
  117. }
  118. public function moveDownload($fileName) {
  119. $newpath = $fileName;
  120. $key = array_search($fileName, $this->session->data['multiseller']['files']);
  121. //strip nonce and timestamp
  122. $original_file_name = substr($fileName, strpos($fileName, '.') + 1, mb_strlen($fileName));
  123. //var_dump($original_file_name);
  124. if ($this->_isNewUpload($fileName)) {
  125. $newpath = $original_file_name . '.' . md5(rand());
  126. //var_dump($newpath);
  127. rename(DIR_DOWNLOAD . $this->config->get('msconf_temp_download_path') . $fileName, DIR_DOWNLOAD . $newpath);
  128. }
  129. unset ($this->session->data['multiseller']['files'][$key]);
  130. return $newpath;
  131. }
  132. public function moveImage($path) {
  133. if (!$this->checkPredefinedAvatar($path)) {
  134. $key = array_search($path, $this->session->data['multiseller']['files']);
  135. if ($key === FALSE) return;
  136. }
  137. $dirname = dirname($path) . '/';
  138. $filename = basename($path);
  139. $imageDir = $this->config->get('msconf_product_image_path');
  140. // Check if folder exists and create if not
  141. if (!is_dir(DIR_IMAGE . $imageDir . $this->customer->getId() . "/")) {
  142. mkdir(DIR_IMAGE . $imageDir . $this->customer->getId() . "/", 0755);
  143. @touch(DIR_IMAGE . $imageDir . $this->customer->getId() . "/" . 'index.html');
  144. }
  145. if ($dirname == './') {
  146. // new upload
  147. $dirname = $this->config->get('msconf_temp_image_path');
  148. //strip nonce and timestamp
  149. $originalFilename = $filename;
  150. $filename = substr($filename, strpos($filename, '.') + 1, mb_strlen($filename));
  151. }
  152. if (DIR_IMAGE . $imageDir . $this->customer->getId() . "/" . $filename != DIR_IMAGE . $path) {
  153. $newFilename = $this->_checkExistingFiles(DIR_IMAGE . $imageDir . $this->customer->getId(), $filename);
  154. $newPath = $imageDir . $this->customer->getId() . "/" . $newFilename;
  155. if ($this->checkPredefinedAvatar($path)) {
  156. copy(DIR_IMAGE . $dirname . (isset($originalFilename) ? $originalFilename : $filename), DIR_IMAGE . $newPath);
  157. } else {
  158. rename(DIR_IMAGE . $dirname . (isset($originalFilename) ? $originalFilename : $filename), DIR_IMAGE . $newPath);
  159. }
  160. } else {
  161. $newPath = $imageDir . $this->customer->getId() . "/" . $filename;
  162. }
  163. if (!$this->checkPredefinedAvatar($path)) {
  164. unset ($this->session->data['multiseller']['files'][$key]);
  165. }
  166. return $newPath;
  167. }
  168. public function deleteDownload($fileName) {
  169. if (empty($fileName))
  170. return false;
  171. $key = array_search($fileName, $this->session->data['multiseller']['files']);
  172. if (file_exists(DIR_DOWNLOAD . $fileName)) {
  173. unlink(DIR_DOWNLOAD. $fileName);
  174. }
  175. unset ($this->session->data['multiseller']['files'][$key]);
  176. }
  177. public function deleteImage($fileName) {
  178. if (empty($fileName))
  179. return false;
  180. $key = array_search($fileName, $this->session->data['multiseller']['files']);
  181. if (file_exists(DIR_IMAGE. $fileName)) {
  182. unlink(DIR_IMAGE. $fileName);
  183. }
  184. unset ($this->session->data['multiseller']['files'][$key]);
  185. }
  186. public function resizeImage($filename, $width, $height) {
  187. // todo consider using default cache folder
  188. if (!file_exists(DIR_IMAGE . $filename) || !$filename || !filesize(DIR_IMAGE . $filename)) {
  189. return;
  190. }
  191. $size = getimagesize(DIR_IMAGE . $filename);
  192. if (!$size) return;
  193. $info = pathinfo($filename);
  194. $extension = $info['extension'];
  195. $temporary_filename = time() . '_' . md5(rand()) . '.' . $info["basename"];
  196. $image = new Image(DIR_IMAGE . $filename);
  197. $image->resize($width, $height);
  198. $image->save(DIR_IMAGE . $this->config->get('msconf_temp_image_path') . $temporary_filename);
  199. $file = substr($info['basename'], 0, strrpos($info['basename'], '.')) . '-' . $width . 'x' . $height . '.' . $extension;
  200. $new_image = $this->_checkExistingFilesMd5(DIR_IMAGE . $this->config->get('msconf_temp_image_path'), $file, md5_file(DIR_IMAGE . $this->config->get('msconf_temp_image_path') . $temporary_filename));
  201. if (copy(DIR_IMAGE . $this->config->get('msconf_temp_image_path') . $temporary_filename, DIR_IMAGE . $this->config->get('msconf_temp_image_path') . $new_image)) {
  202. unlink(DIR_IMAGE . $this->config->get('msconf_temp_image_path') . $temporary_filename);
  203. }
  204. if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
  205. $base = defined('HTTPS_CATALOG') ? HTTPS_CATALOG : HTTPS_SERVER;
  206. return $base . 'image/' . $this->config->get('msconf_temp_image_path') . $new_image;
  207. } else {
  208. $base = defined('HTTP_CATALOG') ? HTTP_CATALOG : HTTP_SERVER;
  209. return $base . 'image/' . $this->config->get('msconf_temp_image_path') . $new_image;
  210. }
  211. }
  212. public function getPredefinedAvatars($path = '') {
  213. static $avatars = array();
  214. $dir = DIR_IMAGE . $this->config->get('msconf_predefined_avatars_path') . $path;
  215. $list = array_values(array_diff(scandir($dir), array('.', '..')));
  216. foreach ($list as $value) {
  217. $full_path = $dir . $value;
  218. if (is_dir($full_path) && is_readable($full_path)) {
  219. $this->getPredefinedAvatars($path . $value . '/');
  220. } elseif (is_file($full_path) && is_readable($full_path)) {
  221. $category = basename(dirname($full_path));
  222. if (!isset($avatars[$category])) {
  223. $avatars[$category] = array();
  224. }
  225. $avatars[$category][] = array(
  226. 'filename' => $value,
  227. 'dir' => $this->config->get('msconf_predefined_avatars_path') . $path, // image can be placed in any subfolder level, so dir not always the same as category
  228. 'image' => $this->resizeImage($this->config->get('msconf_predefined_avatars_path') . $path . $value, $this->config->get('msconf_preview_seller_avatar_image_width'), $this->config->get('msconf_preview_seller_avatar_image_height'))
  229. );
  230. }
  231. }
  232. return $avatars;
  233. }
  234. }
  235. ?>