@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
/**
|
||||
* Theme backward compatibility check.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.2
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Lib.Compatibility
|
||||
*/
|
||||
class Ai1ec_Compatibility_Check extends Ai1ec_Base {
|
||||
|
||||
/**
|
||||
* @var null|bool Calculated response.
|
||||
*/
|
||||
protected $_use_backward_compatibility = null;
|
||||
|
||||
/**
|
||||
* Returns whether calendar is using backward compatibility or not.
|
||||
*
|
||||
* @return bool|null Result
|
||||
*
|
||||
* @throws Ai1ec_Bootstrap_Exception
|
||||
*/
|
||||
public function use_backward_compatibility() {
|
||||
if ( null === $this->_use_backward_compatibility ) {
|
||||
$this->_use_backward_compatibility = (
|
||||
AI1EC_THEME_COMPATIBILITY_FER &&
|
||||
! $this->_registry->get(
|
||||
'model.settings'
|
||||
)->get( 'ai1ec_use_frontend_rendering', false )
|
||||
);
|
||||
}
|
||||
return $this->_use_backward_compatibility;
|
||||
}
|
||||
|
||||
/**
|
||||
* Observes settings changes.
|
||||
*
|
||||
* If setting ai1ec_use_frontend_rendering is changed and set to true
|
||||
* perfoms theme check.
|
||||
*
|
||||
* Checks if Date format was changed, and update dates to the new format
|
||||
*
|
||||
* @param array $old_options Old options array.
|
||||
* @param array $new_options New options array.
|
||||
*
|
||||
* @return void Method does not return.
|
||||
*
|
||||
* @throws Ai1ec_Bootstrap_Exception
|
||||
*/
|
||||
public function ai1ec_settings_observer( $old_options, $new_options ) {
|
||||
|
||||
// Date format change checker
|
||||
$old_date_format_value = isset( $old_options['input_date_format'] )
|
||||
? $old_options['input_date_format']['value']
|
||||
: null;
|
||||
$new_date_format_value = isset( $new_options['input_date_format'] )
|
||||
? $new_options['input_date_format']['value']
|
||||
: null;
|
||||
if (
|
||||
null !== $old_date_format_value&&
|
||||
null !== $new_date_format_value &&
|
||||
$old_date_format_value !== $new_date_format_value
|
||||
) {
|
||||
// Get "Default calendar start date"
|
||||
$exact_date = isset( $old_options['exact_date'] )
|
||||
? $old_options['exact_date']['value']
|
||||
: '';
|
||||
if ( '' !== $exact_date ) {
|
||||
$date_system = $this->_registry->get( 'date.system' );
|
||||
|
||||
// Change "Default calendar start date" format
|
||||
$new_exact_date = $date_system->convert_date_format(
|
||||
$exact_date,
|
||||
$old_date_format_value,
|
||||
$new_date_format_value
|
||||
);
|
||||
|
||||
// Save new value
|
||||
$settings = $this->_registry->get( 'model.settings' );
|
||||
$settings->set( 'exact_date', $new_exact_date );
|
||||
}
|
||||
}
|
||||
|
||||
// Frontend rendering checker
|
||||
$old_value = isset( $old_options['ai1ec_use_frontend_rendering'] )
|
||||
? (bool)$old_options['ai1ec_use_frontend_rendering']['value']
|
||||
: null;
|
||||
$new_value = isset( $new_options['ai1ec_use_frontend_rendering'] )
|
||||
? (bool)$new_options['ai1ec_use_frontend_rendering']['value']
|
||||
: null;
|
||||
if (
|
||||
$old_value === $new_value ||
|
||||
! $new_value
|
||||
) {
|
||||
return;
|
||||
}
|
||||
if ( $this->is_current_theme_outside_core() ) {
|
||||
$this->_registry->get( 'notification.admin' )->store(
|
||||
Ai1ec_I18n::__( 'You have turned on Frontend Rendering and you are using a custom calendar theme. If your theme does not support Frontend Rendering, your calendar may not work correctly.' ),
|
||||
'error',
|
||||
0,
|
||||
array( Ai1ec_Notification_Admin::RCPT_ADMIN ),
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether current calendar theme is located under core directory
|
||||
* or not.
|
||||
*
|
||||
* @return bool Result
|
||||
*
|
||||
* @throws Ai1ec_Bootstrap_Exception
|
||||
*/
|
||||
public function is_current_theme_outside_core() {
|
||||
$option = $this->_registry->get( 'model.option' );
|
||||
$cur_theme = $option->get( 'ai1ec_current_theme', array() );
|
||||
$theme_root = dirname( AI1EC_DEFAULT_THEME_ROOT );
|
||||
return (
|
||||
isset( $cur_theme['theme_root'] ) &&
|
||||
(
|
||||
$theme_root !== dirname( $cur_theme['theme_root'] ) &&
|
||||
false === strpos(
|
||||
$cur_theme['theme_root'],
|
||||
'all-in-one-event-calendar-saas-theme'
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Command line compatibility options.
|
||||
*
|
||||
* @author Time.ly Network, Inc.
|
||||
* @since 2.1
|
||||
* @package Ai1EC
|
||||
* @subpackage Ai1EC.Compatibility
|
||||
*/
|
||||
class Ai1ec_Compatibility_Cli {
|
||||
|
||||
/**
|
||||
* @var bool Whereas current session is command line.
|
||||
*/
|
||||
protected $_is_cli = false;
|
||||
|
||||
/**
|
||||
* Check current SAPI.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->_is_cli = 'cli' === php_sapi_name();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if running command line session.
|
||||
*
|
||||
* @return bool Yes/No
|
||||
*/
|
||||
public function is_cli() {
|
||||
return $this->_is_cli;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable DB debug when in command line session.
|
||||
*
|
||||
* @param bool $debug Current value.
|
||||
*
|
||||
* @return bool Optionally modified value.
|
||||
*/
|
||||
public function disable_db_debug( $debug ) {
|
||||
if ( $this->_is_cli ) {
|
||||
return false;
|
||||
}
|
||||
return $debug;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Memory related methods.
|
||||
*
|
||||
* @author Time.ly Network Inc.
|
||||
* @since 2.1
|
||||
*
|
||||
* @package AI1EC
|
||||
* @subpackage AI1EC.Lib
|
||||
*/
|
||||
class Ai1ec_Compatibility_Memory extends Ai1ec_Base {
|
||||
|
||||
/**
|
||||
* Checks if there is enough available free memory.
|
||||
*
|
||||
* @param string $required_limit String memory value i.e '24M'
|
||||
*
|
||||
* @return bool True or false.
|
||||
*/
|
||||
public function check_available_memory( $required_limit = 0 ) {
|
||||
if ( 0 === $required_limit ) {
|
||||
return true;
|
||||
}
|
||||
$required = $this->_string_to_bytes( $required_limit );
|
||||
$limit = $this->_string_to_bytes( ini_get( 'memory_limit' ) );
|
||||
$used = $this->get_usage();
|
||||
return ( $limit - $used ) >= $required;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns current memory usage if available - otherwise 0.
|
||||
*
|
||||
* @return int Memory usage.
|
||||
*/
|
||||
public function get_usage() {
|
||||
if ( is_callable( 'memory_get_usage' ) ) {
|
||||
return memory_get_usage();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts string value to int.
|
||||
*
|
||||
* @param string $v String value.
|
||||
*
|
||||
* @return int Number.
|
||||
*/
|
||||
protected function _string_to_bytes( $v ) {
|
||||
$letter = substr( $v, -1 );
|
||||
$value = (int)substr( $v, 0, -1 );
|
||||
$powers = array(
|
||||
'K' => 10,
|
||||
'M' => 20,
|
||||
'G' => 30,
|
||||
'T' => 40,
|
||||
'P' => 50,
|
||||
);
|
||||
$multiplier = 1;
|
||||
if ( isset( $powers[$letter] ) ) {
|
||||
$multiplier = pow( 2, $powers[$letter] );
|
||||
}
|
||||
if ( 1 === $multiplier ) {
|
||||
return (int)$v;
|
||||
}
|
||||
return $value * $multiplier;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Wrapper for all the output buffer calls (ob_*)
|
||||
*/
|
||||
class Ai1ec_Compatibility_OutputBuffer extends Ai1ec_Base {
|
||||
|
||||
/**
|
||||
* Wrap the ob_end_flush() method:
|
||||
* Flush (send) the output buffer and turn off output buffering
|
||||
*
|
||||
* @return bool Returns TRUE on success or FALSE on failure
|
||||
*/
|
||||
public function end_flush() {
|
||||
return ob_end_flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap the ob_get_contents() method:
|
||||
* Return the contents of the output buffer
|
||||
*
|
||||
* @retrun string This will return the contents of the output buffer or
|
||||
* FALSE, if output buffering isn't active.
|
||||
*/
|
||||
public function get_contents() {
|
||||
return ob_get_contents();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap the ob_get_level() method:
|
||||
* Returns the nesting level of the output buffering mechanism.
|
||||
*
|
||||
* @return int Returns the level of nested output buffering handlers or zero
|
||||
* if output buffering is not active.
|
||||
*/
|
||||
public function get_level() {
|
||||
return ob_get_level();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap the ob_start() method: turn output buffering on.
|
||||
*
|
||||
* @param callback $output_callback Method to be called on finish.
|
||||
* @param int $chunk_size Buffer size limite.
|
||||
* @param int|bool|null $flags Control performable operations.
|
||||
*
|
||||
* @return bool Returns TRUE on success or FALSE on failure.
|
||||
*/
|
||||
public function start(
|
||||
$output_callback = null,
|
||||
$chunk_size = 0,
|
||||
$flags = null
|
||||
) {
|
||||
if ( 'ob_gzhandler' === $output_callback && $this->is_zlib_active() ) {
|
||||
$output_callback = null; // do not compress again
|
||||
}
|
||||
if ( null === $flags ) {
|
||||
if ( defined( 'PHP_OUTPUT_HANDLER_STDFLAGS' ) ) {
|
||||
$flags = PHP_OUTPUT_HANDLER_STDFLAGS;
|
||||
} else {
|
||||
$flags = true;
|
||||
}
|
||||
}
|
||||
return ob_start( $output_callback, $chunk_size, $flags );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gzip the content if possible.
|
||||
*
|
||||
* @param string $string
|
||||
*/
|
||||
public function gzip_if_possible( $string ) {
|
||||
$gzip = $this->_registry->get( 'http.request' )->client_use_gzip();
|
||||
// only use output buffering for gzip.
|
||||
if ( $gzip ) {
|
||||
$this->start( 'ob_gzhandler' );
|
||||
header( 'Content-Encoding: gzip' );
|
||||
}
|
||||
echo $string;
|
||||
if ( $gzip ) {
|
||||
$this->end_flush();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if zlib compression is activated.
|
||||
*
|
||||
* @return bool Activation status.
|
||||
*/
|
||||
public function is_zlib_active() {
|
||||
$zlib = ini_get( 'zlib.output_compression' );
|
||||
if ( 'off' !== strtolower( $zlib ) && ! empty( $zlib ) ) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap ob_end_clean() and check the zip level to avoid crashing:
|
||||
* Clean (erase) the output buffer and turn off output buffering
|
||||
*
|
||||
* @return bool Returns TRUE on success or FALSE on failure
|
||||
*/
|
||||
public function end_clean() {
|
||||
return ob_end_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the closing of the object buffer when more then one object buffer
|
||||
* is opened. This cause an error if it's not correctly handled
|
||||
*
|
||||
* @return bool Returns TRUE on success or FALSE on failure
|
||||
*/
|
||||
public function end_clean_all() {
|
||||
if ( ini_get( 'zlib.output_compression' ) ) {
|
||||
return false;
|
||||
}
|
||||
$level = $this->get_level();
|
||||
$success = true;
|
||||
while ( $level ) {
|
||||
$this->end_clean();
|
||||
$new_level = $this->get_level();
|
||||
if ( $new_level === $level ) {
|
||||
$success = false;
|
||||
break;
|
||||
}
|
||||
$level = $new_level;
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap the ob_get_clean() method:
|
||||
* Gets the current buffer contents and delete current output buffer.
|
||||
*
|
||||
* @return string Returns the contents of the output buffer and end output
|
||||
* buffering. If output buffering isn't active then FALSE is returned.
|
||||
*/
|
||||
public function get_clean(){
|
||||
return ob_get_clean();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Execution guard.
|
||||
*
|
||||
* Guards process execution for multiple runs at the same moment of time.
|
||||
*
|
||||
* @author Time.ly Network, Inc.
|
||||
* @since 2.0
|
||||
* @package Ai1EC
|
||||
* @subpackage Ai1EC.Compatibility
|
||||
*/
|
||||
class Ai1ec_Compatibility_Xguard extends Ai1ec_Base {
|
||||
|
||||
/**
|
||||
* Return time of last acquisition.
|
||||
*
|
||||
* If execution guard with that name was never acquired it returns 0 (zero).
|
||||
* If acquisition fails it returns false.
|
||||
*
|
||||
* @param string $name Name of guard to be acquired.
|
||||
* @param int $timeout Timeout, how long lock is held after acquisition.
|
||||
*
|
||||
* @return bool Success to acquire lock for given period.
|
||||
*/
|
||||
public function acquire( $name, $timeout = 86400 ) {
|
||||
$name = $this->safe_name( $name );
|
||||
$dbi = $this->_registry->get( 'dbi.dbi' );
|
||||
$entry = array(
|
||||
'time' => time(),
|
||||
'pid' => getmypid(),
|
||||
);
|
||||
$table = $dbi->get_table_name( 'options' );
|
||||
$dbi->query( 'START TRANSACTION' );
|
||||
$query = $dbi->prepare(
|
||||
'SELECT option_value FROM ' . $table .
|
||||
' WHERE option_name = %s',
|
||||
$name
|
||||
);
|
||||
$prev = $dbi->get_var( $query );
|
||||
if ( ! empty( $prev ) ) {
|
||||
$prev = json_decode( $prev, true );
|
||||
}
|
||||
if (
|
||||
! empty( $prev ) &&
|
||||
( (int)$prev['time'] + (int)$timeout ) >= $entry['time']
|
||||
) {
|
||||
$dbi->query( 'ROLLBACK' );
|
||||
return false;
|
||||
}
|
||||
$query = '';
|
||||
if ( empty( $prev ) ) {
|
||||
$query = 'INSERT INTO';
|
||||
} else {
|
||||
$query = 'UPDATE';
|
||||
}
|
||||
$query .= ' `' . $table . '` SET `option_name` = %s, `option_value` = %s, `autoload` = 0';
|
||||
if ( ! empty( $prev ) ) {
|
||||
$query .= ' WHERE `option_name` = %s';
|
||||
}
|
||||
$query = $dbi->prepare( $query, $name, json_encode( $entry ), $name );
|
||||
$success = $dbi->query( $query );
|
||||
if ( ! $success ) {
|
||||
$dbi->query( 'ROLLBACK' );
|
||||
return false;
|
||||
}
|
||||
$dbi->query( 'COMMIT' );
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method release logs execution guard release phase.
|
||||
*
|
||||
* @param string $name Name of acquisition.
|
||||
*
|
||||
* @return bool Not expected to fail.
|
||||
*/
|
||||
public function release( $name ) {
|
||||
return false !== $this->_registry->get( 'dbi.dbi' )->delete(
|
||||
'options',
|
||||
array( 'option_name' => $this->safe_name( $name ) ),
|
||||
array( '%s' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare safe file names.
|
||||
*
|
||||
* @param string $name Name of acquisition
|
||||
*
|
||||
* @return string Actual safeguard name to use.
|
||||
*/
|
||||
protected function safe_name( $name ) {
|
||||
$name = preg_replace( '/[^A-Za-z_0-9\-]/', '_', $name );
|
||||
$name = trim( preg_replace( '/_+/', '_', $name ), '_' );
|
||||
$name = 'ai1ec_xlock_' . $name;
|
||||
return substr( $name, 0, 50 );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user