/includes/classes/upload.php

https://github.com/yama/zencart-sugu · PHP · 215 lines · 166 code · 29 blank · 20 comment · 56 complexity · 15a4020c9e0cbf4426e47349454647e7 MD5 · raw file

  1. <?php
  2. /**
  3. * upload Class.
  4. *
  5. * @package classes
  6. * @copyright Copyright 2003-2006 Zen Cart Development Team
  7. * @copyright Portions Copyright 2003 osCommerce
  8. * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
  9. * @version $Id: upload.php 3041 2006-02-15 21:56:45Z wilt $
  10. */
  11. if (!defined('IS_ADMIN_FLAG')) {
  12. die('Illegal Access');
  13. }
  14. /**
  15. * upload Class.
  16. * This class is used to manage file uploads
  17. *
  18. * @package classes
  19. */
  20. class upload extends base {
  21. var $file, $filename, $destination, $permissions, $extensions, $tmp_filename, $message_location;
  22. function upload($file = '', $destination = '', $permissions = '666', $extensions = array() ) {
  23. $this->set_file($file);
  24. $this->set_destination($destination);
  25. $this->set_permissions($permissions);
  26. if (!zen_not_null($extensions)) {
  27. if (!defined(UPLOAD_FILENAME_EXTENSIONS)) define ('UPLOAD_FILENAME_EXTENSIONS','jpg,jpeg,gif,png,eps,cdr,ai,pdf,tif,tiff,bmp,zip');
  28. $extensions=explode(" ",preg_replace('/[.,;\s]+/',' ',UPLOAD_FILENAME_EXTENSIONS));
  29. }
  30. $this->set_extensions($extensions);
  31. $this->set_output_messages('direct');
  32. if (zen_not_null($this->file) && zen_not_null($this->destination)) {
  33. $this->set_output_messages('session');
  34. if ( ($this->parse() == true) && ($this->save() == true) ) {
  35. return true;
  36. } else {
  37. // self destruct
  38. while(list($key,) = each($this)) {
  39. $this->$key = null;
  40. }
  41. return false;
  42. }
  43. }
  44. }
  45. // iii Added: $key to differentiate between different files uploaded
  46. function parse($key = '') {
  47. global $messageStack;
  48. if (isset($_FILES[$this->file])) {
  49. if (zen_not_null($key)) {
  50. $file = array('name' => $_FILES[$this->file]['name'][$key],
  51. 'type' => $_FILES[$this->file]['type'][$key],
  52. 'size' => $_FILES[$this->file]['size'][$key],
  53. 'tmp_name' => $_FILES[$this->file]['tmp_name'][$key]);
  54. } else {
  55. $file = array('name' => $_FILES[$this->file]['name'],
  56. 'type' => $_FILES[$this->file]['type'],
  57. 'size' => $_FILES[$this->file]['size'],
  58. 'tmp_name' => $_FILES[$this->file]['tmp_name']);
  59. }
  60. } elseif (isset($GLOBALS['HTTP_POST_FILES'][$this->file])) {
  61. global $HTTP_POST_FILES;
  62. $file = array('name' => $HTTP_POST_FILES[$this->file]['name'],
  63. 'type' => $HTTP_POST_FILES[$this->file]['type'],
  64. 'size' => $HTTP_POST_FILES[$this->file]['size'],
  65. 'tmp_name' => $HTTP_POST_FILES[$this->file]['tmp_name']);
  66. } else {
  67. $file = array('name' => (isset($GLOBALS[$this->file . '_name']) ? $GLOBALS[$this->file . '_name'] : ''),
  68. 'type' => (isset($GLOBALS[$this->file . '_type']) ? $GLOBALS[$this->file . '_type'] : ''),
  69. 'size' => (isset($GLOBALS[$this->file . '_size']) ? $GLOBALS[$this->file . '_size'] : ''),
  70. 'tmp_name' => (isset($GLOBALS[$this->file]) ? $GLOBALS[$this->file] : ''));
  71. }
  72. //if (!zen_not_null($file['tmp_name'])) return false;
  73. //if ($file['tmp_name'] == 'none') return false;
  74. //if (!is_uploaded_file($file['tmp_name'])) return false;
  75. if ( zen_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none') && is_uploaded_file($file['tmp_name']) ) {
  76. if (zen_not_null($file['size']) and ($file['size'] > MAX_FILE_UPLOAD_SIZE)) {
  77. if ($this->message_location == 'direct') {
  78. $messageStack->add('header', ERROR_FILE_TOO_BIG, 'error');
  79. } else {
  80. $messageStack->add_session('upload', ERROR_FILE_TOO_BIG, 'error');
  81. }
  82. return false;
  83. }
  84. if (sizeof($this->extensions) > 0) {
  85. if (!in_array(strtolower(substr($file['name'], strrpos($file['name'], '.')+1)), $this->extensions)) {
  86. if ($this->message_location == 'direct') {
  87. $messageStack->add('header', ERROR_FILETYPE_NOT_ALLOWED . ' ' . UPLOAD_FILENAME_EXTENSIONS, 'error');
  88. } else {
  89. $messageStack->add_session('upload', ERROR_FILETYPE_NOT_ALLOWED . ' - ' . UPLOAD_FILENAME_EXTENSIONS, 'error');
  90. }
  91. return false;
  92. }
  93. }
  94. $this->set_file($file);
  95. $this->set_filename($file['name']);
  96. $this->set_tmp_filename($file['tmp_name']);
  97. return $this->check_destination();
  98. } else {
  99. if ($this->message_location == 'direct') {
  100. $messageStack->add('header', WARNING_NO_FILE_UPLOADED, 'warning');
  101. } else {
  102. $messageStack->add_session('upload', WARNING_NO_FILE_UPLOADED, 'warning');
  103. }
  104. return false;
  105. }
  106. }
  107. function save() {
  108. global $messageStack;
  109. if (substr($this->destination, -1) != '/') $this->destination .= '/';
  110. if (move_uploaded_file($this->file['tmp_name'], $this->destination . $this->filename)) {
  111. chmod($this->destination . $this->filename, $this->permissions);
  112. if ($this->message_location == 'direct') {
  113. $messageStack->add('header', SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');
  114. } else {
  115. $messageStack->add_session('upload', SUCCESS_FILE_SAVED_SUCCESSFULLY, 'success');
  116. }
  117. return true;
  118. } else {
  119. if ($this->message_location == 'direct') {
  120. $messageStack->add('header', ERROR_FILE_NOT_SAVED, 'error');
  121. } else {
  122. $messageStack->add_session('upload', ERROR_FILE_NOT_SAVED, 'error');
  123. }
  124. return false;
  125. }
  126. }
  127. function set_file($file) {
  128. $this->file = $file;
  129. }
  130. function set_destination($destination) {
  131. $this->destination = $destination;
  132. }
  133. function set_permissions($permissions) {
  134. $this->permissions = octdec($permissions);
  135. }
  136. function set_filename($filename) {
  137. $this->filename = $filename;
  138. }
  139. function set_tmp_filename($filename) {
  140. $this->tmp_filename = $filename;
  141. }
  142. function set_extensions($extensions) {
  143. if (zen_not_null($extensions)) {
  144. if (is_array($extensions)) {
  145. $this->extensions = $extensions;
  146. } else {
  147. $this->extensions = array($extensions);
  148. }
  149. } else {
  150. $this->extensions = array();
  151. }
  152. }
  153. function check_destination() {
  154. global $messageStack;
  155. if (!is_writeable($this->destination)) {
  156. if (is_dir($this->destination)) {
  157. if ($this->message_location == 'direct') {
  158. $messageStack->add('header', sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error');
  159. } else {
  160. $messageStack->add_session('upload', sprintf(ERROR_DESTINATION_NOT_WRITEABLE, $this->destination), 'error');
  161. }
  162. } else {
  163. if ($this->message_location == 'direct') {
  164. $messageStack->add('header', sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error');
  165. } else {
  166. $messageStack->add_session('upload', sprintf(ERROR_DESTINATION_DOES_NOT_EXIST, $this->destination), 'error');
  167. }
  168. }
  169. return false;
  170. } else {
  171. return true;
  172. }
  173. }
  174. function set_output_messages($location) {
  175. switch ($location) {
  176. case 'session':
  177. $this->message_location = 'session';
  178. break;
  179. case 'direct':
  180. default:
  181. $this->message_location = 'direct';
  182. break;
  183. }
  184. }
  185. }
  186. ?>