PageRenderTime 97ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/ php-ppcms/includes/classes/core.upload.class.php

http://php-ppcms.googlecode.com/
PHP | 928 lines | 760 code | 142 blank | 26 comment | 213 complexity | 8c7ef105975c1ea0c36a08d927676baa MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. * (c) 2009, jianyuzhu@gmail.com
  5. * All rights reserved
  6. * This script is part of the PPEMI project.
  7. ***************************************************************/
  8. class CoreUpload {
  9. var $file;
  10. var $filename;
  11. var $filetype;
  12. var $filesize;
  13. var $fileerror;
  14. var $tmp_filename;
  15. var $save_filename;
  16. var $save_file;
  17. var $extension;
  18. var $destination;
  19. var $permissions;
  20. var $extensions;
  21. var $types;
  22. var $maxsize = 0;
  23. var $message_location;
  24. var $destination_type = 'random';
  25. var $destination_subdir;
  26. var $rand_filename = true;
  27. //constructor
  28. function CoreUpload($file = '', $destination = '', $permissions = '777', $extensions = '') {
  29. $this->setFile($file);
  30. //$this->parseExtension();
  31. $this->setDestination($destination);
  32. $this->setPermissions($permissions);
  33. $this->setExtensions($extensions);
  34. $this->setOutputMessages('direct');
  35. if( $this->_not_null($this->file) && $this->_not_null($this->destination)) {
  36. $this->setOutputMessages('session');
  37. if( ($this->parse() == true) && ($this->save() == true) ) {
  38. return true;
  39. } else {
  40. return false;
  41. }
  42. }
  43. }
  44. //methods
  45. function parse() {
  46. $has_error = false;
  47. if( isset($_FILES[$this->file]) ) {
  48. $file = array('name' => $_FILES[$this->file]['name'],
  49. 'type' => $_FILES[$this->file]['type'],
  50. 'size' => $_FILES[$this->file]['size'],
  51. 'tmp_name' => $_FILES[$this->file]['tmp_name']);
  52. } elseif( isset($GLOBALS['HTTP_POST_FILES'][$this->file]) ) {
  53. global $HTTP_POST_FILES;
  54. $file = array('name' => $HTTP_POST_FILES[$this->file]['name'],
  55. 'type' => $HTTP_POST_FILES[$this->file]['type'],
  56. 'size' => $HTTP_POST_FILES[$this->file]['size'],
  57. 'tmp_name' => $HTTP_POST_FILES[$this->file]['tmp_name']);
  58. } else {
  59. $file = array('name' => (isset($GLOBALS[$this->file . '_name']) ? $GLOBALS[$this->file . '_name'] : ''),
  60. 'type' => (isset($GLOBALS[$this->file . '_type']) ? $GLOBALS[$this->file . '_type'] : ''),
  61. 'size' => (isset($GLOBALS[$this->file . '_size']) ? $GLOBALS[$this->file . '_size'] : ''),
  62. 'tmp_name' => (isset($GLOBALS[$this->file]) ? $GLOBALS[$this->file] : ''));
  63. }
  64. if( $this->_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none') && is_uploaded_file($file['tmp_name']) ) {
  65. if( !empty($this->extensions) && !in_array(strtolower(substr($file['name'], strrpos($file['name'], '.')+1)), $this->extensions)) {
  66. $this->_ms_add('top', 'filetype_not_allowed', 'error');
  67. return false;
  68. }
  69. $this->setFile($file);
  70. $this->setFilename($file['name']);
  71. $this->parseExtension();
  72. $this->setFiletype($file['type']);
  73. $this->setTmpFilename($file['tmp_name']);
  74. if( $this->checkFile($file) == false ) {
  75. $has_error = true;
  76. }
  77. } else {
  78. $this->_ms_add('top', 'no_file_uploaded', 'warning');
  79. $has_error = true;
  80. }
  81. if( $has_error == true ) {
  82. return false;
  83. }
  84. return $this->checkDestination();
  85. }
  86. function save() {
  87. if( substr($this->destination, -1) != '/' ) {
  88. $this->destination .= '/';
  89. }
  90. if( $this->rand_filename == true || strlen($this->filename) == 0 ) {
  91. $this->save_filename = $this->_generate_filename();
  92. } else {
  93. //$this->save_filename = $this->filename;
  94. $this->save_filename = $this->_filter_filename($this->filename);
  95. }
  96. if( strlen($this->destination_subdir) > 0 ) {
  97. $this->save_filename = $this->destination_subdir . '/' . $this->save_filename;
  98. }
  99. $savepath = $this->destination . $this->save_filename;
  100. if( move_uploaded_file($this->file['tmp_name'], $savepath)) {
  101. chmod($savepath, $this->permissions);
  102. $this->save_file = $savepath;
  103. $this->_ms_add('top', 'file_saved_success', 'success');
  104. return true;
  105. } else {
  106. $this->_ms_add('top', 'file_not_saved', 'error');
  107. return false;
  108. }
  109. }
  110. function upload() {
  111. if( ($this->parse() == true) && ($this->save() == true) ) {
  112. return true;
  113. } else {
  114. return false;
  115. }
  116. }
  117. //
  118. function checkFile($file) {
  119. $extension = strtolower(substr($file['name'], strrpos($file['name'], '.')+1));
  120. $type = $file['type'];
  121. $size = $file['size'];
  122. if( $this->checkFileExt($extension) == false || $this->checkFileType($type) == false || $this->checkFileSize($size) == false ) {
  123. return false;
  124. }
  125. return true;
  126. }
  127. function checkFileExt($extension) {
  128. if( !empty($this->extensions) && !in_array(strtolower($extension), $this->extensions)) {
  129. $this->_ms_add('top', 'filetype_not_allowed', 'error');
  130. return false;
  131. }
  132. return true;
  133. }
  134. function checkFileType($type) {
  135. if( !empty($this->types) && !in_array(strtolower($type), $this->types) ) {
  136. $this->_ms_add('top', 'filetype_not_allowed', 'error');
  137. return false;
  138. }
  139. return true;
  140. }
  141. function checkFileSize($size) {
  142. if( $this->maxsize > 0 && $this->maxsize < $size ) {
  143. $this->_ms_add('top', 'filesize_not_allowed', 'error');
  144. return false;
  145. }
  146. return true;
  147. }
  148. function checkFileUploadable($file) {
  149. return is_uploaded_file($file);
  150. }
  151. function checkDestination() {
  152. $has_error = false;
  153. if( !is_writable($this->destination) ) {
  154. if( is_dir($this->destination) ) {
  155. $this->_ms_add('top', array('destination_not_writable', $this->destination), 'error');
  156. } else {
  157. $this->_ms_add('top', array('destination_not_exist', $this->destination), 'error');
  158. }
  159. $has_error = true;
  160. } else {
  161. $this->destination_subdir = '';
  162. if( $this->destination_type == 'month' ) {
  163. $this->destination_subdir = strftime("%Y%m", time());
  164. } elseif( $this->destination_type == 'day' ) {
  165. $this->destination_subdir = strftime("%Y%m%d", time());
  166. } elseif( $this->destination_type == 'random' ) {
  167. $this->destination_subdir = substr(md5(strftime("%Y%m", time())), 0, 6);
  168. }
  169. $this->destination_subdir = trim($this->destination_subdir);
  170. if( strlen($this->destination_subdir) > 0 ) {
  171. if( !is_dir($this->destination . $this->destination_subdir . '/') ) {
  172. @mkdir($this->destination . $this->destination_subdir . '/');
  173. }
  174. if( !is_writable($this->destination . $this->destination_subdir . '/') ) {
  175. if( is_dir($this->destination . $this->destination_subdir . '/') ) {
  176. $this->_ms_add('top', array('destination_not_writable', $this->destination . $this->destination_subdir . '/'), 'error');
  177. } else {
  178. $this->_ms_add('top', array('destination_not_exist', $this->destination . $this->destination_subdir . '/'), 'error');
  179. }
  180. $has_error = true;
  181. }
  182. }
  183. }
  184. if( $has_error == true ) {
  185. return false;
  186. }
  187. return true;
  188. }
  189. function parseExtension() {
  190. if( $this->filename ) {
  191. $this->extension = strtolower(substr($this->filename, strrpos($this->filename, '.')+1));
  192. } else {
  193. $this->extension = strtolower(substr($this->file['name'], strrpos($this->file['name'], '.')+1));
  194. }
  195. }
  196. //SETTER/GETTER
  197. function setFile($file) {
  198. $this->file = $file;
  199. }
  200. function getFile() {
  201. return $this->file;
  202. }
  203. function set_rand_filename($is_rand = true) {
  204. if( $is_rand == true ) {
  205. $this->rand_filename = true;
  206. } else {
  207. $this->rand_filename = false;
  208. }
  209. }
  210. function setRandFilename($is_rand = true) {
  211. if( $is_rand == true ) {
  212. $this->rand_filename = true;
  213. } else {
  214. $this->rand_filename = false;
  215. }
  216. }
  217. function getRandFilename() {
  218. return $this->rand_filename;
  219. }
  220. function setExtension($extension) {
  221. $this->extension = $extension;
  222. }
  223. function getExtension() {
  224. return $this->extension;
  225. }
  226. function setDestination($destination) {
  227. $this->destination = $destination;
  228. }
  229. function getDestination() {
  230. return $this->destinaction;
  231. }
  232. function setDestinationType($destination_type) {
  233. $this->destination_type = $destination_type;
  234. }
  235. function getDestinationType() {
  236. return $this->destination_type;
  237. }
  238. function setDestinationSubDir($destination_subdir) {
  239. $this->destination_subdir = $destination_subdir;
  240. }
  241. function getDestinationSubDir() {
  242. return $this->destination_subdir;
  243. }
  244. function setPermissions($permissions) {
  245. $this->permissions = octdec($permissions);
  246. }
  247. function getPermissions() {
  248. return $this->permissions;
  249. }
  250. function setFilename($filename) {
  251. $this->filename = $filename;
  252. }
  253. function getFilename() {
  254. return $this->filename;
  255. }
  256. function setFiletype($filetype) {
  257. $this->filetype = $filetype;
  258. }
  259. function getFiletype() {
  260. return $this->filetype;
  261. }
  262. function setTmpFilename($filename) {
  263. $this->tmp_filename = $filename;
  264. }
  265. function getTmpFilename() {
  266. return $this->tmp_filename;
  267. }
  268. function getSaveFilename() {
  269. return $this->save_filename;
  270. }
  271. function setExtensions($extensions) {
  272. if( $this->_not_null($extensions) ) {
  273. if( is_array($extensions) ) {
  274. $this->extensions = $extensions;
  275. } else {
  276. $this->extensions = array($extensions);
  277. }
  278. } else {
  279. $this->extensions = array();
  280. }
  281. }
  282. function getExtensions() {
  283. return $this->extensions;
  284. }
  285. function setOutputMessages($location) {
  286. switch($location) {
  287. case 'session':
  288. $this->message_location = 'session';
  289. break;
  290. case 'direct':
  291. default:
  292. $this->message_location = 'direct';
  293. break;
  294. }
  295. }
  296. function getOutputMessages() {
  297. return $this->message_location;
  298. }
  299. function getMessages() {
  300. return $this->_messagestacks;
  301. }
  302. //private
  303. function _not_null($value) {
  304. return util_not_null($value);
  305. }
  306. function _random() {
  307. return util_create_random_value('8', 'mixed');
  308. }
  309. function _generate_filename() {
  310. //return md5(time()) . '.' . $this->extension;
  311. return substr(md5(time()), 0, 16) . '_' . $this->_random() . '.' . $this->extension;
  312. }
  313. function _filter_filename($filename = '', $check = true, $rand = false) {
  314. if( $filename == '' ) {
  315. $filename = $this->filename;
  316. }
  317. $name = $filename;
  318. $filename = substr($filename, 0, -(strlen($this->extension)+1));
  319. $filename = stripslashes($filename);
  320. $filename = trim(ereg_replace("[^A-Za-z0-9\-\_]", " ", $filename));
  321. $filename = ereg_replace("( {1,})", "-", $filename);
  322. $filename = trim(substr($filename, 0, 50));
  323. if( $rand == true ) {
  324. $filename .= '_' . $this->_random();
  325. }
  326. $filename .= '.' . $this->extension;
  327. if( $check == true ) {
  328. if( strlen($this->destination_subdir) > 0 ) {
  329. $savefile = $this->destination . $this->destination_subdir . '/' . $filename;
  330. } else {
  331. $savefile = $this->destination . '/' . $filename;
  332. }
  333. if( file_exists($savefile) ) {
  334. $filename = $this->_filter_filename($name, $check, true);
  335. }
  336. }
  337. return $filename;
  338. }
  339. function _ms_add($class, $message, $type = 'error') {
  340. global $messagestackObj;
  341. if( is_array($message) ) {
  342. $message = sprintf(_lang($message['0'], 'upload'), $message['1']);
  343. } else {
  344. $message = _lang($message, 'upload');
  345. }
  346. if( $this->message_location != 'direct' && is_object($messagestackObj) ) {
  347. $messagestackObj->add($class, $message, $type);
  348. } else {
  349. $this->_messagestacks[] = array('class' => $class, 'message' => $message, 'type' => $type);
  350. }
  351. }
  352. function _ms_add_session($class, $message, $type = 'error') {
  353. global $messagestackObj;
  354. if( is_array($message) ) {
  355. $message = sprintf(_lang($message['0'], 'upload'), $message['1']);
  356. } else {
  357. $message = _lang($message, 'upload');
  358. }
  359. if( $this->message_location != 'direct' && is_object($messagestackObj) ) {
  360. $messagestackObj->add_session($class, $message, $type);
  361. } else {
  362. $this->_messagestacks[] = array('class' => $class, 'message' => $message, 'type' => $type);
  363. }
  364. }
  365. }
  366. //MultiUpload
  367. class CoreMultiUpload {
  368. var $file;
  369. var $filename;
  370. var $filetype;
  371. var $filesize;
  372. var $fileerror;
  373. var $tmp_filename;
  374. var $save_filename;
  375. var $save_file;
  376. var $extension;
  377. var $destination;
  378. var $permissions;
  379. var $extensions;
  380. var $types;
  381. var $maxsize = 0;
  382. var $message_location;
  383. var $destination_type = 'random';
  384. var $destination_subdir;
  385. var $rand_filename = true;
  386. //constructor
  387. function CoreMultiUpload($file = '', $destination = '', $permissions = '777', $extensions = '') {
  388. $this->setFile($file);
  389. //$this->parseExtension();
  390. $this->setDestination($destination);
  391. $this->setPermissions($permissions);
  392. $this->setExtensions($extensions);
  393. $this->setOutputMessages('direct');
  394. if( $this->_not_null($this->file) && $this->_not_null($this->destination)) {
  395. $this->setOutputMessages('session');
  396. if( ($this->parse() == true) && ($this->save() == true) ) {
  397. return true;
  398. } else {
  399. return false;
  400. }
  401. }
  402. }
  403. //methods
  404. function parse() {
  405. $has_error = false;
  406. if( substr($this->destination, -1) != '/' ) {
  407. $this->destination .= '/';
  408. }
  409. if( isset($_FILES[$this->file]) ) {
  410. $file = array('name' => $_FILES[$this->file]['name'],
  411. 'type' => $_FILES[$this->file]['type'],
  412. 'size' => $_FILES[$this->file]['size'],
  413. 'tmp_name' => $_FILES[$this->file]['tmp_name']);
  414. } elseif( isset($GLOBALS['HTTP_POST_FILES'][$this->file]) ) {
  415. global $HTTP_POST_FILES;
  416. $file = array('name' => $HTTP_POST_FILES[$this->file]['name'],
  417. 'type' => $HTTP_POST_FILES[$this->file]['type'],
  418. 'size' => $HTTP_POST_FILES[$this->file]['size'],
  419. 'tmp_name' => $HTTP_POST_FILES[$this->file]['tmp_name']);
  420. } else {
  421. $file = array('name' => (isset($GLOBALS[$this->file . '_name']) ? $GLOBALS[$this->file . '_name'] : ''),
  422. 'type' => (isset($GLOBALS[$this->file . '_type']) ? $GLOBALS[$this->file . '_type'] : ''),
  423. 'size' => (isset($GLOBALS[$this->file . '_size']) ? $GLOBALS[$this->file . '_size'] : ''),
  424. 'tmp_name' => (isset($GLOBALS[$this->file]) ? $GLOBALS[$this->file] : ''));
  425. }
  426. if( is_array($file['tmp_name']) ) {
  427. foreach($file['tmp_name'] as $k => $v) {
  428. if( $this->_not_null($v) && ($v != 'none') && is_uploaded_file($v) ) {
  429. if( !empty($this->extensions) && !in_array(strtolower(substr($v, strrpos($v, '.')+1)), $this->extensions)) {
  430. $this->_ms_add('top', 'filetype_not_allowed', 'error');
  431. $has_error = true;
  432. }
  433. $this->setFile($file);
  434. $this->setFilename($file['name']);
  435. //$this->parseExtension($file['name'][$k]);
  436. $this->setFiletype($file['type']);
  437. $this->setFilesize($file['size']);
  438. $this->setTmpFilename($file['tmp_name']);
  439. } else {
  440. $this->_ms_add('top', 'no_file_uploaded', 'warning');
  441. //$has_error = true;
  442. }
  443. }
  444. } else {
  445. if( $this->_not_null($file['tmp_name']) && ($file['tmp_name'] != 'none') && is_uploaded_file($file['tmp_name']) ) {
  446. if( !empty($this->extensions) && !in_array(strtolower(substr($file['name'], strrpos($file['name'], '.')+1)), $this->extensions)) {
  447. $this->_ms_add('top', 'filetype_not_allowed', 'error');
  448. $has_error = true;
  449. }
  450. $this->setFile($file);
  451. $this->setFilename($file['name']);
  452. $this->parseExtension();
  453. $this->setFiletype($file['type']);
  454. $this->setFilesize($file['size']);
  455. $this->setTmpFilename($file['tmp_name']);
  456. if( $this->checkFile($file) == false ) {
  457. $has_error = true;
  458. }
  459. } else {
  460. $this->_ms_add('top', 'no_file_uploaded', 'warning');
  461. $has_error = true;
  462. }
  463. }
  464. if( $has_error == true ) {
  465. return false;
  466. }
  467. return $this->checkDestination();
  468. }
  469. function save() {
  470. if( is_array($this->file['tmp_name']) ) {
  471. foreach($this->file['tmp_name'] as $k => $v) {
  472. $this->parseExtension($this->file['name'][$k]);
  473. if( $this->rand_filename == true || strlen($this->filename[$k]) == 0 ) {
  474. $save_filename = $this->_generate_filename();
  475. } else {
  476. $save_filename = $this->filename[$k];
  477. $save_filename = $this->_filter_filename($save_filename);
  478. }
  479. if( strlen($this->destination_subdir) > 0 ) {
  480. $save_filename = $this->destination_subdir . '/' . $save_filename;
  481. }
  482. $savepath = $this->destination . $save_filename;
  483. if( move_uploaded_file($v, $savepath) ) {
  484. chmod($savepath, $this->permissions);
  485. $this->save_filename[] = $save_filename;
  486. $this->save_file[] = $savepath;
  487. $this->_ms_add('top', 'file_saved_success', 'success');
  488. } else {
  489. $this->save_filename[] = '';
  490. $this->save_file[] = '';
  491. $this->_ms_add('top', 'file_not_saved', 'error');
  492. }
  493. }
  494. return true;
  495. } else {
  496. if( $this->rand_filename == true || strlen($this->filename) == 0 ) {
  497. $this->save_filename = $this->_generate_filename();
  498. } else {
  499. //$this->save_filename = $this->filename;
  500. $this->save_filename = $this->_filter_filename($this->filename);
  501. }
  502. if( strlen($this->destination_subdir) > 0 ) {
  503. $this->save_filename = $this->destination_subdir . '/' . $this->save_filename;
  504. }
  505. $savepath = $this->destination . $this->save_filename;
  506. if( move_uploaded_file($this->file['tmp_name'], $savepath) ) {
  507. chmod($savepath, $this->permissions);
  508. $this->save_file = $savepath;
  509. $this->_ms_add('top', 'file_saved_success', 'success');
  510. return true;
  511. } else {
  512. $this->_ms_add('top', 'file_not_saved', 'error');
  513. return false;
  514. }
  515. }
  516. }
  517. function upload() {
  518. if( ($this->parse() == true) && ($this->save() == true) ) {
  519. return true;
  520. } else {
  521. return false;
  522. }
  523. }
  524. //
  525. function checkFile($file) {
  526. $extension = strtolower(substr($file['name'], strrpos($file['name'], '.')+1));
  527. $type = $file['type'];
  528. $size = $file['size'];
  529. if( $this->checkFileExt($extension) == false || $this->checkFileType($type) == false || $this->checkFileSize($size) == false ) {
  530. return false;
  531. }
  532. return true;
  533. }
  534. function checkFileExt($extension) {
  535. if( !empty($this->extensions) && !in_array(strtolower($extension), $this->extensions)) {
  536. $this->_ms_add('top', 'filetype_not_allowed', 'error');
  537. return false;
  538. }
  539. return true;
  540. }
  541. function checkFileType($type) {
  542. if( !empty($this->types) && !in_array(strtolower($type), $this->types) ) {
  543. $this->_ms_add('top', 'filetype_not_allowed', 'error');
  544. return false;
  545. }
  546. return true;
  547. }
  548. function checkFileSize($size) {
  549. if( $this->maxsize > 0 && $this->maxsize <= $size ) {
  550. $this->_ms_add('top', 'filesize_not_allowed', 'error');
  551. return false;
  552. }
  553. return true;
  554. }
  555. function checkFileUploadable($file) {
  556. return is_uploaded_file($file);
  557. }
  558. function checkDestination() {
  559. $has_error = false;
  560. if( !is_writable($this->destination) ) {
  561. if( is_dir($this->destination) ) {
  562. $this->_ms_add('top', array('destination_not_writable', $this->destination), 'error');
  563. } else {
  564. $this->_ms_add('top', array('destination_not_exist', $this->destination), 'error');
  565. }
  566. $has_error = true;
  567. } else {
  568. $this->destination_subdir = '';
  569. if( $this->destination_type == 'month' ) {
  570. $this->destination_subdir = strftime("%Y%m", time());
  571. } elseif( $this->destination_type == 'day' ) {
  572. $this->destination_subdir = strftime("%Y%m%d", time());
  573. } elseif( $this->destination_type == 'random' ) {
  574. $this->destination_subdir = substr(md5(strftime("%Y%m", time())), 0, 6);
  575. }
  576. $this->destination_subdir = trim($this->destination_subdir);
  577. if( strlen($this->destination_subdir) > 0 ) {
  578. if( !is_dir($this->destination . $this->destination_subdir . '/') ) {
  579. @mkdir($this->destination . $this->destination_subdir . '/');
  580. }
  581. if( !is_writable($this->destination . $this->destination_subdir . '/') ) {
  582. if( is_dir($this->destination . $this->destination_subdir . '/') ) {
  583. $this->_ms_add('top', array('destination_not_writable', $this->destination . $this->destination_subdir . '/'), 'error');
  584. } else {
  585. $this->_ms_add('top', array('destination_not_exist', $this->destination . $this->destination_subdir . '/'), 'error');
  586. }
  587. $has_error = true;
  588. }
  589. }
  590. }
  591. if( $has_error == true ) {
  592. return false;
  593. }
  594. return true;
  595. }
  596. function parseExtension($filename = '') {
  597. if( $filename == '' ) {
  598. if( $this->filename ) {
  599. $this->extension = strtolower(substr($this->filename, strrpos($this->filename, '.')+1));
  600. } else {
  601. if( !is_array($this->file['name']) ) {
  602. $this->extension = strtolower(substr($this->file['name'], strrpos($this->file['name'], '.')+1));
  603. }
  604. }
  605. } else {
  606. $this->extension = strtolower(substr($filename, strrpos($filename, '.')+1));
  607. }
  608. }
  609. //SETTER/GETTER
  610. function setFile($file) {
  611. $this->file = $file;
  612. }
  613. function getFile() {
  614. return $this->file;
  615. }
  616. function set_rand_filename($is_rand = true) {
  617. if( $is_rand == true ) {
  618. $this->rand_filename = true;
  619. } else {
  620. $this->rand_filename = false;
  621. }
  622. }
  623. function setRandFilename($is_rand = true) {
  624. if( $is_rand == true ) {
  625. $this->rand_filename = true;
  626. } else {
  627. $this->rand_filename = false;
  628. }
  629. }
  630. function getRandFilename() {
  631. return $this->rand_filename;
  632. }
  633. function setExtension($extension) {
  634. $this->extension = $extension;
  635. }
  636. function getExtension() {
  637. return $this->extension;
  638. }
  639. function setDestination($destination) {
  640. $this->destination = $destination;
  641. }
  642. function getDestination() {
  643. return $this->destinaction;
  644. }
  645. function setDestinationType($destination_type) {
  646. $this->destination_type = $destination_type;
  647. }
  648. function getDestinationType() {
  649. return $this->destination_type;
  650. }
  651. function setDestinationSubDir($destination_subdir) {
  652. $this->destination_subdir = $destination_subdir;
  653. }
  654. function getDestinationSubDir() {
  655. return $this->destination_subdir;
  656. }
  657. function setPermissions($permissions) {
  658. $this->permissions = octdec($permissions);
  659. }
  660. function getPermissions() {
  661. return $this->permissions;
  662. }
  663. function setFilename($filename) {
  664. $this->filename = $filename;
  665. }
  666. function getFilename() {
  667. return $this->filename;
  668. }
  669. function setFiletype($filetype) {
  670. $this->filetype = $filetype;
  671. }
  672. function getFilesize() {
  673. return $this->filesize;
  674. }
  675. function setFilesize($filesize) {
  676. $this->filesize = $filesize;
  677. }
  678. function getFiletype() {
  679. return $this->filetype;
  680. }
  681. function setTmpFilename($filename) {
  682. $this->tmp_filename = $filename;
  683. }
  684. function getTmpFilename() {
  685. return $this->tmp_filename;
  686. }
  687. function getSaveFilename() {
  688. return $this->save_filename;
  689. }
  690. function setExtensions($extensions) {
  691. if( $this->_not_null($extensions) ) {
  692. if( is_array($extensions) ) {
  693. $this->extensions = $extensions;
  694. } else {
  695. $this->extensions = array($extensions);
  696. }
  697. } else {
  698. $this->extensions = array();
  699. }
  700. }
  701. function getExtensions() {
  702. return $this->extensions;
  703. }
  704. function setOutputMessages($location) {
  705. switch($location) {
  706. case 'session':
  707. $this->message_location = 'session';
  708. break;
  709. case 'direct':
  710. default:
  711. $this->message_location = 'direct';
  712. break;
  713. }
  714. }
  715. function getOutputMessages() {
  716. return $this->message_location;
  717. }
  718. function getMessages() {
  719. return $this->_messagestacks;
  720. }
  721. //private
  722. function _not_null($value) {
  723. return util_not_null($value);
  724. }
  725. function _random() {
  726. return util_create_random_value('8', 'mixed');
  727. }
  728. function _generate_filename() {
  729. //return md5(time()) . '.' . $this->extension;
  730. return substr(md5(time()), 0, 16) . '_' . $this->_random() . '.' . $this->extension;
  731. }
  732. function _filter_filename($filename = '', $check = true, $rand = false) {
  733. if( $filename == '' ) {
  734. $filename = $this->filename;
  735. }
  736. $name = $filename;
  737. $filename = substr($filename, 0, -(strlen($this->extension)+1));
  738. $filename = stripslashes($filename);
  739. $filename = trim(ereg_replace("[^A-Za-z0-9\-\_]", " ", $filename));
  740. $filename = ereg_replace("( {1,})", "-", $filename);
  741. $filename = trim(substr($filename, 0, 50));
  742. if( $rand == true ) {
  743. $filename .= '_' . $this->_random();
  744. }
  745. $filename .= '.' . $this->extension;
  746. if( $check == true ) {
  747. if( strlen($this->destination_subdir) > 0 ) {
  748. $savefile = $this->destination . $this->destination_subdir . '/' . $filename;
  749. } else {
  750. $savefile = $this->destination . '/' . $filename;
  751. }
  752. if( file_exists($savefile) ) {
  753. $filename = $this->_filter_filename($name, $check, true);
  754. }
  755. }
  756. return $filename;
  757. }
  758. function _ms_add($class, $message, $type = 'error') {
  759. global $messagestackObj;
  760. if( is_array($message) ) {
  761. $message = sprintf(_lang($message['0'], 'upload'), $message['1']);
  762. } else {
  763. $message = _lang($message, 'upload');
  764. }
  765. if( $this->message_location != 'direct' && is_object($messagestackObj) ) {
  766. $messagestackObj->add($class, $message, $type);
  767. } else {
  768. $this->_messagestacks[] = array('class' => $class, 'message' => $message, 'type' => $type);
  769. }
  770. }
  771. function _ms_add_session($class, $message, $type = 'error') {
  772. global $messagestackObj;
  773. if( is_array($message) ) {
  774. $message = sprintf(_lang($message['0'], 'upload'), $message['1']);
  775. } else {
  776. $message = _lang($message, 'upload');
  777. }
  778. if( $this->message_location != 'direct' && is_object($messagestackObj) ) {
  779. $messagestackObj->add_session($class, $message, $type);
  780. } else {
  781. $this->_messagestacks[] = array('class' => $class, 'message' => $message, 'type' => $type);
  782. }
  783. }
  784. }
  785. //
  786. ?>