Page MenuHomeIn-Portal Phabricator

D220.id721.diff
No OneTemporary

File Metadata

Created
Wed, Feb 26, 9:05 AM

D220.id721.diff

Index: core/units/helpers/helpers_config.php
===================================================================
--- core/units/helpers/helpers_config.php
+++ core/units/helpers/helpers_config.php
@@ -78,5 +78,6 @@
Array ('pseudo' => 'AjaxFormHelper', 'class' => 'AjaxFormHelper', 'file' => 'ajax_form_helper.php', 'build_event' => ''),
Array ('pseudo' => 'kCronHelper', 'class' => 'kCronHelper', 'file' => 'cron_helper.php', 'build_event' => ''),
Array ('pseudo' => 'kUploadHelper', 'class' => 'kUploadHelper', 'file' => 'upload_helper.php', 'build_event' => ''),
+ array('pseudo' => 'TemplateCommentParser', 'class' => 'TemplateCommentParser', 'file' => 'template_comment_parser.php', 'build_event' => ''),
),
);
Index: core/units/helpers/template_comment_parser.php
===================================================================
--- /dev/null
+++ core/units/helpers/template_comment_parser.php
@@ -0,0 +1,176 @@
+<?php
+/**
+* @version $Id: image_helper.php 16247 2015-09-02 20:48:06Z alex $
+* @package In-Portal
+* @copyright Copyright (C) 1997 - 2017 Intechnic. All rights reserved.
+* @license GNU/GPL
+* In-Portal is Open Source software.
+* This means that this software may have been modified pursuant
+* the GNU General Public License, and as distributed it includes
+* or is derivative of works licensed under the GNU General Public License
+* or other free or open source software licenses.
+* See http://www.in-portal.org/license for copyright notices and details.
+*/
+
+defined('FULL_PATH') or die('restricted access!');
+
+class TemplateCommentParser
+{
+
+ /**
+ * Path to template
+ *
+ * @var string
+ */
+ protected $templateFile;
+
+ /**
+ * Supported settings
+ *
+ * @var array
+ */
+ protected $supportedSettings = array(
+ 'NAME' => array('type' => 'string', 'default' => ''),
+ 'DESC' => array('type' => 'string', 'default' => ''),
+ 'SECTION' => array('type' => 'category_path', 'default' => ''),
+ 'PRIORITY' => array('type' => 'integer', 'default' => 1),
+ 'INCLUDE_IN_SITEMAP' => array('type' => 'boolean', 'default' => false),
+ );
+
+ /**
+ * Gets and checks path to template
+ *
+ * @param string $template_file Template file.
+ *
+ * @throws Exception When template file not found.
+ */
+ public function __construct($template_file)
+ {
+ if ( !file_exists($template_file) ) {
+ throw new Exception('Template file "' . $template_file . '" not found.');
+ }
+
+ $this->templateFile = $template_file;
+ }
+
+ /**
+ * Parses template comment
+ *
+ * @return array
+ * @throws Exception When missing end of template comment.
+ * @throws Exception When invalid XML in the template comment.
+ * @throws Exception When unsupported setting in the template comment.
+ * @throws Exception When missing integer value in the template comment.
+ * @throws Exception When missing positive value in the template comment.
+ * @throws Exception When missing "yes" or "no" value in the template comment.
+ */
+ public function parse()
+ {
+ $result = $this->getDefaults();
+ $template_data = file_get_contents($this->templateFile);
+
+ if ( substr($template_data, 0, 6) != '<!--##' ) {
+ return $result;
+ }
+
+ // template starts with comment in such format
+ /*<!--##
+ <NAME></NAME>
+ <DESC></DESC>
+ <SECTION>||</SECTION>
+ ##-->*/
+
+ $comment_end = strpos($template_data, '##-->');
+
+ if ( $comment_end === false ) {
+ throw new Exception('Template file "' . $this->templateFile . '" missing end of comment.');
+ }
+
+ $comment = trim(substr($template_data, 6, $comment_end - 6));
+
+ if ( strpos($comment, PHP_EOL) === false ) {
+ return $result;
+ }
+
+ /** @var SimpleXMLElement[] $meta_info */
+ $meta_info = simplexml_load_string('<meta_info>' . $comment . '</meta_info>');
+
+ if ( $meta_info === false ) {
+ // Malformed XML. SimpleXML will print an error itself.
+ throw new Exception('Template file "' . $this->templateFile . '" has invalid XML in the comment.');
+ }
+
+ foreach ( $meta_info as $setting ) {
+ $setting_name = $setting->getName();
+
+ if ( !isset($this->supportedSettings[$setting_name]) ) {
+ throw new Exception(sprintf(
+ 'Template file "%s" has unsupported "%s" setting.',
+ $this->templateFile,
+ $setting_name
+ ));
+ }
+
+ $result_key = strtolower($setting_name);
+ $result[$result_key] = trim($setting);
+
+ switch ( $this->supportedSettings[$setting_name]['type'] ) {
+ case 'category_path':
+ $category_path = explode('||', $result[$result_key]);
+ $category_path = array_map('trim', $category_path);
+ $result[$result_key] = implode('||', $category_path);
+ break;
+
+ case 'integer':
+ $parsed = (int)$result[$result_key];
+
+ if ( (string)$parsed != $result[$result_key] ) {
+ throw new Exception(sprintf(
+ 'Template file "%s" must have integer value in "%s" setting.',
+ $this->templateFile,
+ $setting_name
+ ));
+ }
+
+ if ( $parsed < 1 ) {
+ throw new Exception(sprintf(
+ 'Template file "%s" may have only positive value in "%s" setting.',
+ $this->templateFile,
+ $setting_name
+ ));
+ }
+ break;
+
+ case 'boolean':
+ if ( $result[$result_key] !== 'yes' && $result[$result_key] !== 'no' ) {
+ throw new Exception(sprintf(
+ 'Template file "%s" must have "yes" or "no" value in "%s" setting.',
+ $this->templateFile,
+ $setting_name
+ ));
+ }
+
+ $result[$result_key] = $result[$result_key] == 'yes';
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * Get default values
+ *
+ * @return array
+ */
+ protected function getDefaults()
+ {
+ $result = array();
+
+ foreach ( $this->supportedSettings as $setting => $options ) {
+ $result[strtolower($setting)] = $options['default'];
+ }
+
+ return $result;
+ }
+
+}
Index: core/units/helpers/themes_helper.php
===================================================================
--- core/units/helpers/themes_helper.php
+++ core/units/helpers/themes_helper.php
@@ -376,7 +376,7 @@
elseif ( pathinfo($filename, PATHINFO_EXTENSION) == 'tpl' ) {
$meta_info = $this->_getTemplateMetaInfo(trim($file_path, '/'), $theme_id);
$file_id = isset($this->themeFiles[$file_path]) ? $this->themeFiles[$file_path] : false;
- $file_description = array_key_exists('desc', $meta_info) ? $meta_info['desc'] : '';
+ $file_description = $meta_info['desc'];
if ($file_id) {
// file was found in db & on hdd -> mark as existing
@@ -449,51 +449,9 @@
$template = 'theme:' . $this->_themeNames[$theme_id] . '/' . $template;
$template_file = $this->Application->TemplatesCache->GetRealFilename($template); // ".tpl" was added before
+ $template_comment_parser = $this->Application->makeClass('TemplateCommentParser', array($template_file));
- return $this->parseTemplateMetaInfo($template_file);
- }
-
- function parseTemplateMetaInfo($template_file)
- {
- if (!file_exists($template_file)) {
- // when template without info it's placed in top category
- return Array ();
- }
-
- $template_data = file_get_contents($template_file);
-
- if (substr($template_data, 0, 6) == '<!--##') {
- // template starts with comment in such format
- /*<!--##
- <NAME></NAME>
- <DESC></DESC>
- <SECTION>||</SECTION>
- ##-->*/
-
- $comment_end = strpos($template_data, '##-->');
- if ($comment_end === false) {
- // badly formatted comment
- return Array ();
- }
-
- $comment = trim( substr($template_data, 6, $comment_end - 6) );
- if (preg_match_all('/<(NAME|DESC|SECTION)>(.*?)<\/(NAME|DESC|SECTION)>/is', $comment, $regs)) {
- $ret = Array ();
- foreach ($regs[1] as $param_order => $param_name) {
- $ret[ strtolower($param_name) ] = trim($regs[2][$param_order]);
- }
-
- if (array_key_exists('section', $ret) && $ret['section']) {
- $category_path = explode('||', $ret['section']);
- $category_path = array_map('trim', $category_path);
- $ret['section'] = implode('||', $category_path);
- }
-
- return $ret;
- }
- }
-
- return Array ();
+ return $template_comment_parser->parse();
}
/**
Index: core/units/theme_files/theme_file_eh.php
===================================================================
--- core/units/theme_files/theme_file_eh.php
+++ core/units/theme_files/theme_file_eh.php
@@ -119,13 +119,11 @@
fwrite($fp, $object->GetDBField('FileContents'));
fclose($fp);
- /** @var kThemesHelper $themes_helper */
- $themes_helper = $this->Application->recallObject('ThemesHelper');
+ /** @var TemplateCommentParser $template_comment_parser */
+ $template_comment_parser = $this->Application->makeClass('TemplateCommentParser', array($filename));
+ $meta_info = $template_comment_parser->parse();
- $meta_info = $themes_helper->parseTemplateMetaInfo($filename);
- $file_description = array_key_exists('desc', $meta_info) ? $meta_info['desc'] : '';
-
- $object->SetDBField('Description', $file_description);
+ $object->SetDBField('Description', $meta_info['desc']);
$object->SetDBField('FileMetaInfo', serialize($meta_info));
$object->Update();
}

Event Timeline