/wp-content/plugins/google-maps-easy/classes/fileuploader.php

https://gitlab.com/pankajmohale/chef2go · PHP · 154 lines · 125 code · 2 blank · 27 comment · 13 complexity · 60d389dccbc91c548a62aa63f7a346b3 MD5 · raw file

  1. <?php
  2. class fileuploaderGmp {
  3. private $_error = '';
  4. private $_dest = '';
  5. private $_inputname = '';
  6. private $_fileInfo = array();
  7. /**
  8. * Result filename, if empty - it will be randon generated string
  9. */
  10. private $_destFilename = '';
  11. /**
  12. * Product ID to attach file, can be 0
  13. */
  14. private $_pid = 0;
  15. /**
  16. * File type ID, if no product specified or if file is for selling can be 1
  17. */
  18. private $_typeId = 1;
  19. /**
  20. * Return last error
  21. * @return string error message
  22. */
  23. public function getError() {
  24. return langGmp::_($this->_error);
  25. }
  26. public function __construct() {
  27. }
  28. public function setPid($pid) {
  29. $this->_pid = (int) $pid;
  30. }
  31. public function getPid() {
  32. return $this->_pid;
  33. }
  34. public function setTypeId($typeId) {
  35. $this->_typeId = (int) $typeId;
  36. }
  37. public function getTypeId() {
  38. return $this->_typeId;
  39. }
  40. /**
  41. * Validate before upload
  42. * @param string $inputname name of the input HTML document (key in $_FILES array)
  43. * @param string $destSubDir destination for uploaded file, for wp this should be directory in wp-content/uploads/ dir
  44. * @param string $filename name of a file that be uploaded
  45. */
  46. public function validate($inputname, $destSubDir, $destFilename = '') {
  47. $res = false;
  48. if(!empty($_FILES[$inputname]['error'])) {
  49. switch($_FILES[$inputname]['error']) {
  50. case '1':
  51. $this->_error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
  52. break;
  53. case '2':
  54. $this->_error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
  55. break;
  56. case '3':
  57. $this->_error = 'The uploaded file was only partially uploaded';
  58. break;
  59. case '4':
  60. $this->_error = 'No file was uploaded.';
  61. break;
  62. case '6':
  63. $this->_error = 'Missing a temporary folder';
  64. break;
  65. case '7':
  66. $this->_error = 'Failed to write file to disk';
  67. break;
  68. case '8':
  69. $this->_error = 'File upload stopped by extension';
  70. break;
  71. case '999':
  72. default:
  73. $this->_error = 'No error code avaiable';
  74. }
  75. } elseif(empty($_FILES[$inputname]['tmp_name']) || $_FILES[$inputname]['tmp_name'] == 'none') {
  76. $this->_error = 'No file was uploaded..';
  77. } else {
  78. $res = true;
  79. }
  80. if($res) {
  81. //$this->_fileSize = $_FILES[$inputname]['size'];
  82. $this->_dest = $destSubDir;
  83. $this->_inputname = $inputname;
  84. $this->_destFilename = $destFilename;
  85. }
  86. return $res;
  87. }
  88. /**
  89. * Upload valid file
  90. */
  91. public function upload($params = array()) {
  92. $res = false;
  93. $setRandFileName = isset($params['randFileName']) ? $params['randFileName'] : true;
  94. add_filter('upload_dir', array($this, 'changeUploadDir'));
  95. if($setRandFileName)
  96. add_filter('wp_handle_upload_prefilter', array($this, 'changeFileName'));
  97. $file = $_FILES[ $this->_inputname ];
  98. $upload = wp_handle_upload($file, array('test_form' => FALSE));
  99. if (isset($upload['type']) && !empty($upload['type'])) {
  100. $this->_fileInfo = $file;
  101. $this->_fileInfo['name'] = $_FILES[ $this->_inputname ]['name'];
  102. $this->_fileInfo['path'] = $file['name'];
  103. $this->_fileInfo['url'] =
  104. str_replace('[AFTER_PROTOCOL]', '://',
  105. str_replace('//', '/',
  106. str_replace('://', '[AFTER_PROTOCOL]',
  107. str_replace(DS, '/', $upload['url'])
  108. )));
  109. $res = true;
  110. } elseif(isset($upload['error']))
  111. $this->_error = $upload['error'];
  112. remove_filter('upload_dir', array($this, 'changeUploadDir'));
  113. if($setRandFileName)
  114. remove_filter('wp_handle_upload_prefilter', array($this, 'changeFileName'));
  115. return $res;
  116. }
  117. public function getFileInfo() {
  118. return $this->_fileInfo;
  119. }
  120. public function changeUploadDir($uploads) {
  121. $uploads['subdir'] = $this->_dest;
  122. if(empty($uploads['subdir'])) {
  123. $uploads['path'] = $uploads['basedir'];
  124. $uploads['url'] = $uploads['baseurl'];
  125. } else {
  126. if(strpos($uploads['subdir'], DS) !== 0)
  127. $uploads['subdir'] = DS. $uploads['subdir'];
  128. $uploads['path'] = $uploads['basedir'] . $uploads['subdir'];
  129. $uploads['url'] = $uploads['baseurl'] . '/'.$uploads['subdir'];
  130. }
  131. return $uploads;
  132. }
  133. public function changeFileName($file) {
  134. $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
  135. if(empty($this->_destFilename))
  136. $file['name'] = $this->createFileName().'.'.$ext;
  137. else
  138. $file['name'] = $this->_destFilename;
  139. return $file;
  140. }
  141. private function createFileName() {
  142. return utilsGmp::getRandStr(). '-'. utilsGmp::getRandStr(). '-'. utilsGmp::getRandStr(). '-'. utilsGmp::getRandStr();
  143. }
  144. /**
  145. * Delete uploaded file
  146. * @param int $fid ID of file in files table
  147. */
  148. public function delete($fid) {
  149. return false;
  150. }
  151. }
  152. ?>