Version 5.0.0 beta 1

This commit is contained in:
Neo committed 2025-12-19 16:27:35 -08:00
1 parent 25ddeb65d6
commit 15c7beabc5
6736 files changed
+627902 -497943

No files matched your search

@@ -11,328 +11,25 @@
namespace IPS\core\modules\admin\customization;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
use IPS\Dispatcher\Controller;
use function defined;
if (!defined("\IPS\SUITE_UNIQUE_KEY")) {
header(
( $_SERVER["SERVER_PROTOCOL"] ?? "HTTP/1.0" ) . " 403 Forbidden"
);
exit();
}
/**
* Members AJAX actions
*/
class _ajax extends \IPS\Dispatcher\Controller
class ajax extends Controller
{
/**
* @brief Has been CSRF-protected
*/
public static $csrfProtected = TRUE;
/**
* Return a CSS or HTML menu
*
* @return html
*/
public function loadMenu()
{
$id = \IPS\Request::i()->id;
$t_type = \IPS\Request::i()->t_type ?: 'templates';
$t_app = \IPS\Request::i()->t_app ?: 'core';
$t_location = \IPS\Request::i()->t_location ?: ( ( \IPS\Request::i()->t_app ) ? false : 'front' );
if ( $t_type == 'templates' )
{
$t_group = \IPS\Request::i()->t_group ?: ( ( \IPS\Request::i()->t_app ) ? false : 'global' );
$t_template = \IPS\Request::i()->t_name ?: ( ( \IPS\Request::i()->t_app ) ? false : 'globalTemplate' );
}
else
{
$t_group = \IPS\Request::i()->t_group ?: ( ( \IPS\Request::i()->t_app ) ? false : 'custom' );
$t_template = \IPS\Request::i()->t_name ?: ( ( \IPS\Request::i()->t_app ) ? false : 'custom.css' );
}
$theme = \IPS\Theme::load( $id );
/**
* @brief Has been CSRF-protected
*/
if ( $t_type == 'templates' )
{
$templateNames = $theme->getRawTemplates( '', '', '', \IPS\Theme::RETURN_ALL_NO_CONTENT );
}
else
{
$templateNames = $theme->getRawCss( '', '', '', \IPS\Theme::RETURN_ALL_NO_CONTENT );
}
if ( $t_type === 'templates' )
{
/* Remove Admin Templates */
foreach( $templateNames as $app => $items )
{
foreach( $templateNames[ $app ] as $location => $items )
{
if ( $location == 'admin' )
{
unset( $templateNames[ $app ][ $location ] );
}
}
}
}
else
{
$cssFiles = [];
/* Remove all but custom/custom.css */
foreach ( $templateNames as $app => $items )
{
foreach ( $templateNames[$app] as $location => $items )
{
if ( $location === 'front' or $location === 'global' )
{
foreach ( $templateNames[$app][$location] as $path => $items )
{
if ( $path == 'custom' )
{
$cssFiles[ $app ][ $location ][ $path ] = $items;
}
else
{
foreach( $items as $item )
{
if ( $item['css_added_to'] == $theme->_id or $item['css_user_edited'] )
{
$cssFiles[ $app ][ $location ][ $path ][ $item['css_name'] ] = $item;
}
}
}
}
}
}
}
$templateNames = $cssFiles;
}
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'customization' )->templateEditorMenu( $theme, $templateNames, array( 'app' => $t_app, 'location' => $t_location, 'group' => $t_group, 'template' => $t_template, 'type' => $t_type) );
}
/**
* Return a CSS or HTML template as JSON
*
* @return json
*/
public function loadTemplate()
{
$t_type = \IPS\Request::i()->t_type;
$t_app = \IPS\Request::i()->t_app;
$t_location = \IPS\Request::i()->t_location;
$t_group = ( \IPS\Request::i()->t_group ) ?: '.';
$t_name = \IPS\Request::i()->t_name;
$id = \IPS\Request::i()->id;
$skinSet = \IPS\Theme::load( $id );
if ( $t_type == 'templates' )
{
$templateBits = $skinSet->getRawTemplates( $t_app, $t_location, $t_group, \IPS\Theme::RETURN_ALL );
$templateBit = ( ! empty( $t_name ) ) ? $templateBits[ $t_app ][ $t_location ][ $t_group ][ $t_name ] : null;
$templateBit['template_content'] = htmlentities( $templateBit['template_content'], ENT_DISALLOWED, 'UTF-8', TRUE );
}
else
{
$templateNames = $skinSet->getRawCss( '', '', '', \IPS\Theme::RETURN_ALL_NO_CONTENT );
$templateBits = $skinSet->getRawCss( $t_app, ['global', 'front'], NULL, \IPS\Theme::RETURN_ALL );
$templateBit = ( ! empty( $t_name ) ) ? $templateBits[ $t_app ][ $t_location ][ $t_group ][ $t_name ] : null;
$templateBit['css_content'] = htmlentities( $templateBit['css_content'], ENT_DISALLOWED, 'UTF-8', TRUE );
}
if ( $templateBit !== null )
{
\IPS\Output::i()->json( $templateBit );
}
else
{
\IPS\Output::i()->json( array( 'error' => true ) );
}
}
/**
* [AJAX] Search templates
*
* @return void
*/
public function searchtemplates()
{
\IPS\Dispatcher::i()->checkAcpPermission( 'theme_templates_manage' );
$theme = \IPS\Theme::load( \IPS\Request::i()->id );
$parents = array( $theme->_id );
$where = array();
if ( \IPS\Request::i()->term )
{
$where[] = array( '( LOWER(template_name) LIKE ? OR LOWER(template_content) LIKE ? )', '%' . mb_strtolower( \IPS\Request::i()->term ) . '%', '%' . mb_strtolower( \IPS\Request::i()->term ) . '%' );
/* If searching content we need to specifically only search the templates *actually* being used */
$uniqueIds = array();
foreach ( $theme->getRawTemplates( '', '', '', \IPS\Theme::RETURN_ALL_NO_CONTENT ) as $locations )
{
foreach ( $locations as $groups )
{
foreach ( $groups as $templates )
{
foreach ( $templates as $template )
{
$uniqueIds[] = md5( $template['template_app'] . ',' . $template['template_location'] . ',' . $template['template_group'] . ',' . $template['template_name'] );
}
}
}
}
$where[] = array( \IPS\Db::i()->in( "MD5( CONCAT_WS( ',', template_app, template_location, template_group, template_name ) )", $uniqueIds ) );
}
if ( \in_array( 'inherited', explode( ',', \IPS\Request::i()->filters ) ) )
{
try
{
foreach( $theme->parents() as $parent )
{
$parents[] = $parent->_id;
}
}
catch( \OutOfRangeException $e ) { }
}
array_push( $parents, 0 );
if ( !\in_array( 'unique', explode( ',', \IPS\Request::i()->filters ) ) )
{
$where[] = array( 'template_user_added=0' );
}
if ( !\in_array( 'unmodified', explode( ',', \IPS\Request::i()->filters ) ) )
{
$where[] = array( 'template_user_edited>0 or template_added_to > 0' );
}
if ( !\in_array( 'modified', explode( ',', \IPS\Request::i()->filters ) ) )
{
$where[] = array( 'template_added_to=0' );
}
$where[] = array( 'template_set_id IN (' . implode( ',' , $parents ) . ')' );
$select = \IPS\Db::i()->select(
'*, INSTR(\',' . implode( ',' , $parents ) . ',\', CONCAT(\',\',template_set_id,\',\') ) as theorder',
'core_theme_templates',
$where,
'template_location, template_group, template_name, theorder desc'
);
$return = array();
foreach( $select as $result )
{
if ( $result['template_user_edited'] )
{
try
{
$outOfDate = $result['template_user_edited'] < \IPS\Application::load( $result['template_app'] )->long_version;
}
catch( \UnexpectedValueException | \OutOfRangeException $e )
{
unset( $return[ $result['template_app'] ][ $result['template_location'] ][ $result['template_group'] ][ $result['template_name'] ] );
continue;
}
if ( ( $outOfDate and !\in_array( 'outofdate', explode( ',', \IPS\Request::i()->filters ) ) ) or ( !$outOfDate and !\in_array( 'modified', explode( ',', \IPS\Request::i()->filters ) ) ) )
{
unset( $return[ $result['template_app'] ][ $result['template_location'] ][ $result['template_group'] ][ $result['template_name'] ] );
continue;
}
}
$return[ $result['template_app'] ][ $result['template_location'] ][ $result['template_group'] ][ $result['template_name'] ] = $result['template_name'];
}
\IPS\Output::i()->json( $return );
}
/**
* [AJAX] Search CSS
*
* @return void
*/
public function searchcss()
{
\IPS\Dispatcher::i()->checkAcpPermission( 'theme_templates_manage' );
$theme = \IPS\Theme::load( \IPS\Request::i()->id );
$parents = array( $theme->_id );
$where = array();
if ( \IPS\Request::i()->term )
{
$where[] = array( '( LOWER(css_name) LIKE ? OR LOWER(css_content) LIKE ? )', '%' . mb_strtolower( \IPS\Request::i()->term ) . '%', '%' . mb_strtolower( \IPS\Request::i()->term ) . '%' );
/* If searching content we need to specifically only search the css files *actually* being used */
$uniqueIds = array();
foreach ( $theme->getRawCss( '', '', '', \IPS\Theme::RETURN_ALL_NO_CONTENT ) as $locations )
{
foreach ( $locations as $groups )
{
foreach ( $groups as $templates )
{
foreach ( $templates as $template )
{
$uniqueIds[] = md5( $template['css_app'] . ',' . $template['css_location'] . ',' . $template['css_path'] . ',' . $template['css_name'] );
}
}
}
}
$where[] = array( \IPS\Db::i()->in( "MD5( CONCAT_WS( ',', css_app, css_location, css_path, css_name ) )", $uniqueIds ) );
}
if ( \in_array( 'inherited', explode( ',', \IPS\Request::i()->filters ) ) )
{
try
{
foreach( $theme->parents() as $parent )
{
$parents[] = $parent->_id;
}
}
catch( \OutOfRangeException $e ) { }
}
array_push( $parents, 0 );
if ( !\in_array( 'unique', explode( ',', \IPS\Request::i()->filters ) ) )
{
$where[] = array( 'css_added_to=?', \IPS\Request::i()->id );
}
if ( !\in_array( 'unmodified', explode( ',', \IPS\Request::i()->filters ) ) )
{
$where[] = array( 'css_user_edited>0' );
}
$where[] = array( 'css_set_id IN (' . implode( ',' , $parents ) . ')' );
$return = array();
foreach( \IPS\Db::i()->select(
'*, INSTR(\',' . implode( ',' , $parents ) . ',\', CONCAT(\',\',css_set_id,\',\') ) as theorder',
'core_theme_css',
$where,
'css_location, css_path, css_name, theorder desc'
) as $result )
{
if ( $result['css_user_edited'] )
{
try
{
$outOfDate = $result['css_user_edited'] < \IPS\Application::load( $result['css_app'] )->long_version;
}
catch( \UnexpectedValueException | \OutOfRangeException $e )
{
unset( $return[ $result['css_app'] ][ $result['css_location'] ][ $result['css_path'] ][ $result['css_name'] ] );
continue;
}
if ( !$result['css_added_to'] and ( ( $outOfDate and !\in_array( 'outofdate', explode( ',', \IPS\Request::i()->filters ) ) ) or ( !$outOfDate and !\in_array( 'modified', explode( ',', \IPS\Request::i()->filters ) ) ) ) )
{
unset( $return[ $result['css_app'] ][ $result['css_location'] ][ $result['css_path'] ][ $result['css_name'] ] );
continue;
}
}
$return[ $result['css_app'] ][ $result['css_location'] ][ $result['css_path'] ][ $result['css_name'] ] = $result['css_name'];
}
\IPS\Output::i()->json( $return );
}
}
public static bool $csrfProtected = true;
}
@@ -0,0 +1,248 @@
<?php
/**
* @brief Custom templates
* @author <a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
* @copyright (c) Invision Power Services, Inc.
* @license https://www.invisioncommunity.com/legal/standards/
* @package Invision Community
* @since 3 May 2023
*/
namespace IPS\core\modules\admin\customization;
/* To prevent PHP errors (extending class does not exist) revealing path */
use Exception;
use IPS\Dispatcher;
use IPS\Http\Url;
use IPS\Member;
use IPS\Node\Controller;
use IPS\Node\Model;
use IPS\Output;
use IPS\Request;
use IPS\Theme;
use IPS\Theme\CustomTemplate;
use function defined;
use function htmlspecialchars;
use function implode;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* themes
*/
class customtemplates extends Controller
{
/**
* @brief Has been CSRF-protected
*/
public static bool $csrfProtected = TRUE;
/**
* Node Class
*
* @var CustomTemplate|string
*/
protected string $nodeClass = CustomTemplate::class;
/**
* @brief If true, will prevent any item from being moved out of its current parent, only allowing them to be reordered within their current parent
*/
protected bool $lockParents = TRUE;
/**
* Title can contain HTML?
*/
public bool $_titleHtml = TRUE;
/**
* Description can contain HTML?
*/
public bool $_descriptionHtml = TRUE;
/**
* Execute
*
* @return void
*/
public function execute() : void
{
Dispatcher::i()->checkAcpPermission( 'theme_sets_manage' );
/* Set the scope */
if ( isset( Request::i()->set_id ) )
{
$this->nodeClass::$scopeKey = 'set_id';
$this->nodeClass::$scopeValue = Request::i()->set_id;
}
else if ( isset( Request::i()->application ) )
{
$this->nodeClass::$scopeKey = 'appKey';
$this->nodeClass::$scopeValue = Request::i()->appKey;
}
/* Yummy free food */
Output::i()->breadcrumb = array(
array(
Url::internal('app=core&module=customization&controller=themes'),
'menu__' . Dispatcher::i()->application->directory . '_' . Dispatcher::i()->module->key
)
);
if ( $this->nodeClass::$scopeKey === 'set_id' )
{
Output::i()->breadcrumb[] = array(
Url::internal('app=core&module=customization&controller=customtemplates&' . $this->nodeClass::$scopeKey. '=' . $this->nodeClass::$scopeValue ),
'custom_templates'
);
Output::i()->breadcrumb[] = array(
NULL,
Member::loggedIn()->language()->addToStack( 'core_theme_set_title_' . $this->nodeClass::$scopeValue )
);
}
else
{
/* @todo logic for type=themes/apps */
}
parent::execute();
}
/**
* Manage
*
* @return void
*/
protected function manage() : void
{
if ( $this->nodeClass::$scopeKey === 'set_id' )
{
Output::i()->output = Theme::i()->getTemplate('forms')->blurb( Member::loggedIn()->language()->addToStack( 'template_custom_theme_blurb', NULL, [ 'sprintf' => [ Theme::load( $this->nodeClass::$scopeValue )->_title ] ] ) );
}
parent::manage();
}
/**
* Get and return available template parameters
*
* @return void
*/
protected function ajaxGetAvailableParams(): void
{
$template = new CustomTemplate();
$template->hookpoint = Request::i()->hookpoint;
$template->hookpoint_type = Request::i()->hookpoint_type;
Output::i()->json( implode( ', ', $template->availableParams( null, true ) ) );
}
protected function ajaxGetTemplate(): void
{
$template = new CustomTemplate();
$template->hookpoint = Request::i()->hookpoint;
$masterTemplate = trim( $template->hookedTemplate()['template_content'] );
$masterTemplateEncoded = preg_replace( '#data-ips-hook=&quot;(\w+?)&quot;#', '<strong style="background-color: #FFFF00">data-ips-hook=&quot;$1&quot;</strong>', htmlspecialchars( $masterTemplate ) );
Output::i()->sendOutput( Theme::i()->getTemplate( 'global', 'core' )->blankTemplate( Theme::i()->getTemplate( 'customization', 'core', 'admin' )->viewTemplate( $masterTemplateEncoded ) ) );
}
/**
* Redirect after save
*
* @param Model|null $old A clone of the node as it was before or NULL if this is a creation
* @param Model $new The node now
* @param bool|string $lastUsedTab The tab last used in the form
* @return void
* @throws Exception
*/
protected function _afterSave( ?Model $old, Model $new, bool|string $lastUsedTab = FALSE ): void
{
if( Request::i()->isAjax() )
{
Output::i()->json( array() );
}
else
{
if( isset( Request::i()->save_and_reload ) )
{
$buttons = $new->getButtons( $this->url, !( $new instanceof $this->nodeClass ) );
Output::i()->redirect( ( $lastUsedTab ? $buttons['edit']['link']->setQueryString('activeTab', $lastUsedTab ) : $buttons['edit']['link'] ), 'saved' );
}
else
{
Output::i()->redirect( $this->url->setQueryString( array( 'root' => ( $new->parent() ? $new->parent()->_id : '' ), $this->nodeClass::$scopeKey => $this->nodeClass::$scopeValue ) ), 'saved' );
}
}
}
/**
* Search
*
* @return void
*/
protected function search() : void
{
$rows = array();
/* Get results */
$nodeClass = $this->nodeClass;
/** @var Model $nodeClass */
$results = $nodeClass::search( 'template_name', Request::i()->input, 'template_name' );
/* Convert to HTML */
foreach ( $results as $result )
{
$rows[ $result->_id ] = $this->_getRow($result, FALSE, TRUE);
}
Output::i()->output = Theme::i()->getTemplate( 'trees', 'core' )->rows( $rows, '' );
}
/**
* Rebuilds the flags in core_theme_templates to denote if the template has a hook point or not
* @return void
* @throws Exception
*/
protected function rebuildHookPoints(): void
{
Theme::rebuildHookPointFlags();
Output::i()->redirect( Url::internal('app=core&module=customization&controller=customtemplates&' . $this->nodeClass::$scopeKey. '=' . $this->nodeClass::$scopeValue ), 'updated' );
}
/**
* Get Root Buttons
*
* @return array
*/
public function _getRootButtons(): array
{
if ( \IPS\IN_DEV )
{
/* Add a button for settings */
$buttons['rebuild'] = array(
'primary' => false,
'icon' => 'cogs',
'title' => 'custom_template_rebuild_hookpoints',
'link' => Url::internal( "app=core&module=customization&controller=customtemplates&do=rebuildHookPoints&" . $this->nodeClass::$scopeKey. "=" . $this->nodeClass::$scopeValue )
);
}
/* Add a button for settings */
$buttons['add'] = array(
'primary' => true,
'icon' => 'plus',
'title' => 'custom_template_create_new',
'link' => Url::internal( "app=core&module=customization&controller=customtemplates&do=form&" . $this->nodeClass::$scopeKey. "=" . $this->nodeClass::$scopeValue )
);
return $buttons;
}
}
@@ -11,30 +11,52 @@
namespace IPS\core\modules\admin\customization;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use InvalidArgumentException;
use IPS\Data\Store;
use IPS\Db;
use IPS\Dispatcher;
use IPS\Dispatcher\Controller;
use IPS\Email;
use IPS\Helpers\Form;
use IPS\Helpers\Form\Codemirror;
use IPS\Helpers\Form\TextArea;
use IPS\Helpers\Form\Upload;
use IPS\Http\Url;
use IPS\Member;
use IPS\Output;
use IPS\Request;
use IPS\Session;
use IPS\Theme;
use IPS\Xml\SimpleXML;
use UnderflowException;
use function count;
use function defined;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* Email template management
*/
class _emails extends \IPS\Dispatcher\Controller
class emails extends Controller
{
/**
* @brief Has been CSRF-protected
*/
public static $csrfProtected = TRUE;
public static bool $csrfProtected = TRUE;
/**
* Execute
*
* @return void
*/
public function execute()
public function execute() : void
{
\IPS\Dispatcher::i()->checkAcpPermission( 'emails_manage' );
Dispatcher::i()->checkAcpPermission( 'emails_manage' );
parent::execute();
}
@@ -43,14 +65,13 @@ class _emails extends \IPS\Dispatcher\Controller
*
* @return void
*/
public function manage()
public function manage() : void
{
/* Create the table */
$table = new \IPS\Helpers\Table\Db( 'core_email_templates', \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails' ), array( array( "template_parent=?", 0 ) ) );
$table = new \IPS\Helpers\Table\Db( 'core_email_templates', Url::internal( 'app=core&module=customization&controller=emails' ), array( array( "template_parent=?", 0 ) ) );
$table->langPrefix = 'emailtpl_';
$table->mainColumn = 'template_name';
$table->include = array( 'template_name', 'template_app' );
$table->quickSearch = 'template_name';
$table->limit = 100;
/* Primary Sort */
@@ -64,14 +85,14 @@ class _emails extends \IPS\Dispatcher\Controller
$table->quickSearch = function( $val )
{
$matches = \IPS\Member::loggedIn()->language()->searchCustom( 'emailtpl_', $val, TRUE );
if ( \count( $matches ) )
$matches = Member::loggedIn()->language()->searchCustom( 'emailtpl_', $val, TRUE );
if ( count( $matches ) )
{
return '(' . \IPS\Db::i()->in( '`template_name`', array_keys( $matches ) ) . " OR `template_name` LIKE '%{$val}%')";
return '(' . Db::i()->in( '`template_name`', array_keys( $matches ) ) . " OR `template_name` LIKE '%{$val}%')";
}
else
{
return "`template_name` LIKE '%" . \IPS\Db::i()->escape_string( $val ) . "%'";
return "`template_name` LIKE '%" . Db::i()->escape_string( $val ) . "%'";
}
};
@@ -79,11 +100,11 @@ class _emails extends \IPS\Dispatcher\Controller
$table->parsers = array(
'template_name' => function( $val, $row )
{
return \IPS\Member::loggedIn()->language()->addToStack( 'emailtpl_' . $val );
return Member::loggedIn()->language()->addToStack( 'emailtpl_' . $val );
},
'template_app' => function( $val, $row )
{
return \IPS\Member::loggedIn()->language()->addToStack( '__app_' . $val );
return Member::loggedIn()->language()->addToStack( '__app_' . $val );
}
);
@@ -95,7 +116,7 @@ class _emails extends \IPS\Dispatcher\Controller
$return['edit'] = array(
'icon' => 'pencil',
'title' => 'edit',
'link' => \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails&do=form&key=' ) . $row['template_key'],
'link' => Url::internal( 'app=core&module=customization&controller=emails&do=form&key=' ) . $row['template_key'],
);
if( $row['template_edited'] )
@@ -103,14 +124,14 @@ class _emails extends \IPS\Dispatcher\Controller
$return['revert'] = array(
'icon' => 'undo',
'title' => 'revert',
'link' => \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails&do=revert&key=' . $row['template_key'] )->csrf(),
'data' => array( 'confirm' => '', 'confirmMessage' => \IPS\Member::loggedIn()->language()->addToStack('email_revert_confirm') )
'link' => Url::internal( 'app=core&module=customization&controller=emails&do=revert&key=' . $row['template_key'] )->csrf(),
'data' => array( 'confirm' => '', 'confirmMessage' => Member::loggedIn()->language()->addToStack('email_revert_confirm') )
);
$return['export'] = array(
'icon' => 'download',
'title' => 'download',
'link' => \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails&do=export&key=' ) . $row['template_key'],
'link' => Url::internal( 'app=core&module=customization&controller=emails&do=export&key=' ) . $row['template_key'],
);
}
@@ -118,24 +139,24 @@ class _emails extends \IPS\Dispatcher\Controller
};
/* Buttons */
\IPS\Output::i()->sidebar['actions'] = array(
Output::i()->sidebar['actions'] = array(
'upload' => array(
'title' => 'upload_email_template',
'icon' => 'upload',
'link' => \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails&do=import' ),
'data' => array( 'ipsDialog' => '', 'ipsDialog-title' => \IPS\Member::loggedIn()->language()->addToStack('upload_email_template') )
'link' => Url::internal( 'app=core&module=customization&controller=emails&do=import' ),
'data' => array( 'ipsDialog' => '', 'ipsDialog-title' => Member::loggedIn()->language()->addToStack('upload_email_template') )
),
'preview' => array(
'title' => 'preview_email_wrapper',
'icon' => 'search',
'link' => \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails&do=preview' ),
'data' => array( 'ipsDialog' => \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails&do=preview' ), 'ipsDialog-title' => \IPS\Member::loggedIn()->language()->addToStack('preview_email_wrapper') )
'link' => Url::internal( 'app=core&module=customization&controller=emails&do=preview' ),
'data' => array( 'ipsDialog' => Url::internal( 'app=core&module=customization&controller=emails&do=preview' ), 'ipsDialog-title' => Member::loggedIn()->language()->addToStack('preview_email_wrapper') )
),
);
/* Display */
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('emailtpl_header');
\IPS\Output::i()->output = (string) $table;
Output::i()->title = Member::loggedIn()->language()->addToStack('emailtpl_header');
Output::i()->output = (string) $table;
}
/**
@@ -143,19 +164,19 @@ class _emails extends \IPS\Dispatcher\Controller
*
* @return void
*/
public function export()
public function export() : void
{
/* Get the template info */
try
{
$template = \IPS\Db::i()->select( '*', 'core_email_templates', array( 'template_parent>0 AND template_key=?', \IPS\Request::i()->key ) )->first();
$template = Db::i()->select( '*', 'core_email_templates', array( 'template_parent>0 AND template_key=?', Request::i()->key ) )->first();
}
catch ( \UnderflowException $e )
catch ( UnderflowException $e )
{
\IPS\Output::i()->error( 'email_template_not_found', '3S128/3', 403, '' );
Output::i()->error( 'email_template_not_found', '3S128/3', 403, '' );
}
$xml = \IPS\Xml\SimpleXML::create('emails');
$xml = SimpleXML::create('emails');
$xml->addChild( 'template', array(
'template_app' => $template['template_app'],
@@ -168,7 +189,7 @@ class _emails extends \IPS\Dispatcher\Controller
$name = addslashes( str_replace( array( ' ', '.', ',' ), '_', $template['template_name'] ) . '.xml' );
\IPS\Output::i()->sendOutput( $xml->asXML(), 200, 'application/xml', array( 'Content-Disposition' => \IPS\Output::getContentDisposition( 'attachment', $name ) ) );
Output::i()->sendOutput( $xml->asXML(), 200, 'application/xml', array( 'Content-Disposition' => Output::getContentDisposition( 'attachment', $name ) ) );
}
/**
@@ -176,27 +197,27 @@ class _emails extends \IPS\Dispatcher\Controller
*
* @return void
*/
public function import()
public function import() : void
{
$form = new \IPS\Helpers\Form( 'form', 'upload' );
$form = new Form( 'form', 'upload' );
$form->add( new \IPS\Helpers\Form\Upload( 'email_template_file', NULL, TRUE, array( 'allowedFileTypes' => array( 'xml' ), 'temporary' => TRUE ) ) );
$form->add( new Upload( 'email_template_file', NULL, TRUE, array( 'allowedFileTypes' => array( 'xml' ), 'temporary' => TRUE ) ) );
if ( $values = $form->values() )
{
/* Open XML file */
try
{
$xml = \IPS\Xml\SimpleXML::loadFile( $values['email_template_file'] );
$xml = SimpleXML::loadFile( $values['email_template_file'] );
if( !\count($xml->template) )
if( !count($xml->template) )
{
\IPS\Output::i()->error( 'email_template_badform', '1S128/4', 403, '' );
Output::i()->error( 'email_template_badform', '1S128/4', 403, '' );
}
}
catch( \InvalidArgumentException $e )
catch( InvalidArgumentException $e )
{
\IPS\Output::i()->error( 'email_template_badform', '1C373/1', 403, '' );
Output::i()->error( 'email_template_badform', '1C373/1', 403, '' );
}
foreach( $xml->template as $template )
@@ -212,25 +233,25 @@ class _emails extends \IPS\Dispatcher\Controller
try
{
$existing = \IPS\Db::i()->select( '*', 'core_email_templates', array( 'template_parent=0 AND template_key=?', $update['template_key'] ) )->first();
$existing = Db::i()->select( '*', 'core_email_templates', array( 'template_parent=0 AND template_key=?', $update['template_key'] ) )->first();
}
catch ( \UnderflowException $e )
catch ( UnderflowException $e )
{
\IPS\Output::i()->error( \IPS\Member::loggedIn()->language()->addToStack('email_template_noexist', FALSE, array( 'sprintf' => array( $update['template_name'] ) ) ), '1S128/5', 403, '' );
Output::i()->error( Member::loggedIn()->language()->addToStack('email_template_noexist', FALSE, array( 'sprintf' => array( $update['template_name'] ) ) ), '1S128/5', 403, '' );
}
$update['template_parent'] = $existing['template_id'];
\IPS\Db::i()->replace( 'core_email_templates', $update );
\IPS\Db::i()->update( 'core_email_templates', array( 'template_edited' => 1 ), array( 'template_id=?', $existing['template_id'] ) );
Db::i()->replace( 'core_email_templates', $update );
Db::i()->update( 'core_email_templates', array( 'template_edited' => 1 ), array( 'template_id=?', $existing['template_id'] ) );
}
/* Redirect */
\IPS\Session::i()->log( 'acplogs__emailtemplate_updated' );
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails' ), 'email_template_uploaded' );
Session::i()->log( 'acplogs__emailtemplate_updated' );
Output::i()->redirect( Url::internal( 'app=core&module=customization&controller=emails' ), 'email_template_uploaded' );
}
/* Display */
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'global' )->block( \IPS\Member::loggedIn()->language()->addToStack('upload_email_template'), $form, FALSE );
Output::i()->output = Theme::i()->getTemplate( 'global' )->block( Member::loggedIn()->language()->addToStack('upload_email_template'), $form, FALSE );
}
/**
@@ -238,26 +259,26 @@ class _emails extends \IPS\Dispatcher\Controller
*
* @return void
*/
public function revert()
public function revert() : void
{
/* Make sure the user confirmed the deletion */
\IPS\Request::i()->confirmedDelete();
Request::i()->confirmedDelete();
/* Get the template info for the log */
$template = \IPS\Db::i()->select( '*', 'core_email_templates', array( 'template_parent=0 AND template_key=?', \IPS\Request::i()->key ) )->first();
$template = Db::i()->select( '*', 'core_email_templates', array( 'template_parent=0 AND template_key=?', Request::i()->key ) )->first();
/* Revert any user-edited copies of the specified template */
\IPS\Db::i()->delete( 'core_email_templates', array( 'template_parent>0 AND template_key=?', \IPS\Request::i()->key ) );
Db::i()->delete( 'core_email_templates', array( 'template_parent>0 AND template_key=?', Request::i()->key ) );
/* Reset edited flag on parent */
\IPS\Db::i()->update( 'core_email_templates', array( 'template_edited' => 0 ), array( 'template_id=?', $template['template_id'] ) );
Db::i()->update( 'core_email_templates', array( 'template_edited' => 0 ), array( 'template_id=?', $template['template_id'] ) );
/* Rebuild template */
$this->_buildTemplate( $template );
/* Log and redirect */
\IPS\Session::i()->log( 'acplog__emailtpl_reverted', array( $template['template_name'] => FALSE ) );
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails' ), 'reverted_emailtpl' );
Session::i()->log( 'acplog__emailtpl_reverted', array( $template['template_name'] => FALSE ) );
Output::i()->redirect( Url::internal( 'app=core&module=customization&controller=emails' ), 'reverted_emailtpl' );
}
/**
@@ -265,21 +286,21 @@ class _emails extends \IPS\Dispatcher\Controller
*
* @return void
*/
public function form()
public function form() : void
{
/* Get the template */
if( empty( \IPS\Request::i()->key ) )
if( empty( Request::i()->key ) )
{
\IPS\Output::i()->error( 'emailtpl_nofind', '3S128/1', 403, '' );
Output::i()->error( 'emailtpl_nofind', '3S128/1', 403, '' );
}
try
{
$template = \IPS\Db::i()->select( '*', 'core_email_templates', array( 'template_key=?', \IPS\Request::i()->key ), 'template_parent DESC', 1 )->first();
$template = Db::i()->select( '*', 'core_email_templates', array( 'template_key=?', Request::i()->key ), 'template_parent DESC', 1 )->first();
}
catch ( \UnderflowException $e )
catch ( UnderflowException $e )
{
\IPS\Output::i()->error( 'emailtpl_nofind', '3S128/2', 403, '' );
Output::i()->error( 'emailtpl_nofind', '3S128/2', 403, '' );
}
/* Figure out tags for form helpers */
@@ -294,10 +315,10 @@ class _emails extends \IPS\Dispatcher\Controller
}
/* Start the form */
$form = new \IPS\Helpers\Form;
$form->class = 'ipsForm_vertical';
$form->add( new \IPS\Helpers\Form\Codemirror( 'content_html', $template['template_content_html'], TRUE, array( 'tags' => $tags ) ) );
$form->add( new \IPS\Helpers\Form\TextArea( 'content_plaintext', $template['template_content_plaintext'], FALSE, array( 'tags' => $tags, 'rows' => 14 ) ) );
$form = new Form;
$form->class = 'ipsForm--vertical ipsForm--edit-email';
$form->add( new Codemirror( 'content_html', $template['template_content_html'], TRUE, array( 'tags' => $tags ) ) );
$form->add( new TextArea( 'content_plaintext', $template['template_content_plaintext'], FALSE, array( 'tags' => $tags, 'rows' => 14 ) ) );
/* Handle submissions */
if ( $values = $form->values() )
@@ -314,25 +335,25 @@ class _emails extends \IPS\Dispatcher\Controller
if ( !empty($template['template_parent']) )
{
\IPS\Db::i()->update( 'core_email_templates', $save, array( 'template_id=?', $template['template_id'] ) );
Db::i()->update( 'core_email_templates', $save, array( 'template_id=?', $template['template_id'] ) );
}
else
{
\IPS\Db::i()->insert( 'core_email_templates', $save );
\IPS\Db::i()->update( 'core_email_templates', array( 'template_edited' => 1 ), array( 'template_id=?', $template['template_id'] ) );
Db::i()->insert( 'core_email_templates', $save );
Db::i()->update( 'core_email_templates', array( 'template_edited' => 1 ), array( 'template_id=?', $template['template_id'] ) );
}
$this->_buildTemplate( $save );
\IPS\Session::i()->log( 'acplogs__emailtpl_edited', array( $template['template_name'] => FALSE ) );
Session::i()->log( 'acplogs__emailtpl_edited', array( $template['template_name'] => FALSE ) );
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails' ), 'saved' );
Output::i()->redirect( Url::internal( 'app=core&module=customization&controller=emails' ), 'saved' );
}
/* Output */
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('emailtpl_edit', FALSE, array('sprintf' => \IPS\Member::loggedIn()->language()->addToStack( 'emailtpl_' . $template['template_name']) ) );
\IPS\Output::i()->output = $form->customTemplate( array( \IPS\Theme::i()->getTemplate( 'customization', 'core', 'admin' ), 'email' ) );;
Output::i()->title = Member::loggedIn()->language()->addToStack('emailtpl_edit', FALSE, array('sprintf' => Member::loggedIn()->language()->addToStack( 'emailtpl_' . $template['template_name']) ) );
Output::i()->output = $form->customTemplate( array( Theme::i()->getTemplate( 'customization', 'core', 'admin' ), 'email' ) );
}
/**
@@ -341,16 +362,16 @@ class _emails extends \IPS\Dispatcher\Controller
* @param array $template Template data from core_email_templates
* @return void
*/
protected function _buildTemplate( $template )
protected function _buildTemplate( array $template ) : void
{
$htmlFunction = 'namespace IPS\Theme;' . "\n" . \IPS\Theme::compileTemplate( $template['template_content_html'], "email_html_{$template['template_app']}_{$template['template_name']}", $template['template_data'] );
$ptFunction = 'namespace IPS\Theme;' . "\n" . \IPS\Theme::compileTemplate( $template['template_content_plaintext'], "email_plaintext_{$template['template_app']}_{$template['template_name']}", $template['template_data'] );
$htmlFunction = 'namespace IPS\Theme;' . "\n" . Theme::compileTemplate( $template['template_content_html'], "email_html_{$template['template_app']}_{$template['template_name']}", $template['template_data'] );
$ptFunction = 'namespace IPS\Theme;' . "\n" . Theme::compileTemplate( $template['template_content_plaintext'], "email_plaintext_{$template['template_app']}_{$template['template_name']}", $template['template_data'] );
$key = $template['template_key'] . '_email_html';
\IPS\Data\Store::i()->$key = $htmlFunction;
Store::i()->$key = $htmlFunction;
$key = $template['template_key'] . '_email_plaintext';
\IPS\Data\Store::i()->$key = $ptFunction;
Store::i()->$key = $ptFunction;
}
/**
@@ -358,10 +379,10 @@ class _emails extends \IPS\Dispatcher\Controller
*
* @return void
*/
public function preview()
public function preview() : void
{
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('emailtpl_edit');
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'customization', 'core', 'admin' )->emailFrame( \IPS\Http\Url::internal( 'app=core&module=customization&controller=emails&do=emailPreview' ) );
Output::i()->title = Member::loggedIn()->language()->addToStack('emailtpl_edit');
Output::i()->output = Theme::i()->getTemplate( 'customization', 'core', 'admin' )->emailFrame( Url::internal( 'app=core&module=customization&controller=emails&do=emailPreview' ) );
}
/**
@@ -369,9 +390,9 @@ class _emails extends \IPS\Dispatcher\Controller
*
* @return void
*/
public function emailPreview()
public function emailPreview() : void
{
$email = \IPS\Email::buildFromContent( '', \IPS\Member::loggedIn()->language()->addToStack( 'email_preview_content' ), NULL, \IPS\Email::TYPE_TRANSACTIONAL );
\IPS\Output::i()->sendOutput( $email->compileContent( 'html' ) );
$email = Email::buildFromContent( '', Member::loggedIn()->language()->addToStack( 'email_preview_content' ), NULL, Email::TYPE_TRANSACTIONAL );
Output::i()->sendOutput( $email->compileContent( 'html' ) );
}
}
@@ -11,30 +11,44 @@
namespace IPS\core\modules\admin\customization;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
use IPS\Dispatcher;
use IPS\Dispatcher\Controller;
use IPS\File;
use IPS\Helpers\Form;
use IPS\Helpers\Form\Upload;
use IPS\Member;
use IPS\Output;
use IPS\Session;
use IPS\Settings;
use IPS\Theme;
use function count;
use function defined;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* icons
*/
class _icons extends \IPS\Dispatcher\Controller
class icons extends Controller
{
/**
* @brief Has been CSRF-protected
*/
public static $csrfProtected = TRUE;
public static bool $csrfProtected = TRUE;
/**
* Execute
*
* @return void
*/
public function execute()
public function execute() : void
{
\IPS\Dispatcher::i()->checkAcpPermission( 'theme_sets_manage' );
Dispatcher::i()->checkAcpPermission( 'theme_sets_manage' );
parent::execute();
}
@@ -43,16 +57,16 @@ class _icons extends \IPS\Dispatcher\Controller
*
* @return void
*/
protected function manage()
protected function manage() : void
{
$form = new \IPS\Helpers\Form;
$form = new Form;
/* Generic favicon - easy enough */
$form->add( new \IPS\Helpers\Form\Upload( 'icons_favicon', \IPS\Settings::i()->icons_favicon ? \IPS\File::get( 'core_Icons', \IPS\Settings::i()->icons_favicon ) : NULL, FALSE, array( 'obscure' => false, 'allowedFileTypes' => array( 'ico', 'png', 'gif', 'jpeg', 'jpg', 'jpe' ), 'storageExtension' => 'core_Icons' ) ) );
$form->add( new Upload( 'icons_favicon', Settings::i()->icons_favicon ? File::get( 'core_Icons', Settings::i()->icons_favicon ) : NULL, FALSE, array( 'obscure' => false, 'allowedFileTypes' => array( 'ico', 'png', 'gif', 'jpeg', 'jpg', 'jpe' ), 'storageExtension' => 'core_Icons' ) ) );
/* Sharer logos, allows multiple images to be uploaded */
$shareLogos = \IPS\Settings::i()->icons_sharer_logo ? json_decode( \IPS\Settings::i()->icons_sharer_logo, true ) : array();
$form->add( new \IPS\Helpers\Form\Upload( 'icons_sharer_logo', \count( $shareLogos ) ? array_map( function( $val ) { return \IPS\File::get( 'core_Icons', $val ); }, $shareLogos ) : array(), FALSE, array( 'image' => true, 'storageExtension' => 'core_Icons', 'multiple' => true ) ) );
$shareLogos = Settings::i()->icons_sharer_logo ? json_decode( Settings::i()->icons_sharer_logo, true ) : array();
$form->add( new Upload( 'icons_sharer_logo', count( $shareLogos ) ? array_map( function( $val ) { return File::get( 'core_Icons', $val ); }, $shareLogos ) : array(), FALSE, array( 'image' => true, 'storageExtension' => 'core_Icons', 'multiple' => true ) ) );
/* We've submitted, check our values! */
if ( $values = $form->values() )
@@ -61,7 +75,7 @@ class _icons extends \IPS\Dispatcher\Controller
$values['icons_favicon'] = (string) $values['icons_favicon'];
/* Sharer logos are easy too, except it's an array of images instead of a single image */
if( \count( $values['icons_sharer_logo'] ) )
if( count( $values['icons_sharer_logo'] ) )
{
$values['icons_sharer_logo'] = json_encode( array_map( function( $val ){ return (string) $val; }, $values['icons_sharer_logo'] ) );
}
@@ -69,17 +83,14 @@ class _icons extends \IPS\Dispatcher\Controller
/* Save the settings */
$form->saveAsSettings( $values );
/* Clear guest page caches */
\IPS\Data\Cache::i()->clearAll();
/* And log */
\IPS\Session::i()->log( 'acplogs__icons_and_logos' );
Session::i()->log( 'acplogs__icons_and_logos' );
/* And Redirect */
\IPS\Output::i()->redirect( $this->url, 'saved' );
Output::i()->redirect( $this->url, 'saved' );
}
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('menu__core_customization_icons');
\IPS\Output::i()->output .= \IPS\Theme::i()->getTemplate( 'global' )->block( 'menu__core_customization_icons', $form );
Output::i()->title = Member::loggedIn()->language()->addToStack('menu__core_customization_icons');
Output::i()->output .= Theme::i()->getTemplate( 'global' )->block( 'menu__core_customization_icons', $form );
}
}
@@ -0,0 +1,484 @@
<?php
/**
* @brief Theme Media Management
* @author <a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
* @copyright (c) Invision Power Services, Inc.
* @license https://www.invisioncommunity.com/legal/standards/
* @package Invision Community
* @subpackage Content
* @since 18 Jan 2024
*/
namespace IPS\core\modules\admin\customization;
/* To prevent PHP errors (extending class does not exist) revealing path */
use Exception;
use IPS\Db;
use IPS\Dispatcher;
use IPS\File;
use IPS\Helpers\Form;
use IPS\Helpers\Form\Upload;
use IPS\Http\Url;
use IPS\Image;
use IPS\Member;
use IPS\Dispatcher\Controller;
use IPS\Output;
use IPS\Output\Plugin\Filesize;
use IPS\Request;
use IPS\Session;
use IPS\Theme;
use IPS\Widget;
use OutOfRangeException;
use UnderflowException;
use function array_merge;
use function count;
use function defined;
use function in_array;
use function intval;
use function is_array;
use function json_encode;
use function ltrim;
use function mt_rand;
use function time;
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* media
*/
class media extends Controller
{
/**
* @brief Has been CSRF-protected
*/
public static bool $csrfProtected = TRUE;
/**
* Execute
*
* @return void
*/
public function execute() : void
{
Dispatcher::i()->checkAcpPermission( 'theme_sets_manage' );
parent::execute();
}
/**
* Delete many at once
*
* @return void
*/
public function deleteByFileIds() : void
{
Session::i()->csrfCheck();
$setId = intval( Request::i()->set_id );
try
{
$set = Theme::load( $setId );
}
catch( OutOfRangeException $ex )
{
Output::i()->error( 'node_error', '3T334/G', 403, '' );
}
if ( isset( Request::i()->fileIds ) )
{
$ids = Request::i()->fileIds;
if ( ! is_array( $ids ) )
{
$try = json_decode( $ids, TRUE );
if ( ! is_array( $try ) )
{
$ids = array( $ids );
}
else
{
$ids = $try;
}
}
if ( count( $ids ) )
{
foreach( $ids as $id )
{
try
{
$current = Db::i()->select( '*', 'core_theme_resources', array( 'resource_set_id=? and resource_id=?', $set->_id, $id ) )->first();
}
catch( UnderflowException $ex )
{
/* Just ignore it ssssh */
}
/* Don't delete master resources */
if ( $current['resource_set_id'] > 0 )
{
try
{
File::get( 'core_Theme', $current['resource_filename'] )->delete();
}
catch( Exception $ex ) { }
Db::i()->delete( 'core_theme_resources', array( 'resource_id=?', $id ) );
}
Session::i()->log( 'acplog__theme_resource_deleted', array( $current['resource_filename'] => FALSE ) );
}
/* Delete widget caches */
Widget::deleteCaches();
$set->buildResourceMap();
}
}
}
/**
* Show the pages tree
*
* @return void
*/
protected function manage() : void
{
$setId = intval( Request::i()->set_id );
try
{
$set = Theme::load( $setId );
}
catch( OutOfRangeException $ex )
{
Output::i()->error( 'node_error', '3T334/4', 403, '' );
}
/* Display */
Output::i()->title = Member::loggedIn()->language()->addToStack('theme_custom_resources_page_title', FALSE, array( 'sprintf' => array( $set->_title ) ) );
Output::i()->breadcrumb[] = array( Url::internal( "app=core&module=customization&controller=themes" ), 'menu__core_customization_themes' );
Output::i()->breadcrumb[] = array( NULL, Output::i()->title );
/* Get the resources */
$media = [];
foreach( Db::i()->select( '*', 'core_theme_resources', array( 'resource_set_id=? and resource_user_edited=1', $setId ) ) as $row )
{
$media[ $row['resource_id'] ] = media::getItemRow( array_merge( $row, [ 'url' => $set->resource( ltrim( $row['resource_path'], '/' ) . $row['resource_name'], $row['resource_app'], $row['resource_location'] ) ] ) );
}
Output::i()->jsFiles = array_merge( Output::i()->jsFiles, Output::i()->js( 'admin_customization.js', 'core' ) );
Output::i()->cssFiles = array_merge( Output::i()->cssFiles, Theme::i()->css( 'customization/media.css', 'core', 'admin' ) );
if( Request::i()->isAjax() )
{
Output::i()->sendOutput( json_encode( $media ), 200, 'application/json' );
}
else
{
Output::i()->output = Theme::i()->getTemplate( 'customization', 'core', 'admin' )->media( $media, $set );
}
}
/**
* Replace an existing file
*
* @return void
*/
public function replace() : void
{
if( !isset( Request::i()->id ) OR !Request::i()->id )
{
Output::i()->error( 'missing_media_file', '3T334/2', 404, '' );
}
$setId = intval( Request::i()->set_id );
try
{
$set = Theme::load( $setId );
}
catch( OutOfRangeException $ex )
{
Output::i()->error( 'node_error', '3T334/C', 403, '' );
}
try
{
$row = Db::i()->select( '*', 'core_theme_resources', array( 'resource_id=? and resource_set_id=? and resource_user_edited=1', Request::i()->id, $setId ) )->first();
}
catch( Exception $ex )
{
Output::i()->error( 'missing_media_file', '3T334/3', 404, '' );
}
$form = new Form( 'form', 'upload' );
$form->class = 'ipsForm--vertical ipsForm--replace-file ipsForm_noLabels';
$form->add( new Upload( 'media_filename', null, true, array( 'obscure' => false, 'multiple' => false, 'maxFileSize' => 1.2, 'storageExtension' => 'core_Theme', 'storageContainer' => 'set_resources_' . $set->_id ), NULL, NULL, NULL, 'core_theme_resource_filename' ) );
if ( $values = $form->values() )
{
$save = $row;
unset( $save['resource_id'] );
$save['resource_data'] = $values['media_filename']->contents();
$save['resource_added'] = time();
$save['resource_filename'] = (string) $values['media_filename'];
$existingFileExtension = mb_substr( $row['resource_name'], mb_strrpos( $row['resource_name'], '.' ) + 1 );
$newFileExtension = mb_substr( $values['media_filename']->originalFilename, mb_strrpos( $values['media_filename']->originalFilename, '.' ) + 1 );
/* File extention changed? Change the name */
if( $existingFileExtension !== $newFileExtension )
{
$save['resource_name'] = $values['media_filename']->originalFilename;
}
Session::i()->log( 'acplog__theme_resource_edited', array( $save['resource_filename'] => FALSE ) );
Db::i()->update( 'core_theme_resources', $save, array( 'resource_id=?', $row['resource_id'] ) );
/* Do any clean up */
$this->postUpload( $set );
if ( Request::i()->isAjax() )
{
$media = [];
foreach( Db::i()->select( '*', 'core_theme_resources', array( 'resource_set_id=? and resource_user_edited=1', $set->id ) ) as $row )
{
$media[ $row['resource_id'] ] = media::getItemRow( array_merge( $row, [ 'url' => $set->resource( ltrim( $row['resource_path'], '/' ) . $row['resource_name'], $row['resource_app'], $row['resource_location'] ) ] ) );
}
Output::i()->sendOutput( json_encode( array( 'fileID' => $row['resource_id'], 'rows' => $media ) ), 200, 'application/json' );
}
else
{
Output::i()->redirect( Url::internal( 'app=core&module=customization&controller=media&set_id=' . $set->id ) );
}
}
Output::i()->output = Theme::i()->getTemplate( 'global', 'core', 'admin' )->block( 'upload', $form, FALSE );
}
/**
* Upload items
*
* @return void
*/
public function upload() : void
{
$setId = intval( Request::i()->set_id );
try
{
$set = Theme::load( $setId );
}
catch( OutOfRangeException $ex )
{
Output::i()->error( 'node_error', '3T334/A', 403, '' );
}
$form = new Form( 'form', 'upload' );
$form->class = 'ipsForm--vertical ipsForm--upload-items ipsForm_noLabels';
$form->add( new Upload( 'media_filename', null, true, array( 'obscure' => false, 'multiple' => true, 'maxFileSize' => 1.2, 'storageExtension' => 'core_Theme', 'storageContainer' => 'set_resources_' . $set->_id ), NULL, NULL, NULL, 'core_theme_resource_filename' ) );
if ( $values = $form->values() )
{
$count = 0;
foreach( $values['media_filename'] as $media )
{
$save = array(
'resource_app' => 'core',
'resource_location' => 'front',
'resource_path' => '/custom/',
'resource_set_id' => $set->_id,
'resource_user_edited' => 1,
'resource_name' => $media->originalFilename,
'resource_added' => time(),
'resource_filename' => (string) $media,
'resource_data' => $media->contents()
);
try
{
Db::i()->select( '*', 'core_theme_resources', array( 'resource_set_id=? and resource_path=? and resource_location=? and resource_app=? and resource_name=?', $set->_id, $save['resource_path'], $save['resource_location'], $save['resource_app'], $save['resource_name'] ) )->first();
$ext = mb_substr( $save['resource_name'], ( mb_strrpos( $save['resource_name'], '.' ) + 1 ) );
$save['resource_name'] = mb_substr( $save['resource_name'], 0, ( mb_strrpos( $save['resource_name'], '.' ) ) ) . '_' . mt_rand() . '.' . $ext;
}
catch( UnderflowException $ex ) { }
Session::i()->log( 'acplog__theme_resource_added', array( $save['resource_filename'] => FALSE ) );
Db::i()->insert( 'core_theme_resources', $save );
$count++;
}
/* Do any clean up */
$this->postUpload( $set );
if ( Request::i()->isAjax() )
{
$media = [];
foreach( Db::i()->select( '*', 'core_theme_resources', array( 'resource_set_id=? and resource_user_edited=1', $set->id ) ) as $row )
{
$media[ $row['resource_id'] ] = media::getItemRow( array_merge( $row, [ 'url' => $set->resource( ltrim( $row['resource_path'], '/' ) . $row['resource_name'], $row['resource_app'], $row['resource_location'] ) ] ) );
}
Output::i()->sendOutput( json_encode( array( 'count' => $count, 'rows' => $media ) ), 200, 'application/json' );
}
else
{
Output::i()->redirect( Url::internal( 'app=core&module=customization&controller=media&set_id=' . $set->id ) );
}
}
Output::i()->output = Theme::i()->getTemplate( 'global', 'core', 'admin' )->block( 'upload', $form, FALSE );
}
/**
* Do stuff after an upload / replace
*
* @param Theme $set
* @return void
*/
protected function postUpload( Theme $set ) : void
{
$set->buildResourceMap();
/* Delete widget caches */
Widget::deleteCaches();
/* Resource may be used in CSS */
File::getClass('core_Theme')->deleteContainer( 'css_built_' . $set->id );
$set->css_map = array();
$set->css_updated = time();
$set->save();
foreach( $set->children() as $child )
{
File::getClass('core_Theme')->deleteContainer( 'css_built_' . $child->id );
$child->css_map = array();
$child->css_updated = time();
$child->save();
}
}
/**
* Tree Search
*
* @return void
*/
protected function search() : void
{
$setId = intval( Request::i()->set_id );
try
{
$set = Theme::load( $setId );
}
catch( OutOfRangeException $ex )
{
Output::i()->error( 'node_error', '3T334/E', 403, '' );
}
$url = Url::internal( "app=core&module=customization&controller=media&set_id=" . $setId );
$media = array();
/* Get results */
foreach( Db::i()->select( '*', 'core_theme_resources', array( "resource_set_id=? and resource_user_edited=1 and resource_name LIKE CONCAT( '%', ?, '%' )", $setId, Request::i()->input ) ) as $row )
{
$media[] = media::getItemRow( array_merge( $row, [ 'url' => $set->resource( ltrim( $row['resource_path'], '/' ) . $row['resource_name'], $row['resource_app'], $row['resource_location'] ) ] ) );
}
if( Request::i()->isAjax() )
{
Output::i()->sendOutput( json_encode( $media ), 200, 'application/json' );
}
else
{
Output::i()->output = Theme::i()->getTemplate( 'trees', 'core' )->rows( $media, $url );
}
}
/**
* Returns a JSON object with some file information
*
* @return void
*/
protected function getFileInfo() : void
{
$setId = intval( Request::i()->set_id );
try
{
$set = Theme::load( $setId );
}
catch( OutOfRangeException $ex )
{
Output::i()->error( 'node_error', '3T334/B', 403, '' );
}
try
{
$row = Db::i()->select( '*', 'core_theme_resources', array( 'resource_id=? and resource_set_id=? and resource_user_edited=1', Request::i()->id, $setId ) )->first();
$fileObject = File::get( 'core_Theme', $row['resource_filename'] );
}
catch( OutOfRangeException $ex )
{
return;
}
/* Make a human-readable size */
$filesize = Filesize::humanReadableFilesize( $fileObject->filesize() );
Member::loggedIn()->language()->parseOutputForDisplay( $filesize );
$output = array(
'fileSize' => $filesize,
'dimensions' => NULL
);
/* If this is an image we'll also show the dimensions */
if( $fileObject->isImage() )
{
$dimensions = $fileObject->getImageDimensions();
$output['dimensions'] = $dimensions[0] . ' x ' . $dimensions[1];
}
Output::i()->sendOutput( json_encode( $output ), 200, 'application/json' );
}
/**
* Return HTML for a page row
*
* @param array $item Row data
* @return string HTML
*/
public static function getItemRow( array $item ): string
{
$item['file_type'] = 'other';
$ext = mb_strtolower( mb_substr( $item['resource_name'], mb_strrpos( $item['resource_name'], '.' ) + 1 ) );
if ( in_array( $ext, array_merge( [ 'svg' ], Image::supportedExtensions() ) ) )
{
$item['file_type'] = 'image';
}
else if ( in_array( $ext, [ 'txt', 'htm', 'html', 'css', 'js' ] ) )
{
$item['file_type'] = 'text';
}
else if ( in_array( $ext, [ 'woff', 'woff2', 'ttf', 'eot' ] ) )
{
$item['file_type'] = 'font';
}
return Theme::i()->getTemplate( 'customization', 'core', 'admin' )->mediaFileListing( $item );
}
}
@@ -0,0 +1,214 @@
<?php
/**
* @brief themeeditor
* @author <a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
* @copyright (c) Invision Power Services, Inc.
* @license https://www.invisioncommunity.com/legal/standards/
* @package Invision Community
* @since 19 Feb 2024
*/
namespace IPS\core\modules\admin\customization;
use IPS\Dispatcher;
use IPS\Node\Controller;
use IPS\Helpers\Table\Db as TableDb;
use IPS\Http\Url;
use IPS\IPS;
use IPS\Member;
use IPS\Output;
use IPS\Request;
use IPS\Theme;
use IPS\Theme\Editor\Category;
use IPS\Theme\Editor\Setting;
use function defined;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* themeeditor
*/
class themeeditor extends Controller
{
/**
* @var bool
*/
public static bool $csrfProtected = true;
/**
* @var Theme|null
*/
protected ?Theme $theme = null;
/**
* Node Class
*
* @var string
*/
protected string $nodeClass = Category::class;
/**
* @brief Tabs array
*/
protected array $tabs = array();
/**
* @brief Active tab
*/
protected ?string $activeTab = NULL;
/**
* Execute
*
* @return void
*/
public function execute() : void
{
Dispatcher::i()->checkAcpPermission( 'theme_sets_manage' );
/* Set the scope */
if ( isset( Request::i()->set_id ) )
{
$this->theme = Theme::load( Request::i()->set_id );
$this->url = $this->url->setQueryString( 'set_id', $this->theme->id );
}
$this->tabs = array(
'settings' => 'dev_themeeditor_settings',
'categories' => 'dev_themeeditor_categories'
);
/* Choose active tab */
if ( isset( Request::i()->tab ) and array_key_exists( Request::i()->tab, $this->tabs ) )
{
$this->activeTab = Request::i()->tab;
}
else
{
$keys = array_keys( $this->tabs );
$this->activeTab = array_shift( $keys );
}
/* Yummy free food */
Output::i()->breadcrumb = array(
array(
Url::internal('app=core&module=customization&controller=themes'),
'menu__' . Dispatcher::i()->application->directory . '_' . Dispatcher::i()->module->key
)
);
Output::i()->breadcrumb[] = array(
Url::internal('app=core&module=customization&controller=themeeditor&set_id=' . $this->theme->_id ),
'themeeditor_settings'
);
Output::i()->breadcrumb[] = array( null, $this->theme->_title );
parent::execute();
}
protected function manage() : void
{
/* Work out output */
$methodFunction = '_manage' . IPS::mb_ucfirst( $this->activeTab );
$activeTabContents = $this->$methodFunction();
/* If this is an AJAX request, just return it */
if ( Request::i()->isAjax() )
{
Output::i()->output = $activeTabContents;
return;
}
Output::i()->title = Member::loggedIn()->language()->addToStack( 'dev_themeeditor' );
Output::i()->output = Theme::i()->getTemplate( 'global' )->tabs( $this->tabs, $this->activeTab, $activeTabContents, $this->url );
}
/**
* Editor Settings tab
* @return string
*/
protected function _manageSettings() : string
{
$table = new TableDb( 'core_theme_editor_settings', $this->url, array( array( 'setting_set_id=?', $this->theme->id ) ) );
$table->include = [ 'setting_name', 'setting_type', 'setting_category_id' ];
$table->noSort = $table->include;
$table->rootButtons = [
'add' => [
'icon' => 'plus',
'title' => 'add',
'link' => $this->url->setQueryString( array( 'do' => 'form', 'subnode' => 1 ) )
]
];
$table->rowButtons = function( $row )
{
return Setting::constructFromData( $row )->getButtons( $this->url, true );
};
$table->parsers = [
'setting_name' => function( $val, $row )
{
return Member::loggedIn()->language()->addToStack( Setting::$titleLangPrefix . $row['setting_id'] );
},
'setting_type' => function( $val )
{
return Member::loggedIn()->language()->addToStack( 'themeeditor_setting_type__' . $val );
},
'setting_category_id' => function( $val )
{
return Member::loggedIn()->language()->addToStack( Category::$titleLangPrefix . $val );
}
];
return (string) $table;
}
/**
* Editor Categories tab
* @return string
*/
protected function _manageCategories() : string
{
$table = new TableDb( 'core_theme_editor_categories', $this->url, array( array( 'cat_set_id=?', $this->theme->id ) ) );
$table->include = [ 'cat_name' ];
$table->noSort = [ 'cat_name' ];
$table->langPrefix = 'themeeditor_';
$table->rootButtons = [
'add' => [
'icon' => 'plus',
'title' => 'add',
'link' => $this->url->setQueryString( array( 'do' => 'form' ) )
]
];
$table->rowButtons = function( $row )
{
$buttons = Category::constructFromData( $row )->getButtons( $this->url );
if( isset( $buttons['add'] ) )
{
unset( $buttons['add'] );
}
if( isset( $buttons['child'] ) )
{
unset( $buttons['child'] );
}
return $buttons;
};
$table->parsers = [
'cat_name' => function( $val, $row )
{
return "<i class='" . Category::constructFromData( $row )->icon() ."'></i> " . Member::loggedIn()->language()->addToStack( Category::$titleLangPrefix . $row['cat_id'] );
}
];
return (string) $table;
}
}
File diff suppressed because it is too large. Load diff
@@ -1,541 +0,0 @@
<?php
/**
* @brief theme Settings
* @author <a href='https://www.invisioncommunity.com'>Invision Power Services, Inc.</a>
* @copyright (c) Invision Power Services, Inc.
* @license https://www.invisioncommunity.com/legal/standards/
* @package Invision Community
* @since 17 May 2013
*/
namespace IPS\core\modules\admin\customization;
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
exit;
}
/**
* themesettings
*/
class _themesettings extends \IPS\Dispatcher\Controller
{
/**
* @brief Has been CSRF-protected
*/
public static $csrfProtected = TRUE;
/**
* Execute
*
* @return void
*/
public function execute()
{
\IPS\Dispatcher::i()->checkAcpPermission( 'theme_sets_manage' );
parent::execute();
/* Display */
\IPS\Output::i()->breadcrumb = array(
array(
\IPS\Http\Url::internal('app=core&module=customization&controller=themes'),
'menu__' . \IPS\Dispatcher::i()->application->directory . '_' . \IPS\Dispatcher::i()->module->key
),
array(
NULL,
\IPS\Member::loggedIn()->language()->addToStack( 'core_theme_set_title_' . \intval( \IPS\Request::i()->set_id ) )
)
);
}
/**
* Manage
*
* @return void
*/
protected function manage()
{
/* Load Theme */
try
{
$theme = \IPS\Theme::load( \IPS\Request::i()->set_id );
}
catch ( \OutOfRangeException $e )
{
\IPS\Output::i()->error( 'node_error', '2C260/1', 404, '' );
}
if ( \IPS\Theme::designersModeEnabled() )
{
\IPS\Theme\Advanced\Theme::loadLanguage( $theme->id );
}
/* What tabs do we have? */
$tabs = array();
foreach ( \IPS\Db::i()->select( 'sc_tab_key', 'core_theme_settings_fields', array( 'sc_set_id=?', $theme->id ), NULL, NULL, 'sc_tab_key' ) as $tab )
{
$tabs[ $tab ] = 'theme_custom_tab_' . $tab;
}
/* Which is the active tab? */
if ( isset( \IPS\Request::i()->tab ) and isset( $tabs[ \IPS\Request::i()->tab ] ) )
{
$activeTab = \IPS\Request::i()->tab;
}
else
{
$_tabs = array_keys( $tabs );
$activeTab = array_shift( $_tabs );
}
/* Create tree */
$self = $this;
$tree = new \IPS\Helpers\Tree\Tree(
\IPS\Http\Url::internal( "app=core&module=customization&controller=themesettings&set_id={$theme->id}" ),
"TITLE",
// getRoots
function() use( $theme, $activeTab, $self )
{
$return = array();
foreach( \IPS\Db::i()->select( '*', 'core_theme_settings_fields', array( 'sc_set_id=? AND sc_tab_key=?', $theme->id, $activeTab ), 'sc_order' ) as $row )
{
$return[ $row['sc_id'] ] = $self->_settingRow( $row );
}
return $return;
},
// getRow
function( $id ) use ( $self )
{
return $self->_settingRow( \IPS\Db::i()->select( '*', 'core_theme_settings_fields', array( 'sc_id=?', $id ) )->first() );
},
// getRowParentId
function()
{
return NULL;
},
// getChildren
function()
{
return array();
},
// getRootButtons
function()
{
return array();
},
FALSE,
TRUE,
TRUE
);
/* If this is an AJAX request, just return it */
if( \IPS\Request::i()->isAjax() )
{
\IPS\Output::i()->output = (string) $tree;
return;
}
/* Output */
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'global' )->tabs( $tabs, $activeTab, $tree, \IPS\Http\Url::internal( "app=core&module=customization&controller=themesettings&set_id={$theme->id}" ) );
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack('theme_custom_setting_page_title');
if ( \IPS\Member::loggedIn()->hasAcpRestriction( 'core', 'customization', 'theme_settings_add' ) )
{
\IPS\Output::i()->sidebar['actions']['add'] = array(
'icon' => 'plus',
'title' => 'theme_custom_setting_add',
'data' => array( 'ipsDialog' => '', 'ipsDialog-title' => \IPS\Member::loggedIn()->language()->addToStack('theme_custom_setting_add') ),
'link' => \IPS\Http\Url::internal("app=core&module=customization&controller=themesettings&set_id={$theme->id}&do=form"),
);
}
}
/**
* Get row for tree
*
* @param array $setting The row from the database
* @return string
*/
public function _settingRow( $setting )
{
$buttons = array();
if ( \IPS\Member::loggedIn()->hasAcpRestriction( 'core', 'customization', 'theme_settings_add' ) )
{
$buttons['edit'] = array(
'icon' => 'pencil',
'title' => 'edit',
'data' => array( 'ipsDialog' => '', 'ipsDialog-title' => \IPS\Member::loggedIn()->language()->addToStack( $setting['sc_title'] ) ),
'link' => \IPS\Http\Url::internal( 'app=core&module=customization&controller=themesettings&do=form&id=' ) . $setting['sc_id'] . '&set_id=' . $setting['sc_set_id'],
);
}
if ( \IPS\Member::loggedIn()->hasAcpRestriction( 'core', 'customization', 'theme_settings_remove' ) )
{
$buttons['delete'] = array(
'icon' => 'times-circle',
'title' => 'delete',
'link' => \IPS\Http\Url::internal( 'app=core&module=customization&controller=themesettings&do=delete&id=' ) . $setting['sc_id'] . '&set_id=' . $setting['sc_set_id'],
'data' => array( 'delete' => '' ),
);
}
return \IPS\Theme::i()->getTemplate( 'trees', 'core' )->row(
\IPS\Http\Url::internal( "app=core&module=customization&controller=themesettings&set_id={$setting['sc_set_id']}" ),
$setting['sc_id'],
\IPS\Member::loggedIn()->language()->addToStack( $setting['sc_title'] ),
FALSE,
$buttons,
$setting['sc_key'],
NULL,
$setting['sc_order'],
FALSE,
NULL,
NULL,
NULL,
FALSE,
FALSE,
FALSE
);
}
/**
* Tree sorting calls to do=reorder, but we want to let the individual tabs handle that in this case.
* This method just takes the request and passes it to manage(), which in turn passes it to the correct tab, which then finally handles the reordering.
*
* @return void
*/
public function reorder()
{
\IPS\Session::i()->csrfCheck();
/* Normalise AJAX vs non-AJAX */
if( isset( \IPS\Request::i()->ajax_order ) )
{
$order = array();
$position = 1;
foreach( \IPS\Request::i()->ajax_order as $id => $parent )
{
$order[ $id ] = $position++;
}
}
/* Non-AJAX way */
else
{
$order = \IPS\Request::i()->order;
}
/* Okay, now order */
foreach( $order as $id => $position )
{
\IPS\Db::i()->update( 'core_theme_settings_fields', array( 'sc_order' => $position ), array( 'sc_id=?', $id ) );
}
/* Write themesettings.json for the default theme */
if ( \IPS\IN_DEV and \IPS\Request::i()->set_id == 1 )
{
\IPS\Theme\Dev\Theme::writeThemeSettingsToDisk();
}
/* If this is an AJAX request, just respond */
if( \IPS\Request::i()->isAjax() )
{
return;
}
/* Otherwise, redirect */
else
{
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( "app=core&module=customization&controller=themesettings&set_id=" . \IPS\Request::i()->set_id ) );
}
}
/**
* Add/Edit Theme Setting
*
* @return void
*/
public function form()
{
$current = NULL;
$setId = \intval( \IPS\Request::i()->set_id );
$id = \intval( \IPS\Request::i()->id );
$set = \IPS\Theme::load( $setId );
if ( $id )
{
$current = \IPS\Db::i()->select( '*', 'core_theme_settings_fields', array( 'sc_id=?', $id ) )->first();
}
else
{
/* Check permission */
\IPS\Dispatcher::i()->checkAcpPermission( 'theme_settings_add' );
}
/* Apps */
$apps = array();
foreach( \IPS\Application::applications() as $key => $data )
{
$apps[ $key ] = $data->_title;
}
/* Tab keys */
$tabs = iterator_to_array( \IPS\Db::i()->select( 'sc_tab_key', 'core_theme_settings_fields', array( 'sc_set_id=?', $setId ) )->setKeyField( 'sc_tab_key' )->setValueField('sc_tab_key') );
/* Build form */
$form = new \IPS\Helpers\Form();
$form->hiddenValues['set_id'] = $setId;
$form->add( new \IPS\Helpers\Form\Text( 'theme_custom_setting_title', $id ? $current['sc_title'] : null, TRUE ) );
$form->add( new \IPS\Helpers\Form\Select( 'theme_custom_setting_app', $id ? $current['sc_app'] : null, TRUE, array( 'options' => $apps ) ) );
$form->add( new \IPS\Helpers\Form\Text( 'theme_custom_setting_key' , ( $id ? $current['sc_key'] : null ), TRUE, array(
'placeholder' => \IPS\Member::loggedIn()->language()->addToStack('theme_custom_setting_key_placeholder')
),
function( $val )
{
/* Check key format */
if( !preg_match( '/^[a-zA-Z0-9_]+$/', $val ) )
{
throw new \InvalidArgumentException('core_theme_settings_error_key_disallowed_characters');
}
/* Make sure key is unique */
try
{
$row = \IPS\Db::i()->select( '*', 'core_theme_settings_fields', array( "sc_set_id=? AND sc_key=?", \IPS\Request::i()->set_id, $val ) )->first();
if ( isset( \IPS\Request::i()->id ) )
{
if ( $row['sc_id'] != \IPS\Request::i()->id )
{
throw new \InvalidArgumentException('core_theme_settings_error_key_not_unique');
}
}
else
{
throw new \InvalidArgumentException('core_theme_settings_error_key_not_unique');
}
}
catch( \UnderflowException $e )
{
/* Key is OK as select failed */
}
return true;
} ) );
if ( \count( $tabs ) )
{
$form->add( new \IPS\Helpers\Form\Radio( 'theme_custom_setting_tab_key_type', 'existing', FALSE, array(
'options' => array( 'existing' => 'theme_custom_setting_tab_key_o_existing',
'new' => 'theme_custom_setting_tab_key_o_new' ),
'toggles' => array( 'existing' => array( 'theme_template_tab_key_existing' ),
'new' => array( 'theme_template_tab_key_new' ) )
) ) );
$form->add( new \IPS\Helpers\Form\Select( 'theme_template_tab_key_existing', $id ? $current['sc_tab_key'] : null, TRUE, array( 'options' => array_map( function( $item )
{
return ( \IPS\Member::loggedIn()->language()->addToStack('theme_custom_tab_' . $item) ) ?: $item;
}, $tabs ) ), NULL, NULL, NULL, 'theme_template_tab_key_existing' ) );
}
else
{
$form->hiddenValues['theme_custom_setting_tab_key_type'] = 'new';
}
$form->add( new \IPS\Helpers\Form\Text( 'theme_template_tab_key_new', NULL, FALSE, array( 'regex' => '#[0-9a-z_]+#' ),
function( $val )
{
$val = preg_replace( '#[^0-9a-z_]#', '', mb_strtolower( $val ) );
if ( \IPS\Request::i()->theme_custom_setting_tab_key_type === 'new' )
{
/* Make sure its unique */
try
{
$row = \IPS\Db::i()->select( '*', 'core_theme_settings_fields', array( "sc_set_id=? AND sc_tab_key=?", \IPS\Request::i()->set_id, $val ) )->first();
if ( isset( \IPS\Request::i()->id ) )
{
if ( $row['sc_id'] != \IPS\Request::i()->id )
{
throw new \InvalidArgumentException('core_theme_settings_error_tab_key_not_unique');
}
}
}
catch( \UnderflowException $e )
{
/* Key is OK as select failed */
}
}
return true;
}, 'theme_custom_tab_', NULL, 'theme_template_tab_key_new' ) );
$form->add( new \IPS\Helpers\Form\Select( 'theme_custom_setting_type', $id ? $current['sc_type'] : 'Text', TRUE, array(
'options' => array(
'Editor' => 'pf_type_Editor',
'Number' => 'pf_type_Number',
'Radio' => 'pf_type_Radio',
'Select' => 'pf_type_Select',
'Text' => 'pf_type_Text',
'TextArea' => 'pf_type_TextArea',
'Upload' => 'pf_type_Upload',
'Url' => 'pf_type_Url',
'Color' => 'pf_type_Color',
'YesNo' => 'pf_type_YesNo',
'other' => 'theme_custom_setting_type_other',
),
'toggles' => array(
'Radio' => array( 'sc_content' ),
'Select' => array( 'sc_content', 'sc_multiple' ),
'other' => array( 'sc_manual_code' )
)
) ) );
$form->add( new \IPS\Helpers\Form\Stack( 'theme_custom_setting_type_content', ( $id and $current['sc_type'] != 'other' ) ? json_decode( $current['sc_content'], TRUE ) : array(), FALSE, array( 'stackFieldType' => 'KeyValue' ), NULL, NULL, NULL, 'sc_content' ) );
$form->add( new \IPS\Helpers\Form\YesNo( 'theme_custom_setting_type_multiple', $id ? $current['sc_multiple'] : FALSE, FALSE, array(), NULL, NULL, NULL, 'sc_multiple' ) );
$form->add( new \IPS\Helpers\Form\Codemirror( 'theme_custom_setting_manual_code', '<?php' . "\n\n" . ( ( $id and $current['sc_type'] == 'other' ) ? $current['sc_content'] : 'return new \IPS\Helpers\Form\Text( "core_theme_setting_title_{$row[\'sc_id\']}", $value, FALSE, array(), NULL, NULL, NULL, \'theme_setting_\' . $row[\'sc_key\'] );' ), NULL, array( 'mode' => 'php' ), function( $val ) use ( $set )
{
$val = trim( preg_replace( '/^<\?php(.*)$/is', '$1', trim( $val ) ) );
if ( $val )
{
$row = array( 'sc_id' => 0, 'sc_key' => \IPS\Request::i()->theme_custom_setting_key );
$value = NULL;
$theme = $set;
if ( !( eval( $val ) instanceof \IPS\Helpers\Form\FormAbstract ) )
{
throw new \DomainException('theme_custom_setting_manual_code_bad');
}
}
elseif ( \IPS\Request::i()->theme_custom_setting_type == 'other' )
{
throw new \DomainException('form_required');
}
}, NULL, NULL, 'sc_manual_code' ) );
$form->add( new \IPS\Helpers\Form\Text( 'theme_custom_setting_type_default', $id ? $current['sc_default'] : FALSE, FALSE, array(), NULL, NULL, NULL, 'sc_default' ) );
if ( \IPS\IN_DEV )
{
$form->add( new \IPS\Helpers\Form\YesNo( 'theme_custom_setting_show_in_vse', $id ? $current['sc_show_in_vse'] : FALSE, FALSE, array(), NULL, NULL, NULL, 'sc_show_in_vse' ) );
}
$form->add( new \IPS\Helpers\Form\YesNo( 'theme_custom_setting_conditional', $id ? $current['sc_condition'] : FALSE, FALSE, array( 'togglesOn' => array( 'theme_custom_setting_condition' ) ), NULL, NULL, NULL, 'theme_custom_setting_conditional' ) );
$form->add( new \IPS\Helpers\Form\Codemirror( 'theme_custom_setting_condition', '<?php' . "\n\n" . ( ( $id and $current['sc_condition'] ) ? $current['sc_condition'] : 'return TRUE;' ), FALSE, array( 'mode' => 'php' ), NULL, NULL, NULL, 'theme_custom_setting_condition' ) );
if ( $values = $form->values() )
{
$save = array(
'sc_set_id' => $setId,
'sc_key' => $values['theme_custom_setting_key'],
'sc_type' => $values['theme_custom_setting_type'],
'sc_default' => $values['theme_custom_setting_type_default'],
'sc_multiple' => $values['theme_custom_setting_type_multiple'],
'sc_updated' => time(),
'sc_app' => $values['theme_custom_setting_app'],
'sc_show_in_vse' => ( isset( $values['theme_custom_setting_show_in_vse'] ) ) ? $values['theme_custom_setting_show_in_vse'] : 0,
'sc_content' => ( $values['theme_custom_setting_type'] === 'other' ) ? trim( preg_replace( '/^<\?php(.*)$/is', '$1', trim( $values['theme_custom_setting_manual_code'] ) ) ) : json_encode( $values['theme_custom_setting_type_content'] ),
'sc_title' => $values['theme_custom_setting_title'],
'sc_condition' => $values['theme_custom_setting_conditional'] ? trim( preg_replace( '/^<\?php(.*)$/is', '$1', trim( $values['theme_custom_setting_condition'] ) ) ) : NULL
);
$type = ( isset( \IPS\Request::i()->theme_custom_setting_tab_key_type ) ) ? \IPS\Request::i()->theme_custom_setting_tab_key_type : $values['theme_custom_setting_tab_key_type'];
if ( $type === 'existing' )
{
$save['sc_tab_key'] = $values['theme_template_tab_key_existing'];
}
else
{
$save['sc_tab_key'] = mb_strtolower( $values['theme_template_tab_key_new'] );
}
if ( $current )
{
\IPS\Db::i()->update( 'core_theme_settings_fields', $save, array( 'sc_id=?', $current['sc_id'] ) );
$id = $current['sc_id'];
\IPS\Session::i()->log( 'acplogs__theme_setting_edited', array( $save['sc_key'] => FALSE ) );
}
else
{
$save['sc_order'] = \IPS\Db::i()->select( 'MAX(sc_order)', 'core_theme_settings_fields', array( 'sc_set_id=? AND sc_tab_key=?', $setId, $save['sc_tab_key'] ) )->first() + 1;
$id = \IPS\Db::i()->insert( 'core_theme_settings_fields', $save );
\IPS\Session::i()->log( 'acplogs__theme_setting_created', array( $save['sc_key'] => FALSE ) );
}
/* Default is changed, change value? */
if ( $current and ( $save['sc_default'] !== $current['sc_default'] ) )
{
try
{
$value = \IPS\Db::i()->select( '*', 'core_theme_settings_values', array( "sv_id=?", $id ) )->first();
if ( $value['sv_value'] == $current['sc_default'] )
{
\IPS\Db::i()->update( 'core_theme_settings_values', array( 'sv_value' => $save['sc_default'] ), array( "sv_id=?", $id ) );
}
}
catch( \UnderFlowException $e ) {}
}
if ( $type === 'new' )
{
\IPS\Lang::saveCustom( 'core', "theme_custom_tab_" . $save['sc_tab_key'], $values['theme_template_tab_key_new'] );
}
if ( \IPS\IN_DEV AND $setId === 1 )
{
\IPS\Theme\Dev\Theme::writeThemeSettingsToDisk();
}
if ( ! $current )
{
$field = $set->getCustomSettingField( array_merge( $save, array( 'sc_id' => $id ) ) );
$themeSettings = json_decode( $set->template_settings, TRUE );
$themeSettings[ $values['theme_custom_setting_key'] ] = $field::stringValue( $values['theme_custom_setting_type_default'] );
$set->template_settings = json_encode( $themeSettings );
$set->save();
}
/* Update children */
$set->updateChildrenThemeSettings();
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=core&module=customization&controller=themesettings&set_id=' . $setId ), 'saved' );
}
/* Display */
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'global' )->block( 'theme_set_custom_setting', $form, FALSE );
\IPS\Output::i()->title = \IPS\Member::loggedIn()->language()->addToStack( 'core_theme_set_title_' . $setId );
}
/**
* Delete
*
* @return void
*/
public function delete()
{
$setId = \intval( \IPS\Request::i()->set_id );
\IPS\Dispatcher::i()->checkAcpPermission( 'theme_settings_remove' );
/* Make sure the user confirmed the deletion */
\IPS\Request::i()->confirmedDelete();
try
{
$current = \IPS\Db::i()->select( '*', 'core_theme_settings_fields', array( 'sc_id=?', \IPS\Request::i()->id ) )->first();
\IPS\Session::i()->log( 'acplogs__theme_setting_deleted', array( $current['sc_key'] => FALSE ) );
\IPS\Db::i()->delete( 'core_theme_settings_fields', array( 'sc_id=?', \IPS\Request::i()->id ) );
}
catch ( \Exception $e ) { }
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=core&module=customization&controller=themesettings&set_id=' . $setId ) );
}
}