Go away evil carriage returns\!

git-svn-id: file:///svn/phpbb/trunk@8477 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Nils Adermann 2008-03-29 00:08:34 +00:00
parent 22c304092e
commit 195cd7e148

View file

@ -1,298 +1,298 @@
<?php <?php
/** /**
* *
* @package phpBB3 * @package phpBB3
* @version * @version
* @copyright (c) 2005 phpBB Group * @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License * @license http://opensource.org/licenses/gpl-license.php GNU Public License
* *
*/ */
/** /**
* @ignore * @ignore
*/ */
if (!defined('IN_PHPBB')) if (!defined('IN_PHPBB'))
{ {
exit; exit;
} }
/** /**
* Formatted text class to handle any text that can contain BBCodes, smielys, magic URLs or under word censor. * Formatted text class to handle any text that can contain BBCodes, smielys, magic URLs or under word censor.
*/ */
class formatted_text class formatted_text
{ {
/** /**
* Unformated text. * Unformated text.
* *
* @var string * @var string
*/ */
private $text; private $text;
/** /**
* Internal representation (first_pass data). * Internal representation (first_pass data).
* *
* @var string * @var string
*/ */
private $meta; private $meta;
/** /**
* Formatting options as bit flag. * Formatting options as bit flag.
* *
* @see bbcode_parser * @see bbcode_parser
* *
* @var int * @var int
*/ */
private $flags; private $flags;
/** /**
* Compiled $text. For dispaly. * Compiled $text. For dispaly.
* *
* @var string * @var string
*/ */
private $compiled; private $compiled;
/** /**
* Set to true if text, meta or flags have been changed, false otherwise. * Set to true if text, meta or flags have been changed, false otherwise.
* *
* @var bool * @var bool
*/ */
private $changed; private $changed;
/** /**
* DB table to update * DB table to update
* *
* @var string * @var string
*/ */
private $update_table = ''; private $update_table = '';
/** /**
* Column in $update_table to update. * Column in $update_table to update.
* *
* @var string * @var string
*/ */
private $update_column = ''; private $update_column = '';
/** /**
* Where clause for auto update. * Where clause for auto update.
* *
* @var string * @var string
*/ */
private $update_where = ''; private $update_where = '';
/** /**
* Creates a new instance. * Creates a new instance.
* *
* @param string $text * @param string $text
* @param string $meta * @param string $meta
* @param int $flags * @param int $flags
*/ */
public function __construct($text, $meta = '', $flags = 0) public function __construct($text, $meta = '', $flags = 0)
{ {
$this->text = $text; $this->text = $text;
$this->meta = $meta; $this->meta = $meta;
$this->flags = $flags; $this->flags = $flags;
$this->compiled = ''; $this->compiled = '';
if ($meta == '') if ($meta == '')
{ {
$this->changed = true; $this->changed = true;
} }
else else
{ {
$this->changed = false; $this->changed = false;
} }
} }
public function __destruct() public function __destruct()
{ {
if ($this->changed && $this->update_table) if ($this->changed && $this->update_table)
{ {
$this->to_db($this->update_table, $this->update_column, $this->update_where); $this->to_db($this->update_table, $this->update_column, $this->update_where);
} }
} }
/** /**
* Convieniently initialize a formatted_text object from * Convieniently initialize a formatted_text object from
* a database result set. The array must contain the following indexes: * a database result set. The array must contain the following indexes:
* $column, {$column}_meta and {$column}_flags * $column, {$column}_meta and {$column}_flags
* *
* *
* @param array $data * @param array $data
* @param string $column * @param string $column
* @return formatted_text * @return formatted_text
*/ */
public static function from_db_data(array $data, $column) public static function from_db_data(array $data, $column)
{ {
return new formatted_text($data[$column], $data[$column . '_meta'], (int) $data[$column . '_flags']); return new formatted_text($data[$column], $data[$column . '_meta'], (int) $data[$column . '_flags']);
} }
/** /**
* Returns the $text formatted, ready to be displayed on a webpage. * Returns the $text formatted, ready to be displayed on a webpage.
* *
* @return string * @return string
*/ */
public function to_display() public function to_display()
{ {
$this->set_compiled(); $this->set_compiled();
return $this->compiled; return $this->compiled;
} }
/** /**
* Updates $table, sets $column, {$column}_meta and {$column}_flags. * Updates $table, sets $column, {$column}_meta and {$column}_flags.
* All 3 columns must exist. * All 3 columns must exist.
* *
* @param string $table * @param string $table
* @param string $column * @param string $column
* @param string $where * @param string $where
* @return bool * @return bool
*/ */
public function to_db($table, $column, $where = '1') public function to_db($table, $column, $where = '1')
{ {
global $db; global $db;
$this->changed = false; $this->changed = false;
$sql = 'UPDATE ' . $table . ' SET ' . $db->sql_build_query('UPDATE', $this->to_db_data($column)) $sql = 'UPDATE ' . $table . ' SET ' . $db->sql_build_query('UPDATE', $this->to_db_data($column))
. ' WHERE ' . $where; . ' WHERE ' . $where;
return (bool) $db->sql_query($sql); return (bool) $db->sql_query($sql);
} }
/** /**
* Returns an array containing $column, {$column}_meta and {$column}_flags * Returns an array containing $column, {$column}_meta and {$column}_flags
* indexes to be used with query generating functions. * indexes to be used with query generating functions.
* *
* @param string $column * @param string $column
* @return array * @return array
*/ */
public function to_db_data($column) public function to_db_data($column)
{ {
$this->set_meta(); $this->set_meta();
return array($column => $this->text, $column . '_meta' => $this->meta, $column . '_flags' => $this->flags); return array($column => $this->text, $column . '_meta' => $this->meta, $column . '_flags' => $this->flags);
} }
/** /**
* Enable automatic database update on * Enable automatic database update on
* *
* @param string $table * @param string $table
* @param string $column * @param string $column
* @param string $where * @param string $where
*/ */
public function set_auto_update($table, $column, $where = '1') public function set_auto_update($table, $column, $where = '1')
{ {
$this->update_table = $table; $this->update_table = $table;
$this->update_column = $column; $this->update_column = $column;
$this->update_where = $where; $this->update_where = $where;
} }
/** /**
* Sets $meta if not set. * Sets $meta if not set.
*/ */
private function set_meta() private function set_meta()
{ {
if (strlen($this->meta)) if (strlen($this->meta))
{ {
return; return;
} }
$parser = new phpbb_bbcode_parser; $parser = new phpbb_bbcode_parser;
$this->meta = $parser->first_pass($this->text); $this->meta = $parser->first_pass($this->text);
} }
/** /**
* Sets $compiled if not set. * Sets $compiled if not set.
*/ */
private function set_compiled() private function set_compiled()
{ {
$this->set_meta(); $this->set_meta();
if (strlen($this->compiled)) if (strlen($this->compiled))
{ {
return; return;
} }
$parser = new phpbb_bbcode_parser; $parser = new phpbb_bbcode_parser;
$parser->set_flags($this->flags); $parser->set_flags($this->flags);
$this->compiled = $parser->second_pass($this->meta); $this->compiled = $parser->second_pass($this->meta);
} }
/** /**
* Sets $text. * Sets $text.
* *
* @param string $text * @param string $text
*/ */
public function set_text($text) public function set_text($text)
{ {
if ($this->text != $text) if ($this->text != $text)
{ {
$this->text = (string) $text; $this->text = (string) $text;
$this->meta = ''; $this->meta = '';
$this->compiled = ''; $this->compiled = '';
} }
} }
/** /**
* Sets $flags. * Sets $flags.
* *
* @param int $flags * @param int $flags
*/ */
public function set_flags($flags) public function set_flags($flags)
{ {
$flags = (int) $flags; $flags = (int) $flags;
if ($this->flags != $flags) if ($this->flags != $flags)
{ {
$this->flags = $flags; $this->flags = $flags;
$this->compiled = ''; $this->compiled = '';
} }
} }
/** /**
* Returns the current text. * Returns the current text.
* *
* @return string * @return string
*/ */
public function get_text() public function get_text()
{ {
return $this->text; return $this->text;
} }
/** /**
* Returns the current(!!) metadata. * Returns the current(!!) metadata.
* *
* @return string * @return string
*/ */
public function get_meta() public function get_meta()
{ {
return $this->meta; return $this->meta;
} }
/** /**
* Returns the current flags. * Returns the current flags.
* *
* @return int * @return int
*/ */
public function get_flags() public function get_flags()
{ {
return $this->flags; return $this->flags;
} }
/** /**
* Returns true if $this is equal to $other. * Returns true if $this is equal to $other.
* Objects are only equal if $text and $flags are equal. * Objects are only equal if $text and $flags are equal.
* *
* @param formatted_text $other * @param formatted_text $other
* @return bool * @return bool
*/ */
public function eq(formatted_text $other) public function eq(formatted_text $other)
{ {
return $this->flags == $other->get_flags() && $this->text == $other->get_text(); return $this->flags == $other->get_flags() && $this->text == $other->get_text();
} }
/** /**
* Cast to string. Object is represented by the formatted version of $text with respect to $flags. * Cast to string. Object is represented by the formatted version of $text with respect to $flags.
* *
* @return string * @return string
*/ */
public function __toString() public function __toString()
{ {
return $this->to_display(); return $this->to_display();
} }
} }