pacKman/inc/Utils.php

255 lines
8.2 KiB
PHP
Raw Normal View History

2021-08-16 20:48:26 +00:00
<?php
2021-09-02 20:14:32 +00:00
/**
* @brief pacKman, a plugin for Dotclear 2
2021-11-01 10:39:02 +00:00
*
2021-09-02 20:14:32 +00:00
* @package Dotclear
* @subpackage Plugin
2021-11-01 10:39:02 +00:00
*
2021-09-02 20:14:32 +00:00
* @author Jean-Christian Denis
2021-11-01 10:39:02 +00:00
*
2021-09-02 20:14:32 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2022-12-17 21:12:10 +00:00
declare(strict_types=1);
2022-12-18 23:21:11 +00:00
namespace Dotclear\Plugin\pacKman;
2021-08-16 20:48:26 +00:00
2022-12-17 21:12:10 +00:00
/* dotclear ns */
use dcCore;
/* clearbricks ns */
use form;
use html;
use path;
class Utils
2021-08-16 20:48:26 +00:00
{
2022-11-12 20:42:05 +00:00
public static function is_configured(string $repo, string $file_a, string $file_b): bool
2021-08-23 13:07:29 +00:00
{
if (!is_dir(DC_TPL_CACHE) || !is_writable(DC_TPL_CACHE)) {
2022-11-12 20:42:05 +00:00
dcCore::app()->error->add(
2021-08-23 13:07:29 +00:00
__('Cache directory is not writable.')
);
}
if (!is_writable($repo)) {
2022-11-12 20:42:05 +00:00
dcCore::app()->error->add(
2021-08-23 13:07:29 +00:00
__('Path to repository is not writable.')
);
}
if (empty($file_a)) {
2022-11-12 20:42:05 +00:00
dcCore::app()->error->add(
2021-08-23 13:07:29 +00:00
__('You must specify the name of package to export.')
);
}
if (!is_writable(dirname($repo . '/' . $file_a))) {
2022-11-12 20:42:05 +00:00
dcCore::app()->error->add(
2021-08-23 13:07:29 +00:00
__('Path to first export package is not writable.')
);
}
2021-10-23 14:46:03 +00:00
if (!empty($file_b) && !is_writable(dirname($repo . '/' . $file_b))) {
2022-11-12 20:42:05 +00:00
dcCore::app()->error->add(
2021-08-23 13:07:29 +00:00
__('Path to second export package is not writable.')
);
}
2022-11-12 20:42:05 +00:00
return !dcCore::app()->error->flag();
2021-08-23 13:07:29 +00:00
}
2021-11-08 21:19:07 +00:00
public static function is_writable(string $path, string $file): bool
2021-08-23 13:07:29 +00:00
{
2021-10-23 14:46:03 +00:00
return !(empty($path) || empty($file) || !is_writable(dirname($path . '/' . $file)));
2021-08-23 13:07:29 +00:00
}
2022-12-03 13:40:00 +00:00
public static function getModules(string $type, ?string $id = null): ?array
{
$type = $type == 'themes' ? 'themes' : 'plugins';
$modules = array_merge(dcCore::app()->{$type}->getDisabledModules(), dcCore::app()->{$type}->getModules());
if (empty($id)) {
return $modules;
} elseif (array_key_exists($id, $modules)) {
return $modules[$id];
}
return null;
}
public static function moduleExists(string $type, ?string $id): bool
{
$type = $type == 'themes' ? 'themes' : 'plugins';
return array_key_exists($id, array_merge(dcCore::app()->{$type}->getDisabledModules(), dcCore::app()->{$type}->getModules()));
}
2022-11-12 20:42:05 +00:00
public static function modules(array $modules, string $type, string $title): ?bool
2021-08-23 13:07:29 +00:00
{
2021-11-08 21:19:07 +00:00
if (empty($modules) || !is_array($modules)) {
2021-10-23 14:51:33 +00:00
return null;
}
2021-08-23 13:07:29 +00:00
$type = $type == 'themes' ? 'themes' : 'plugins';
2021-11-01 10:39:02 +00:00
echo
2021-08-23 13:07:29 +00:00
'<div class="multi-part" ' .
'id="packman-' . $type . '" title="' . $title . '">' .
2021-10-23 14:51:33 +00:00
'<h3>' . $title . '</h3>' .
2021-08-23 13:07:29 +00:00
'<form action="plugin.php" method="post">' .
'<table class="clear"><tr>' .
'<th class="nowrap">' . __('Id') . '</th>' .
'<th class="nowrap">' . __('Version') . '</th>' .
'<th class="nowrap maximal">' . __('Name') . '</th>' .
'<th class="nowrap">' . __('Root') . '</th>' .
'</tr>';
2021-11-01 10:39:02 +00:00
foreach (self::sort($modules) as $id => $module) {
2021-08-23 13:07:29 +00:00
echo
'<tr class="line">' .
'<td class="nowrap"><label class="classic">' .
form::checkbox(['modules[' . html::escapeHTML($module['root']) . ']'], html::escapeHTML($id)) .
html::escapeHTML($id) .
'</label></td>' .
'<td class="nowrap count">' .
html::escapeHTML($module['version']) .
'</td>' .
'<td class="nowrap maximal">' .
__(html::escapeHTML($module['name'])) .
'</td>' .
'<td class="nowrap">' .
2021-11-08 21:19:07 +00:00
dirname((string) path::real($module['root'], false)) .
2021-08-23 13:07:29 +00:00
'</td>' .
'</tr>';
}
echo
'</table>' .
'<p class="checkboxes-helpers"></p>' .
'<p>' .
2021-11-01 10:39:02 +00:00
(
!empty($_REQUEST['redir']) ?
2021-08-23 13:07:29 +00:00
form::hidden(
['redir'],
html::escapeHTML($_REQUEST['redir'])
) : ''
) .
2022-12-10 17:49:10 +00:00
form::hidden(['p'], basename(dirname('../' . __DIR__))) .
2021-08-23 13:07:29 +00:00
form::hidden(['type'], $type) .
form::hidden(['action'], 'packup') .
'<input type="submit" name="packup" value="' .
2021-11-01 10:39:02 +00:00
__('Pack up selected modules') . '" />' .
2022-11-12 20:42:05 +00:00
dcCore::app()->formNonce() . '</p>' .
2021-08-23 13:07:29 +00:00
'</form>' .
'</div>';
2021-11-08 21:19:07 +00:00
return true;
2021-08-23 13:07:29 +00:00
}
2021-09-02 20:14:32 +00:00
2022-11-12 20:42:05 +00:00
public static function repository(array $modules, string $type, string $title): ?bool
2021-08-23 13:07:29 +00:00
{
2021-10-23 14:51:33 +00:00
if (empty($modules) || !is_array($modules)) {
return null;
}
2021-08-23 13:07:29 +00:00
if (!in_array($type, ['plugins', 'themes', 'repository'])) {
return null;
}
2021-11-01 10:39:02 +00:00
echo
2021-08-23 13:07:29 +00:00
'<div class="multi-part" ' .
'id="packman-repository-' . $type . '" title="' . $title . '">' .
'<h3>' . $title . '</h3>';
$combo_action = [__('delete') => 'delete'];
2021-09-02 20:14:32 +00:00
2021-08-23 13:07:29 +00:00
if ($type == 'plugins' || $type == 'themes') {
$combo_action[__('install')] = 'install';
}
if ($type != 'plugins') {
$combo_action[sprintf(__('copy to %s directory'), __('plugins'))] = 'copy_to_plugins';
$combo_action[sprintf(__('move to %s directory'), __('plugins'))] = 'move_to_plugins';
}
if ($type != 'themes') {
$combo_action[sprintf(__('copy to %s directory'), __('themes'))] = 'copy_to_themes';
$combo_action[sprintf(__('move to %s directory'), __('themes'))] = 'move_to_themes';
}
if ($type != 'repository') {
$combo_action[sprintf(__('copy to %s directory'), __('repository'))] = 'copy_to_repository';
$combo_action[sprintf(__('move to %s directory'), __('repository'))] = 'move_to_repository';
}
2021-11-01 10:39:02 +00:00
echo
2021-08-23 13:07:29 +00:00
'<form action="plugin.php" method="post">' .
'<table class="clear"><tr>' .
'<th class="nowrap">' . __('Id') . '</th>' .
'<th class="nowrap">' . __('Version') . '</th>' .
'<th class="nowrap">' . __('Name') . '</th>' .
'<th class="nowrap">' . __('File') . '</th>' .
'</tr>';
$dup = [];
2021-11-01 10:39:02 +00:00
foreach (self::sort($modules) as $module) {
2021-08-23 13:07:29 +00:00
if (isset($dup[$module['root']])) {
continue;
}
$dup[$module['root']] = 1;
echo
'<tr class="line">' .
'<td class="nowrap"><label class="classic" title="' .
html::escapeHTML($module['root']) . '">' .
form::checkbox(['modules[' . html::escapeHTML($module['root']) . ']'], $module['id']) .
html::escapeHTML($module['id']) .
'</label></td>' .
'<td class="nowrap count">' .
html::escapeHTML($module['version']) .
'</td>' .
'<td class="nowrap maximal">' .
__(html::escapeHTML($module['name'])) .
'</td>' .
'<td class="nowrap">' .
2022-12-17 21:12:10 +00:00
'<a class="packman-download" href="' .
2022-12-10 17:49:10 +00:00
dcCore::app()->adminurl->get('admin.plugin.' . basename(dirname('../' . __DIR__)), [
'package' => basename($module['root']),
2022-12-17 21:12:10 +00:00
'repo' => $type,
2022-12-10 17:49:10 +00:00
]) . '" title="' . __('Download') . '">' .
2021-08-23 13:07:29 +00:00
html::escapeHTML(basename($module['root'])) . '</a>' .
'</td>' .
'</tr>';
}
echo
'</table>' .
'<div class="two-cols">' .
'<p class="col checkboxes-helpers"></p>' .
'<p class="col right">' . __('Selected modules action:') . ' ' .
form::combo(['action'], $combo_action) .
'<input type="submit" name="packup" value="' . __('ok') . '" />' .
2022-12-10 17:49:10 +00:00
form::hidden(['p'], basename(dirname('../' . __DIR__))) .
2021-08-23 13:07:29 +00:00
form::hidden(['tab'], 'repository') .
form::hidden(['type'], $type) .
2022-11-12 20:42:05 +00:00
dcCore::app()->formNonce() .
2021-08-23 13:07:29 +00:00
'</p>' .
'</div>' .
'</form>' .
'</div>';
2021-11-08 21:19:07 +00:00
return true;
2021-08-23 13:07:29 +00:00
}
2021-11-08 21:19:07 +00:00
protected static function sort(array $modules): array
2021-08-23 13:07:29 +00:00
{
2021-10-23 13:59:28 +00:00
$key = $ver = [];
2021-11-01 10:39:02 +00:00
foreach ($modules as $i => $module) {
2021-10-23 13:59:28 +00:00
$key[$i] = $module['id'] ?? $i;
$ver[$i] = $module['version'];
2021-08-23 13:07:29 +00:00
}
2021-10-23 13:59:28 +00:00
array_multisort($key, SORT_ASC, $ver, SORT_ASC, $modules);
2021-08-23 13:07:29 +00:00
return $modules;
}
2021-11-06 13:56:29 +00:00
}