<? 
/*
  Markting Web Mail, 
  http://www.markeringwebmail.com

  Copyright (c) 2007 

  Todos los Derechos reservados
*/
function conectar(){

	if (!($link=mysql_connect(DB_SERVER,DB_SERVER_USERNAME,DB_SERVER_PASSWORD))){
		echo "Error conectando con el servidor";
		exit();
	}
	if (!mysql_select_db(DB_DATABASE,$link)){
		echo "Error conectanto con la base de datos";
		exit();
	}
return $link;
}
function desconectar($conn){
	mysql_close($conn);
}

function ip() { return $REMOTE_ADDR; }
function hora() { return time(); }
// ------------------------------------------------------ //
function update_date($user,$mail,$clave){
	$link = Conectar();
	$fecha = date("Y-m-d");
	mysql_db_query (DB_DATABASE,"UPDATE usuarios SET ultimo_acceso = '$fecha' WHERE usuario='$user' AND clave='$clave' AND 
	dmail='$mail'",$link)or 
	die (mysql_error());
	}
// ------------------------------------------------------ //
function comillas($text)
{
 $chrs = '"';
 $repl = "'";
 $text = str_replace($chrs, $repl, $text);
 return $text;
}
function activo($valor)
{
 if ($valor == 1) return "Activo";
 if ($valor == 0) return "Desactivado";

}
// ------------------------------------------------------ //
function edit_mensaje($id)
{
 $link = Conectar();
 $query = mysql_db_query (DB_DATABASE,"SELECT cuerpo_mail FROM mymails WHERE id_mail='$id' ",$link) or die (mysql_error());
	if ($row = mysql_fetch_array($query))
		{
			return $row[0];
		}
}
// ------------------------------------------------------ //
function max_id($campo,$tabla)
{
 $link = Conectar();
 $query = mysql_db_query (DB_DATABASE,"SELECT max(".$campo.")   FROM ".$tabla ,$link) or die (mysql_error());
	if ($row = mysql_fetch_array($query))
	{
		return $row[0];
	}
}
// ------------------------------------------------------ //
function contador($sql,$tabla)
{
 $link = Conectar();
 $query = mysql_db_query (DB_DATABASE,"SELECT count(*)   FROM ".$tabla." WHERE $sql " ,$link) or die (mysql_error());
	if ($row = mysql_fetch_array($query))
	{
		return $row[0];
	}
}
// ------------------------------------------------------ //
function incrementar_id($campo,$tabla)
{
 $link = Conectar();
 $query = mysql_db_query (DB_DATABASE,"SELECT max(".$campo.")   FROM ".$tabla ,$link) or die (mysql_error());
	if ($row = mysql_fetch_array($query))
	{
		return $row[0] + 1;
	}else{
		return 1;
	}
}
// ------------------------------------------------------ //
function fecha($fecha , $tipo = 1)
{
	if ($fecha && $tipo == 1){
	list($ano,$mes,$dia) = split("-",$fecha);
	$lista_mes = array 
	('','Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Dicicembre');
	$lista_dias = array('Domingo','Lunes','Martes','Miercoles','Jueves','Viernes','Sabado');
	$formato  = $dia." de ".$lista_mes[(int)$mes]." del ".$ano;
	return $formato;
	}
	
	if ($tipo==2){
		list($dia,$mes,$ano) = split("-",$fecha);
		$formato = $dia."-".$mes."-".$ano;
		return $formato;
	}
	
	if ($tipo==3){
		list($ano,$mes,$dia) = split("-",$fecha);
		$formato = (int)$dia."-".(int)$mes."-".$ano;
		return $formato;
	}

	if ($tipo==4){
		list($dia,$mes,$ano) = split("-",$fecha);
		$formato = $ano."-".$mes."-".$dia;
		return $formato;
	}
}
// ------------------------------------------------------ //
function comprobar_email($email){ 
    $mail_correcto = 0; 
    //compruebo unas cosas primeras 
    if ((strlen($email) >= 6) && (substr_count($email,"@") == 1) && (substr($email,0,1) != "@") && (substr($email,strlen($email)-1,1) != 
	"@")){ 
       if ((!strstr($email,"'")) && (!strstr($email,"\"")) && (!strstr($email,"\\")) && (!strstr($email,"\$")) && (!strstr($email," "))) { 
          //miro si tiene caracter . 
          if (substr_count($email,".")>= 1){ 
             //obtengo la terminacion del dominio 
             $term_dom = substr(strrchr ($email, '.'),1); 
             //compruebo que la terminación del dominio sea correcta 
             if (strlen($term_dom)>1 && strlen($term_dom)<5 && (!strstr($term_dom,"@")) ){ 
                //compruebo que lo de antes del dominio sea correcto 
                $antes_dom = substr($email,0,strlen($email) - strlen($term_dom) - 1); 
                $caracter_ult = substr($antes_dom,strlen($antes_dom)-1,1); 
                if ($caracter_ult != "@" && $caracter_ult != "."){ 
                   $mail_correcto = 1; 
                } 
             } 
          } 
       } 
    } 
    if ($mail_correcto) 
       return 1; 
    else 
       return 0; 
} 

function fn_send_mail($subject, $body,  $TO = "",$From = "") // Envia un E-mail 
{ 

	global $HTTP_SERVER_VARS;
	
	if ($From == "" ) $From = "info@buscalla.com";
	if ($TO   == "") $TO   = "info@buscalla.com";
	if ($id =="") $id = 0;
	
	$headers = '';
	
	if ($From != '')
	{
		 $headers .= "From: " .$From. "\r\n" ;
	}

	if ($Cc != '')
	{
		 $headers .= "Cc: " .$Cc. "\r\n" ;
	}

	if ($Bcc != '')
	{
		 $headers .= "Bcc: " .$Bcc. "\r\n" ;
	}


	$headers .= 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

 
	if ($headers != '')
	{
		 $headers .= 'X-Mailer: PHP/' . phpversion();
	}
	@mail($TO, $subject, $body, $headers);
} 
// ------------------------------------------------------ //

function escribe($msj)
{
	$ddf = fopen("error.log","a");
	fwrite($ddf,$msj." \n");
	fclose($ddf);
}

function enter($text){
	$chrs = chr(13);
	$repl = "<br />";
	$text = str_replace($chrs, $repl, $text);
	return $text;
}

function out_enter($text){
	$chrs = "<br />";
	$repl = "";
	$text = str_replace($chrs, $repl, $text);
	return $text;
}


function estrellas($nivel){
	if ($nivel == 1) return "images/1estrellas.gif";
	if ($nivel == 2) return "images/2estrellas.gif";
	if ($nivel == 3) return "images/3estrellas.gif";
	if ($nivel == 4) return "images/4estrellas.gif";
	if ($nivel == 5) return "images/5estrellas.gif";
}


function tipo3($tipo){

if ($tipo == "P") return "En vivo";
if ($tipo == "I") return "Internet";

}
function menor($valor) {
	if ($valor<9) 
		$valor="0".$valor;
	return $valor;
}

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
    $file = $filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
//    if (mail($mailto, $subject, "", $header)) { //
//        echo = "mail send  OK";//
//    } else {//
//        echo = "mail send ERROR!";//
//    }//
}


?>