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

/application/controllers/hauth.php

https://bitbucket.org/DaveMC08/hybridigniter
PHP | 261 lines | 221 code | 30 blank | 10 comment | 38 complexity | cd3edb88265fe9c2bb0fe8d95ee5e18d MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class HAuth extends CI_Controller {
  3. public function __construct()
  4. {
  5. // Constructor to auto-load HybridAuthLib
  6. parent::__construct();
  7. $this->load->library('HybridAuthLib');
  8. }
  9. public function index()
  10. {
  11. // Send to the view all permitted services as a user profile if authenticated
  12. $data['providers'] = $this->hybridauthlib->getProviders();
  13. foreach($data['providers'] as $provider=>$d) {
  14. if ($d['connected'] == 1) {
  15. $data['providers'][$provider]['user_profile'] = $this->hybridauthlib->authenticate($provider)->getUserProfile();
  16. }
  17. }
  18. $this->load->view('hauth/home', $data);
  19. }
  20. public function login($provider)
  21. {
  22. log_message('debug', "controllers.HAuth.login($provider) called");
  23. try
  24. {
  25. log_message('debug', 'controllers.HAuth.login: loading HybridAuthLib');
  26. if ($this->hybridauthlib->providerEnabled($provider))
  27. {
  28. log_message('debug', "controllers.HAuth.login: service $provider enabled, trying to authenticate.");
  29. $service = $this->hybridauthlib->authenticate($provider);
  30. if ($service->isUserConnected())
  31. {
  32. log_message('debug', 'controller.HAuth.login: user authenticated.');
  33. // Redirect back to the index to show the profile
  34. redirect('hauth/', 'refresh');
  35. }
  36. else // Cannot authenticate user
  37. {
  38. show_error('Cannot authenticate user');
  39. }
  40. }
  41. else // This service is not enabled.
  42. {
  43. log_message('error', 'controllers.HAuth.login: This provider is not enabled ('.$provider.')');
  44. show_404($_SERVER['REQUEST_URI']);
  45. }
  46. }
  47. catch(Exception $e)
  48. {
  49. $error = 'Unexpected error';
  50. switch($e->getCode())
  51. {
  52. case 0 : $error = 'Unspecified error.'; break;
  53. case 1 : $error = 'Hybriauth configuration error.'; break;
  54. case 2 : $error = 'Provider not properly configured.'; break;
  55. case 3 : $error = 'Unknown or disabled provider.'; break;
  56. case 4 : $error = 'Missing provider application credentials.'; break;
  57. case 5 : log_message('debug', 'controllers.HAuth.login: Authentification failed. The user has canceled the authentication or the provider refused the connection.');
  58. //redirect();
  59. if (isset($service))
  60. {
  61. log_message('debug', 'controllers.HAuth.login: logging out from service.');
  62. $service->logout();
  63. }
  64. show_error('User has cancelled the authentication or the provider refused the connection.');
  65. break;
  66. case 6 : $error = 'User profile request failed. Most likely the user is not connected to the provider and he should to authenticate again.';
  67. break;
  68. case 7 : $error = 'User not connected to the provider.';
  69. break;
  70. }
  71. if (isset($service))
  72. {
  73. $service->logout();
  74. }
  75. log_message('error', 'controllers.HAuth.login: '.$error);
  76. show_error('Error authenticating user.');
  77. }
  78. }
  79. public function logout($provider = "")
  80. {
  81. log_message('debug', "controllers.HAuth.logout($provider) called");
  82. try
  83. {
  84. if ($provider == "") {
  85. log_message('debug', "controllers.HAuth.logout() called, no provider specified. Logging out of all services.");
  86. $data['service'] = "all";
  87. $this->hybridauthlib->logoutAllProviders();
  88. } else {
  89. if ($this->hybridauthlib->providerEnabled($provider)) {
  90. log_message('debug', "controllers.HAuth.logout: service $provider enabled, trying to check if user is authenticated.");
  91. $service = $this->hybridauthlib->authenticate($provider);
  92. if ($service->isUserConnected()) {
  93. log_message('debug', 'controller.HAuth.logout: user is authenticated, logging out.');
  94. $service->logout();
  95. $data['service'] = $provider;
  96. } else { // Cannot authenticate user
  97. show_error('User not authenticated, success.');
  98. $data['service'] = $provider;
  99. }
  100. } else { // This service is not enabled.
  101. log_message('error', 'controllers.HAuth.login: This provider is not enabled ('.$provider.')');
  102. show_404($_SERVER['REQUEST_URI']);
  103. }
  104. }
  105. // Redirect back to the main page. We're done with logout
  106. redirect('hauth/', 'refresh');
  107. }
  108. catch(Exception $e)
  109. {
  110. $error = 'Unexpected error';
  111. switch($e->getCode())
  112. {
  113. case 0 : $error = 'Unspecified error.'; break;
  114. case 1 : $error = 'Hybriauth configuration error.'; break;
  115. case 2 : $error = 'Provider not properly configured.'; break;
  116. case 3 : $error = 'Unknown or disabled provider.'; break;
  117. case 4 : $error = 'Missing provider application credentials.'; break;
  118. case 5 : log_message('debug', 'controllers.HAuth.login: Authentification failed. The user has canceled the authentication or the provider refused the connection.');
  119. //redirect();
  120. if (isset($service))
  121. {
  122. log_message('debug', 'controllers.HAuth.login: logging out from service.');
  123. $service->logout();
  124. }
  125. show_error('User has cancelled the authentication or the provider refused the connection.');
  126. break;
  127. case 6 : $error = 'User profile request failed. Most likely the user is not connected to the provider and he should to authenticate again.';
  128. break;
  129. case 7 : $error = 'User not connected to the provider.';
  130. break;
  131. }
  132. if (isset($service))
  133. {
  134. $service->logout();
  135. }
  136. log_message('error', 'controllers.HAuth.login: '.$error);
  137. show_error('Error authenticating user.');
  138. }
  139. }
  140. // Little json api and variable output testing function. Make it easy with JS to verify a session. ;)
  141. public function status($provider = "")
  142. {
  143. try
  144. {
  145. if ($provider == "") {
  146. log_message('debug', "controllers.HAuth.status($provider) called, no provider specified. Providing details on all connected services.");
  147. $connected = $this->hybridauthlib->getConnectedProviders();
  148. if (count($connected) == 0) {
  149. $data['status'] = "User not authenticated.";
  150. } else {
  151. $connected = $this->hybridauthlib->getConnectedProviders();
  152. foreach($connected as $provider) {
  153. if ($this->hybridauthlib->providerEnabled($provider)) {
  154. log_message('debug', "controllers.HAuth.status: service $provider enabled, trying to check if user is authenticated.");
  155. $service = $this->hybridauthlib->authenticate($provider);
  156. if ($service->isUserConnected()) {
  157. log_message('debug', 'controller.HAuth.status: user is authenticated to $provider, providing profile.');
  158. $data['status'][$provider] = (array)$this->hybridauthlib->getAdapter($provider)->getUserProfile();
  159. } else { // Cannot authenticate user
  160. $data['status'][$provider] = "User not authenticated.";
  161. }
  162. } else { // This service is not enabled.
  163. log_message('error', 'controllers.HAuth.status: This provider is not enabled ('.$provider.')');
  164. $data['status'][$provider] = "provider not enabled.";
  165. }
  166. }
  167. }
  168. } else {
  169. if ($this->hybridauthlib->providerEnabled($provider)) {
  170. log_message('debug', "controllers.HAuth.status: service $provider enabled, trying to check if user is authenticated.");
  171. $service = $this->hybridauthlib->authenticate($provider);
  172. if ($service->isUserConnected()) {
  173. log_message('debug', 'controller.HAuth.status: user is authenticated to $provider, providing profile.');
  174. $data['status'][$provider] = (array)$this->hybridauthlib->getAdapter($provider)->getUserProfile();
  175. } else { // Cannot authenticate user
  176. $data['status'] = "User not authenticated.";
  177. }
  178. } else { // This service is not enabled.
  179. log_message('error', 'controllers.HAuth.status: This provider is not enabled ('.$provider.')');
  180. $data['status'] = "provider not enabled.";
  181. }
  182. }
  183. $this->load->view('hauth/status', $data);
  184. }
  185. catch(Exception $e)
  186. {
  187. $error = 'Unexpected error';
  188. switch($e->getCode())
  189. {
  190. case 0 : $error = 'Unspecified error.'; break;
  191. case 1 : $error = 'Hybriauth configuration error.'; break;
  192. case 2 : $error = 'Provider not properly configured.'; break;
  193. case 3 : $error = 'Unknown or disabled provider.'; break;
  194. case 4 : $error = 'Missing provider application credentials.'; break;
  195. case 5 : log_message('debug', 'controllers.HAuth.login: Authentification failed. The user has canceled the authentication or the provider refused the connection.');
  196. //redirect();
  197. if (isset($service))
  198. {
  199. log_message('debug', 'controllers.HAuth.login: logging out from service.');
  200. $service->logout();
  201. }
  202. show_error('User has cancelled the authentication or the provider refused the connection.');
  203. break;
  204. case 6 : $error = 'User profile request failed. Most likely the user is not connected to the provider and he should to authenticate again.';
  205. break;
  206. case 7 : $error = 'User not connected to the provider.';
  207. break;
  208. }
  209. if (isset($service))
  210. {
  211. $service->logout();
  212. }
  213. log_message('error', 'controllers.HAuth.login: '.$error);
  214. show_error('Error authenticating user.');
  215. }
  216. }
  217. public function endpoint()
  218. {
  219. log_message('debug', 'controllers.HAuth.endpoint called.');
  220. log_message('info', 'controllers.HAuth.endpoint: $_REQUEST: '.print_r($_REQUEST, TRUE));
  221. if ($_SERVER['REQUEST_METHOD'] === 'GET')
  222. {
  223. log_message('debug', 'controllers.HAuth.endpoint: the request method is GET, copying REQUEST array into GET array.');
  224. $_GET = $_REQUEST;
  225. }
  226. log_message('debug', 'controllers.HAuth.endpoint: loading the original HybridAuth endpoint script.');
  227. require_once APPPATH.'/third_party/hybridauth/index.php';
  228. }
  229. }
  230. /* End of file hauth.php */
  231. /* Location: ./application/controllers/hauth.php */