root/ip/trunk/includes/bbcode.php

144155
84
=================================
84
=================================
85
85
86
$text = $bbcode->acronym_pass($text);
86
$text = $bbcode->acronym_pass($text);
87
$text = autolink_text($text, $forum_id);
87
$text = $bbcode->autolink_text($text, $forum_id);
88
====================
88
====================
89
89
90
90
...
...
169
	array_merge(array(create_server_url()), $urls_local);
169
	array_merge(array(create_server_url()), $urls_local);
170
}
170
}
171
171
172
// Need to initialize the random numbers only ONCE
173
mt_srand((double) microtime() * 1000000);
174
172
class bbcode
175
class bbcode
173
{
176
{
174
	var $text = '';
177
	var $text = '';
...
...
945
			{
948
			{
946
				if ($tag === 'attachment')
949
				if ($tag === 'attachment')
947
				{
950
				{
951
					if (!function_exists('get_attachment_details'))
952
					{
953
						include_once(IP_ROOT_PATH . 'includes/functions_bbcode.' . PHP_EXT);
954
					}
948
					$is_auth_ary = auth(AUTH_READ, AUTH_LIST_ALL, $userdata);
955
					$is_auth_ary = auth(AUTH_READ, AUTH_LIST_ALL, $userdata);
949
					$is_download_auth_ary = auth(AUTH_DOWNLOAD, AUTH_LIST_ALL, $userdata);
956
					$is_download_auth_ary = auth(AUTH_DOWNLOAD, AUTH_LIST_ALL, $userdata);
950
					$attachment_details = get_attachment_details($params['id']);
957
					$attachment_details = get_attachment_details($params['id']);
...
...
955
				}
962
				}
956
				else
963
				else
957
				{
964
				{
965
					if (!function_exists('get_download_details'))
966
					{
967
						include_once(IP_ROOT_PATH . 'includes/functions_bbcode.' . PHP_EXT);
968
					}
958
					$attachment_details = get_download_details($params['id']);
969
					$attachment_details = get_download_details($params['id']);
959
					$errored = ($attachment_details == false) ? true : false;
970
					$errored = ($attachment_details == false) ? true : false;
960
				}
971
				}
...
...
3322
				$id ? ':' . $id : '',
3333
				$id ? ':' . $id : '',
3323
				'code:1]',
3334
				'code:1]',
3324
				'list:o]',
3335
				'list:o]',
3325
				);
3336
			);
3326
			$replace = array(
3337
			$replace = array(
3327
				'',
3338
				'',
3328
				'code]',
3339
				'code]',
3329
				'list]',
3340
				'list]',
3330
				);
3341
			);
3331
			$text = str_replace($search, $replace, $text);
3342
			$text = str_replace($search, $replace, $text);
3332
			// We need this after having removed bbcode_uid... but don't know why
3343
			// We need this after having removed bbcode_uid... but don't know why
3333
			$text = undo_htmlspecialchars($text);
3344
			$text = $this->undo_htmlspecialchars($text);
3334
			/*
3345
			/*
3335
			if($id)
3346
			if($id)
3336
			{
3347
			{
3337
				$text = undo_htmlspecialchars($text);
3348
				$text = $this->undo_htmlspecialchars($text);
3338
			}
3349
			}
3339
			*/
3350
			*/
3340
		}
3351
		}
...
...
3779
	}
3790
	}
3780
3791
3781
	/*
3792
	/*
3793
	* Undo HTML special chars
3794
	*/
3795
	function undo_htmlspecialchars($input, $full_undo = false)
3796
	{
3797
		if($full_undo)
3798
		{
3799
			$input = str_replace(' ', '', $input);
3800
		}
3801
		$input = preg_replace("/>/i", ">", $input);
3802
		$input = preg_replace("/&lt;/i", "<", $input);
3803
		$input = preg_replace("/&quot;/i", "\"", $input);
3804
		$input = preg_replace("/&amp;/i", "&", $input);
3805
3806
		if($full_undo)
3807
		{
3808
			if(preg_match_all('/&\#([0-9]+);/', $input, $matches) && sizeof($matches))
3809
			{
3810
				$list = array();
3811
				for($i = 0; $i < sizeof($matches[1]); $i++)
3812
				{
3813
					$list[$matches[1][$i]] = true;
3814
				}
3815
				$search = array();
3816
				$replace = array();
3817
				foreach($list as $var => $value)
3818
				{
3819
					$search[] = '&#' . $var . ';';
3820
					$replace[] = chr($var);
3821
				}
3822
				$input = str_replace($search, $replace, $input);
3823
			}
3824
		}
3825
3826
		return $input;
3827
	}
3828
3829
	/*
3782
	* This function will strip common BBCodes tags, but some of them will be left there (such as CODE or QUOTE)
3830
	* This function will strip common BBCodes tags, but some of them will be left there (such as CODE or QUOTE)
3783
	*/
3831
	*/
3784
	function bbcode_killer($text, $id = false)
3832
	function bbcode_killer($text, $id = false)
...
...
4000
		return $text;
4048
		return $text;
4001
	}
4049
	}
4002
4050
4003
}
4051
	// Autolinks - BEGIN
4004
4052
	//
4005
$bbcode = new bbcode();
4053
	// Obtain list of autolink words and build preg style replacement arrays for use by the calling script, note that the vars are passed as references this just makes it easier to return both sets of arrays
4006
4054
	//
4007
if (defined('SMILIES_TABLE'))
4055
	function obtain_autolinks_list($forum_id)
4008
{
4009
	$bbcode->allowed_smilies = array();
4010
	$bbcode->allowed_smilies = $cache->obtain_smileys(false);
4011
}
4012
4013
// Need to initialize the random numbers only ONCE
4014
mt_srand((double) microtime() * 1000000);
4015
4016
/**
4017
* For display of custom parsed text on user-facing pages
4018
* Expects $text to be the value directly from the database (stored value)
4019
*/
4020
function generate_text_for_display($text, $only_smileys = false, $censor = true, $acro_autolinks = false, $forum_id = '999999')
4021
{
4022
	global $bbcode, $config, $userdata;
4023
4024
	if (empty($text))
4025
	{
4056
	{
4026
		return '';
4057
		global $db;
4027
	}
4028
4058
4029
	if (defined('IS_ICYPHOENIX') && $censor)
4059
		$where = ($forum_id) ? ' WHERE link_forum = 0 OR link_forum IN (' . $forum_id . ')' : ' WHERE link_forum = -1';
4030
	{
4060
		$sql = "SELECT * FROM " . AUTOLINKS . $where;
4031
		$text = censor_text($text);
4061
		$result = $db->sql_query($sql, 0, 'autolinks_', TOPICS_CACHE_FOLDER);
4032
	}
4033
4062
4034
	if (!class_exists('bbcode'))
4063
		$autolinks = array();
4035
	{
4064
		while($row = $db->sql_fetchrow($result))
4036
		include(IP_ROOT_PATH . 'includes/bbcode.' . PHP_EXT);
4037
	}
4038
4039
	if (empty($bbcode))
4040
	{
4041
		$bbcode = new bbcode();
4042
		if (!$userdata['session_logged_in'])
4043
		{
4065
		{
4044
			$userdata['user_allowhtml'] = $config['allow_html'] ? true : false;
4066
			// Munge word boundaries to stop autolinks from linking to
4045
			$userdata['user_allowbbcode'] = $config['allow_bbcode'] ? true : false;
4067
			// themselves or other autolinks in step 2 in the function below.
4046
			$userdata['user_allowsmile'] = $config['allow_smilies'] ? true : false;
4068
			$row['link_url'] = preg_replace('/(\b)/', '\\1ALSPACEHOLDER', $row['link_url']);
4047
		}
4069
			$row['link_comment'] = preg_replace('/(\b)/', '\\1ALSPACEHOLDER', $row['link_comment']);
4048
		$bbcode->allow_html = ($userdata['user_allowhtml'] && $config['allow_html']) ? true : false;
4049
		$bbcode->allow_bbcode = ($userdata['user_allowbbcode'] && $config['allow_bbcode']) ? true : false;
4050
		$bbcode->allow_smilies = ($userdata['user_allowsmile'] && $config['allow_smilies']) ? true : false;
4051
	}
4052
4070
4053
	if ($only_smileys)
4071
			if($row['link_style'])
4054
	{
4055
		$text = $bbcode->parse_only_smilies($text);
4056
	}
4057
	else
4058
	{
4059
		$text = $bbcode->parse($text);
4060
		if (defined('IS_ICYPHOENIX') && $acro_autolinks)
4061
		{
4062
			$text = $bbcode->acronym_pass($text);
4063
			$text = autolink_text($text, $forum_id);
4064
		}
4065
	}
4066
4067
	return $text;
4068
}
4069
4070
/*
4071
* Generate bbcode uid
4072
*/
4073
function make_bbcode_uid()
4074
{
4075
	// Unique ID for this message..
4076
	$uid = unique_id();
4077
	$uid = substr($uid, 0, BBCODE_UID_LEN);
4078
	return $uid;
4079
}
4080
4081
/*
4082
* Generate a single row of smileys
4083
* Moved here from functions_post to optimize viewtopic and remove the full include of functions_post
4084
*/
4085
if (defined('SMILIES_TABLE'))
4086
{
4087
	function generate_smilies_row()
4088
	{
4089
		global $db, $cache, $config, $template;
4090
		if (defined('IN_PA_POSTING'))
4091
		{
4092
			global $pafiledb_template;
4093
		}
4094
4095
		$max_smilies = (!empty($config['smilie_single_row']) ? intval($config['smilie_single_row']) : 20);
4096
4097
		$sql = "SELECT emoticon, code, smile_url FROM " . SMILIES_TABLE . " GROUP BY smile_url ORDER BY smilies_order LIMIT " . $max_smilies;
4098
		$result = $db->sql_query($sql, 0, 'smileys_');
4099
4100
		$host = extract_current_hostname();
4101
4102
		$orig = array();
4103
		$repl = array();
4104
		while ($row = $db->sql_fetchrow($result))
4105
		{
4106
			$parsing_template = array(
4107
				'CODE' => $row['code'],
4108
				'URL' => 'http://' . $host . $config['script_path'] . $config['smilies_path'] . '/' . $row['smile_url'],
4109
				'DESC' => htmlspecialchars($row['emoticon'])
4110
			);
4111
			if (defined('IN_PA_POSTING'))
4112
			{
4072
			{
4113
				$pafiledb_template->assign_block_vars('smilies', $parsing_template);
4073
				$row['link_style'] = preg_replace('/(\b)/', '\\1ALSPACEHOLDER', $row['link_style']);
4074
				$style = ' style="' . htmlspecialchars($row['link_style']) . '" ';
4114
			}
4075
			}
4115
			else
4076
			else
4116
			{
4077
			{
4117
				$template->assign_block_vars('smilies', $parsing_template);
4078
				$style = ' ';
4118
			}
4079
			}
4119
		}
4080
			$autolinks['match'][] = '/(?<![\/\w@\.:-])(?!\.\w)(' . phpbb_preg_quote($row['link_keyword'], '/'). ')(?![\/\w@:-])(?!\.\w)/i';
4120
		$db->sql_freeresult($result);
4081
			if($row['link_int'])
4121
	}
4122
}
4123
4124
/*
4125
* Undo HTML special chars
4126
*/
4127
function undo_htmlspecialchars($input, $full_undo = false)
4128
{
4129
	if($full_undo)
4130
	{
4131
		$input = str_replace('&nbsp;', '', $input);
4132
	}
4133
	$input = preg_replace("/&gt;/i", ">", $input);
4134
	$input = preg_replace("/&lt;/i", "<", $input);
4135
	$input = preg_replace("/&quot;/i", "\"", $input);
4136
	$input = preg_replace("/&amp;/i", "&", $input);
4137
4138
	if($full_undo)
4139
	{
4140
		if(preg_match_all('/&\#([0-9]+);/', $input, $matches) && sizeof($matches))
4141
		{
4142
			$list = array();
4143
			for($i = 0; $i < sizeof($matches[1]); $i++)
4144
			{
4082
			{
4145
				$list[$matches[1][$i]] = true;
4083
				$autolinks['replace'][] = '<a href="' . append_sid(htmlspecialchars($row['link_url'])) . '" target="_self"' . $style . 'title="' . htmlspecialchars($row['link_comment']) . '">' . htmlspecialchars($row['link_title']) . '</a>';
4146
			}
4084
			}
4147
			$search = array();
4085
			else
4148
			$replace = array();
4149
			foreach($list as $var => $value)
4150
			{
4086
			{
4151
				$search[] = '&#' . $var . ';';
4087
				$autolinks['replace'][] = '<a href="' . htmlspecialchars($row['link_url']) . '" target="_blank"' . $style . 'title="' . htmlspecialchars($row['link_comment']) . '">' . htmlspecialchars($row['link_title']) . '</a>';
4152
				$replace[] = chr($var);
4153
			}
4088
			}
4154
			$input = str_replace($search, $replace, $input);
4155
		}
4089
		}
4090
		$db->sql_freeresult($result);
4091
4092
		return $autolinks;
4156
	}
4093
	}
4157
4094
4158
	return $input;
4095
	/**
4159
}
4096
	* Autolinks
4160
4097
	* Original Author - Jim McDonald - Edited by Mighty Gorgon
4161
/*
4098
	*/
4162
* Make a link clickable
4099
	function autolink_text($text, $forum_id = '')
4163
*/
4164
function make_clickable($text)
4165
{
4166
	$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);
4167
	$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1&#058;", $text);
4168
4169
	// pad it with a space so we can match things at the start of the 1st line.
4170
	$ret = ' ' . $text;
4171
4172
	// matches an "xxxx://yyyy" URL at the start of a line, or after a space.
4173
	// xxxx can only be alpha characters.
4174
	// yyyy is anything up to the first space, newline, comma, double quote or <
4175
	$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
4176
4177
	// matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing
4178
	// Must contain at least 2 dots. xxxx contains either alphanum, or "-"
4179
	// zzzz is optional.. will contain everything up to the first space, newline,
4180
	// comma, double quote or <.
4181
	$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
4182
4183
4184
	// matches an email@domain type address at the start of a line, or after a space.
4185
	// Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
4186
	$ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
4187
4188
	// Remove our padding..
4189
	$ret = substr($ret, 1);
4190
4191
	return($ret);
4192
}
4193
4194
// Autolinks - BEGIN
4195
//
4196
// Obtain list of autolink words and build preg style replacement arrays for use by the calling script, note that the vars are passed as references this just makes it easier to return both sets of arrays
4197
//
4198
function obtain_autolinks_list($forum_id)
4199
{
4200
	global $db;
4201
4202
	$where = ($forum_id) ? ' WHERE link_forum = 0 OR link_forum IN (' . $forum_id . ')' : ' WHERE link_forum = -1';
4203
	$sql = "SELECT * FROM " . AUTOLINKS . $where;
4204
	$result = $db->sql_query($sql, 0, 'autolinks_', TOPICS_CACHE_FOLDER);
4205
4206
	$autolinks = array();
4207
	while($row = $db->sql_fetchrow($result))
4208
	{
4100
	{
4209
		// Munge word boundaries to stop autolinks from linking to
4101
		static $autolinks;
4210
		// themselves or other autolinks in step 2 in the function below.
4211
		$row['link_url'] = preg_replace('/(\b)/', '\\1ALSPACEHOLDER', $row['link_url']);
4212
		$row['link_comment'] = preg_replace('/(\b)/', '\\1ALSPACEHOLDER', $row['link_comment']);
4213
4102
4214
		if($row['link_style'])
4103
		if (empty($text))
4215
		{
4104
		{
4216
			$row['link_style'] = preg_replace('/(\b)/', '\\1ALSPACEHOLDER', $row['link_style']);
4105
			return $text;
4217
			$style = ' style="' . htmlspecialchars($row['link_style']) . '" ';
4218
		}
4106
		}
4219
		else
4107
4108
		if (!isset($autolinks) || !is_array($autolinks))
4220
		{
4109
		{
4221
			$style = ' ';
4110
			$autolinks = $this->obtain_autolinks_list($forum_id);
4222
		}
4111
		}
4223
		$autolinks['match'][] = '/(?<![\/\w@\.:-])(?!\.\w)(' . phpbb_preg_quote($row['link_keyword'], '/'). ')(?![\/\w@:-])(?!\.\w)/i';
4112
4224
		if($row['link_int'])
4113
		if (sizeof($autolinks))
4225
		{
4114
		{
4226
			$autolinks['replace'][] = '<a href="' . append_sid(htmlspecialchars($row['link_url'])) . '" target="_self"' . $style . 'title="' . htmlspecialchars($row['link_comment']) . '">' . htmlspecialchars($row['link_title']) . '</a>';
4115
			global $config;
4227
		}
4116
			// Step 1 - move all tags out of the text and replace them with placeholders
4228
		else
4117
			preg_match_all('/(<a\s+.*?\/a>|<[^>]+>)/i', $text, $matches);
4229
		{
4118
			$matchnum = sizeof($matches[1]);
4230
			$autolinks['replace'][] = '<a href="' . htmlspecialchars($row['link_url']) . '" target="_blank"' . $style . 'title="' . htmlspecialchars($row['link_comment']) . '">' . htmlspecialchars($row['link_title']) . '</a>';
4119
			for($i = 0; $i < $matchnum; $i++)
4231
		}
4120
			{
4232
	}
4121
				$text = preg_replace('/' . preg_quote($matches[1][$i], '/') . '/', "ALPLACEHOLDER{$i}PH", $text, 1);
4233
	$db->sql_freeresult($result);
4122
			}
4234
4123
4235
	return $autolinks;
4124
			// Step 2 - s/r of the remaining text
4236
}
4125
			if($config['autolink_first'])
4126
			{
4127
				$text = preg_replace($autolinks['match'], $autolinks['replace'], $text, 1);
4128
			}
4129
			else
4130
			{
4131
				$text = preg_replace($autolinks['match'], $autolinks['replace'], $text);
4132
			}
4237
4133
4238
/**
4134
			// Step 3 - replace the spaces we munged in step 1
4239
* Autolinks
4135
			$text = preg_replace('/ALSPACEHOLDER/', '', $text);
4240
* Original Author - Jim McDonald - Edited by Mighty Gorgon
4241
*/
4242
function autolink_text($text, $forum_id = '')
4243
{
4244
	static $autolinks;
4245
4136
4246
	if (empty($text))
4137
			// Step 4 - replace the HTML tags that we removed in step 1
4247
	{
4138
			for($i = 0; $i < $matchnum; $i++)
4139
			{
4140
				$text = preg_replace("/ALPLACEHOLDER{$i}PH/", $matches[1][$i], $text, 1);
4141
			}
4142
		}
4143
4248
		return $text;
4144
		return $text;
4249
	}
4145
	}
4146
	// Autolinks - END
4250
4147
4251
	if (!isset($autolinks) || !is_array($autolinks))
4148
	/*
4149
	* Generate bbcode uid
4150
	*/
4151
	function make_bbcode_uid()
4252
	{
4152
	{
4253
		$autolinks = obtain_autolinks_list($forum_id);
4153
		// Unique ID for this message..
4154
		$uid = unique_id();
4155
		$uid = substr($uid, 0, BBCODE_UID_LEN);
4156
		return $uid;
4254
	}
4157
	}
4255
4158
4256
	if (sizeof($autolinks))
4159
	/*
4160
	* Make a link clickable
4161
	*/
4162
	function make_clickable($text)
4257
	{
4163
	{
4258
		global $config;
4164
		$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);
4259
		// Step 1 - move all tags out of the text and replace them with placeholders
4165
		$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1&#058;", $text);
4260
		preg_match_all('/(<a\s+.*?\/a>|<[^>]+>)/i', $text, $matches);
4261
		$matchnum = sizeof($matches[1]);
4262
		for($i = 0; $i < $matchnum; $i++)
4263
		{
4264
			$text = preg_replace('/' . preg_quote($matches[1][$i], '/') . '/', "ALPLACEHOLDER{$i}PH", $text, 1);
4265
		}
4266
4166
4267
		// Step 2 - s/r of the remaining text
4167
		// pad it with a space so we can match things at the start of the 1st line.
4268
		if($config['autolink_first'])
4168
		$ret = ' ' . $text;
4269
		{
4270
			$text = preg_replace($autolinks['match'], $autolinks['replace'], $text, 1);
4271
		}
4272
		else
4273
		{
4274
			$text = preg_replace($autolinks['match'], $autolinks['replace'], $text);
4275
		}
4276
4169
4277
		// Step 3 - replace the spaces we munged in step 1
4170
		// matches an "xxxx://yyyy" URL at the start of a line, or after a space.
4278
		$text = preg_replace('/ALSPACEHOLDER/', '', $text);
4171
		// xxxx can only be alpha characters.
4172
		// yyyy is anything up to the first space, newline, comma, double quote or <
4173
		$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
4279
4174
4280
		// Step 4 - replace the HTML tags that we removed in step 1
4175
		// matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing
4281
		for($i = 0; $i < $matchnum; $i++)
4176
		// Must contain at least 2 dots. xxxx contains either alphanum, or "-"
4282
		{
4177
		// zzzz is optional.. will contain everything up to the first space, newline,
4283
			$text = preg_replace("/ALPLACEHOLDER{$i}PH/", $matches[1][$i], $text, 1);
4178
		// comma, double quote or <.
4284
		}
4179
		$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
4285
	}
4286
4180
4287
	return $text;
4288
}
4289
// Autolinks - END
4290
4181
4291
/*
4182
		// matches an email@domain type address at the start of a line, or after a space.
4292
* Get attachment details
4183
		// Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
4293
*/
4184
		$ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);
4294
function get_attachment_details($attach_id)
4295
{
4296
	global $db;
4297
	$sql = "SELECT a.*, d.*, s.*, p.forum_id
4298
		FROM " . ATTACHMENTS_TABLE . " a, " . ATTACHMENTS_DESC_TABLE . " d, " . ATTACHMENTS_STATS_TABLE . " s, " . POSTS_TABLE . " p
4299
		WHERE a.attach_id = " . $attach_id . "
4300
			AND d.attach_id = a.attach_id
4301
			AND s.attach_id = a.attach_id
4302
			AND a.post_id > 0
4303
			AND p.post_id = a.post_id
4304
		LIMIT 1";
4305
	$db->sql_return_on_error(true);
4306
	$result = $db->sql_query($sql);
4307
	$db->sql_return_on_error(false);
4308
4185
4309
	if ($row = $db->sql_fetchrow($result))
4186
		// Remove our padding..
4310
	{
4187
		$ret = substr($ret, 1);
4311
		$db->sql_freeresult($result);
4312
		return $row;
4313
	}
4314
	else
4315
	{
4316
		return false;
4317
	}
4318
}
4319
4188
4320
/*
4189
		return($ret);
4321
* Get download details
4322
*/
4323
function get_download_details($file_id)
4324
{
4325
	global $db, $userdata;
4326
	$sql = "SELECT f.*, c.*
4327
		FROM " . PA_FILES_TABLE . " f, " . PA_CATEGORY_TABLE . " c
4328
		WHERE file_id = " . $file_id . "
4329
			AND file_approved = '1'
4330
			AND c.cat_id = f.file_catid
4331
		LIMIT 1";
4332
	$db->sql_return_on_error(true);
4333
	$result = $db->sql_query($sql);
4334
	$db->sql_return_on_error(false);
4335
4336
	if ($row = $db->sql_fetchrow($result))
4337
	{
4338
		$db->sql_freeresult($result);
4339
		$allowed = false;
4340
		if (($row['auth_view_file'] == AUTH_ALL) || ($userdata['user_level'] == ADMIN))
4341
		{
4342
			$allowed = true;
4343
		}
4344
		elseif (($row['auth_view_file'] == AUTH_REG) && $userdata['session_logged_in'])
4345
		{
4346
			$allowed = true;
4347
		}
4348
		return ($allowed ? $row : false);
4349
	}
4190
	}
4350
	else
4351
	{
4352
		return false;
4353
	}
4354
}
4355
4191
4356
/*
4357
* This function turns HTML into text... strips tags, comments spanning multiple lines including CDATA, and anything else that gets in it's way.
4358
*/
4359
function html2txt($document)
4360
{
4361
	$search = array(
4362
						'@<script[^>]*?>.*?</script>@si',	// Strip out javascript
4363
						'@<[\/\!]*?[^<>]*?>@si',					// Strip out HTML tags
4364
						'@<style[^>]*?>.*?</style>@siU',	// Strip style tags properly
4365
						'@<![\s\S]*?--[ \t\n\r]*>@'				// Strip multi-line comments including CDATA
4366
					);
4367
	$text = preg_replace($search, '', $document);
4368
	return $text;
4369
}
4192
}
4370
4193
4371
/*
4194
$bbcode = new bbcode();
4372
* Convert newline to paragraph
4373
*/
4374
function nl2any($text, $tag = 'p', $feed = '')
4375
{
4376
	// making tags
4377
	$start_tag = "<$tag" . ($feed ? ' ' . $feed : '') . '>';
4378
	$end_tag = "</$tag>";
4379
4195
4380
	// exploding string to lines
4196
if (defined('SMILIES_TABLE'))
4381
	$lines = preg_split('`[\n\r]+`', trim($text));
4382
4383
	// making new string
4384
	$string = '';
4385
	foreach($lines as $line)
4386
	$string .= "$start_tag$line$end_tag\n";
4387
4388
	return $string;
4389
}
4390
4391
/*
4392
* Convert paragraphs to newline
4393
*/
4394
function any2nl($text, $tag = 'p')
4395
{
4197
{
4396
	//exploding
4198
	$bbcode->allowed_smilies = array();
4397
	preg_match_all("`<" . $tag . "[^>]*>(.*)</" . $tag . ">`Ui", $text, $results);
4199
	$bbcode->allowed_smilies = $cache->obtain_smileys(false);
4398
	// reimploding without tags
4399
	return implode("\n", array_filter($results[1]));
4400
}
4200
}
4401
4201
4402
/*
4403
* Convert BR to newline
4404
*/
4405
function br2nl($text, $remove_linebreaks = false)
4406
{
4407
	if ($remove_linebreaks)
4408
	{
4409
		$text = preg_replace("/(\r\n|\n|\r)/", "", $text);
4410
	}
4411
	return preg_replace("=<br */?>=i", "\n", $text);
4412
}
4413
4414
/*
4415
* Convert newline to BR
4416
*/
4417
function nl2br_mg($text)
4418
{
4419
	/*
4420
	$text = preg_replace("/\r\n/", "\n", $text);
4421
	$text = str_replace('<br />', "\n", $text);
4422
	*/
4423
	$text = preg_replace(array("/<br \/>\r\n/", "/<br>\r\n/", "/(\r\n|\n|\r)/"), array("\r\n", "\r\n", "<br />\r\n"), $text);
4424
	return $text;
4425
}
4426
4427
?>
4202
?>