PageRenderTime 86ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/formulaire/upload_FA.php

https://github.com/severnaya99/Sg-2010
PHP | 380 lines | 209 code | 29 blank | 142 comment | 39 complexity | dee27f6afc0eb5f56fbf39cdee96e636 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0
  1. <?php
  2. ###############################################################################
  3. ## Formulaire - Information submitting module for XOOPS ##
  4. ## Copyright (c) 2005 philou@xoops.org ##
  5. ## <http://www.frxoops.org/> ##
  6. ###############################################################################
  7. ## XOOPS - PHP Content Management System ##
  8. ## Copyright (c) 2000 XOOPS.org ##
  9. ## <http://www.xoops.org/> ##
  10. ###############################################################################
  11. ## This program is free software; you can redistribute it and/or modify ##
  12. ## it under the terms of the GNU General Public License as published by ##
  13. ## the Free Software Foundation; either version 2 of the License, or ##
  14. ## (at your option) any later version. ##
  15. ## ##
  16. ## You may not change or alter any portion of this comment or credits ##
  17. ## of supporting developers from this source code or any supporting ##
  18. ## source code which is considered copyrighted (c) material of the ##
  19. ## original comment or credit authors. ##
  20. ## ##
  21. ## This program is distributed in the hope that it will be useful, ##
  22. ## but WITHOUT ANY WARRANTY; without even the implied warranty of ##
  23. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##
  24. ## GNU General Public License for more details. ##
  25. ## ##
  26. ## You should have received a copy of the GNU General Public License ##
  27. ## along with this program; if not, write to the Free Software ##
  28. ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ##
  29. ###############################################################################
  30. ## URL: http://www.frxoops.org/ ##
  31. ## Project: Formulaire ##
  32. ###############################################################################
  33. class XoopsMediaUploader_FA
  34. {
  35. var $mediaName;
  36. var $mediaType;
  37. var $mediaSize;
  38. var $mediaTmpName;
  39. var $mediaError;
  40. var $uploadDir = '';
  41. var $allowedMimeTypes = array();
  42. var $maxFileSize = 0;
  43. var $maxWidth;
  44. var $maxHeight;
  45. var $targetFileName;
  46. var $prefix;
  47. var $errors = array();
  48. var $savedDestination;
  49. var $savedFileName;
  50. /**
  51. * Constructor
  52. *
  53. * @param string $uploadDir
  54. * @param array $allowedMimeTypes
  55. * @param int $maxFileSize
  56. * @param int $maxWidth
  57. * @param int $maxHeight
  58. * @param int $cmodvalue
  59. **/
  60. function XoopsMediaUploader_FA($uploadDir, $allowedMimeTypes, $maxFileSize, $maxWidth=null, $maxHeight=null)
  61. {
  62. if (is_array($allowedMimeTypes)) {
  63. $this->allowedMimeTypes =& $allowedMimeTypes;
  64. }
  65. $this->uploadDir = $uploadDir;
  66. $this->maxFileSize = intval($maxFileSize);
  67. if(isset($maxWidth)) {
  68. $this->maxWidth = intval($maxWidth);
  69. }
  70. if(isset($maxHeight)) {
  71. $this->maxHeight = intval($maxHeight);
  72. }
  73. }
  74. /**
  75. * Fetch the uploaded file
  76. *
  77. * @param string $media_name Name of the file field
  78. * @param int $index Index of the file (if more than one uploaded under that name)
  79. * @global $HTTP_POST_FILES
  80. * @return bool
  81. **/
  82. function fetchMedia($media_name, $index = null)
  83. {
  84. global $HTTP_POST_FILES;
  85. if (!isset($HTTP_POST_FILES[$media_name])) {
  86. $this->setErrors('File not found');
  87. return false;
  88. } elseif (is_array($HTTP_POST_FILES[$media_name]['name']) && isset($index)) {
  89. $index = intval($index);
  90. $this->mediaName = (get_magic_quotes_gpc()) ? stripslashes($HTTP_POST_FILES[$media_name]['name'][$index]) : $HTTP_POST_FILES[$media_name]['name'][$index];
  91. $this->mediaType = $HTTP_POST_FILES[$media_name]['type'][$index];
  92. $this->mediaSize = $HTTP_POST_FILES[$media_name]['size'][$index];
  93. $this->mediaTmpName = $HTTP_POST_FILES[$media_name]['tmp_name'][$index];
  94. $this->mediaError = !empty($HTTP_POST_FILES[$media_name]['error'][$index]) ? $HTTP_POST_FILES[$media_name]['errir'][$index] : 0;
  95. } else {
  96. $media_name =& $HTTP_POST_FILES[$media_name];
  97. $this->mediaName = (get_magic_quotes_gpc()) ? stripslashes($media_name['name']) : $media_name['name'];
  98. $this->mediaName = $media_name['name'];
  99. $this->mediaType = $media_name['type'];
  100. $this->mediaSize = $media_name['size'];
  101. $this->mediaTmpName = $media_name['tmp_name'];
  102. $this->mediaError = !empty($media_name['error']) ? $media_name['error'] : 0;
  103. }
  104. $this->errors = array();
  105. if (intval($this->mediaSize) < 0) {
  106. $this->setErrors('Invalid File Size');
  107. return false;
  108. }
  109. if ($this->mediaName == '') {
  110. $this->setErrors('Filename Is Empty');
  111. return false;
  112. }
  113. if ($this->mediaTmpName == 'none' || !is_uploaded_file($this->mediaTmpName)) {
  114. $this->setErrors('No file uploaded');
  115. return false;
  116. }
  117. if ($this->mediaError > 0) {
  118. $this->setErrors('Error occurred: Error #'.$this->mediaError);
  119. return false;
  120. }
  121. return true;
  122. }
  123. /**
  124. * Set the target filename
  125. *
  126. * @param string $value
  127. **/
  128. function setTargetFileName($value){
  129. $this->targetFileName = strval(trim($value));
  130. }
  131. /**
  132. * Set the prefix
  133. *
  134. * @param string $value
  135. **/
  136. function setPrefix($value){
  137. $this->prefix = strval(trim($value));
  138. }
  139. /**
  140. * Get the uploaded filename
  141. *
  142. * @return string
  143. **/
  144. function getMediaName()
  145. {
  146. return $this->mediaName;
  147. }
  148. /**
  149. * Get the type of the uploaded file
  150. *
  151. * @return string
  152. **/
  153. function getMediaType()
  154. {
  155. return $this->mediaType;
  156. }
  157. /**
  158. * Get the size of the uploaded file
  159. *
  160. * @return int
  161. **/
  162. function getMediaSize()
  163. {
  164. return $this->mediaSize;
  165. }
  166. /**
  167. * Get the temporary name that the uploaded file was stored under
  168. *
  169. * @return string
  170. **/
  171. function getMediaTmpName()
  172. {
  173. return $this->mediaTmpName;
  174. }
  175. /**
  176. * Get the saved filename
  177. *
  178. * @return string
  179. **/
  180. function getSavedFileName(){
  181. return $this->savedFileName;
  182. }
  183. /**
  184. * Get the destination the file is saved to
  185. *
  186. * @return string
  187. **/
  188. function getSavedDestination(){
  189. return $this->savedDestination;
  190. }
  191. /**
  192. * Check the file and copy it to the destination
  193. *
  194. * @return bool
  195. **/
  196. function upload($chmod = 0644)
  197. {
  198. if ($this->uploadDir == '') {
  199. $this->setErrors('Upload directory not set');
  200. return false;
  201. }
  202. if (!is_dir($this->uploadDir)) {
  203. $this->setErrors('Failed opening directory: '.$this->uploadDir);
  204. }
  205. if (!is_writeable($this->uploadDir)) {
  206. $this->setErrors('Failed opening directory with write permission: '.$this->uploadDir);
  207. }
  208. if (!$this->checkMaxFileSize()) {
  209. $this->setErrors('File size too large: '.$this->mediaSize);
  210. }
  211. /*if (!$this->checkMaxWidth()) {
  212. $this->setErrors(sprintf('File width must be smaller than %u', $this->maxWidth));
  213. }
  214. if (!$this->checkMaxHeight()) {
  215. $this->setErrors(sprintf('File height must be smaller than %u', $this->maxHeight));
  216. }
  217. if (!$this->checkMimeType()) {
  218. $this->setErrors('MIME type not allowed: '.$this->mediaType);
  219. }*/
  220. if (count($this->errors) > 0) {
  221. return false;
  222. }
  223. if (!$this->_copyFile($chmod)) {
  224. $this->setErrors('Failed uploading file: '.$this->mediaName);
  225. return false;
  226. }
  227. return true;
  228. }
  229. /**
  230. * Copy the file to its destination
  231. *
  232. * @return bool
  233. **/
  234. function _copyFile($chmod)
  235. {
  236. $matched = array();
  237. if (!preg_match("/\.([a-zA-Z0-9]+)$/", $this->mediaName, $matched)) {
  238. return false;
  239. }
  240. if (isset($this->targetFileName)) {
  241. $this->savedFileName = $this->targetFileName;
  242. } elseif (isset($this->prefix)) {
  243. $this->savedFileName = uniqid($this->prefix).'.'.strtolower($matched[1]);
  244. } else {
  245. $this->savedFileName = strtolower($this->mediaName);
  246. }
  247. $this->savedDestination = $this->uploadDir.'/'.$this->savedFileName;
  248. if (!move_uploaded_file($this->mediaTmpName, $this->savedDestination)) {
  249. return false;
  250. }
  251. @chmod($this->savedDestination, $chmod);
  252. return true;
  253. }
  254. /**
  255. * Is the file the right size?
  256. *
  257. * @return bool
  258. **/
  259. function checkMaxFileSize()
  260. {
  261. if ($this->mediaSize > $this->maxFileSize) {
  262. return false;
  263. }
  264. return true;
  265. }
  266. /**
  267. * Is the picture the right width?
  268. *
  269. * @return bool
  270. **/
  271. function checkMaxWidth()
  272. {
  273. if (!isset($this->maxWidth)) {
  274. return true;
  275. }
  276. if (false !== $dimension = getimagesize($this->mediaTmpName)) {
  277. if ($dimension[0] > $this->maxWidth) {
  278. return false;
  279. }
  280. } else {
  281. trigger_error(sprintf('Failed fetching image size of %s, skipping max width check..', $this->mediaTmpName), E_USER_WARNING);
  282. }
  283. return true;
  284. }
  285. /**
  286. * Is the picture the right height?
  287. *
  288. * @return bool
  289. **/
  290. function checkMaxHeight()
  291. {
  292. if (!isset($this->maxHeight)) {
  293. return true;
  294. }
  295. if (false !== $dimension = getimagesize($this->mediaTmpName)) {
  296. if ($dimension[1] > $this->maxWidth) {
  297. return false;
  298. }
  299. } else {
  300. trigger_error(sprintf('Failed fetching image size of %s, skipping max height check..', $this->mediaTmpName), E_USER_WARNING);
  301. }
  302. return true;
  303. }
  304. /**
  305. * Is the file the right Mime type
  306. *
  307. * (is there a right type of mime? ;-)
  308. *
  309. * @return bool
  310. **/
  311. function checkMimeType()
  312. {
  313. if (count($this->allowedMimeTypes) > 0 && !in_array($this->mediaType, $this->allowedMimeTypes)) {
  314. return false;
  315. } else {
  316. return true;
  317. }
  318. }
  319. /**
  320. * Add an error
  321. *
  322. * @param string $error
  323. **/
  324. function setErrors($error)
  325. {
  326. $this->errors[] = trim($error);
  327. }
  328. /**
  329. * Get generated errors
  330. *
  331. * @param bool $ashtml Format using HTML?
  332. *
  333. * @return array|string Array of array messages OR HTML string
  334. */
  335. function &getErrors($ashtml = true)
  336. {
  337. if (!$ashtml) {
  338. return $this->errors;
  339. } else {
  340. $ret = '';
  341. if (count($this->errors) > 0) {
  342. $ret = '<h4>Errors Returned While Uploading</h4>';
  343. foreach ($this->errors as $error) {
  344. $ret .= $error.'<br />';
  345. }
  346. }
  347. return $ret;
  348. }
  349. }
  350. }
  351. ?>