<?php
	/*
	 *
	 * 2003 - 2020 @ Yal Publicidad - YMS::intalacion
	 * www.yalpublicidad.com
	 * online@yalpublicidad.com
	 *
	 */
	 
	  /* Autorizo acceso */
		defined( 'MSai' ) or die( '<h2>No dispones de acceso<h2>' );
	
	define( "YPATH", dirname( dirname( dirname(__FILE__) ) ) );
	
	$mysql_host	= ( isset( $_POST['mysql_host'] ) ) ? $_POST['mysql_host'] : 'localhost';
	$mysql_port = ( isset( $_POST['mysql_port'] ) && $_POST['mysql_port'] != NULL ) ? $_POST['mysql_port'] : 3306;
	$mysql_user = ( isset( $_POST['mysql_user'] ) ) ? $_POST['mysql_user'] : 'root';
	$mysql_pass	= ( isset( $_POST['mysql_pass'] ) ) ? $_POST['mysql_pass'] : 1234;
	$mysql_db	= ( isset( $_POST['mysql_db'] ) ) ? $_POST['mysql_db'] : NULL;
	$mysql_prefijo= ( isset( $_POST['mysql_prefijo'] ) ) ? $_POST['mysql_prefijo'] : 'wy_';

	$user		= ( isset( $_POST['user'] ) ) ? $_POST['user'] : NULL;
	$pass		= ( isset( $_POST['pass'] ) ) ? $_POST['pass'] : NULL;
	$email		= ( isset( $_POST['email'] ) ) ? $_POST['email'] : NULL;
	$utf		= ( isset( $_POST['charset'] ) ) ? $_POST['charset'] : 'utf-8';
	$lng		= ( isset( $_POST['lng'] ) ) ? $_POST['lng'] : 'es';
	$gmt		= ( isset( $_POST['gmt'] ) ) ? $_POST['gmt'] : -5;
	$title		= ( isset( $_POST['title'] ) ) ? $_POST['title'] : NULL;
	$descripcion= ( isset( $_POST['descripcion'] ) ) ? $_POST['descripcion'] : NULL;
	$keywords	= ( isset( $_POST['keywords'] ) ) ? $_POST['keywords'] : NULL;
	$organizacion= ( isset( $_POST['organizacion'] ) ) ? $_POST['organizacion'] : NULL;
	
	$estado		= ( isset( $_POST['estado'] ) ) ? $_POST['estado'] : 1;
	$idWeby		= IdWebyPlus();
	$idActWeby	= IdWebyPlus( NULL, 'CA' );
	
	$a	= array( '<%Y%>', '<%user%>', '<%dbhost%>', '<%dbuser%>', '<%dbpass%>', '<%dbname%>', '<%dbprefijo%>', '<%wyutf%>', '<%wytitle%>', '<%wydescription%>', '<%wykeywords%>', '<%wyorganizacion%>', '<%wyemail%>','<%kid%>', '<%kactivacion%>', '<%gmt%>' );
	$a1 = array( date('Y'), $user, $mysql_host, $mysql_user, $mysql_pass, $mysql_db, $mysql_prefijo, $utf, $title, $descripcion, $keywords, $organizacion, $email, $idWeby, $idActWeby, $gmt );
	
	define( "TBLPREFIJO", $mysql_prefijo );
		
	
	/* Encriptar cadena 
		 * ej. FNX_Encriptar('mi_cadena', 'mi_llave', 'Encriptar');
		 * ej. FNX_Encriptar('mi_cadena', 'mi_llave', 'Desencriptar');
		 */
	function encriptar($string, $key, $acc = 'Encriptar') {
			$result = '';
			$retorno = '/%?==';
			
			$acc	= strtolower( $acc );
			
			if($acc=='encriptar'){
				for($i=0; $i<strlen($string); $i++) {
					$char = substr($string, $i, 1);
					$keychar = substr($key, ($i % strlen($key))-1, 1);
					$char = chr(ord($char)+ord($keychar));
					$result.=$char;
				}
				$retorno = base64_encode($result);
			}
			
			if($acc=='desencriptar'){
				$string = base64_decode($string);
				for($i=0; $i<strlen($string); $i++) {
					$char = substr($string, $i, 1);
					$keychar = substr($key, ($i % strlen($key))-1, 1);
					$char = chr(ord($char)-ord($keychar));
					$result.=$char;
				}
				$retorno = $result;
			}
			
			return $retorno;
	}
	
	// ** Guardar fichero
	function gfichero( $ruta, $contenido = '' ){
		
		if ( !empty( $ruta ) )
		{
			$file = fopen( $ruta , 'w' );
			fwrite($file, $contenido  . PHP_EOL);
			fclose($file);
			return $contenido;
		}
		return false;
	}
	
	// ** obtener base sitio Weby+
	function base_sitioWeby(){
			$base = rtrim( dirname( dirname( dirname($_SERVER['SCRIPT_NAME'] ) ) ), '/\\' );
			$base = $base . '/';
			return $base;
	}
	
	// ** setear fichero de configuracion
	function dconfg(){
		global $a, $a1;
		if( file_exists( 'bin/configuracion.y' ) ){
			$c	= file_get_contents( 'bin/configuracion.y' );
			$c	= str_replace( $a, $a1, $c );
			
			return $c;
		}
		return false;
		
	}
	
	function IdWebySN(){
    	$protocolos = array('http://', 'https://', 'ftp://', 'www.');
    	$sn = explode( '/', str_replace( $protocolos, '', $_SERVER['SERVER_NAME'] ) );
    	return $sn[0];
	}
	
	function IdWebyPlus( $Idwp = NULL, $accion = 'L')
	{
		global $wy_func;
		
		$SERVER_NAME	= IdWebySN();
		$CA				= encriptar( md5($_SERVER['SERVER_ADDR'] ), md5( $SERVER_NAME ) );
		$IdCadena		=  $CA . '://' . YPATH;
		
		$Id = encriptar( $IdCadena, $SERVER_NAME );
		
		if( $accion == 'CA' ) return $CA;
		
		if( $accion == 'C' ) return ( $Idwp == $Id ) ? true : false;
		
		return $Id;
	}
	
	/*function IdWebyPlusRecuperar()
	{
		$SN		= url_codificar( IdWebySN() );
		$SA		= url_codificar( $_SERVER['SERVER_ADDR'] );
		$YP		= url_codificar( YPATH );
		$CA		= md5( WEBYPLUS_ID_ACTIVACION );
		$URI	= WYCFG_url_Tecnologia . '/'; //'//localhost/webyplus/';
		$USR	= url_codificar( WYCFG_MAILUSER );
		$e_Idwpr= $URI . "p_Idwpr.php?sn=$SN&sa=$SA&yp=$YP&ca=$CA&usr=$USR";
		
		return $e_Idwpr;
		
	}*/
	
	function url_codificar($cadena, $acc = 'codificar', $base64 = true){
        if( $acc == 'codificar' )
			return ( $base64 ) ? urlencode( base64_encode( $cadena ) ) : urlencode( $cadena );
			
		if( $acc == 'decodificar' )
			return ( $base64 ) ? base64_decode( urldecode( $cadena ) ) : urldecode( $cadena );
    }
				
	// crear fichero wy_configuración:
		if( gfichero( YPATH . "/wy_configuracion.php", dconfg() ) ){
			msg_error( LNG::etiqueta( 'msg_final_ok' ), 'fa fa-check-circle', 'alert alert-success text-center' );
			// -- regserv
			$url	= 'http://webyplus.com/xml/prcssregserv.php?os=' . OS() . '&h=' . $_SERVER['SERVER_NAME'] . '&ip=' . $_SERVER['SERVER_ADDR'];
			$regserv= obtener_contenido_remoto( $url );
		}else{
			msg_error( LNG::etiqueta( 'msg_final_noconfig' ), 'fa fa-info-circle', 'alert alert-warning text-center' );
		}
	
?>