tweakStores/inc/class.tweakstores.php

210 lines
6 KiB
PHP
Raw Normal View History

2021-08-26 22:38:34 +00:00
<?php
2021-09-27 22:41:10 +00:00
/**
* @brief tweakStores, a plugin for Dotclear 2
*
2021-09-27 22:41:10 +00:00
* @package Dotclear
* @subpackage Plugin
*
2021-09-27 22:41:10 +00:00
* @author Jean-Christian Denis and Contributors
*
2021-09-27 22:41:10 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-08-26 22:38:34 +00:00
class tweakStores
{
public static $notice = [];
public static $failed = [];
# taken from lib.moduleslist.php
public static function sanitizeModule($id, $module)
{
$label = empty($module['label']) ? $id : $module['label'];
$name = __(empty($module['name']) ? $label : $module['name']);
$oname = empty($module['name']) ? $label : $module['name'];
2021-08-26 22:38:34 +00:00
return array_merge(
# Default values
[
'desc' => '',
'author' => '',
'version' => 0,
'current_version' => 0,
'root' => '',
'root_writable' => false,
'permissions' => null,
'parent' => null,
'priority' => 1000,
'standalone_config' => false,
'support' => '',
'section' => '',
'tags' => '',
'details' => '',
'sshot' => '',
'score' => 0,
'type' => null,
'requires' => [],
2021-08-26 22:38:34 +00:00
'settings' => [],
'repository' => '',
'dc_min' => 0
],
# Module's values
$module,
# Clean up values
[
'id' => $id,
'sid' => self::sanitizeString($id),
'label' => $label,
'name' => $name,
'oname' => $oname,
2021-08-26 22:38:34 +00:00
'sname' => self::sanitizeString($name)
]
);
}
# taken from lib.moduleslist.php
public static function sanitizeString($str)
{
return preg_replace('/[^A-Za-z0-9\@\#+_-]/', '', strtolower($str));
}
public static function parseFilePattern($id, $module, $file_pattern)
{
$module = self::sanitizeModule($id, $module);
2021-08-26 22:38:34 +00:00
return text::tidyURL(str_replace(
[
'%type%',
'%id%',
'%version%',
'%author%'
],
[
$module['type'],
$module['id'],
$module['version'],
$module['author']
],
$file_pattern
));
}
public static function generateXML($id, $module, $file_pattern)
{
if (!is_array($module) || empty($module)) {
return false;
}
$module = self::sanitizeModule($id, $module);
$rsp = new xmlTag('module');
2021-09-27 22:46:41 +00:00
2021-08-26 22:38:34 +00:00
self::$notice = [];
self::$failed = [];
# id
if (empty($module['id'])) {
self::$failed[] = 'unknow module';
}
2021-09-27 22:41:10 +00:00
$rsp->id = $module['id'];
2021-08-26 22:38:34 +00:00
# name
if (empty($module['name'])) {
2021-08-27 09:47:27 +00:00
self::$failed[] = 'no module name set in _define.php';
2021-08-26 22:38:34 +00:00
}
2021-09-27 22:41:10 +00:00
$rsp->name($module['oname']);
2021-08-26 22:38:34 +00:00
# version
if (empty($module['version'])) {
2021-08-27 09:47:27 +00:00
self::$failed[] = 'no module version set in _define.php';
2021-08-26 22:38:34 +00:00
}
2021-09-27 22:41:10 +00:00
$rsp->version($module['version']);
2021-08-26 22:38:34 +00:00
# author
if (empty($module['author'])) {
2021-08-27 09:47:27 +00:00
self::$failed[] = 'no module author set in _define.php';
2021-08-26 22:38:34 +00:00
}
2021-09-27 22:41:10 +00:00
$rsp->author($module['author']);
2021-08-26 22:38:34 +00:00
# desc
if (empty($module['desc'])) {
2021-08-27 09:47:27 +00:00
self::$failed[] = 'no module description set in _define.php';
2021-08-26 22:38:34 +00:00
}
2021-09-27 22:41:10 +00:00
$rsp->desc($module['desc']);
2021-08-26 22:38:34 +00:00
# repository
if (empty($module['repository'])) {
2021-08-27 09:47:27 +00:00
self::$failed[] = 'no repository set in _define.php';
2021-08-26 22:38:34 +00:00
}
# file
$file_pattern = self::parseFilePattern($id, $module, $file_pattern);
if (empty($file_pattern)) {
2021-08-27 09:47:27 +00:00
self::$failed[] = 'no zip file pattern set in Tweak Store configuration';
2021-08-26 22:38:34 +00:00
}
2021-09-27 22:41:10 +00:00
$rsp->file($file_pattern);
2021-08-26 22:38:34 +00:00
# da dc_min or requires core
2021-09-27 22:41:10 +00:00
if (!empty($module['requires']) && is_array($module['requires'])) {
foreach ($module['requires'] as $req) {
if (!is_array($req)) {
$req = [$req];
}
if ($req[0] == 'core') {
$module['dc_min'] = $req[1];
break;
}
}
}
2021-08-26 22:38:34 +00:00
if (empty($module['dc_min'])) {
self::$notice[] = 'no minimum dotclear version';
} else {
2021-09-27 22:41:10 +00:00
$rsp->insertNode(new xmlTag('da:dcmin', $module['dc_min']));
2021-08-26 22:38:34 +00:00
}
# details
if (empty($module['details'])) {
self::$notice[] = 'no details URL';
2021-09-27 22:41:10 +00:00
} else {
$rsp->insertNode(new xmlTag('da:details', $module['details']));
2021-08-26 22:38:34 +00:00
}
# section
2021-09-27 22:41:10 +00:00
if (!empty($module['section'])) {
$rsp->insertNode(new xmlTag('da:section', $module['section']));
}
2021-08-26 22:38:34 +00:00
# support
if (empty($module['support'])) {
self::$notice[] = 'no support URL';
2021-09-27 22:41:10 +00:00
} else {
$rsp->insertNode(new xmlTag('da:support', $module['support']));
2021-08-26 22:38:34 +00:00
}
2021-09-27 22:41:10 +00:00
$res = new xmlTag('modules', $rsp);
$res->insertAttr('xmlns:da', 'http://dotaddict.org/da/');
2021-08-26 22:38:34 +00:00
2021-09-27 22:41:10 +00:00
return $res->toXML();
2021-08-26 22:38:34 +00:00
}
public static function writeXML($id, $module, $file_pattern)
{
2021-08-27 09:47:27 +00:00
self::$failed = [];
2021-08-26 22:38:34 +00:00
if (!$module['root_writable']) {
return false;
}
$content = self::generateXML($id, $module, $file_pattern);
if (!empty(self::$failed)) {
return false;
}
2021-08-26 22:38:34 +00:00
try {
2021-09-27 22:44:16 +00:00
files::putContent($module['root'] . '/dcstore.xml', str_replace('><', ">\n<", $content));
} catch (Exception $e) {
2021-08-26 22:38:34 +00:00
self::$failed[] = $e->getMessage();
2021-08-26 22:38:34 +00:00
return false;
}
2021-08-26 22:38:34 +00:00
return true;
}
}