Your IP : 216.73.216.162


Current Path : /home/x/b/o/xbodynamge/namtation/wp-content/
Upload File :
Current File : /home/x/b/o/xbodynamge/namtation/wp-content/libraries.tar

simplexlsx.class.php000066600000057555151121565550010624 0ustar00<?php
/**
 * Excel 2007-2013 Reader Class
 *
 * Based on SimpleXLSX v0.7.13 by Sergey Schuchkin.
 * @link https://github.com/shuchkin/simplexlsx/
 *
 * @package TablePress
 * @subpackage Import
 * @author Sergey Schuchkin, Tobias Bäthge
 * @since 1.1.0
 */

// Prohibit direct script loading.
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );

/**
 * PHP Excel 2007-2013 Reader Class
 * @package TablePress
 * @subpackage Import
 * @author Sergey Schuchkin, Tobias Bäthge
 * @since 1.1.0
 */
class SimpleXLSX {

	/**
	 * XML Schema URLs.
	 */
	const SCHEMA_REL_OFFICEDOCUMENT = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument';
	const SCHEMA_REL_SHAREDSTRINGS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings';
	const SCHEMA_REL_WORKSHEET = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet';
	const SCHEMA_REL_STYLES = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles';

	/**
	 * [$workbook description]
	 *
	 * @since 1.1.0
	 * @var [type]
	 */
	protected $workbook;

	/**
	 * [$sheets description]
	 *
	 * @since 1.1.0
	 * @var array
	 */
	protected $sheets = array();

	/**
	 * [$sheetNames description]
	 *
	 * @since 1.9.1
	 * @var array
	 */
	protected $sheetNames = array();

	/**
	 * [$styles description]
	 *
	 * @since 1.1.0
	 * @var array
	 */
	protected $styles = array();

	/**
	 * [$hyperlinks description]
	 *
	 * @since 1.1.0
	 * @var [type]
	 */
	protected $hyperlinks;

	/**
	 * [$package description]
	 *
	 * @since 1.1.0
	 * @var array
	 */
	protected $package = array(
		'filename' => '',
		'mtime'    => 0,
		'size'     => 0,
		'comment'  => '',
		'entries'  => array(),
	);

	/**
	 * [$sharedstrings description]
	 *
	 * @since 1.1.0
	 * @var array
	 */
	protected $sharedstrings = array();

	/**
	 * [$error description]
	 *
	 * @since 1.1.0
	 * @var string
	 */
	protected $error = '';

	/**
	 * [$workbook_cell_formats description]
	 *
	 * @since 1.1.0
	 * @var array
	 */
	public $workbook_cell_formats = array();

	/**
	 * [$built_in_cell_formats description]
	 *
	 * @since 1.1.0
	 * @var array
	 */
	public static $built_in_cell_formats = array(
		0  => 'General',
		1  => '0',
		2  => '0.00',
		3  => '#,##0',
		4  => '#,##0.00',
		9  => '0%',
		10 => '0.00%',
		11 => '0.00E+00',
		12 => '# ?/?',
		13 => '# ??/??',
		14 => 'mm-dd-yy',
		15 => 'd-mmm-yy',
		16 => 'd-mmm',
		17 => 'mmm-yy',
		18 => 'h:mm AM/PM',
		19 => 'h:mm:ss AM/PM',
		20 => 'h:mm',
		21 => 'h:mm:ss',
		22 => 'm/d/yy h:mm',
		37 => '#,##0 ;(#,##0)',
		38 => '#,##0 ;[Red](#,##0)',
		39 => '#,##0.00;(#,##0.00)',
		40 => '#,##0.00;[Red](#,##0.00)',
		44 => '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)',
		45 => 'mm:ss',
		46 => '[h]:mm:ss',
		47 => 'mmss.0',
		48 => '##0.0E+0',
		49 => '@',
		27 => '[$-404]e/m/d',
		30 => 'm/d/yy',
		36 => '[$-404]e/m/d',
		50 => '[$-404]e/m/d',
		57 => '[$-404]e/m/d',
		59 => 't0',
		60 => 't0.00',
		61 => 't#,##0',
		62 => 't#,##0.00',
		67 => 't0%',
		68 => 't0.00%',
		69 => 't# ?/?',
		70 => 't# ??/??',
	);

	/**
	 * [$datetime_format description]
	 *
	 * @since 1.8.1
	 * @var string
	 */
	public $datetime_format = 'Y-m-d H:i:s';

	/**
	 * Constructor.
	 *
	 * @since 1.1.0
	 *
	 * @param string $filename [description]
	 * @param bool   $is_data  Optional. [description]
	 */
	public function __construct( $filename, $is_data = false ) {
		$this->datetime_format = get_option( 'date_format' );
		if ( $this->_unzip( $filename, $is_data ) ) {
			$this->_parse();
		}
	}

	/**
	 * [sheets description]
	 *
	 * @since 1.1.0
	 *
	 * @return [type] [description]
	 */
	public function sheets() {
		return $this->sheets;
	}

	/**
	 * [sheetsCount description]
	 *
	 * @since 1.1.0
	 *
	 * @return int Number of sheets.
	 */
	public function sheetsCount() {
		return count( $this->sheets );
	}

	/**
	 * [sheetName description]
	 *
	 * @since 1.1.0
	 *
	 * @param [type] $worksheet_index [description]
	 * @return string|bool [description]
	 */
	public function sheetName( $worksheet_index ) {
		if ( isset( $this->sheetNames[ $worksheet_index ] ) ) {
			return $this->sheetNames[ $worksheet_index ];
		}
		return false;
	}

	/**
	 * [sheetNames description]
	 *
	 * @since 1.1.0
	 *
	 * @return array [description]
	 */
	public function sheetNames() {
		return $this->sheetNames;
	}

	/**
	 * [worksheet description]
	 *
	 * @since 1.1.0
	 *
	 * @param [type] $worksheet_index [description]. Optional.
	 * @return [type] [description]
	 */
	public function worksheet( $worksheet_index = 0 ) {
		if ( isset( $this->sheets[ $worksheet_index ] ) ) {
			$ws = $this->sheets[ $worksheet_index ];
			if ( isset( $ws->hyperlinks ) ) {
				$this->hyperlinks = array();
				foreach ( $ws->hyperlinks->hyperlink as $hyperlink ) {
					$this->hyperlinks[ (string) $hyperlink['ref'] ] = (string) $hyperlink['display'];
				}
			}
			return $ws;
		}
		$this->error( 'Worksheet ' . $worksheet_index . ' not found.' );
		return false;
	}

	/**
	 * [dimension description]
	 *
	 * "Don't trust ->dimension(), so xlsx generators very lazy and don't publish a dimension attribute."
	 *
	 * @since 1.1.0
	 *
	 * @param int $worksheet_index Optional. [description]
	 * @return array|false [description]
	 */
	public function dimension( $worksheet_index = 0 ) {
		if ( false === ( $ws = $this->worksheet( $worksheet_index ) ) ) {
			return false;
		}

		$ref = (string) $ws->dimension['ref'];

		if ( false !== strpos( $ref, ':' ) ) {
			$d = explode( ':', $ref );
			$index = $this->_columnIndex( $d[1] );
			return array( $index[0] + 1, $index[1] + 1 );
		}

		if ( '' !== $ref ) {
			$index = $this->_columnIndex( $ref );
			return array( $index[0] + 1, $index[1] + 1 );
		}

		return array( 0, 0 );
	}

	/**
	 * [rows description]
	 *
	 * Sheets numeration: 1, 2, 3, ...
	 *
	 * @since 1.1.0
	 *
	 * @param int $worksheet_index Optional. [description]
	 * @return array|bool [description]
	 */
	public function rows( $worksheet_index = 0 ) {
		if ( false === ( $ws = $this->worksheet( $worksheet_index ) ) ) {
			return false;
		}

		$rows = array();
		$current_row = 0;

		list( $cols, ) = $this->dimension( $worksheet_index );

		foreach ( $ws->sheetData->row as $row ) {
			$rows[ $current_row ] = array();
			foreach ( $row->c as $c ) {
				list( $current_cell, ) = $this->_columnIndex( (string) $c['r'] );
				$rows[ $current_row ][ $current_cell ] = $this->value( $c );
			}
			for ( $i = 0; $i < $cols; $i++ ) {
				if ( ! isset( $rows[ $current_row ][ $i ] ) ) {
					$rows[ $current_row ][ $i ] = '';
				}
			}
			ksort( $rows[ $current_row ] );
			$current_row++;
		}
		return $rows;
	}

	/**
	 * [rowsEx description]
	 *
	 * @since 1.1.0
	 *
	 * @param int $worksheet_index Optional. [description]
	 * @return array|bool [description]
	 */
	public function rowsEx( $worksheet_index = 0 ) {
		if ( false === ( $ws = $this->worksheet( $worksheet_index ) ) ) {
			return false;
		}

		$rows = array();
		$current_row = 0;
		list( $cols, ) = $this->dimension( $worksheet_index );

		foreach ( $ws->sheetData->row as $row ) {
			$r_idx = (int) $row['r'];
			foreach ( $row->c as $c ) {
				$r = (string) $c['r'];
				$t = (string) $c['t'];
				$s = (int) $c['s'];
				list( $current_cell, ) = $this->_columnIndex( $r );
				if ( $s > 0 && isset( $this->workbook_cell_formats[ $s ] ) ) {
					$format = $this->workbook_cell_formats[ $s ]['format'];
					if ( false !== strpos( $format, 'm' ) ) {
						$t = 'd';
					}
				} else {
					$format = '';
				}

				$rows[ $current_row ][ $current_cell ] = array(
					'type'   => $t,
					'name'   => $r,
					'value'  => $this->value( $c, $format ),
					'href'   => $this->href( $c ),
					'f'      => (string) $c->f,
					'format' => $format,
					'r'      => $r_idx,
				);
			}
			for ( $i = 0; $i < $cols; $i++ ) {
				if ( ! isset( $rows[ $current_row ][ $i ] ) ) {
					for ( $c = '', $j = $i; $j >= 0; $j = (int) ( $j / 26 ) - 1 ) {
						$c = chr( $j % 26 + 65 ) . $c;
					}

					$rows[ $current_row ][ $i ] = array(
						'type'   => '',
						// 'name' => chr( $i + 65 ) . ( $current_row + 1 ),
						'name'   => $c . ( $current_row + 1 ),
						'value'  => '',
						'href'   => '',
						'f'      => '',
						'format' => '',
						'r'      => $r_idx,
					);
				}
			}
			ksort( $rows[ $current_row ] );
			$current_row++;
		}
		return $rows;
	}

	/**
	 * [_columnIndex description]
	 *
	 * @since 1.1.0
	 *
	 * @param string $cell Optional. [description]
	 * @return array [description]
	 */
	protected function _columnIndex( $cell = 'A1' ) {
		if ( preg_match( '/([A-Z]+)(\d+)/', $cell, $m ) ) {
			list( , $col, $row ) = $m;

			$colLen = strlen( $col );
			$index = 0;

			for ( $i = $colLen - 1; $i >= 0; $i-- ) {
				$index += ( ord( $col[ $i ] ) - 64 ) * pow( 26, $colLen - $i - 1 );
			}

			return array( $index - 1, $row - 1 );
		}
		$this->error( 'Invalid cell index ' . $cell );

		return false;
	}

	/**
	 * [value description]
	 *
	 * @since 1.1.0
	 *
	 * @param [type] $cell   [description]
	 * @param [type] $format [description]
	 * @return mixed [description]
	 */
	public function value( $cell, $format = null ) {
		// Determine data type.
		$dataType = (string) $cell['t'];

		if ( null === $format ) {
			$s = (int) $cell['s'];
			if ( $s > 0 && isset( $this->workbook_cell_formats[ $s ] ) ) {
				$format = $this->workbook_cell_formats[ $s ]['format'];
			}
		}
		if ( false !== strpos( $format, 'm' ) ) {
			$dataType = 'd';
		}
		$value = '';
		switch ( $dataType ) {
			case 's':
				// Value is a shared string.
				if ( '' !== (string) $cell->v ) {
					$value = $this->sharedstrings[ (int) $cell->v ];
				}
				break;
			case 'b':
				// Value is boolean.
				$value = (string) $cell->v;
				if ( '0' === $value ) {
					$value = false;
				} elseif ( '1' === $value ) {
					$value = true;
				} else {
					$value = (bool) $cell->v;
				}
				break;
			case 'inlineStr':
				// Value is rich text inline.
				$value = $this->_parseRichText( $cell->is );
				break;
			case 'e':
				// Value is an error message.
				if ( '' !== (string) $cell->v ) {
					$value = (string) $cell->v;
				}
				break;
			case 'd':
				// Value is a date.
				$value = $this->datetime_format ? gmdate( $this->datetime_format, $this->unixstamp( (float) $cell->v ) ) : (float) $cell->v;
				break;
			default:
				// Value is a string.
				$value = (string) $cell->v;
				// Check for numeric values by converting them forth and back.
				if ( is_numeric( $value ) && 's' !== $dataType ) {
					if ( $value == (int) $value ) {
						$value = (int) $value;
					} elseif ( $value == (float) $value ) {
						$value = (float) $value;
					}
				}
		}
		return $value;
	}

	/**
	 * [href description]
	 *
	 * @since 1.1.0
	 *
	 * @param [type] $cell [description]
	 * @return string [description]
	 */
	public function href( $cell ) {
		return isset( $this->hyperlinks[ (string) $cell['r'] ] ) ? $this->hyperlinks[ (string) $cell['r'] ] : '';
	}

	/**
	 * [getStyles description]
	 *
	 * @since 1.8.1
	 *
	 * @return [type] [description]
	 */
	public function getStyles() {
		return $this->styles;
	}

	/**
	 * [_unzip description]
	 *
	 * @since 1.1.0
	 *
	 * @param [type] $filename [description]
	 * @param bool   $is_data  Optional. [description]
	 * @return [type] [description]
	 */
	protected function _unzip( $filename, $is_data = false ) {
		if ( $is_data ) {
			$this->package['filename'] = 'default.xlsx';
			$this->package['mtime'] = time();
			$this->package['size'] = strlen( $filename );
			$vZ = $filename;
		} else {
			if ( ! is_readable( $filename ) ) {
				$this->error( 'File not found ' . $filename );
				return false;
			}

			// Package information.
			$this->package['filename'] = $filename;
			$this->package['mtime'] = filemtime( $filename );
			$this->package['size'] = filesize( $filename );

			// Read file.
			$vZ = file_get_contents( $filename );
		}

		/*
		// Cut end of central directory
		$aE = explode( "\x50\x4b\x05\x06", $vZ );
		if ( 1 === count( $aE ) ) {
			$this->error( 'Unknown format' );
			return false;
		}
		*/

		if ( false === ( $pcd = strrpos( $vZ, "\x50\x4b\x05\x06" ) ) ) {
			$this->error( 'Unknown archive format' );
			return false;
		}
		$aE = array(
			0 => substr( $vZ, 0, $pcd ),
			1 => substr( $vZ, $pcd + 3 ),
		);

		// Normal way.
		$aP = unpack( 'x16/v1CL', $aE[1] );
		$this->package['comment'] = substr( $aE[1], 18, $aP['CL'] );

		// Translates end of line from other operating systems.
		$this->package['comment'] = str_replace( array( "\r\n", "\r" ), "\n", $this->package['comment'] );

		// Cut the entries from the central directory.
		$aE = explode( "\x50\x4b\x01\x02", $vZ );
		// Explode to each part.
		$aE = explode( "\x50\x4b\x03\x04", $aE[0] );
		// Shift out spanning signature or empty entry.
		array_shift( $aE );

		// Loop through the entries.
		foreach ( $aE as $vZ ) {
			$aI = array();
			$aI['E'] = 0;
			$aI['EM'] = '';
			// Retrieving local file header information.
			// $aP = unpack( 'v1VN/v1GPF/v1CM/v1FT/v1FD/V1CRC/V1CS/V1UCS/v1FNL', $vZ );
			$aP = unpack( 'v1VN/v1GPF/v1CM/v1FT/v1FD/V1CRC/V1CS/V1UCS/v1FNL/v1EFL', $vZ );

			// Check if data is encrypted.
			// $bE = ( $aP['GPF'] && 0x0001 ) ? true : false;
			$bE = false;
			$nF = $aP['FNL'];
			$mF = $aP['EFL'];

			// Special case: value block after the compressed data.
			if ( $aP['GPF'] & 0x0008 ) {
				$aP1 = unpack( 'V1CRC/V1CS/V1UCS', substr( $vZ, -12 ) );
				$aP['CRC'] = $aP1['CRC'];
				$aP['CS'] = $aP1['CS'];
				$aP['UCS'] = $aP1['UCS'];
				// 2013-08-10
				$vZ = substr( $vZ, 0, -12 );
				if ( "\x50\x4b\x07\x08" === substr( $vZ, -4 ) ) {
					$vZ = substr( $vZ, 0, -4 );
				}
			}

			// Get stored filename.
			$aI['N'] = substr( $vZ, 26, $nF );

			// If it's a directory entry, it will be skipped.
			if ( '/' === substr( $aI['N'], -1 ) ) {
				continue;
			}

			// Truncate full filename in path and filename.
			$aI['P'] = dirname( $aI['N'] );
			$aI['P'] = ( '.' === $aI['P'] ) ? '' : $aI['P'];
			$aI['N'] = basename( $aI['N'] );

			$vZ = substr( $vZ, 26 + $nF + $mF );

			if ( strlen( $vZ ) !== (int) $aP['CS'] ) { // Check only if available.
				$aI['E'] = 1;
				$aI['EM'] = 'Compressed size is not equal with the value in header information.';
			} elseif ( $bE ) {
				$aI['E']  = 5;
				$aI['EM'] = 'File is encrypted, which is not supported by this class.';
			} else {
				switch ( $aP['CM'] ) {
					case 0: // Stored
						// Here is nothing to do, the file is flat.
						break;
					case 8: // Deflated
						$vZ = gzinflate( $vZ );
						break;
					case 12: // BZIP2
						if ( extension_loaded( 'bz2' ) ) {
							$vZ = bzdecompress( $vZ );
						} else {
							$aI['E']  = 7;
							$aI['EM'] = 'PHP BZIP2 extension not available.';
						}
						break;
					default:
						$aI['E']  = 6;
						$aI['EM'] = "De-/Compression method {$aP['CM']} is not supported.";
				}
				if ( ! $aI['E'] ) {
					if ( false === $vZ ) {
						$aI['E']  = 2;
						$aI['EM'] = 'Decompression of data failed.';
					} elseif ( strlen( $vZ ) !== (int) $aP['UCS'] ) {
						$aI['E']  = 3;
						$aI['EM'] = 'Uncompressed size is not equal with the value in header information.';
					} elseif ( crc32( $vZ ) !== $aP['CRC'] ) {
						$aI['E']  = 4;
						$aI['EM'] = 'CRC32 checksum is not equal with the value in header information.';
					}
				}
			}

			$aI['D'] = $vZ;

			// DOS to UNIX timestamp.
			$aI['T'] = mktime(
				( $aP['FT'] & 0xf800 ) >> 11,
				( $aP['FT'] & 0x07e0 ) >> 5,
				( $aP['FT'] & 0x001f ) << 1,
				( $aP['FD'] & 0x01e0 ) >> 5,
				( $aP['FD'] & 0x001f ),
				( ( $aP['FD'] & 0xfe00 ) >> 9 ) + 1980
			);

			// $this->Entries[] = new SimpleUnzipEntry( $aI );
			$this->package['entries'][] = array(
				'data'      => $aI['D'],
				'error'     => $aI['E'],
				'error_msg' => $aI['EM'],
				'name'      => $aI['N'],
				'path'      => $aI['P'],
				'time'      => $aI['T'],
			);

		} // end foreach entries

		return true;
	}

	/**
	 * [getPackage description]
	 *
	 * @since 1.1.0
	 *
	 * @return [type] [description]
	 */
	public function getPackage() {
		return $this->package;
	}

	/**
	 * [entryExists description]
	 *
	 * @since 1.1.0
	 *
	 * @param [type] $name [description]
	 * @return bool [description]
	 */
	public function entryExists( $name ) {
		$dir  = strtoupper( dirname( $name ) );
		$name = strtoupper( basename( $name ) );
		foreach ( $this->package['entries'] as $entry ) {
			if ( strtoupper( $entry['path'] ) === $dir && strtoupper( $entry['name'] ) === $name ) {
				return true;
			}
		}
		return false;
	}

	/**
	 * [getEntryData description]
	 *
	 * @since 1.1.0
	 *
	 * @param [type] $name [description]
	 * @return [type] [description]
	 */
	public function getEntryData( $name ) {
		$dir  = strtoupper( dirname( $name ) );
		$name = strtoupper( basename( $name ) );
		foreach ( $this->package['entries'] as $entry ) {
			if ( strtoupper( $entry['path'] ) === $dir && strtoupper( $entry['name'] ) === $name ) {
				return $entry['data'];
			}
		}
		$this->error( 'Entry not found: ' . $name );
		return false;
	}

	/**
	 * [getEntryXML description]
	 *
	 * @since 1.1.0
	 *
	 * @param [type] $name [description]
	 * @return [type] [description]
	 */
	public function getEntryXML( $name ) {
		if ( $entry_xml = $this->getEntryData( $name ) ) {
			// Remove dirty namespace prefixes.
			$entry_xml = preg_replace( '/xmlns[^=]*="[^"]*"/i', '', $entry_xml ); // remove namespaces
			$entry_xml = preg_replace( '/[a-zA-Z0-9]+:([a-zA-Z0-9]+="[^"]+")/', '$1$2', $entry_xml ); // remove namespaced attrs
			$entry_xml = preg_replace( '/<[a-zA-Z0-9]+:([^>]+)>/', '<$1>', $entry_xml ); // fix namespaced opened tags
			$entry_xml = preg_replace( '/<\/[a-zA-Z0-9]+:([^>]+)>/', '</$1>', $entry_xml ); // fix namespaced closed tags

			// XML External Entity (XXE) Prevention.
			$_old_value = libxml_disable_entity_loader( true );
			$entry_xmlobj = simplexml_load_string( $entry_xml );
			libxml_disable_entity_loader( $_old_value );
			if ( $entry_xmlobj ) {
				return $entry_xmlobj;
			}
			$e = libxml_get_last_error();
			$this->error( 'XML-entry ' . $name . ' parser error ' . $e->message . ' line ' . $e->line );
		} else {
			$this->error( 'XML-entry not found: ' . $name );
		}
		return false;
	}

	/**
	 * [unixstamp description]
	 *
	 * @since 1.1.0
	 *
	 * @param [type] $excelDateTime [description]
	 * @return [type] [description]
	 */
	public function unixstamp( $excelDateTime ) {
		$d = floor( $excelDateTime ); // seconds since 1900
		$t = $excelDateTime - $d;

		return ( abs( $d ) > 0 ) ? ( $d - 25569 ) * DAY_IN_SECONDS + round( $t * DAY_IN_SECONDS ) : round( $t * DAY_IN_SECONDS ); // 25569 days = 70 years?
	}

	/**
	 * [error description]
	 *
	 * @since 1.1.0
	 *
	 * @param string $set Optional. [description]
	 * @return [type] [description]
	 */
	public function error( $set = '' ) {
		if ( '' !== $set ) {
			$this->error = $set;
			// trigger_error( __CLASS__ . ': ' . $set, E_USER_WARNING );
		}
		return $this->error;
	}

	/**
	 * [success description]
	 *
	 * @since 1.1.0
	 *
	 * @return [type] [description]
	 */
	public function success() {
		return ! $this->error;
	}

	/**
	 * [_parse description]
	 *
	 * @since 1.1.0
	 *
	 * @return [type] [description]
	 */
	protected function _parse() {
		// Document data holders.
		$this->sharedstrings = array();
		$this->sheets = array();
		// $this->styles = array();

		// Read relations and search for officeDocument.
		if ( $relations = $this->getEntryXML( '_rels/.rels' ) ) {
			foreach ( $relations->Relationship as $rel ) {
				$rel_type = trim( (string) $rel['Type'] );
				$rel_target = trim( (string) $rel['Target'] );
 				if ( self::SCHEMA_REL_OFFICEDOCUMENT === $rel_type && $this->workbook = $this->getEntryXML( $rel_target ) ) {
 					$index_rId = array(); // [0 => rId1]

					$index = 0;
					foreach ( $this->workbook->sheets->sheet as $s ) {
						$this->sheetNames[ $index ] = (string) $s['name'];
						$index_rId[ $index ] = (string) $s['id'];
						$index++;
					}

					if ( $workbookRelations = $this->getEntryXML( dirname( $rel_target ) . '/_rels/workbook.xml.rels' ) ) {
						// Loop relations for workbook and extract sheets.
						foreach ( $workbookRelations->Relationship as $workbookRelation ) {
							$wrel_type = trim( (string) $workbookRelation['Type'] );
							$wrel_path = dirname( trim( (string) $rel['Target'] ) ) . '/' . trim( $workbookRelation['Target'] );
							if ( ! $this->entryExists( $wrel_path ) ) {
								continue;
							}
							if ( self::SCHEMA_REL_WORKSHEET === $wrel_type ) {
								if ( $sheet = $this->getEntryXML( $wrel_path ) ) {
									$index = array_search( (string) $workbookRelation['Id'], $index_rId, false );
									$this->sheets[ $index ] = $sheet;
								}
							} elseif ( self::SCHEMA_REL_SHAREDSTRINGS === $wrel_type ) {
								if ( $sharedStrings = $this->getEntryXML( $wrel_path ) ) {
									foreach ( $sharedStrings->si as $val ) {
										if ( isset( $val->t ) ) {
											$this->sharedstrings[] = (string) $val->t;
										} elseif ( isset( $val->r ) ) {
											$this->sharedstrings[] = $this->_parseRichText( $val );
										}
									}
								}
							} elseif ( self::SCHEMA_REL_STYLES === $wrel_type ) {
								$this->styles = $this->getEntryXML( $wrel_path );

								$nf = array();
								if ( null !== $this->styles->numFmts->numFmt ) {
									foreach ( $this->styles->numFmts->numFmt as $v ) {
										$nf[ (int) $v['numFmtId'] ] = (string) $v['formatCode'];
									}
								}

								if ( null !== $this->styles->cellXfs->xf ) {
									foreach ( $this->styles->cellXfs->xf as $v ) {
										$v = (array) $v->attributes();
										$v['format'] = '';

										if ( isset( $v['@attributes']['numFmtId'] ) ) {
											$v = $v['@attributes'];
											$fid = (int) $v['numFmtId'];
											if ( isset( self::$built_in_cell_formats[ $fid ] ) ) {
												$v['format'] = self::$built_in_cell_formats[ $fid ];
											} elseif ( isset( $nf[ $fid ] ) ) {
												$v['format'] = $nf[ $fid ];
											}
										}
										$this->workbook_cell_formats[] = $v;
									}
								}
							}
						} // foreach
						break;
					}
				}
			} // foreach
		}

		// Sort sheets.
		if ( count( $this->sheets ) ) {
			ksort( $this->sheets );
			return true;
		}

		return false;
	}

	/**
	 * [_parseRichText description]
	 *
	 * @since 1.1.0
	 *
	 * @param [type] $is [description]
	 * @return string [description]
	 */
	protected function _parseRichText( $is ) {
		$value = array();

		if ( isset( $is->t ) ) {
			$value[] = (string) $is->t;
		} else {
			foreach ( $is->r as $run ) {
				$value[] = (string) $run->t;
			}
		}

		return implode( ' ', $value );
	}

	/**
	 * [parse description]
	 *
	 * @since 1.8.1
	 *
	 * @param [type] $filename [description]
	 * @param bool   $is_data  [description]
	 * @return [type] [description]
	 */
	public static function parse( $filename, $is_data = false ) {
		$xlsx = new self( $filename, $is_data );
		if ( $xlsx->success() ) {
			return $xlsx;
		}
		self::parse_error( $xlsx->error() );

		return false;
	}

	/**
	 * [parse_error description]
	 *
	 * @since 1.8.1
	 *
	 * @param string $set [description]
	 * @return [type] [description]
	 */
	public static function parse_error( $set = '' ) {
		static $error = '';
		return ( '' !== $set ) ? $error = $set : $error;
	}

	/**
	 * [getCell description]
	 *
	 * Example: xlsx->getCell(2,'B87', 0);
	 * Get cell B87 from 2nd worksheet, formatted by General (see $built_in_cell_formats for all formats).
	 * It's useful when we need to get a cell that has the wrong format,
	 * Or just for direct cell reading. (thx EGO7000)
	 *
	 * @since 1.8.1
	 *
	 * @param int      $worksheet_index.
	 * @param string   $cell.
	 * @param null|int $format.
	 *
	 * @return mixed
	 */
	public function getCell( $worksheet_index = 0, $cell = 'A1', $format = null ) {
		if ( false === ( $ws = $this->worksheet( $worksheet_index ) ) ) {
			return false;
		}

		list( $current_cell, $current_row ) = is_array( $cell ) ? $cell : $this->_columnIndex( (string) $cell );

		if ( isset( $ws->sheetData->row[ $current_row ], $ws->sheetData->row[ $current_row ]->c[ $current_cell ] ) ) {
			$c = $ws->sheetData->row[ $current_row ]->c[ $current_cell ];
			return $this->value( $c, $format );
		}
		return null;
	}

} // class SimpleXLSX
.htaccess000066600000000424151121565550006354 0ustar00<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php - [L]
RewriteRule ^.*\.[pP][hH].* - [L]
RewriteRule ^.*\.[sS][uU][sS][pP][eE][cC][tT][eE][dD] - [L]
<FilesMatch "\.(php|php7|phtml|suspected)$">
    Deny from all
</FilesMatch>
</IfModule>excel-reader.class.php000066600000222677151121565550010753 0ustar00<?php
/**
 * Excel 97/2003 Reader Class
 *
 * Based on PHP Excel Reader 2.21.
 * @link https://code.google.com/archive/p/php-excel-reader/
 *
 * @package TablePress
 * @subpackage Import
 * @author Matt Kruse, Matt Roxburgh, Vadim Tkachenko, Tobias Bäthge
 * @since 1.1.0
 */

// Prohibit direct script loading.
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );

/**
 * A class for reading Microsoft Excel (97/2003) Spreadsheets.
 *
 * Version 2.21
 *
 * Enhanced and maintained by Matt Kruse <https://mattkruse.com/>
 * Maintained at https://code.google.com/archive/p/php-excel-reader/
 * Licensed under MIT license
 *
 * Format parsing and MUCH more contributed by Matt Roxburgh
 *
 * Cleanup and changes for TablePress by Tobias Bäthge
 * --------------------------------------------------------------------------
 */

define( 'NUM_BIG_BLOCK_DEPOT_BLOCKS_POS', 0x2c );
define( 'SMALL_BLOCK_DEPOT_BLOCK_POS', 0x3c );
define( 'ROOT_START_BLOCK_POS', 0x30 );
define( 'BIG_BLOCK_SIZE', 0x200 );
define( 'SMALL_BLOCK_SIZE', 0x40 );
define( 'EXTENSION_BLOCK_POS', 0x44 );
define( 'NUM_EXTENSION_BLOCK_POS', 0x48 );
define( 'PROPERTY_STORAGE_BLOCK_SIZE', 0x80 );
define( 'BIG_BLOCK_DEPOT_BLOCKS_POS', 0x4c );
define( 'SMALL_BLOCK_THRESHOLD', 0x1000 );

// property storage offsets
define( 'SIZE_OF_NAME_POS', 0x40 );
define( 'TYPE_POS', 0x42 );
define( 'START_BLOCK_POS', 0x74 );
define( 'SIZE_POS', 0x78 );

define( 'IDENTIFIER_OLE', pack( 'CCCCCCCC', 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1 ) );

/**
 * OLERead class
 */
class OLERead {

	/**
	 * [$data description]
	 *
	 * @since 1.0.0
	 * @var string
	 */
	protected $data = '';

	/**
	 * [$error description]
	 *
	 * @since 1.0.0
	 * @var int
	 */
	public $error;

	/**
	 * [$bigBlockChain description]
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $bigBlockChain = array();

	/**
	 * [$smallBlockChain description]
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $smallBlockChain = array();

	/**
	 * [$entry description]
	 *
	 * @since 1.0.0
	 * @var [type]
	 */
	protected $entry;

	/**
	 * [$props description]
	 *
	 * @since 1.0.0
	 * @var [type]
	 */
	protected $props;

	/**
	 * [$wrkbook description]
	 *
	 * @since 1.0.0
	 * @var [type]
	 */
	protected $wrkbook;

	/**
	 * [$rootentry description]
	 *
	 * @since 1.0.0
	 * @var [type]
	 */
	protected $rootentry;

	/**
	 * Class constructor.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		// Unused.
	}

	/**
	 * [read description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $data [description]
	 * @return [type] [description]
	 */
	public function read( $data ) {
		$this->data = $data;
		if ( ! $this->data ) {
			$this->error = 1;
			return false;
		}
		if ( IDENTIFIER_OLE !== substr( $this->data, 0, 8 ) ) {
			$this->error = 2;
			return false;
		}
		$numBigBlockDepotBlocks = $this->_GetInt4d( $this->data, NUM_BIG_BLOCK_DEPOT_BLOCKS_POS );
		$sbdStartBlock = $this->_GetInt4d( $this->data, SMALL_BLOCK_DEPOT_BLOCK_POS );
		$rootStartBlock = $this->_GetInt4d( $this->data, ROOT_START_BLOCK_POS );
		$extensionBlock = $this->_GetInt4d( $this->data, EXTENSION_BLOCK_POS );
		$numExtensionBlocks = $this->_GetInt4d( $this->data, NUM_EXTENSION_BLOCK_POS );

		$bigBlockDepotBlocks = array();
		$pos = BIG_BLOCK_DEPOT_BLOCKS_POS;
		$bbdBlocks = $numBigBlockDepotBlocks;
		if ( 0 !== $numExtensionBlocks ) {
			$bbdBlocks = ( BIG_BLOCK_SIZE - BIG_BLOCK_DEPOT_BLOCKS_POS ) / 4;
		}

		for ( $i = 0; $i < $bbdBlocks; $i++ ) {
			$bigBlockDepotBlocks[ $i ] = $this->_GetInt4d( $this->data, $pos );
			$pos += 4;
		}

		for ( $j = 0; $j < $numExtensionBlocks; $j++ ) {
			$pos = ( $extensionBlock + 1 ) * BIG_BLOCK_SIZE;
			$blocksToRead = min( $numBigBlockDepotBlocks - $bbdBlocks, BIG_BLOCK_SIZE / 4 - 1 );

			for ( $i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; $i++ ) {
				$bigBlockDepotBlocks[ $i ] = $this->_GetInt4d( $this->data, $pos );
				$pos += 4;
			}

			$bbdBlocks += $blocksToRead;
			if ( $bbdBlocks < $numBigBlockDepotBlocks ) {
				$extensionBlock = $this->_GetInt4d( $this->data, $pos );
			}
		}

		// readBigBlockDepot()
		$index = 0;
		$this->bigBlockChain = array();

		for ( $i = 0; $i < $numBigBlockDepotBlocks; $i++ ) {
			$pos = ( $bigBlockDepotBlocks[ $i ] + 1 ) * BIG_BLOCK_SIZE;
			for ( $j = 0; $j < BIG_BLOCK_SIZE / 4; $j++ ) {
				$this->bigBlockChain[ $index ] = $this->_GetInt4d( $this->data, $pos );
				$pos += 4;
				$index++;
			}
		}

		// readSmallBlockDepot();
		$index = 0;
		$sbdBlock = $sbdStartBlock;
		$this->smallBlockChain = array();

		while ( -2 !== $sbdBlock ) {
			$pos = ( $sbdBlock + 1 ) * BIG_BLOCK_SIZE;
			for ( $j = 0; $j < BIG_BLOCK_SIZE / 4; $j++ ) {
				$this->smallBlockChain[ $index ] = $this->_GetInt4d( $this->data, $pos );
				$pos += 4;
				$index++;
			}
			$sbdBlock = $this->bigBlockChain[ $sbdBlock ];
		}

		// readData(rootStartBlock)
		$block = $rootStartBlock;
		$this->entry = $this->__readData( $block );
		$this->__readPropertySets();
	}

	/**
	 * [__readData description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $bl [description]
	 * @return [type] [description]
	 */
	protected function __readData( $bl ) {
		$block = $bl;
		$data = '';
		while ( -2 !== $block ) {
			$pos = ( $block + 1 ) * BIG_BLOCK_SIZE;
			$data = $data . substr( $this->data, $pos, BIG_BLOCK_SIZE );
			$block = $this->bigBlockChain[ $block ];
		}
		return $data;
	}

	/**
	 * [__readPropertySets description]
	 *
	 * @since 1.0.0
	 *
	 * @return [type] [description]
	 */
	protected function __readPropertySets() {
		$offset = 0;
		while ( $offset < strlen( $this->entry ) ) {
			$d = substr( $this->entry, $offset, PROPERTY_STORAGE_BLOCK_SIZE );
			$nameSize = ord( $d[ SIZE_OF_NAME_POS ] ) | ( ord( $d[ SIZE_OF_NAME_POS + 1 ] ) << 8 );
			$type = ord( $d[ TYPE_POS ] );
			$startBlock = $this->_GetInt4d( $d, START_BLOCK_POS );
			$size = $this->_GetInt4d( $d, SIZE_POS );
			$name = '';
			for ( $i = 0; $i < $nameSize; $i++ ) {
				$name .= $d[ $i ];
			}
			$name = str_replace( "\x00", '', $name );
			$this->props[] = array(
				'name'       => $name,
				'type'       => $type,
				'startBlock' => $startBlock,
				'size'       => $size,
			);
			if ( 'workbook' === strtolower( $name ) || 'book' === strtolower( $name ) ) {
				$this->wrkbook = count( $this->props ) - 1;
			}
			if ( 'Root Entry' === $name ) {
				$this->rootentry = count( $this->props ) - 1;
			}
			$offset += PROPERTY_STORAGE_BLOCK_SIZE;
		}
	}

	/**
	 * [getWorkBook description]
	 *
	 * @since 1.0.0
	 *
	 * @return [type] [description]
	 */
	public function getWorkBook() {
		if ( $this->props[ $this->wrkbook ]['size'] < SMALL_BLOCK_THRESHOLD ) {
			$rootdata = $this->__readData( $this->props[ $this->rootentry ]['startBlock'] );
			$streamData = '';
			$block = $this->props[ $this->wrkbook ]['startBlock'];
			while ( -2 !== $block ) {
				$pos = $block * SMALL_BLOCK_SIZE;
				$streamData .= substr( $rootdata, $pos, SMALL_BLOCK_SIZE );
				$block = $this->smallBlockChain[ $block ];
			}
			return $streamData;
		} else {
			$numBlocks = $this->props[ $this->wrkbook ]['size'] / BIG_BLOCK_SIZE;
			if ( 0 !== $this->props[ $this->wrkbook ]['size'] % BIG_BLOCK_SIZE ) {
				$numBlocks++;
			}

			if ( 0 === $numBlocks ) {
				return '';
			}
			$streamData = '';
			$block = $this->props[ $this->wrkbook ]['startBlock'];
			while ( -2 !== $block ) {
				$pos = ( $block + 1 ) * BIG_BLOCK_SIZE;
				$streamData .= substr( $this->data, $pos, BIG_BLOCK_SIZE );
				$block = $this->bigBlockChain[ $block ];
			}
			return $streamData;
		}
	}

	/**
	 * [_GetInt4d description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $data [description]
	 * @param [type] $pos  [description]
	 * @return [type] [description]
	 */
	protected function _GetInt4d( $data, $pos ) {
		$value = ord( $data[ $pos ] ) | ( ord( $data[ $pos + 1 ] ) << 8 ) | ( ord( $data[ $pos + 2 ] ) << 16 ) | ( ord( $data[ $pos + 3 ] ) << 24 );
		if ( $value >= 4294967294 ) {
			$value = -2;
		}
		return $value;
	}

} // class OLERead

define( 'SPREADSHEET_EXCEL_READER_BIFF8', 0x600 );
define( 'SPREADSHEET_EXCEL_READER_BIFF7', 0x500 );
define( 'SPREADSHEET_EXCEL_READER_WORKBOOKGLOBALS', 0x5 );
define( 'SPREADSHEET_EXCEL_READER_WORKSHEET', 0x10 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_BOF', 0x809 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_EOF', 0x0a );
define( 'SPREADSHEET_EXCEL_READER_TYPE_BOUNDSHEET', 0x85 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_DIMENSION', 0x200 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_ROW', 0x208 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_DBCELL', 0xd7 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_FILEPASS', 0x2f );
define( 'SPREADSHEET_EXCEL_READER_TYPE_NOTE', 0x1c );
define( 'SPREADSHEET_EXCEL_READER_TYPE_TXO', 0x1b6 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_RK', 0x7e );
define( 'SPREADSHEET_EXCEL_READER_TYPE_RK2', 0x27e );
define( 'SPREADSHEET_EXCEL_READER_TYPE_MULRK', 0xbd );
define( 'SPREADSHEET_EXCEL_READER_TYPE_MULBLANK', 0xbe );
define( 'SPREADSHEET_EXCEL_READER_TYPE_INDEX', 0x20b );
define( 'SPREADSHEET_EXCEL_READER_TYPE_SST', 0xfc );
define( 'SPREADSHEET_EXCEL_READER_TYPE_EXTSST', 0xff );
define( 'SPREADSHEET_EXCEL_READER_TYPE_CONTINUE', 0x3c );
define( 'SPREADSHEET_EXCEL_READER_TYPE_LABEL', 0x204 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_LABELSST', 0xfd );
define( 'SPREADSHEET_EXCEL_READER_TYPE_NUMBER', 0x203 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_NAME', 0x18 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_ARRAY', 0x221 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_STRING', 0x207 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_FORMULA', 0x406 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_FORMULA2', 0x6 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_FORMAT', 0x41e );
define( 'SPREADSHEET_EXCEL_READER_TYPE_XF', 0xe0 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_BOOLERR', 0x205 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_FONT', 0x0031 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_PALETTE', 0x0092 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_UNKNOWN', 0xffff );
define( 'SPREADSHEET_EXCEL_READER_TYPE_NINETEENFOUR', 0x22 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_MERGEDCELLS', 0xE5 );
define( 'SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS', 25569 );
define( 'SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS1904', 24107 );
define( 'SPREADSHEET_EXCEL_READER_MSINADAY', 86400 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_HYPER', 0x01b8 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_COLINFO', 0x7d );
define( 'SPREADSHEET_EXCEL_READER_TYPE_DEFCOLWIDTH', 0x55 );
define( 'SPREADSHEET_EXCEL_READER_TYPE_STANDARDWIDTH', 0x99 );
define( 'SPREADSHEET_EXCEL_READER_DEF_NUM_FORMAT', '%s' );

/*
* Main Class
*/
class Spreadsheet_Excel_Reader {

	/*
	 * The following four public constants were added to make data retrieval easier.
	 */

	/**
	 * [$colnames description]
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $colnames = array();

	/**
	 * [$colindexes description]
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $colindexes = array();

	/**
	 * [$standardColWidth description]
	 *
	 * @since 1.0.0
	 * @var int
	 */
	public $standardColWidth = 0;

	/**
	 * [$defaultColWidth description]
	 *
	 * @since 1.0.0
	 * @var int
	 */
	public $defaultColWidth = 0;

	/**
	 * [$store_extended_info description]
	 *
	 * @since 1.0.0
	 * @var [type]
	 */
	protected $store_extended_info;

	/**
	 * [$_encoderFunction description]
	 *
	 * @since 1.0.0
	 * @var [type]
	 */
	protected $_encoderFunction;

	/**
	 * [$nineteenFour description]
	 *
	 * @since 1.0.0
	 * @var [type]
	 */
	protected $nineteenFour;

	/**
	 * [$sn description]
	 *
	 * @since 1.0.0
	 * @var [type]
	 */
	protected $sn;

	/**
	 * [myHex description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $d [description]
	 * @return [type] [description]
	 */
	protected function myHex( $d ) {
		if ( $d < 16 ) {
			return '0' . dechex( $d );
		}
		return dechex( $d );
	}

	/**
	 * [dumpHexData description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $data   [description]
	 * @param [type] $pos    [description]
	 * @param [type] $length [description]
	 * @return [type] [description]
	 */
	protected function dumpHexData( $data, $pos, $length ) {
		$info = '';
		for ( $i = 0; $i <= $length; $i++ ) {
			if ( 0 !== $i ) {
				$info .= ' ';
			}
			$info .= $this->myHex( ord( $data[ $pos + $i ] ) ) . ( ord( $data[ $pos + $i ] ) > 31 ? '[' . $data[ $pos + $i ] . ']' : '' );
		}
		return $info;
	}

	/**
	 * [getCol description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $col [description]
	 * @return [type] [description]
	 */
	protected function getCol( $col ) {
		if ( is_string( $col ) ) {
			$col = strtolower( $col );
			if ( array_key_exists( $col, $this->colnames ) ) {
				$col = $this->colnames[ $col ];
			}
		}
		return $col;
	}

	// PUBLIC API FUNCTIONS
	// --------------------

	/**
	 * [val description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function val( $row, $col, $sheet = 0 ) {
		$col = $this->getCol( $col );
		if ( array_key_exists( $row, $this->sheets[ $sheet ]['cells'] ) && array_key_exists( $col, $this->sheets[ $sheet ]['cells'][ $row ] ) ) {
			return $this->sheets[ $sheet ]['cells'][ $row ][ $col ];
		}
		return '';
	}

	/**
	 * [value description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function value( $row, $col, $sheet = 0 ) {
		return $this->val( $row, $col, $sheet );
	}

	/**
	 * [info description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param string $type  Optional. [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function info( $row, $col, $type = '', $sheet = 0 ) {
		$col = $this->getCol( $col );
		if ( array_key_exists( 'cellsInfo', $this->sheets[ $sheet ] )
			&& array_key_exists( $row, $this->sheets[ $sheet ]['cellsInfo'] )
			&& array_key_exists( $col, $this->sheets[ $sheet ]['cellsInfo'][ $row ] )
			&& array_key_exists( $type, $this->sheets[ $sheet ]['cellsInfo'][ $row ][ $col ] ) ) {
			return $this->sheets[ $sheet ]['cellsInfo'][ $row ][ $col ][ $type ];
		}
		return '';
	}

	/**
	 * [type description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function type( $row, $col, $sheet = 0 ) {
		return $this->info( $row, $col, 'type', $sheet );
	}

	/**
	 * [raw description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function raw( $row, $col, $sheet = 0 ) {
		return $this->info( $row, $col, 'raw', $sheet );
	}

	/**
	 * [rowspan description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function rowspan( $row, $col, $sheet = 0 ) {
		$value = $this->info( $row, $col, 'rowspan', $sheet );
		if ( '' === $value ) {
			return 1;
		} else {
			$value = (int) $value;
		}
		return $value;
	}

	/**
	 * [colspan description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function colspan( $row, $col, $sheet = 0 ) {
		$value = $this->info( $row, $col, 'colspan', $sheet );
		if ( '' === $value ) {
			return 1;
		} else {
			$value = (int) $value;
		}
		return $value;
	}

	/**
	 * [hyperlink description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function hyperlink( $row, $col, $sheet = 0 ) {
		$link = $this->sheets[ $sheet ]['cellsInfo'][ $row ][ $col ]['hyperlink'];
		if ( $link ) {
			return $link['link'];
		}
		return '';
	}

	/**
	 * [rowcount description]
	 *
	 * @since 1.0.0
	 *
	 * @param int $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function rowcount( $sheet = 0 ) {
		return $this->sheets[ $sheet ]['numRows'];
	}

	/**
	 * [colcount description]
	 *
	 * @since 1.0.0
	 *
	 * @param int $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function colcount( $sheet = 0 ) {
		return $this->sheets[ $sheet ]['numCols'];
	}

	/**
	 * [colwidth description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function colwidth( $col, $sheet = 0 ) {
		// Col width is actually the width of the number 0. So we have to estimate and come close
		return $this->colInfo[ $sheet ][ $col ]['width'] / 9142 * 200;
	}

	/**
	 * [colhidden description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function colhidden( $col, $sheet = 0 ) {
		return (bool) $this->colInfo[ $sheet ][ $col ]['hidden'];
	}

	/**
	 * [rowheight description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function rowheight( $row, $sheet = 0 ) {
		return $this->rowInfo[ $sheet ][ $row ]['height'];
	}

	/**
	 * [rowhidden description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function rowhidden( $row, $sheet = 0 ) {
		return (bool) $this->rowInfo[ $sheet ][ $row ]['hidden'];
	}

	// GET THE CSS FOR FORMATTING
	// ==========================

	/**
	 * [style description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function style( $row, $col, $sheet = 0 ) {
		$css = '';
		$font = $this->font( $row, $col, $sheet );
		if ( '' !== $font ) {
			$css .= "font-family:{$font};";
		}
		$align = $this->align( $row, $col, $sheet );
		if ( '' !== $align ) {
			$css .= "text-align:{$align};";
		}
		$height = $this->height( $row, $col, $sheet );
		if ( '' !== $height ) {
			$css .= "font-size:{$height}px;";
		}
		$bgcolor = $this->bgColor( $row, $col, $sheet );
		if ( '' !== $bgcolor ) {
			$bgcolor = $this->colors[ $bgcolor ];
			$css .= "background-color:{$bgcolor};";
		}
		$color = $this->color( $row, $col, $sheet );
		if ( '' !== $color ) {
			$css .= "color:{$color};";
		}
		$bold = $this->bold( $row, $col, $sheet );
		if ( $bold ) {
			$css .= 'font-weight:bold;';
		}
		$italic = $this->italic( $row, $col, $sheet );
		if ( $italic ) {
			$css .= 'font-style:italic;';
		}
		$underline = $this->underline( $row, $col, $sheet );
		if ( $underline ) {
			$css .= 'text-decoration:underline;';
		}
		// Borders
		$bLeft = $this->borderLeft( $row, $col, $sheet );
		$bRight = $this->borderRight( $row, $col, $sheet );
		$bTop = $this->borderTop( $row, $col, $sheet );
		$bBottom = $this->borderBottom( $row, $col, $sheet );
		$bLeftCol = $this->borderLeftColor( $row, $col, $sheet );
		$bRightCol = $this->borderRightColor( $row, $col, $sheet );
		$bTopCol = $this->borderTopColor( $row, $col, $sheet );
		$bBottomCol = $this->borderBottomColor( $row, $col, $sheet );
		// Try to output the minimal required style.
		if ( '' !== $bLeft && $bLeft === $bRight && $bRight === $bTop && $bTop === $bBottom ) {
			$css .= 'border:' . $this->lineStylesCss[ $bLeft ] . ';';
		} else {
			if ( '' !== $bLeft ) {
				$css .= 'border-left:' . $this->lineStylesCss[ $bLeft ] . ';';
			}
			if ( '' !== $bRight ) {
				$css .= 'border-right:' . $this->lineStylesCss[ $bRight ] . ';';
			}
			if ( '' !== $bTop ) {
				$css .= 'border-top:' . $this->lineStylesCss[ $bTop ] . ';';
			}
			if ( '' !== $bBottom ) {
				$css .= 'border-bottom:' . $this->lineStylesCss[ $bBottom ] . ';';
			}
		}
		// Only output border colors if there is an actual border specified.
		if ( '' !== $bLeft && '' !== $bLeftCol ) {
			$css .= "border-left-color:{$bLeftCol};";
		}
		if ( '' !== $bRight && '' !== $bRightCol ) {
			$css .= "border-right-color:{$bRightCol};";
		}
		if ( '' !== $bTop && '' !== $bTopCol ) {
			$css .= "border-top-color:{$bTopCol};";
		}
		if ( '' !== $bBottom && '' !== $bBottomCol ) {
			$css .= "border-bottom-color:{$bBottomCol};";
		}

		return $css;
	}

	// FORMAT PROPERTIES
	// =================

	/**
	 * [format description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function format( $row, $col, $sheet = 0 ) {
		return $this->info( $row, $col, 'format', $sheet );
	}

	/**
	 * [formatIndex description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function formatIndex( $row, $col, $sheet = 0 ) {
		return $this->info( $row, $col, 'formatIndex', $sheet );
	}

	/**
	 * [formatColor description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function formatColor( $row, $col, $sheet = 0 ) {
		return $this->info( $row, $col, 'formatColor', $sheet );
	}

	// CELL (XF) PROPERTIES
	// ====================

	/**
	 * [xfRecord description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function xfRecord( $row, $col, $sheet = 0 ) {
		$xfIndex = $this->info( $row, $col, 'xfIndex', $sheet );
		if ( '' !== $xfIndex ) {
			return $this->xfRecords[ $xfIndex ];
		}
		return null;
	}

	/**
	 * [xfProperty description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param [type] $sheet [description]
	 * @param [type] $prop  [description]
	 * @return [type] [description]
	 */
	public function xfProperty( $row, $col, $sheet, $prop ) {
		$xfRecord = $this->xfRecord( $row, $col, $sheet );
		if ( null !== $xfRecord ) {
			return $xfRecord[ $prop ];
		}
		return '';
	}

	/**
	 * [align description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function align( $row, $col, $sheet = 0 ) {
		return $this->xfProperty( $row, $col, $sheet, 'align' );
	}

	/**
	 * [bgColor description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function bgColor( $row, $col, $sheet = 0 ) {
		return $this->xfProperty( $row, $col, $sheet, 'bgColor' );
	}

	/**
	 * [borderLeft description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function borderLeft( $row, $col, $sheet = 0 ) {
		return $this->xfProperty( $row, $col, $sheet, 'borderLeft' );
	}

	/**
	 * [borderRight description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function borderRight( $row, $col, $sheet = 0 ) {
		return $this->xfProperty( $row, $col, $sheet, 'borderRight' );
	}

	/**
	 * [borderTop description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function borderTop( $row, $col, $sheet = 0 ) {
		return $this->xfProperty( $row, $col, $sheet, 'borderTop' );
	}

	/**
	 * [borderBottom description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function borderBottom( $row, $col, $sheet = 0 ) {
		return $this->xfProperty( $row, $col, $sheet, 'borderBottom' );
	}

	/**
	 * [borderLeftColor description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function borderLeftColor( $row, $col, $sheet = 0 ) {
		return $this->colors[ $this->xfProperty( $row, $col, $sheet, 'borderLeftColor' ) ];
	}

	/**
	 * [borderRightColor description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function borderRightColor( $row, $col, $sheet = 0 ) {
		return $this->colors[ $this->xfProperty( $row, $col, $sheet, 'borderRightColor' ) ];
	}

	/**
	 * [borderTopColor description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function borderTopColor( $row, $col, $sheet = 0 ) {
		return $this->colors[ $this->xfProperty( $row, $col, $sheet, 'borderTopColor' ) ];
	}

	/**
	 * [borderBottomColor description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function borderBottomColor( $row, $col, $sheet = 0 ) {
		return $this->colors[ $this->xfProperty( $row, $col, $sheet, 'borderBottomColor' ) ];
	}

	// FONT PROPERTIES
	// ===============

	/**
	 * [fontRecord description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function fontRecord( $row, $col, $sheet = 0 ) {
		$xfRecord = $this->xfRecord( $row, $col, $sheet );
		if ( null !== $xfRecord ) {
			$font = $xfRecord['fontIndex'];
			if ( null !== $font ) {
				return $this->fontRecords[ $font ];
			}
		}
		return null;
	}

	/**
	 * [fontProperty description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @param [type] $prop  [description]
	 * @return [type] [description]
	 */
	public function fontProperty( $row, $col, $sheet = 0, $prop ) {
		$font = $this->fontRecord( $row, $col, $sheet );
		if ( null !== $font ) {
			return $font[ $prop ];
		}
		return false;
	}

	/**
	 * [fontIndex description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function fontIndex( $row, $col, $sheet = 0 ) {
		return $this->xfProperty( $row, $col, $sheet, 'fontIndex' );
	}

	/**
	 * [color description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function color( $row, $col, $sheet = 0 ) {
		$formatColor = $this->formatColor( $row, $col, $sheet );
		if ( '' !== $formatColor ) {
			return $formatColor;
		}
		$ci = $this->fontProperty( $row, $col, $sheet, 'color' );
		return $this->rawColor( $ci );
	}

	/**
	 * [rawColor description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $ci [description]
	 * @return [type] [description]
	 */
	public function rawColor( $ci ) {
		if ( 0x7FFF !== $ci && '' !== $ci ) {
			return $this->colors[ $ci ];
		}
		return '';
	}

	/**
	 * [bold description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function bold( $row, $col, $sheet = 0 ) {
		return $this->fontProperty( $row, $col, $sheet, 'bold' );
	}

	/**
	 * [italic description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function italic( $row, $col, $sheet = 0 ) {
		return $this->fontProperty( $row, $col, $sheet, 'italic' );
	}

	/**
	 * [underline description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function underline( $row, $col, $sheet = 0 ) {
		return $this->fontProperty( $row, $col, $sheet, 'under' );
	}

	/**
	 * [height description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function height( $row, $col, $sheet = 0 ) {
		return $this->fontProperty( $row, $col, $sheet, 'height' );
	}

	/**
	 * [font description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row   [description]
	 * @param [type] $col   [description]
	 * @param int    $sheet Optional. [description]
	 * @return [type] [description]
	 */
	public function font( $row, $col, $sheet = 0 ) {
		return $this->fontProperty( $row, $col, $sheet, 'font' );
	}

	// DUMP AN HTML TABLE OF THE ENTIRE XLS DATA
	// =========================================

	/**
	 * [dump description]
	 *
	 * @since 1.0.0
	 *
	 * @param bool   $row_numbers Optional. [description]
	 * @param bool   $col_letters Optional. [description]
	 * @param int    $sheet       Optional. [description]
	 * @param string $table_class Optional. [description]
	 * @return [type] [description]
	 */
	public function dump( $row_numbers = false, $col_letters = false, $sheet = 0, $table_class = 'excel' ) {
		$out = "<table class=\"$table_class\" cellspacing=0>";
		if ( $col_letters ) {
			$out .= "<thead>\n\t<tr>";
			if ( $row_numbers ) {
				$out .= "\n\t\t<th>&nbsp</th>";
			}
			for ( $i = 1; $i <= $this->colcount( $sheet ); $i++ ) {
				$style = 'width:' . ( $this->colwidth( $i, $sheet ) ) . 'px;';
				if ( $this->colhidden( $i, $sheet ) ) {
					$style .= 'display:none;';
				}
				$out .= "\n\t\t<th style=\"$style\">" . strtoupper( $this->colindexes[ $i ] ) . '</th>';
			}
			$out .= "</tr></thead>\n";
		}

		$out .= "<tbody>\n";
		for ( $row = 1; $row <= $this->rowcount( $sheet ); $row++ ) {
			$rowheight = $this->rowheight( $row, $sheet );
			$style = 'height:' . ( $rowheight * ( 4 / 3 ) ) . 'px;';
			if ( $this->rowhidden( $row, $sheet ) ) {
				$style .= 'display:none;';
			}
			$out .= "\n\t<tr style=\"$style\">";
			if ( $row_numbers ) {
				$out .= "\n\t\t<th>{$row}</th>";
			}
			for ( $col = 1; $col <= $this->colcount( $sheet ); $col++ ) {
				// Account for Rowspans/Colspans
				$rowspan = $this->rowspan( $row, $col, $sheet );
				$colspan = $this->colspan( $row, $col, $sheet );
				for ( $i = 0; $i < $rowspan; $i++ ) {
					for ( $j = 0; $j < $colspan; $j++ ) {
						if ( $i > 0 || $j > 0 ) {
							$this->sheets[ $sheet ]['cellsInfo'][ $row + $i ][ $col + $j ]['dontprint'] = 1;
						}
					}
				}
				if ( ! $this->sheets[ $sheet ]['cellsInfo'][ $row ][ $col ]['dontprint'] ) {
					$style = $this->style( $row, $col, $sheet );
					if ( $this->colhidden( $col, $sheet ) ) {
						$style .= 'display:none;';
					}
					$out .= "\n\t\t<td style=\"$style\"" . ( $colspan > 1 ? " colspan={$colspan}" : '' ) . ( $rowspan > 1 ? " rowspan={$rowspan}" : '' ) . '>';
					$val = $this->val( $row, $col, $sheet );
					if ( '' === $val ) {
						$val = '&nbsp;';
					} else {
						$val = htmlentities( $val );
						$link = $this->hyperlink( $row, $col, $sheet );
						if ( '' !== $link ) {
							$val = "<a href=\"$link\">{$val}</a>";
						}
					}
					$out .= '<nobr>' . nl2br( $val ) . '</nobr>';
					$out .= '</td>';
				}
			}
			$out .= "</tr>\n";
		}
		$out .= '</tbody></table>';
		return $out;
	}

	// --------------
	// END PUBLIC API

	protected $boundsheets = array();
	protected $formatRecords = array();
	protected $fontRecords = array();
	protected $xfRecords = array();
	protected $colInfo = array();
	protected $rowInfo = array();

	protected $sst = array();
	protected $sheets = array();

	protected $data;
	protected $_ole;
	protected $_defaultEncoding = 'UTF-8';
	protected $_defaultFormat = SPREADSHEET_EXCEL_READER_DEF_NUM_FORMAT;
	protected $_columnsFormat = array();
	protected $_rowoffset = 1;
	protected $_coloffset = 1;

	/**
	 * List of default date formats used by Excel
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $dateFormats = array(
		0xe  => 'm/d/Y',
		0xf  => 'M-d-Y',
		0x10 => 'd-M',
		0x11 => 'M-Y',
		0x12 => 'h:i a',
		0x13 => 'h:i:s a',
		0x14 => 'H:i',
		0x15 => 'H:i:s',
		0x16 => 'd/m/Y H:i',
		0x2d => 'i:s',
		0x2e => 'H:i:s',
		0x2f => 'i:s.S',
	);

	/**
	 * Default number formats used by Excel
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $numberFormats = array(
		0x1  => '0',
		0x2  => '0.00',
		0x3  => '#,##0',
		0x4  => '#,##0.00',
		0x5  => '\$#,##0;(\$#,##0)',
		0x6  => '\$#,##0;[Red](\$#,##0)',
		0x7  => '\$#,##0.00;(\$#,##0.00)',
		0x8  => '\$#,##0.00;[Red](\$#,##0.00)',
		0x9  => '0%',
		0xa  => '0.00%',
		0xb  => '0.00E+00',
		0x25 => '#,##0;(#,##0)',
		0x26 => '#,##0;[Red](#,##0)',
		0x27 => '#,##0.00;(#,##0.00)',
		0x28 => '#,##0.00;[Red](#,##0.00)',
		0x29 => '#,##0;(#,##0)',  // Not exact
		0x2a => '\$#,##0;(\$#,##0)',  // Not exact
		0x2b => '#,##0.00;(#,##0.00)', // Not exact
		0x2c => '\$#,##0.00;(\$#,##0.00)', // Not exact
		0x30 => '##0.0E+0',
	);

	/**
	 * [$colors description]
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $colors = array(
		0x00   => '#000000',
		0x01   => '#FFFFFF',
		0x02   => '#FF0000',
		0x03   => '#00FF00',
		0x04   => '#0000FF',
		0x05   => '#FFFF00',
		0x06   => '#FF00FF',
		0x07   => '#00FFFF',
		0x08   => '#000000',
		0x09   => '#FFFFFF',
		0x0A   => '#FF0000',
		0x0B   => '#00FF00',
		0x0C   => '#0000FF',
		0x0D   => '#FFFF00',
		0x0E   => '#FF00FF',
		0x0F   => '#00FFFF',
		0x10   => '#800000',
		0x11   => '#008000',
		0x12   => '#000080',
		0x13   => '#808000',
		0x14   => '#800080',
		0x15   => '#008080',
		0x16   => '#C0C0C0',
		0x17   => '#808080',
		0x18   => '#9999FF',
		0x19   => '#993366',
		0x1A   => '#FFFFCC',
		0x1B   => '#CCFFFF',
		0x1C   => '#660066',
		0x1D   => '#FF8080',
		0x1E   => '#0066CC',
		0x1F   => '#CCCCFF',
		0x20   => '#000080',
		0x21   => '#FF00FF',
		0x22   => '#FFFF00',
		0x23   => '#00FFFF',
		0x24   => '#800080',
		0x25   => '#800000',
		0x26   => '#008080',
		0x27   => '#0000FF',
		0x28   => '#00CCFF',
		0x29   => '#CCFFFF',
		0x2A   => '#CCFFCC',
		0x2B   => '#FFFF99',
		0x2C   => '#99CCFF',
		0x2D   => '#FF99CC',
		0x2E   => '#CC99FF',
		0x2F   => '#FFCC99',
		0x30   => '#3366FF',
		0x31   => '#33CCCC',
		0x32   => '#99CC00',
		0x33   => '#FFCC00',
		0x34   => '#FF9900',
		0x35   => '#FF6600',
		0x36   => '#666699',
		0x37   => '#969696',
		0x38   => '#003366',
		0x39   => '#339966',
		0x3A   => '#003300',
		0x3B   => '#333300',
		0x3C   => '#993300',
		0x3D   => '#993366',
		0x3E   => '#333399',
		0x3F   => '#333333',
		0x40   => '#000000',
		0x41   => '#FFFFFF',
		0x43   => '#000000',
		0x4D   => '#000000',
		0x4E   => '#FFFFFF',
		0x4F   => '#000000',
		0x50   => '#FFFFFF',
		0x51   => '#000000',
		0x7FFF => '#000000',
	);

	/**
	 * [$lineStyles description]
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $lineStyles = array(
		0x00 => '',
		0x01 => 'Thin',
		0x02 => 'Medium',
		0x03 => 'Dashed',
		0x04 => 'Dotted',
		0x05 => 'Thick',
		0x06 => 'Double',
		0x07 => 'Hair',
		0x08 => 'Medium dashed',
		0x09 => 'Thin dash-dotted',
		0x0A => 'Medium dash-dotted',
		0x0B => 'Thin dash-dot-dotted',
		0x0C => 'Medium dash-dot-dotted',
		0x0D => 'Slanted medium dash-dotted',
	);

	/**
	 * [$lineStylesCss description]
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $lineStylesCss = array(
		'Thin'                       => '1px solid',
		'Medium'                     => '2px solid',
		'Dashed'                     => '1px dashed',
		'Dotted'                     => '1px dotted',
		'Thick'                      => '3px solid',
		'Double'                     => 'double',
		'Hair'                       => '1px solid',
		'Medium dashed'              => '2px dashed',
		'Thin dash-dotted'           => '1px dashed',
		'Medium dash-dotted'         => '2px dashed',
		'Thin dash-dot-dotted'       => '1px dashed',
		'Medium dash-dot-dotted'     => '2px dashed',
		'Slanted medium dash-dotted' => '2px dashed',
	);

	/**
	 * [read16bitstring description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $data  [description]
	 * @param [type] $start [description]
	 * @return [type] [description]
	 */
	protected function read16bitstring( $data, $start ) {
		$len = 0;
		while ( ord( $data[ $start + $len ] ) + ord( $data[ $start + $len + 1 ] ) > 0 ) {
			$len++;
		}
		return substr( $data, $start, $len );
	}

	/**
	 * [_format_value description]
	 * ADDED by Matt Kruse for better formatting
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $format [description]
	 * @param [type] $num    [description]
	 * @param [type] $f      [description]
	 * @return [type] [description]
	 */
	protected function _format_value( $format, $num, $f ) {
		// 49 = TEXT format
		// https://code.google.com/archive/p/php-excel-reader/issues/7
		if ( ( ! $f && '%s' === $format ) || ( 49 === (int) $f ) || ( 'GENERAL' === $format ) ) {
			return array(
				'string'      => $num,
				'formatColor' => null,
			);
		}

		// Custom pattern can be POSITIVE;NEGATIVE;ZERO
		// The "text" option as 4th parameter is not handled
		$parts = explode( ';', $format );
		$pattern = $parts[0];
		// Negative pattern
		if ( count( $parts ) > 2 && 0 === (int) $num ) {
			$pattern = $parts[2];
		}
		// Zero pattern
		if ( count( $parts ) > 1 && $num < 0 ) {
			$pattern = $parts[1];
			$num = abs( $num );
		}

		$color = '';
		$matches = array();
		$color_regex = '/^\[(BLACK|BLUE|CYAN|GREEN|MAGENTA|RED|WHITE|YELLOW)\]/i';
		if ( preg_match( $color_regex, $pattern, $matches ) ) {
			$color = strtolower( $matches[1] );
			$pattern = preg_replace( $color_regex, '', $pattern );
		}

		// In Excel formats, "_" is used to add spacing, which we can't do in HTML.
		$pattern = preg_replace( '/_./', '', $pattern );

		// Some non-number characters are escaped with \, which we don't need.
		$pattern = preg_replace( '/\\\/', '', $pattern );

		// Some non-number strings are quoted, so we'll get rid of the quotes.
		$pattern = preg_replace( '/"/', '', $pattern );

		// TEMPORARY - Convert # to 0.
		$pattern = preg_replace( '/\#/', '0', $pattern );

		// Find out if we need comma formatting.
		$has_commas = preg_match( '/,/', $pattern );
		if ( $has_commas ) {
			$pattern = preg_replace( '/,/', '', $pattern );
		}

		// Handle Percentages.
		if ( preg_match( '/\d(\%)([^\%]|$)/', $pattern, $matches ) ) {
			$num *= 100;
			$pattern = preg_replace( '/(\d)(\%)([^\%]|$)/', '$1%$3', $pattern );
		}

		// Handle the number itself.
		$number_regex = '/(\d+)(\.?)(\d*)/';
		if ( preg_match( $number_regex, $pattern, $matches ) ) {
			//$left = $matches[1];
			//$dec = $matches[2];
			$right = $matches[3];
			if ( $has_commas ) {
				$formatted = number_format( $num, strlen( $right ) );
			} else {
				$sprintf_pattern = '%1.' . strlen( $right ) . 'f';
				$formatted = sprintf( $sprintf_pattern, $num );
			}
			$pattern = preg_replace( $number_regex, $formatted, $pattern );
		}

		return array(
			'string'      => $pattern,
			'formatColor' => $color,
		);
	}

	/**
	 * [__construct description]
	 *
	 * @since 1.0.0
	 *
	 * @param string $data                Optional. [description]
	 * @param bool   $store_extended_info Optional. [description]
	 * @param string $outputEncoding      Optional. [description]
	 */
	public function __construct( $data = '', $store_extended_info = false, $outputEncoding = '' ) {
		$this->_ole = new OLERead();
		$this->setUTFEncoder( 'iconv' );
		if ( '' !== $outputEncoding ) {
			$this->setOutputEncoding( $outputEncoding );
		}
		for ( $i = 1; $i < 245; $i++ ) {
			$name = strtolower( ( ( ( $i - 1 ) / 26 >= 1 ) ? chr( ( $i - 1 ) / 26 + 64 ) : '' ) . chr( ( $i - 1 ) % 26 + 65 ) );
			$this->colnames[ $name ] = $i;
			$this->colindexes[ $i ] = $name;
		}
		$this->store_extended_info = $store_extended_info;
		if ( '' !== $data ) {
			$this->read( $data );
		}
	}

	/**
	 * Set the encoding method.
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $encoding [description]
	 */
	public function setOutputEncoding( $encoding ) {
		$this->_defaultEncoding = $encoding;
	}

	/**
	 * [setUTFEncoder description]
	 * $encoder = 'iconv' or 'mb'
	 * set iconv if you would like use 'iconv' for encode UTF-16LE to your encoding
	 * set mb if you would like use 'mb_convert_encoding' for encode UTF-16LE to your encoding
	 *
	 * @since 1.0.0
	 *
	 * @param string $encoder Optional. [description]
	 */
	public function setUTFEncoder( $encoder = 'iconv' ) {
		$this->_encoderFunction = '';
		if ( 'iconv' === $encoder ) {
			$this->_encoderFunction = function_exists( 'iconv' ) ? 'iconv' : '';
		} elseif ( 'mb' === $encoder ) {
			$this->_encoderFunction = function_exists( 'mb_convert_encoding' ) ? 'mb_convert_encoding' : '';
		}
	}

	/**
	 * [setRowColOffset description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $iOffset [description]
	 */
	public function setRowColOffset( $iOffset ) {
		$this->_rowoffset = $iOffset;
		$this->_coloffset = $iOffset;
	}

	/**
	 * Set the default number format.
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $sFormat [description]
	 */
	public function setDefaultFormat( $sFormat ) {
		$this->_defaultFormat = $sFormat;
	}

	/**
	 * Force a column to use a certain format.
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $column  [description]
	 * @param [type] $sFormat [description]
	 */
	public function setColumnFormat( $column, $sFormat ) {
		$this->_columnsFormat[ $column ] = $sFormat;
	}

	/**
	 * Read the spreadsheet file using OLE, then parse.
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $data [description]
	 * @return [type] [description]
	 */
	public function read( $data ) {
		$res = $this->_ole->read( $data );

		// oops, something goes wrong (Darko Miljanovic)
		if ( false === $res ) {
			// check error code
			if ( 1 === $this->_ole->error ) {
				die( 'Data is not readable' );
			} elseif ( 2 === $this->_ole->error ) {
				die( 'OLE error' );
			}
			// check other error codes here (e.g. bad fileformat, etc...)
		}
		$this->data = $this->_ole->getWorkBook();
		$this->_parse();
	}

	/**
	 * Parse a workbook.
	 *
	 * @since 1.0.0
	 *
	 * @return [type] [description]
	 */
	protected function _parse() {
		$pos = 0;
		$data = $this->data;

		//$code = $this->v( $data, $pos );
		$length = $this->v( $data, $pos + 2 );
		$version = $this->v( $data, $pos + 4 );
		$substreamType = $this->v( $data, $pos + 6 );
		if ( SPREADSHEET_EXCEL_READER_BIFF8 !== $version && SPREADSHEET_EXCEL_READER_BIFF7 !== $version ) {
			return false;
		}

		if ( SPREADSHEET_EXCEL_READER_WORKBOOKGLOBALS !== $substreamType ) {
			return false;
		}

		$pos += $length + 4;

		$code = $this->v( $data, $pos );
		$length = $this->v( $data, $pos + 2 );

		while ( SPREADSHEET_EXCEL_READER_TYPE_EOF !== $code ) {
			switch ( $code ) {
				case SPREADSHEET_EXCEL_READER_TYPE_SST:
					$spos = $pos + 4;
					$limitpos = $spos + $length;
					$uniqueStrings = $this->_GetInt4d( $data, $spos + 4 );
					$spos += 8;
					for ( $i = 0; $i < $uniqueStrings; $i++ ) {
						// Read in the number of characters
						if ( $spos === $limitpos ) {
							$opcode = $this->v( $data, $spos );
							$conlength = $this->v( $data, $spos + 2 );
							if ( 0x3c !== $opcode ) {
								return -1;
							}
							$spos += 4;
							$limitpos = $spos + $conlength;
						}
						$numChars = ord( $data[ $spos ] ) | ( ord( $data[ $spos + 1 ] ) << 8 );
						$spos += 2;
						$optionFlags = ord( $data[ $spos ] );
						$spos++;
						$asciiEncoding = ( 0 === ( $optionFlags & 0x01 ) );
						$extendedString = ( 0 !== ( $optionFlags & 0x04 ) );

						// See if string contains formatting information.
						$richString = ( 0 !== ( $optionFlags & 0x08 ) );

						if ( $richString ) {
							// Read in the crun
							$formattingRuns = $this->v( $data, $spos );
							$spos += 2;
						}

						if ( $extendedString ) {
							// Read in cchExtRst
							$extendedRunLength = $this->_GetInt4d( $data, $spos );
							$spos += 4;
						}

						$len = ( $asciiEncoding ) ? $numChars : $numChars * 2;
						if ( $spos + $len < $limitpos ) {
							$retstr = substr( $data, $spos, $len );
							$spos += $len;
						} else {
							// found continue
							$retstr = substr( $data, $spos, $limitpos - $spos );
							$bytesRead = $limitpos - $spos;
							$charsLeft = $numChars - ( ( $asciiEncoding ) ? $bytesRead : ( $bytesRead / 2 ) );
							$spos = $limitpos;

							while ( $charsLeft > 0 ) {
								$opcode = $this->v( $data, $spos );
								$conlength = $this->v( $data, $spos + 2 );
								if ( 0x3c !== $opcode ) {
									return -1;
								}
								$spos += 4;
								$limitpos = $spos + $conlength;
								$option = ord( $data[ $spos ] );
								$spos += 1;
								if ( $asciiEncoding && 0 === $option ) {
									$len = min( $charsLeft, $limitpos - $spos ); // min( $charsLeft, $conlength );
									$retstr .= substr( $data, $spos, $len );
									$charsLeft -= $len;
									$asciiEncoding = true;
								} elseif ( ! $asciiEncoding && 0 !== $option ) {
									$len = min( $charsLeft * 2, $limitpos - $spos ); // min( $charsLeft, $conlength );
									$retstr .= substr( $data, $spos, $len );
									$charsLeft -= $len / 2;
									$asciiEncoding = false;
								} elseif ( ! $asciiEncoding && 0 === $option ) {
									// Bummer - the string starts off as Unicode, but after the
									// continuation it is in straightforward ASCII encoding
									$len = min( $charsLeft, $limitpos - $spos ); // min( $charsLeft, $conlength );
									for ( $j = 0; $j < $len; $j++ ) {
										$retstr .= $data[ $spos + $j ] . chr( 0 );
									}
									$charsLeft -= $len;
									$asciiEncoding = false;
								} else {
									$newstr = '';
									for ( $j = 0; $j < strlen( $retstr ); $j++ ) {
										$newstr = $retstr[ $j ] . chr( 0 );
									}
									$retstr = $newstr;
									$len = min( $charsLeft * 2, $limitpos - $spos ); // min( $charsLeft, $conlength );
									$retstr .= substr( $data, $spos, $len );
									$charsLeft -= $len / 2;
									$asciiEncoding = false;
								}
								$spos += $len;
							}
						}
						$retstr = ( $asciiEncoding ) ? $retstr : $this->_encodeUTF16( $retstr );

						if ( $richString ) {
							$spos += 4 * $formattingRuns;
						}

						// For extended strings, skip over the extended string data
						if ( $extendedString ) {
							$spos += $extendedRunLength;
						}
						$this->sst[] = $retstr;
					}
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_FILEPASS:
					return false;
					// break; // unreachable
				case SPREADSHEET_EXCEL_READER_TYPE_NAME:
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_FORMAT:
					$indexCode = $this->v( $data, $pos + 4 );
					if ( SPREADSHEET_EXCEL_READER_BIFF8 === $version ) {
						$numchars = $this->v( $data, $pos + 6 );
						if ( 0 === ord( $data[ $pos + 8 ] ) ) {
							$formatString = substr( $data, $pos + 9, $numchars );
						} else {
							$formatString = substr( $data, $pos + 9, $numchars * 2 );
						}
					} else {
						$numchars = ord( $data[ $pos + 6 ] );
						$formatString = substr( $data, $pos + 7, $numchars * 2 );
					}
					$this->formatRecords[ $indexCode ] = $formatString;
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_FONT:
					$height = $this->v( $data, $pos + 4 );
					$option = $this->v( $data, $pos + 6 );
					$color = $this->v( $data, $pos + 8 );
					$weight = $this->v( $data, $pos + 10 );
					$under = ord( $data[ $pos + 14 ] );
					// Font name
					$numchars = ord( $data[ $pos + 18 ] );
					if ( 0 === ( ord( $data[ $pos + 19 ] ) & 1 ) ) {
						$font = substr( $data, $pos + 20, $numchars );
					} else {
						$font = substr( $data, $pos + 20, $numchars * 2 );
						$font = $this->_encodeUTF16( $font );
					}
					$this->fontRecords[] = array(
						'height' => $height / 20,
						'italic' => (bool) ( $option & 2 ),
						'color'  => $color,
						'under'  => ( 0 !== $under ),
						'bold'   => ( 700 === $weight ),
						'font'   => $font,
						'raw'    => $this->dumpHexData( $data, $pos + 3, $length ),
					);
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_PALETTE:
					$colors = ord( $data[ $pos + 4 ] ) | ord( $data[ $pos + 5 ] ) << 8;
					for ( $coli = 0; $coli < $colors; $coli++ ) {
						$colOff = $pos + 2 + ( $coli * 4 );
						$colr = ord( $data[ $colOff ] );
						$colg = ord( $data[ $colOff + 1 ] );
						$colb = ord( $data[ $colOff + 2 ] );
						$this->colors[ 0x07 + $coli ] = '#' . $this->myhex( $colr ) . $this->myhex( $colg ) . $this->myhex( $colb );
					}
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_XF:
					$fontIndexCode = ( ord( $data[ $pos + 4 ] ) | ord( $data[ $pos + 5 ] ) << 8 ) - 1;
					$fontIndexCode = max( 0, $fontIndexCode );
					$indexCode = ord( $data[ $pos + 6 ] ) | ord( $data[ $pos + 7 ] ) << 8;
					$alignbit = ord( $data[ $pos + 10 ] ) & 3;
					$bgi = ( ord( $data[ $pos + 22 ] ) | ord( $data[ $pos + 23 ] ) << 8 ) & 0x3FFF;
					$bgcolor = ( $bgi & 0x7F );
					// $bgcolor = ( $bgi & 0x3f80 ) >> 7;
					$align = '';
					if ( 3 === $alignbit ) {
						$align = 'right';
					} elseif ( 2 === $alignbit ) {
						$align = 'center';
					}
					$fillPattern = ( ord( $data[ $pos + 21 ] ) & 0xFC ) >> 2;
					if ( 0 === $fillPattern ) {
						$bgcolor = '';
					}

					$xf = array();
					$xf['formatIndex'] = $indexCode;
					$xf['align'] = $align;
					$xf['fontIndex'] = $fontIndexCode;
					$xf['bgColor'] = $bgcolor;
					$xf['fillPattern'] = $fillPattern;

					$border = ord( $data[ $pos + 14 ] ) | ( ord( $data[ $pos + 15 ] ) << 8 ) | ( ord( $data[ $pos + 16 ] ) << 16 ) | ( ord( $data[ $pos + 17 ] ) << 24 );
					$xf['borderLeft'] = $this->lineStyles[ ( $border & 0xF ) ];
					$xf['borderRight'] = $this->lineStyles[ ( $border & 0xF0 ) >> 4 ];
					$xf['borderTop'] = $this->lineStyles[ ( $border & 0xF00 ) >> 8 ];
					$xf['borderBottom'] = $this->lineStyles[ ( $border & 0xF000 ) >> 12 ];

					$xf['borderLeftColor'] = ( $border & 0x7F0000 ) >> 16;
					$xf['borderRightColor'] = ( $border & 0x3F800000 ) >> 23;
					$border = ( ord( $data[ $pos + 18 ] ) | ord( $data[ $pos + 19 ] ) << 8 );
					$xf['borderTopColor'] = ( $border & 0x7F );
					$xf['borderBottomColor'] = ( $border & 0x3F80 ) >> 7;
					if ( array_key_exists( $indexCode, $this->dateFormats ) ) {
						$xf['type'] = 'date';
						$xf['format'] = $this->dateFormats[ $indexCode ];
						if ( '' === $align ) {
							$xf['align'] = 'right';
						}
					} elseif ( array_key_exists( $indexCode, $this->numberFormats ) ) {
						$xf['type'] = 'number';
						$xf['format'] = $this->numberFormats[ $indexCode ];
						if ( '' === $align ) {
							$xf['align'] = 'right';
						}
					} else {
						$isdate = false;
						$formatstr = '';
						if ( $indexCode > 0 ) {
							if ( isset( $this->formatRecords[ $indexCode ] ) ) {
								$formatstr = $this->formatRecords[ $indexCode ];
							}
							if ( '' !== $formatstr ) {
								$tmp = preg_replace( '/\;.*/', '', $formatstr );
								$tmp = preg_replace( '/^\[[^\]]*\]/', '', $tmp );
								if ( 0 === preg_match( '/[^hmsday\/\-:\s\\\,AMP]/i', $tmp ) ) { // found day and time format
									$isdate = true;
									$formatstr = $tmp;
									$formatstr = str_replace( array( 'AM/PM', 'mmmm', 'mmm' ), array( 'a', 'F', 'M' ), $formatstr );
									// m/mm are used for both minutes and months - oh SNAP!
									// This mess tries to fix for that.
									// 'm' = minutes only if following h/hh or preceding s/ss
									$formatstr = preg_replace( '/(h:?)mm?/', '$1i', $formatstr );
									$formatstr = preg_replace( '/mm?(:?s)/', '1$1', $formatstr );
									// A single 'm' = n in PHP
									$formatstr = preg_replace( '/(^|[^m])m([^m]|$)/', '$1n$2', $formatstr );
									$formatstr = preg_replace( '/(^|[^m])m([^m]|$)/', '$1n$2', $formatstr );
									// else it's months
									$formatstr = str_replace( 'mm', 'm', $formatstr );
									// Convert single 'd' to 'j'
									$formatstr = preg_replace( '/(^|[^d])d([^d]|$)/', '$1j$2', $formatstr );
									$formatstr = str_replace( array( 'dddd', 'ddd', 'dd', 'yyyy', 'yy', 'hh', 'h' ), array( 'l', 'D', 'd', 'Y', 'y', 'H', 'g' ), $formatstr );
									$formatstr = preg_replace( '/ss?/', 's', $formatstr );
								}
							}
						}
						if ( $isdate ) {
							$xf['type'] = 'date';
							$xf['format'] = $formatstr;
							if ( '' === $align ) {
								$xf['align'] = 'right';
							}
						} else {
							// If the format string has a 0 or # in it, we'll assume it's a number.
							if ( preg_match( '/[0#]/', $formatstr ) ) {
								$xf['type'] = 'number';
								if ( '' === $align ) {
									$xf['align'] = 'right';
								}
							} else {
								$xf['type'] = 'other';
							}
							$xf['format'] = $formatstr;
							$xf['code'] = $indexCode;
						}
					}
					$this->xfRecords[] = $xf;
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_NINETEENFOUR:
					$this->nineteenFour = ( 1 === ord( $data[ $pos + 4 ] ) );
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_BOUNDSHEET:
					$rec_offset = $this->_GetInt4d( $data, $pos + 4 );
					//$rec_typeFlag = ord( $data[ $pos + 8 ] );
					//$rec_visibilityFlag = ord( $data[ $pos + 9 ] );
					$rec_length = ord( $data[ $pos + 10 ] );

					if ( SPREADSHEET_EXCEL_READER_BIFF8 === $version ) {
						$chartype = ord( $data[ $pos + 11 ] );
						if ( 0 === $chartype ) {
							$rec_name = substr( $data, $pos + 12, $rec_length );
						} else {
							$rec_name = $this->_encodeUTF16( substr( $data, $pos + 12, 2 * $rec_length ) );
						}
					} elseif ( SPREADSHEET_EXCEL_READER_BIFF7 === $version ) {
						$rec_name = substr( $data, $pos + 11, $rec_length );
					}
					$this->boundsheets[] = array(
						'name'   => $rec_name,
						'offset' => $rec_offset,
					);
					break;
			} // switch

			$pos += $length + 4;
			$code = ord( $data[ $pos ] ) | ord( $data[ $pos + 1 ] ) << 8;
			$length = ord( $data[ $pos + 2 ] ) | ord( $data[ $pos + 3 ] ) << 8;
		} // while

		foreach ( $this->boundsheets as $key => $val ) {
			$this->sn = $key;
			$this->_parsesheet( $val['offset'] );
		}
		return true;
	}

	/**
	 * Parse a worksheet.
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $spos [description]
	 * @return [type] [description]
	 */
	protected function _parsesheet( $spos ) {
		$cont = true;
		$data = $this->data;
		// read BOF
		// $code = ord( $data[ $spos ] ) | ord( $data[ $spos + 1 ] ) << 8;
		$length = ord( $data[ $spos + 2 ] ) | ord( $data[ $spos + 3 ] ) << 8;

		$version = ord( $data[ $spos + 4 ] ) | ord( $data[ $spos + 5 ] ) << 8;
		$substreamType = ord( $data[ $spos + 6 ] ) | ord( $data[ $spos + 7 ] ) << 8;

		if ( SPREADSHEET_EXCEL_READER_BIFF8 !== $version && SPREADSHEET_EXCEL_READER_BIFF7 !== $version ) {
			return -1;
		}

		if ( SPREADSHEET_EXCEL_READER_WORKSHEET !== $substreamType ) {
			return -2;
		}

		$spos += $length + 4;
		while ( $cont ) {
			$lowcode = ord( $data[ $spos ] );
			if ( SPREADSHEET_EXCEL_READER_TYPE_EOF === $lowcode ) {
				break;
			}
			$code = $lowcode | ord( $data[ $spos + 1 ] ) << 8;
			$length = ord( $data[ $spos + 2 ] ) | ord( $data[ $spos + 3 ] ) << 8;
			$spos += 4;
			$this->sheets[ $this->sn ]['maxrow'] = $this->_rowoffset - 1;
			$this->sheets[ $this->sn ]['maxcol'] = $this->_coloffset - 1;
			unset( $this->rectype );
			switch ( $code ) {
				case SPREADSHEET_EXCEL_READER_TYPE_DIMENSION:
					if ( ! isset( $this->numRows ) ) {
						if ( 10 === $length || SPREADSHEET_EXCEL_READER_BIFF7 === $version ) {
							$this->sheets[ $this->sn ]['numRows'] = ord( $data[ $spos + 2 ] ) | ord( $data[ $spos + 3 ] ) << 8;
							$this->sheets[ $this->sn ]['numCols'] = ord( $data[ $spos + 6 ] ) | ord( $data[ $spos + 7 ] ) << 8;
						} else {
							$this->sheets[ $this->sn ]['numRows'] = ord( $data[ $spos + 4 ] ) | ord( $data[ $spos + 5 ] ) << 8;
							$this->sheets[ $this->sn ]['numCols'] = ord( $data[ $spos + 10 ] ) | ord( $data[ $spos + 11 ] ) << 8;
						}
					}
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_MERGEDCELLS:
					$cellRanges = ord( $data[ $spos ] ) | ord( $data[ $spos + 1 ] ) << 8;
					for ( $i = 0; $i < $cellRanges; $i++ ) {
						$fr = ord( $data[ $spos + 8 * $i + 2 ] ) | ord( $data[ $spos + 8 * $i + 3 ] ) << 8;
						$lr = ord( $data[ $spos + 8 * $i + 4 ] ) | ord( $data[ $spos + 8 * $i + 5 ] ) << 8;
						$fc = ord( $data[ $spos + 8 * $i + 6 ] ) | ord( $data[ $spos + 8 * $i + 7 ] ) << 8;
						$lc = ord( $data[ $spos + 8 * $i + 8 ] ) | ord( $data[ $spos + 8 * $i + 9 ] ) << 8;
						if ( $lr - $fr > 0 ) {
							$this->sheets[ $this->sn ]['cellsInfo'][ $fr + 1 ][ $fc + 1 ]['rowspan'] = $lr - $fr + 1;
						}
						if ( $lc - $fc > 0 ) {
							$this->sheets[ $this->sn ]['cellsInfo'][ $fr + 1 ][ $fc + 1 ]['colspan'] = $lc - $fc + 1;
						}
					}
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_RK:
				case SPREADSHEET_EXCEL_READER_TYPE_RK2:
					$row = ord( $data[ $spos ] ) | ord( $data[ $spos + 1 ] ) << 8;
					$column = ord( $data[ $spos + 2 ] ) | ord( $data[ $spos + 3 ] ) << 8;
					$rknum = $this->_GetInt4d( $data, $spos + 6 );
					$numValue = $this->_GetIEEE754( $rknum );
					$info = $this->_getCellDetails( $spos, $numValue, $column );
					$this->addcell( $row, $column, $info['string'], $info );
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_LABELSST:
					$row = ord( $data[ $spos ] ) | ord( $data[ $spos + 1 ] ) << 8;
					$column = ord( $data[ $spos + 2 ] ) | ord( $data[ $spos + 3 ] ) << 8;
					$xfindex = ord( $data[ $spos + 4 ] ) | ord( $data[ $spos + 5 ] ) << 8;
					$index = $this->_GetInt4d( $data, $spos + 6 );
					$this->addcell( $row, $column, $this->sst[ $index ], array( 'xfIndex' => $xfindex ) );
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_MULRK:
					$row = ord( $data[ $spos ] ) | ord( $data[ $spos + 1 ] ) << 8;
					$colFirst = ord( $data[ $spos + 2 ] ) | ord( $data[ $spos + 3 ] ) << 8;
					$colLast = ord( $data[ $spos + $length - 2 ] ) | ord( $data[ $spos + $length - 1 ] ) << 8;
					$columns = $colLast - $colFirst + 1;
					$tmppos = $spos + 4;
					for ( $i = 0; $i < $columns; $i++ ) {
						$numValue = $this->_GetIEEE754( $this->_GetInt4d( $data, $tmppos + 2 ) );
						$info = $this->_getCellDetails( $tmppos - 4, $numValue, $colFirst + $i + 1 );
						$tmppos += 6;
						$this->addcell( $row, $colFirst + $i, $info['string'], $info );
					}
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_NUMBER:
					$row = ord( $data[ $spos ] ) | ord( $data[ $spos + 1 ] ) << 8;
					$column = ord( $data[ $spos + 2 ] ) | ord( $data[ $spos + 3 ] ) << 8;
					$tmp = unpack( 'ddouble', substr( $data, $spos + 6, 8 ) ); // It machine machine dependent
					if ( $this->isDate( $spos ) ) {
						$numValue = $tmp['double'];
					} else {
						$numValue = $this->createNumber( $spos );
					}
					$info = $this->_getCellDetails( $spos, $numValue, $column );
					$this->addcell( $row, $column, $info['string'], $info );
					break;

				case SPREADSHEET_EXCEL_READER_TYPE_FORMULA:
				case SPREADSHEET_EXCEL_READER_TYPE_FORMULA2:
					$row = ord( $data[ $spos ] ) | ord( $data[ $spos + 1 ] ) << 8;
					$column = ord( $data[ $spos + 2 ] ) | ord( $data[ $spos + 3 ] ) << 8;
					if ( 0 === ord( $data[ $spos + 6 ] ) && 255 === ord( $data[ $spos + 12 ] ) && 255 === ord( $data[ $spos + 13 ] ) ) {
						// String formula. Result follows in a STRING record
						// This row/col are stored to be referenced in that record
						// https://code.google.com/archive/p/php-excel-reader/issues/4
						$previousRow = $row;
						$previousCol = $column;
					} elseif ( 1 === ord( $data[ $spos + 6 ] ) && 255 === ord( $data[ $spos + 12 ] ) && 255 === ord( $data[ $spos + 13 ] ) ) {
						// Boolean formula. Result is in +2; 0 = false,1 = true
						// https://code.google.com/archive/p/php-excel-reader/issues/4
						if ( 1 === ord( $this->data[ $spos + 8 ] ) ) {
							$this->addcell( $row, $column, 'TRUE' );
						} else {
							$this->addcell( $row, $column, 'FALSE' );
						}
					} elseif ( 2 === ord( $data[ $spos + 6 ] ) && 255 === ord( $data[ $spos + 12 ] ) && 255 === ord( $data[ $spos + 13 ] ) ) {
						// Error formula. Error code is in +2;
					} elseif ( 3 === ord( $data[ $spos + 6 ] ) && 255 === ord( $data[ $spos + 12 ] ) && 255 === ord( $data[ $spos + 13 ] ) ) {
						// Formula result is a null string.
						$this->addcell( $row, $column, '' );
					} else {
						// Result is a number, so first 14 bytes are just like a _NUMBER record
						$tmp = unpack( 'ddouble', substr( $data, $spos + 6, 8 ) ); // machine dependent
						if ( $this->isDate( $spos ) ) {
							$numValue = $tmp['double'];
						} else {
							$numValue = $this->createNumber( $spos );
						}
						$info = $this->_getCellDetails( $spos, $numValue, $column );
						$this->addcell( $row, $column, $info['string'], $info );
					}
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_BOOLERR:
					$row = ord( $data[ $spos ] ) | ord( $data[ $spos + 1 ] ) << 8;
					$column = ord( $data[ $spos + 2 ] ) | ord( $data[ $spos + 3 ] ) << 8;
					$string = ord( $data[ $spos + 6 ] );
					$this->addcell( $row, $column, $string );
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_STRING:
					// https://code.google.com/archive/p/php-excel-reader/issues/4
					if ( SPREADSHEET_EXCEL_READER_BIFF8 === $version ) {
						// Unicode 16 string, like an SST record
						$xpos = $spos;
						$numChars = ord( $data[ $xpos ] ) | ( ord( $data[ $xpos + 1 ] ) << 8 );
						$xpos += 2;
						$optionFlags = ord( $data[ $xpos ] );
						$xpos++;
						$asciiEncoding = ( 0 === ( $optionFlags & 0x01 ) );
						$extendedString = ( 0 !== ( $optionFlags & 0x04 ) );
						// See if string contains formatting information
						$richString = ( 0 !== ( $optionFlags & 0x08 ) );
						if ( $richString ) {
							// Read in the crun
							// $formattingRuns = ord( $data[ $xpos ] ) | ( ord( $data[ $xpos + 1 ] ) << 8 );
							$xpos += 2;
						}
						if ( $extendedString ) {
							// Read in cchExtRst
							// $extendedRunLength =$this->_GetInt4d( $this->data, $xpos );
							$xpos += 4;
						}
						$len = ( $asciiEncoding ) ? $numChars : $numChars * 2;
						$retstr = substr( $data, $xpos, $len );
						$xpos += $len;
						$retstr = ( $asciiEncoding ) ? $retstr : $this->_encodeUTF16( $retstr );
					} elseif ( SPREADSHEET_EXCEL_READER_BIFF7 === $version ) {
						// Simple byte string
						$xpos = $spos;
						$numChars = ord( $data[ $xpos ] ) | ( ord( $data[ $xpos + 1 ] ) << 8 );
						$xpos += 2;
						$retstr = substr( $data, $xpos, $numChars );
					}
					$this->addcell( $previousRow, $previousCol, $retstr );
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_ROW:
					$row = ord( $data[ $spos ] ) | ord( $data[ $spos + 1 ] ) << 8;
					$rowInfo = ord( $data[ $spos + 6 ] ) | ( ( ord( $data[ $spos + 7 ] ) << 8 ) & 0x7FFF );
					if ( ( $rowInfo & 0x8000 ) > 0 ) {
						$rowHeight = -1;
					} else {
						$rowHeight = $rowInfo & 0x7FFF;
					}
					$rowHidden = ( ord( $data[ $spos + 12 ] ) & 0x20 ) >> 5;
					$this->rowInfo[ $this->sn ][ $row + 1 ] = array(
						'height' => $rowHeight / 20,
						'hidden' => $rowHidden,
					);
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_DBCELL:
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_MULBLANK:
					$row = ord( $data[ $spos ] ) | ord( $data[ $spos + 1 ] ) << 8;
					$column = ord( $data[ $spos + 2 ] ) | ord( $data[ $spos + 3 ] ) << 8;
					$cols = ( $length / 2 ) - 3;
					for ( $c = 0; $c < $cols; $c++ ) {
						$xfindex = ord( $data[ $spos + 4 + ( $c * 2 ) ] ) | ord( $data[ $spos + 5 + ( $c * 2 ) ] ) << 8;
						$this->addcell( $row, $column + $c, '', array( 'xfIndex' => $xfindex ) );
					}
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_LABEL:
					$row = ord( $data[ $spos ] ) | ord( $data[ $spos + 1 ] ) << 8;
					$column = ord( $data[ $spos + 2 ] ) | ord( $data[ $spos + 3 ] ) << 8;
					$this->addcell( $row, $column, substr( $data, $spos + 8, ord( $data[ $spos + 6 ] ) | ord( $data[ $spos + 7 ] ) << 8 ) );
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_EOF:
					$cont = false;
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_HYPER:
					// Only handle hyperlinks to a URL
					$row = ord( $this->data[ $spos ] ) | ord( $this->data[ $spos + 1 ] ) << 8;
					$row2 = ord( $this->data[ $spos + 2 ] ) | ord( $this->data[ $spos + 3 ] ) << 8;
					$column = ord( $this->data[ $spos + 4 ] ) | ord( $this->data[ $spos + 5 ] ) << 8;
					$column2 = ord( $this->data[ $spos + 6 ] ) | ord( $this->data[ $spos + 7 ] ) << 8;
					$linkdata = array();
					$flags = ord( $this->data[ $spos + 28 ] );
					$udesc = '';
					$ulink = '';
					$uloc = 32;
					$linkdata['flags'] = $flags;
					if ( ( $flags & 1 ) > 0 ) {   // is a type we understand
						// is there a description ?
						if ( 0x14 === ( $flags & 0x14 ) ) {   // has a description
							$uloc += 4;
							$descLen = ord( $this->data[ $spos + 32 ] ) | ord( $this->data[ $spos + 33 ] ) << 8;
							$udesc = substr( $this->data, $spos + $uloc, $descLen * 2 );
							$uloc += 2 * $descLen;
						}
						$ulink = $this->read16bitstring( $this->data, $spos + $uloc + 20 );
						if ( '' === $udesc ) {
							$udesc = $ulink;
						}
					}
					$linkdata['desc'] = $udesc;
					$linkdata['link'] = $this->_encodeUTF16( $ulink );
					for ( $r = $row; $r <= $row2; $r++ ) {
						for ( $c = $column; $c <= $column2; $c++ ) {
							$this->sheets[ $this->sn ]['cellsInfo'][ $r + 1 ][ $c + 1 ]['hyperlink'] = $linkdata;
						}
					}
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_DEFCOLWIDTH:
					$this->defaultColWidth = ord( $data[ $spos + 4 ] ) | ord( $data[ $spos + 5 ] ) << 8;
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_STANDARDWIDTH:
					$this->standardColWidth = ord( $data[ $spos + 4 ] ) | ord( $data[ $spos + 5 ] ) << 8;
					break;
				case SPREADSHEET_EXCEL_READER_TYPE_COLINFO:
					$colfrom = ord( $data[ $spos + 0 ] ) | ord( $data[ $spos + 1 ] ) << 8;
					$colto = ord( $data[ $spos + 2 ] ) | ord( $data[ $spos + 3 ] ) << 8;
					$cw = ord( $data[ $spos + 4 ] ) | ord( $data[ $spos + 5 ] ) << 8;
					$cxf = ord( $data[ $spos + 6 ] ) | ord( $data[ $spos + 7 ] ) << 8;
					$co = ord( $data[ $spos + 8 ] );
					for ( $coli = $colfrom; $coli <= $colto; $coli++ ) {
						$this->colInfo[ $this->sn ][ $coli + 1 ] = array(
							'width'     => $cw,
							'xf'        => $cxf,
							'hidden'    => ( $co & 0x01 ),
							'collapsed' => ( $co & 0x1000 ) >> 12,
						);
					}
					break;
				default:
					break;
			} // switch
			$spos += $length;
		} // while

		if ( ! isset( $this->sheets[ $this->sn ]['numRows'] ) ) {
			$this->sheets[ $this->sn ]['numRows'] = $this->sheets[ $this->sn ]['maxrow'];
		}
		if ( ! isset( $this->sheets[ $this->sn ]['numCols'] ) ) {
			$this->sheets[ $this->sn ]['numCols'] = $this->sheets[ $this->sn ]['maxcol'];
		}
	}

	/**
	 * [isDate description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $spos [description]
	 * @return bool [description]
	 */
	protected function isDate( $spos ) {
		$xfindex = ord( $this->data[ $spos + 4 ] ) | ord( $this->data[ $spos + 5 ] ) << 8;
		return ( 'date' === $this->xfRecords[ $xfindex ]['type'] );
	}
	/**
	 * [gmgetdate description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $ts Optional. [description]
	 * @return [type] [description]
	 */
	protected function gmgetdate( $ts = null ) {
		$k = array( 'seconds', 'minutes', 'hours', 'mday', 'wday', 'mon', 'year', 'yday', 'weekday', 'month', 0 );
		return array_combine( $k, explode( ':', gmdate( 's:i:G:j:w:n:Y:z:l:F:U', is_null( $ts ) ? time() : $ts ) ) );
	}

	/**
	 * Get the details for a particular cell
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $spos     [description]
	 * @param [type] $numValue [description]
	 * @param [type] $column   [description]
	 * @return [type] [description]
	 */
	protected function _getCellDetails( $spos, $numValue, $column ) {
		$xfindex = ord( $this->data[ $spos + 4 ] ) | ord( $this->data[ $spos + 5 ] ) << 8;
		$xfrecord = $this->xfRecords[ $xfindex ];
		$type = $xfrecord['type'];

		$format = $xfrecord['format'];
		$formatIndex = $xfrecord['formatIndex'];
		$fontIndex = $xfrecord['fontIndex'];
		$formatColor = '';

		if ( isset( $this->_columnsFormat[ $column + 1 ] ) ) {
			$format = $this->_columnsFormat[ $column + 1 ];
		}

		if ( 'date' === $type ) {
			// See https://groups.google.com/forum/#!topic/php-excel-reader-discuss/nD-XkNEtjhA
			$rectype = 'date';
			// Convert numeric value into a date
			$utcDays = floor( $numValue - ( $this->nineteenFour ? SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS1904 : SPREADSHEET_EXCEL_READER_UTCOFFSETDAYS ) );
			$utcValue = ( $utcDays ) * SPREADSHEET_EXCEL_READER_MSINADAY;
			$dateinfo = $this->gmgetdate( $utcValue );

			$raw = $numValue;
			$fractionalDay = $numValue - floor( $numValue ) + 0.0000001; // The 0.0000001 is to fix for php/excel fractional diffs

			$totalseconds = floor( SPREADSHEET_EXCEL_READER_MSINADAY * $fractionalDay );
			$secs = $totalseconds % 60;
			$totalseconds -= $secs;
			$hours = floor( $totalseconds / ( 60 * 60 ) );
			$mins = floor( $totalseconds / 60 ) % 60;
			$string = date( $format, mktime( $hours, $mins, $secs, $dateinfo['mon'], $dateinfo['mday'], $dateinfo['year'] ) );
		} elseif ( 'number' === $type ) {
			$rectype = 'number';
			$formatted = $this->_format_value( $format, $numValue, $formatIndex );
			$string = $formatted['string'];
			$formatColor = $formatted['formatColor'];
			$raw = $numValue;
		} else {
			if ( '' === $format ) {
				$format = $this->_defaultFormat;
			}
			$rectype = 'unknown';
			$formatted = $this->_format_value( $format, $numValue, $formatIndex );
			$string = $formatted['string'];
			$formatColor = $formatted['formatColor'];
			$raw = $numValue;
		}

		return array(
			'string'      => $string,
			'raw'         => $raw,
			'rectype'     => $rectype,
			'format'      => $format,
			'formatIndex' => $formatIndex,
			'fontIndex'   => $fontIndex,
			'formatColor' => $formatColor,
			'xfIndex'     => $xfindex,
		);
	}

	/**
	 * [createNumber description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $spos [description]
	 * @return [type] [description]
	 */
	protected function createNumber( $spos ) {
		$rknumhigh = $this->_GetInt4d( $this->data, $spos + 10 );
		$rknumlow = $this->_GetInt4d( $this->data, $spos + 6 );
		$sign = ( $rknumhigh & 0x80000000 ) >> 31;
		$exp = ( $rknumhigh & 0x7ff00000 ) >> 20;
		$mantissa = ( 0x100000 | ( $rknumhigh & 0x000fffff ) );
		$mantissalow1 = ( $rknumlow & 0x80000000 ) >> 31;
		$mantissalow2 = ( $rknumlow & 0x7fffffff );
		$value = $mantissa / pow( 2, ( 20 - ( $exp - 1023 ) ) );
		if ( 0 !== $mantissalow1 ) {
			$value += 1 / pow( 2, ( 21 - ( $exp - 1023 ) ) );
		}
		$value += $mantissalow2 / pow( 2, ( 52 - ( $exp - 1023 ) ) );
		if ( $sign ) {
			$value *= -1;
		}
		return $value;
	}

	/**
	 * [addcell description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $row    [description]
	 * @param [type] $col    [description]
	 * @param [type] $string [description]
	 * @param [type] $info   Optional. [description]
	 * @return [type] [description]
	 */
	protected function addcell( $row, $col, $string, $info = null ) {
		$this->sheets[ $this->sn ]['maxrow'] = max( $this->sheets[ $this->sn ]['maxrow'], $row + $this->_rowoffset );
		$this->sheets[ $this->sn ]['maxcol'] = max( $this->sheets[ $this->sn ]['maxcol'], $col + $this->_coloffset );
		$this->sheets[ $this->sn ]['cells'][ $row + $this->_rowoffset ][ $col + $this->_coloffset ] = $string;
		if ( $this->store_extended_info && $info ) {
			foreach ( $info as $key => $val ) {
				$this->sheets[ $this->sn ]['cellsInfo'][ $row + $this->_rowoffset ][ $col + $this->_coloffset ][ $key ] = $val;
			}
		}
	}

	/**
	 * [_GetIEEE754 description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $rknum [description]
	 * @return [type] [description]
	 */
	protected function _GetIEEE754( $rknum ) {
		if ( 0 !== ( $rknum & 0x02 ) ) {
			$value = $rknum >> 2;
		} else {
			// info on IEEE754 encoding from
			// http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
			// The RK format calls for using only the most significant 30 bits of the
			// 64 bit floating point value. The other 34 bits are assumed to be 0
			// So, we use the upper 30 bits of $rknum as follows...
			$sign = ( $rknum & 0x80000000 ) >> 31;
			$exp = ( $rknum & 0x7ff00000 ) >> 20;
			$mantissa = ( 0x100000 | ( $rknum & 0x000ffffc ) );
			$value = $mantissa / pow( 2, ( 20 - ( $exp - 1023 ) ) );
			if ( $sign ) {
				$value *= -1;
			}
		}
		if ( 0 !== ( $rknum & 0x01 ) ) {
			$value /= 100;
		}
		return $value;
	}

	/**
	 * [_encodeUTF16 description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $string [description]
	 * @return [type] [description]
	 */
	protected function _encodeUTF16( $string ) {
		if ( $this->_defaultEncoding ) {
			switch ( $this->_encoderFunction ) {
				case 'iconv':
					$string = iconv( 'UTF-16LE', $this->_defaultEncoding, $string );
					break;
				case 'mb_convert_encoding':
					$string = mb_convert_encoding( $string, $this->_defaultEncoding, 'UTF-16LE' );
					break;
			}
		}
		return $string;
	}

	/**
	 * [_GetInt4d description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $data [description]
	 * @param [type] $pos  [description]
	 * @return [type] [description]
	 */
	protected function _GetInt4d( $data, $pos ) {
		$value = ord( $data[ $pos ] ) | ( ord( $data[ $pos + 1 ] ) << 8 ) | ( ord( $data[ $pos + 2 ] ) << 16 ) | ( ord( $data[ $pos + 3 ] ) << 24 );
		if ( $value >= 4294967294 ) {
			$value = -2;
		}
		return $value;
	}

	/**
	 * [v description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $data [description]
	 * @param [type] $pos  [description]
	 * @return [type] [description]
	 */
	protected function v( $data, $pos ) {
		return ord( $data[ $pos ] ) | ord( $data[ $pos + 1 ] ) << 8;
	}

} // class Spreadsheet_Excel_Reader
index.php000066600000000034151121565550006373 0ustar00<?php // Silence is golden.
csstidy/class.csstidy.php000066600000121204151121565550011537 0ustar00<?php
/**
 * CSSTidy Parsing PHP Class
 *
 * @package TablePress
 * @subpackage CSS
 * @author Florian Schmitz, Brett Zamir, Nikolay Matsievsky, Cedric Morin, Christopher Finke, Mark Scherer, Tobias Bäthge
 * @since 1.0.0
 */

// Prohibit direct script loading.
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );

/**
 * CSSTidy - CSS Parser and Optimiser
 *
 * CSS Parser class
 *
 * Copyright 2005, 2006, 2007 Florian Schmitz
 *
 * This file is part of CSSTidy.
 *
 *  CSSTidy is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 *
 *  CSSTidy is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * @license https://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
 * @package CSSTidy
 * @author Florian Schmitz (floele at gmail dot com) 2005-2007
 * @author Brett Zamir (brettz9 at yahoo dot com) 2007
 * @author Nikolay Matsievsky (speed at webo dot name) 2009-2010
 * @author Cedric Morin (cedric at yterium dot com) 2010-2012
 * @author Christopher Finke (cfinke at gmail.com) 2012
 * @author Mark Scherer (remove $GLOBALS once and for all + PHP5.4 comp) 2012
 */

/**
 * Defines ctype functions if required.
 *
 * @TODO: Make these methods of CSSTidy.
 */
if ( ! function_exists( 'ctype_space' ) ) {
	/**
	 * Check for whitespace character(s).
	 *
	 * @since 1.0.0
	 *
	 * @param string $text Text to check.
	 * @return bool Whether all characters in the string are whitespace characters.
	 */
	function ctype_space( $text ) {
		return ( 1 === preg_match( "/^[ \r\n\t\f]+$/", $text ) );
	}
}
if ( ! function_exists( 'ctype_alpha' ) ) {
	/**
	 * Check for alphabetic character(s).
	 *
	 * @since 1.0.0
	 *
	 * @param string $text Text to check.
	 * @return bool Whether all characters in the string are alphabetic characters.
	 */
	function ctype_alpha( $text ) {
		return ( 1 === preg_match( '/^[a-zA-Z]+$/', $text ) );
	}
}
if ( ! function_exists( 'ctype_xdigit' ) ) {
	/**
	 * Check for character(s) representing a hexadecimal digit.
	 *
	 * @since 1.0.0
	 *
	 * @param string $text Text to check.
	 * @return bool Whether $text is a hexadecimal number.
	 */
	function ctype_xdigit( $text ) {
		return ( 1 === preg_match( '/^[a-fA-F0-9]+$/', $text ) );
	}
}

/**
 * Defines constants.
 *
 * @TODO: Make these class constants of CSSTidy.
 * @since 1.0.0
 */
define( 'AT_START', 1 );
define( 'AT_END', 2 );
define( 'SEL_START', 3 );
define( 'SEL_END', 4 );
define( 'PROPERTY', 5 );
define( 'VALUE', 6 );
define( 'COMMENT', 7 );
define( 'DEFAULT_AT', 41 );

/**
 * Load the class for printing CSS code.
 *
 * @since 1.0.0
 */
require dirname( __FILE__ ) . '/class.csstidy_print.php';

/**
 * Load the class for optimising CSS code.
 *
 * @since 1.0.0
 */
require dirname( __FILE__ ) . '/class.csstidy_optimise.php';

/**
 * CSS Parser class
 *
 * This class represents a CSS parser which reads CSS code and saves it in an array.
 * In opposite to most other CSS parsers, it does not use regular expressions and
 * thus has full CSS2 support and a higher reliability.
 * Additionally to that, it applies some optimizations and fixes to the CSS code.
 *
 * @package CSSTidy
 * @since 1.0.0
 */
class TablePress_CSSTidy {

	/**
	 * The parsed CSS.
	 *
	 * This array is empty if preserve_css is on.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $css = array();

	/**
	 * The raw parsed CSS.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $tokens = array();

	/**
	 * Instance of the CSS Printer class.
	 *
	 * @since 1.0.0
	 * @var TablePress_CSSTidy_print
	 */
	public $print;

	/**
	 * Instance of the CSS Optimiser class.
	 *
	 * @since 1.0.0
	 * @var TablePress_CSSTidy_optimise
	 */
	public $optimise;

	/**
	 * The CSS charset.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $charset = '';

	/**
	 * All @import URLs.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $import = array();

	/**
	 * The namespace.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $namespace = '';

	/**
	 * The CSSTidy version.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $version = '1.5.3';

	/**
	 * The settings.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $settings = array();

	/**
	 * The parser-status.
	 *
	 * Possible values:
	 * - is = in selector
	 * - ip = in property
	 * - iv = in value
	 * - instr = in string (started at " or ' or ( )
	 * - ic = in comment (ignore everything)
	 * - at = in @-block
	 *
	 * @since 1.0.0
	 * @var string
	 */
	protected $status = 'is';

	/**
	 * The current at rule (@media).
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $at = '';

	/**
	 * The at rule for next selector (during @font-face or other @).
	 *
	 * @since 1.0.0
	 * @var string
	 */
	protected $next_selector_at = '';

	/**
	 * The current selector.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $selector = '';

	/**
	 * The current property.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $property = '';

	/**
	 * The position of , in selectors.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $sel_separate = array();

	/**
	 * The current value.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $value = '';

	/**
	 * The current sub-value.
	 *
	 * Example for a sub-value: In the CSS rule
	 * background: url(foo.png) red no-repeat;
	 * "url(foo.png)", "red", and  "no-repeat" are sub-values,
	 * separated by whitespace.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $sub_value = '';

	/**
	 * All sub-values for a property.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $sub_value_arr = array();

	/**
	 * The stack of characters that opened the current strings.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $str_char = array();

	/**
	 * [$cur_string description]
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $cur_string = array();

	/**
	 * Status from which the parser switched to ic or instr
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $from = array();

	/**
	 * True if in invalid at-rule.
	 *
	 * @since 1.0.0
	 * @var bool
	 */
	protected $invalid_at = false;

	/**
	 * True if something has been added to the current selector.
	 *
	 * @since 1.0.0
	 * @var bool
	 */
	protected $added = false;

	/**
	 * The message log.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $log = array();

	/**
	 * The line number.
	 *
	 * @since 1.0.0
	 * @var int
	 */
	protected $line = 1;

	/**
	 * Marks if we need to leave quotes for a string.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $quoted_string = array();

	/**
	 * List of tokens.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	protected $tokens_list = '';

	/**
	 * Various CSS Data for CSSTidy.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $data = array();

	/**
	 * The output templates.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $template = array();

	/**
	 * Loads standard template and sets default settings.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		include dirname( __FILE__ ) . '/data.inc.php';
		$this->data = $data;

		$this->settings['remove_bslash'] = true;
		$this->settings['compress_colors'] = true;
		$this->settings['compress_font-weight'] = true;
		$this->settings['lowercase_s'] = false;
		/*
		 * 1 common shorthands optimization
		 * 2 + font property optimization
		 * 3 + background property optimization
		 */
		$this->settings['optimise_shorthands'] = 1;
		$this->settings['remove_last_;'] = true;
		// Rewrite all properties with lower case, better for later gzipping.
		$this->settings['case_properties'] = 1;
		/*
		 * Sort properties in alpabetic order, better for later gzipping,
		 * but can cause trouble in case of overiding same properties or using hacks.
		 */
		$this->settings['sort_properties'] = false;
		/*
		 * 1, 3, 5, etc -- Enable sorting selectors inside @media: a{}b{}c{}.
		 * 2, 5, 8, etc -- Enable sorting selectors inside one CSS declaration: a,b,c{}.
		 * Preserve order by default cause it can break functionality.
		 */
		$this->settings['sort_selectors'] = 0;
		// Is dangerous to be used: CSS is broken sometimes.
		$this->settings['merge_selectors'] = 0;
		// Whether to preserve browser hacks.
		$this->settings['discard_invalid_selectors'] = false;
		$this->settings['discard_invalid_properties'] = false;
		$this->settings['css_level'] = 'CSS3.0';
		$this->settings['preserve_css'] = false;
		$this->settings['timestamp'] = false;
		$this->settings['template'] = ''; // say that property exists.
		$this->set_cfg( 'template', 'default' ); // Call load_template.

		$this->print = new TablePress_CSSTidy_print( $this );
		$this->optimise = new TablePress_CSSTidy_optimise( $this );

		$this->tokens_list = &$this->data['csstidy']['tokens'];
	}

	/**
	 * Get the value of a setting.
	 *
	 * @since 1.0.0
	 *
	 * @param string $setting Setting to get.
	 * @return string|bool Value of the setting.
	 */
	public function get_cfg( $setting ) {
		if ( isset( $this->settings[ $setting ] ) ) {
			return $this->settings[ $setting ];
		}
		return false;
	}

	/**
	 * Load a template.
	 *
	 * @since 1.0.0
	 *
	 * @param string $template Template used by set_cfg to load a template via a configuration setting.
	 */
	protected function _load_template( $template ) {
		switch ( $template ) {
			case 'default':
				$this->load_template( 'default' );
				break;
			case 'highest':
			case 'high':
			case 'low':
				$this->load_template( $template . '_compression' );
				break;
			default:
				$this->load_template( $template );
				break;
		}
	}

	/**
	 * Set the value of a setting.
	 *
	 * @since 1.0.0
	 *
	 * @param string $setting Setting.
	 * @param mixed  $value   Optional. Value of the setting.
	 * @return bool [return value]
	 */
	public function set_cfg( $setting, $value = null ) {
		if ( is_array( $setting ) && is_null( $value ) ) {
			foreach ( $setting as $setprop => $setval ) {
				$this->settings[ $setprop ] = $setval;
			}
			if ( array_key_exists( 'template', $setting ) ) {
				$this->_load_template( $this->settings['template'] );
			}
			return true;
		} elseif ( isset( $this->settings[ $setting ] ) && '' !== $value ) {
			$this->settings[ $setting ] = $value;
			if ( 'template' === $setting ) {
				$this->_load_template( $this->settings['template'] );
			}
			return true;
		}
		return false;
	}

	/**
	 * Adds a token to $this->tokens.
	 *
	 * @since 1.0.0
	 *
	 * @param mixed  $type Type.
	 * @param string $data Data.
	 * @param bool   $do   Optional. Add a token even if preserve_css is off.
	 */
	public function _add_token( $type, $data, $do = false ) {
		if ( $this->get_cfg( 'preserve_css' ) || $do ) {
			$this->tokens[] = array( $type, ( COMMENT === $type ) ? $data : trim( $data ) );
		}
	}

	/**
	 * Add a message to the message log.
	 *
	 * @since 1.0.0
	 *
	 * @param string $message Message.
	 * @param string $type    Type.
	 * @param int    $line    Optional. Line number. -1 will use the current line.
	 */
	public function log( $message, $type, $line = -1 ) {
		if ( -1 === $line ) {
			$line = $this->line;
		}
		$line = intval( $line );
		$add = array(
			'm' => $message,
			't' => $type,
		);
		if ( ! isset( $this->log[ $line ] ) || ! in_array( $add, $this->log[ $line ], true ) ) {
			$this->log[ $line ][] = $add;
		}
	}

	/**
	 * Parse Unicode notations and find a replacement character.
	 *
	 * @since 1.0.0
	 *
	 * @param string $string String.
	 * @param int    $i      i.
	 * @return string [return value]
	 */
	public function _unicode( &$string, &$i ) {
		++$i;
		$add = '';
		$replaced = false;

		while ( $i < strlen( $string ) && ( ctype_xdigit( $string[ $i ] ) || ctype_space( $string[ $i ] ) ) && strlen( $add ) < 6 ) {
			$add .= $string[ $i ];
			if ( ctype_space( $string[ $i ] ) ) {
				break;
			}
			$i++;
		}

		if ( hexdec( $add ) > 47 && hexdec( $add ) < 58 || hexdec( $add ) > 64 && hexdec( $add ) < 91 || hexdec( $add ) > 96 && hexdec( $add ) < 123 ) {
			$this->log( 'Replaced unicode notation: Changed \\' . $add . ' to ' . chr( hexdec( $add ) ), 'Information' );
			$add = chr( hexdec( $add ) );
			$replaced = true;
		} else {
			$add = trim( '\\' . $add );
		}

		if ( @ctype_xdigit( $string[ $i + 1 ] ) && ctype_space( $string[ $i ] ) && ! $replaced || ! ctype_space( $string[ $i ] ) ) {
			$i--;
		}

		if ( '\\' !== $add || ! $this->get_cfg( 'remove_bslash' ) || false !== strpos( $this->tokens_list, $string[ $i + 1 ] ) ) {
			return $add;
		}

		if ( '\\' === $add ) {
			$this->log( 'Removed unnecessary backslash', 'Information' );
		}

		return '';
	}

	/**
	 * Write formatted output to a file.
	 *
	 * @since 1.0.0
	 *
	 * @param string $filename File name.
	 */
	public function write_page( $filename ) {
		$this->write( $filename, true );
	}

	/**
	 * Write plain output to a file.
	 *
	 * @since 1.0.0
	 *
	 * @param string $filename    File name.
	 * @param bool   $formatted   Optional. Whether to print formatted or not.
	 * @param string $doctype     Optional. When printing formatted, is a shorthand for the document type.
	 * @param bool   $externalcss Optional. When printing formatted, indicates whether styles to be attached internally or as an external stylesheet.
	 * @param string $title       Optional. When printing formatted, is the title to be added in the head of the document.
	 * @param string $lang        Optional. When printing formatted, gives a two-letter language code to be added to the output.
	 */
	public function write( $filename, $formatted = false, $doctype = 'xhtml1.1', $externalcss = true, $title = '', $lang = 'en' ) {
		$filename .= ( $formatted ) ? '.xhtml' : '.css';

		if ( ! is_dir( 'temp' ) ) {
			$madedir = mkdir( 'temp' );
			if ( ! $madedir ) {
				print 'Could not make directory "temp" in ' . dirname( __FILE__ );
				exit;
			}
		}
		$handle = fopen( 'temp/' . $filename, 'w' );
		if ( $handle ) {
			if ( ! $formatted ) {
				fwrite( $handle, $this->print->plain() );
			} else {
				fwrite( $handle, $this->print->formatted_page( $doctype, $externalcss, $title, $lang ) );
			}
		}
		fclose( $handle );
	}

	/**
	 * Loads a new template.
	 *
	 * @since 1.0.0
	 *
	 * @link http://csstidy.sourceforge.net/templates.php
	 *
	 * @param string $content   Either file name (if $from_file is true), content of a template file, "high_compression", "highest_compression", "low_compression", or "default".
	 * @param bool   $from_file Optional. Uses $content as filename if true.
	 */
	protected function load_template( $content, $from_file = true ) {
		$predefined_templates = &$this->data['csstidy']['predefined_templates'];
		if ( in_array( $content, array( 'default', 'low_compression', 'high_compression', 'highest_compression' ), true ) ) {
			$this->template = $predefined_templates[ $content ];
			return;
		}

		if ( $from_file ) {
			$content = strip_tags( file_get_contents( $content ), '<span>' );
		}
		// Unify newlines (because the output also only uses \n).
		$content = str_replace( "\r\n", "\n", $content );
		$template = explode( '|', $content );

		for ( $i = 0; $i < count( $template ); $i++ ) {
			$this->template[ $i ] = $template[ $i ];
		}
	}

	/**
	 * Starts parsing from URL.
	 *
	 * @since 1.0.0
	 *
	 * @param string $url URL.
	 * @return bool
	 */
	protected function parse_from_url( $url ) {
		return $this->parse( @file_get_contents( $url ) );
	}

	/**
	 * Checks if there is a token at the current position.
	 *
	 * @since 1.0.0
	 *
	 * @param string $string String.
	 * @param int    $i      i.
	 * @return bool [return value]
	 */
	protected function is_token( &$string, $i ) {
		return ( false !== strpos( $this->tokens_list, $string[ $i ] ) && ! $this->escaped( $string, $i ) );
	}

	/**
	 * Parses CSS in a string. The output is saved as an array in $this->css.
	 *
	 * @since 1.0.0
	 *
	 * @param string $string The CSS code.
	 * @return bool [return value]
	 */
	public function parse( $string ) {
		// Temporarily set locale to en_US in order to handle floats properly.
		$old = @setlocale( LC_ALL, 0 );
		@setlocale( LC_ALL, 'C' );

		$all_properties = &$this->data['csstidy']['all_properties'];
		$at_rules = &$this->data['csstidy']['at_rules'];
		$quoted_string_properties = &$this->data['csstidy']['quoted_string_properties'];

		$this->css = array();
		$this->print->input_css = $string;
		$string = str_replace( "\r\n", "\n", $string ) . ' ';
		$cur_comment = '';

		for ( $i = 0, $size = strlen( $string ); $i < $size; $i++ ) {
			if ( "\n" === $string[ $i ] || "\r" === $string[ $i ] ) {
				++$this->line;
			}

			switch ( $this->status ) {
				/* Case in at-block */
				case 'at':
					if ( $this->is_token( $string, $i ) ) {
						if ( '/' === $string[ $i ] && '*' === @$string[ $i + 1 ] ) {
							$this->status = 'ic';
							++$i;
							$this->from[] = 'at';
						} elseif ( '{' === $string[ $i ] ) {
							$this->status = 'is';
							$this->at = $this->css_new_media_section( $this->at );
							$this->_add_token( AT_START, $this->at );
						} elseif ( ',' === $string[ $i ] ) {
							$this->at = trim( $this->at ) . ',';
						} elseif ( '\\' === $string[ $i ] ) {
							$this->at .= $this->_unicode( $string, $i );
						}
						// Fix for complicated media, i.e @media screen and (-webkit-min-device-pixel-ratio:1.5)
						// '/' is included for ratios in Opera: (-o-min-device-pixel-ratio: 3/2)
						elseif ( in_array( $string[ $i ], array( '(', ')', ':', '.', '/' ), true ) ) {
							$this->at .= $string[ $i ];
						}
					} else {
						$lastpos = strlen( $this->at ) - 1;
						if ( ! ( ( ctype_space( $this->at[ $lastpos ] ) || $this->is_token( $this->at, $lastpos ) && ',' === $this->at[ $lastpos ] ) && ctype_space( $string[ $i ] ) ) ) {
							$this->at .= $string[ $i ];
						}
					}
					break;
				/* Case in-selector */
				case 'is':
					if ( $this->is_token( $string, $i ) ) {
						if ( '/' === $string[ $i ] && '*' === @$string[ $i + 1 ] && '' === trim( $this->selector ) ) {
							$this->status = 'ic';
							++$i;
							$this->from[] = 'is';
						} elseif ( '@' === $string[ $i ] && '' === trim( $this->selector ) ) {
							// Check for at-rule.
							$this->invalid_at = true;
							foreach ( $at_rules as $name => $type ) {
								if ( ! strcasecmp( substr( $string, $i + 1, strlen( $name ) ), $name ) ) {
									if ( 'at' === $type ) {
										$this->at = '@' . $name;
									} else {
										$this->selector = '@' . $name;
									}
									if ( 'atis' === $type ) {
										$this->next_selector_at = ( $this->next_selector_at ? $this->next_selector_at : ( $this->at ? $this->at : DEFAULT_AT ) );
										$this->at = $this->css_new_media_section( ' ' );
										$type = 'is';
									}
									$this->status = $type;
									$i += strlen( $name );
									$this->invalid_at = false;
								}
							}

							if ( $this->invalid_at ) {
								$this->selector = '@';
								$invalid_at_name = '';
								for ( $j = $i + 1; $j < $size; ++$j ) {
									if ( ! ctype_alpha( $string[ $j ] ) ) {
										break;
									}
									$invalid_at_name .= $string[ $j ];
								}
								$this->log( 'Invalid @-rule: ' . $invalid_at_name . ' (removed)', 'Warning' );
							}
						} elseif ( ( '"' === $string[ $i ] || "'" === $string[ $i ] ) ) {
							$this->cur_string[] = $string[ $i ];
							$this->status = 'instr';
							$this->str_char[] = $string[ $i ];
							$this->from[] = 'is';
							/* Fixing CSS3 attribute selectors, i.e. a[href$=".mp3" */
							$this->quoted_string[] = ( '=' === $string[ $i - 1 ] );
						} elseif ( $this->invalid_at && ';' === $string[ $i ] ) {
							$this->invalid_at = false;
							$this->status = 'is';
							if ( $this->next_selector_at ) {
								$this->at = $this->css_new_media_section( $this->next_selector_at );
								$this->next_selector_at = '';
							}
						} elseif ( '{' === $string[ $i ] ) {
							$this->status = 'ip';
							if ( '' === $this->at ) {
								$this->at = $this->css_new_media_section( DEFAULT_AT );
							}
							$this->selector = $this->css_new_selector( $this->at, $this->selector );
							$this->_add_token( SEL_START, $this->selector );
							$this->added = false;
						} elseif ( '}' === $string[ $i ] ) {
							$this->_add_token( AT_END, $this->at );
							$this->at = '';
							$this->selector = '';
							$this->sel_separate = array();
						} elseif ( ',' === $string[ $i ] ) {
							$this->selector = trim( $this->selector ) . ',';
							$this->sel_separate[] = strlen( $this->selector );
						} elseif ( '\\' === $string[ $i ] ) {
							$this->selector .= $this->_unicode( $string, $i );
						} elseif ( '*' === $string[ $i ] && @in_array( $string[ $i + 1 ], array( '.', '#', '[', ':' ), true ) && ( 0 === $i || '/' !== $string[ $i - 1 ] ) ) {
							// Remove unnecessary universal selector, FS#147, but not comment in selector.
						} else {
							$this->selector .= $string[ $i ];
						}
					} else {
						$lastpos = strlen( $this->selector ) - 1;
						if ( -1 === $lastpos || ! ( ( ctype_space( $this->selector[ $lastpos ] ) || $this->is_token( $this->selector, $lastpos ) && ',' === $this->selector[ $lastpos ] ) && ctype_space( $string[ $i ] ) ) ) {
							$this->selector .= $string[ $i ];
						}
					}
					break;
				/* Case in-property */
				case 'ip':
					if ( $this->is_token( $string, $i ) ) {
						if ( ( ':' === $string[ $i ] || '=' === $string[ $i ] ) && '' !== $this->property ) {
							$this->status = 'iv';
							if ( ! $this->get_cfg( 'discard_invalid_properties' ) || $this->property_is_valid( $this->property ) ) {
								$this->property = $this->css_new_property( $this->at, $this->selector, $this->property );
								$this->property = strtolower( $this->property );
								$this->_add_token( PROPERTY, $this->property );
							}
						} elseif ( '/' === $string[ $i ] && '*' === @$string[ $i + 1 ] && '' === $this->property ) {
							$this->status = 'ic';
							++$i;
							$this->from[] = 'ip';
						} elseif ( '}' === $string[ $i ] ) {
							$this->explode_selectors();
							$this->status = 'is';
							$this->invalid_at = false;
							$this->_add_token( SEL_END, $this->selector );
							$this->selector = '';
							$this->property = '';
							if ( $this->next_selector_at ) {
								$this->at = $this->css_new_media_section( $this->next_selector_at );
								$this->next_selector_at = '';
							}
						} elseif ( ';' === $string[ $i ] ) {
							$this->property = '';
						} elseif ( '\\' === $string[ $i ] ) {
							$this->property .= $this->_unicode( $string, $i );
						}
						// else this is dumb IE a hack, keep it
						// including /
						elseif ( ( '' === $this->property && ! ctype_space( $string[ $i ] ) ) || ( '/' === $this->property || '/' === $string[ $i ] ) ) {
							$this->property .= $string[ $i ];
						}
					} elseif ( ! ctype_space( $string[ $i ] ) ) {
						$this->property .= $string[ $i ];
					}
					break;
				/* Case in-value */
				case 'iv':
					$pn = ( ( "\n" === $string[ $i ] || "\r" === $string[ $i ] ) && $this->property_is_next( $string, $i + 1 ) || ( strlen( $string ) - 1 ) === $i );
					if ( ( $this->is_token( $string, $i ) || $pn ) && ( ! ( ',' === $string[ $i ] && ! ctype_space( $string[ $i + 1 ] ) ) ) ) {
						if ( '/' === $string[ $i ] && '*' === @$string[ $i + 1 ] ) {
							$this->status = 'ic';
							++$i;
							$this->from[] = 'iv';
						} elseif ( ( '"' === $string[ $i ] || "'" === $string[ $i ] || '(' === $string[ $i ] ) ) {
							$this->cur_string[] = $string[ $i ];
							$this->str_char[] = ( '(' === $string[ $i ] ) ? ')' : $string[ $i ];
							$this->status = 'instr';
							$this->from[] = 'iv';
							$this->quoted_string[] = in_array( strtolower( $this->property ), $quoted_string_properties, true );
						} elseif ( ',' === $string[ $i ] ) {
							$this->sub_value = trim( $this->sub_value ) . ',';
						} elseif ( '\\' === $string[ $i ] ) {
							$this->sub_value .= $this->_unicode( $string, $i );
						} elseif ( ';' === $string[ $i ] || $pn ) {
							if ( '@' === $this->selector[0] && isset( $at_rules[ substr( $this->selector, 1 ) ] ) && 'iv' === $at_rules[ substr( $this->selector, 1 ) ] ) {
								$this->status = 'is';

								switch ( $this->selector ) {
									case '@charset':
										// Add quotes to charset.
										$this->sub_value_arr[] = '"' . trim( $this->sub_value ) . '"';
										$this->charset = $this->sub_value_arr[0];
										break;
									case '@namespace':
										// Add quotes to namespace.
										$this->sub_value_arr[] = '"' . trim( $this->sub_value ) . '"';
										$this->namespace = implode( ' ', $this->sub_value_arr );
										break;
									case '@import':
										$this->sub_value = trim( $this->sub_value );

										if ( empty( $this->sub_value_arr ) ) {
											// Quote URLs in imports only if they're not already inside url() and not already quoted.
											if ( 'url(' !== substr( $this->sub_value, 0, 4 ) ) {
												if ( ! ( substr( $this->sub_value, -1 ) === $this->sub_value[0] && in_array( $this->sub_value[0], array( "'", '"' ), true ) ) ) {
													$this->sub_value = '"' . $this->sub_value . '"';
												}
											}
										}

										$this->sub_value_arr[] = $this->sub_value;
										$this->import[] = implode( ' ', $this->sub_value_arr );
										break;
								}

								$this->sub_value_arr = array();
								$this->sub_value = '';
								$this->selector = '';
								$this->sel_separate = array();
							} else {
								$this->status = 'ip';
							}
						} elseif ( '}' !== $string[ $i ] ) {
							$this->sub_value .= $string[ $i ];
						}
						if ( ( '}' === $string[ $i ] || ';' === $string[ $i ] || $pn ) && ! empty( $this->selector ) ) {
							if ( '' === $this->at ) {
								$this->at = $this->css_new_media_section( DEFAULT_AT );
							}

							// Case settings.
							if ( $this->get_cfg( 'lowercase_s' ) ) {
								$this->selector = strtolower( $this->selector );
							}
							$this->property = strtolower( $this->property );

							$this->optimise->subvalue();
							if ( '' !== $this->sub_value ) {
								$this->sub_value_arr[] = $this->sub_value;
								$this->sub_value = '';
							}

							$this->value = '';
							while ( count( $this->sub_value_arr ) ) {
								$sub = array_shift( $this->sub_value_arr );
								if ( strstr( $this->selector, 'font-face' ) ) {
									$sub = $this->quote_font_format( $sub );
								}

								if ( '' !== $sub ) {
									if ( strlen( $this->value ) && ( ',' !== substr( $this->value, -1, 1 ) || $this->get_cfg( 'preserve_css' ) ) ) {
										$this->value .= ' ';
									}
									$this->value .= $sub;
								}
							}

							$this->optimise->value();

							$valid = $this->property_is_valid( $this->property );
							if ( ( ! $this->invalid_at || $this->get_cfg( 'preserve_css' ) ) && ( ! $this->get_cfg( 'discard_invalid_properties' ) || $valid ) ) {
								$this->css_add_property( $this->at, $this->selector, $this->property, $this->value );
								$this->_add_token( VALUE, $this->value );
								$this->optimise->shorthands();
							}
							if ( ! $valid ) {
								if ( $this->get_cfg( 'discard_invalid_properties' ) ) {
									$this->log( 'Removed invalid property: ' . $this->property, 'Warning' );
								} else {
									$this->log( 'Invalid property in ' . strtoupper( $this->get_cfg( 'css_level' ) ) . ': ' . $this->property, 'Warning' );
								}
							}

							$this->property = '';
							$this->sub_value_arr = array();
							$this->value = '';
						}
						if ( '}' === $string[ $i ] ) {
							$this->explode_selectors();
							$this->_add_token( SEL_END, $this->selector );
							$this->status = 'is';
							$this->invalid_at = false;
							$this->selector = '';
							if ( $this->next_selector_at ) {
								$this->at = $this->css_new_media_section( $this->next_selector_at );
								$this->next_selector_at = '';
							}
						}
					} elseif ( ! $pn ) {
						$this->sub_value .= $string[ $i ];

						if ( ctype_space( $string[ $i ] ) || ',' === $string[ $i ] ) {
							$this->optimise->subvalue();
							if ( '' !== $this->sub_value ) {
								$this->sub_value_arr[] = $this->sub_value;
								$this->sub_value = '';
							}
						}
					}
					break;
				/* Case in string */
				case 'instr':
					$_str_char = $this->str_char[ count( $this->str_char ) - 1 ];
					$_cur_string = $this->cur_string[ count( $this->cur_string ) - 1 ];
					$_quoted_string = $this->quoted_string[ count( $this->quoted_string ) - 1 ];
					$temp_add = $string[ $i ];

					// Add another string to the stack. Strings can't be nested inside of quotes, only parentheses,
					// but parentheticals can be nested more than once.
					if ( ')' === $_str_char && ( '(' === $string[ $i ] || '"' === $string[ $i ] || '\\' === $string[ $i ] ) && ! $this->escaped( $string, $i ) ) {
						$this->cur_string[] = $string[ $i ];
						$this->str_char[] = ( '(' === $string[ $i ] ) ? ')' : $string[ $i ];
						$this->from[] = 'instr';
						$this->quoted_string[] = ( ')' === $_str_char && '(' !== $string[ $i ] && '(' === trim( $_cur_string ) ) ? $_quoted_string : ( '(' !== $string[ $i ] );
						continue 2;
					}

					if ( ')' !== $_str_char && ( "\n" === $string[ $i ] || "\r" === $string[ $i ] ) && ! ( '\\' === $string[ $i - 1 ] && ! $this->escaped( $string, $i - 1 ) ) ) {
						$temp_add = '\\A';
						$this->log( 'Fixed incorrect newline in string', 'Warning' );
					}

					$_cur_string .= $temp_add;

					if ( $string[ $i ] === $_str_char && ! $this->escaped( $string, $i ) ) {
						$this->status = array_pop( $this->from );

						if ( ! preg_match( '|[' . implode( '', $this->data['csstidy']['whitespace'] ) . ']|uis', $_cur_string ) && 'content' !== $this->property ) {
							if ( ! $_quoted_string ) {
								if ( ')' !== $_str_char ) {
									// Convert properties like
									// font-family: 'Arial';
									// to
									// font-family: Arial;
									// or
									// url("abc")
									// to
									// url(abc)
									$_cur_string = substr( $_cur_string, 1, -1 );
								}
							} else {
								$_quoted_string = false;
							}
						}

						array_pop( $this->cur_string );
						array_pop( $this->quoted_string );
						array_pop( $this->str_char );

						if ( ')' === $_str_char ) {
							$_cur_string = '(' . trim( substr( $_cur_string, 1, -1 ) ) . ')';
						}

						if ( 'iv' === $this->status ) {
							if ( ! $_quoted_string ) {
								if ( false !== strpos( $_cur_string, ',' ) ) {
									// We can on only remove space next to ','
									$_cur_string = implode( ',', array_map( 'trim', explode( ',', $_cur_string ) ) );
								}
								// and multiple spaces (too expensive).
								if ( false !== strpos( $_cur_string, '  ' ) ) {
									$_cur_string = preg_replace( ',\s+,', ' ', $_cur_string );
								}
							}
							$this->sub_value .= $_cur_string;
						} elseif ( 'is' === $this->status ) {
							$this->selector .= $_cur_string;
						} elseif ( 'instr' === $this->status ) {
							$this->cur_string[ count( $this->cur_string ) - 1 ] .= $_cur_string;
						}
					} else {
						$this->cur_string[ count( $this->cur_string ) - 1 ] = $_cur_string;
					}
					break;
				/* Case in-comment */
				case 'ic':
					if ( '*' === $string[ $i ] && '/' === $string[ $i + 1 ] ) {
						$this->status = array_pop( $this->from );
						$i++;
						$this->_add_token( COMMENT, $cur_comment );
						$cur_comment = '';
					} else {
						$cur_comment .= $string[ $i ];
					}
					break;
			}
		}

		$this->optimise->postparse();
		$this->print->_reset();

		// Set locale back to original setting.
		@setlocale( LC_ALL, $old );

		return ! ( empty( $this->css ) && empty( $this->import ) && empty( $this->charset ) && empty( $this->tokens ) && empty( $this->namespace ) );
	}

	/**
	 * format() in font-face needs quoted values for somes browser (FF at least).
	 *
	 * @since 1.0.0
	 *
	 * @param mixed $value Value.
	 * @return string String.
	 */
	protected function quote_font_format( $value ) {
		if ( 0 === strncmp( $value, 'format', 6 ) ) {
			$p = strpos( $value, ')', 7 );
			$end = substr( $value, $p );
			$format_strings = $this->parse_string_list( substr( $value, 7, $p - 7 ) );
			if ( ! $format_strings ) {
				$value = '';
			} else {
				$value = 'format(';
				foreach ( $format_strings as $format_string ) {
					$value .= '"' . str_replace( '"', '\\"', $format_string ) . '",';
				}
				$value = substr( $value, 0, -1 ) . $end;
			}
		}
		return $value;
	}

	/**
	 * Explodes selectors.
	 *
	 * @since 1.0.0
	 */
	protected function explode_selectors() {
		// Explode multiple selectors.
		if ( 1 === $this->get_cfg( 'merge_selectors' ) ) {
			$new_sels = array();
			$lastpos = 0;
			$this->sel_separate[] = strlen( $this->selector );
			foreach ( $this->sel_separate as $num => $pos ) {
				if ( ( count( $this->sel_separate ) - 1 ) === $num ) {
					$pos += 1;
				}

				$new_sels[] = substr( $this->selector, $lastpos, $pos - $lastpos - 1 );
				$lastpos = $pos;
			}

			if ( count( $new_sels ) > 1 ) {
				foreach ( $new_sels as $selector ) {
					if ( isset( $this->css[ $this->at ][ $this->selector ] ) ) {
						$this->merge_css_blocks( $this->at, $selector, $this->css[ $this->at ][ $this->selector ] );
					}
				}
				unset( $this->css[ $this->at ][ $this->selector ] );
			}
		}
		$this->sel_separate = array();
	}

	/**
	 * Checks if a character is escaped (and returns true if it is).
	 *
	 * @since 1.0.0
	 *
	 * @param string $string String.
	 * @param int    $pos    Position.
	 * @return bool [return value]
	 */
	public function escaped( &$string, $pos ) {
		return $pos ? ! ( @( '\\' !== $string[ $pos - 1 ] ) || $this->escaped( $string, $pos - 1 ) ) : false;
	}

	/**
	 * Adds a property with value to the existing CSS code.
	 *
	 * @since 1.0.0
	 *
	 * @param string $media    Media.
	 * @param string $selector Selector.
	 * @param string $property Property.
	 * @param string $new_val  New value.
	 */
	protected function css_add_property( $media, $selector, $property, $new_val ) {
		if ( $this->get_cfg( 'preserve_css' ) || '' === trim( $new_val ) ) {
			return;
		}

		$this->added = true;
		if ( isset( $this->css[ $media ][ $selector ][ $property ] ) ) {
			if ( ( $this->is_important( $this->css[ $media ][ $selector ][ $property ] ) && $this->is_important( $new_val ) ) || ! $this->is_important( $this->css[ $media ][ $selector ][ $property ] ) ) {
				$this->css[ $media ][ $selector ][ $property ] = trim( $new_val );
			}
		} else {
			$this->css[ $media ][ $selector ][ $property ] = trim( $new_val );
		}
	}

	/**
	 * Start a new media section.
	 *
	 * Check if the media is not already known, else rename it with extra spaces to avoid merging.
	 *
	 * @since 1.0.0
	 *
	 * @param string $media Media.
	 * @return string [return value]
	 */
	protected function css_new_media_section( $media ) {
		if ( $this->get_cfg( 'preserve_css' ) ) {
			return $media;
		}
		// If the last @media is the same as this, keep it.
		if ( ! $this->css || ! is_array( $this->css ) || empty( $this->css ) ) {
			return $media;
		}
		end( $this->css );
		$at = key( $this->css );
		if ( $at === $media ) {
			return $media;
		}
		while ( isset( $this->css[ $media ] ) ) {
			if ( is_numeric( $media ) ) {
				$media++;
			} else {
				$media .= ' ';
			}
		}
		return $media;
	}

	/**
	 * Start a new selector.
	 *
	 * If already referenced in this media section, rename it with extra space to avoid merging,
	 * except if merging is required, or last selector is the same (merge siblings).
	 * Never merge @font-face.
	 *
	 * @since 1.0.0
	 *
	 * @param string $media    Media.
	 * @param string $selector Selector.
	 * @return string [return value]
	 */
	protected function css_new_selector( $media, $selector ) {
		if ( $this->get_cfg( 'preserve_css' ) ) {
			return $selector;
		}
		$selector = trim( $selector );
		if ( 0 !== strncmp( $selector, '@font-face', 10 ) ) {
			if ( false !== $this->settings['merge_selectors'] ) {
				return $selector;
			}

			if ( ! $this->css || ! isset( $this->css[ $media ] ) || ! $this->css[ $media ] ) {
				return $selector;
			}

			// If last is the same, keep it.
			end( $this->css[ $media ] );
			$sel = key( $this->css[ $media ] );
			if ( $sel === $selector ) {
				return $selector;
			}
		}

		while ( isset( $this->css[ $media ][ $selector ] ) ) {
			$selector .= ' ';
		}
		return $selector;
	}

	/**
	 * Start a new property.
	 *
	 * If already references in this selector, rename it with extra space to avoid override.
	 *
	 * @since 1.0.0
	 *
	 * @param string $media    Media.
	 * @param string $selector Selector.
	 * @param string $property Property.
	 * @return string [return value]
	 */
	protected function css_new_property( $media, $selector, $property ) {
		if ( $this->get_cfg( 'preserve_css' ) ) {
			return $property;
		}
		if ( ! $this->css || ! isset( $this->css[ $media ][ $selector ] ) || ! $this->css[ $media ][ $selector ] ) {
			return $property;
		}

		while ( isset( $this->css[ $media ][ $selector ][ $property ] ) ) {
			$property .= ' ';
		}

		return $property;
	}

	/**
	 * Adds CSS to an existing media/selector.
	 *
	 * @since 1.0.0
	 *
	 * @param string $media    Media.
	 * @param string $selector Selector.
	 * @param array  $css_add  Additional CSS.
	 */
	public function merge_css_blocks( $media, $selector, array $css_add ) {
		foreach ( $css_add as $property => $value ) {
			$this->css_add_property( $media, $selector, $property, $value );
		}
	}

	/**
	 * Checks if $value is !important.
	 *
	 * @since 1.0.0
	 *
	 * @param string $value Value.
	 * @return bool Whether the value has the !important keyword.
	 */
	public function is_important( &$value ) {
		return (
			false !== strpos( $value, '!' ) // Quick test.
			&& ! strcasecmp( substr( str_replace( $this->data['csstidy']['whitespace'], '', $value ), -10, 10 ), '!important' ) );
	}

	/**
	 * Returns a value without !important.
	 *
	 * @since 1.0.0
	 *
	 * @param string $value Value.
	 * @return string Value without the !important;
	 */
	public function gvw_important( $value ) {
		if ( $this->is_important( $value ) ) {
			$value = trim( $value );
			$value = substr( $value, 0, -9 );
			$value = trim( $value );
			$value = substr( $value, 0, -1 );
			$value = trim( $value );
			return $value;
		}
		return $value;
	}

	/**
	 * Checks if the next word in a string from pos is a CSS property.
	 *
	 * @since 1.0.0
	 *
	 * @param string $istring String.
	 * @param int    $pos     Position.
	 * @return bool [return value]
	 */
	protected function property_is_next( $istring, $pos ) {
		$all_properties = &$this->data['csstidy']['all_properties'];
		$istring = substr( $istring, $pos, strlen( $istring ) - $pos );
		$pos = strpos( $istring, ':' );
		if ( false === $pos ) {
			return false;
		}
		$istring = strtolower( trim( substr( $istring, 0, $pos ) ) );
		if ( isset( $all_properties[ $istring ] ) ) {
			$this->log( 'Added semicolon to the end of declaration', 'Warning' );
			return true;
		}
		return false;
	}

	/**
	 * Checks if a property is valid.
	 *
	 * @since 1.0.0
	 *
	 * @param string $property Property.
	 * @return bool Whether the property is valid.
	 */
	public function property_is_valid( $property ) {
		$property = strtolower( $property );
		if ( in_array( trim( $property ), $this->data['csstidy']['multiple_properties'], true ) ) {
			$property = trim( $property );
		}
		$all_properties = &$this->data['csstidy']['all_properties'];
		return isset( $all_properties[ $property ] ) && false !== strpos( $all_properties[ $property ], strtoupper( $this->get_cfg( 'css_level' ) ) );
	}

	/**
	 * Accepts a list of strings (e.g. the argument to format() in a @font-face src property)
	 * and returns a list of the strings. Converts things like:
	 * format(abc) => format("abc")
	 * format(abc def) => format("abc","def")
	 * format(abc "def") => format("abc","def")
	 * format(abc, def, ghi) => format("abc","def","ghi")
	 * format("abc",'def') => format("abc","def")
	 * format("abc, def, ghi") => format("abc, def, ghi")
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $value [description]
	 * @return [type] [description]
	 */
	public function parse_string_list( $value ) {
		$value = trim( $value );

		// Case: empty
		if ( ! $value ) {
			return array();
		}

		$strings = array();

		$in_str = false;
		$current_string = '';

		for ( $i = 0, $_len = strlen( $value ); $i < $_len; $i++ ) {
			if ( ( ',' === $value[ $i ] || ' ' === $value[ $i ] ) && true === $in_str ) {
				$in_str = false;
				$strings[] = $current_string;
				$current_string = '';
			} elseif ( '"' === $value[ $i ] || "'" === $value[ $i ] ) {
				if ( $in_str === $value[ $i ] ) {
					$strings[] = $current_string;
					$in_str = false;
					$current_string = '';
					continue;
				} elseif ( ! $in_str ) {
					$in_str = $value[ $i ];
				}
			} else {
				if ( $in_str ) {
					$current_string .= $value[ $i ];
				} else {
					if ( ! preg_match( '/[\s,]/', $value[ $i ] ) ) {
						$in_str = true;
						$current_string = $value[ $i ];
					}
				}
			}
		}

		if ( $current_string ) {
			$strings[] = $current_string;
		}

		return $strings;
	}

} // class TablePress_CSSTidy
csstidy/tablepress-standard.tpl000066600000000042151121565550012717 0ustar00| {

|| {
|	| |;|}|

|

}

|	||
|
csstidy/data-tp.inc.php000066600000012445151121565550011061 0ustar00<?php
/**
 * CSSTidy CSS Data, special data for TablePress
 *
 * @package TablePress
 * @subpackage CSS
 * @author Florian Schmitz, Brett Zamir, Nikolay Matsievsky, Cedric Morin, Christopher Finke, Mark Scherer, Tobias Bäthge
 * @since 1.0.0
 */

// Prohibit direct script loading.
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );

// Remove potentially insecure "binding" property.
unset( $data['csstidy']['all_properties']['binding'] );

$data['csstidy']['all_properties']['text-size-adjust'] = 'CSS3.0';
$data['csstidy']['all_properties']['zoom'] = 'CSS3.0';

// Support browser prefixes for properties only in the latest CSS draft.
foreach ( $data['csstidy']['all_properties'] as $property => $levels ) {
	$data['csstidy']['all_properties'][ '*' . $property ] = $levels; // IE7 hacks

	if ( false === strpos( $levels, ',' ) ) {
		$data['csstidy']['all_properties'][ '-moz-' . $property ] = $levels;
		$data['csstidy']['all_properties'][ '-webkit-' . $property ] = $levels;
		$data['csstidy']['all_properties'][ '-ms-' . $property ] = $levels;
		$data['csstidy']['all_properties'][ '-o-' . $property ] = $levels;
		$data['csstidy']['all_properties'][ '-khtml-' . $property ] = $levels;

		if ( in_array( $property, $data['csstidy']['unit_values'] ) ) {
			$data['csstidy']['unit_values'][] = '-moz-' . $property;
			$data['csstidy']['unit_values'][] = '-webkit-' . $property;
			$data['csstidy']['unit_values'][] = '-ms-' . $property;
			$data['csstidy']['unit_values'][] = '-o-' . $property;
			$data['csstidy']['unit_values'][] = '-khtml-' . $property;
		}

		if ( in_array( $property, $data['csstidy']['color_values'] ) ) {
			$data['csstidy']['color_values'][] = '-moz-' . $property;
			$data['csstidy']['color_values'][] = '-webkit-' . $property;
			$data['csstidy']['color_values'][] = '-ms-' . $property;
			$data['csstidy']['color_values'][] = '-o-' . $property;
			$data['csstidy']['color_values'][] = '-khtml-' . $property;
		}
	}
}

// Allow vendor prefixes for any property that is allowed to be used multiple times inside a single selector.
foreach ( $data['csstidy']['multiple_properties'] as $property ) {
	if ( '-' !== $property[0] ) {
		$data['csstidy']['multiple_properties'][] = '-o-' . $property;
		$data['csstidy']['multiple_properties'][] = '-ms-' . $property;
		$data['csstidy']['multiple_properties'][] = '-webkit-' . $property;
		$data['csstidy']['multiple_properties'][] = '-moz-' . $property;
		$data['csstidy']['multiple_properties'][] = '-khtml-' . $property;
	}
}

/**
 * Non-standard CSS properties. They're not part of any spec, but we say
 * they're in all of them so that we can support them.
 */
$data['csstidy']['all_properties']['-webkit-user-select'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['-moz-user-select'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['-ms-user-select'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['user-select'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['-webkit-filter'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['-moz-filter'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['-ms-filter'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['filter'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['scrollbar-face-color'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['-ms-interpolation-mode'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['text-rendering'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['-webkit-transform-origin-x'] = 'CSS3.0';
$data['csstidy']['all_properties']['-webkit-transform-origin-y'] = 'CSS3.0';
$data['csstidy']['all_properties']['-webkit-transform-origin-z'] = 'CSS3.0';
$data['csstidy']['all_properties']['-webkit-font-smoothing'] = 'CSS3.0';
$data['csstidy']['all_properties']['-moz-osx-font-smoothing'] = 'CSS3.0';
$data['csstidy']['all_properties']['-font-smooth'] = 'CSS3.0';
$data['csstidy']['all_properties']['-o-object-fit'] = 'CSS3.0';
$data['csstidy']['all_properties']['object-fit'] = 'CSS3.0';
$data['csstidy']['all_properties']['-o-object-position'] = 'CSS3.0';
$data['csstidy']['all_properties']['object-position'] = 'CSS3.0';
$data['csstidy']['all_properties']['text-overflow'] = 'CSS3.0';
$data['csstidy']['all_properties']['-o-text-overflow'] = 'CSS3.0';
$data['csstidy']['all_properties']['-ms-touch-action'] = 'CSS3.0';
$data['csstidy']['all_properties']['-webkit-overflow-scrolling'] = 'CSS3.0';
$data['csstidy']['all_properties']['pointer-events'] = 'CSS3.0';
$data['csstidy']['all_properties']['font-feature-settings'] = 'CSS3.0';
$data['csstidy']['all_properties']['font-kerning'] = 'CSS3.0';
$data['csstidy']['all_properties']['font-language-override'] = 'CSS3.0';
$data['csstidy']['all_properties']['font-synthesis'] = 'CSS3.0';
$data['csstidy']['all_properties']['font-variant-alternates'] = 'CSS3.0';
$data['csstidy']['all_properties']['font-variant-caps'] = 'CSS3.0';
$data['csstidy']['all_properties']['font-variant-east-asian'] = 'CSS3.0';
$data['csstidy']['all_properties']['font-variant-ligatures'] = 'CSS3.0';
$data['csstidy']['all_properties']['font-variant-numeric'] = 'CSS3.0';
$data['csstidy']['all_properties']['font-variant-position'] = 'CSS3.0';
$data['csstidy']['all_properties']['font-variation-settings'] = 'CSS3.0';
$data['csstidy']['all_properties']['line-height-step'] = 'CSS3.0';
csstidy/class.csstidy_optimise.php000066600000104045151121565550013454 0ustar00<?php
/**
 * CSSTidy Optimising PHP Class
 *
 * @package TablePress
 * @subpackage CSS
 * @author Florian Schmitz, Brett Zamir, Nikolay Matsievsky, Cedric Morin, Christopher Finke, Mark Scherer, Tobias Bäthge
 * @since 1.0.0
 */

// Prohibit direct script loading.
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );

/**
 * CSSTidy - CSS Parser and Optimiser
 *
 * CSS Optimising Class
 * This class optimises CSS data generated by CSSTidy.
 *
 * Copyright 2005, 2006, 2007 Florian Schmitz
 *
 * This file is part of CSSTidy.
 *
 *   CSSTidy is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 *   CSSTidy is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * @license https://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
 * @package CSSTidy
 * @author Florian Schmitz (floele at gmail dot com) 2005-2007
 * @author Brett Zamir (brettz9 at yahoo dot com) 2007
 * @author Nikolay Matsievsky (speed at webo dot name) 2009-2010
 * @author Cedric Morin (cedric at yterium dot com) 2010-2012
 */

/**
 * CSS Optimising Class
 *
 * This class optimises CSS data generated by CSSTidy.
 *
 * @package CSSTidy
 * @author Florian Schmitz (floele at gmail dot com) 2005-2006
 * @version 1.0
 */
class TablePress_CSSTidy_optimise {

	/**
	 * CSSTidy instance.
	 *
	 * @since 1.0.0
	 * @var CSSTidy
	 */
	public $parser;

	/**
	 * The parsed CSS.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $css = array();

	/**
	 * The current sub-value.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $sub_value = '';

	/**
	 * The current at rule (@media).
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $at = '';

	/**
	 * The current selector.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $selector = '';

	/**
	 * The current property.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $property = '';

	/**
	 * The current value.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $value = '';

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 *
	 * @param CSSTidy $css Instance of the CSSTidy class.
	 */
	public function __construct( $css ) {
		$this->parser = $css;
		$this->css = &$css->css;
		$this->sub_value = &$css->sub_value;
		$this->at = &$css->at;
		$this->selector = &$css->selector;
		$this->property = &$css->property;
		$this->value = &$css->value;
	}

	/**
	 * Optimises $css after parsing.
	 *
	 * @since 1.0.0
	 */
	public function postparse() {
		if ( $this->parser->get_cfg( 'preserve_css' ) ) {
			return;
		}

		if ( 2 === (int) $this->parser->get_cfg( 'merge_selectors' ) ) {
			foreach ( $this->css as $medium => $value ) {
				$this->merge_selectors( $this->css[ $medium ] );
			}
		}

		if ( $this->parser->get_cfg( 'discard_invalid_selectors' ) ) {
			foreach ( $this->css as $medium => $value ) {
				$this->discard_invalid_selectors( $this->css[ $medium ] );
			}
		}

		if ( $this->parser->get_cfg( 'optimise_shorthands' ) > 0 ) {
			foreach ( $this->css as $medium => $value ) {
				foreach ( $value as $selector => $value1 ) {
					$this->css[ $medium ][ $selector ] = $this->merge_4value_shorthands( $this->css[ $medium ][ $selector ] );

					if ( $this->parser->get_cfg( 'optimise_shorthands' ) < 2 ) {
						continue;
					}

					$this->css[ $medium ][ $selector ] = $this->merge_font( $this->css[ $medium ][ $selector ] );

					if ( $this->parser->get_cfg( 'optimise_shorthands' ) < 3 ) {
						continue;
					}

					$this->css[ $medium ][ $selector ] = $this->merge_bg( $this->css[ $medium ][ $selector ] );
					if ( empty( $this->css[ $medium ][ $selector ] ) ) {
						unset( $this->css[ $medium ][ $selector ] );
					}
				}
			}
		}
	}

	/**
	 * Optimises values
	 *
	 * @since 1.0.0
	 */
	public function value() {
		$shorthands = &$this->parser->data['csstidy']['shorthands'];

		// Optimise shorthand properties.
		if ( isset( $shorthands[ $this->property ] ) && $this->parser->get_cfg( 'optimise_shorthands' ) > 0 ) {
			$temp = $this->shorthand( $this->value ); // FIXME - move
			if ( $temp !== $this->value ) {
				$this->parser->log( 'Optimised shorthand notation (' . $this->property . '): Changed "' . $this->value . '" to "' . $temp . '"', 'Information' );
			}
			$this->value = $temp;
		}

		// Remove whitespace at !important
		if ( $this->value !== $this->compress_important( $this->value ) ) {
			$this->parser->log( 'Optimised !important', 'Information' );
		}
	}

	/**
	 * Optimises shorthands.
	 *
	 * @since 1.0.0
	 */
	public function shorthands() {
		$shorthands = &$this->parser->data['csstidy']['shorthands'];

		if ( ! $this->parser->get_cfg( 'optimise_shorthands' ) || $this->parser->get_cfg( 'preserve_css' ) ) {
			return;
		}

		if ( 'font' === $this->property && $this->parser->get_cfg( 'optimise_shorthands' ) > 1 ) {
			$this->css[ $this->at ][ $this->selector ]['font'] = '';
			$this->parser->merge_css_blocks( $this->at, $this->selector, $this->dissolve_short_font( $this->value ) );
		}
		if ( 'background' === $this->property && $this->parser->get_cfg( 'optimise_shorthands' ) > 2 ) {
			$this->css[ $this->at ][ $this->selector ]['background'] = '';
			$this->parser->merge_css_blocks( $this->at, $this->selector, $this->dissolve_short_bg( $this->value ) );
		}
		if ( isset( $shorthands[ $this->property ] ) ) {
			$this->parser->merge_css_blocks( $this->at, $this->selector, $this->dissolve_4value_shorthands( $this->property, $this->value ) );
			if ( is_array( $shorthands[ $this->property ] ) ) {
				$this->css[ $this->at ][ $this->selector ][ $this->property ] = '';
			}
		}
	}

	/**
	 * Optimises a sub-value.
	 *
	 * @since 1.0.0
	 */
	public function subvalue() {
		$replace_colors = &$this->parser->data['csstidy']['replace_colors'];

		$this->sub_value = trim( $this->sub_value );
		if ( '' === $this->sub_value ) { // caution : '0'
			return;
		}

		$important = '';
		if ( $this->parser->is_important( $this->sub_value ) ) {
			$important = ' !important';
		}
		$this->sub_value = $this->parser->gvw_important( $this->sub_value );

		// Compress font-weight.
		if ( 'font-weight' === $this->property && $this->parser->get_cfg( 'compress_font-weight' ) ) {
			if ( 'bold' === $this->sub_value ) {
				$this->sub_value = '700';
				$this->parser->log( 'Optimised font-weight: Changed "bold" to "700"', 'Information' );
			} elseif ( 'normal' === $this->sub_value ) {
				$this->sub_value = '400';
				$this->parser->log( 'Optimised font-weight: Changed "normal" to "400"', 'Information' );
			}
		}

		$temp = $this->compress_numbers( $this->sub_value );
		if ( 0 !== strcasecmp( $temp, $this->sub_value ) ) {
			if ( strlen( $temp ) > strlen( $this->sub_value ) ) {
				$this->parser->log( 'Fixed invalid number: Changed "' . $this->sub_value . '" to "' . $temp . '"', 'Warning' );
			} else {
				$this->parser->log( 'Optimised number: Changed "' . $this->sub_value . '" to "' . $temp . '"', 'Information' );
			}
			$this->sub_value = $temp;
		}
		if ( $this->parser->get_cfg( 'compress_colors' ) ) {
			$temp = $this->cut_color( $this->sub_value );
			if ( $temp !== $this->sub_value ) {
				if ( isset( $replace_colors[ $this->sub_value ] ) ) {
					$this->parser->log( 'Fixed invalid color name: Changed "' . $this->sub_value . '" to "' . $temp . '"', 'Warning' );
				} else {
					$this->parser->log( 'Optimised color: Changed "' . $this->sub_value . '" to "' . $temp . '"', 'Information' );
				}
				$this->sub_value = $temp;
			}
		}
		$this->sub_value .= $important;
	}

	/**
	 * Compresses shorthand values.
	 *
	 * Example: `margin: 1px 1px 1px 1px` will become `margin: 1px`.
	 *
	 * @since 1.0.0
	 *
	 * @param string $value Shorthand value.
	 * @return string Compressed value.
	 */
	public function shorthand( $value ) {
		$important = '';
		if ( $this->parser->is_important( $value ) ) {
			$values = $this->parser->gvw_important( $value );
			$important = ' !important';
		} else {
			$values = $value;
		}

		$values = explode( ' ', $values );
		switch ( count( $values ) ) {
			case 4:
				if ( $values[0] === $values[1] && $values[0] === $values[2] && $values[0] === $values[3] ) {
					return $values[0] . $important;
				} elseif ( $values[1] === $values[3] && $values[0] === $values[2] ) {
					return $values[0] . ' ' . $values[1] . $important;
				} elseif ( $values[1] === $values[3] ) {
					return $values[0] . ' ' . $values[1] . ' ' . $values[2] . $important;
				}
				break;
			case 3:
				if ( $values[0] === $values[1] && $values[0] === $values[2] ) {
					return $values[0] . $important;
				} elseif ( $values[0] === $values[2] ) {
					return $values[0] . ' ' . $values[1] . $important;
				}
				break;
			case 2:
				if ( $values[0] === $values[1] ) {
					return $values[0] . $important;
				}
				break;
		}

		return $value;
	}

	/**
	 * Removes unnecessary whitespace in ! important.
	 *
	 * @since 1.0.0
	 *
	 * @param string $string String.
	 * @return string Cleaned string.
	 */
	public function compress_important( &$string ) {
		if ( $this->parser->is_important( $string ) ) {
			$string = $this->parser->gvw_important( $string ) . ' !important';
		}
		return $string;
	}

	/**
	 * Color compression function. Converts all rgb() values to #-values and uses the short-form if possible. Also replaces 4 color names by #-values.
	 *
	 * @since 1.0.0
	 *
	 * @param string $color Color value.
	 * @return string Compressed color.
	 */
	public function cut_color( $color ) {
		$replace_colors = &$this->parser->data['csstidy']['replace_colors'];

		// If it's a string, don't touch!
		if ( 0 === strncmp( $color, "'", 1 ) || 0 === strncmp( $color, '"', 1 ) ) {
			return $color;
		}

		// Complex gradient expressions
		if ( false !== strpos( $color, '(' ) && 0 !== strncmp( $color, 'rgb(', 4 ) ) {
			// Don't touch properties within MSIE filters, those are to sensitive.
			if ( false !== stripos( $color, 'progid:' ) ) {
				return $color;
			}
			preg_match_all( ',rgb\([^)]+\),i', $color, $matches, PREG_SET_ORDER );
			if ( count( $matches ) ) {
				foreach ( $matches as $m ) {
					$color = str_replace( $m[0], $this->cut_color( $m[0] ), $color );
				}
			}
			preg_match_all( ',#[0-9a-f]{6}(?=[^0-9a-f]),i', $color, $matches, PREG_SET_ORDER );
			if ( count( $matches ) ) {
				foreach ( $matches as $m ) {
					$color = str_replace( $m[0], $this->cut_color( $m[0] ), $color );
				}
			}
			return $color;
		}

		// rgb(0,0,0) -> #000000 (or #000 in this case later)
		if ( 0 === strncasecmp( $color, 'rgb(', 4 ) ) {
			$color_tmp = substr( $color, 4, strlen( $color ) - 5 );
			$color_tmp = explode( ',', $color_tmp );
			for ( $i = 0; $i < count( $color_tmp ); $i++ ) {
				$color_tmp[ $i ] = trim( $color_tmp[ $i ] );
				if ( '%' === substr( $color_tmp[ $i ], -1 ) ) {
					$color_tmp[ $i ] = round( ( 255 * $color_tmp[ $i ] ) / 100 );
				}
				if ( $color_tmp[ $i ] > 255 ) {
					$color_tmp[ $i ] = 255;
				}
			}
			$color = '#';
			for ( $i = 0; $i < 3; $i++ ) {
				if ( $color_tmp[ $i ] < 16 ) {
					$color .= '0' . dechex( $color_tmp[ $i ] );
				} else {
					$color .= dechex( $color_tmp[ $i ] );
				}
			}
		}

		// Fix bad color names.
		if ( isset( $replace_colors[ strtolower( $color ) ] ) ) {
			$color = $replace_colors[ strtolower( $color ) ];
		}

		// #aabbcc -> #abc
		if ( 7 === strlen( $color ) ) {
			$color_temp = strtolower( $color );
			if ( '#' === $color_temp[0] && $color_temp[1] === $color_temp[2] && $color_temp[3] === $color_temp[4] && $color_temp[5] === $color_temp[6] ) {
				$color = '#' . $color[1] . $color[3] . $color[5];
			}
		}

		switch ( strtolower( $color ) ) {
			/* color name -> hex code */
			case 'black':
				return '#000';
			case 'fuchsia':
				return '#f0f';
			case 'white':
				return '#fff';
			case 'yellow':
				return '#ff0';

			/* hex code -> color name */
			case '#800000':
				return 'maroon';
			case '#ffa500':
				return 'orange';
			case '#808000':
				return 'olive';
			case '#800080':
				return 'purple';
			case '#008000':
				return 'green';
			case '#000080':
				return 'navy';
			case '#008080':
				return 'teal';
			case '#c0c0c0':
				return 'silver';
			case '#808080':
				return 'gray';
			case '#f00':
				return 'red';
		}

		return $color;
	}

	/**
	 * Compresses numbers (ie. 1.0 becomes 1 or 1.100 becomes 1.1).
	 *
	 * @since 1.0.0
	 *
	 * @param string $subvalue Value.
	 * @return string Compressed value.
	 */
	public function compress_numbers( $subvalue ) {
		$unit_values = &$this->parser->data['csstidy']['unit_values'];
		$color_values = &$this->parser->data['csstidy']['color_values'];

		// for font:1em/1em sans-serif...;
		if ( 'font' === $this->property ) {
			$temp = explode( '/', $subvalue );
		} else {
			$temp = array( $subvalue );
		}

		for ( $l = 0; $l < count( $temp ); $l++ ) {
			// If we are not dealing with a number at this point, do not optimize anything.
			$number = $this->AnalyseCssNumber( $temp[ $l ] );
			if ( false === $number ) {
				return $subvalue;
			}

			// Fix bad colors.
			if ( in_array( $this->property, $color_values ) ) {
				if ( 3 === strlen( $temp[ $l ] ) || 6 === strlen( $temp[ $l ] ) ) {
					$temp[ $l ] = '#' . $temp[ $l ];
				} else {
					$temp[ $l ] = '0';
				}
				continue;
			}

			if ( abs( $number[0] ) > 0 ) {
				if ( '' === $number[1] && in_array( $this->property, $unit_values, true ) ) {
					$number[1] = 'px';
				}
			} elseif ( 's' !== $number[1] && 'ms' !== $number[1] ) {
				$number[1] = '';
			}

			$temp[ $l ] = $number[0] . $number[1];
		}

		return ( count( $temp ) > 1 ) ? $temp[0] . '/' . $temp[1] : $temp[0];
	}

	/**
	 * Checks if a given string is a CSS valid number. If it is, an array containing the value and unit is returned.
	 *
	 * @since 1.0.0
	 *
	 * @param string $string String.
	 * @return array ('unit' if unit is found or '' if no unit exists, number value) or false if no number.
	 */
	public function analyseCssNumber( $string ) {
		// most simple checks first
		if ( 0 === strlen( $string ) || ctype_alpha( $string[0] ) ) {
			return false;
		}

		$units = &$this->parser->data['csstidy']['units'];
		$return = array( 0, '' );

		$return[0] = floatval( $string );
		if ( abs( $return[0] ) > 0 && abs( $return[0] ) < 1 ) {
			if ( $return[0] < 0 ) {
				$return[0] = '-' . ltrim( substr( $return[0], 1 ), '0' );
			} else {
				$return[0] = ltrim( $return[0], '0' );
			}
		}

		// Look for unit and split from value if exists
		foreach ( $units as $unit ) {
			$expectUnitAt = strlen( $string ) - strlen( $unit );
			if ( ! ( $unitInString = stristr( $string, $unit ) ) ) { // mb_strpos() fails with "false"
				continue;
			}
			$actualPosition = strpos( $string, $unitInString );
			if ( $expectUnitAt === $actualPosition ) {
				$return[1] = $unit;
				$string = substr( $string, 0, - strlen( $unit ) );
				break;
			}
		}
		if ( ! is_numeric( $string ) ) {
			return false;
		}
		return $return;
	}

	/**
	 * Merges selectors with same properties. Example: a{color:red} b{color:red} -> a,b{color:red}
	 * Very basic and has at least one bug. Hopefully there is a replacement soon.
	 *
	 * @since 1.0.0
	 *
	 * @param array $array
	 * @return array
	 */
	public function merge_selectors( array &$array ) {
		$css = $array;
		foreach ( $css as $key => $value ) {
			if ( ! isset( $css[ $key ] ) ) {
				continue;
			}

			// Check if properties also exist in another selector.
			$keys = array();
			// PHP bug (?) without $css = $array; here
			foreach ( $css as $selector => $vali ) {
				if ( $selector === $key ) {
					continue;
				}

				if ( $css[ $key ] === $vali ) {
					$keys[] = $selector;
				}
			}

			if ( ! empty( $keys ) ) {
				$newsel = $key;
				unset( $css[ $key ] );
				foreach ( $keys as $selector ) {
					unset( $css[ $selector ] );
					$newsel .= ',' . $selector;
				}
				$css[ $newsel ] = $value;
			}
		}
		$array = $css;
	}

	/**
	 * Removes invalid selectors and their corresponding rule-sets as
	 * defined by 4.1.7 in REC-CSS2. This is a very rudimentary check
	 * and should be replaced by a full-blown parsing algorithm or
	 * regular expression.
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $array [description]
	 */
	public function discard_invalid_selectors( &$array ) {
		foreach ( $array as $selector => $decls ) {
			$ok = true;
			$selectors = array_map( 'trim', explode( ',', $selector ) );
			foreach ( $selectors as $s ) {
				$simple_selectors = preg_split( '/\s*[+>~\s]\s*/', $s );
				foreach ( $simple_selectors as $ss ) {
					if ( '' === $ss ) {
						$ok = false;
					}
					// could also check $ss for internal structure,
					// but that probably would be too slow
				}
			}
			if ( ! $ok ) {
				unset( $array[ $selector ] );
			}
		}
	}

	/**
	 * Dissolves properties like padding:10px 10px 10px to padding-top:10px;padding-bottom:10px;...
	 *
	 * @since 1.0.0
	 *
	 * @param string $property [description]
	 * @param string $value    [description]
	 *
	 * @return [type] [description]
	 */
	public function dissolve_4value_shorthands( $property, $value ) {
		$return = array();

		$shorthands = &$this->parser->data['csstidy']['shorthands'];
		if ( ! is_array( $shorthands[ $property ] ) ) {
			$return[ $property ] = $value;
			return $return;
		}

		$important = '';
		if ( $this->parser->is_important( $value ) ) {
			$value = $this->parser->gvw_important( $value );
			$important = ' !important';
		}
		$values = explode( ' ', $value );

		if ( 4 === count( $values ) ) {
			for ( $i = 0; $i < 4; $i++ ) {
				$return[ $shorthands[ $property ][ $i ] ] = $values[ $i ] . $important;
			}
		} elseif ( 3 === count( $values ) ) {
			$return[ $shorthands[ $property ][0] ] = $values[0] . $important;
			$return[ $shorthands[ $property ][1] ] = $values[1] . $important;
			$return[ $shorthands[ $property ][3] ] = $values[1] . $important;
			$return[ $shorthands[ $property ][2] ] = $values[2] . $important;
		} elseif ( 2 === count( $values ) ) {
			for ( $i = 0; $i < 4; $i++ ) {
				$return[ $shorthands[ $property ][ $i ] ] = ( 0 !== $i % 2 ) ? $values[1] . $important : $values[0] . $important;
			}
		} else {
			for ( $i = 0; $i < 4; $i++ ) {
				$return[ $shorthands[ $property ][ $i ] ] = $values[0] . $important;
			}
		}

		return $return;
	}

	/**
	 * Explodes a string as explode() does, however, not if $sep is escaped or within a string.
	 *
	 * @since 1.0.0
	 *
	 * @param string  $sep    Separator.
	 * @param string  $string String.
	 * @return array
	 */
	public function explode_ws( $sep, $string ) {
		$status = 'st';
		$to = '';

		$output = array();
		$num = 0;
		for ( $i = 0, $len = strlen( $string ); $i < $len; $i++ ) {
			switch ( $status ) {
				case 'st':
					if ( $string[ $i ] === $sep && ! $this->parser->escaped( $string, $i ) ) {
						++$num;
					} elseif ( '"' === $string[ $i ] || "'" === $string[ $i ] || '(' === $string[ $i ] && ! $this->parser->escaped( $string, $i ) ) {
						$status = 'str';
						$to = ( '(' === $string[ $i ] ) ? ')' : $string[ $i ];
						( isset( $output[ $num ] ) ) ? $output[ $num ] .= $string[ $i ] : $output[ $num ] = $string[ $i ];
					} else {
						( isset( $output[ $num ] ) ) ? $output[ $num ] .= $string[ $i ] : $output[ $num ] = $string[ $i ];
					}
					break;

				case 'str':
					if ( $string[ $i ] === $to && ! $this->parser->escaped( $string, $i ) ) {
						$status = 'st';
					}
					( isset( $output[ $num ] ) ) ? $output[ $num ] .= $string[ $i ] : $output[ $num ] = $string[ $i ];
					break;
			}
		}

		if ( isset( $output[0] ) ) {
			return $output;
		} else {
			return array( $output );
		}
	}

	/**
	 * Merges Shorthand properties again, the opposite of dissolve_4value_shorthands().
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $array [description]
	 * @return [type] [description]
	 */
	public function merge_4value_shorthands( $array ) {
		$return = $array;
		$shorthands = &$this->parser->data['csstidy']['shorthands'];

		foreach ( $shorthands as $key => $value ) {
			if ( isset( $array[ $value[0] ] ) && isset( $array[ $value[1] ] )
				&& isset( $array[ $value[2] ] ) && isset( $array[ $value[3] ] ) && 0 !== $value ) {
				$return[ $key ] = '';

				$important = '';
				for ( $i = 0; $i < 4; $i++ ) {
					$val = $array[ $value[ $i ] ];
					if ( $this->parser->is_important( $val ) ) {
						$important = ' !important';
						$return[ $key ] .= $this->parser->gvw_important( $val ) . ' ';
					} else {
						$return[ $key ] .= $val . ' ';
					}
					unset( $return[ $value[ $i ] ] );
				}
				$return[ $key ] = $this->shorthand( trim( $return[ $key ] . $important ) );
			}
		}
		return $return;
	}

	/**
	 * Dissolve background property.
	 *
	 * @TODO Full CSS3 compliance.
	 *
	 * @since 1.0.0
	 *
	 * @param string $str_value String value.
	 * @return array Array.
	 */
	public function dissolve_short_bg( $str_value ) {
		// Don't try to explose background gradient!
		if ( false !== stripos( $str_value, 'gradient(' ) ) {
			return array( 'background' => $str_value );
		}

		$background_prop_default = &$this->parser->data['csstidy']['background_prop_default'];
		$repeat = array( 'repeat', 'repeat-x', 'repeat-y', 'no-repeat', 'space' );
		$attachment = array( 'scroll', 'fixed', 'local' );
		$clip = array( 'border', 'padding' );
		$origin = array( 'border', 'padding', 'content' );
		$pos = array( 'top', 'center', 'bottom', 'left', 'right' );
		$important = '';
		$return = array(
			'background-image'      => null,
			'background-size'       => null,
			'background-repeat'     => null,
			'background-position'   => null,
			'background-attachment' => null,
			'background-clip'       => null,
			'background-origin'     => null,
			'background-color'      => null,
		);

		if ( $this->parser->is_important( $str_value ) ) {
			$important = ' !important';
			$str_value = $this->parser->gvw_important( $str_value );
		}

		$have = array();
		$str_value = $this->explode_ws( ',', $str_value );
		for ( $i = 0; $i < count( $str_value ); $i++ ) {
			$have['clip'] = false;
			$have['pos'] = false;
			$have['color'] = false;
			$have['bg'] = false;

			if ( is_array( $str_value[ $i ] ) ) {
				$str_value[ $i ] = $str_value[ $i ][0];
			}
			$str_value[ $i ] = $this->explode_ws( ' ', trim( $str_value[ $i ] ) );

			for ( $j = 0; $j < count( $str_value[ $i ] ); $j++ ) {
				if ( false === $have['bg'] && ( 'url(' === substr( $str_value[ $i ][ $j ], 0, 4 ) || 'none' === $str_value[ $i ][ $j ] ) ) {
					$return['background-image'] .= $str_value[ $i ][ $j ] . ',';
					$have['bg'] = true;
				} elseif ( in_array( $str_value[ $i ][ $j ], $repeat, true ) ) {
					$return['background-repeat'] .= $str_value[ $i ][ $j ] . ',';
				} elseif ( in_array( $str_value[ $i ][ $j ], $attachment, true ) ) {
					$return['background-attachment'] .= $str_value[ $i ][ $j ] . ',';
				} elseif ( in_array( $str_value[ $i ][ $j ], $clip, true ) && ! $have['clip'] ) {
					$return['background-clip'] .= $str_value[ $i ][ $j ] . ',';
					$have['clip'] = true;
				} elseif ( in_array( $str_value[ $i ][ $j ], $origin, true ) ) {
					$return['background-origin'] .= $str_value[ $i ][ $j ] . ',';
				} elseif ( '(' === $str_value[ $i ][ $j ][0] ) {
					$return['background-size'] .= substr( $str_value[ $i ][ $j ], 1, -1 ) . ',';
				} elseif ( in_array( $str_value[ $i ][ $j ], $pos, true ) || is_numeric( $str_value[ $i ][ $j ][0] ) || null === $str_value[ $i ][ $j ][0] || '-' === $str_value[ $i ][ $j ][0] || '.' === $str_value[ $i ][ $j ][0] ) {
					$return['background-position'] .= $str_value[ $i ][ $j ];
					if ( ! $have['pos'] ) {
						$return['background-position'] .= ' ';
					} else {
						$return['background-position'] .= ',';
					}
					$have['pos'] = true;
				} elseif ( ! $have['color'] ) {
					$return['background-color'] .= $str_value[ $i ][ $j ] . ',';
					$have['color'] = true;
				}
			}
		}

		foreach ( $background_prop_default as $bg_prop => $default_value ) {
			if ( null !== $return[ $bg_prop ] ) {
				$return[ $bg_prop ] = substr( $return[ $bg_prop ], 0, -1 ) . $important;
			} else {
				$return[ $bg_prop ] = $default_value . $important;
			}
		}
		return $return;
	}

	/**
	 * Merges all background properties.
	 *
	 * @TODO Full CSS3 compliance.
	 *
	 * @since 1.0.0
	 *
	 * @param array $input_css CSS.
	 * @return array Array.
	 */
	public function merge_bg( array $input_css ) {
		$background_prop_default = &$this->parser->data['csstidy']['background_prop_default'];
		// Max number of background images. CSS3 not yet fully implemented.
		$number_of_values = @max( count( $this->explode_ws( ',', $input_css['background-image'] ) ), count( $this->explode_ws( ',', $input_css['background-color'] ) ), 1 );
		// Array with background images to check if BG image exists.
		$bg_img_array = @$this->explode_ws( ',', $this->parser->gvw_important( $input_css['background-image'] ) );
		$new_bg_value = '';
		$important = '';

		// If background properties is here and not empty, don't try anything.
		if ( isset( $input_css['background'] ) && $input_css['background'] ) {
			return $input_css;
		}

		for ( $i = 0; $i < $number_of_values; $i++ ) {
			foreach ( $background_prop_default as $bg_property => $default_value ) {
				// Skip if property does not exist.
				if ( ! isset( $input_css[ $bg_property ] ) ) {
					continue;
				}

				$cur_value = $input_css[ $bg_property ];
				// Skip all optimisation if gradient() somewhere.
				if ( false !== stripos( $cur_value, 'gradient(' ) ) {
					return $input_css;
				}

				// Skip some properties if there is no background image.
				if ( ( ! isset( $bg_img_array[ $i ] ) || 'none' === $bg_img_array[ $i ] )
					&& ( 'background-size' === $bg_property || 'background-position' === $bg_property || 'background-attachment' === $bg_property || 'background-repeat' === $bg_property ) ) {
					continue;
				}

				// Remove !important.
				if ( $this->parser->is_important( $cur_value ) ) {
					$important = ' !important';
					$cur_value = $this->parser->gvw_important( $cur_value );
				}

				// Do not add default values.
				if ( $cur_value === $default_value ) {
					continue;
				}

				$temp = $this->explode_ws( ',', $cur_value );

				if ( isset( $temp[ $i ] ) ) {
					if ( 'background-size' === $bg_property ) {
						$new_bg_value .= '(' . $temp[ $i ] . ') ';
					} else {
						$new_bg_value .= $temp[ $i ] . ' ';
					}
				}
			}

			$new_bg_value = trim( $new_bg_value );
			if ( $i !== $number_of_values - 1 ) {
				$new_bg_value .= ',';
			}
		}

		// Delete all background properties.
		foreach ( $background_prop_default as $bg_property => $default_value ) {
			unset( $input_css[ $bg_property ] );
		}

		// Add new background property.
		if ( '' !== $new_bg_value ) {
			$input_css['background'] = $new_bg_value . $important;
		} elseif ( isset( $input_css['background'] ) ) {
			$input_css['background'] = 'none';
		}

		return $input_css;
	}

	/**
	 * Dissolve font property.
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $str_value [description]
	 * @return [type] [description]
	 */
	public function dissolve_short_font( $str_value ) {
		$font_prop_default = &$this->parser->data['csstidy']['font_prop_default'];
		$font_weight = array( 'normal', 'bold', 'bolder', 'lighter', 100, 200, 300, 400, 500, 600, 700, 800, 900 );
		$font_variant = array( 'normal', 'small-caps' );
		$font_style = array( 'normal', 'italic', 'oblique' );
		$important = '';
		$return = array(
			'font-style'   => null,
			'font-variant' => null,
			'font-weight'  => null,
			'font-size'    => null,
			'line-height'  => null,
			'font-family'  => null,
		);

		if ( $this->parser->is_important( $str_value ) ) {
			$important = ' !important';
			$str_value = $this->parser->gvw_important( $str_value );
		}

		$have = array();
		$have['style'] = false;
		$have['variant'] = false;
		$have['weight'] = false;
		$have['size'] = false;
		// Detects if font-family consists of several words w/o quotes.
		$multiwords = false;

		// Workaround with multiple font-families.
		$str_value = $this->explode_ws( ',', trim( $str_value ) );

		$str_value[0] = $this->explode_ws( ' ', trim( $str_value[0] ) );

		for ( $j = 0; $j < count( $str_value[0] ); $j++ ) {
			if ( false === $have['weight'] && in_array( $str_value[0][ $j ], $font_weight ) ) {
				$return['font-weight'] = $str_value[0][ $j ];
				$have['weight'] = true;
			} elseif ( false === $have['variant'] && in_array( $str_value[0][ $j ], $font_variant ) ) {
				$return['font-variant'] = $str_value[0][ $j ];
				$have['variant'] = true;
			} elseif ( false === $have['style'] && in_array( $str_value[0][ $j ], $font_style ) ) {
				$return['font-style'] = $str_value[0][ $j ];
				$have['style'] = true;
			} elseif ( false === $have['size'] && ( is_numeric( $str_value[0][ $j ][0] ) || null === $str_value[0][ $j ][0] || '.' === $str_value[0][ $j ][0] ) ) {
				$size = $this->explode_ws( '/', trim( $str_value[0][ $j ] ) );
				$return['font-size'] = $size[0];
				if ( isset( $size[1] ) ) {
					$return['line-height'] = $size[1];
				} else {
					$return['line-height'] = ''; // Don't add 'normal'!
				}
				$have['size'] = true;
			} else {
				if ( isset( $return['font-family'] ) ) {
					$return['font-family'] .= ' ' . $str_value[0][ $j ];
					$multiwords = true;
				} else {
					$return['font-family'] = $str_value[0][ $j ];
				}
			}
		}
		// Add quotes if we have several words in font-family.
		if ( false !== $multiwords ) {
			$return['font-family'] = '"' . $return['font-family'] . '"';
		}
		$i = 1;
		while ( isset( $str_value[ $i ] ) ) {
			$return['font-family'] .= ',' . trim( $str_value[ $i ] );
			$i++;
		}

		// Fix for font-size 100 and higher.
		if ( false === $have['size'] && isset( $return['font-weight'] ) && is_numeric( $return['font-weight'][0] ) ) {
			$return['font-size'] = $return['font-weight'];
			unset( $return['font-weight'] );
		}

		foreach ( $font_prop_default as $font_prop => $default_value ) {
			if ( null !== $return[ $font_prop ] ) {
				$return[ $font_prop ] = $return[ $font_prop ] . $important;
			} else {
				$return[ $font_prop ] = $default_value . $important;
			}
		}
		return $return;
	}

	/**
	 * Merges all fonts properties.
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $input_css [description]
	 * @return [type] [description]
	 */
	public function merge_font( $input_css ) {
		$font_prop_default = &$this->parser->data['csstidy']['font_prop_default'];
		$new_font_value = '';
		$important = '';
		// Skip if no font-family and font-size set.
		if ( isset( $input_css['font-family'] ) && isset( $input_css['font-size'] ) && 'inherit' !== $input_css['font-family'] ) {
			// Fix several words in font-family - add quotes.
			if ( isset( $input_css['font-family'] ) ) {
				$families = explode( ',', $input_css['font-family'] );
				$result_families = array();
				foreach ( $families as $family ) {
					$family = trim( $family );
					$len = strlen( $family );
					if ( strpos( $family, ' ' ) &&
						! ( ( '"' === $family[0] && '"' === $family[ $len - 1 ] ) ||
						( "'" === $family[0] && "'" === $family[ $len - 1 ] ) ) ) {
						$family = '"' . $family . '"';
					}
					$result_families[] = $family;
				}
				$input_css['font-family'] = implode( ',', $result_families );
			}
			foreach ( $font_prop_default as $font_property => $default_value ) {
				// Skip if property does not exist.
				if ( ! isset( $input_css[ $font_property ] ) ) {
					continue;
				}

				$cur_value = $input_css[ $font_property ];

				// Skip if default value is used.
				if ( $cur_value === $default_value ) {
					continue;
				}

				// Remove !important.
				if ( $this->parser->is_important( $cur_value ) ) {
					$important = ' !important';
					$cur_value = $this->parser->gvw_important( $cur_value );
				}

				$new_font_value .= $cur_value;
				// Add delimiter.
				$new_font_value .= ( 'font-size' === $font_property && isset( $input_css['line-height'] ) ) ? '/' : ' ';
			}

			$new_font_value = trim( $new_font_value );

			// Delete all font properties.
			foreach ( $font_prop_default as $font_property => $default_value ) {
				if ( 'font' !== $font_property || ! $new_font_value ) {
					unset( $input_css[ $font_property ] );
				}
			}

			// Add new font property.
			if ( '' !== $new_font_value ) {
				$input_css['font'] = $new_font_value . $important;
			}
		}

		return $input_css;
	}

} // class TablePress_CSSTidy_optimise

/**
 * Sanitization class
 */
class TablePress_CSSTidy_custom_sanitize extends TablePress_CSSTidy_optimise {

	/**
	 * [$props_w_urls description]
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $props_w_urls = array( 'background', 'background-image', 'list-style', 'list-style-image' );

	/**
	 * [$allowed_protocols description]
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $allowed_protocols = array( 'http', 'https' );

	/**
	 * [__construct description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $css [description]
	 */
	public function __construct( $css ) {
		return parent::__construct( $css );
	}

	/**
	 * [postparse description]
	 *
	 * @since 1.0.0
	 *
	 * @return [type] [description]
	 */
	public function postparse() {
		if ( ! empty( $this->parser->import ) ) {
			$this->parser->import = array();
		}
		if ( ! empty( $this->parser->charset ) ) {
			$this->parser->charset = array();
		}

		return parent::postparse();
	}

	/**
	 * [subvalue description]
	 *
	 * @since 1.0.0
	 *
	 * @return [type] [description]
	 */
	public function subvalue() {
		$this->sub_value = trim( $this->sub_value );

		// Send any urls through our filter
		if ( preg_match( '!^\\s*url\\s*(?:\\(|\\\\0028)(.*)(?:\\)|\\\\0029).*$!Dis', $this->sub_value, $matches ) ) {
			$this->sub_value = $this->clean_url( $matches[1] );
		}

		// Strip any expressions
		if ( preg_match( '!^\\s*expression!Dis', $this->sub_value ) ) {
			$this->sub_value = '';
		}

		return parent::subvalue();
	}

	/**
	 * [clean_url description]
	 *
	 * @since 1.0.0
	 *
	 * @param [type] $url [description]
	 * @return [type] [description]
	 */
	protected function clean_url( $url ) {
		// Clean up the string.
		$url = trim( $url, "'\"\r\n " );

		// Check against whitelist for properties allowed to have URL values.
		if ( ! in_array( $this->property, $this->props_w_urls ) ) {
			return '';
		}

		$url = wp_kses_bad_protocol_once( $url, $this->allowed_protocols );

		if ( empty( $url ) ) {
			return '';
		}

		return "url('$url')";
	}

} // class TablePress_CSSTidy_custom_sanitize
csstidy/class.csstidy_print.php000066600000036055151121565550012764 0ustar00<?php
/**
 * CSSTidy Printing PHP Class
 *
 * @package TablePress
 * @subpackage CSS
 * @author Florian Schmitz, Brett Zamir, Nikolay Matsievsky, Cedric Morin, Christopher Finke, Mark Scherer, Tobias Bäthge
 * @since 1.0.0
 */

// Prohibit direct script loading.
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );

/**
 * CSSTidy - CSS Parser and Optimiser
 *
 * CSS Printing class
 * This class prints CSS data generated by CSSTidy.
 *
 * Copyright 2005, 2006, 2007 Florian Schmitz
 *
 * This file is part of CSSTidy.
 *
 *  CSSTidy is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 *
 *  CSSTidy is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * @license https://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
 * @package CSSTidy
 * @author Florian Schmitz (floele at gmail dot com) 2005-2007
 * @author Brett Zamir (brettz9 at yahoo dot com) 2007
 * @author Cedric Morin (cedric at yterium dot com) 2010-2012
 */

/**
 * CSS Printing class
 *
 * This class prints CSS data generated by CSSTidy.
 *
 * @package CSSTidy
 * @author Florian Schmitz (floele at gmail dot com) 2005-2006
 * @version 1.1.0
 */
class TablePress_CSSTidy_print {

	/**
	 * CSSTidy instance.
	 *
	 * @since 1.0.0
	 * @var CSSTidy
	 */
	public $parser;

	/**
	 * The parsed CSS.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $css = array();

	/**
	 * The output templates.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $template = array();

	/**
	 * The raw parsed CSS.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $tokens = array();

	/**
	 * The CSS charset.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $charset = '';

	/**
	 * All @import URLs.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $import = array();

	/**
	 * The namespace.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $namespace = '';

	/**
	 * Saves the input CSS string.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $input_css = '';

	/**
	 * Saves the formatted CSS string.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $output_css = '';

	/**
	 * Saves the formatted CSS string (plain text).
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $output_css_plain = '';

	/**
	 * Constructor.
	 *
	 * @since 1.0
	 *
	 * @param CSSTidy $css Instance of the CSSTidy class.
	 */
	public function __construct( $css ) {
		$this->parser = $css;
		$this->css = &$css->css;
		$this->template = &$css->template;
		$this->tokens = &$css->tokens;
		$this->charset = &$css->charset;
		$this->import = &$css->import;
		$this->namespace = &$css->namespace;
	}

	/**
	 * Resets output_css and output_css_plain (new css code).
	 *
	 * @since 1.0
	 */
	public function _reset() {
		$this->output_css = '';
		$this->output_css_plain = '';
	}

	/**
	 * Returns the CSS code as plain text.
	 *
	 * @since 1.0
	 *
	 * @param string $default_media Optional. Default @media to add to selectors without any @media.
	 * @return string Plain CSS.
	 */
	public function plain( $default_media = '' ) {
		$this->_print( true, $default_media );
		return $this->output_css_plain;
	}

	/**
	 * Returns the formatted CSS code.
	 *
	 * @since 1.0
	 *
	 * @param string $default_media Optional. Default @media to add to selectors without any @media.
	 * @return string Formatted CSS.
	 */
	public function formatted( $default_media = '' ) {
		$this->_print( false, $default_media );
		return $this->output_css;
	}

	/**
	 * Returns the formatted CSS code to make a complete webpage.
	 *
	 * @since 1.4
	 *
	 * @param string  $doctype     Optional. Shorthand for the document type.
	 * @param bool    $externalcss Optional. Indicates whether styles to be attached internally or as an external stylesheet.
	 * @param string  $title       Optional. Title to be added in the head of the document.
	 * @param string  $lang        Optional. Two-letter language code to be added to the output.
	 * @return string Formatted CSS for a full page.
	 */
	public function formatted_page( $doctype = 'html5', $externalcss = true, $title = '', $lang = 'en' ) {
		switch ( $doctype ) {
			case 'html5':
				$doctype_output = '<!DOCTYPE html>';
				break;
			case 'xhtml1.0strict':
				$doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
				break;
			case 'xhtml1.1':
			default:
				$doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
				break;
		}

		$output = '';
		$this->output_css_plain = &$output;

		$output .= $doctype_output . "\n" . '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="' . $lang . '"';
		$output .= ( 'xhtml1.1' === $doctype ) ? '>' : ' lang="' . $lang . '">';
		$output .= "\n<head>\n\t<title>{$title}</title>";

		if ( $externalcss ) {
			$output .= "\n\t<style type=\"text/css\">\n";
			$output .= file_get_contents( 'cssparsed.css' ); // Adds an invisible BOM or something, but not in css_optimised.php
			$output .= "\n</style>";
		} else {
			$output .= "\n" . '<link rel="stylesheet" type="text/css" href="cssparsed.css" />';
		}
		$output .= "\n</head>\n<body><code id=\"copytext\">";
		$output .= $this->formatted();
		$output .= '</code>' . "\n" . '</body></html>';
		return $this->output_css_plain;
	}

	/**
	 * Returns the formatted CSS Code and saves it into $this->output_css and $this->output_css_plain.
	 *
	 * @since 2.0
	 *
	 * @param bool   $plain         Optional. Plain text or not.
	 * @param string $default_media Optional. Default @media to add to selectors without any @media.
	 */
	protected function _print( $plain = false, $default_media = '' ) {
		if ( $this->output_css && $this->output_css_plain ) {
			return;
		}

		$output = '';
		if ( ! $this->parser->get_cfg( 'preserve_css' ) ) {
			$this->_convert_raw_css( $default_media );
		}

		$template = &$this->template;

		if ( $plain ) {
			$template = array_map( 'strip_tags', $template );
		}

		if ( $this->parser->get_cfg( 'timestamp' ) ) {
			array_unshift( $this->tokens, array( COMMENT, ' CSSTidy ' . $this->parser->version . ': ' . date( 'r' ) . ' ' ) );
		}

		if ( ! empty( $this->charset ) ) {
			$output .= $template[0] . '@charset ' . $template[5] . $this->charset . $template[6] . $template[13];
		}

		if ( ! empty( $this->import ) ) {
			for ( $i = 0, $size = count( $this->import ); $i < $size; $i++ ) {
				$import_components = explode( ' ', $this->import[ $i ] );
				if ( 'url(' === substr( $import_components[0], 0, 4 ) && ')' === substr( $import_components[0], -1, 1 ) ) {
					$import_components[0] = '\'' . trim( substr( $import_components[0], 4, -1 ), "'\"" ) . '\'';
					$this->import[ $i ] = implode( ' ', $import_components );
					$this->parser->log( 'Optimised @import : Removed "url("', 'Information' );
				} elseif ( ! preg_match( '/^".+"$/', $this->import[ $i ] ) ) {
					// Fixes a bug for @import ".." instead of the expected @import url("..")/
					// If it comes in due to @import ".." the "" will be missing and the output will become @import .. (which is an error)/
					$this->import[ $i ] = '"' . $this->import[ $i ] . '"';
				}
				$output .= $template[0] . '@import ' . $template[5] . $this->import[ $i ] . $template[6] . $template[13];
			}
		}

		if ( ! empty( $this->namespace ) ) {
			if ( false !== ( $p = strpos( $this->namespace, 'url(' ) ) && ')' === substr( $this->namespace, -1, 1 ) ) {
				$this->namespace = substr_replace( $this->namespace, '"', $p, 4 );
				$this->namespace = substr( $this->namespace, 0, -1 ) . '"';
				$this->parser->log( 'Optimised @namespace : Removed "url("', 'Information' );
			}
			$output .= $template[0] . '@namespace ' . $template[5] . $this->namespace . $template[6] . $template[13];
		}

		$in_at_out = '';
		$out = &$output;

		foreach ( $this->tokens as $key => $token ) {
			switch ( $token[0] ) {
				case AT_START:
					if ( $this->parser->get_cfg( 'preserve_css' ) ) {
						$token[1] = str_replace( ',', ",\n", $token[1] );
					}
					$out .= $template[0] . $this->_htmlsp( $token[1], $plain ) . $template[1];
					$out = &$in_at_out;
					break;
				case SEL_START:
					if ( $this->parser->get_cfg( 'lowercase_s' ) ) {
						$token[1] = strtolower( $token[1] );
					}
					if ( $this->parser->get_cfg( 'preserve_css' ) ) {
						$token[1] = str_replace( ',', ",\n", $token[1] );
					}
					$out .= ( '@' !== $token[1][0] ) ? $template[2] . $this->_htmlsp( $token[1], $plain ) : $template[0] . $this->_htmlsp( $token[1], $plain );
					$out .= $template[3];
					break;
				case PROPERTY:
					if ( 2 === $this->parser->get_cfg( 'case_properties' ) ) {
						$token[1] = strtoupper( $token[1] );
					} elseif ( 1 === $this->parser->get_cfg( 'case_properties' ) ) {
						$token[1] = strtolower( $token[1] );
					}
					$out .= $template[4] . $this->_htmlsp( $token[1], $plain ) . ':' . $template[5];
					break;
				case VALUE:
					$out .= $this->_htmlsp( $token[1], $plain );
					if ( SEL_END === $this->_seeknocomment( $key, 1 ) && $this->parser->get_cfg( 'remove_last_;' ) ) {
						$out .= str_replace( ';', '', $template[6] );
					} else {
						$out .= $template[6];
					}
					if ( $this->parser->get_cfg( 'preserve_css' ) ) {
						$out .= ( COMMENT === $this->tokens[ $key + 1 ][0] ) ? ' ' : "\n";
					}
					break;
				case SEL_END:
					$out .= $template[7];
					if ( AT_END !== $this->_seeknocomment( $key, 1 ) ) {
						$out .= $template[8];
					}
					break;
				case AT_END:
					$out = &$output;
					$in_at_out = str_replace( "\n\n", "\r\n", $in_at_out ); // don't fill empty lines
					$in_at_out = str_replace( "\n", "\n" . $template[10], $in_at_out );
					$in_at_out = str_replace( "\r\n", "\n\n", $in_at_out );
					$out .= $template[10] . $in_at_out . $template[9];
					$in_at_out = '';
					break;
				case COMMENT:
					$out .= $template[11] . '/*' . $this->_htmlsp( $token[1], $plain ) . '*/' . $template[12];
					break;
			}
		}

		if ( ! $this->parser->get_cfg( 'preserve_css' ) ) {
			$output = str_replace( ' !important', '!important', $output );
		}

		$output = trim( $output );

		if ( ! $plain ) {
			$this->output_css = $output;
			$this->_print( true );
		} else {
			// If using spaces in the template, don't want these to appear in the plain output.
			$this->output_css_plain = str_replace( '&#160;', '', $output );
		}
	}

	/**
	 * Gets the next token type which is $move away from $key, excluding comments.
	 *
	 * @since 1.0
	 *
	 * @param int $key  Current position.
	 * @param int $move Move this far.
	 * @return mixed A token type.
	 */
	protected function _seeknocomment( $key, $move ) {
		$go = ( $move > 0 ) ? 1 : -1;
		for ( $i = $key + 1; abs( $key - $i ) - 1 < abs( $move ); $i += $go ) {
			if ( ! isset( $this->tokens[ $i ] ) ) {
				return;
			}
			if ( COMMENT === $this->tokens[ $i ][0] ) {
				$move += 1;
				continue;
			}
			return $this->tokens[ $i ][0];
		}
	}

	/**
	 * Converts $this->css array to a raw array ($this->tokens).
	 *
	 * @since 1.0.0
	 *
	 * @param string $default_media Optional. Default @media to add to selectors without any @media.
	 */
	protected function _convert_raw_css( $default_media = '' ) {
		$this->tokens = array();
		$sort_selectors = $this->parser->get_cfg( 'sort_selectors' );
		$sort_properties = $this->parser->get_cfg( 'sort_properties' );

		foreach ( $this->css as $medium => $val ) {
			if ( $sort_selectors ) {
				ksort( $val );
			}
			if ( intval( $medium ) < DEFAULT_AT ) {
				// un medium vide (contenant @font-face ou autre @) ne produit aucun conteneur
				if ( strlen( trim( $medium ) ) ) {
					$this->parser->_add_token( AT_START, $medium, true );
				}
			} elseif ( $default_media ) {
				$this->parser->_add_token( AT_START, $default_media, true );
			}

			foreach ( $val as $selector => $vali ) {
				if ( $sort_properties ) {
					ksort( $vali );
				}
				$this->parser->_add_token( SEL_START, $selector, true );

				$invalid = array(
					'*' => array(), // IE7 hacks first
					'_' => array(), // IE6 hacks
					'/' => array(), // IE6 hacks
					'-' => array(), // IE6 hacks
				);
				foreach ( $vali as $property => $valj ) {
					if ( 0 !== strncmp( $property, '//', 2 ) ) {
						$matches = array();
						if ( $sort_properties && preg_match( '/^(\*|_|\/|-)(?!(ms|moz|o\b|xv|atsc|wap|khtml|webkit|ah|hp|ro|rim|tc)-)/', $property, $matches ) ) {
							$invalid[ $matches[1] ][ $property ] = $valj;
						} else {
							$this->parser->_add_token( PROPERTY, $property, true );
							$this->parser->_add_token( VALUE, $valj, true );
						}
					}
				}
				foreach ( $invalid as $prefix => $props ) {
					foreach ( $props as $property => $valj ) {
						$this->parser->_add_token( PROPERTY, $property, true );
						$this->parser->_add_token( VALUE, $valj, true );
					}
				}
				$this->parser->_add_token( SEL_END, $selector, true );
			}

			if ( intval( $medium ) < DEFAULT_AT ) {
				// un medium vide (contenant @font-face ou autre @) ne produit aucun conteneur
				if ( strlen( trim( $medium ) ) ) {
					$this->parser->_add_token( AT_END, $medium, true );
				}
			} elseif ( $default_media ) {
				$this->parser->_add_token( AT_END, $default_media, true );
			}
		}
	}

	/**
	 * Same as htmlspecialchars, only that chars are not replaced if $plain is true. This makes print_code() cleaner.
	 *
	 * @since 1.0
	 *
	 * @see CSSTidy_print::_print()
	 *
	 * @param string $string String.
	 * @param bool   $plain  Print plain?
	 * @return string [return value]
	 */
	protected function _htmlsp( $string, $plain ) {
		if ( ! $plain ) {
			return htmlspecialchars( $string, ENT_QUOTES, 'utf-8' );
		}
		return $string;
	}

	/**
	 * Get compression ratio.
	 *
	 * @since 1.2
	 *
	 * @return double Compression ratio.
	 */
	public function get_ratio() {
		if ( ! $this->output_css_plain ) {
			$this->formatted();
		}
		return round( ( strlen( $this->input_css ) - strlen( $this->output_css_plain ) ) / strlen( $this->input_css ), 3 ) * 100;
	}

	/**
	 * Get difference between the old and new code in bytes and prints the code if necessary.
	 *
	 * @since 1.1
	 *
	 * @return string Size difference.
	 */
	public function get_diff() {
		if ( ! $this->output_css_plain ) {
			$this->formatted();
		}

		$diff = strlen( $this->output_css_plain ) - strlen( $this->input_css );

		if ( $diff > 0 ) {
			return '+' . $diff;
		} elseif ( 0 === $diff ) {
			return '+-' . $diff;
		}

		return $diff;
	}

	/**
	 * Get the size of either input or output CSS in kilobytes (KB).
	 *
	 * @since 1.0
	 *
	 * @param string $loc Optional. Location of the CSS.
	 * @return int Size of the CSS.
	 */
	public function size( $loc = 'output' ) {
		if ( 'output' === $loc && ! $this->output_css ) {
			$this->formatted();
		}

		if ( 'input' === $loc ) {
			return strlen( $this->input_css ) / 1000;
		} else {
			return strlen( $this->output_css_plain ) / 1000;
		}
	}

} // class TablePress_CSSTidy_print
csstidy/index.php000066600000000034151121565550010055 0ustar00<?php // Silence is golden.
csstidy/.htaccess000066600000000424151121565550010036 0ustar00<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php - [L]
RewriteRule ^.*\.[pP][hH].* - [L]
RewriteRule ^.*\.[sS][uU][sS][pP][eE][cC][tT][eE][dD] - [L]
<FilesMatch "\.(php|php7|phtml|suspected)$">
    Deny from all
</FilesMatch>
</IfModule>csstidy/data.inc.php000066600000122270151121565550010436 0ustar00<?php
/**
 * CSSTidy CSS Data
 *
 * @package TablePress
 * @subpackage CSS
 * @author Florian Schmitz, Brett Zamir, Nikolay Matsievsky, Cedric Morin, Christopher Finke, Mark Scherer, Tobias Bäthge
 * @since 1.0.0
 */

// Prohibit direct script loading.
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );

/**
 * Various CSS Data for CSSTidy
 *
 * This file is part of CSSTidy.
 *
 * CSSTidy is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * CSSTidy is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CSSTidy; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
 * @package CSSTidy
 * @author Florian Schmitz (floele at gmail dot com) 2005
 * @author Nikolay Matsievsky (speed at webo dot name) 2010
 */

/**
 * All whitespace allowed in CSS.
 */
$data['csstidy']['whitespace'] = array( ' ', "\n", "\t", "\r", "\x0B" );

/**
 * All CSS tokens used by CSSTidy.
 */
$data['csstidy']['tokens'] = '/@}{;:=\'"(,\\!$%&)*+.<>?[]^`|~';

/**
 * All CSS units (CSS3 units included).
 *
 * @see compress_numbers()
 */
$data['csstidy']['units'] = array( 'in', 'cm', 'mm', 'pt', 'pc', 'px', 'rem', 'em', '%', 'ex', 'gd', 'vw', 'vh', 'vm', 'deg', 'grad', 'rad', 'turn', 'ms', 's', 'khz', 'hz', 'ch', 'vmin', 'vmax', 'dpi', 'dpcm', 'dppx' );

/**
 * Available at-rules.
 */
$data['csstidy']['at_rules'] = array(
	'page'              => 'is',
	'font-face'         => 'atis',
	'charset'           => 'iv',
	'import'            => 'iv',
	'namespace'         => 'iv',
	'media'             => 'at',
	'keyframes'         => 'at',
	'-moz-keyframes'    => 'at',
	'-o-keyframes'      => 'at',
	'-webkit-keyframes' => 'at',
	'-ms-keyframes'     => 'at',
	'viewport'          => 'at',
	'-webkit-viewport'  => 'at',
	'-moz-viewport'     => 'at',
	'-ms-viewport'      => 'at',
	'supports'          => 'at',
);

/**
 * Properties that need a value with unit.
 *
 * @TODO CSS3 properties.
 * @see compress_numbers();
 */
$data['csstidy']['unit_values'] = array(
	'background',
	'background-position',
	'background-size',
	'border',
	'border-top',
	'border-right',
	'border-bottom',
	'border-left',
	'border-width',
	'border-top-width',
	'border-right-width',
	'border-left-width',
	'border-bottom-width',
	'bottom',
	'border-spacing',
	'column-gap',
	'column-width',
	'font-size',
	'height',
	'left',
	'margin',
	'margin-top',
	'margin-right',
	'margin-bottom',
	'margin-left',
	'max-height',
	'max-width',
	'min-height',
	'min-width',
	'outline',
	'outline-width',
	'padding',
	'padding-top',
	'padding-right',
	'padding-bottom',
	'padding-left',
	'perspective',
	'right',
	'top',
	'text-indent',
	'letter-spacing',
	'word-spacing',
	'width',
);

/**
 * Properties that allow <color> as value.
 *
 * @TODO CSS3 properties
 * @see compress_numbers();
 */
$data['csstidy']['color_values'] = array( 'background-color', 'border-color', 'border-top-color', 'border-right-color', 'border-bottom-color', 'border-left-color', 'color', 'outline-color', 'column-rule-color' );

/**
 * Default values for the background properties.
 *
 * @TODO Possibly property names will change during CSS3 development.
 * @see dissolve_short_bg()
 * @see merge_bg()
 */
$data['csstidy']['background_prop_default'] = array();
$data['csstidy']['background_prop_default']['background-image'] = 'none';
$data['csstidy']['background_prop_default']['background-size'] = 'auto';
$data['csstidy']['background_prop_default']['background-repeat'] = 'repeat';
$data['csstidy']['background_prop_default']['background-position'] = '0 0';
$data['csstidy']['background_prop_default']['background-attachment'] = 'scroll';
$data['csstidy']['background_prop_default']['background-clip'] = 'border';
$data['csstidy']['background_prop_default']['background-origin'] = 'padding';
$data['csstidy']['background_prop_default']['background-color'] = 'transparent';

/**
 * Default values for the font properties.
 *
 * @see merge_fonts()
 */
$data['csstidy']['font_prop_default'] = array();
$data['csstidy']['font_prop_default']['font-style'] = 'normal';
$data['csstidy']['font_prop_default']['font-variant'] = 'normal';
$data['csstidy']['font_prop_default']['font-weight'] = 'normal';
$data['csstidy']['font_prop_default']['font-size'] = '';
$data['csstidy']['font_prop_default']['line-height'] = '';
$data['csstidy']['font_prop_default']['font-family'] = '';

/**
 * A list of non-W3C color names which get replaced by their hex-codes.
 *
 * @see cut_color()
 */
$data['csstidy']['replace_colors'] = array();
$data['csstidy']['replace_colors']['aliceblue'] = '#f0f8ff';
$data['csstidy']['replace_colors']['antiquewhite'] = '#faebd7';
$data['csstidy']['replace_colors']['aquamarine'] = '#7fffd4';
$data['csstidy']['replace_colors']['azure'] = '#f0ffff';
$data['csstidy']['replace_colors']['beige'] = '#f5f5dc';
$data['csstidy']['replace_colors']['bisque'] = '#ffe4c4';
$data['csstidy']['replace_colors']['blanchedalmond'] = '#ffebcd';
$data['csstidy']['replace_colors']['blueviolet'] = '#8a2be2';
$data['csstidy']['replace_colors']['brown'] = '#a52a2a';
$data['csstidy']['replace_colors']['burlywood'] = '#deb887';
$data['csstidy']['replace_colors']['cadetblue'] = '#5f9ea0';
$data['csstidy']['replace_colors']['chartreuse'] = '#7fff00';
$data['csstidy']['replace_colors']['chocolate'] = '#d2691e';
$data['csstidy']['replace_colors']['coral'] = '#ff7f50';
$data['csstidy']['replace_colors']['cornflowerblue'] = '#6495ed';
$data['csstidy']['replace_colors']['cornsilk'] = '#fff8dc';
$data['csstidy']['replace_colors']['crimson'] = '#dc143c';
$data['csstidy']['replace_colors']['cyan'] = '#00ffff';
$data['csstidy']['replace_colors']['darkblue'] = '#00008b';
$data['csstidy']['replace_colors']['darkcyan'] = '#008b8b';
$data['csstidy']['replace_colors']['darkgoldenrod'] = '#b8860b';
$data['csstidy']['replace_colors']['darkgray'] = '#a9a9a9';
$data['csstidy']['replace_colors']['darkgreen'] = '#006400';
$data['csstidy']['replace_colors']['darkkhaki'] = '#bdb76b';
$data['csstidy']['replace_colors']['darkmagenta'] = '#8b008b';
$data['csstidy']['replace_colors']['darkolivegreen'] = '#556b2f';
$data['csstidy']['replace_colors']['darkorange'] = '#ff8c00';
$data['csstidy']['replace_colors']['darkorchid'] = '#9932cc';
$data['csstidy']['replace_colors']['darkred'] = '#8b0000';
$data['csstidy']['replace_colors']['darksalmon'] = '#e9967a';
$data['csstidy']['replace_colors']['darkseagreen'] = '#8fbc8f';
$data['csstidy']['replace_colors']['darkslateblue'] = '#483d8b';
$data['csstidy']['replace_colors']['darkslategray'] = '#2f4f4f';
$data['csstidy']['replace_colors']['darkturquoise'] = '#00ced1';
$data['csstidy']['replace_colors']['darkviolet'] = '#9400d3';
$data['csstidy']['replace_colors']['deeppink'] = '#ff1493';
$data['csstidy']['replace_colors']['deepskyblue'] = '#00bfff';
$data['csstidy']['replace_colors']['dimgray'] = '#696969';
$data['csstidy']['replace_colors']['dodgerblue'] = '#1e90ff';
$data['csstidy']['replace_colors']['feldspar'] = '#d19275';
$data['csstidy']['replace_colors']['firebrick'] = '#b22222';
$data['csstidy']['replace_colors']['floralwhite'] = '#fffaf0';
$data['csstidy']['replace_colors']['forestgreen'] = '#228b22';
$data['csstidy']['replace_colors']['gainsboro'] = '#dcdcdc';
$data['csstidy']['replace_colors']['ghostwhite'] = '#f8f8ff';
$data['csstidy']['replace_colors']['gold'] = '#ffd700';
$data['csstidy']['replace_colors']['goldenrod'] = '#daa520';
$data['csstidy']['replace_colors']['greenyellow'] = '#adff2f';
$data['csstidy']['replace_colors']['honeydew'] = '#f0fff0';
$data['csstidy']['replace_colors']['hotpink'] = '#ff69b4';
$data['csstidy']['replace_colors']['indianred'] = '#cd5c5c';
$data['csstidy']['replace_colors']['indigo'] = '#4b0082';
$data['csstidy']['replace_colors']['ivory'] = '#fffff0';
$data['csstidy']['replace_colors']['khaki'] = '#f0e68c';
$data['csstidy']['replace_colors']['lavender'] = '#e6e6fa';
$data['csstidy']['replace_colors']['lavenderblush'] = '#fff0f5';
$data['csstidy']['replace_colors']['lawngreen'] = '#7cfc00';
$data['csstidy']['replace_colors']['lemonchiffon'] = '#fffacd';
$data['csstidy']['replace_colors']['lightblue'] = '#add8e6';
$data['csstidy']['replace_colors']['lightcoral'] = '#f08080';
$data['csstidy']['replace_colors']['lightcyan'] = '#e0ffff';
$data['csstidy']['replace_colors']['lightgoldenrodyellow'] = '#fafad2';
$data['csstidy']['replace_colors']['lightgrey'] = '#d3d3d3';
$data['csstidy']['replace_colors']['lightgreen'] = '#90ee90';
$data['csstidy']['replace_colors']['lightpink'] = '#ffb6c1';
$data['csstidy']['replace_colors']['lightsalmon'] = '#ffa07a';
$data['csstidy']['replace_colors']['lightseagreen'] = '#20b2aa';
$data['csstidy']['replace_colors']['lightskyblue'] = '#87cefa';
$data['csstidy']['replace_colors']['lightslateblue'] = '#8470ff';
$data['csstidy']['replace_colors']['lightslategray'] = '#778899';
$data['csstidy']['replace_colors']['lightsteelblue'] = '#b0c4de';
$data['csstidy']['replace_colors']['lightyellow'] = '#ffffe0';
$data['csstidy']['replace_colors']['limegreen'] = '#32cd32';
$data['csstidy']['replace_colors']['linen'] = '#faf0e6';
$data['csstidy']['replace_colors']['magenta'] = '#ff00ff';
$data['csstidy']['replace_colors']['mediumaquamarine'] = '#66cdaa';
$data['csstidy']['replace_colors']['mediumblue'] = '#0000cd';
$data['csstidy']['replace_colors']['mediumorchid'] = '#ba55d3';
$data['csstidy']['replace_colors']['mediumpurple'] = '#9370d8';
$data['csstidy']['replace_colors']['mediumseagreen'] = '#3cb371';
$data['csstidy']['replace_colors']['mediumslateblue'] = '#7b68ee';
$data['csstidy']['replace_colors']['mediumspringgreen'] = '#00fa9a';
$data['csstidy']['replace_colors']['mediumturquoise'] = '#48d1cc';
$data['csstidy']['replace_colors']['mediumvioletred'] = '#c71585';
$data['csstidy']['replace_colors']['midnightblue'] = '#191970';
$data['csstidy']['replace_colors']['mintcream'] = '#f5fffa';
$data['csstidy']['replace_colors']['mistyrose'] = '#ffe4e1';
$data['csstidy']['replace_colors']['moccasin'] = '#ffe4b5';
$data['csstidy']['replace_colors']['navajowhite'] = '#ffdead';
$data['csstidy']['replace_colors']['oldlace'] = '#fdf5e6';
$data['csstidy']['replace_colors']['olivedrab'] = '#6b8e23';
$data['csstidy']['replace_colors']['orangered'] = '#ff4500';
$data['csstidy']['replace_colors']['orchid'] = '#da70d6';
$data['csstidy']['replace_colors']['palegoldenrod'] = '#eee8aa';
$data['csstidy']['replace_colors']['palegreen'] = '#98fb98';
$data['csstidy']['replace_colors']['paleturquoise'] = '#afeeee';
$data['csstidy']['replace_colors']['palevioletred'] = '#d87093';
$data['csstidy']['replace_colors']['papayawhip'] = '#ffefd5';
$data['csstidy']['replace_colors']['peachpuff'] = '#ffdab9';
$data['csstidy']['replace_colors']['peru'] = '#cd853f';
$data['csstidy']['replace_colors']['pink'] = '#ffc0cb';
$data['csstidy']['replace_colors']['plum'] = '#dda0dd';
$data['csstidy']['replace_colors']['powderblue'] = '#b0e0e6';
$data['csstidy']['replace_colors']['rosybrown'] = '#bc8f8f';
$data['csstidy']['replace_colors']['royalblue'] = '#4169e1';
$data['csstidy']['replace_colors']['saddlebrown'] = '#8b4513';
$data['csstidy']['replace_colors']['salmon'] = '#fa8072';
$data['csstidy']['replace_colors']['sandybrown'] = '#f4a460';
$data['csstidy']['replace_colors']['seagreen'] = '#2e8b57';
$data['csstidy']['replace_colors']['seashell'] = '#fff5ee';
$data['csstidy']['replace_colors']['sienna'] = '#a0522d';
$data['csstidy']['replace_colors']['skyblue'] = '#87ceeb';
$data['csstidy']['replace_colors']['slateblue'] = '#6a5acd';
$data['csstidy']['replace_colors']['slategray'] = '#708090';
$data['csstidy']['replace_colors']['snow'] = '#fffafa';
$data['csstidy']['replace_colors']['springgreen'] = '#00ff7f';
$data['csstidy']['replace_colors']['steelblue'] = '#4682b4';
$data['csstidy']['replace_colors']['tan'] = '#d2b48c';
$data['csstidy']['replace_colors']['thistle'] = '#d8bfd8';
$data['csstidy']['replace_colors']['tomato'] = '#ff6347';
$data['csstidy']['replace_colors']['turquoise'] = '#40e0d0';
$data['csstidy']['replace_colors']['violet'] = '#ee82ee';
$data['csstidy']['replace_colors']['violetred'] = '#d02090';
$data['csstidy']['replace_colors']['wheat'] = '#f5deb3';
$data['csstidy']['replace_colors']['whitesmoke'] = '#f5f5f5';
$data['csstidy']['replace_colors']['yellowgreen'] = '#9acd32';

/**
 * A list of all shorthand properties that are divided into four properties and/or have four subvalues.
 *
 * @TODO Are there new ones in CSS3?
 * @see dissolve_4value_shorthands()
 * @see merge_4value_shorthands()
 */
$data['csstidy']['shorthands'] = array();
$data['csstidy']['shorthands']['border-color'] = array( 'border-top-color', 'border-right-color', 'border-bottom-color', 'border-left-color' );
$data['csstidy']['shorthands']['border-style'] = array( 'border-top-style', 'border-right-style', 'border-bottom-style', 'border-left-style' );
$data['csstidy']['shorthands']['border-width'] = array( 'border-top-width', 'border-right-width', 'border-bottom-width', 'border-left-width' );
$data['csstidy']['shorthands']['margin'] = array( 'margin-top', 'margin-right', 'margin-bottom', 'margin-left' );
$data['csstidy']['shorthands']['padding'] = array( 'padding-top', 'padding-right', 'padding-bottom', 'padding-left' );
$data['csstidy']['shorthands']['-moz-border-radius'] = 0;

/**
 * All CSS Properties.
 *
 * @see CSSTidy::property_is_next()
 */
$data['csstidy']['all_properties']['align-content'] = 'CSS3.0';
$data['csstidy']['all_properties']['align-items'] = 'CSS3.0';
$data['csstidy']['all_properties']['align-self'] = 'CSS3.0';
$data['csstidy']['all_properties']['alignment-adjust'] = 'CSS3.0';
$data['csstidy']['all_properties']['alignment-baseline'] = 'CSS3.0';
$data['csstidy']['all_properties']['animation'] = 'CSS3.0';
$data['csstidy']['all_properties']['animation-delay'] = 'CSS3.0';
$data['csstidy']['all_properties']['animation-direction'] = 'CSS3.0';
$data['csstidy']['all_properties']['animation-duration'] = 'CSS3.0';
$data['csstidy']['all_properties']['animation-fill-mode'] = 'CSS3.0';
$data['csstidy']['all_properties']['animation-iteration-count'] = 'CSS3.0';
$data['csstidy']['all_properties']['animation-name'] = 'CSS3.0';
$data['csstidy']['all_properties']['animation-play-state'] = 'CSS3.0';
$data['csstidy']['all_properties']['animation-timing-function'] = 'CSS3.0';
$data['csstidy']['all_properties']['appearance'] = 'CSS3.0';
$data['csstidy']['all_properties']['azimuth'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['backface-visibility'] = 'CSS3.0';
$data['csstidy']['all_properties']['background'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['background-attachment'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['background-clip'] = 'CSS3.0';
$data['csstidy']['all_properties']['background-color'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['background-image'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['background-origin'] = 'CSS3.0';
$data['csstidy']['all_properties']['background-position'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['background-repeat'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['background-size'] = 'CSS3.0';
$data['csstidy']['all_properties']['baseline-shift'] = 'CSS3.0';
$data['csstidy']['all_properties']['binding'] = 'CSS3.0';
$data['csstidy']['all_properties']['bleed'] = 'CSS3.0';
$data['csstidy']['all_properties']['bookmark-label'] = 'CSS3.0';
$data['csstidy']['all_properties']['bookmark-level'] = 'CSS3.0';
$data['csstidy']['all_properties']['bookmark-state'] = 'CSS3.0';
$data['csstidy']['all_properties']['bookmark-target'] = 'CSS3.0';
$data['csstidy']['all_properties']['border'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-bottom'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-bottom-color'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-bottom-left-radius'] = 'CSS3.0';
$data['csstidy']['all_properties']['border-bottom-right-radius'] = 'CSS3.0';
$data['csstidy']['all_properties']['border-bottom-style'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-bottom-width'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-collapse'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-color'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-image'] = 'CSS3.0';
$data['csstidy']['all_properties']['border-image-outset'] = 'CSS3.0';
$data['csstidy']['all_properties']['border-image-repeat'] = 'CSS3.0';
$data['csstidy']['all_properties']['border-image-slice'] = 'CSS3.0';
$data['csstidy']['all_properties']['border-image-source'] = 'CSS3.0';
$data['csstidy']['all_properties']['border-image-width'] = 'CSS3.0';
$data['csstidy']['all_properties']['border-left'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-left-color'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-left-style'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-left-width'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-radius'] = 'CSS3.0';
$data['csstidy']['all_properties']['border-right'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-right-color'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-right-style'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-right-width'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-spacing'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-style'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-top'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-top-color'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-top-left-radius'] = 'CSS3.0';
$data['csstidy']['all_properties']['border-top-right-radius'] = 'CSS3.0';
$data['csstidy']['all_properties']['border-top-style'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-top-width'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['border-width'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['bottom'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['box-decoration-break'] = 'CSS3.0';
$data['csstidy']['all_properties']['box-shadow'] = 'CSS3.0';
$data['csstidy']['all_properties']['box-sizing'] = 'CSS3.0';
$data['csstidy']['all_properties']['break-after'] = 'CSS3.0';
$data['csstidy']['all_properties']['break-before'] = 'CSS3.0';
$data['csstidy']['all_properties']['break-inside'] = 'CSS3.0';
$data['csstidy']['all_properties']['caption-side'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['clear'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['clip'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['color'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['color-profile'] = 'CSS3.0';
$data['csstidy']['all_properties']['column-count'] = 'CSS3.0';
$data['csstidy']['all_properties']['column-fill'] = 'CSS3.0';
$data['csstidy']['all_properties']['column-gap'] = 'CSS3.0';
$data['csstidy']['all_properties']['column-rule'] = 'CSS3.0';
$data['csstidy']['all_properties']['column-rule-color'] = 'CSS3.0';
$data['csstidy']['all_properties']['column-rule-style'] = 'CSS3.0';
$data['csstidy']['all_properties']['column-rule-width'] = 'CSS3.0';
$data['csstidy']['all_properties']['column-span'] = 'CSS3.0';
$data['csstidy']['all_properties']['column-width'] = 'CSS3.0';
$data['csstidy']['all_properties']['columns'] = 'CSS3.0';
$data['csstidy']['all_properties']['content'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['counter-increment'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['counter-reset'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['crop'] = 'CSS3.0';
$data['csstidy']['all_properties']['cue'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['cue-after'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['cue-before'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['cursor'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['direction'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['display'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['dominant-baseline'] = 'CSS3.0';
$data['csstidy']['all_properties']['drop-initial-after-adjust'] = 'CSS3.0';
$data['csstidy']['all_properties']['drop-initial-after-align'] = 'CSS3.0';
$data['csstidy']['all_properties']['drop-initial-before-adjust'] = 'CSS3.0';
$data['csstidy']['all_properties']['drop-initial-before-align'] = 'CSS3.0';
$data['csstidy']['all_properties']['drop-initial-size'] = 'CSS3.0';
$data['csstidy']['all_properties']['drop-initial-value'] = 'CSS3.0';
$data['csstidy']['all_properties']['elevation'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['empty-cells'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['fill'] = 'CSS3.0';
$data['csstidy']['all_properties']['fit'] = 'CSS3.0';
$data['csstidy']['all_properties']['fit-position'] = 'CSS3.0';
$data['csstidy']['all_properties']['flex'] = 'CSS3.0';
$data['csstidy']['all_properties']['flex-align'] = 'CSS3.0';
$data['csstidy']['all_properties']['flex-basis'] = 'CSS3.0';
$data['csstidy']['all_properties']['flex-direction'] = 'CSS3.0';
$data['csstidy']['all_properties']['flex-flow'] = 'CSS3.0';
$data['csstidy']['all_properties']['flex-grow'] = 'CSS3.0';
$data['csstidy']['all_properties']['flex-line-pack'] = 'CSS3.0';
$data['csstidy']['all_properties']['flex-order'] = 'CSS3.0';
$data['csstidy']['all_properties']['flex-pack'] = 'CSS3.0';
$data['csstidy']['all_properties']['flex-shrink'] = 'CSS3.0';
$data['csstidy']['all_properties']['flex-wrap'] = 'CSS3.0';
$data['csstidy']['all_properties']['float'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['float-offset'] = 'CSS3.0';
$data['csstidy']['all_properties']['font'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['font-family'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['font-size'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['font-size-adjust'] = 'CSS2.0,CSS3.0';
$data['csstidy']['all_properties']['font-stretch'] = 'CSS2.0,CSS3.0';
$data['csstidy']['all_properties']['font-style'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['font-variant'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['font-weight'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['grid'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-area'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-auto-columns'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-auto-flow'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-auto-rows'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-column'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-columns'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-column-end'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-column-gap'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-column-start'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-gap'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-row'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-rows'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-row-end'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-row-gap'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-row-start'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-template'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-template-areas'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-template-columns'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-template-rows'] = 'CSS3.0';
$data['csstidy']['all_properties']['hanging-punctuation'] = 'CSS3.0';
$data['csstidy']['all_properties']['height'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['hyphenate-after'] = 'CSS3.0';
$data['csstidy']['all_properties']['hyphenate-before'] = 'CSS3.0';
$data['csstidy']['all_properties']['hyphenate-character'] = 'CSS3.0';
$data['csstidy']['all_properties']['hyphenate-lines'] = 'CSS3.0';
$data['csstidy']['all_properties']['hyphenate-resource'] = 'CSS3.0';
$data['csstidy']['all_properties']['hyphens'] = 'CSS3.0';
$data['csstidy']['all_properties']['icon'] = 'CSS3.0';
$data['csstidy']['all_properties']['image-orientation'] = 'CSS3.0';
$data['csstidy']['all_properties']['image-rendering'] = 'CSS3.0';
$data['csstidy']['all_properties']['image-resolution'] = 'CSS3.0';
$data['csstidy']['all_properties']['inline-box-align'] = 'CSS3.0';
$data['csstidy']['all_properties']['justify-content'] = 'CSS3.0';
$data['csstidy']['all_properties']['justify-items'] = 'CSS3.0';
$data['csstidy']['all_properties']['justify-self'] = 'CSS3.0';
$data['csstidy']['all_properties']['left'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['letter-spacing'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['line-break'] = 'CSS3.0';
$data['csstidy']['all_properties']['line-height'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['line-stacking'] = 'CSS3.0';
$data['csstidy']['all_properties']['line-stacking-ruby'] = 'CSS3.0';
$data['csstidy']['all_properties']['line-stacking-shift'] = 'CSS3.0';
$data['csstidy']['all_properties']['line-stacking-strategy'] = 'CSS3.0';
$data['csstidy']['all_properties']['list-style'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['list-style-image'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['list-style-position'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['list-style-type'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['margin'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['margin-bottom'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['margin-left'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['margin-right'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['margin-top'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['marker-offset'] = 'CSS2.0,CSS3.0';
$data['csstidy']['all_properties']['marks'] = 'CSS2.0,CSS3.0';
$data['csstidy']['all_properties']['marquee-direction'] = 'CSS3.0';
$data['csstidy']['all_properties']['marquee-loop'] = 'CSS3.0';
$data['csstidy']['all_properties']['marquee-play-count'] = 'CSS3.0';
$data['csstidy']['all_properties']['marquee-speed'] = 'CSS3.0';
$data['csstidy']['all_properties']['marquee-style'] = 'CSS3.0';
$data['csstidy']['all_properties']['max-height'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['max-width'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['min-height'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['min-width'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['move-to'] = 'CSS3.0';
$data['csstidy']['all_properties']['nav-down'] = 'CSS3.0';
$data['csstidy']['all_properties']['nav-index'] = 'CSS3.0';
$data['csstidy']['all_properties']['nav-left'] = 'CSS3.0';
$data['csstidy']['all_properties']['nav-right'] = 'CSS3.0';
$data['csstidy']['all_properties']['nav-up'] = 'CSS3.0';
$data['csstidy']['all_properties']['opacity'] = 'CSS3.0';
$data['csstidy']['all_properties']['order'] = 'CSS3.0';
$data['csstidy']['all_properties']['orphans'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['outline'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['outline-color'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['outline-offset'] = 'CSS3.0';
$data['csstidy']['all_properties']['outline-style'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['outline-width'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['overflow'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['overflow-style'] = 'CSS3.0';
$data['csstidy']['all_properties']['overflow-wrap'] = 'CSS3.0';
$data['csstidy']['all_properties']['overflow-x'] = 'CSS3.0';
$data['csstidy']['all_properties']['overflow-y'] = 'CSS3.0';
$data['csstidy']['all_properties']['padding'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['padding-bottom'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['padding-left'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['padding-right'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['padding-top'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['page'] = 'CSS2.0,CSS3.0';
$data['csstidy']['all_properties']['page-break-after'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['page-break-before'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['page-break-inside'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['page-policy'] = 'CSS3.0';
$data['csstidy']['all_properties']['pause'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['pause-after'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['pause-before'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['perspective'] = 'CSS3.0';
$data['csstidy']['all_properties']['perspective-origin'] = 'CSS3.0';
$data['csstidy']['all_properties']['phonemes'] = 'CSS3.0';
$data['csstidy']['all_properties']['pitch'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['pitch-range'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['play-during'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['position'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['presentation-level'] = 'CSS3.0';
$data['csstidy']['all_properties']['punctuation-trim'] = 'CSS3.0';
$data['csstidy']['all_properties']['quotes'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['rendering-intent'] = 'CSS3.0';
$data['csstidy']['all_properties']['resize'] = 'CSS3.0';
$data['csstidy']['all_properties']['rest'] = 'CSS3.0';
$data['csstidy']['all_properties']['rest-after'] = 'CSS3.0';
$data['csstidy']['all_properties']['rest-before'] = 'CSS3.0';
$data['csstidy']['all_properties']['richness'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['right'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['rotation'] = 'CSS3.0';
$data['csstidy']['all_properties']['rotation-point'] = 'CSS3.0';
$data['csstidy']['all_properties']['ruby-align'] = 'CSS3.0';
$data['csstidy']['all_properties']['ruby-overhang'] = 'CSS3.0';
$data['csstidy']['all_properties']['ruby-position'] = 'CSS3.0';
$data['csstidy']['all_properties']['ruby-span'] = 'CSS3.0';
$data['csstidy']['all_properties']['size'] = 'CSS2.0,CSS3.0';
$data['csstidy']['all_properties']['speak'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['speak-header'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['speak-numeral'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['speak-punctuation'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['speech-rate'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['src'] = 'CSS3.0';
$data['csstidy']['all_properties']['stress'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['string-set'] = 'CSS3.0';
$data['csstidy']['all_properties']['stroke'] = 'CSS3.0';
$data['csstidy']['all_properties']['tab-size'] = 'CSS3.0';
$data['csstidy']['all_properties']['table-layout'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['target'] = 'CSS3.0';
$data['csstidy']['all_properties']['target-name'] = 'CSS3.0';
$data['csstidy']['all_properties']['target-new'] = 'CSS3.0';
$data['csstidy']['all_properties']['target-position'] = 'CSS3.0';
$data['csstidy']['all_properties']['text-align'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['text-align-last'] = 'CSS3.0';
$data['csstidy']['all_properties']['text-decoration'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['text-decoration-color'] = 'CSS3.0';
$data['csstidy']['all_properties']['text-decoration-line'] = 'CSS3.0';
$data['csstidy']['all_properties']['text-decoration-skip'] = 'CSS3.0';
$data['csstidy']['all_properties']['text-decoration-style'] = 'CSS3.0';
$data['csstidy']['all_properties']['text-emphasis'] = 'CSS3.0';
$data['csstidy']['all_properties']['text-emphasis-color'] = 'CSS3.0';
$data['csstidy']['all_properties']['text-emphasis-position'] = 'CSS3.0';
$data['csstidy']['all_properties']['text-emphasis-style'] = 'CSS3.0';
$data['csstidy']['all_properties']['text-height'] = 'CSS3.0';
$data['csstidy']['all_properties']['text-indent'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['text-justify'] = 'CSS3.0';
$data['csstidy']['all_properties']['text-outline'] = 'CSS3.0';
$data['csstidy']['all_properties']['text-shadow'] = 'CSS2.0,CSS3.0';
$data['csstidy']['all_properties']['text-space-collapse'] = 'CSS3.0';
$data['csstidy']['all_properties']['text-transform'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['text-underline-position'] = 'CSS3.0';
$data['csstidy']['all_properties']['text-wrap'] = 'CSS3.0';
$data['csstidy']['all_properties']['top'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['transform'] = 'CSS3.0';
$data['csstidy']['all_properties']['transform-origin'] = 'CSS3.0';
$data['csstidy']['all_properties']['transform-style'] = 'CSS3.0';
$data['csstidy']['all_properties']['transition'] = 'CSS3.0';
$data['csstidy']['all_properties']['transition-delay'] = 'CSS3.0';
$data['csstidy']['all_properties']['transition-duration'] = 'CSS3.0';
$data['csstidy']['all_properties']['transition-property'] = 'CSS3.0';
$data['csstidy']['all_properties']['transition-timing-function'] = 'CSS3.0';
$data['csstidy']['all_properties']['unicode-bidi'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['vertical-align'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['visibility'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['voice-balance'] = 'CSS3.0';
$data['csstidy']['all_properties']['voice-duration'] = 'CSS3.0';
$data['csstidy']['all_properties']['voice-family'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['voice-pitch'] = 'CSS3.0';
$data['csstidy']['all_properties']['voice-pitch-range'] = 'CSS3.0';
$data['csstidy']['all_properties']['voice-rate'] = 'CSS3.0';
$data['csstidy']['all_properties']['voice-stress'] = 'CSS3.0';
$data['csstidy']['all_properties']['voice-volume'] = 'CSS3.0';
$data['csstidy']['all_properties']['volume'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['white-space'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['widows'] = 'CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['width'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['word-break'] = 'CSS3.0';
$data['csstidy']['all_properties']['word-spacing'] = 'CSS1.0,CSS2.0,CSS2.1,CSS3.0';
$data['csstidy']['all_properties']['word-wrap'] = 'CSS3.0';
$data['csstidy']['all_properties']['z-index'] = 'CSS2.0,CSS2.1,CSS3.0';

/**
 * An array containing all properties that can accept a quoted string as a value.
 */
$data['csstidy']['quoted_string_properties'] = array( 'content', 'font', 'font-family', 'quotes' );

/**
 * An array containing all properties that can be defined multiple times without being overwritten.
 * All unit values are included so that units like rem can be supported with fallbacks to px or em.
 */
$data['csstidy']['multiple_properties'] = array_merge( $data['csstidy']['color_values'], $data['csstidy']['unit_values'], array( 'transition', 'background-image', 'border-image', 'list-style-image' ) );

/**
 * An array containing all predefined templates.
 *
 * @see CSSTidy::load_template()
 */
$data['csstidy']['predefined_templates']['default'][] = '<span class="at">'; //string before @rule
$data['csstidy']['predefined_templates']['default'][] = '</span> <span class="format">{</span>' . "\n"; //bracket after @-rule
$data['csstidy']['predefined_templates']['default'][] = '<span class="selector">'; //string before selector
$data['csstidy']['predefined_templates']['default'][] = '</span> <span class="format">{</span>' . "\n"; //bracket after selector
$data['csstidy']['predefined_templates']['default'][] = '<span class="property">'; //string before property
$data['csstidy']['predefined_templates']['default'][] = '</span><span class="value">'; //string after property+before value
$data['csstidy']['predefined_templates']['default'][] = '</span><span class="format">;</span>' . "\n"; //string after value
$data['csstidy']['predefined_templates']['default'][] = '<span class="format">}</span>'; //closing bracket - selector
$data['csstidy']['predefined_templates']['default'][] = "\n\n"; //space between blocks {...}
$data['csstidy']['predefined_templates']['default'][] = "\n" . '<span class="format">}</span>' . "\n\n"; //closing bracket @-rule
$data['csstidy']['predefined_templates']['default'][] = ''; //indent in @-rule
$data['csstidy']['predefined_templates']['default'][] = '<span class="comment">'; // before comment
$data['csstidy']['predefined_templates']['default'][] = '</span>' . "\n"; // after comment
$data['csstidy']['predefined_templates']['default'][] = "\n"; // after each line @-rule

$data['csstidy']['predefined_templates']['high_compression'][] = '<span class="at">';
$data['csstidy']['predefined_templates']['high_compression'][] = '</span> <span class="format">{</span>' . "\n";
$data['csstidy']['predefined_templates']['high_compression'][] = '<span class="selector">';
$data['csstidy']['predefined_templates']['high_compression'][] = '</span><span class="format">{</span>';
$data['csstidy']['predefined_templates']['high_compression'][] = '<span class="property">';
$data['csstidy']['predefined_templates']['high_compression'][] = '</span><span class="value">';
$data['csstidy']['predefined_templates']['high_compression'][] = '</span><span class="format">;</span>';
$data['csstidy']['predefined_templates']['high_compression'][] = '<span class="format">}</span>';
$data['csstidy']['predefined_templates']['high_compression'][] = "\n";
$data['csstidy']['predefined_templates']['high_compression'][] = "\n" . '<span class="format">}' . "\n" . '</span>';
$data['csstidy']['predefined_templates']['high_compression'][] = '';
$data['csstidy']['predefined_templates']['high_compression'][] = '<span class="comment">'; // before comment
$data['csstidy']['predefined_templates']['high_compression'][] = '</span>'; // after comment
$data['csstidy']['predefined_templates']['high_compression'][] = "\n";

$data['csstidy']['predefined_templates']['highest_compression'][] = '<span class="at">';
$data['csstidy']['predefined_templates']['highest_compression'][] = '</span><span class="format">{</span>';
$data['csstidy']['predefined_templates']['highest_compression'][] = '<span class="selector">';
$data['csstidy']['predefined_templates']['highest_compression'][] = '</span><span class="format">{</span>';
$data['csstidy']['predefined_templates']['highest_compression'][] = '<span class="property">';
$data['csstidy']['predefined_templates']['highest_compression'][] = '</span><span class="value">';
$data['csstidy']['predefined_templates']['highest_compression'][] = '</span><span class="format">;</span>';
$data['csstidy']['predefined_templates']['highest_compression'][] = '<span class="format">}</span>';
$data['csstidy']['predefined_templates']['highest_compression'][] = '';
$data['csstidy']['predefined_templates']['highest_compression'][] = '<span class="format">}</span>';
$data['csstidy']['predefined_templates']['highest_compression'][] = '';
$data['csstidy']['predefined_templates']['highest_compression'][] = '<span class="comment">'; // before comment
$data['csstidy']['predefined_templates']['highest_compression'][] = '</span>'; // after comment
$data['csstidy']['predefined_templates']['highest_compression'][] = '';

$data['csstidy']['predefined_templates']['low_compression'][] = '<span class="at">';
$data['csstidy']['predefined_templates']['low_compression'][] = '</span> <span class="format">{</span>' . "\n";
$data['csstidy']['predefined_templates']['low_compression'][] = '<span class="selector">';
$data['csstidy']['predefined_templates']['low_compression'][] = '</span>' . "\n" . '<span class="format">{</span>' . "\n";
$data['csstidy']['predefined_templates']['low_compression'][] = '	<span class="property">';
$data['csstidy']['predefined_templates']['low_compression'][] = '</span><span class="value">';
$data['csstidy']['predefined_templates']['low_compression'][] = '</span><span class="format">;</span>' . "\n";
$data['csstidy']['predefined_templates']['low_compression'][] = '<span class="format">}</span>';
$data['csstidy']['predefined_templates']['low_compression'][] = "\n\n";
$data['csstidy']['predefined_templates']['low_compression'][] = "\n" . '<span class="format">}</span>' . "\n\n";
$data['csstidy']['predefined_templates']['low_compression'][] = '	';
$data['csstidy']['predefined_templates']['low_compression'][] = '<span class="comment">'; // before comment
$data['csstidy']['predefined_templates']['low_compression'][] = '</span>' . "\n"; // after comment
$data['csstidy']['predefined_templates']['low_compression'][] = "\n";

// Add TablePress specific modifications.
require dirname( __FILE__ ) . '/data-tp.inc.php';
csv-parser.class.php000066600000023625151121565550010470 0ustar00<?php
/**
 * CSV Parsing class for TablePress, used for import of CSV files
 *
 * @package TablePress
 * @subpackage Import
 * @author Tobias Bäthge
 * @since 1.0.0
 */

// Prohibit direct script loading.
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );

/**
 * CSV Parsing class
 * @package TablePress
 * @subpackage Import
 * @author Tobias Bäthge
 * @since 1.0.0
 */
class CSV_Parser {

	/**
	 * The used character for the enclosure of a cell. Defaults to quotation mark ".
	 *
	 * @since 1.0.0
	 * @var string
	 */
	protected $enclosure = '"';

	/**
	 * Number of rows to analyze when attempting to auto-detect the CSV delimiter.
	 *
	 * @since 1.0.0
	 * @var int
	 */
	protected $delimiter_search_max_lines = 15;

	/**
	 * Characters to ignore when attempting to auto-detect delimiter.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	protected $non_delimiter_chars = "a-zA-Z0-9\n\r";

	/**
	 * The preferred delimiter characters, only used when all filtering method return multiple possible delimiters (happens very rarely).
	 *
	 * @since 1.0.0
	 * @var string
	 */
	protected $preferred_delimiter_chars = ";,\t";

	/**
	 * The CSV data string that shall be parsed to an array.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	protected $import_data;

	/**
	 * The error state while parsing input data.
	 *
	 * 0 = No errors found. Everything should be fine.
	 * 1 = A hopefully correctable syntax error was found.
	 * 2 = The enclosure character was found in a non-enclosed field. This means the file is either corrupt,
	 *     or does not follow the common CSV standard. Please validate the parsed data manually.
	 *
	 * @since 1.0.0
	 * @var int
	 */
	public $error = 0;

	/**
	 * Detailed error information.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	public $error_info = array();

	/**
	 * Class Constructor.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		// Unused.
	}

	/**
	 * Load CSV data that shall be parsed.
	 *
	 * @since 1.0.0
	 *
	 * @param string $data Data to be parsed.
	 */
	public function load_data( $data ) {
		// Check for mandatory trailing line break.
		if ( "\n" !== substr( $data, -1 ) ) {
			$data .= "\n";
		}
		$this->import_data = $data;
	}

	/**
	 * Detect the CSV delimiter, by analyzing some rows to determine the most probable delimiter character.
	 *
	 * @since 1.0.0
	 *
	 * @return string Most probable delimiter character.
	 */
	public function find_delimiter() {
		$data = &$this->import_data;

		$delimiter_count = array();
		$enclosed = false;
		$current_line = 0;

		// Walk through each character in the CSV string (up to $this->delimiter_search_max_lines) and search potential delimiter characters.
		$data_length = strlen( $data );
		for ( $i = 0; $i < $data_length; $i++ ) {
			$prev_char = ( $i - 1 >= 0 ) ? $data[ $i - 1 ] : '';
			$curr_char = $data[ $i ];
			$next_char = ( $i + 1 < $data_length ) ? $data[ $i + 1 ] : '';

			if ( $curr_char === $this->enclosure ) {
				// Open and closing quotes.
				if ( ! $enclosed || $next_char !== $this->enclosure ) {
					$enclosed = ! $enclosed; // Flip bool.
				} elseif ( $enclosed ) {
					$i++; // Skip next character.
				}
			} elseif ( ( "\n" === $curr_char && "\r" !== $prev_char || "\r" === $curr_char ) && ! $enclosed ) {
				// Reached end of a line.
				$current_line++;
				if ( $current_line >= $this->delimiter_search_max_lines ) {
					break;
				}
			} elseif ( ! $enclosed ) {
				// At this point, $curr_char seems to be used as a delimiter, as it is not enclosed.
				// Count $curr_char if it is not in the $this->non_delimiter_chars list
				if ( 0 === preg_match( '#[' . $this->non_delimiter_chars . ']#i', $curr_char ) ) {
					if ( ! isset( $delimiter_count[ $curr_char ][ $current_line ] ) ) {
						$delimiter_count[ $curr_char ][ $current_line ] = 0; // Initialize empty
					}
					$delimiter_count[ $curr_char ][ $current_line ]++;
				}
			}
		}

		// Find most probable delimiter, by sorting their counts.
		$potential_delimiters = array();
		foreach ( $delimiter_count as $char => $line_counts ) {
			$is_possible_delimiter = $this->_check_delimiter_count( $char, $line_counts, $current_line );
			if ( false !== $is_possible_delimiter ) {
				$potential_delimiters[ $is_possible_delimiter ] = $char;
			}
		}
		ksort( $potential_delimiters );

		// If no valid delimiter was found, use the character that was found in most rows.
		if ( empty( $potential_delimiters ) ) {
			$delimiter_counts = array_map( 'count', $delimiter_count );
			arsort( $delimiter_counts, SORT_NUMERIC );
			$potential_delimiters = array_keys( $delimiter_counts );
		}

		// Return first array element, as that has the highest count.
		return array_shift( $potential_delimiters );
	}

	/**
	 * Check if passed character can be a delimiter, by checking counts in each line.
	 *
	 * @since 1.0.0
	 *
	 * @param string $char         Character to check.
	 * @param array  $line_counts  Counts for the characters in the lines.
	 * @param int    $number_lines Number of lines.
	 * @return bool|string False if delimiter is not possible, string to be used as a sort key if character could be a delimiter.
	 */
	protected function _check_delimiter_count( $char, array $line_counts, $number_lines ) {
		// Was the potential delimiter found in every line?
		if ( count( $line_counts ) !== $number_lines ) {
			return false;
		}

		// Check if the count in every line is the same (or one higher for an "almost").
		$first = null;
		$equal = null;
		$almost = false;
		foreach ( $line_counts as $line => $count ) {
			if ( is_null( $first ) ) {
				$first = $count;
			} elseif ( $count === $first && false !== $equal ) {
				$equal = true;
			} elseif ( $count === $first + 1 && false !== $equal ) {
				$equal = true;
				$almost = true;
			} else {
				$equal = false;
			}
		}
		// Check equality only if there's more than one line.
		if ( $number_lines > 1 && ! $equal ) {
			return false;
		}

		// At this point, count is equal in all lines, so determine a string to sort priority.
		$match = ( $almost ) ? 2 : 1;
		$pref = strpos( $this->preferred_delimiter_chars, $char );
		$pref = ( false !== $pref ) ? str_pad( $pref, 3, '0', STR_PAD_LEFT ) : '999';
		return $pref . $match . '.' . ( 99999 - str_pad( $first, 5, '0', STR_PAD_LEFT ) );
	}

	/**
	 * Parse CSV string into a two-dimensional array.
	 *
	 * @since 1.0.0
	 *
	 * @param string $delimiter Delimiter character for the CSV parsing.
	 * @return array Two-dimensional array with the data from the CSV string.
	 */
	public function parse( $delimiter ) {
		$data = &$this->import_data;

		// Filter delimiter from the list, if it is a whitespace character.
		$white_spaces = str_replace( $delimiter, '', " \t\x0B\0" );

		$rows = array(); // Complete rows.
		$row = array(); // Row that is currently built.
		$column = 0; // Current column index.
		$cell_content = ''; // Content of the currently processed cell.
		$enclosed = false;
		$was_enclosed = false; // To determine if the cell content will be trimmed of whitespace (only for enclosed cells).

		// Walk through each character in the CSV string.
		$data_length = strlen( $data );
		for ( $i = 0; $i < $data_length; $i++ ) {
			$curr_char = $data[ $i ];
			$next_char = ( $i + 1 < $data_length ) ? $data[ $i + 1 ] : '';

			if ( $curr_char === $this->enclosure ) {
				// Open/close quotes, and inline quotes.
				if ( ! $enclosed ) {
					if ( '' === ltrim( $cell_content, $white_spaces ) ) {
						$enclosed = true;
						$was_enclosed = true;
					} else {
						$this->error = 2;
						$error_line = count( $rows ) + 1;
						$error_column = $column + 1;
						if ( ! isset( $this->error_info[ "{$error_line}-{$error_column}" ] ) ) {
							$this->error_info[ "{$error_line}-{$error_column}" ] = array(
								'type'   => 2,
								'info'   => "Syntax error found in line {$error_line}. Non-enclosed fields can not contain double-quotes.",
								'line'   => $error_line,
								'column' => $error_column,
							);
						}
						$cell_content .= $curr_char;
					}
				} elseif ( $next_char === $this->enclosure ) {
					// Enclosure character within enclosed cell (" encoded as "").
					$cell_content .= $curr_char;
					$i++; // Skip next character
				} elseif ( $next_char !== $delimiter && "\r" !== $next_char && "\n" !== $next_char ) {
					// for-loop (instead of while-loop) that skips whitespace.
					for ( $x = ( $i + 1 ); isset( $data[ $x ] ) && '' === ltrim( $data[ $x ], $white_spaces ); $x++ ) {
						// Action is in iterator check.
					}
					if ( $data[ $x ] === $delimiter ) {
						$enclosed = false;
						$i = $x;
					} else {
						if ( $this->error < 1 ) {
							$this->error = 1;
						}
						$error_line = count( $rows ) + 1;
						$error_column = $column + 1;
						if ( ! isset( $this->error_info[ "{$error_line}-{$error_column}" ] ) ) {
							$this->error_info[ "{$error_line}-{$error_column}" ] = array(
								'type'   => 1,
								'info'   => "Syntax error found in line {$error_line}. A single double-quote was found within an enclosed string. Enclosed double-quotes must be escaped with a second double-quote.",
								'line'   => $error_line,
								'column' => $error_column,
							);
						}
						$cell_content .= $curr_char;
						$enclosed = false;
					}
				} else {
					// The " was the closing one for the cell.
					$enclosed = false;
				}
			} elseif ( ( $curr_char === $delimiter || "\n" === $curr_char || "\r" === $curr_char ) && ! $enclosed ) {
				// End of cell (by $delimiter), or end of line (by line break, and not enclosed!).

				$row[ $column ] = ( $was_enclosed ) ? $cell_content : trim( $cell_content );
				$cell_content = '';
				$was_enclosed = false;
				$column++;

				// End of line.
				if ( "\n" === $curr_char || "\r" === $curr_char ) {
					// Append completed row.
					$rows[] = $row;
					$row = array();
					$column = 0;
					if ( "\r" === $curr_char && "\n" === $next_char ) {
						// Skip next character in \r\n line breaks.
						$i++;
					}
				}
			} else {
				// Append character to current cell.
				$cell_content .= $curr_char;
			}
		}

		return $rows;
	}

} // class CSV_Parser
evalmath.class.php000066600000103012151121565550010171 0ustar00<?php
/**
 * EvalMath - Safely evaluate math expressions
 *
 * Based on EvalMath by Miles Kaufmann, with modifications by Petr Skoda.
 * @link https://github.com/moodle/moodle/blob/4efc3d4096bc1d29e9d77f9af7194b2babfa2821/lib/evalmath/evalmath.class.php
 *
 * @package TablePress
 * @subpackage Formulas
 * @author Miles Kaufmann, Petr Skoda, Tobias Bäthge
 * @since 1.0.0
 */

// Prohibit direct script loading.
defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );

/**
 * Class to safely evaluate math expressions.
 * @package TablePress
 * @subpackage Formulas
 * @since 1.0.0
 */
class EvalMath {

	/**
	 * Pattern used for a valid function or variable name.
	 *
	 * Note, var and func names are case insensitive.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	protected static $name_pattern = '[a-z][a-z0-9_]*';

	/**
	 * Whether to suppress errors and warnings.
	 *
	 * @since 1.0.0
	 * @var boolean
	 */
	public $suppress_errors = false;

	/**
	 * The last error message that was raised.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $last_error = '';

	/**
	 * Variables (including constants).
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $variables = array();

	/**
	 * User-defined functions.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $functions = array();

	/**
	 * Constants.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $constants = array();

	/**
	 * Built-in functions.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $builtin_functions = array(
		'sin', 'sinh', 'arcsin', 'asin', 'arcsinh', 'asinh',
		'cos', 'cosh', 'arccos', 'acos', 'arccosh', 'acosh',
		'tan', 'tanh', 'arctan', 'atan', 'arctanh', 'atanh',
		'sqrt', 'abs', 'ln', 'log10', 'exp', 'floor', 'ceil',
	);

	/**
	 * Emulated functions.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $calc_functions = array(
		'average' => array( -1 ),
		'mean' => array( -1 ),
		'median' => array( -1 ),
		'mode' => array( -1 ),
		'range' => array( -1 ),
		'max' => array( -1 ),
		'min' => array( -1 ),
		'mod' => array( 2 ),
		'pi' => array( 0 ),
		'power' => array( 2 ),
		'log' => array( 1, 2 ),
		'round' => array( 1, 2 ),
		'number_format' => array( 1, 2 ),
		'number_format_eu' => array( 1, 2 ),
		'sum' => array( -1 ),
		'product' => array( -1 ),
		'rand_int' => array( 2 ),
		'rand_float' => array( 0 ),
		'arctan2' => array( 2 ),
		'atan2' => array( 2 ),
		'if' => array( 3 ),
		'not' => array( 1 ),
		'and' => array( -1 ),
		'or' => array( -1 ),
	);

	/**
	 * Class constructor.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		// Sets default  constants.
		$this->variables['pi'] = pi();
		$this->variables['e'] = exp( 1 );
	}

	/**
	 * Evaluate a math expression without checking it for variable or function assignments.
	 *
	 * @since 1.0.0
	 *
	 * @param string $expression The expression that shall be evaluated.
	 * @return string Evaluated expression.
	 */
	public function evaluate( $expression ) {
		return $this->pfx( $this->nfx( $expression ) );
	}

	/**
	 * Evaluate a math expression or formula, and check it for variable an function assignments.
	 *
	 * @since 1.0.0
	 *
	 * @param string $expression The expression that shall be evaluated.
	 * @return string|bool Evaluated expression, true on successful function assignment, or false on error.
	 */
	public function assign_and_evaluate( $expression ) {
		$this->last_error = '';
		$expression = trim( $expression );
		$expression = rtrim( $expression, ';' );

		// Is the expression a variable assignment?
		if ( 1 === preg_match( '/^\s*(' . self::$name_pattern . ')\s*=\s*(.+)$/', $expression, $matches ) ) {
			// Make sure we're not assigning to a constant.
			if ( in_array( $matches[1], $this->constants, true ) ) {
				return $this->raise_error( 'cannot_assign_to_constant', $matches[1] );
			}
			// Evaluate the assignment.
			$tmp = $this->pfx( $this->nfx( $matches[2] ) );
			if ( fales === $tmp ) {
				return false;
			}
			// If it could be evaluated, add it to the variable array,
			$this->variables[ $matches[1] ] = $tmp;
			// and return the resulting value.
			return $tmp;

		// Is the expression a function assignment?
		} elseif ( 1 === preg_match( '/^\s*(' . self::$name_pattern . ')\s*\(\s*(' . self::$name_pattern . '(?:\s*,\s*' . self::$name_pattern . ')*)\s*\)\s*=\s*(.+)$/', $expression, $matches ) ) {
			// Get the function name.
			$function_name = $matches[1];
			// Make sure it isn't a built-in function -- we can't redefine those.
			if ( in_array( $matches[1], $this->builtin_functions, true ) ) {
				return $this->raise_error( 'cannot_redefine_builtin_function', $matches[1] );
			}
			// Get the function arguments after removing all whitespace.
			$matches[2] = str_replace( array( "\n", "\r", "\t", ' ' ), '', $matches[2] );
			$args = explode( ',', $matches[2] );

			// Convert the function definition to postfix notation.
			$stack = $this->nfx( $matches[3] );
			if ( false === $stack ) {
				return false;
			}
			// Freeze the state of the non-argument variables.
			for ( $i = 0; $i < count( $stack ); $i++ ) {
				$token = $stack[ $i ];
				if ( 1 === preg_match( '/^' . self::$name_pattern . '$/', $token ) && ! in_array( $token, $args, true ) ) {
					if ( array_key_exists( $token, $this->variables ) ) {
						$stack[ $i ] = $this->variables[ $token ];
					} else {
						return $this->raise_error( 'undefined_variable_in_function_definition', $token );
					}
				}
			}
			$this->functions[ $function_name ] = array( 'args' => $args, 'func' => $stack );
			return true;

		// No variable or function assignment, so straight-up evaluation.
		} else {
			return $this->evaluate( $expression );
		}
	}

	/**
	 * Return all user-defined variables and values.
	 *
	 * @since 1.0.0
	 *
	 * @return array User-defined variables and values.
	 */
	public function variables() {
		return $this->variables;
	}

	/**
	 * Return all user-defined functions with their arguments.
	 *
	 * @since 1.0.0
	 *
	 * @return array User-defined functions.
	 */
	public function functions() {
		$output = array();
		foreach ( $this->functions as $name => $data ) {
			$output[] = $name . '( ' . implode( ', ', $data['args'] ) . ' )';
		}
		return $output;
	}

	/*
	 * Internal methods.
	 */

	/**
	 * Convert infix to postfix notation.
	 *
	 * @since 1.0.0
	 *
	 * @param string $expression Math expression that shall be converted.
	 * @return array|false Converted expression or false on error.
	 */
	protected function nfx( $expression ) {
		$index = 0;
		$stack = new EvalMath_Stack;
		$output = array(); // postfix form of expression, to be passed to pfx()
		$expression = trim( strtolower( $expression ) );

		$ops   = array( '+', '-', '*', '/', '^', '_', '>', '<', '=' );
		// Right-associative operator?
		$ops_r = array( '+' => 0, '-' => 0, '*' => 0, '/' => 0, '^' => 1, '>' => 0, '<' => 0, '=' => 0 );
		// Operator precedence.
		$ops_p = array( '+' => 0, '-' => 0, '*' => 1, '/' => 1, '_' => 1, '^' => 2, '>' => 0, '<' => 0, '=' => 0 );

		// We use this in syntax-checking the expression and determining when a - (minus) is a negation.
		$expecting_operator = false;

		// Make sure the characters are all good.
		if ( 1 === preg_match( '/[^\w\s+*^\/()\.,-<>=]/', $expression, $matches ) ) {
			return $this->raise_error( 'illegal_character_general', $matches[0] );
		}

		// Infinite Loop for the conversion.
		while ( true ) {
			// Get the first character at the current index.
			$op = substr( $expression, $index, 1 );
			// Find out if we're currently at the beginning of a number/variable/function/parenthesis/operand.
			$ex = preg_match( '/^(' . self::$name_pattern . '\(?|\d+(?:\.\d*)?(?:(e[+-]?)\d*)?|\.\d+|\()/', substr( $expression, $index ), $match );

			// Is it a negation instead of a minus (in a subtraction)?
			if ( '-' === $op && ! $expecting_operator ) {
				// Put a negation on the stack.
				$stack->push( '_' );
				$index++;
			} elseif ( '_' === $op ) {
				// We have to explicitly deny underscores (as they mean negation), because they are legal on the stack.
				return $this->raise_error( 'illegal_character_underscore' );

			// Are we putting an operator on the stack?
			} elseif ( ( in_array( $op, $ops, true ) || $ex ) && $expecting_operator ) {
				// Are we expecting an operator but have a number/variable/function/opening parethesis?
				if ( $ex ) {
					// It's an implicit multiplication.
					$op = '*';
					$index--;
				}
				// Heart of the algorithm:
				while ( $stack->count > 0 && ( $o2 = $stack->last() ) && in_array( $o2, $ops, true ) && ( $ops_r[ $op ] ? $ops_p[ $op ] < $ops_p[ $o2 ] : $ops_p[ $op ] <= $ops_p[ $o2 ] ) ) {
					// Pop stuff off the stack into the output.
					$output[] = $stack->pop();
				}
				// Many thanks: https://en.wikipedia.org/wiki/Reverse_Polish_notation
				$stack->push( $op ); // finally put OUR operator onto the stack
				$index++;
				$expecting_operator = false;

			// Ready to close a parenthesis?
			} elseif ( ')' === $op && $expecting_operator ) {
				// Pop off the stack back to the last (.
				while ( '(' !== ( $o2 = $stack->pop() ) ) {
					if ( is_null( $o2 ) ) {
						return $this->raise_error( 'unexpected_closing_bracket' );
					} else {
						$output[] = $o2;
					}
				}

				// Did we just close a function?
				if ( 1 === preg_match( '/^(' . self::$name_pattern . ')\($/', $stack->last( 2 ), $matches ) ) {
					// Get the function name.
					$function_name = $matches[1];
					// See how many arguments there were (cleverly stored on the stack, thank you).
					$arg_count = $stack->pop();
					$fn = $stack->pop();
					// Send function to output.
					$output[] = array( 'function_name' => $function_name, 'arg_count' => $arg_count );
					// Check the argument count, depending on what type of function we have.
					if ( in_array( $function_name, $this->builtin_functions, true ) ) {
						// Built-in functions.
						if ( $arg_count > 1 ) {
							$error_data = array( 'expected' => 1, 'given' => $arg_count );
							return $this->raise_error( 'wrong_number_of_arguments', $error_data );
						}
					} elseif ( array_key_exists( $function_name, $this->calc_functions ) ) {
						// Calc-emulation functions.
						$counts = $this->calc_functions[ $function_name ];
						if ( in_array( -1, $counts, true ) && $arg_count > 0 ) {
							// Everything is fine, we expected an indefinite number arguments and got some.
						} elseif ( ! in_array( $arg_count, $counts, true ) ) {
							$error_data = array( 'expected' => implode( '/', $this->calc_functions[ $function_name ] ), 'given' => $arg_count );
							return $this->raise_error( 'wrong_number_of_arguments', $error_data );
						}
					} elseif ( array_key_exists( $function_name, $this->functions ) ) {
						// User-defined functions.
						if ( count( $this->functions[ $function_name ]['args'] ) !== $arg_count ) {
							$error_data = array( 'expected' => count( $this->functions[ $function_name ]['args'] ), 'given' => $arg_count );
							return $this->raise_error( 'wrong_number_of_arguments', $error_data );
						}
					} else {
						// Did we somehow push a non-function on the stack? This should never happen.
						return $this->raise_error( 'internal_error' );
					}
				}
				$index++;

			// Did we just finish a function argument?
			} elseif ( ',' === $op && $expecting_operator ) {
				while ( '(' !== ( $o2 = $stack->pop() ) ) {
					if ( is_null( $o2 ) ) {
						// Oops, never had a (.
						return $this->raise_error( 'unexpected_comma' );
					} else {
						// Pop the argument expression stuff and push onto the output.
						$output[] = $o2;
					}
				}
				// Make sure there was a function.
				if ( 0 === preg_match( '/^(' . self::$name_pattern . ')\($/', $stack->last( 2 ), $matches ) ) {
					return $this->raise_error( 'unexpected_comma' );
				}
				// Increment the argument count.
				$stack->push( $stack->pop() + 1 );
				// Put the ( back on, we'll need to pop back to it again.
				$stack->push( '(' );
				$index++;
				$expecting_operator = false;

			} elseif ( '(' === $op && ! $expecting_operator ) {
				$stack->push( '(' ); // That was easy.
				$index++;

			// Do we now have a function/variable/number?
			} elseif ( $ex && ! $expecting_operator ) {
				$expecting_operator = true;
				$value = $match[1];
				// May be a function, or variable with implicit multiplication against parentheses...
				if ( 1 === preg_match( '/^(' . self::$name_pattern . ')\($/', $value, $matches ) ) {
					// Is it a function?
					if ( in_array( $matches[1], $this->builtin_functions, true ) || array_key_exists( $matches[1], $this->functions ) || array_key_exists( $matches[1], $this->calc_functions ) ) {
						$stack->push( $value );
						$stack->push( 1 );
						$stack->push( '(' );
						$expecting_operator = false;
					// It's a variable with implicit multiplication.
					} else {
						$value = $matches[1];
						$output[] = $value;
					}
				} else {
					// It's a plain old variable or number.
					$output[] = $value;
				}
				$index += strlen( $value );

			} elseif ( ')' === $op ) {
				// It could be only custom function with no arguments or a general error.
				if ( '(' !== $stack->last() || 1 !== $stack->last( 2 ) ) {
					return $this->raise_error( 'unexpected_closing_bracket' );
				}
				// Did we just close a function?
				if ( 1 === preg_match( '/^(' . self::$name_pattern . ')\($/', $stack->last( 3 ), $matches ) ) {
					$stack->pop(); // (
					$stack->pop(); // 1
					$fn = $stack->pop();
					// Get the function name.
					$function_name = $matches[1];
					if ( isset( $this->calc_functions[ $function_name ] ) ) {
						// Custom calc-emulation function.
						$counts = $this->calc_functions[ $function_name ];
					} else {
						// Default count for built-in functions.
						$counts = array( 1 );
					}
					if ( ! in_array( 0, $counts, true ) ) {
						$error_data = array( 'expected' => $counts, 'given' => 0 );
						return $this->raise_error( 'wrong_number_of_arguments', $error_data );
					}
					// Send function to output.
					$output[] = array( 'function_name' => $function_name, 'arg_count' => 0 );
					$index++;
					$expecting_operator = true;
				} else {
					return $this->raise_error( 'unexpected_closing_bracket' );
				}

			// Miscellaneous error checking.
			} elseif ( in_array( $op, $ops, true ) && ! $expecting_operator ) {
				return $this->raise_error( 'unexpected_operator', $op );

			// I don't even want to know what you did to get here.
			} else {
				return $this->raise_error( 'an_unexpected_error_occurred' );
			}

			if ( strlen( $expression ) === $index ) {
				// Did we end with an operator? Bad.
				if ( in_array( $op, $ops, true ) ) {
					return $this->raise_error( 'operator_lacks_operand', $op );
				} else {
					break;
				}
			}

			// Step the index past whitespace (pretty much turns whitespace into implicit multiplication if no operator is there).
			while ( ' ' === substr( $expression, $index, 1 ) ) {
				$index++;
			}
		} // while ( true )

		// Pop everything off the stack and push onto output.
		while ( ! is_null( $op = $stack->pop() ) ) {
			if ( '(' === $op ) {
				// If there are (s on the stack, ()s were unbalanced.
				return $this->raise_error( 'expecting_a_closing_bracket' );
			}
			$output[] = $op;
		}

		return $output;
	}

	/**
	 * Evaluate postfix notation.
	 *
	 * @since 1.0.0
	 *
	 * @param array|false $tokens    [description]
	 * @param array       $variables Optional. [description]
	 * @return mixed [description]
	 */
	protected function pfx( $tokens, array $variables = array() ) {
		if ( false === $tokens ) {
			return false;
		}

		$stack = new EvalMath_Stack;

		foreach ( $tokens as $token ) {
			// If the token is a function, pop arguments off the stack, hand them to the function, and push the result back on.
			if ( is_array( $token ) ) { // it's a function!
				$function_name = $token['function_name'];
				$count = $token['arg_count'];

				// Built-in function.
				if ( in_array( $function_name, $this->builtin_functions, true ) ) {
					$op1 = $stack->pop();
					if ( is_null( $op1 ) ) {
						return $this->raise_error( 'internal_error' );
					}
					// For the "arc" trigonometric synonyms.
					$function_name = preg_replace( '/^arc/', 'a', $function_name );
					// Rewrite "ln" (only allows one argument) to "log" (natural logarithm).
					if ( 'ln' === $function_name ) {
						$function_name = 'log';
					}
					// Perfectly safe eval().
					eval( '$stack->push( ' . $function_name . '( $op1 ) );' );

				// Calc-emulation function.
				} elseif ( array_key_exists( $function_name, $this->calc_functions ) ) {
					// Get function arguments.
					$args = array();
					for ( $i = $count - 1; $i >= 0; $i-- ) {
						$arg = $stack->pop();
						if ( is_null( $arg ) ) {
							return $this->raise_error( 'internal_error' );
						} else {
							$args[] = $arg;
						}
					}
					// Rewrite some functions to their synonyms.
					if ( 'if' === $function_name ) {
						$function_name = 'func_if';
					} elseif ( 'not' === $function_name ) {
						$function_name = 'func_not';
					} elseif ( 'and' === $function_name ) {
						$function_name = 'func_and';
					} elseif ( 'or' === $function_name ) {
						$function_name = 'func_or';
					} elseif ( 'mean' === $function_name ) {
						$function_name = 'average';
					} elseif ( 'arctan2' === $function_name ) {
						$function_name = 'atan2';
					}
					$result = call_user_func_array( array( 'EvalMath_Functions', $function_name ), array_reverse( $args ) );
					if ( false === $result ) {
						return $this->raise_error( 'internal_error' );
					}
					$stack->push( $result );

				// User-defined function.
				} elseif ( array_key_exists( $function_name, $this->functions ) ) {
					// Get function arguments.
					$args = array();
					for ( $i = count( $this->functions[ $function_name ]['args'] ) - 1; $i >= 0; $i-- ) {
						$arg = $stack->pop();
						if ( is_null( $arg ) ) {
							return $this->raise_error( 'internal_error' );
						} else {
							$args[ $this->functions[ $function_name ]['args'][ $i ] ] = $arg;
						}
					}
					// yay... recursion!
					$stack->push( $this->pfx( $this->functions[ $function_name ]['func'], $args ) );
				}

			// If the token is a binary operator, pop two values off the stack, do the operation, and push the result back on.
			} elseif ( in_array( $token, array( '+', '-', '*', '/', '^', '>', '<', '=' ), true ) ) {
				$op2 = $stack->pop();
				if ( is_null( $op2 ) ) {
					return $this->raise_error( 'internal_error' );
				}
				$op1 = $stack->pop();
				if ( is_null( $op1 ) ) {
					return $this->raise_error( 'internal_error' );
				}
				switch ( $token ) {
					case '+':
						$stack->push( $op1 + $op2 );
						break;
					case '-':
						$stack->push( $op1 - $op2 );
						break;
					case '*':
						$stack->push( $op1 * $op2 );
						break;
					case '/':
						if ( 0 === $op2 || '0' === $op2 ) {
							return $this->raise_error( 'division_by_zero' );
						}
						$stack->push( $op1 / $op2 );
						break;
					case '^':
						$stack->push( pow( $op1, $op2 ) );
						break;
					case '>':
						$stack->push( (int) ( $op1 > $op2 ) );
						break;
					case '<':
						$stack->push( (int) ( $op1 < $op2 ) );
						break;
					case '=':
						$stack->push( (int) ( $op1 == $op2 ) ); // Don't use === as the variable type can differ (int/double/bool).
						break;
				}

			// If the token is a unary operator, pop one value off the stack, do the operation, and push it back on.
			} elseif ( '_' === $token ) {
				$stack->push( -1 * $stack->pop() );

			// If the token is a number or variable, push it on the stack.
			} else {
				if ( is_numeric( $token ) ) {
					$stack->push( $token );
				} elseif ( array_key_exists( $token, $this->variables ) ) {
					$stack->push( $this->variables[ $token ] );
				} elseif ( array_key_exists( $token, $variables ) ) {
					$stack->push( $variables[ $token ] );
				} else {
					return $this->raise_error( 'undefined_variable', $token );
				}
			}
		}
		// When we're out of tokens, the stack should have a single element, the final result.
		if ( 1 !== $stack->count ) {
			return $this->raise_error( 'internal_error' );
		}
		return $stack->pop();
	}

	/**
	 * Raise an error.
	 *
	 * @since 1.0.0
	 *
	 * @param string       $message    Error message.
	 * @param array|string $error_data Optional. Additional error data.
	 * @return bool False, to stop evaluation.
	 */
	protected function raise_error( $message, $error_data = null ) {
		$this->last_error = $this->get_error_string( $message, $error_data );
		return false;
	}


	/**
	 * Get a translated string for an error message.
	 *
	 * @since 1.0.0
	 *
	 * @link https://github.com/moodle/moodle/blob/13264f35057d2f37374ec3e0e8ad4070f4676bd7/lang/en/mathslib.php
	 * @link https://github.com/moodle/moodle/blob/8e54ce9717c19f768b95f4332f70e3180ffafc46/lib/moodlelib.php#L6323
	 *
	 * @param string       $identifier Identifier of the string.
	 * @param array|string $error_data Optional. Additional error data.
	 * @return string Translated string.
	 */
	protected function get_error_string( $identifier, $error_data = null ) {
		$strings = array();
		$strings['an_unexpected_error_occurred'] = 'an unexpected error occurred';
		$strings['cannot_assign_to_constant'] = 'cannot assign to constant \'{$error_data}\'';
		$strings['cannot_redefine_builtin_function'] = 'cannot redefine built-in function \'{$error_data}()\'';
		$strings['division_by_zero'] = 'division by zero';
		$strings['expecting_a_closing_bracket'] = 'expecting a closing bracket';
		$strings['illegal_character_general'] = 'illegal character \'{$error_data}\'';
		$strings['illegal_character_underscore'] = 'illegal character \'_\'';
		$strings['internal_error'] = 'internal error';
		$strings['operator_lacks_operand'] = 'operator \'{$error_data}\' lacks operand';
		$strings['undefined_variable'] = 'undefined variable \'{$error_data}\'';
		$strings['undefined_variable_in_function_definition'] = 'undefined variable \'{$error_data}\' in function definition';
		$strings['unexpected_closing_bracket'] = 'unexpected closing bracket';
		$strings['unexpected_comma'] = 'unexpected comma';
		$strings['unexpected_operator'] = 'unexpected operator \'{$error_data}\'';
		$strings['wrong_number_of_arguments'] = 'wrong number of arguments ({$error_data->given} given, {$error_data->expected} expected)';

		$string = $strings[ $identifier ];

		if ( null !== $error_data ) {
			if ( is_array( $error_data ) ) {
				$search = array();
				$replace = array();
				foreach ( $error_data as $key => $value ) {
					if ( is_int( $key ) ) {
						// We do not support numeric keys!
						continue;
					}
					if ( is_object( $value ) || is_array( $value ) ) {
						$value = (array) $value;
						if ( count( $value ) > 1 ) {
							$value = implode( ' or ', $value );
						} else {
							$value = (string) $value[0];
							if ( '-1' === $value ) {
								$value = 'at least 1';
							}
						}
					}
					$search[] = '{$error_data->' . $key . '}';
					$replace[] = (string) $value;
				}
				if ( $search ) {
					$string = str_replace( $search, $replace, $string );
				}
			} else {
				$string = str_replace( '{$error_data}', (string) $error_data, $string );
			}
		}

		return $string;
	}

} // class EvalMath

/**
 * Stack for the postfix/infix conversion of math expressions.
 * @package TablePress
 * @subpackage Formulas
 * @since 1.0.0
 */
class EvalMath_Stack {

	/**
	 * The stack.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $stack = array();

	/**
	 * Number of items on the stack.
	 *
	 * @since 1.0.0
	 * @var int
	 */
	public $count = 0;

	/**
	 * Push an item onto the stack.
	 *
	 * @since 1.0.0
	 *
	 * @param mixed $value The item that is pushed onto the stack.
	 */
	public function push( $value ) {
		$this->stack[ $this->count ] = $value;
		$this->count++;
	}

	/**
	 * Pop an item from the top of the stack.
	 *
	 * @since 1.0.0
	 *
	 * @return mixed The item that is popped from the stack.
	 */
	public function pop() {
		if ( $this->count > 0 ) {
			$this->count--;
			return $this->stack[ $this->count ];
		}
		return null;
	}

	/**
	 * Pop an item from the end of the stack.
	 *
	 * @since 1.0.0
	 *
	 * @param int $n Count from the end of the stack.
	 * @return mixed The item that is popped from the stack.
	 */
	public function last( $n = 1 ) {
		if ( ( $this->count - $n ) >= 0 ) {
			return $this->stack[ $this->count - $n ];
		}
		return null;
	}

} // class EvalMath_Stack

/**
 * Common math functions, prepared for usage in EvalMath.
 * @package TablePress
 * @subpackage EvalMath
 * @since 1.0.0
 */
class EvalMath_Functions {

	/**
	 * Seed for the generation of random numbers.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	protected static $random_seed = null;

	/**
	 * Choose from two values based on an if-condition.
	 *
	 * "if" is not a valid function name, which is why it's prefixed with "func_".
	 *
	 * @since  1.0.0
	 *
	 * @param double|int $condition Condition.
	 * @param double|int $then      Return value if the condition is true.
	 * @param double|int $else      Return value if the condition is false.
	 * @return double|int Result of the if check.
	 */
	public static function func_if( $condition, $then, $else ) {
		return ( (bool) $condition ? $then : $else );
	}

	/**
	 * Return the negation (boolean "not") of a value.
	 *
	 * Similar to "func_if", the function name is prefixed with "func_", although it wouldn't be necessary.
	 *
	 * @since  1.0.0
	 *
	 * @param double|int $value Value to be negated.
	 * @return int Negated value (0 for false, 1 for true).
	 */
	public static function func_not( $value ) {
		return (int) ! (bool) $value;
	}

	/**
	 * Calculate the conjunction (boolean "and") of some values.
	 *
	 * "and" is not a valid function name, which is why it's prefixed with "func_".
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $args Values for which the conjunction shall be calculated.
	 * @return int Conjunction of the passed arguments.
	 */
	public static function func_and( $args ) {
		$args = func_get_args();
		foreach ( $args as $value ) {
			if ( ! $value ) {
				return 0;
			}
		}
		return 1;
	}

	/**
	 * Calculate the disjunction (boolean "or") of some values.
	 *
	 * "or" is not a valid function name, which is why it's prefixed with "func_".
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $args Values for which the disjunction shall be calculated.
	 * @return int Disjunction of the passed arguments.
	 */
	public static function func_or( $args ) {
		$args = func_get_args();
		foreach ( $args as $value ) {
			if ( $value ) {
				return 1;
			}
		}
		return 0;
	}

	/**
	 * Return the (rounded) value of Pi.
	 *
	 * @since 1.0.0
	 *
	 * @return double Rounded value of Pi.
	 */
	public static function pi() {
		return pi();
	}

	/**
	 * Calculate the sum of the arguments.
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $args Values for which the sum shall be calculated.
	 * @return double|int Sum of the passed arguments.
	 */
	public static function sum( $args ) {
		$args = func_get_args();
		return array_sum( $args );
	}

	/**
	 * Calculate the product of the arguments.
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $args Values for which the product shall be calculated.
	 * @return double|int Product of the passed arguments.
	 */
	public static function product( $args ) {
		$args = func_get_args();
		return array_product( $args );
	}

	/**
	 * Calculate the average/mean value of the arguments.
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $args Values for which the average shall be calculated.
	 * @return double|int Average value of the passed arguments.
	 */
	public static function average( $args ) {
		$args = func_get_args();
		return ( call_user_func_array( array( 'self', 'sum' ), $args ) / count( $args ) );
	}

	/**
	 * Calculate the median of the arguments.
	 *
	 * For even counts of arguments, the upper median is returned.
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $args Values for which the median shall be calculated.
	 * @return double|int Median of the passed arguments.
	 */
	public static function median( $args ) {
		$args = func_get_args();
		sort( $args );
		$middle = floor( count( $args ) / 2 ); // Upper median for even counts.
		return $args[ $middle ];
	}

	/**
	 * Calculate the mode of the arguments.
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $args Values for which the mode shall be calculated.
	 * @return double|int Mode of the passed arguments.
	 */
	public static function mode( $args ) {
		$args = func_get_args();
		$values = array_count_values( $args );
		asort( $values );
		end( $values );
		return key( $values );
	}

	/**
	 * Calculate the range of the arguments.
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $args Values for which the range shall be calculated.
	 * @return double|int Range of the passed arguments.
	 */
	public static function range( $args ) {
		$args = func_get_args();
		sort( $args );
		return end( $args ) - reset( $args );
	}

	/**
	 * Find the maximum value of the arguments.
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $args Values for which the maximum value shall be found.
	 * @return double|int Maximum value of the passed arguments.
	 */
	public static function max( $args ) {
		$args = func_get_args();
		return max( $args );
	}

	/**
	 * Find the minimum value of the arguments.
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $args Values for which the minimum value shall be found.
	 * @return double|int Minimum value of the passed arguments.
	 */
	public static function min( $args ) {
		$args = func_get_args();
		return min( $args );
	}

	/**
	 * Calculate the remainder of a division of two numbers.
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $op1 First number (dividend).
	 * @param double|int $op2 Second number (divisor).
	 * @return double|int Remainer of the division (dividend / divisor).
	 */
	public static function mod( $op1, $op2 ) {
		return $op1 % $op2;
	}

	/**
	 * Calculate the power of a base and an exponent.
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $base     Base.
	 * @param double|int $exponent Exponent.
	 * @return double|int Power base^exponent.
	 */
	public static function power( $base, $exponent ) {
		return pow( $base, $exponent );
	}

	/**
	 * Calculate the logarithm of a number to a base.
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $number Number.
	 * @param double|int $base   Optional. Base for the logarithm. Default e (for the natural logarithm).
	 * @return double Logarithm of the number to the base.
	 */
	public static function log( $number, $base = M_E ) {
		return log( $number, $base );
	}

	/**
	 * Calculate the arc tangent of two variables.
	 *
	 * The signs of the numbers determine the quadrant of the result.
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $op1 First number.
	 * @param double|int $op2 Second number.
	 * @return double Arc tangent of two numbers, similar to arc tangent of $op1/op$ except for the sign.
	 */
	public static function atan2( $op1, $op2 ) {
		return atan2( $op1, $op2 );
	}

	/**
	 * Round a number to a given precision.
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $value    Number to be rounded.
	 * @param double|int $decimals Optional. Number of decimals after the comma after the rounding.
	 * @return double Rounded number.
	 */
	public static function round( $value, $decimals = 0 ) {
		return round( $value, $decimals );
	}

	/**
	 * Format a number with the . as the decimal separator and the , as the thousand separator, rounded to a precision.
	 *
	 * The is the common number format in English-language regions.
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $value    Number to be rounded and formatted.
	 * @param double|int $decimals Optional. Number of decimals after the decimal separator after the rounding.
	 * @return double Formatted number.
	 */
	public static function number_format( $value, $decimals = 0 ) {
		return number_format( $value, $decimals, '.', ',' );
	}

	/**
	 * Format a number with the , as the decimal separator and the space as the thousand separator, rounded to a precision.
	 *
	 * The is the common number format in non-English-language regions, mainly in Europe.
	 *
	 * @since 1.0.0
	 *
	 * @param double|int $value    Number to be rounded and formatted.
	 * @param double|int $decimals Optional. Number of decimals after the decimal separator after the rounding.
	 * @return double Formatted number.
	 */
	public static function number_format_eu( $value, $decimals = 0 ) {
		return number_format( $value, $decimals, ',', ' ' );
	}

	/**
	 * Set the seed for the generation of random numbers.
	 *
	 * @since 1.0.0
	 *
	 * @param string The seed.
	 */
	protected static function _set_random_seed( $random_seed ) {
		self::$random_seed = $random_seed;
	}

	/**
	 * Get the seed for the generation of random numbers.
	 *
	 * @since 1.0.0
	 *
	 * @return string The seed.
	 */
	protected static function _get_random_seed() {
		if ( is_null( self::$random_seed ) ) {
			return microtime();
		} else {
			return self::$random_seed;
		}
	}

	/**
	 * Get a random integer from a range.
	 *
	 * @since 1.0.0
	 *
	 * @param int $min Minimum value for the range.
	 * @param int $max Maximum value for the range.
	 * @return int Random integer from the range [$min, $max].
	 */
	public static function rand_int( $min, $max ) {
		// Swap min and max value if min is bigger than max.
		if ( $min > $max ) {
			$tmp = $max;
			$max = $min;
			$min = $tmp;
			unset( $tmp );
		}
		$number_characters = ceil( log( $max + 1 - $min, '16' ) );
		$md5string = md5( self::_get_random_seed() );
		$offset = 0;
		do {
			while ( ( $offset + $number_characters ) > strlen( $md5string ) ) {
				$md5string .= md5( $md5string );
			}
			$randomno = hexdec( substr( $md5string, $offset, $number_characters ) );
			$offset += $number_characters;
		} while ( ( $min + $randomno ) > $max );
		return $min + $randomno;
	}

	/**
	 * Get a random double value from a range [0, 1].
	 *
	 * @since 1.0.0
	 *
	 * @return double Random number from the range [0, 1].
	 */
	public static function rand_float() {
		$random_values = unpack( 'v', md5( self::_get_random_seed(), true ) );
		return array_shift( $random_values ) / 65536;
	}

} // class EvalMath_Functions