-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssets.php
More file actions
117 lines (100 loc) · 3.59 KB
/
Assets.php
File metadata and controls
117 lines (100 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace MerapiPanel\Module\Website {
use Exception;
use MerapiPanel\Box\Module\__Fragment;
use MerapiPanel\Box\Module\Entity\Module;
use MerapiPanel\Utility\Util;
class Assets extends __Fragment
{
protected $module;
function onCreate(Module $module)
{
$this->module = $module;
}
function listpops()
{
$file = __DIR__ . "/data/assets.json";
if (file_exists($file))
return json_decode(file_get_contents($file) ?? "{}", 1) ?? [];
else return [];
}
function addpop($name, $type)
{
if (empty($name)) throw new Exception("Please enter asset name", 401);
if (!in_array($type, ["style", "link", "link:style", "script"])) throw new Exception("Please enter asset name", 401);
$realType = preg_replace("/\:.*$/", "", $type);
$minimal = [
"link" => [
"name" => "",
"id" => "",
"attributes" => []
],
"link:style" => [
"name" => "",
"id" => "",
"attributes" => [
[
"key" => "rel",
"value" => "stylesheet",
],
[
"key" => "href",
"value" => "",
]
]
],
"style" => [
"name" => "",
"id" => "",
"content" => ""
],
"script" => [
"name" => "",
"id" => "",
"attributes" => [],
"content" => ""
]
];
$file = __DIR__ . "/data/assets.json";
$data = $this->listpops();
$data[] = [
...$minimal[$type],
...[
"id" => Util::uniq(12),
"name" => $name,
"type" => $realType
]
];
file_put_contents($file, json_encode(array_values($data)));
}
function savepop($id, $name, $attributes, $content, $type)
{
$file = __DIR__ . "/data/assets.json";
$data = $this->listpops();
$index = array_search($id, array_column($data, "id"));
if ($index < 0) throw new Exception("Asset not found!", 404);
$asset = $data[$index];
$keys = array_keys($asset);
if (in_array("attributes", $keys)) {
$asset['attributes'] = !is_array($attributes) ? json_decode($attributes, 1) : $attributes;
}
if (in_array("content", $keys)) {
$asset['content'] = trim(preg_replace("/\s+|\r\n/", " ", $content));
}
if ($name != $asset['name']) {
$asset['name'] = $name;
}
$data[$index] = $asset;
file_put_contents($file, json_encode(array_values($data)));
}
function rmpop($id)
{
$file = __DIR__ . "/data/assets.json";
$data = $this->listpops();
$index = array_search($id, array_column($data, "id"));
if ($index < 0) throw new Exception("Asset not found!", 404);
unset($data[$index]);
file_put_contents($file, json_encode(array_values($data)));
}
}
}