daily pastebin goal
57%
SHARE
TWEET

Untitled

a guest Sep 24th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var INFO = {
  2.     PHP_SESSION_NAME: "PHPSESSID",
  3.     SAFARI_AUTH_URL: "/safari.php",
  4.     APP_ID: "your app id"
  5. };
  6.  
  7. $(function () {
  8.    
  9.     initFacebook(function () {
  10.         var authResponse = FB.getAuthResponse();
  11.  
  12.         var fields = [];
  13.         var cookieName = "fbsr_" + INFO.APP_ID;
  14.         fields['session_id'] = cookie(INFO.PHP_SESSION_NAME);
  15.         fields['auth_response'] = authResponse || 0;
  16.         fields['app_id'] = INFO.APP_ID;
  17.  
  18.         $.post(
  19.             INFO.SAFARI_AUTH_URL,
  20.             $.param(fields),
  21.             function (response) {
  22.                 // make sure the cookie has been set
  23.                 var sessionId = cookie(INFO.PHP_SESSION_NAME);
  24.                 console.log(sessionId);
  25.                 var authResponse = cookie(cookieName);
  26.                 console.log(authResponse);
  27.             },
  28.             'json'
  29.         );
  30.     });
  31. });
  32.  
  33.  
  34. /**
  35.  * Gets, sets or a deletes a cookie
  36.  * @param name
  37.  *   The name of the cookie
  38.  * @param value
  39.  *   Optional. If passed, this is the new value of the cookie.
  40.  *   If null is passed here, the cookie is "deleted".
  41.  * @param options
  42.  *   Optional hash of options, including:
  43.  *   "expires": number of milliseconds until expiration. Defaults to session cookie.
  44.  *   "domain": the domain to set cookie
  45.  *   "path": path to set cookie. Defaults to location.pathname
  46.  * @return String
  47.  *   If only name was passed, returns the stored value of the cookie, or null.
  48.  */
  49. function cookie(name, value, options) {
  50.     if (typeof value != 'undefined') {
  51.         var path, parts, domain = '';
  52.         parts = Q.info.baseUrl.split('://');
  53.         if (options && ('path' in options)) {
  54.             path = ';path='+options.path;
  55.         } else {
  56.             path = ';path=/' + parts[1].split('/').slice(1).join('/');
  57.         }
  58.         if (options && ('domain' in options)) {
  59.             domain = ';domain='+options.domain;
  60.         } else {
  61.             domain = ';domain=.' + parts[1].split('/').slice(0, 1);
  62.         }
  63.         if (value === null) {
  64.             document.cookie = encodeURIComponent(name)+'=;expires=Thu, 01-Jan-1970 00:00:01 GMT'+path+domain;
  65.             return null;
  66.         }
  67.         var expires = '';
  68.         if (options && options.expires) {
  69.             expires = new Date();
  70.             expires.setTime((new Date()).getTime() + options.expires);
  71.             expires = ';expires='+expires.toGMTString();
  72.         }
  73.         document.cookie = encodeURIComponent(name)+'='+encodeURIComponent(value)+expires+path+domain;
  74.         return null;
  75.     }
  76.  
  77.     // Otherwise, return the value
  78.     var cookies = document.cookie.split(';');
  79.     var result;
  80.     for (var i=0; i<cookies.length; ++i) {
  81.         parts = cookies[i].split('=');
  82.         result = parts.splice(0, 1);
  83.         result.push(parts.join('='));
  84.         if (decodeURIComponent(result[0].trim()) === name) {
  85.             return result.length < 2 ? null : decodeURIComponent(result[1]);
  86.         }
  87.     }
  88.     return null;
  89. };
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. function initFacebook(callback) {
  98.     // should be only called once per app
  99.     if (initFacebook.completed[Q.app]) {
  100.         callback();
  101.         return;
  102.     }
  103.     if (!$('#fb-root').length) {
  104.         $('body').append($('<div id="fb-root" />'));
  105.     }
  106.     Q.addScript((window.location.protocol || 'http') + '//connect.facebook.net/en_US/all.js', function () {
  107.         if (!initFacebook.completed[Q.app]) {
  108.             FB.init(Q.extend({
  109.                 appId: INFO.APP_ID,
  110.                 status: true,
  111.                 cookie: true,
  112.                 oauth: true,
  113.                 xfbml: true
  114.             }));
  115.         }
  116.         initFacebook.completed[Q.app] = true;
  117.         if (callback) {
  118.             callback();
  119.         }
  120.     });
  121. };
  122. initFacebook.completed = {};
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top