rename p1
This commit is contained in:
47
renamed/.gitignore
vendored
Normal file
47
renamed/.gitignore
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
# Windows image file caches
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
|
||||
# Folder config file
|
||||
Desktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Windows Installer files
|
||||
*.cab
|
||||
*.msi
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# Windows shortcuts
|
||||
*.lnk
|
||||
|
||||
# =========================
|
||||
# Operating System Files
|
||||
# =========================
|
||||
|
||||
# OSX
|
||||
# =========================
|
||||
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
||||
# Files that might appear in the root of a volume
|
||||
.DocumentRevisions-V100
|
||||
.fseventsd
|
||||
.Spotlight-V100
|
||||
.TemporaryItems
|
||||
.Trashes
|
||||
.VolumeIcon.icns
|
||||
|
||||
# Directories potentially created on remote AFP share
|
||||
.AppleDB
|
||||
.AppleDesktop
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
49
renamed/.gitlab-ci.yml
Normal file
49
renamed/.gitlab-ci.yml
Normal file
@ -0,0 +1,49 @@
|
||||
stages:
|
||||
- update
|
||||
|
||||
variables:
|
||||
TIMEZONE: "America/New_York" # For the system in general
|
||||
DATE_TIMEZONE: ${TIMEZONE} # For PHP
|
||||
|
||||
GIT_DEPTH: 1
|
||||
GITLAB_API_URL: ${CI_API_V4_URL}
|
||||
TARGET_BRANCH: ${CI_COMMIT_REF_NAME} # This is the branch chosen in the `Pipeline Schedule`
|
||||
TARGET_REMOTE: "https://${GITLAB_USERNAME}:${GITLAB_ACCESS_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}.git"
|
||||
|
||||
# These could/should be overridden in an extending job:
|
||||
UPDATE_BRANCH_PREFIX: "update_PHP_deps_" # Used for the update branch name, it will be followed by the datetime
|
||||
GIT_USER: "DependBot" # Used for the update commit
|
||||
GIT_EMAIL: "webmaster@thetempusproject.com" # Used for the update commit
|
||||
GITLAB_USERNAME: "root" # Used for pushing the new branch and opening the MR
|
||||
GITLAB_ACCESS_TOKEN: "glpat-PKEmivGtBfbz4DVPdhzk" # Used for pushing the new branch and opening the MR
|
||||
MERGE_IF_SUCCESSFUL: "true" # Set to true, to merge automatically if the pipeline succeeds
|
||||
SECONDS_BETWEEN_POOLING: 10 # Nbr of seconds between checking if the MR pipeline is successful, so then it will merge
|
||||
JOB_GIT_FLAGS: ""
|
||||
JOB_CURL_FLAGS: ""
|
||||
JOB_COMPOSER_FLAGS: ""
|
||||
|
||||
composer_update:
|
||||
stage: update
|
||||
rules:
|
||||
- if: '$CI_COMMIT_BRANCH == "main"'
|
||||
image: composer:latest
|
||||
interruptible: true # allows to stop the job if a newer pipeline starts, saving resources and allowing new jobs to start because job concurrency is limited
|
||||
script:
|
||||
- git ${JOB_GIT_FLAGS} fetch origin ${TARGET_BRANCH}
|
||||
- git ${JOB_GIT_FLAGS} checkout ${TARGET_BRANCH}
|
||||
- git reset --hard origin/main
|
||||
- export DATE_TIME="$(date '+%Y%m%d%H%M%S')"
|
||||
- export MR_BRANCH="${UPDATE_BRANCH_PREFIX}${DATE_TIME}"
|
||||
- git ${JOB_GIT_FLAGS} checkout -b "${MR_BRANCH}"
|
||||
- composer update ${JOB_COMPOSER_FLAGS}
|
||||
- if [ "$(git diff)" == "" ]; then echo "No updates needed!"; exit 0; fi
|
||||
- export TITLE="Update PHP dependencies [${DATE_TIME}]"
|
||||
- git ${JOB_GIT_FLAGS} commit -a -m "${TITLE}"
|
||||
- git ${JOB_GIT_FLAGS} push "${TARGET_REMOTE}" "${MR_BRANCH}"
|
||||
artifacts:
|
||||
paths:
|
||||
- vendor/
|
||||
cache:
|
||||
key: ${CI_COMMIT_REF_SLUG}
|
||||
paths:
|
||||
- vendor/
|
21
renamed/LICENSE
Normal file
21
renamed/LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Joey Kimsey
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
70
renamed/README.md
Normal file
70
renamed/README.md
Normal file
@ -0,0 +1,70 @@
|
||||
|
||||
# Bedrock
|
||||
|
||||
Bedrock is the core functionality used by [The Tempus Project](https://github.com/TheTempusProject/TheTempusProject) a rapid prototyping framework. It provides database support, configuration, base models, and base controller functionality a in addition to a host of other integrated functions.
|
||||
|
||||
This Library can be utilized outside of TheTempusProject, but the functionality has not been tested well as a stand-alone library.
|
||||
|
||||
## Installation
|
||||
|
||||
To install simply use the composer command:
|
||||
|
||||
`php composer.phar require thetempusproject/bedrock`
|
||||
|
||||
## Usage
|
||||
|
||||
Typical usage would be through including the package via composer.
|
||||
|
||||
```php
|
||||
|
||||
namespace MyApp;
|
||||
|
||||
use TheTempusProject\Bedrock\Bin\Bedrock;
|
||||
|
||||
require_once VENDOR_DIRECTORY . 'autoload.php';
|
||||
|
||||
class MyApp extends Bedrock {
|
||||
// "Stuff", "Things", and "What-not"
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
If you would like to use hermes own autoloading, simply inclode the constants file and the autoload file inside `/bin/`.
|
||||
|
||||
```php
|
||||
|
||||
use TheTempusProject\Bedrock\Bin\Bedrock;
|
||||
|
||||
// Bedrock Constants
|
||||
if ( ! defined( 'BEDROCK_CONSTANTS_LOADED' ) ) {
|
||||
if ( defined( 'BEDROCK_CONFIG_DIRECTORY' ) ) {
|
||||
require_once BEDROCK_CONFIG_DIRECTORY . 'constants.php';
|
||||
}
|
||||
}
|
||||
|
||||
// Bedrock Autoloader (Autoloader)
|
||||
if ( ! defined( 'BEDROCK_AUTOLOADED' ) ) {
|
||||
if ( defined( 'BEDROCK_ROOT_DIRECTORY' ) ) {
|
||||
require_once BEDROCK_ROOT_DIRECTORY . 'bin' . DIRECTORY_SEPARATOR . 'autoload.php';
|
||||
}
|
||||
}
|
||||
|
||||
class MyApp extends Bedrock {
|
||||
// "Stuff", "Things", and "What-not"
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### To-Do
|
||||
|
||||
- [ ] Expansion of PDO to allow different database types
|
||||
- [ ] Updates for configs and models to re-build based on a delta model, to make version changes simpler. (Migration system)
|
||||
- [ ] Implement better uniformity in terms of error reporting, exceptions, logging.
|
||||
|
||||
## Issues / Bugs / Contact
|
||||
|
||||
If anyone actually uses this library and runs into any issues, feel free to contact me and I'll look into it.
|
||||
|
||||
[Joey Kimsey](mailto:Joey@thetempusproject.com) - _Lead Developer_
|
||||
|
||||
[JoeyKimsey.com](https://JoeyKimsey.com)
|
45
renamed/bin/autoload.php
Normal file
45
renamed/bin/autoload.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* bin/autoload.php
|
||||
*
|
||||
* Handles the initial setup like autoloading, basic functions, constants, etc.
|
||||
*
|
||||
* @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;
|
||||
|
||||
use TheTempusProject\Hermes\Classes\Autoloader;
|
||||
|
||||
if ( ! defined('BEDROCK_ROOT_DIRECTORY' ) ) {
|
||||
define('BEDROCK_ROOT_DIRECTORY', dirname(__DIR__) . DIRECTORY_SEPARATOR);
|
||||
}
|
||||
if ( ! defined('BEDROCK_CONFIG_DIRECTORY' ) ) {
|
||||
define('BEDROCK_CONFIG_DIRECTORY', BEDROCK_ROOT_DIRECTORY . 'Config' . DIRECTORY_SEPARATOR);
|
||||
}
|
||||
if ( ! defined('BEDROCK_CONSTANTS_LOADED' ) ) {
|
||||
require_once BEDROCK_CONFIG_DIRECTORY . 'constants.php';
|
||||
}
|
||||
if ( class_exists( 'TheTempusProject\Hermes\Classes\Autoloader' ) ) {
|
||||
$autoloader = new Autoloader;
|
||||
$autoloader->setRootFolder( BEDROCK_ROOT_DIRECTORY );
|
||||
$autoloader->addNamespace(
|
||||
'TheTempusProject\Bedrock',
|
||||
'Bin'
|
||||
);
|
||||
$autoloader->addNamespace(
|
||||
'TheTempusProject\Bedrock\Classes',
|
||||
'Classes'
|
||||
);
|
||||
$autoloader->addNamespace(
|
||||
'TheTempusProject\Bedrock\Functions',
|
||||
'Functions'
|
||||
);
|
||||
$autoloader->register();
|
||||
}
|
||||
|
||||
require_once 'Bedrock.php';
|
||||
|
||||
define( 'BEDROCK_AUTOLOADED', true );
|
274
renamed/bin/bedrock.php
Normal file
274
renamed/bin/bedrock.php
Normal file
@ -0,0 +1,274 @@
|
||||
<?php
|
||||
/**
|
||||
* bin/bedrock.php
|
||||
*
|
||||
* This file parses any given url and separates it into controller,
|
||||
* method, and data. This allows the application to direct the user
|
||||
* to the desired location and provide the controller any additional
|
||||
* information it may require to run.
|
||||
*
|
||||
* @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\Bin;
|
||||
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
use TheTempusProject\Hermes\Functions\Route as Routes;
|
||||
use TheTempusProject\Bedrock\Functions\Input;
|
||||
use TheTempusProject\Bedrock\Functions\Check;
|
||||
use TheTempusProject\Bedrock\Functions\Token;
|
||||
use TheTempusProject\Bedrock\Functions\Sanitize;
|
||||
use TheTempusProject\Bedrock\Classes\Config;
|
||||
use TheTempusProject\Hermes\Classes\Autoloader;
|
||||
use TheTempusProject\Houdini\Classes\Template;
|
||||
use TheTempusProject\Houdini\Classes\Components;
|
||||
|
||||
class Bedrock {
|
||||
public static $controllerName = '';
|
||||
public static $methodName = '';
|
||||
public static $activeConfig = [];
|
||||
protected $controllerObject = null;
|
||||
protected $controllerClass = '';
|
||||
protected $params = [];
|
||||
protected $controllerError = false;
|
||||
|
||||
/**
|
||||
* The constructor handles the entire process of parsing the url,
|
||||
* finding the controller/method, and calling the appropriate
|
||||
* class/function for the application.
|
||||
*
|
||||
* @param string $url - A custom URL to be parsed to determine
|
||||
* controller/method. (GET) url is used by
|
||||
* default if none is provided
|
||||
*/
|
||||
public function __construct( $url = '' ) {
|
||||
Debug::group( 'Bedrock Application' );
|
||||
ob_start();
|
||||
self::$activeConfig = new Config( CONFIG_JSON );
|
||||
set_error_handler( [ BEDROCK_DEFAULT_ERROR_HANDLER, 'handle_error' ] );
|
||||
set_exception_handler( [ BEDROCK_DEFAULT_EXCEPTION_HANDLER, 'handle_exception' ] );
|
||||
self::$controllerName = DEFAULT_CONTROLLER_CLASS;
|
||||
self::$methodName = DEFAULT_CONTROLLER_METHOD;
|
||||
$this->setUrl( $url );
|
||||
}
|
||||
|
||||
public function setUrl( $url ) {
|
||||
if ( empty( $url ) ) {
|
||||
Debug::log( 'Using GET url.' );
|
||||
$url = Input::get( 'url' );
|
||||
}
|
||||
$trimmedUrl = trim( $url, '/' );
|
||||
$url = str_ireplace( '.php', '', $trimmedUrl );
|
||||
$urlArray = explode( '/', Sanitize::url( $url ) );
|
||||
$this->setVarsFromUrlArray( $urlArray );
|
||||
}
|
||||
|
||||
protected function setVarsFromUrlArray( $urlArray ) {
|
||||
if ( !empty( $urlArray[0] ) ) {
|
||||
$urlPart = array_shift( $urlArray );
|
||||
if ( $urlPart == 'admin' ) {
|
||||
$namespace = APP_SPACE . '\\Controllers\\Admin\\';
|
||||
if ( empty( $urlArray[0] ) ) {
|
||||
// if there is no second param, assume they want the home controller
|
||||
$urlPart = 'home';
|
||||
} else {
|
||||
$urlPart = array_shift( $urlArray ); // to drop the admin
|
||||
}
|
||||
if ( $urlPart == 'index' ) {
|
||||
// if its admin/index, again, assume the home controller
|
||||
$urlPart = 'home';
|
||||
}
|
||||
} elseif ( $urlPart == 'api' ) {
|
||||
$namespace = APP_SPACE . '\\Controllers\\Api\\';
|
||||
if ( empty( $urlArray[0] ) ) {
|
||||
// if there is no second param, assume they want the home controller
|
||||
$urlPart = 'home';
|
||||
} else {
|
||||
$urlPart = array_shift( $urlArray ); // to drop the admin
|
||||
}
|
||||
if ( $urlPart == 'index' ) {
|
||||
// if its admin/index, again, assume the home controller
|
||||
$urlPart = 'home';
|
||||
}
|
||||
} else {
|
||||
$namespace = APP_SPACE . '\\Controllers\\';
|
||||
}
|
||||
$this->setController( $urlPart, $namespace );
|
||||
}
|
||||
if ( !empty( $urlArray[0] ) ) {
|
||||
$urlPart = array_shift( $urlArray );
|
||||
$this->setPage( $urlPart );
|
||||
}
|
||||
if ( !empty( $urlArray ) ) {
|
||||
$this->params = array_values( $urlArray );
|
||||
}
|
||||
}
|
||||
|
||||
public function load() {
|
||||
$this->loadController();
|
||||
$this->loadPage();
|
||||
}
|
||||
|
||||
public static function getUrl() {
|
||||
return Routes::getAddress() . Input::get( 'url' );
|
||||
}
|
||||
|
||||
protected function loadPage() {
|
||||
if ( !method_exists( $this->controllerClass, self::$methodName ) ) {
|
||||
return false;
|
||||
}
|
||||
Components::set( 'META_IMAGE', Routes::getAddress() . Config::getValue( 'main/logoLarge' ) );
|
||||
Components::set( 'CURRENT_URL', self::getCurrentUrl() );
|
||||
Components::set( 'SITENAME', Config::getValue( 'main/name' ) ?? APP_NAME );
|
||||
Components::set( 'AUTHOR', '<meta name="author" content="' . Config::getValue( 'main/name' ) . '">' );
|
||||
call_user_func_array( [ $this->controllerObject, self::$methodName ], $this->params );
|
||||
Components::set( 'TITLE', Template::parse( $this->controllerObject::$title ) );
|
||||
Components::set( 'PAGE_DESCRIPTION', Template::parse( $this->controllerObject::$pageDescription ) );
|
||||
Template::render();
|
||||
Debug::closeAllGroups();
|
||||
// self::$session->updatePage( self::getUrl() ); // where did this method go?
|
||||
}
|
||||
|
||||
protected function loadController() {
|
||||
if ( empty( $this->controllerClass ) ) {
|
||||
$this->controllerClass = (string) APP_SPACE . '\\Controllers\\' . self::$controllerName;
|
||||
}
|
||||
if ( empty( $this->controllerError ) ) {
|
||||
Components::set( 'TOKEN', Token::generate() );
|
||||
}
|
||||
$this->controllerObject = new $this->controllerClass;
|
||||
}
|
||||
|
||||
protected function setController( $name, $namespace ) {
|
||||
$controllerClass = $namespace . ucfirst( $name );
|
||||
if ( Autoloader::testLoad( $controllerClass ) ) {
|
||||
$this->controllerClass = $controllerClass;
|
||||
self::$controllerName = $name;
|
||||
} else {
|
||||
$this->controllerError = 'setController - Controller not found. Name: ' . $name . ' Namespace: ' . $namespace;
|
||||
Debug::info( $this->controllerError );
|
||||
}
|
||||
}
|
||||
|
||||
protected function setPage( $name ) {
|
||||
$name = strtolower( $name );
|
||||
if ( empty( $this->controllerClass ) ) {
|
||||
Debug::info( 'setPage - controllerClass Empty' );
|
||||
return false;
|
||||
}
|
||||
if ( ! method_exists( $this->controllerClass, $name ) ) {
|
||||
Debug::info( 'setPage - Method not found. Controller: ' . $this->controllerClass . ' Method: ' . $name );
|
||||
return false;
|
||||
}
|
||||
self::$methodName = $name;
|
||||
}
|
||||
|
||||
public static function getCurrentUrl() {
|
||||
return Sanitize::url( Input::get( 'url' ) );
|
||||
}
|
||||
|
||||
protected function printDebug() {
|
||||
echo '<div style="margin: 0 auto; padding-bottom: 25px; background: #eee; width: 1000px;">';
|
||||
echo '<h1 style="text-align: center;">PHP Info</h1>';
|
||||
echo '<table style="margin: 0 auto; padding-bottom: 25px; background: #eee; width: 950px;">';
|
||||
echo '<tr><td>PHP version: </td><td><code>' . phpversion() . '</code><br></td></tr>';
|
||||
echo '<tr><td>PDO Loaded version: </td><td><code>' . extension_loaded( 'pdo' ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>PHP extensions: </td><td><pre>';
|
||||
foreach ( get_loaded_extensions() as $i => $ext ) {
|
||||
echo $ext . ' => ' . phpversion( $ext ) . '<br/>';
|
||||
}
|
||||
echo '</pre><br></td></tr>';
|
||||
echo '</table>';
|
||||
echo '<h1 style="text-align: center;">Tempus Core Info</h1>';
|
||||
echo '<table style="margin: 0 auto; padding-bottom: 25px; background: #eee; width: 950px;">';
|
||||
// Just in case
|
||||
echo '<tr><td>_SERVER: </td><td><pre>';
|
||||
echo var_export( $_SERVER, true );
|
||||
echo '</pre><br></td></tr>';
|
||||
// Checks
|
||||
echo '<tr><td style="text-align: center; padding-top: 25px; padding-bottom: 10px;" colspan="2"><h2>Checks</h2></td></tr>';
|
||||
echo '<tr><td>Uploads work?: </td><td><code>' . var_export( Check::uploads(), true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>PHP: </td><td><code>' . var_export( Check::php(), true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>Mail works?: </td><td><code>' . var_export( Check::mail(), true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>Is safe mode?: </td><td><code>' . var_export( Check::safe(), true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>Sessions work?: </td><td><code>' . var_export( Check::sessions(), true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>Cookies work?: </td><td><code>' . var_export( Check::cookies(), true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>is Apache?: </td><td><code>' . var_export( Check::isApache(), true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>is Nginx?: </td><td><code>' . var_export( Check::isNginx(), true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>Is token enabled?: </td><td><code>' . var_export( Token::isTokenEnabled(), true ) . '</code><br></td></tr>';
|
||||
// Routes
|
||||
echo '<tr><td style="text-align: center; padding-top: 25px; padding-bottom: 10px;" colspan="2"><h2>Routes</h2></td></tr>';
|
||||
echo '<tr><td>Root: </td><td><code>' . var_export( Routes::getRoot(), true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>Address: </td><td><code>' . var_export( Routes::getAddress(), true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>Protocol: </td><td><code>' . var_export( Routes::getProtocol(), true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>App URL: </td><td><code>' . var_export( self::getUrl(), true ) . '</code><br></td></tr>';
|
||||
// Debugging
|
||||
echo '<tr><td style="text-align: center; padding-top: 25px; padding-bottom: 10px;" colspan="2"><h2>Debugging</h2></td></tr>';
|
||||
echo '<tr><td>Console Debugging Enabled: </td><td><code>' . var_export( Debug::status( 'console' ), true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>Debug Trace Enabled: </td><td><code>' . var_export( Debug::status( 'trace' ), true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>Debugging Enabled: </td><td><code>' . var_export( Debug::status( 'debug' ), true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>Rendering Enabled: </td><td><code>' . var_export( Debug::status( 'render' ), true ) . '</code><br></td></tr>';
|
||||
// Main
|
||||
echo '<tr><td style="text-align: center; padding-top: 25px; padding-bottom: 10px;" colspan="2"><h2>Main App Variables</h2></td></tr>';
|
||||
echo '<tr><td>Template Location: </td><td><code>' . var_export( Template::getLocation(), true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>Configuration: </td><td><pre>' . var_export( Config::$config, true ) . '</pre></td></tr>';
|
||||
echo '<tr><td>Check Errors: </td><td><pre>' . var_export( Check::systemErrors(), true ) . '</pre></td></tr>';
|
||||
echo '<tr><td>GET: </td><td><pre>' . var_export( $_GET, true ) . '</pre></td></tr>';
|
||||
// Constants
|
||||
echo '<tr><td style="text-align: center; padding-top: 25px; padding-bottom: 10px;" colspan="2"><h2>Constants</h2></td></tr>';
|
||||
// Debugging
|
||||
echo '<tr><td style="text-align: center;"><b>Debugging:</b></td><td></td></tr>';
|
||||
echo '<tr><td>HERMES_REDIRECTS_ENABLED: </td><td><code>' . var_export( HERMES_REDIRECTS_ENABLED, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>CANARY_TRACE_ENABLED: </td><td><code>' . var_export( CANARY_TRACE_ENABLED, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>CANARY_ENABLED: </td><td><code>' . var_export( CANARY_ENABLED, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>CANARY_DEBUG_TO_CONSOLE: </td><td><code>' . var_export( CANARY_DEBUG_TO_CONSOLE, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>CANARY_DEBUG_TO_FILE: </td><td><code>' . var_export( CANARY_DEBUG_TO_FILE, true ) . '</code><br></td></tr>';
|
||||
// Tempus Debugger
|
||||
echo '<tr><td style="text-align: center;"><b>Tempus Debugger:</b></td><td></td></tr>';
|
||||
echo '<tr><td>CANARY_SECURE_HASH: </td><td><code>' . var_export( CANARY_SECURE_HASH, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>CANARY_SHOW_LINES: </td><td><code>' . var_export( CANARY_SHOW_LINES, true ) . '</code><br></td></tr>';
|
||||
// Tokens
|
||||
echo '<tr><td style="text-align: center;"><b>Tokens:</b></td><td></td></tr>';
|
||||
echo '<tr><td>DEFAULT_TOKEN_NAME: </td><td><code>' . var_export( DEFAULT_TOKEN_NAME, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>TOKEN_ENABLED: </td><td><code>' . var_export( TOKEN_ENABLED, true ) . '</code><br></td></tr>';
|
||||
// Cookies
|
||||
echo '<tr><td style="text-align: center;"><b>Cookies:</b></td><td></td></tr>';
|
||||
echo '<tr><td>DEFAULT_COOKIE_EXPIRATION: </td><td><code>' . var_export( DEFAULT_COOKIE_EXPIRATION, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>DEFAULT_COOKIE_PREFIX: </td><td><code>' . var_export( DEFAULT_COOKIE_PREFIX, true ) . '</code><br></td></tr>';
|
||||
// Directories
|
||||
echo '<tr><td style="text-align: center;"><b>Directories:</b></td><td></td></tr>';
|
||||
echo '<tr><td>APP_ROOT_DIRECTORY: </td><td><code>' . var_export( APP_ROOT_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>BIN_DIRECTORY: </td><td><code>' . var_export( BIN_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>UPLOAD_DIRECTORY: </td><td><code>' . var_export( UPLOAD_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>IMAGE_UPLOAD_DIRECTORY: </td><td><code>' . var_export( IMAGE_UPLOAD_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>CLASSES_DIRECTORY: </td><td><code>' . var_export( CLASSES_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>CONFIG_DIRECTORY: </td><td><code>' . var_export( CONFIG_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>FUNCTIONS_DIRECTORY: </td><td><code>' . var_export( FUNCTIONS_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>TEMPLATE_DIRECTORY: </td><td><code>' . var_export( TEMPLATE_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>VIEW_DIRECTORY: </td><td><code>' . var_export( VIEW_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>ERRORS_DIRECTORY: </td><td><code>' . var_export( ERRORS_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>RESOURCES_DIRECTORY: </td><td><code>' . var_export( RESOURCES_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>BEDROCK_ROOT_DIRECTORY: </td><td><code>' . var_export( BEDROCK_ROOT_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>BEDROCK_BIN_DIRECTORY: </td><td><code>' . var_export( BEDROCK_BIN_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>BEDROCK_CLASSES_DIRECTORY: </td><td><code>' . var_export( BEDROCK_CLASSES_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>BEDROCK_CONFIG_DIRECTORY: </td><td><code>' . var_export( BEDROCK_CONFIG_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>BEDROCK_FUNCTIONS_DIRECTORY: </td><td><code>' . var_export( BEDROCK_FUNCTIONS_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>BEDROCK_RESOURCES_DIRECTORY: </td><td><code>' . var_export( BEDROCK_RESOURCES_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>BEDROCK_VIEW_DIRECTORY: </td><td><code>' . var_export( BEDROCK_VIEW_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>BEDROCK_ERRORS_DIRECTORY: </td><td><code>' . var_export( BEDROCK_ERRORS_DIRECTORY, true ) . '</code><br></td></tr>';
|
||||
// other
|
||||
echo '<tr><td style="text-align: center;"><b>Other:</b></td><td></td></tr>';
|
||||
echo '<tr><td>MINIMUM_PHP_VERSION: </td><td><code>' . var_export( MINIMUM_PHP_VERSION, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>DATA_TITLE_PREG: </td><td><code>' . var_export( DATA_TITLE_PREG, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>PATH_PREG_REQS: </td><td><code>' . var_export( PATH_PREG_REQS, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>SIMPLE_NAME_PREG: </td><td><code>' . var_export( SIMPLE_NAME_PREG, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>ALLOWED_IMAGE_UPLOAD_EXTENTIONS: </td><td><code>' . var_export( ALLOWED_IMAGE_UPLOAD_EXTENTIONS, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>MAX_RESULTS_PER_PAGE: </td><td><code>' . var_export( MAX_RESULTS_PER_PAGE, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>DEFAULT_RESULTS_PER_PAGE: </td><td><code>' . var_export( DEFAULT_RESULTS_PER_PAGE, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>DEFAULT_SESSION_PREFIX: </td><td><code>' . var_export( DEFAULT_SESSION_PREFIX, true ) . '</code><br></td></tr>';
|
||||
echo '<tr><td>DEFAULT_CONTROLLER_CLASS: </td><td><code>' . var_export( DEFAULT_CONTROLLER_CLASS, true ) . '</code><br></td></tr>';
|
||||
echo '</table></div>';
|
||||
}
|
||||
}
|
380
renamed/classes/config.php
Normal file
380
renamed/classes/config.php
Normal file
@ -0,0 +1,380 @@
|
||||
<?php
|
||||
/**
|
||||
* classes/config.php
|
||||
*
|
||||
* This class handles all the hard-coded configurations.
|
||||
*
|
||||
* @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\Classes;
|
||||
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
use TheTempusProject\Bedrock\Functions\Check;
|
||||
use TheTempusProject\Bedrock\Functions\Input;
|
||||
use TheTempusProject\Bedrock\Functions\Upload;
|
||||
|
||||
class Config {
|
||||
public static $config = false;
|
||||
private static $location = false;
|
||||
private static $initialized = false;
|
||||
|
||||
/**
|
||||
* Default constructor which will attempt to load the config from the location specified.
|
||||
*
|
||||
* @param {string} [$location]
|
||||
* @return {null|object}
|
||||
*/
|
||||
public function __construct( $location = '' ) {
|
||||
if ( self::$initialized !== false ) {
|
||||
Debug::log( 'Config already initialized.' );
|
||||
return $this;
|
||||
}
|
||||
if ( empty( $location ) ) {
|
||||
$location = CONFIG_JSON;
|
||||
}
|
||||
self::$initialized = $this->load( $location );
|
||||
if ( self::$initialized !== false ) {
|
||||
Debug::log( 'Config initialization succeeded.' );
|
||||
return $this;
|
||||
}
|
||||
Debug::warn( 'Config initialization failed.' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to retrieve then set the configuration from a file.
|
||||
* @note This function will reset the config every time it is used.
|
||||
*
|
||||
* @param {string} $location
|
||||
* @return {bool}
|
||||
*/
|
||||
public function load( $location ) {
|
||||
self::$config = $this->getConfigFile( $location );
|
||||
self::$location = $location;
|
||||
if ( self::$config === false || empty( self::$config ) ) {
|
||||
Debug::warn( 'Config load failed.' );
|
||||
return false;
|
||||
}
|
||||
Debug::log( 'Config load succeeded.' );
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens and decodes the config json from the location provided.
|
||||
*
|
||||
* @param {string} [$location]
|
||||
* @return {bool|array}
|
||||
*/
|
||||
public function getConfigFile( $location ) {
|
||||
if ( file_exists( $location ) ) {
|
||||
Debug::debug( "Config json found: $location" );
|
||||
return json_decode( file_get_contents( $location ), true );
|
||||
} else {
|
||||
Debug::warn( "Config json not found: $location" );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new config option for the specified category.
|
||||
*
|
||||
* NOTE: Use a default option when using this function to
|
||||
* aid in fail safe execution.
|
||||
*
|
||||
* @param {string} [$category] - The primary category to add the option to.
|
||||
* @param {string} [$node] - The name of the new option.
|
||||
* @param {wild} [$value] - The desired value for the new option.
|
||||
* @param {bool} [$createMissing] - Whether or not to create missing options.
|
||||
* @param {bool} [$save] - Whether or not to save the config.
|
||||
* @param {bool} [$saveDefault] - Whether or not to save the default config.
|
||||
* @return {bool}
|
||||
*/
|
||||
public function update( $category, $node, $value, $createMissing = false, $save = false, $saveDefault = false ) {
|
||||
// @todo: createMissing is unused here
|
||||
if ( self::$config === false ) {
|
||||
Debug::warn( 'Config not loaded.' );
|
||||
return false;
|
||||
}
|
||||
if ( !Check::simpleName( $category ) ) {
|
||||
Debug::warn( "Category name invalid: $categoryName" );
|
||||
return false;
|
||||
}
|
||||
if ( !isset( self::$config[$category] ) ) {
|
||||
Debug::warn( "No such category: $category" );
|
||||
return false;
|
||||
}
|
||||
if ( !Check::simpleName( $node ) ) {
|
||||
Debug::warn( "Node name invalid: $categoryName" );
|
||||
return false;
|
||||
}
|
||||
if ( !isset( self::$config[$category][$node] ) ) {
|
||||
Debug::warn( 'Config not found.' );
|
||||
return false;
|
||||
}
|
||||
if ( $value === 'true' ) {
|
||||
$value = true;
|
||||
}
|
||||
if ( $value === 'false' ) {
|
||||
$value = false;
|
||||
}
|
||||
self::$config[$category][$node]['value'] = $value;
|
||||
if ( $save ) {
|
||||
$this->save( $saveDefault );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function updateFromForm( $save = false, $saveDefault = false ) {
|
||||
if ( self::$config === false ) {
|
||||
Debug::warn( 'Config not loaded.' );
|
||||
return;
|
||||
}
|
||||
foreach ( self::$config as $category => $fields ) {
|
||||
if ( empty( self::$config[ $category ] ) ) {
|
||||
Debug::warn( "Config category not found: $category" );
|
||||
continue;
|
||||
}
|
||||
foreach ( self::$config[ $category ] as $field => $node ) {
|
||||
$name = $category . '/' . $field;
|
||||
if ( empty( $node ) ) {
|
||||
continue;
|
||||
}
|
||||
if ( !empty( $node['protected'] ) ) {
|
||||
continue;
|
||||
}
|
||||
$fieldname = str_ireplace( '/', '-', $name );
|
||||
if ( Input::exists( $fieldname ) ) {
|
||||
if ( 'file' == $node['type'] ) {
|
||||
$upload = Upload::image( $fieldname, IMAGE_DIRECTORY );
|
||||
if ( $upload ) {
|
||||
$route = str_replace( APP_ROOT_DIRECTORY, '', IMAGE_DIRECTORY );
|
||||
$this->update( $category, $field, $route . Upload::last() );
|
||||
} else {
|
||||
Debug::error( 'There was an error with your upload.');
|
||||
}
|
||||
} else {
|
||||
$this->update( $category, $field, Input::post( $fieldname ) );
|
||||
}
|
||||
} elseif ( 'radio' == $node['type'] ) {
|
||||
$this->update( $category, $field, false );
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( $save ) {
|
||||
return $this->save( $saveDefault );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the current config.
|
||||
*
|
||||
* @param {bool} [$default] - Whether or not to save a default copy.
|
||||
* @return {bool}
|
||||
*/
|
||||
public function save( $default = false ) {
|
||||
if ( self::$config === false ) {
|
||||
Debug::warn( 'Config not loaded.' );
|
||||
return false;
|
||||
}
|
||||
if ( self::$location === false ) {
|
||||
Debug::warn( 'Config location not set.' );
|
||||
return false;
|
||||
}
|
||||
if ( $default ) {
|
||||
$locationArray = explode( '.', self::$location );
|
||||
$locationArray[] = 'bak';
|
||||
$backupLoction = implode( '.', $locationArray );
|
||||
if ( !file_put_contents( $backupLoction, json_encode( self::$config ) ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if ( file_put_contents( self::$location, json_encode( self::$config ) ) ) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new category to the $config array.
|
||||
*
|
||||
* @param {string} [$categoryName]
|
||||
* @return {bool}
|
||||
*/
|
||||
public function addCategory( $categoryName ) {
|
||||
if ( self::$config === false ) {
|
||||
self::$config = [];
|
||||
}
|
||||
if ( !Check::simpleName( $categoryName ) ) {
|
||||
Debug::warn( "Category name invalid: $categoryName" );
|
||||
return false;
|
||||
}
|
||||
if ( isset( self::$config[$categoryName] ) ) {
|
||||
Debug::warn( "Category already exists: $categoryName" );
|
||||
return false;
|
||||
}
|
||||
self::$config[$categoryName] = [];
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an existing category from the $config array.
|
||||
*
|
||||
* @param {string} [$categoryName]
|
||||
* @param {string} [$save]
|
||||
* @return {bool}
|
||||
*/
|
||||
public function removeCategory( $categoryName, $save = false, $saveDefault = true ) {
|
||||
if ( self::$config === false ) {
|
||||
Debug::warn( 'Config not loaded.' );
|
||||
return;
|
||||
}
|
||||
if ( !isset( self::$config[$categoryName] ) ) {
|
||||
Debug::warn( "Config does not have category: $categoryName" );
|
||||
return true;
|
||||
}
|
||||
unset( self::$config[$categoryName] );
|
||||
if ( $save ) {
|
||||
$this->save( $saveDefault );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new config node for the specified category.
|
||||
*
|
||||
* @param {string} [$category] - The primary category to add the option to.
|
||||
* @param {string} [$node] - The name of the new option.
|
||||
* @param {wild} [$value] - The desired value for the new option.
|
||||
* @return {bool}
|
||||
*/
|
||||
public function add( $category, $node, $details, $updateExisting = false ) {
|
||||
if ( self::$config === false ) {
|
||||
Debug::warn( 'Config not loaded.' );
|
||||
return false;
|
||||
}
|
||||
if ( !Check::simpleName( $category ) ) {
|
||||
Debug::warn( "Category name invalid: $category" );
|
||||
return false;
|
||||
}
|
||||
if ( !isset( self::$config[$category] ) ) {
|
||||
Debug::warn( "No such category: $category" );
|
||||
return false;
|
||||
}
|
||||
if ( !Check::simpleName( $node ) ) {
|
||||
Debug::warn( "Category Node name invalid: $node" );
|
||||
return false;
|
||||
}
|
||||
if ( isset( self::$config[$category][$node] ) ) {
|
||||
if ( $updateExisting ) {
|
||||
$details = array_replace(self::$config[$category][$node], $details );
|
||||
} else {
|
||||
Debug::warn( "Config already exists: $node" );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if ( !isset( $details['value'] ) ) {
|
||||
$details['value'] = $details['default'];
|
||||
}
|
||||
self::$config[$category][$node] = $details;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function generate( $location, $mods = [] ) {
|
||||
self::$location = $location;
|
||||
if ( !empty( $mods ) ) {
|
||||
foreach ( $mods as $category => $node ) {
|
||||
$this->addCategory( $category );
|
||||
foreach ( $node as $name => $details ) {
|
||||
$this->add( $category, $name, $details, true );
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( $this->save( true ) ) {
|
||||
Debug::info( 'config file generated successfully.' );
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the config option for $name.
|
||||
*
|
||||
* @param {string} [$name] - Must be in <category>/<option> format.
|
||||
* @return {WILD}
|
||||
*/
|
||||
public static function get( $name ) {
|
||||
$data = explode( '/', $name );
|
||||
if ( count( $data ) != 2 ) {
|
||||
Debug::warn( "Config not properly formatted: $name" );
|
||||
return;
|
||||
}
|
||||
if ( self::$config === false ) {
|
||||
Debug::warn( 'Config not loaded.' );
|
||||
return;
|
||||
}
|
||||
$category = $data[0];
|
||||
$field = $data[1];
|
||||
if ( !isset( self::$config[$category][$field] ) ) {
|
||||
Debug::warn( "Config not found: $name" );
|
||||
return;
|
||||
}
|
||||
$node = self::$config[$category][$field];
|
||||
if ( !isset( $node['type'] ) ) {
|
||||
$node['type'] = 'text';
|
||||
}
|
||||
if ( !isset( $node['pretty'] ) ) {
|
||||
$node['pretty'] = $node;
|
||||
}
|
||||
if ( !isset( $node['default'] ) ) {
|
||||
$node['default'] = '';
|
||||
}
|
||||
if ( !isset( $node['value'] ) ) {
|
||||
$node['value'] = $node['default'];
|
||||
}
|
||||
if ( !isset( $node['protected'] ) ) {
|
||||
$node['protected'] = false;
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the config option for $name and if the result is bool, converts it to a string.
|
||||
*
|
||||
* @param {string} [$name] - Must be in <category>/<option> format.
|
||||
* @return {WILD}
|
||||
*/
|
||||
public static function getString( $name ) {
|
||||
$result = self::getValue( $name );
|
||||
if ( is_bool( $result ) ) {
|
||||
$result = ( $result ? 'true' : 'false' );
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function getValue( $name ) {
|
||||
$node = self::get( $name );
|
||||
if ( empty( $node ) ) {
|
||||
return;
|
||||
}
|
||||
if ( !isset( $node['value'] ) ) {
|
||||
Debug::warn( 'Node Value not set.' );
|
||||
return;
|
||||
}
|
||||
return $node['value'];
|
||||
}
|
||||
|
||||
public static function getDefault( $name ) {
|
||||
$node = self::get( $name );
|
||||
if ( empty( $node ) ) {
|
||||
return;
|
||||
}
|
||||
if ( !isset( $node['default'] ) ) {
|
||||
Debug::warn( 'Node default not set.' );
|
||||
return;
|
||||
}
|
||||
return $node['default'];
|
||||
}
|
||||
}
|
56
renamed/classes/controller.php
Normal file
56
renamed/classes/controller.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* classes/controller.php
|
||||
*
|
||||
* The controller handles our main template and provides the
|
||||
* model and view functions which are the backbone of the tempus
|
||||
* project. Used to hold and keep track of many of the variables
|
||||
* that support the applications execution.
|
||||
*
|
||||
* @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\Classes;
|
||||
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
use TheTempusProject\Bedrock\Functions\Session;
|
||||
use TheTempusProject\Houdini\Classes\Issues;
|
||||
|
||||
class Controller {
|
||||
public static $title = null;
|
||||
public static $pageDescription = null;
|
||||
public static $template = null;
|
||||
|
||||
/**
|
||||
* Check for issues stored in sessions and add them to current issues.
|
||||
*/
|
||||
public static function checkSessions() {
|
||||
$success = Session::checkFlash( 'success' );
|
||||
$notice = Session::checkFlash( 'notice' );
|
||||
$error = Session::checkFlash( 'error' );
|
||||
$info = Session::checkFlash( 'info' );
|
||||
if ( !empty( $success ) ) {
|
||||
Issues::add( 'success', $success );
|
||||
}
|
||||
if ( !empty( $notice ) ) {
|
||||
Issues::add( 'notice', $notice );
|
||||
}
|
||||
if ( !empty( $error ) ) {
|
||||
Issues::add( 'error', $error );
|
||||
}
|
||||
if ( !empty( $info ) ) {
|
||||
Issues::add( 'info', $info );
|
||||
}
|
||||
}
|
||||
|
||||
public function __construct() {
|
||||
Debug::log( 'Controller Constructing: ' . get_class( $this ) );
|
||||
self::checkSessions();
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
Debug::log( 'Controller Destructing: ' . get_class( $this ) );
|
||||
}
|
||||
}
|
764
renamed/classes/database.php
Normal file
764
renamed/classes/database.php
Normal file
@ -0,0 +1,764 @@
|
||||
<?php
|
||||
/**
|
||||
* classes/database.php
|
||||
*
|
||||
* Defines all interactions with the database.
|
||||
*
|
||||
* @todo - Add more than just MySQL
|
||||
*
|
||||
* @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\Classes;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
use TheTempusProject\Canary\Classes\CustomException;
|
||||
|
||||
class Database {
|
||||
public static $instance = null;
|
||||
private $pdo = null;
|
||||
private $query = null;
|
||||
private $error = false;
|
||||
private $results = null;
|
||||
private $count = 0;
|
||||
private $maxQuery = 0;
|
||||
private $totalResults = 0;
|
||||
private $errorMessage = null;
|
||||
private $tableBuff = null;
|
||||
private $fieldBuff = null;
|
||||
private $queryStatus = false;
|
||||
|
||||
/**
|
||||
* Checks the DB connection with the provided information.
|
||||
*
|
||||
* @param string $host - Database Host.
|
||||
* @param string $db - Database Name.
|
||||
* @param string $user - Database Username.
|
||||
* @param string $pass - Database Password.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function check( $host = null, $db = null, $user = null, $pass = null ) {
|
||||
if ( empty( $host ) || empty( $db ) || empty( $user ) || empty( $pass ) ) {
|
||||
Debug::error( 'check::db: one or more parameters are missing.' );
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
Debug::log( 'Attempting to connect to DB with supplied credentials.' );
|
||||
$test = new PDO( 'mysql:host=' . $host . ';dbname=' . $db, $user, $pass );
|
||||
} catch ( PDOException $Exception ) {
|
||||
Debug::error( 'Cannot connect to DB with provided credentials: ' . $Exception->getMessage() );
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the current database in the configuration file for version verification.
|
||||
*
|
||||
* @return boolean
|
||||
*
|
||||
* @todo - Update this function to be more effective.
|
||||
*/
|
||||
public static function mysql() {
|
||||
self::connect();
|
||||
$dbVersion = self::$db->version();
|
||||
preg_match( '@[0-9]+\.[0-9]+\.[0-9]+@', $dbVersion, $version );
|
||||
if ( version_compare( $version[0], '10.0.0', '>' ) ) {
|
||||
return true;
|
||||
}
|
||||
self::addError( "MySQL Version is too low! Current version is $version[0]. Version 10.0.0 or higher is required." );
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Automatically open the DB connection with settings from our global config.
|
||||
*/
|
||||
private function __construct( $host = null, $name = null, $user = null, $pass = null ) {
|
||||
Debug::debug( 'Class initialized: ' . get_class( $this ) );
|
||||
if ( isset( $host ) && isset( $name ) && isset( $user ) && isset( $pass ) ) {
|
||||
try {
|
||||
Debug::log( 'Attempting to connect to DB with supplied credentials.' );
|
||||
$this->pdo = new PDO( 'mysql:host=' . $host . ';dbname=' . $name, $user, $pass );
|
||||
} catch ( PDOException $Exception ) {
|
||||
$this->error = true;
|
||||
$this->errorMessage = $Exception->getMessage();
|
||||
}
|
||||
}
|
||||
if ( !$this->enabled() ) {
|
||||
$this->error = true;
|
||||
$this->errorMessage = 'Database disabled in config.';
|
||||
}
|
||||
if ( $this->error === false ) {
|
||||
try {
|
||||
Debug::debug( 'Attempting to connect to DB with config credentials.' );
|
||||
$this->pdo = new PDO( 'mysql:host=' . Config::getValue( 'database/dbHost' ) . ';dbname=' . Config::getValue( 'database/dbName' ), Config::getValue( 'database/dbUsername' ), Config::getValue( 'database/dbPassword' ) );
|
||||
} catch ( PDOException $Exception ) {
|
||||
$this->error = true;
|
||||
$this->errorMessage = $Exception->getMessage();
|
||||
}
|
||||
}
|
||||
if ( $this->error !== false ) {
|
||||
new CustomException( 'dbConnection', $this->errorMessage );
|
||||
return;
|
||||
}
|
||||
$this->maxQuery = Config::getValue( 'database/dbMaxQuery' );
|
||||
// @TODO add a toggle for this
|
||||
$this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
|
||||
Debug::debug( 'DB connection successful' );
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the DB is enabled via the config file.
|
||||
*
|
||||
* @return bool - whether the db module is enabled or not.
|
||||
*/
|
||||
public function enabled( $type = '' ) {
|
||||
if ( Config::getValue( 'database/dbEnabled' ) === true ) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function lastId() {
|
||||
return $this->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if there is already a DB instance open, and if not; create one.
|
||||
*
|
||||
* @return function - Returns the PDO DB connection.
|
||||
*/
|
||||
public static function getInstance( $host = null, $name = null, $user = null, $pass = null, $new = false ) {
|
||||
// used to force a new connection
|
||||
if ( !empty( $host ) && !empty( $name ) && !empty( $user ) && !empty( $pass ) ) {
|
||||
self::$instance = new self( $host, $name, $user, $pass );
|
||||
}
|
||||
if ( empty( self::$instance ) || $new ) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the DB version.
|
||||
*
|
||||
* @return bool|string
|
||||
*/
|
||||
public function version() {
|
||||
if ( !$this->enabled() ) {
|
||||
$this->error = true;
|
||||
$this->errorMessage = 'Database disabled';
|
||||
return false;
|
||||
}
|
||||
$sql = 'select version()';
|
||||
if ( $this->query = $this->pdo->prepare( $sql ) ) {
|
||||
try {
|
||||
$this->query->execute();
|
||||
} catch ( PDOException $Exception ) {
|
||||
$this->error = true;
|
||||
$this->errorMessage = $Exception->getMessage();
|
||||
Debug::error( 'DB Version Error' );
|
||||
Debug::error( $this->errorMessage );
|
||||
return false;
|
||||
}
|
||||
return $this->query->fetchColumn();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the database to see if the specified table exists.
|
||||
*
|
||||
* @param string $name - The name of the table to check for.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
protected function tableExists( $name ) {
|
||||
$name = Config::getValue( 'database/dbPrefix' ) . $name;
|
||||
$this->raw( "SHOW TABLES LIKE '$name'" );
|
||||
if ( $this->error ) {
|
||||
Debug::error( var_export( $this->errorMessage, true ) );
|
||||
return false;
|
||||
}
|
||||
if ( $this->count === 0 ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks first that the table exists, then checks if the specified
|
||||
* column exists in the table.
|
||||
*
|
||||
* @param string $table - The table to search.
|
||||
* @param string $column - The column to look for.
|
||||
*
|
||||
* @return boolean
|
||||
*
|
||||
* @todo - Is it necessary to check the current $fields list too?
|
||||
*/
|
||||
protected function columnExists( $table, $column ) {
|
||||
if ( !$this->tableExists( $table ) ) {
|
||||
return false;
|
||||
}
|
||||
$table = Config::getValue( 'database/dbPrefix' ) . $table;
|
||||
$this->raw( "SHOW COLUMNS FROM `$table` LIKE '$column'" );
|
||||
if ( !$this->error && $this->count === 0 ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a raw DB query.
|
||||
*
|
||||
* @param string $data the query to execute
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function raw( $data ) {
|
||||
$this->queryReset();
|
||||
if ( !$this->enabled() ) {
|
||||
$this->error = true;
|
||||
$this->errorMessage = 'Database disabled';
|
||||
return false;
|
||||
}
|
||||
$this->query = $this->pdo->prepare( $data );
|
||||
try {
|
||||
$this->query->execute();
|
||||
} catch ( PDOException $Exception ) {
|
||||
$this->error = true;
|
||||
$this->errorMessage = $Exception->getMessage();
|
||||
Debug::warn( 'DB Raw Query Error' );
|
||||
Debug::warn( $this->errorMessage );
|
||||
return false;
|
||||
}
|
||||
// @todo i think this will cause an error some circumstances
|
||||
$this->count = $this->query->rowCount();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The actual Query function. This function takes our setup queries
|
||||
* and send them to the database. it then properly sets our instance
|
||||
* variables with the proper info from the DB, as secondary constructor
|
||||
* for almost all objects in this class.
|
||||
*
|
||||
* @param string $sql - The SQL to execute.
|
||||
* @param array $params - Any bound parameters for the query.
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public function query( $sql, $params = [], $noFetch = false ) {
|
||||
$this->queryReset();
|
||||
if ( $this->pdo == false ) {
|
||||
Debug::warn( 'DB::query - no database connection established' );
|
||||
$this->error = true;
|
||||
$this->errorMessage = 'DB::query - no database connection established';
|
||||
return $this;
|
||||
}
|
||||
$this->query = $this->pdo->prepare( $sql );
|
||||
if ( !empty( $params ) ) {
|
||||
$x = 0;
|
||||
foreach ( $params as $param ) {
|
||||
$x++;
|
||||
if ( is_array( $param ) ) {
|
||||
// dv( $param );
|
||||
}
|
||||
$this->query->bindValue( $x, $param );
|
||||
}
|
||||
}
|
||||
try {
|
||||
$this->query->execute();
|
||||
} catch ( PDOException $Exception ) {
|
||||
$this->error = true;
|
||||
$this->errorMessage = $Exception->getMessage();
|
||||
Debug::error( 'DB Query Error' );
|
||||
Debug::error( $this->errorMessage );
|
||||
return $this;
|
||||
}
|
||||
if ( $noFetch === true ) {
|
||||
$this->results = null;
|
||||
$this->count = 1;
|
||||
} else {
|
||||
$this->results = $this->query->fetchAll( PDO::FETCH_OBJ );
|
||||
$this->count = $this->query->rowCount();
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function resets the values used for creating or modifying tables.
|
||||
* Essentially a cleaner function.
|
||||
*/
|
||||
public function queryReset( $includeBuffers = false ) {
|
||||
$this->results = null;
|
||||
$this->count = 0;
|
||||
$this->error = false;
|
||||
$this->errorMessage = null;
|
||||
$this->query = null;
|
||||
if ( $includeBuffers ) {
|
||||
$this->tableBuff = null;
|
||||
$this->fieldBuff = null;
|
||||
$this->queryStatus = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The action function builds all of our SQL.
|
||||
*
|
||||
* @todo : Clean this up.
|
||||
*
|
||||
* @param string $action - The type of action being carried out.
|
||||
* @param string $tableName - The table name being used.
|
||||
* @param array $where - The parameters for the action
|
||||
* @param string $by - The key to sort by.
|
||||
* @param string $direction - The direction to sort the results.
|
||||
* @param array $limit - The result limit of the query.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function action( $action, $tableName, $where, $by = null, $direction = 'DESC', $reqLimit = null ) {
|
||||
$this->totalResults = 0; // since this is how we paginate, it can't be reset when we exceed the max
|
||||
if ( !$this->enabled() ) {
|
||||
$this->error = true;
|
||||
$this->errorMessage = 'Database disabled';
|
||||
return $this;
|
||||
}
|
||||
$whereCount = count( $where );
|
||||
if ( $whereCount < 3 ) {
|
||||
Debug::error( 'DB::action - Not enough arguments supplied for "where" clause' );
|
||||
$this->error = true;
|
||||
$this->errorMessage = 'DB::action - Not enough arguments supplied for "where" clause';
|
||||
return $this;
|
||||
}
|
||||
if ( $action == 'DELETE' ) {
|
||||
$noFetch = true;
|
||||
}
|
||||
$tableName = Config::getValue( 'database/dbPrefix' ) . $tableName;
|
||||
$sql = "{$action} FROM `{$tableName}` WHERE ";
|
||||
$validOperators = ['=', '!=', '>', '<', '>=', '<=', 'LIKE', 'IS'];
|
||||
$validDelimiters = ['AND', 'OR'];
|
||||
$values = [];
|
||||
while ( $whereCount > 2 ) {
|
||||
$whereCount = $whereCount - 3;
|
||||
$field = array_shift( $where );
|
||||
$operator = array_shift( $where );
|
||||
array_push( $values, array_shift( $where ) );
|
||||
if ( !in_array( $operator, $validOperators ) ) {
|
||||
Debug::error( 'DB::action - Invalid operator.' );
|
||||
$this->error = true;
|
||||
$this->errorMessage = 'DB::action - Invalid operator.';
|
||||
return $this;
|
||||
}
|
||||
$sql .= "{$field} {$operator} ?";
|
||||
if ( $whereCount > 0 ) {
|
||||
$delimiter = array_shift( $where );
|
||||
if ( !in_array( $delimiter, $validDelimiters ) ) {
|
||||
Debug::error( 'DB::action - Invalid delimiter.' );
|
||||
$this->error = true;
|
||||
$this->errorMessage = 'DB::action - Invalid delimiter.';
|
||||
return $this;
|
||||
}
|
||||
$sql .= " {$delimiter} ";
|
||||
$whereCount--;
|
||||
}
|
||||
}
|
||||
if ( isset( $by ) ) {
|
||||
$sql .= " ORDER BY {$by} {$direction}";
|
||||
}
|
||||
$sqlPreLimit = $sql;
|
||||
if ( !empty( $reqLimit ) ) {
|
||||
$lim = implode(',',$reqLimit);
|
||||
$sql .= " LIMIT {$lim}";
|
||||
}
|
||||
if ( isset( $values ) ) {
|
||||
if ( !empty( $noFetch ) ) {
|
||||
$error = $this->query( $sql, $values, true )->error();
|
||||
} else {
|
||||
$error = $this->query( $sql, $values )->error();
|
||||
}
|
||||
} else {
|
||||
$error = $this->query( $sql )->error();
|
||||
}
|
||||
if ( $error ) {
|
||||
Debug::warn( 'DB Action Error: ' );
|
||||
Debug::warn( $this->errorMessage );
|
||||
return $this;
|
||||
}
|
||||
$this->totalResults = $this->count;
|
||||
if ( $this->count <= $this->maxQuery ) {
|
||||
return $this;
|
||||
}
|
||||
Debug::warn( 'Query exceeded maximum results. Maximum allowed is ' . $this->maxQuery );
|
||||
if ( !empty( $limit ) ) {
|
||||
$newLimit = ( $reqLimit[0] + Pagination::perPage() );
|
||||
$limit = " LIMIT {$reqLimit[0]},{$newLimit}";
|
||||
} else {
|
||||
$limit = ' LIMIT 0,' .Pagination::perPage();
|
||||
}
|
||||
$sql = $sqlPreLimit . $limit;
|
||||
if ( isset( $values ) ) {
|
||||
$error = $this->query( $sql, $values )->error();
|
||||
} else {
|
||||
$error = $this->query( $sql )->error();
|
||||
}
|
||||
if ( $error ) {
|
||||
Debug::warn( 'DB Action Error: ' );
|
||||
Debug::warn( $this->errorMessage );
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to insert into the DB.
|
||||
*
|
||||
* @param string $table - The table you wish to insert into.
|
||||
* @param array $fields - The array of fields you wish to insert.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function insert( $table, $fields = [] ) {
|
||||
$keys = array_keys( $fields );
|
||||
$valuesSQL = null;
|
||||
$x = 0;
|
||||
$keysSQL = implode( '`, `', $keys );
|
||||
foreach ( $fields as $value ) {
|
||||
$x++;
|
||||
$valuesSQL .= '?';
|
||||
if ( $x < count( $fields ) ) {
|
||||
$valuesSQL .= ', ';
|
||||
}
|
||||
}
|
||||
$table = Config::getValue( 'database/dbPrefix' ) . $table;
|
||||
$sql = "INSERT INTO `{$table}` (`" . $keysSQL . "`) VALUES ({$valuesSQL})";
|
||||
if ( !$this->query( $sql, $fields, true )->error() ) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to duplicate a database entry.
|
||||
*
|
||||
* @param string $table - The table you wish to duplicate the entry in.
|
||||
* @param int $id - The ID of the entry you wish to duplicate.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function duplicateEntry($table, $id) {
|
||||
// Get the original entry
|
||||
$originalEntry = $this->action('SELECT', $table, ['ID', '=', $id])->results();
|
||||
|
||||
// Exclude the ID field
|
||||
unset($originalEntry->ID);
|
||||
|
||||
// Insert the duplicated entry
|
||||
if ($this->insert($table, (array) $originalEntry)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to update the database.
|
||||
*
|
||||
* @param string $table - The table you wish to update in.
|
||||
* @param int $id - The ID of the entry you wish to update.
|
||||
* @param array $fields - the various fields you wish to update
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function update( $table, $id, $fields = [] ) {
|
||||
$updateSQL = null;
|
||||
$x = 0;
|
||||
foreach ( $fields as $name => $value ) {
|
||||
$x++;
|
||||
$updateSQL .= "{$name} = ?";
|
||||
if ( $x < count( $fields ) ) {
|
||||
$updateSQL .= ', ';
|
||||
}
|
||||
}
|
||||
$table = Config::getValue( 'database/dbPrefix' ) . $table;
|
||||
$sql = "UPDATE {$table} SET {$updateSQL} WHERE ID = {$id}";
|
||||
if ( !$this->query( $sql, $fields, true )->error() ) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a series of, or a single instance(s) in the database.
|
||||
*
|
||||
* @param string $table - The table you are deleting from.
|
||||
* @param string $where - The criteria for deletion.
|
||||
*
|
||||
* @return function
|
||||
*/
|
||||
public function delete( $table, $where ) {
|
||||
return $this->action( 'DELETE', $table, $where );
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the object to create a new table if none already exists.
|
||||
*
|
||||
* NOTE: All tables created with this function will automatically
|
||||
* have an 11 digit integer called ID added as a primary key.
|
||||
*
|
||||
* @param string $name - The name of the table you wish to create.
|
||||
*
|
||||
* @return boolean
|
||||
*
|
||||
* @todo - add a check for the name.
|
||||
*/
|
||||
public function newTable( $name, $addID = true ) {
|
||||
if ( $this->tableExists( $name ) ) {
|
||||
$this->tableBuff = null;
|
||||
Debug::error( "Table already exists: $name" );
|
||||
|
||||
return false;
|
||||
}
|
||||
$this->queryReset( true );
|
||||
$this->tableBuff = $name;
|
||||
if ( $addID === true ) {
|
||||
$this->addField( 'ID', 'int', 11, false );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds and executes a database query to to create a table
|
||||
* using the current object's table name and fields.
|
||||
*
|
||||
* NOTE: By default: All tables have an auto incrementing primary key named 'ID'.
|
||||
*
|
||||
* @todo - Come back and add more versatility here.
|
||||
*/
|
||||
public function createTable() {
|
||||
if ( empty( $this->tableBuff ) ) {
|
||||
Debug::info( 'No Table set.' );
|
||||
|
||||
return false;
|
||||
}
|
||||
$table = Config::getValue( 'database/dbPrefix' ) . $this->tableBuff;
|
||||
if ( $this->tableExists( $this->tableBuff ) ) {
|
||||
Debug::error( "Table already exists: $table" );
|
||||
|
||||
return false;
|
||||
}
|
||||
$queryBuff = "CREATE TABLE `$table` (";
|
||||
$x = 0;
|
||||
$y = count( $this->fieldBuff );
|
||||
while ( $x < $y ) {
|
||||
$queryBuff .= $this->fieldBuff[$x];
|
||||
$x++;
|
||||
$queryBuff .= ( $x < $y ) ? ',' : '';
|
||||
}
|
||||
$queryBuff .= ') ENGINE=InnoDB DEFAULT CHARSET=latin1; ALTER TABLE `' . $table . '` ADD PRIMARY KEY (`ID`); ';
|
||||
$queryBuff .= 'ALTER TABLE `' . $table . "` MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary index value';";
|
||||
$this->queryStatus = ( $this->raw( $queryBuff ) ? true : false );
|
||||
return $this->queryStatus;
|
||||
}
|
||||
|
||||
public function removeTable( $name ) {
|
||||
if ( !$this->tableExists( $name ) ) {
|
||||
Debug::error( "No table exists: $name" );
|
||||
|
||||
return false;
|
||||
}
|
||||
$table = Config::getValue( 'database/dbPrefix' ) . $name;
|
||||
$this->queryStatus = ( $this->raw( 'DROP TABLE `' . $table . '`' ) ? true : false );
|
||||
return $this->queryStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function allows you to add a new field to be
|
||||
* added to a previously specified table.
|
||||
*
|
||||
* @param string $name - The name of the field to add
|
||||
* @param string $type - The type of field.
|
||||
* @param integer $length - The maximum length value for the field.
|
||||
* @param boolean $null - Whether or not the field can be null
|
||||
* @param string $default - The default value to use for new entries if any.
|
||||
* @param string $comment - DB comment for this field.
|
||||
*
|
||||
* @return boolean
|
||||
*
|
||||
* @todo - add more error reporting and checks
|
||||
* use switch/cases?
|
||||
*/
|
||||
public function addField( $name, $type, $length, $null = true, $default = null, $comment = '' ) {
|
||||
if ( empty( $this->tableBuff ) ) {
|
||||
Debug::info( 'No Table set.' );
|
||||
return false;
|
||||
}
|
||||
if ( $this->columnExists( $this->tableBuff, $name ) ) {
|
||||
Debug::error( "Column already exists: $this->tableBuff > $name" );
|
||||
return false;
|
||||
}
|
||||
if ( $null === true ) {
|
||||
$sDefault = ' DEFAULT NULL';
|
||||
} else {
|
||||
$sDefault = ' NOT NULL';
|
||||
if ( !empty( $default ) ) {
|
||||
$sDefault .= " DEFAULT '$default'";
|
||||
}
|
||||
}
|
||||
if ( !empty( $length ) ) {
|
||||
if ( is_int( $length ) ) {
|
||||
$sType = $type . '(' . $length . ')';
|
||||
} elseif ( is_string( $length ) && ctype_digit( $length ) ) {
|
||||
$sType = $type . '(' . $length . ')';
|
||||
} else {
|
||||
$sType = $type;
|
||||
}
|
||||
} else {
|
||||
$sType = $type;
|
||||
}
|
||||
if ( !empty( $comment ) ) {
|
||||
$sComment = " COMMENT '$comment'";
|
||||
} else {
|
||||
$sComment = '';
|
||||
}
|
||||
$this->fieldBuff[] = ' `' . $name . '` ' . $sType . $sDefault . $sComment;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function searchColumn( $table, $column, $param ) {
|
||||
return $this->action( 'SELECT *', $table, [$column, 'LIKE', '%' . $param . '%'] );
|
||||
}
|
||||
|
||||
public function search( $table, $columns, $param ) {
|
||||
if ( empty( $columns ) || ! is_array( $columns ) ) {
|
||||
Debug::log( 'No columns provided for search' );
|
||||
return [];
|
||||
}
|
||||
|
||||
$conditions = [];
|
||||
foreach ( $columns as $column ) {
|
||||
$conditions[] = $column;
|
||||
$conditions[] = 'LIKE';
|
||||
$conditions[] = '%' . $param . '%';
|
||||
$conditions[] = 'OR';
|
||||
}
|
||||
array_pop( $conditions );
|
||||
|
||||
return $this->action( 'SELECT *', $table, $conditions );
|
||||
// return $this->action( 'SELECT ' . implode( ',', $columns ), $table, $conditions ); // need to find a way to casually make this the default....
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects data from the database.
|
||||
*
|
||||
* @param string $table - The table we wish to select from.
|
||||
* @param string $where - The criteria we wish to select.
|
||||
* @param string $by - The key we wish to order by.
|
||||
* @param string $direction - The direction we wish to order the results.
|
||||
*
|
||||
* @return function
|
||||
*/
|
||||
public function get( $table, $where, $by = 'ID', $direction = 'DESC', $limit = null ) {
|
||||
if ( $where === '*' ) {
|
||||
$where = ['ID', '>=', '0'];
|
||||
}
|
||||
return $this->action( 'SELECT *', $table, $where, $by, $direction, $limit );
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects data from the database and automatically builds the pagination filter for the results array.
|
||||
*
|
||||
* @param string $table - The table we wish to select from.
|
||||
* @param string $where - The criteria we wish to select.
|
||||
* @param string $by - The key we wish to order by.
|
||||
* @param string $direction - The direction we wish to order the results.
|
||||
*
|
||||
* @return function
|
||||
*/
|
||||
public function getPaginated( $table, $where, $by = 'ID', $direction = 'DESC', $limit = null ) {
|
||||
if ( $where === '*' ) {
|
||||
$where = ['ID', '>=', '0'];
|
||||
}
|
||||
$db = $this->action( 'SELECT ID', $table, $where, $by, $direction );
|
||||
|
||||
Pagination::updatePaginationTotal( $this->totalResults );
|
||||
|
||||
if ( ! is_array( $limit ) ) {
|
||||
$limit = [ Pagination::getMin(), Pagination::getMax() ];
|
||||
}
|
||||
|
||||
Pagination::paginate();
|
||||
|
||||
return $this->action( 'SELECT *', $table, $where, $by, $direction, $limit );
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for returning the entire $results array.
|
||||
*
|
||||
* @return array - Returns the current query's results.
|
||||
*/
|
||||
public function results() {
|
||||
return $this->results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for returning the first result in the results array.
|
||||
*
|
||||
* @return array - Returns the current first member of the results array.
|
||||
*/
|
||||
public function first() {
|
||||
if ( !empty( $this->results[0] ) ) {
|
||||
return $this->results[0];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for returning current results' row count.
|
||||
*
|
||||
* @return int - Returns the current instance's SQL result count.
|
||||
*/
|
||||
public function count() {
|
||||
return $this->count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if there are errors with the current query or not.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function error() {
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if there are errors with the current query or not.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function errorMessage() {
|
||||
//$this->query->errorInfo();
|
||||
return $this->errorMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the boolean status of the most recently executed query.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getStatus() {
|
||||
return $this->queryStatus;
|
||||
}
|
||||
}
|
209
renamed/classes/databaseModel.php
Normal file
209
renamed/classes/databaseModel.php
Normal file
@ -0,0 +1,209 @@
|
||||
<?php
|
||||
/**
|
||||
* classes/DatabaseModel.php
|
||||
*
|
||||
* The class provides some basic functionality for models that interact
|
||||
* with the database.
|
||||
*
|
||||
* @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\Classes;
|
||||
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
use TheTempusProject\Bedrock\Functions\Check;
|
||||
use TheTempusProject\Bedrock\Bedrock;
|
||||
|
||||
class DatabaseModel extends Model {
|
||||
public $tableName;
|
||||
public $databaseMatrix;
|
||||
public $searchFields;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function enabled() {
|
||||
if ( empty( $this->enabled ) ) {
|
||||
$this->enabled = self::$db->enabled();
|
||||
}
|
||||
return $this->enabled;
|
||||
}
|
||||
|
||||
public function filter( $data, $params = [] ) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will remove all the installed model components.
|
||||
*
|
||||
* @return bool - If the uninstall was completed without error
|
||||
*/
|
||||
public function uninstall() {
|
||||
parent::uninstall();
|
||||
$this->uninstallTable();
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rowMatrixToArray( $row_matrix ) {
|
||||
$row_array = [];
|
||||
$row_array['row_name'] = array_shift( $row_matrix );
|
||||
$row_array['row_type'] = array_shift( $row_matrix );
|
||||
$row_array['length'] = array_shift( $row_matrix );
|
||||
if ( !empty( $row_matrix ) ) {
|
||||
$row_array['is_null'] = array_shift( $row_matrix );
|
||||
} else {
|
||||
$row_array['is_null'] = true;
|
||||
}
|
||||
if ( !empty( $row_matrix ) ) {
|
||||
$row_array['default_value'] = array_shift( $row_matrix );
|
||||
} else {
|
||||
$row_array['default_value'] = null;
|
||||
}
|
||||
if ( !empty( $row_matrix ) ) {
|
||||
$row_array['comment'] = array_shift( $row_matrix );
|
||||
} else {
|
||||
$row_array['comment'] = '';
|
||||
}
|
||||
return $row_array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Install db tables needed for the model.
|
||||
*
|
||||
* @return {bool}
|
||||
*/
|
||||
public function installTable() {
|
||||
// should have some sort of DELTA functionality and safeguards
|
||||
if ( empty( $this->tableName ) || empty( $this->databaseMatrix )) {
|
||||
Debug::log( 'databaseMatrix is empty' );
|
||||
return true;
|
||||
}
|
||||
if ( false === self::$db->newTable( $this->tableName ) ) {
|
||||
return false;
|
||||
}
|
||||
Debug::log( 'adding a new table named: ' . $this->tableName );
|
||||
foreach ( $this->databaseMatrix as $key => $row_matrix ) {
|
||||
$row = $this->rowMatrixToArray( $row_matrix );
|
||||
self::$db->addField(
|
||||
$row['row_name'],
|
||||
$row['row_type'],
|
||||
$row['length'],
|
||||
$row['is_null'],
|
||||
$row['default_value'],
|
||||
$row['comment'],
|
||||
);
|
||||
}
|
||||
self::$db->createTable();
|
||||
return self::$db->getStatus();
|
||||
}
|
||||
|
||||
public function uninstallTable() {
|
||||
if ( empty( $this->tableName ) ) {
|
||||
return;
|
||||
}
|
||||
return self::$db->removeTable( $this->tableName );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a row by its ID and filters it.
|
||||
*
|
||||
* @param {int} [$id]
|
||||
* @return {object} - The filtered db entry.
|
||||
*/
|
||||
public function findById( $id ) {
|
||||
if ( !Check::id( $id ) ) {
|
||||
Debug::warn( "$this->tableName fingByID: illegal ID: $id" );
|
||||
return false;
|
||||
}
|
||||
$data = self::$db->get( $this->tableName, ['ID', '=', $id] );
|
||||
if ( !$data->count() ) {
|
||||
Debug::info( 'No ' . $this->tableName . ' data found.' );
|
||||
return false;
|
||||
}
|
||||
return $this->filter( $data->first() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to delete the specified entry.
|
||||
*
|
||||
* @param int|array $ID the log ID or array of ID's to be deleted
|
||||
* @return bool
|
||||
*/
|
||||
public function delete( $idArray ) {
|
||||
if ( !is_array( $idArray ) ) {
|
||||
$idArray = [ $idArray ];
|
||||
}
|
||||
foreach ( $idArray as $id ) {
|
||||
if ( !Check::id( $id ) ) {
|
||||
Debug::info( "invalid ID: $id." );
|
||||
$error = true;
|
||||
continue;
|
||||
}
|
||||
if ( self::$db->delete( $this->tableName, [ 'ID', '=', $id ] )->error() ) {
|
||||
Debug::info( "Error Deleting id: $id" );
|
||||
$error = true;
|
||||
continue;
|
||||
}
|
||||
Debug::info( $this->tableName . " deleted: $id" );
|
||||
}
|
||||
if ( !empty( $error ) ) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to clear entries of a defined type.
|
||||
*
|
||||
* @todo this is probably dumb
|
||||
* @param string $data - The log type to be cleared
|
||||
* @return bool
|
||||
*/
|
||||
public function empty() {
|
||||
self::$db->delete( $this->tableName, ['ID', '>=', '0'] );
|
||||
Debug::info( $this->tableName . ' Cleared' );
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieves a list of paginated (limited) results.
|
||||
*
|
||||
* @param array $filter - A filter to be applied to the list.
|
||||
* @return bool|object - Depending on success.
|
||||
*/
|
||||
public function listPaginated( $filter = null ) {
|
||||
$data = self::$db->getPaginated( $this->tableName, '*' );
|
||||
if ( !$data->count() ) {
|
||||
Debug::info( $this->tableName . ' - No entries found' );
|
||||
return false;
|
||||
}
|
||||
return $this->filter( $data->results() );
|
||||
}
|
||||
|
||||
public function list( $filter = null ) {
|
||||
$data = self::$db->get( $this->tableName, '*' );
|
||||
if ( !$data->count() ) {
|
||||
Debug::info( $this->tableName . ' - No entries found' );
|
||||
return false;
|
||||
}
|
||||
return $this->filter( $data->results() );
|
||||
}
|
||||
|
||||
public function search($param) {
|
||||
if (empty($this->searchFields)) {
|
||||
Debug::log('searchFields is empty');
|
||||
return [];
|
||||
}
|
||||
|
||||
$result = self::$db->search($this->tableName, $this->searchFields, $param);
|
||||
|
||||
if ( $result->count() ) {
|
||||
return $this->filter( $result->results() );
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
124
renamed/classes/model.php
Normal file
124
renamed/classes/model.php
Normal file
@ -0,0 +1,124 @@
|
||||
<?php
|
||||
/**
|
||||
* classes/model.php
|
||||
*
|
||||
* The class provides some basic functionality for models.
|
||||
*
|
||||
* @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\Classes;
|
||||
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
use TheTempusProject\Bedrock\Classes\Config;
|
||||
use TheTempusProject\Bedrock\Classes\Database;
|
||||
|
||||
class Model {
|
||||
public static $db;
|
||||
public $configName;
|
||||
public $configMatrix = [];
|
||||
public $resourceMatrix = [];
|
||||
public $modelVersion;
|
||||
public $enabled = false;
|
||||
|
||||
/**
|
||||
* The model constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
Debug::debug( 'Model Constructed: ' . get_class( $this ) );
|
||||
self::$db = Database::getInstance();
|
||||
$this->load();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will remove all the installed model components.
|
||||
*
|
||||
* @return bool - If the uninstall was completed without error
|
||||
*/
|
||||
public function uninstall() {
|
||||
$this->uninstallResources();
|
||||
$this->uninstallConfigs();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Install resources needed for the model.
|
||||
*
|
||||
* @return {bool}
|
||||
*/
|
||||
public function installResources() {
|
||||
// should have some sort of DELTA functionality and safeguards
|
||||
$ids = [];
|
||||
if ( empty($this->resourceMatrix) ) {
|
||||
return true;
|
||||
}
|
||||
foreach ( $this->resourceMatrix as $entry ) {
|
||||
foreach ( $entry as $key => $value ) {
|
||||
if ( '{time}' == $value ) {
|
||||
$entry[$key] = time();
|
||||
}
|
||||
}
|
||||
self::$db->insert( $this->tableName, $entry );
|
||||
$id = self::$db->lastId();
|
||||
if ( $id ) {
|
||||
$ids[] = $id;
|
||||
}
|
||||
}
|
||||
return $ids;
|
||||
}
|
||||
|
||||
public function uninstallResources() {
|
||||
// this one needs some work
|
||||
// should probably save the created resource ID's or something and remove them.
|
||||
// presumably this isn't a big issue because i would imagine the table is removed
|
||||
// there may be future instances where you can create resources for other tables
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Install configs needed for the model.
|
||||
*
|
||||
* @return {bool}
|
||||
*/
|
||||
public function installConfigs() {
|
||||
$config = new Config( CONFIG_JSON );
|
||||
// should have some sort of DELTA functionality and safeguards
|
||||
$config->addCategory( $this->configName );
|
||||
foreach ( $this->configMatrix as $name => $details ) {
|
||||
$config->add( $this->configName, $name, $details );
|
||||
}
|
||||
return $config->save();
|
||||
}
|
||||
|
||||
public function uninstallConfigs() {
|
||||
if ( empty( $this->configName ) ) {
|
||||
return true;
|
||||
}
|
||||
$config = new Config( CONFIG_JSON );
|
||||
return $config->removeCategory( $this->configName, true, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells the installer which types of integrations your model needs to install.
|
||||
*
|
||||
* @return bool - if the model was loaded without error
|
||||
*/
|
||||
public function load() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getModelVersion() {
|
||||
return $this->modelVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the model is enabled.
|
||||
*
|
||||
* @return bool - if the model is enabled or not
|
||||
*/
|
||||
public function enabled() {
|
||||
return $this->enabled;
|
||||
}
|
||||
}
|
226
renamed/classes/pagination.php
Normal file
226
renamed/classes/pagination.php
Normal file
@ -0,0 +1,226 @@
|
||||
<?php
|
||||
/**
|
||||
* classes/pagination.php
|
||||
*
|
||||
* This class is for managing template pagination.
|
||||
*
|
||||
* @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\Classes;
|
||||
|
||||
use TheTempusProject\Houdini\Classes\Template;
|
||||
use TheTempusProject\Hermes\Functions\Route as Routes;
|
||||
use TheTempusProject\Bedrock\Functions\Input;
|
||||
use TheTempusProject\Bedrock\Classes\Config;
|
||||
use TheTempusProject\Bedrock\Functions\Check;
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
use TheTempusProject\Houdini\Classes\Components;
|
||||
use TheTempusProject\Houdini\Classes\Views;
|
||||
|
||||
class Pagination extends Template {
|
||||
//The settings that will not change
|
||||
public static $paginationSettings = [];
|
||||
|
||||
//The instance for each generation
|
||||
public static $instance = null;
|
||||
|
||||
public function __construct() {
|
||||
if ( empty( self::$paginationSettings['limit'] ) ) {
|
||||
$this->loadSettings();
|
||||
}
|
||||
// check for user settings
|
||||
if ( empty( self::$paginationSettings['perPage'] ) ) {
|
||||
self::$paginationSettings['perPage'] = DEFAULT_RESULTS_PER_PAGE;
|
||||
if ( ( !empty( self::$paginationSettings['userPerPage'] ) ) && ( self::$paginationSettings['userPerPage'] <= self::$paginationSettings['maxPerPage'] ) ) {
|
||||
self::$paginationSettings['perPage'] = self::$paginationSettings['userPerPage'];
|
||||
}
|
||||
}
|
||||
// The query minimum and maximum based on current page and page limit
|
||||
if ( self::$paginationSettings['currentPage'] == 1 ) {
|
||||
self::$paginationSettings['min'] = 0;
|
||||
} else {
|
||||
self::$paginationSettings['min'] = ( ( self::$paginationSettings['currentPage'] - 1 ) * self::$paginationSettings['perPage'] );
|
||||
}
|
||||
// if ( self::$paginationSettings['currentPage'] == 1 ) {
|
||||
self::$paginationSettings['max'] = self::$paginationSettings['perPage'];
|
||||
// } else {
|
||||
// self::$paginationSettings['max'] = ( self::$paginationSettings['currentPage'] * self::$paginationSettings['perPage'] );
|
||||
// }
|
||||
// The query limit based on our settings here
|
||||
self::$paginationSettings['limit'] = [self::$paginationSettings['min'], self::$paginationSettings['max']];
|
||||
}
|
||||
|
||||
private static function loadSettings() {
|
||||
Debug::log( 'Loading Pagination Settings.' );
|
||||
// hard cap built into system for displaying results
|
||||
self::$paginationSettings['maxPerPage'] = MAX_RESULTS_PER_PAGE;
|
||||
|
||||
// hard cap built into system retrieving results
|
||||
self::$paginationSettings['maxQuery'] = Config::getValue( 'database/dbMaxQuery' );
|
||||
|
||||
// Set max query to the lowest of the three settings since this will modify how many results are possible.
|
||||
if ( self::$paginationSettings['maxQuery'] <= self::$paginationSettings['maxPerPage'] ) {
|
||||
self::$paginationSettings['maxPerPage'] = self::$paginationSettings['maxQuery'];
|
||||
}
|
||||
|
||||
// Check for results request to set/modify the perPage setting
|
||||
if ( Input::exists( 'results' ) ) {
|
||||
if ( Check::ID( Input::get( 'results' ) ) ) {
|
||||
if ( Input::get( 'results' ) <= self::$paginationSettings['maxPerPage'] ) {
|
||||
self::$paginationSettings['perPage'] = Input::get( 'results' );
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( empty( self::$paginationSettings['perPage'] ) ) {
|
||||
self::$paginationSettings['perPage'] = self::$paginationSettings['maxPerPage'];
|
||||
}
|
||||
|
||||
// Check for pagination in get
|
||||
if ( Input::exists( 'page' ) ) {
|
||||
if ( Check::ID( Input::get( 'page' ) ) ) {
|
||||
self::$paginationSettings['currentPage'] = (int) Input::get( 'page' );
|
||||
} else {
|
||||
self::$paginationSettings['currentPage'] = 1;
|
||||
}
|
||||
} else {
|
||||
self::$paginationSettings['currentPage'] = 1;
|
||||
}
|
||||
|
||||
if ( ( self::$paginationSettings['currentPage'] - 3 ) > 1 ) {
|
||||
self::$paginationSettings['firstPage'] = ( self::$paginationSettings['currentPage'] - 2 );
|
||||
} else {
|
||||
self::$paginationSettings['firstPage'] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
public static function generate() {
|
||||
// account for empty values here instead of inside the script.
|
||||
Debug::log( 'Creating new Pagination Instance.' );
|
||||
self::$instance = new self();
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public static function updatePaginationTotal( $count ) {
|
||||
if ( empty( self::$paginationSettings ) ) {
|
||||
self::generate();
|
||||
}
|
||||
if ( Check::id( $count ) ) {
|
||||
Debug::log( 'Pagination: Updating results count' );
|
||||
self::$paginationSettings['results'] = $count;
|
||||
self::$paginationSettings['totalPages'] = ceil( ( self::$paginationSettings['results'] / self::$paginationSettings['perPage'] ) );
|
||||
if ( ( self::$paginationSettings['currentPage'] + 3 ) < self::$paginationSettings['totalPages'] ) {
|
||||
self::$paginationSettings['lastPage'] = self::$paginationSettings['currentPage'] + 3;
|
||||
} else {
|
||||
self::$paginationSettings['lastPage'] = self::$paginationSettings['totalPages'];
|
||||
}
|
||||
Debug::info( 'Pagination: results update completed.' );
|
||||
} else {
|
||||
Debug::info( 'Pagination: results update failed.' );
|
||||
}
|
||||
}
|
||||
|
||||
public static function paginate() {
|
||||
$pageData = [];
|
||||
if ( self::firstPage() != 1 ) {
|
||||
$data[1]['ACTIVEPAGE'] = '';
|
||||
$data[1]['PAGENUMBER'] = 1;
|
||||
$data[1]['LABEL'] = 'First';
|
||||
$pageData[1] = (object) $data[1];
|
||||
}
|
||||
for ( $x = self::firstPage(); $x < self::lastPage(); $x++ ) {
|
||||
if ( $x == self::currentPage() ) {
|
||||
$active = ' class="active"';
|
||||
} else {
|
||||
$active = '';
|
||||
}
|
||||
$data[$x]['ACTIVEPAGE'] = $active;
|
||||
$data[$x]['PAGENUMBER'] = $x;
|
||||
$data[$x]['LABEL'] = $x;
|
||||
$pageData[$x] = (object) $data[$x];
|
||||
}
|
||||
if ( self::lastPage() <= self::totalPages() ) {
|
||||
$x = self::totalPages();
|
||||
if ( $x == self::currentPage() ) {
|
||||
$active = ' class="active"';
|
||||
} else {
|
||||
$active = '';
|
||||
}
|
||||
$data[$x]['ACTIVEPAGE'] = $active;
|
||||
$data[$x]['PAGENUMBER'] = $x;
|
||||
$data[$x]['LABEL'] = 'Last';
|
||||
$pageData[$x] = (object) $data[$x];
|
||||
}
|
||||
$pageData = (object) $pageData;
|
||||
|
||||
if ( self::totalPages() <= 1 ) {
|
||||
Components::set( 'PAGINATION', '' );
|
||||
} else {
|
||||
Components::set( 'PAGINATION', Views::simpleView( 'nav.pagination', $pageData ) );
|
||||
}
|
||||
}
|
||||
|
||||
public static function updatePrefs( $pageLimit ) {
|
||||
if ( Check::id( $pageLimit ) ) {
|
||||
Debug::log( 'Pagination: Updating user pref' );
|
||||
self::$paginationSettings['userPerPage'] = $pageLimit;
|
||||
} else {
|
||||
Debug::info( 'Pagination: User pref update failed.' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Getters
|
||||
*/
|
||||
public static function getMin() {
|
||||
if ( isset( self::$paginationSettings['min'] ) ) {
|
||||
return self::$paginationSettings['min'];
|
||||
} else {
|
||||
Debug::info( 'Pagination: Min not found' );
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
public static function getMax() {
|
||||
if ( isset( self::$paginationSettings['max'] ) ) {
|
||||
return self::$paginationSettings['max'];
|
||||
} else {
|
||||
Debug::info( 'Pagination: Max not found' );
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
public static function perPage() {
|
||||
if ( !empty( self::$paginationSettings['perPage'] ) ) {
|
||||
return self::$paginationSettings['perPage'];
|
||||
}
|
||||
}
|
||||
public static function firstPage() {
|
||||
if ( !empty( self::$paginationSettings['firstPage'] ) ) {
|
||||
return self::$paginationSettings['firstPage'];
|
||||
} else {
|
||||
Debug::info( 'Pagination: firstPage not found' );
|
||||
}
|
||||
}
|
||||
public static function lastPage() {
|
||||
if ( !empty( self::$paginationSettings['lastPage'] ) ) {
|
||||
return self::$paginationSettings['lastPage'];
|
||||
} else {
|
||||
Debug::info( 'Pagination: lastPage not found' );
|
||||
}
|
||||
}
|
||||
public static function totalPages() {
|
||||
if ( !empty( self::$paginationSettings['totalPages'] ) ) {
|
||||
return self::$paginationSettings['totalPages'];
|
||||
} else {
|
||||
Debug::info( 'Pagination: totalPages not found' );
|
||||
}
|
||||
}
|
||||
public static function currentPage() {
|
||||
if ( !empty( self::$paginationSettings['currentPage'] ) ) {
|
||||
return self::$paginationSettings['currentPage'];
|
||||
} else {
|
||||
Debug::info( 'Pagination: currentPage not found' );
|
||||
}
|
||||
}
|
||||
}
|
44
renamed/composer.json
Normal file
44
renamed/composer.json
Normal file
@ -0,0 +1,44 @@
|
||||
{
|
||||
"name": "thetempusproject/bedrock",
|
||||
"type": "library",
|
||||
"description": "Bedrock is intended as the core functionality used by The Tempus Project: a rapid prototyping framework. This library utilizes the MVC architecture in addition to a custom templating engine designed to make building web applications simple.",
|
||||
"license": "MIT",
|
||||
"minimum-stability": "dev",
|
||||
"keywords":
|
||||
[
|
||||
"thetempusproject",
|
||||
"php",
|
||||
"mvc",
|
||||
"framework"
|
||||
],
|
||||
"homepage": "https://thetempusproject.com/libraries/bedrock",
|
||||
"authors":
|
||||
[
|
||||
{
|
||||
"name": "Joey Kimsey",
|
||||
"email": "Joey@thetempusproject.com",
|
||||
"homepage": "https://JoeyKimsey.com",
|
||||
"role": "Lead Developer"
|
||||
}
|
||||
],
|
||||
"require":
|
||||
{
|
||||
"php": ">=8.1.0",
|
||||
"thetempusproject/canary": "1.0.7",
|
||||
"thetempusproject/hermes": "1.0.5",
|
||||
"thetempusproject/houdini": "2.0.3"
|
||||
},
|
||||
"autoload":
|
||||
{
|
||||
"psr-4":
|
||||
{
|
||||
"TheTempusProject\\Bedroock\\Classes\\": "Classes",
|
||||
"TheTempusProject\\Bedroock\\Functions\\": "Functions"
|
||||
},
|
||||
"files":
|
||||
[
|
||||
"Config/constants.php",
|
||||
"Bin/Bedrock.php"
|
||||
]
|
||||
}
|
||||
}
|
153
renamed/composer.lock
generated
Normal file
153
renamed/composer.lock
generated
Normal file
@ -0,0 +1,153 @@
|
||||
{
|
||||
"_readme": [
|
||||
"This file locks the dependencies of your project to a known state",
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "a5baf978df78c7223d6c2f4bf2421050",
|
||||
"packages": [
|
||||
{
|
||||
"name": "thetempusproject/canary",
|
||||
"version": "1.0.7",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://git.thetempusproject.com/the-tempus-project/canary",
|
||||
"reference": "9c48e66bf54e63ba5ad2d4af90306c87b69f7048"
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.1.0"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"files": [
|
||||
"Config/constants.php",
|
||||
"Bin/Canary.php"
|
||||
],
|
||||
"psr-4": {
|
||||
"TheTempusProject\\Canary\\Classes\\": "Classes"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Joey Kimsey",
|
||||
"email": "Joey@thetempusproject.com",
|
||||
"homepage": "https://JoeyKimsey.com",
|
||||
"role": "Lead Developer"
|
||||
}
|
||||
],
|
||||
"description": "Functionality for tracking, logging, and sending log messages to chrome for debugging.",
|
||||
"homepage": "https://thetempusproject.com/libraries/canary",
|
||||
"keywords": [
|
||||
"debugging",
|
||||
"logging",
|
||||
"php",
|
||||
"thetempusproject",
|
||||
"tools"
|
||||
],
|
||||
"time": "2025-02-02T23:02:51+00:00"
|
||||
},
|
||||
{
|
||||
"name": "thetempusproject/hermes",
|
||||
"version": "1.0.5",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://git.thetempusproject.com/the-tempus-project/hermes",
|
||||
"reference": "802581b1d2d70877ccc75d8954b33efcc05d9371"
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.1.0"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"files": [
|
||||
"config/constants.php"
|
||||
],
|
||||
"classmap": [
|
||||
"classes",
|
||||
"functions"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Joey Kimsey",
|
||||
"email": "Joey@thetempusproject.com",
|
||||
"homepage": "https://JoeyKimsey.com",
|
||||
"role": "Lead Developer"
|
||||
}
|
||||
],
|
||||
"description": "This library handles redirects, provides a common backbone for routing, and can handle autoloading in cases where composer is unavailable.",
|
||||
"homepage": "https://thetempusproject.com/libraries/hermes",
|
||||
"keywords": [
|
||||
"autoloader",
|
||||
"php",
|
||||
"routing",
|
||||
"thetempusproject",
|
||||
"tools"
|
||||
],
|
||||
"time": "2025-02-02T23:22:15+00:00"
|
||||
},
|
||||
{
|
||||
"name": "thetempusproject/houdini",
|
||||
"version": "2.0.3",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://git.thetempusproject.com/the-tempus-project/houdini",
|
||||
"reference": "4c9c9b60233c4dd7a366758c8436560098761eb5"
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.1.0",
|
||||
"thetempusproject/canary": "1.0.7",
|
||||
"thetempusproject/hermes": "1.0.5"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"files": [
|
||||
"config/constants.php"
|
||||
],
|
||||
"classmap": [
|
||||
"classes"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Joey Kimsey",
|
||||
"email": "Joey@thetempusproject.com",
|
||||
"homepage": "https://JoeyKimsey.com",
|
||||
"role": "Lead Developer"
|
||||
}
|
||||
],
|
||||
"description": "This library handles creating, managing, and displaying frontend components.",
|
||||
"homepage": "https://thetempusproject.com/libraries/houdini",
|
||||
"keywords": [
|
||||
"frontend",
|
||||
"php",
|
||||
"thetempusproject",
|
||||
"tools"
|
||||
],
|
||||
"time": "2025-02-02T23:31:11+00:00"
|
||||
}
|
||||
],
|
||||
"packages-dev": [],
|
||||
"aliases": [],
|
||||
"minimum-stability": "dev",
|
||||
"stability-flags": [],
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
"php": ">=8.1.0"
|
||||
},
|
||||
"platform-dev": [],
|
||||
"plugin-api-version": "2.3.0"
|
||||
}
|
105
renamed/config/config.json
Normal file
105
renamed/config/config.json
Normal file
@ -0,0 +1,105 @@
|
||||
{
|
||||
"main": {
|
||||
"logo": {
|
||||
"type": "file",
|
||||
"pretty": "Site Logo (Used mostly in emails)",
|
||||
"default": "images/logo.png",
|
||||
"value": "images/logo.png"
|
||||
},
|
||||
"name": {
|
||||
"type": "text",
|
||||
"pretty": "Site Name",
|
||||
"default": "TTP Example",
|
||||
"value": "TTP Example"
|
||||
},
|
||||
"template": {
|
||||
"type": "text",
|
||||
"pretty": "Default Site Template",
|
||||
"default": "default",
|
||||
"value": "default"
|
||||
},
|
||||
"tokenEnabled": {
|
||||
"type": "radio",
|
||||
"pretty": "Enable CSRF Token for all forms.",
|
||||
"default": true,
|
||||
"value": true
|
||||
}
|
||||
},
|
||||
"uploads": {
|
||||
"files": {
|
||||
"type": "radio",
|
||||
"pretty": "Enable File Uploads",
|
||||
"default": true,
|
||||
"value": true
|
||||
},
|
||||
"images": {
|
||||
"type": "radio",
|
||||
"pretty": "Enable Image Uploads",
|
||||
"default": true,
|
||||
"value": true
|
||||
},
|
||||
"maxFileSize": {
|
||||
"type": "text",
|
||||
"pretty": "Maximum File Size",
|
||||
"default": 5000000,
|
||||
"value": 5000000
|
||||
},
|
||||
"maxImageSize": {
|
||||
"type": "text",
|
||||
"pretty": "Maximum Image Size",
|
||||
"default": 500000,
|
||||
"value": 500000
|
||||
}
|
||||
},
|
||||
"database": {
|
||||
"dbEnabled": {
|
||||
"type": "radio",
|
||||
"pretty": "Database Enabled",
|
||||
"default": true,
|
||||
"protected": true,
|
||||
"value": true
|
||||
},
|
||||
"dbHost": {
|
||||
"type": "text",
|
||||
"pretty": "Database Host (IE: http://localhost:3306)",
|
||||
"default": "127.0.0.1",
|
||||
"protected": true,
|
||||
"value": "127.0.0.1"
|
||||
},
|
||||
"dbMaxQuery": {
|
||||
"type": "text",
|
||||
"pretty": "Maximum results per query",
|
||||
"default": 100,
|
||||
"protected": true,
|
||||
"value": 100
|
||||
},
|
||||
"dbName": {
|
||||
"type": "text",
|
||||
"pretty": "Database Name",
|
||||
"default": "ttp-example",
|
||||
"protected": true,
|
||||
"value": "ttp-example"
|
||||
},
|
||||
"dbPassword": {
|
||||
"type": "text",
|
||||
"pretty": "Database Password",
|
||||
"default": "",
|
||||
"protected": true,
|
||||
"value": ""
|
||||
},
|
||||
"dbPrefix": {
|
||||
"type": "text",
|
||||
"pretty": "Database table Prefix",
|
||||
"default": "TTP_",
|
||||
"protected": true,
|
||||
"value": "TTP_"
|
||||
},
|
||||
"dbUsername": {
|
||||
"type": "text",
|
||||
"pretty": "Database Username",
|
||||
"default": "root",
|
||||
"protected": true,
|
||||
"value": "root"
|
||||
}
|
||||
}
|
||||
}
|
183
renamed/config/constants.php
Normal file
183
renamed/config/constants.php
Normal file
@ -0,0 +1,183 @@
|
||||
<?php
|
||||
// Check
|
||||
if (!defined('MINIMUM_PHP_VERSION')) {
|
||||
define('MINIMUM_PHP_VERSION', 8.2);
|
||||
}
|
||||
if (!defined('DATA_TITLE_PREG')) {
|
||||
define('DATA_TITLE_PREG', '#^[a-z 0-9\-\_ ]+$#mi');
|
||||
}
|
||||
if (!defined('REDIRECT_PREG_REQS')) {
|
||||
define('REDIRECT_PREG_REQS', '#^[a-zA-Z0-9\-\_\./]+$#mi');
|
||||
}
|
||||
if (!defined('PATH_PREG_REQS')) {
|
||||
define('PATH_PREG_REQS', '#^[^/?*:;\\{}]+$#mi');
|
||||
}
|
||||
if (!defined('SIMPLE_NAME_PREG')) {
|
||||
define('SIMPLE_NAME_PREG', '#^[a-zA-Z0-9\-\_]+$#mi');
|
||||
}
|
||||
if (!defined('ALLOWED_IMAGE_UPLOAD_EXTENTIONS')) {
|
||||
define('ALLOWED_IMAGE_UPLOAD_EXTENTIONS', [".jpg",".jpeg",".gif",".png"]);
|
||||
}
|
||||
if (!defined('MINIMUM_PASSWORD_LENGTH')) {
|
||||
define('MINIMUM_PASSWORD_LENGTH', 8);
|
||||
}
|
||||
if (!defined('MAXIMUM_PASSWORD_LENGTH')) {
|
||||
define('MAXIMUM_PASSWORD_LENGTH', 24);
|
||||
}
|
||||
if (!defined('ADDITIONAL_PASSWORD_REGEX')) {
|
||||
// define('ADDITIONAL_PASSWORD_REGEX', '#^[a-z 0-9\-\_ ]+$#mi');
|
||||
define('ADDITIONAL_PASSWORD_REGEX', '#^[a-z0-9\~\_\+\=\.\,\<\>\(\)\#\?\!\@\$\%\^\&\*\-\ ]+$#mi');
|
||||
}
|
||||
if (!defined('SESSION_NAME_REGEX')) {
|
||||
define('SESSION_NAME_REGEX', '#^[a-zA-Z0-9\_\-]+$#mi');
|
||||
}
|
||||
// Cookies
|
||||
if (!defined('DEFAULT_COOKIE_EXPIRATION')) {
|
||||
define('DEFAULT_COOKIE_EXPIRATION', 604800);
|
||||
}
|
||||
if (!defined('DEFAULT_COOKIE_PREFIX')) {
|
||||
define('DEFAULT_COOKIE_PREFIX', 'TTP_BEDROCK_');
|
||||
}
|
||||
// Database
|
||||
if (!defined('MAX_RESULTS_PER_PAGE')) {
|
||||
define('MAX_RESULTS_PER_PAGE', 50);
|
||||
}
|
||||
if (!defined('DEFAULT_RESULTS_PER_PAGE')) {
|
||||
define('DEFAULT_RESULTS_PER_PAGE', 10);
|
||||
}
|
||||
// Debug
|
||||
// Log Levels
|
||||
if ( ! defined('CANARY_DEBUG_LEVEL_ERROR' ) ) {
|
||||
define( 'CANARY_DEBUG_LEVEL_ERROR', 'error' );
|
||||
}
|
||||
if ( ! defined('CANARY_DEBUG_LEVEL_WARN' ) ) {
|
||||
define( 'CANARY_DEBUG_LEVEL_WARN', 'warn' );
|
||||
}
|
||||
if ( ! defined('CANARY_DEBUG_LEVEL_INFO' ) ) {
|
||||
define( 'CANARY_DEBUG_LEVEL_INFO', 'info' );
|
||||
}
|
||||
if ( ! defined('CANARY_DEBUG_LEVEL_LOG' ) ) {
|
||||
define( 'CANARY_DEBUG_LEVEL_LOG', 'log' );
|
||||
}
|
||||
if ( ! defined('CANARY_DEBUG_LEVEL_DEBUG' ) ) {
|
||||
define( 'CANARY_DEBUG_LEVEL_DEBUG', 'debug' );
|
||||
}
|
||||
if (!defined('CANARY_ENABLED')) {
|
||||
define('CANARY_ENABLED', false);
|
||||
}
|
||||
if (!defined('BEDROCK_DEFAULT_ERROR_HANDLER')) {
|
||||
define('BEDROCK_DEFAULT_ERROR_HANDLER', 'TheTempusProject\\Canary\\Bin\\Canary');
|
||||
}
|
||||
if (!defined('BEDROCK_DEFAULT_EXCEPTION_HANDLER')) {
|
||||
define('BEDROCK_DEFAULT_EXCEPTION_HANDLER', 'TheTempusProject\\Canary\\Bin\\Canary');
|
||||
}
|
||||
if (!defined('DEBUG_EMAIL')) {
|
||||
define('DEBUG_EMAIL', 'webmaster@' . $_SERVER['HTTP_HOST']);
|
||||
}
|
||||
if (!defined('HERMES_REDIRECTS_ENABLED')) {
|
||||
define('HERMES_REDIRECTS_ENABLED', true);
|
||||
}
|
||||
if (!defined('RENDERING_ENABLED')) {
|
||||
define('RENDERING_ENABLED', true);
|
||||
}
|
||||
if (!defined('CANARY_TRACE_ENABLED')) {
|
||||
define('CANARY_TRACE_ENABLED', false);
|
||||
}
|
||||
if (!defined('CANARY_DEBUG_TO_CONSOLE')) {
|
||||
define('CANARY_DEBUG_TO_CONSOLE', false);
|
||||
}
|
||||
if (!defined('CANARY_DEBUG_TO_FILE')) {
|
||||
define('CANARY_DEBUG_TO_FILE', false);
|
||||
}
|
||||
if (!defined('CANARY_DEBUG_TO_FILE_LEVEL')) {
|
||||
define('CANARY_DEBUG_TO_FILE_LEVEL', CANARY_DEBUG_LEVEL_ERROR);
|
||||
}
|
||||
// Directories
|
||||
if (!defined('BEDROCK_ROOT_DIRECTORY')) {
|
||||
define('BEDROCK_ROOT_DIRECTORY', dirname(__DIR__) . DIRECTORY_SEPARATOR);
|
||||
}
|
||||
if (!defined('BEDROCK_CONFIG_DIRECTORY')) {
|
||||
define('BEDROCK_CONFIG_DIRECTORY', BEDROCK_ROOT_DIRECTORY . 'config' . DIRECTORY_SEPARATOR);
|
||||
}
|
||||
if (!defined('BEDROCK_BIN_DIRECTORY')) {
|
||||
define('BEDROCK_BIN_DIRECTORY', BEDROCK_ROOT_DIRECTORY . 'bin' . DIRECTORY_SEPARATOR);
|
||||
}
|
||||
if (!defined('BEDROCK_RESOURCES_DIRECTORY')) {
|
||||
define('BEDROCK_RESOURCES_DIRECTORY', BEDROCK_ROOT_DIRECTORY . 'resources' . DIRECTORY_SEPARATOR);
|
||||
}
|
||||
if (!defined('BEDROCK_VIEW_DIRECTORY')) {
|
||||
define('BEDROCK_VIEW_DIRECTORY', BEDROCK_ROOT_DIRECTORY . 'views' . DIRECTORY_SEPARATOR);
|
||||
}
|
||||
if (!defined('BEDROCK_ERRORS_DIRECTORY')) {
|
||||
define('BEDROCK_ERRORS_DIRECTORY', BEDROCK_VIEW_DIRECTORY . 'errors' . DIRECTORY_SEPARATOR);
|
||||
}
|
||||
if (!defined('BEDROCK_CLASSES_DIRECTORY')) {
|
||||
define('BEDROCK_CLASSES_DIRECTORY', BEDROCK_ROOT_DIRECTORY . 'classes' . DIRECTORY_SEPARATOR);
|
||||
}
|
||||
if (!defined('BEDROCK_FUNCTIONS_DIRECTORY')) {
|
||||
define('BEDROCK_FUNCTIONS_DIRECTORY', BEDROCK_ROOT_DIRECTORY . 'functions' . DIRECTORY_SEPARATOR);
|
||||
}
|
||||
if (!defined('BEDROCK_CONFIG_JSON')) {
|
||||
define('BEDROCK_CONFIG_JSON', BEDROCK_CONFIG_DIRECTORY . 'config.json');
|
||||
}
|
||||
// Files
|
||||
if (!defined('COMPOSER_JSON_LOCATION')) {
|
||||
define('COMPOSER_JSON_LOCATION', APP_ROOT_DIRECTORY . 'composer.json');
|
||||
}
|
||||
if (!defined('COMPOSER_LOCK_LOCATION')) {
|
||||
define('COMPOSER_LOCK_LOCATION', APP_ROOT_DIRECTORY . 'composer.lock');
|
||||
}
|
||||
if (!defined('CONFIG_JSON')) {
|
||||
define('CONFIG_JSON', CONFIG_DIRECTORY . 'config.json');
|
||||
}
|
||||
// Other
|
||||
if (!defined('DEFAULT_CONTROLLER_CLASS')) {
|
||||
define('DEFAULT_CONTROLLER_CLASS', 'Home');
|
||||
}
|
||||
if (!defined('DEFAULT_CONTROLLER_METHOD')) {
|
||||
define('DEFAULT_CONTROLLER_METHOD', 'index');
|
||||
}
|
||||
if (!defined('EMAIL_FROM_EMAIL')) {
|
||||
define('EMAIL_FROM_EMAIL', 'noreply@' . $_SERVER['HTTP_HOST']);
|
||||
}
|
||||
if (!defined('DEFAULT_TIMEZONE')) {
|
||||
define('DEFAULT_TIMEZONE', "America/New_York");
|
||||
}
|
||||
if (!defined('DEFAULT_DATE_FORMAT')) {
|
||||
define('DEFAULT_DATE_FORMAT', "F j, Y");
|
||||
}
|
||||
if (!defined('DEFAULT_TIME_FORMAT')) {
|
||||
define('DEFAULT_TIME_FORMAT', "g:i:s A");
|
||||
}
|
||||
// Random
|
||||
if (!defined('KB')) {
|
||||
define('KB', 1024);
|
||||
}
|
||||
if (!defined('MB')) {
|
||||
define('MB', 1048576);
|
||||
}
|
||||
if (!defined('GB')) {
|
||||
define('GB', 1073741824);
|
||||
}
|
||||
if (!defined('TB')) {
|
||||
define('TB', 1099511627776);
|
||||
}
|
||||
// Sessions
|
||||
if (!defined('DEFAULT_SESSION_PREFIX')) {
|
||||
define('DEFAULT_SESSION_PREFIX', 'TTP_BEDROCK_');
|
||||
}
|
||||
// Token
|
||||
if (!defined('DEFAULT_TOKEN_NAME')) {
|
||||
define('DEFAULT_TOKEN_NAME', 'TTP_BEDROCK_SESSION_TOKEN');
|
||||
}
|
||||
if (!defined('TOKEN_ENABLED')) {
|
||||
define('TOKEN_ENABLED', true);
|
||||
}
|
||||
// dUH
|
||||
if (!defined('APP_NAME')) {
|
||||
define('APP_NAME', 'Bedrock Application');
|
||||
}
|
||||
# Tell the app all constants have been loaded.
|
||||
if ( ! defined('BEDROCK_CONSTANTS_LOADED' ) ) {
|
||||
define( 'BEDROCK_CONSTANTS_LOADED', true );
|
||||
}
|
541
renamed/functions/check.php
Normal file
541
renamed/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
renamed/functions/code.php
Normal file
51
renamed/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
renamed/functions/cookie.php
Normal file
95
renamed/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
renamed/functions/date.php
Normal file
409
renamed/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
renamed/functions/hash.php
Normal file
43
renamed/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
renamed/functions/input.php
Normal file
111
renamed/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
renamed/functions/sanitize.php
Normal file
44
renamed/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
renamed/functions/session.php
Normal file
136
renamed/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
renamed/functions/token.php
Normal file
120
renamed/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
renamed/functions/upload.php
Normal file
73
renamed/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;
|
||||
}
|
||||
}
|
15
renamed/resources/htaccess.html
Normal file
15
renamed/resources/htaccess.html
Normal file
@ -0,0 +1,15 @@
|
||||
RewriteEngine On
|
||||
RewriteBase {APP_WEB_ROOT}
|
||||
|
||||
# Intercepts other errors
|
||||
RewriteRule ^errors/(.*)$ index.php?error=$1 [L,NC,QSA]
|
||||
|
||||
# Intercept all traffic not originating locally and not going to images or uploads
|
||||
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1
|
||||
RewriteCond %{REMOTE_ADDR} !^\:\:1
|
||||
RewriteRule ^(.+)$ {APP_WEB_ROOT}?url=$1 [QSA,L]
|
||||
|
||||
# Catchall for any non existent files or folders
|
||||
RewriteCond %{REQUEST_FILENAME} !-f
|
||||
RewriteCond %{REQUEST_FILENAME} !-d
|
||||
RewriteRule ^(.+)$ {APP_WEB_ROOT}?url=$1 [QSA,L]";
|
12
renamed/resources/nginx.html
Normal file
12
renamed/resources/nginx.html
Normal file
@ -0,0 +1,12 @@
|
||||
location /web/www/errors {
|
||||
rewrite ^/web/www/errors/(.*)$ /web/www/index.php?error=$1 break;
|
||||
}
|
||||
|
||||
location /web/www/ {
|
||||
if ($remote_addr !~ "^127\.0\.0\.1"){
|
||||
rewrite ^/web/www/(.+)$ /web/www/indexParser.php?local=false&url=$1 break;
|
||||
}
|
||||
if (!-e $request_filename){
|
||||
rewrite ^/web/www/(.+)$ /web/www/indexParser.php?missing=true&url=$1 [QSA,L]";;
|
||||
}
|
||||
}
|
2
renamed/vendor/.gitignore
vendored
Normal file
2
renamed/vendor/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
Reference in New Issue
Block a user