<?php 
/*
##################################################################
#                       PHP 4&5 MySQL 4&5                        #
##################################################################
#           Copyright (c) 2004-2008 Armia Systems Inc            #
##################################################################
#               This file is part of Cybermatch site             #
##################################################################
#    Developed By: simi (simi@armia.com)                         #

##################################################################
#               Developed On:   03-04-2008                       #
##################################################################
#               Modified By:                                     #
##################################################################
#               Modified On:                                     #
##################################################################
# This script may not be distributed, sold, given away for free  #
# to third party, or used as a part of any internet services 	 #
# such as webdesign etc.                                         #
##################################################################
*/
ob_start();
require_once ("config.inc.php");
require_once ("includes/mysql.inc.php");
require_once ("classes/misc.class.php");
require_once ("classes/userside.class.php");
require_once ("facebook/facebook.php");
$message	= "";
$script		= "";
$mail		= "";

if (!$objDB) {
	$message	= "Unable to Locate Database Server.";
} else {
	$objM		= new misc($objDB);
        $objU		= new userside($objDB);
        //Redirect to my profile if already logged in
        if (isset($_SESSION['userlogged']) AND $_SESSION['userlogged'] <> "")
            header("Location: myprofile.php");
        /////////////////////////////////////////
	if (isset($_POST['LN'])) {
		$message	= $objM->login($_POST);
		if ($message == "") {
		//echo $_SESSION['triedpage'];
			if (isset($_SESSION['triedpage']) AND $_SESSION['triedpage'] <> "")
				header("Location: ".$_SESSION['triedpage']);
			else
                        {
                            if(isset($_SESSION['searchPOST']))
                                header("Location:search_profiles.php");
                            else
                                header("Location: myprofile.php");
                            exit;
                        }
		}
	}
	$script		= $objM->get_script($message);
	$message	= $objM->remove_script($message);
        
        
        
        
        
        
}
include_once ("title.inc.php");
include_once ("tophtml.inc.php");


$appid      = $facebook_app_id;
    $appsecret  = $facebook_secret_id;
    $facebook   = new Facebook(array(
        'appId' => $appid,
        'secret' => $appsecret,
        'cookie' => TRUE,

       
    ));
    
    Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;
if($_GET['facebook']){
 $accessToken = $facebook->getAccessToken();
 //print_r($accessToken);
    $fbuser = $facebook->getUser();

//    $user_profile = $facebook->api('/me');
    //print_r($fbuser);

    
    if ($fbuser) {
        try {
            $user_profile = $facebook->api('/'.$fbuser);
           // print_r($user_profile);
        }
        catch (Exception $e) {
            echo $e->getMessage();
            
        }
        $user_fbid  = $fbuser;
        $user_email = $user_profile["email"];
        $user_fnmae = $user_profile["first_name"];
        $user_image = "https://graph.facebook.com/".$user_fbid."/picture?type=large";
        $user_name = explode('@', $user_profile["email"]);
        $user_name = $user_name[0];
        
        
        
        if($fla)
        {
           // $message = "This Email already registered";
        }else{
            $logresult = $objM->loginfacebook($user_email);
           //print_r($logresult);
            if ($logresult['status'] == 1) {
		//echo $_SESSION['triedpage'];
			if (isset($_SESSION['triedpage']) AND $_SESSION['triedpage'] <> "")
				header("Location: ".$_SESSION['triedpage']);
			else
                        {
                            if(isset($_SESSION['searchPOST']))
                                header("Location:search_profiles.php");
                            else
                                header("Location: myprofile.php");
                            exit;
                        }
		}
        }
       
        if($logresult['status'] == 0)
        {
            $message = $logresult['message'];
        }
        
        if($logresult['status'] == 2)
        {
            $username = $objU->createusername($user_name);
           
        
       
        $arr = array();
        
        $arr['vLogin'] = $username;
        $arr['vPass'] = $username;
        $arr['vFirstName']  = $user_profile['first_name'];
        $arr['vLastName']  = $user_profile['last_name'];
        $arr['vEmail'] = $user_email;
        $arr['vGender'] = ucfirst($user_profile['gender']);
        $arr['vCountry'] = 'USA';
        $arr['vState'] = 'ALABAMA';
        $arr['vHearing_Source'] = 'Search Engine';
        $arr['fbId'] = $user_profile['id'];
        
        $objU->set_values($arr);
//        $_SESSION['USERNAME']=$arr['vLogin'];
//        $_SESSION['PASSWORD']=$arr['vPass']; 
        $message=$objU->register_step_facebook();
//       print_r($user_profile);
    
        if($message['message']=='')
        {
            header('location:myprofile.php');
        }
        }
    }else{
        
        $message = "Couldn't retrieve information from facebook";
    }
 
}
//$string = file_get_contents("dummydatas.json");
//$json_a = json_decode($string, true);




    
    //$photos = $val['photos'];
    
    //unset($val['photos']);
    
  //  $id = $objU->dbRowInsert('user',$val);
    
//    for($i=0;$i<5;$i++)
//    {
//        
//        $datauser = $json_a['data']['profiles'][1];
//        $k1 = array_rand($json_a['data']['maleNames']);
//        $username1 = $json_a['data']['maleNames'][$i].$json_a['data']['maleNames'][$k1];
//        $datauser['vLogin'] = strtolower($username1);
//        $datauser['vPass'] = md5(strtolower($username1));
//        $datauser['vEmail'] = $json_a['data']['emailList'][$i];
//        $datauser['vFirstName'] = $json_a['data']['maleNames'][$i];
//        $datauser['vLastName'] = $json_a['data']['maleNames'][$k1];
//        unset($datauser['photo']);
////      echo "<pre>";
////       print_r($datauser);
//        $id = $objU->dbRowInsert('user',$datauser);
//       // echo $id.'<br>';
//          $data = array();
//          $data['nUser_Id'] = $id;
//          $data['vTitle'] = $username1;
//          $data['vUrl'] = $json_a['data']['malePhots'][$i];
//          $data['dDate'] = date('Y-m-d');
//          $objU->dbRowInsert('photos', $data);
//          if($fid)
//          {
//              copy('dummyphoto/'.$data['vUrl'],'images/profiles/'.$data['vUrl']);
//              copy('dummyphoto/'.'pthumb_'.$data['vUrl'],'images/profiles/'.'pthumb_'.$data['vUrl']);
//              copy('dummyphoto/'.'sthumb_'.$data['vUrl'],'images/profiles/'.'sthumb_'.$data['vUrl']);
//          }
//      
//    }
//   
//    for($i=0;$i<5;$i++)
//    {
//        
//        $datauser = $json_a['data']['profiles'][0];
//        $k1 = array_rand($json_a['data']['femaleNames']);
//        $username1 = $json_a['data']['femaleNames'][$i].$json_a['data']['femaleNames'][$k1];
//        $datauser['vLogin'] = strtolower($username1);
//        $datauser['vPass'] = md5(strtolower($username1));
//        $datauser['vEmail'] = $json_a['data']['emailList'][$i];
//        $datauser['vFirstName'] = $json_a['data']['femaleNames'][$i];
//        $datauser['vLastName'] = $json_a['data']['femaleNames'][$k1];
//        unset($datauser['photo']);
////      echo "<pre>";
////       print_r($datauser);
//        $id = $objU->dbRowInsert('user',$datauser);
//       // echo $id.'<br>';
//          $data = array();
//          $data['nUser_Id'] = $id;
//          $data['vTitle'] = $username1;
//          $data['vUrl'] = $json_a['data']['femalePhots'][$i];
//          $data['dDate'] = date('Y-m-d');
//          $fid = $objU->dbRowInsert('photos', $data);
//          if($fid)
//          {
//              copy('dummyphoto/'.$data['vUrl'],'images/profiles/'.$data['vUrl']);
//              copy('dummyphoto/'.'pthumb_'.$data['vUrl'],'images/profiles/'.'pthumb_'.$data['vUrl']);
//              copy('dummyphoto/'.'sthumb_'.$data['vUrl'],'images/profiles/'.'sthumb_'.$data['vUrl']);
//          }
//    }
    
//    foreach ($photos as $k=>$v)
//    {
//        $data = array();
//        
//        $data['nUser_Id'] = $id;
//        $data['vTitle'] = $v;
//        $data['vUrl'] = $v;
//        $data['dDate'] = date('Y-m-d');
//        $objU->dbRowInsert('photos',$data);
//    }
    





?>
<script language="javascript" type="text/javascript">
function changeCSS(obj, type)
{
	if (type == 'text') {
		obj.className = 'textbox txt-bx01';
	} else if (type == 'textarea') {
		obj.className = 'textbox txt-bx01';
	} else if (type == 'select') {
		obj.className = 'textbox txt-bx01';
	} else {
		obj.className = 'textbox txt-bx01';
	}
}

function validate()
{
	var frm = document.frmTells;
	if (frm.user.value=="") 
	{
		alert("Please enter username.");
		frm.user.className ='textbox txt-bx01';
		frm.user.focus();
		return false;
	}//end if
	if (frm.pass.value=="") 
	{
		alert("Please enter password.");
		frm.pass.className ='textbox txt-bx01';
		frm.pass.focus();
		return false;
	}//end if
	return true;
}
</script>


<div id="fb-root"></div>
<script type="text/javascript">
//<![CDATA[
window.fbAsyncInit = function() {
   FB.init({
     appId      : '<?php echo $facebook_app_id;?>', // App ID
     channelURL : '', // Channel File, not required so leave empty
     status     : true, // check login status
     cookie     : true, // enable cookies to allow the server to access the session
     oauth      : true, // enable OAuth 2.0
     xfbml      : false  // parse XFBML
   });
};
// logs the user in the application and facebook
function login(){
FB.getLoginStatus(function(r){
     if(r.status === 'connected'){
            window.location.href = 'login.php?facebook=login';
     }else{
        FB.login(function(response) {
                if(response.authResponse) {
              //if (response.perms)
                    window.location.href = 'login.php?facebook=login';
            } else {
              // user is not logged in
            }
     },{scope:'email'}); // which data to access from user profile
 }
});
}
// Load the SDK Asynchronously
(function() {
   var e = document.createElement('script'); e.async = true;
   e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';                
   document.getElementById('fb-root').appendChild(e);
}());
//]]>
</script>


		<div class="search-rslts01">
					
			<div class="row">
				<div class="inenr-hd"><h2>Login</h2></div>
			</div>  
			<div class="full_row">
				<div class="row"><h2 class="headings2">Existing members? Login Now</h2></div>
			</div>
			<div class="row maintext1">
				<form name="frmTells" method="post" action="<?php echo $_SERVER['PHP_SELF']?>" onSubmit="return validate();">
					<div class="row warning">
						<?php if(isset($message) && trim($message)!='')
							{
								echo $message;
							}//end if
						?>
					</div>
					<div class="full_row">
						<div class="four columns">Username</div>
						<div class="five columns margin_R_280"><input name="user" type="text" size="25" maxlength="100" class="textbox txt-bx01 new_feild_1" onKeyDown="changeCSS(this, 'text');"></div>
					</div>
					<div class="full_row">
						<div class="four columns">Password</div>
						<div class="five columns margin_R_280"><input name="pass" type="password" size="25" maxlength="100" class="textbox txt-bx01 new_feild_1" onKeyDown="changeCSS(this, 'text');"></div>
					</div>
					<div class="full_row">
						<div class="four columns">&nbsp;</div>
						<div class="five columns txt-link-stl margin_R_280">
							<a href="forgot_password.php">Forgot your password?</a> &nbsp;<br/>
							<a href="register.php">New member register here</a>
						</div>
					</div>
					<div class="full_row">
						<div class="four columns">&nbsp;</div>
						<div class="five columns margin_R_280">
							<input name="LN" type="submit" class="loginbutton" value="Login">
							<input type="hidden" name="sex" value="<?php echo $_SESSION['gender_search']; ?>">
							<input type="hidden" name="agefrom" value="<?php echo $_SESSION['agefrom']; ?>">
							<input type="hidden" name="ageto" value="<?php echo $_SESSION['ageto']; ?>"><br/>
							<div class="clear">&nbsp;</div>
							<?php if($facebook_app_id && $facebook_secret_id){ ?>	<a href='javascript:void(0)' onclick='login();' ><img src="images/login_fb.jpg"/></a><br/><br/>
							<span class="facebook_info"> *While authenticating email permission is required </span>
							<?php } ?>
						</div>
					</div>
				</form>
			</div>
			<div id="hide"><?php echo $script?></div>
			
		</div>
		<!--<div class="row"><?php //include_once ("registermenu.inc.php");?></div>-->
		


<?php include_once('bottomhtml.inc.php');
 ob_flush();
?>
