Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

remote.php

Zuletzt modifiziert: 09.10.2024, 12:54 - Dateigröße: 5.54 KiB


001  <?php
002  /**
003   *
004   * This file is part of the phpBB Forum Software package.
005   *
006   * @copyright (c) phpBB Limited <https://www.phpbb.com>
007   * @license GNU General Public License, version 2 (GPL-2.0)
008   *
009   * For full copyright and license information, please see
010   * the docs/CREDITS.txt file.
011   *
012   */
013   
014  namespace phpbb\files\types;
015   
016  use bantu\IniGetWrapper\IniGetWrapper;
017  use phpbb\config\config;
018  use phpbb\files\factory;
019  use phpbb\files\filespec;
020  use phpbb\language\language;
021  use phpbb\request\request_interface;
022   
023  class remote extends base
024  {
025      /** @var config phpBB config */
026      protected $config;
027   
028      /** @var factory Files factory */
029      protected $factory;
030   
031      /** @var language */
032      protected $language;
033   
034      /** @var IniGetWrapper */
035      protected $php_ini;
036   
037      /** @var request_interface */
038      protected $request;
039   
040      /** @var \phpbb\files\upload */
041      protected $upload;
042   
043      /** @var string phpBB root path */
044      protected $phpbb_root_path;
045   
046      /**
047       * Construct a form upload type
048       *
049       * @param config $config phpBB config
050       * @param factory $factory Files factory
051       * @param language $language Language class
052       * @param IniGetWrapper $php_ini ini_get() wrapper
053       * @param request_interface $request Request object
054       * @param string $phpbb_root_path phpBB root path
055       */
056      public function __construct(config $config, factory $factory, language $language, IniGetWrapper $php_ini, request_interface $request, $phpbb_root_path)
057      {
058          $this->config = $config;
059          $this->factory = $factory;
060          $this->language = $language;
061          $this->php_ini = $php_ini;
062          $this->request = $request;
063          $this->phpbb_root_path = $phpbb_root_path;
064      }
065   
066      /**
067       * {@inheritdoc}
068       */
069      public function upload()
070      {
071          $args = func_get_args();
072          return $this->remote_upload($args[0]);
073      }
074   
075      /**
076       * Remote upload method
077       * Uploads file from given url
078       *
079       * @param string $upload_url URL pointing to file to upload, for example http://www.foobar.com/example.gif
080       * @return filespec $file Object "filespec" is returned, all further operations can be done with this object
081       * @access public
082       */
083      protected function remote_upload($upload_url)
084      {
085          $upload_ary = array();
086          $upload_ary['local_mode'] = true;
087   
088          if (!preg_match('#^(https?://).*?\.(' . implode('|', $this->upload->allowed_extensions) . ')$#i', $upload_url, $match))
089          {
090              return $this->factory->get('filespec')->set_error($this->language->lang($this->upload->error_prefix . 'URL_INVALID'));
091          }
092   
093          $url = parse_url($upload_url);
094   
095          $upload_ary['type'] = 'application/octet-stream';
096   
097          $url['path'] = explode('.', $url['path']);
098          $ext = array_pop($url['path']);
099   
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  }
208