[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
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\textreparser; 15 16 abstract class base implements reparser_interface 17 { 18 /** 19 * @var string The reparser name 20 */ 21 protected $name; 22 23 /** 24 * @var bool Whether to save changes to the database 25 */ 26 protected $save_changes = true; 27 28 /** 29 * {@inheritdoc} 30 */ 31 abstract public function get_max_id(); 32 33 /** 34 * Return all records in given range 35 * 36 * @param integer $min_id Lower bound 37 * @param integer $max_id Upper bound 38 * @return array Array of records 39 */ 40 abstract protected function get_records_by_range($min_id, $max_id); 41 42 /** 43 * {@inheritdoc} 44 */ 45 abstract protected function save_record(array $record); 46 47 /** 48 * Add fields to given record, if applicable 49 * 50 * The enable_* fields are not always saved to the database. Sometimes we need to guess their 51 * original value based on the text content or possibly other fields 52 * 53 * @param array $record Original record 54 * @return array Complete record 55 */ 56 protected function add_missing_fields(array $record) 57 { 58 if (!isset($record['enable_bbcode'], $record['enable_smilies'], $record['enable_magic_url'])) 59 { 60 if (isset($record['options'])) 61 { 62 $record += array( 63 'enable_bbcode' => (bool) ($record['options'] & OPTION_FLAG_BBCODE), 64 'enable_smilies' => (bool) ($record['options'] & OPTION_FLAG_SMILIES), 65 'enable_magic_url' => (bool) ($record['options'] & OPTION_FLAG_LINKS), 66 ); 67 } 68 else 69 { 70 $record += array( 71 'enable_bbcode' => $this->guess_bbcodes($record), 72 'enable_smilies' => $this->guess_smilies($record), 73 'enable_magic_url' => $this->guess_magic_url($record), 74 ); 75 } 76 } 77 78 // Those BBCodes are disabled based on context and user permissions and that value is never 79 // stored in the database. Here we test whether they were used in the original text. 80 $bbcodes = array('flash', 'img', 'quote', 'url'); 81 foreach ($bbcodes as $bbcode) 82 { 83 $field_name = 'enable_' . $bbcode . '_bbcode'; 84 $record[$field_name] = $this->guess_bbcode($record, $bbcode); 85 } 86 87 // Magic URLs are tied to the URL BBCode, that's why if magic URLs are enabled we make sure 88 // that the URL BBCode is also enabled 89 if ($record['enable_magic_url']) 90 { 91 $record['enable_url_bbcode'] = true; 92 } 93 94 return $record; 95 } 96 97 /** 98 * Returns the name of the reparser 99 * 100 * @return string Name of reparser 101 */ 102 public function get_name() 103 { 104 return $this->name; 105 } 106 107 /** 108 * Sets the name of the reparser 109 * 110 * @param string $name The reparser name 111 */ 112 public function set_name($name) 113 { 114 $this->name = $name; 115 } 116 117 /** 118 * Disable saving changes to the database 119 */ 120 public function disable_save() 121 { 122 $this->save_changes = false; 123 } 124 125 /** 126 * Enable saving changes to the database 127 */ 128 public function enable_save() 129 { 130 $this->save_changes = true; 131 } 132 133 /** 134 * Guess whether given BBCode is in use in given record 135 * 136 * @param array $record 137 * @param string $bbcode 138 * @return bool 139 */ 140 protected function guess_bbcode(array $record, $bbcode) 141 { 142 if (!empty($record['bbcode_uid'])) 143 { 144 // Look for the closing tag, e.g. [/url] 145 $match = '[/' . $bbcode . ':' . $record['bbcode_uid']; 146 if (strpos($record['text'], $match) !== false) 147 { 148 return true; 149 } 150 } 151 152 if (substr($record['text'], 0, 2) === '<r') 153 { 154 // Look for the closing tag inside of a e element, in an element of the same name, e.g. 155 // <e>[/url]</e></URL> 156 $match = '<e>[/' . $bbcode . ']</e></' . $bbcode . '>'; 157 if (stripos($record['text'], $match) !== false) 158 { 159 return true; 160 } 161 } 162 163 return false; 164 } 165 166 /** 167 * Guess whether any BBCode is in use in given record 168 * 169 * @param array $record 170 * @return bool 171 */ 172 protected function guess_bbcodes(array $record) 173 { 174 if (!empty($record['bbcode_uid'])) 175 { 176 // Test whether the bbcode_uid is in use 177 $match = ':' . $record['bbcode_uid']; 178 if (strpos($record['text'], $match) !== false) 179 { 180 return true; 181 } 182 } 183 184 if (substr($record['text'], 0, 2) === '<r') 185 { 186 // Look for a closing tag inside of an e element 187 return (bool) preg_match('(<e>\\[/\\w+\\]</e>)', $match); 188 } 189 190 return false; 191 } 192 193 /** 194 * Guess whether magic URLs are in use in given record 195 * 196 * @param array $record 197 * @return bool 198 */ 199 protected function guess_magic_url(array $record) 200 { 201 // Look for magic URL markers or for a URL tag that's not immediately followed by <s> 202 return preg_match('#<!-- ([lmwe]) -->.*?<!-- \1 -->#', $record['text']) || preg_match('(<URL [^>]++>(?!<s>))', $record['text']); 203 } 204 205 /** 206 * Guess whether smilies are in use in given record 207 * 208 * @param array $record 209 * @return bool 210 */ 211 protected function guess_smilies(array $record) 212 { 213 return (strpos($record['text'], '<!-- s') !== false || strpos($record['text'], '<E>') !== false); 214 } 215 216 /** 217 * {@inheritdoc} 218 */ 219 public function reparse_range($min_id, $max_id, bool $force_bbcode_reparsing = false) 220 { 221 foreach ($this->get_records_by_range($min_id, $max_id) as $record) 222 { 223 $this->reparse_record($record, $force_bbcode_reparsing); 224 } 225 } 226 227 /** 228 * Reparse given record 229 * 230 * @param array $record Associative array containing the record's data 231 * @param bool $force_bbcode_reparsing Flag indicating if BBCode should be reparsed unconditionally 232 */ 233 protected function reparse_record(array $record, bool $force_bbcode_reparsing = false) 234 { 235 // Guess magic URL state based on actual record content before adding fields 236 $record['enable_magic_url'] = $this->guess_magic_url($record); 237 $record = $this->add_missing_fields($record); 238 239 $flags = ($record['enable_bbcode'] || $force_bbcode_reparsing) ? OPTION_FLAG_BBCODE : 0; 240 $flags |= ($record['enable_smilies'] || $force_bbcode_reparsing) ? OPTION_FLAG_SMILIES : 0; 241 $flags |= ($record['enable_magic_url'] || $force_bbcode_reparsing) ? OPTION_FLAG_LINKS : 0; 242 $unparsed = array_merge( 243 $record, 244 generate_text_for_edit($record['text'], $record['bbcode_uid'], $flags) 245 ); 246 247 // generate_text_for_edit() and decode_message() actually return the text as HTML. It has to 248 // be decoded to plain text before it can be reparsed 249 $text = html_entity_decode($unparsed['text'], ENT_QUOTES, 'UTF-8'); 250 $bitfield = $flags = null; 251 generate_text_for_storage( 252 $text, 253 $unparsed['bbcode_uid'], 254 $bitfield, 255 $flags, 256 $unparsed['enable_bbcode'] || $force_bbcode_reparsing, 257 $unparsed['enable_magic_url'] || $force_bbcode_reparsing, 258 $unparsed['enable_smilies'] || $force_bbcode_reparsing, 259 $unparsed['enable_img_bbcode'] || $force_bbcode_reparsing, 260 $unparsed['enable_flash_bbcode'], 261 $unparsed['enable_quote_bbcode'] || $force_bbcode_reparsing, 262 $unparsed['enable_url_bbcode'] || $force_bbcode_reparsing, 263 'text_reparser.' . $this->get_name() 264 ); 265 266 // Save the new text if it has changed and it's not a dry run 267 if ($text !== $record['text'] && $this->save_changes) 268 { 269 $record['text'] = $text; 270 $this->save_record($record); 271 } 272 } 273 }
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 |