-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ea5468
commit 4590934
Showing
1 changed file
with
72 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
declare (strict_types = 1); | ||
|
||
namespace Leaf; | ||
|
||
/** | ||
* Leaf Security Module | ||
* --------------------------------- | ||
* Simple to use security based utility methods | ||
* | ||
* | ||
* @author Michael Darko <[email protected]> | ||
* @since v2.2 | ||
* @version 1.0 | ||
*/ | ||
class Anchor | ||
{ | ||
protected static $config = [ | ||
'SECRET_KEY' => '_token', | ||
'SECRET' => '@nkor_leaf$0Secret!', | ||
'EXCEPT' => [], | ||
'METHODS' => ['POST', 'PUT', 'PATCH', 'DELETE'], | ||
]; | ||
protected static $config = [ | ||
'SECRET_KEY' => '_token', | ||
'SECRET' => '@nkor_leaf$0Secret!', | ||
'EXCEPT' => [], | ||
'METHODS' => ['POST', 'PUT', 'PATCH', 'DELETE'], | ||
]; | ||
|
||
protected static $errors = []; | ||
protected static $errors = []; | ||
|
||
/** | ||
* Manage config for leaf anchor | ||
* | ||
* @param array|null $config The config to set | ||
*/ | ||
public static function config($config = null) | ||
{ | ||
if ($config === null) { | ||
return static::$config; | ||
} | ||
/** | ||
* Manage config for leaf anchor | ||
* | ||
* @param array|null $config The config to set | ||
*/ | ||
public static function config($config = null) | ||
{ | ||
if ($config === null) { | ||
return static::$config; | ||
} | ||
|
||
static::$config = array_merge(static::$config, $config); | ||
} | ||
|
||
/** | ||
* Escape malicious characters | ||
* | ||
* @param mixed $data The data to sanitize. | ||
*/ | ||
public static function sanitize($data) | ||
{ | ||
if (is_array($data)) { | ||
foreach ($data as $key => $value) { | ||
$data[is_string($key) ? self::sanitize($key) : $key] = self::sanitize($value); | ||
} | ||
} | ||
|
||
static::$config = array_merge(static::$config, $config); | ||
} | ||
if (is_string($data)) { | ||
$data = htmlspecialchars($data, ENT_QUOTES, 'UTF-8'); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Escape malicious characters | ||
* Get an item or items from an array of data. | ||
* | ||
* @param mixed $data The data to sanitize. | ||
* @param array $dataSource An array of data to search through | ||
* @param string|array $item The items to return | ||
*/ | ||
public static function sanitize($data) | ||
{ | ||
if (is_array($data)) { | ||
foreach ($data as $key => $value) { | ||
$data[is_string($key) ? self::sanitize($key) : $key] = self::sanitize($value); | ||
} | ||
public static function deepGet($dataSource, $item = null) | ||
{ | ||
if (!$item) { | ||
return $dataSource; | ||
} | ||
|
||
if (is_string($data)) { | ||
$data = htmlspecialchars($data, ENT_QUOTES, 'UTF-8'); | ||
} | ||
$output = []; | ||
|
||
return $data; | ||
} | ||
if (is_array($item)) { | ||
foreach ($item as $dataItem) { | ||
$output[$dataItem] = $dataSource[$dataItem] ?? null; | ||
} | ||
} else { | ||
$output = $dataSource[$item] ?? null; | ||
} | ||
|
||
/** | ||
* Generate a token for identifying your application | ||
* | ||
* @param int $strength Number of random characters to attach to token | ||
*/ | ||
public static function generateToken(int $strength = 16): string | ||
{ | ||
return bin2hex(static::$config['SECRET'] . '.' . random_bytes($strength));; | ||
} | ||
return $output; | ||
} | ||
|
||
/** | ||
* Generate a token for identifying your application | ||
* | ||
* @param int $strength Number of random characters to attach to token | ||
*/ | ||
public static function generateToken(int $strength = 16): string | ||
{ | ||
return bin2hex(static::$config['SECRET'] . '.' . random_bytes($strength)); | ||
} | ||
|
||
public static function errors(): array | ||
{ | ||
return static::$errors; | ||
} | ||
public static function errors(): array | ||
{ | ||
return static::$errors; | ||
} | ||
} |