-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnginx-secure.php
203 lines (179 loc) · 6.68 KB
/
nginx-secure.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/*
Plugin Name: nginx-secure
Plugin URI: ''
Description: ''
Author: Jaap Marcus
Version: 4.2.8
Author URI: https://eris.nu
Text Domain: nginx-secure
Domain Path: /languages/
License: MIT
*/
class NginxSecure {
private $options;
private $domains;
function __construct(){
add_filter('the_content', array($this,'content'),20);
$this -> options = get_option('nginx-secure');
if(empty($this -> options)){
$this -> options = array('secret' => 'secret_key', 'ttl' => 3600,
'extensions' => 'mp4,webm,ogg,ogv,mp3','domains' => '');
}
$this -> domains = explode(',',$this -> options['domains']);
$this -> domains[] = $_SERVER['HTTP_HOST'];
}
function secure_url($url, $path){
$expires = time() + $this -> options['ttl'];
$secret = $this -> options['secret'];
$md5 = md5("$expires$path $secret", true);
$md5 = base64_encode($md5);
$md5 = strtr($md5, '+/', '-_');
$md5 = str_replace('=', '', $md5);
//var_dump($url . $path . '?md5=' . $md5 . '&expires=' . $expires);
return $url . $path . '?md5=' . $md5 . '&expires=' . $expires;
}
function convert($match){
$nmatch = str_replace($this -> domains, '',$match[0]);
if($nmatch != $match[0]){
//we need to phrase it
$url = parse_url($match[1].'.'.$match[2]);
//echo $match[1].'.'.$match[2];
return 'src="'.$this -> secure_url($url['scheme'].'://'.$url['host'], $url['path']).'"';
}else{
return $match[0];
}
}
function content($content){
$new_content = preg_replace_callback('/src="?([^ ]*)?\.('.implode('|',explode(',',$this -> options['extensions'])).')([^ ]*)"/', array($this, 'convert'), $content);
return $new_content ;
}
}
$s = new NginxSecure();
Class NginxSecureSettings {
private $settings;
function __construct(){
//add a new page the settings section
add_action( 'admin_menu', array( $this, 'addPluginPage' ) );
//register posible settings
add_action( 'admin_init', array( $this, 'registerSettings' ) );
add_action( 'plugins_loaded', array( $this, 'textDomain' ));
$this -> settings = get_option( 'nginx-secure' );
}
function textDomain(){
load_plugin_textdomain( 'nginx-secure', false, dirname( plugin_basename(__FILE__) ) . '/languages/' );
}
function addPluginPage(){
add_options_page(
__('Nginx Secure Link','nginx-secure'), // page_title
__('Nginx Secure Link','nginx-secure'), // menu_title
'manage_options', // capability
'nginx-secure', // menu_slug
array( $this, 'SettingsPage' ) // function
);
}
function registerSettings(){
register_setting('nginx-secure',
'nginx-secure',
array( $this, 'santize' )
);
add_settings_section(
'nginx-secure-settings', // id
__('Settings','nginx-secure'), // title
array( $this, 'info' ), // callback
'nginx-secure-settings' // page
);
add_settings_field(
'secret', // id
__('Secret Key','nginx-secure'), // title
array( $this, 'secret' ), // callback
'nginx-secure-settings',
'nginx-secure-settings',
);
add_settings_field(
'ttl', // id
__('TTL','nginx-secure'), // title
array( $this, 'ttl' ), // callback
'nginx-secure-settings',
'nginx-secure-settings',
);
add_settings_field(
'extensions', // id
__('Extensions','nginx-secure'), // title
array( $this, 'extensions' ), // callback
'nginx-secure-settings',
'nginx-secure-settings',
);
add_settings_field(
'domains', // id
__('Domains','nginx-secure'), // title
array( $this, 'domains' ), // callback
'nginx-secure-settings',
'nginx-secure-settings',
);
}
function info(){
?>
<div>Domains without http(s):// and folder names. Also exclude the <?php echo $_SERVER['SERVER_NAME'];?></div>
<?php
}
function santize($input){
$sanitary_values = array();
if ( isset( $input['secret'] ) ) {
$sanitary_values['secret'] = sanitize_text_field( $input['secret'] );
}
if ( isset( $input['ttl'] ) ) {
$sanitary_values['ttl'] = sanitize_text_field( $input['ttl'] );
}
if ( isset( $input['extensions'] ) ) {
$sanitary_values['extensions'] = sanitize_text_field( $input['extensions'] );
}
if ( isset( $input['domains'] ) ) {
$sanitary_values['domains'] = sanitize_text_field( preg_replace('#\s+#',',',trim($input['domains'])));
}
return $sanitary_values;
}
function secret(){
printf(
'<input class="regular-text" type="text" name="nginx-secure[secret]" id="secret" value="%s">',
isset( $this->settings['secret'] ) ? esc_attr( $this->settings['secret']) : ''
);
}
function ttl(){
printf(
'<input class="regular-text" type="text" name="nginx-secure[ttl]" id="ttl" value="%s">',
isset( $this->settings['ttl'] ) ? esc_attr( $this->settings['ttl']) : ''
);
}
function extensions(){
printf(
'<input class="regular-text" type="text" name="nginx-secure[extensions]" id="extensions" value="%s">',
isset( $this->settings['extensions'] ) ? esc_attr( $this->settings['extensions']) : ''
);
}
function domains(){
printf(
'<textarea class="regular-text" type="text" name="nginx-secure[domains]" id="domains">%s</textarea>',
isset( $this->settings['domains'] ) ? esc_attr( str_replace(',',"\n",$this->settings['domains'])) : ''
);
}
function SettingsPage(){
?>
<div class="">
<h1 class = "wp-heading-inline"><?php _e('NGINX Secure Link Protection','nginx-secret');?></h1>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php
settings_fields( 'nginx-secure' );
do_settings_sections( 'nginx-secure-settings' );
submit_button();
?>
</form>
</div>
<?php
}
}
if(is_admin()){
$nginx_secure = new NginxSecureSettings();
}
?>