PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/local/Excellence/Social/Block/Linkedln/Index.php

https://bitbucket.org/excellencetechnologies/sociallogin
PHP | 146 lines | 108 code | 17 blank | 21 comment | 16 complexity | e36e5a1f409fd59a5165103691a7acc3 MD5 | raw file
  1. <?php
  2. class Excellence_Social_Block_Linkedln_Index extends Excellence_Social_Block_Linkedln{
  3. public function __construct(){
  4. parent::__construct();
  5. if($this->getRequest()->getPost('linkedln_oauth')){
  6. return;
  7. }
  8. if(isset($_REQUEST['close'])){
  9. $url = $this->getUrl('*/*/*',array('_current'=>true));
  10. $url = str_replace('close=true&', '', $url);
  11. $url = str_replace('close=true', '', $url);
  12. return $this->killPopup($url);
  13. }
  14. $API_CONFIG = $this->getApiConfig();
  15. $param = $this->getRequest()->getParam(LINKEDIN::_GET_TYPE,false);
  16. switch($param) {
  17. case 'initiate':
  18. /**
  19. * Handle user initiated LinkedIn connection, create the LinkedIn object.
  20. */
  21. // set the callback url
  22. if($this->getRequest()->getParam('popup',false)){
  23. $API_CONFIG['callbackUrl'] = Mage::getUrl('social/linkedln/login') .'?' . LINKEDIN::_GET_TYPE . '=initiate&' . LINKEDIN::_GET_RESPONSE . '=1&close=true';
  24. }else{
  25. $API_CONFIG['callbackUrl'] = Mage::getUrl('social/linkedln/login') .'?' . LINKEDIN::_GET_TYPE . '=initiate&' . LINKEDIN::_GET_RESPONSE . '=1';
  26. }
  27. $OBJ_linkedin = new LinkedIn($API_CONFIG);
  28. // check for response from LinkedIn
  29. $_GET[LINKEDIN::_GET_RESPONSE] = (isset($_GET[LINKEDIN::_GET_RESPONSE])) ? $_GET[LINKEDIN::_GET_RESPONSE] : '';
  30. if(!$_GET[LINKEDIN::_GET_RESPONSE]) {
  31. // LinkedIn hasn't sent us a response, the user is initiating the connection
  32. // send a request for a LinkedIn access token
  33. $response = $OBJ_linkedin->retrieveTokenRequest();
  34. if($response['success'] === TRUE) {
  35. // store the request token
  36. $_SESSION['oauth']['linkedin']['request'] = $response['linkedin'];
  37. // redirect the user to the LinkedIn authentication/authorisation page to initiate validation.
  38. header('Location: ' . LINKEDIN::_URL_AUTHENTICATE . $response['linkedin']['oauth_token']);
  39. exit;
  40. } else {
  41. // bad token request
  42. $data = array();
  43. $data['error'] = "Request token retrieval failed:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response, TRUE) . "</pre><br /><br />LINKEDIN OBJ:<br /><br /><pre>" . print_r($OBJ_linkedin, TRUE) . "</pre>";
  44. $data['type'] = 'linkedln';
  45. Mage::register('oauth_customer_data', $data);
  46. return;
  47. }
  48. } else {
  49. // LinkedIn has sent a response, user has granted permission, take the temp access token, the user's secret and the verifier to request the user's real secret key
  50. $response = $OBJ_linkedin->retrieveTokenAccess($_SESSION['oauth']['linkedin']['request']['oauth_token'], $_SESSION['oauth']['linkedin']['request']['oauth_token_secret'], $_GET['oauth_verifier']);
  51. if($response['success'] === TRUE) {
  52. // the request went through without an error, gather user's 'access' tokens
  53. if($response['linkedin']) {
  54. $OBJ_linkedin = new LinkedIn($API_CONFIG);
  55. $OBJ_linkedin->setTokenAccess($response['linkedin']);
  56. $OBJ_linkedin->setResponseFormat(LINKEDIN::_RESPONSE_JSON);
  57. $response = $OBJ_linkedin->profile('~:(id,first-name,last-name,picture-url)');
  58. if($response['success'] === TRUE) {
  59. $response = json_decode($response['linkedin'],true);
  60. $data = array();
  61. $data['social_id'] = $response['id'];
  62. $data['firstname'] = $response['firstName'];
  63. $data['lastname'] = $response['lastName'];
  64. $data['social_data'] = $response;
  65. $data['social_type'] = 'linkedln';
  66. Mage::register('oauth_customer_data', $data);
  67. } else {
  68. // request failed
  69. $data = array();
  70. $data['error'] = "Error retrieving profile information:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response) . "</pre>";
  71. $data['type'] = 'linkedln';
  72. Mage::register('oauth_customer_data', $data);
  73. return;
  74. }
  75. }
  76. } else {
  77. // bad token access
  78. $data = array();
  79. $data['error'] = "Access token retrieval failed:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response, TRUE) . "</pre><br /><br />LINKEDIN OBJ:<br /><br /><pre>" . print_r($OBJ_linkedin, TRUE) . "</pre>";
  80. $data['type'] = 'linkedln';
  81. Mage::register('oauth_customer_data', $data);
  82. return;
  83. }
  84. }
  85. break;
  86. case 'revoke':
  87. /**
  88. * Handle authorization revocation.
  89. */
  90. // check the session
  91. if(!oauth_session_exists()) {
  92. $data = array();
  93. $data['error'] = 'This script requires session support, which doesn\'t appear to be working correctly.';
  94. $data['error'] = 'linkedln';
  95. Mage::register('oauth_customer_data', $data);
  96. return;
  97. }
  98. $OBJ_linkedin = new LinkedIn($API_CONFIG);
  99. $OBJ_linkedin->setTokenAccess($_SESSION['oauth']['linkedin']['access']);
  100. $response = $OBJ_linkedin->revoke();
  101. if($response['success'] === TRUE) {
  102. // revocation successful, clear session
  103. session_unset();
  104. $_SESSION = array();
  105. if(session_destroy()) {
  106. // session destroyed
  107. header('Location: ' . $this->getUrl('customer/account/login',array('_secure'=>true)));
  108. exit;
  109. } else {
  110. // session not destroyed
  111. $data = array();
  112. $data['error'] = "Error clearing user's session";
  113. $data['error'] = 'linkedln';
  114. Mage::register('oauth_customer_data', $data);
  115. return;
  116. }
  117. } else {
  118. // revocation failed
  119. $data = array();
  120. $data['error'] = "Error revoking user's token:<br /><br />RESPONSE:<br /><br /><pre>" . print_r($response, TRUE) . "</pre><br /><br />LINKEDIN OBJ:<br /><br /><pre>" . print_r($OBJ_linkedin, TRUE) . "</pre>";
  121. $data['error'] = 'linkedln';
  122. Mage::register('oauth_customer_data', $data);
  123. return;
  124. }
  125. break;
  126. }
  127. }
  128. }