Invision Power Services, Inc. * @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc. * @license http://www.invisionpower.com/legal/standards/ * @package IPS Social Suite * @since 25 Feb 2013 * @version SVN_VERSION_NUMBER */ namespace IPS\cms\modules\admin\pages; /* 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; } /** * templates */ class _templates extends \IPS\Dispatcher\Controller { /** * Execute * * @return void */ public function execute() { \IPS\Dispatcher::i()->checkAcpPermission( 'template_manage' ); parent::execute(); } /** * Import the IN_DEV templates * * @return void */ public function importInDev() { \IPS\cms\Theme::importInDev('database'); \IPS\cms\Theme::importInDev('page'); \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