<?php
session_start(); 
ini_set('display_errors', 'On');
	require ('milfs/xajax/xajax.inc.php');
	$xajax = new xajax("");
	require ("milfs/funciones/conex.php");
  	require ('milfs/funciones/funciones.php');
   require ("milfs/includes/parametrizacion.php");
 
   //$login_uid ="547312246";
   if($_REQUEST['provider'] !="") {
   $login_servicio ="$_REQUEST[provider]";
	}
	else {
	 $login_servicio ="twitter";
	}
 /*  
 	$nombres ="nombres apellidos";
   $usuario ="displayName";
   $email ="email@email";
   $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
   $info = json_encode($arr);
   */
   
   	$xajax->processRequests();  
  /* 	
   	$existe = social_login("consultar","$login_servicio","$login_uid");
   	if($existe =="") {
   		$control = md5(rand(1,99999999).microtime());
   		$password = md5(rand(1,99999999).microtime());
	$insertar_uid = insertar_linea("form_datos","$login_formulario","$social_login_uid","$login_uid",$control,"","");	
	$insertar_servicio = insertar_linea("form_datos","$login_formulario","$social_login_servicio","$login_servicio",$control,"","");	
	$insertar_nombres = insertar_linea("form_datos","$login_formulario","$login_campo_nombres","$nombres",$control,"","");	
	$insertar_password = insertar_linea("form_datos","$login_formulario","$login_campo_password","$password",$control,"","");	
	$insertar_usuario = insertar_linea("form_datos","$login_formulario","$login_campo_usuario","$usuario",$control,"","");	
	$insertar_email = insertar_linea("form_datos","$login_formulario","$login_campo_email","$email",$control,"","");	
	$insertar_info = insertar_linea("form_datos","$login_formulario","$login_campo_info","$info",$control,"","");	
   	}
   	else {$resultado ="$existe";}
   	*/
   	
	
   	  require_once( "./milfs/librerias/hybridauth/Hybrid/Auth.php" );
   	  require_once( "./milfs/librerias/hybridauth/Hybrid/Endpoint.php" );
   $config = './milfs/librerias/hybridauth/config.php';
 
 if (isset($_REQUEST['hauth_start']))
        {
            Hybrid_Endpoint::process();

        }
           try{
               $hybridauth = new Hybrid_Auth( $config );
 
               $twitter = $hybridauth->authenticate( "$login_servicio" );
 
               $user_profile = $twitter->getUserProfile();
               
 					$login_uid = $user_profile->identifier;
						if($login_uid !=""  ) {
						
						   $existe = social_login("consultar","$login_servicio","$login_uid");
						   	if($existe =="") {
						   		$nombre = $user_profile->firstName;
						   		$apellido = $user_profile->lastName;
						   		$nombres ="$nombre $apellido";
								   $usuario =$user_profile->displayName;
								   $email =$user_profile->email;
								   $avatar =$user_profile->photoURL;
								   $info = json_encode($user_profile);
						   		$control = md5(rand(1,99999999).microtime());
						   		$password = md5(rand(1,99999999).microtime());
										$insertar_uid = insertar_linea("form_datos","$login_formulario","$social_login_uid","$login_uid",$control,"","");	
										$insertar_servicio = insertar_linea("form_datos","$login_formulario","$social_login_servicio","$login_servicio",$control,"","");	
										$insertar_nombres = insertar_linea("form_datos","$login_formulario","$login_campo_nombres","$nombres",$control,"","");	
										$insertar_password = insertar_linea("form_datos","$login_formulario","$login_campo_password","$password",$control,"","");	
										$insertar_usuario = insertar_linea("form_datos","$login_formulario","$login_campo_usuario","$usuario",$control,"","");	
										$insertar_email = insertar_linea("form_datos","$login_formulario","$login_campo_email","$email",$control,"","");	
										$insertar_avatar = insertar_linea("form_datos","$login_formulario","$login_campo_avatar","$avatar",$control,"","");	
										$insertar_info = insertar_linea("form_datos","$login_formulario","$login_campo_info","$info",$control,"","");	
									$login = social_login("login","$login_servicio","$login_uid");
						   	}
						   	else {
									$login = social_login("login","$login_servicio","$login_uid");						   	
						   	}
	   	
	   	
						} 
 
 
               $resultado = "Hola! " . $user_profile->displayName." //  ". $user_profile->identifier;
 
              // $twitter->setUserStatus( "Estoy en http://tupale.co donde toda causa se gana!  " );
 
              // $user_contacts = $twitter->getUserContacts();
           }
           catch( Exception $e ){
              $resultado = "Ooophs, we got an error: " . $e->getMessage();
           }
          
echo $resultado;