2025-01-31 18:20:13 +03:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2020-06-11 23:21:49 +03:00
|
|
|
|
namespace openvk\Web\Themes;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
2020-06-11 23:21:49 +03:00
|
|
|
|
use Nette\InvalidStateException as ISE;
|
|
|
|
|
use Chandler\Session\Session;
|
|
|
|
|
use Chandler\Patterns\TSimpleSingleton;
|
|
|
|
|
|
|
|
|
|
class Themepacks implements \ArrayAccess
|
|
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
|
use TSimpleSingleton;
|
|
|
|
|
public const THEMPACK_ENGINE_VERSION = 1;
|
|
|
|
|
public const DEFAULT_THEME_ID = "ovk"; # блин было бы смешно если было бы Fore, потому что Лунка, а Luna это название дефолт темы винхп
|
|
|
|
|
|
2020-06-11 23:21:49 +03:00
|
|
|
|
private $loadedThemepacks = [];
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
|
|
public function __construct()
|
2020-06-11 23:21:49 +03:00
|
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
|
foreach (glob(OPENVK_ROOT . "/themepacks/*", GLOB_ONLYDIR) as $themeDir) {
|
2020-06-11 23:21:49 +03:00
|
|
|
|
try {
|
|
|
|
|
$theme = Themepack::themepackFromDir($themeDir);
|
|
|
|
|
$tid = $theme->getId();
|
2025-01-31 18:20:13 +03:00
|
|
|
|
if (isset($this->loadedThemepacks[$tid])) {
|
2020-06-11 23:21:49 +03:00
|
|
|
|
trigger_error("Duplicate theme $tid found at $themeDir, skipping...", E_USER_WARNING);
|
2025-01-31 18:20:13 +03:00
|
|
|
|
} else {
|
2020-06-11 23:21:49 +03:00
|
|
|
|
$this->loadedThemepacks[$tid] = $theme;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
}
|
|
|
|
|
} catch (\Exception $e) {
|
2021-12-02 15:00:15 +03:00
|
|
|
|
trigger_error("Could not load theme at $themeDir. Exception: $e", E_USER_WARNING);
|
2020-06-11 23:21:49 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
2020-06-11 23:21:49 +03:00
|
|
|
|
private function installUnpacked(string $path): bool
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$theme = Themepack::themepackFromDir($path);
|
|
|
|
|
$tid = $theme->getId();
|
2025-01-31 18:20:13 +03:00
|
|
|
|
if (isset($this->loadedThemepacks[$tid])) {
|
2020-06-11 23:21:49 +03:00
|
|
|
|
return false;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-11 23:21:49 +03:00
|
|
|
|
rename($path, OPENVK_ROOT . "/themepacks/$tid");
|
|
|
|
|
$this->loadedThemepacks[$tid] = $theme;
|
|
|
|
|
return true;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
} catch (\Exception $e) {
|
2020-06-11 23:21:49 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
|
|
public function getThemeList(): \Traversable
|
2020-06-11 23:21:49 +03:00
|
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
|
foreach ($this->loadedThemepacks as $id => $theme) {
|
|
|
|
|
if ($theme->isEnabled()) {
|
2020-06-14 14:08:50 +03:00
|
|
|
|
yield $id => ($theme->getName(Session::i()->get("lang", "ru")));
|
2025-01-31 18:20:13 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-11 23:21:49 +03:00
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
|
|
public function getAllThemes(): array
|
2020-06-16 17:15:36 +03:00
|
|
|
|
{
|
|
|
|
|
return $this->loadedThemepacks;
|
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
2020-06-11 23:21:49 +03:00
|
|
|
|
/* ArrayAccess */
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
|
|
public function offsetExists($offset): bool
|
2020-06-11 23:21:49 +03:00
|
|
|
|
{
|
|
|
|
|
return $offset === Themepacks::DEFAULT_THEME_ID ? false : isset($this->loadedThemepacks[$offset]);
|
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
|
|
public function offsetGet($offset): mixed
|
2020-06-11 23:21:49 +03:00
|
|
|
|
{
|
|
|
|
|
return $this->loadedThemepacks[$offset];
|
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
|
|
public function offsetSet($offset, $value): void
|
2020-06-11 23:21:49 +03:00
|
|
|
|
{
|
|
|
|
|
throw new ISE("Theme substitution in runtime is prohbited");
|
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
|
|
public function offsetUnset($offset): void
|
2020-06-11 23:21:49 +03:00
|
|
|
|
{
|
|
|
|
|
$this->uninstall($offset);
|
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
2020-06-11 23:21:49 +03:00
|
|
|
|
/* /ArrayAccess */
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
|
|
public function install(string $archivePath): bool
|
2020-06-11 23:21:49 +03:00
|
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
|
if (!file_exists($archivePath)) {
|
2020-06-11 23:21:49 +03:00
|
|
|
|
return false;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-11 23:21:49 +03:00
|
|
|
|
$tmpDir = mkdir(tempnam(OPENVK_ROOT . "/tmp/themepack_artifacts/", "themex_"));
|
|
|
|
|
try {
|
|
|
|
|
$archive = new \CabArchive($archivePath);
|
|
|
|
|
$archive->extract($tmpDir);
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
2020-06-11 23:21:49 +03:00
|
|
|
|
return $this->installUnpacked($tmpDir);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return false;
|
|
|
|
|
} finally {
|
|
|
|
|
rmdir($tmpDir);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
|
|
public function uninstall(string $id): bool
|
2020-06-11 23:21:49 +03:00
|
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
|
if (!isset($loadedThemepacks[$id])) {
|
2020-06-11 23:21:49 +03:00
|
|
|
|
return false;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-11 23:21:49 +03:00
|
|
|
|
rmdir(OPENVK_ROOT . "/themepacks/$id");
|
|
|
|
|
unset($loadedThemepacks[$id]);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|