2003-02-27 23:37:02 +00:00
< ? php
2007-06-09 11:11:20 +00:00
/**
2005-04-09 12:26:45 +00:00
*
* @ package phpBB3
* @ version $Id $
2007-06-09 11:11:20 +00:00
* @ copyright ( c ) 2005 phpBB Group
* @ license http :// opensource . org / licenses / gpl - license . php GNU Public License
2005-04-09 12:26:45 +00:00
*
*/
/**
* @ ignore
*/
2004-09-04 19:32:23 +00:00
if ( ! defined ( 'IN_PHPBB' ))
{
exit ;
}
if ( ! class_exists ( 'bbcode' ))
2003-02-27 23:37:02 +00:00
{
2004-09-04 19:32:23 +00:00
include ( $phpbb_root_path . 'includes/bbcode.' . $phpEx );
}
2004-02-15 14:03:19 +00:00
2005-04-09 12:26:45 +00:00
/**
* BBCODE FIRSTPASS
* BBCODE first pass class ( functions for parsing messages for db storage )
2006-06-13 21:06:29 +00:00
* @ package phpBB3
2005-04-09 12:26:45 +00:00
*/
2004-09-04 19:32:23 +00:00
class bbcode_firstpass extends bbcode
{
var $message = '' ;
var $warn_msg = array ();
2004-11-06 14:22:04 +00:00
var $parsed_items = array ();
2004-02-15 14:03:19 +00:00
2006-06-10 20:16:02 +00:00
/**
* Parse BBCode
*/
2004-09-04 19:32:23 +00:00
function parse_bbcode ()
2003-04-09 22:17:55 +00:00
{
2003-05-22 01:32:27 +00:00
if ( ! $this -> bbcodes )
{
$this -> bbcode_init ();
}
global $user ;
2006-07-24 10:08:36 +00:00
$this -> bbcode_bitfield = '' ;
$bitfield = new bitfield ();
2003-04-23 22:55:53 +00:00
2003-05-22 01:32:27 +00:00
foreach ( $this -> bbcodes as $bbcode_name => $bbcode_data )
2003-04-11 00:19:29 +00:00
{
2004-08-14 18:29:10 +00:00
if ( isset ( $bbcode_data [ 'disabled' ]) && $bbcode_data [ 'disabled' ])
2003-05-22 01:32:27 +00:00
{
foreach ( $bbcode_data [ 'regexp' ] as $regexp => $replacement )
{
if ( preg_match ( $regexp , $this -> message ))
{
2006-12-24 14:34:26 +00:00
$this -> warn_msg [] = sprintf ( $user -> lang [ 'UNAUTHORISED_BBCODE' ] , '[' . $bbcode_name . ']' );
2003-05-22 01:32:27 +00:00
continue ;
}
}
}
else
2003-04-11 00:19:29 +00:00
{
2003-05-22 01:32:27 +00:00
foreach ( $bbcode_data [ 'regexp' ] as $regexp => $replacement )
{
2006-08-17 01:15:01 +00:00
// The pattern gets compiled and cached by the PCRE extension,
// it should not demand recompilation
if ( preg_match ( $regexp , $this -> message ))
2006-08-15 01:08:51 +00:00
{
2006-08-17 01:15:01 +00:00
$this -> message = preg_replace ( $regexp , $replacement , $this -> message );
$bitfield -> set ( $bbcode_data [ 'bbcode_id' ]);
2006-08-15 01:08:51 +00:00
}
2003-05-22 01:32:27 +00:00
}
2003-04-11 00:19:29 +00:00
}
}
2006-07-24 10:08:36 +00:00
2006-08-11 21:52:46 +00:00
$this -> bbcode_bitfield = $bitfield -> get_base64 ();
2003-04-11 00:19:29 +00:00
}
2006-06-22 15:14:03 +00:00
/**
* Prepare some bbcodes for better parsing
*/
function prepare_bbcodes ()
{
2007-05-18 17:04:34 +00:00
// Ok, seems like users instead want the no-parsing of urls, smilies, etc. after and before and within quote tags being tagged as "not a bug".
// Fine by me ;) Will ease our live... but do not come back and cry at us, we won't hear you.
/* Add newline at the end and in front of each quote block to prevent parsing errors ( urls , smilies , etc . )
2006-12-24 14:28:01 +00:00
if ( strpos ( $this -> message , '[quote' ) !== false && strpos ( $this -> message , '[/quote]' ) !== false )
2006-06-22 15:14:03 +00:00
{
2006-11-19 00:41:16 +00:00
$this -> message = str_replace ( " \r \n " , " \n " , $this -> message );
2006-06-22 15:14:03 +00:00
2006-12-24 14:28:01 +00:00
// We strip newlines and spaces after and before quotes in quotes (trimming) and then add exactly one newline
$this -> message = preg_replace ( '#\[quote(=".*?")?\]\s*(.*?)\s*\[/quote\]#siu' , '[quote\1]' . " \n " . '\2' . " \n [/quote] " , $this -> message );
2006-06-22 15:14:03 +00:00
}
2007-05-18 17:04:34 +00:00
*/
2006-06-22 15:14:03 +00:00
// Add other checks which needs to be placed before actually parsing anything (be it bbcodes, smilies, urls...)
}
2006-06-10 20:16:02 +00:00
/**
* Init bbcode data for later parsing
*/
2003-04-11 00:19:29 +00:00
function bbcode_init ()
{
2003-04-23 22:55:53 +00:00
static $rowset ;
2003-10-05 19:59:17 +00:00
// This array holds all bbcode data. BBCodes will be processed in this
// order, so it is important to keep [code] in first position and
// [quote] in second position.
2003-05-22 01:32:27 +00:00
$this -> bbcodes = array (
2006-06-11 18:13:52 +00:00
'code' => array ( 'bbcode_id' => 8 , 'regexp' => array ( '#\[code(?:=([a-z]+))?\](.+\[/code\])#ise' => " \$ this->bbcode_code(' \$ 1', ' \$ 2') " )),
'quote' => array ( 'bbcode_id' => 0 , 'regexp' => array ( '#\[quote(?:="(.*?)")?\](.+)\[/quote\]#ise' => " \$ this->bbcode_quote(' \$ 0') " )),
'attachment' => array ( 'bbcode_id' => 12 , 'regexp' => array ( '#\[attachment=([0-9]+)\](.*?)\[/attachment\]#ise' => " \$ this->bbcode_attachment(' \$ 1', ' \$ 2') " )),
'b' => array ( 'bbcode_id' => 1 , 'regexp' => array ( '#\[b\](.*?)\[/b\]#ise' => " \$ this->bbcode_strong(' \$ 1') " )),
'i' => array ( 'bbcode_id' => 2 , 'regexp' => array ( '#\[i\](.*?)\[/i\]#ise' => " \$ this->bbcode_italic(' \$ 1') " )),
'url' => array ( 'bbcode_id' => 3 , 'regexp' => array ( '#\[url(=(.*))?\](.*)\[/url\]#iUe' => " \$ this->validate_url(' \$ 2', ' \$ 3') " )),
2007-07-15 13:47:01 +00:00
'img' => array ( 'bbcode_id' => 4 , 'regexp' => array ( '#\[img\](.*)\[/img\]#iUe' => " \$ this->bbcode_img(' \$ 1') " )),
2007-03-08 01:00:51 +00:00
'size' => array ( 'bbcode_id' => 5 , 'regexp' => array ( '#\[size=([\-\+]?\d+)\](.*?)\[/size\]#ise' => " \$ this->bbcode_size(' \$ 1', ' \$ 2') " )),
2007-01-09 14:10:44 +00:00
'color' => array ( 'bbcode_id' => 6 , 'regexp' => array ( '!\[color=(#[0-9a-f]{6}|[a-z\-]+)\](.*?)\[/color\]!ise' => " \$ this->bbcode_color(' \$ 1', ' \$ 2') " )),
2006-06-11 18:13:52 +00:00
'u' => array ( 'bbcode_id' => 7 , 'regexp' => array ( '#\[u\](.*?)\[/u\]#ise' => " \$ this->bbcode_underline(' \$ 1') " )),
2006-12-17 17:59:08 +00:00
'list' => array ( 'bbcode_id' => 9 , 'regexp' => array ( '#\[list(?:=(?:[a-z0-9]|disc|circle|square))?].*\[/list]#ise' => " \$ this->bbcode_parse_list(' \$ 0') " )),
2006-06-11 18:13:52 +00:00
'email' => array ( 'bbcode_id' => 10 , 'regexp' => array ( '#\[email=?(.*?)?\](.*?)\[/email\]#ise' => " \$ this->validate_email(' \$ 1', ' \$ 2') " )),
'flash' => array ( 'bbcode_id' => 11 , 'regexp' => array ( '#\[flash=([0-9]+),([0-9]+)\](.*?)\[/flash\]#ie' => " \$ this->bbcode_flash(' \$ 1', ' \$ 2', ' \$ 3') " ))
2003-04-11 00:19:29 +00:00
);
2006-03-06 14:03:56 +00:00
// Zero the parsed items array
$this -> parsed_items = array ();
foreach ( $this -> bbcodes as $tag => $bbcode_data )
{
$this -> parsed_items [ $tag ] = 0 ;
}
2004-11-06 14:22:04 +00:00
2004-11-10 14:15:16 +00:00
if ( ! is_array ( $rowset ))
2003-04-23 22:55:53 +00:00
{
global $db ;
$rowset = array ();
2006-06-10 20:16:02 +00:00
$sql = ' SELECT *
2003-08-28 20:50:20 +00:00
FROM ' . BBCODES_TABLE ;
$result = $db -> sql_query ( $sql );
2006-06-11 18:13:52 +00:00
2003-04-23 22:55:53 +00:00
while ( $row = $db -> sql_fetchrow ( $result ))
{
$rowset [] = $row ;
}
2006-03-06 14:03:56 +00:00
$db -> sql_freeresult ( $result );
2003-04-23 22:55:53 +00:00
}
2006-03-06 14:03:56 +00:00
2003-04-23 22:55:53 +00:00
foreach ( $rowset as $row )
2003-04-11 00:19:29 +00:00
{
2003-08-28 20:50:20 +00:00
$this -> bbcodes [ $row [ 'bbcode_tag' ]] = array (
2006-06-10 20:16:02 +00:00
'bbcode_id' => ( int ) $row [ 'bbcode_id' ],
2005-05-10 17:03:56 +00:00
'regexp' => array ( $row [ 'first_pass_match' ] => str_replace ( '$uid' , $this -> bbcode_uid , $row [ 'first_pass_replace' ]))
2003-05-22 01:32:27 +00:00
);
2003-04-11 00:19:29 +00:00
}
}
2006-06-10 20:16:02 +00:00
/**
* Making some pre - checks for bbcodes as well as increasing the number of parsed items
*/
2005-05-15 20:08:05 +00:00
function check_bbcode ( $bbcode , & $in )
2005-05-10 17:03:56 +00:00
{
2006-05-12 20:52:58 +00:00
// when using the /e modifier, preg_replace slashes double-quotes but does not
// seem to slash anything else
2006-06-10 20:16:02 +00:00
$in = str_replace ( " \r \n " , " \n " , str_replace ( '\"' , '"' , $in ));
2005-05-10 17:03:56 +00:00
2006-06-11 18:13:52 +00:00
// Trimming here to make sure no empty bbcodes are parsed accidently
2006-07-09 16:23:57 +00:00
if ( trim ( $in ) == '' )
2005-05-10 17:03:56 +00:00
{
2005-05-15 20:08:05 +00:00
return false ;
2005-05-10 17:03:56 +00:00
}
2006-06-10 20:16:02 +00:00
2005-05-15 20:08:05 +00:00
$this -> parsed_items [ $bbcode ] ++ ;
2005-05-10 17:03:56 +00:00
2005-05-15 20:08:05 +00:00
return true ;
}
2006-06-10 20:16:02 +00:00
/**
* Transform some characters in valid bbcodes
*/
function bbcode_specialchars ( $text )
{
$str_from = array ( '<' , '>' , '[' , ']' , '.' , ':' );
$str_to = array ( '<' , '>' , '[' , ']' , '.' , ':' );
return str_replace ( $str_from , $str_to , $text );
}
/**
2006-06-11 18:13:52 +00:00
* Parse size tag
2006-06-10 20:16:02 +00:00
*/
2005-05-15 20:08:05 +00:00
function bbcode_size ( $stx , $in )
{
2006-02-25 05:46:52 +00:00
global $user , $config ;
2005-05-15 20:08:05 +00:00
if ( ! $this -> check_bbcode ( 'size' , $in ))
{
2008-02-23 15:03:50 +00:00
return $in ;
2005-05-15 20:08:05 +00:00
}
2006-06-10 20:16:02 +00:00
2006-02-25 05:46:52 +00:00
if ( $config [ 'max_' . $this -> mode . '_font_size' ] && $config [ 'max_' . $this -> mode . '_font_size' ] < $stx )
{
$this -> warn_msg [] = sprintf ( $user -> lang [ 'MAX_FONT_SIZE_EXCEEDED' ], $config [ 'max_' . $this -> mode . '_font_size' ]);
2007-04-12 16:20:39 +00:00
return '[size=' . $stx . ']' . $in . '[/size]' ;
2006-02-25 05:46:52 +00:00
}
2007-11-29 14:09:32 +00:00
// Do not allow size=0
if ( $stx <= 0 )
{
return '[size=' . $stx . ']' . $in . '[/size]' ;
}
2005-05-30 06:42:49 +00:00
return '[size=' . $stx . ':' . $this -> bbcode_uid . ']' . $in . '[/size:' . $this -> bbcode_uid . ']' ;
2005-05-10 17:03:56 +00:00
}
2006-06-10 20:16:02 +00:00
/**
2006-06-11 18:13:52 +00:00
* Parse color tag
2006-06-10 20:16:02 +00:00
*/
2005-05-10 17:03:56 +00:00
function bbcode_color ( $stx , $in )
{
2005-05-15 20:08:05 +00:00
if ( ! $this -> check_bbcode ( 'color' , $in ))
2005-05-10 17:03:56 +00:00
{
2008-02-23 15:03:50 +00:00
return $in ;
2005-05-10 17:03:56 +00:00
}
return '[color=' . $stx . ':' . $this -> bbcode_uid . ']' . $in . '[/color:' . $this -> bbcode_uid . ']' ;
}
2006-06-10 20:16:02 +00:00
/**
2006-06-11 18:13:52 +00:00
* Parse u tag
2006-06-10 20:16:02 +00:00
*/
2005-05-10 17:03:56 +00:00
function bbcode_underline ( $in )
{
2005-05-15 20:08:05 +00:00
if ( ! $this -> check_bbcode ( 'u' , $in ))
2005-05-10 17:03:56 +00:00
{
2008-02-23 15:03:50 +00:00
return $in ;
2005-05-10 17:03:56 +00:00
}
return '[u:' . $this -> bbcode_uid . ']' . $in . '[/u:' . $this -> bbcode_uid . ']' ;
}
2006-06-10 20:16:02 +00:00
/**
2006-06-11 18:13:52 +00:00
* Parse b tag
2006-06-10 20:16:02 +00:00
*/
2005-05-10 17:03:56 +00:00
function bbcode_strong ( $in )
{
2005-05-15 20:08:05 +00:00
if ( ! $this -> check_bbcode ( 'b' , $in ))
2005-05-10 17:03:56 +00:00
{
2008-02-23 15:03:50 +00:00
return $in ;
2005-05-10 17:03:56 +00:00
}
return '[b:' . $this -> bbcode_uid . ']' . $in . '[/b:' . $this -> bbcode_uid . ']' ;
}
2006-06-10 20:16:02 +00:00
/**
2006-06-11 18:13:52 +00:00
* Parse i tag
2006-06-10 20:16:02 +00:00
*/
2005-05-10 17:03:56 +00:00
function bbcode_italic ( $in )
{
2005-05-15 20:08:05 +00:00
if ( ! $this -> check_bbcode ( 'i' , $in ))
2005-05-10 17:03:56 +00:00
{
2008-02-23 15:03:50 +00:00
return $in ;
2005-05-10 17:03:56 +00:00
}
return '[i:' . $this -> bbcode_uid . ']' . $in . '[/i:' . $this -> bbcode_uid . ']' ;
}
2006-06-10 20:16:02 +00:00
/**
2006-06-11 18:13:52 +00:00
* Parse img tag
2006-06-10 20:16:02 +00:00
*/
2004-11-06 14:22:04 +00:00
function bbcode_img ( $in )
{
2006-12-10 01:19:54 +00:00
global $user , $config ;
2006-02-25 05:46:52 +00:00
2005-05-15 20:08:05 +00:00
if ( ! $this -> check_bbcode ( 'img' , $in ))
2005-05-10 17:03:56 +00:00
{
2008-02-23 15:03:50 +00:00
return $in ;
2005-05-10 17:03:56 +00:00
}
2006-06-11 18:13:52 +00:00
$in = trim ( $in );
2007-04-12 16:20:39 +00:00
$error = false ;
2006-06-11 18:13:52 +00:00
2007-07-15 13:47:01 +00:00
$in = str_replace ( ' ' , '%20' , $in );
// Checking urls
if ( ! preg_match ( '#^' . get_preg_expression ( 'url' ) . '$#i' , $in ) && ! preg_match ( '#^' . get_preg_expression ( 'www_url' ) . '$#i' , $in ))
{
return '[img]' . $in . '[/img]' ;
}
// Try to cope with a common user error... not specifying a protocol but only a subdomain
if ( ! preg_match ( '#^[a-z0-9]+://#i' , $in ))
{
$in = 'http://' . $in ;
}
2006-02-25 05:46:52 +00:00
if ( $config [ 'max_' . $this -> mode . '_img_height' ] || $config [ 'max_' . $this -> mode . '_img_width' ])
{
2007-05-19 16:40:56 +00:00
$stats = @ getimagesize ( $in );
2006-06-11 18:13:52 +00:00
2006-06-16 16:54:51 +00:00
if ( $stats === false )
2006-02-25 05:46:52 +00:00
{
2007-04-12 16:20:39 +00:00
$error = true ;
2006-06-16 16:54:51 +00:00
$this -> warn_msg [] = $user -> lang [ 'UNABLE_GET_IMAGE_SIZE' ];
2006-02-25 05:46:52 +00:00
}
2006-06-16 16:54:51 +00:00
else
2006-02-25 05:46:52 +00:00
{
2006-06-16 16:54:51 +00:00
if ( $config [ 'max_' . $this -> mode . '_img_height' ] && $config [ 'max_' . $this -> mode . '_img_height' ] < $stats [ 1 ])
{
2007-04-12 16:20:39 +00:00
$error = true ;
2006-06-16 16:54:51 +00:00
$this -> warn_msg [] = sprintf ( $user -> lang [ 'MAX_IMG_HEIGHT_EXCEEDED' ], $config [ 'max_' . $this -> mode . '_img_height' ]);
}
if ( $config [ 'max_' . $this -> mode . '_img_width' ] && $config [ 'max_' . $this -> mode . '_img_width' ] < $stats [ 0 ])
{
2007-04-12 16:20:39 +00:00
$error = true ;
2006-06-16 16:54:51 +00:00
$this -> warn_msg [] = sprintf ( $user -> lang [ 'MAX_IMG_WIDTH_EXCEEDED' ], $config [ 'max_' . $this -> mode . '_img_width' ]);
}
2006-02-25 05:46:52 +00:00
}
}
2007-04-12 16:20:39 +00:00
if ( $error || $this -> path_in_domain ( $in ))
2006-06-11 18:13:52 +00:00
{
2006-06-13 15:06:32 +00:00
return '[img]' . $in . '[/img]' ;
2006-06-11 18:13:52 +00:00
}
return '[img:' . $this -> bbcode_uid . ']' . $this -> bbcode_specialchars ( $in ) . '[/img:' . $this -> bbcode_uid . ']' ;
2004-11-06 14:22:04 +00:00
}
2006-06-10 20:16:02 +00:00
/**
2006-06-11 18:13:52 +00:00
* Parse flash tag
2006-06-10 20:16:02 +00:00
*/
2004-11-06 14:22:04 +00:00
function bbcode_flash ( $width , $height , $in )
{
2006-12-10 01:19:54 +00:00
global $user , $config ;
2006-06-11 18:13:52 +00:00
2005-05-15 20:08:05 +00:00
if ( ! $this -> check_bbcode ( 'flash' , $in ))
2005-05-10 17:03:56 +00:00
{
2008-02-23 15:03:50 +00:00
return $in ;
2005-05-10 17:03:56 +00:00
}
2006-06-06 20:53:46 +00:00
2006-06-11 18:13:52 +00:00
$in = trim ( $in );
2007-04-12 16:20:39 +00:00
$error = false ;
2006-06-10 20:16:02 +00:00
2008-06-07 13:40:37 +00:00
// Do not allow 0-sizes generally being entered
if ( $width <= 0 || $height <= 0 )
{
return '[flash=' . $width . ',' . $height . ']' . $in . '[/flash]' ;
}
2006-06-06 20:53:46 +00:00
// Apply the same size checks on flash files as on images
if ( $config [ 'max_' . $this -> mode . '_img_height' ] || $config [ 'max_' . $this -> mode . '_img_width' ])
{
if ( $config [ 'max_' . $this -> mode . '_img_height' ] && $config [ 'max_' . $this -> mode . '_img_height' ] < $height )
{
2007-04-12 16:20:39 +00:00
$error = true ;
2006-06-06 20:53:46 +00:00
$this -> warn_msg [] = sprintf ( $user -> lang [ 'MAX_FLASH_HEIGHT_EXCEEDED' ], $config [ 'max_' . $this -> mode . '_img_height' ]);
}
if ( $config [ 'max_' . $this -> mode . '_img_width' ] && $config [ 'max_' . $this -> mode . '_img_width' ] < $width )
{
2007-04-12 16:20:39 +00:00
$error = true ;
2006-06-06 20:53:46 +00:00
$this -> warn_msg [] = sprintf ( $user -> lang [ 'MAX_FLASH_WIDTH_EXCEEDED' ], $config [ 'max_' . $this -> mode . '_img_width' ]);
}
}
2007-04-12 16:20:39 +00:00
if ( $error || $this -> path_in_domain ( $in ))
2006-06-11 18:13:52 +00:00
{
2006-06-13 15:06:32 +00:00
return '[flash=' . $width . ',' . $height . ']' . $in . '[/flash]' ;
2006-06-11 18:13:52 +00:00
}
return '[flash=' . $width . ',' . $height . ':' . $this -> bbcode_uid . ']' . $this -> bbcode_specialchars ( $in ) . '[/flash:' . $this -> bbcode_uid . ']' ;
2004-11-06 14:22:04 +00:00
}
2006-06-10 20:16:02 +00:00
/**
* Parse inline attachments [ ia ]
*/
2004-02-08 18:02:17 +00:00
function bbcode_attachment ( $stx , $in )
{
2005-05-15 20:08:05 +00:00
if ( ! $this -> check_bbcode ( 'attachment' , $in ))
2005-05-10 17:03:56 +00:00
{
2008-02-23 15:03:50 +00:00
return $in ;
2005-05-10 17:03:56 +00:00
}
2006-06-10 20:16:02 +00:00
return '[attachment=' . $stx . ':' . $this -> bbcode_uid . ']<!-- ia' . $stx . ' -->' . trim ( $in ) . '<!-- ia' . $stx . ' -->[/attachment:' . $this -> bbcode_uid . ']' ;
2004-02-08 18:02:17 +00:00
}
2006-06-10 20:16:02 +00:00
/**
2007-05-11 22:46:41 +00:00
* Parse code text from code tag
2008-08-23 17:23:40 +00:00
* @ access private
2006-06-10 20:16:02 +00:00
*/
2007-06-11 00:12:42 +00:00
function bbcode_parse_code ( $stx , & $code )
2003-04-11 00:19:29 +00:00
{
2007-05-11 12:25:28 +00:00
switch ( strtolower ( $stx ))
2003-04-11 00:19:29 +00:00
{
2007-05-11 12:25:28 +00:00
case 'php' :
$remove_tags = false ;
2008-06-21 15:09:44 +00:00
$str_from = array ( '<' , '>' , '[' , ']' , '.' , ':' , ':' );
$str_to = array ( '<' , '>' , '[' , ']' , '.' , ':' , ':' );
$code = str_replace ( $str_from , $str_to , $code );
2003-04-11 00:19:29 +00:00
2007-05-11 12:25:28 +00:00
if ( ! preg_match ( '/\<\?.*?\?\>/is' , $code ))
2003-04-11 00:19:29 +00:00
{
2007-05-11 12:25:28 +00:00
$remove_tags = true ;
$code = " <?php $code ?> " ;
2003-04-11 00:19:29 +00:00
}
2007-05-11 12:25:28 +00:00
$conf = array ( 'highlight.bg' , 'highlight.comment' , 'highlight.default' , 'highlight.html' , 'highlight.keyword' , 'highlight.string' );
foreach ( $conf as $ini_var )
2003-04-11 00:19:29 +00:00
{
2007-05-11 12:25:28 +00:00
@ ini_set ( $ini_var , str_replace ( 'highlight.' , 'syntax' , $ini_var ));
2003-04-11 00:19:29 +00:00
}
2003-05-22 01:32:27 +00:00
2007-05-11 12:25:28 +00:00
// Because highlight_string is specialcharing the text (but we already did this before), we have to reverse this in order to get correct results
$code = htmlspecialchars_decode ( $code );
$code = highlight_string ( $code , true );
2003-04-20 20:30:01 +00:00
2007-05-11 12:25:28 +00:00
$str_from = array ( '<span style="color: ' , '<font color="syntax' , '</font>' , '<code>' , '</code>' , '[' , ']' , '.' , ':' );
$str_to = array ( '<span class="' , '<span class="syntax' , '</span>' , '' , '' , '[' , ']' , '.' , ':' );
2003-05-22 01:32:27 +00:00
2007-05-11 12:25:28 +00:00
if ( $remove_tags )
{
$str_from [] = '<span class="syntaxdefault"><?php </span>' ;
$str_to [] = '' ;
$str_from [] = '<span class="syntaxdefault"><?php ' ;
$str_to [] = '<span class="syntaxdefault">' ;
}
2003-04-20 20:30:01 +00:00
2007-05-11 12:25:28 +00:00
$code = str_replace ( $str_from , $str_to , $code );
$code = preg_replace ( '#^(<span class="[a-z_]+">)\n?(.*?)\n?(</span>)$#is' , '$1$2$3' , $code );
2003-08-28 20:50:20 +00:00
2007-05-11 12:25:28 +00:00
if ( $remove_tags )
{
2007-05-17 14:59:33 +00:00
$code = preg_replace ( '#(<span class="[a-z]+">)?\?>(</span>)#' , '$1 $2' , $code );
2007-05-11 12:25:28 +00:00
}
2003-08-28 20:50:20 +00:00
2007-05-11 12:25:28 +00:00
$code = preg_replace ( '#^<span class="[a-z]+"><span class="([a-z]+)">(.*)</span></span>#s' , '<span class="$1">$2</span>' , $code );
2007-08-16 19:24:32 +00:00
$code = preg_replace ( '#(?:\s++| )*+</span>$#u' , '</span>' , $code );
2003-04-20 20:30:01 +00:00
2007-05-11 12:25:28 +00:00
// remove newline at the end
2007-08-16 19:24:32 +00:00
if ( ! empty ( $code ) && substr ( $code , - 1 ) == " \n " )
2007-05-11 12:25:28 +00:00
{
$code = substr ( $code , 0 , - 1 );
}
2003-04-20 20:30:01 +00:00
2007-05-11 22:46:41 +00:00
return " [code= $stx : " . $this -> bbcode_uid . ']' . $code . '[/code:' . $this -> bbcode_uid . ']' ;
2007-05-11 12:25:28 +00:00
break ;
2003-04-09 22:17:55 +00:00
2007-05-11 12:25:28 +00:00
default :
2007-05-11 22:46:41 +00:00
return '[code:' . $this -> bbcode_uid . ']' . $this -> bbcode_specialchars ( $code ) . '[/code:' . $this -> bbcode_uid . ']' ;
2007-05-11 12:25:28 +00:00
break ;
2003-04-11 00:19:29 +00:00
}
2007-05-11 22:46:41 +00:00
}
/**
* Parse code tag
* Expects the argument to start right after the opening [ code ] tag and to end with [ / code ]
*/
function bbcode_code ( $stx , $in )
{
if ( ! $this -> check_bbcode ( 'code' , $in ))
{
2008-02-23 15:03:50 +00:00
return $in ;
2007-05-11 22:46:41 +00:00
}
// We remove the hardcoded elements from the code block here because it is not used in code blocks
// Having it here saves us one preg_replace per message containing [code] blocks
// Additionally, magic url parsing should go after parsing bbcodes, but for safety those are stripped out too...
$htm_match = get_preg_expression ( 'bbcode_htm' );
unset ( $htm_match [ 4 ], $htm_match [ 5 ]);
$htm_replace = array ( '\1' , '\1' , '\2' , '\1' );
$out = $code_block = '' ;
$open = 1 ;
while ( $in )
{
// Determine position and tag length of next code block
preg_match ( '#(.*?)(\[code(?:=([a-z]+))?\])(.+)#is' , $in , $buffer );
$pos = ( isset ( $buffer [ 1 ])) ? strlen ( $buffer [ 1 ]) : false ;
$tag_length = ( isset ( $buffer [ 2 ])) ? strlen ( $buffer [ 2 ]) : false ;
// Determine position of ending code tag
$pos2 = stripos ( $in , '[/code]' );
// Which is the next block, ending code or code block
if ( $pos !== false && $pos < $pos2 )
{
// Open new block
if ( ! $open )
{
$out .= substr ( $in , 0 , $pos );
$in = substr ( $in , $pos );
$stx = ( isset ( $buffer [ 3 ])) ? $buffer [ 3 ] : '' ;
$code_block = '' ;
}
else
{
// Already opened block, just append to the current block
$code_block .= substr ( $in , 0 , $pos ) . (( isset ( $buffer [ 2 ])) ? $buffer [ 2 ] : '' );
$in = substr ( $in , $pos );
}
$in = substr ( $in , $tag_length );
$open ++ ;
}
else
{
// Close the block
if ( $open == 1 )
{
$code_block .= substr ( $in , 0 , $pos2 );
2007-06-03 18:47:05 +00:00
$code_block = preg_replace ( $htm_match , $htm_replace , $code_block );
2007-05-11 22:46:41 +00:00
// Parse this code block
$out .= $this -> bbcode_parse_code ( $stx , $code_block );
$code_block = '' ;
$open -- ;
}
else if ( $open )
{
// Close one open tag... add to the current code block
$code_block .= substr ( $in , 0 , $pos2 + 7 );
$open -- ;
}
else
{
// end code without opening code... will be always outside code block
$out .= substr ( $in , 0 , $pos2 + 7 );
}
$in = substr ( $in , $pos2 + 7 );
}
}
// if now $code_block has contents we need to parse the remaining code while removing the last closing tag to match up.
if ( $code_block )
{
$code_block = substr ( $code_block , 0 , - 7 );
2007-06-03 18:47:05 +00:00
$code_block = preg_replace ( $htm_match , $htm_replace , $code_block );
2007-05-11 22:46:41 +00:00
$out .= $this -> bbcode_parse_code ( $stx , $code_block );
}
2003-04-11 00:19:29 +00:00
return $out ;
}
2006-06-10 20:16:02 +00:00
/**
* Parse list bbcode
* Expects the argument to start with a tag
*/
2005-04-20 20:36:10 +00:00
function bbcode_parse_list ( $in )
2003-04-11 00:19:29 +00:00
{
2005-05-15 20:08:05 +00:00
if ( ! $this -> check_bbcode ( 'list' , $in ))
2005-05-10 17:03:56 +00:00
{
2008-02-23 15:03:50 +00:00
return $in ;
2005-05-10 17:03:56 +00:00
}
2006-06-10 20:16:02 +00:00
2003-04-16 22:31:20 +00:00
// $tok holds characters to stop at. Since the string starts with a '[' we'll get everything up to the first ']' which should be the opening [list] tag
2003-04-11 00:19:29 +00:00
$tok = ']' ;
$out = '[' ;
2003-04-16 22:31:20 +00:00
2006-10-07 16:49:44 +00:00
// First character is [
2005-04-20 20:36:10 +00:00
$in = substr ( $in , 1 );
2006-12-17 17:59:08 +00:00
$list_end_tags = $item_end_tags = array ();
2003-04-11 00:19:29 +00:00
do
{
$pos = strlen ( $in );
2006-10-07 16:49:44 +00:00
for ( $i = 0 , $tok_len = strlen ( $tok ); $i < $tok_len ; ++ $i )
2003-04-11 00:19:29 +00:00
{
2006-12-17 17:59:08 +00:00
$tmp_pos = strpos ( $in , $tok [ $i ]);
2005-04-20 20:36:10 +00:00
2004-01-25 14:30:15 +00:00
if ( $tmp_pos !== false && $tmp_pos < $pos )
2003-04-11 00:19:29 +00:00
{
$pos = $tmp_pos ;
}
}
$buffer = substr ( $in , 0 , $pos );
2006-12-17 17:59:08 +00:00
$tok = $in [ $pos ];
2003-04-11 00:19:29 +00:00
2005-04-20 20:36:10 +00:00
$in = substr ( $in , $pos + 1 );
2006-06-10 20:16:02 +00:00
2003-04-11 00:19:29 +00:00
if ( $tok == ']' )
{
2003-04-16 22:31:20 +00:00
// if $tok is ']' the buffer holds a tag
2006-09-16 06:07:06 +00:00
if ( strtolower ( $buffer ) == '/list' && sizeof ( $list_end_tags ))
2003-04-11 00:19:29 +00:00
{
2007-01-18 05:09:35 +00:00
// valid [/list] tag, check nesting so that we don't hit false positives
if ( sizeof ( $item_end_tags ) && sizeof ( $item_end_tags ) >= sizeof ( $list_end_tags ))
2006-12-17 17:59:08 +00:00
{
// current li tag has not been closed
$out = preg_replace ( '/\n?\[$/' , '[' , $out ) . array_pop ( $item_end_tags ) . '][' ;
}
2003-05-18 23:29:35 +00:00
$out .= array_pop ( $list_end_tags ) . ']' ;
2003-04-11 00:19:29 +00:00
$tok = '[' ;
}
2007-01-18 04:25:11 +00:00
else if ( preg_match ( '#^list(=[0-9a-z])?$#i' , $buffer , $m ))
2003-04-11 00:19:29 +00:00
{
2003-04-16 22:31:20 +00:00
// sub-list, add a closing tag
2007-01-18 04:25:11 +00:00
if ( empty ( $m [ 1 ]) || preg_match ( '/^(?:disc|square|circle)$/i' , $m [ 1 ]))
2003-06-05 19:17:36 +00:00
{
array_push ( $list_end_tags , '/list:u:' . $this -> bbcode_uid );
}
else
{
array_push ( $list_end_tags , '/list:o:' . $this -> bbcode_uid );
}
2007-04-13 03:57:04 +00:00
$out .= 'list' . substr ( $buffer , 4 ) . ':' . $this -> bbcode_uid . ']' ;
2003-04-11 00:19:29 +00:00
$tok = '[' ;
}
else
{
2006-12-17 17:59:08 +00:00
if (( $buffer == '*' || substr ( $buffer , - 2 ) == '[*' ) && sizeof ( $list_end_tags ))
{
// the buffer holds a bullet tag and we have a [list] tag open
if ( sizeof ( $item_end_tags ) >= sizeof ( $list_end_tags ))
{
if ( substr ( $buffer , - 2 ) == '[*' )
{
$out .= substr ( $buffer , 0 , - 2 ) . '[' ;
}
// current li tag has not been closed
if ( preg_match ( '/\n\[$/' , $out , $m ))
{
$out = preg_replace ( '/\n\[$/' , '[' , $out );
$buffer = array_pop ( $item_end_tags ) . " ] \n [*: " . $this -> bbcode_uid ;
}
else
{
$buffer = array_pop ( $item_end_tags ) . '][*:' . $this -> bbcode_uid ;
}
}
else
{
$buffer = '*:' . $this -> bbcode_uid ;
}
$item_end_tags [] = '/*:m:' . $this -> bbcode_uid ;
}
else if ( $buffer == '/*' )
{
2007-01-18 05:09:35 +00:00
array_pop ( $item_end_tags );
2006-12-17 17:59:08 +00:00
$buffer = '/*:' . $this -> bbcode_uid ;
}
2003-04-11 00:19:29 +00:00
$out .= $buffer . $tok ;
$tok = '[]' ;
}
}
else
{
2003-04-16 22:31:20 +00:00
// Not within a tag, just add buffer to the return string
2003-04-11 00:19:29 +00:00
$out .= $buffer . $tok ;
$tok = ( $tok == '[' ) ? ']' : '[]' ;
}
}
while ( $in );
2006-12-17 17:59:08 +00:00
// do we have some tags open? close them now
if ( sizeof ( $item_end_tags ))
{
$out .= '[' . implode ( '][' , $item_end_tags ) . ']' ;
}
2004-09-16 18:33:22 +00:00
if ( sizeof ( $list_end_tags ))
2003-04-11 00:19:29 +00:00
{
2003-05-18 23:29:35 +00:00
$out .= '[' . implode ( '][' , $list_end_tags ) . ']' ;
2003-04-11 00:19:29 +00:00
}
return $out ;
}
2006-06-10 20:16:02 +00:00
/**
* Parse quote bbcode
* Expects the argument to start with a tag
*/
2003-04-23 22:55:53 +00:00
function bbcode_quote ( $in )
{
2003-05-22 01:32:27 +00:00
global $config , $user ;
2007-10-08 14:38:44 +00:00
/**
* If you change this code , make sure the cases described within the following reports are still working :
2007-10-19 13:10:13 +00:00
* #3572 - [quote="[test]test"]test [ test[/quote] - (correct: parsed)
* #14667 - [quote]test[/quote] test ] and [ test [quote]test[/quote] (correct: parsed)
* #14770 - [quote="["]test[/quote] (correct: parsed)
* [ quote = " [i]test[/i] " ] test [ / quote ] ( correct : parsed )
2008-02-23 15:29:38 +00:00
* [ quote = " [quote]test[/quote] " ] test [ / quote ] ( correct : parsed - Username displayed as [ quote ] test [ / quote ])
* #20735 - [quote]test[/[/b]quote] test [/quote][/quote] test - (correct: quoted: "test[/[/b]quote] test" / non-quoted: "[/quote] test" - also failed if layout distorted)
2007-10-08 14:38:44 +00:00
*/
2006-05-12 20:52:58 +00:00
$in = str_replace ( " \r \n " , " \n " , str_replace ( '\"' , '"' , trim ( $in )));
2005-05-10 17:03:56 +00:00
if ( ! $in )
{
return '' ;
}
2006-06-10 20:16:02 +00:00
2007-10-19 13:10:13 +00:00
// To let the parser not catch tokens within quote_username quotes we encode them before we start this...
$in = preg_replace ( '#quote="(.*?)"\]#ie' , " 'quote="' . str_replace(array('[', ']'), array('[', ']'), ' \$ 1') . '"]' " , $in );
2003-04-23 22:55:53 +00:00
$tok = ']' ;
$out = '[' ;
2006-05-12 20:52:58 +00:00
$in = substr ( $in , 1 );
2003-05-22 01:32:27 +00:00
$close_tags = $error_ary = array ();
2003-05-09 20:24:15 +00:00
$buffer = '' ;
2003-04-23 22:55:53 +00:00
do
{
$pos = strlen ( $in );
2006-10-07 12:36:31 +00:00
for ( $i = 0 , $tok_len = strlen ( $tok ); $i < $tok_len ; ++ $i )
2003-04-23 22:55:53 +00:00
{
2006-11-15 15:35:50 +00:00
$tmp_pos = strpos ( $in , $tok [ $i ]);
2004-01-25 14:30:15 +00:00
if ( $tmp_pos !== false && $tmp_pos < $pos )
2003-04-23 22:55:53 +00:00
{
$pos = $tmp_pos ;
}
}
2003-05-09 20:24:15 +00:00
$buffer .= substr ( $in , 0 , $pos );
2006-11-15 15:35:50 +00:00
$tok = $in [ $pos ];
2003-04-23 22:55:53 +00:00
$in = substr ( $in , $pos + 1 );
if ( $tok == ']' )
{
2007-11-19 16:44:30 +00:00
if ( strtolower ( $buffer ) == '/quote' && sizeof ( $close_tags ) && substr ( $out , - 1 , 1 ) == '[' )
2003-04-23 22:55:53 +00:00
{
2003-05-09 20:24:15 +00:00
// we have found a closing tag
2006-11-15 15:35:50 +00:00
$out .= array_pop ( $close_tags ) . ']' ;
2003-04-23 22:55:53 +00:00
$tok = '[' ;
2003-05-09 20:24:15 +00:00
$buffer = '' ;
2006-11-15 15:35:50 +00:00
2007-05-16 17:38:05 +00:00
/* Add space at the end of the closing tag if not happened before to allow following urls / smilies to be parsed correctly
* Do not try to think for the user . :/ Do not parse urls / smilies if there is no space - is the same as with other bbcodes too .
* Also , we won ' t have any spaces within $in anyway , only adding up spaces -> #10982
2006-11-15 15:35:50 +00:00
if ( ! $in || $in [ 0 ] !== ' ' )
{
$out .= ' ' ;
2007-05-16 17:38:05 +00:00
} */
2003-04-23 22:55:53 +00:00
}
2008-02-23 15:29:38 +00:00
else if ( preg_match ( '#^quote(?:="(.*?)")?$#is' , $buffer , $m ) && substr ( $out , - 1 , 1 ) == '[' )
2003-04-23 22:55:53 +00:00
{
2004-11-06 14:22:04 +00:00
$this -> parsed_items [ 'quote' ] ++ ;
2003-05-09 20:24:15 +00:00
// the buffer holds a valid opening tag
2004-09-16 18:33:22 +00:00
if ( $config [ 'max_quote_depth' ] && sizeof ( $close_tags ) >= $config [ 'max_quote_depth' ])
2003-05-22 01:32:27 +00:00
{
// there are too many nested quotes
2003-07-12 16:04:24 +00:00
$error_ary [ 'quote_depth' ] = sprintf ( $user -> lang [ 'QUOTE_DEPTH_EXCEEDED' ], $config [ 'max_quote_depth' ]);
2003-05-22 01:32:27 +00:00
$out .= $buffer . $tok ;
$tok = '[]' ;
$buffer = '' ;
continue ;
}
2003-04-23 22:55:53 +00:00
array_push ( $close_tags , '/quote:' . $this -> bbcode_uid );
2003-05-09 20:24:15 +00:00
2004-09-04 19:32:23 +00:00
if ( isset ( $m [ 1 ]) && $m [ 1 ])
2003-05-09 20:24:15 +00:00
{
2007-10-19 13:10:13 +00:00
$username = str_replace ( array ( '[' , ']' ), array ( '[' , ']' ), $m [ 1 ]);
$username = preg_replace ( '#\[(?!b|i|u|color|url|email|/b|/i|/u|/color|/url|/email)#iU' , '[$1' , $username );
2003-05-25 19:17:39 +00:00
$end_tags = array ();
2004-01-25 14:30:15 +00:00
$error = false ;
2003-05-25 19:17:39 +00:00
preg_match_all ( '#\[((?:/)?(?:[a-z]+))#i' , $username , $tags );
foreach ( $tags [ 1 ] as $tag )
{
2006-10-07 17:40:07 +00:00
if ( $tag [ 0 ] != '/' )
2003-05-25 19:17:39 +00:00
{
$end_tags [] = '/' . $tag ;
}
else
{
2003-06-05 19:17:36 +00:00
$end_tag = array_pop ( $end_tags );
2006-11-27 18:00:41 +00:00
$error = ( $end_tag != $tag ) ? true : false ;
2003-05-25 19:17:39 +00:00
}
}
2006-06-10 20:16:02 +00:00
2003-05-25 19:17:39 +00:00
if ( $error )
{
2007-10-19 13:10:13 +00:00
$username = $m [ 1 ];
2003-05-25 19:17:39 +00:00
}
2003-09-07 18:11:37 +00:00
$out .= 'quote="' . $username . '":' . $this -> bbcode_uid . ']' ;
2003-05-09 20:24:15 +00:00
}
else
{
$out .= 'quote:' . $this -> bbcode_uid . ']' ;
}
2003-04-23 22:55:53 +00:00
$tok = '[' ;
2003-05-09 20:24:15 +00:00
$buffer = '' ;
}
2005-11-28 18:38:49 +00:00
else if ( preg_match ( '#^quote="(.*?)#is' , $buffer , $m ))
2003-05-09 20:24:15 +00:00
{
// the buffer holds an invalid opening tag
$buffer .= ']' ;
2003-04-23 22:55:53 +00:00
}
else
{
$out .= $buffer . $tok ;
$tok = '[]' ;
2003-05-09 20:24:15 +00:00
$buffer = '' ;
2003-04-23 22:55:53 +00:00
}
}
else
{
2006-11-27 18:00:41 +00:00
/**
* Old quote code working fine , but having errors listed in bug #3572
*
* $out .= $buffer . $tok ;
* $tok = ( $tok == '[' ) ? ']' : '[]' ;
* $buffer = '' ;
*/
2003-04-23 22:55:53 +00:00
$out .= $buffer . $tok ;
2006-11-27 18:00:41 +00:00
if ( $tok == '[' )
{
// Search the text for the next tok... if an ending quote comes first, then change tok to []
2007-11-19 16:44:30 +00:00
$pos1 = stripos ( $in , '[/quote' );
2007-10-08 14:38:44 +00:00
// If the token ] comes first, we change it to ]
2006-11-27 18:00:41 +00:00
$pos2 = strpos ( $in , ']' );
2007-10-08 14:38:44 +00:00
// If the token [ comes first, we change it to [
$pos3 = strpos ( $in , '[' );
2006-11-27 18:00:41 +00:00
2007-10-08 14:38:44 +00:00
if ( $pos1 !== false && ( $pos2 === false || $pos1 < $pos2 ) && ( $pos3 === false || $pos1 < $pos3 ))
2006-11-27 18:00:41 +00:00
{
$tok = '[]' ;
}
2007-10-08 14:38:44 +00:00
else if ( $pos3 !== false && ( $pos2 === false || $pos3 < $pos2 ))
{
$tok = '[' ;
}
2006-11-27 18:00:41 +00:00
else
{
$tok = ']' ;
}
}
else
{
$tok = '[]' ;
}
2003-05-09 20:24:15 +00:00
$buffer = '' ;
2003-04-23 22:55:53 +00:00
}
}
while ( $in );
2004-09-16 18:33:22 +00:00
if ( sizeof ( $close_tags ))
2003-04-23 22:55:53 +00:00
{
$out .= '[' . implode ( '][' , $close_tags ) . ']' ;
}
2003-05-22 01:32:27 +00:00
foreach ( $error_ary as $error_msg )
{
$this -> warn_msg [] = $error_msg ;
}
2003-04-23 22:55:53 +00:00
return $out ;
}
2006-06-10 20:16:02 +00:00
/**
* Validate email
*/
2003-04-11 00:19:29 +00:00
function validate_email ( $var1 , $var2 )
{
2006-05-12 20:52:58 +00:00
$var1 = str_replace ( " \r \n " , " \n " , str_replace ( '\"' , '"' , trim ( $var1 )));
$var2 = str_replace ( " \r \n " , " \n " , str_replace ( '\"' , '"' , trim ( $var2 )));
$txt = $var2 ;
$email = ( $var1 ) ? $var1 : $var2 ;
2003-08-28 20:50:20 +00:00
2004-01-25 14:30:15 +00:00
$validated = true ;
2003-04-16 22:31:20 +00:00
2006-07-01 19:11:52 +00:00
if ( ! preg_match ( '/^' . get_preg_expression ( 'email' ) . '$/i' , $email ))
2003-08-28 20:50:20 +00:00
{
2004-01-25 14:30:15 +00:00
$validated = false ;
2003-08-28 20:50:20 +00:00
}
if ( ! $validated )
{
return '[email' . (( $var1 ) ? " = $var1 " : '' ) . ']' . $var2 . '[/email]' ;
}
2004-11-06 14:22:04 +00:00
$this -> parsed_items [ 'email' ] ++ ;
2006-06-10 20:16:02 +00:00
2003-08-28 20:50:20 +00:00
if ( $var1 )
{
2006-06-12 16:09:55 +00:00
$retval = '[email=' . $this -> bbcode_specialchars ( $email ) . ':' . $this -> bbcode_uid . ']' . $txt . '[/email:' . $this -> bbcode_uid . ']' ;
2003-08-28 20:50:20 +00:00
}
else
{
2006-06-10 20:16:02 +00:00
$retval = '[email:' . $this -> bbcode_uid . ']' . $this -> bbcode_specialchars ( $email ) . '[/email:' . $this -> bbcode_uid . ']' ;
2003-08-28 20:50:20 +00:00
}
2005-05-15 20:08:05 +00:00
2003-04-11 00:19:29 +00:00
return $retval ;
}
2006-06-10 20:16:02 +00:00
/**
* Validate url
2006-12-16 20:24:34 +00:00
*
* @ param string $var1 optional url parameter for url bbcode : [ url ( = $var1 )] $var2 [ / url ]
2007-10-05 14:30:11 +00:00
* @ param string $var2 url bbcode content : [ url ( = $var1 )] $var2 [ / url ]
2006-06-10 20:16:02 +00:00
*/
2003-04-11 00:19:29 +00:00
function validate_url ( $var1 , $var2 )
2003-02-27 23:37:02 +00:00
{
global $config ;
2006-06-10 20:16:02 +00:00
2006-05-12 20:52:58 +00:00
$var1 = str_replace ( " \r \n " , " \n " , str_replace ( '\"' , '"' , trim ( $var1 )));
$var2 = str_replace ( " \r \n " , " \n " , str_replace ( '\"' , '"' , trim ( $var2 )));
2005-05-10 17:03:56 +00:00
2006-05-12 20:52:58 +00:00
$url = ( $var1 ) ? $var1 : $var2 ;
2003-02-27 23:37:02 +00:00
2008-01-05 16:10:10 +00:00
if ( $var1 && ! $var2 )
2005-05-10 17:03:56 +00:00
{
2008-01-05 16:10:10 +00:00
$var2 = $var1 ;
}
if ( ! $url )
{
return '[url' . (( $var1 ) ? '=' . $var1 : '' ) . ']' . $var2 . '[/url]' ;
2005-05-10 17:03:56 +00:00
}
2006-12-16 20:24:34 +00:00
$valid = false ;
$url = str_replace ( ' ' , '%20' , $url );
// Checking urls
if ( preg_match ( '#^' . get_preg_expression ( 'url' ) . '$#i' , $url ) ||
preg_match ( '#^' . get_preg_expression ( 'www_url' ) . '$#i' , $url ) ||
preg_match ( '#^' . preg_quote ( generate_board_url (), '#' ) . get_preg_expression ( 'relative_url' ) . '$#i' , $url ))
2003-08-28 17:32:18 +00:00
{
2006-12-16 20:24:34 +00:00
$valid = true ;
2003-08-28 17:32:18 +00:00
}
2004-02-15 14:03:19 +00:00
if ( $valid )
2003-02-27 23:37:02 +00:00
{
2004-11-06 14:22:04 +00:00
$this -> parsed_items [ 'url' ] ++ ;
2006-06-11 05:06:48 +00:00
2006-12-16 20:24:34 +00:00
// if there is no scheme, then add http schema
if ( ! preg_match ( '#^[a-z][a-z\d+\-.]*:/{2}#i' , $url ))
2003-08-28 17:32:18 +00:00
{
2004-02-15 14:03:19 +00:00
$url = 'http://' . $url ;
2003-08-28 17:32:18 +00:00
}
2006-12-16 20:24:34 +00:00
// Is this a link to somewhere inside this board? If so then remove the session id from the url
2006-06-12 16:09:55 +00:00
if ( strpos ( $url , generate_board_url ()) !== false && strpos ( $url , 'sid=' ) !== false )
2006-06-11 05:06:48 +00:00
{
2007-02-25 22:09:53 +00:00
$url = preg_replace ( '/(&|\?)sid=[0-9a-f]{32}&/' , '\1' , $url );
$url = preg_replace ( '/(&|\?)sid=[0-9a-f]{32}$/' , '' , $url );
$url = append_sid ( $url );
2006-06-11 05:06:48 +00:00
}
2007-10-05 14:30:11 +00:00
return ( $var1 ) ? '[url=' . $this -> bbcode_specialchars ( $url ) . ':' . $this -> bbcode_uid . ']' . $var2 . '[/url:' . $this -> bbcode_uid . ']' : '[url:' . $this -> bbcode_uid . ']' . $this -> bbcode_specialchars ( $url ) . '[/url:' . $this -> bbcode_uid . ']' ;
2003-02-27 23:37:02 +00:00
}
2004-02-15 14:03:19 +00:00
2006-05-12 20:52:58 +00:00
return '[url' . (( $var1 ) ? '=' . $var1 : '' ) . ']' . $var2 . '[/url]' ;
2003-02-27 23:37:02 +00:00
}
2006-06-13 15:06:32 +00:00
/**
* Check if url is pointing to this domain / script_path / php - file
*
* @ param string $url the url to check
* @ return true if the url is pointing to this domain / script_path / php - file , false if not
*
2006-08-22 21:26:06 +00:00
* @ access private
2006-06-13 15:06:32 +00:00
*/
function path_in_domain ( $url )
{
global $config , $phpEx , $user ;
2006-12-08 15:20:57 +00:00
if ( $config [ 'force_server_vars' ])
{
$check_path = $config [ 'script_path' ];
}
else
{
$check_path = ( $user -> page [ 'root_script_path' ] != '/' ) ? substr ( $user -> page [ 'root_script_path' ], 0 , - 1 ) : '/' ;
}
2006-07-06 16:46:53 +00:00
2006-06-13 15:06:32 +00:00
// Is the user trying to link to a php file in this domain and script path?
2006-07-06 16:46:53 +00:00
if ( strpos ( $url , " . { $phpEx } " ) !== false && strpos ( $url , $check_path ) !== false )
2006-06-13 15:06:32 +00:00
{
2008-01-29 15:49:15 +00:00
$server_name = $user -> host ;
2006-06-13 15:06:32 +00:00
// Forcing server vars is the only way to specify/override the protocol
if ( $config [ 'force_server_vars' ] || ! $server_name )
{
$server_name = $config [ 'server_name' ];
}
// Check again in correct order...
$pos_ext = strpos ( $url , " . { $phpEx } " );
2006-07-06 16:46:53 +00:00
$pos_path = strpos ( $url , $check_path );
2006-06-13 15:06:32 +00:00
$pos_domain = strpos ( $url , $server_name );
if ( $pos_domain !== false && $pos_path >= $pos_domain && $pos_ext >= $pos_path )
{
2007-05-19 13:25:18 +00:00
// Ok, actually we allow linking to some files (this may be able to be extended in some way later...)
2007-10-02 18:29:05 +00:00
if ( strpos ( $url , '/' . $check_path . '/download/file.' . $phpEx ) !== 0 )
2007-05-19 13:25:18 +00:00
{
return false ;
}
2006-06-13 15:06:32 +00:00
return true ;
}
}
return false ;
}
2004-09-04 19:32:23 +00:00
}
2005-04-09 12:26:45 +00:00
/**
* Main message parser for posting , pm , etc . takes raw message
2006-03-06 14:03:56 +00:00
* and parses it for attachments , bbcode and smilies
2006-06-13 21:06:29 +00:00
* @ package phpBB3
2005-04-09 12:26:45 +00:00
*/
2004-09-04 19:32:23 +00:00
class parse_message extends bbcode_firstpass
{
var $attachment_data = array ();
var $filename_data = array ();
// Helps ironing out user error
var $message_status = '' ;
var $allow_img_bbcode = true ;
var $allow_flash_bbcode = true ;
var $allow_quote_bbcode = true ;
2006-09-13 16:08:36 +00:00
var $allow_url_bbcode = true ;
2004-09-04 19:32:23 +00:00
2006-02-25 05:46:52 +00:00
var $mode ;
2006-06-10 20:16:02 +00:00
/**
* Init - give message here or manually
*/
2004-09-04 19:32:23 +00:00
function parse_message ( $message = '' )
{
// Init BBCode UID
2007-10-04 04:42:39 +00:00
$this -> bbcode_uid = substr ( base_convert ( unique_id (), 16 , 36 ), 0 , BBCODE_UID_LEN );
2004-09-04 19:32:23 +00:00
if ( $message )
{
$this -> message = $message ;
}
}
2006-06-10 20:16:02 +00:00
/**
2006-06-11 18:13:52 +00:00
* Parse Message
2006-06-10 20:16:02 +00:00
*/
2006-09-13 16:08:36 +00:00
function parse ( $allow_bbcode , $allow_magic_url , $allow_smilies , $allow_img_bbcode = true , $allow_flash_bbcode = true , $allow_quote_bbcode = true , $allow_url_bbcode = true , $update_this_message = true , $mode = 'post' )
2004-09-04 19:32:23 +00:00
{
global $config , $db , $user ;
2004-11-06 14:22:04 +00:00
$mode = ( $mode != 'post' ) ? 'sig' : 'post' ;
2006-02-25 05:46:52 +00:00
$this -> mode = $mode ;
2004-09-04 19:32:23 +00:00
$this -> allow_img_bbcode = $allow_img_bbcode ;
$this -> allow_flash_bbcode = $allow_flash_bbcode ;
$this -> allow_quote_bbcode = $allow_quote_bbcode ;
2006-09-13 16:08:36 +00:00
$this -> allow_url_bbcode = $allow_url_bbcode ;
2004-09-04 19:32:23 +00:00
2004-11-10 14:15:16 +00:00
// If false, then $this->message won't be altered, the text will be returned instead.
2004-09-04 19:32:23 +00:00
if ( ! $update_this_message )
{
$tmp_message = $this -> message ;
$return_message = & $this -> message ;
}
if ( $this -> message_status == 'display' )
{
$this -> decode_message ();
}
// Do some general 'cleanup' first before processing message,
// e.g. remove excessive newlines(?), smilies(?)
2007-05-11 12:25:28 +00:00
$match = array ( '#(script|about|applet|activex|chrome):#i' );
$replace = array ( " \\ 1: " );
2004-09-04 19:32:23 +00:00
$this -> message = preg_replace ( $match , $replace , trim ( $this -> message ));
2007-10-11 08:12:41 +00:00
// Message length check. 0 disables this check completely.
if ( $config [ 'max_' . $mode . '_chars' ] > 0 )
2004-09-04 19:32:23 +00:00
{
2006-11-27 19:32:18 +00:00
$msg_len = ( $mode == 'post' ) ? utf8_strlen ( $this -> message ) : utf8_strlen ( preg_replace ( '#\[\/?[a-z\*\+\-]+(=[\S]+)?\]#ius' , ' ' , $this -> message ));
2008-01-29 15:49:15 +00:00
2006-03-21 23:45:59 +00:00
if (( ! $msg_len && $mode !== 'sig' ) || $config [ 'max_' . $mode . '_chars' ] && $msg_len > $config [ 'max_' . $mode . '_chars' ])
2005-01-24 13:14:31 +00:00
{
2007-06-11 00:12:42 +00:00
$this -> warn_msg [] = ( ! $msg_len ) ? $user -> lang [ 'TOO_FEW_CHARS' ] : sprintf ( $user -> lang [ 'TOO_MANY_CHARS_' . strtoupper ( $mode )], $msg_len , $config [ 'max_' . $mode . '_chars' ]);
2008-03-13 14:39:53 +00:00
return ( ! $update_this_message ) ? $return_message : $this -> warn_msg ;
2005-01-24 13:14:31 +00:00
}
2004-09-04 19:32:23 +00:00
}
2007-10-11 08:12:41 +00:00
// Check for "empty" message
2008-01-05 16:10:10 +00:00
if ( $mode !== 'sig' && utf8_clean_string ( $this -> message ) === '' )
2007-10-11 08:12:41 +00:00
{
$this -> warn_msg [] = $user -> lang [ 'TOO_FEW_CHARS' ];
2008-03-13 14:39:53 +00:00
return ( ! $update_this_message ) ? $return_message : $this -> warn_msg ;
2007-10-11 08:12:41 +00:00
}
2006-06-22 15:14:03 +00:00
// Prepare BBcode (just prepares some tags for better parsing)
2004-09-04 19:32:23 +00:00
if ( $allow_bbcode && strpos ( $this -> message , '[' ) !== false )
{
$this -> bbcode_init ();
2006-09-13 16:08:36 +00:00
$disallow = array ( 'img' , 'flash' , 'quote' , 'url' );
2004-09-04 19:32:23 +00:00
foreach ( $disallow as $bool )
{
if ( ! $ { 'allow_' . $bool . '_bbcode' })
{
$this -> bbcodes [ $bool ][ 'disabled' ] = true ;
}
}
2006-06-10 20:16:02 +00:00
2006-06-22 15:14:03 +00:00
$this -> prepare_bbcodes ();
}
// Parse smilies
if ( $allow_smilies )
{
$this -> smilies ( $config [ 'max_' . $mode . '_smilies' ]);
}
$num_urls = 0 ;
// Parse BBCode
if ( $allow_bbcode && strpos ( $this -> message , '[' ) !== false )
{
$this -> parse_bbcode ();
2004-11-06 14:22:04 +00:00
$num_urls += $this -> parsed_items [ 'url' ];
}
2004-11-14 17:12:07 +00:00
// Parse URL's
if ( $allow_magic_url )
{
2006-03-01 21:48:02 +00:00
$this -> magic_url ( generate_board_url ());
2007-05-05 18:09:58 +00:00
2004-11-14 17:12:07 +00:00
if ( $config [ 'max_' . $mode . '_urls' ])
{
2006-12-10 01:19:54 +00:00
$num_urls += preg_match_all ( '#\<!-- ([lmwe]) --\>.*?\<!-- \1 --\>#' , $this -> message , $matches );
2004-11-14 17:12:07 +00:00
}
}
2004-11-06 14:22:04 +00:00
// Check number of links
if ( $config [ 'max_' . $mode . '_urls' ] && $num_urls > $config [ 'max_' . $mode . '_urls' ])
{
$this -> warn_msg [] = sprintf ( $user -> lang [ 'TOO_MANY_URLS' ], $config [ 'max_' . $mode . '_urls' ]);
2008-03-13 14:39:53 +00:00
return ( ! $update_this_message ) ? $return_message : $this -> warn_msg ;
2004-09-04 19:32:23 +00:00
}
if ( ! $update_this_message )
{
unset ( $this -> message );
$this -> message = $tmp_message ;
return $return_message ;
}
$this -> message_status = 'parsed' ;
2006-05-26 15:04:27 +00:00
return false ;
2004-09-04 19:32:23 +00:00
}
2006-06-11 18:13:52 +00:00
/**
* Formatting text for display
*/
2006-03-06 14:03:56 +00:00
function format_display ( $allow_bbcode , $allow_magic_url , $allow_smilies , $update_this_message = true )
2004-09-04 19:32:23 +00:00
{
// If false, then the parsed message get returned but internal message not processed.
if ( ! $update_this_message )
{
$tmp_message = $this -> message ;
$return_message = & $this -> message ;
}
if ( $this -> message_status == 'plain' )
{
// Force updating message - of course.
2006-09-13 16:08:36 +00:00
$this -> parse ( $allow_bbcode , $allow_magic_url , $allow_smilies , $this -> allow_img_bbcode , $this -> allow_flash_bbcode , $this -> allow_quote_bbcode , $this -> allow_url_bbcode , true );
2004-09-04 19:32:23 +00:00
}
2007-08-19 13:40:53 +00:00
// Replace naughty words such as farty pants
$this -> message = censor_text ( $this -> message );
2004-09-04 19:32:23 +00:00
// Parse BBcode
if ( $allow_bbcode )
{
$this -> bbcode_cache_init ();
// We are giving those parameters to be able to use the bbcode class on its own
$this -> bbcode_second_pass ( $this -> message , $this -> bbcode_uid );
}
2007-08-19 13:40:53 +00:00
$this -> message = bbcode_nl2br ( $this -> message );
2005-03-21 23:10:11 +00:00
$this -> message = smiley_text ( $this -> message , ! $allow_smilies );
2004-09-04 19:32:23 +00:00
if ( ! $update_this_message )
{
unset ( $this -> message );
$this -> message = $tmp_message ;
return $return_message ;
}
$this -> message_status = 'display' ;
2006-05-26 15:04:27 +00:00
return false ;
2006-06-11 18:13:52 +00:00
}
/**
* Decode message to be placed back into form box
*/
2004-09-04 19:32:23 +00:00
function decode_message ( $custom_bbcode_uid = '' , $update_this_message = true )
{
// If false, then the parsed message get returned but internal message not processed.
if ( ! $update_this_message )
{
$tmp_message = $this -> message ;
$return_message = & $this -> message ;
}
( $custom_bbcode_uid ) ? decode_message ( $this -> message , $custom_bbcode_uid ) : decode_message ( $this -> message , $this -> bbcode_uid );
if ( ! $update_this_message )
{
unset ( $this -> message );
$this -> message = $tmp_message ;
return $return_message ;
}
$this -> message_status = 'plain' ;
2006-05-26 15:04:27 +00:00
return false ;
2004-09-04 19:32:23 +00:00
}
2006-06-11 18:13:52 +00:00
/**
* Replace magic urls of form http :// xxx . xxx . , www . xxx . and xxx @ xxx . xxx .
* Cuts down displayed size of link if over 50 chars , turns absolute links
* into relative versions when the server / script path matches the link
*/
2006-03-01 21:48:02 +00:00
function magic_url ( $server_url )
2004-09-04 19:32:23 +00:00
{
2006-03-25 14:16:17 +00:00
// We use the global make_clickable function
$this -> message = make_clickable ( $this -> message , $server_url );
2004-09-04 19:32:23 +00:00
}
2006-06-11 18:13:52 +00:00
/**
* Parse Smilies
*/
2005-03-21 23:10:11 +00:00
function smilies ( $max_smilies = 0 )
2004-09-04 19:32:23 +00:00
{
2006-12-10 01:19:54 +00:00
global $db , $user ;
2004-11-10 14:15:16 +00:00
static $match ;
static $replace ;
2004-09-04 19:32:23 +00:00
2004-11-10 14:15:16 +00:00
// See if the static arrays have already been filled on an earlier invocation
2004-11-14 17:12:07 +00:00
if ( ! is_array ( $match ))
2004-09-04 19:32:23 +00:00
{
2006-06-11 18:13:52 +00:00
$match = $replace = array ();
2004-11-10 14:15:16 +00:00
// NOTE: obtain_* function? chaching the table contents?
2008-01-29 15:49:15 +00:00
2004-11-10 14:15:16 +00:00
// For now setting the ttl to 10 minutes
2006-10-14 14:56:46 +00:00
switch ( $db -> sql_layer )
2004-09-04 19:32:23 +00:00
{
2004-11-10 14:15:16 +00:00
case 'mssql' :
2005-08-17 15:57:50 +00:00
case 'mssql_odbc' :
2007-10-05 14:30:11 +00:00
$sql = ' SELECT *
2004-11-10 14:15:16 +00:00
FROM ' . SMILIES_TABLE . '
ORDER BY LEN ( code ) DESC ' ;
2006-05-13 02:14:59 +00:00
break ;
2008-01-29 15:49:15 +00:00
2006-05-13 02:14:59 +00:00
case 'firebird' :
2007-10-05 14:30:11 +00:00
$sql = ' SELECT *
2006-05-13 02:14:59 +00:00
FROM ' . SMILIES_TABLE . '
2006-10-01 08:48:32 +00:00
ORDER BY CHAR_LENGTH ( code ) DESC ' ;
2006-05-13 02:14:59 +00:00
break ;
2004-11-10 14:15:16 +00:00
// LENGTH supported by MySQL, IBM DB2, Oracle and Access for sure...
default :
2007-10-05 14:30:11 +00:00
$sql = ' SELECT *
2004-11-10 14:15:16 +00:00
FROM ' . SMILIES_TABLE . '
ORDER BY LENGTH ( code ) DESC ' ;
2006-05-13 02:14:59 +00:00
break ;
2004-09-04 19:32:23 +00:00
}
2004-11-10 14:15:16 +00:00
$result = $db -> sql_query ( $sql , 600 );
2006-02-21 10:32:19 +00:00
2006-06-11 18:13:52 +00:00
while ( $row = $db -> sql_fetchrow ( $result ))
2004-11-10 14:15:16 +00:00
{
2007-07-12 16:14:07 +00:00
if ( empty ( $row [ 'code' ]))
{
continue ;
}
2006-06-11 18:13:52 +00:00
// (assertion)
2007-06-28 14:55:48 +00:00
$match [] = '(?<=^|[\n .])' . preg_quote ( $row [ 'code' ], '#' ) . '(?![^<>]*>)' ;
2007-03-08 11:14:40 +00:00
$replace [] = '<!-- s' . $row [ 'code' ] . ' --><img src="{SMILIES_PATH}/' . $row [ 'smiley_url' ] . '" alt="' . $row [ 'code' ] . '" title="' . $row [ 'emotion' ] . '" /><!-- s' . $row [ 'code' ] . ' -->' ;
2004-11-10 14:15:16 +00:00
}
$db -> sql_freeresult ( $result );
}
2006-02-21 10:32:19 +00:00
2004-11-14 17:12:07 +00:00
if ( sizeof ( $match ))
2004-11-10 14:15:16 +00:00
{
2004-09-04 19:32:23 +00:00
if ( $max_smilies )
{
2007-06-28 14:55:48 +00:00
$num_matches = preg_match_all ( '#' . implode ( '|' , $match ) . '#' , $this -> message , $matches );
2007-06-10 13:39:04 +00:00
unset ( $matches );
if ( $num_matches !== false && $num_matches > $max_smilies )
2004-09-04 19:32:23 +00:00
{
2007-06-10 13:39:04 +00:00
$this -> warn_msg [] = sprintf ( $user -> lang [ 'TOO_MANY_SMILIES' ], $max_smilies );
return ;
2004-09-04 19:32:23 +00:00
}
}
2007-06-10 13:39:04 +00:00
2007-06-28 14:55:48 +00:00
// Make sure the delimiter # is added in front and at the end of every element within $match
2007-07-01 09:08:03 +00:00
$this -> message = trim ( preg_replace ( explode ( chr ( 0 ), '#' . implode ( '#' . chr ( 0 ) . '#' , $match ) . '#' ), $replace , $this -> message ));
2004-09-04 19:32:23 +00:00
}
}
2003-02-27 23:37:02 +00:00
2006-06-11 18:13:52 +00:00
/**
* Parse Attachments
*/
2005-03-21 23:10:11 +00:00
function parse_attachments ( $form_name , $mode , $forum_id , $submit , $preview , $refresh , $is_message = false )
2003-02-27 23:37:02 +00:00
{
2006-09-13 16:08:36 +00:00
global $config , $auth , $user , $phpbb_root_path , $phpEx , $db ;
2003-03-22 15:48:46 +00:00
2003-05-11 16:21:35 +00:00
$error = array ();
2003-03-22 15:48:46 +00:00
2004-05-02 13:06:57 +00:00
$num_attachments = sizeof ( $this -> attachment_data );
2006-11-15 15:35:50 +00:00
$this -> filename_data [ 'filecomment' ] = utf8_normalize_nfc ( request_var ( 'filecomment' , '' , true ));
2005-03-21 23:10:11 +00:00
$upload_file = ( isset ( $_FILES [ $form_name ]) && $_FILES [ $form_name ][ 'name' ] != 'none' && trim ( $_FILES [ $form_name ][ 'name' ])) ? true : false ;
2006-04-17 13:09:50 +00:00
$add_file = ( isset ( $_POST [ 'add_file' ])) ? true : false ;
$delete_file = ( isset ( $_POST [ 'delete_file' ])) ? true : false ;
2006-12-24 13:11:54 +00:00
// First of all adjust comments if changed
$actual_comment_list = utf8_normalize_nfc ( request_var ( 'comment_list' , array ( '' ), true ));
foreach ( $actual_comment_list as $comment_key => $comment )
{
if ( ! isset ( $this -> attachment_data [ $comment_key ]))
{
continue ;
}
if ( $this -> attachment_data [ $comment_key ][ 'attach_comment' ] != $actual_comment_list [ $comment_key ])
{
$this -> attachment_data [ $comment_key ][ 'attach_comment' ] = $actual_comment_list [ $comment_key ];
}
}
2003-03-22 15:48:46 +00:00
2004-05-02 13:06:57 +00:00
$cfg = array ();
$cfg [ 'max_attachments' ] = ( $is_message ) ? $config [ 'max_attachments_pm' ] : $config [ 'max_attachments' ];
$forum_id = ( $is_message ) ? 0 : $forum_id ;
2005-03-21 23:10:11 +00:00
if ( $submit && in_array ( $mode , array ( 'post' , 'reply' , 'quote' , 'edit' )) && $upload_file )
2003-03-22 15:48:46 +00:00
{
2006-06-14 18:59:12 +00:00
if ( $num_attachments < $cfg [ 'max_attachments' ] || $auth -> acl_get ( 'a_' ) || $auth -> acl_get ( 'm_' , $forum_id ))
2003-03-22 15:48:46 +00:00
{
2005-03-21 23:10:11 +00:00
$filedata = upload_attachment ( $form_name , $forum_id , false , '' , $is_message );
2003-05-11 16:21:35 +00:00
$error = $filedata [ 'error' ];
2003-03-22 15:48:46 +00:00
2004-05-02 13:06:57 +00:00
if ( $filedata [ 'post_attach' ] && ! sizeof ( $error ))
2003-03-22 15:48:46 +00:00
{
2006-09-13 16:08:36 +00:00
$sql_ary = array (
2005-03-21 23:10:11 +00:00
'physical_filename' => $filedata [ 'physical_filename' ],
- renamed the following columns:
comment -> attach_comment
new, forwarded, unread, marked, deleted -> pm_new, pm_forwarded, pm_unread, pm_marked, pm_deleted
module_name -> module_basename
value -> lang_value
- every column is now NOT NULL
- every column is now having a DEFAULT value
- hopefully mostly consistent across every db schema
- untested schemas: sqlite, oracle, firebird
git-svn-id: file:///svn/phpbb/trunk@6177 89ea8834-ac86-4346-8a33-228a782c2dd0
2006-07-13 12:51:56 +00:00
'attach_comment' => $this -> filename_data [ 'filecomment' ],
2005-03-21 23:10:11 +00:00
'real_filename' => $filedata [ 'real_filename' ],
2003-06-20 17:20:22 +00:00
'extension' => $filedata [ 'extension' ],
'mimetype' => $filedata [ 'mimetype' ],
'filesize' => $filedata [ 'filesize' ],
'filetime' => $filedata [ 'filetime' ],
2006-09-13 16:08:36 +00:00
'thumbnail' => $filedata [ 'thumbnail' ],
'is_orphan' => 1 ,
'in_message' => ( $is_message ) ? 1 : 0 ,
'poster_id' => $user -> data [ 'user_id' ],
);
$db -> sql_query ( 'INSERT INTO ' . ATTACHMENTS_TABLE . ' ' . $db -> sql_build_array ( 'INSERT' , $sql_ary ));
$new_entry = array (
'attach_id' => $db -> sql_nextid (),
'is_orphan' => 1 ,
'real_filename' => $filedata [ 'real_filename' ],
'attach_comment' => $this -> filename_data [ 'filecomment' ],
2003-05-01 18:24:18 +00:00
);
$this -> attachment_data = array_merge ( array ( 0 => $new_entry ), $this -> attachment_data );
2004-02-08 18:02:17 +00:00
$this -> message = preg_replace ( '#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e' , " '[attachment='.( \\ 1 + 1).'] \\ 2[/attachment]' " , $this -> message );
2006-06-11 18:13:52 +00:00
2003-05-01 18:24:18 +00:00
$this -> filename_data [ 'filecomment' ] = '' ;
2004-01-25 14:30:15 +00:00
// This Variable is set to false here, because Attachments are entered into the
2003-10-09 14:49:21 +00:00
// Database in two modes, one if the id_list is 0 and the second one if post_attach is true
2003-03-22 15:48:46 +00:00
// Since post_attach is automatically switched to true if an Attachment got added to the filesystem,
2003-10-09 14:49:21 +00:00
// but we are assigning an id of 0 here, we have to reset the post_attach variable to false.
2003-03-22 15:48:46 +00:00
//
// This is very relevant, because it could happen that the post got not submitted, but we do not
// know this circumstance here. We could be at the posting page or we could be redirected to the entered
// post. :)
2004-01-25 14:30:15 +00:00
$filedata [ 'post_attach' ] = false ;
2003-03-22 15:48:46 +00:00
}
}
else
{
2004-05-02 13:06:57 +00:00
$error [] = sprintf ( $user -> lang [ 'TOO_MANY_ATTACHMENTS' ], $cfg [ 'max_attachments' ]);
2003-03-22 15:48:46 +00:00
}
}
2004-09-16 18:33:22 +00:00
if ( $preview || $refresh || sizeof ( $error ))
2003-03-22 15:48:46 +00:00
{
// Perform actions on temporary attachments
if ( $delete_file )
{
2006-03-01 21:48:02 +00:00
include_once ( $phpbb_root_path . 'includes/functions_admin.' . $phpEx );
2007-08-15 17:32:48 +00:00
$index = array_keys ( request_var ( 'delete_file' , array ( 0 => 0 )));
2007-08-16 12:19:26 +00:00
$index = ( ! empty ( $index )) ? $index [ 0 ] : false ;
2003-09-24 19:41:14 +00:00
2007-08-15 17:32:48 +00:00
if ( $index !== false && ! empty ( $this -> attachment_data [ $index ]))
2003-03-22 15:48:46 +00:00
{
2006-09-13 16:08:36 +00:00
// delete selected attachment
if ( $this -> attachment_data [ $index ][ 'is_orphan' ])
2003-05-01 18:24:18 +00:00
{
2006-09-13 16:08:36 +00:00
$sql = ' SELECT attach_id , physical_filename , thumbnail
FROM ' . ATTACHMENTS_TABLE . '
WHERE attach_id = ' . (int) $this->attachment_data[$index][' attach_id '] . '
AND is_orphan = 1
AND poster_id = ' . $user->data[' user_id ' ];
$result = $db -> sql_query ( $sql );
$row = $db -> sql_fetchrow ( $result );
$db -> sql_freeresult ( $result );
if ( $row )
{
phpbb_unlink ( $row [ 'physical_filename' ], 'file' );
if ( $row [ 'thumbnail' ])
{
phpbb_unlink ( $row [ 'physical_filename' ], 'thumbnail' );
}
$db -> sql_query ( 'DELETE FROM ' . ATTACHMENTS_TABLE . ' WHERE attach_id = ' . ( int ) $this -> attachment_data [ $index ][ 'attach_id' ]);
}
}
else
{
delete_attachments ( 'attach' , array ( intval ( $this -> attachment_data [ $index ][ 'attach_id' ])));
2003-05-01 18:24:18 +00:00
}
2006-06-11 18:13:52 +00:00
2006-09-13 16:08:36 +00:00
unset ( $this -> attachment_data [ $index ]);
$this -> message = preg_replace ( '#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e' , " ( \\ 1 == \$ index) ? '' : (( \\ 1 > \$ index) ? '[attachment=' . ( \\ 1 - 1) . '] \\ 2[/attachment]' : ' \\ 0') " , $this -> message );
2004-02-08 18:02:17 +00:00
2006-09-13 16:08:36 +00:00
// Reindex Array
$this -> attachment_data = array_values ( $this -> attachment_data );
}
2003-03-22 15:48:46 +00:00
}
2006-12-24 13:11:54 +00:00
else if (( $add_file || $preview ) && $upload_file )
2003-03-22 15:48:46 +00:00
{
2006-12-24 13:11:54 +00:00
if ( $num_attachments < $cfg [ 'max_attachments' ] || $auth -> acl_gets ( 'm_' , 'a_' , $forum_id ))
2003-03-22 15:48:46 +00:00
{
2006-12-24 13:11:54 +00:00
$filedata = upload_attachment ( $form_name , $forum_id , false , '' , $is_message );
$error = array_merge ( $error , $filedata [ 'error' ]);
2003-03-22 15:48:46 +00:00
2006-12-24 13:11:54 +00:00
if ( ! sizeof ( $error ))
2003-03-22 15:48:46 +00:00
{
2006-12-24 13:11:54 +00:00
$sql_ary = array (
'physical_filename' => $filedata [ 'physical_filename' ],
'attach_comment' => $this -> filename_data [ 'filecomment' ],
'real_filename' => $filedata [ 'real_filename' ],
'extension' => $filedata [ 'extension' ],
'mimetype' => $filedata [ 'mimetype' ],
'filesize' => $filedata [ 'filesize' ],
'filetime' => $filedata [ 'filetime' ],
'thumbnail' => $filedata [ 'thumbnail' ],
'is_orphan' => 1 ,
'in_message' => ( $is_message ) ? 1 : 0 ,
'poster_id' => $user -> data [ 'user_id' ],
);
$db -> sql_query ( 'INSERT INTO ' . ATTACHMENTS_TABLE . ' ' . $db -> sql_build_array ( 'INSERT' , $sql_ary ));
$new_entry = array (
'attach_id' => $db -> sql_nextid (),
'is_orphan' => 1 ,
'real_filename' => $filedata [ 'real_filename' ],
'attach_comment' => $this -> filename_data [ 'filecomment' ],
);
$this -> attachment_data = array_merge ( array ( 0 => $new_entry ), $this -> attachment_data );
$this -> message = preg_replace ( '#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e' , " '[attachment='.( \\ 1 + 1).'] \\ 2[/attachment]' " , $this -> message );
$this -> filename_data [ 'filecomment' ] = '' ;
2003-03-22 15:48:46 +00:00
}
}
2006-12-24 13:11:54 +00:00
else
{
$error [] = sprintf ( $user -> lang [ 'TOO_MANY_ATTACHMENTS' ], $cfg [ 'max_attachments' ]);
}
2003-03-22 15:48:46 +00:00
}
}
2003-02-27 23:37:02 +00:00
2003-05-22 01:32:27 +00:00
foreach ( $error as $error_msg )
{
$this -> warn_msg [] = $error_msg ;
}
2003-02-27 23:37:02 +00:00
}
2006-04-17 13:09:50 +00:00
/**
* Get Attachment Data
*/
2006-06-06 10:54:51 +00:00
function get_submitted_attachment_data ( $check_user_id = false )
2004-05-02 13:06:57 +00:00
{
2006-04-17 13:09:50 +00:00
global $user , $db , $phpbb_root_path , $phpEx , $config ;
2006-11-15 15:35:50 +00:00
$this -> filename_data [ 'filecomment' ] = utf8_normalize_nfc ( request_var ( 'filecomment' , '' , true ));
2006-09-13 16:08:36 +00:00
$attachment_data = ( isset ( $_POST [ 'attachment_data' ])) ? $_POST [ 'attachment_data' ] : array ();
$this -> attachment_data = array ();
2004-05-02 13:06:57 +00:00
2006-06-06 10:54:51 +00:00
$check_user_id = ( $check_user_id === false ) ? $user -> data [ 'user_id' ] : $check_user_id ;
2006-09-13 16:08:36 +00:00
if ( ! sizeof ( $attachment_data ))
{
return ;
}
$not_orphan = $orphan = array ();
2006-04-17 13:09:50 +00:00
2006-09-13 16:08:36 +00:00
foreach ( $attachment_data as $pos => $var_ary )
2004-05-02 13:06:57 +00:00
{
2006-09-13 16:08:36 +00:00
if ( $var_ary [ 'is_orphan' ])
2006-04-17 13:09:50 +00:00
{
2006-09-13 16:08:36 +00:00
$orphan [( int ) $var_ary [ 'attach_id' ]] = $pos ;
2006-04-17 13:09:50 +00:00
}
else
{
2006-09-13 16:08:36 +00:00
$not_orphan [( int ) $var_ary [ 'attach_id' ]] = $pos ;
2006-04-17 13:09:50 +00:00
}
}
2006-09-13 16:08:36 +00:00
// Regenerate already posted attachments
if ( sizeof ( $not_orphan ))
2006-04-17 13:09:50 +00:00
{
2006-09-13 16:08:36 +00:00
// Get the attachment data, based on the poster id...
$sql = ' SELECT attach_id , is_orphan , real_filename , attach_comment
2006-04-17 13:09:50 +00:00
FROM ' . ATTACHMENTS_TABLE . '
2006-09-13 16:08:36 +00:00
WHERE ' . $db->sql_in_set(' attach_id ', array_keys($not_orphan)) . '
2006-06-06 10:54:51 +00:00
AND poster_id = ' . $check_user_id ;
2006-04-17 13:09:50 +00:00
$result = $db -> sql_query ( $sql );
while ( $row = $db -> sql_fetchrow ( $result ))
{
2006-09-13 16:08:36 +00:00
$pos = $not_orphan [ $row [ 'attach_id' ]];
$this -> attachment_data [ $pos ] = $row ;
set_var ( $this -> attachment_data [ $pos ][ 'attach_comment' ], $_POST [ 'attachment_data' ][ $pos ][ 'attach_comment' ], 'string' , true );
2006-04-17 13:09:50 +00:00
2006-09-13 16:08:36 +00:00
unset ( $not_orphan [ $row [ 'attach_id' ]]);
2006-04-17 13:09:50 +00:00
}
$db -> sql_freeresult ( $result );
2006-09-13 16:08:36 +00:00
}
2006-04-17 13:09:50 +00:00
2006-09-13 16:08:36 +00:00
if ( sizeof ( $not_orphan ))
{
2007-07-22 14:04:26 +00:00
trigger_error ( 'NO_ACCESS_ATTACHMENT' , E_USER_ERROR );
2006-04-17 13:09:50 +00:00
}
// Regenerate newly uploaded attachments
2006-09-13 16:08:36 +00:00
if ( sizeof ( $orphan ))
2006-04-17 13:09:50 +00:00
{
2006-09-13 16:08:36 +00:00
$sql = ' SELECT attach_id , is_orphan , real_filename , attach_comment
2006-08-12 13:14:39 +00:00
FROM ' . ATTACHMENTS_TABLE . '
2006-09-13 16:08:36 +00:00
WHERE ' . $db->sql_in_set(' attach_id ', array_keys($orphan)) . '
AND poster_id = ' . $user->data[' user_id '] . '
AND is_orphan = 1 ' ;
$result = $db -> sql_query ( $sql );
2006-04-17 13:09:50 +00:00
2006-09-13 16:08:36 +00:00
while ( $row = $db -> sql_fetchrow ( $result ))
2004-05-02 13:06:57 +00:00
{
2006-09-13 16:08:36 +00:00
$pos = $orphan [ $row [ 'attach_id' ]];
$this -> attachment_data [ $pos ] = $row ;
- renamed the following columns:
comment -> attach_comment
new, forwarded, unread, marked, deleted -> pm_new, pm_forwarded, pm_unread, pm_marked, pm_deleted
module_name -> module_basename
value -> lang_value
- every column is now NOT NULL
- every column is now having a DEFAULT value
- hopefully mostly consistent across every db schema
- untested schemas: sqlite, oracle, firebird
git-svn-id: file:///svn/phpbb/trunk@6177 89ea8834-ac86-4346-8a33-228a782c2dd0
2006-07-13 12:51:56 +00:00
set_var ( $this -> attachment_data [ $pos ][ 'attach_comment' ], $_POST [ 'attachment_data' ][ $pos ][ 'attach_comment' ], 'string' , true );
2006-04-17 13:09:50 +00:00
2006-09-13 16:08:36 +00:00
unset ( $orphan [ $row [ 'attach_id' ]]);
2004-05-02 13:06:57 +00:00
}
2006-09-13 16:08:36 +00:00
$db -> sql_freeresult ( $result );
}
if ( sizeof ( $orphan ))
{
2007-07-22 14:04:26 +00:00
trigger_error ( 'NO_ACCESS_ATTACHMENT' , E_USER_ERROR );
2004-05-02 13:06:57 +00:00
}
2006-09-13 16:08:36 +00:00
ksort ( $this -> attachment_data );
2004-05-02 13:06:57 +00:00
}
2006-06-11 18:13:52 +00:00
/**
* Parse Poll
*/
2004-09-05 15:45:50 +00:00
function parse_poll ( & $poll )
2003-03-10 19:39:50 +00:00
{
2004-09-05 15:45:50 +00:00
global $auth , $user , $config ;
2003-03-10 19:39:50 +00:00
2004-09-05 15:45:50 +00:00
$poll_max_options = $poll [ 'poll_max_options' ];
2003-03-10 19:39:50 +00:00
2004-09-05 15:45:50 +00:00
// Parse Poll Option text ;)
$tmp_message = $this -> message ;
$this -> message = $poll [ 'poll_option_text' ];
2007-06-10 19:58:20 +00:00
$bbcode_bitfield = $this -> bbcode_bitfield ;
2006-07-24 10:08:36 +00:00
2006-09-13 16:08:36 +00:00
$poll [ 'poll_option_text' ] = $this -> parse ( $poll [ 'enable_bbcode' ], ( $config [ 'allow_post_links' ]) ? $poll [ 'enable_urls' ] : false , $poll [ 'enable_smilies' ], $poll [ 'img_status' ], false , false , $config [ 'allow_post_links' ], false );
2006-07-24 10:08:36 +00:00
2007-08-16 19:24:32 +00:00
$bbcode_bitfield = base64_encode ( base64_decode ( $bbcode_bitfield ) | base64_decode ( $this -> bbcode_bitfield ));
2004-09-05 15:45:50 +00:00
$this -> message = $tmp_message ;
2003-03-10 19:39:50 +00:00
2004-09-05 15:45:50 +00:00
// Parse Poll Title
$tmp_message = $this -> message ;
$this -> message = $poll [ 'poll_title' ];
2007-06-10 19:58:20 +00:00
$this -> bbcode_bitfield = $bbcode_bitfield ;
2006-07-24 10:08:36 +00:00
2006-12-10 01:19:54 +00:00
$poll [ 'poll_options' ] = explode ( " \n " , trim ( $poll [ 'poll_option_text' ]));
$poll [ 'poll_options_size' ] = sizeof ( $poll [ 'poll_options' ]);
2003-06-29 10:59:36 +00:00
2006-12-10 01:19:54 +00:00
if ( ! $poll [ 'poll_title' ] && $poll [ 'poll_options_size' ])
{
$this -> warn_msg [] = $user -> lang [ 'NO_POLL_TITLE' ];
}
else
{
2007-02-22 19:58:05 +00:00
if ( utf8_strlen ( preg_replace ( '#\[\/?[a-z\*\+\-]+(=[\S]+)?\]#ius' , ' ' , $this -> message )) > 100 )
{
$this -> warn_msg [] = $user -> lang [ 'POLL_TITLE_TOO_LONG' ];
}
2006-12-10 01:19:54 +00:00
$poll [ 'poll_title' ] = $this -> parse ( $poll [ 'enable_bbcode' ], ( $config [ 'allow_post_links' ]) ? $poll [ 'enable_urls' ] : false , $poll [ 'enable_smilies' ], $poll [ 'img_status' ], false , false , $config [ 'allow_post_links' ], false );
2007-02-22 19:58:05 +00:00
if ( strlen ( $poll [ 'poll_title' ]) > 255 )
{
$this -> warn_msg [] = $user -> lang [ 'POLL_TITLE_COMP_TOO_LONG' ];
}
2006-12-10 01:19:54 +00:00
}
2006-07-24 10:08:36 +00:00
2007-06-10 19:58:20 +00:00
$this -> bbcode_bitfield = base64_encode ( base64_decode ( $bbcode_bitfield ) | base64_decode ( $this -> bbcode_bitfield ));
2004-09-05 15:45:50 +00:00
$this -> message = $tmp_message ;
2007-06-10 19:58:20 +00:00
unset ( $tmp_message );
2004-09-05 15:45:50 +00:00
if ( sizeof ( $poll [ 'poll_options' ]) == 1 )
{
$this -> warn_msg [] = $user -> lang [ 'TOO_FEW_POLL_OPTIONS' ];
}
else if ( $poll [ 'poll_options_size' ] > ( int ) $config [ 'max_poll_options' ])
{
$this -> warn_msg [] = $user -> lang [ 'TOO_MANY_POLL_OPTIONS' ];
}
else if ( $poll_max_options > $poll [ 'poll_options_size' ])
{
$this -> warn_msg [] = $user -> lang [ 'TOO_MANY_USER_OPTIONS' ];
2003-03-10 19:39:50 +00:00
}
2003-04-22 16:47:34 +00:00
2004-09-05 15:45:50 +00:00
$poll [ 'poll_max_options' ] = ( $poll [ 'poll_max_options' ] < 1 ) ? 1 : (( $poll [ 'poll_max_options' ] > $config [ 'max_poll_options' ]) ? $config [ 'max_poll_options' ] : $poll [ 'poll_max_options' ]);
2003-03-10 19:39:50 +00:00
}
2003-02-27 23:37:02 +00:00
}
?>