[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/phpbb/textformatter/s9e/ -> utils.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\textformatter\s9e;
  15  
  16  /**
  17  * Text manipulation utilities
  18  */
  19  class utils implements \phpbb\textformatter\utils_interface
  20  {
  21      /**
  22      * Replace BBCodes and other formatting elements with whitespace
  23      *
  24      * NOTE: preserves smilies as text
  25      *
  26      * @param  string $xml Parsed text
  27      * @return string      Plain text
  28      */
  29  	public function clean_formatting($xml)
  30      {
  31          // Insert a space before <s> and <e> then remove formatting
  32          $xml = preg_replace('#<[es]>#', ' $0', $xml);
  33  
  34          return utf8_htmlspecialchars(\s9e\TextFormatter\Utils::removeFormatting($xml));
  35      }
  36  
  37      /**
  38      * Format given string to be used as an attribute value
  39      *
  40      * Will return the string as-is if it can be used in a BBCode without quotes. Otherwise,
  41      * it will use either single- or double- quotes depending on whichever requires less escaping.
  42      * Quotes and backslashes are escaped with backslashes where necessary
  43      *
  44      * @param  string $str Original string
  45      * @return string      Same string if possible, escaped string within quotes otherwise
  46      */
  47  	protected function format_attribute_value($str)
  48      {
  49          if (!preg_match('/[ "\'\\\\\\]]/', $str))
  50          {
  51              // Return as-is if it contains none of: space, ' " \ or ]
  52              return $str;
  53          }
  54          $singleQuoted = "'" . addcslashes($str, "\\'") . "'";
  55          $doubleQuoted = '"' . addcslashes($str, '\\"') . '"';
  56  
  57          return (strlen($singleQuoted) < strlen($doubleQuoted)) ? $singleQuoted : $doubleQuoted;
  58      }
  59  
  60      /**
  61      * {@inheritdoc}
  62      */
  63  	public function generate_quote($text, array $attributes = array())
  64      {
  65          $text = trim($text);
  66          $quote = '[quote';
  67          if (isset($attributes['author']))
  68          {
  69              // Add the author as the BBCode's default attribute
  70              $quote .= '=' . $this->format_attribute_value($attributes['author']);
  71              unset($attributes['author']);
  72          }
  73  
  74          if (isset($attributes['user_id']) && $attributes['user_id'] == ANONYMOUS)
  75          {
  76              unset($attributes['user_id']);
  77          }
  78  
  79          ksort($attributes);
  80          foreach ($attributes as $name => $value)
  81          {
  82              $quote .= ' ' . $name . '=' . $this->format_attribute_value($value);
  83          }
  84          $quote .= ']';
  85          $newline = (strlen($quote . $text . '[/quote]') > 80 || strpos($text, "\n") !== false) ? "\n" : '';
  86          $quote .= $newline . $text . $newline . '[/quote]';
  87  
  88          return $quote;
  89      }
  90  
  91      /**
  92      * Get a list of quote authors, limited to the outermost quotes
  93      *
  94      * @param  string   $xml Parsed text
  95      * @return string[]      List of authors
  96      */
  97  	public function get_outermost_quote_authors($xml)
  98      {
  99          $authors = array();
 100          if (strpos($xml, '<QUOTE ') === false)
 101          {
 102              return $authors;
 103          }
 104  
 105          $dom = new \DOMDocument;
 106          $dom->loadXML($xml);
 107          $xpath = new \DOMXPath($dom);
 108          foreach ($xpath->query('//QUOTE[not(ancestor::QUOTE)]/@author') as $author)
 109          {
 110              $authors[] = $author->textContent;
 111          }
 112  
 113          return $authors;
 114      }
 115  
 116      /**
 117      * Remove given BBCode and its content, at given nesting depth
 118      *
 119      * @param  string  $xml         Parsed text
 120      * @param  string  $bbcode_name BBCode's name
 121      * @param  integer $depth       Minimum nesting depth (number of parents of the same name)
 122      * @return string               Parsed text
 123      */
 124  	public function remove_bbcode($xml, $bbcode_name, $depth = 0)
 125      {
 126          return \s9e\TextFormatter\Utils::removeTag($xml, strtoupper($bbcode_name), $depth);
 127      }
 128  
 129      /**
 130      * Return a parsed text to its original form
 131      *
 132      * @param  string $xml Parsed text
 133      * @return string      Original plain text
 134      */
 135  	public function unparse($xml)
 136      {
 137          return \s9e\TextFormatter\Unparser::unparse($xml);
 138      }
 139  
 140      /**
 141       * {@inheritdoc}
 142       */
 143  	public function is_empty($text)
 144      {
 145          if ($text === null || $text === '')
 146          {
 147              return true;
 148          }
 149  
 150          return trim($this->unparse($text)) === '';
 151      }
 152  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1