<?php

function fck_assign_permissions($img_status, $flash_status, $smilies_status, $bbcode_status, $url_status)
{
	global $template;
	
	$template->assign_vars(array(
		'FCK_IMG_STATUS' => $img_status ? 1 : 0,
		'FCK_FLASH_STATUS' => $flash_status ? 1 : 0,
		'FCK_SMILIES_STATUS' => $smilies_status ? 1 : 0,
		'FCK_URL_STATUS' => ($bbcode_status && $url_status) ? 1 : 0,
	));
}

/**
* Assign/Build custom bbcodes for FCKeditor processing
*/
function fck_assign_custom_bbcodes()
{
	global $db, $template;

	// Start counting from 22 for the bbcode ids (every bbcode takes two ids - opening/closing)
	$num_predefined_bbcodes = 22;

	$sql = 'SELECT bbcode_match, bbcode_helpline, bbcode_tpl, bbcode_tag, display_on_posting, bbcode_richeditor_transform 
		FROM ' . BBCODES_TABLE ;
	
	$result = $db->sql_query($sql);

	$i = 0;
	while ($row = $db->sql_fetchrow($result))
	{
		$row['bbcode_match'] = str_replace("\"", "\\\"", $row['bbcode_match']);
		$row['bbcode_tpl'] = str_replace("\"", "\\\"", $row['bbcode_tpl']);
		$row['bbcode_tag'] = str_replace("\"", "\\\"", $row['bbcode_tag']);
		$row['bbcode_helpline'] = str_replace("\"", "\\\"", $row['bbcode_helpline']);
		//are mulitline custom bbcodes allowed (?)
		//$row['bbcode_match'] = preg_replace("/(\r\n|\n|\r)/", "/$1", $row['bbcode_match']);
		//$row['bbcode_tpl'] = preg_replace("/(\r\n|\n|\r)/", "/$1", $row['bbcode_tpl']);
		
		$template->assign_block_vars('fck_custom_tags', array(
			'BBCODE_MATCH'		=> preg_replace("/(?:\r\n|\n|\r)/", "\\\n", preg_replace('/{L_([A-Z_]+)}/e', "(!empty(\$user->lang['\$1'])) ? \$user->lang['\$1'] : ucwords(strtolower(str_replace('_', ' ', '\$1')))", $row['bbcode_match'])),
			'BBCODE_HELPLINE'		=> preg_replace("/(?:\r\n|\n|\r)/", "\\\n", preg_replace('/{L_([A-Z_]+)}/e', "(!empty(\$user->lang['\$1'])) ? \$user->lang['\$1'] : ucwords(strtolower(str_replace('_', ' ', '\$1')))", $row['bbcode_helpline'])),
			'BBCODE_TPL'		=> $row['bbcode_richeditor_transform']?preg_replace("/(?:\r\n|\n|\r)/", "\\\n", preg_replace('/{L_([A-Z_]+)}/e', "(!empty(\$user->lang['\$1'])) ? \$user->lang['\$1'] : ucwords(strtolower(str_replace('_', ' ', '\$1')))", $row['bbcode_tpl'])):'',
			'BBCODE_TAG'		=> $row['bbcode_tag'],
			'BBCODE_DISPLAY'	=> $row['display_on_posting'],
			)
		);

		$i++;
	}
	$db->sql_freeresult($result);
}

/**
* Assign/Build custom bbcodes for FCKeditor processing
*/
function fck_assign_smilies()
{
	global $db, $template, $config, $phpbb_root_path;

	// NOTE: obtain_* function? chaching the table contents?
	
	// For now setting the ttl to 10 minutes
	switch ($db->sql_layer)
	{
		case 'mssql':
		case 'mssql_odbc':
			$sql = 'SELECT code,smiley_url 
				FROM ' . SMILIES_TABLE . '
				ORDER BY LEN(code) DESC';
		break;
	
		case 'firebird':
			$sql = 'SELECT code,smiley_url 
				FROM ' . SMILIES_TABLE . '
				ORDER BY CHAR_LENGTH(code) DESC';
		break;

		// LENGTH supported by MySQL, IBM DB2, Oracle and Access for sure...
		default:
			$sql = 'SELECT code,smiley_url 
				FROM ' . SMILIES_TABLE . '
				ORDER BY LENGTH(code) DESC';
		break;
	}
	$result = $db->sql_query($sql, 600);

	
	while ($row = $db->sql_fetchrow($result))
	{
		if (empty($row['code']))
		{
			continue;
		}
		$template->assign_block_vars('fck_smilies', array(
				'A_SMILEY_CODE'	=> addslashes($row['code']),
				'SMILEY_IMG'	=> $phpbb_root_path . $config['smilies_path'] . '/' . $row['smiley_url'],
			)
		);

		$i++;

	}
	$db->sql_freeresult($result);
}

?>