2003-02-27 23:37:02 +00:00
< ? php
2005-04-09 12:26:45 +00:00
/**
*
* @ package phpBB3
* @ version $Id $
* @ copyright ( c ) 2005 phpBB Group
* @ license http :// opensource . org / licenses / gpl - license . php GNU Public License
*
*/
/**
* @ ignore
*/
2004-09-04 19:32:23 +00:00
if ( ! defined ( 'IN_PHPBB' ))
{
exit ;
}
2003-04-16 22:31:20 +00:00
// case-insensitive strpos() - needed for some functions
if ( ! function_exists ( 'stripos' ))
{
function stripos ( $haystack , $needle )
{
if ( preg_match ( '#' . preg_quote ( $needle , '#' ) . '#i' , $haystack , $m ))
{
return strpos ( $haystack , $m [ 0 ]);
}
2004-01-25 14:30:15 +00:00
return false ;
2003-04-16 22:31:20 +00:00
}
}
2004-09-04 19:32:23 +00:00
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
/**
* @ package phpBB3
* BBCODE FIRSTPASS
* BBCODE first pass class ( functions for parsing messages for db storage )
*/
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
// 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 ;
2003-04-16 22:31:20 +00:00
$this -> bbcode_bitfield = 0 ;
2003-04-23 22:55:53 +00:00
2003-04-11 00:19:29 +00:00
$size = strlen ( $this -> message );
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 ))
{
$this -> warn_msg [] = $user -> lang [ 'UNAUTHORISED_BBCODE' ] . '[' . $bbcode_name . ']' ;
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 )
{
$this -> message = preg_replace ( $regexp , $replacement , $this -> message );
}
2003-04-11 00:19:29 +00:00
}
2003-10-05 19:59:17 +00:00
// Because we add bbcode_uid to all tags, the message length
// will increase whenever a tag is found
2003-04-16 22:31:20 +00:00
$new_size = strlen ( $this -> message );
if ( $size != $new_size )
{
2003-07-02 22:23:45 +00:00
$this -> bbcode_bitfield |= ( 1 << $bbcode_data [ 'bbcode_id' ]);
2003-04-16 22:31:20 +00:00
$size = $new_size ;
}
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 (
2003-10-05 19:59:17 +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') " )),
2004-02-08 18:02:17 +00:00
'attachment' => array ( 'bbcode_id' => 12 , 'regexp' => array ( '#\[attachment=([0-9]+)\](.*?)\[/attachment\]#ise' => " \$ this->bbcode_attachment(' \$ 1', ' \$ 2') " )),
2005-12-21 18:18:12 +00:00
'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') " )),
2005-11-28 18:38:49 +00:00
'url' => array ( 'bbcode_id' => 3 , 'regexp' => array ( '#\[url(=(.*))?\](.*)\[/url\]#iUe' => " \$ this->validate_url(' \$ 2', ' \$ 3') " )),
2004-11-06 14:22:04 +00:00
'img' => array ( 'bbcode_id' => 4 , 'regexp' => array ( '#\[img\](https?://)([a-z0-9\-\.,\?!%\*_:;~\\&$@/=\+]+)\[/img\]#ie' => " \$ this->bbcode_img(' \$ 1 \$ 2') " )),
2005-05-30 06:42:49 +00:00
'size' => array ( 'bbcode_id' => 5 , 'regexp' => array ( '#\[size=([\-\+]?[1-2]?[0-9])\](.*?)\[/size\]#ise' => " \$ this->bbcode_size(' \$ 1', ' \$ 2') " )),
'color' => array ( 'bbcode_id' => 6 , 'regexp' => array ( '!\[color=(#[0-9A-F]{6}|[a-z\-]+)\](.*?)\[/color\]!ise' => " \$ this->bbcode_color(' \$ 1', ' \$ 2') " )),
'u' => array ( 'bbcode_id' => 7 , 'regexp' => array ( '#\[u\](.*?)\[/u\]#ise' => " \$ this->bbcode_underline(' \$ 1') " )),
2005-04-20 20:36:10 +00:00
'list' => array ( 'bbcode_id' => 9 , 'regexp' => array ( '#\[list(=[a-z|0-9|(?:disc|circle|square))]+)?\].*\[/list\]#ise' => " \$ this->bbcode_parse_list(' \$ 0') " )),
2003-10-05 19:59:17 +00:00
'email' => array ( 'bbcode_id' => 10 , 'regexp' => array ( '#\[email=?(.*?)?\](.*?)\[/email\]#ise' => " \$ this->validate_email(' \$ 1', ' \$ 2') " )),
2004-11-06 14:22:04 +00:00
'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
);
2005-05-15 20:08:05 +00:00
$this -> parsed_items = array ( 'code' => 0 , 'quote' => 0 , 'attachment' => 0 , 'b' => 0 , 'i' => 0 , 'url' => 0 , 'img' => 0 , 'size' => 0 , 'color' => 0 , 'u' => 0 , 'list' => 0 , 'email' => 0 , 'flash' => 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 ();
2003-08-28 20:50:20 +00:00
$sql = ' SELECT bbcode_id , bbcode_tag , first_pass_match , first_pass_replace
FROM ' . BBCODES_TABLE ;
$result = $db -> sql_query ( $sql );
2003-04-23 22:55:53 +00:00
while ( $row = $db -> sql_fetchrow ( $result ))
{
$rowset [] = $row ;
}
}
2004-09-04 19:32:23 +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 (
2005-05-10 17:03:56 +00:00
'bbcode_id' => intval ( $row [ 'bbcode_id' ]),
'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
}
}
2005-05-15 20:08:05 +00:00
function check_bbcode ( $bbcode , & $in )
2005-05-10 17:03:56 +00:00
{
2005-09-25 14:29:05 +00:00
$in = str_replace ( " \r \n " , " \n " , str_replace ( '\"' , '"' , trim ( $in )));
2005-05-10 17:03:56 +00:00
if ( ! $in )
{
2005-05-15 20:08:05 +00:00
return false ;
2005-05-10 17:03:56 +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 ;
}
function bbcode_size ( $stx , $in )
{
if ( ! $this -> check_bbcode ( 'size' , $in ))
{
return '' ;
}
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
}
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
{
return '' ;
}
return '[color=' . $stx . ':' . $this -> bbcode_uid . ']' . $in . '[/color:' . $this -> bbcode_uid . ']' ;
}
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
{
return '' ;
}
return '[u:' . $this -> bbcode_uid . ']' . $in . '[/u:' . $this -> bbcode_uid . ']' ;
}
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
{
return '' ;
}
return '[b:' . $this -> bbcode_uid . ']' . $in . '[/b:' . $this -> bbcode_uid . ']' ;
}
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
{
return '' ;
}
return '[i:' . $this -> bbcode_uid . ']' . $in . '[/i:' . $this -> bbcode_uid . ']' ;
}
2004-11-06 14:22:04 +00:00
function bbcode_img ( $in )
{
2005-05-15 20:08:05 +00:00
if ( ! $this -> check_bbcode ( 'img' , $in ))
2005-05-10 17:03:56 +00:00
{
return '' ;
}
2005-05-15 20:08:05 +00:00
return '[img:' . $this -> bbcode_uid . ']' . $in . '[/img:' . $this -> bbcode_uid . ']' ;
2004-11-06 14:22:04 +00:00
}
function bbcode_flash ( $width , $height , $in )
{
2005-05-15 20:08:05 +00:00
if ( ! $this -> check_bbcode ( 'flash' , $in ))
2005-05-10 17:03:56 +00:00
{
return '' ;
}
2004-11-06 14:22:04 +00:00
2005-05-15 20:08:05 +00:00
return '[flash=' . $width . ',' . $height . ':' . $this -> bbcode_uid . ']' . $in . '[/flash:' . $this -> bbcode_uid . ']' ;
2004-11-06 14:22:04 +00:00
}
2004-05-02 13:06:57 +00:00
// Hardcode 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
{
return '' ;
}
2005-05-15 20:08:05 +00:00
return '[attachment=' . $stx . ':' . $this -> bbcode_uid . ']<!-- ia' . $stx . ' -->' . $in . '<!-- ia' . $stx . ' -->[/attachment:' . $this -> bbcode_uid . ']' ;
2004-02-08 18:02:17 +00:00
}
2003-04-16 22:31:20 +00:00
// Expects the argument to start right after the opening [code] tag and to end with [/code]
2003-04-20 20:30:01 +00:00
function bbcode_code ( $stx , $in )
2003-04-11 00:19:29 +00:00
{
2003-08-28 20:50:20 +00:00
// when using the /e modifier, preg_replace slashes double-quotes but does not
// seem to slash anything else
2005-05-10 17:03:56 +00:00
$in = str_replace ( " \r \n " , " \n " , str_replace ( '\"' , '"' , trim ( $in )));
2004-09-16 18:33:22 +00:00
2005-05-10 17:03:56 +00:00
if ( ! $in )
{
return '' ;
}
$this -> parsed_items [ 'code' ] ++ ;
2004-09-16 18:33:22 +00:00
// We remove the hardcoded elements from the code block here because it is not used in code blocks
2004-11-06 14:22:04 +00:00
// Having it here saves us one preg_replace per message containing [code] blocks
2004-11-14 17:12:07 +00:00
// Additionally, magic url parsing should go after parsing bbcodes, but for safety those are stripped out too...
2004-09-16 18:33:22 +00:00
$htm_match = array (
'#<!\-\- e \-\-><a href="mailto:(.*?)">.*?</a><!\-\- e \-\->#' ,
'#<!\-\- m \-\-><a href="(.*?)" target="_blank">.*?</a><!\-\- m \-\->#' ,
'#<!\-\- w \-\-><a href="http:\/\/(.*?)" target="_blank">.*?</a><!\-\- w \-\->#' ,
2005-01-15 18:49:54 +00:00
'#<!\-\- l \-\-><a href="(.*?)">.*?</a><!\-\- l \-\->#' ,
2005-03-21 23:10:11 +00:00
'#<!\-\- s(.*?) \-\-><img src="\{SMILIES_PATH\}\/.*? \/><!\-\- s\1 \-\->#' ,
2004-09-16 18:33:22 +00:00
'#<!\-\- h \-\-><(.*?)><!\-\- h \-\->#' ,
2005-04-30 14:24:13 +00:00
'#&\#([0-9]+);#' ,
2004-09-16 18:33:22 +00:00
);
2005-04-30 14:24:13 +00:00
$htm_replace = array ( '\1' , '\1' , '\1' , '\1' , '\1' , '<\1>' , '&#\1;' );
2004-09-16 18:33:22 +00:00
2003-04-11 00:19:29 +00:00
$out = '' ;
do
{
2003-04-16 22:31:20 +00:00
$pos = stripos ( $in , '[/code]' ) + 7 ;
2003-04-20 20:30:01 +00:00
$code = substr ( $in , 0 , $pos );
2003-04-11 00:19:29 +00:00
$in = substr ( $in , $pos );
2004-09-16 18:33:22 +00:00
2003-04-20 20:30:01 +00:00
// $code contains everything that was between code tags (including the ending tag) but we're trying to grab as much extra text as possible, as long as it does not contain open [code] tags
2003-04-11 00:19:29 +00:00
while ( $in )
{
2003-04-16 22:31:20 +00:00
$pos = stripos ( $in , '[/code]' ) + 7 ;
2003-04-20 20:30:01 +00:00
$buffer = substr ( $in , 0 , $pos );
2003-04-11 00:19:29 +00:00
2003-04-20 20:30:01 +00:00
if ( preg_match ( '#\[code(?:=([a-z]+))?\]#i' , $buffer ))
2003-04-11 00:19:29 +00:00
{
break ;
}
else
{
$in = substr ( $in , $pos );
2003-04-20 20:30:01 +00:00
$code .= $buffer ;
2003-04-11 00:19:29 +00:00
}
}
2003-04-20 20:30:01 +00:00
$code = substr ( $code , 0 , - 7 );
2004-09-04 19:32:23 +00:00
// $code = preg_replace('#^[\r\n]*(.*?)[\n\r\s\t]*$#s', '$1', $code);
2004-09-16 18:33:22 +00:00
$code = preg_replace ( $htm_match , $htm_replace , $code );
2003-04-20 20:30:01 +00:00
switch ( strtolower ( $stx ))
2003-04-16 22:31:20 +00:00
{
case 'php' :
2004-01-25 14:30:15 +00:00
$remove_tags = false ;
2003-05-22 01:32:27 +00:00
$str_from = array ( '<' , '>' );
$str_to = array ( '<' , '>' );
$code = str_replace ( $str_from , $str_to , $code );
2003-07-12 16:04:24 +00:00
if ( ! preg_match ( '/^\<\?.*?\?\>/is' , $code ))
2003-04-20 20:30:01 +00:00
{
2004-01-25 14:30:15 +00:00
$remove_tags = true ;
2003-04-20 20:30:01 +00:00
$code = " <?php $code ?> " ;
}
2003-05-22 01:32:27 +00:00
$conf = array ( 'highlight.bg' , 'highlight.comment' , 'highlight.default' , 'highlight.html' , 'highlight.keyword' , 'highlight.string' );
foreach ( $conf as $ini_var )
{
2003-07-04 17:19:00 +00:00
ini_set ( $ini_var , str_replace ( 'highlight.' , 'syntax' , $ini_var ));
2003-05-22 01:32:27 +00:00
}
2004-12-31 13:58:41 +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
2006-01-17 18:03:15 +00:00
$code = html_entity_decode ( $code );
2004-12-31 13:58:41 +00:00
2003-04-20 20:30:01 +00:00
ob_start ();
highlight_string ( $code );
$code = ob_get_contents ();
ob_end_clean ();
2004-11-14 17:12:07 +00:00
$str_from = array ( '<font color="syntax' , '</font>' , '<code>' , '</code>' , '[' , ']' , '.' , ':' );
$str_to = array ( '<span class="syntax' , '</span>' , '' , '' , '[' , ']' , '.' , ':' );
2003-05-22 01:32:27 +00:00
2003-04-20 20:30:01 +00:00
if ( $remove_tags )
{
2004-09-16 18:33:22 +00:00
$str_from [] = '<span class="syntaxdefault"><?php </span>' ;
2003-05-22 01:32:27 +00:00
$str_to [] = '' ;
2004-09-16 18:33:22 +00:00
$str_from [] = '<span class="syntaxdefault"><?php ' ;
2003-07-04 17:19:00 +00:00
$str_to [] = '<span class="syntaxdefault">' ;
2003-04-20 20:30:01 +00:00
}
2003-05-22 01:32:27 +00:00
$code = str_replace ( $str_from , $str_to , $code );
2003-08-28 20:50:20 +00:00
$code = preg_replace ( '#^(<span class="[a-z_]+">)\n?(.*?)\n?(</span>)$#is' , '$1$2$3' , $code );
if ( $remove_tags )
{
$code = preg_replace ( '#(<span class="[a-z]+">)?\?></span>#' , '' , $code );
}
$code = preg_replace ( '#^<span class="[a-z]+"><span class="([a-z]+)">(.*)</span></span>#s' , '<span class="$1">$2</span>' , $code );
$code = preg_replace ( '#(?:[\n\r\s\t]| )*</span>$#' , '</span>' , $code );
2003-04-20 20:30:01 +00:00
2003-07-12 16:04:24 +00:00
$out .= " [code= $stx : " . $this -> bbcode_uid . ']' . trim ( $code ) . '[/code:' . $this -> bbcode_uid . ']' ;
2003-05-22 01:32:27 +00:00
break ;
2003-04-20 20:30:01 +00:00
2003-04-16 22:31:20 +00:00
default :
2004-11-14 17:12:07 +00:00
$str_from = array ( '<' , '>' , '[' , ']' , '.' , ':' );
$str_to = array ( '<' , '>' , '[' , ']' , '.' , ':' );
2003-04-20 20:30:01 +00:00
2004-09-04 19:32:23 +00:00
$out .= '[code:' . $this -> bbcode_uid . ']' . str_replace ( $str_from , $str_to , $code ) . '[/code:' . $this -> bbcode_uid . ']' ;
2003-04-16 22:31:20 +00:00
}
2003-04-09 22:17:55 +00:00
2004-09-16 18:33:22 +00:00
if ( preg_match ( '#(.*?)\[code(?:=([a-z]+))?\](.+)#is' , $in , $m ))
2003-04-11 00:19:29 +00:00
{
2003-04-16 22:31:20 +00:00
$out .= $m [ 1 ];
2004-09-16 18:33:22 +00:00
$stx = $m [ 2 ];
$in = $m [ 3 ];
2003-04-11 00:19:29 +00:00
}
}
while ( $in );
return $out ;
}
2003-04-16 22:31:20 +00:00
// 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
{
return '' ;
}
2005-04-20 20:36:10 +00:00
$in = str_replace ( '\"' , '"' , $in );
$out = '[' ;
// Grab item_start with no item_end
$in = preg_replace ( '#\[\*\](.*?)(\[\/list\]|\[list(=?(?:[0-9]|[a-z]|))\]|\[\*\])#is' , '[*:' . $this -> bbcode_uid . ']\1[/*:m:' . $this -> bbcode_uid . ']\2' , $in );
// Grab them again as backreference
$in = preg_replace ( '#\[\*\](.*?)(\[\/list\]|\[list(=?(?:[0-9]|[a-z]|))\]|\[\*\])(^\[\/*\])#is' , '[*:' . $this -> bbcode_uid . ']\1[/*:m:' . $this -> bbcode_uid . ']\2' , $in );
// Grab end tag following start tag
$in = preg_replace ( '#\[\/\*:m:' . $this -> bbcode_uid . '\](\n|)\[\*\]#is' , '[/*:m:' . $this -> bbcode_uid . '][*:' . $this -> bbcode_uid . ']' , $in );
// Replace end tag
$in = preg_replace ( '#\[\/\*\]#i' , '[/*:' . $this -> bbcode_uid . ']' , $in );
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
2005-04-20 20:36:10 +00:00
$in = substr ( $in , 1 );
$list_end_tags = array ();
2003-04-11 00:19:29 +00:00
do
{
$pos = strlen ( $in );
for ( $i = 0 ; $i < strlen ( $tok ); ++ $i )
{
$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 );
$tok = $in { $pos };
2005-04-20 20:36:10 +00:00
$in = substr ( $in , $pos + 1 );
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
2004-09-16 18:33:22 +00:00
if ( $buffer == '/list' && sizeof ( $list_end_tags ))
2003-04-11 00:19:29 +00:00
{
2003-05-18 23:29:35 +00:00
$out .= array_pop ( $list_end_tags ) . ']' ;
2003-04-11 00:19:29 +00:00
$tok = '[' ;
}
2005-11-28 18:38:49 +00:00
else if ( preg_match ( '#list(=?(?:[0-9]|[a-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
2003-06-05 19:17:36 +00:00
if ( ! $m [ 1 ] || preg_match ( '/^(disc|square|circle)$/i' , $m [ 1 ]))
{
array_push ( $list_end_tags , '/list:u:' . $this -> bbcode_uid );
}
else
{
array_push ( $list_end_tags , '/list:o:' . $this -> bbcode_uid );
}
2003-04-11 00:19:29 +00:00
$out .= $buffer . ':' . $this -> bbcode_uid . ']' ;
$tok = '[' ;
}
else
{
$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 );
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 ;
}
2003-04-23 22:55:53 +00:00
// Expects the argument to start with a tag
function bbcode_quote ( $in )
{
2003-05-22 01:32:27 +00:00
global $config , $user ;
2005-05-10 17:03:56 +00:00
$in = trim ( $in );
if ( ! $in )
{
return '' ;
}
2003-04-23 22:55:53 +00:00
$tok = ']' ;
$out = '[' ;
2004-11-14 17:12:07 +00:00
// Add newline at the end and in front of each quote block to prevent parsing errors (urls, smilies, etc.)
2005-06-10 18:59:08 +00:00
$in = preg_replace ( array ( '#\[quote(=".*?")?\]([^\n])#is' , '#([^\n])\[\/quote\]#is' ), array ( " [quote \\ 1] \n \\ 2 " , " \\ 1 \n [/quote] " ), $in );
$in = preg_replace ( array ( '#\[quote(=".*?")?\]([^\n])#is' , '#([^\n])\[\/quote\]#is' ), array ( " [quote \\ 1] \n \\ 2 " , " \\ 1 \n [/quote] " ), $in );
2004-09-16 18:33:22 +00:00
2003-08-28 20:50:20 +00:00
$in = substr ( str_replace ( '\"' , '"' , $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 );
for ( $i = 0 ; $i < strlen ( $tok ); ++ $i )
{
$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 );
2003-04-23 22:55:53 +00:00
$tok = $in { $pos };
$in = substr ( $in , $pos + 1 );
if ( $tok == ']' )
{
2004-09-16 18:33:22 +00:00
if ( $buffer == '/quote' && sizeof ( $close_tags ))
2003-04-23 22:55:53 +00:00
{
2003-05-09 20:24:15 +00:00
// we have found a closing tag
2003-05-22 01:32:27 +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 = '' ;
2003-04-23 22:55:53 +00:00
}
2005-11-28 18:38:49 +00:00
else if ( preg_match ( '#^quote(?:="(.*?)")?$#is' , $buffer , $m ))
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
{
2003-08-28 20:50:20 +00:00
$username = preg_replace ( '#\[(?!b|i|u|color|url|email|/b|/i|/u|/color|/url|/email)#iU' , '[$1' , $m [ 1 ]);
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 )
{
if ( $tag { 0 } != '/' )
{
$end_tags [] = '/' . $tag ;
}
else
{
2003-06-05 19:17:36 +00:00
$end_tag = array_pop ( $end_tags );
if ( $end_tag != $tag )
{
2004-01-25 14:30:15 +00:00
$error = true ;
2003-06-05 19:17:36 +00:00
}
else
2003-05-25 19:17:39 +00:00
{
2004-01-25 14:30:15 +00:00
$error = false ;
2003-05-25 19:17:39 +00:00
}
}
}
if ( $error )
{
$username = str_replace ( '[' , '[' , str_replace ( ']' , ']' , $m [ 1 ]));
}
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
{
$out .= $buffer . $tok ;
$tok = ( $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 ;
}
2003-04-11 00:19:29 +00:00
function validate_email ( $var1 , $var2 )
{
2003-08-28 20:50:20 +00:00
$txt = stripslashes ( $var2 );
2004-01-25 14:30:15 +00:00
$email = ( $var1 ) ? stripslashes ( $var1 ) : stripslashes ( $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
2003-08-28 20:50:20 +00:00
if ( ! preg_match ( '!([a-z0-9]+[a-z0-9\-\._]*@(?:(?:[0-9]{1,3}\.){3,5}[0-9]{1,3}|[a-z0-9]+[a-z0-9\-\._]*\.[a-z]+))!i' , $email ))
{
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' ] ++ ;
2003-08-28 20:50:20 +00:00
if ( $var1 )
{
$retval = '[email=' . $email . ':' . $this -> bbcode_uid . ']' . $txt . '[/email:' . $this -> bbcode_uid . ']' ;
}
else
{
$retval = '[email:' . $this -> bbcode_uid . ']' . $email . '[/email:' . $this -> bbcode_uid . ']' ;
}
2005-05-15 20:08:05 +00:00
2003-04-11 00:19:29 +00:00
return $retval ;
}
function validate_url ( $var1 , $var2 )
2003-02-27 23:37:02 +00:00
{
global $config ;
2005-04-30 14:24:13 +00:00
2005-05-10 17:03:56 +00:00
$var1 = trim ( $var1 );
$var2 = trim ( $var2 );
2004-02-15 14:03:19 +00:00
$url = ( $var1 ) ? stripslashes ( $var1 ) : stripslashes ( $var2 );
$valid = false ;
2003-02-27 23:37:02 +00:00
2005-05-10 17:03:56 +00:00
if ( ! $url || ( $var1 && ! $var2 ))
{
return '' ;
}
2004-02-15 14:03:19 +00:00
$server_protocol = ( $config [ 'cookie_secure' ] ) ? 'https://' : 'http://' ;
$server_port = ( $config [ 'server_port' ] <> 80 ) ? ':' . trim ( $config [ 'server_port' ]) . '/' : '/' ;
2003-02-27 23:37:02 +00:00
2004-02-15 14:03:19 +00:00
// relative urls for this board
if ( preg_match ( '#' . $server_protocol . trim ( $config [ 'server_name' ]) . $server_port . preg_replace ( '/^\/?(.*?)(\/)?$/' , '$1' , trim ( $config [ 'script_path' ])) . '/([^ \t\n\r<"\']+)#i' , $url ) ||
preg_match ( '#([\w]+?://.*?[^ \t\n\r<"\']*)#i' , $url ) ||
preg_match ( '#(www\.[\w\-]+\.[\w\-.\~]+(?:/[^ \t\n\r<"\']*)?)#i' , $url ))
2003-08-28 17:32:18 +00:00
{
2004-02-15 14:03:19 +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' ] ++ ;
2004-02-15 14:03:19 +00:00
if ( ! preg_match ( '#^[\w]+?://.*?#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
}
2005-05-15 20:08:05 +00:00
return ( $var1 ) ? '[url=' . str_replace ( array ( ']' , '[' ), array ( ']' , '[' ), $url ) . ':' . $this -> bbcode_uid . ']' . stripslashes ( $var2 ) . '[/url:' . $this -> bbcode_uid . ']' : '[url:' . $this -> bbcode_uid . ']' . $url . '[/url:' . $this -> bbcode_uid . ']' ;
2003-02-27 23:37:02 +00:00
}
2004-02-15 14:03:19 +00:00
return '[url' . (( $var1 ) ? '=' . stripslashes ( $var1 ) : '' ) . ']' . stripslashes ( $var2 ) . '[/url]' ;
2003-02-27 23:37:02 +00:00
}
2004-09-04 19:32:23 +00:00
}
2005-04-09 12:26:45 +00:00
/**
* @ package phpBB3
* Main message parser for posting , pm , etc . takes raw message
* and parses it for attachments , html , bbcode and smilies
*/
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 ;
// Init - give message here or manually
function parse_message ( $message = '' )
{
// Init BBCode UID
$this -> bbcode_uid = substr ( md5 ( time ()), 0 , BBCODE_UID_LEN );
if ( $message )
{
$this -> message = $message ;
}
}
// Parse Message : public
2004-11-06 14:22:04 +00:00
function parse ( $allow_html , $allow_bbcode , $allow_magic_url , $allow_smilies , $allow_img_bbcode = true , $allow_flash_bbcode = true , $allow_quote_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' ;
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 ;
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(?)
// Transform \r\n and \r into \n
2005-05-10 17:03:56 +00:00
$match = array ( '#\r\n?#' , '#sid=[a-z0-9]*?&?#' , " #([ \n ][ \ s]+) { 3,}# " , '#(script|about|applet|activex|chrome):#i' );
$replace = array ( " \n " , '' , " \n \n " , " \\ 1: " );
2004-09-04 19:32:23 +00:00
$this -> message = preg_replace ( $match , $replace , trim ( $this -> message ));
2005-01-24 13:14:31 +00:00
// Message length check. -1 disables this check completely, even allows empty messsages.
2005-01-27 18:59:38 +00:00
if ( $config [ 'max_' . $mode . '_chars' ] != - 1 )
2004-09-04 19:32:23 +00:00
{
2005-01-24 13:14:31 +00:00
$msg_len = ( $mode == 'post' ) ? strlen ( $this -> message ) : strlen ( preg_replace ( '#\[\/?[a-z\*\+\-]+(=[\S]+)?\]#is' , ' ' , $this -> message ));
if ( ! $msg_len || ( $config [ 'max_' . $mode . '_chars' ] && $msg_len > $config [ 'max_' . $mode . '_chars' ]))
{
$this -> warn_msg [] = ( ! $msg_len ) ? $user -> lang [ 'TOO_FEW_CHARS' ] : $user -> lang [ 'TOO_MANY_CHARS' ];
return $this -> warn_msg ;
}
2004-09-04 19:32:23 +00:00
}
// Parse HTML
if ( $allow_html && $config [ 'allow_html_tags' ])
{
$this -> html ( $config [ 'allow_html_tags' ]);
}
2005-03-21 23:10:11 +00:00
// Parse smilies
2004-09-16 18:33:22 +00:00
if ( $allow_smilies )
{
2005-03-21 23:10:11 +00:00
$this -> smilies ( $config [ 'max_' . $mode . '_smilies' ]);
2004-09-16 18:33:22 +00:00
}
2004-11-06 14:22:04 +00:00
$num_urls = 0 ;
2004-09-04 19:32:23 +00:00
// Parse BBCode
if ( $allow_bbcode && strpos ( $this -> message , '[' ) !== false )
{
$this -> bbcode_init ();
$disallow = array ( 'img' , 'flash' , 'quote' );
foreach ( $disallow as $bool )
{
if ( ! $ { 'allow_' . $bool . '_bbcode' })
{
$this -> bbcodes [ $bool ][ 'disabled' ] = true ;
}
}
$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 )
{
$this -> magic_url ((( $config [ 'cookie_secure' ]) ? 'https://' : 'http://' ), $config [ 'server_name' ], $config [ 'server_port' ], $config [ 'script_path' ]);
if ( $config [ 'max_' . $mode . '_urls' ])
{
$num_urls += preg_match_all ( '#\<!-- (l|m|w|e) --\>.*?\<!-- \1 --\>#' , $this -> message , $matches );
}
}
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' ]);
2004-12-19 17:59:15 +00:00
return $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' ;
return ;
}
// Formatting text for display
function format_display ( $allow_html , $allow_bbcode , $allow_magic_url , $allow_smilies , $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 ;
}
if ( $this -> message_status == 'plain' )
{
// Force updating message - of course.
$this -> parse ( $allow_html , $allow_bbcode , $allow_magic_url , $allow_smilies , $this -> allow_img_bbcode , $this -> allow_flash_bbcode , $this -> allow_quote_bbcode , true );
}
// 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 );
}
2005-03-21 23:10:11 +00:00
$this -> message = smiley_text ( $this -> message , ! $allow_smilies );
2004-09-04 19:32:23 +00:00
// Replace naughty words such as farty pants
$this -> message = str_replace ( " \n " , '<br />' , censor_text ( $this -> message ));
if ( ! $update_this_message )
{
unset ( $this -> message );
$this -> message = $tmp_message ;
return $return_message ;
}
$this -> message_status = 'display' ;
return ;
}
// Decode message to be placed back into form box
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' ;
}
// Parse HTML
function html ( $allowed_tags )
{
// If $allow_html is true then "allowed_tags" are converted back from entity
// form, others remain
$allowed_tags = split ( ',' , $allowed_tags );
if ( sizeof ( $allowed_tags ))
{
2004-09-16 18:33:22 +00:00
$this -> message = preg_replace ( '#<(\/?)(' . str_replace ( '*' , '.*?' , implode ( '|' , $allowed_tags )) . ')>#is' , '<!-- h --><$1$2><!-- h -->' , $this -> message );
2004-09-04 19:32:23 +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
function magic_url ( $server_protocol , $server_name , $server_port , $script_path )
{
2004-11-10 14:15:16 +00:00
static $match ;
static $replace ;
2006-02-21 10:32:19 +00:00
2004-09-04 19:32:23 +00:00
$server_port = ( $server_port <> 80 ) ? ':' . trim ( $server_port ) . '/' : '/' ;
2004-11-14 17:12:07 +00:00
if ( ! is_array ( $match ))
2004-11-10 14:15:16 +00:00
{
2004-11-14 17:12:07 +00:00
$match = $replace = array ();
2004-11-10 14:15:16 +00:00
// Be sure to not let the matches cross over. ;)
2006-02-21 10:32:19 +00:00
2004-11-10 14:15:16 +00:00
// relative urls for this board
2006-02-21 10:32:19 +00:00
$match [] = '#(^|[\n ]|\()(' . preg_quote ( $server_protocol . trim ( $server_name ) . $server_port . preg_replace ( '/^\/?(.*?)(\/)?$/' , '$1' , trim ( $script_path )), '#' ) . ')/([^ \t\n\r<"\'\)&]+|&(?!lt;))*)#i' ;
2005-01-15 18:49:54 +00:00
$replace [] = '$1<!-- l --><a href="$2/$3">$3</a><!-- l -->' ;
2006-02-21 10:32:19 +00:00
2004-11-10 14:15:16 +00:00
// matches a xxxx://aaaaa.bbb.cccc. ...
2006-02-21 10:32:19 +00:00
$match [] = '#(^|[\n ]|\()([\w]+:/{2}.*?([^ \t\n\r<"\'\)&]+|&(?!lt;))*)#ie' ;
2005-01-05 06:35:44 +00:00
$replace [] = " ' \$ 1<!-- m --><a href= \" \$ 2 \" target= \" _blank \" >' . ((strlen(' \$ 2') > 55) ? substr(str_replace('&', '&', ' \$ 2'), 0, 39) . ' ... ' . substr(str_replace('&', '&', ' \$ 2'), -10) : ' \$ 2') . '</a><!-- m -->' " ;
2006-02-21 10:32:19 +00:00
2004-11-10 14:15:16 +00:00
// matches a "www.xxxx.yyyy[/zzzz]" kinda lazy URL thing
2006-02-21 10:32:19 +00:00
$match [] = '#(^|[\n ]|\()(w{3}\.[\w\-]+\.[\w\-.\~]+(?:[^ \t\n\r<"\'\)&]+|&(?!lt;))*)#ie' ;
2005-01-05 06:35:44 +00:00
$replace [] = " ' \$ 1<!-- w --><a href= \" http:// \$ 2 \" target= \" _blank \" >' . ((strlen(' \$ 2') > 55) ? substr(str_replace('&', '&', ' \$ 2'), 0, 39) . ' ... ' . substr(str_replace('&', '&', ' \$ 2'), -10) : ' \$ 2') . '</a><!-- w -->' " ;
2006-02-21 10:32:19 +00:00
2004-11-10 14:15:16 +00:00
// matches an email@domain type address at the start of a line, or after a space.
2006-02-21 10:32:19 +00:00
$match [] = '#(^|[\n ]|\()([a-z0-9&\-_.]+?@[\w\-]+\.(?:[\w\-\.]+\.)?[\w]+)#ie' ;
2004-11-10 14:15:16 +00:00
$replace [] = " ' \$ 1<!-- e --><a href= \" mailto: \$ 2 \" >' . ((strlen(' \$ 2') > 55) ? substr(' \$ 2', 0, 39) . ' ... ' . substr(' \$ 2', -10) : ' \$ 2') . '</a><!-- e -->' " ;
}
2006-02-21 10:32:19 +00:00
$this -> message = preg_replace ( $match , $replace , $this -> message );
2004-09-04 19:32:23 +00:00
}
2005-03-21 23:10:11 +00:00
// Parse Smilies
function smilies ( $max_smilies = 0 )
2004-09-04 19:32:23 +00:00
{
global $db , $user , $phpbb_root_path ;
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
// NOTE: There is a memory leak in this block somewhere :\
// 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
{
2004-11-10 14:15:16 +00:00
// NOTE: obtain_* function? chaching the table contents?
// For now setting the ttl to 10 minutes
switch ( 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' :
2004-11-10 14:15:16 +00:00
$sql = ' SELECT *
FROM ' . SMILIES_TABLE . '
ORDER BY LEN ( code ) DESC ' ;
break ;
// LENGTH supported by MySQL, IBM DB2, Oracle and Access for sure...
default :
$sql = ' SELECT *
FROM ' . SMILIES_TABLE . '
ORDER BY LENGTH ( code ) DESC ' ;
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
2004-11-10 14:15:16 +00:00
if ( $row = $db -> sql_fetchrow ( $result ))
{
$match = $replace = array ();
do
{
// (assertion)
$match [] = '#(?<=^|[\n ]|\.)' . preg_quote ( $row [ 'code' ], '#' ) . '#' ;
2005-04-20 20:36:10 +00:00
$replace [] = '<!-- s' . $row [ 'code' ] . ' --><img src="{SMILIES_PATH}/' . $row [ 'smiley_url' ] . '" border="0" alt="' . $row [ 'emotion' ] . '" title="' . $row [ 'emotion' ] . '" /><!-- s' . $row [ 'code' ] . ' -->' ;
2004-11-10 14:15:16 +00:00
}
while ( $row = $db -> sql_fetchrow ( $result ));
}
else
{
$match = $replace = array ();
}
$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 )
{
$num_matches = preg_match_all ( '#' . str_replace ( '#' , '' , implode ( '|' , $match )) . '#' , $this -> message , $matches );
unset ( $matches );
if ( $num_matches !== false && $num_matches > $max_smilies )
{
2004-11-06 14:22:04 +00:00
$this -> warn_msg [] = sprintf ( $user -> lang [ 'TOO_MANY_SMILIES' ], $max_smilies );
2004-09-04 19:32:23 +00:00
return ;
}
}
$this -> message = trim ( preg_replace ( $match , $replace , $this -> message ));
}
}
2003-02-27 23:37:02 +00:00
2004-02-15 14:03:19 +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
{
2005-03-21 23:10:11 +00:00
global $config , $auth , $user , $phpbb_root_path ;
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 );
2005-03-21 23:10:11 +00:00
$this -> filename_data [ 'filecomment' ] = request_var ( 'filecomment' , '' , true );
$upload_file = ( isset ( $_FILES [ $form_name ]) && $_FILES [ $form_name ][ 'name' ] != 'none' && trim ( $_FILES [ $form_name ][ 'name' ])) ? true : false ;
2004-01-25 14:30:15 +00:00
$add_file = ( isset ( $_POST [ 'add_file' ]));
$delete_file = ( isset ( $_POST [ 'delete_file' ]));
$edit_comment = ( isset ( $_POST [ 'edit_comment' ]));
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
{
2004-05-02 13:06:57 +00:00
if ( $num_attachments < $cfg [ 'max_attachments' ] || $auth -> acl_gets ( 'm_' , 'a_' ))
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
{
2003-05-01 18:24:18 +00:00
$new_entry = array (
2005-03-21 23:10:11 +00:00
'physical_filename' => $filedata [ 'physical_filename' ],
2003-06-20 17:20:22 +00:00
'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' ],
2003-10-09 14:49:21 +00:00
'attach_id' => 0 ,
2003-06-20 17:20:22 +00:00
'thumbnail' => $filedata [ 'thumbnail' ]
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 );
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 )
{
2003-09-24 19:41:14 +00:00
$index = ( int ) key ( $_POST [ 'delete_file' ]);
// delete selected attachment
2003-10-09 14:49:21 +00:00
if ( ! $this -> attachment_data [ $index ][ 'attach_id' ])
2003-03-22 15:48:46 +00:00
{
2003-09-24 19:41:14 +00:00
phpbb_unlink ( $this -> attachment_data [ $index ][ 'physical_filename' ], 'file' );
2003-03-22 15:48:46 +00:00
2003-09-24 19:41:14 +00:00
if ( $this -> attachment_data [ $index ][ 'thumbnail' ])
2003-05-01 18:24:18 +00:00
{
2003-11-16 21:53:56 +00:00
phpbb_unlink ( $this -> attachment_data [ $index ][ 'physical_filename' ], 'thumbnail' );
2003-05-01 18:24:18 +00:00
}
2003-09-24 19:41:14 +00:00
}
else
{
2006-02-22 21:42:26 +00:00
if ( ! function_exists ( 'delete_attachments' ))
{
include_once ( $phpbb_root_path . 'includes/functions_admin.' . $phpEx );
}
2003-11-04 22:05:38 +00:00
delete_attachments ( 'attach' , array ( intval ( $this -> attachment_data [ $index ][ 'attach_id' ])));
2003-03-22 15:48:46 +00:00
}
2003-05-01 18:24:18 +00:00
2003-09-24 19:41:14 +00:00
unset ( $this -> attachment_data [ $index ]);
2004-02-08 18:02:17 +00:00
$this -> message = preg_replace ( '#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e' , " ( \\ 1 == \$ index) ? '' : (( \\ 1 > \$ index) ? '[attachment=' . ( \\ 1 - 1) . '] \\ 2[/attachment]' : ' \\ 0') " , $this -> message );
2003-09-24 19:41:14 +00:00
// Reindex Array
2003-11-23 22:25:46 +00:00
$this -> attachment_data = array_values ( $this -> attachment_data );
2003-03-22 15:48:46 +00:00
}
2003-06-20 17:20:22 +00:00
else if ( $edit_comment || $add_file || $preview )
2003-03-22 15:48:46 +00:00
{
if ( $edit_comment )
{
2005-05-15 20:08:05 +00:00
$actual_comment_list = request_var ( 'comment_list' , array ( '' ));
2003-03-22 15:48:46 +00:00
2003-05-01 18:24:18 +00:00
foreach ( $actual_comment_list as $index => $entry )
2003-03-22 15:48:46 +00:00
{
2003-10-12 09:13:04 +00:00
$this -> attachment_data [ $index ][ 'comment' ] = preg_replace ( '#&(\#[0-9]+;)#' , '&\1' , $entry );
2003-03-22 15:48:46 +00:00
}
}
2005-03-21 23:10:11 +00:00
if (( $add_file || $preview ) && $upload_file )
2003-03-22 15:48:46 +00:00
{
2004-05-02 13:06:57 +00:00
if ( $num_attachments < $cfg [ 'max_attachments' ] || $auth -> acl_gets ( 'm_' , 'a_' ))
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 = array_merge ( $error , $filedata [ 'error' ]);
2003-03-22 15:48:46 +00:00
2004-09-16 18:33:22 +00:00
if ( ! sizeof ( $error ))
2003-03-22 15:48:46 +00:00
{
2003-05-01 18:24:18 +00:00
$new_entry = array (
2005-03-21 23:10:11 +00:00
'physical_filename' => $filedata [ 'physical_filename' ],
2003-06-20 17:20:22 +00:00
'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' ],
2003-10-09 14:49:21 +00:00
'attach_id' => 0 ,
2003-06-20 17:20:22 +00:00
'thumbnail' => $filedata [ 'thumbnail' ]
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 );
2003-05-01 18:24:18 +00:00
$this -> filename_data [ 'filecomment' ] = '' ;
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
}
}
}
}
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
}
2004-05-02 13:06:57 +00:00
// Get Attachment Data
function get_submitted_attachment_data ()
{
2005-03-21 23:10:11 +00:00
$this -> filename_data [ 'filecomment' ] = request_var ( 'filecomment' , '' , true );
2004-05-02 13:06:57 +00:00
$this -> attachment_data = ( isset ( $_POST [ 'attachment_data' ])) ? $_POST [ 'attachment_data' ] : array ();
//
$data_prepare = array ( 'physical_filename' => 's' , 'real_filename' => 's' , 'comment' => 's' , 'extension' => 's' , 'mimetype' => 's' ,
'filesize' => 'i' , 'filetime' => 'i' , 'attach_id' => 'i' , 'thumbnail' => 'i' );
foreach ( $this -> attachment_data as $pos => $var_ary )
{
foreach ( $data_prepare as $var => $type )
{
if ( $type == 's' )
{
2005-05-15 20:08:05 +00:00
$this -> attachment_data [ $pos ][ $var ] = trim ( htmlspecialchars ( str_replace ( array ( " \r \n " , " \r " , '\xFF' ), array ( " \n " , " \n " , ' ' ), stripslashes ( $this -> attachment_data [ $pos ][ $var ]))));
2004-05-02 13:06:57 +00:00
}
else
{
$this -> attachment_data [ $pos ][ $var ] = ( int ) $this -> attachment_data [ $pos ][ $var ];
}
}
}
}
2003-03-12 14:21:57 +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' ];
$bbcode_bitfield = $this -> bbcode_bitfield ;
2003-09-15 21:04:18 +00:00
2004-09-05 15:45:50 +00:00
$poll [ 'poll_option_text' ] = $this -> parse ( $poll [ 'enable_html' ], $poll [ 'enable_bbcode' ], $poll [ 'enable_urls' ], $poll [ 'enable_smilies' ], $poll [ 'img_status' ], false , false , false );
$this -> bbcode_bitfield |= $bbcode_bitfield ;
$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' ];
$bbcode_bitfield = $this -> bbcode_bitfield ;
2003-06-29 10:59:36 +00:00
2004-09-05 15:45:50 +00:00
$poll [ 'poll_title' ] = $this -> parse ( $poll [ 'enable_html' ], $poll [ 'enable_bbcode' ], $poll [ 'enable_urls' ], $poll [ 'enable_smilies' ], $poll [ 'img_status' ], false , false , false );
$this -> bbcode_bitfield |= $bbcode_bitfield ;
$this -> message = $tmp_message ;
unset ( $tmp_message );
$poll [ 'poll_options' ] = explode ( " \n " , trim ( $poll [ 'poll_option_text' ]));
$poll [ 'poll_options_size' ] = sizeof ( $poll [ 'poll_options' ]);
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
if ( ! $poll [ 'poll_title' ] && $poll [ 'poll_options_size' ])
{
$this -> warn_msg [] = $user -> lang [ 'NO_POLL_TITLE' ];
}
$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
}
?>