summaryrefslogtreecommitdiff
path: root/hauth/Hybrid/Auth.php
diff options
context:
space:
mode:
Diffstat (limited to 'hauth/Hybrid/Auth.php')
-rw-r--r--hauth/Hybrid/Auth.php277
1 files changed, 138 insertions, 139 deletions
diff --git a/hauth/Hybrid/Auth.php b/hauth/Hybrid/Auth.php
index aa0a610..fb30ef9 100644
--- a/hauth/Hybrid/Auth.php
+++ b/hauth/Hybrid/Auth.php
@@ -1,11 +1,11 @@
<?php
-
+
/**
* HybridAuth
* http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth
* (c) 2009-2017, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html
- */
-
+ */
+
/**
* Hybrid_Auth class
*
@@ -13,34 +13,34 @@
*
* Generally, Hybrid_Auth is the only class you should instanciate and use throughout your application.
*/
-class Hybrid_Auth {
-
- public static $version = "2.14.0";
-
+class Hybrid_Auth {
+
+ public static $version = "2.14.0";
+
/**
* Configuration array
* @var array
*/
- public static $config = array();
-
+ public static $config = [];
+
/**
* Auth cache
* @var Hybrid_Storage
*/
- public static $store = null;
-
+ public static $store = null;
+
/**
* Error pool
* @var Hybrid_Error
*/
- public static $error = null;
-
+ public static $error = null;
+
/**
* Logger
* @var Hybrid_Logger
*/
- public static $logger = null;
-
+ public static $logger = null;
+
/**
* Try to start a new session of none then initialize Hybrid_Auth
*
@@ -53,8 +53,8 @@ class Hybrid_Auth {
*/
function __construct($config) {
Hybrid_Auth::initialize($config);
- }
-
+ }
+
/**
* Try to initialize Hybrid_Auth with given $config hash or file
*
@@ -64,115 +64,114 @@ class Hybrid_Auth {
*/
public static function initialize($config) {
if (!is_array($config) && !file_exists($config)) {
- throw new Exception("Hybriauth config does not exist on the given path.", 1);
- }
-
+ throw new Exception("Hybriauth config does not exist on the given path.", 1);
+ }
+
if (!is_array($config)) {
$config = include $config;
- }
-
+ }
+
// build some need'd paths
$config["path_base"] = realpath(dirname(__FILE__)) . "/";
$config["path_libraries"] = $config["path_base"] . "thirdparty/";
$config["path_resources"] = $config["path_base"] . "resources/";
- $config["path_providers"] = $config["path_base"] . "Providers/";
-
+ $config["path_providers"] = $config["path_base"] . "Providers/";
+
// reset debug mode
if (!isset($config["debug_mode"])) {
$config["debug_mode"] = false;
$config["debug_file"] = null;
- }
-
+ }
+
# load hybridauth required files, a autoload is on the way...
require_once $config["path_base"] . "Error.php";
require_once $config["path_base"] . "Exception.php";
- require_once $config["path_base"] . "Logger.php";
-
- require_once $config["path_base"] . "Provider_Adapter.php";
-
+ require_once $config["path_base"] . "Logger.php";
+
+ require_once $config["path_base"] . "Provider_Adapter.php";
+
require_once $config["path_base"] . "Provider_Model.php";
require_once $config["path_base"] . "Provider_Model_OpenID.php";
require_once $config["path_base"] . "Provider_Model_OAuth1.php";
- require_once $config["path_base"] . "Provider_Model_OAuth2.php";
-
+ require_once $config["path_base"] . "Provider_Model_OAuth2.php";
+
require_once $config["path_base"] . "User.php";
require_once $config["path_base"] . "User_Profile.php";
require_once $config["path_base"] . "User_Contact.php";
- require_once $config["path_base"] . "User_Activity.php";
-
+ require_once $config["path_base"] . "User_Activity.php";
+
if (!class_exists("Hybrid_Storage", false)) {
require_once $config["path_base"] . "Storage.php";
- }
-
+ }
+
// hash given config
- Hybrid_Auth::$config = $config;
-
+ Hybrid_Auth::$config = $config;
+
// instance of log mng
- Hybrid_Auth::$logger = new Hybrid_Logger();
-
+ Hybrid_Auth::$logger = new Hybrid_Logger();
+
// instance of errors mng
- Hybrid_Auth::$error = new Hybrid_Error();
-
+ Hybrid_Auth::$error = new Hybrid_Error();
+
// start session storage mng
- Hybrid_Auth::$store = new Hybrid_Storage();
-
+ Hybrid_Auth::$store = new Hybrid_Storage();
+
Hybrid_Logger::info("Enter Hybrid_Auth::initialize()");
Hybrid_Logger::info("Hybrid_Auth::initialize(). PHP version: " . PHP_VERSION);
Hybrid_Logger::info("Hybrid_Auth::initialize(). Hybrid_Auth version: " . Hybrid_Auth::$version);
- Hybrid_Logger::info("Hybrid_Auth::initialize(). Hybrid_Auth called from: " . Hybrid_Auth::getCurrentUrl());
-
+ Hybrid_Logger::info("Hybrid_Auth::initialize(). Hybrid_Auth called from: " . Hybrid_Auth::getCurrentUrl());
+
// PHP Curl extension [http://www.php.net/manual/en/intro.curl.php]
if (!function_exists('curl_init')) {
Hybrid_Logger::error('Hybridauth Library needs the CURL PHP extension.');
- throw new Exception('Hybridauth Library needs the CURL PHP extension.');
- }
-
+ throw new Exception('Hybridauth Library needs the CURL PHP extension.');
+ }
+
// PHP JSON extension [http://php.net/manual/en/book.json.php]
if (!function_exists('json_decode')) {
Hybrid_Logger::error('Hybridauth Library needs the JSON PHP extension.');
- throw new Exception('Hybridauth Library needs the JSON PHP extension.');
- }
-
+ throw new Exception('Hybridauth Library needs the JSON PHP extension.');
+ }
+
// session.name
if (session_name() != "PHPSESSID") {
Hybrid_Logger::info('PHP session.name diff from default PHPSESSID. http://php.net/manual/en/session.configuration.php#ini.session.name.');
- }
-
+ }
+
// safe_mode is on
if (ini_get('safe_mode')) {
Hybrid_Logger::info('PHP safe_mode is on. http://php.net/safe-mode.');
- }
-
+ }
+
// open basedir is on
if (ini_get('open_basedir')) {
Hybrid_Logger::info('PHP open_basedir is on. http://php.net/open-basedir.');
- }
-
+ }
+
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...");
-
+ Hybrid_Logger::info("Hybrid_Auth initialize: check if any error is stored on the endpoint...");
+
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_Error::clearError();
-
+ $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_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);
+ throw new Exception($m, $c, $p);
}
- }
-
+ throw new Exception($m, $c);
+ }
+
Hybrid_Logger::info("Hybrid_Auth initialize: no error found. initialization succeed.");
- }
-
+ }
+
/**
* Hybrid storage system accessor
*
@@ -184,16 +183,16 @@ class Hybrid_Auth {
*/
public static function storage() {
return Hybrid_Auth::$store;
- }
-
+ }
+
/**
* Get hybridauth session data
* @return string|null
*/
function getSessionData() {
return Hybrid_Auth::storage()->getSessionData();
- }
-
+ }
+
/**
* Restore hybridauth session data
*
@@ -202,8 +201,8 @@ class Hybrid_Auth {
*/
function restoreSessionData($sessiondata = null) {
Hybrid_Auth::storage()->restoreSessionData($sessiondata);
- }
-
+ }
+
/**
* Try to authenticate the user with a given provider.
*
@@ -221,8 +220,8 @@ class Hybrid_Auth {
* @return
*/
public static function authenticate($providerId, $params = null) {
- Hybrid_Logger::info("Enter Hybrid_Auth::authenticate( $providerId )");
-
+ 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..");
@@ -233,8 +232,8 @@ class Hybrid_Auth {
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
*
@@ -244,8 +243,8 @@ class Hybrid_Auth {
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
*
@@ -254,30 +253,30 @@ class Hybrid_Auth {
* @return Hybrid_Provider_Adapter
*/
public static function setup($providerId, $params = null) {
- Hybrid_Logger::debug("Enter Hybrid_Auth::setup( $providerId )", $params);
-
+ Hybrid_Logger::debug("Enter Hybrid_Auth::setup( $providerId )", $params);
+
if (!$params) {
- $params = Hybrid_Auth::storage()->get("hauth_session.$providerId.id_provider_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();
+ $params = [];
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"]);
- }
-
+ }
+
# instantiate a new IDProvider Adapter
$provider = new Hybrid_Provider_Adapter();
$provider->factory($providerId, $params);
return $provider;
- }
-
+ }
+
/**
* Check if the current user is connected to a given provider
*
@@ -286,24 +285,24 @@ class Hybrid_Auth {
*/
public static function isConnectedWith($providerId) {
return (bool) Hybrid_Auth::storage()->get("hauth_session.{$providerId}.is_logged_in");
- }
-
+ }
+
/**
* Return array listing all authenticated providers
* @return array
*/
public static function getConnectedProviders() {
- $idps = array();
-
+ $idps = [];
+
foreach (Hybrid_Auth::$config["providers"] as $idpid => $params) {
if (Hybrid_Auth::isConnectedWith($idpid)) {
$idps[] = $idpid;
}
- }
-
+ }
+
return $idps;
- }
-
+ }
+
/**
* Return array listing all enabled providers as well as a flag if you are connected
*
@@ -317,34 +316,34 @@ class Hybrid_Auth {
* @return array
*/
public static function getProviders() {
- $idps = array();
-
+ $idps = [];
+
foreach (Hybrid_Auth::$config["providers"] as $idpid => $params) {
if ($params['enabled']) {
- $idps[$idpid] = array('connected' => false);
-
+ $idps[$idpid] = ['connected' => false];
+
if (Hybrid_Auth::isConnectedWith($idpid)) {
$idps[$idpid]['connected'] = true;
}
}
- }
-
+ }
+
return $idps;
- }
-
+ }
+
/**
* A generic function to logout all connected provider at once
* @return void
*/
public static function logoutAllProviders() {
- $idps = Hybrid_Auth::getConnectedProviders();
-
+ $idps = Hybrid_Auth::getConnectedProviders();
+
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
*
@@ -355,13 +354,13 @@ class Hybrid_Auth {
if(!$mode){
$mode = 'PHP';
}
- Hybrid_Logger::info("Enter Hybrid_Auth::redirect( $url, $mode )");
-
+ Hybrid_Logger::info("Enter Hybrid_Auth::redirect( $url, $mode )");
+
// 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();
- }
-
+ }
+
if ($mode == "PHP") {
header("Location: $url");
} elseif ($mode == "JS") {
@@ -375,11 +374,11 @@ class Hybrid_Auth {
echo 'Redirecting, please wait...';
echo '</body>';
echo '</html>';
- }
-
+ }
+
die();
- }
-
+ }
+
/**
* Utility function, return the current url
*
@@ -389,18 +388,18 @@ class Hybrid_Auth {
public static function getCurrentUrl($request_uri = true) {
if (php_sapi_name() == 'cli') {
return '';
- }
-
- $protocol = 'http://';
-
+ }
+
+ $protocol = 'http://';
+
if ((isset($_SERVER['HTTPS']) && ( $_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1 ))
|| (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'))
{
$protocol = 'https://';
- }
-
- $url = $protocol . $_SERVER['HTTP_HOST'];
-
+ }
+
+ $url = $protocol . $_SERVER['HTTP_HOST'];
+
if ($request_uri) {
// If $_SERVER['REQUEST_URI'] is already a FQDN, use it
if (stripos($_SERVER['REQUEST_URI'], $url) === 0) {
@@ -410,10 +409,10 @@ class Hybrid_Auth {
}
} else {
$url .= $_SERVER['PHP_SELF'];
- }
-
+ }
+
// return current url
return $url;
- }
-
-}
+ }
+
+}