-
Notifications
You must be signed in to change notification settings - Fork 1
/
encrypted_blog.php
256 lines (224 loc) · 8.33 KB
/
encrypted_blog.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
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
<?php
/*
Plugin Name: Encrypted Blog
Plugin URI: https://github.com/marcusds/EncryptedBlog
Description: Encrypts blog posts so that even with access to the WordPress database your posts will be private.
Version: 0.0.6.2
Author: marcusds
Author URI: https://github.com/marcusds
License: GPL2
*/
/* Copyright 2012 Marcus S.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class encryptblog {
/**
* Decrypts and returns content
* @param string $val Content coming from wordpress
* @return text Decrypted content
**/
function decrypt_content( $val ) {
if( isset( $_SESSION['encryption_key'] ) ) {
$val = encryptblog::decrypt( $val, $_SESSION['encryption_key'] );
}
return $val;
}
/**
* Encrypts and returns content
* @param string $val Content coming from wordpress
* @return text Encrypted content
**/
function encrypt_content( $val ) {
if( isset( $_SESSION['encryption_key'] ) ) {
$val = encryptblog::encrypt( $val, $_SESSION['encryption_key'] );
}
return $val;
}
/**
* Encrypts our content
* @param string $content Content to encrypt
* @param string $key Key to encrypt against
* @return boolean|string
*/
function encrypt( $content, $key ) {
$keyhash = hash('SHA256', $key, true);
if ( version_compare( PHP_VERSION, '5.3', '<' ) ) {
srand();
}
$iv = mcrypt_create_iv( mcrypt_get_iv_size( MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC ), MCRYPT_RAND );
if ( strlen( $iv_base64 = rtrim( base64_encode( $iv ), '=' ) ) != 22 ) {
return false;
}
$encrypted = base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_128, $keyhash, $content . md5( $content ), MCRYPT_MODE_CBC, $iv ) );
return $iv_base64 . $encrypted;
}
/**
* Decrypts our content
* @param string $content Content to decrypt
* @param string $key Key to decrypt against
* @parama boolean $falseonerror return false on error, true on success
* @return string Error on fail, content on success
*/
function decrypt( $content, $key, $falseonerror = false ) {
if( get_query_var( 'encrypt' ) && is_user_logged_in() && isset( $_SESSION['encryption_key'] ) && $falseonerror == false ) {
$nonce = $_REQUEST['_wpnonce'];
if ( ! wp_verify_nonce( $nonce, 'encrypt_old' ) ) exit( 'Security check' );
remove_action( 'the_content', array( 'encryptblog', 'decrypt_content' ) );
if( encryptblog::decrypt( get_the_content(), $key, true ) !== false ) {
return '<p><a href="'.get_permalink().'">Post encrypted - click here to continue.</a></p>';
}
$update_post = array();
$update_post['ID'] = get_the_ID();
$update_post['post_content'] = $content;
wp_update_post( $update_post );
return '<p><a href="'.get_permalink().'">Post encrypted - click here to continue.</a></p>';
}
$keyhash = hash( 'SHA256', $key, true );
$iv = base64_decode( substr( $content, 0, 22 ) . '==' );
if ( empty( $iv ) ) {
if( $falseonerror == true ) {
return false;
}
if( is_user_logged_in() && isset( $_SESSION['encryption_key'] ) ) {
return encryptblog::encrypt_old( get_the_ID() ).$content;
} else {
return $content;
}
}
$encrypted = substr( $content, 22 );
$decrypted = @mcrypt_decrypt( MCRYPT_RIJNDAEL_128, $keyhash, base64_decode( $encrypted ), MCRYPT_MODE_CBC, $iv );
$decrypted = rtrim( $decrypted, "\0\4" );
$hash = substr( $decrypted, -32 );
$decrypted = substr( $decrypted, 0, -32 );
if ( md5($decrypted) != $hash ) {
if( $falseonerror == true ) {
return false;
}
if( is_user_logged_in() && isset( $_SESSION['encryption_key'] ) ) {
return encryptblog::encrypt_old( get_the_ID() ).$content;
} else {
return $content;
}
}
return $decrypted;
}
/**
* Returns a link that users can click to encrypt entries that aren't encrypted.
* @param int $postid
* @return string HTML
*/
function encrypt_old( $postid ) {
$link = get_permalink( $postid );
if( strpos( $link, '?' ) !== false ) {
$link .= '&encrypt=true';
} else {
$link .= '?encrypt=true';
}
$link = wp_nonce_url( $link, 'encrypt_old' );
return '<p><a onclick="return confirm(\'Are you sure? This will not encode any previous revisions saved.\');" href="'.$link.'">Would you like to encrypt this post against the current key?</a></p>';
}
/**
* Starts a session in WordPress and sets encryption key in session.
*/
function start_session() {
if( ! session_id() ) {
session_start();
}
if( isset( $_POST['encryption_key'] ) ) {
$_SESSION['encryption_key'] = $_POST['encryption_key'];
}
}
/**
* Ends a session in WordPress
*/
function end_session() {
session_destroy();
}
/**
* When no key is set show form asking for one.
* @param $template - Full path to the normal template file.
*/
function key_get_template( $template ) {
if( is_user_logged_in() && ! isset( $_SESSION['encryption_key'] ) ) {
return dirname( __FILE__ ) . '/encrypted_blog_form.php';
} else {
return $template;
}
}
/**
* Redirects users always to homepage, never to wp-admin. We need to do so we can force them to enter a key.
* @param string $redirect_to
* @param string $request
*/
function login_redirect( $redirect_to, $request ) {
if( strpos($redirect_to, 'wp-admin') !== false ) {
return home_url();
} else {
return $redirect_to;
}
}
/**
* Redirect non-logged in users to the log in page.
*/
function must_be_logged_in() {
if( ! is_user_logged_in() ) {
if( is_front_page() ) {
wp_safe_redirect( wp_login_url(), 302 ); // Using safe redirect so users don't get redirected to 3rd party site to steal encryption key.
} else {
wp_safe_redirect( wp_login_url( get_permalink() ), 302 );
}
exit;
}
}
/**
* Redirects user to homepage, but with redirect url so we can go there after getting the key.
*/
function setup_redirect() {
wp_safe_redirect( get_site_url().'?redirect_to='.urlencode($_POST['redirect_to']), 302 );
exit;
}
/**
* Users who are not logged in won't able to see the blog's title.
* @return string
*/
function hide_title( $array ) {
if( ! is_user_logged_in() ) {
return 'Log in required';
} else {
return $array;
}
}
function setup_queryvars( $qvars )
{
$qvars[] = 'encrypt';
return $qvars;
}
}
// Setup filters & actions.
add_filter( 'the_content', array( 'encryptblog', 'decrypt_content' ), 1, 1 );
add_filter( 'content_edit_pre', array( 'encryptblog', 'decrypt_content' ), 1, 1 );
add_filter( 'content_save_pre', array( 'encryptblog', 'encrypt_content' ), 1, 1 );
add_filter( 'template_include', array( 'encryptblog', 'key_get_template' ), 1, 1 );
add_filter( 'init', array( 'encryptblog', 'start_session' ), 1 );
add_filter( 'login_redirect', array( 'encryptblog', 'login_redirect' ), 10, 3 );
add_action( 'template_redirect', array('encryptblog', 'must_be_logged_in' ) );
add_action( 'wp_logout', array( 'encryptblog', 'end_session' ) );
//add_action( 'wp_login', array( 'encryptblog', 'end_session' ) );
add_filter( 'query_vars', array( 'encryptblog', 'setup_queryvars' ) );
add_action( 'wp_login', array( 'encryptblog', 'setup_redirect' ), 10 );
add_action( 'bloginfo', array( 'encryptblog', 'hide_title') , 10, 1 );
// Remove feeds - they won't be decrypted, so there is no point in having them. They are just another potential hole. I may provide a way in the future to decrypt feeds, but it'll be far down the list because I think it's silly.
remove_action( 'do_feed_rdf', 'do_feed_rdf', 10, 1 );
remove_action( 'do_feed_rss', 'do_feed_rss', 10, 1 );
remove_action( 'do_feed_rss2', 'do_feed_rss2', 10, 1 );
remove_action( 'do_feed_atom', 'do_feed_atom', 10, 1 );
?>