PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/Uploader.php

https://gitlab.com/staging06/myproject
PHP | 292 lines | 220 code | 46 blank | 26 comment | 30 complexity | 58cf7400c08a39305224c84a391cef59 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class UploaderCore
  27. {
  28. const DEFAULT_MAX_SIZE = 10485760;
  29. private $_check_file_size;
  30. private $_accept_types;
  31. private $_files;
  32. private $_max_size;
  33. private $_name;
  34. private $_save_path;
  35. public function __construct($name = null)
  36. {
  37. $this->setName($name);
  38. $this->setCheckFileSize(true);
  39. $this->files = array();
  40. }
  41. public function setAcceptTypes($value)
  42. {
  43. $this->_accept_types = $value;
  44. return $this;
  45. }
  46. public function getAcceptTypes()
  47. {
  48. return $this->_accept_types;
  49. }
  50. public function setCheckFileSize($value)
  51. {
  52. $this->_check_file_size = $value;
  53. return $this;
  54. }
  55. public function getFilePath($file_name = null)
  56. {
  57. if (!isset($file_name)) {
  58. return tempnam($this->getSavePath(), $this->getUniqueFileName());
  59. }
  60. return $this->getSavePath().$file_name;
  61. }
  62. public function getFiles()
  63. {
  64. if (!isset($this->_files)) {
  65. $this->_files = array();
  66. }
  67. return $this->_files;
  68. }
  69. public function setMaxSize($value)
  70. {
  71. $this->_max_size = intval($value);
  72. return $this;
  73. }
  74. public function getMaxSize()
  75. {
  76. if (!isset($this->_max_size) || empty($this->_max_size)) {
  77. $this->setMaxSize(self::DEFAULT_MAX_SIZE);
  78. }
  79. return $this->_max_size;
  80. }
  81. public function setName($value)
  82. {
  83. $this->_name = $value;
  84. return $this;
  85. }
  86. public function getName()
  87. {
  88. return $this->_name;
  89. }
  90. public function setSavePath($value)
  91. {
  92. $this->_save_path = $value;
  93. return $this;
  94. }
  95. public function getPostMaxSizeBytes()
  96. {
  97. $post_max_size = ini_get('post_max_size');
  98. $bytes = trim($post_max_size);
  99. $last = strtolower($post_max_size[strlen($post_max_size) - 1]);
  100. switch ($last) {
  101. case 'g': $bytes *= 1024;
  102. case 'm': $bytes *= 1024;
  103. case 'k': $bytes *= 1024;
  104. }
  105. if ($bytes == '') {
  106. $bytes = null;
  107. }
  108. return $bytes;
  109. }
  110. public function getSavePath()
  111. {
  112. if (!isset($this->_save_path)) {
  113. $this->setSavePath(_PS_UPLOAD_DIR_);
  114. }
  115. return $this->_normalizeDirectory($this->_save_path);
  116. }
  117. public function getUniqueFileName($prefix = 'PS')
  118. {
  119. return uniqid($prefix, true);
  120. }
  121. public function checkFileSize()
  122. {
  123. return (isset($this->_check_file_size) && $this->_check_file_size);
  124. }
  125. public function process($dest = null)
  126. {
  127. $upload = isset($_FILES[$this->getName()]) ? $_FILES[$this->getName()] : null;
  128. if ($upload && is_array($upload['tmp_name'])) {
  129. $tmp = array();
  130. foreach ($upload['tmp_name'] as $index => $value) {
  131. $tmp[$index] = array(
  132. 'tmp_name' => $upload['tmp_name'][$index],
  133. 'name' => $upload['name'][$index],
  134. 'size' => $upload['size'][$index],
  135. 'type' => $upload['type'][$index],
  136. 'error' => $upload['error'][$index]
  137. );
  138. $this->files[] = $this->upload($tmp[$index], $dest);
  139. }
  140. } elseif ($upload) {
  141. $this->files[] = $this->upload($upload, $dest);
  142. }
  143. return $this->files;
  144. }
  145. public function upload($file, $dest = null)
  146. {
  147. if ($this->validate($file)) {
  148. if (isset($dest) && is_dir($dest)) {
  149. $file_path = $dest;
  150. } else {
  151. $file_path = $this->getFilePath(isset($dest) ? $dest : $file['name']);
  152. }
  153. if ($file['tmp_name'] && is_uploaded_file($file['tmp_name'])) {
  154. move_uploaded_file($file['tmp_name'], $file_path);
  155. } else {
  156. // Non-multipart uploads (PUT method support)
  157. file_put_contents($file_path, fopen('php://input', 'r'));
  158. }
  159. $file_size = $this->_getFileSize($file_path, true);
  160. if ($file_size === $file['size']) {
  161. $file['save_path'] = $file_path;
  162. } else {
  163. $file['size'] = $file_size;
  164. unlink($file_path);
  165. $file['error'] = Tools::displayError('Server file size is different from local file size');
  166. }
  167. }
  168. return $file;
  169. }
  170. protected function checkUploadError($error_code)
  171. {
  172. $error = 0;
  173. switch ($error_code) {
  174. case 1:
  175. $error = sprintf(Tools::displayError('The uploaded file exceeds %s'), ini_get('upload_max_filesize'));
  176. break;
  177. case 2:
  178. $error = sprintf(Tools::displayError('The uploaded file exceeds %s'), ini_get('post_max_size'));
  179. break;
  180. case 3:
  181. $error = Tools::displayError('The uploaded file was only partially uploaded');
  182. break;
  183. case 4:
  184. $error = Tools::displayError('No file was uploaded');
  185. break;
  186. case 6:
  187. $error = Tools::displayError('Missing temporary folder');
  188. break;
  189. case 7:
  190. $error = Tools::displayError('Failed to write file to disk');
  191. break;
  192. case 8:
  193. $error = Tools::displayError('A PHP extension stopped the file upload');
  194. break;
  195. default:
  196. break;
  197. }
  198. return $error;
  199. }
  200. protected function validate(&$file)
  201. {
  202. $file['error'] = $this->checkUploadError($file['error']);
  203. $post_max_size = $this->getPostMaxSizeBytes();
  204. if ($post_max_size && ($this->_getServerVars('CONTENT_LENGTH') > $post_max_size)) {
  205. $file['error'] = Tools::displayError('The uploaded file exceeds the post_max_size directive in php.ini');
  206. return false;
  207. }
  208. if (preg_match('/\%00/', $file['name'])) {
  209. $file['error'] = Tools::displayError('Invalid file name');
  210. return false;
  211. }
  212. $types = $this->getAcceptTypes();
  213. //TODO check mime type.
  214. if (isset($types) && !in_array(pathinfo($file['name'], PATHINFO_EXTENSION), $types)) {
  215. $file['error'] = Tools::displayError('Filetype not allowed');
  216. return false;
  217. }
  218. if ($this->checkFileSize() && $file['size'] > $this->getMaxSize()) {
  219. $file['error'] = sprintf(Tools::displayError('File (size : %1s) is too big (max : %2s)'), $file['size'], $this->getMaxSize());
  220. return false;
  221. }
  222. return true;
  223. }
  224. protected function _getFileSize($file_path, $clear_stat_cache = false)
  225. {
  226. if ($clear_stat_cache) {
  227. clearstatcache(true, $file_path);
  228. }
  229. return filesize($file_path);
  230. }
  231. protected function _getServerVars($var)
  232. {
  233. return (isset($_SERVER[$var]) ? $_SERVER[$var] : '');
  234. }
  235. protected function _normalizeDirectory($directory)
  236. {
  237. $last = $directory[strlen($directory) - 1];
  238. if (in_array($last, array('/', '\\'))) {
  239. $directory[strlen($directory) - 1] = DIRECTORY_SEPARATOR;
  240. return $directory;
  241. }
  242. $directory .= DIRECTORY_SEPARATOR;
  243. return $directory;
  244. }
  245. }