[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/phpbb/files/types/ -> 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\files\types;
  15  
  16  use bantu\IniGetWrapper\IniGetWrapper;
  17  use phpbb\config\config;
  18  use phpbb\files\factory;
  19  use phpbb\files\filespec;
  20  use phpbb\language\language;
  21  use phpbb\request\request_interface;
  22  
  23  class remote extends base
  24  {
  25      /** @var config phpBB config */
  26      protected $config;
  27  
  28      /** @var factory Files factory */
  29      protected $factory;
  30  
  31      /** @var language */
  32      protected $language;
  33  
  34      /** @var IniGetWrapper */
  35      protected $php_ini;
  36  
  37      /** @var request_interface */
  38      protected $request;
  39  
  40      /** @var \phpbb\files\upload */
  41      protected $upload;
  42  
  43      /** @var string phpBB root path */
  44      protected $phpbb_root_path;
  45  
  46      /**
  47       * Construct a form upload type
  48       *
  49       * @param config $config phpBB config
  50       * @param factory $factory Files factory
  51       * @param language $language Language class
  52       * @param IniGetWrapper $php_ini ini_get() wrapper
  53       * @param request_interface $request Request object
  54       * @param string $phpbb_root_path phpBB root path
  55       */
  56  	public function __construct(config $config, factory $factory, language $language, IniGetWrapper $php_ini, request_interface $request, $phpbb_root_path)
  57      {
  58          $this->config = $config;
  59          $this->factory = $factory;
  60          $this->language = $language;
  61          $this->php_ini = $php_ini;
  62          $this->request = $request;
  63          $this->phpbb_root_path = $phpbb_root_path;
  64      }
  65  
  66      /**
  67       * {@inheritdoc}
  68       */
  69  	public function upload()
  70      {
  71          $args = func_get_args();
  72          return $this->remote_upload($args[0]);
  73      }
  74  
  75      /**
  76       * Remote upload method
  77       * Uploads file from given url
  78       *
  79       * @param string $upload_url URL pointing to file to upload, for example http://www.foobar.com/example.gif
  80       * @return filespec $file Object "filespec" is returned, all further operations can be done with this object
  81       * @access public
  82       */
  83  	protected function remote_upload($upload_url)
  84      {
  85          $upload_ary = array();
  86          $upload_ary['local_mode'] = true;
  87  
  88          if (!preg_match('#^(https?://).*?\.(' . implode('|', $this->upload->allowed_extensions) . ')$#i', $upload_url, $match))
  89          {
  90              return $this->factory->get('filespec')->set_error($this->language->lang($this->upload->error_prefix . 'URL_INVALID'));
  91          }
  92  
  93          $url = parse_url($upload_url);
  94  
  95          $upload_ary['type'] = 'application/octet-stream';
  96  
  97          $url['path'] = explode('.', $url['path']);
  98          $ext = array_pop($url['path']);
  99  
 100          $url['path'] = implode('', $url['path']);
 101          $upload_ary['name'] = utf8_basename($url['path']) . (($ext) ? '.' . $ext : '');
 102  
 103          $remote_max_filesize = $this->get_max_file_size();
 104  
 105          $guzzle_options = [
 106              'timeout'            => $this->upload->upload_timeout,
 107              'connect_timeout'    => $this->upload->upload_timeout,
 108              'verify'            => !empty($this->config['remote_upload_verify']) ? (bool) $this->config['remote_upload_verify'] : false,
 109          ];
 110          $client = new \GuzzleHttp\Client($guzzle_options);
 111  
 112          try
 113          {
 114              $response = $client->get($upload_url, $guzzle_options);
 115          }
 116          catch (\GuzzleHttp\Exception\ClientException $clientException)
 117          {
 118              return $this->factory->get('filespec')->set_error($this->upload->error_prefix . 'URL_NOT_FOUND');
 119          }
 120          catch (\GuzzleHttp\Exception\RequestException $requestException)
 121          {
 122              if (strpos($requestException->getMessage(), 'cURL error 28') !== false || preg_match('/408|504/', $requestException->getCode()))
 123              {
 124                  return $this->factory->get('filespec')->set_error($this->upload->error_prefix . 'REMOTE_UPLOAD_TIMEOUT');
 125              }
 126              else
 127              {
 128                  return $this->factory->get('filespec')->set_error($this->language->lang($this->upload->error_prefix . 'NOT_UPLOADED'));
 129              }
 130          }
 131          catch (\Exception $e)
 132          {
 133              return $this->factory->get('filespec')->set_error($this->language->lang($this->upload->error_prefix . 'NOT_UPLOADED'));
 134          }
 135  
 136          $content_length = $response->getBody()->getSize();
 137          if ($remote_max_filesize && $content_length > $remote_max_filesize)
 138          {
 139              $max_filesize = get_formatted_filesize($remote_max_filesize, false);
 140  
 141              return $this->factory->get('filespec')->set_error($this->language->lang($this->upload->error_prefix . 'WRONG_FILESIZE', $max_filesize['value'], $max_filesize['unit']));
 142          }
 143  
 144          if ($content_length == 0)
 145          {
 146              return $this->factory->get('filespec')->set_error($this->upload->error_prefix . 'EMPTY_REMOTE_DATA');
 147          }
 148  
 149          $data = $response->getBody();
 150  
 151          $filename = tempnam(sys_get_temp_dir(), unique_id() . '-');
 152  
 153          if (!($fp = @fopen($filename, 'wb')))
 154          {
 155              return $this->factory->get('filespec')->set_error($this->upload->error_prefix . 'NOT_UPLOADED');
 156          }
 157  
 158          $upload_ary['size'] = fwrite($fp, $data);
 159          fclose($fp);
 160          unset($data);
 161  
 162          $upload_ary['tmp_name'] = $filename;
 163  
 164          /** @var filespec $file */
 165          $file = $this->factory->get('filespec')
 166              ->set_upload_ary($upload_ary)
 167              ->set_upload_namespace($this->upload);
 168          $this->upload->common_checks($file);
 169  
 170          return $file;
 171      }
 172  
 173      /**
 174       * Get maximum file size for remote uploads
 175       *
 176       * @return int Maximum file size
 177       */
 178  	protected function get_max_file_size()
 179      {
 180          $max_file_size = $this->upload->max_filesize;
 181          if (!$max_file_size)
 182          {
 183              $max_file_size = $this->php_ini->getString('upload_max_filesize');
 184  
 185              if (!empty($max_file_size))
 186              {
 187                  $unit = strtolower(substr($max_file_size, -1, 1));
 188                  $max_file_size = (int) $max_file_size;
 189  
 190                  switch ($unit)
 191                  {
 192                      case 'g':
 193                          $max_file_size *= 1024;
 194                      // no break
 195                      case 'm':
 196                          $max_file_size *= 1024;
 197                      // no break
 198                      case 'k':
 199                          $max_file_size *= 1024;
 200                      // no break
 201                  }
 202              }
 203          }
 204  
 205          return $max_file_size;
 206      }
 207  }


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