Tuesday, September 11, 2012

facebook login using php and get user emai id and orther data from face book





stp-1->include_once(COMMONINCLUDES.'classes/facebook.php');
--------------------------------------------------------------------------
stp-2
------------------------------------------------------------------------
class facebookapi {
   
   
    function facebookapi($postArr)
    {       
        global $g_criteria1;
       
        parent :: CommonClass();
        //parent :: Facebook();

       
               
        $this->CheckGetMagicQuotes($postArr);
       
        switch($g_criteria1)
        {
           
            case "nu":
                $this->News_Update();
            break;
           
            case "tm":
                $this->Testimonial();
                               
           
            case "fcall":
                $this->Facebook_Site_Callback();
           
           
            case "flogin":
                $this->Facebook_Login();
               
            case "fus":
                $this->Facebook_Save_User();
               
               
            default:
                $this->Facebook_Login();
        }
    }
   
   
    /*==============================================
        Author     :     bikash rajan nayak
        Modified By :  bikash rajan nayak
        Date    :    Nov 03, 2011
        Function to display home page
    ===================================================*/   
   
   
   
    function News_Update()
    {
   
        $smarty = new Smarty;
        $smarty->compile_check = true;
        $smarty->debugging = false;   
       
        $trail = new Trail();
        $trail->addStep('News & Update');
        $smarty->assign('TPL_PAGE_TITLE', 'News & Update');
       
        $plan_list_Order = $this->GetHomePlanList_Order_Two();
        $smarty->assign('TPL_PLAN_LIST_ORDERBY', $plan_list_Order);
       
        $news_list = $this->GetNEWS_List('I');
       
        $smarty->assign('TPL_NEWS_LIST', $news_list);
               
        $smarty->assign_by_ref('trail', $trail->path);
        $smarty->display('news-and-update.tpl');
    }
   
    function Facebook_Site_Callback()
    {
   
        $smarty = new Smarty;
        $smarty->compile_check = true;
        $smarty->debugging = false;   
               
        $hidArr = array();
        $hidArr = $this->GetHiddenVarVal($this);       
                                   
                   
                   
                   
                                $facebook = new Facebook(array('appId'  => '17378553343966053114','secret' => '63bb9447a4cf7df7d4be985bs55a6df3b648a9d',));
                                // Get User ID
                                $user = $facebook->getUser();
                               
                                if ($user)
                                {
                                try
                                {
                               
                                // Proceed knowing you have a logged in user who's authenticated.
                                    $user_profile = $facebook->api('/me');
                                   
                                   
                                        $Fuid = $user_profile['id'];
                                        $smarty->assign('FACE_UID',$Fuid);
                                       
                                        $FemailID = $user_profile['email'];
                                        $smarty->assign('FACE_EMAILID',$FemailID);
                                       
                                        $FfirstName = $user_profile['first_name'];
                                        $smarty->assign('FACE_FirstName',$FfirstName);
                                       
                                        $FlastName = $user_profile['last_name'];
                                        $smarty->assign('FACE_LastName',$FlastName);
                                       
                                        if(isset($user_profile['gender']))
                                        {
                                            $Fgender = $user_profile['gender'];
                                            $smarty->assign('FACE_gender',$Fgender);
                                        }
                                       
                                    //echo '<pre>'; print_r($user_profile); exit;
                                                                       
                                    //session_destroy();
                                                                       
                                        $u_selquery = "SELECT
                                                                    fld_id
                                                            FROM
                                                                            tbl_user
                                                            WHERE 
                                                                    fld_email = '$FemailID' AND flogin ='$Fuid' ";            
                                           
                                   
                                    $u_queryexe = mysql_query($u_selquery);
                                    if(mysql_affected_rows() > 0)
                                    {
                                       
                                                                               
                                        $whrCls1 = " WHERE (fld_email='$FemailID') AND flogin ='$Fuid' ";
                                        $table_fields1 = array('fld_id', 'fld_userName',  'fld_firstName', 'fld_lastName', 'fld_status', 'flogin');
                                        $sql_sel1 = $this->buildSelectSQL('tbl_user', $table_fields1, $whrCls1);
                                        $rs_valid1 = @mysql_query($sql_sel1);
                                       
                                            if($rs_valid1 && @mysql_num_rows($rs_valid1) > 0)
                                                {
                                               
                                                    $row = @mysql_fetch_assoc($rs_valid1);
                                                    $user_name = $row['fld_userName'];
                                                    $id = $row['fld_id'];
                                                    $fullName = $row['fld_firstName'];
                                                    $email = $FemailID;
                                                   
                                                    $facebookId = $row['flogin'];
                                                   
                                                    $objSessions = new Sessions($user_name, $id, 'customer', $fullName, $facebookId , '', $email);
                                                    $objSessions->set_sessions();
                                                   
                                                    //$_SESSION['facebookId'] = $facebookId;
                                                   
                                                   
                                                    //echo "<pre>";print_r($_SESSION);
                                                    //exit;

                                                 
//$logoutUrl = $facebook->getLogoutUrl();
//header("Location: $logoutUrl");
                                                    $this->redirectPage('myaccount.php?event=myaccount', $hidArr);   
                                                    exit;
                                                               
                                                               
                                                }
                                                else
                                                {
                                                    $hidArr = 'user name alrady exist, please choose another user name';
                                                    $this->redirectPage('myaccount.php?event=myaccount', $hidArr);   
                                                    exit;
                                               
                                                }
                   
                                   
                                    }
                                    else
                                    {
                                   
                                   
                                    //$logoutUrl = $facebook->getLogoutUrl();
                                    //header("Location: $logoutUrl");

                                    //$smarty->display('facebook-login.tpl');
                                    //exit;
                                    //07-March-Work............................................................................
                                   
                                   
                                        $Fuid = $user_profile['id'];
                                        $smarty->assign('FACE_UID',$Fuid);
                                       
                                        $FemailID = $user_profile['email'];
                                        $smarty->assign('FACE_EMAILID',$FemailID);
                                       
                                        $FfirstName = $user_profile['first_name'];
                                        $smarty->assign('FACE_FirstName',$FfirstName);
                                       
                                        $FlastName = $user_profile['last_name'];
                                        $smarty->assign('FACE_LastName',$FlastName);
                                       
                                        if(isset($user_profile['gender']))
                                        {
                                            $Fgender = $user_profile['gender'];
                                            $smarty->assign('FACE_gender',$Fgender);
                                        }
                                   
//echo "hello1";
//exit;   

                                    if($Fuid != '' && $FemailID != '' && $FfirstName != '' && $FlastName != '' )
                                    {
                                   
                                            $defaultImage = '2011_1.gif';
                                           
                                            $tbl_fld = array(
                                            'fld_userName', 'fld_email', 'fld_firstName', 'fld_lastName',
                                            'fld_sex', 'fld_image', 'fld_profileView','fld_wishView',
                                            'fld_status' ,'fld_creationDate','flogin' 
                                            );
                                            $tbl_fld_val = array(   
                                            $FemailID, $FemailID,  $FfirstName, $FlastName,
                                            $Fgender, $defaultImage, '1' , '1',
                                            '1', date('Y-m-d H:i:s'), $Fuid    
                                            );
                                           
                                            $sql_ins = $this->buildManipSQL('tbl_user', $tbl_fld, $tbl_fld_val, 'IN');
                                            $rs = mysql_query($sql_ins);
                                            $fld_id = mysql_insert_id();
                                           
                                           
                                            if($rs &&  $fld_id!='')
                                            {   
                                                    $whrCls1 = " WHERE (fld_userName ='".$FemailID."' OR fld_email='".$FemailID."') AND flogin='$Fuid'";
                                                    $table_fields1 = array('fld_id', 'fld_firstName', 'fld_lastName', 'fld_status');
                                                    $sql_sel1 = $this->buildSelectSQL('tbl_user', $table_fields1, $whrCls1);
                                                    $rs_valid1 = @mysql_query($sql_sel1);
                                                   
                                                    if($rs_valid1 && @mysql_num_rows($rs_valid1) > 0)
                                                        {
                                                           
                                                            $row = @mysql_fetch_assoc($rs_valid1);
                                                            $user_id = $row['fld_id'];
                                                           
                                                            if($row['fld_status'] == 0)
                                                            {                   
                                                                $hidArr['ec'] = NOT_ACT;                   
                                                                $hidArr['am'] = "YES";
                                                                $this->redirectPage('login.php', $hidArr);
                                                                exit;
                                                            }
                                                            else
                                                            {
                                                           
                                                                $user_name = $FemailID;
                                                                $id = $row['fld_id'];
                                                                $fullName = $row['fld_firstName'];
                                                                $email = $FemailID;
                                                                $objSessions = new Sessions($user_name, $id, 'customer', $fullName, '', '', $email);
                                                                $objSessions->set_sessions();
                                                                //echo "<pre>";print_r($_SESSION);
                                                                $this->redirectPage('myaccount.php?event=myaccount', $hidArr);   
                                                                exit;                   
                                                            }               
                                                        }
                                                   
                                           
                                            }
                               
                                   
                                    }
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                   
                                    }
                               
                                }
                                catch (FacebookApiException $e)
                                {
                                error_log($e);
                                $user = null;
                                }
                                }
                               
                               
                               
                                // Login or logout url will be needed depending on current user state.
                                if ($user) {
                               
                               
                                $logoutUrl = $facebook->getLogoutUrl();
                               
                                } else
                                {
                                    $loginUrl = $facebook->getLoginUrl(array('scope' => 'email,read_stream'));
                                    //echo '<script>self.location = '.$loginUrl.';
                                    header("Location: $loginUrl");
                                        //$hidArr['ec'] = " ";
                                        //$this->redirectPage($loginUrl, $hidArr);
                                }
                               
       
               
       
       
    }
   
   
   
   
   
   
   
   
   

    function Facebook_Login()
    {
        $smarty = new Smarty;
        $smarty->compile_check = true;
        $smarty->debugging = false;   
       
       
       
       
       
       
       
        $facebook = new Facebook(array('appId'  => '1737324242385966053114','secret' => '63bb9447a4cf7df7d4be985b3s6s53a2db648a9d',));

        // Get User ID
        echo "hjjdfgljf";
        $user = $facebook->getUser();
        if ($user) {
          try {
            // Proceed knowing you have a logged in user who's authenticated.
            $user_profile = $facebook->api('/me');
          } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
          }
        }
    //echo '<pre>'; print_r($user_profile); exit;
       
        $Fuid = $user_profile['id'];
        $smarty->assign('FACE_UID',$Fuid);
       
        $FemailID = $user_profile['email'];
        $smarty->assign('FACE_EMAILID',$FemailID);
       
        $FfirstName = $user_profile['first_name'];
        $smarty->assign('FACE_FirstName',$FfirstName);
       
        $FlastName = $user_profile['last_name'];
        $smarty->assign('FACE_LastName',$FlastName);
       
        $Fgender = $user_profile['gender'];
        $smarty->assign('FACE_gender',$Fgender);
       
        //echo '<pre>'; print_r( $user_profile); exit;
        //echo "<pre>"; print_r($_SESSION); exit;   
        //echo $facebook->getLogoutUrl();
       
       
        $cmsPageContent = $this->GetHomeCMSPage(1);
        $pageUrl = $this->getPageURLString();
        $smarty->assign('MENU_SEL',$pageUrl);
       
        //echo $cmsPageTitle = $cmsPageContent[0]['id'];
        $cmsPageTitle = $cmsPageContent[0]['title'];
        $cmsPageContent = $cmsPageContent[0]['content'];
       
        $smarty->assign('TPL_CMS_PAGE_TITLE', $cmsPageTitle);
        $smarty->assign('TPL_CMS_PAGE_CONTENT', $cmsPageContent);
               
       
       
       
        //echo "<pre>"; print_r($cmsPageContent); exit;
        //$smarty->assign('TPL_PLAN_LIST_ORDERBY', $plan_list_Order);
       
       
        $eventSlider = $this->GetUserEventSlider();
        $smarty->assign('event_list', $eventSlider);   
        //echo "<pre>";    print_r($eventSlider);exit;
               
        $smarty->display('facebook-login.tpl');
       
       
       
    }
   
   
   
    function Facebook_Save_User()
    {
   
        $smarty = new Smarty;
        $smarty->compile_check = true;
        $smarty->debugging = false;
       
        $hidArr = array();
        //$hidArr = $this->GetHiddenVarVal($this);
       
        $status = '0';       
        $ipaddress=$_SERVER['REMOTE_ADDR'];   
       
               
        //echo "<br />";
         date('Y-m-d H:i:s');
        //exit;
        $defaultImage = '2011_1.gif';
               
         if($this->hidface_gender == 'male')
         {
             $sex = '1';
         }
         else
         {
             $sex = '0';
         }
       
       
         if($this->txtemailID != '')
        {
            $u_selquery = "SELECT
                                fld_id
                         FROM
                                 tbl_user
                        WHERE 
                                fld_userName = '$this->txtUserName' OR  fld_email = '$this->txtemailID' ";            
       
         $u_queryexe = mysql_query($u_selquery);
         if(mysql_affected_rows() > 0)
             {
                $hidArr['ec'] = 'User Name or Email ID already exit Plz choose another name';
                $this->redirectPage('sign-up.php?event=ur', $hidArr);
            }
         }   
       
               
       
       
         if($this->hidface_emailid != '' && $this->hidface_fname != '' && $this->hidface_lname != '' && $this->hidface_gender != '' && $this->txtUserName != ''
            && $this->txtPassword != ''
            )
        {
         
       
            $defaultImage = '2011_1.gif';
            $tbl_fld = array(
                                'fld_userName', 'fld_email', 'fld_password', 'fld_firstName', 'fld_lastName',
                                'fld_sex', 'fld_image', 'fld_profileView','fld_wishView','fld_ipAddr',
                                'fld_status' ,'fld_creationDate','flogin' 
                            );
            $tbl_fld_val = array(   
                                    $this->txtUserName, $this->hidface_emailid,  md5($this->txtPassword), $this->hidface_fname, $this->hidface_lname,
                                    $sex, $defaultImage, '1' , '1',  $ipaddress,
                                    '1', date('Y-m-d H:i:s'), $this->hidface_id    
                                );
           
            $sql_ins = $this->buildManipSQL('tbl_user', $tbl_fld, $tbl_fld_val, 'IN');
            $rs = mysql_query($sql_ins);
            $fld_id = mysql_insert_id();
                               
            if($rs)
            {   
           
                 $txt_pass = md5($this->txtPassword);
            if($this->txtUserName != '' && $fld_id != '')
            {
           
                       
               $whrCls1 = " WHERE (fld_userName ='".$this->txtUserName."' OR fld_email='".$this->hidface_emailid."') AND fld_password='$txt_pass'";
               $table_fields1 = array('fld_id', 'fld_firstName', 'fld_lastName', 'fld_status');
                 $sql_sel1 = $this->buildSelectSQL('tbl_user', $table_fields1, $whrCls1);
       
           
            $rs_valid1 = @mysql_query($sql_sel1);
            if($rs_valid1 && @mysql_num_rows($rs_valid1) > 0)
            {
               
                $row = @mysql_fetch_assoc($rs_valid1);
                $user_id = $row['fld_id'];
               
                if($row['fld_status'] == 0)
                {                   
                    $hidArr['ec'] = NOT_ACT;                   
                    $hidArr['am'] = "YES";
                    $this->redirectPage('login.php', $hidArr);
                    exit;
                }
                else
                {
               
                    $user_name = $this->txtUserName;
                    $id = $row['fld_id'];
                    $fullName = $row['fld_firstName'];
                    $email = $this->txtUserName;
                    $objSessions = new Sessions($user_name, $id, 'customer', $fullName, '', '', $email);
                    $objSessions->set_sessions();
                    echo "<pre>";print_r($_SESSION);
                    $this->redirectPage('myaccount.php?event=myaccount', $hidArr);   
                    exit;                   
                }               
            }
           
        }   
            }
            else
            {
                $hidArr['ec'] = USER_ADD_FAIL;
                $this->redirectPage('sign-up.php?event=ur', $hidArr);
            }
             }
        else
        {

            $hidArr['ec'] = MEND_FIELDS;
            $this->redirectPage('sign-up.php?event=ur', $hidArr);
            exit;
        }
       
    }

 }
---------------------end face book class--------------------------------------------

last step pass user request for login
include_once('classes/facebookapi.class.php');

$obj_Facebookapi = new facebookapi($_REQUEST); 


No comments:

Post a Comment