Location: PHPKode > projects > Drafter classes > drafter-classes/funkpack_drafter.php
<?php
/**
 * Function helper for Drafter class
 *
 * Created 26-10-2006
 *
 * @version 1.0
 * @author Juan Prada
 *
 *
 */


// Auto instantiate the class, as it have to be always avaliable
// (Some kind of singleton)
if (!isset($fndrf)) $fndrf=new funkpack_drafter();


class funkpack_drafter {

var $db_link;

/**
 * Does the database login
 *
 * It can use various sets of configurations
 *
 * @param $dl	Array with different database login settings
 */
function db_login($dl) {
	// Test within the different connections
	$num = count($dl);
	if ($num) {
		$link_db = false;
		$i = 0;
		while ( ($i < $num) && (!$link_db) ) {
			$link = @mysql_connect ($dl[$i]['DBHostName'], $dl[$i]['DBUserName'], $dl[$i]['DBPWD']);
			if ($link) $link_db = mysql_select_db($dl[$i]['DBdb_name']);
			$i++;
		}
	}
	if (!$link_db) die ("Couldn't connect to database");

	$this->db_link = $link;
}

function db_desconectar() {
	return(mysql_close($this->db_link));
}


function db_comprueba_tabla($as_tabla) {
	$lb_ret = true;
	if (!mysql_query("SELECT count(*) AS valor FROM {$as_tabla}")) {
		if (mysql_errno() == 1146) $lb_ret = false;
	}
	return $lb_ret;
}

//----------------------------------
// Do a QUERY
function do_mysql_query($as_query) {
	 $l_dbq = mysql_query($as_query) or die ("<hr/>Invalid query '$as_query'<br>".mysql_errno().':'.mysql_error());
	 return $l_dbq;
}




// Returns a field named 'valor' from a select
function GetSelectValor($as_select) {
	$ls_ret='';
	$dbq = $this->do_mysql_query($as_select);

	if (mysql_num_rows($dbq)) {
		$datawindow[0] = mysql_fetch_assoc($dbq);
		$ls_ret= $datawindow[0]['valor'];
	}
	mysql_free_result($dbq);

	return $ls_ret;
}





// Gets a row from a select
function GetSelectData($as_select, $ai_modo=MYSQL_ASSOC) {
	$dbq = $this->do_mysql_query($as_select);

	if (mysql_num_rows($dbq)) {
		$fila = mysql_fetch_array($dbq, $ai_modo);
	}
	mysql_free_result($dbq);

	return $fila;
}



function GetSelectListDataEx($as_select, &$as_datawindow, $ai_modo=MYSQL_ASSOC)
{
	$ll_rows=0;

	$dbq = $this->do_mysql_query($as_select);

	if (mysql_num_rows($dbq))
		while ($temp = mysql_fetch_array($dbq,$ai_modo)) {
			$as_datawindow[$ll_rows] = $temp;
			$ll_rows++;
		}

	mysql_free_result($dbq);

	return $ll_rows;
}














//
// Paint a link
//
//	$as_url
//	$as_texto=''
//	$as_titulo=''
//	$al_ventana=0
//	$as_class=''
//	$as_adic=''
//
function f_draw_link($as_url, $as_texto='', $as_titulo='', $al_ventana=0, $as_class='', $as_adic='') {

	// Si no se indica texto, pone el de la URL
	if (!strlen($as_texto)) $as_texto=$as_url;

	// Si se indica titulo, prepara el tag
	if (strlen($as_titulo)) $as_titulo=' title="'.$as_titulo.'"';

	// La class
	if (strlen($as_class)) $as_class=' class="'.$as_class.'"';

	// Lo del target
	$ls_tar=' target="_self"';
	if ($al_ventana==1) $ls_tar=' target="_blank"';

	return '<a href="'.$as_url.'" '.$ls_tar.$as_titulo.$as_class.' '.$as_adic.'>'.$as_texto.'</a>';
}



//
// Paint a table
//
//	1	$as_contenido
//	2	$as_class=""
//	3	$as_adic=""
//	4	$as_width=''
//	5	$as_height=''
//	6	$as_border=""
//	7	$as_cellspacing=''
//	8	$as_cellpadding=''
//
function f_draw_tabla($as_contenido, $as_class="", $as_adic="", $as_width='', $as_height='', $as_border="", $as_cellspacing='', $as_cellpadding='') {

	$as_class = trim($as_class);
	$as_adic = trim($as_adic);
	if (!empty($as_adic)) $as_adic=' '.$as_adic;

	if (!empty($as_class)) $as_class=' class="'.$as_class.'"';
	if (strlen($as_width)) $as_width=' width="'.$as_width.'"';
	if (strlen($as_height)) $as_height=' height="'.$as_height.'"';

	if (!strlen($as_border)) $as_border='0';

	if (!strlen($as_cellspacing)) $as_cellspacing='0';
	if (!strlen($as_cellpadding)) $as_cellpadding='0';

	if (strlen($as_border)) $as_border=' border="'.$as_border.'"';
	if (strlen($as_cellspacing)) $as_cellspacing=' cellspacing="'.$as_cellspacing.'"';
	if (strlen($as_cellpadding)) $as_cellpadding=' cellpadding="'.$as_cellpadding.'"';

	return '<table'.$as_class.$as_width.$as_height.$as_border.$as_cellspacing.$as_cellpadding.$as_adic.'>'."\n".$as_contenido.'</table>'."\n";

}




//
// Pinta una tabla pero en realidad es una dentro de otra debido a que explorer
// no entiende el paddign global para un CSS de tabla, tiene que hacerlo por TD
// Nota: Para el correcto funcionamiento, tiene que estar definido el CSS sin margenes
// ni paddings ni nada
//
//	$al_padding
//	$as_contenido
//	$as_class=""
//	$as_adic=""
//	$as_width=''
//	$as_height=''
//	$as_border=""
//
function f_draw_tablap($al_padding, $as_contenido, $as_class="", $as_adic="", $as_width='', $as_height='' ,$as_border="") {
	return $this->f_draw_tabla($this->f_draw_tr($this->f_draw_td($as_contenido)), $as_class, $as_adic, $as_width, $as_height, '', $al_padding);
}







//
//	f_draw_tr()
//
// Paint a row <TR> </TR> for a table.
//
//	string			$as_contenido	TR contents
//	string	(op)	$as_class=""	TR class
//	string	(op)	$as_adic	=""	Additional parameters
//	array		(op)	$adata	= ""	Array (see código) with the OnMouseOver and OnMouseOut colors
//											Class have to be empty if this get used.
//
function f_draw_tr($as_contenido, $as_class="", $as_adic="", $adata="") {
	if (is_array($adata)) {
		if (strlen($adata["colorFondo"])) $as_adic = ' bgcolor="$adata["colorOut"]"';
		if (strlen($adata["colorOver"])) $ls_omOv = 'this.bgcolor="$adata["colorOver"]"';
		if (strlen($adata["colorOut"])) $ls_omOu = 'this.bgcolor="$adata["colorOut"]"';
	}
	$as_class = trim($as_class);

	if (strlen($as_class)) $as_class=' class="'.$as_class.'"';
	if (strlen($ls_omOv)) $as_adic.='OnMouseOver="'.$ls_omOv.'"';
	if (strlen($ls_omOu)) $as_adic.='OnMouseOut="'.$ls_omOu.'"';

	if (strlen($as_adic)) $as_adic = ' '.$as_adic;
	return '<tr'.$as_class.$as_adic.'>'.$as_contenido.'</tr>'."\n";
}





//
// Paint a cell
//
//	 1- $as_contenido
//	 2- $as_class=""
//	 3- $as_adic=""
//	 4- $as_ancho=''
//	 5- $as_alto=''
//	 6- $as_rowspan=''
//	 7- $as_colspan=''
//	 8- $as_background=''
//	 9- $as_stylebg=''		Background usando styles
//
function f_draw_td($as_contenido, $as_class="", $as_adic="", $as_ancho='', $as_alto='', $as_rowspan='', $as_colspan='', $as_background='', $as_stylebg="") {
	$as_class = trim($as_class);
	$as_adic = trim($as_adic);
	if (!empty($as_class)) $as_class=' class="'.$as_class.'"';
	if (!empty($as_adic)) $as_adic=' '.$as_adic;
	if (strlen($as_ancho)) $ls_w=' width="'.$as_ancho.'"';
	if (strlen($as_alto)) $ls_h=' height="'.$as_alto.'"';
	if (strlen($as_rowspan)) $ls_rs=' rowspan="'.$as_rowspan.'"';
	if (strlen($as_colspan)) $ls_cs=' colspan="'.$as_colspan.'"';
	if (!empty($as_background)) $ls_bg=' background="'.$as_background.'"';

	if (!empty($as_stylebg)) {
		$ls_styleadd="background-image: url({$as_stylebg}); background-repeat: repeat;	";
//		$ls_h="";$ls_w="";
//		if (strlen($as_ancho)) $ls_styleadd.="width: ".$as_ancho."; ";
//		if (strlen($as_alto)) $ls_styleadd.="height: ".$as_alto."; ";
		$ls_style=' style="'.$ls_styleadd.'"';
	}

	return '<td'.$as_class.$ls_w.$ls_h.$ls_rs.$ls_cs.$ls_bg.$ls_style.$as_adic.'>'.$as_contenido.'</td>'."\n";
}



//////
//
// Paint some form elements
//
function f_form_hidden($as_nombre, $as_value, $as_id='') {
	if (strlen($as_id)) $ls_id=' id="'.$as_id.'"';
	return '<input type="hidden"'.$ls_id.' name="'.$as_nombre.'" value="'.$as_value.'">'."\n";
}
function f_form_hidden_a($data, $as_campo, $as_id='') {
	return $this->f_form_hidden($as_campo, $data[$as_campo]);
}



function f_form_text($as_class, $as_nombre, $as_value, $as_size, $as_maxlength=0, $lb_br=true, $as_tpre="", $as_tpost="", $as_type="text", $as_adic='', $as_tabindex='') {
	if (empty($as_class)) $as_class="texto";
	if ($as_maxlength==0) $as_maxlength=$as_size;
	if (empty($as_type)) $as_type="text";
	if (strlen($as_tabindex)) $as_tabindex=' tabindex="'.$as_tabindex.'"';
	($lb_br)?$ls_br='<br>':$ls_br='';
	return $as_tpre.'<input type="'.$as_type.'" name="'.$as_nombre.'" class="'.$as_class.'" size="'.$as_size.'" maxlength="'.$as_maxlength.'" value="'.$as_value.'"'.$as_tabindex.' '.$as_adic.'>'.$as_tpost.$ls_br."\n";
}
function f_form_text_a($as_class, $data, $as_campo, $as_size, $as_maxlength=0, $lb_br=true, $as_tpre="", $as_tpost="", $as_type="text", $as_adic='', $as_tabindex='') {
	return $this->f_form_text($as_class, $as_campo, $data[$as_campo], $as_size, $as_maxlength, $lb_br, $as_tpre, $as_tpost, $as_type, $as_adic, $as_tabindex);
}



function f_form_textarea($as_class, $as_nombre, $as_value, $as_cols, $as_rows, $lb_br=true, $as_adic='') {
	if (empty($as_class)) $as_class="texto";
	($lb_br)?$ls_br='<br>':$ls_br='';
	return '<textarea name="'.$as_nombre.'" class="'.$as_class.'" cols="'.$as_cols.'" rows="'.$as_rows.'" '.$as_adic.'>'.$as_value.'</textarea>'.$ls_br."\n";
}
function f_form_textarea_a($as_class, $data, $as_campo, $as_cols, $as_rows, $lb_br=true, $as_adic='') {
	return $this->f_form_textarea($as_class, $as_campo, $data[$as_campo], $as_cols, $as_rows, $lb_br, $as_adic);
}




function f_form_cbx($as_class, $as_nombre, $ab_checked, $lb_br=true, $as_tpre="", $as_tpost="", $as_value="1") {
	if (empty($as_class)) $as_class="texto";
	($lb_br)?$ls_br='<br>':$ls_br='';
	if ($ab_checked) $ls_checked = "checked";
	return $as_tpre.'<input type="checkbox" name="'.$as_nombre.'" class="'.$as_class.'" value="'.$as_value.'" '.$ls_checked.'>'.$as_tpost.$ls_br."\n";
}
function f_form_cbx_a($as_class, $data, $as_campo, $ab_checked, $lb_br=true, $as_tpre="", $as_tpost="", $as_value="1") {
	return $this->f_form_text($as_class, $as_campo, $data[$as_campo], $ab_checked, $lb_br, $as_tpre, $as_tpost, $as_value);
}





// Type can be 'image'
//
//	1	$as_class
//	2	$as_nombre
//	3	$as_value
//	4	$lb_br=true
//	5	$as_type="submit"
//	6	$as_src=''
//	7	$as_alt=''
//	8	$as_border=''
//	9	$as_width=''
//	10	$as_height=''
//	11	$as_align=''
//	12	$as_adic=''
//	13	$as_tabindex=''
//
function f_form_boton($as_class, $as_nombre, $as_value, $lb_br=true, $as_type="submit", $as_src='', $as_alt='', $as_border='', $as_width='', $as_height='', $as_align='', $as_adic='', $as_tabindex='') {
	if ((!strlen($as_type)) && strlen($as_src)) $as_type='image';
	if (empty($as_class)) $as_class="texto";
	if (empty($as_type)) $as_type="submit";
	if (strlen($as_src)) $as_src=' src="'.$as_src.'"';
	if (strlen($as_alt)) $as_alt=' alt="'.$as_alt.'" title="'.$as_alt.'"';
	if (strlen($as_border)) $as_border=' border="'.$as_border.'"';
	if (strlen($as_width)) $as_width=' width="'.$as_width.'"';
	if (strlen($as_height)) $as_height=' height="'.$as_height.'"';
	if (strlen($as_align)) $as_align=' align="'.$as_align.'"';
	if (strlen($as_tabindex)) $as_tabindex=' tabindex="'.$as_tabindex.'"';
	($lb_br)?$ls_br='<br>':$ls_br='';
	return '<input class="'.$as_class.'" type="'.$as_type.'" name="'.$as_nombre.'" value="'.$as_value.'" '.$as_src.$as_alt.$as_border.$as_width.$as_height.$as_align.$as_tabindex.$as_adic.'>'.$ls_br;
}
function f_form_boton_a($as_class, $data, $as_campo, $lb_br=true, $as_type="submit") {
	return $this->f_form_boton($as_class, $as_campo, $data[$as_campo], $lb_br, $as_type);
}






function f_form_crear($as_contenido, $as_class='texto', $as_nombre='formulario', $as_method='POST', $as_action='', $as_target='', $as_enctype='') {
	if (strlen($as_target)) $as_target=' target="'.$as_target.'"';
	if (strlen($as_enctype)) $as_enctype=' enctype="'.$as_enctype.'"';
	return '<form '.$as_enctype.' '.$as_target.' name="'.$as_nombre.'" method="'.$as_method.'" action="'.$as_action.'" class="'.$as_class.'">'.$as_contenido.'</form>';
}





//
//	f_string_parseBBCode()
//
//	Parse BBCode
//
//	string	$text
//
//	Retorno:	string	HTML con el código parseado
//
Function f_string_parseBBCode($text)	{

	//	list
	$text	= str_replace("[list]", "<ul>", $text);
	$text	= str_replace("[/list]", "</ul>", $text);
	// *
	$text	= str_replace("[*]", "<li>", $text);

	// colours
	$text	= preg_replace("/\[color=(\#[0-9A-F]{6}|[a-z]+)\]/si", "<font color=\"\\1\">", $text);
	$text	= str_replace("[/color]", "</font>", $text);

	// test background-color
	//	<font style=\"background-color:#FFFF33\">$1</font>
	$text	= preg_replace("/\[bgcolor=(\#[0-9A-F]{6}|[a-z]+)\]/si", "<font style=\"background-color:\\1\">", $text);
	$text	= str_replace("[/bgcolor]", "</font>", $text);

	// size
	$text	= preg_replace("/\[size=([\-\+]?[1-2]?[0-9])\]/si", "<font size=\"$1\">", $text);
	$text	= str_replace("[/size]", "</font>", $text);

	// family
	$text	= preg_replace("/\[font=(.*?)\]/si", "<font face=\"$1\">", $text);
	$text	= str_replace("[/font]", "</font>", $text);

	// [b] and [/b] for bolding text.
	$text	= str_replace("[b]", "<b>", $text);
	$text	= str_replace("[/b]", "</b>", $text);

	// [u] and [/u] for underlining text.
	$text	= str_replace("[u]", "<u>", $text);
	$text	= str_replace("[/u]", "</u>", $text);

	// [i] and [/i] for italicizing text.
	$text	= str_replace("[i]", "<em>", $text);
	$text	= str_replace("[/i]", "</em>", $text);

	// [code] and [/code] for code text.
	$text	= str_replace("[code]", "<code>", $text);
	$text	= str_replace("[/code]", "</code>", $text);

	// [strike] and [/strike] for striked text.
	$text	= str_replace("[strike]", "<s>", $text);
	$text	= str_replace("[/strike]", "</s>", $text);

	// [blink] and [/blink] for blink text.
	$text	= str_replace("[blink]", "<blink>", $text);
	$text	= str_replace("[/blink]", "</blink>", $text);

	//	Image
	//	default (align left is default)
	$i	= 0;		//	COUNTER FOR ARRAY REPLACEMENT
	$patterns[$i]		= "#\[img\](.*?)\[/img\]#si";
	//$replacements[$i]	= "<img src=\"$1\" alt=\"\" align=\"left\">";
	$replacements[$i]	= "<img src=\"$1\" alt=\"\" border=\"0\">";
	//	with alignment (left or right)
	$addDivO	= "";
	$addDivC	= "";
	$i++;			//	COUNTER FOR ARRAY REPLACEMENT
	$patterns[$i]		= "#\[img=(.*?)\](.*?)\[/img\]#si";
	$replacements[$i]	= "<img src=\"$2\" align=\"$1\" border=\"0\" alt=\"\">";

	$i++;			//	COUNTER FOR ARRAY REPLACEMENT
	$patterns[$i]		= "#\[flash=(.*?)x(.*?)\](.*?)\[/flash\]#si";
	$replacements[$i]	= "<script>FlashWrite('$3','$1','$2')</script>";

	// documents
	$i++;			//	COUNTER FOR ARRAY REPLACEMENT
	$patterns[$i]		= "#\[doc=(.*?)~(.*?)\](.*?)\[/doc\]#si";
	$replacements[$i]	= "<img src=\"img/common/repository/icons/$1\" alt=\"\" border=0 align=\"absmiddle\"> <a href=\"$2\" target=\"_blank\">$3</a>";

	//	quote
	$i++;
	$patterns[$i]		= "#\[quote=(.*)](.*)\[/quote]#si";
	$replacements[$i]	= "<div class=\"openQuote\"></div><div class=\"quoteWords\">$2</span></div><div class=\"closeQuote\"></div><div class=\"quoteFrom\">$1</div>";

	//	url
	$i++;
	$patterns[$i] = "#\[url\]([a-z]+?://){1}([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+\(\)]+)\[/url\]#si";
	$replacements[$i] = "<a href=\"\\1\\2\">\\1\\2</a>";
	// [url]www.phpbb.com[/url] code.. (no xxxx:// prefix).
	$i++;
	$patterns[$i] = "#\[url\]([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+\(\)]+)\[/url\]#si";
	$replacements[$i] = "<a href=\"\\1\">\\1</a>";
	// [url=xxxx://www.phpbb.com]phpBB[/url] code..
	$i++;
	$patterns[$i] = "#\[url=([a-z]+?://){1}([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+\(\)]+)\](.*?)\[/url\]#si";
	$replacements[$i] = "<a href=\"\\1\\2\">\\3</a>";
	// [url=www.phpbb.com]phpBB[/url] code.. (no xxxx:// prefix).
	$i++;
	$patterns[$i] = "#\[url=([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+\(\)]+)\](.*?)\[/url\]#si";
	$replacements[$i] = "<a href=\"\\1\">\\2</a>";

	//	email
	// [email]hide@address.com[/email] code..
	$i++;
	$patterns[$i] = "#\[email\]([a-z0-9\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)\[/email\]#si";
	$replacements[$i] = "<a href=\"mailto:\\1\">\\1</a>";
	// [email=hide@address.com]testo[/email] code..
	$i++;
	$patterns[$i] = "#\[email=(.*?)\](.*?)\[/email\]#si";
	$replacements[$i] = "<a href=\"mailto:\\1\">\\2</a>";

	//	Paragraph
	$text = str_replace("[t_left]", "<div align=\"left\">", $text);
	$text = str_replace("[/t_left]", "</div>", $text);
	$text = str_replace("[t_center]", "<div align=\"center\">", $text);
	$text = str_replace("[/t_center]", "</div>", $text);
	$text = str_replace("[t_right]", "<div align=\"right\">", $text);
	$text = str_replace("[/t_right]", "</div>", $text);
	$text = str_replace("[t_justify]", "<div align=\"justify\">", $text);
	$text = str_replace("[/t_justify]", "</div>", $text);

	$text = @preg_replace($patterns, $replacements, $text);

	return $text;
}



// End of this helper class
}
?>
Return current item: Drafter classes