[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/phpbb/search/sphinx/ -> config.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\search\sphinx;
  15  
  16  /**
  17  * An object representing the sphinx configuration
  18  * Can read it from file and write it back out after modification
  19  */
  20  class config
  21  {
  22      private $sections = array();
  23  
  24      /**
  25      * Constructor which optionally loads data from a variable
  26      *
  27      * @param    string    $config_data    Variable containing the sphinx configuration data
  28      *
  29      * @access    public
  30      */
  31  	function __construct($config_data)
  32      {
  33          if ($config_data != '')
  34          {
  35              $this->read($config_data);
  36          }
  37      }
  38  
  39      /**
  40      * Get a section object by its name
  41      *
  42      * @param    string                                 $name    The name of the section that shall be returned
  43      * @return    \phpbb\search\sphinx\config_section            The section object or null if none was found
  44      *
  45      * @access    public
  46      */
  47  	function get_section_by_name($name)
  48      {
  49          for ($i = 0, $size = count($this->sections); $i < $size; $i++)
  50          {
  51              // Make sure this is really a section object and not a comment
  52              if (($this->sections[$i] instanceof \phpbb\search\sphinx\config_section) && $this->sections[$i]->get_name() == $name)
  53              {
  54                  return $this->sections[$i];
  55              }
  56          }
  57      }
  58  
  59      /**
  60      * Appends a new empty section to the end of the config
  61      *
  62      * @param    string                                $name    The name for the new section
  63      * @return    \phpbb\search\sphinx\config_section            The newly created section object
  64      *
  65      * @access    public
  66      */
  67  	function add_section($name)
  68      {
  69          $this->sections[] = new \phpbb\search\sphinx\config_section($name, '');
  70          return $this->sections[count($this->sections) - 1];
  71      }
  72  
  73      /**
  74      * Reads the config file data
  75      *
  76      * @param    string    $config_data    The config file data
  77      *
  78      * @access    private
  79      */
  80  	function read($config_data)
  81      {
  82          $this->sections = array();
  83  
  84          $section = null;
  85          $found_opening_bracket = false;
  86          $in_value = false;
  87  
  88          foreach ($config_data as $i => $line)
  89          {
  90              // If the value of a variable continues to the next line because the line
  91              // break was escaped then we don't trim leading space but treat it as a part of the value
  92              if ($in_value)
  93              {
  94                  $line = rtrim($line);
  95              }
  96              else
  97              {
  98                  $line = trim($line);
  99              }
 100  
 101              // If we're not inside a section look for one
 102              if (!$section)
 103              {
 104                  // Add empty lines and comments as comment objects to the section list
 105                  // that way they're not deleted when reassembling the file from the sections
 106                  if (!$line || $line[0] == '#')
 107                  {
 108                      $this->sections[] = new \phpbb\search\sphinx\config_comment($config_file[$i]);
 109                      continue;
 110                  }
 111                  else
 112                  {
 113                      // Otherwise we scan the line reading the section name until we find
 114                      // an opening curly bracket or a comment
 115                      $section_name = '';
 116                      $section_name_comment = '';
 117                      $found_opening_bracket = false;
 118                      for ($j = 0, $length = strlen($line); $j < $length; $j++)
 119                      {
 120                          if ($line[$j] == '#')
 121                          {
 122                              $section_name_comment = substr($line, $j);
 123                              break;
 124                          }
 125  
 126                          if ($found_opening_bracket)
 127                          {
 128                              continue;
 129                          }
 130  
 131                          if ($line[$j] == '{')
 132                          {
 133                              $found_opening_bracket = true;
 134                              continue;
 135                          }
 136  
 137                          $section_name .= $line[$j];
 138                      }
 139  
 140                      // And then we create the new section object
 141                      $section_name = trim($section_name);
 142                      $section = new \phpbb\search\sphinx\config_section($section_name, $section_name_comment);
 143                  }
 144              }
 145              else
 146              {
 147                  // If we're looking for variables inside a section
 148                  $skip_first = false;
 149  
 150                  // If we're not in a value continuing over the line feed
 151                  if (!$in_value)
 152                  {
 153                      // Then add empty lines and comments as comment objects to the variable list
 154                      // of this section so they're not deleted on reassembly
 155                      if (!$line || $line[0] == '#')
 156                      {
 157                          $section->add_variable(new \phpbb\search\sphinx\config_comment($config_file[$i]));
 158                          continue;
 159                      }
 160  
 161                      // As long as we haven't yet actually found an opening bracket for this section
 162                      // we treat everything as comments so it's not deleted either
 163                      if (!$found_opening_bracket)
 164                      {
 165                          if ($line[0] == '{')
 166                          {
 167                              $skip_first = true;
 168                              $line = substr($line, 1);
 169                              $found_opening_bracket = true;
 170                          }
 171                          else
 172                          {
 173                              $section->add_variable(new \phpbb\search\sphinx\config_comment($config_file[$i]));
 174                              continue;
 175                          }
 176                      }
 177                  }
 178  
 179                  // If we did not find a comment in this line or still add to the previous
 180                  // line's value ...
 181                  if ($line || $in_value)
 182                  {
 183                      if (!$in_value)
 184                      {
 185                          $name = '';
 186                          $value = '';
 187                          $comment = '';
 188                          $found_assignment = false;
 189                      }
 190                      $in_value = false;
 191                      $end_section = false;
 192  
 193                      /* ... then we should prase this line char by char:
 194                       - first there's the variable name
 195                       - then an equal sign
 196                       - the variable value
 197                       - possibly a backslash before the linefeed in this case we need to continue
 198                         parsing the value in the next line
 199                       - a # indicating that the rest of the line is a comment
 200                       - a closing curly bracket indicating the end of this section*/
 201                      for ($j = 0, $length = strlen($line); $j < $length; $j++)
 202                      {
 203                          if ($line[$j] == '#')
 204                          {
 205                              $comment = substr($line, $j);
 206                              break;
 207                          }
 208                          else if ($line[$j] == '}')
 209                          {
 210                              $comment = substr($line, $j + 1);
 211                              $end_section = true;
 212                              break;
 213                          }
 214                          else if (!$found_assignment)
 215                          {
 216                              if ($line[$j] == '=')
 217                              {
 218                                  $found_assignment = true;
 219                              }
 220                              else
 221                              {
 222                                  $name .= $line[$j];
 223                              }
 224                          }
 225                          else
 226                          {
 227                              if ($line[$j] == '\\' && $j == $length - 1)
 228                              {
 229                                  $value .= "\n";
 230                                  $in_value = true;
 231                                  // Go to the next line and keep processing the value in there
 232                                  continue 2;
 233                              }
 234                              $value .= $line[$j];
 235                          }
 236                      }
 237  
 238                      // If a name and an equal sign were found then we have append a
 239                      // new variable object to the section
 240                      if ($name && $found_assignment)
 241                      {
 242                          $section->add_variable(new \phpbb\search\sphinx\config_variable(trim($name), trim($value), ($end_section) ? '' : $comment));
 243                          continue;
 244                      }
 245  
 246                      /* If we found a closing curly bracket this section has been completed
 247                      and we can append it to the section list and continue with looking for
 248                      the next section */
 249                      if ($end_section)
 250                      {
 251                          $section->set_end_comment($comment);
 252                          $this->sections[] = $section;
 253                          $section = null;
 254                          continue;
 255                      }
 256                  }
 257  
 258                  // If we did not find anything meaningful up to here, then just treat it
 259                  // as a comment
 260                  $comment = ($skip_first) ? "\t" . substr(ltrim($config_file[$i]), 1) : $config_file[$i];
 261                  $section->add_variable(new \phpbb\search\sphinx\config_comment($comment));
 262              }
 263          }
 264  
 265      }
 266  
 267      /**
 268      * Returns the config data
 269      *
 270      * @return    string    $data    The config data that is generated
 271      *
 272      * @access    public
 273      */
 274  	function get_data()
 275      {
 276          $data = "";
 277          foreach ($this->sections as $section)
 278          {
 279              $data .= $section->to_string();
 280          }
 281  
 282          return $data;
 283      }
 284  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1