forked from christian-krieg/yourls-plugin-base-url-rewrite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
129 lines (115 loc) · 4.5 KB
/
Copy pathplugin.php
File metadata and controls
129 lines (115 loc) · 4.5 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
<?php
/*
Plugin Name: BaseURL Rewrite
Plugin URI: https://github.com/christian-krieg/yourls-plugin-rewrite-yourls-site
Description: Rewrite short URLs such that they point to alternative (base) URLs
(e.g., https://acme.com/r), different from the base domain of the
Yourls installation. This can be helpful if you want to serve
Yourls from a sub-domain (e.g., https://link.acme.com),
Version: 1.1.0
Author: Christian Krieg <christian@drkrieg.at>
*/
// No direct call
if( !defined( 'YOURLS_ABSPATH' ) ) die();
//
// This filter function actually rewrites short URLs by replacing the original
// base URL (as specified by ``YOURLS_SITE`` in `config.php`) with the base URL
// given in the configuration option. If the base URL configuration option is
// empty, the original short URL is returned.
//
// EXAMPLE:
//
// Lets assume the following:
//
// YOURLS_SITE: "https://link.acme.com" (a constant defined in `config.php`)
// ck_base_url: "https://acme.com/r" (a configuration option)
// ck_base_url_query_parameter: "base_url" (a configuration option)
//
// Then, YOURLS will create the (example) short link (when accessed from the
// YOURLS admin interface, or from the REST API without specifying the
// `base_url` query parameter)
//
// https://acme.com/r/Ac3fG
//
// instead of
//
// https://link.acme.com/Ac3fG
//
// If invoking YOURLS via its REST API while setting the ``base_url`` query
// parameter to ``https://foo.bar``, YOURLS returns the following shortlink
// (let's say the full API call is specified like this:
// `https://lnk.acme.com/yourls-api.php?signature=XXXXXXXXXX&action=shorturl&format=txt&base_url=https://foo.bar&url=https://short.this/url/for/me`):
//
// https://foo.bar/Ac3fG
//
// It is assumed that the system behind ``https://acme.com/r`` performs (e.g.,
// wildcard) redirects from ``https://acme.com/r/(.*)`` to
// ``https://link.acme.com/$1`` which are then further redirected to the target
// URL.
//
yourls_add_filter( 'yourls_link', 'ck_base_url_rewrite' );
function ck_base_url_rewrite ($link) {
$base_url_default = yourls_get_option( 'ck_base_url_default', '');
$base_url_query_parameter = yourls_get_option( 'ck_base_url_query_parameter', '');
$base_url = (
isset($_REQUEST[$base_url_query_parameter])
? $_REQUEST[$base_url_query_parameter]
: $base_url_default
);
if ( empty($base_url) )
return $link;
$rewritten = str_replace(
yourls_get_yourls_site(),
trim($base_url, '/'),
$link,
);
return $rewritten;
}
// Add a configuration page to the admin panel
yourls_add_action( 'plugins_loaded', 'ck_base_url_rewrite_init' );
function ck_base_url_rewrite_init() {
yourls_register_plugin_page(
'ck_base_url_rewrite',
'BaseURL Rewrite',
'ck_base_url_rewrite_display_page',
);
}
// The function that will draw the admin page
function ck_base_url_rewrite_display_page () {
// Check if form was submitted
if( isset( $_POST['base_url_default'] ) or
isset( $_POST['base_url_query_parameter'] )
) {
// If so, verify nonce
yourls_verify_nonce( 'base_url_settings' );
// and process submission if nonce is valid
ck_base_url_rewrite_settings_update();
}
$base_url_default = yourls_get_option('ck_base_url_default', '');
$base_url_query_parameter = yourls_get_option('ck_base_url_query_parameter', '');
$nonce = yourls_create_nonce('base_url_settings');
echo <<<HTML
<main>
<h2>BaseURL Rewrite Settings</h2>
<form method="post">
<input type="hidden" name="nonce" value="$nonce" />
<p>
<label>Shortlink Default Base URL:</label>
<input type="text" name="base_url_default" value="$base_url_default" />
</p>
<p>
<label>Query parameter (Leave blank to disable):</label>
<input type="text" name="base_url_query_parameter" value="$base_url_query_parameter" />
</p>
<p><input type="submit" value="Save" class="button" /></p>
</form>
</main>
HTML;
}
// The function that updates the configuration option
function ck_base_url_rewrite_settings_update() {
$base_url_default = $_POST['base_url_default'];
$base_url_query_parameter = $_POST['base_url_query_parameter'];
yourls_update_option( 'ck_base_url_default', $base_url_default );
yourls_update_option( 'ck_base_url_query_parameter', $base_url_query_parameter);
}