Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

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

file_downloader.php

Zuletzt modifiziert: 02.04.2025, 15:01 - Dateigröße: 3.70 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;
015   
016  use GuzzleHttp\Client;
017  use GuzzleHttp\Exception\RequestException;
018  use phpbb\exception\runtime_exception;
019   
020  class file_downloader
021  {
022      const OK = 200;
023      const NOT_FOUND = 404;
024      const REQUEST_TIMEOUT = 408;
025   
026      /** @var string Error string */
027      protected $error_string = '';
028   
029      /** @var int Error number */
030      protected $error_number = 0;
031   
032      /**
033       * Create new guzzle client
034       *
035       * @param string $host
036       * @param int $port
037       * @param int $timeout
038       *
039       * @return Client
040       */
041      protected function create_client(string $host, int $port = 443, int $timeout = 6): Client
042      {
043          // Only set URL scheme if not specified in URL
044          $url_parts = parse_url($host);
045          if (!isset($url_parts['scheme']))
046          {
047              $host = (($port === 443) ? 'https://' : 'http://') . $host;
048          }
049   
050          // Initialize Guzzle client
051          return new Client([
052              'base_uri' => $host,
053              'timeout'  => $timeout,
054              'headers' => [
055                  'user-agent' => 'phpBB/' . PHPBB_VERSION,
056                  'accept' => '*/*',
057                ],
058          ]);
059      }
060   
061      /**
062       * Retrieve contents from remotely stored file
063       *
064       * @param string    $host            File host
065       * @param string    $directory       Directory file is in
066       * @param string    $filename        Filename of file to retrieve
067       * @param int       $port            Port to connect to; default: 443
068       * @param int       $timeout         Connection timeout in seconds; default: 6
069       *
070       * @return false|string File data as string if file can be read and there is no
071       *         timeout, false if there were errors or the connection timed out
072       *
073       * @throws runtime_exception If data can't be retrieved and no error
074       *         message is returned
075       */
076      public function get(string $host, string $directory, string $filename, int $port = 443, int $timeout = 6)
077      {
078          try
079          {
080              // Initialize Guzzle client
081              $client = $this->create_client($host, $port, $timeout);
082          }
083          catch (\RuntimeException $exception)
084          {
085              throw new runtime_exception('HTTP_HANDLER_NOT_FOUND');
086          }
087   
088          // Set default values for error variables
089          $this->error_number = 0;
090          $this->error_string = '';
091   
092          try
093          {
094              $response = $client->request('GET', "$directory/$filename");
095   
096              // Check if the response status code is 200 (OK)
097              if ($response->getStatusCode() == self::OK)
098              {
099                  return $response->getBody()->getContents();
100              }
101              else
102              {
103                  $this->error_number = $response->getStatusCode();
104                  throw new runtime_exception('FILE_NOT_FOUND', [$filename]);
105              }
106          }
107          catch (RequestException $exception)
108          {
109              if ($exception->hasResponse())
110              {
111                  $this->error_number = $exception->getResponse()->getStatusCode();
112   
113                  if ($this->error_number == self::NOT_FOUND)
114                  {
115                      throw new runtime_exception('FILE_NOT_FOUND', [$filename]);
116                  }
117              }
118              else
119              {
120                  $this->error_number = self::REQUEST_TIMEOUT;
121                  throw new runtime_exception('FSOCK_TIMEOUT');
122              }
123   
124              $this->error_string = utf8_convert_message($exception->getMessage());
125              return false;
126          }
127          catch (runtime_exception $exception)
128          {
129              // Rethrow runtime_exceptions, only handle unknown cases below
130              throw $exception;
131          }
132          catch (\Throwable $exception)
133          {
134              $this->error_string = utf8_convert_message($exception->getMessage());
135              throw new runtime_exception('FSOCK_DISABLED');
136          }
137      }
138   
139      /**
140       * Get error string
141       *
142       * @return string Error string
143       */
144      public function get_error_string(): string
145      {
146          return $this->error_string;
147      }
148   
149      /**
150       * Get error number
151       *
152       * @return int Error number
153       */
154      public function get_error_number(): int
155      {
156          return $this->error_number;
157      }
158  }
159