PageRenderTime 36ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/phpbb/avatar/driver/remote.php

http://github.com/phpbb/phpbb
PHP | 236 lines | 165 code | 29 blank | 42 comment | 39 complexity | bd0bf430f2f263f6898435f7d5c79f93 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0
  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 hosted remotely
  16. */
  17. class remote extends \phpbb\avatar\driver\driver
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function get_data($row)
  23. {
  24. return array(
  25. 'src' => $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. $template->assign_vars(array(
  36. 'AVATAR_REMOTE_WIDTH' => ((in_array($row['avatar_type'], array(AVATAR_REMOTE, $this->get_name(), 'remote'))) && $row['avatar_width']) ? $row['avatar_width'] : $request->variable('avatar_remote_width', ''),
  37. 'AVATAR_REMOTE_HEIGHT' => ((in_array($row['avatar_type'], array(AVATAR_REMOTE, $this->get_name(), 'remote'))) && $row['avatar_height']) ? $row['avatar_height'] : $request->variable('avatar_remote_width', ''),
  38. 'AVATAR_REMOTE_URL' => ((in_array($row['avatar_type'], array(AVATAR_REMOTE, $this->get_name(), 'remote'))) && $row['avatar']) ? $row['avatar'] : '',
  39. ));
  40. return true;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function process_form($request, $template, $user, $row, &$error)
  46. {
  47. global $phpbb_dispatcher;
  48. $url = $request->variable('avatar_remote_url', '');
  49. $width = $request->variable('avatar_remote_width', 0);
  50. $height = $request->variable('avatar_remote_height', 0);
  51. if (empty($url))
  52. {
  53. return false;
  54. }
  55. if (!preg_match('#^(http|https|ftp)://#i', $url))
  56. {
  57. $url = 'http://' . $url;
  58. }
  59. if (!function_exists('validate_data'))
  60. {
  61. require($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
  62. }
  63. $validate_array = validate_data(
  64. array(
  65. 'url' => $url,
  66. ),
  67. array(
  68. 'url' => array('string', true, 5, 255),
  69. )
  70. );
  71. $error = array_merge($error, $validate_array);
  72. if (!empty($error))
  73. {
  74. return false;
  75. }
  76. /**
  77. * Event to make custom validation of avatar upload
  78. *
  79. * @event core.ucp_profile_avatar_upload_validation
  80. * @var string url Image url
  81. * @var string width Image width
  82. * @var string height Image height
  83. * @var array error Error message array
  84. * @since 3.2.9-RC1
  85. */
  86. $vars = array('url', 'width', 'height', 'error');
  87. extract($phpbb_dispatcher->trigger_event('core.ucp_profile_avatar_upload_validation', compact($vars)));
  88. if (!empty($error))
  89. {
  90. return false;
  91. }
  92. // Check if this url looks alright
  93. // Do not allow specifying the port (see RFC 3986) or IP addresses
  94. if (!preg_match('#^(http|https|ftp)://(?:(.*?\.)*?[a-z0-9\-]+?\.[a-z]{2,4}|(?:\d{1,3}\.){3,5}\d{1,3}):?([0-9]*?).*?\.('. implode('|', $this->allowed_extensions) . ')$#i', $url) ||
  95. preg_match('@^(http|https|ftp)://[^/:?#]+:[0-9]+[/:?#]@i', $url) ||
  96. preg_match('#^(http|https|ftp)://(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])#i', $url) ||
  97. preg_match('#^(http|https|ftp)://(?:(?:(?:[\dA-F]{1,4}:){6}(?:[\dA-F]{1,4}:[\dA-F]{1,4}|(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])))|(?:::(?:[\dA-F]{1,4}:){0,5}(?:[\dA-F]{1,4}(?::[\dA-F]{1,4})?|(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])))|(?:(?:[\dA-F]{1,4}:):(?:[\dA-F]{1,4}:){4}(?:[\dA-F]{1,4}:[\dA-F]{1,4}|(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])))|(?:(?:[\dA-F]{1,4}:){1,2}:(?:[\dA-F]{1,4}:){3}(?:[\dA-F]{1,4}:[\dA-F]{1,4}|(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])))|(?:(?:[\dA-F]{1,4}:){1,3}:(?:[\dA-F]{1,4}:){2}(?:[\dA-F]{1,4}:[\dA-F]{1,4}|(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])))|(?:(?:[\dA-F]{1,4}:){1,4}:(?:[\dA-F]{1,4}:)(?:[\dA-F]{1,4}:[\dA-F]{1,4}|(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])))|(?:(?:[\dA-F]{1,4}:){1,5}:(?:[\dA-F]{1,4}:[\dA-F]{1,4}|(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])))|(?:(?:[\dA-F]{1,4}:){1,6}:[\dA-F]{1,4})|(?:(?:[\dA-F]{1,4}:){1,7}:)|(?:::))#i', $url))
  98. {
  99. $error[] = 'AVATAR_URL_INVALID';
  100. return false;
  101. }
  102. // Get image dimensions
  103. if (($width <= 0 || $height <= 0) && (($image_data = $this->imagesize->getImageSize($url)) === false))
  104. {
  105. $error[] = 'UNABLE_GET_IMAGE_SIZE';
  106. return false;
  107. }
  108. if (!empty($image_data) && ($image_data['width'] <= 0 || $image_data['height'] <= 0))
  109. {
  110. $error[] = 'AVATAR_NO_SIZE';
  111. return false;
  112. }
  113. $width = ($width && $height) ? $width : $image_data['width'];
  114. $height = ($width && $height) ? $height : $image_data['height'];
  115. if ($width <= 0 || $height <= 0)
  116. {
  117. $error[] = 'AVATAR_NO_SIZE';
  118. return false;
  119. }
  120. $types = \phpbb\files\upload::image_types();
  121. $extension = strtolower(\phpbb\files\filespec::get_extension($url));
  122. // Check if this is actually an image
  123. if ($file_stream = @fopen($url, 'r'))
  124. {
  125. // Timeout after 1 second
  126. stream_set_timeout($file_stream, 1);
  127. // read some data to ensure headers are present
  128. fread($file_stream, 1024);
  129. $meta = stream_get_meta_data($file_stream);
  130. if (isset($meta['wrapper_data']['headers']) && is_array($meta['wrapper_data']['headers']))
  131. {
  132. $headers = $meta['wrapper_data']['headers'];
  133. }
  134. else if (isset($meta['wrapper_data']) && is_array($meta['wrapper_data']))
  135. {
  136. $headers = $meta['wrapper_data'];
  137. }
  138. else
  139. {
  140. $headers = array();
  141. }
  142. foreach ($headers as $header)
  143. {
  144. $header = preg_split('/ /', $header, 2);
  145. if (strtr(strtolower(trim($header[0], ':')), '_', '-') === 'content-type')
  146. {
  147. if (strpos($header[1], 'image/') !== 0)
  148. {
  149. $error[] = 'AVATAR_URL_INVALID';
  150. fclose($file_stream);
  151. return false;
  152. }
  153. else
  154. {
  155. fclose($file_stream);
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. else
  162. {
  163. $error[] = 'AVATAR_URL_INVALID';
  164. return false;
  165. }
  166. if (!empty($image_data) && (!isset($types[$image_data['type']]) || !in_array($extension, $types[$image_data['type']])))
  167. {
  168. if (!isset($types[$image_data['type']]))
  169. {
  170. $error[] = 'UNABLE_GET_IMAGE_SIZE';
  171. }
  172. else
  173. {
  174. $error[] = array('IMAGE_FILETYPE_MISMATCH', $types[$image_data['type']][0], $extension);
  175. }
  176. return false;
  177. }
  178. if ($this->config['avatar_max_width'] || $this->config['avatar_max_height'])
  179. {
  180. if ($width > $this->config['avatar_max_width'] || $height > $this->config['avatar_max_height'])
  181. {
  182. $error[] = array('AVATAR_WRONG_SIZE', $this->config['avatar_min_width'], $this->config['avatar_min_height'], $this->config['avatar_max_width'], $this->config['avatar_max_height'], $width, $height);
  183. return false;
  184. }
  185. }
  186. if ($this->config['avatar_min_width'] || $this->config['avatar_min_height'])
  187. {
  188. if ($width < $this->config['avatar_min_width'] || $height < $this->config['avatar_min_height'])
  189. {
  190. $error[] = array('AVATAR_WRONG_SIZE', $this->config['avatar_min_width'], $this->config['avatar_min_height'], $this->config['avatar_max_width'], $this->config['avatar_max_height'], $width, $height);
  191. return false;
  192. }
  193. }
  194. return array(
  195. 'avatar' => $url,
  196. 'avatar_width' => $width,
  197. 'avatar_height' => $height,
  198. );
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function get_template_name()
  204. {
  205. return 'ucp_avatar_options_remote.html';
  206. }
  207. }