[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/phpbb/avatar/driver/ -> remote.php (source)

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


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1