1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223:
<?php
namespace LoginRadiusSDK\Utility;
use LoginRadiusSDK\Clients\IHttpClient;
use LoginRadiusSDK\Clients\DefaultHttpClient;
use LoginRadiusSDK\LoginRadiusException;
if (!defined('API_DOMAIN')) {
define('API_DOMAIN', 'https://api.loginradius.com');
}
if (!defined('API_CONFIG_DOMAIN')) {
define('API_CONFIG_DOMAIN', 'https://config.lrcontent.com');
}
class Functions {
const version = '5.0.1';
private static $apikey;
private static $apisecret;
private static $options = array();
public function __construct($apikey = '', $apisecret = '', $customize_options = array()) {
if (!empty($apikey) && !empty($apisecret)) {
self::setDefaultApplication($apikey, $apisecret);
} elseif (empty($apikey) || empty($apisecret)) {
if (empty(self::$apikey) || empty(self::$apisecret)) {
if (defined('LR_API_KEY') && defined('LR_API_SECRET')) {
self::setDefaultApplication(LR_API_KEY, LR_API_SECRET);
} else {
throw new LoginRadiusException('Required "LoginRadius" API Key and API Secret.');
}
}
}
self::$options = array_merge(self::$options, $customize_options);
}
public static function setDefaultApplication($apikey, $apisecret) {
self::checkAPIValidation($apikey, $apisecret);
self::$apikey = $apikey;
self::$apisecret = $apisecret;
}
private static function checkAPIValidation($apikey, $apisecret) {
if (empty($apikey) || !self::isValidGuid($apikey)) {
throw new LoginRadiusException('Required "LoginRadius" API key in valid guid format.');
}
if (empty($apisecret) || !self::isValidGuid($apisecret)) {
throw new LoginRadiusException('Required "LoginRadius" API secret in valid guid format.');
}
}
public static function getApiKey() {
if (empty(self::$apikey) && defined('LR_API_KEY')) {
self::$apikey = LR_API_KEY;
}
return self::$apikey;
}
public static function getCustomizeOptions() {
return self::$options;
}
public static function setCustomizeOptions($options = array()) {
self::$options = $options;
}
public static function getApiSecret() {
if (empty(self::$apisecret) && defined('LR_API_SECRET')) {
self::$apisecret = LR_API_SECRET;
}
return self::$apisecret;
}
public static function isValidGuid($value) {
return preg_match('/^\{?[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}\}?$/i', $value);
}
public static function apiClient($path, $query_array = array(), $options = array()) {
global $apiClient_class;
$merge_options = array_merge($options, self::$options);
if (isset($apiClient_class) && class_exists($apiClient_class)) {
$client = new $apiClient_class();
} else {
$client = new DefaultHttpClient();
}
$output_format = isset($merge_options['output_format']) && $merge_options['output_format'] == 'json' ? true : false;
$response = $client->request($path, $query_array, $merge_options);
return $output_format && (is_object(json_decode($response)) || is_array(json_decode($response))) ? json_decode($response) : $response;
}
public static function authentication($array = array(), $secure = 'key', $request_url = '') {
$result = array();
if ($secure == 'key') {
$result = array('apikey' => Functions::getApiKey());
} else if ($secure == 'secretparams') {
$result = array('apikey' => Functions::getApiKey(), 'apisecret' => Functions::getApiSecret());
} else if ($secure == 'secret') {
$result = array('X-LoginRadius-ApiSecret' => Functions::getApiSecret());
} else if ($secure == 'hashsecret') {
$expiry_time = gmdate("Y-m-d H:i:s", strtotime('1 hour'));
$encoded_url = self::urlReplacement(urlencode(urldecode($request_url)));
if (isset($array['method']) && (($array['method'] == 'POST') || ($array['method'] == 'PUT') || ($array['method'] == 'DELETE')) && $array['post_data'] !== true) {
$post_data = $array['post_data'];
if ((is_array($array['post_data']) || is_object($array['post_data']))) {
$post_data = json_encode($array['post_data']);
}
$string_to_hash = $expiry_time . ':' . strtolower($encoded_url) . ':' . $post_data;
} else {
$string_to_hash = $expiry_time . ':' . strtolower($encoded_url);
}
$sha_hash = hash_hmac('sha256', $string_to_hash, Functions::getApiSecret(), true);
$result = array('X-Request-Expires' => $expiry_time, 'digest' => "SHA-256=" . base64_encode($sha_hash));
}
return (is_array($array) && sizeof($array) > 0) ? array_merge($result, $array) : $result;
}
public function urlReplacement($decoded_url) {
$replacementArray = array('%2A' => '*','%28' => '(','%29' => ')');
return str_replace(array_keys($replacementArray), array_values($replacementArray), $decoded_url);
}
public static function queryBuild($data = array()) {
if (is_array($data) && sizeof($data) > 0) {
return http_build_query($data);
}
return '';
}
}