Reimplement skin XML parser

This commit is contained in:
Kijin Sung 2025-06-01 15:28:37 +09:00
parent 07e3298eb1
commit b849c597bc
3 changed files with 184 additions and 236 deletions

View file

@ -121,7 +121,7 @@ abstract class BaseParser
$group_name = $extra_vars->getName() === 'group' ? self::_getChildrenByLang($extra_vars, 'title', $lang) : null;
foreach ($extra_vars->group ?: [] as $group)
{
$group_result = self::_getExtraVars($group, $lang, $type);
$group_result = self::_getExtraVars($group, $lang, $type, $options);
foreach ($group_result as $key => $val)
{
$result->{$key} = $val;
@ -173,13 +173,36 @@ abstract class BaseParser
{
$item->default = self::_getChildrenByLang($var, 'default', $lang);
}
$item->value = null;
if ($type === 'skin')
{
$item->value = trim($var['value'] ?? '') ?: null;
if ($item->value && preg_match('/(,|\|@\|)/', $item->value ?? '', $delimiter))
{
$item->value = explode($delimiter[1], $item->value);
}
if ($item->type === 'mid_list' && !is_array($item->value))
{
$item->value = [$item->value];
}
}
else
{
$item->value = null;
}
// Options
if ($var->options)
if ($type === 'skin' && $options['version'] === '0.1')
{
$item->options = array();
foreach ($var->options as $option)
$xml_options = $var->default ?? null;
}
else
{
$xml_options = $var->options ?? null;
}
if ($xml_options)
{
$item->options = [];
foreach ($xml_options as $option)
{
if ($type === 'widget' || $type === 'widgetstyle')
{
@ -207,10 +230,24 @@ abstract class BaseParser
}
}
$title = self::_getChildrenByLang($option, 'title', $lang);
$value = trim($option['value'] ?? '') ?: trim($option->value ?? '');
$value = trim($option['value'] ?? '');
$option_item->val = $title;
$item->options[$value] = $option_item;
}
elseif ($type === 'skin' && $options['version'] === '0.1')
{
$option_item = new \stdClass;
$option_item->title = trim($option);
$option_item->value = trim($option);
$item->options[] = $option_item; // Numeric keys only
}
elseif ($type === 'skin' && $options['version'] === '0.2')
{
$option_item = new \stdClass;
$option_item->title = self::_getChildrenByLang($option, 'title', $lang);
$option_item->value = trim($option['value'] ?? '');
$item->options[] = $option_item; // Numeric keys only
}
else
{
$option_item = new \stdClass;
@ -221,6 +258,17 @@ abstract class BaseParser
}
}
// Other attributes
if ($type === 'skin' && $options['version'] === '0.1')
{
$item->width = intval($var['width'] ?? 0) ?: null;
$item->height = intval($var['height'] ?? 0) ?: null;
if (isset($item->options) && count($item->options))
{
$item->default = reset($item->options)->value;
}
}
// Add to list of variables
if ($type === 'widget' || $type === 'widgetstyle')
{

View file

@ -0,0 +1,122 @@
<?php
namespace Rhymix\Framework\Parsers;
/**
* Skin (info.xml) parser class for XE compatibility.
*/
class SkinInfoParser extends BaseParser
{
/**
* Load an XML file.
*
* @param string $filename
* @param string $skin_name
* @param string $skin_path
* @param string $lang
* @return ?object
*/
public static function loadXML(string $filename, string $skin_name, string $skin_path, string $lang = ''): ?object
{
// Load the XML file.
$xml = simplexml_load_string(file_get_contents($filename));
if ($xml === false)
{
return null;
}
// Get the current language.
$lang = $lang ?: (\Context::getLangType() ?: 'en');
// Initialize the layout definition.
$info = new \stdClass;
$info->skin = $skin_name;
$info->path = $skin_path;
// Get the XML schema version.
$version = strval($xml['version']) ?: '0.1';
// Parse version 0.2
if ($version === '0.2')
{
$info->title = self::_getChildrenByLang($xml, 'title', $lang) ?: $skin_name;
$info->description = self::_getChildrenByLang($xml, 'description', $lang);
$info->version = trim($xml->version);
$info->date = ($xml->date === 'RX_CORE') ? '' : date('Ymd', strtotime($xml->date . 'T12:00:00Z'));
$info->homepage = trim($xml->link);
$info->license = trim($xml->license);
$info->license_link = trim($xml->license['link'] ?? '');
$info->author = array();
foreach ($xml->author as $author)
{
$author_info = new \stdClass;
$author_info->name = self::_getChildrenByLang($author, 'name', $lang);
$author_info->email_address = trim($author['email_address'] ?? '');
$author_info->homepage = trim($author['link'] ?? '');
$info->author[] = $author_info;
}
}
// Parse version 0.1
else
{
$info->title = self::_getChildrenByLang($xml, 'title', $lang) ?: $skin_name;
$info->description = self::_getChildrenByLang($xml->maker, 'description', $lang);
$info->version = trim($xml['version'] ?? '');
$info->date = date('Ymd', strtotime($xml->maker['date'] . 'T12:00:00Z'));
$info->homepage = trim($xml->link);
$info->license = trim($xml->license);
$info->license_link = trim($xml->license['link'] ?? '');
$info->author = array();
$author_info = new \stdClass;
$author_info->name = self::_getChildrenByLang($xml->maker, 'name', $lang);
$author_info->email_address = trim($xml->maker['email_address']);
$author_info->homepage = trim($xml->maker['link'] ?? '');
$info->author[] = $author_info;
}
// Get extra_vars.
if ($xml->extra_vars)
{
$info->extra_vars = get_object_vars(self::_getExtraVars($xml->extra_vars, $lang, 'skin', ['version' => $version]));
}
else
{
$info->extra_vars = [];
}
// Get colorsets.
if ($xml->colorset && $xml->colorset->color)
{
$info->colorset = [];
foreach ($xml->colorset->color as $color)
{
$color_item = new \stdClass;
$color_item->name = trim($color['name'] ?? '');
$color_item->title = self::_getChildrenByLang($color, 'title', $lang);
$screenshot = trim($color['src'] ?? '');
if ($screenshot)
{
$screenshot = $info->path . $screenshot;
}
$color_item->screenshot = $screenshot;
$info->colorset[] = $color_item;
}
}
// Get thumbnail path.
if (file_exists($info->path . 'thumbnail.png'))
{
$info->thumbnail = $info->path . 'thumbnail.png';
}
else
{
$info->thumbnail = '';
}
// Return the complete result.
return $info;
}
}

View file

@ -1019,245 +1019,23 @@ class ModuleModel extends Module
public static function loadSkinInfo($path, $skin, $dir = 'skins')
{
// Read xml file having skin information
if(substr($path,-1)!='/') $path .= '/';
if(!preg_match('/^[a-zA-Z0-9_-]+$/', $skin ?? ''))
if (!str_ends_with($path, '/'))
{
return;
$path .= '/';
}
$skin_xml_file = sprintf("%s%s/%s/skin.xml", $path, $dir, $skin);
if(!file_exists($skin_xml_file))
if (!preg_match('/^[a-zA-Z0-9_-]+$/', $skin ?? ''))
{
return;
}
// Create XmlParser object
$_xml_obj = Rhymix\Framework\Parsers\XEXMLParser::loadXMLFile($skin_xml_file);
// Return if no skin information is
if(!$_xml_obj->skin) return;
$xml_obj = $_xml_obj->skin;
// Skin Name
$skin_info = new stdClass();
$skin_info->title = $xml_obj->title->body;
$skin_info->author = array();
$skin_info->extra_vars = array();
$skin_info->colorset = array();
// Author information
if($xml_obj->version && $xml_obj->attrs->version == '0.2')
$skin_path = sprintf("%s%s/%s/", $path, $dir, $skin);
$skin_xml_file = $skin_path . 'skin.xml';
if (!file_exists($skin_xml_file))
{
// skin format v0.2
$date_obj = (object)array('y' => 0, 'm' => 0, 'd' => 0);
sscanf($xml_obj->date->body ?? null, '%d-%d-%d', $date_obj->y, $date_obj->m, $date_obj->d);
$skin_info->version = $xml_obj->version->body ?? null;
$skin_info->date = sprintf('%04d%02d%02d', $date_obj->y, $date_obj->m, $date_obj->d);
$skin_info->homepage = $xml_obj->link->body ?? null;
$skin_info->license = $xml_obj->license->body ?? null;
$skin_info->license_link = $xml_obj->license->attrs->link ?? null;
$skin_info->description = $xml_obj->description->body ?? null;
if(!is_array($xml_obj->author ?? null)) $author_list = array($xml_obj->author);
else $author_list = $xml_obj->author;
foreach($author_list as $author)
{
$author_obj = new stdClass();
$author_obj->name = $author->name->body ?? null;
$author_obj->email_address = $author->attrs->email_address ?? null;
$author_obj->homepage = $author->attrs->link ?? null;
$skin_info->author[] = $author_obj;
}
// List extra vars
if($xml_obj->extra_vars)
{
$extra_var_groups = $xml_obj->extra_vars->group;
if(!$extra_var_groups) $extra_var_groups = $xml_obj->extra_vars;
if(!is_array($extra_var_groups)) $extra_var_groups = array($extra_var_groups);
foreach($extra_var_groups as $group)
{
$extra_vars = $group->var;
if(!$extra_vars)
{
continue;
}
if(!is_array($group->var))
{
$extra_vars = array($group->var);
}
foreach($extra_vars as $key => $val)
{
$obj = new stdClass;
$obj->group = $group->title->body ?? null;
$obj->name = $val->attrs->name ?? null;
$obj->title = $val->title->body ?? null;
$obj->type = ($val->attrs->type ?? null) ?: 'text';
$obj->description = $val->description->body ?? null;
$obj->value = $val->attrs->value ?? null;
$obj->default = $val->attrs->default ?? null;
if(preg_match('/,|\|@\|/', $obj->value ?? '', $delimiter) && $delimiter[0])
{
$obj->value = explode($delimiter[0], $obj->value);
}
if($obj->type == 'mid_list' && !is_array($obj->value))
{
$obj->value = array($obj->value);
}
// Get an option list from 'select'type
if(is_array($val->options))
{
$option_count = count($val->options);
for($i = 0; $i < $option_count; $i++)
{
$obj->options[$i] = new stdClass();
$obj->options[$i]->title = $val->options[$i]->title->body ?? null;
$obj->options[$i]->value = $val->options[$i]->attrs->value ?? null;
}
}
else
{
$obj->options[0] = new stdClass();
$obj->options[0]->title = $val->options->title->body ?? null;
$obj->options[0]->value = $val->options->attrs->value ?? null;
}
$skin_info->extra_vars[] = $obj;
}
}
}
}
else
{
// skin format v0.1
$date_obj = (object)array('y' => 0, 'm' => 0, 'd' => 0);
if (isset($xml_obj->maker->attrs->date))
{
sscanf($xml_obj->maker->attrs->date, '%d-%d-%d', $date_obj->y, $date_obj->m, $date_obj->d);
}
$skin_info->version = $xml_obj->version->body ?? null;
$skin_info->date = sprintf('%04d%02d%02d', $date_obj->y, $date_obj->m, $date_obj->d);
$skin_info->homepage = $xml_obj->link->body ?? null;
$skin_info->license = $xml_obj->license->body ?? null;
$skin_info->license_link = $xml_obj->license->attrs->link ?? null;
$skin_info->description = $xml_obj->maker->description->body ?? null;
$skin_info->author[0] = new stdClass();
$skin_info->author[0]->name = $xml_obj->maker->name->body ?? null;
$skin_info->author[0]->email_address = $xml_obj->maker->attrs->email_address ?? null;
$skin_info->author[0]->homepage = $xml_obj->maker->attrs->link ?? null;
// Variables used in the skin
$extra_var_groups = $xml_obj->extra_vars->group ?? null;
if(!$extra_var_groups) $extra_var_groups = $xml_obj->extra_vars ?? null;
if(!is_array($extra_var_groups)) $extra_var_groups = array($extra_var_groups);
foreach($extra_var_groups as $group)
{
$extra_vars = $group->var ?? null;
if($extra_vars)
{
if(!is_array($extra_vars)) $extra_vars = array($extra_vars);
foreach($extra_vars as $var)
{
$options = array();
$group = $group->title->body;
$name = $var->attrs->name;
$type = $var->attrs->type;
$title = $var->title->body;
$description = $var->description->body;
// Get an option list from 'select'type.
if(is_array($var->default))
{
$option_count = count($var->default);
for($i = 0; $i < $option_count; $i++)
{
$options[$i] = new stdClass();
$options[$i]->title = $var->default[$i]->body;
$options[$i]->value = $var->default[$i]->body;
}
}
else
{
$options[0] = new stdClass();
$options[0]->title = $var->default->body;
$options[0]->value = $var->default->body;
}
$width = $var->attrs->width;
$height = $var->attrs->height;
$obj = new stdClass();
$obj->group = $group;
$obj->title = $title;
$obj->description = $description;
$obj->name = $name;
$obj->type = $type;
$obj->options = $options;
$obj->width = $width;
$obj->height = $height;
$obj->default = $options[0]->value;
$skin_info->extra_vars[] = $obj;
}
}
}
return;
}
// colorset
$colorset = $xml_obj->colorset->color ?? null;
if($colorset)
{
if(!is_array($colorset)) $colorset = array($colorset);
foreach($colorset as $color)
{
$name = $color->attrs->name;
$title = $color->title->body;
$screenshot = $color->attrs->src;
if($screenshot)
{
$screenshot = sprintf("%s%s/%s/%s", $path, $dir, $skin, $screenshot);
if(!file_exists($screenshot)) $screenshot = "";
}
else $screenshot = "";
$obj = new stdClass();
$obj->name = $name;
$obj->title = $title;
$obj->screenshot = $screenshot;
$skin_info->colorset[] = $obj;
}
}
// Menu type (settings for layout)
if($xml_obj->menus->menu ?? null)
{
$menus = $xml_obj->menus->menu;
if(!is_array($menus)) $menus = array($menus);
$menu_count = count($menus);
$skin_info->menu_count = $menu_count;
for($i=0;$i<$menu_count;$i++)
{
unset($obj);
$obj->name = $menus[$i]->attrs->name;
if($menus[$i]->attrs->default == "true") $obj->default = true;
$obj->title = $menus[$i]->title->body;
$obj->maxdepth = $menus[$i]->maxdepth->body;
$skin_info->menu->{$obj->name} = $obj;
}
}
$thumbnail = sprintf("%s%s/%s/thumbnail.png", $path, $dir, $skin);
$skin_info->thumbnail = (file_exists($thumbnail))?$thumbnail:null;
$skin_info = Rhymix\Framework\Parsers\SkinInfoParser::loadXML($skin_xml_file, $skin, $skin_path);
return $skin_info;
}