<?php
    # Libreria Nusoap
    require_once "lib/nusoap.php";

    # Espacio de nombre
    //$namespace = "http://www.ecuinfo.net/ws/wsgemasys.php";
    //$namespace = "http://tempuri.org"; 
    $namespace= "wsgemasys";
	

    # Definicion de web services
    $server = new soap_server();
    $server->configureWSDL("wsgemasys",  "urn:wsgemasys");
    //$server->wsdl->schemaTargetNamespace = $namespace;

    # UTF-8
    date_default_timezone_set('America/Guayaquil');
    $server->soap_defencoding = 'utf-8';
    $server->decode_utf8 = false;

    # Tipo inforeg
    $server->wsdl->addComplexType(
     'inforeg',
     'complexType',
     'struct',
     '',
     'SOAP-ENC:Array',
     array(
      	'nomleg' => array('name' => "nomleg", 'type' => 'xsd:string'),
      	'nomrep' => array('name' => "nomrep", 'type' => 'xsd:string'),
      	'estado' => array('name' => "estado", 'type' => 'xsd:string'),
      	'direcc' => array('name' => "direcc", 'type' => 'xsd:string'),
        'tipoid' => array('name' => "tipoid", 'type' => 'xsd:string'),
        'guid' => array('name' => "guid", 'type' => 'xsd:string'),
        'totlic' => array('name' => "totlic", 'type' => 'xsd:string'),
        )
    );

    # Tipo infosri
    $server->wsdl->addComplexType(
     'infosri',
     'complexType',
     'struct',
     '',
     'SOAP-ENC:Array',
     array(
      	'nomleg' => array('name' => "nomleg", 'type' => 'xsd:string'),
      	'nomfan' => array('name' => "nomfan", 'type' => 'xsd:string'),
      	'estado' => array('name' => "estado", 'type' => 'xsd:string'),
      	'direcc' => array('name' => "direcc", 'type' => 'xsd:string'),
      	'ciudad' => array('name' => "ciudad", 'type' => 'xsd:string'),
        'tipoid' => array('name' => "tipoid", 'type' => 'xsd:string'),
        'email1' => array('name' => "email1", 'type' => 'xsd:string'),
        )
    );

    # Tipo infoced
    $server->wsdl->addComplexType(
     'infoced',
     'complexType',
     'struct',
     '',
     'SOAP-ENC:Array',
     array(
      	'nomleg' => array('name' => "nomleg", 'type' => 'xsd:string'),
      	'estado' => array('name' => "estado", 'type' => 'xsd:string'),
      	'direcc' => array('name' => "direcc", 'type' => 'xsd:string'),
      	'ciudad' => array('name' => "ciudad", 'type' => 'xsd:string'),
        'tipoid' => array('name' => "tipoid", 'type' => 'xsd:string'),
        'email1' => array('name' => "email1", 'type' => 'xsd:string'),
        )
    );

    # Tipo defparam
    $server->wsdl->addComplexType(
     'defparam',
     'complexType',
     'struct',
     '',
     'SOAP-ENC:Array',
     array(
      	'nompar' => array('name' => "nompar", 'type' => 'xsd:string'),
      	'tippar' => array('name' => "tippar", 'type' => 'xsd:string'),
      	'valdef' => array('name' => "valdef", 'type' => 'xsd:string'),
      	'valmax' => array('name' => "valmax", 'type' => 'xsd:string'),
      	'observ' => array('name' => "observ", 'type' => 'xsd:string'),
        'tipcia' => array('name' => "tipcia", 'type' => 'xsd:string'),
        'sistema' => array('name' => "sistema", 'type' => 'xsd:string'),
        )
    );

    # Tipo vparam
    $server->wsdl->addComplexType(
     'vparam',
     'complexType',
     'struct',
     '',
     'SOAP-ENC:Array',
     array(
      	'fecact' => array('name' => "fecact", 'type' => 'xsd:string'),
      	'tippar' => array('name' => "tippar", 'type' => 'xsd:string'),
      	'valdef' => array('name' => "valdef", 'type' => 'xsd:string'),
      	'valmax' => array('name' => "valmax", 'type' => 'xsd:string'),
      	'observ' => array('name' => "observ", 'type' => 'xsd:string'),
        'tipcia' => array('name' => "tipcia", 'type' => 'xsd:string'),
        'sistema' => array('name' => "sistema", 'type' => 'xsd:string'),
        )
    );

    # Tipo chksop
    $server->wsdl->addComplexType(
     'clientes',
     'complexType',
     'struct',
     '',
     'SOAP-ENC:Array',
     array(
      	'nombre' => array('name' => "nombre", 'type' => 'xsd:string'),
      	'repres' => array('name' => "repres", 'type' => 'xsd:string'),
      	'ruc'    => array('name' => "ruc",    'type' => 'xsd:string'),
      	'guid'   => array('name' => "guid",   'type' => 'xsd:string'),
      	'fecfin' => array('name' => "fecfin", 'type' => 'xsd:string'),
      	'tiplic' => array('name' => "tiplic", 'type' => 'xsd:string'),
      	'estado' => array('name' => "estado", 'type' => 'xsd:string'),
        )
    );

    # Tipo edocsop
    $server->wsdl->addComplexType(
     'cliedoc',
     'complexType',
     'struct',
     '',
     'SOAP-ENC:Array',
     array(
      	'nombre' => array('name' => "nombre", 'type' => 'xsd:string'),
      	'repres' => array('name' => "repres", 'type' => 'xsd:string'),
      	'ruc'    => array('name' => "ruc",    'type' => 'xsd:string'),
      	'guid'   => array('name' => "guid",   'type' => 'xsd:string'),
      	'fecfin' => array('name' => "fecfin", 'type' => 'xsd:string'),
      	'buzon'  => array('name' => "buzon",  'type' => 'xsd:string'),
      	'cnweb'  => array('name' => "cnweb",  'type' => 'xsd:string'),
      	'dvenc'  => array('name' => "dvenc",  'type' => 'xsd:string'),
      	'tpsop'  => array('name' => "tpsop",  'type' => 'xsd:string'),
      	'tplic'  => array('name' => "tplic",  'type' => 'xsd:string'),
      	'estado' => array('name' => "estado", 'type' => 'xsd:string'),
        )
    );

    # Registro infreg
    $server->register('infreg',    // nombre del metodo
        array('codruc' => 'xsd:string'),  // parametros de entrada
        array('inforeg' => 'tns:inforeg'), // resultado
        $namespace,           // espacio de nombre namespace
        $namespace . '#infreg',   // soapaction
        'rpc',   // estilo
        'literal',      // uso
        "Consulta Registro");  // detalle

    # Registro infsri
    $server->register('infsri',    // nombre del metodo
        array('codruc' => 'xsd:string'),  // parametros de entrada
        array('infosri' => 'tns:infosri'), // resultado
        $namespace,           // espacio de nombre namespace
        $namespace . '#infsri',   // soapaction
        'rpc',   // estilo
        'literal',      // uso
        "Consulta DNI");  // detalle

    # Registro infced
    $server->register('infced',    // nombre del metodo
        array('codced' => 'xsd:string'),  // parametros de entrada
        array('infoced' => 'tns:infoced'), // resultado
        $namespace,           // espacio de nombre namespace
        $namespace . '#infced',   // soapaction
        'rpc',   // estilo
        'literal',      // uso
        "Consulta DNI");  // detalle

    # Registro getpardef
    $server->register('getpardef',    // nombre del metodo
        array('xmod' => 'xsd:string',
              'xpar' => 'xsd:string'),  // parametros de entrada
        array('defparam' => 'tns:defparam'), // resultado
        $namespace,           // espacio de nombre namespace
        $namespace . '#detpardef',   // soapaction
        'rpc',   // estilo
        'literal',      // uso
        "Parametro Default");  // detalle

    # Registro getverpar
    $server->register('getverpar',    // nombre del metodo
        array('vmod' => 'xsd:string',
              'vpar' => 'xsd:string'),  // parametros de entrada
        array('vparam' => 'tns:vparam'), // resultado
        $namespace,           // espacio de nombre namespace
        $namespace . '#getverpar',   // soapaction
        'rpc',   // estilo
        'literal',      // uso
        "Parametro Default");  // detalle

    # Registro chksop
    $server->register('chksop',    // nombre del metodo
        array('guid' => 'xsd:string'),  // parametros de entrada
        array('clientes' => 'tns:clientes'), // resultado
        $namespace,           // espacio de nombre namespace
        $namespace . '#chksop',   // soapaction
        'rpc',   // estilo
        'literal',      // uso
        "Validacion de Soporte");  // detalle


    # Registro edocsop
    $server->register('edocsop',    // nombre del metodo
        array('guid' => 'xsd:string'),  // parametros de entrada
        array('cliedoc' => 'tns:cliedoc'), // resultado
        $namespace,           // espacio de nombre namespace
        $namespace . '#edocsop',   // soapaction
        'rpc',   // estilo
        'literal',      // uso
        "Validacion de Soporte eDOC");  // detalle


    # Registro gemlog
    $server->register('gemlog',    // nombre del metodo
        array('guid'   => 'xsd:string',
              'regcia' => 'xsd:string',
              'regusr' => 'xsd:string',
              'sysver' => 'xsd:string',
              'dbsver' => 'xsd:string',
              'usrnom' => 'xsd:string',
              'iporig' => 'xsd:string'),  // parametros de entrada
        array('return' => 'xsd:boolean'), // resultado
        $namespace,           // espacio de nombre namespace
        $namespace . '#gemlog',   // soapaction
        'rpc',   // estilo
        'literal',      // uso
        "Bitacora de uso");  // detalle


    # Registro gemerror
    $server->register('gemerror',    // nombre del metodo
        array('guid'   => 'xsd:string',
              'regcia' => 'xsd:string',
              'sysver' => 'xsd:string',
              'dbsver' => 'xsd:string',
              'usrnom' => 'xsd:string',
              'iporig' => 'xsd:string',
              'errnum' => 'xsd:string',
              'errprc' => 'xsd:string',
              'errmod' => 'xsd:string',
              'errpar' => 'xsd:string',
              'syserr' => 'xsd:string',
              'errdet' => 'xsd:string'),  // parametros de entrada
        array('return' => 'xsd:boolean'), // resultado
        $namespace,           // espacio de nombre namespace
        $namespace . '#gemerror',   // soapaction
        'rpc',   // estilo
        'literal',      // uso
        "Registro de errores");  // detalle


    # Registro gemrep
    $server->register('gemrep',             // nombre del metodo
        array('sysmod' => 'xsd:string',     // parametros de entrada
              'tiprep' => 'xsd:string',
              'codrep' => 'xsd:string',
              'fecver' => 'xsd:string'),  
        array('return' => 'xsd:boolean'),   // resultado
        $namespace,                // espacio de nombre namespace
        $namespace . '#gemrep',   // soapaction
        'rpc',                    // estilo
        'literal',                // uso
        'Revision de Reportes');  // detalle

    # Registro getrep
    $server->register('getrep',                                 // method
        array('sysmod' => 'xsd:string',
              'tiprep' => 'xsd:string',
              'codrep' => 'xsd:string'),                       // input parameters
        array('return' => 'xsd:string',
	      'nombre' => 'xsd:string',                         // output parameters
	      'fecact' => 'xsd:string'),                         // output parameters
        $namespace,                                              // namespace
        $namespace . '#getrep',                                  // soapaction
        'rpc',                                                   // style
        'encoded',                                               // use
        'Envio de Reporte');                                      // documentation

    # Registro getban
    $server->register('getban',                    // method
        array('id'  => 'xsd:string'),                 // input parameters
        array('rid' => 'xsd:string',
              'url' => 'xsd:string',
              'img' => 'xsd:string'),               // output parameters
        $namespace,                                    // namespace
        $namespace . '#getban',                        // soapaction
        'rpc',                                                   // style
        'literal',                                               // use
        'Envio de Banner');                          // documentation

    # Registro getbck
    $server->register('getbck',                                 // method
        array('nombre' => 'xsd:string'),                       // input parameters
        array('return' => 'xsd:string'),                         // output parameters
        $namespace,                                              // namespace
        $namespace . '#getbck',                                  // soapaction
        'rpc',                                                   // style
        'literal',                                               // use
        'Envio de Bckgnd');                                       // documentation

    # Registro getrss
    $server->register('getrss',                              // method
        array('fecrss' => 'xsd:string'),                         // input parameters
        array('return' => 'xsd:string'),                         // output parameters
        $namespace,                                              // namespace
        $namespace . '#getrss',                                  // soapaction
        'rpc',                                                   // style
        'literal',                                               // use
        'Noticias RSS');                                           // documentation

    # Check version update
    $server->register('chkvmod',                              // method
        array('modcod' => 'xsd:string'),                         // input parameters
        array('return' => 'xsd:string'),                         // output parameters
        $namespace,                                              // namespace
        $namespace . '#chkvmod',                                 // soapaction
        'rpc',                                                   // style
        'literal',                                               // use
        'Module Version Check');                                   // documentation

    # Registro getclima
    $server->register('getclima',                            // method
        array('codciu' => 'xsd:string'),                          // input parameters
        array('return' => 'xsd:string'),                         // output parameters
        $namespace,                                              // namespace
        $namespace . '#getclima',                                // soapaction
        'rpc',                                                   // style
        'literal',                                               // use
        'Datos de Clima');                                         // documentation

    # Registro gemreg
    $server->register('gemreg',                              // method
        array('GUID' => 'xsd:string',                            // input parameters
              'regcia' => 'xsd:string',
              'regusr' => 'xsd:string',                          // input parameters
              'iporig' => 'xsd:string',                          // input parameters
              'sysver' => 'xsd:string',                          // input parameters
              'wsname' => 'xsd:string',                          // input parameters
              'usname' => 'xsd:string',                          // input parameters
              'pidkey' => 'xsd:string'),                         // input parameters
        array('gemregResult' => 'xsd:string'),                         // output parameters
        $namespace,                                              // namespace
        $namespace . '#gemreg',                                  // soapaction
        'rpc',                                                   // style
        'literal',                                               // use
        'Registro de GEMASYS');                                    // documentation

    #####################################################
    # Function de Consulta Cedula en DB ecuinfo.infoced #
    #####################################################
    function infced($codced) {
        if ($codced == "") {
            return "No hay nada que buscar";
        }
        else {
            include_once "inc/db_connect.php";

	    # Fecha
  	    $today = date("d M Y H:i:s");
	    # Log File
    	    $logfile = "/var/log/wservices.log";
	    # Cabecera de log
	    $ip = '';
            $addr = inet_pton($_SERVER['REMOTE_ADDR']);
            foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
            $ipx = substr($ip,0,-1);

    	    $logdat = $today . ' [' . $ipx . '] ';

            # Busqueda de datos
            $strSql = "select nomleg,estado,direcc,ciudad,tipid,email"
                    . " from ecuinfo.infoced where codced like ? LIMIT 1";

            $par1 = $codced . "%";

            if ($stmt = $mysqli->prepare($strSql)) {
	        $stmt->bind_param('s', $par1);  // Bind codruc to parameter.
	        $stmt->execute();                 // Execute the prepared query.
	        #$stmt->store_result();
	        # get variables from result.
	        $stmt->bind_result($db_nomleg, $db_estado, $db_direcc, $db_ciudad, $db_tid, $db_mail);
	        $stmt->fetch();

                $info = array('nomleg'=>$db_nomleg,
                              'estado'=>$db_estado,
                              'direcc'=>$db_direcc,
                              'ciudad'=>$db_ciudad,
                              'tipoid'=>$db_tid,
                              'email1'=>$db_mail);
 	 
                # Graba bitacora
                $fp = fopen($logfile, 'a');
                if ($db_nomleg == "") {
                    fwrite($fp, $logdat . '[infced] DNI ' . $codced . ' - BAD' . PHP_EOL);
                    wslog($ipx, 'infced', 'DNI ' . $codced . ' - BAD');
                }
                else {
                    fwrite($fp, $logdat . '[infced] DNI ' . $codced . ' - OK' . PHP_EOL);
                    wslog($ipx, 'infced', 'DNI ' . $codced . ' - OK');
                }
                fclose($fp);
                return $info;
            }
            mysqli_close($mysqli);
        }
    }

    #####################################################
    # Function de Consulta de CIA DB ecuinfo.clientes   #
    #####################################################
    function infreg($codruc) {
        if ($codruc == "") {
            return "No hay nada que buscar";
        }
        else {
            include_once "inc/db_connect.php";
	    # Fecha
  	    $today = date("d M Y H:i:s");
	    # Log File
    	    $logfile = "/var/log/wservices.log";
	    # Cabecera de log
	    $ip = '';
            $addr = inet_pton($_SERVER['REMOTE_ADDR']);
            foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
            $ipx = substr($ip,0,-1);

    	    $logdat = $today . ' [' . $ipx . '] ';

            # Busqueda de datos
            #$strSql = "select a.nomleg,a.nomfan,a.estado,a.direcc,a.ciudad,"
            #        . "a.tipcon,a.email,a.obliga,a.clasec,b.negpop"
            #        . " from ecuinfo.infosri a"
            #        . " left join ecuinfo.inforimpe b on b.codruc=a.codruc" 
   	    #	     . " where a.codruc like ? LIMIT 1";

            #$par1 = $codruc . "%";

            #if ($stmt = $mysqli->prepare($strSql)) {
	    #    $stmt->bind_param('s', $par1);  // Bind codruc to parameter.
	    #    $stmt->execute();                 // Execute the prepared query.
	    #    #$stmt->store_result();
	    #    # get variables from result.
	    #    $stmt->bind_result($db_nomleg, $db_nomfan, $db_estado, $db_direcc, $db_ciudad, $db_tipcon, $db_mail,$db_obliga,$db_clasec,$db_negpop);
	    #    $stmt->fetch();
		
            #    $info = array('nomleg'=>$db_nomleg,
            #                  'nomfan'=>$db_nomfan,
            #                  'estado'=>$db_estado,
            #                  'direcc'=>$db_direcc,
            #                  'ciudad'=>$db_ciudad,
            #                  'tipoid'=>$db_tipcon,
            #                  'email1'=>$db_mail,
	    #		      'obliga'=>$db_obliga,
            #                  'clasec'=>$db_clasec,
            #                  'rimpet'=>$rimpet);
 	    #
                # Graba bitacora
            #    $fp = fopen($logfile, 'a');
            #    if ($db_nomleg == "") {
            #        fwrite($fp,  $logdat . '[infreg] RUC ' . $codruc . ' - BAD' . PHP_EOL);
            #        wslog($ipx, 'infreg', 'RUC ' . $codruc . ' - BAD');
            #    }
            #    else {
            #        fwrite($fp, $logdat . '[infreg] RUC ' . $codruc . ' - OK' . PHP_EOL);
            #        wslog($ipx, 'infreg', 'RUC ' . $codruc . ' - OK');
            #    }
            #    fclose($fp);
            #    return $info;
            }
            mysqli_close($mysqli);
        }
    }


    #####################################################
    # Function de Consulta de RUC en DB ecuinfo.infosri #
    #####################################################
    function infsri($codruc) {
        if ($codruc == "") {
            return "No hay nada que buscar";
        }
        else {
            include_once "inc/db_connect.php";

	    # Fecha
  	    $today = date("d M Y H:i:s");
	    # Log File
    	    $logfile = "/var/log/wservices.log";
	    # Cabecera de log
	    $ip = '';
            $addr = inet_pton($_SERVER['REMOTE_ADDR']);
            foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
            $ipx = substr($ip,0,-1);

    	    $logdat = $today . ' [' . $ipx . '] ';

            # Busqueda de datos
            $strSql = "select a.nomleg,a.nomfan,a.estado,a.direcc,a.ciudad,"
                    . "a.tipcon,a.email,a.obliga,a.clasec,b.negpop"
                    . " from ecuinfo.infosri a"
                    . " left join ecuinfo.inforimpe b on b.codruc=a.codruc" 
		    . " where a.codruc like ? LIMIT 1";

            $par1 = $codruc . "%";

            if ($stmt = $mysqli->prepare($strSql)) {
	        $stmt->bind_param('s', $par1);  // Bind codruc to parameter.
	        $stmt->execute();                 // Execute the prepared query.
	        #$stmt->store_result();
	        # get variables from result.
	        $stmt->bind_result($db_nomleg, $db_nomfan, $db_estado, $db_direcc, $db_ciudad, $db_tipcon, $db_mail,$db_obliga,$db_clasec,$db_negpop);
	        $stmt->fetch();

                if (is_null($db_negpop)) {
                  $rimpet = 0;
                }
		else {
                  if ($db_negpop == 0) {
                    $rimpet = 1;
                  }
                  if ($db_negpop == 1) {
                    $rimpet = 2;
                  }
	        }
		
                $info = array('nomleg'=>$db_nomleg,
                              'nomfan'=>$db_nomfan,
                              'estado'=>$db_estado,
                              'direcc'=>$db_direcc,
                              'ciudad'=>$db_ciudad,
                              'tipoid'=>$db_tipcon,
                              'email1'=>$db_mail,
			      'obliga'=>$db_obliga,
                              'clasec'=>$db_clasec,
                              'rimpet'=>$rimpet);
 	 
                # Graba bitacora
                $fp = fopen($logfile, 'a');
                if ($db_nomleg == "") {
                    fwrite($fp,  $logdat . '[infsri] RUC ' . $codruc . ' - BAD' . PHP_EOL);
                    wslog($ipx, 'infsri', 'RUC ' . $codruc . ' - BAD');
                }
                else {
                    fwrite($fp, $logdat . '[infsri] RUC ' . $codruc . ' - OK' . PHP_EOL);
                    wslog($ipx, 'infsri', 'RUC ' . $codruc . ' - OK');
                }
                fclose($fp);
                return $info;
            }
            mysqli_close($mysqli);
        }
    }

    #########################################################
    # Function Adquirir Parametro Default ecuinfo.getpardef #
    #########################################################
    function getpardef($smod,$spar) {
        if ($smod == "" or $spar == "") {
            return "No hay nada que buscar";
        }
        else {
            include_once "inc/db_connect.php";

	    # Fecha
	    $today = date("d M Y H:i:s");
            # Log File
    	    $logfile = "/var/log/wservices.log";
	    # Cabecera de log
	    $ip = '';
            $addr = inet_pton($_SERVER['REMOTE_ADDR']);
            foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
            $ipx = substr($ip,0,-1);

    	    $logdat = $today . ' [' . $ipx . '] ';

            # Busqueda de datos
            $strSql = "select nompar,tippar,valdef,valmax,observ,tipcia,sistema"
                    . " from ecuinfo.gempar "
                    . " where modulo='$smod' and nompar='$spar' limit 1";

            if ($stmt = $mysqli->prepare($strSql)) {
	        $stmt->execute();                 // Execute the prepared query.
	        #$stmt->store_result();
	        # get variables from result.
	        $stmt->bind_result($db_nompar, $db_tippar, $db_valdef, $db_valmax, $db_observ, $db_tipcia, $db_sistema);
	        $stmt->fetch();

                $info = array('nompar'=>$db_nompar,
                              'tippar'=>$db_tippar,
                              'valdef'=>$db_valdef,
                              'valmax'=>$db_valmax,
                              'observ'=>$db_observ,
                              'tipcia'=>$db_tipcia,
                              'sistema'=>$db_sistema);
 	 
                # Graba bitacora
                $fp = fopen($logfile, 'a');
                if ($db_nompar == "") {
                    fwrite($fp,  $logdat . '[getpar] Par ' . $smod . $spar . ' - BAD' . PHP_EOL);
                    wslog($ipx, 'getpar', 'Par ' . $smod . $spar . ' - BAD');
                }
                else {
                    fwrite($fp, $logdat . '[getpar] Par ' . $smod . $spar . ' - OK' . PHP_EOL);
                    wslog($ipx, 'getpar', 'Par ' . $smod . $spar . ' - OK');
                }
                fclose($fp);
                return $info;
            }
            mysqli_close($mysqli);
        }
    }


    ###########################################################
    # Function Verificar Datos de Parametro ecuinfo.getverpar #
    ###########################################################
    function getverpar($smod,$spar) {
        if ($smod == "" or $spar == "") {
            return "No hay nada que buscar";
        }
        else {
            include_once "inc/db_connect.php";

	    # Fecha
	    $today = date("d M Y H:i:s");
            # Log File
    	    $logfile = "/var/log/wservices.log";
	    # Cabecera de log
	    $ip = '';
            $addr = inet_pton($_SERVER['REMOTE_ADDR']);
            foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
            $ipx = substr($ip,0,-1);

    	    $logdat = $today . ' [' . $ipx . '] ';

            # Busqueda de datos
            $strSql = "select tippar,valdef,valmax,observ,tipcia,sistema,fecact"
                    . " from ecuinfo.gempar"
                    . " where modulo='$smod' and nompar='$spar' limit 1";

            if ($stmt = $mysqli->prepare($strSql)) {
	        $stmt->execute();                 // Execute the prepared query.
	        #$stmt->store_result();
	        # get variables from result.
	        $stmt->bind_result($db_tippar, $db_valdef, $db_valmax, $db_observ, $db_tipcia, $db_sistema, $db_fecact);
	        $stmt->fetch();

                $info = array('fecact'=>$db_fecact,
                              'tippar'=>$db_tippar,
                              'valdef'=>$db_valdef,
                              'valmax'=>$db_valmax,
                              'observ'=>$db_observ,
                              'tipcia'=>$db_tipcia,
                              'sistema'=>$db_sistema);
 	 
                # Graba bitacora
                $fp = fopen($logfile, 'a');
                if ($db_tippar  == "") {
                    fwrite($fp,  $logdat . '[getver] Par ' . $smod . $spar . ' - BAD' . PHP_EOL);
                    wslog($ipx, 'getver', 'Par ' . $smod . $spar . ' - BAD');
                }
                else {
                    fwrite($fp, $logdat . '[getver] Par ' . $smod . $spar . ' - OK' . PHP_EOL);
                    wslog($ipx, 'getver', 'Par ' . $smod . $spar . ' - OK');
                }
                fclose($fp);
                return $info;
            }
            mysqli_close($mysqli);
        }
    }

    #######################################################
    # Function de Consulta de soporte db ecuinfo.clientes #
    #######################################################
    function chksop($guid) {
        if ($guid == "") {
            return "No hay nada que buscar";
        }
        else {
            include_once "inc/db_connect.php";
	    # Fecha
	    $today = date("d M Y H:i:s");
            # Log File
    	    $logfile = "/var/log/wservices.log";
	    # Cabecera de log
	    $ip = '';
            $addr = inet_pton($_SERVER['REMOTE_ADDR']);
            foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
            $ipx = substr($ip,0,-1);

    	    $logdat = $today . ' [' . $ipx . '] ';

            # Busqueda de datos
            $strSql = "select nombre,repres,ruc,guid,tiplic,fecfin,estado from ecuinfo.clientes where guid = ? LIMIT 1";

            if ($stmt = $mysqli->prepare($strSql)) {
	       $stmt->bind_param('s', $guid);  // Bind codruc to parameter.
	       $stmt->execute(); // Execute the prepared query.
	       #$stmt->store_result();
	       # get variables from result.
	       $stmt->bind_result($db_nombre, $db_repres, $db_ruc, $db_guid, $db_tiplic, $db_fecfin, $db_estado);
	       $stmt->fetch();

               # Graba bitacora
               $fp = fopen($logfile, 'a');
               
               if ($db_guid == "") {
		  # No se encontro la cedula pasa dato en blanco
                  $info = array('nombre' => 'Demo Test',
                                'repres' => 'Demo Test',
                        	'ruc'    => '0999999999001',
                        	'guid'   => '34F1265B-A303-4544-A806A757F1503705',
                        	'tiplic' => '2',
                        	'fecfin' => '2013-01-01',
                        	'estado' => '1');
                  fwrite($fp, $logdat . '[chksop] GUID ' . $guid . ' - BAD' . PHP_EOL);
                  wslog($ipx, 'chksop', 'GUID ' . $guid . ' - BAD');
	       }
	       else {
                  $info = array('nombre' =>$db_nombre,
                                'repres' =>$db_repres,
                  	        'ruc'    =>$db_ruc,
                       	        'guid'   =>$db_guid,
                                'fecfin' =>$db_fecfin,
                                'tiplic' =>$db_tiplic,
                                'estado' =>$db_estado);
                  fwrite($fp, $logdat . '[chksop] GUID ' . $guid . ' - OK' . PHP_EOL);
                  wslog($ipx, 'chksop', 'GUID ' . $guid . ' - OK');
	       }               
               fclose($fp);
               return $info;
            }
            mysqli_close($mysqli);
        }
    }


    #######################################################
    # Function de Consulta de soporte clientes eDOC       #
    #######################################################
    function edocsop($guid) {
        if ($guid == "") {
            return "No hay nada que buscar";
        }
        else {
            include_once "inc/db_connect.php";
	    # Fecha
	    $today = date("d M Y H:i:s");
	    # Log File
    	    $logfile = "/var/log/wservices.log";
	    # Cabecera de log
	    $ip = '';
            $addr = inet_pton($_SERVER['REMOTE_ADDR']);
            foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
            $ipx = substr($ip,0,-1);

    	    $logdat = $today . ' [' . $ipx . '] ';

            # Busqueda de datos
            $strSql = "select nombre,repres,ruc,guid,edoc_buz,edoc_web,edoc_dven,edoc_lic,edoc_sop," 
	            . "estado from ecuinfo.clientes where guid = ? LIMIT 1";

            if ($stmt = $mysqli->prepare($strSql)) {
	       $stmt->bind_param('s', $guid);  // Bind codruc to parameter.
	       $stmt->execute(); // Execute the prepared query.
	       #$stmt->store_result();
	       # get variables from result.
	       $stmt->bind_result($db_nom,$db_rep,$db_ruc,$db_guid,$db_buz,$db_web,$db_dven,$db_tplic,$db_sop,$db_est);
	       $stmt->fetch();

               # Graba bitacora
               $fp = fopen($logfile, 'a');
               
               if ($db_guid == "") {
		  # No se encontro pasa dato en blanco
                  $info = array('nombre' => 'Demo Test',
                                'repres' => 'Demo Test',
                        	'ruc'    => '0999999999001',
                        	'guid'   => '34F1265B-A303-4544-A806A757F1503705',
				'buzon'  => '0',
				'cnweb'  => '0',
				'dvenc'  => '999',
                                'tpsop'  => '0',
                                'tplic'  => '2',
                        	'estado' => '1');
                  fwrite($fp, $logdat . '[edocsp] GUID ' . $guid . ' - BAD' . PHP_EOL);
                  wslog($ipx, 'edocsp', 'GUID ' . $guid . ' - BAD');
	       }
	       else {
                  $info = array('nombre'=>$db_nom,
                                'repres'=>$db_rep,
                  	        'ruc'   =>$db_ruc,
                       	        'guid'  =>$db_guid,
				'buzon' =>$db_buz,
				'cnweb' =>$db_web,
				'dvenc' =>$db_dven,
                                'tpsop' =>$db_tsop,
                                'tplic' =>$db_tplic,
                                'estado'=>$db_est);
                  fwrite($fp, $logdat . '[edocsp] GUID ' . $guid . ' - OK' . PHP_EOL);
                  wslog($ipx, 'edocsp', 'GUID ' . $guid . ' - OK');
                }               
               fclose($fp);
               return $info;
            }
            mysqli_close($mysqli);
        }
    }

    ################################################
    # Funcion de Registro de Uso db ecuinfo.gemlog #
    ################################################
    function gemlog($guid,$regcia,$regusr,$sysver,$dbsver,$usrnom,$iporig) {
        if ($guid == "") {
            return false;
        }
	else {
            include_once "inc/db_connect.php";
	    # Fecha
	    $today = date("d M Y H:i:s");
	    # Log File
    	    $logfile = "/var/log/wservices.log";
	    # Cabecera de log
	    $ip = '';
            $addr = inet_pton($_SERVER['REMOTE_ADDR']);
            foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
            $ipx = substr($ip,0,-1);

    	    $logdat = $today . ' [' . $ipx . '] ';

	    $sysver = trim($sysver);
        
            # Busqueda de datos
            $strSql = "insert into ecuinfo.gemlog (guid,regcia,regusr,sysver,dbsver,usrnom,ipuser,iporig,fecha) values ("
                    . "'$guid',"
  	            . "'$regcia',"
		    . "'$regusr',"
		    . "'$sysver',"
		    . "'$dbsver',"
		    . "'$usrnom',"
                    . "'$iporig',"
		    . "'$ipx',"
                    . "'" . date("Y-m-d H:i:s") . "')";

            $fp = fopen($logfile, 'a');
            if ($mysqli->query($strSql)) {
               fwrite($fp, $logdat . '[gemlog] GUID ' . $guid . ' - OK' . PHP_EOL);
               wslog($ipx, 'gemlog', 'GUID ' . $guid , ' - OK');
               fwrite($fp, $logdat . '[gemlog] ' . $sysver .' (DB '. $dbsver . ')' . PHP_EOL);
               wslog($ipx, 'gemlog', $sysver . ' (DB '. $dbsver . ')' , '');
               fwrite($fp, $logdat . '[gemlog] ' . $regcia . '/' . $usrnom . PHP_EOL);
               wslog($ipx, 'gemlog', $regcia . '/' . $usrnom ,'');
            }
            else {
               fwrite($fp, $logdat . '[gemlog] GUID ' . $guid . ' - BAD' . PHP_EOL);
               wslog($ipx, 'gemlog', 'GUID ' . $guid , ' - BAD');
            }
            fclose($fp);
            mysqli_close($mysqli);
            return true;
        }
    }

    ##################################################
    # Funcion de Registro de Error db ecuinfo.gemerr #
    ##################################################
    function gemerror($guid,$regcia,$sysver,$dbsver,$usrnom,$iporig,$errnum,$errprc,$errmod,$errpar,$syserr,$errdet) 
    {
        if ($guid == "") {
           return false;
        }
        else {
            include_once "inc/db_connect.php";
	    # Fecha
	    $today = date("d M Y H:i:s");
	    # Log File
    	    $logfile = "/var/log/wservices.log";
	    # Cabecera de log
	    $ip = '';
            $addr = inet_pton($_SERVER['REMOTE_ADDR']);
            foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
            $ipx = substr($ip,0,-1);

    	    $logdat = $today . ' [' . $ipx . '] ';

            if ($errnum == "100244") {
               return true;
	    }
            #else {
               # Decode Base 64 - Imagen
            #   $utf8_imgerr = base64_decode($imgerr);
            #   $e_img = mysqli_real_escape_string($mysqli,$utf8_imgerr);
            #}

            # Ingresa datos
            $strSql = "insert into ecuinfo.gemerr "
                 . "(guid,regcia,sysver,dbsver,usrnom,iporig,"
                 . "errnum,errprc,errmod,errpar,syserr,"
                 . "errdet,fecha) values ("
                 . "'$guid',"
	         . "'$regcia',"
		 . "'$sysver',"
		 . "'$dbsver',"
		 . "'$usrnom',"
		 . "'" . $_SERVER['REMOTE_ADDR'] . "',"
		 . "'$errnum',"
		 . "'$errprc',"
		 . "'$errmod',"
		 . "'$errpar',"
		 . "'$syserr',"
		 . "'$errdet',"
                 . "'" . date("Y-m-d H:i:s") . "')";

            $fp = fopen($logfile, 'a');
            if ($mysqli->query($strSql)) {
               fwrite($fp, $logdat . '[gemerr] GUID ' . $guid . ' - OK' . PHP_EOL);
               wslog($ipx, 'gemerr', 'GUID ' . $guid . ' - OK');
            }
            else {
               fwrite($fp, $logdat . '[gemerr] GUID ' . $guid . ' - BAD' . PHP_EOL);
               wslog($ipx, 'gemerr', 'GUID ' . $guid . ' - BAD');
            }
            fclose($fp);
            mysqli_close($mysqli);
            return true;
        }
    }

    ##################################################
    # Funcion de Revision Reportes db ecuinfo.gemrep #
    ##################################################
    function gemrep($sysmod,$tiprep,$codrep,$fecver) {
        if ($codrep == "") {
            return false;
        }
        else {
            include_once "inc/db_connect.php";
	    # Fecha
	    $today = date("d M Y H:i:s");
	    # Log File
            $logfile = "/var/log/wservices.log";
	    # Cabecera de log
	    $ip = '';
            $addr = inet_pton($_SERVER['REMOTE_ADDR']);
            foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
            $ipx = substr($ip,0,-1);

    	    $logdat = $today . ' [' . $ipx . '] ';

            # Busqueda de datos
            $strSql = "select fecact from ecuinfo.gemrep where " 
		. "module='$sysmod' and codigo='$codrep' and tipdoc='$tiprep'";

            if ($stmt = $mysqli->prepare($strSql)) {
	      #  $stmt->bind_param('sss', $sysmod,$codrep,$tiprep);  // Bind codruc to parameter.
	        $stmt->execute(); // Execute the prepared query.
     	      #  $stmt->store_result();
	        # get variables from result.
	        $stmt->bind_result($db_fecact);
	        $stmt->fetch();

                # Graba bitacora

                $fp = fopen($logfile, 'a');
                #fwrite($fp, $db_fecact .' ' . $fecver  . PHP_EOL);
                if ($db_fecact > $fecver) {
	           # Procede actualizar
                   fwrite($fp, $logdat . '[gemrep] ' . $sysmod . ':' . $codrep . ' - OK' . PHP_EOL);
                   wslog($ipx, 'gemrep', $sysmod . ':' . $codrep . ' - OK');
                   return true;
	        }
	        else {
                   fwrite($fp, $logdat . '[gemrep] ' . $sysmod . ':' . $codrep . ' - BAD' . PHP_EOL);
                   wslog($ipx, 'gemrep', $sysmod . ':' . $codrep . ' - BAD');
                   return false;
	        }
                fclose($fp);
             }
            mysqli_close($mysqli);
        }
    }


    #############################
    # Funcion Envio de Reporte  #
    #############################
    // Define the method as a PHP function
    function getrep($sysmod,$tiprep,$codrep) {
        if ($codrep == "") {
            return false;
        }
        else {
            include_once "inc/db_connect.php";
	    # Fecha
	    $today = date("d M Y H:i:s");
	    # Log File
            $logfile = "/var/log/wservices.log";
	    # Cabecera de log
	    $ip = '';
            $addr = inet_pton($_SERVER['REMOTE_ADDR']);
            foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
            $ipx = substr($ip,0,-1);

    	    $logdat = $today . ' [' . $ipx . '] ';

            # Busqueda de datos
            $strSql = "select detalle,nombre,fecact from ecuinfo.gemrep where " 
		. "module='$sysmod' and codigo='$codrep' and tipdoc='$tiprep'";

            if ($stmt = $mysqli->prepare($strSql)) {
	        $stmt->execute(); // Execute the prepared query.
     	      #  $stmt->store_result();
	        # get variables from result.
	        $stmt->bind_result($db_detalle,$db_nombre,$db_fecact);
	        $stmt->fetch();

		#if ($tiprep == 'REP') {
                   # Encode Base64
                   
                   $repoenc =  base64_encode($db_detalle);
		#}
	        #else {
                #   $repoenc = $db_detalle;
                #}

                # Graba bitacora
                $fp = fopen($logfile, 'a');
                fwrite($fp, $logdat . '[getrep] ' . $sysmod . ':' . $codrep . ' - OK' . PHP_EOL);
                fwrite($fp, $logdat . substr($db_detalle,0,10) . PHP_EOL);
                fwrite($fp, $logdat . substr($repoenc,0,10) . PHP_EOL);
		# Procede enviar
                $info = array('return'=>$repoenc,
		              'nombre'=>$db_nombre,
                              'fecact'=>$db_fecact);
                fclose($fp);
                return $info;
             }
             mysqli_close($mysqli);
        }

    }

    ############################
    # Funcion Envio de Banner  #
    ############################
    // Define the method as a PHP function
    function getban($id) {
        if ($id == "") {
            $id = "1";
        }
        
        include_once "inc/db_connect.php";
        # Fecha
        $today = date("d M Y H:i:s");
	# Log File
        $logfile = "/var/log/wservices.log";
	# Cabecera de log
	$ip = '';
        $addr = inet_pton($_SERVER['REMOTE_ADDR']);
        foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
        $ipx = substr($ip,0,-1);

    	$logdat = $today . ' [' . $ipx . '] ';

        # Busqueda de datos
	$strsql = "select id,nombre,image,url from ecuinfo.banner " 
                . " where id = (select nid from ecuinfo.banner "
                . " where id = '$id')";

        if ($stmt = $mysqli->prepare($strsql)) {
            $stmt->execute(); // Execute the prepared query.
     	    #  $stmt->store_result();
	    # get variables from result.
	    $stmt->bind_result($rid, $bnom, $img, $url);
	    $stmt->fetch();

            # Encode Base64
            $imgenc =  base64_encode($img);

            # Graba bitacora
            $fp = fopen($logfile, 'a');
            fwrite($fp, $logdat . '[getban] (' . $rid . ') ' . $bnom . ' - OK' . PHP_EOL);
            wslog($ipx, 'getban', '(' . $rid . ') ' . $bnom . ' - OK');
            # Procede enviar
            $info = array('rid'=>$rid,
                          'img'=>$imgenc,
                          'url'=>$url);
            fclose($fp);
            return $info;
            }
            mysqli_close($mysqli);
    }

    ############################
    # Funcion Envio de Bckgnd  #
    ############################
    // Define the method as a PHP function
    function getbck($nombre) {
        if ($nombre == "") {
            return false;
        }
        else {
            include_once "inc/db_connect.php";
	    # Fecha
  	    $today = date("d M Y H:i:s");
	    # Log File
            $logfile = "/var/log/wservices.log";
	    # Cabecera de log
	    $ip = '';
            $addr = inet_pton($_SERVER['REMOTE_ADDR']);
            foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
            $ipx = substr($ip,0,-1);

    	    $logdat = $today . ' - ' . $ipx . ' - ';

            # Busqueda de datos
            $strSql = "select image from ecuinfo.backgnd where nombre='$nombre'";

            if ($stmt = $mysqli->prepare($strSql)) {
	        $stmt->execute(); // Execute the prepared query.
     	      #  $stmt->store_result();
	        # get variables from result.
	        $stmt->bind_result($db_image);
	        $stmt->fetch();

                # Encode Base64
                $repoenc =  base64_encode($db_image);

                # Graba bitacora
                $fp = fopen($logfile, 'a');
                fwrite($fp, $logdat . '[getbck] ' . $nombre . ' - OK' . PHP_EOL);
                wslog($ipx, 'getbck', $nombre . ' - OK');
                # Procede enviar
                return $repoenc;
                fclose($fp);
             }
             mysqli_close($mysqli);
        }

    }

    #########################
    # Funcion Noticias RSS  #
    #########################
    // Define the method as a PHP function
    function getrss($fecrss) {
        if ($fecrss == "") {
            return false;
        }
        else {
            include_once "inc/db_connect.php";
	    # Fecha
  	    $today = date("d M Y H:i:s");
	    # Log File
            $logfile = "/var/log/wservices.log";
	    # Cabecera de log
	    $ip = '';
            $addr = inet_pton($_SERVER['REMOTE_ADDR']);
            foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
            $ipx = substr($ip,0,-1);

    	    $logdat = $today . ' [' . $ipx . '] ';

            # Busqueda de datos
            #$now = date("Y-m-d H:i:s");            
            $strSql = "select item_title,item_url,fetch_date from ecuinfo.rssingest" 
                    . " where fetch_date > '$fecrss'"
                    . " order by fetch_date limit 1";

            if ($stmt = $mysqli->prepare($strSql)) {
	        $stmt->execute(); // Execute the prepared query.
     	      #  $stmt->store_result();
	        # get variables from result.
	        $stmt->bind_result($db_title,$db_url,$db_date);
	        $stmt->fetch();

                $resultado =  $db_title . ";" . $db_url . ";" . $db_date . ";"  ;
                # Graba bitacora
                $fp = fopen($logfile, 'a');
                fwrite($fp, $logdat . '[getrss] ' . $fecrss . ' - OK' . PHP_EOL);
                wslog($ipx, 'getrss', $fecrss . ' - OK');
                # Procede enviar
                return $resultado ;
                fclose($fp);
             }
             mysqli_close($mysqli);
        }

    }

    ###########################
    # Funcion Datos de Clima  #
    ###########################
    // Define the method as a PHP function
    function getclima($codciu) {
        if ($codciu == "") {
            return false;
        }
        else {
            include_once "inc/db_connect.php";
	    # Fecha
  	    $today = date("d M Y H:i:s");
	    # Log File
            $logfile = "/var/log/wservices.log";
	    # Cabecera de log
	    $ip = '';
            $addr = inet_pton($_SERVER['REMOTE_ADDR']);
            foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
            $ipx = substr($ip,0,-1);

    	    $logdat = $today . ' [' . $ipx . '] ';

            # Busqueda de datos
            $strSql = "select * from ecuinfo.ecuclima where codciu='$codciu'";

            if ($stmt = $mysqli->prepare($strSql)) {
	        $stmt->execute(); // Execute the prepared query.
     	      #  $stmt->store_result();
	        # get variables from result.
                $stmt->bind_result($db_codciu,$db_idciu,$db_nomciu,$db_mintemp,$db_maxtemp,$db_valtemp,$db_icocod,$db_humedad,$db_presion,$db_vtovel,$db_vtodir,$db_clima,$db_feccli,$db_fecact);
	        $stmt->fetch();

		$resultado = $db_nomciu . ";"
                           . $db_mintemp . ";"
                           . $db_maxtemp . ";"
                           . $db_valtemp . ";"
                           . $db_icocod  . ";"
                           . $db_humedad . ";"
                           . $db_presion . ";"
                           . $db_vtovel . ";"
                           . $db_vtodir . ";"
                           . $db_clima . ";"
                           . $db_feccli . ";"
                           . $db_fecact . ";";
                # Graba bitacora
                $fp = fopen($logfile, 'a');
                fwrite($fp, $logdat . '[getcli] ' . $codciu . ' - OK' . PHP_EOL);
                wslog($ipx,'getcli', $codciu . ' - OK');
                # Procede enviar
                return $resultado ;
                fclose($fp);
             }
             mysqli_close($mysqli);
        }

    }

    ###############################################
    # Funcion Verificacion de version de Modulos  #
    ##############################################
    // Define the method as a PHP function
    function chkvmod($modcod) {
        if ($modcod == "") {
            return false;
        }
        else {
            include_once "inc/db_connect.php";
	    # Fecha
	    $today = date("d M Y H:i:s");
	    # Log File
            $logfile = "/var/log/wservices.log";
	    # Cabecera de log
	    $ip = '';
            $addr = inet_pton($_SERVER['REMOTE_ADDR']);
            foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
            $ipx = substr($ip,0,-1);

    	    $logdat = $today . ' [' . $ipx . '] ';

            # Busqueda de datos
            #$now = date("Y-m-d H:i:s");            
            $strSql = "select mayor,minor,compile,revision from ecuinfo.modules" 
                    . " where codigo = '$modcod' limit 1";

            if ($stmt = $mysqli->prepare($strSql)) {
	        $stmt->execute(); // Execute the prepared query.
     	      #  $stmt->store_result();
	        # get variables from result.
	        $stmt->bind_result($db_mayor,$db_minor,$db_compile,$db_revision);
	        $stmt->fetch();

                $resultado =  " v" . $db_mayor . "." . $db_minor . "." . $db_compile . $db_revision  ;
                # Graba bitacora
                $fp = fopen($logfile, 'a');
                fwrite($fp, $logdat . '[chkmod] Modulo ' . $modcod . ':' . $resultado . PHP_EOL);
                wslog($ipx, 'chkmod', 'Modulo ' . $modcod . ':' . $resultado);
                # Procede enviar
                return $resultado ;
                fclose($fp);
             }
             mysqli_close($mysqli);
        }

    }

    ######################################
    # Funcion Datos de Registro GEMASYS  #
    ######################################
    // Define the method as a PHP function
    function gemreg($guid,$regcia,$regusr,$iporig,$sysver,$wsname,$usname,$pidkey) {
        if ($guid == "" or $pidkey == "") {
            return 'false';
        }
        else {
            include_once "inc/db_connect.php";
	    # Fecha
	    $today = date("d M Y H:i:s");
	    # Log File
            $logfile = "/var/log/wservices.log";
	    # Cabecera de log
	    $ip = '';
            $addr = inet_pton($_SERVER['REMOTE_ADDR']);
            foreach(str_split($addr) as $char) $ip = $ip . str_pad(ord($char),3,'0',STR_PAD_LEFT) . '.';
            $ipx = substr($ip,0,-1);

    	    $logdat = $today . ' [' . $ipx . '] ';

            # Grabacion de datos
            $strSql = "insert into ecuinfo.gemreg "
                 . "(guid,regcia,regusr,iporig,sysver,wsname,usname,pidkey,estado,fecact) values ("
                 . "'$guid',"
	         . "'$regcia',"
		 . "'$regusr',"
		 . "'$iporig',"
		 . "'$sysver',"
		 . "'$wsname',"
		 . "'$usname',"
		 . "'$pidkey',"
		 . "'0',"
                 . "'" . date("Y-m-d H:i:s") . "')";

            $fp = fopen($logfile, 'a');
            if ($mysqli->query($strSql)) {
               fwrite($fp, $logdat . '[gemreg] GUID ' . $guid . ' - OK' . PHP_EOL);
               wslog($ipx, 'gemreg', 'GUID ' . $guid . ' - OK');
            }
            else {
               fwrite($fp, $logdat . '[gemreg] GUID ' . $guid . ' - BAD' . PHP_EOL);
               wslog($ipx, 'gemreg', 'GUID ' . $guid . ' - BAD');
            }
            fclose($fp);
            mysqli_close($mysqli);
            return 'true';

        }
    }

    ######################################
    # Funcion LOG GEMASYS  #
    ######################################
    function wslog($ip,$tipo,$obs) {
      $mysqli = new mysqli('localhost','ecuws','ecuws_2020#','ecuinfo'); 
      # Fecha
      # Grabacion de datos
      $strSql = "insert into wslog "
                 . "(fecha,ipnum,iplocal,equipo,service,observ) values ("
                 . "'" . date("Y-m-d H:i:s") . "',"
                 . "'$ip',"
                 . "'" . $_SERVER['HTTP_USER_AGENT'] . "',"
		 . "'" . $_SERVER['REMOTE_ADDR'] . "',"
	         . "'$tipo',"
		 . "'$obs')";
      $mysqli->query($strSql);
      mysqli_close($mysqli);

    }

   $POSTDATA = file_get_contents('php://input');
  // if ( !isset( $HTTP_RAW_POST_DATA ) ) $HTTP_RAW_POST_DATA =file_get_contents( 'php://input' );
  // $server->service($HTTP_RAW_POST_DATA);
   $server->service($POSTDATA);
?>
