[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package s9e\TextFormatter 5 * @copyright Copyright (c) 2010-2022 The s9e authors 6 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 7 */ 8 namespace s9e\TextFormatter; 9 10 use DOMDocument; 11 use InvalidArgumentException; 12 13 abstract class Renderer 14 { 15 /** 16 * @var array Associative array of [paramName => paramValue] 17 */ 18 protected $params = []; 19 20 /** 21 * @var string Saved locale 22 */ 23 protected $savedLocale = '0'; 24 25 /** 26 * Create a return a new DOMDocument loaded with given XML 27 * 28 * @param string $xml Source XML 29 * @return DOMDocument 30 */ 31 protected function loadXML($xml) 32 { 33 $this->checkUnsupported($xml); 34 35 // Activate small nodes allocation and relax LibXML's hardcoded limits if applicable. Limits 36 // on tags can be set during configuration 37 $flags = (LIBXML_VERSION >= 20700) ? LIBXML_COMPACT | LIBXML_PARSEHUGE : 0; 38 39 $useErrors = libxml_use_internal_errors(true); 40 $dom = new DOMDocument; 41 $success = $dom->loadXML($xml, $flags); 42 libxml_use_internal_errors($useErrors); 43 44 if (!$success) 45 { 46 throw new InvalidArgumentException('Cannot load XML: ' . libxml_get_last_error()->message); 47 } 48 49 return $dom; 50 } 51 52 /** 53 * Render an intermediate representation 54 * 55 * @param string $xml Intermediate representation 56 * @return string Rendered result 57 */ 58 public function render($xml) 59 { 60 if (substr($xml, 0, 3) === '<t>' && substr($xml, -4) === '</t>') 61 { 62 return $this->renderPlainText($xml); 63 } 64 else 65 { 66 return $this->renderRichText(preg_replace('(<[eis]>[^<]*</[eis]>)', '', $xml)); 67 } 68 } 69 70 /** 71 * Render an intermediate representation of plain text 72 * 73 * @param string $xml Intermediate representation 74 * @return string Rendered result 75 */ 76 protected function renderPlainText($xml) 77 { 78 // Remove the <t> and </t> tags 79 $html = substr($xml, 3, -4); 80 81 // Replace all <br/> with <br> 82 $html = str_replace('<br/>', '<br>', $html); 83 84 // Decode encoded characters from the Supplementary Multilingual Plane 85 $html = $this->decodeSMP($html); 86 87 return $html; 88 } 89 90 /** 91 * Render an intermediate representation of rich text 92 * 93 * @param string $xml Intermediate representation 94 * @return string Rendered result 95 */ 96 abstract protected function renderRichText($xml); 97 98 /** 99 * Get the value of a parameter 100 * 101 * @param string $paramName 102 * @return string 103 */ 104 public function getParameter($paramName) 105 { 106 return $this->params[$paramName] ?? ''; 107 } 108 109 /** 110 * Get the values of all parameters 111 * 112 * @return array Associative array of parameter names and values 113 */ 114 public function getParameters() 115 { 116 return $this->params; 117 } 118 119 /** 120 * Set the value of a parameter from the stylesheet 121 * 122 * @param string $paramName Parameter name 123 * @param mixed $paramValue Parameter's value 124 * @return void 125 */ 126 public function setParameter($paramName, $paramValue) 127 { 128 $this->params[$paramName] = (string) $paramValue; 129 } 130 131 /** 132 * Set the values of several parameters from the stylesheet 133 * 134 * @param array $params Associative array of [parameter name => parameter value] 135 * @return void 136 */ 137 public function setParameters(array $params) 138 { 139 foreach ($params as $paramName => $paramValue) 140 { 141 $this->setParameter($paramName, $paramValue); 142 } 143 } 144 145 /** 146 * Test for the presence of unsupported XML and throw an exception if found 147 * 148 * @param string $xml XML 149 * @return void 150 */ 151 protected function checkUnsupported($xml) 152 { 153 if (preg_match('((?<=<)[!?])', $xml, $m)) 154 { 155 $errors = [ 156 '!' => 'DTDs, CDATA nodes and comments are not allowed', 157 '?' => 'Processing instructions are not allowed' 158 ]; 159 160 throw new InvalidArgumentException($errors[$m[0]]); 161 } 162 } 163 164 /** 165 * Decode encoded characters from the Supplementary Multilingual Plane 166 * 167 * @param string $str Encoded string 168 * @return string Decoded string 169 */ 170 protected function decodeSMP($str) 171 { 172 if (strpos($str, '&#') === false) 173 { 174 return $str; 175 } 176 177 return preg_replace_callback('(&#(?:x[0-9A-Fa-f]+|[0-9]+);)', __CLASS__ . '::decodeEntity', $str); 178 } 179 180 /** 181 * Decode a matched SGML entity 182 * 183 * @param string[] $m Captures from PCRE 184 * @return string Decoded entity 185 */ 186 protected static function decodeEntity(array $m) 187 { 188 return htmlspecialchars(html_entity_decode($m[0], ENT_QUOTES, 'UTF-8'), ENT_COMPAT); 189 } 190 191 /** 192 * Restore the original locale 193 */ 194 protected function restoreLocale(): void 195 { 196 if ($this->savedLocale !== 'C') 197 { 198 setlocale(LC_NUMERIC, $this->savedLocale); 199 } 200 } 201 202 /** 203 * Temporarily set the locale to C 204 */ 205 protected function setLocale(): void 206 { 207 $this->savedLocale = setlocale(LC_NUMERIC, '0'); 208 if ($this->savedLocale !== 'C') 209 { 210 setlocale(LC_NUMERIC, 'C'); 211 } 212 } 213 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |