Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

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

type_cast_helper.php

Zuletzt modifiziert: 09.10.2024, 12:51 - Dateigröße: 4.60 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\request;
015   
016  /**
017  * A helper class that provides convenience methods for type casting.
018  */
019  class type_cast_helper implements \phpbb\request\type_cast_helper_interface
020  {
021   
022      /**
023      * @var    string    Whether slashes need to be stripped from input
024      */
025      protected $strip;
026   
027      /**
028      * Initialises the type cast helper class.
029      * All it does is find out whether magic quotes are turned on.
030      */
031      public function __construct()
032      {
033          if (version_compare(PHP_VERSION, '5.4.0-dev', '>='))
034          {
035              $this->strip = false;
036          }
037          else
038          {
039              $this->strip = (@get_magic_quotes_gpc()) ? true : false;
040          }
041      }
042   
043      /**
044      * Recursively applies addslashes to a variable.
045      *
046      * @param    mixed    &$var    Variable passed by reference to which slashes will be added.
047      */
048      public function addslashes_recursively(&$var)
049      {
050          if (is_string($var))
051          {
052              $var = addslashes($var);
053          }
054          else if (is_array($var))
055          {
056              $var_copy = $var;
057              $var = array();
058              foreach ($var_copy as $key => $value)
059              {
060                  if (is_string($key))
061                  {
062                      $key = addslashes($key);
063                  }
064                  $var[$key] = $value;
065   
066                  $this->addslashes_recursively($var[$key]);
067              }
068          }
069      }
070   
071      /**
072      * Recursively applies addslashes to a variable if magic quotes are turned on.
073      *
074      * @param    mixed    &$var    Variable passed by reference to which slashes will be added.
075      */
076      public function add_magic_quotes(&$var)
077      {
078          if ($this->strip)
079          {
080              $this->addslashes_recursively($var);
081          }
082      }
083   
084      /**
085      * Set variable $result to a particular type.
086      *
087      * @param mixed    &$result        The variable to fill
088      * @param mixed    $var            The contents to fill with
089      * @param mixed    $type            The variable type. Will be used with {@link settype()}
090      * @param bool    $multibyte        Indicates whether string values may contain UTF-8 characters.
091      *                                 Default is false, causing all bytes outside the ASCII range (0-127) to be replaced with question marks.
092      * @param bool    $trim            Indicates whether trim() should be applied to string values.
093      *                                 Default is true.
094      */
095      public function set_var(&$result, $var, $type, $multibyte = false, $trim = true)
096      {
097          settype($var, $type);
098          $result = $var;
099   
100          if ($type == 'string')
101          {
102              $result = str_replace(array("\r\n", "\r", "\0"), array("\n", "\n", ''), $result);
103   
104              if ($trim)
105              {
106                  $result = trim($result);
107              }
108   
109              $result = htmlspecialchars($result, ENT_COMPAT, 'UTF-8');
110   
111              if ($multibyte)
112              {
113                  $result = utf8_normalize_nfc($result);
114              }
115   
116              if (!empty($result))
117              {
118                  // Make sure multibyte characters are wellformed
119                  if ($multibyte)
120                  {
121                      if (!preg_match('/^./u', $result))
122                      {
123                          $result = '';
124                      }
125                  }
126                  else
127                  {
128                      // no multibyte, allow only ASCII (0-127)
129                      $result = preg_replace('/[\x80-\xFF]/', '?', $result);
130                  }
131              }
132   
133              $result = ($this->strip) ? stripslashes($result) : $result;
134          }
135      }
136   
137      /**
138      * Recursively sets a variable to a given type using {@link set_var set_var}
139      *
140      * @param    string    $var        The value which shall be sanitised (passed by reference).
141      * @param    mixed    $default    Specifies the type $var shall have.
142      *                                 If it is an array and $var is not one, then an empty array is returned.
143      *                                 Otherwise var is cast to the same type, and if $default is an array all
144      *                                 keys and values are cast recursively using this function too.
145      * @param    bool    $multibyte    Indicates whether string keys and values may contain UTF-8 characters.
146      *                                 Default is false, causing all bytes outside the ASCII range (0-127) to
147      *                                 be replaced with question marks.
148      * @param    bool    $trim        Indicates whether trim() should be applied to string values.
149      *                                 Default is true.
150      */
151      public function recursive_set_var(&$var, $default, $multibyte, $trim = true)
152      {
153          if (is_array($var) !== is_array($default))
154          {
155              $var = (is_array($default)) ? array() : $default;
156              return;
157          }
158   
159          if (!is_array($default))
160          {
161              $type = gettype($default);
162              $this->set_var($var, $var, $type, $multibyte, $trim);
163          }
164          else
165          {
166              // make sure there is at least one key/value pair to use get the
167              // types from
168              if (empty($default))
169              {
170                  $var = array();
171                  return;
172              }
173   
174              list($default_key, $default_value) = each($default);
175              $value_type = gettype($default_value);
176              $key_type = gettype($default_key);
177   
178              $_var = $var;
179              $var = array();
180   
181              foreach ($_var as $k => $v)
182              {
183                  $this->set_var($k, $k, $key_type, $multibyte);
184   
185                  $this->recursive_set_var($v, $default_value, $multibyte, $trim);
186                  $var[$k] = $v;
187              }
188          }
189      }
190  }
191