1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-15 09:34:14 +02:00

Flextype Admin: Classes removed. We will use controllers instead of them.

This commit is contained in:
Awilum
2019-04-19 11:03:27 +03:00
parent 3ee75b3d8b
commit 1c12224457
9 changed files with 0 additions and 1656 deletions

View File

@@ -1,13 +0,0 @@
<?php
namespace Flextype;
use Flextype\Component\Http\Http;
class DashboardManager
{
public static function getDashboardManager()
{
Http::redirect(Http::getBaseUrl() . '/admin/entries');
}
}

View File

@@ -1,672 +0,0 @@
<?php
namespace Flextype;
use Flextype\Component\Arr\Arr;
use Flextype\Component\I18n\I18n;
use Flextype\Component\Http\Http;
use Flextype\Component\Event\Event;
use Flextype\Component\Filesystem\Filesystem;
use Flextype\Component\Session\Session;
use Flextype\Component\Registry\Registry;
use Flextype\Component\Token\Token;
use Flextype\Component\Text\Text;
use Flextype\Component\Form\Form;
use Flextype\Component\Notification\Notification;
use function Flextype\Component\I18n\__;
use Gajus\Dindent\Indenter;
use Intervention\Image\ImageManagerStatic as Image;
$app->get('/admin/entries', function (Request $request, Response $response, array $args) {
return 'asd';
})->setName('entries');
class EntriesManager
{
public static function getEntriesManager() : void
{
Registry::set('sidebar_menu_item', 'entries');
$query = EntriesManager::getEntriesQuery();
switch (Http::getUriSegment(2)) {
case 'add':
EntriesManager::addEntry();
break;
case 'delete':
EntriesManager::deleteEntry();
break;
case 'duplicate':
EntriesManager::duplicateEntry();
break;
case 'rename':
EntriesManager::renameEntry();
break;
case 'type':
EntriesManager::typeEntry();
break;
case 'move':
EntriesManager::moveEntry();
break;
case 'edit':
EntriesManager::editEntry();
break;
default:
EntriesManager::listEntry();
break;
}
}
public static function getMediaList(string $entry, bool $path = false) : array
{
$files = [];
foreach (array_diff(scandir(PATH['entries'] . '/' . $entry), ['..', '.']) as $file) {
if (strpos(Registry::get('settings.entries.media.accept_file_types'), $file_ext = substr(strrchr($file, '.'), 1)) !== false) {
if (strpos($file, strtolower($file_ext), 1)) {
if ($path) {
$files[Http::getBaseUrl() . '/' . $entry . '/' . $file] = Http::getBaseUrl() . '/' . $entry . '/' . $file;
} else {
$files[$file] = $file;
}
}
}
}
return $files;
}
protected static function getEntriesQuery() : string
{
if (Http::get('entry') && Http::get('entry') != '') {
$query = Http::get('entry');
} else {
$query = '';
}
return $query;
}
protected static function listEntry() : void
{
Themes::view('admin/views/templates/content/entries/list')
->assign('entries_list', Entries::fetchAll(EntriesManager::getEntriesQuery(), 'date', 'DESC'))
->display();
}
protected static function processFilesManager() : void
{
$files_directory = PATH['entries'] . '/' . Http::get('entry') . '/';
if (Http::get('delete_file') != '') {
if (Token::check((Http::get('token')))) {
Filesystem::delete($files_directory . Http::get('delete_file'));
Notification::set('success', __('admin_message_entry_file_deleted'));
Http::redirect(Http::getBaseUrl() . '/admin/entries/edit?entry=' . Http::get('entry') . '&media=true');
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
if (Http::post('upload_file')) {
if (Token::check(Http::post('token'))) {
$file = EntriesManager::uploadFile($_FILES['file'], $files_directory, Registry::get('settings.entries.media.accept_file_types'), 27000000);
if ($file !== false) {
if (in_array(pathinfo($file)['extension'], ['jpg', 'jpeg', 'png', 'gif'])) {
// open an image file
$img = Image::make($file);
// now you are able to resize the instance
if (Registry::get('settings.entries.media.upload_images_width') > 0 && Registry::get('settings.entries.media.upload_images_height') > 0) {
$img->resize(Registry::get('settings.entries.media.upload_images_width'), Registry::get('settings.entries.media.upload_images_height'), function($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
} elseif (Registry::get('settings.entries.media.upload_images_width') > 0) {
$img->resize(Registry::get('settings.entries.media.upload_images_width'), null, function($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
} elseif (Registry::get('settings.entries.media.upload_images_height') > 0) {
$img->resize(null, Registry::get('settings.entries.media.upload_images_height'), function($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
}
// finally we save the image as a new file
$img->save($file, Registry::get('settings.entries.media.upload_images_quality'));
// destroy
$img->destroy();
}
Notification::set('success', __('admin_message_entry_file_uploaded'));
Http::redirect(Http::getBaseUrl() . '/admin/entries/edit?entry=' . Http::get('entry') . '&media=true');
} else {
Notification::set('error', __('admin_message_entry_file_not_uploaded'));
Http::redirect(Http::getBaseUrl() . '/admin/entries/edit?entry=' . Http::get('entry') . '&media=true');
}
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
}
protected static function editEntry() : void
{
$entry = Entries::fetch(Http::get('entry'));
if (Http::get('media') && Http::get('media') == 'true') {
EntriesManager::processFilesManager();
Themes::view('admin/views/templates/content/entries/media')
->assign('entry_name', Http::get('entry'))
->assign('files', EntriesManager::getMediaList(Http::get('entry')), true)
->assign('entry', $entry)
->display();
} else {
if (Http::get('source') && Http::get('source') == 'true') {
$action = Http::post('action');
if (isset($action) && $action == 'save-form') {
if (Token::check((Http::post('token')))) {
if (Filesystem::write(
PATH['entries'] . '/' . Http::post('entry_name') . '/entry.yaml',
Http::post('entry_content')
)) {
Notification::set('success', __('admin_message_entry_changes_saved'));
} else {
Notification::set('success', __('admin_message_entry_changes_not_saved'));
}
Http::redirect(Http::getBaseUrl() . '/admin/entries/edit?entry=' . Http::post('entry_name') . '&source=true');
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
$entry_content = Filesystem::read(PATH['entries'] . '/' . Http::get('entry') . '/entry.yaml');
Themes::view('admin/views/templates/content/entries/source')
->assign('entry_name', Http::get('entry'))
->assign('entry_content', $entry_content)
->assign('entry', $entry)
->assign('files', EntriesManager::getMediaList(Http::get('entry')), true)
->display();
} else {
$action = Http::post('action');
$indenter = new Indenter();
if (isset($action) && $action == 'save-form') {
if (Token::check((Http::post('token')))) {
$entry = Entries::fetch(Http::get('entry'));
Arr::delete($entry, 'slug');
$data = [];
$_data = $_POST;
Arr::delete($_data, 'token');
Arr::delete($_data, 'action');
foreach ($_data as $key => $_d) {
$data[$key] = $indenter->indent($_d);
}
$data = array_merge($entry, $data);
if (Entries::update(Http::get('entry'), $data)) {
Notification::set('success', __('admin_message_entry_changes_saved'));
} else {
Notification::set('error', __('admin_message_entry_changes_not_saved'));
}
Http::redirect(Http::getBaseUrl() . '/admin/entries/edit?entry=' . Http::get('entry'));
}
}
// Fieldset for current entry template
$fieldset_path = PATH['themes'] . '/' . Registry::get('settings.theme') . '/fieldsets/' . (isset($entry['fieldset']) ? $entry['fieldset'] : 'default') . '.yaml';
$fieldset = YamlParser::decode(Filesystem::read($fieldset_path));
is_null($fieldset) and $fieldset = [];
Themes::view('admin/views/templates/content/entries/content')
->assign('entry_name', Http::get('entry'))
->assign('entry', $entry)
->assign('fieldset', $fieldset)
->assign('templates', Themes::getTemplates())
->assign('files', EntriesManager::getMediaList(Http::get('entry')), true)
->display();
}
}
}
protected static function duplicateEntry() : void
{
if (Http::get('entry') != '') {
if (Token::check((Http::get('token')))) {
if (Entries::copy(Http::get('entry'), Http::get('entry') . '-duplicate-' . date("Ymd_His"), true)) {
Notification::set('success', __('admin_message_entry_duplicated'));
} else {
Notification::set('error', __('admin_message_entry_was_not_duplicated'));
}
Http::redirect(Http::getBaseUrl() . '/admin/entries/?entry=' . implode('/', array_slice(explode("/", Http::get('entry')), 0, -1)));
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
}
protected static function moveEntry() : void
{
$entry = Entries::fetch(Http::get('entry'));
$move_entry = Http::post('move_entry');
if (isset($move_entry)) {
if (Token::check((Http::post('token')))) {
if (!Entries::has(Http::post('parent_entry') . '/' . Http::post('name_current'))) {
if (Entries::rename(
Http::post('entry_path_current'),
Http::post('parent_entry') . '/' . Text::safeString(Http::post('name_current'), '-', true)
)) {
Notification::set('success', __('admin_message_entry_moved'));
} else {
Notification::set('error', __('admin_message_entry_was_not_moved'));
}
Http::redirect(Http::getBaseUrl() . '/admin/entries/?entry=' . Http::post('parent_entry'));
}
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
$_entries_list = Entries::fetchAll('', 'slug');
$entries_list['/'] = '/';
foreach ($_entries_list as $_entry) {
if ($_entry['slug'] != '') {
$entries_list[$_entry['slug']] = $_entry['slug'];
} else {
$entries_list[Registry::get('settings.entries.main')] = Registry::get('settings.entries.main');
}
}
Themes::view('admin/views/templates/content/entries/move')
->assign('entry_path_current', Http::get('entry'))
->assign('entries_list', $entries_list)
->assign('name_current', Arr::last(explode("/", Http::get('entry'))))
->assign('entry_parent', implode('/', array_slice(explode("/", Http::get('entry')), 0, -1)))
->assign('entry', $entry)
->display();
}
protected static function deleteEntry() : void
{
if (Http::get('entry') != '') {
if (Token::check((Http::get('token')))) {
if (Entries::delete(Http::get('entry'))) {
Notification::set('success', __('admin_message_entry_deleted'));
} else {
Notification::set('error', __('admin_message_entry_was_not_deleted'));
}
Http::redirect(Http::getBaseUrl() . '/admin/entries/?entry=' . Http::get('entry_current'));
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
}
protected static function renameEntry() : void
{
$entry = Entries::fetch(Http::get('entry'));
$rename_entry = Http::post('rename_entry');
if (isset($rename_entry)) {
if (Token::check((Http::post('token')))) {
if (!Entries::has(Http::post('name'))) {
if (Entries::rename(
Http::post('entry_path_current'),
Http::post('entry_parent') . '/' . Text::safeString(Http::post('name'), '-', true)
)) {
Notification::set('success', __('admin_message_entry_renamed'));
} else {
Notification::set('error', __('admin_message_entry_was_not_renamed'));
}
Http::redirect(Http::getBaseUrl() . '/admin/entries/?entry=' . Http::post('entry_parent'));
}
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
Themes::view('admin/views/templates/content/entries/rename')
->assign('name_current', Arr::last(explode("/", Http::get('entry'))))
->assign('entry_path_current', Http::get('entry'))
->assign('entry_parent', implode('/', array_slice(explode("/", Http::get('entry')), 0, -1)))
->assign('entry', $entry)
->display();
}
protected static function typeEntry() : void
{
$type_entry = Http::post('type_entry');
if (isset($type_entry)) {
if (Token::check((Http::post('token')))) {
$entry = Entries::fetch(Http::get('entry'));
Arr::delete($entry, 'slug');
$data = [];
$_data = $_POST;
Arr::delete($_data, 'token');
Arr::delete($_data, 'type_entry');
Arr::delete($_data, 'entry');
$data = array_merge($entry, $_data);
if (Entries::update(Http::get('entry'), $data)) {
Notification::set('success', __('admin_message_entry_changes_saved'));
} else {
Notification::set('success', __('admin_message_entry_was_not_moved'));
}
Http::redirect(Http::getBaseUrl() . '/admin/entries?entry=' . implode('/', array_slice(explode("/", Http::get('entry')), 0, -1)));
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
$entry = Entries::fetch(Http::get('entry'));
$fieldsets = [];
// Get fieldsets files
$_fieldsets = Filesystem::listContents(PATH['themes'] . '/' . Registry::get('settings.theme') . '/fieldsets/');
// If there is any template file then go...
if (count($_fieldsets) > 0) {
foreach ($_fieldsets as $fieldset) {
if ($fieldset['type'] == 'file' && $fieldset['extension'] == 'yaml') {
$fieldset_content = YamlParser::decode(Filesystem::read($fieldset['path']));
if (isset($fieldset_content['sections']) && isset($fieldset_content['sections']['main']) && isset($fieldset_content['sections']['main']['fields'])) {
$fieldsets[$fieldset['basename']] = $fieldset_content['title'];
}
}
}
}
Themes::view('admin/views/templates/content/entries/type')
->assign('fieldset', $entry['fieldset'])
->assign('fieldsets', $fieldsets)
->display();
}
protected static function addEntry() : void
{
$create_entry = Http::post('create_entry');
if (isset($create_entry)) {
if (Token::check((Http::post('token')))) {
// Set parent entry
if (Http::post('parent_entry')) {
$parent_entry = '/' . Http::post('parent_entry');
} else {
$parent_entry = '/';
}
// Set new entry name
$entry = $parent_entry . Text::safeString(Http::post('slug'), '-', true);
// Check if new entry exists
if (!Entries::has($entry)) {
// Get fieldset
$fieldset = YamlParser::decode(Filesystem::read(PATH['themes'] . '/' . Registry::get('settings.theme') . '/fieldsets/' . Http::post('fieldset') . '.yaml'));
// We need to check if template for current fieldset is exists
// if template is not exist then default template will be used!
$template_path = PATH['themes'] . '/' . Registry::get('settings.theme') . '/views/templates/' . Http::post('fieldset') . '.php';
if (Filesystem::has($template_path)) {
$template = Http::post('fieldset');
} else {
$template = 'default';
}
// Init entry data
$data = [];
$default_data = [];
// Define data values based on POST data
$default_data['title'] = Http::post('title');
$default_data['template'] = $template;
$default_data['fieldset'] = Http::post('fieldset');
$default_data['date'] = date(Registry::get('settings.date_format'), time());
// Predefine data values based on selected fieldset
foreach ($fieldset['sections'] as $section) {
foreach ($section as $key => $field) {
// Get values from default data
if (isset($default_data[$key])) {
$_value = $default_data[$key];
// Get values from fieldsets predefined field values
} elseif (isset($field['value'])) {
$_value = $field['value'];
// or set empty value
} else {
$_value = '';
}
$data[$key] = $_value;
}
}
// Merge data
$data = array_replace_recursive($data, $default_data);
// Create a new entry!
if (Entries::create($entry, $data)) {
Notification::set('success', __('admin_message_entry_created'));
} else {
Notification::set('success', __('admin_message_entry_was_not_created'));
}
Http::redirect(Http::getBaseUrl() . '/admin/entries/?entry=' . Http::post('parent_entry'));
}
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
$fieldsets = [];
// Get fieldsets files
$_fieldsets = Filesystem::listContents(PATH['themes'] . '/' . Registry::get('settings.theme') . '/fieldsets/');
// If there is any template file then go...
if (count($_fieldsets) > 0) {
foreach ($_fieldsets as $fieldset) {
if ($fieldset['type'] == 'file' && $fieldset['extension'] == 'yaml') {
$fieldset_content = YamlParser::decode(Filesystem::read($fieldset['path']));
if (isset($fieldset_content['sections']) && isset($fieldset_content['sections']['main']) && isset($fieldset_content['sections']['main']['fields'])) {
$fieldsets[$fieldset['basename']] = $fieldset_content['title'];
}
}
}
}
Themes::view('admin/views/templates/content/entries/add')
->assign('fieldsets', $fieldsets)
->assign('entries_list', Entries::fetchAll('', 'slug'))
->display();
}
/**
* Upload files on the Server with several type of Validations!
*
* Entries::uploadFile($_FILES['file'], $files_directory);
*
* @param array $file Uploaded file data
* @param string $upload_directory Upload directory
* @param string $allowed Allowed file extensions
* @param int $max_size Max file size in bytes
* @param string $filename New filename
* @param bool $remove_spaces Remove spaces from the filename
* @param int $max_width Maximum width of image
* @param int $max_height Maximum height of image
* @param bool $exact Match width and height exactly?
* @param int $chmod Chmod mask
* @return string on success, full path to new file
* @return false on failure
*/
public static function uploadFile(
array $file,
string $upload_directory,
string $allowed = 'jpeg, png, gif, jpg',
int $max_size = 3000000,
string $filename = null,
bool $remove_spaces = true,
int $max_width = null,
int $max_height = null,
bool $exact = false,
int $chmod = 0644
) {
//
// Tests if a successful upload has been made.
//
if (isset($file['error'])
and isset($file['tmp_name'])
and $file['error'] === UPLOAD_ERR_OK
and is_uploaded_file($file['tmp_name'])) {
//
// Tests if upload data is valid, even if no file was uploaded.
//
if (isset($file['error'])
and isset($file['name'])
and isset($file['type'])
and isset($file['tmp_name'])
and isset($file['size'])) {
//
// Test if an uploaded file is an allowed file type, by extension.
//
if (strpos($allowed, strtolower(pathinfo($file['name'], PATHINFO_EXTENSION))) !== false) {
//
// Validation rule to test if an uploaded file is allowed by file size.
//
if (($file['error'] != UPLOAD_ERR_INI_SIZE)
and ($file['error'] == UPLOAD_ERR_OK)
and ($file['size'] <= $max_size)) {
//
// Validation rule to test if an upload is an image and, optionally, is the correct size.
//
if (in_array(mime_content_type($file['tmp_name']), ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'])) {
function validateImage($file, $max_width, $max_height, $exact)
{
try {
// Get the width and height from the uploaded image
list($width, $height) = getimagesize($file['tmp_name']);
} catch (ErrorException $e) {
// Ignore read errors
}
if (empty($width) or empty($height)) {
// Cannot get image size, cannot validate
return false;
}
if (!$max_width) {
// No limit, use the image width
$max_width = $width;
}
if (!$max_height) {
// No limit, use the image height
$max_height = $height;
}
if ($exact) {
// Check if dimensions match exactly
return ($width === $max_width and $height === $max_height);
} else {
// Check if size is within maximum dimensions
return ($width <= $max_width and $height <= $max_height);
}
return false;
}
if (validateImage($file, $max_width, $max_height, $exact) === false) {
return false;
}
}
if (!isset($file['tmp_name']) or !is_uploaded_file($file['tmp_name'])) {
// Ignore corrupted uploads
return false;
}
if ($filename === null) {
// Use the default filename
$filename = $file['name'];
}
if ($remove_spaces === true) {
// Remove spaces from the filename
$filename = Text::safeString(pathinfo($filename)['filename'], '-', true) . '.' . pathinfo($filename)['extension'];
}
if (!is_dir($upload_directory) or !is_writable(realpath($upload_directory))) {
throw new \RuntimeException("Directory {$upload_directory} must be writable");
}
// Make the filename into a complete path
$filename = realpath($upload_directory) . DIRECTORY_SEPARATOR . $filename;
if (move_uploaded_file($file['tmp_name'], $filename)) {
// Set permissions on filename
chmod($filename, $chmod);
// Return new file path
return $filename;
}
}
}
}
}
return false;
}
}

View File

@@ -1,290 +0,0 @@
<?php
namespace Flextype;
use Flextype\Component\Registry\Registry;
use Flextype\Component\Http\Http;
use Flextype\Component\Filesystem\Filesystem;
use Flextype\Component\Token\Token;
use Flextype\Component\Text\Text;
use Flextype\Component\Notification\Notification;
use Flextype\Component\Form\Form;
use Flextype\Component\Arr\Arr;
use function Flextype\Component\I18n\__;
class FieldsetsManager
{
public static function getFieldsetsManager()
{
Registry::set('sidebar_menu_item', 'fieldsets');
switch (Http::getUriSegment(2)) {
case 'add':
FieldsetsManager::addFieldsets();
break;
case 'delete':
FieldsetsManager::deleteFieldsets();
break;
case 'rename':
FieldsetsManager::renameFieldsets();
break;
case 'duplicate':
FieldsetsManager::duplicateFieldsets();
break;
case 'edit':
FieldsetsManager::editFieldsets();
break;
default:
FieldsetsManager::listFieldsets();
break;
}
}
/**
* Fetch Fieldset form
*
* @access public
* @param array $fieldset Fieldset
* @param string $values Fieldset values
* @return string Returns form based on fieldsets
*/
public static function fetchForm(array $fieldset, array $values = []) : string
{
$form = '';
$form .= Form::open(null, ['id' => 'form']);
$form .= Form::hidden('token', Token::generate());
$form .= Form::hidden('action', 'save-form');
if (count($fieldset['sections']) > 0) {
$form .= '<ul class="nav nav-pills nav-justified" id="pills-tab" role="tablist">';
foreach ($fieldset['sections'] as $key => $section) {
$form .= '<li class="nav-item">
<a class="nav-link '.(($key == 'main') ? 'active' : '').'" id="pills-'.$key.'-tab" data-toggle="pill" href="#pills-'.$key.'" role="tab" aria-controls="pills-'.$key.'" aria-selected="true">'.$section['title'].'</a>
</li>';
}
$form .= '</ul>';
$form .= '<div class="tab-content" id="pills-tabContent">';
foreach ($fieldset['sections'] as $key => $section) {
$form .= '<div class="tab-pane fade show ' . (($key == 'main') ? 'active' : '') . '" id="pills-' . $key . '" role="tabpanel" aria-labelledby="pills-' . $key . '-tab">';
$form .= '<div class="row">';
foreach ($section['fields'] as $element => $property) {
// Create attributes
$property['attributes'] = Arr::keyExists($property, 'attributes') ? $property['attributes'] : [];
// Create attribute class
$property['attributes']['class'] = Arr::keyExists($property, 'attributes.class') ? 'form-control ' . $property['attributes']['class'] : 'form-control';
// Create attribute size
$property['size'] = Arr::keyExists($property, 'size') ? $property['size'] : 'col-12';
// Create attribute value
$property['value'] = Arr::keyExists($property, 'value') ? $property['value'] : '';
$pos = strpos($element, '.');
if ($pos === false) {
$form_element_name = $element;
} else {
$form_element_name = str_replace(".", "][", "$element") . ']';
}
$pos = strpos($form_element_name, ']');
if ($pos !== false) {
$form_element_name = substr_replace($form_element_name, '', $pos, strlen(']'));
}
// Form value
$form_value = Arr::keyExists($values, $element) ? Arr::get($values, $element) : $property['value'];
// Form label
$form_label = Form::label($element, __($property['title']));
// Form elements
switch ($property['type']) {
// Simple text-input, for multi-line fields.
case 'textarea':
$form_element = Form::textarea($element, $form_value, $property['attributes']);
break;
// The hidden field is like the text field, except it's hidden from the content editor.
case 'hidden':
$form_element = Form::hidden($element, $form_value);
break;
// A WYSIWYG HTML field.
case 'html':
$property['attributes']['class'] .= ' js-html-editor';
$form_element = Form::textarea($element, $form_value, $property['attributes']);
break;
// Selectbox field
case 'select':
$form_element = Form::select($form_element_name, $property['options'], $form_value, $property['attributes']);
break;
// Template select field for selecting entry template
case 'template_select':
$form_element = Form::select($form_element_name, Themes::getTemplates(), $form_value, $property['attributes']);
break;
// Visibility select field for selecting entry visibility state
case 'visibility_select':
$form_element = Form::select($form_element_name, ['draft' => __('admin_entries_draft'), 'visible' => __('admin_entries_visible'), 'hidden' => __('admin_entries_hidden')], (!empty($form_value) ? $form_value : 'visible'), $property['attributes']);
break;
// Media select field
case 'media_select':
$form_element = Form::select($form_element_name, EntriesManager::getMediaList(Http::get('entry'), false), $form_value, $property['attributes']);
break;
// Simple text-input, for single-line fields.
default:
$form_element = Form::input($form_element_name, $form_value, $property['attributes']);
break;
}
// Render form elments with labels
if ($property['type'] == 'hidden') {
$form .= $form_element;
} else {
$form .= '<div class="form-group ' . $property['size'] . '">';
$form .= $form_label . $form_element;
$form .= '</div>';
}
}
$form .= '</div>';
$form .= '</div>';
}
$form .= '</div>';
}
$form .= Form::close();
return $form;
}
protected static function addFieldsets()
{
$create_fieldset = Http::post('create_fieldset');
if (isset($create_fieldset)) {
if (Token::check((Http::post('token')))) {
$file = PATH['themes'] . '/' . Registry::get('settings.theme') . '/fieldsets/' . Text::safeString(Http::post('name'), '-', true) . '.yaml';
if (!Filesystem::has($file)) {
// Create a fieldset!
if (Filesystem::write(
$file,
YamlParser::encode(['title' => Http::post('title')])
)) {
Notification::set('success', __('admin_message_fieldset_created'));
Http::redirect(Http::getBaseUrl() . '/admin/fieldsets');
}
}
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
Themes::view('admin/views/templates/extends/fieldsets/add')
->display();
}
protected static function renameFieldsets()
{
$rename_fieldset = Http::post('rename_fieldset');
if (isset($rename_fieldset)) {
if (Token::check((Http::post('token')))) {
if (!Filesystem::has(PATH['themes'] . '/' . Registry::get('settings.theme') . '/fieldsets/' . Http::post('name') . '.yaml')) {
if (rename(
PATH['themes'] . '/' . Registry::get('settings.theme') . '/fieldsets/' . Http::post('name_current') . '.yaml',
PATH['themes'] . '/' . Registry::get('settings.theme') . '/fieldsets/' . Http::post('name') . '.yaml')
) {
Notification::set('success', __('admin_message_fieldset_renamed'));
Http::redirect(Http::getBaseUrl() . '/admin/fieldsets');
}
}
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
Themes::view('admin/views/templates/extends/fieldsets/rename')
->assign('name_current', Http::get('fieldset'))
->display();
}
protected static function duplicateFieldsets()
{
if (Http::get('fieldset') != '') {
if (Token::check((Http::get('token')))) {
Filesystem::copy(PATH['themes'] . '/' . Registry::get('settings.theme') . '/fieldsets/' . Http::get('fieldset') . '.yaml',
PATH['themes'] . '/' . Registry::get('settings.theme') . '/fieldsets/' . Http::get('fieldset') . '-duplicate-' . date("Ymd_His") . '.yaml');
Notification::set('success', __('admin_message_fieldset_duplicated'));
Http::redirect(Http::getBaseUrl() . '/admin/fieldsets');
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
}
protected static function deleteFieldsets()
{
if (Http::get('fieldset') != '') {
if (Token::check((Http::get('token')))) {
Filesystem::delete(PATH['themes'] . '/' . Registry::get('settings.theme') . '/fieldsets/' . Http::get('fieldset') . '.yaml');
Notification::set('success', __('admin_message_fieldset_deleted'));
Http::redirect(Http::getBaseUrl() . '/admin/fieldsets');
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
}
protected static function editFieldsets()
{
$action = Http::post('action');
if (isset($action) && $action == 'save-form') {
if (Token::check((Http::post('token')))) {
// Save a fieldset!
if (Filesystem::write(
PATH['themes'] . '/' . Registry::get('settings.theme') . '/fieldsets/' . Http::post('name') . '.yaml',
Http::post('fieldset')
)) {
Notification::set('success', __('admin_message_fieldset_saved'));
Http::redirect(Http::getBaseUrl() . '/admin/fieldsets/edit?fieldset=' . Http::post('name'));
}
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
Themes::view('admin/views/templates/extends/fieldsets/edit')
->assign('fieldset', Filesystem::read(PATH['themes'] . '/' . Registry::get('settings.theme') . '/fieldsets/' . Http::get('fieldset') . '.yaml'))
->display();
}
protected static function listFieldsets()
{
Themes::view('admin/views/templates/extends/fieldsets/list')
->assign('fieldsets_list', Fieldsets::fetchList())
->display();
}
}

View File

@@ -1,65 +0,0 @@
<?php
namespace Flextype;
use Flextype\Component\Registry\Registry;
use function Flextype\Component\I18n\__;
use Slim\Http\Request;
use Slim\Http\Response;
use Psr\Container\ContainerInterface;
$app->get('/admin/information', InformationController::class . ':index')->setName('admin.information');
class InformationController {
protected $container;
// constructor receives container instance
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function index()
{
if (function_exists('apache_get_modules')) {
if (!in_array('mod_rewrite', apache_get_modules())) {
$apache_mod_rewrite_installed = false;
} else {
$apache_mod_rewrite_installed = true;
}
} else {
$apache_mod_rewrite_installed = true;
}
if (!function_exists('password_hash')) {
$password_hash_installed = false;
} else {
$password_hash_installed = true;
}
if (!function_exists('password_verify')) {
$password_verify_installed = false;
} else {
$password_verify_installed = true;
}
return $this->view->render($response,
'plugins/admin/views/templates/system/information/index.html', [
'menu_item' => 'information',
'php_uname' => php_uname(),
'webserver' => isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : @getenv('SERVER_SOFTWARE'),
'php_sapi_name' => php_sapi_name(),
'apache_mod_rewrite_installed' => $apache_mod_rewrite_installed,
'password_verify_installed' => $password_verify_installed,
'password_hash_installed' => $password_hash_installed,
'links' => [
'information' => [
'link' => '/admin/information',
'title' => __('admin_information'),
'attributes' => ['class' => 'navbar-item active']
],
]
]);
}
}

View File

@@ -1,33 +0,0 @@
<?php
namespace Flextype;
use Flextype\Component\Arr\Arr;
use Flextype\Component\Http\Http;
use Flextype\Component\Event\Event;
use Flextype\Component\Filesystem\Filesystem;
use Flextype\Component\Registry\Registry;
use Flextype\Component\Token\Token;
use Slim\Http\Request;
use Slim\Http\Response;
$app->get('/admin/plugins', function (Request $request, Response $response, array $args) {
return $this->view->render($response,
'plugins/admin/views/templates/extends/plugins/index.html', [
'plugins_list' => $this->get('registry')->get('plugins'),
'menu_item' => 'plugins'
]);
})->setName('plugins');
$app->post('/admin/plugins/change_status', function (Request $request, Response $response, array $args) {
$data = $request->getParsedBody();
$plugin_settings = YamlParser::decode(Filesystem::read(PATH['plugins'] . '/' . $data['plugin'] . '/' . 'settings.yaml'));
Arr::set($plugin_settings, 'enabled', ($data['status'] == 'true' ? true : false));
Filesystem::write(PATH['plugins'] . '/' . $data['plugin'] . '/' . 'settings.yaml', YamlParser::encode($plugin_settings));
$this->get('cache')->clear();
})->setName('plugins-change-status');

View File

@@ -1,141 +0,0 @@
<?php
namespace Flextype;
use Flextype\Component\Arr\Arr;
use Flextype\Component\Http\Http;
use Flextype\Component\Filesystem\Filesystem;
use Flextype\Component\Registry\Registry;
use Flextype\Component\Token\Token;
use Flextype\Component\Date\Date;
use Flextype\Component\Notification\Notification;
use function Flextype\Component\I18n\__;
use Slim\Http\Request;
use Slim\Http\Response;
$app->get('/admin/settings', function (Request $request, Response $response, array $args) {
$entries = [];
foreach ($this->entries->fetchAll('', 'date', 'DESC') as $entry) {
$entries[$entry['slug']] = $entry['title'];
}
$themes = [];
foreach (Filesystem::listContents(PATH['themes']) as $theme) {
if ($theme['type'] == 'dir' && Filesystem::has($theme['path'] . '/' . $theme['dirname'] . '.yaml')) {
$themes[$theme['dirname']] = $theme['dirname'];
}
}
$available_locales = Filesystem::listContents(PATH['plugins'] . '/admin/languages/');
$system_locales = $this->plugins->getLocales();
$locales = [];
foreach ($available_locales as $locale) {
if ($locale['type'] == 'file' && $locale['extension'] == 'yaml') {
$locales[$locale['basename']] = $system_locales[$locale['basename']]['nativeName'];
}
}
$cache_driver = ['auto' => 'Auto Detect',
'file' => 'File',
'apcu' => 'APCu',
'wincache' => 'WinCache',
'memcached' => 'Memcached',
'redis' => 'Redis',
'sqlite3' => 'SQLite3',
'zend' => 'Zend',
'array' => 'Array'];
return $this->view->render($response,
'plugins/admin/views/templates/system/settings/index.html', [
'timezones' => Date::timezones(),
'settings' => $this->registry->get('settings'),
'cache_driver' => $cache_driver,
'locales' => $locales,
'entries' => $entries,
'themes' => $themes,
'links' => [
'settings' => [
'link' => '/admin/settings',
'title' => __('admin_settings'),
'attributes' => ['class' => 'navbar-item active']
]
],
'buttons' => [
'save' => [
'link' => 'javascript:;',
'title' => __('admin_save'),
'attributes' => ['class' => 'js-save-form-submit float-right btn']
],
'settings_clear_cache' => [
'link' => '/admin/settings?clear_cache=1&token=' . Token::generate(),
'title' => __('admin_clear_cache'),
'attributes' => ['class' => 'float-right btn']
]
]
]);
})->setName('information');
class SettingsManager
{
public static function getSettingsManager()
{
Registry::set('sidebar_menu_item', 'settings');
SettingsManager::clearCache();
SettingsManager::saveSettings();
Themes::view('admin/views/templates/system/settings/list')
->assign('settings', Registry::get('settings'))
->assign('cache_driver', SettingsManager::cacheDriverList())
->assign('locales', SettingsManager::localesList())
->assign('entries', SettingsManager::entriesList())
->assign('themes', SettingsManager::themesList())
->display();
}
private static function saveSettings()
{
if (Http::post('action') !== null && Http::post('action') == 'save-form' && Http::post('token') !== null) {
if (Token::check((Http::post('token')))) {
$settings = $_POST;
Arr::delete($settings, 'token');
Arr::delete($settings, 'action');
Arr::set($settings, 'errors.display', (Http::post('errors.display') == '1' ? true : false));
Arr::set($settings, 'cache.enabled', (Http::post('cache.enabled') == '1' ? true : false));
Arr::set($settings, 'cache.lifetime', (int) Http::post('cache.lifetime'));
Arr::set($settings, 'entries.media.upload_images_quality', (int) Http::post('entries.media.upload_images_quality'));
Arr::set($settings, 'entries.media.upload_images_width', (int) Http::post('entries.media.upload_images_width'));
Arr::set($settings, 'entries.media.upload_images_height', (int) Http::post('entries.media.upload_images_height'));
if (Filesystem::write(PATH['config']['site'] . '/settings.yaml', YamlParser::encode(array_merge(Registry::get('settings'), $settings)))) {
Notification::set('success', __('admin_message_settings_saved'));
} else {
Notification::set('error', __('admin_message_settings_was_not_saved'));
}
Http::redirect(Http::getBaseUrl() . '/admin/settings');
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
}
private static function clearCache()
{
// Clear cache
if (Http::get('clear_cache') !== null && Http::get('clear_cache') == '1' && Http::get('token') !== null) {
if (Token::check((Http::get('token')))) {
Cache::clear();
Notification::set('success', __('admin_message_cache_files_deleted'));
Http::redirect(Http::getBaseUrl() . '/admin/settings');
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
}
}

View File

@@ -1,178 +0,0 @@
<?php
namespace Flextype;
use Flextype\Component\Registry\Registry;
use Flextype\Component\Http\Http;
use Flextype\Component\Filesystem\Filesystem;
use Flextype\Component\Token\Token;
use Flextype\Component\Text\Text;
use Flextype\Component\Notification\Notification;
use function Flextype\Component\I18n\__;
class SnippetsManager
{
public static function getSnippetsManager()
{
Registry::set('sidebar_menu_item', 'snippets');
// Create directory for snippets
!Filesystem::has(PATH['snippets']) and Filesystem::createDir(PATH['snippets']);
switch (Http::getUriSegment(2)) {
case 'add':
SnippetsManager::addSnippet();
break;
case 'delete':
SnippetsManager::deleteSnippet();
break;
case 'rename':
SnippetsManager::renameSnippet();
break;
case 'duplicate':
SnippetsManager::duplicateSnippet();
break;
case 'edit':
SnippetsManager::editSnippet();
break;
default:
SnippetsManager::listSnippet();
break;
}
}
private static function editSnippet()
{
$action = Http::post('action');
if (isset($action) && $action == 'save-form') {
if (Token::check((Http::post('token')))) {
// Save a snippet!
if (Snippets::update(
Http::post('name'),
Http::post('snippet')
)) {
Notification::set('success', __('admin_message_snippet_saved'));
} else {
Notification::set('error', __('admin_message_snippet_was_not_saved'));
}
Http::redirect(Http::getBaseUrl() . '/admin/snippets/edit?snippet=' . Http::post('name'));
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
Themes::view('admin/views/templates/extends/snippets/edit')
->assign('snippet', Filesystem::read(PATH['snippets'] . '/' . Http::get('snippet') . '.php'))
->display();
}
private static function listSnippet()
{
$snippets = [];
foreach (Filesystem::listContents(PATH['snippets']) as $snippet) {
if ($snippet['type'] == 'file' && $snippet['extension'] == 'php') {
$snippets[$snippet['basename']] = $snippet['basename'];
}
}
Themes::view('admin/views/templates/extends/snippets/list')
->assign('snippets_list', $snippets)
->display();
}
private static function duplicateSnippet()
{
if (Http::get('snippet') != '') {
if (Token::check((Http::get('token')))) {
if (Snippets::copy(Http::get('snippet'),
Http::get('snippet') . '-duplicate-' . date("Ymd_His"))) {
Notification::set('success', __('admin_message_snippet_duplicated'));
} else {
Notification::set('error', __('admin_message_snippet_was_not_duplicated'));
}
Http::redirect(Http::getBaseUrl() . '/admin/snippets');
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
}
private static function renameSnippet()
{
$rename_snippet = Http::post('rename_snippet');
if (isset($rename_snippet)) {
if (Token::check((Http::post('token')))) {
if (!Snippets::has(Http::post('name'))) {
if (Snippets::rename(
Http::post('name_current'),
Http::post('name'))
) {
Notification::set('success', __('admin_message_snippet_renamed'));
} else {
Notification::set('error', __('admin_message_snippet_was_not_renamed'));
}
Http::redirect(Http::getBaseUrl() . '/admin/snippets');
}
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
Themes::view('admin/views/templates/extends/snippets/rename')
->assign('name_current', Http::get('snippet'))
->display();
}
private static function deleteSnippet()
{
if (Http::get('snippet') != '') {
if (Token::check((Http::get('token')))) {
if (Snippets::delete(Http::get('snippet'))) {
Notification::set('success', __('admin_message_snippet_deleted'));
} else {
Notification::set('error', __('admin_message_snippet_was_not_deleted'));
}
Http::redirect(Http::getBaseUrl() . '/admin/snippets');
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
}
private static function addSnippet()
{
$create_snippet = Http::post('create_snippet');
if (isset($create_snippet)) {
if (Token::check((Http::post('token')))) {
$snippet_name = Text::safeString(Http::post('name'), '-', true);
if (!Snippets::has($snippet_name)) {
// Create a snippet!
if (Snippets::create($snippet_name)) {
Notification::set('success', __('admin_message_snippet_created'));
} else {
Notification::set('error', __('admin_message_snippet_was_not_created'));
}
Http::redirect(Http::getBaseUrl() . '/admin/snippets');
}
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
Themes::view('admin/views/templates/extends/snippets/add')
->display();
}
}

View File

@@ -1,136 +0,0 @@
<?php
namespace Flextype;
use Flextype\Component\Registry\Registry;
use Flextype\Component\Http\Http;
use Flextype\Component\Filesystem\Filesystem;
use Flextype\Component\Token\Token;
use Flextype\Component\Text\Text;
use Flextype\Component\Notification\Notification;
use function Flextype\Component\I18n\__;
class TemplatesManager
{
public static function getTemplatesManager()
{
Registry::set('sidebar_menu_item', 'templates');
switch (Http::getUriSegment(2)) {
case 'add':
$create_template = Http::post('create_template');
if (isset($create_template)) {
if (Token::check((Http::post('token')))) {
$type = (Http::post('type') && Http::post('type') == 'partial') ? 'partial' : 'template';
$file = PATH['themes'] . '/' . Registry::get('settings.theme') . '/views/' . $type . 's' . '/' . Text::safeString(Http::post('name'), '-', true) . '.php';
if (!Filesystem::has($file)) {
// Create a template!
if (Filesystem::write(
$file,
""
)) {
Notification::set('success', __('admin_message_template_created'));
Http::redirect(Http::getBaseUrl() . '/admin/templates');
}
}
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
Themes::view('admin/views/templates/extends/templates/add')
->display();
break;
case 'delete':
if (Http::get('template') != '') {
if (Token::check((Http::get('token')))) {
$type = (Http::get('type') && Http::get('type') == 'partial') ? 'partial' : 'template';
Filesystem::delete(PATH['themes'] . '/' . Registry::get('settings.theme') . '/views/' . $type . 's' . '/' . Http::get('template') . '.php');
Notification::set('success', __('admin_message_template_deleted'));
Http::redirect(Http::getBaseUrl() . '/admin/templates');
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
break;
case 'rename':
$rename_template = Http::post('rename_template');
if (isset($rename_template)) {
if (Token::check((Http::post('token')))) {
$type = (Http::post('type') && Http::post('type') == 'partial') ? 'partial' : 'template';
$type_current = (Http::post('type_current') && Http::post('type_current') == 'partial') ? 'partial' : 'template';
if (!Filesystem::has(PATH['themes'] . '/' . Registry::get('settings.theme') . '/views/' . $type . 's' . '/' . Http::post('name') . '.php')) {
if (rename(
PATH['themes'] . '/' . Registry::get('settings.theme') . '/views/' . $type_current . 's' . '/' . Http::post('name_current') . '.php',
PATH['themes'] . '/' . Registry::get('settings.theme') . '/views/' . $type . 's' . '/' . Http::post('name') . '.php')
) {
Notification::set('success', __('admin_message_template_renamed'));
Http::redirect(Http::getBaseUrl() . '/admin/templates');
}
}
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
Themes::view('admin/views/templates/extends/templates/rename')
->assign('name_current', Http::get('template'))
->assign('type', ((Http::get('type') && Http::get('type') == 'partial') ? 'partial' : 'template'))
->display();
break;
case 'duplicate':
if (Http::get('template') != '') {
if (Token::check((Http::get('token')))) {
$type = (Http::get('type') && Http::get('type') == 'partial') ? 'partial' : 'template';
Filesystem::copy(PATH['themes'] . '/' . Registry::get('settings.theme') . '/views/' . $type . 's' . '/' . Http::get('template') . '.php',
PATH['themes'] . '/' . Registry::get('settings.theme') . '/views/' . $type . 's' . '/' . Http::get('template') . '-duplicate-' . date("Ymd_His") . '.php');
Notification::set('success', __('admin_message_template_duplicated'));
Http::redirect(Http::getBaseUrl() . '/admin/templates');
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
break;
case 'edit':
$action = Http::post('action');
if (isset($action) && $action == 'save-form') {
if (Token::check((Http::post('token')))) {
$type = (Http::post('type') && Http::post('type') == 'partial') ? 'partial' : 'template';
// Save a template!
if (Filesystem::write(
PATH['themes'] . '/' . Registry::get('settings.theme') . '/views/' . $type . 's' . '/' . Http::post('name') . '.php',
Http::post('template')
)) {
Notification::set('success', __('admin_message_template_saved'));
Http::redirect(Http::getBaseUrl() . '/admin/templates/edit?template=' . Http::post('name') . '&type=' . $type);
}
} else {
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
}
}
$type = (Http::get('type') && Http::get('type') == 'partial') ? 'partials' : 'templates';
Themes::view('admin/views/templates/extends/templates/edit')
->assign('template', Filesystem::read(PATH['themes'] . '/' . Registry::get('settings.theme') . '/views/' . $type . '/' . Http::get('template') . '.php'))
->assign('type', ((Http::get('type') && Http::get('type') == 'partial') ? 'partial' : 'template'))
->display();
break;
default:
Themes::view('admin/views/templates/extends/templates/list')
->assign('templates_list', Themes::getTemplates())
->assign('partials_list', Themes::getPartials())
->display();
break;
}
}
}

View File

@@ -1,128 +0,0 @@
<?php
namespace Flextype;
use Flextype\Component\Filesystem\Filesystem;
use Flextype\Component\Session\Session;
use Flextype\Component\Registry\Registry;
use Flextype\Component\Text\Text;
use function Flextype\Component\I18n\__;
use Slim\Http\Request;
use Slim\Http\Response;
use Psr\Container\ContainerInterface;
$app->get('/admin/login', UsersController::class . ':login')->setName('admin.login');
$app->get('/admin/profile', UsersController::class . ':profile')->setName('admin.profile');
$app->get('/admin/logout', UsersController::class . ':processLogoutForm')->setName('admin.logout');
$app->get('/admin/registration', UsersController::class . ':registration')->setName('admin.registration');
$app->post('/admin/registration', UsersController::class . ':processRegistrationForm');
$app->post('/admin/login', UsersController::class . ':processLoginForm');
class UsersController {
protected $container;
// constructor receives container instance
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function login($request, $response, $args)
{
if (!Users::isLoggedIn()) {
return $this->container->get('view')->render($response,
'plugins/admin/views/templates/users/login.html', [
'user_is_logged' => Users::isLoggedIn()
]);
} else {
return $response->withRedirect($this->container->get('router')->urlFor('admin.registration'));
}
}
public function profile($request, $response, $args)
{
if (Users::isLoggedIn()) {
return $this->container->get('view')->render($response,
'plugins/admin/views/templates/users/profile.html', [
'username' => Session::get('username'),
'rolename' => Session::get('role'),
'sidebar_menu_item' => 'profile',
'user_is_logged' => 'user_is_logged!'
]);
} else {
return $response->withRedirect($this->container->get('router')->urlFor('admin.login'));
}
}
public function processLoginForm($request, $response, $args)
{
if (Filesystem::has($_user_file = PATH['site'] . '/accounts/' . $data['username'] . '.yaml')) {
$user_file = YamlParser::decode(Filesystem::read($_user_file));
if (password_verify(trim($data['password']), $user_file['hashed_password'])) {
Session::set('username', $user_file['username']);
Session::set('role', $user_file['role']);
return $response->withRedirect('admin/entries');
} else {
//Notification::set('error', __('admin_message_wrong_username_password'));
}
} else {
//Notification::set('error', __('admin_message_wrong_username_password'));
}
}
public function processLogoutForm($request, $response, $args)
{
Session::destroy();
return $response->withRedirect('/admin');
}
public function registration($request, $response, $args)
{
if (!Users::isLoggedIn()) {
return $this->view->render($response,
'plugins/admin/views/templates/users/registration.html');
} else {
return $response->withRedirect($this->container->get('router')->urlFor('admin.login'));
}
}
public function processRegistrationForm($request, $response, $args)
{
if (!Filesystem::has($_user_file = PATH['site'] . '/accounts/' . Text::safeString($data['username']) . '.yaml')) {
if (Filesystem::write(
PATH['site'] . '/accounts/' . $data['username'] . '.yaml',
YamlParser::encode(['username' => Text::safeString($data['username']),
'hashed_password' => password_hash($data['password'], PASSWORD_BCRYPT),
'email' => $data['email'],
'role' => 'admin',
'state' => 'enabled']))) {
return $response->withRedirect('admin/entries');
} else {
//return false;
}
} else {
//return false;
}
}
}
class Users
{
public static function isUsersExists() : bool
{
// Get Users Profiles
$users = Filesystem::listContents(PATH['site'] . '/accounts/');
// If any users exists then return true
return ($users && count($users) > 0) ? true : false;
}
public static function isLoggedIn() : bool
{
return (Session::exists('role') && Session::get('role') == 'admin') ? true : false;
}
}