| 1 |
<?php |
| 2 |
/*! |
| 3 |
* Hybridauth |
| 4 |
* https://hybridauth.github.io | https://github.com/hybridauth/hybridauth |
| 5 |
* (c) 2017 Hybridauth authors | https://hybridauth.github.io/license.html |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Hybridauth\HttpClient; |
| 9 |
|
| 10 |
use Hybridauth\Data; |
| 11 |
|
| 12 |
/** |
| 13 |
* HttpClient\Util home to a number of utility functions. |
| 14 |
*/ |
| 15 |
class Util |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Redirect handler. |
| 19 |
* |
| 20 |
* @var callable|null |
| 21 |
*/ |
| 22 |
protected static $redirectHandler; |
| 23 |
|
| 24 |
/** |
| 25 |
* Exit handler. |
| 26 |
* |
| 27 |
* @var callable|null |
| 28 |
*/ |
| 29 |
protected static $exitHandler; |
| 30 |
|
| 31 |
/** |
| 32 |
* Redirect to a given URL. |
| 33 |
* |
| 34 |
* In case your application need to perform certain required actions before Hybridauth redirect users |
| 35 |
* to IDPs websites, the default behaviour can be altered in one of two ways: |
| 36 |
* If callable $redirectHandler is defined, it will be called instead. |
| 37 |
* If callable $exitHandler is defined, it will be called instead of exit(). |
| 38 |
* |
| 39 |
* @param string $url |
| 40 |
* |
| 41 |
* @return mixed |
| 42 |
*/ |
| 43 |
public static function redirect($url) |
| 44 |
{ |
| 45 |
if (static::$redirectHandler) { |
| 46 |
return call_user_func(static::$redirectHandler, $url); |
| 47 |
} |
| 48 |
|
| 49 |
header(sprintf('Location: %s', $url)); |
| 50 |
|
| 51 |
if (static::$exitHandler) { |
| 52 |
return call_user_func(static::$exitHandler); |
| 53 |
} |
| 54 |
|
| 55 |
exit(1); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Redirect handler to which the regular redirect() will yield the action of redirecting users. |
| 60 |
* |
| 61 |
* @param callable $callback |
| 62 |
*/ |
| 63 |
public static function setRedirectHandler($callback) |
| 64 |
{ |
| 65 |
self::$redirectHandler = $callback; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Exit handler will be called instead of regular exit() when calling Util::redirect() method. |
| 70 |
* |
| 71 |
* @param callable $callback |
| 72 |
*/ |
| 73 |
public static function setExitHandler($callback) |
| 74 |
{ |
| 75 |
self::$exitHandler = $callback; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Returns the Current URL. |
| 80 |
* |
| 81 |
* @param bool $requestUri TRUE to use $_SERVER['REQUEST_URI'], FALSE for $_SERVER['PHP_SELF'] |
| 82 |
* |
| 83 |
* @return string |
| 84 |
*/ |
| 85 |
public static function getCurrentUrl($requestUri = false) |
| 86 |
{ |
| 87 |
$collection = new Data\Collection($_SERVER); |
| 88 |
|
| 89 |
$protocol = 'http://'; |
| 90 |
|
| 91 |
if (($collection->get('HTTPS') && $collection->get('HTTPS') !== 'off') || |
| 92 |
$collection->get('HTTP_X_FORWARDED_PROTO') === 'https') { |
| 93 |
$protocol = 'https://'; |
| 94 |
} |
| 95 |
|
| 96 |
return $protocol . |
| 97 |
$collection->get('HTTP_HOST') . |
| 98 |
$collection->get($requestUri ? 'REQUEST_URI' : 'PHP_SELF'); |
| 99 |
} |
| 100 |
} |
| 101 |
|