daily pastebin goal
38%
SHARE
TWEET

PP: PF Google Login Website Widget

globiws Jun 13th, 2018 (edited) 7 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* google login
  2.  * will do the following:
  3.  *  - allow visitors to authenticate using Google+
  4.  *  - show custom content from Podio once logged in
  5.  */
  6.  
  7. // configuration
  8. $api_service = "PFdemoG"; // the name you gave your podio integration on the Account page under "API Services"
  9. $customers_app_id = 1234567; // App in Podio to search for customers Google email address
  10. $email_field_id = "google-email-address"; // field in above app that has the email address
  11. $content_field_id = "page-content"; // multi-line text field in above app to show to customer when logged in
  12. $public_page_url = "http://globi.ca/ppdemo_login_google.php"; // url of page on your website that's calling this widget
  13. // end configuration
  14.  
  15. $data = @json_decode($pf_payload, true);
  16. $pfsessid = false;
  17. $userEmail = false;
  18.  
  19. // are we coming back from authentication?
  20. if ( isset($data['GET']) && isset($data['GET']['ok']) && isset($data['GET']['session_id']) ) {
  21.     if ( $data['GET']['ok'] != 1 ) return "Authentication Failed";
  22.     if ( empty($data['GET']['session_id']) ) return "No Session ID";
  23.     $pfsessid = $data['GET']['session_id'];
  24.     pf_session_start($pfsessid);
  25.     $session = [];
  26.     $user = @json_decode(urldecode($data['GET']['data']), true);
  27.     if ( $user === false ) return "unknown user";
  28.     //$session['userEmail'] = $user['user']['mail']; // Podio
  29.     $session['userEmail'] = $user['emails'][0]['value']; // Google Plus
  30.     pf_session_set($session);
  31.     $url = $public_page_url . "?pfsessid=" . $pfsessid;
  32.     return('<script>document.location.href="' . ($url) . '";</script>');
  33. }
  34.  
  35. // normal page load: expect format: /widgets/html/widget_id?pfsessid=session_id
  36.  
  37. // make sure we have a valid session ID
  38. if ( ! isset($data['GET']) || ! isset($data['GET']['pfsessid']) ) return "Bad Session ID";
  39. $pfsessid = $data['GET']['pfsessid'];
  40. pf_session_start($pfsessid);
  41. $session = pf_session_get();
  42.  
  43. // check if we're logged in
  44. if ( ! isset($session['userEmail']) ) {
  45.     $url = call_pf_script("api_login_ident.pf", ["api_service" => $api_service, "widget_id" => $data['widget_id'], "session_id" => $pfsessid]);
  46.     $html = '<h3>Login Required</h3>';
  47.     $html .= '<p>Please login using the button below.</p>';
  48.     $html .= '<p><a href="' . $url . '"><img src="https://developers.google.com/identity/images/btn_google_signin_dark_normal_web.png"></a></p>';
  49.     return $html;
  50. }
  51.  
  52. // OK - we're logged in
  53. $userEmail = $session['userEmail'];
  54.  
  55. // Do we have an authorized record
  56. if ( ! isset($session['userRecord']) ) {
  57.     $customers = @json_decode(call_pf_script("podio_search_app.pf", ["app_id" => $customers_app_id, "field_id" => $email_field_id, "search_val" => $userEmail, "condition" => "E", "max_results" => 1]), true);
  58.     if ( $customers === null || sizeof($customers) == 0 ) {
  59.         // no customer record for this email address
  60.         $html = '<h3>Ooops!</h3><p>It seems that ' . $userEmail . ' does not have access to this page.<p>';
  61.         $html .= '<p><i>{perhaps offer a link to a webform to request access here}</i></p>';
  62.         return $html;
  63.     }
  64.     $session['userRecord'] = $customers[0];
  65.     pf_session_set($session);
  66. }
  67.  
  68. // OK - we're logged in and have a user record
  69. // so show the multi-line text field on the user record to the user
  70. $html = '<h3>Hello ' . $userEmail . '</h3>';;
  71. foreach ( $session['userRecord']['fields'] as $field ) {
  72.     if ( $field['external_id'] == $content_field_id ) {
  73.         $html .= $field['values'][0]['value'];
  74.     }
  75. }
  76.  
  77. return $html;
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