-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
442 lines (393 loc) · 21.3 KB
/
Copy pathplugin.php
File metadata and controls
442 lines (393 loc) · 21.3 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
declare(strict_types=1);
/*
| Redirects Redirect your URLs to another ones
| @file ./plugin.php
| @author SamBrishes <sam@pytes.net>
| @version 0.1.0 [0.1.0] - Alpha
|
| @website https://github.com/pytesNET/media
| @license X11 / MIT License
| @copyright Copyright © 2019 - 2020 pytesNET <info@pytes.net>
*/
if(!defined("BLUDIT")) { die("Go directly to Jail. Do not pass Go. Do not collect 200 Cookies!"); }
// Load Helper Functions
require_once "functions.php";
// Redirect Class
class RedirectPlugin extends Plugin {
const VERSION = "0.1.0";
const STATUS = "Alpha";
/*
| PLUGIN :: INIT
| @since 0.1.0
*/
public function init(): bool {
$this->dbFields = [
"force-https" => false, // Force HTTPs
"https-status" => 301, // Force HTTPs permanently or temporary
"allow-external" => false, // Allow external redirects
"redirects" => [ ] // All Redirects
];
return true;
}
/*
| HOOK :: BEFORE ALL
| @since 0.1.0
*/
public function beforeAll(): void {
global $url;
// Force HTTPs
if($this->getValue("force-https") && ($_SERVER["HTTPS"] ?? "off") !== "on") {
$redirect = str_replace("http:", "https:", DOMAIN_BASE) . ltrim($url->uri(), "/");
if(!empty($_GET)) {
$redirect .= "?" . http_build_query($_GET);
}
if($this->getValue("https-status") === 301) {
header("HTTP/1.1 301 Moved Permanently");
} else if($this->getValue("https-status") === 302) {
header("HTTP/1.1 302 Found (Moved Temporarily) ");
}
header("Location: {$redirect}");
die();
}
// Check Redirects
$redirects = $this->getValue("redirects");
if(!array_key_exists($url->uri(), $redirects)) {
return;
}
// Check Redirect
[$to, $status] = array_values($redirects[$url->uri()]);
if(strpos($to, "http") === 0) {
if(!$this->getValue("allow-external")) {
return;
}
} else {
$to = DOMAIN_BASE . ltrim($to, "/");
}
// Add Parameters
if(!empty($_GET)) {
$to .= "?" . http_build_query($_GET);
}
// Redirect
if($status === 301) {
header("HTTP/1.1 301 Moved Permanently");
} else if($status === 302) {
header("HTTP/1.1 302 Found (Moved Temporarily) ");
}
header("Location: {$to}");
die();
}
/*
| PLUGIN :: FORM
| @since 0.1.0
*/
public function form(): void {
global $site;
?>
<div class="form-group row">
<div class="col-3 pt-3">
<label for="force-https" class="text-right pt-2 mr-3" style="margin-top:0!important;"><?php paw_e("Force HTTPs"); ?></label>
</div>
<div class="col-9 pt-2 pl-4">
<div class="custom-control custom-checkbox">
<input type="hidden" name="force-https" value="false" />
<input id="force-https" type="checkbox" name="force-https" value="true" class="custom-control-input" <?php paw_checked($this->getValue("force-https")); ?> />
<label class="custom-control-label" for="force-https"><?php paw_e("Force HTTPs everywhere"); ?></label>
</div>
<span class="tip"><?php paw_e("Please check if SSL is activated on your domain BEFORE you enable this option."); ?></span>
<?php if(strpos($site->url(), "https") === false) { ?>
<span class="tip">
<?php paw_e("Your site URL does currently not start with"); ?> https://.
(<a href="<?php echo HTML_PATH_ADMIN_ROOT; ?>settings#advanced-tab"><?php echo paw_e("Change it here"); ?></a>)
</span>
<?php } ?>
</div>
</div>
<div class="form-group row">
<div class="col-3 pt-3">
<label for="https-status" class="text-right pt-2 mr-3" style="margin-top:0!important;"><?php paw_e("HTTPs Status") ?></label>
</div>
<div class="col-9 pt-3">
<?php $val = $this->getValue("https-status"); ?>
<select id="https-status" name="https-status" class="custom-select">
<option value="301" <?php paw_selected($val, 301); ?>><?php echo "301 - " . paw__("Moved Permanently"); ?></option>
<option value="302" <?php paw_selected($val, 302); ?>><?php echo "302 - " . paw__("Moved Temporary"); ?></option>
</select>
<span class="tip"><?php paw_e("A permanently HTTP Status should be used for the most cases."); ?></span>
</div>
</div>
<div class="col-12 mt-4 mb-2"><hr /></div>
<div class="form-group row">
<div class="col-3 pt-3">
<label for="allow-external" class="text-right pt-2 mr-3" style="margin-top:0!important;"><?php paw_e("External Redirects"); ?></label>
</div>
<div class="col-9 pt-2 pl-4">
<div class="custom-control custom-checkbox">
<input type="hidden" name="allow-external" value="false" />
<input id="allow-external" type="checkbox" name="allow-external" value="true" class="custom-control-input" <?php paw_checked($this->getValue("allow-external")); ?> />
<label class="custom-control-label" for="allow-external"><?php paw_e("Allow external Redirects"); ?></label>
</div>
<span class="tip"><?php paw_e("Pay attention to where you are forwarding."); ?></span>
</div>
</div>
<div class="col-12 mt-4 mb-3"><hr /></div>
<div class="form-group row">
<div class="col-3 pt-3">
<label for="redirects" class="text-right pt-2 mr-3" style="margin-top:0!important;"><?php paw_e("Redirects"); ?></label>
</div>
<div class="col-9 pt-3">
<div class="input-group">
<input type="text" class="form-control" name="redirects[old]" value="" placeholder="/current/slug" />
<input type="text" class="form-control" name="redirects[new]" value="" placeholder="/forward/slug" />
<div class="input-group-append">
<span class="input-group-text border-0 p-0">
<select name="redirects[status]" class="custom-select pr-4 rounded-0">
<option value="301"><?php paw_e("Permanently"); ?></option>
<option value="302"><?php paw_e("Temporary"); ?></option>
</select>
</span>
<button id="addRedirect" class="btn btn-outline-secondary" type="button"><?php paw_e("Add Redirect"); ?></button>
</div>
</div>
<span class="tip"><?php paw_e("You just need to write the slug, the part after your site URL."); ?></span>
<div id="redirect-error" class="tip text-danger d-none"></div>
<div id="redirect-success" class="tip text-success d-none"><?php paw_e("The redirect rule has been added, but don't forget to save the page!"); ?></div>
</div>
</div>
<div class="form-group row">
<div class="col-3 pt-3">
<label class="text-right pt-2 mr-3" style="margin-top:0!important;"><?php paw_e("Active Redirects"); ?></label>
</div>
<div class="col-9 pt-3">
<div id="redirects" class="redirects-list">
<?php
$count = 0;
$redirects = $this->getValue("redirects");
foreach($redirects AS $from => $data) {
[$to, $status] = array_values($data);
?>
<div class="redirect mb-2" data-redirect-number="<?php echo ++$count; ?>">
<div class="input-group">
<input type="text" class="form-control" name="redirect[<?php echo $count; ?>][old]" value="<?php echo $from; ?>" placeholder="/current/slug" data-redirect="old" />
<input type="text" class="form-control" name="redirect[<?php echo $count; ?>][new]" value="<?php echo $to; ?>" placeholder="/forward/slug" data-redirect="new" />
<div class="input-group-append">
<span class="input-group-text border-0 p-0">
<select name="redirect[<?php echo $count; ?>][status]" class="custom-select pr-4 rounded-0" data-redirect="status">
<option value="301" <?php echo $status === 301? 'selected="selected"': ''; ?>><?php paw_e("Permanently"); ?></option>
<option value="302" <?php echo $status === 302? 'selected="selected"': ''; ?>><?php paw_e("Temporary"); ?></option>
</select>
</span>
<button class="btn btn-outline-danger" type="button" data-redirect="delete" style="height:34.4px;">
<span class="fa fa-trash pr-0"></span>
</button>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</div>
<?php
}
/*
| PLUGIN :: POST
| @since 0.1.0
*/
public function post(): bool {
$data = $_POST;
// Check HTTPs
if(isset($data["force-https"])) {
$this->db["force-https"] = $data["force-https"] === "true";
}
if(isset($data["https-status"])) {
$this->db["https-status"] = $data["https-status"] === "302"? 302: 301;
}
// External Redirects
if(isset($data["allow-external"])) {
$this->db["allow-external"] = $data["allow-external"] === "true";
}
// Redirects
$redirects = [];
if(isset($data["redirects"])) {
foreach($data["redirect"] AS $redirect) {
[$from, $to, $status] = array_values($redirect);
// Sanitize FROM
$from = rtrim(trim(strtolower($from)), "/");
if(stripos($from, DOMAIN_BASE)) {
$from = str_replace(DOMAIN_BASE, "", $from);
} else if(stripos($from, DOMAIN)) {
$from = str_replace(DOMAIN, "", $from);
}
// Validate
if(strlen($from) === 0) {
continue;
}
if(strpos($from, "http") === 0) {
continue;
}
$from = strpos($from, "/") !== 0? "/" . $from: $from;
if(array_key_exists($from, $redirects)) {
continue;
}
// Sanitize TO
$to = rtrim(trim(strtolower($to)), "/");
if(stripos($to, DOMAIN_BASE)) {
$to = str_replace(DOMAIN_BASE, "", $to);
} else if(stripos($to, DOMAIN)) {
$to = str_replace(DOMAIN, "", $to);
}
// Validate
if(strlen($to) === 0) {
continue;
}
if(strpos($to, "http") === 0 && !$this->db["allow-external"]) {
continue;
} else if(strpos($to, "http") === false) {
$to = strpos($to, "/") !== 0? "/" . $to: $to;
}
if($from === $to) {
continue;
}
// Sanitize STATUS
$status = ($status === "301")? 301: 302;
// Add
$redirects[$from] = [$to, $status];
}
}
$this->db["redirects"] = $redirects;
return $this->save();
}
/*
| HOOK :: ADMIN SIDEBAR
| @since 0.1.0
*/
public function adminSidebar(): string {
return '<a href="' . HTML_PATH_ADMIN_ROOT . 'configure-plugin/RedirectPlugin" class="nav-link">'.paw__("Redirects").'</a>';
}
/*
| HOOK :: ADMIN BODY END
| @since 0.1.0
*/
public function adminBodyEnd(): void {
global $url;
if($url->slug() !== "configure-plugin/RedirectPlugin") {
return;
}
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
function showError(text) {
$("#redirect-error").text(text).removeClass("d-none");
$("#redirect-success").addClass("d-none");
return showAlert(text);
};
$("#addRedirect").click(function(event) {
event.preventDefault();
// Get List
var list = $("#redirects");
// Get Fields
var from = $("input[name='redirects[old]']");
var to = $("input[name='redirects[new]']");
var status = $("select[name='redirects[status]']");
var ext = $("input#allow-external");
// Sanitize FROM
var from_value = from.val().replace(/\/$/, "").trim().toLowerCase();
if(from_value.startsWith(DOMAIN_BASE)) {
from_value = from_value.replace(DOMAIN_BASE, "");
} else if(from_value.startsWith(DOMAIN)) {
from_value = from_value.replace(DOMAIN, "");
}
// ... starts with HTTP
if(from_value.startsWith("http")) {
return showError("<?php paw_e("The current slug must be just the page slug of your own website."); ?>");
}
// ... is empty
if(from_value.length === 0) {
return showError("<?php paw_e("The current slug field cannot be empty."); ?>");
}
if(!from_value.startsWith("/")) {
from_value = "/" + from_value;
}
// ... does already exist
list.find("input[data-redirect='old']").each(function() {
if(this.value === from_value) {
list = false;
return showError("<?php paw_e("A forward rule for the current slug does already exist."); ?>");
}
});
if(list === false) {
return;
}
// Sanitize TO
var to_value = to.val().replace(/\/$/, "").trim().toLowerCase();
if(to_value.startsWith(DOMAIN_BASE)) {
to_value = to_value.replace(DOMAIN_BASE, "");
} else if(to_value.startsWith(DOMAIN)) {
to_value = to_value.replace(DOMAIN, "");
}
// ... is empty
if(to_value.length === 0) {
return showError("<?php paw_e("The forward slug field cannot be empty."); ?>");
}
// ... starts with HTTP
if(to_value.startsWith("http") && !ext.prop("checked")) {
return showError("<?php paw_e("The forward slug must be just the page slug of your own website."); ?>");
} else if(!to_value.startsWith("http")) {
if(!to_value.startsWith("/")) {
to_value = "/" + to_value;
}
}
// ... is the same as the current url
if(to_value === from_value) {
return showError("<?php paw_e("The current and forward slug cannot be the same."); ?>");
}
// Sanitize STATUS
var status_value = status.val();
if(status_value !== "301" && status_value !== "302") {
return showError("<?php paw_e("The passed HTTP status is invalid."); ?>");
}
// Create Unique Number
var num = list.children().length;
while($(".redirect[data-redirect-number='" + num + "']").length > 0) {
num++;
}
// Create Form
var redirect = $("<div></div>", {
"class": "redirect mb-2",
"data-redirect-number": num,
html: '<div class="input-group">'
+ ' <input type="text" class="form-control" name="redirect[' + num + '][old]" value="' + from_value + '" placeholder="' + from.attr("placeholder") + '" data-redirect="old" />'
+ ' <input type="text" class="form-control" name="redirect[' + num + '][new]" value="' + to_value + '" placeholder="' + to.attr("placeholder") + '" data-redirect="new" />'
+ ' <div class="input-group-append">'
+ ' <span class="input-group-text border-0 p-0">'
+ ' <select name="redirect[' + num + '][status]" class="custom-select pr-4 rounded-0" data-redirect="status">'
+ ' <option value="301" ' + (status_value === "301"? "selected='selected'": "") + '>' + status.children().get(0).innerText + '</option>'
+ ' <option value="302" ' + (status_value === "302"? "selected='selected'": "") + '>' + status.children().get(1).innerText + '</option>'
+ ' </select>'
+ ' </span>'
+ ' <button class="btn btn-outline-danger" type="button" data-redirect="delete" style="height:34.4px;">'
+ ' <span class="fa fa-trash pr-0"></span>'
+ ' </button>'
+ ' </div>'
+ '</div>'
});
redirect.prependTo(list);
$("#redirect-error").addClass("d-none");
$("#redirect-success").removeClass("d-none");
});
// Delete Button
$(document).on("click", "button[data-redirect='delete']", function(event) {
event.preventDefault();
var redirect = this;
do {
redirect = redirect.parentElement;
} while(redirect.classList.contains("redirect") === false);
redirect.parentElement.removeChild(redirect);
})
});
</script>
<?php
}
}