PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/includes/functions_upload.php

https://github.com/naderman/phpbb-orchestra
PHP | 1012 lines | 771 code | 116 blank | 125 comment | 88 complexity | bdaf296f26a0e9b3449591998919aa3e MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package phpBB3
  5. * @version $Id$
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. if (!defined('IN_PHPBB'))
  14. {
  15. exit;
  16. }
  17. /**
  18. * Responsible for holding all file relevant information, as well as doing file-specific operations.
  19. * The {@link fileupload fileupload class} can be used to upload several files, each of them being this object to operate further on.
  20. * @package phpBB3
  21. */
  22. class filespec
  23. {
  24. var $filename = '';
  25. var $realname = '';
  26. var $uploadname = '';
  27. var $mimetype = '';
  28. var $extension = '';
  29. var $filesize = 0;
  30. var $width = 0;
  31. var $height = 0;
  32. var $image_info = array();
  33. var $destination_file = '';
  34. var $destination_path = '';
  35. var $file_moved = false;
  36. var $init_error = false;
  37. var $local = false;
  38. var $error = array();
  39. var $upload = '';
  40. /**
  41. * File Class
  42. * @access private
  43. */
  44. function filespec($upload_ary, $upload_namespace)
  45. {
  46. if (!isset($upload_ary))
  47. {
  48. $this->init_error = true;
  49. return;
  50. }
  51. $this->filename = $upload_ary['tmp_name'];
  52. $this->filesize = $upload_ary['size'];
  53. $name = (STRIP) ? stripslashes($upload_ary['name']) : $upload_ary['name'];
  54. $name = trim(utf8_htmlspecialchars(utf8_basename($name)));
  55. $this->realname = $this->uploadname = $name;
  56. $this->mimetype = $upload_ary['type'];
  57. // Opera adds the name to the mime type
  58. $this->mimetype = (strpos($this->mimetype, '; name') !== false) ? str_replace(strstr($this->mimetype, '; name'), '', $this->mimetype) : $this->mimetype;
  59. if (!$this->mimetype)
  60. {
  61. $this->mimetype = 'application/octetstream';
  62. }
  63. $this->extension = strtolower($this->get_extension($this->realname));
  64. // Try to get real filesize from temporary folder (not always working) ;)
  65. $this->filesize = (@filesize($this->filename)) ? @filesize($this->filename) : $this->filesize;
  66. $this->width = $this->height = 0;
  67. $this->file_moved = false;
  68. $this->local = (isset($upload_ary['local_mode'])) ? true : false;
  69. $this->upload = $upload_namespace;
  70. }
  71. /**
  72. * Cleans destination filename
  73. *
  74. * @param real|unique|unique_ext $mode real creates a realname, filtering some characters, lowering every character. Unique creates an unique filename
  75. * @param string $prefix Prefix applied to filename
  76. * @access public
  77. */
  78. function clean_filename($mode = 'unique', $prefix = '', $user_id = '')
  79. {
  80. if ($this->init_error)
  81. {
  82. return;
  83. }
  84. switch ($mode)
  85. {
  86. case 'real':
  87. // Remove every extension from filename (to not let the mime bug being exposed)
  88. if (strpos($this->realname, '.') !== false)
  89. {
  90. $this->realname = substr($this->realname, 0, strpos($this->realname, '.'));
  91. }
  92. // Replace any chars which may cause us problems with _
  93. $bad_chars = array("'", "\\", ' ', '/', ':', '*', '?', '"', '<', '>', '|');
  94. $this->realname = rawurlencode(str_replace($bad_chars, '_', strtolower($this->realname)));
  95. $this->realname = preg_replace("/%(\w{2})/", '_', $this->realname);
  96. $this->realname = $prefix . $this->realname . '.' . $this->extension;
  97. break;
  98. case 'unique':
  99. $this->realname = $prefix . md5(unique_id());
  100. break;
  101. case 'avatar':
  102. $this->extension = strtolower($this->extension);
  103. $this->realname = $prefix . $user_id . '.' . $this->extension;
  104. break;
  105. case 'unique_ext':
  106. default:
  107. $this->realname = $prefix . md5(unique_id()) . '.' . $this->extension;
  108. break;
  109. }
  110. }
  111. /**
  112. * Get property from file object
  113. */
  114. function get($property)
  115. {
  116. if ($this->init_error || !isset($this->$property))
  117. {
  118. return false;
  119. }
  120. return $this->$property;
  121. }
  122. /**
  123. * Check if file is an image (mimetype)
  124. *
  125. * @return true if it is an image, false if not
  126. */
  127. function is_image()
  128. {
  129. return (strpos($this->mimetype, 'image/') !== false) ? true : false;
  130. }
  131. /**
  132. * Check if the file got correctly uploaded
  133. *
  134. * @return true if it is a valid upload, false if not
  135. */
  136. function is_uploaded()
  137. {
  138. if (!$this->local && !is_uploaded_file($this->filename))
  139. {
  140. return false;
  141. }
  142. if ($this->local && !file_exists($this->filename))
  143. {
  144. return false;
  145. }
  146. return true;
  147. }
  148. /**
  149. * Remove file
  150. */
  151. function remove()
  152. {
  153. if ($this->file_moved)
  154. {
  155. @unlink($this->destination_file);
  156. }
  157. }
  158. /**
  159. * Get file extension
  160. */
  161. function get_extension($filename)
  162. {
  163. if (strpos($filename, '.') === false)
  164. {
  165. return '';
  166. }
  167. $filename = explode('.', $filename);
  168. return array_pop($filename);
  169. }
  170. /**
  171. * Get mimetype. Utilize mime_content_type if the function exist.
  172. * Not used at the moment...
  173. */
  174. function get_mimetype($filename)
  175. {
  176. $mimetype = '';
  177. if (function_exists('mime_content_type'))
  178. {
  179. $mimetype = mime_content_type($filename);
  180. }
  181. // Some browsers choke on a mimetype of application/octet-stream
  182. if (!$mimetype || $mimetype == 'application/octet-stream')
  183. {
  184. $mimetype = 'application/octetstream';
  185. }
  186. return $mimetype;
  187. }
  188. /**
  189. * Get filesize
  190. */
  191. function get_filesize($filename)
  192. {
  193. return @filesize($filename);
  194. }
  195. /**
  196. * Check the first 256 bytes for forbidden content
  197. */
  198. function check_content($disallowed_content)
  199. {
  200. if (empty($disallowed_content))
  201. {
  202. return true;
  203. }
  204. $fp = @fopen($this->filename, 'rb');
  205. if ($fp !== false)
  206. {
  207. $ie_mime_relevant = fread($fp, 256);
  208. fclose($fp);
  209. foreach ($disallowed_content as $forbidden)
  210. {
  211. if (stripos($ie_mime_relevant, '<' . $forbidden) !== false)
  212. {
  213. return false;
  214. }
  215. }
  216. }
  217. return true;
  218. }
  219. /**
  220. * Move file to destination folder
  221. * The phpbb_root_path variable will be applied to the destination path
  222. *
  223. * @param string $destination_path Destination path, for example $config['avatar_path']
  224. * @param bool $overwrite If set to true, an already existing file will be overwritten
  225. * @param string $chmod Permission mask for chmodding the file after a successful move. The mode entered here reflects the mode defined by {@link phpbb_chmod()}
  226. *
  227. * @access public
  228. */
  229. function move_file($destination, $overwrite = false, $skip_image_check = false, $chmod = false)
  230. {
  231. global $user, $phpbb_root_path;
  232. if (sizeof($this->error))
  233. {
  234. return false;
  235. }
  236. $chmod = ($chmod === false) ? CHMOD_READ | CHMOD_WRITE : $chmod;
  237. // We need to trust the admin in specifying valid upload directories and an attacker not being able to overwrite it...
  238. $this->destination_path = $phpbb_root_path . $destination;
  239. // Check if the destination path exist...
  240. if (!file_exists($this->destination_path))
  241. {
  242. @unlink($this->filename);
  243. return false;
  244. }
  245. $upload_mode = (@ini_get('open_basedir') || @ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'on') ? 'move' : 'copy';
  246. $upload_mode = ($this->local) ? 'local' : $upload_mode;
  247. $this->destination_file = $this->destination_path . '/' . utf8_basename($this->realname);
  248. // Check if the file already exist, else there is something wrong...
  249. if (file_exists($this->destination_file) && !$overwrite)
  250. {
  251. @unlink($this->filename);
  252. }
  253. else
  254. {
  255. if (file_exists($this->destination_file))
  256. {
  257. @unlink($this->destination_file);
  258. }
  259. switch ($upload_mode)
  260. {
  261. case 'copy':
  262. if (!@copy($this->filename, $this->destination_file))
  263. {
  264. if (!@move_uploaded_file($this->filename, $this->destination_file))
  265. {
  266. $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
  267. }
  268. }
  269. break;
  270. case 'move':
  271. if (!@move_uploaded_file($this->filename, $this->destination_file))
  272. {
  273. if (!@copy($this->filename, $this->destination_file))
  274. {
  275. $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
  276. }
  277. }
  278. break;
  279. case 'local':
  280. if (!@copy($this->filename, $this->destination_file))
  281. {
  282. $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'GENERAL_UPLOAD_ERROR'], $this->destination_file);
  283. }
  284. break;
  285. }
  286. // Remove temporary filename
  287. @unlink($this->filename);
  288. if (sizeof($this->error))
  289. {
  290. return false;
  291. }
  292. phpbb_chmod($this->destination_file, $chmod);
  293. }
  294. // Try to get real filesize from destination folder
  295. $this->filesize = (@filesize($this->destination_file)) ? @filesize($this->destination_file) : $this->filesize;
  296. if ($this->is_image() && !$skip_image_check)
  297. {
  298. $this->width = $this->height = 0;
  299. if (($this->image_info = @getimagesize($this->destination_file)) !== false)
  300. {
  301. $this->width = $this->image_info[0];
  302. $this->height = $this->image_info[1];
  303. if (!empty($this->image_info['mime']))
  304. {
  305. $this->mimetype = $this->image_info['mime'];
  306. }
  307. // Check image type
  308. $types = $this->upload->image_types();
  309. if (!isset($types[$this->image_info[2]]) || !in_array($this->extension, $types[$this->image_info[2]]))
  310. {
  311. if (!isset($types[$this->image_info[2]]))
  312. {
  313. $this->error[] = sprintf($user->lang['IMAGE_FILETYPE_INVALID'], $this->image_info[2], $this->mimetype);
  314. }
  315. else
  316. {
  317. $this->error[] = sprintf($user->lang['IMAGE_FILETYPE_MISMATCH'], $types[$this->image_info[2]][0], $this->extension);
  318. }
  319. }
  320. // Make sure the dimensions match a valid image
  321. if (empty($this->width) || empty($this->height))
  322. {
  323. $this->error[] = $user->lang['ATTACHED_IMAGE_NOT_IMAGE'];
  324. }
  325. }
  326. else
  327. {
  328. $this->error[] = $user->lang['UNABLE_GET_IMAGE_SIZE'];
  329. }
  330. }
  331. $this->file_moved = true;
  332. $this->additional_checks();
  333. unset($this->upload);
  334. return true;
  335. }
  336. /**
  337. * Performing additional checks
  338. */
  339. function additional_checks()
  340. {
  341. global $user;
  342. if (!$this->file_moved)
  343. {
  344. return false;
  345. }
  346. // Filesize is too big or it's 0 if it was larger than the maxsize in the upload form
  347. if ($this->upload->max_filesize && ($this->get('filesize') > $this->upload->max_filesize || $this->filesize == 0))
  348. {
  349. $max_filesize = get_formatted_filesize($this->upload->max_filesize, false);
  350. $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']);
  351. return false;
  352. }
  353. if (!$this->upload->valid_dimensions($this))
  354. {
  355. $this->error[] = sprintf($user->lang[$this->upload->error_prefix . 'WRONG_SIZE'], $this->upload->min_width, $this->upload->min_height, $this->upload->max_width, $this->upload->max_height, $this->width, $this->height);
  356. return false;
  357. }
  358. return true;
  359. }
  360. }
  361. /**
  362. * Class for assigning error messages before a real filespec class can be assigned
  363. *
  364. * @package phpBB3
  365. */
  366. class fileerror extends filespec
  367. {
  368. function fileerror($error_msg)
  369. {
  370. $this->error[] = $error_msg;
  371. }
  372. }
  373. /**
  374. * File upload class
  375. * Init class (all parameters optional and able to be set/overwritten separately) - scope is global and valid for all uploads
  376. *
  377. * @package phpBB3
  378. */
  379. class fileupload
  380. {
  381. var $allowed_extensions = array();
  382. var $disallowed_content = array('body', 'head', 'html', 'img', 'plaintext', 'a href', 'pre', 'script', 'table', 'title');
  383. var $max_filesize = 0;
  384. var $min_width = 0;
  385. var $min_height = 0;
  386. var $max_width = 0;
  387. var $max_height = 0;
  388. var $error_prefix = '';
  389. /**
  390. * Init file upload class.
  391. *
  392. * @param string $error_prefix Used error messages will get prefixed by this string
  393. * @param array $allowed_extensions Array of allowed extensions, for example array('jpg', 'jpeg', 'gif', 'png')
  394. * @param int $max_filesize Maximum filesize
  395. * @param int $min_width Minimum image width (only checked for images)
  396. * @param int $min_height Minimum image height (only checked for images)
  397. * @param int $max_width Maximum image width (only checked for images)
  398. * @param int $max_height Maximum image height (only checked for images)
  399. *
  400. */
  401. function fileupload($error_prefix = '', $allowed_extensions = false, $max_filesize = false, $min_width = false, $min_height = false, $max_width = false, $max_height = false, $disallowed_content = false)
  402. {
  403. $this->set_allowed_extensions($allowed_extensions);
  404. $this->set_max_filesize($max_filesize);
  405. $this->set_allowed_dimensions($min_width, $min_height, $max_width, $max_height);
  406. $this->set_error_prefix($error_prefix);
  407. $this->set_disallowed_content($disallowed_content);
  408. }
  409. /**
  410. * Reset vars
  411. */
  412. function reset_vars()
  413. {
  414. $this->max_filesize = 0;
  415. $this->min_width = $this->min_height = $this->max_width = $this->max_height = 0;
  416. $this->error_prefix = '';
  417. $this->allowed_extensions = array();
  418. $this->disallowed_content = array();
  419. }
  420. /**
  421. * Set allowed extensions
  422. */
  423. function set_allowed_extensions($allowed_extensions)
  424. {
  425. if ($allowed_extensions !== false && is_array($allowed_extensions))
  426. {
  427. $this->allowed_extensions = $allowed_extensions;
  428. }
  429. }
  430. /**
  431. * Set allowed dimensions
  432. */
  433. function set_allowed_dimensions($min_width, $min_height, $max_width, $max_height)
  434. {
  435. $this->min_width = (int) $min_width;
  436. $this->min_height = (int) $min_height;
  437. $this->max_width = (int) $max_width;
  438. $this->max_height = (int) $max_height;
  439. }
  440. /**
  441. * Set maximum allowed filesize
  442. */
  443. function set_max_filesize($max_filesize)
  444. {
  445. if ($max_filesize !== false && (int) $max_filesize)
  446. {
  447. $this->max_filesize = (int) $max_filesize;
  448. }
  449. }
  450. /**
  451. * Set disallowed strings
  452. */
  453. function set_disallowed_content($disallowed_content)
  454. {
  455. if ($disallowed_content !== false && is_array($disallowed_content))
  456. {
  457. $this->disallowed_content = array_diff($disallowed_content, array(''));
  458. }
  459. }
  460. /**
  461. * Set error prefix
  462. */
  463. function set_error_prefix($error_prefix)
  464. {
  465. $this->error_prefix = $error_prefix;
  466. }
  467. /**
  468. * Form upload method
  469. * Upload file from users harddisk
  470. *
  471. * @param string $form_name Form name assigned to the file input field (if it is an array, the key has to be specified)
  472. * @return object $file Object "filespec" is returned, all further operations can be done with this object
  473. * @access public
  474. */
  475. function form_upload($form_name)
  476. {
  477. global $user;
  478. unset($_FILES[$form_name]['local_mode']);
  479. $file = new filespec($_FILES[$form_name], $this);
  480. if ($file->init_error)
  481. {
  482. $file->error[] = '';
  483. return $file;
  484. }
  485. // Error array filled?
  486. if (isset($_FILES[$form_name]['error']))
  487. {
  488. $error = $this->assign_internal_error($_FILES[$form_name]['error']);
  489. if ($error !== false)
  490. {
  491. $file->error[] = $error;
  492. return $file;
  493. }
  494. }
  495. // Check if empty file got uploaded (not catched by is_uploaded_file)
  496. if (isset($_FILES[$form_name]['size']) && $_FILES[$form_name]['size'] == 0)
  497. {
  498. $file->error[] = $user->lang[$this->error_prefix . 'EMPTY_FILEUPLOAD'];
  499. return $file;
  500. }
  501. // PHP Upload filesize exceeded
  502. if ($file->get('filename') == 'none')
  503. {
  504. $max_filesize = @ini_get('upload_max_filesize');
  505. $unit = 'MB';
  506. if (!empty($max_filesize))
  507. {
  508. $unit = strtolower(substr($max_filesize, -1, 1));
  509. $max_filesize = (int) $max_filesize;
  510. $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB');
  511. }
  512. $file->error[] = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]);
  513. return $file;
  514. }
  515. // Not correctly uploaded
  516. if (!$file->is_uploaded())
  517. {
  518. $file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
  519. return $file;
  520. }
  521. $this->common_checks($file);
  522. return $file;
  523. }
  524. /**
  525. * Move file from another location to phpBB
  526. */
  527. function local_upload($source_file, $filedata = false)
  528. {
  529. global $user;
  530. $form_name = 'local';
  531. $_FILES[$form_name]['local_mode'] = true;
  532. $_FILES[$form_name]['tmp_name'] = $source_file;
  533. if ($filedata === false)
  534. {
  535. $_FILES[$form_name]['name'] = utf8_basename($source_file);
  536. $_FILES[$form_name]['size'] = 0;
  537. $mimetype = '';
  538. if (function_exists('mime_content_type'))
  539. {
  540. $mimetype = mime_content_type($source_file);
  541. }
  542. // Some browsers choke on a mimetype of application/octet-stream
  543. if (!$mimetype || $mimetype == 'application/octet-stream')
  544. {
  545. $mimetype = 'application/octetstream';
  546. }
  547. $_FILES[$form_name]['type'] = $mimetype;
  548. }
  549. else
  550. {
  551. $_FILES[$form_name]['name'] = $filedata['realname'];
  552. $_FILES[$form_name]['size'] = $filedata['size'];
  553. $_FILES[$form_name]['type'] = $filedata['type'];
  554. }
  555. $file = new filespec($_FILES[$form_name], $this);
  556. if ($file->init_error)
  557. {
  558. $file->error[] = '';
  559. return $file;
  560. }
  561. if (isset($_FILES[$form_name]['error']))
  562. {
  563. $error = $this->assign_internal_error($_FILES[$form_name]['error']);
  564. if ($error !== false)
  565. {
  566. $file->error[] = $error;
  567. return $file;
  568. }
  569. }
  570. // PHP Upload filesize exceeded
  571. if ($file->get('filename') == 'none')
  572. {
  573. $max_filesize = @ini_get('upload_max_filesize');
  574. $unit = 'MB';
  575. if (!empty($max_filesize))
  576. {
  577. $unit = strtolower(substr($max_filesize, -1, 1));
  578. $max_filesize = (int) $max_filesize;
  579. $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB');
  580. }
  581. $file->error[] = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]);
  582. return $file;
  583. }
  584. // Not correctly uploaded
  585. if (!$file->is_uploaded())
  586. {
  587. $file->error[] = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
  588. return $file;
  589. }
  590. $this->common_checks($file);
  591. return $file;
  592. }
  593. /**
  594. * Remote upload method
  595. * Uploads file from given url
  596. *
  597. * @param string $upload_url URL pointing to file to upload, for example http://www.foobar.com/example.gif
  598. * @return object $file Object "filespec" is returned, all further operations can be done with this object
  599. * @access public
  600. */
  601. function remote_upload($upload_url)
  602. {
  603. global $user, $phpbb_root_path;
  604. $upload_ary = array();
  605. $upload_ary['local_mode'] = true;
  606. if (!preg_match('#^(https?://).*?\.(' . implode('|', $this->allowed_extensions) . ')$#i', $upload_url, $match))
  607. {
  608. $file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']);
  609. return $file;
  610. }
  611. if (empty($match[2]))
  612. {
  613. $file = new fileerror($user->lang[$this->error_prefix . 'URL_INVALID']);
  614. return $file;
  615. }
  616. $url = parse_url($upload_url);
  617. $host = $url['host'];
  618. $path = $url['path'];
  619. $port = (!empty($url['port'])) ? (int) $url['port'] : 80;
  620. $upload_ary['type'] = 'application/octet-stream';
  621. $url['path'] = explode('.', $url['path']);
  622. $ext = array_pop($url['path']);
  623. $url['path'] = implode('', $url['path']);
  624. $upload_ary['name'] = utf8_basename($url['path']) . (($ext) ? '.' . $ext : '');
  625. $filename = $url['path'];
  626. $filesize = 0;
  627. $errno = 0;
  628. $errstr = '';
  629. if (!($fsock = @fsockopen($host, $port, $errno, $errstr)))
  630. {
  631. $file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']);
  632. return $file;
  633. }
  634. // Make sure $path not beginning with /
  635. if (strpos($path, '/') === 0)
  636. {
  637. $path = substr($path, 1);
  638. }
  639. fputs($fsock, 'GET /' . $path . " HTTP/1.1\r\n");
  640. fputs($fsock, "HOST: " . $host . "\r\n");
  641. fputs($fsock, "Connection: close\r\n\r\n");
  642. $get_info = false;
  643. $data = '';
  644. while (!@feof($fsock))
  645. {
  646. if ($get_info)
  647. {
  648. $block = @fread($fsock, 1024);
  649. $filesize += strlen($block);
  650. if ($this->max_filesize && $filesize > $this->max_filesize)
  651. {
  652. $max_filesize = get_formatted_filesize($this->max_filesize, false);
  653. $file = new fileerror(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']));
  654. return $file;
  655. }
  656. $data .= $block;
  657. }
  658. else
  659. {
  660. $line = @fgets($fsock, 1024);
  661. if ($line == "\r\n")
  662. {
  663. $get_info = true;
  664. }
  665. else
  666. {
  667. if (stripos($line, 'content-type: ') !== false)
  668. {
  669. $upload_ary['type'] = rtrim(str_replace('content-type: ', '', strtolower($line)));
  670. }
  671. else if ($this->max_filesize && stripos($line, 'content-length: ') !== false)
  672. {
  673. $length = (int) str_replace('content-length: ', '', strtolower($line));
  674. if ($length && $length > $this->max_filesize)
  675. {
  676. $max_filesize = get_formatted_filesize($this->max_filesize, false);
  677. $file = new fileerror(sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']));
  678. return $file;
  679. }
  680. }
  681. else if (stripos($line, '404 not found') !== false)
  682. {
  683. $file = new fileerror($user->lang[$this->error_prefix . 'URL_NOT_FOUND']);
  684. return $file;
  685. }
  686. }
  687. }
  688. }
  689. @fclose($fsock);
  690. if (empty($data))
  691. {
  692. $file = new fileerror($user->lang[$this->error_prefix . 'EMPTY_REMOTE_DATA']);
  693. return $file;
  694. }
  695. $tmp_path = (!@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'off') ? false : $phpbb_root_path . 'cache';
  696. $filename = tempnam($tmp_path, unique_id() . '-');
  697. if (!($fp = @fopen($filename, 'wb')))
  698. {
  699. $file = new fileerror($user->lang[$this->error_prefix . 'NOT_UPLOADED']);
  700. return $file;
  701. }
  702. $upload_ary['size'] = fwrite($fp, $data);
  703. fclose($fp);
  704. unset($data);
  705. $upload_ary['tmp_name'] = $filename;
  706. $file = new filespec($upload_ary, $this);
  707. $this->common_checks($file);
  708. return $file;
  709. }
  710. /**
  711. * Assign internal error
  712. * @access private
  713. */
  714. function assign_internal_error($errorcode)
  715. {
  716. global $user;
  717. switch ($errorcode)
  718. {
  719. case 1:
  720. $max_filesize = @ini_get('upload_max_filesize');
  721. $unit = 'MB';
  722. if (!empty($max_filesize))
  723. {
  724. $unit = strtolower(substr($max_filesize, -1, 1));
  725. $max_filesize = (int) $max_filesize;
  726. $unit = ($unit == 'k') ? 'KB' : (($unit == 'g') ? 'GB' : 'MB');
  727. }
  728. $error = (empty($max_filesize)) ? $user->lang[$this->error_prefix . 'PHP_SIZE_NA'] : sprintf($user->lang[$this->error_prefix . 'PHP_SIZE_OVERRUN'], $max_filesize, $user->lang[$unit]);
  729. break;
  730. case 2:
  731. $max_filesize = get_formatted_filesize($this->max_filesize, false);
  732. $error = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']);
  733. break;
  734. case 3:
  735. $error = $user->lang[$this->error_prefix . 'PARTIAL_UPLOAD'];
  736. break;
  737. case 4:
  738. $error = $user->lang[$this->error_prefix . 'NOT_UPLOADED'];
  739. break;
  740. case 6:
  741. $error = 'Temporary folder could not be found. Please check your PHP installation.';
  742. break;
  743. default:
  744. $error = false;
  745. break;
  746. }
  747. return $error;
  748. }
  749. /**
  750. * Perform common checks
  751. */
  752. function common_checks(&$file)
  753. {
  754. global $user;
  755. // Filesize is too big or it's 0 if it was larger than the maxsize in the upload form
  756. if ($this->max_filesize && ($file->get('filesize') > $this->max_filesize || $file->get('filesize') == 0))
  757. {
  758. $max_filesize = get_formatted_filesize($this->max_filesize, false);
  759. $file->error[] = sprintf($user->lang[$this->error_prefix . 'WRONG_FILESIZE'], $max_filesize['value'], $max_filesize['unit']);
  760. }
  761. // check Filename
  762. if (preg_match("#[\\/:*?\"<>|]#i", $file->get('realname')))
  763. {
  764. $file->error[] = sprintf($user->lang[$this->error_prefix . 'INVALID_FILENAME'], $file->get('realname'));
  765. }
  766. // Invalid Extension
  767. if (!$this->valid_extension($file))
  768. {
  769. $file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_EXTENSION'], $file->get('extension'));
  770. }
  771. // MIME Sniffing
  772. if (!$this->valid_content($file))
  773. {
  774. $file->error[] = sprintf($user->lang[$this->error_prefix . 'DISALLOWED_CONTENT']);
  775. }
  776. }
  777. /**
  778. * Check for allowed extension
  779. */
  780. function valid_extension(&$file)
  781. {
  782. return (in_array($file->get('extension'), $this->allowed_extensions)) ? true : false;
  783. }
  784. /**
  785. * Check for allowed dimension
  786. */
  787. function valid_dimensions(&$file)
  788. {
  789. if (!$this->max_width && !$this->max_height && !$this->min_width && !$this->min_height)
  790. {
  791. return true;
  792. }
  793. if (($file->get('width') > $this->max_width && $this->max_width) ||
  794. ($file->get('height') > $this->max_height && $this->max_height) ||
  795. ($file->get('width') < $this->min_width && $this->min_width) ||
  796. ($file->get('height') < $this->min_height && $this->min_height))
  797. {
  798. return false;
  799. }
  800. return true;
  801. }
  802. /**
  803. * Check if form upload is valid
  804. */
  805. function is_valid($form_name)
  806. {
  807. return (isset($_FILES[$form_name]) && $_FILES[$form_name]['name'] != 'none') ? true : false;
  808. }
  809. /**
  810. * Check for allowed extension
  811. */
  812. function valid_content(&$file)
  813. {
  814. return ($file->check_content($this->disallowed_content));
  815. }
  816. /**
  817. * Return image type/extension mapping
  818. */
  819. function image_types()
  820. {
  821. return array(
  822. 1 => array('gif'),
  823. 2 => array('jpg', 'jpeg'),
  824. 3 => array('png'),
  825. 4 => array('swf'),
  826. 5 => array('psd'),
  827. 6 => array('bmp'),
  828. 7 => array('tif', 'tiff'),
  829. 8 => array('tif', 'tiff'),
  830. 9 => array('jpg', 'jpeg'),
  831. 10 => array('jpg', 'jpeg'),
  832. 11 => array('jpg', 'jpeg'),
  833. 12 => array('jpg', 'jpeg'),
  834. 13 => array('swc'),
  835. 14 => array('iff'),
  836. 15 => array('wbmp'),
  837. 16 => array('xbm'),
  838. );
  839. }
  840. }
  841. ?>