uo_tincanny_file_motified_http_status_code
Filters the HTTP status code returned for modified files, defaulting to 304 Not Modified.
add_filter( 'uo_tincanny_file_motified_http_status_code', $callback, 10, 1 );
Description
Allows modification of the HTTP status code returned for tinCanny protected files. Developers can filter this hook to override the default 304 Not Modified status, for example, to force a revalidation or report a different status if the file is not found or has changed.
Usage
add_filter( 'uo_tincanny_file_motified_http_status_code', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
/**
* Example of modifying the HTTP status code for unmodified files.
*
* This filter allows you to change the default 304 Not Modified
* status code to something else if needed, for specific scenarios.
* For example, you might want to return a 200 OK with an empty body
* in some edge cases, though 304 is standard.
*/
add_filter( 'uo_tincanny_file_motified_http_status_code', 'my_custom_unmodified_file_status', 10, 1 );
function my_custom_unmodified_file_status( $http_status_code ) {
// In most cases, you'll want to return the original status code
// or a standard one like 304. This is just an example of how
// you *could* change it.
// For instance, if you wanted to always return 200 OK (not recommended
// for performance and caching) if the client's ETag matches,
// you would modify the logic in the source file accordingly.
// This filter is placed *after* the check, so it receives the
// intended status code from the original logic.
// Let's add a hypothetical condition: if the file size is very small,
// maybe we don't want to send a 304, but this is a very unusual case.
// We need access to the file path, which this filter doesn't directly provide.
// Therefore, a more realistic use case is to simply return the default or
// a known alternative.
// For demonstration, let's say we want to default to 304, but if a specific
// query parameter is present in the request, we want to force a 200.
// This requires checking $_GET, which is available globally.
if ( isset( $_GET['force_refresh'] ) && 'true' === $_GET['force_refresh'] ) {
return 200; // Force a 200 OK status code
}
// Otherwise, return the original status code (which is 304 by default)
return $http_status_code;
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/core/tin-canny-protection.php:173
private function set_headers() {
$headers = array(
// Set content type
'Content-Type' => 'Content-Type: ' . $this->get_content_type(),
// Set X-Robots-Tag
'X-Robots-Tag' => 'X-Robots-Tag: none',
);
// Set content length
if ( strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS' ) === false ) {
$headers['Content-Length'] = 'Content-Length: ' . filesize( $this->content_data->complete_path );
}
// Set Cache control
$headers['Cache-Control'] = 'Cache-Control: no-store, no-cache, must-revalidate';
$headers['Pragma'] = 'Pragma: no-cache';
$headers['Expires'] = 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 100000000 ) . ' GMT';
// Get and set last modified date
$last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $this->content_data->complete_path ) );
$headers['Last-Modified'] = 'Last-Modified: ' . $last_modified . ' GMT';
// Create eTag using a md5 hash of the last modified date
$etag = '"' . md5( $last_modified ) . '"';
$headers['ETag'] = 'ETag: ' . $etag;
// Check if it supports xsendfile
if ( $this->supports_mod_xsendfile() ) {
$headers['X-Sendfile'] = 'X-Sendfile: ' . $this->content_data->complete_path;
}
$headers = apply_filters( 'uo_tincanny_protection_headers', $headers );
if ( is_array( $headers ) && ! empty( $headers ) ) {
foreach ( $headers as $header ) {
if ( ! empty( $header ) ) {
header( $header );
}
}
}
// Get client eTag
$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;
}
// Get client last modified
$client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
// Get the 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 );
// Check if the client data is defined
$is_client_data_defined = $client_last_modified && $client_etag;
// Check if the client has a more recent change to the file than the file in the server
$client_has_more_recent_modification = $client_modified_timestamp >= $modified_timestamp;
// Check if the client has the same eTag
$client_has_same_etag = $client_etag == $etag;
// Compare last modified from the file with the last modified from the client
if ( $is_client_data_defined ? ( $client_has_more_recent_modification && $client_has_same_etag ) : ( $client_has_more_recent_modification || $client_has_same_etag ) ) {
status_header( apply_filters( 'uo_tincanny_file_motified_http_status_code', 304 ) );
exit;
}
}