Origin commit

This commit is contained in:
Hoang Huu
2019-09-10 11:27:33 +07:00
commit 499e068e4f
844 changed files with 188705 additions and 0 deletions

149
inc/libraries/array2xml.php Executable file
View File

@@ -0,0 +1,149 @@
<?php
/**
* $Desc$
*
* @version $Id$
* @package opalestate
* @author Opal Team <info@wpopal.com >
* @copyright Copyright (C) 2019 wpopal.com. All Rights Reserved.
* @license GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
*
* @website http://www.wpopal.com
* @support http://www.wpopal.com/support/forum.html
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Array2XML {
private static $xml = null;
private static $encoding = 'UTF-8';
/**
* Initialize the root XML node [optional]
* @param $version
* @param $encoding
* @param $format_output
*/
public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) {
self::$xml = new DomDocument($version, $encoding);
self::$xml->formatOutput = $format_output;
self::$encoding = $encoding;
}
/**
* Convert an Array to XML
* @param string $node_name - name of the root node to be converted
* @param array $arr - aray to be converterd
* @return DomDocument
*/
public static function &createXML($node_name, $arr=array()) {
$xml = self::getXMLRoot();
$xml->appendChild(self::convert($node_name, $arr));
self::$xml = null; // clear the xml node in the class for 2nd time use.
return $xml;
}
/**
* Convert an Array to XML
* @param string $node_name - name of the root node to be converted
* @param array $arr - aray to be converterd
* @return DOMNode
*/
private static function &convert($node_name, $arr=array()) {
//print_arr($node_name);
$xml = self::getXMLRoot();
$node = $xml->createElement($node_name);
if(is_array($arr)){
// get the attributes first.;
if(isset($arr['@attributes'])) {
foreach($arr['@attributes'] as $key => $value) {
if(!self::isValidTagName($key)) {
throw new Exception('[Array2XML] Illegal character in attribute name. attribute: '.$key.' in node: '.$node_name);
}
$node->setAttribute($key, self::bool2str($value));
}
unset($arr['@attributes']); //remove the key from the array once done.
}
// check if it has a value stored in @value, if yes store the value and return
// else check if its directly stored as string
if(isset($arr['@value'])) {
$node->appendChild($xml->createTextNode(self::bool2str($arr['@value'])));
unset($arr['@value']); //remove the key from the array once done.
//return from recursion, as a note with value cannot have child nodes.
return $node;
} else if(isset($arr['@cdata'])) {
$node->appendChild($xml->createCDATASection(self::bool2str($arr['@cdata'])));
unset($arr['@cdata']); //remove the key from the array once done.
//return from recursion, as a note with cdata cannot have child nodes.
return $node;
}
}
//create subnodes using recursion
if(is_array($arr)){
// recurse to get the node for that key
foreach($arr as $key=>$value){
if(!self::isValidTagName($key)) {
throw new Exception('[Array2XML] Illegal character in tag name. tag: '.$key.' in node: '.$node_name);
}
if(is_array($value) && is_numeric(key($value))) {
// MORE THAN ONE NODE OF ITS KIND;
// if the new array is numeric index, means it is array of nodes of the same kind
// it should follow the parent key name
foreach($value as $k=>$v){
$node->appendChild(self::convert($key, $v));
}
} else {
// ONLY ONE NODE OF ITS KIND
$node->appendChild(self::convert($key, $value));
}
unset($arr[$key]); //remove the key from the array once done.
}
}
// after we are done with all the keys in the array (if it is one)
// we check if it has any text value, if yes, append it.
if(!is_array($arr)) {
$node->appendChild($xml->createTextNode(self::bool2str($arr)));
}
return $node;
}
/*
* Get the root XML node, if there isn't one, create it.
*/
private static function getXMLRoot(){
if(empty(self::$xml)) {
self::init();
}
return self::$xml;
}
/*
* Get string representation of boolean value
*/
private static function bool2str($v){
//convert boolean to text value.
$v = $v === true ? 'true' : $v;
$v = $v === false ? 'false' : $v;
return $v;
}
/*
* Check if the tag name or attribute name contains illegal characters
* Ref: http://www.w3.org/TR/xml/#sec-common-syn
*/
private static function isValidTagName($tag){
$pattern = '/^[a-z_]+[a-z0-9\:\-\.\_]*[^:]*$/i';
return preg_match($pattern, $tag, $matches) && $matches[0] == $tag;
}
}
?>

178
inc/libraries/wp-session.php Executable file
View File

@@ -0,0 +1,178 @@
<?php
/**
* WordPress session managment.
*
* Standardizes WordPress session data and uses either database transients or in-memory caching
* for storing user session information.
*
* @package WordPress
* @subpackage Session
* @since 3.7.0
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Return the current cache expire setting.
*
* @return int
*/
function wp_session_cache_expire() {
$wp_session = WP_Session::get_instance();
return $wp_session->cache_expiration();
}
/**
* Alias of wp_session_write_close()
*/
function wp_session_commit() {
wp_session_write_close();
}
/**
* Load a JSON-encoded string into the current session.
*
* @param string $data
*/
function wp_session_decode( $data ) {
$wp_session = WP_Session::get_instance();
return $wp_session->json_in( $data );
}
/**
* Encode the current session's data as a JSON string.
*
* @return string
*/
function wp_session_encode() {
$wp_session = WP_Session::get_instance();
return $wp_session->json_out();
}
/**
* Regenerate the session ID.
*
* @param bool $delete_old_session
*
* @return bool
*/
function wp_session_regenerate_id( $delete_old_session = false ) {
$wp_session = WP_Session::get_instance();
$wp_session->regenerate_id( $delete_old_session );
return true;
}
/**
* Start new or resume existing session.
*
* Resumes an existing session based on a value sent by the _wp_session cookie.
*
* @return bool
*/
function wp_session_start() {
$wp_session = WP_Session::get_instance();
do_action( 'wp_session_start' );
return $wp_session->session_started();
}
add_action( 'plugins_loaded', 'wp_session_start' );
/**
* Return the current session status.
*
* @return int
*/
function wp_session_status() {
$wp_session = WP_Session::get_instance();
if ( $wp_session->session_started() ) {
return PHP_SESSION_ACTIVE;
}
return PHP_SESSION_NONE;
}
/**
* Unset all session variables.
*/
function wp_session_unset() {
$wp_session = WP_Session::get_instance();
$wp_session->reset();
}
/**
* Write session data and end session
*/
function wp_session_write_close() {
$wp_session = WP_Session::get_instance();
$wp_session->write_data();
do_action( 'wp_session_commit' );
}
add_action( 'shutdown', 'wp_session_write_close' );
/**
* Clean up expired sessions by removing data and their expiration entries from
* the WordPress options table.
*
* This method should never be called directly and should instead be triggered as part
* of a scheduled task or cron job.
*/
function wp_session_cleanup() {
global $wpdb;
if ( defined( 'WP_SETUP_CONFIG' ) ) {
return;
}
if ( ! defined( 'WP_INSTALLING' ) ) {
$expiration_keys = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '_wp_session_expires_%'" );
$now = current_time( 'timestamp' );
$expired_sessions = array();
foreach( $expiration_keys as $expiration ) {
// If the session has expired
if ( $now > intval( $expiration->option_value ) ) {
// Get the session ID by parsing the option_name
$session_id = substr( $expiration->option_name, 20 );
if( (int) -1 === (int) $session_id || ! preg_match( '/^[a-f0-9]{32}$/', $session_id ) ) {
continue;
}
$expired_sessions[] = $expiration->option_name;
$expired_sessions[] = esc_sql( "_wp_session_$session_id" );
}
}
// Delete all expired sessions in a single query
if ( ! empty( $expired_sessions ) ) {
$option_names = implode( "','", $expired_sessions );
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name IN ('$option_names')" );
}
}
// Allow other plugins to hook in to the garbage collection process.
do_action( 'wp_session_cleanup' );
}
add_action( 'wp_session_garbage_collection', 'wp_session_cleanup' );
/**
* Register the garbage collector as a twice daily event.
*/
function wp_session_register_garbage_collection() {
if ( ! wp_next_scheduled( 'wp_session_garbage_collection' ) ) {
wp_schedule_event( current_time( 'timestamp' ), 'twicedaily', 'wp_session_garbage_collection' );
}
}
add_action( 'wp', 'wp_session_register_garbage_collection' );

View File

@@ -0,0 +1,341 @@
<?php
/**
* WordPress session managment.
*
* Standardizes WordPress session data using database-backed options for storage.
* for storing user session information.
*
* @package WordPress
* @subpackage Session
* @since 3.6.0
*/
/**
* WordPress Session class for managing user session data.
*
* @package WordPress
* @since 3.6.0
*/
class WP_Session implements ArrayAccess, Iterator, Countable {
/**
* Internal data collection.
*
* @var array
*/
private $container;
/**
* ID of the current session.
*
* @var string
*/
private $session_id;
/**
* Unix timestamp when session expires.
*
* @var int
*/
private $expires;
/**
* Singleton instance.
*
* @var bool|WP_Session
*/
private static $instance = false;
/**
* Retrieve the current session instance.
*
* @param bool $session_id Session ID from which to populate data.
*
* @return bool|WP_Session
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Default constructor.
* Will rebuild the session collection from the given session ID if it exists. Otherwise, will
* create a new session with that ID.
*
* @param $session_id
* @uses apply_filters Calls `wp_session_expiration` to determine how long until sessions expire.
*/
private function __construct() {
if ( isset( $_COOKIE[WP_SESSION_COOKIE] ) ) {
$this->session_id = stripslashes( $_COOKIE[WP_SESSION_COOKIE] );
} else {
$this->session_id = $this->generate_id();
}
$this->expires = time() + intval( apply_filters( 'wp_session_expiration', 24 * 60 ) );
$this->read_data();
setcookie( WP_SESSION_COOKIE, $this->session_id, $this->expires, COOKIEPATH, COOKIE_DOMAIN );
}
/**
* Generate a cryptographically strong unique ID for the session token.
*
* @return string
*/
private function generate_id() {
require_once ABSPATH . 'wp-includes/class-phpass.php';
$hasher = new PasswordHash( 8, false );
return md5( $hasher->get_random_bytes( 32 ) );
}
/**
* Read data from a transient for the current session.
*
* Automatically resets the expiration time for the session transient to some time in the future.
*
* @return array
*/
private function read_data() {
$this->touch_session();
$this->container = get_option( "_wp_session_{$this->session_id}", array() );
return $this->container;
}
/**
* Write the data from the current session to the data storage system.
*/
public function write_data() {
$session_list = get_option( '_wp_session_list', array() );
$this->touch_session();
update_option( "_wp_session_{$this->session_id}", $this->container );
}
private function touch_session() {
$session_list = get_option( '_wp_session_list', array() );
$session_list[ $this->session_id ] = $this->expires;
foreach( $session_list as $id => $expires ) {
if ( time() > $this->expires ) {
delete_option( "_wp_session_{$id}" );
unset( $session_list[$id] );
}
}
update_option( '_wp_session_list', $session_list );
}
/**
* Output the current container contents as a JSON-encoded string.
*
* @return string
*/
public function json_out() {
return json_encode( $this->container );
}
/**
* Decodes a JSON string and, if the object is an array, overwrites the session container with its contents.
*
* @param string $data
*
* @return bool
*/
public function json_in( $data ) {
$array = json_decode( $data );
if ( is_array( $array ) ) {
$this->container = $array;
return true;
}
return false;
}
/**
* Regenerate the current session's ID.
*
* @param bool $delete_old Flag whether or not to delete the old session data from the server.
*/
public function regenerate_id( $delete_old = false ) {
if ( $delete_old ) {
delete_option( "_wp_session_{$this->session_id}" );
$session_list = get_option( '_wp_session_list', array() );
unset ($session_list[ $this->session_id ] );
update_option( '_wp_session_list', $session_list );
}
$this->session_id = $this->generate_id();
setcookie( WP_SESSION_COOKIE, $this->session_id, time() + $this->expires, COOKIEPATH, COOKIE_DOMAIN );
}
/**
* Check if a session has been initialized.
*
* @return bool
*/
public function session_started() {
return !!self::$instance;
}
/**
* Return the read-only cache expiration value.
*
* @return int
*/
public function cache_expiration() {
return $this->expires;
}
/**
* Flushes all session variables.
*/
public function reset() {
$this->container = array();
}
/*****************************************************************/
/* ArrayAccess Implementation */
/*****************************************************************/
/**
* Whether a offset exists
*
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
*
* @param mixed $offset An offset to check for.
*
* @return boolean true on success or false on failure.
*/
public function offsetExists( $offset ) {
return isset( $this->container[ $offset ]) ;
}
/**
* Offset to retrieve
*
* @link http://php.net/manual/en/arrayaccess.offsetget.php
*
* @param mixed $offset The offset to retrieve.
*
* @return mixed Can return all value types.
*/
public function offsetGet( $offset ) {
return isset( $this->container[ $offset ] ) ? $this->container[ $offset ] : null;
}
/**
* Offset to set
*
* @link http://php.net/manual/en/arrayaccess.offsetset.php
*
* @param mixed $offset The offset to assign the value to.
* @param mixed $value The value to set.
*
* @return void
*/
public function offsetSet( $offset, $value ) {
if ( is_null( $offset ) ) {
$this->container[] = $value;
} else {
$this->container[ $offset ] = $value;
}
}
/**
* Offset to unset
*
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
*
* @param mixed $offset The offset to unset.
*
* @return void
*/
public function offsetUnset( $offset ) {
unset( $this->container[ $offset ] );
}
/*****************************************************************/
/* Iterator Implementation */
/*****************************************************************/
/**
* Current position of the array.
*
* @link http://php.net/manual/en/iterator.current.php
*
* @return mixed
*/
public function current() {
return current( $this->container );
}
/**
* Key of the current element.
*
* @link http://php.net/manual/en/iterator.key.php
*
* @return mixed
*/
public function key() {
return key( $this->container );
}
/**
* Move the internal point of the container array to the next item
*
* @link http://php.net/manual/en/iterator.next.php
*
* @return void
*/
public function next() {
next( $this->container );
}
/**
* Rewind the internal point of the container array.
*
* @link http://php.net/manual/en/iterator.rewind.php
*
* @return void
*/
public function rewind() {
reset( $this->container );
}
/**
* Is the current key valid?
*
* @link http://php.net/manual/en/iterator.rewind.php
*
* @return bool
*/
public function valid() {
return $this->offsetExists( $this->key() );
}
/*****************************************************************/
/* Countable Implementation */
/*****************************************************************/
/**
* Get the count of elements in the container array.
*
* @link http://php.net/manual/en/countable.count.php
*
* @return int
*/
public function count() {
return count( $this->container );
}
}

View File

@@ -0,0 +1,118 @@
<?php
/**
* WordPress session managment.
*
* Standardizes WordPress session data and uses either database transients or in-memory caching
* for storing user session information.
*
* @package WordPress
* @subpackage Session
* @since 3.6.0
*/
/**
* Return the current cache expire setting.
*
* @return int
*/
function wp_session_cache_expire() {
$wp_session = WP_Session::get_instance();
return $wp_session->cache_expiration();
}
/**
* Alias of wp_session_write_close()
*/
function wp_session_commit() {
wp_session_write_close();
}
/**
* Load a JSON-encoded string into the current session.
*
* @param string $data
*/
function wp_session_decode( $data ) {
$wp_session = WP_Session::get_instance();
return $wp_session->json_in( $data );
}
/**
* Encode the current session's data as a JSON string.
*
* @return string
*/
function wp_session_encode() {
$wp_session = WP_Session::get_instance();
return $wp_session->json_out();
}
/**
* Regenerate the session ID.
*
* @param bool $delete_old_session
*
* @return bool
*/
function wp_session_regenerate_id( $delete_old_session = false ) {
$wp_session = WP_Session::get_instance();
$wp_session->regenerate_id( $delete_old_session );
return true;
}
/**
* Start new or resume existing session.
*
* Resumes an existing session based on a value sent by the _wp_session cookie.
*
* @return bool
*/
function wp_session_start() {
$wp_session = WP_Session::get_instance();
$wp_session = WP_Session::get_instance();
do_action( 'wp_session_start' );
return $wp_session->session_started();
}
add_action( 'plugins_loaded', 'wp_session_start' );
/**
* Return the current session status.
*
* @return int
*/
function wp_session_status() {
$wp_session = WP_Session::get_instance();
if ( $wp_session->session_started() ) {
return PHP_SESSION_ACTIVE;
}
return PHP_SESSION_NONE;
}
/**
* Unset all session variables.
*/
function wp_session_unset() {
$wp_session = WP_Session::get_instance();
$wp_session->reset();
}
/**
* Write session data and end session
*/
function wp_session_write_close() {
$wp_session = WP_Session::get_instance();
$wp_session->write_data();
do_action( 'wp_session_commit' );
}
add_action( 'shutdown', 'wp_session_write_close' );