从facebook注销时请求在api中重新验证

时间:2014-08-22 10:44:56

标签: php facebook facebook-graph-api authentication

我使用Facebook提供的Facebook Open Graph(php)来获取用户的图像。一切都很好。当用户访问我的网站并点击Facebook按钮时,它首先要求进行身份验证,然后导入用户的图像。由于此过程,用户也会自动登录facebook.com。这是预期的行为。

但是在完成所有这个过程后,如果用户从facebook.com注销,然后再次点击我网站上的facebook按钮,我相信我的应用程序会要求进行身份验证,但它不会要求身份验证,而是提供图像来自用户的Facebook帐户。 如果我不清楚,请询问更多细节。 提前谢谢。

修改

代码:

的index.php

<?php
     /**********************************************************************
     * Plugin Name: Facebook Plugin                                        *
     * Plugin URI: http://www.picpixa.com/                                 *
     * Version: 1.0                                                        *
     * Author: Ashish Shah                                                 *
     * Description: Plugin To Import Images From User's Facebook Account   *
     *              as well as post the image of personalized product to   *
     *              his/her facebook account                               *
     **********************************************************************/
    session_start();
    include_once '/home/picpixa/wp-config.php';
    include_once "fbmain.php";
    include_once "facebook.php";

    ini_set("display_errors",1);
?>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Facebook Images</title>      
        <!-- Bootstrap -->
        <link href="css/bootstrap.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">

        <style>
            .loader {
                position: fixed;
                left: 0px;
                top: 0px;
                width: 100%;
                height: 100%;
                z-index: 9999;
                background: url('Images/page-loader.gif') 50% 50% no-repeat rgb(249,249,249);
            }
        </style>

        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(window).load(function() {
                $(".loader").fadeOut("slow");
            });

            function loader(){
                $('#load').show();
            }
            /* function loadExistingImages(){
                window.opener.$('#loader_img').show();
                result=null;
                window.opener.$('#fileupload').each(function () {
                    var that = this;
                    window.opener.$.getJSON(this.action, function (result) {
                        if (result && result.length) {
                            window.opener.$(that).fileupload('option', 'done')
                                .call(that, null, {result: result});
                            //console.log('ss='+$('.table-striped tbody').find('.btn-danger').length);
                            if(window.opener.$('.table-striped .files').find('.btn-danger').length>0){
                                window.opener.$('.coo-images-other-buttons').show();
                            }else{
                                window.opener.$('.coo-images-other-buttons').hide();
                            }
                        }
                        window.opener.$('#loader_img').hide();

                        if (window.opener.$('.table-striped.files').children().length > 0)
                        {
                            window.opener.$('.table_tagline').show();
                        }
                    });
                });
            }*/
        </script>
        <script type="text/javascript">$('#load').hide();</script>
    </head>
<?php
        if(isset($_POST['copy']) && $_POST['facebook'])
        {
            $imgArray = $_POST['facebook'];
            $current_user = wp_get_current_user();

            if(isset($current_user->ID) && trim($current_user->ID)!='' && trim($current_user->ID)!=0){
                $extraSessionStr = 'usr-'.md5($current_user->ID).'/';
                $userPicpixa = $current_user->ID;
            }else{
                $sesstionId = session_id();
                $userPicpixa = $sesstionId;
                $extraSessionStr = $sesstionId.'/';
            }

            foreach ($imgArray as $img)
            {
                //Getting a file name
                $imgInfo = pathinfo($img); //This will become an array with keys ('dirname','basename','extension','filename')
                $oriFileName = (string) uniqid("FB-");//"FB-image";
                $imgExtension = "jpg";

                //sometime facebook sends some odd extension including ? after the extension
                //Ex. <image path & name>.jpg?oh=dea6bece1e47875b65b9e3eb9299ea20&oe=545564F2&__gda__=1413855237_b5ab7de5ee45555ff6b87cbad003e3f4

                //That is why giving static extension

                $fileName = (string) $oriFileName.".".$imgExtension;//Creating a file name with extension

                //Check wether the file is exists or not rename the file if exists
                $i=1;
                if(file_exists('/home/picpixa/server/php/files/'.$extraSessionStr.$fileName)){
                    while(file_exists('/home/picpixa/server/php/files/'.$extraSessionStr.$fileName)){
                        $fileName = (string) $oriFileName."(".$i.").".$imgExtension;
                        $i++;
                    }
                }

                //if (extension_loaded('imagick')){ //This condition is to check that imagick extension has been installed or not
                //if( class_exists("Imagick") ){ //This condition is to check that imagick class exists or not or not

                /*if ( !version_compare( phpversion( 'imagick' ), '2.2.0', '<' ) )
                    die("Not a PHP version error");*/



                // Read file content
                $file_content = file_get_contents($img);
                //Putting the main file into the directory
                file_put_contents('/home/picpixa/server/php/files/'.$extraSessionStr.$fileName, $file_content);

                /* To create thumbnail */

                // Max vert or horiz resolution
                $maxsize=80;

                // create new Imagick object
                $image = new Imagick($img); //"input_image_filename_and_location"

                // Resizes to whichever is larger, width or height
                if($image->getImageHeight() <= $image->getImageWidth())
                {
                    // Resize image using the lanczos resampling algorithm based on width
                    $image->resizeImage($maxsize,0,Imagick::FILTER_LANCZOS,1);
                }
                else
                {
                    // Resize image using the lanczos resampling algorithm based on height
                    $image->resizeImage(0,$maxsize,Imagick::FILTER_LANCZOS,1);
                }

                // Set to use jpeg compression
                $image->setImageCompression(Imagick::COMPRESSION_JPEG);
                // Set compression level (1 lowest quality, 100 highest quality)
                $image->setImageCompressionQuality(75);
                // Strip out unneeded meta data
                $image->stripImage();
                // Writes resultant image to output directory
                $image->writeImage('/home/picpixa/server/php/thumbnails/'.$extraSessionStr.$fileName); //"output_image_filename_and_location"
                // Destroys Imagick object, freeing allocated resources in the process
                $image->destroy();
            }
?>
            <script type="text/javascript">

                window.opener.$('tbody.files').find('tr').remove();
                //loadExistingImages();
                    var myVar;
                    if (/(MSIE\ [0-9]{1})/i.test(navigator.userAgent)) {
                        window.opener.$(window.opener.loadExistingFiles());
                        myVar = setTimeout(function(){
                            window.opener.$('tbody.files').find('tr .preview a[title="<?php echo $fileName;?>"]').click();
                        },1000);
                    }
                    else{
                        window.opener.$.when(window.opener.loadExistingFiles()).done(function(){
                            myVar = setTimeout(function(){
                                window.opener.$('tbody.files').find('tr .preview a[title="<?php echo $fileName;?>"]').click();
                            },1000);
                        });
                    }

            </script>
<?php
            echo "<h2>The selected images have been uploaded successfully.</h2>";
            //echo "<h3>Please click on \"Proceed With Uploaded Images\" button to Proceed OR ";
            //echo "Click on the \"Upload More Images\" Button to upload more images.</h3>";
?>
            <div class="modal-footer">
                <input type='button' name='continue' value='Upload More Images' class='btn btn-primary' onclick='loader(); window.location.href="https://www.picpixa.com/wp-content/plugins/create-own-object/plugin-facebook/index.php";'>
                <!-- <input type='button' name='closeWindow' value='Close' class='btn btn-primary' onClick="window.close();"> -->
            </div>
<?php
            die();          
        }
        elseif (isset($_POST['copy']))
        {
            echo "<h2>You have not selected any image(s) to move.</h2><br><br>";
            //echo "<h3>Please click on \"Close\" button to Close the window OR ";
            //echo "Click on the \"Upload Images\" Button to upload images.</h3>";
?>
            <div class="modal-footer">
                <input type='button' name='continue' value='Upload Images' class='btn btn-primary' onclick='loader(); window.location.href="https://www.picpixa.com/wp-content/plugins/create-own-object/plugin-facebook/index.php";'>
                <!-- <input type='button' name='closeWindow' value='Close' class='btn btn-primary' onClick="window.close();"> -->
            </div>
<?php
            die();
        }
?>
    <body>
        <div id="load" class="loader"></div>
<?php 
        if(isset($_GET['logout']) && $_GET['logout']=='yes'){
            $user = null;
            $facebook->logoutFromSite();
        }

        if (!$user){
?>
            <div id="wrap">
                <div class="header">
                    <h4>Facebook</h4>
                    <p>Display Your Photo Stream</p>
                </div>
                <div class="cl"></div>
                <div id="middal_part">
                    <div class="left_side">
                        <img src="Images/fb.jpg"/>
                    </div>
                    <div class="right_side">
                        <a class="btn btn-primary" href="<?= $loginUrl ?>" onClick='loader()'>Login</a><br><br>
                        <!-- <button class="btn btn-primary close_window" type="button"  onClick="window.close();">Close</button> -->
                    </div>
                    <div class="cl"></div>
                </div>
            </div>
<?php
        }// else { ?>
            <!--    <div class="right_side">
                        <a href="<? //= $logoutUrl; ?>" onClick='loader()'><img src="Images/logout.png"></a>
                    </div> -->
        <?php //} ?>

        <!-- all time check if user session is valid or not -->
<?php
        if ($user) {
?>
            <form method="post" action="index.php">
                <div class="container">
                    <div class="row">
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                            <div class="modal-body">
                                <div id='folderList'>

                                <!-- Data retrived from user profile are shown here -->
<?php
                                $albums = $facebook->api("/me/albums");
                                foreach($albums['data'] as $album){
                                    ?><div class="baby_img">
                                            <a href="#" onclick="loader();showAlbum('<?php echo $album['id']; ?>')">
                                                <img src="Images/folder.jpeg" style="width:150px !important; height:120px !important;">
                                                <div style="clear: both;"></div><?php echo $album['name']; ?>
                                            </a>
                                </div><?php
                                }
?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div id="fbhtmlImage">
                    <!-- Album images are being loaded here. -->
                </div>
                <!--<div class="modal-footer" id="fbmodal-footer" <?php if(!$showBtn){ ?> style="display:none;"<?php  } ?> >
                    <input type='submit' name='copy' value='Copy Selected Files' class='btn btn-primary' onclick='loader()' />
                    <input type='button' name='closeWindow' value='Close This Window' class='btn btn-primary' onClick="window.close();">
                </div>-->



            </form>
        <?php } ?>

        <script type="text/javascript">
            function backToAlbums(){
                $('#fbhtmlImage').hide();
                $('#fbmodal-footer').hide();
                $('#folderList').show();
            }

            function showAlbum(albumId){
                $('#folderList').hide();
                var fbhtml = '';
                //fbhtml = document.write("<a class='btn btn-primary' href='" + document.referrer + "'>Back</a>");
                fbhtml += "<div class='container'>\
                    <div class='row'>\
                        <div class='col-lg-12 col-md-12 col-sm-12 col-xs-12'>\
                            <div class='modal-body'>\
                                <a class='btn btn-primary' href='javascript: void(0);' onClick='backToAlbums();'>Back</a>\
                            </div>\
                            <div class='clearfix'></div>\
                        </div>\
                    </div>\
                </div>";
                $.ajax({                    
                     type: "GET",
                     url: "fbphotos.php",
                     data: { albumId: albumId }

                    }).done(function(response) {
                        var response = jQuery.parseJSON(response );
                        var showBtn = false;
                        fbhtml += "<div class='container'>\
                            <div class='row'>\
                                <div class='col-lg-12 col-md-12 col-sm-12 col-xs-12'>\
                                    <div class='modal-body'>";

                                        if(response['data'].length>0)
                                        {
                                            for(var x=0; x<response['data'].length; x++){
                                                fbhtml += '<div class="baby_img">';
                                                fbhtml += '<input type="checkbox" id="facebook_'+x+'" name="facebook[]" value="'+response['data'][x]['source']+'" class="styled" />';
                                                fbhtml += '<img src="'+response['data'][x]['source']+'" class="img-responsive" style="width:100px !important; height:100px !important;" />';
                                                fbhtml += '</div>';
                                                showBtn = true;
                                            }
                                        }
                                        else
                                        {
                                            fbhtml += '<div class="baby_img">';
                                            fbhtml += 'There are no image(s) in this album.';
                                            fbhtml += '</div>';
                                        }

                                    fbhtml += "</div>\
                                    <div class='clearfix'></div>\
                                    <div class='modal-footer'>";
                                    if(showBtn == true)
                                    {
                                        fbhtml += "<input type='submit' name='copy' value='Copy Selected Files' class='btn btn-primary' onclick='loader()' />";
                                    }

                                /*  fbhtml += "<input type='button' name='closeWindow' value='Close This Window' class='btn btn-primary' onClick='window.close();'>\
                                    </div>\
                                </div>\
                            </div>\
                        </div>";*/
                                    fbhtml += "</div>\
                                    </div>\
                                </div>\
                            </div>";

                        //$('#fbhtmlImage').html(''); // does blank
                        //alert($('#fbhtmlImage').html()); //gets images as alert($('#fbhtmlImage').html()); 
                        $('#fbhtmlImage').html(fbhtml);
                        $('#fbhtmlImage').show();
                        //$('#fbmodal-footer').show();

                        //alert("Div filled");
                        $(".loader").fadeOut("slow");
                });
            }
        </script>


    </body>
</html>

1 个答案:

答案 0 :(得分:0)

这是对的。用户之前已授予授权。因此,如果用户未撤消应用程序权限,则应用程序将需要再次重新进行身份验证。如果用户从Facebook注销,您/或SDK应该通过登录对话框提示他。不需要对同一个应用程序进行身份验证。

请阅读https://developers.facebook.com/docs/facebook-login/login-flow-for-web/v2.1部分“Looging people in”部分。