Skip to content

Commit

Permalink
spent some time commenting the code so hoefully it become more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
hybridauth committed Nov 23, 2011
1 parent a940d1c commit a7df270
Show file tree
Hide file tree
Showing 24 changed files with 332 additions and 317 deletions.
60 changes: 33 additions & 27 deletions hybridauth/Hybrid/Auth.php
Expand Up @@ -5,10 +5,13 @@
* (c) 2009-2011 HybridAuth authors | hybridauth.sourceforge.net/licenses.html
*/

// ------------------------------------------------------------------------
// The main file to include in Hybrid_Auth package
// ------------------------------------------------------------------------

/**
* Hybrid_Auth class
*
* Hybrid_Auth class provide a simple way to authenticate users via OpenID and OAuth.
*
* Generally, Hybrid_Auth is the only class you should instanciate and use throughout your application.
*/
class Hybrid_Auth
{
public static $version = "2.0.9";
Expand All @@ -25,6 +28,11 @@ class Hybrid_Auth

/**
* Try to start a new session of none then initialize Hybrid_Auth
*
* Hybrid_Auth constructor will require either a valid config array or
* a path for a configuration file as parameter. To know more please
* refer to the Configuration section:
* http://hybridauth.sourceforge.net/userguide/Configuration.html
*/
function __construct( $config )
{
Expand All @@ -45,7 +53,7 @@ function __construct( $config )
throw new Exception('Hybriauth Library needs the JSON PHP extension.');
}

// the PECL extension is present, which is not compatible with this library
// OAuth PECL extension is not compatible with this library
if( extension_loaded('oauth') ) {
throw new Exception('Hybriauth Library not compatible with installed PECL OAuth extension. Please disable it.');
}
Expand All @@ -57,7 +65,7 @@ function __construct( $config )
// --------------------------------------------------------------------

/**
* Try to initialize Hybrid_Auth
* Try to initialize Hybrid_Auth with given $config hash or file
*/
public static function initialize( $config )
{
Expand Down Expand Up @@ -85,7 +93,7 @@ public static function initialize( $config )
$config["debug_file"] = null;
}

# some required includes
# load hybridauth required files, a autoload is on the way...
require_once $config["path_base"] . "Error.php";
require_once $config["path_base"] . "Logger.php";

Expand All @@ -106,7 +114,7 @@ public static function initialize( $config )
// hash given config
Hybrid_Auth::$config = $config;

// start session storage
// start session storage mng
Hybrid_Auth::$store = new Hybrid_Storage();

// instace of errors mng
Expand All @@ -115,10 +123,11 @@ public static function initialize( $config )
// instace of log mng
Hybrid_Auth::$logger = new Hybrid_Logger();

// store php session.. well juste pour faire beau
// store php session and version..
$_SESSION["HA::PHP_SESSION_ID"] = session_id();
$_SESSION["HA::VERSION"] = Hybrid_Auth::$version;

// almost done, check for error then move on
// almost done, check for errors then move on
Hybrid_Logger::info( "Hybrid_Auth::initialize(), stated. Hybrid_Auth has been called from: " . Hybrid_Auth::getCurrentUrl() );
Hybrid_Logger::debug( "Hybrid_Auth initialize. dump used config: ", serialize( $config ) );
Hybrid_Logger::debug( "Hybrid_Auth initialize. dump current session: ", serialize( $_SESSION ) );
Expand All @@ -134,8 +143,7 @@ public static function initialize( $config )
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
// Exception::getPrevious (PHP 5 >= 5.3.0) http://php.net/manual/en/exception.getprevious.php
if ( version_compare( PHP_VERSION, '5.3.0', '>=' ) ) {
throw new Exception( $m, $c, $p );
}
Expand Down Expand Up @@ -166,7 +174,7 @@ public static function storage()
// --------------------------------------------------------------------

/**
* Get the session stored data. To be used in case you want to store session on a more persistent backend
* Get hybridauth session data.
*/
function getSessionData()
{
Expand All @@ -176,7 +184,7 @@ function getSessionData()
// --------------------------------------------------------------------

/**
* set the session data. To be used in case you want to store session on a more persistent backend
* restore hybridauth session data.
*/
function restoreSessionData( $sessiondata = NULL )
{
Expand Down Expand Up @@ -221,7 +229,7 @@ public static function authenticate( $providerId, $params = NULL )
// --------------------------------------------------------------------

/**
* Return the adapter instance for a given provider
* Return the adapter instance for an authenticated provider
*/
public static function getAdapter( $providerId = NULL )
{
Expand Down Expand Up @@ -269,11 +277,10 @@ public static function setup( $providerId, $params = NULL )

/**
* Check if the current user is connected to a given provider
*/
public static function isConnectedWith( $providerId = NULL )
*/
public static function isConnectedWith( $providerId )
{
return
( bool) Hybrid_Auth::storage()->get( "hauth_session.{$providerId}.is_logged_in" );
return (bool) Hybrid_Auth::storage()->get( "hauth_session.{$providerId}.is_logged_in" );
}

// --------------------------------------------------------------------
Expand All @@ -283,22 +290,21 @@ public static function isConnectedWith( $providerId = NULL )
*/
public static function getConnectedProviders()
{
$authenticatedProviders = ARRAY();
$idps = array();

foreach( Hybrid_Auth::$config["providers"] as $idpid => $params ){
if( ( bool) Hybrid_Auth::storage()->get( "hauth_session.{$idpid}.is_logged_in" ) ){
$authenticatedProviders[] = $idpid;
if( Hybrid_Auth::isConnectedWith( $idpid ) ){
$idps[] = $idpid;
}
}

return $authenticatedProviders;
return $idps;
}

// --------------------------------------------------------------------

/**
* A generic function to logout all connected provider at once
* #3435186, http://sourceforge.net/tracker/?func=detail&atid=1195295&aid=3435186&group_id=281757
* A generic function to logout all connected provider at once
*/
public static function logoutAllProviders()
{
Expand All @@ -316,7 +322,7 @@ public static function logoutAllProviders()
/**
* Utility function, redirect to a given URL with php header or using javascript location.href
*/
public static function redirect( $url, $mode = "PHP", $postdata = ARRAY() )
public static function redirect( $url, $mode = "PHP" )
{
Hybrid_Logger::info( "Enter Hybrid_Auth::redirect( $url, $mode )" );

Expand Down
43 changes: 17 additions & 26 deletions hybridauth/Hybrid/Error.php
Expand Up @@ -6,26 +6,28 @@
*/

/**
* The Hybrid_Error manage and store error for HybridAuth
* Errors manager
*
* HybridAuth errors are stored in Hybrid::storage() and not displayed directly to the end user
*/
class Hybrid_Error
{
/**
* store error in HybridAuth cache system
* store error in session
*/
public static function setError( $message, $code = NULL, $trace = NULL, $previous = NULL )
{
Hybrid_Logger::info( "Enter Hybrid_Error::setError( $message )" );

Hybrid_Auth::storage()->set( "hauth_session.error.status" , 1 );
Hybrid_Auth::storage()->set( "hauth_session.error.message" , $message );
Hybrid_Auth::storage()->set( "hauth_session.error.code" , $code );
Hybrid_Auth::storage()->set( "hauth_session.error.trace" , $trace );
Hybrid_Auth::storage()->set( "hauth_session.error.previous", $previous );
Hybrid_Auth::storage()->set( "hauth_session.error.status" , 1 );
Hybrid_Auth::storage()->set( "hauth_session.error.message" , $message );
Hybrid_Auth::storage()->set( "hauth_session.error.code" , $code );
Hybrid_Auth::storage()->set( "hauth_session.error.trace" , $trace );
Hybrid_Auth::storage()->set( "hauth_session.error.previous", $previous );
}

/**
* store error in HybridAuth cache system
* clear the last error
*/
public static function clearError()
{
Expand All @@ -38,52 +40,41 @@ public static function clearError()
Hybrid_Auth::storage()->delete( "hauth_session.error.previous" );
}

/**
* Checks to see if there is a an error.
*
* errors are stored in Hybrid::storage() Hybrid storage system
* and not displayed directly to user
*
/**
* Checks to see if there is a an error.
*
* @return boolean True if there is an error.
*/
public static function hasError()
{
return (bool) Hybrid_Auth::storage()->get( "hauth_session.error.status" );
}

/**
* a naive error message getter
*
* @return string very short error message.
/**
* return error message
*/
public static function getErrorMessage()
{
return Hybrid_Auth::storage()->get( "hauth_session.error.message" );
}

/**
* a naive error code getter
*
* @return int error code defined on Hybrid_Auth.
* return error code
*/
public static function getErrorCode()
{
return Hybrid_Auth::storage()->get( "hauth_session.error.code" );
}

/**
* a naive error backtrace getter
*
* @return string detailled error backtrace as string.
* return string detailled error backtrace as string.
*/
public static function getErrorTrace()
{
return Hybrid_Auth::storage()->get( "hauth_session.error.trace" );
}

/**
* a naive error backtrace getter
*
* @return string detailled error backtrace as string.
*/
public static function getErrorPrevious()
Expand Down
7 changes: 4 additions & 3 deletions hybridauth/Hybrid/Logger.php
Expand Up @@ -6,16 +6,17 @@
*/

/**
* Debugging and Logging class
* Debugging and Logging manager
*/
class Hybrid_Logger
{
function __construct()
{
// if debug mode is set to true, then check for the writable log file
if ( Hybrid_Auth::$config["debug_mode"] ){
if ( ! file_exists( Hybrid_Auth::$config["debug_file"] ) ){
throw new Exception( "'debug_mode' is set to 'true', but the given log file path 'debug_file' do not exist.", 1 );
}
throw new Exception( "'debug_mode' is set to 'true', but no log file path 'debug_file' given.", 1 );
}

if ( ! is_writable( Hybrid_Auth::$config["debug_file"] ) ){
throw new Exception( "'debug_mode' is set to 'true', but the given log file path 'debug_file' is not a writable file.", 1 );
Expand Down

0 comments on commit a7df270

Please sign in to comment.