Version 4.1.2

This commit is contained in:
Neo committed 2025-12-19 05:32:19 -08:00
1 parent 3ba79295ee
commit 28bd025b35
1131 files changed
+63658 -20579

No files matched your search

@@ -46,7 +46,292 @@ class _templates extends \IPS\Dispatcher\Controller
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=cms&module=pages&controller=templates' ), 'completed' );
}
/**
* Import dialog
*
* @return void
*/
public function import()
{
$form = new \IPS\Helpers\Form( 'form', 'next' );
$form->add( new \IPS\Helpers\Form\Upload( 'cms_templates_import', NULL, FALSE, array( 'allowedFileTypes' => array( 'xml' ), 'temporary' => TRUE ), NULL, NULL, NULL, 'cms_templates_import' ) );
if ( $values = $form->values() )
{
if ( $values['cms_templates_import'] )
{
/* Move it to a temporary location */
$tempFile = tempnam( \IPS\TEMP_DIRECTORY, 'IPS' );
move_uploaded_file( $values['cms_templates_import'], $tempFile );
/* Initate a redirector */
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=cms&module=pages&controller=templates&do=importProcess&file=' . urlencode( $tempFile ) . '&key=' . md5_file( $tempFile ) ) );
}
else
{
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=cms&module=pages&controller=templates&do=manage' ) );
}
}
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'global', 'core', 'admin' )->block( 'cms_templates_import_title', $form, FALSE );
}
/**
* Import from upload
*
* @return void
*/
public function importProcess()
{
if ( !file_exists( \IPS\Request::i()->file ) or md5_file( \IPS\Request::i()->file ) !== \IPS\Request::i()->key )
{
\IPS\Output::i()->error( 'generic_error', '3T259/3', 403, '' );
}
/* Open XML file */
$xml = new \XMLReader;
$xml->open( \IPS\Request::i()->file );
$prefix = date('m') . '.' . date('d') . '.' . date('y') . '_';
if ( ! @$xml->read() )
{
\IPS\Output::i()->error( 'xml_upload_invalid', '2C163/1', 403, '' );
}
$xml->read();
while ( $xml->read() )
{
if ( $xml->name === 'template' )
{
$templates = \IPS\cms\Theme::i()->getRawTemplates( 'cms', NULL, NULL, \IPS\cms\Theme::RETURN_ALL );
$node = new \SimpleXMLElement( $xml->readOuterXML() );
$attrs = array();
foreach( $node->attributes() as $k => $v )
{
$attrs[ $k ] = (string) $v;
}
/* Any kids of your own? */
foreach( $node->children() as $k => $v )
{
$tryJson = json_decode( $v, TRUE );
$attrs[ $k ] = ( $tryJson ) ? $tryJson : (string) $v;
}
if ( isset( $attrs['template_title'] ) and isset( $attrs['template_location'] ) and isset( $attrs['template_group'] ) )
{
/* Got this template? */
if ( $attrs['template_location'] === 'database' )
{
/* Database templates are really governed by the group as they are a collection of templates */
if ( isset( $templates['cms'][$attrs['template_location']][$attrs['template_group']] ) )
{
$attrs['template_group'] = $prefix . $attrs['template_group'];
}
}
else
{
/* But post and block templates are not */
if ( isset( $templates['cms'][$attrs['template_location']][$attrs['template_group']] ) )
{
$exists = FALSE;
foreach ( $templates['cms'][$attrs['template_location']][$attrs['template_group']] as $key => $template )
{
if ( $template['template_title'] === $attrs['template_title'] )
{
$exists = TRUE;
break;
}
}
if ( $exists )
{
$attrs['template_title'] = $prefix . $attrs['template_title'];
}
}
}
$obj = new \IPS\cms\Templates;
$obj->location = $attrs['template_location'];
$obj->group = $attrs['template_group'];
$obj->title = $attrs['template_title'];
$obj->params = $attrs['template_params'];
$obj->content = $attrs['template_content'];
$obj->original_group = $attrs['template_original_group'];
$obj->type = 'template';
$obj->user_created = 1;
$obj->user_edited = 1;
$obj->desc = '';
$obj->save();
$obj->key = $obj->location . \IPS\Http\Url::seoTitle( $obj->group ) . '_' . \IPS\Http\Url::seoTitle( $obj->title ) . '_' . $obj->id;
$obj->save();
}
}
$xml->next();
}
/* Done */
\IPS\Output::i()->redirect( \IPS\Http\Url::internal( 'app=cms&module=pages&controller=templates&do=manage' ), 'cms_templates_imported' );
}
/**
* Export templates
*
* @return void
*/
public function export()
{
$form = new \IPS\Helpers\Form( 'form', 'next' );
$templates = array();
$form->addMessage( 'cms_templates_export_description', 'ipsMessage ipsMessage_information' );
foreach( \IPS\cms\Templates::getTemplates( \IPS\cms\Templates::RETURN_DATABASE_ONLY + \IPS\cms\Templates::RETURN_ALL ) as $template )
{
$title = \IPS\cms\Templates::readableGroupName( $template->group );
if ( $template->location === 'database' )
{
$templates['database'][ $template->group ] = $title;
}
else if ( $template->location === 'block' )
{
$templates['block'][ $template->key ] = $title . ' > ' . $template->title;
}
else if ( $template->location === 'page' )
{
$templates['page'][ $template->key ] = $title . ' > ' . $template->title;
}
}
foreach( $templates as $location => $data )
{
$form->add( new \IPS\Helpers\Form\CheckboxSet( 'templates_' . $location, FALSE, FALSE, array( 'options' => $data ) ) );
}
if ( $values = $form->values() )
{
$exportTemplates = array();
foreach( $templates as $location => $data )
{
if ( isset( $values[ 'templates_' . $location ] ) and count( $values[ 'templates_' . $location ] ) )
{
if ( $location === 'database' )
{
$tmp = \IPS\cms\Theme::i()->getRawTemplates( 'cms', array('database'), array_values( $values[ 'templates_' . $location ] ) );
foreach( $tmp['cms'] as $loc => $data )
{
if ( $loc === $location )
{
foreach ( $data as $key => $tdata )
{
foreach ( $tdata as $tkey => $tkdata )
{
$exportTemplates['cms'][ $location ][ $key ][ $tkey ] = $tkdata;
}
}
}
}
}
else
{
$tmp = \IPS\cms\Theme::i()->getRawTemplates( 'cms', array( $location ), NULL, \IPS\cms\Theme::RETURN_DATABASE_ONLY + \IPS\cms\Theme::RETURN_ALL );
foreach( $tmp['cms'] as $loc => $data )
{
if ( $loc === $location )
{
foreach( $data as $key => $tdata )
{
foreach( $tdata as $tkey => $tkdata )
{
if ( in_array( $tkey, $values[ 'templates_' . $location ] ) )
{
$exportTemplates['cms'][ $location ][ $key ][ $tkey ] = $tkdata;
}
}
}
}
}
}
}
}
if ( count( $exportTemplates ) )
{
/* Init */
$xml = new \XMLWriter;
$xml->openMemory();
$xml->setIndent( TRUE );
$xml->startDocument( '1.0', 'UTF-8' );
/* Root tag */
$xml->startElement('templates');
foreach ( $exportTemplates as $app => $location )
{
foreach ( $location as $key => $data )
{
foreach ( $data as $group => $template )
{
foreach( $template as $key => $templateData )
{
/* Initiate the <template> tag */
$xml->startElement( 'template' );
foreach ( $templateData as $k => $v )
{
if ( ! in_array( \substr( $k, 9 ), array( 'content', 'params' ) ) )
{
$xml->startAttribute( $k );
$xml->text( $v );
$xml->endAttribute();
}
}
/* Write (potential) HTML fields */
foreach( array( 'template_params', 'template_content' ) as $field )
{
if ( isset( $templateData[ $field ] ) )
{
$xml->startElement( $field );
if ( preg_match( '/<|>|&/', $templateData[ $field ] ) )
{
$xml->writeCData( str_replace( ']]>', ']]]]><![CDATA[>', $templateData[ $field ] ) );
}
else
{
$xml->text( $templateData[ $field ] );
}
$xml->endElement();
}
}
/* Close the <template> tag */
$xml->endElement();
}
}
}
}
/* Finish */
$xml->endDocument();
\IPS\Output::i()->sendOutput( $xml->outputMemory(), 200, 'application/xml', array( 'Content-Disposition' => \IPS\Output::getContentDisposition( 'attachment', "pages_templates.xml" ) ) );
}
else
{
\IPS\Output::i()->error( 'cms_no_templates_selected', '1T285/1', 403, '' );
}
}
\IPS\Output::i()->output = \IPS\Theme::i()->getTemplate( 'global', 'core', 'admin' )->block( 'cms_templates_export_title', $form, FALSE );
}
/**
* List templates
*
@@ -143,6 +428,20 @@ class _templates extends \IPS\Dispatcher\Controller
);
}
\IPS\Output::i()->sidebar['actions']['download'] = array(
'icon' => 'download',
'title' => 'cms_templates_export_title',
'link' => \IPS\Http\Url::internal( "app=cms&module=pages&controller=templates&do=export" ),
'data' => array( 'ipsDialog' => '', 'ipsDialog-title' => \IPS\Member::loggedIn()->language()->addToStack('cms_templates_export_title') )
);
\IPS\Output::i()->sidebar['actions']['upload'] = array(
'icon' => 'upload',
'title' => 'cms_templates_import_title',
'link' => \IPS\Http\Url::internal( "app=cms&module=pages&controller=templates&do=import" ),
'data' => array( 'ipsDialog' => '', 'ipsDialog-title' => \IPS\Member::loggedIn()->language()->addToStack('cms_templates_import_title') )
);
\IPS\Output::i()->jsFiles = array_merge( \IPS\Output::i()->jsFiles, \IPS\Output::i()->js( 'codemirror/diff_match_patch.js', 'core', 'interface' ) );
\IPS\Output::i()->jsFiles = array_merge( \IPS\Output::i()->jsFiles, \IPS\Output::i()->js( 'codemirror/codemirror.js', 'core', 'interface' ) );
\IPS\Output::i()->cssFiles = array_merge( \IPS\Output::i()->cssFiles, \IPS\Theme::i()->css( 'codemirror/codemirror.css', 'core', 'interface' ) );
@@ -214,7 +513,7 @@ class _templates extends \IPS\Dispatcher\Controller
$form = new \IPS\Helpers\Form();
$form->hiddenValues['type'] = $type;
$form->add( new \IPS\Helpers\Form\Text( 'template_title', NULL, TRUE ) );
$form->add( new \IPS\Helpers\Form\Text( 'template_title', NULL, TRUE, array( 'regex' => '/^([A-Z0-9_]+?)$/i' ) ) );
/* Very specific */
if ( $type === 'database' )
@@ -570,7 +869,10 @@ class _templates extends \IPS\Dispatcher\Controller
/* Clear guest page caches */
\IPS\Data\Cache::i()->clearAll();
/* Clear block caches */
\IPS\cms\Blocks\Block::deleteCompiled();
if( \IPS\Request::i()->isAjax() )
{
\IPS\Output::i()->json( array( 'template_id' => $obj->id, 'template_title' => $obj->title, 'template_container' => $obj->container, 'template_user_added' => $obj->user_created ) );