PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/phpbb/avatar/driver/upload.php

https://github.com/callumacrae/phpbb3
PHP | 190 lines | 148 code | 18 blank | 24 comment | 14 complexity | 1c77314ed1b499950c7bb8959672e248 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. namespace phpbb\avatar\driver;
  14. /**
  15. * Handles avatars uploaded to the board
  16. */
  17. class upload extends \phpbb\avatar\driver\driver
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function get_data($row, $ignore_config = false)
  23. {
  24. return array(
  25. 'src' => $this->path_helper->get_web_root_path() . 'download/file.' . $this->php_ext . '?avatar=' . $row['avatar'],
  26. 'width' => $row['avatar_width'],
  27. 'height' => $row['avatar_height'],
  28. );
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function prepare_form($request, $template, $user, $row, &$error)
  34. {
  35. if (!$this->can_upload())
  36. {
  37. return false;
  38. }
  39. $template->assign_vars(array(
  40. 'S_UPLOAD_AVATAR_URL' => ($this->config['allow_avatar_remote_upload']) ? true : false,
  41. 'AVATAR_UPLOAD_SIZE' => $this->config['avatar_filesize'],
  42. ));
  43. return true;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function process_form($request, $template, $user, $row, &$error)
  49. {
  50. if (!$this->can_upload())
  51. {
  52. return false;
  53. }
  54. if (!class_exists('fileupload'))
  55. {
  56. include($this->phpbb_root_path . 'includes/functions_upload.' . $this->php_ext);
  57. }
  58. $upload = new \fileupload('AVATAR_', $this->allowed_extensions, $this->config['avatar_filesize'], $this->config['avatar_min_width'], $this->config['avatar_min_height'], $this->config['avatar_max_width'], $this->config['avatar_max_height'], (isset($this->config['mime_triggers']) ? explode('|', $this->config['mime_triggers']) : false));
  59. $url = $request->variable('avatar_upload_url', '');
  60. $upload_file = $request->file('avatar_upload_file');
  61. if (!empty($upload_file['name']))
  62. {
  63. $file = $upload->form_upload('avatar_upload_file');
  64. }
  65. else if (!empty($this->config['allow_avatar_remote_upload']) && !empty($url))
  66. {
  67. if (!preg_match('#^(http|https|ftp)://#i', $url))
  68. {
  69. $url = 'http://' . $url;
  70. }
  71. if (!function_exists('validate_data'))
  72. {
  73. require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
  74. }
  75. $validate_array = validate_data(
  76. array(
  77. 'url' => $url,
  78. ),
  79. array(
  80. 'url' => array('string', true, 5, 255),
  81. )
  82. );
  83. $error = array_merge($error, $validate_array);
  84. if (!empty($error))
  85. {
  86. return false;
  87. }
  88. $file = $upload->remote_upload($url);
  89. }
  90. else
  91. {
  92. $error[] = 'NO_AVATAR_SELECTED';
  93. return false;
  94. }
  95. $prefix = $this->config['avatar_salt'] . '_';
  96. $file->clean_filename('avatar', $prefix, $row['id']);
  97. $destination = $this->config['avatar_path'];
  98. // Adjust destination path (no trailing slash)
  99. if (substr($destination, -1, 1) == '/' || substr($destination, -1, 1) == '\\')
  100. {
  101. $destination = substr($destination, 0, -1);
  102. }
  103. $destination = str_replace(array('../', '..\\', './', '.\\'), '', $destination);
  104. if ($destination && ($destination[0] == '/' || $destination[0] == "\\"))
  105. {
  106. $destination = '';
  107. }
  108. // Move file and overwrite any existing image
  109. $file->move_file($destination, true);
  110. if (sizeof($file->error))
  111. {
  112. $file->remove();
  113. $error = array_merge($error, $file->error);
  114. return false;
  115. }
  116. return array(
  117. 'avatar' => $row['id'] . '_' . time() . '.' . $file->get('extension'),
  118. 'avatar_width' => $file->get('width'),
  119. 'avatar_height' => $file->get('height'),
  120. );
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function prepare_form_acp($user)
  126. {
  127. return array(
  128. 'allow_avatar_remote_upload'=> array('lang' => 'ALLOW_REMOTE_UPLOAD', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true),
  129. 'avatar_filesize' => array('lang' => 'MAX_FILESIZE', 'validate' => 'int:0', 'type' => 'number:0', 'explain' => true, 'append' => ' ' . $user->lang['BYTES']),
  130. 'avatar_path' => array('lang' => 'AVATAR_STORAGE_PATH', 'validate' => 'rpath', 'type' => 'text:20:255', 'explain' => true),
  131. );
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function delete($row)
  137. {
  138. $ext = substr(strrchr($row['avatar'], '.'), 1);
  139. $filename = $this->phpbb_root_path . $this->config['avatar_path'] . '/' . $this->config['avatar_salt'] . '_' . $row['id'] . '.' . $ext;
  140. if (file_exists($filename))
  141. {
  142. @unlink($filename);
  143. }
  144. return true;
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function get_template_name()
  150. {
  151. return 'ucp_avatar_options_upload.html';
  152. }
  153. /**
  154. * Check if user is able to upload an avatar
  155. *
  156. * @return bool True if user can upload, false if not
  157. */
  158. protected function can_upload()
  159. {
  160. return (file_exists($this->phpbb_root_path . $this->config['avatar_path']) && phpbb_is_writable($this->phpbb_root_path . $this->config['avatar_path']) && (@ini_get('file_uploads') || strtolower(@ini_get('file_uploads')) == 'on'));
  161. }
  162. }