-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.php
187 lines (160 loc) · 5.21 KB
/
boot.php
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
$authKey = rex_config::get('rex-sync', 'auth');
$isRexSyncRequest = isset($_POST['rex-sync-request']) ? $_POST['rex-sync-request'] : null;
if ($isRexSyncRequest) {
function createDirIfNotExisting($path) {
$info = pathinfo($path);
if (is_dir($info['dirname']) === false) {
return mkdir($info['dirname']);
}
return true;
}
header('Content-Type: application/json');
// authorization
if ($_SERVER['HTTP_AUTH'] !== $authKey) {
http_response_code(401);
echo '{ "error": "unauthorized" }';
die();
}
// data
$type = $_POST['type'];
$path = $_POST['path'];
$event = $_POST['event'];
$file = $_FILES['file'];
// handle assets
if ($type === 'assets') {
$path = rex_path::base('assets'.$path);
if ($event === 'unlink') {
if (unlink($path) === false) {
echo '{ "error": "failed to delete file" }';
die();
}
} else {
if (createDirIfNotExisting($path) === false) {
echo '{ "error": "failed to create directory" }';
die();
}
if (move_uploaded_file($file['tmp_name'], $path) === false) {
echo '{ "error": "failed to move uploaded file" }';
die();
}
}
}
// handle addons
if ($type === 'addons') {
$path = rex_path::src('addons'.$path);
if ($event === 'unlink') {
if (unlink($path) === false) {
echo '{ "error": "failed to delete file" }';
die();
}
} else {
if (createDirIfNotExisting($path) === false) {
echo '{ "error": "failed to create directory" }';
die();
}
if (move_uploaded_file($file['tmp_name'], $path) === false) {
echo '{ "error": "failed to move uploaded file" }';
die();
}
}
}
// handle templates
if ($type === 'templates') {
$name = pathinfo($path)['filename'];
if ($event === 'unlink') {
// delete template
// don't delete for now. too many things can break
// $sql = rex_sql::factory();
// $sql->setTable('rex_template');
// $sql->setWhere('name="'.$name.'"');
// $sql->select();
// $id = $sql->getValue('id');
// $id = $sql->getValue('id');
// $template = new rex_template($id);
// $template->deleteCache();
// $sql = rex_sql::factory();
// $sql->setTable('rex_template');
// $sql->setWhere('name="'.$name.'"');
// $sql->delete();
} else {
$contents = file_get_contents($file['tmp_name']);
if ($contents === false) {
echo '{ "error": "failed to read uploaded file" }';
die();
}
// check if template already exists
$sql = rex_sql::factory();
$sql->setTable('rex_template');
$sql->setWhere('name="'.$name.'"');
$sql->select();
$rows = $sql->getRows();
// name, content
if ($rows <= 0) {
// create template
$sql = rex_sql::factory();
$sql->setTable('rex_template');
$sql->setValue('name', $name);
$sql->setValue('content', $contents);
$sql->insert();
} else {
// update template
$sql = rex_sql::factory();
$sql->setTable('rex_template');
$sql->setWhere('name="'.$name.'"');
$sql->setValue('content', $contents);
$sql->update();
}
$sql = rex_sql::factory();
$sql->setTable('rex_template');
$sql->setWhere('name="'.$name.'"');
$sql->select('id');
$id = $sql->getValue('id');
$template = new rex_template($id);
$template->deleteCache();
}
}
// handle modules
if ($type === 'modules') {
if ($event === 'unlink') {
// ignore for yet
} else {
$contents = file_get_contents($file['tmp_name']);
if ($contents === false) {
echo '{ "error": "failed to read uploaded file" }';
die();
}
$area = pathinfo($path)['filename']; // input or output
$name = explode('/', $path)[1];
if ($area !== 'output' && $area !== 'input') {
echo '{ "error": "module must either be input.php or output.php" }';
die();
}
// exists
$sql = rex_sql::factory();
$sql->setTable('rex_module');
$sql->setWhere('name="'.$name.'"');
$sql->select('id');
$rows = $sql->getRows();
if ($rows <= 0) {
// create module
$sql = rex_sql::factory();
$sql->setTable('rex_module');
$sql->setValue('name', $name);
$sql->setValue($area, $contents);
$sql->insert();
} else {
// update module
$sql = rex_sql::factory();
$sql->setTable('rex_module');
$sql->setWhere('name="'.$name.'"');
$sql->setValue($area, $contents);
$sql->update();
}
rex_delete_cache();
}
}
echo '{ "success": true }';
die();
}
?>