-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclass-Authenticator_Protect_Upload.php
More file actions
executable file
·177 lines (136 loc) · 4.67 KB
/
Copy pathclass-Authenticator_Protect_Upload.php
File metadata and controls
executable file
·177 lines (136 loc) · 4.67 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
<?php
/**
* Protect Uploads
*
* @since 11/10/2012
* @author fb
*/
class Authenticator_Protect_Upload {
/**
* Constructor
*/
public function __construct() {
add_action( 'init', array( $this, 'check_get' ) );
}
/**
* Check the GET param file
*
* @return void
*/
public function check_get() {
if ( '' !== $_GET[ 'file' ] ) {
$this->get_file( $_GET[ 'file' ] );
}
}
/**
* Check for access to current file
*
* @param String $file
*/
public function get_file( $file ) {
$upload = wp_upload_dir();
$the_file = $file;
$file = $upload[ 'basedir' ] . '/' . $file;
if ( ! is_file( $file ) ) {
status_header( 404 );
die( '404 — File not found.' );
} else {
$image = get_posts( array(
'post_type' => 'attachment',
'meta_query' => array(
array( 'key' => '_wp_attached_file', 'value' => $the_file )
)
) );
if ( 0 < count( $image ) && 0 < $image[ 0 ]->post_parent ) { // attachment found and parent available
if ( post_password_required( $image[ 0 ]->post_parent ) ) // password for the post is not available
{
wp_die( get_the_password_form() );
}// show the password form
$status = get_post_meta( $image[ 0 ]->post_parent, '_inpsyde_protect_content', TRUE );
if ( 1 === $status && ! is_user_logged_in() ) {
wp_redirect( wp_login_url( $upload[ 'baseurl' ] . '/' . $the_file ) );
die();
}
} else {
// not a normal attachment check for thumbnail
$filename = pathinfo( $the_file );
$images = get_posts( array(
'post_type' => 'attachment',
'meta_query' => array(
array(
'key' => '_wp_attachment_metadata',
'compare' => 'LIKE',
'value' => $filename[ 'filename' ] . '.' . $filename[ 'extension' ]
)
)
) );
if ( 0 < count( $images ) ) {
foreach ( $images as $single_image ) {
$meta = wp_get_attachment_metadata( $single_image->ID );
if ( 0 < count( $meta[ 'sizes' ] ) ) {
$filepath = pathinfo( $meta[ 'file' ] );
if ( $filepath[ 'dirname' ] === $filename[ 'dirname' ] ) {// current path of the thumbnail
foreach ( $meta[ 'sizes' ] as $single_size ) {
if ( $filename[ 'filename' ] . '.' . $filename[ 'extension' ] === $single_size[ 'file' ] ) {
if ( post_password_required( $single_image->post_parent ) ) // password for the post is not available
{
wp_die( get_the_password_form() );
}// show the password form
die( 'dD' );
$status = get_post_meta(
$single_image->post_parent,
'_inpsyde_protect_content',
TRUE
);
if ( 1 === $status && ! is_user_logged_in() ) {
wp_redirect( wp_login_url( $upload[ 'baseurl' ] . '/' . $the_file ) );
die();
}
}
}
} // end if
}
} // end foreach
}
} // end if else
} // end if else
$mime = wp_check_filetype( $file );
if ( FALSE === $mime[ 'type' ] && function_exists( 'mime_content_type' ) ) {
$mime[ 'type' ] = mime_content_type( $file );
}
if ( $mime[ 'type' ] ) {
$mimetype = $mime[ 'type' ];
} else {
$mimetype = 'image/' . substr( $file, strrpos( $file, '.' ) + 1 );
}
header( 'Content-type: ' . $mimetype ); // always send this
if ( FALSE === strpos( $_SERVER[ 'SERVER_SOFTWARE' ], 'Microsoft-IIS' ) ) {
header( 'Content-Length: ' . filesize( $file ) );
}
$last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) );
$etag = '"' . md5( $last_modified ) . '"';
header( 'Last-Modified: ' . $last_modified . ' GMT' );
header( 'ETag: ' . $etag );
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 100000000 ) . ' GMT' );
// Support for Conditional GET
$client_etag = isset( $_SERVER[ 'HTTP_IF_NONE_MATCH' ] ) ? stripslashes( $_SERVER[ 'HTTP_IF_NONE_MATCH' ] ) : FALSE;
if ( ! isset( $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] ) ) {
$_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] = FALSE;
}
$client_last_modified = trim( $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] );
// If string is empty, return 0. If not, attempt to parse into a timestamp
$client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0;
// Make a timestamp for our most recent modification...
$modified_timestamp = strtotime( $last_modified );
if ( ( $client_last_modified && $client_etag )
? ( ( $client_modified_timestamp >= $modified_timestamp ) && ( $client_etag === $etag ) )
: ( ( $client_modified_timestamp >= $modified_timestamp ) || ( $client_etag === $etag ) )
) {
status_header( 304 );
exit;
}
// If we made it this far, just serve the file
readfile( $file );
die();
}
} // end class