mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2025-10-30 01:36:27 +01:00
Merge branch 'develop' into feature/new-folder-modal
This commit is contained in:
@@ -8,7 +8,7 @@ use Grav\Common\GPM\Installer;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Uri;
|
||||
use Grav\Common\Data;
|
||||
use Grav\Common\Page;
|
||||
use Grav\Common\Page\Page;
|
||||
use Grav\Common\Page\Pages;
|
||||
use Grav\Common\Page\Collection;
|
||||
use Grav\Common\Plugin;
|
||||
@@ -16,10 +16,10 @@ use Grav\Common\Theme;
|
||||
use Grav\Common\User\User;
|
||||
use Grav\Common\Utils;
|
||||
use Grav\Common\Backup\ZipBackup;
|
||||
use Grav\Common\Markdown\Parsedown;
|
||||
use Grav\Common\Markdown\ParsedownExtra;
|
||||
use RocketTheme\Toolbox\Event\Event;
|
||||
use RocketTheme\Toolbox\File\File;
|
||||
use RocketTheme\Toolbox\File\JsonFile;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
class AdminController
|
||||
@@ -380,7 +380,7 @@ class AdminController
|
||||
protected function taskClearCache()
|
||||
{
|
||||
if (!$this->authorizeTask('clear cache', ['admin.cache', 'admin.super'])) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
// get optional cleartype param
|
||||
@@ -411,7 +411,7 @@ class AdminController
|
||||
{
|
||||
$param_sep = $this->grav['config']->get('system.param_sep', ':');
|
||||
if (!$this->authorizeTask('backup', ['admin.maintenance', 'admin.super'])) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
$download = $this->grav['uri']->param('download');
|
||||
@@ -562,7 +562,7 @@ class AdminController
|
||||
protected function taskListmedia()
|
||||
{
|
||||
if (!$this->authorizeTask('list media', ['admin.pages', 'admin.super'])) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
$page = $this->admin->page(true);
|
||||
@@ -583,11 +583,13 @@ class AdminController
|
||||
|
||||
/**
|
||||
* Handles adding a media file to a page
|
||||
*
|
||||
* @return bool True if the action was performed.
|
||||
*/
|
||||
protected function taskAddmedia()
|
||||
{
|
||||
if (!$this->authorizeTask('add media', ['admin.pages', 'admin.super'])) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
$page = $this->admin->page(true);
|
||||
@@ -597,7 +599,7 @@ class AdminController
|
||||
|
||||
if (!isset($_FILES['file']['error']) || is_array($_FILES['file']['error'])) {
|
||||
$this->admin->json_response = ['status' => 'error', 'message' => $this->admin->translate('PLUGIN_ADMIN.INVALID_PARAMETERS')];
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check $_FILES['file']['error'] value.
|
||||
@@ -606,44 +608,47 @@ class AdminController
|
||||
break;
|
||||
case UPLOAD_ERR_NO_FILE:
|
||||
$this->admin->json_response = ['status' => 'error', 'message' => $this->admin->translate('PLUGIN_ADMIN.NO_FILES_SENT')];
|
||||
return;
|
||||
return false;
|
||||
case UPLOAD_ERR_INI_SIZE:
|
||||
case UPLOAD_ERR_FORM_SIZE:
|
||||
$this->admin->json_response = ['status' => 'error', 'message' => $this->admin->translate('PLUGIN_ADMIN.EXCEEDED_FILESIZE_LIMIT')];
|
||||
return;
|
||||
return false;
|
||||
default:
|
||||
$this->admin->json_response = ['status' => 'error', 'message' => $this->admin->translate('PLUGIN_ADMIN.UNKNOWN_ERRORS')];
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
$grav_limit = $config->get('system.media.upload_limit', 0);
|
||||
// You should also check filesize here.
|
||||
if ($grav_limit > 0 && $_FILES['file']['size'] > $grav_limit) {
|
||||
$this->admin->json_response = ['status' => 'error', 'message' => $this->admin->translate('PLUGIN_ADMIN.EXCEEDED_GRAV_FILESIZE_LIMIT')];
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Check extension
|
||||
$fileParts = pathinfo($_FILES['file']['name']);
|
||||
$fileExt = strtolower($fileParts['extension']);
|
||||
|
||||
// If not a supported type, return
|
||||
if (!$config->get("media.{$fileExt}")) {
|
||||
$this->admin->json_response = ['status' => 'error', 'message' => $this->admin->translate('PLUGIN_ADMIN.UNSUPPORTED_FILE_TYPE') . ': '.$fileExt];
|
||||
return;
|
||||
$fileExt = '';
|
||||
if (isset($fileParts['extension'])) {
|
||||
$fileExt = strtolower($fileParts['extension']);
|
||||
}
|
||||
|
||||
// If not a supported type, return
|
||||
if (!$fileExt || !$config->get("media.{$fileExt}")) {
|
||||
$this->admin->json_response = ['status' => 'error', 'message' => $this->admin->translate('PLUGIN_ADMIN.UNSUPPORTED_FILE_TYPE') . ': '.$fileExt];
|
||||
return false;
|
||||
}
|
||||
|
||||
// Upload it
|
||||
if (!move_uploaded_file($_FILES['file']['tmp_name'], sprintf('%s/%s', $page->path(), $_FILES['file']['name']))) {
|
||||
$this->admin->json_response = ['status' => 'error', 'message' => $this->admin->translate('PLUGIN_ADMIN.FAILED_TO_MOVE_UPLOADED_FILE')];
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->admin->json_response = ['status' => 'success', 'message' => $this->admin->translate('PLUGIN_ADMIN.FILE_UPLOADED_SUCCESSFULLY')];
|
||||
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -654,7 +659,7 @@ class AdminController
|
||||
protected function taskDelmedia()
|
||||
{
|
||||
if (!$this->authorizeTask('delete media', ['admin.pages', 'admin.super'])) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
$page = $this->admin->page(true);
|
||||
@@ -666,7 +671,7 @@ class AdminController
|
||||
|
||||
$filename = !empty($this->post['filename']) ? $this->post['filename'] : null;
|
||||
if ($filename) {
|
||||
$targetPath = $page->path().'/'.$filename;
|
||||
$targetPath = $page->path() . '/' . $filename;
|
||||
|
||||
if (file_exists($targetPath)) {
|
||||
if (unlink($targetPath)) {
|
||||
@@ -677,18 +682,20 @@ class AdminController
|
||||
} else {
|
||||
//Try with responsive images @1x, @2x, @3x
|
||||
$ext = pathinfo($targetPath, PATHINFO_EXTENSION);
|
||||
$filename = $page->path() . '/'. basename($targetPath, ".$ext");
|
||||
$responsiveTargetPath = $filename . '@1x.' . $ext;
|
||||
$fullPathFilename = $page->path() . '/'. basename($targetPath, ".$ext");
|
||||
$responsiveTargetPath = $fullPathFilename . '@1x.' . $ext;
|
||||
|
||||
$deletedResponsiveImage = false;
|
||||
if (file_exists($responsiveTargetPath) && unlink($responsiveTargetPath)) {
|
||||
$deletedResponsiveImage = true;
|
||||
}
|
||||
|
||||
$responsiveTargetPath = $filename . '@2x.' . $ext;
|
||||
$responsiveTargetPath = $fullPathFilename . '@2x.' . $ext;
|
||||
if (file_exists($responsiveTargetPath) && unlink($responsiveTargetPath)) {
|
||||
$deletedResponsiveImage = true;
|
||||
}
|
||||
$responsiveTargetPath = $filename . '@3x.' . $ext;
|
||||
|
||||
$responsiveTargetPath = $fullPathFilename . '@3x.' . $ext;
|
||||
if (file_exists($responsiveTargetPath) && unlink($responsiveTargetPath)) {
|
||||
$deletedResponsiveImage = true;
|
||||
}
|
||||
@@ -709,6 +716,8 @@ class AdminController
|
||||
|
||||
/**
|
||||
* Process the page Markdown
|
||||
*
|
||||
* @return bool True if the action was performed.
|
||||
*/
|
||||
protected function taskProcessMarkdown()
|
||||
{
|
||||
@@ -734,11 +743,13 @@ class AdminController
|
||||
$html = $page->content();
|
||||
|
||||
$this->admin->json_response = ['status' => 'success', 'message' => $html];
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$this->admin->json_response = ['status' => 'error', 'message' => $e->getMessage()];
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -749,7 +760,7 @@ class AdminController
|
||||
public function taskEnable()
|
||||
{
|
||||
if (!$this->authorizeTask('enable plugin', ['admin.plugins', 'admin.super'])) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->view != 'plugins') {
|
||||
@@ -775,7 +786,7 @@ class AdminController
|
||||
public function taskDisable()
|
||||
{
|
||||
if (!$this->authorizeTask('disable plugin', ['admin.plugins', 'admin.super'])) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->view != 'plugins') {
|
||||
@@ -801,7 +812,7 @@ class AdminController
|
||||
public function taskActivate()
|
||||
{
|
||||
if (!$this->authorizeTask('activate theme', ['admin.themes', 'admin.super'])) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->view != 'themes') {
|
||||
@@ -840,7 +851,7 @@ class AdminController
|
||||
{
|
||||
$type = $this->view === 'plugins' ? 'plugins' : 'themes';
|
||||
if (!$this->authorizeTask('install ' . $type, ['admin.' . $type, 'admin.super'])) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
require_once __DIR__ . '/gpm.php';
|
||||
@@ -915,7 +926,7 @@ class AdminController
|
||||
|
||||
foreach ($permissions as $type => $p) {
|
||||
if (!$this->authorizeTask('update ' . $type , $p)) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -951,7 +962,7 @@ class AdminController
|
||||
{
|
||||
$type = $this->view === 'plugins' ? 'plugins' : 'themes';
|
||||
if (!$this->authorizeTask('uninstall ' . $type, ['admin.' . $type, 'admin.super'])) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
require_once __DIR__ . '/gpm.php';
|
||||
@@ -971,6 +982,11 @@ class AdminController
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $file
|
||||
* @return bool
|
||||
*/
|
||||
private function cleanFilesData($key, $file)
|
||||
{
|
||||
$config = $this->grav['config'];
|
||||
@@ -1031,6 +1047,11 @@ class AdminController
|
||||
return $cleanFiles[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $needle
|
||||
* @param array|string $haystack
|
||||
* @return bool
|
||||
*/
|
||||
private function match_in_array($needle, $haystack)
|
||||
{
|
||||
foreach ((array)$haystack as $item) {
|
||||
@@ -1042,6 +1063,10 @@ class AdminController
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $obj
|
||||
* @return mixed
|
||||
*/
|
||||
private function processFiles($obj)
|
||||
{
|
||||
foreach ((array)$_FILES as $key => $file) {
|
||||
@@ -1108,6 +1133,29 @@ class AdminController
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* @param string $frontmatter
|
||||
* @return bool
|
||||
*/
|
||||
public function checkValidFrontmatter($frontmatter)
|
||||
{
|
||||
try {
|
||||
// Try native PECL YAML PHP extension first if available.
|
||||
if (function_exists('yaml_parse')) {
|
||||
$saved = @ini_get('yaml.decode_php');
|
||||
@ini_set('yaml.decode_php', 0);
|
||||
@yaml_parse("---\n" . $frontmatter . "\n...");
|
||||
@ini_set('yaml.decode_php', $saved);
|
||||
} else {
|
||||
Yaml::parse($frontmatter);
|
||||
}
|
||||
} catch (ParseException $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles form and saves the input data if its valid.
|
||||
*
|
||||
@@ -1116,21 +1164,27 @@ class AdminController
|
||||
public function taskSave()
|
||||
{
|
||||
if (!$this->authorizeTask('save', $this->dataPermissions())) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = $this->post;
|
||||
|
||||
$config = $this->grav['config'];
|
||||
|
||||
// Special handler for pages data.
|
||||
if ($this->view == 'pages') {
|
||||
/** @var Page\Pages $pages */
|
||||
/** @var Pages $pages */
|
||||
$pages = $this->grav['pages'];
|
||||
$config = $this->grav['config'];
|
||||
|
||||
// Find new parent page in order to build the path.
|
||||
$route = !isset($data['route']) ? dirname($this->admin->route) : $data['route'];
|
||||
$obj = $this->admin->page(true);
|
||||
|
||||
if (isset($data['frontmatter']) && !$this->checkValidFrontmatter($data['frontmatter'])) {
|
||||
$this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.INVALID_FRONTMATTER_COULD_NOT_SAVE'), 'error');
|
||||
return false;
|
||||
}
|
||||
|
||||
//Handle system.home.hide_in_urls
|
||||
$hide_home_route = $config->get('system.home.hide_in_urls', false);
|
||||
if ($hide_home_route) {
|
||||
@@ -1187,6 +1241,9 @@ class AdminController
|
||||
}
|
||||
|
||||
if ($obj) {
|
||||
// Event to manipulate data before saving the object
|
||||
$this->grav->fireEvent('onAdminSave', new Event(['object' => &$obj]));
|
||||
|
||||
$obj->save(true);
|
||||
$this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.SUCCESSFULLY_SAVED'), 'info');
|
||||
}
|
||||
@@ -1203,7 +1260,7 @@ class AdminController
|
||||
}
|
||||
|
||||
// Always redirect if a page route was changed, to refresh it
|
||||
if ($obj instanceof Page\Page) {
|
||||
if ($obj instanceof Page) {
|
||||
if (method_exists($obj, 'unsetRouteSlug')) {
|
||||
$obj->unsetRouteSlug();
|
||||
}
|
||||
@@ -1287,7 +1344,7 @@ class AdminController
|
||||
protected function taskCopy()
|
||||
{
|
||||
if (!$this->authorizeTask('copy page', ['admin.pages', 'admin.super'])) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Only applies to pages.
|
||||
@@ -1296,7 +1353,7 @@ class AdminController
|
||||
}
|
||||
|
||||
try {
|
||||
/** @var Page\Pages $pages */
|
||||
/** @var Pages $pages */
|
||||
$pages = $this->grav['pages'];
|
||||
$data = $this->post;
|
||||
|
||||
@@ -1345,7 +1402,7 @@ class AdminController
|
||||
protected function taskReorder()
|
||||
{
|
||||
if (!$this->authorizeTask('reorder pages', ['admin.pages', 'admin.super'])) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Only applies to pages.
|
||||
@@ -1366,7 +1423,7 @@ class AdminController
|
||||
protected function taskDelete()
|
||||
{
|
||||
if (!$this->authorizeTask('delete page', ['admin.pages', 'admin.super'])) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Only applies to pages.
|
||||
@@ -1443,7 +1500,7 @@ class AdminController
|
||||
protected function taskSaveas()
|
||||
{
|
||||
if (!$this->authorizeTask('save', $this->dataPermissions())) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = $this->post;
|
||||
@@ -1480,7 +1537,7 @@ class AdminController
|
||||
$aFile = File::instance($path);
|
||||
$aFile->save();
|
||||
|
||||
$aPage = new Page\Page();
|
||||
$aPage = new Page();
|
||||
$aPage->init(new \SplFileInfo($path), $language .'.md');
|
||||
$aPage->header($obj->header());
|
||||
$aPage->rawMarkdown($obj->rawMarkdown());
|
||||
|
||||
Reference in New Issue
Block a user