add wp-rocket
This commit is contained in:
877
wp-content/plugins/wp-rocket/inc/vendors/classes/class-imagify-partner.php
vendored
Normal file
877
wp-content/plugins/wp-rocket/inc/vendors/classes/class-imagify-partner.php
vendored
Normal file
@@ -0,0 +1,877 @@
|
||||
<?php
|
||||
/**
|
||||
* Tool allowing 3rd party WordPress plugins to handle partnership with Imagify.
|
||||
*
|
||||
* @package wp-media/wp-imagify-partner
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
if ( ! class_exists( 'Imagify_Partner' ) ) :
|
||||
|
||||
/**
|
||||
* Class allowing to download, install, and activate Imagify plugin.
|
||||
*
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
class Imagify_Partner {
|
||||
|
||||
/**
|
||||
* Class version.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const VERSION = '1.0';
|
||||
|
||||
/**
|
||||
* Name of the option that stores the partner identifier.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const OPTION_NAME = 'imagifyp_id';
|
||||
|
||||
/**
|
||||
* Name of the transient that stores the error messages.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const ERROR_TRANSIENT_NAME = 'imagifyp_error';
|
||||
|
||||
/**
|
||||
* Name of the URL argument used on success.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SUCCESS_ARG = 'imp-success';
|
||||
|
||||
/**
|
||||
* Name of the URL argument used to display an error notice.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const ERROR_ARG = 'imp-error';
|
||||
|
||||
/**
|
||||
* ID of the nonce used to install Imagify.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const NONCE_NAME = 'install_imagify_from_partner';
|
||||
|
||||
/**
|
||||
* Message used as fallback in get_message().
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const FALLBACK_MESSAGE = 'Unknown message';
|
||||
|
||||
/**
|
||||
* Partner identifier.
|
||||
*
|
||||
* @var string
|
||||
* @access protected
|
||||
*/
|
||||
protected $partner;
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** INSTANCE, INIT ========================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Class constructor: sanitize and set the partner identifier.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $partner Partner identifier.
|
||||
*/
|
||||
public function __construct( $partner ) {
|
||||
$this->partner = self::sanitize_partner( $partner );
|
||||
}
|
||||
|
||||
/**
|
||||
* Class init.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function init() {
|
||||
if ( ! $this->get_partner() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! is_admin() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! self::has_imagify_api_key() ) {
|
||||
add_action( 'wp_ajax_' . $this->get_post_action(), array( $this, 'post_callback' ) );
|
||||
add_action( 'admin_post_' . $this->get_post_action(), array( $this, 'post_callback' ) );
|
||||
}
|
||||
|
||||
if ( self::is_success() || self::is_error() ) {
|
||||
add_action( 'all_admin_notices', array( __CLASS__, 'error_notice' ) );
|
||||
add_filter( 'removable_query_args', array( __CLASS__, 'add_query_args' ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** MAIN PUBLIC TOOLS ======================================================================= */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Tell if Imagify's API key is set.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function has_imagify_api_key() {
|
||||
static $has;
|
||||
|
||||
if ( isset( $has ) ) {
|
||||
return $has;
|
||||
}
|
||||
|
||||
if ( function_exists( 'get_imagify_option' ) ) {
|
||||
// Imagify is already installed and activated.
|
||||
$has = (bool) get_imagify_option( 'api_key' );
|
||||
return $has;
|
||||
}
|
||||
|
||||
if ( defined( 'IMAGIFY_API_KEY' ) && IMAGIFY_API_KEY ) {
|
||||
// It's defined in wp-config.php.
|
||||
$has = true;
|
||||
return $has;
|
||||
}
|
||||
|
||||
if ( ! is_multisite() ) {
|
||||
// Monosite: grab the value from the options table.
|
||||
$options = get_option( 'imagify_settings' );
|
||||
$has = ! empty( $options['api_key'] );
|
||||
return $has;
|
||||
}
|
||||
|
||||
$options = get_site_option( 'imagify_settings' );
|
||||
|
||||
if ( ! empty( $options['api_key'] ) ) {
|
||||
// Multisite: Imagify was activated in the network.
|
||||
$has = true;
|
||||
return $has;
|
||||
}
|
||||
|
||||
// Multisite: Imagify was activated for this site.
|
||||
$options = get_option( 'imagify_settings' );
|
||||
$has = ! empty( $options['api_key'] );
|
||||
return $has;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if Imagify is activated.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_imagify_activated() {
|
||||
return defined( 'IMAGIFY_VERSION' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if Imagify is installed.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_imagify_installed() {
|
||||
if ( self::is_imagify_activated() ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return file_exists( self::get_imagify_path() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if Imagify has been successfully installed.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_success() {
|
||||
return ! empty( $_GET[ self::SUCCESS_ARG ] ); // WPCS: CSRF ok.
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if Imagify install failed.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_error() {
|
||||
return ! empty( $_GET[ self::ERROR_ARG ] ); // WPCS: CSRF ok.
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL to install and activate Imagify.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string The URL.
|
||||
*/
|
||||
public function get_post_install_url() {
|
||||
if ( ! $this->get_partner() || ! self::current_user_can() ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$install_url = admin_url( 'admin-post.php' );
|
||||
$args = array(
|
||||
'action' => $this->get_post_action(),
|
||||
'_wpnonce' => wp_create_nonce( self::NONCE_NAME ),
|
||||
// To make sure we have a referrer.
|
||||
'_wp_http_referer' => rawurlencode( self::get_current_url() ),
|
||||
);
|
||||
|
||||
return add_query_arg( $args, $install_url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the partner identifier.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string Partner identifier.
|
||||
*/
|
||||
public function get_partner() {
|
||||
return $this->partner;
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** HOOKS =================================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Post callback to install and activate Imagify.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public function post_callback() {
|
||||
if ( ! check_ajax_referer( self::NONCE_NAME, '_wpnonce', false ) ) {
|
||||
$this->error_die();
|
||||
}
|
||||
|
||||
if ( ! self::current_user_can() ) {
|
||||
$this->error_die( 'cant_install' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the partner ID before doing anything.
|
||||
* If something goes wrong during the plugin installation, the partner ID will still be saved.
|
||||
*/
|
||||
self::store_partner( $this->get_partner() );
|
||||
|
||||
// Install Imagify.
|
||||
$result = $this->install_imagify();
|
||||
|
||||
if ( is_wp_error( $result ) ) {
|
||||
// Install failed.
|
||||
if ( self::doing_ajax() ) {
|
||||
$this->send_json_error( $result );
|
||||
}
|
||||
// Redirect to the plugins search page.
|
||||
$this->error_redirect( $result );
|
||||
}
|
||||
|
||||
// Activate Imagify.
|
||||
$result = $this->activate_imagify();
|
||||
|
||||
if ( is_wp_error( $result ) ) {
|
||||
// Activation failed.
|
||||
if ( self::doing_ajax() ) {
|
||||
$this->send_json_error( $result );
|
||||
}
|
||||
// Redirect to the plugins search page.
|
||||
$this->error_redirect( $result );
|
||||
}
|
||||
|
||||
if ( self::doing_ajax() ) {
|
||||
$this->send_json_success();
|
||||
}
|
||||
// Redirect to the partner's page.
|
||||
$this->success_redirect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe print an error notice on the plugins install page.
|
||||
* We add the query argument we use to display an error message.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public static function error_notice() {
|
||||
if ( ! self::is_error() ) {
|
||||
// No URL argument.
|
||||
return;
|
||||
}
|
||||
|
||||
$screen = get_current_screen();
|
||||
|
||||
if ( ! $screen || 'plugin-install' !== $screen->id ) {
|
||||
// Not the good page.
|
||||
return;
|
||||
}
|
||||
|
||||
$partner = self::get_stored_partner();
|
||||
|
||||
if ( ! $partner ) {
|
||||
// No partner stored in the database.
|
||||
return;
|
||||
}
|
||||
|
||||
$errors = get_transient( self::ERROR_TRANSIENT_NAME );
|
||||
|
||||
if ( ! $errors ) {
|
||||
// No error messages.
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! is_wp_error( $errors ) ) {
|
||||
// Invalid value.
|
||||
delete_transient( self::ERROR_TRANSIENT_NAME );
|
||||
return;
|
||||
}
|
||||
|
||||
$errors = $errors->get_error_messages();
|
||||
|
||||
if ( $errors ) {
|
||||
foreach ( $errors as $i => $error ) {
|
||||
if ( self::FALLBACK_MESSAGE === $error ) {
|
||||
unset( $errors[ $i ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $errors ) {
|
||||
// Add a generic message.
|
||||
$instance = new self( $partner );
|
||||
$errors[] = $instance->get_message( 'process_failed' );
|
||||
}
|
||||
|
||||
echo '<div class="error notice is-dismissible"><p>' . implode( '<br/>', $errors ) . '</p></div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the list of query variables to remove from admin area URLs.
|
||||
* We add the query arguments we use on success or error.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @see wp_removable_query_args()
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $removable_query_args An array of query variables to remove from a URL.
|
||||
* @return array
|
||||
*/
|
||||
public static function add_query_args( $removable_query_args ) {
|
||||
$removable_query_args[] = self::SUCCESS_ARG;
|
||||
$removable_query_args[] = self::ERROR_ARG;
|
||||
return $removable_query_args;
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** INFOS, INSTALL, ACTIVATE ================================================================ */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get Imagify infos from the repository.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return object The plugin infos on success. A WP_Error object on failure.
|
||||
*/
|
||||
protected function get_imagify_infos() {
|
||||
static $infos;
|
||||
|
||||
if ( isset( $infos ) ) {
|
||||
return $infos;
|
||||
}
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
|
||||
|
||||
// Get Plugin Infos.
|
||||
$infos = plugins_api( 'plugin_information', array(
|
||||
'slug' => 'imagify',
|
||||
'fields' => array(
|
||||
'short_description' => false,
|
||||
'sections' => false,
|
||||
'rating' => false,
|
||||
'ratings' => false,
|
||||
'downloaded' => false,
|
||||
'last_updated' => false,
|
||||
'added' => false,
|
||||
'tags' => false,
|
||||
'homepage' => false,
|
||||
'donate_link' => false,
|
||||
),
|
||||
) );
|
||||
|
||||
return $infos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL to download Imagify.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string The URL. An empty string on error.
|
||||
*/
|
||||
protected function get_download_url() {
|
||||
$infos = $this->get_imagify_infos();
|
||||
return ! empty( $infos->download_link ) ? $infos->download_link : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Install Imagify.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return object|null A WP_Object on failure, null on success.
|
||||
*/
|
||||
protected function install_imagify() {
|
||||
if ( self::is_imagify_installed() ) {
|
||||
// Imagify is already installed.
|
||||
return null;
|
||||
}
|
||||
|
||||
$infos = $this->get_imagify_infos();
|
||||
|
||||
if ( is_wp_error( $infos ) ) {
|
||||
return $infos;
|
||||
}
|
||||
|
||||
ob_start();
|
||||
@set_time_limit( 0 );
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
|
||||
|
||||
$upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() );
|
||||
$result = $upgrader->install( $this->get_download_url() );
|
||||
|
||||
ob_end_clean();
|
||||
|
||||
if ( is_wp_error( $result ) ) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
clearstatcache();
|
||||
|
||||
if ( ! self::is_imagify_installed() ) {
|
||||
return new WP_Error( 'process_failed', $this->get_message( 'process_failed' ) );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate Imagify.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return object|null A WP_Object on failure, null on success.
|
||||
*/
|
||||
protected function activate_imagify() {
|
||||
return activate_plugin( self::get_imagify_path(), false, is_multisite() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a message used by the class.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $message_id A message ID.
|
||||
* @return string A message.
|
||||
*/
|
||||
protected function get_message( $message_id ) {
|
||||
$messages = array(
|
||||
'success' => __( 'Plugin installed successfully.' ),
|
||||
'cant_install' => __( 'Sorry, you are not allowed to install plugins on this site.' ),
|
||||
'not_allowed' => __( 'Sorry, you are not allowed to do that.' ),
|
||||
'process_failed' => __( 'Plugin install failed.' ),
|
||||
'go_back' => __( 'Go back' ),
|
||||
);
|
||||
|
||||
/**
|
||||
* Filter messages used everywhere in the class.
|
||||
* Default messages are already translated by WordPress.
|
||||
*
|
||||
* @since 1.0
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param array $messages Messages.
|
||||
*/
|
||||
$messages = apply_filters( 'imagify_partner_messages_' . $this->get_partner(), $messages );
|
||||
|
||||
return ! empty( $messages[ $message_id ] ) ? $messages[ $message_id ] : self::FALLBACK_MESSAGE;
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** HANDLE SUCCESS ========================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Send a JSON response back to an Ajax request, indicating success.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected function send_json_success() {
|
||||
delete_transient( self::ERROR_TRANSIENT_NAME );
|
||||
|
||||
wp_send_json_success( $this->get_message( 'success' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect the user after Imagify is successfully installed and activated.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
protected function success_redirect() {
|
||||
delete_transient( self::ERROR_TRANSIENT_NAME );
|
||||
|
||||
wp_safe_redirect( esc_url_raw( $this->get_success_redirection_url() ) );
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL to redirect the user to after Imagify is successfully installed and activated: the referrer (partner's page URL).
|
||||
* A "success" argument is added.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_success_redirection_url() {
|
||||
$success_url = add_query_arg( array(
|
||||
self::SUCCESS_ARG => 1,
|
||||
self::ERROR_ARG => false,
|
||||
), wp_get_referer() );
|
||||
|
||||
/**
|
||||
* Filter the URL to redirect the user to after Imagify is successfully installed and activated.
|
||||
* Default is the partner's page URL.
|
||||
*
|
||||
* @since 1.0
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $success_url The URL.
|
||||
*/
|
||||
return apply_filters( 'imagify_partner_success_url_' . $this->get_partner(), $success_url );
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** HANDLE ERROR ============================================================================ */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Die on error.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $message_id An error message ID.
|
||||
*/
|
||||
protected function error_die( $message_id = 'not_allowed' ) {
|
||||
$message = $this->get_message( $message_id );
|
||||
|
||||
if ( self::doing_ajax() ) {
|
||||
$message = new WP_Error( $message_id, $message );
|
||||
$this->send_json_error( $message );
|
||||
}
|
||||
|
||||
if ( wp_get_referer() ) {
|
||||
$message .= '</p><p>';
|
||||
$message .= sprintf( '<a href="%s">%s</a>',
|
||||
esc_url( remove_query_arg( 'updated', wp_get_referer() ) ),
|
||||
$this->get_message( 'go_back' )
|
||||
);
|
||||
}
|
||||
|
||||
wp_die( $message, '', 403 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a JSON response back to an Ajax request, indicating failure.
|
||||
* This is a backward compatible version of wp_send_json_error(): WP_Error object handling was introduced in WP 4.1.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param mixed $data Data to encode as JSON, then print and die.
|
||||
*/
|
||||
protected function send_json_error( $data ) {
|
||||
if ( is_wp_error( $data ) ) {
|
||||
$result = array();
|
||||
foreach ( $data->errors as $code => $messages ) {
|
||||
foreach ( $messages as $message ) {
|
||||
$result[] = array(
|
||||
'code' => $code,
|
||||
'message' => $message,
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$result = $data;
|
||||
}
|
||||
|
||||
wp_send_json_error( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Store an error message in a transient then redirect the user.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param object $error A WP_Error object.
|
||||
*/
|
||||
protected function error_redirect( $error ) {
|
||||
set_transient( self::ERROR_TRANSIENT_NAME, $error, 30 );
|
||||
|
||||
wp_safe_redirect( esc_url_raw( $this->get_error_redirection_url() ) );
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL to redirect the user to after Imagify installation failure: the plugins search page URL, searching for Imagify.
|
||||
* An "error" argument is added, to display an error notice.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_error_redirection_url() {
|
||||
$error_url = 'plugin-install.php?s=imagify&tab=search&type=term&' . self::ERROR_ARG . '=1';
|
||||
$error_url = is_multisite() ? network_admin_url( $error_url ) : admin_url( $error_url );
|
||||
|
||||
/**
|
||||
* Filter the URL to redirect the user to after Imagify installation failure.
|
||||
* Default is the plugins search page URL.
|
||||
*
|
||||
* @since 1.0
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $error_url The URL.
|
||||
*/
|
||||
return apply_filters( 'imagify_partner_error_url_' . $this->get_partner(), $error_url );
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** STORING THE PARTNER ID IN DATABASE ====================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the partner identifier stored in the Database.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string|bool The partner identifier, or false if none is stored.
|
||||
*/
|
||||
public static function get_stored_partner() {
|
||||
$partner = get_option( self::OPTION_NAME );
|
||||
|
||||
if ( $partner && is_string( $partner ) ) {
|
||||
$partner = self::sanitize_partner( $partner );
|
||||
}
|
||||
|
||||
return $partner ? $partner : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the partner identifier stored in the Database.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*/
|
||||
public static function delete_stored_partner() {
|
||||
if ( false !== get_option( self::OPTION_NAME ) ) {
|
||||
delete_option( self::OPTION_NAME );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the partner identifier in Database.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $partner The partner identifier to store.
|
||||
*/
|
||||
protected static function store_partner( $partner ) {
|
||||
if ( false === get_option( self::OPTION_NAME ) ) {
|
||||
add_option( self::OPTION_NAME, $partner );
|
||||
} else {
|
||||
update_option( self::OPTION_NAME, $partner );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize a partner ID.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access protected
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @param string $partner Partner identifier.
|
||||
* @return string
|
||||
*/
|
||||
protected static function sanitize_partner( $partner ) {
|
||||
return preg_replace( '@[^a-z0-9_-]@', '', strtolower( (string) $partner ) );
|
||||
}
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
/** VARIOUS TOOLS =========================================================================== */
|
||||
/** ----------------------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Get the action.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string Partner identifier.
|
||||
*/
|
||||
public function get_post_action() {
|
||||
return 'install_imagify_from_partner_' . $this->get_partner();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether the current request is a WordPress Ajax request.
|
||||
* This is a clone of wp_doing_ajax(), intriduced in WP 4.7.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool True if it's a WordPress Ajax request, false otherwise.
|
||||
*/
|
||||
public static function doing_ajax() {
|
||||
/**
|
||||
* Filters whether the current request is a WordPress Ajax request.
|
||||
*
|
||||
* @since 1.0
|
||||
*
|
||||
* @param bool $wp_doing_ajax Whether the current request is a WordPress Ajax request.
|
||||
*/
|
||||
return apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Imagify's file path.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string The file path.
|
||||
*/
|
||||
public static function get_imagify_path() {
|
||||
if ( defined( 'IMAGIFY_FILE' ) ) {
|
||||
return IMAGIFY_FILE;
|
||||
}
|
||||
|
||||
return WP_PLUGIN_DIR . '/imagify/imagify.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Tell if the current user can install and activate Imagify.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function current_user_can() {
|
||||
static $can;
|
||||
|
||||
if ( ! isset( $can ) ) {
|
||||
$can = is_multisite() ? 'manage_network_plugins' : 'install_plugins';
|
||||
$can = current_user_can( $can );
|
||||
}
|
||||
|
||||
return $can;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current URL.
|
||||
*
|
||||
* @since 1.0
|
||||
* @access public
|
||||
* @author Grégory Viguier
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_current_url() {
|
||||
$port = (int) $_SERVER['SERVER_PORT'];
|
||||
$port = 80 !== $port && 443 !== $port ? ( ':' . $port ) : '';
|
||||
$url = ! empty( $GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI'] ) ? $GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI'] : ( ! empty( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '' );
|
||||
|
||||
return 'http' . ( is_ssl() ? 's' : '' ) . '://' . $_SERVER['HTTP_HOST'] . $port . $url;
|
||||
}
|
||||
}
|
||||
|
||||
endif;
|
358
wp-content/plugins/wp-rocket/inc/vendors/classes/class-minify-css-urirewriter.php
vendored
Normal file
358
wp-content/plugins/wp-rocket/inc/vendors/classes/class-minify-css-urirewriter.php
vendored
Normal file
@@ -0,0 +1,358 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Minify_CSS_UriRewriter
|
||||
*
|
||||
* @package Minify
|
||||
*/
|
||||
|
||||
/**
|
||||
* Rewrite file-relative URIs as root-relative in CSS files
|
||||
*
|
||||
* @package Minify
|
||||
* @author Stephen Clay <steve@mrclay.org>
|
||||
*/
|
||||
class Minify_CSS_UriRewriter {
|
||||
|
||||
/**
|
||||
* rewrite() and rewriteRelative() append debugging information here
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $debugText = '';
|
||||
|
||||
/**
|
||||
* In CSS content, rewrite file relative URIs as root relative
|
||||
*
|
||||
* @param string $css
|
||||
*
|
||||
* @param string $currentDir The directory of the current CSS file.
|
||||
*
|
||||
* @param string $docRoot The document root of the web site in which
|
||||
* the CSS file resides (default = $_SERVER['DOCUMENT_ROOT']).
|
||||
*
|
||||
* @param array $symlinks (default = array()) If the CSS file is stored in
|
||||
* a symlink-ed directory, provide an array of link paths to
|
||||
* target paths, where the link paths are within the document root. Because
|
||||
* paths need to be normalized for this to work, use "//" to substitute
|
||||
* the doc root in the link paths (the array keys). E.g.:
|
||||
* <code>
|
||||
* array('//symlink' => '/real/target/path') // unix
|
||||
* array('//static' => 'D:\\staticStorage') // Windows
|
||||
* </code>
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function rewrite($css, $currentDir, $docRoot = null, $symlinks = array())
|
||||
{
|
||||
self::$_docRoot = self::_realpath(
|
||||
$docRoot ? $docRoot : $_SERVER['DOCUMENT_ROOT']
|
||||
);
|
||||
self::$_currentDir = self::_realpath($currentDir);
|
||||
self::$_symlinks = array();
|
||||
|
||||
// normalize symlinks in order to map to link
|
||||
foreach ($symlinks as $link => $target) {
|
||||
$link = ($link === '//') ? self::$_docRoot : str_replace('//', self::$_docRoot . '/', $link);
|
||||
$link = strtr($link, '/', DIRECTORY_SEPARATOR);
|
||||
|
||||
self::$_symlinks[$link] = self::_realpath($target);
|
||||
}
|
||||
|
||||
self::$debugText .= "docRoot : " . self::$_docRoot . "\n"
|
||||
. "currentDir : " . self::$_currentDir . "\n";
|
||||
if (self::$_symlinks) {
|
||||
self::$debugText .= "symlinks : " . var_export(self::$_symlinks, 1) . "\n";
|
||||
}
|
||||
self::$debugText .= "\n";
|
||||
|
||||
$css = self::_trimUrls($css);
|
||||
|
||||
$css = self::_owlifySvgPaths($css);
|
||||
|
||||
// rewrite
|
||||
$pattern = '/@import\\s+([\'"])(.*?)[\'"]/';
|
||||
$css = preg_replace_callback($pattern, array(self::$className, '_processUriCB'), $css);
|
||||
|
||||
$pattern = '/url\\(\\s*([\'"](.*?)[\'"]|[^\\)\\s]+)\\s*\\)/';
|
||||
$css = preg_replace_callback($pattern, array(self::$className, '_processUriCB'), $css);
|
||||
|
||||
$css = self::_unOwlify($css);
|
||||
|
||||
return $css;
|
||||
}
|
||||
|
||||
/**
|
||||
* In CSS content, prepend a path to relative URIs
|
||||
*
|
||||
* @param string $css
|
||||
*
|
||||
* @param string $path The path to prepend.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function prepend($css, $path)
|
||||
{
|
||||
self::$_prependPath = $path;
|
||||
|
||||
$css = self::_trimUrls($css);
|
||||
|
||||
$css = self::_owlifySvgPaths($css);
|
||||
|
||||
// append
|
||||
$pattern = '/@import\\s+([\'"])(.*?)[\'"]/';
|
||||
$css = preg_replace_callback($pattern, array(self::$className, '_processUriCB'), $css);
|
||||
|
||||
$pattern = '/url\\(\\s*([\'"](.*?)[\'"]|[^\\)\\s]+)\\s*\\)/';
|
||||
$css = preg_replace_callback($pattern, array(self::$className, '_processUriCB'), $css);
|
||||
|
||||
$css = self::_unOwlify($css);
|
||||
|
||||
self::$_prependPath = null;
|
||||
|
||||
return $css;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a root relative URI from a file relative URI
|
||||
*
|
||||
* <code>
|
||||
* Minify_CSS_UriRewriter::rewriteRelative(
|
||||
* '../img/hello.gif'
|
||||
* , '/home/user/www/css' // path of CSS file
|
||||
* , '/home/user/www' // doc root
|
||||
* );
|
||||
* // returns '/img/hello.gif'
|
||||
*
|
||||
* // example where static files are stored in a symlinked directory
|
||||
* Minify_CSS_UriRewriter::rewriteRelative(
|
||||
* 'hello.gif'
|
||||
* , '/var/staticFiles/theme'
|
||||
* , '/home/user/www'
|
||||
* , array('/home/user/www/static' => '/var/staticFiles')
|
||||
* );
|
||||
* // returns '/static/theme/hello.gif'
|
||||
* </code>
|
||||
*
|
||||
* @param string $uri file relative URI
|
||||
*
|
||||
* @param string $realCurrentDir realpath of the current file's directory.
|
||||
*
|
||||
* @param string $realDocRoot realpath of the site document root.
|
||||
*
|
||||
* @param array $symlinks (default = array()) If the file is stored in
|
||||
* a symlink-ed directory, provide an array of link paths to
|
||||
* real target paths, where the link paths "appear" to be within the document
|
||||
* root. E.g.:
|
||||
* <code>
|
||||
* array('/home/foo/www/not/real/path' => '/real/target/path') // unix
|
||||
* array('C:\\htdocs\\not\\real' => 'D:\\real\\target\\path') // Windows
|
||||
* </code>
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function rewriteRelative($uri, $realCurrentDir, $realDocRoot, $symlinks = array())
|
||||
{
|
||||
// prepend path with current dir separator (OS-independent)
|
||||
$path = strtr($realCurrentDir, '/', DIRECTORY_SEPARATOR);
|
||||
$path .= DIRECTORY_SEPARATOR . strtr($uri, '/', DIRECTORY_SEPARATOR);
|
||||
|
||||
self::$debugText .= "file-relative URI : {$uri}\n"
|
||||
. "path prepended : {$path}\n";
|
||||
|
||||
// "unresolve" a symlink back to doc root
|
||||
foreach ($symlinks as $link => $target) {
|
||||
if (0 === strpos($path, $target)) {
|
||||
// replace $target with $link
|
||||
$path = $link . substr($path, strlen($target));
|
||||
|
||||
self::$debugText .= "symlink unresolved : {$path}\n";
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
// strip doc root
|
||||
$path = substr($path, strlen($realDocRoot));
|
||||
|
||||
self::$debugText .= "docroot stripped : {$path}\n";
|
||||
|
||||
// fix to root-relative URI
|
||||
$uri = strtr($path, '/\\', '//');
|
||||
$uri = self::removeDots($uri);
|
||||
|
||||
self::$debugText .= "traversals removed : {$uri}\n\n";
|
||||
|
||||
return $uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove instances of "./" and "../" where possible from a root-relative URI
|
||||
*
|
||||
* @param string $uri
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function removeDots($uri)
|
||||
{
|
||||
$uri = str_replace('/./', '/', $uri);
|
||||
// inspired by patch from Oleg Cherniy
|
||||
do {
|
||||
$uri = preg_replace('@/[^/]+/\\.\\./@', '/', $uri, 1, $changed);
|
||||
} while ($changed);
|
||||
|
||||
return $uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines which class to call as part of callbacks, change this
|
||||
* if you extend Minify_CSS_UriRewriter
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $className = 'Minify_CSS_UriRewriter';
|
||||
|
||||
/**
|
||||
* Get realpath with any trailing slash removed. If realpath() fails,
|
||||
* just remove the trailing slash.
|
||||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @return mixed path with no trailing slash
|
||||
*/
|
||||
protected static function _realpath($path)
|
||||
{
|
||||
$realPath = realpath($path);
|
||||
if ($realPath !== false) {
|
||||
$path = $realPath;
|
||||
}
|
||||
|
||||
return rtrim($path, '/\\');
|
||||
}
|
||||
|
||||
/**
|
||||
* Directory of this stylesheet
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $_currentDir = '';
|
||||
|
||||
/**
|
||||
* DOC_ROOT
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $_docRoot = '';
|
||||
|
||||
/**
|
||||
* directory replacements to map symlink targets back to their
|
||||
* source (within the document root) E.g. '/var/www/symlink' => '/var/realpath'
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $_symlinks = array();
|
||||
|
||||
/**
|
||||
* Path to prepend
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $_prependPath = null;
|
||||
|
||||
/**
|
||||
* @param string $css
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function _trimUrls($css)
|
||||
{
|
||||
$pattern = '/
|
||||
url\\( # url(
|
||||
\\s*
|
||||
([^\\)]+?) # 1 = URI (assuming does not contain ")")
|
||||
\\s*
|
||||
\\) # )
|
||||
/x';
|
||||
|
||||
return preg_replace($pattern, 'url($1)', $css);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $m
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function _processUriCB($m)
|
||||
{
|
||||
// $m matched either '/@import\\s+([\'"])(.*?)[\'"]/' or '/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
|
||||
$isImport = ($m[0][0] === '@');
|
||||
// determine URI and the quote character (if any)
|
||||
if ($isImport) {
|
||||
$quoteChar = $m[1];
|
||||
$uri = $m[2];
|
||||
} else {
|
||||
// $m[1] is either quoted or not
|
||||
$quoteChar = ($m[1][0] === "'" || $m[1][0] === '"') ? $m[1][0] : '';
|
||||
|
||||
$uri = ($quoteChar === '') ? $m[1] : substr($m[1], 1, strlen($m[1]) - 2);
|
||||
}
|
||||
|
||||
if ($uri === '') {
|
||||
return $m[0];
|
||||
}
|
||||
|
||||
// if not root/scheme relative and not starts with scheme
|
||||
if (!preg_match('~^(/|[a-z]+\:)~', $uri)) {
|
||||
// URI is file-relative: rewrite depending on options
|
||||
if (self::$_prependPath === null) {
|
||||
$uri = self::rewriteRelative($uri, self::$_currentDir, self::$_docRoot, self::$_symlinks);
|
||||
} else {
|
||||
$uri = self::$_prependPath . $uri;
|
||||
if ($uri[0] === '/') {
|
||||
$root = '';
|
||||
$rootRelative = $uri;
|
||||
$uri = $root . self::removeDots($rootRelative);
|
||||
} elseif (preg_match('@^((https?\:)?//([^/]+))/@', $uri, $m) && (false !== strpos($m[3], '.'))) {
|
||||
$root = $m[1];
|
||||
$rootRelative = substr($uri, strlen($root));
|
||||
$uri = $root . self::removeDots($rootRelative);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($isImport) {
|
||||
return "@import {$quoteChar}{$uri}{$quoteChar}";
|
||||
} else {
|
||||
return "url({$quoteChar}{$uri}{$quoteChar})";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mungs some inline SVG URL declarations so they won't be touched
|
||||
*
|
||||
* @link https://github.com/mrclay/minify/issues/517
|
||||
* @see _unOwlify
|
||||
*
|
||||
* @param string $css
|
||||
* @return string
|
||||
*/
|
||||
private static function _owlifySvgPaths($css)
|
||||
{
|
||||
$pattern = '~\b((?:clip-path|mask|-webkit-mask)\s*\:\s*)url(\(\s*#\w+\s*\))~';
|
||||
|
||||
return preg_replace($pattern, '$1owl$2', $css);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undo work of _owlify
|
||||
*
|
||||
* @see _owlifySvgPaths
|
||||
*
|
||||
* @param string $css
|
||||
* @return string
|
||||
*/
|
||||
private static function _unOwlify($css)
|
||||
{
|
||||
$pattern = '~\b((?:clip-path|mask|-webkit-mask)\s*\:\s*)owl~';
|
||||
|
||||
return preg_replace($pattern, '$1url', $css);
|
||||
}
|
||||
}
|
1472
wp-content/plugins/wp-rocket/inc/vendors/classes/class-rocket-mobile-detect.php
vendored
Normal file
1472
wp-content/plugins/wp-rocket/inc/vendors/classes/class-rocket-mobile-detect.php
vendored
Normal file
File diff suppressed because one or more lines are too long
209
wp-content/plugins/wp-rocket/inc/vendors/ip_in_range.php
vendored
Normal file
209
wp-content/plugins/wp-rocket/inc/vendors/ip_in_range.php
vendored
Normal file
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ip_in_range.php - Function to determine if an IP is located in a
|
||||
* specific range as specified via several alternative
|
||||
* formats.
|
||||
*
|
||||
* Network ranges can be specified as:
|
||||
* 1. Wildcard format: 1.2.3.*
|
||||
* 2. CIDR format: 1.2.3/24 OR 1.2.3.4/255.255.255.0
|
||||
* 3. Start-End IP format: 1.2.3.0-1.2.3.255
|
||||
*
|
||||
* Return value BOOLEAN : ip_in_range($ip, $range);
|
||||
*
|
||||
* Copyright 2008: Paul Gregg <pgregg@pgregg.com>
|
||||
* 10 January 2008
|
||||
* Version: 1.2
|
||||
*
|
||||
* Source website: http://www.pgregg.com/projects/php/ip_in_range/
|
||||
* Version 1.2
|
||||
*
|
||||
* This software is Donationware - if you feel you have benefited from
|
||||
* the use of this tool then please consider a donation. The value of
|
||||
* which is entirely left up to your discretion.
|
||||
* http://www.pgregg.com/donate/
|
||||
*
|
||||
* Please do not remove this header, or source attibution from this file.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Modified by James Greene <james@cloudflare.com> to include IPV6 support
|
||||
* (original version only supported IPV4).
|
||||
* 21 May 2012
|
||||
*/
|
||||
|
||||
// In order to simplify working with IP addresses (in binary) and their
|
||||
// netmasks, it is easier to ensure that the binary strings are padded
|
||||
// with zeros out to 32 characters - IP addresses are 32 bit numbers
|
||||
function rocket_decbin32($dec) {
|
||||
return str_pad(decbin($dec), 32, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
// This function takes 2 arguments, an IP address and a "range" in several
|
||||
// different formats.
|
||||
// Network ranges can be specified as:
|
||||
// 1. Wildcard format: 1.2.3.*
|
||||
// 2. CIDR format: 1.2.3/24 OR 1.2.3.4/255.255.255.0
|
||||
// 3. Start-End IP format: 1.2.3.0-1.2.3.255
|
||||
// The function will return true if the supplied IP is within the range.
|
||||
// Note little validation is done on the range inputs - it expects you to
|
||||
// use one of the above 3 formats.
|
||||
function rocket_ipv4_in_range($ip, $range) {
|
||||
if (strpos($range, '/') !== false) {
|
||||
// $range is in IP/NETMASK format
|
||||
list($range, $netmask) = explode('/', $range, 2);
|
||||
if (strpos($netmask, '.') !== false) {
|
||||
// $netmask is a 255.255.0.0 format
|
||||
$netmask = str_replace('*', '0', $netmask);
|
||||
$netmask_dec = ip2long($netmask);
|
||||
return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) );
|
||||
} else {
|
||||
// $netmask is a CIDR size block
|
||||
// fix the range argument
|
||||
$x = explode('.', $range);
|
||||
while(count($x)<4) $x[] = '0';
|
||||
list($a,$b,$c,$d) = $x;
|
||||
$range = sprintf("%u.%u.%u.%u", empty($a)?'0':$a, empty($b)?'0':$b,empty($c)?'0':$c,empty($d)?'0':$d);
|
||||
$range_dec = ip2long($range);
|
||||
$ip_dec = ip2long($ip);
|
||||
|
||||
# Strategy 1 - Create the netmask with 'netmask' 1s and then fill it to 32 with 0s
|
||||
#$netmask_dec = bindec(str_pad('', $netmask, '1') . str_pad('', 32-$netmask, '0'));
|
||||
|
||||
# Strategy 2 - Use math to create it
|
||||
$wildcard_dec = pow(2, (32-$netmask)) - 1;
|
||||
$netmask_dec = ~ $wildcard_dec;
|
||||
|
||||
return (($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec));
|
||||
}
|
||||
} else {
|
||||
// range might be 255.255.*.* or 1.2.3.0-1.2.3.255
|
||||
if (strpos($range, '*') !==false) { // a.b.*.* format
|
||||
// Just convert to A-B format by setting * to 0 for A and 255 for B
|
||||
$lower = str_replace('*', '0', $range);
|
||||
$upper = str_replace('*', '255', $range);
|
||||
$range = "$lower-$upper";
|
||||
}
|
||||
|
||||
if (strpos($range, '-')!==false) { // A-B format
|
||||
list($lower, $upper) = explode('-', $range, 2);
|
||||
$lower_dec = (float)sprintf("%u",ip2long($lower));
|
||||
$upper_dec = (float)sprintf("%u",ip2long($upper));
|
||||
$ip_dec = (float)sprintf("%u",ip2long($ip));
|
||||
return ( ($ip_dec>=$lower_dec) && ($ip_dec<=$upper_dec) );
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function rocket_ip2long6($ip) {
|
||||
if (substr_count($ip, '::')) {
|
||||
$ip = str_replace('::', str_repeat(':0000', 8 - substr_count($ip, ':')) . ':', $ip);
|
||||
}
|
||||
|
||||
$ip = explode(':', $ip);
|
||||
$r_ip = '';
|
||||
foreach ($ip as $v) {
|
||||
$r_ip .= str_pad( base_convert( preg_replace("/[^0-9a-fA-F]/", "", $v ), 16, 2 ), 16, 0, STR_PAD_LEFT );
|
||||
}
|
||||
|
||||
return base_convert($r_ip, 2, 10);
|
||||
}
|
||||
|
||||
// Get the ipv6 full format and return it as a decimal value.
|
||||
function get_rocket_ipv6_full($ip) {
|
||||
$pieces = explode ("/", $ip, 2);
|
||||
$left_piece = $pieces[0];
|
||||
$right_piece = null;
|
||||
if (count($pieces) > 1) $right_piece = $pieces[1];
|
||||
|
||||
// Extract out the main IP pieces
|
||||
$ip_pieces = explode("::", $left_piece, 2);
|
||||
$main_ip_piece = $ip_pieces[0];
|
||||
$last_ip_piece = null;
|
||||
if (count($ip_pieces) > 1) $last_ip_piece = $ip_pieces[1];
|
||||
|
||||
// Pad out the shorthand entries.
|
||||
$main_ip_pieces = explode(":", $main_ip_piece);
|
||||
foreach($main_ip_pieces as $key=>$val) {
|
||||
$main_ip_pieces[$key] = str_pad($main_ip_pieces[$key], 4, "0", STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
// Check to see if the last IP block (part after ::) is set
|
||||
$last_piece = "";
|
||||
$size = count($main_ip_pieces);
|
||||
if (trim($last_ip_piece) != "") {
|
||||
$last_piece = str_pad($last_ip_piece, 4, "0", STR_PAD_LEFT);
|
||||
|
||||
// Build the full form of the IPV6 address considering the last IP block set
|
||||
for ($i = $size; $i < 7; $i++) {
|
||||
$main_ip_pieces[$i] = "0000";
|
||||
}
|
||||
$main_ip_pieces[7] = $last_piece;
|
||||
}
|
||||
else {
|
||||
// Build the full form of the IPV6 address
|
||||
for ($i = $size; $i < 8; $i++) {
|
||||
$main_ip_pieces[$i] = "0000";
|
||||
}
|
||||
}
|
||||
|
||||
// Rebuild the final long form IPV6 address
|
||||
$final_ip = implode(":", $main_ip_pieces);
|
||||
|
||||
return rocket_ip2long6($final_ip);
|
||||
}
|
||||
|
||||
// Determine whether the IPV6 address is within range.
|
||||
// $ip is the IPV6 address in decimal format to check if its within the IP range created by the cloudflare IPV6 address, $range_ip.
|
||||
// $ip and $range_ip are converted to full IPV6 format.
|
||||
// Returns true if the IPV6 address, $ip, is within the range from $range_ip. False otherwise.
|
||||
function rocket_ipv6_in_range($ip, $range_ip) {
|
||||
$pieces = explode ("/", $range_ip, 2);
|
||||
$left_piece = $pieces[0];
|
||||
$right_piece = $pieces[1];
|
||||
|
||||
// Extract out the main IP pieces
|
||||
$ip_pieces = explode("::", $left_piece, 2);
|
||||
$main_ip_piece = $ip_pieces[0];
|
||||
$last_ip_piece = $ip_pieces[1];
|
||||
|
||||
// Pad out the shorthand entries.
|
||||
$main_ip_pieces = explode(":", $main_ip_piece);
|
||||
foreach($main_ip_pieces as $key=>$val) {
|
||||
$main_ip_pieces[$key] = str_pad($main_ip_pieces[$key], 4, "0", STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
// Create the first and last pieces that will denote the IPV6 range.
|
||||
$first = $main_ip_pieces;
|
||||
$last = $main_ip_pieces;
|
||||
|
||||
// Check to see if the last IP block (part after ::) is set
|
||||
$last_piece = "";
|
||||
$size = count($main_ip_pieces);
|
||||
if (trim($last_ip_piece) != "") {
|
||||
$last_piece = str_pad($last_ip_piece, 4, "0", STR_PAD_LEFT);
|
||||
|
||||
// Build the full form of the IPV6 address considering the last IP block set
|
||||
for ($i = $size; $i < 7; $i++) {
|
||||
$first[$i] = "0000";
|
||||
$last[$i] = "ffff";
|
||||
}
|
||||
$main_ip_pieces[7] = $last_piece;
|
||||
}
|
||||
else {
|
||||
// Build the full form of the IPV6 address
|
||||
for ($i = $size; $i < 8; $i++) {
|
||||
$first[$i] = "0000";
|
||||
$last[$i] = "ffff";
|
||||
}
|
||||
}
|
||||
|
||||
// Rebuild the final long form IPV6 address
|
||||
$first = rocket_ip2long6(implode(":", $first));
|
||||
$last = rocket_ip2long6(implode(":", $last));
|
||||
$in_range = ($ip >= $first && $ip <= $last);
|
||||
|
||||
return $in_range;
|
||||
}
|
Reference in New Issue
Block a user