diff --git a/e107_handlers/hybridauth/Hybrid/Auth.php b/e107_handlers/hybridauth/Hybrid/Auth.php index b62238694..a388ccfb4 100644 --- a/e107_handlers/hybridauth/Hybrid/Auth.php +++ b/e107_handlers/hybridauth/Hybrid/Auth.php @@ -1,67 +1,84 @@ getSessionData() ); - Hybrid_Logger::info( "Hybrid_Auth initialize: check if any error is stored on the endpoint..." ); + Hybrid_Logger::debug("Hybrid_Auth initialize. dump used config: ", serialize($config)); + Hybrid_Logger::debug("Hybrid_Auth initialize. dump current session: ", Hybrid_Auth::storage()->getSessionData()); + Hybrid_Logger::info("Hybrid_Auth initialize: check if any error is stored on the endpoint..."); - if( Hybrid_Error::hasError() ){ + if (Hybrid_Error::hasError()) { $m = Hybrid_Error::getErrorMessage(); $c = Hybrid_Error::getErrorCode(); $p = Hybrid_Error::getErrorPrevious(); - Hybrid_Logger::error( "Hybrid_Auth initialize: A stored Error found, Throw an new Exception and delete it from the store: Error#$c, '$m'" ); + Hybrid_Logger::error("Hybrid_Auth initialize: A stored Error found, Throw an new Exception and delete it from the store: Error#$c, '$m'"); Hybrid_Error::clearError(); // try to provide the previous if any // Exception::getPrevious (PHP 5 >= 5.3.0) http://php.net/manual/en/exception.getprevious.php - if ( version_compare( PHP_VERSION, '5.3.0', '>=' ) && ($p instanceof Exception) ) { - throw new Exception( $m, $c, $p ); - } - else{ - throw new Exception( $m, $c ); + if (version_compare(PHP_VERSION, '5.3.0', '>=') && ($p instanceof Exception)) { + throw new Exception($m, $c, $p); + } else { + throw new Exception($m, $c); } } - Hybrid_Logger::info( "Hybrid_Auth initialize: no error found. initialization succeed." ); - - // Endof initialize + Hybrid_Logger::info("Hybrid_Auth initialize: no error found. initialization succeed."); } - // -------------------------------------------------------------------- - /** - * Hybrid storage system accessor - * - * Users sessions are stored using HybridAuth storage system ( HybridAuth 2.0 handle PHP Session only) and can be accessed directly by - * Hybrid_Auth::storage()->get($key) to retrieves the data for the given key, or calling - * Hybrid_Auth::storage()->set($key, $value) to store the key => $value set. - */ - public static function storage() - { + * Hybrid storage system accessor + * + * Users sessions are stored using HybridAuth storage system ( HybridAuth 2.0 handle PHP Session only) and can be accessed directly by + * Hybrid_Auth::storage()->get($key) to retrieves the data for the given key, or calling + * Hybrid_Auth::storage()->set($key, $value) to store the key => $value set. + * + * @return Hybrid_Storage + */ + public static function storage() { return Hybrid_Auth::$store; } - // -------------------------------------------------------------------- - /** - * Get hybridauth session data. - */ - function getSessionData() - { + * Get hybridauth session data + * @return string|null + */ + function getSessionData() { return Hybrid_Auth::storage()->getSessionData(); } - // -------------------------------------------------------------------- - /** - * restore hybridauth session data. - */ - function restoreSessionData( $sessiondata = NULL ) - { - Hybrid_Auth::storage()->restoreSessionData( $sessiondata ); + * Restore hybridauth session data + * + * @param string $sessiondata Serialized session data + * @retun void + */ + function restoreSessionData($sessiondata = null) { + Hybrid_Auth::storage()->restoreSessionData($sessiondata); } - // -------------------------------------------------------------------- - /** - * Try to authenticate the user with a given provider. - * - * If the user is already connected we just return and instance of provider adapter, - * ELSE, try to authenticate and authorize the user with the provider. - * - * $params is generally an array with required info in order for this provider and HybridAuth to work, - * like : - * hauth_return_to: URL to call back after authentication is done - * openid_identifier: The OpenID identity provider identifier - * google_service: can be "Users" for Google user accounts service or "Apps" for Google hosted Apps - */ - public static function authenticate( $providerId, $params = NULL ) - { - Hybrid_Logger::info( "Enter Hybrid_Auth::authenticate( $providerId )" ); - - // if user not connected to $providerId then try setup a new adapter and start the login process for this provider - if( ! Hybrid_Auth::storage()->get( "hauth_session.$providerId.is_logged_in" ) ){ - Hybrid_Logger::info( "Hybrid_Auth::authenticate( $providerId ), User not connected to the provider. Try to authenticate.." ); - - $provider_adapter = Hybrid_Auth::setup( $providerId, $params ); + * Try to authenticate the user with a given provider. + * + * If the user is already connected we just return and instance of provider adapter, + * ELSE, try to authenticate and authorize the user with the provider. + * + * $params is generally an array with required info in order for this provider and HybridAuth to work, + * like : + * hauth_return_to: URL to call back after authentication is done + * openid_identifier: The OpenID identity provider identifier + * google_service: can be "Users" for Google user accounts service or "Apps" for Google hosted Apps + * + * @param string $providerId ID of the provider + * @param array $params Params + * @return + */ + public static function authenticate($providerId, $params = null) { + Hybrid_Logger::info("Enter Hybrid_Auth::authenticate( $providerId )"); + if (!Hybrid_Auth::storage()->get("hauth_session.$providerId.is_logged_in")) { + // if user not connected to $providerId then try setup a new adapter and start the login process for this provider + Hybrid_Logger::info("Hybrid_Auth::authenticate( $providerId ), User not connected to the provider. Try to authenticate.."); + $provider_adapter = Hybrid_Auth::setup($providerId, $params); $provider_adapter->login(); - } - - // else, then return the adapter instance for the given provider - else{ - Hybrid_Logger::info( "Hybrid_Auth::authenticate( $providerId ), User is already connected to this provider. Return the adapter instance." ); - - return Hybrid_Auth::getAdapter( $providerId ); + } else { + // else, then return the adapter instance for the given provider + Hybrid_Logger::info("Hybrid_Auth::authenticate( $providerId ), User is already connected to this provider. Return the adapter instance."); + return Hybrid_Auth::getAdapter($providerId); } } - // -------------------------------------------------------------------- - /** - * Return the adapter instance for an authenticated provider - */ - public static function getAdapter( $providerId = NULL ) - { - Hybrid_Logger::info( "Enter Hybrid_Auth::getAdapter( $providerId )" ); - - return Hybrid_Auth::setup( $providerId ); + * Return the adapter instance for an authenticated provider + * + * @param string $providerId ID of the provider + * @return Hybrid_Provider_Adapter + */ + public static function getAdapter($providerId = null) { + Hybrid_Logger::info("Enter Hybrid_Auth::getAdapter( $providerId )"); + return Hybrid_Auth::setup($providerId); } - // -------------------------------------------------------------------- - /** - * Setup an adapter for a given provider - */ - public static function setup( $providerId, $params = NULL ) - { - Hybrid_Logger::debug( "Enter Hybrid_Auth::setup( $providerId )", $params ); + * Setup an adapter for a given provider + * + * @param string $providerId ID of the provider + * @param array $params Adapter params + * @return Hybrid_Provider_Adapter + */ + public static function setup($providerId, $params = null) { + Hybrid_Logger::debug("Enter Hybrid_Auth::setup( $providerId )", $params); - if( ! $params ){ - $params = Hybrid_Auth::storage()->get( "hauth_session.$providerId.id_provider_params" ); - - Hybrid_Logger::debug( "Hybrid_Auth::setup( $providerId ), no params given. Trying to get the stored for this provider.", $params ); + if (!$params) { + $params = Hybrid_Auth::storage()->get("hauth_session.$providerId.id_provider_params"); + + Hybrid_Logger::debug("Hybrid_Auth::setup( $providerId ), no params given. Trying to get the stored for this provider.", $params); } - if( ! $params ){ - $params = ARRAY(); - - Hybrid_Logger::info( "Hybrid_Auth::setup( $providerId ), no stored params found for this provider. Initialize a new one for new session" ); + if (!$params) { + $params = array(); + Hybrid_Logger::info("Hybrid_Auth::setup( $providerId ), no stored params found for this provider. Initialize a new one for new session"); } - if( is_array($params) && ! isset( $params["hauth_return_to"] ) ){ - $params["hauth_return_to"] = Hybrid_Auth::getCurrentUrl(); - - Hybrid_Logger::debug( "Hybrid_Auth::setup( $providerId ). HybridAuth Callback URL set to: ", $params["hauth_return_to"] ); + if (is_array($params) && !isset($params["hauth_return_to"])) { + $params["hauth_return_to"] = Hybrid_Auth::getCurrentUrl(); + Hybrid_Logger::debug("Hybrid_Auth::setup( $providerId ). HybridAuth Callback URL set to: ", $params["hauth_return_to"]); } # instantiate a new IDProvider Adapter - $provider = new Hybrid_Provider_Adapter(); - - $provider->factory( $providerId, $params ); - + $provider = new Hybrid_Provider_Adapter(); + $provider->factory($providerId, $params); return $provider; - } - - // -------------------------------------------------------------------- - - /** - * Check if the current user is connected to a given provider - */ - public static function isConnectedWith( $providerId ) - { - return (bool) Hybrid_Auth::storage()->get( "hauth_session.{$providerId}.is_logged_in" ); } - // -------------------------------------------------------------------- + /** + * Check if the current user is connected to a given provider + * + * @param string $providerId ID of the provider + * @return bool + */ + public static function isConnectedWith($providerId) { + return (bool) Hybrid_Auth::storage()->get("hauth_session.{$providerId}.is_logged_in"); + } /** - * Return array listing all authenticated providers - */ - public static function getConnectedProviders() - { + * Return array listing all authenticated providers + * @return array + */ + public static function getConnectedProviders() { $idps = array(); - foreach( Hybrid_Auth::$config["providers"] as $idpid => $params ){ - if( Hybrid_Auth::isConnectedWith( $idpid ) ){ + foreach (Hybrid_Auth::$config["providers"] as $idpid => $params) { + if (Hybrid_Auth::isConnectedWith($idpid)) { $idps[] = $idpid; } } @@ -303,20 +304,26 @@ class Hybrid_Auth return $idps; } - // -------------------------------------------------------------------- - /** - * Return array listing all enabled providers as well as a flag if you are connected. - */ - public static function getProviders() - { + * Return array listing all enabled providers as well as a flag if you are connected + * + * + * array( + * 'Facebook' => array( + * 'connected' => true + * ) + * ) + * + * @return array + */ + public static function getProviders() { $idps = array(); - foreach( Hybrid_Auth::$config["providers"] as $idpid => $params ){ - if($params['enabled']) { - $idps[$idpid] = array( 'connected' => false ); + foreach (Hybrid_Auth::$config["providers"] as $idpid => $params) { + if ($params['enabled']) { + $idps[$idpid] = array('connected' => false); - if( Hybrid_Auth::isConnectedWith( $idpid ) ){ + if (Hybrid_Auth::isConnectedWith($idpid)) { $idps[$idpid]['connected'] = true; } } @@ -325,35 +332,36 @@ class Hybrid_Auth return $idps; } - // -------------------------------------------------------------------- - /** - * A generic function to logout all connected provider at once - */ - public static function logoutAllProviders() - { + * A generic function to logout all connected provider at once + * @return void + */ + public static function logoutAllProviders() { $idps = Hybrid_Auth::getConnectedProviders(); - foreach( $idps as $idp ){ - $adapter = Hybrid_Auth::getAdapter( $idp ); - + foreach ($idps as $idp) { + $adapter = Hybrid_Auth::getAdapter($idp); $adapter->logout(); } } - // -------------------------------------------------------------------- - /** - * Utility function, redirect to a given URL with php header or using javascript location.href - */ - public static function redirect( $url, $mode = "PHP" ) - { - Hybrid_Logger::info( "Enter Hybrid_Auth::redirect( $url, $mode )" ); + * Utility function, redirect to a given URL with php header or using javascript location.href + * + * @param string $url URL to redirect to + * @param string $mode PHP|JS + */ + public static function redirect($url, $mode = "PHP") { + Hybrid_Logger::info("Enter Hybrid_Auth::redirect( $url, $mode )"); - if( $mode == "PHP" ){ - header( "Location: $url" ) ; + // Ensure session is saved before sending response, see https://github.com/symfony/symfony/pull/12341 + if ((PHP_VERSION_ID >= 50400 && PHP_SESSION_ACTIVE === session_status()) || (PHP_VERSION_ID < 50400 && isset($_SESSION) && session_id())) { + session_write_close(); } - elseif( $mode == "JS" ){ + + if ($mode == "PHP") { + header("Location: $url"); + } elseif ($mode == "JS") { echo ''; echo ''; echo '