rename p2
This commit is contained in:
541
functions/check.php
Normal file
541
functions/check.php
Normal file
@ -0,0 +1,541 @@
|
||||
<?php
|
||||
/**
|
||||
* functions/check.php
|
||||
*
|
||||
* This class is used to test various inputs.
|
||||
*
|
||||
* @version 1.1.2
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com/libraries/Bedrock
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Bedrock\Functions;
|
||||
|
||||
use TheTempusProject\Bedrock\Classes\Config;
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
|
||||
class Check {
|
||||
private static $formValidator = null;
|
||||
private static $errorLog = [];
|
||||
private static $errorLogFull = [];
|
||||
private static $errorLogUser = [];
|
||||
|
||||
/**
|
||||
* This function only logs an error for the user-error-log.
|
||||
* Primarily used for providing generalized form feedback
|
||||
*
|
||||
* @param {string} [$error] - The error information to be added to the list.
|
||||
*/
|
||||
public static function addUserError( $error, $data = false ) {
|
||||
if ( false !== $data ) {
|
||||
$error .= ' Data: ' . var_export( $data, true );
|
||||
}
|
||||
self::$errorLogUser[] = $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to properly document and handle any errors we encounter in the check.
|
||||
*
|
||||
* @param {string} [$error] - The error information to be added to the list, and used in debug info.
|
||||
* @param string|array $data - Any additional variables or information.
|
||||
*/
|
||||
public static function addError( $error, $data = null ) {
|
||||
/**
|
||||
* If an array is provided for $error, it is split into
|
||||
* 2 separate errors for the logging.
|
||||
*/
|
||||
if ( is_array( $error ) ) {
|
||||
$userError = $error[1];
|
||||
$error = $error[0];
|
||||
}
|
||||
|
||||
Debug::info( "Check error: $error" );
|
||||
if ( !empty( $data ) ) {
|
||||
Debug::info( 'Additional error information:' );
|
||||
Debug::v( $data );
|
||||
}
|
||||
self::$errorLog[] = ['errorInfo' => $error, 'errorData' => $data];
|
||||
if ( isset( $userError ) ) {
|
||||
self::$errorLogUser[] = $userError;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for returning the system error array.
|
||||
*
|
||||
* @param $full - Flag for returning the full error log.
|
||||
* @return array - Returns an Array of all the failed checks up until this point.
|
||||
*/
|
||||
public static function systemErrors( $full = false ) {
|
||||
if ( $full ) {
|
||||
return self::$errorLogFull;
|
||||
}
|
||||
return self::$errorLog;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for returning the user error array.
|
||||
*
|
||||
* @return array - Returns an Array of all the recorded user errors.
|
||||
*/
|
||||
public static function userErrors() {
|
||||
return self::$errorLogUser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for resetting the current error logs and adding the old log
|
||||
* to the complete error log.
|
||||
*/
|
||||
public static function errorReset() {
|
||||
self::$errorLogFull = array_merge( self::$errorLogFull, self::$errorLog );
|
||||
self::$errorLog = [];
|
||||
self::$errorLogUser = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks an uploaded image for proper size, formatting, and lack of errors in the upload.
|
||||
*
|
||||
* @param string $data - The name of the upload field.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function imageUpload( $imageName ) {
|
||||
if ( ! Config::getValue( 'uploads/images' ) ) {
|
||||
self::addUserError( 'Image uploads are disabled.' );
|
||||
return false;
|
||||
}
|
||||
if ( ! isset( $_FILES[ $imageName ] ) ) {
|
||||
self::addUserError( 'File not found.', $imageName );
|
||||
return false;
|
||||
}
|
||||
if ( $_FILES[$imageName]['error'] != 0 ) {
|
||||
self::addUserError( 'File error:' . $_FILES[$imageName]['error'] );
|
||||
return false;
|
||||
}
|
||||
if ( $_FILES[$imageName]['size'] > Config::getValue( 'uploads/maxImageSize' ) ) {
|
||||
self::addUserError( 'Image is too large.' );
|
||||
return false;
|
||||
}
|
||||
$fileType = strrchr( $_FILES[$imageName]['name'], '.' );
|
||||
if ( !( in_array( $fileType, ALLOWED_IMAGE_UPLOAD_EXTENTIONS ) ) ) {
|
||||
self::addUserError( 'Invalid image type', $fileType );
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks a string for a boolean string value.
|
||||
*
|
||||
* @param string $data - The data being checked.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function tf( $data ) {
|
||||
if ( true === $data || '1' === $data || 1 === $data || strtolower( $data ) === 'true' ) {
|
||||
return true;
|
||||
}
|
||||
if ( false === $data || '0' === $data || 0 === $data || strtolower( $data ) === 'false' ) {
|
||||
return true;
|
||||
}
|
||||
self::addError( 'Invalid true-false: ', $data );
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for alpha-numeric type.
|
||||
*
|
||||
* @param string $data - The data being checked.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function alnum( $data ) {
|
||||
if ( ctype_alpha( $data ) ) {
|
||||
return true;
|
||||
}
|
||||
self::addError( 'Invalid alpha-numeric.', $data );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks an input for spaces.
|
||||
*
|
||||
* @param string $data - The data being checked.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function nospace( $data ) {
|
||||
if ( !stripos( $data, ' ' ) ) {
|
||||
return true;
|
||||
}
|
||||
self::addError( 'Invalid no-space input.', $data );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the data to see if it is a digit.
|
||||
*
|
||||
* @param mixed $data - Data being checked.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function id( $data ) {
|
||||
if ( is_numeric( $data ) ) {
|
||||
return true;
|
||||
}
|
||||
self::addError( 'Invalid ID.', $data );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the data to see if it is a valid data string. It can
|
||||
* only contain letters, numbers, space, underscore, and dashes.
|
||||
*
|
||||
* @param mixed $data - Data being checked.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function dataTitle( $data ) {
|
||||
if ( preg_match( DATA_TITLE_PREG, $data ) ) {
|
||||
return true;
|
||||
}
|
||||
self::addError( 'Invalid data title.', $data );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the data to see if there are any illegal characters
|
||||
* in the filename.
|
||||
*
|
||||
* @param {string} [$data]
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function path( $data = null ) {
|
||||
if ( preg_match( REDIRECT_PREG_REQS, $data ) ) {
|
||||
return true;
|
||||
}
|
||||
self::addError( 'Invalid path.', $data );
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the form token.
|
||||
*
|
||||
* @param {string|null} - String to check for the token. (Post Token assumed)
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function token( $data = null ) {
|
||||
if ( false === Token::isTokenEnabled() ) {
|
||||
return true;
|
||||
}
|
||||
if ( empty( $data ) ) {
|
||||
$data = Input::post( 'token' );
|
||||
}
|
||||
$result = Token::check( $data );
|
||||
if ( $result === false ) {
|
||||
self::addUserError( 'Invalid Token.', $data );
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for proper url formatting.
|
||||
*
|
||||
* @param {string} [$data] The input being checked
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function url( $data ) {
|
||||
$url = filter_var( $data, FILTER_SANITIZE_URL );
|
||||
if ( filter_var( $url, FILTER_VALIDATE_URL ) === false ) {
|
||||
self::addError( 'Invalid Url' );
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks email formatting.
|
||||
*
|
||||
* @param {string} [$data] - The string being tested.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function email( $data ) {
|
||||
$sanitizedEmail = filter_var( $data, FILTER_SANITIZE_EMAIL );
|
||||
if ( !filter_var( $sanitizedEmail, FILTER_VALIDATE_EMAIL ) ) {
|
||||
self::addError( 'Email is not properly formatted.' );
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks password formatting.
|
||||
*
|
||||
* @param {string} [$password] - The string being tested.
|
||||
* @param {string} [$secondaryPassword] - The string it is being compared to.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function password( $password, $secondaryPassword = null ) {
|
||||
if ( strlen( $password ) < MINIMUM_PASSWORD_LENGTH ) {
|
||||
self::addError([
|
||||
'Password is too short.',
|
||||
'Password must be longer than ' . MINIMUM_PASSWORD_LENGTH . ' characters.',
|
||||
]);
|
||||
return false;
|
||||
}
|
||||
if ( strlen( $password ) > MAXIMUM_PASSWORD_LENGTH ) {
|
||||
self::addError([
|
||||
'Password is too long.',
|
||||
'Password must not be longer than ' . MAXIMUM_PASSWORD_LENGTH . ' characters.',
|
||||
]);
|
||||
return false;
|
||||
}
|
||||
if ( defined( 'ADDITIONAL_PASSWORD_REGEX' ) ) {
|
||||
if ( !preg_match( ADDITIONAL_PASSWORD_REGEX, $password ) ) {
|
||||
self::addError([
|
||||
'Password does not pass requirements.',
|
||||
]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if ( isset( $secondaryPassword ) && $password !== $secondaryPassword ) {
|
||||
self::addError( 'Passwords do not match.' );
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks name formatting.
|
||||
*
|
||||
* Requirements:
|
||||
* - 2 - 20 characters long
|
||||
* - must only contain letters: [A-Z] , [a-z]
|
||||
*
|
||||
* @param {string} [$data] - The string being tested.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function name( $data ) {
|
||||
if ( strlen( $data ) > 20 ) {
|
||||
self::addError( 'Name is too long.', $data );
|
||||
return false;
|
||||
}
|
||||
if ( strlen( $data ) < 2 ) {
|
||||
self::addError( 'Name is too short.', $data );
|
||||
return false;
|
||||
}
|
||||
if ( !ctype_alpha( str_replace( ' ', '', $data ) ) ) {
|
||||
self::addError( 'Name is not properly formatted.', $data );
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for alpha-numeric type.
|
||||
*
|
||||
* @param {string} [$data] - The data being checked.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function uploads() {
|
||||
if ( ini_get( 'file_uploads' ) == 1 ) {
|
||||
return true;
|
||||
}
|
||||
self::addError( 'Uploads are disabled.' );
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the PHP version.
|
||||
*
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function php() {
|
||||
$phpVersion = phpversion();
|
||||
if ( version_compare( $phpVersion, MINIMUM_PHP_VERSION, '>=' ) ) {
|
||||
return true;
|
||||
}
|
||||
self::addError( 'PHP version is too old.', $phpVersion );
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks PHP's mail function.
|
||||
*
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function mail() {
|
||||
if ( function_exists( 'mail' ) ) {
|
||||
return true;
|
||||
}
|
||||
self::addError( 'PHP Mail function is not enabled.' );
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if PHP's Safe mode is enabled.
|
||||
*
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function safe() {
|
||||
if ( !ini_get( 'safe_mode' ) ) {
|
||||
return true;
|
||||
}
|
||||
self::addError( 'PHP Safe Mode is enabled.' );
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if PHP's Safe mode is enabled.
|
||||
* @todo - come back and make this more variable
|
||||
* pdo_firebird
|
||||
* pdo_informix
|
||||
* pdo_mssql
|
||||
* pdo_oci
|
||||
* pdo_oci8
|
||||
* pdo_odbc
|
||||
* pdo_pgsql
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function phpExtensions() {
|
||||
if ( !extension_loaded( 'pdo' ) ) {
|
||||
self::addError( 'PHP PDO is not enabled.' );
|
||||
return false;
|
||||
}
|
||||
if ( extension_loaded( 'pdo_mysql' ) ) {
|
||||
return true;
|
||||
} elseif ( extension_loaded( 'pdo_sqlite' ) ) {
|
||||
return true;
|
||||
}
|
||||
self::addError( 'No usable PDO extension is loaded.' );
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to make sure sessions are working properly.
|
||||
*
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function sessions() {
|
||||
$_SESSION['sessionTest'] = 1;
|
||||
if ( !empty( $_SESSION['sessionTest'] ) ) {
|
||||
unset( $_SESSION['sessionTest'] );
|
||||
return true;
|
||||
}
|
||||
self::addError( 'There is an error with saving sessions.' );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if cookies are enabled.
|
||||
*
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function cookies() {
|
||||
Cookie::put( 'test', 'test' );
|
||||
if ( count( $_COOKIE ) > 0 ) {
|
||||
Cookie::delete( 'test' );
|
||||
return true;
|
||||
}
|
||||
self::addError( 'Cookies are not enabled.' );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if $data contains only numbers, letters, underscores, and dashes
|
||||
*
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function simpleName( $data ) {
|
||||
if ( empty( $data ) ) {
|
||||
self::addError( 'Empty simple name.', $data );
|
||||
|
||||
return false;
|
||||
}
|
||||
if ( preg_match( SIMPLE_NAME_PREG, $data ) ) {
|
||||
return true;
|
||||
}
|
||||
self::addError( 'Invalid simple name.', $data );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if the server is running an Nginx configuration.
|
||||
*
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function isApache() {
|
||||
if ( isset( $_SERVER['SERVER_SOFTWARE'] ) ) {
|
||||
if ( false !== stripos( $_SERVER['SERVER_SOFTWARE'], 'Apache' ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if the Apache server has the appropriate modules enabled.
|
||||
*
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function apacheMods() {
|
||||
if ( !self::isApache() ) {
|
||||
self::addError( 'Server is not Apache.' );
|
||||
return false;
|
||||
}
|
||||
$error_log = count( self::$errorLog );
|
||||
$mods = apache_get_modules();
|
||||
if ( !in_array( 'mod_rewrite', $mods ) ) {
|
||||
self::addError( 'The Apache "rewrite" module is disabled.', $data );
|
||||
}
|
||||
if ( !in_array( 'mod_buffer', $mods ) ) {
|
||||
self::addError( 'The Apache "buffer" module is disabled.', $data );
|
||||
}
|
||||
if ( !in_array( 'mod_headers', $mods ) ) {
|
||||
self::addError( 'The Apache "header" module is disabled.', $data );
|
||||
}
|
||||
if ( !in_array( 'mod_alias', $mods ) ) {
|
||||
self::addError( 'The Apache "alias" module is disabled.', $data );
|
||||
}
|
||||
if ( !in_array( 'mod_dir', $mods ) ) {
|
||||
self::addError( 'The Apache "dir" module is disabled.', $data );
|
||||
}
|
||||
if ( !in_array( 'mod_expires', $mods ) ) {
|
||||
self::addError( 'The Apache "expires" module is disabled.', $data );
|
||||
}
|
||||
if ( count( self::$errorLog ) > $error_log ) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if the server is running an Apache configuration.
|
||||
*
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function isNginx() {
|
||||
if ( isset( $_SERVER['SERVER_SOFTWARE'] ) ) {
|
||||
if ( false !== stripos( $_SERVER['SERVER_SOFTWARE'], 'Nginx' ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the data to see if it is a valid data string. It can
|
||||
* only contain letters, numbers, space, underscore, and dashes.
|
||||
*
|
||||
* @param mixed $data - Data being checked.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function sessionName( $data ) {
|
||||
if ( preg_match( SESSION_NAME_REGEX, $data ) ) {
|
||||
return true;
|
||||
}
|
||||
self::addError( 'Invalid session title.', $data );
|
||||
return false;
|
||||
}
|
||||
}
|
51
functions/code.php
Normal file
51
functions/code.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* functions/code.php
|
||||
*
|
||||
* This class is used for creation of custom codes used by the application.
|
||||
*
|
||||
* @todo Better code generation.
|
||||
*
|
||||
* @version 1.1.2
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com/libraries/Bedrock
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Bedrock\Functions;
|
||||
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
|
||||
class Code {
|
||||
/**
|
||||
* Generates a new confirmation code.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function genConfirmation() {
|
||||
$code = md5( uniqid() );
|
||||
Debug::log( "Code Generated: Confirmation: $code" );
|
||||
return $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a new install hash.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function genInstall() {
|
||||
$code = md5( uniqid() );
|
||||
Debug::log( "Code Generated: Token: $code" );
|
||||
return $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a new token code.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function genToken() {
|
||||
$code = md5( uniqid() );
|
||||
Debug::log( "Code Generated: Token: $code" );
|
||||
return $code;
|
||||
}
|
||||
}
|
95
functions/cookie.php
Normal file
95
functions/cookie.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/**
|
||||
* functions/cookie.php
|
||||
*
|
||||
* This class is used for manipulation of cookies.
|
||||
*
|
||||
* @version 1.1.2
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com/libraries/Bedrock
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Bedrock\Functions;
|
||||
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
|
||||
class Cookie {
|
||||
/**
|
||||
* Checks whether $data is a valid saved cookie or not.
|
||||
*
|
||||
* @param {string} [$data] - Name of the cookie to check for.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function exists( $data ) {
|
||||
if ( !Check::dataTitle( $data ) ) {
|
||||
return false;
|
||||
}
|
||||
$cookieName = DEFAULT_COOKIE_PREFIX . $data;
|
||||
if ( isset( $_COOKIE[$cookieName] ) ) {
|
||||
Debug::log( "Cookie found: $data" );
|
||||
return true;
|
||||
}
|
||||
Debug::info( "Cookie not found: $data" );
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a specific cookie if it exists.
|
||||
*
|
||||
* @param {string} [$data] - Cookie to retrieve data from.
|
||||
* @return {bool|string} - String from the requested cookie, or false if the cookie does not exist.
|
||||
*/
|
||||
public static function get( $data ) {
|
||||
if ( !Check::dataTitle( $data ) ) {
|
||||
return false;
|
||||
}
|
||||
if ( self::exists( $data ) ) {
|
||||
$cookieName = DEFAULT_COOKIE_PREFIX . $data;
|
||||
return $_COOKIE[$cookieName];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create cookie function.
|
||||
*
|
||||
* @param {string} [$name] - Cookie name.
|
||||
* @param {string} [$value] - Cookie value.
|
||||
* @param {int} [$expiry] - How long (in seconds) until the cookie should expire.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function put( $name, $value, $expire = null ) {
|
||||
if ( ! Check::dataTitle( $name ) ) {
|
||||
return false;
|
||||
}
|
||||
if ( ! $expire ) {
|
||||
$expire = time() + DEFAULT_COOKIE_EXPIRATION;
|
||||
}
|
||||
if ( ! Check::ID( $expire ) ) {
|
||||
return false;
|
||||
}
|
||||
$cookieName = DEFAULT_COOKIE_PREFIX . $name;
|
||||
$test = setcookie( $cookieName, $value, $expire, '/' );
|
||||
if ( ! $test ) {
|
||||
Debug::error( "Cookie not created: '$name', until: $expire" );
|
||||
return false;
|
||||
}
|
||||
Debug::debug( "Cookie Created: $name till $expire" );
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete cookie function.
|
||||
*
|
||||
* @param {string} [$name] - Name of cookie to be deleted.
|
||||
*/
|
||||
public static function delete( $name ) {
|
||||
if ( !Check::dataTitle( $name ) ) {
|
||||
return false;
|
||||
}
|
||||
$cookieName = DEFAULT_COOKIE_PREFIX . $name;
|
||||
setcookie( $cookieName, '', ( time() - 1 ), '/' );
|
||||
Debug::log( "Cookie deleted: $name" );
|
||||
return true;
|
||||
}
|
||||
}
|
409
functions/date.php
Normal file
409
functions/date.php
Normal file
@ -0,0 +1,409 @@
|
||||
<?php
|
||||
/**
|
||||
* functions/date.php
|
||||
*
|
||||
* This class is used to manage date inputs in a site-wide repeatable way.
|
||||
*
|
||||
* @version 1.1.2
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com/libraries/Bedrock
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Bedrock\Functions;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeZone;
|
||||
|
||||
class Date {
|
||||
private static $date = null;
|
||||
private static $errorLog = [];
|
||||
private static $errorLogFull = [];
|
||||
private static $errorLogUser = [];
|
||||
|
||||
public static function formatTimestamp( $type, $timestamp ) {
|
||||
if ( stripos( $type, 'date' ) ) {
|
||||
$dateFormat = self::getDateFormat();
|
||||
} elseif ( stripos( $type, 'time' ) ) {
|
||||
$dateFormat = self::getTimeFormat();
|
||||
} else {
|
||||
$dateFormat = self::getDateFormat() . ' ' . self::getTimeFormat();
|
||||
}
|
||||
$time = intval( $timestamp );
|
||||
$dt = new DateTime( self::getTimezone() );
|
||||
$dt->setTimestamp( $time );
|
||||
return $dt->format( $dateFormat );
|
||||
}
|
||||
|
||||
public static function applyTimezoneToTimestamp( $timestamp ) {
|
||||
$timestamp = intval( $timestamp );
|
||||
$date = new DateTime();
|
||||
$date->setTimestamp( $timestamp );
|
||||
$timezone = new DateTimeZone( self::getTimezone() );
|
||||
$date->setTimezone( $timezone );
|
||||
$formattedDate = $date->format('Y-m-d H:i:s');
|
||||
return $formattedDate;
|
||||
}
|
||||
|
||||
public static function applyUtcToDate( $date ) {
|
||||
$timestamp = intval( strtotime( $date ) );
|
||||
$startDate = new DateTime( $date, new DateTimeZone( self::getTimezone() ) );
|
||||
$startDate->setTimezone( new DateTimeZone( 'UTC' ) );
|
||||
$firstSecond = $startDate->getTimestamp();
|
||||
return $firstSecond;
|
||||
}
|
||||
public static function getReadableDate( $timestamp, $with_timezone = false ) {
|
||||
if ( $with_timezone ) {
|
||||
$date = new DateTime();
|
||||
$date->setTimestamp( $timestamp );
|
||||
$timezone = new DateTimeZone( self::getTimezone() );
|
||||
$date->setTimezone( $timezone );
|
||||
$formattedDate = $date->format('Y-m-d H:i:s');
|
||||
return $formattedDate;
|
||||
}
|
||||
return date( 'Y-m-d H:i:s', $timestamp );
|
||||
}
|
||||
public static function convertToTimestampWithTimezone( $dateString, $timeString, $timezoneString ) {
|
||||
$datetimeString = $dateString . ' ' . $timeString;
|
||||
$date = new DateTime($datetimeString, new DateTimeZone($timezoneString));
|
||||
$timestamp = $date->getTimestamp();
|
||||
return $timestamp;
|
||||
}
|
||||
public static function getDateBreakdown( $timestamp, $with_timezone = false ) {
|
||||
$timestamp = intval( $timestamp );
|
||||
$init = date('Y-m-d H:i:s', $timestamp);
|
||||
if ( true === $with_timezone ) {
|
||||
$readableDate = self::applyTimezoneToTimestamp( $timestamp );
|
||||
} else {
|
||||
$readableDate = date('Y-m-d H:i:s', $timestamp);
|
||||
}
|
||||
$date = date( 'Y-m-d', strtotime( $readableDate ) );
|
||||
$time = date( 'H:i', strtotime( $readableDate ) );
|
||||
return [
|
||||
'time' => $time,
|
||||
'date' => $date,
|
||||
];
|
||||
}
|
||||
public static function determineDateInput( $with_timezone = false) {
|
||||
$currentTimestamp = time();
|
||||
$currentDateString = date( 'F d, Y', $currentTimestamp );
|
||||
|
||||
// Prioritize inputs: Post > Get > defaults
|
||||
if ( Input::exists('time') ) {
|
||||
$time = Input::post('time') ? Input::post('time') : Input::get('time');
|
||||
} else {
|
||||
$time = '00:00';
|
||||
}
|
||||
if ( Input::exists('date') ) {
|
||||
$date = Input::post('date') ? Input::post('date') : Input::get('date');
|
||||
} else {
|
||||
$date = $currentDateString;
|
||||
}
|
||||
if ( Input::exists('hour') ) {
|
||||
$hour = Input::post('hour') ? Input::post('hour') : Input::get('hour');
|
||||
} else {
|
||||
$hour = '00';
|
||||
}
|
||||
if ( Input::exists('minute') ) {
|
||||
$minute = Input::post('minute') ? Input::post('minute') : Input::get('minute');
|
||||
} else {
|
||||
$minute = '00';
|
||||
}
|
||||
if ( Input::exists('day') ) {
|
||||
$day = Input::post('day') ? Input::post('day') : Input::get('day');
|
||||
} else {
|
||||
$day = date( 'd', $currentTimestamp );
|
||||
}
|
||||
if ( Input::exists('month') ) {
|
||||
$month = Input::post('month') ? Input::post('month') : Input::get('month');
|
||||
} else {
|
||||
$month = date( 'M', $currentTimestamp );
|
||||
}
|
||||
if ( Input::exists('year') ) {
|
||||
$year = Input::post('year') ? Input::post('year') : Input::get('year');
|
||||
} else {
|
||||
$year = date( 'Y', $currentTimestamp );
|
||||
}
|
||||
|
||||
// prioritize a time input over individual hours and minutes
|
||||
if ( '00:00' == $time ) {
|
||||
$time = $hour . ':' . $minute;
|
||||
}
|
||||
|
||||
// prioritize a date input over individual day, month, year
|
||||
if ( $currentDateString == $date ) {
|
||||
$inputTimestamp = strtotime( $time. ' ' . $month . ' ' . $day . ', ' . $year );
|
||||
|
||||
/**
|
||||
* Its possible to select IE: 31, Feb; in navigation.
|
||||
* This will back the day down until it finds an acceptable date.
|
||||
*/
|
||||
$intDay = intval( $day );
|
||||
$intMonth = intval( date( 'm', $inputTimestamp ) );
|
||||
$intYear = intval( $year );
|
||||
// 29
|
||||
if ( ! checkdate( $intMonth, $intDay, $intYear ) ) {
|
||||
$day = $intDay - 1;
|
||||
}
|
||||
// 30
|
||||
if ( ! checkdate( $intMonth, $intDay, $intYear ) ) {
|
||||
$day = $intDay - 1;
|
||||
}
|
||||
// 31
|
||||
if ( ! checkdate( $intMonth, $intDay, $intYear ) ) {
|
||||
$day = $intDay - 1;
|
||||
}
|
||||
|
||||
$inputTimestamp = strtotime( $time. ' ' . $month . ' ' . $day . ', ' . $year );
|
||||
} else {
|
||||
$inputTimestamp = strtotime( $time . ' ' . $date );
|
||||
}
|
||||
|
||||
$timestamp = self::getReadableDate( $inputTimestamp, $with_timezone );
|
||||
|
||||
return $timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Application getters
|
||||
*/
|
||||
public static function getDateFormat() {
|
||||
$format = DEFAULT_DATE_FORMAT;
|
||||
if ( !empty( self::$activePrefs ) && !empty( self::$activePrefs['dateFormat'] ) ) {
|
||||
$format = self::$activePrefs['dateFormat'];
|
||||
}
|
||||
return $format;
|
||||
}
|
||||
public static function getTimeFormat() {
|
||||
$format = DEFAULT_TIME_FORMAT;
|
||||
if ( !empty( self::$activePrefs ) && !empty( self::$activePrefs['timeFormat'] ) ) {
|
||||
$format = self::$activePrefs['timeFormat'];
|
||||
}
|
||||
return $format;
|
||||
}
|
||||
public static function getTimezone() {
|
||||
$format = DEFAULT_TIMEZONE;
|
||||
if ( !empty( self::$activePrefs ) && !empty( self::$activePrefs['timezone'] ) ) {
|
||||
$format = self::$activePrefs['timezone'];
|
||||
}
|
||||
return $format;
|
||||
}
|
||||
|
||||
/**
|
||||
* Current getters
|
||||
*/
|
||||
public static function getCurrentDay() {
|
||||
$date = new DateTime();
|
||||
$date->setTimestamp( time() );
|
||||
$timezone = new DateTimeZone( self::getTimezone() );
|
||||
$date->setTimezone( $timezone );
|
||||
$hourNow = $date->format('d');
|
||||
return $hourNow;
|
||||
}
|
||||
public static function getCurrentMonth() {
|
||||
$date = new DateTime();
|
||||
$date->setTimestamp( time() );
|
||||
$timezone = new DateTimeZone( self::getTimezone() );
|
||||
$date->setTimezone( $timezone );
|
||||
$hourNow = $date->format('M');
|
||||
return $hourNow;
|
||||
}
|
||||
public static function getCurrentYear() {
|
||||
$date = new DateTime();
|
||||
$date->setTimestamp( time() );
|
||||
$timezone = new DateTimeZone( self::getTimezone() );
|
||||
$date->setTimezone( $timezone );
|
||||
$hourNow = $date->format('Y');
|
||||
return $hourNow;
|
||||
}
|
||||
public static function getCurrentHour() {
|
||||
$date = new DateTime();
|
||||
$date->setTimestamp( time() );
|
||||
$timezone = new DateTimeZone( self::getTimezone() );
|
||||
$date->setTimezone( $timezone );
|
||||
$hourNow = $date->format('H');
|
||||
return $hourNow;
|
||||
}
|
||||
|
||||
/**
|
||||
* Relative Dates
|
||||
*/
|
||||
public static function getDayStartTimestamp( $timestamp = 0, $with_timezone = false ) {
|
||||
if ( empty( $timestamp ) ) {
|
||||
$timestamp = self::determineDateInput();
|
||||
}
|
||||
$day = date( 'd', $timestamp );
|
||||
$month = date( 'M', $timestamp );
|
||||
$year = date( 'Y', $timestamp );
|
||||
$startTime = '00:00:00';
|
||||
$firstSecond = date( 'U', strtotime( "$startTime $day $month $year" ) );
|
||||
|
||||
if ( $with_timezone ) {
|
||||
$specificDateString = $year.'-'.$month.'-'.$day;
|
||||
|
||||
$startDate = new DateTime( $specificDateString . ' ' . $startTime, new DateTimeZone( self::getTimezone() ) );
|
||||
$startDate->setTimezone( new DateTimeZone( 'UTC' ) );
|
||||
$firstSecond = $startDate->getTimestamp();
|
||||
}
|
||||
return $firstSecond;
|
||||
}
|
||||
public static function getDayEndTimestamp( $timestamp = 0, $with_timezone = false ) {
|
||||
if ( empty( $timestamp ) ) {
|
||||
$timestamp = self::determineDateInput();
|
||||
}
|
||||
$day = date( 'd', $timestamp );
|
||||
$month = date( 'M', $timestamp );
|
||||
$year = date( 'Y', $timestamp );
|
||||
$endTime = '23:59:59';
|
||||
$lastSecond = date( 'U', strtotime( "$endTime $day $month $year" ) );
|
||||
|
||||
if ( $with_timezone ) {
|
||||
$specificDateString = $year.'-'.$month.'-'.$day;
|
||||
|
||||
$endDate = new DateTime( $specificDateString . ' ' . $endTime, new DateTimeZone( self::getTimezone() ) );
|
||||
$endDate->setTimezone( new DateTimeZone( 'UTC' ) );
|
||||
$lastSecond = $endDate->getTimestamp();
|
||||
}
|
||||
return $lastSecond;
|
||||
}
|
||||
public static function getWeekStartTimestamp( $timestamp = 0, $with_timezone = false ) {
|
||||
if ( empty( $timestamp ) ) {
|
||||
$timestamp = self::determineDateInput();
|
||||
}
|
||||
// $timestamp = intval( $timestamp );
|
||||
$startTime = '00:00:00';
|
||||
|
||||
// find the first sunday in the week containing the date
|
||||
if ( date( 'N', $timestamp ) == 7 ) {
|
||||
$firstDate = $timestamp;
|
||||
} else {
|
||||
$firstDate = strtotime('last Sunday', $timestamp);
|
||||
}
|
||||
$firstSecond = date( 'Y-M-d '. $startTime, $firstDate );
|
||||
|
||||
if ( $with_timezone ) {
|
||||
$startDate = new DateTime( $firstSecond, new DateTimeZone( self::getTimezone() ) );
|
||||
$startDate->setTimezone( new DateTimeZone( 'UTC' ) );
|
||||
$firstSecond = $startDate->getTimestamp();
|
||||
} else {
|
||||
$firstSecond = strtotime( $firstSecond );
|
||||
}
|
||||
return $firstSecond;
|
||||
}
|
||||
public static function getWeekEndTimestamp( $timestamp = 0, $with_timezone = false ) {
|
||||
if ( empty( $timestamp ) ) {
|
||||
$timestamp = self::determineDateInput();
|
||||
}
|
||||
// $timestamp = intval( $timestamp );
|
||||
$endTime = '23:59:59';
|
||||
|
||||
// find the last saturday in the week containing the date
|
||||
if ( date( 'N', $timestamp ) == 6 ) {
|
||||
$lastDate = $timestamp;
|
||||
} else {
|
||||
$lastDate = strtotime( 'next Saturday', $timestamp );
|
||||
}
|
||||
$lastSecond = date( 'Y-M-d '. $endTime, $lastDate );
|
||||
|
||||
if ( $with_timezone ) {
|
||||
$endDate = new DateTime( $lastSecond, new DateTimeZone( self::getTimezone() ) );
|
||||
$endDate->setTimezone( new DateTimeZone( 'UTC' ) );
|
||||
$lastSecond = $endDate->getTimestamp();
|
||||
} else {
|
||||
$lastSecond = strtotime( $lastSecond );
|
||||
}
|
||||
return $lastSecond;
|
||||
}
|
||||
public static function getMonthStartTimestamp( $timestamp = 0, $with_timezone = false ) {
|
||||
if ( empty( $timestamp ) ) {
|
||||
$timestamp = self::determineDateInput();
|
||||
}
|
||||
$startTime = '00:00:00';
|
||||
$year = date( 'Y', $timestamp );
|
||||
$month = date( 'M', $timestamp );
|
||||
|
||||
$firstDayUnix = strtotime( "$startTime $month 01 $year" );
|
||||
|
||||
// find the first sunday in the week containing the date
|
||||
if ( date( 'N', $firstDayUnix ) == 7 ) {
|
||||
$firstDate = $firstDayUnix;
|
||||
} else {
|
||||
$firstDate = strtotime('last Sunday', $firstDayUnix);
|
||||
}
|
||||
$firstSecond = date( 'Y-M-d '. $startTime, $firstDate );
|
||||
|
||||
if ( $with_timezone ) {
|
||||
$startDate = new DateTime( $firstSecond, new DateTimeZone( self::getTimezone() ) );
|
||||
$startDate->setTimezone( new DateTimeZone( 'UTC' ) );
|
||||
$firstSecond = $startDate->getTimestamp();
|
||||
} else {
|
||||
$firstSecond = strtotime( $firstSecond );
|
||||
}
|
||||
return $firstSecond;
|
||||
}
|
||||
public static function getMonthEndTimestamp( $timestamp = 0, $with_timezone = false ) {
|
||||
if ( empty( $timestamp ) ) {
|
||||
$timestamp = self::determineDateInput();
|
||||
}
|
||||
$endTime = '23:59:59';
|
||||
$year = date( 'Y', $timestamp );
|
||||
|
||||
// Find last day of month
|
||||
$month = date( 'm', $timestamp );
|
||||
$lastDay = cal_days_in_month( CAL_GREGORIAN, $month, $year );
|
||||
$month = date( 'M', $timestamp );
|
||||
$lastDayUnix = strtotime( "$endTime $month $lastDay $year" );
|
||||
|
||||
// find the last saturday in the week containing the date
|
||||
if ( date( 'N', $lastDayUnix ) == 6 ) {
|
||||
$lastDate = $lastDayUnix;
|
||||
} else {
|
||||
$lastDate = strtotime('next Saturday', $lastDayUnix);
|
||||
}
|
||||
$lastSecond = date( 'Y-M-d '. $endTime, $lastDate );
|
||||
|
||||
if ( $with_timezone ) {
|
||||
$endDate = new DateTime( $lastSecond, new DateTimeZone( self::getTimezone() ) );
|
||||
$endDate->setTimezone( new DateTimeZone( 'UTC' ) );
|
||||
$lastSecond = $endDate->getTimestamp();
|
||||
} else {
|
||||
$lastSecond = strtotime( $lastSecond );
|
||||
}
|
||||
return $lastSecond;
|
||||
}
|
||||
public static function getYearStartTimestamp( $timestamp = 0, $with_timezone = false ) {
|
||||
if ( empty( $timestamp ) ) {
|
||||
$timestamp = self::determineDateInput();
|
||||
}
|
||||
$startTime = '00:00:00';
|
||||
$year = date( 'Y', $timestamp );
|
||||
$firstDayUnix = strtotime( "$startTime January 01 $year" );
|
||||
$firstSecond = date( 'Y-M-d '. $startTime, $firstDayUnix );
|
||||
|
||||
if ( $with_timezone ) {
|
||||
$startDate = new DateTime( $firstSecond, new DateTimeZone( self::getTimezone() ) );
|
||||
$startDate->setTimezone( new DateTimeZone( 'UTC' ) );
|
||||
$firstSecond = $startDate->getTimestamp();
|
||||
} else {
|
||||
$firstSecond = strtotime( $firstSecond );
|
||||
}
|
||||
return $firstSecond;
|
||||
}
|
||||
public static function getYearEndTimestamp( $timestamp = 0, $with_timezone = false ) {
|
||||
if ( empty( $timestamp ) ) {
|
||||
$timestamp = self::determineDateInput();
|
||||
}
|
||||
$endTime = '23:59:59';
|
||||
$year = date( 'Y', $timestamp );
|
||||
$lastDayUnix = strtotime( "$endTime December 31 $year" );
|
||||
$lastSecond = date( 'Y-M-d '. $endTime, $lastDayUnix );
|
||||
|
||||
if ( $with_timezone ) {
|
||||
$endDate = new DateTime( $lastSecond, new DateTimeZone( self::getTimezone() ) );
|
||||
$endDate->setTimezone( new DateTimeZone( 'UTC' ) );
|
||||
$lastSecond = $endDate->getTimestamp();
|
||||
} else {
|
||||
$lastSecond = strtotime( $lastSecond );
|
||||
}
|
||||
return $lastSecond;
|
||||
}
|
||||
}
|
43
functions/hash.php
Normal file
43
functions/hash.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* functions/hash.php
|
||||
*
|
||||
* This class is used to salt, hash, and check passwords.
|
||||
*
|
||||
* @version 1.1.2
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com/libraries/Bedrock
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Bedrock\Functions;
|
||||
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
|
||||
class Hash {
|
||||
/**
|
||||
* Uses php native hashing scheme to make a password hash.
|
||||
*
|
||||
* @param string $password - Validated password input.
|
||||
* @return string - salted/hashed and ready to use password hash.
|
||||
*/
|
||||
public static function make( $password ) {
|
||||
return password_hash( $password, PASSWORD_DEFAULT );
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses php native password support to verify the given password.
|
||||
*
|
||||
* @param string $password - Password being verified.
|
||||
* @param string $hash - Saved password hash.
|
||||
* @return bool
|
||||
*/
|
||||
public static function check( $password, $hash ) {
|
||||
$result = password_verify( $password, $hash );
|
||||
if ( $result ) {
|
||||
return true;
|
||||
}
|
||||
Debug::info( 'Hash::check: Failed to verify password match.' );
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
111
functions/input.php
Normal file
111
functions/input.php
Normal file
@ -0,0 +1,111 @@
|
||||
<?php
|
||||
/**
|
||||
* functions/input.php
|
||||
*
|
||||
* This class manages and returns GET, FILE, and POST variables.
|
||||
*
|
||||
* @version 1.1.2
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com/libraries/Bedrock
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Bedrock\Functions;
|
||||
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
|
||||
class Input {
|
||||
/**
|
||||
* Checks to see if input exists in the order of POST, GET, FILE.
|
||||
* A default name value of "submit" is used if none is specified.
|
||||
*
|
||||
* @param {string} [$data] - Name of the desired input (default: 'submit')
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function exists( $data = 'submit' ) {
|
||||
if ( self::post( $data ) ) {
|
||||
return true;
|
||||
} elseif ( self::get( $data ) ) {
|
||||
return true;
|
||||
} elseif ( self::file( $data ) ) {
|
||||
return true;
|
||||
} else {
|
||||
Debug::log( 'Input::exists: No input Found: '. $data );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a files existence and that it is not null
|
||||
* then returns its value or bool false if none is found
|
||||
*
|
||||
* @param {string} [$data] - Name of desired $_FILES value.
|
||||
* @return {bool|string} - Returns false if not found and a string if found.
|
||||
*/
|
||||
public static function file( $data ) {
|
||||
if ( !isset( $_FILES[$data] ) ) {
|
||||
Debug::log( "Input - file : $data not found." );
|
||||
return false;
|
||||
}
|
||||
if ( $_FILES[$data]['tmp_name'] == '' ) {
|
||||
Debug::log( "Input - file : $data empty." );
|
||||
return false;
|
||||
}
|
||||
return $_FILES[$data];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a post variable named $data and returns
|
||||
* its value if true or bool false if none is found.
|
||||
*
|
||||
* @param {string} [$data] - Name of desired $_POST value.
|
||||
* @return {bool|string} - Returns false if not found and a string if found.
|
||||
*/
|
||||
public static function post( $data ) {
|
||||
if ( !isset( $_POST[$data] ) ) {
|
||||
Debug::debug( "Input - post : $data not found." );
|
||||
return false;
|
||||
}
|
||||
if ( empty( $_POST[$data] ) ) {
|
||||
Debug::debug( "Input - post : $data empty." );
|
||||
return false;
|
||||
}
|
||||
return $_POST[$data];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a post variable named $data and returns
|
||||
* its value if found or null if not found.
|
||||
*
|
||||
* @param {string} [$data] - Name of desired $_POST value.
|
||||
* @return {string}
|
||||
*/
|
||||
public static function postNull( $data ) {
|
||||
if ( !isset( $_POST[$data] ) ) {
|
||||
Debug::debug( "Input - post : $data not found." );
|
||||
return;
|
||||
}
|
||||
if ( empty( $_POST[$data] ) ) {
|
||||
Debug::debug( "Input - post : $data empty." );
|
||||
return;
|
||||
}
|
||||
return $_POST[$data];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a get variable named $data.
|
||||
*
|
||||
* @param {string} [$data] - Name of desired $_GET value.
|
||||
* @return {bool|string} - Returns false if not found and a string if found.
|
||||
*/
|
||||
public static function get( $data ) {
|
||||
if ( !isset( $_GET[$data] ) ) {
|
||||
Debug::debug( "Input - get : $data not found." );
|
||||
return false;
|
||||
}
|
||||
if ( empty( $_GET[$data] ) ) {
|
||||
Debug::debug( "Input - get : $data empty." );
|
||||
return false;
|
||||
}
|
||||
return $_GET[$data];
|
||||
}
|
||||
}
|
44
functions/sanitize.php
Normal file
44
functions/sanitize.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* functions/sanitize.php
|
||||
*
|
||||
* This class is used to sanitize user input.
|
||||
*
|
||||
* @version 1.1.2
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com/libraries/Bedrock
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Bedrock\Functions;
|
||||
|
||||
class Sanitize {
|
||||
/**
|
||||
* This function strips all html tags except for p/a/br from the given string.
|
||||
*
|
||||
* @param {string} [$data] - The string to be parsed
|
||||
* @return {string} - The sanitized string.
|
||||
*/
|
||||
public static function contentShort( $data ) {
|
||||
return strip_tags( $data, '<p><a><br>' );
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is to remove $'s and brackets from the rich HTML editor
|
||||
* which are the only parts that cause parse issues
|
||||
*
|
||||
* @param {string} [$data] - The string to be parsed
|
||||
* @return {string} - The sanitized string.
|
||||
*/
|
||||
public static function rich( $data ) {
|
||||
$data = preg_replace( '#\{#', '{', $data );
|
||||
$data = preg_replace( '#\}#', '}', $data );
|
||||
$data = preg_replace( '#\$#', '$', $data );
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function url( $data ) {
|
||||
$trimmed = rtrim( $data, '/' );
|
||||
$filtered = filter_var( $trimmed, FILTER_SANITIZE_URL );
|
||||
return $filtered;
|
||||
}
|
||||
}
|
136
functions/session.php
Normal file
136
functions/session.php
Normal file
@ -0,0 +1,136 @@
|
||||
<?php
|
||||
/**
|
||||
* functions/session.php
|
||||
*
|
||||
* This class is used for management of session data.
|
||||
*
|
||||
* @version 1.1.2
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com/libraries/Bedrock
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Bedrock\Functions;
|
||||
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
|
||||
class Session {
|
||||
/**
|
||||
* Checks if a session exists.
|
||||
*
|
||||
* @param {string} [$name] - The name of the session being checked for.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function exists( $name ) {
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
if ( ! Check::sessionName( $name ) ) {
|
||||
return false;
|
||||
}
|
||||
$sessionName = DEFAULT_SESSION_PREFIX . $name;
|
||||
if ( isset( $_SESSION[ $sessionName ] ) ) {
|
||||
return true;
|
||||
}
|
||||
Debug::log( "Session::exists - Session not found: $sessionName" );
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the value of a session if it exists
|
||||
*
|
||||
* @param {string} [$name] - The name of the session variable you are trying to retrieve.
|
||||
* @return {string|bool} - Returns the data from the session or false if nothing is found..
|
||||
*/
|
||||
public static function get( $name ) {
|
||||
if ( ! Check::sessionName( $name ) ) {
|
||||
return false;
|
||||
}
|
||||
$sessionName = DEFAULT_SESSION_PREFIX . $name;
|
||||
if ( self::exists( $name ) ) {
|
||||
return $_SESSION[ $sessionName ];
|
||||
}
|
||||
Debug::log( "Session::get - Session not found: $sessionName" );
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a session.
|
||||
*
|
||||
* @param {string} [$name] - Session name.
|
||||
* @param {string} [$data] - Session data.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function put( $name, $data ) {
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
if ( ! Check::sessionName( $name ) ) {
|
||||
return false;
|
||||
}
|
||||
$sessionName = DEFAULT_SESSION_PREFIX . $name;
|
||||
$_SESSION[ $sessionName ] = $data;
|
||||
Debug::log( "Session::get - Created/Updated: $sessionName" );
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the specified session.
|
||||
*
|
||||
* @param {string} [$name] - The name of the session to be destroyed.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function delete( $name ) {
|
||||
if ( ! Check::sessionName( $name ) ) {
|
||||
return false;
|
||||
}
|
||||
$sessionName = DEFAULT_SESSION_PREFIX . $name;
|
||||
if ( self::exists( $name ) ) {
|
||||
unset( $_SESSION[$sessionName] );
|
||||
Debug::info( "Session::delete - Deleted $sessionName" );
|
||||
return true;
|
||||
}
|
||||
Debug::error( "Session::delete - Session not found." );
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Intended as a self-destruct session. If the specified session does not
|
||||
* exist, it is created. If the specified session does exist, it will be
|
||||
* destroyed and returned.
|
||||
*
|
||||
* @param {string} [$name] - Session name to be created or checked
|
||||
* @param {string} [$data] - The string to be used if session needs to be created. (optional)
|
||||
* @return bool|string - Returns bool if creating, and a string if the check is successful.
|
||||
*/
|
||||
public static function checkFlash( $name ) {
|
||||
if ( ! Check::sessionName( $name ) ) {
|
||||
return false;
|
||||
}
|
||||
if ( self::exists( $name ) ) {
|
||||
Debug::log("Session::flash - Exists");
|
||||
$session = self::get( $name );
|
||||
self::delete( $name );
|
||||
return $session;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
public static function flash( $name, $data = null ) {
|
||||
if ( ! Check::sessionName( $name ) ) {
|
||||
return false;
|
||||
}
|
||||
if ( ! empty( $data ) ) {
|
||||
self::put( $name, $data );
|
||||
Debug::log("Session::flash - Session created.");
|
||||
return true;
|
||||
}
|
||||
if ( self::exists( $name ) ) {
|
||||
Debug::log("Session::flash - Exists");
|
||||
$session = self::get( $name );
|
||||
self::delete( $name );
|
||||
return $session;
|
||||
}
|
||||
Debug::error("Session::flash - null return");
|
||||
return;
|
||||
}
|
||||
}
|
120
functions/token.php
Normal file
120
functions/token.php
Normal file
@ -0,0 +1,120 @@
|
||||
<?php
|
||||
/**
|
||||
* functions/token.php
|
||||
*
|
||||
* This class handles form tokens.
|
||||
*
|
||||
* @version 1.1.2
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com/libraries/Bedrock
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Bedrock\Functions;
|
||||
|
||||
use TheTempusProject\Bedrock\Classes\Config;
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
|
||||
class Token {
|
||||
private static $tokenName;
|
||||
private static $tokenSaved;
|
||||
private static $tokenEnabled = 'not_set';
|
||||
|
||||
public static function start() {
|
||||
if ( !self::isTokenEnabled() ) {
|
||||
return false;
|
||||
}
|
||||
if ( empty( self::$tokenName ) ) {
|
||||
self::setTokenName();
|
||||
}
|
||||
if ( empty( self::$tokenSaved ) ) {
|
||||
self::$tokenSaved = Session::get( self::$tokenName );
|
||||
Debug::info( 'Token saved: ' . Session::get( self::$tokenName ) );
|
||||
} else {
|
||||
Debug::log( 'Original token was already saved' );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function setTokenName( $name = '' ) {
|
||||
if ( !empty( $name ) ) {
|
||||
if ( !Check::simpleName( $name ) ) {
|
||||
Debug::warn( "Token name invalid: $name" );
|
||||
return false;
|
||||
}
|
||||
self::$tokenName = $name;
|
||||
}
|
||||
if ( !empty( self::$tokenName ) ) {
|
||||
return true;
|
||||
}
|
||||
self::$tokenName = DEFAULT_TOKEN_NAME;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines, saves, then returns whether or not tokens are enabled.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isTokenEnabled() {
|
||||
if ( self::$tokenEnabled !== 'not_set' ) {
|
||||
return self::$tokenEnabled;
|
||||
}
|
||||
|
||||
$sessionCheck = Check::sessions();
|
||||
if ( $sessionCheck === false ) {
|
||||
self::$tokenEnabled = false;
|
||||
return self::$tokenEnabled;
|
||||
}
|
||||
|
||||
$tokenConfig = Config::getValue( 'main/tokenEnabled' );
|
||||
if ( !empty( $tokenConfig ) ) {
|
||||
self::$tokenEnabled = $tokenConfig;
|
||||
return self::$tokenEnabled;
|
||||
}
|
||||
|
||||
if ( !empty( TOKEN_ENABLED ) ) {
|
||||
self::$tokenEnabled = TOKEN_ENABLED;
|
||||
return self::$tokenEnabled;
|
||||
}
|
||||
|
||||
self::$tokenEnabled = false;
|
||||
return self::$tokenEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a token and stores it as a session variable.
|
||||
*
|
||||
* @return string - Returns the string of the token generated.
|
||||
*/
|
||||
public static function generate() {
|
||||
if ( !self::start() ) {
|
||||
Debug::warn( 'Token disabled' );
|
||||
return false;
|
||||
}
|
||||
$token = Code::genToken();
|
||||
Session::put( self::$tokenName, $token );
|
||||
Debug::info( 'New token generated' );
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks a form token against a session token to confirm no XSS has occurred.
|
||||
*
|
||||
* @param string $token - This should be a post variable from the hidden token field.
|
||||
* @return bool
|
||||
*/
|
||||
public static function check( $token ) {
|
||||
if ( !self::start() ) {
|
||||
Debug::warn( 'Token disabled' );
|
||||
return false;
|
||||
}
|
||||
if ( $token === self::$tokenSaved ) {
|
||||
Debug::info( 'Token check passed' );
|
||||
return true;
|
||||
}
|
||||
Debug::error( 'Token check failed' );
|
||||
Debug::error( 'token: ' . $token );
|
||||
Debug::error( 'tokenSaved: ' . self::$tokenSaved );
|
||||
return false;
|
||||
}
|
||||
}
|
73
functions/upload.php
Normal file
73
functions/upload.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/**
|
||||
* functions/upload.php
|
||||
*
|
||||
* This class is used for manipulation of Images used by the application.
|
||||
*
|
||||
* @todo Add the config switches.
|
||||
*
|
||||
* @version 1.1.2
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com/libraries/Bedrock
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Bedrock\Functions;
|
||||
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
|
||||
class Upload {
|
||||
public static $lastUpload = null;
|
||||
public static $lastUploadLocation = null;
|
||||
|
||||
/**
|
||||
* This function verifies a valid image upload, creates any
|
||||
* necessary directories, moves, and saves, the image.
|
||||
*
|
||||
* @param {string} [$fieldname] - The name of the input field for the upload.
|
||||
* @param {string} [$folder] - The sub-folder to store the uploaded image.
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function image( $fieldname, $folder ) {
|
||||
if ( !Check::imageUpload( $fieldname ) ) {
|
||||
Debug::error( Check::systemErrors() );
|
||||
return false;
|
||||
}
|
||||
// @todo Let's try and avoid 777 if possible
|
||||
// Try catch here for better error handling
|
||||
if ( empty( $folder ) ) {
|
||||
$folder = IMAGE_UPLOAD_DIRECTORY;
|
||||
}
|
||||
if ( !file_exists( $folder ) ) {
|
||||
Debug::Info( 'Creating Directory because it does not exist' );
|
||||
mkdir( $folder, 0777, true );
|
||||
}
|
||||
self::$lastUpload = basename( $_FILES[$fieldname]['name'] );
|
||||
self::$lastUploadLocation = $folder . self::$lastUpload;
|
||||
if ( move_uploaded_file( $_FILES[$fieldname]['tmp_name'], self::$lastUploadLocation ) ) {
|
||||
return true;
|
||||
} else {
|
||||
Debug::error( 'failed to move the file.' );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the file location of the most recent
|
||||
* uploaded image if one exists.
|
||||
*
|
||||
* @return {string} - The file location of the most recent uploaded image.
|
||||
*/
|
||||
public static function lastLocation() {
|
||||
return self::$lastUploadLocation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the most recent
|
||||
* uploaded image if one exists.
|
||||
*
|
||||
* @return {string} - The filename of the most recent uploaded image.
|
||||
*/
|
||||
public static function last() {
|
||||
return self::$lastUpload;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user