wip from ATB
This commit is contained in:
@ -18,6 +18,8 @@ use TheTempusProject\Hermes\Functions\Redirect;
|
||||
use TheTempusProject\Bedrock\Functions\Session;
|
||||
use TheTempusProject\Bedrock\Classes\Config;
|
||||
use TheTempusProject\Models\Token;
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
use TheTempusProject\Houdini\Classes\Views;
|
||||
|
||||
class ApiController extends Controller {
|
||||
protected static $canAccessApplicationApi = false;
|
||||
@ -26,16 +28,15 @@ class ApiController extends Controller {
|
||||
protected static $authToken;
|
||||
|
||||
public function __construct( $secure = true ) {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
parent::__construct();
|
||||
$this->verifyApiRequest();
|
||||
if ( $secure && ! $this->canUseApi() ) {
|
||||
Session::flash( 'error', 'You do not have permission to view this page.' );
|
||||
return Redirect::home();
|
||||
}
|
||||
Template::setTemplate( 'api' );
|
||||
Template::noFollow();
|
||||
Template::noIndex();
|
||||
Template::addHeader( 'Content-Type: application/json; charset=utf-8' );
|
||||
Template::setTemplate( 'api' );
|
||||
$res = $this->verifyApiRequest();
|
||||
if ( $secure && ! $this->canUseApi() ) {
|
||||
exit( $res );
|
||||
}
|
||||
}
|
||||
|
||||
protected function canUseApi() {
|
||||
@ -72,16 +73,16 @@ class ApiController extends Controller {
|
||||
} else {
|
||||
$secret = $this->getSecretToken();
|
||||
if ( empty( $secret ) ) {
|
||||
return;
|
||||
return Views::simpleView( 'api.response', ['response' => json_encode( [ 'error' => 'invalid secret' ], true )]);
|
||||
}
|
||||
$token = $tokens->findBySecret( $secret );
|
||||
}
|
||||
if ( empty( $token ) ) {
|
||||
return;
|
||||
return Views::simpleView( 'api.response', ['response' => json_encode( [ 'error' => 'invalid token' ], true )]);
|
||||
}
|
||||
self::$authToken = $token;
|
||||
if ( $token->expiresAt <= time() && empty( $secret ) ) {
|
||||
return;
|
||||
return Views::simpleView( 'api.response', ['response' => json_encode( [ 'error' => 'token expired' ], true )]);
|
||||
}
|
||||
if ( $token->expiresAt <= time() ) {
|
||||
self::$canAccessAuthenticationApi = true;
|
||||
|
@ -114,6 +114,7 @@ class Forms extends Check {
|
||||
self::addHandler( 'install', __CLASS__, 'install' );
|
||||
self::addHandler( 'adminCreateToken', __CLASS__, 'adminCreateToken' );
|
||||
self::addHandler( 'apiLogin', __CLASS__, 'apiLogin' );
|
||||
self::addHandler( 'updatePreference', __CLASS__, 'updatePreference' );
|
||||
self::addHandler( 'installStart', __CLASS__, 'install', [ 'start' ] );
|
||||
self::addHandler( 'installAgreement', __CLASS__, 'install', [ 'agreement' ] );
|
||||
self::addHandler( 'installCheck', __CLASS__, 'install', [ 'check' ] );
|
||||
@ -650,4 +651,16 @@ class Forms extends Check {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function updatePreference() {
|
||||
if ( !Input::exists( 'prefName' ) ) {
|
||||
self::addUserError( 'You must specify a name' );
|
||||
return false;
|
||||
}
|
||||
if ( !Input::exists( 'prefValue' ) ) {
|
||||
self::addUserError( 'You must specify a value' );
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ namespace TheTempusProject\Classes;
|
||||
|
||||
use TheTempusProject\Houdini\Classes\Issues;
|
||||
use TheTempusProject\Houdini\Classes\Forms;
|
||||
use TheTempusProject\Houdini\Classes\Template;
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
use TheTempusProject\Bedrock\Functions\Check;
|
||||
use TheTempusProject\Bedrock\Functions\Upload;
|
||||
@ -186,17 +187,92 @@ class Preferences {
|
||||
}
|
||||
|
||||
public function getFormHtml( $populated = [] ) {
|
||||
// dv( self::$preferences );
|
||||
$form = '';
|
||||
// Added so i can force some sort of ordering
|
||||
$inputTypes = [
|
||||
'file' => [],
|
||||
'select' => [],
|
||||
'timezone' => [],
|
||||
'checkbox' => [],
|
||||
'switch' => [],
|
||||
];
|
||||
foreach ( self::$preferences as $name => $details ) {
|
||||
$tempPrefsArray = $this->normalizePreferenceArray( $name, $details );
|
||||
if ( isset( $populated[ $name ] ) ) {
|
||||
$tempPrefsArray['default'] = $populated[$name];
|
||||
$tempPrefsArray['value'] = $populated[$name];
|
||||
} else {
|
||||
$tempPrefsArray['value'] = $tempPrefsArray['default'];
|
||||
}
|
||||
$form .= Forms::getFormFieldHtml( $name, $tempPrefsArray['pretty'], $tempPrefsArray['type'], $tempPrefsArray['default'], $tempPrefsArray['options'] );
|
||||
// $form .= Forms::getFormFieldHtml( $name, $tempPrefsArray['pretty'], $tempPrefsArray['type'], $tempPrefsArray['default'], $tempPrefsArray['options'] );
|
||||
if ( $tempPrefsArray['type'] == 'checkbox' ) {
|
||||
$tempPrefsArray['type'] = 'switch';
|
||||
}
|
||||
$inputTypes[ $tempPrefsArray['type'] ][] = self::getFormFieldHtml( $name, $tempPrefsArray['pretty'], $tempPrefsArray['type'], $tempPrefsArray['value'], $tempPrefsArray['options'] );
|
||||
}
|
||||
foreach ( $inputTypes as $skip => $items ) {
|
||||
$form .= implode( ' ', $items );
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
||||
public static function getFormFieldHtml( $fieldname, $fieldTitle, $type, $defaultValue = '', $options = null ) {
|
||||
$html = '';
|
||||
switch ( $type ) {
|
||||
case 'radio':
|
||||
case 'bool':
|
||||
case 'boolean':
|
||||
$fieldHtml = Forms::getRadioHtml( $fieldname, [ 'true', 'false' ], $defaultValue );
|
||||
break;
|
||||
case 'select':
|
||||
$fieldHtml = Forms::getSelectHtml( $fieldname, $options, $defaultValue );
|
||||
break;
|
||||
case 'customSelect':
|
||||
if ( empty( $options ) ) {
|
||||
$options = '{' . $fieldname . '-options}';
|
||||
}
|
||||
$fieldHtml = Forms::getSelectHtml( $fieldname, $options, $defaultValue );
|
||||
break;
|
||||
case 'block':
|
||||
$fieldHtml = Forms::getTextBlockHtml( $fieldname, $defaultValue );
|
||||
break;
|
||||
case 'text':
|
||||
case 'url':
|
||||
$fieldHtml = Forms::getTextHtml( $fieldname, $defaultValue );
|
||||
break;
|
||||
case 'checkbox':
|
||||
$fieldHtml = Forms::getCheckboxHtml( $fieldname, $defaultValue );
|
||||
break;
|
||||
case 'switch':
|
||||
$fieldHtml = Forms::getSwitchHtml( $fieldname, $defaultValue );
|
||||
break;
|
||||
case 'timezone':
|
||||
$fieldHtml = Forms::getTimezoneHtml( $defaultValue );
|
||||
break;
|
||||
case 'file':
|
||||
$fieldHtml = Forms::getFileHtml( $fieldname );
|
||||
break;
|
||||
default:
|
||||
Debug::error( "unknown field type: $type" );
|
||||
break;
|
||||
}
|
||||
|
||||
$html .= '<div class="mb-3 row">';
|
||||
$html .= '<label for="' . $fieldname . '" class="col-lg-6 col-form-label text-end">' . $fieldTitle . '</label>';
|
||||
$html .= '<div class="col-lg-6">';
|
||||
$html .= $fieldHtml;
|
||||
$html .= '</div>';
|
||||
if ( 'file' === $type ) {
|
||||
$html .= '<div class="mb-3 row">';
|
||||
$html .= '<h4 class="col-lg-6 col-form-label text-end">Current Image</h4>';
|
||||
$html .= '<div class="col-lg-6">';
|
||||
$html .= '<img alt="User Avatar" src="{ROOT_URL}' . $defaultValue . '" class="img-circle img-fluid p-2 avatar-125">';
|
||||
$html .= '</div>';
|
||||
}
|
||||
$html .= '</div>';
|
||||
return Template::parse( $html );
|
||||
}
|
||||
|
||||
public function convertFormToArray( $fillMissing = true, $defaultsOnly = true ) {
|
||||
$prefsArray = [];
|
||||
foreach ( self::$preferences as $name => $details ) {
|
||||
|
Reference in New Issue
Block a user