Commit inicial - WordPress Análisis de Precios Unitarios

- WordPress core y plugins
- Tema Twenty Twenty-Four configurado
- Plugin allow-unfiltered-html.php simplificado
- .gitignore configurado para excluir wp-config.php y uploads

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-11-03 21:04:30 -06:00
commit a22573bf0b
24068 changed files with 4993111 additions and 0 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,480 @@
<?php
/**
* 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 <http://www.gnu.org/licenses/>.
*
* @license http://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 csstidy_print {
/**
* csstidy object
* @var object
*/
public $parser;
/**
* Saves the input CSS string
* @var string
* @access private
*/
public $input_css = '';
/**
* Saves the formatted CSS string
* @var string
* @access public
*/
public $output_css = '';
/**
* Saves the formatted CSS string (plain text)
* @var string
* @access public
*/
public $output_css_plain = '';
/**
* Constructor
* @param array $css contains the class csstidy
* @access private
* @version 1.0
*/
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)
* @access private
* @version 1.0
*/
public function _reset() {
$this->output_css = '';
$this->output_css_plain = '';
}
/**
* Returns the CSS code as plain text
* @param string $default_media default @media to add to selectors without any @media
* @return string
* @access public
* @version 1.0
*/
public function plain($default_media='') {
$this->_print(true, $default_media);
return $this->output_css_plain;
}
/**
* Returns the formatted CSS code
* @param string $default_media default @media to add to selectors without any @media
* @return string
* @access public
* @version 1.0
*/
public function formatted($default_media='') {
$this->_print(false, $default_media);
return $this->output_css;
}
/**
* Returns the formatted CSS code to make a complete webpage
* @param string $doctype shorthand for the document type
* @param bool $externalcss indicates whether styles to be attached internally or as an external stylesheet
* @param string $title title to be added in the head of the document
* @param string $lang two-letter language code to be added to the output
* @return string
* @access public
* @version 1.4
*/
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 = $cssparsed = '';
$this->output_css_plain = & $output;
$output .= $doctype_output . "\n" . '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="' . $lang . '"';
$output .= ( $doctype === 'xhtml1.1') ? '>' : ' lang="' . $lang . '">';
$output .= "\n<head>\n <title>$title</title>";
if ($externalcss) {
$output .= "\n <style type=\"text/css\">\n";
$cssparsed = file_get_contents('cssparsed.css');
$output .= $cssparsed; // 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
* @param bool $plain plain text or not
* @param string $default_media default @media to add to selectors without any @media
* @access private
* @version 2.0
*/
public 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++) {
if (substr($this->import[$i], 0, 4) === 'url(' && substr($this->import[$i], -1, 1) === ')') {
$this->import[$i] = '"' . substr($this->import[$i], 4, -1) . '"';
$this->parser->log('Optimised @import : Removed "url("', 'Information');
}
else if (!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 (($p=strpos($this->namespace,"url("))!==false && 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;
$indent_level = 0;
foreach ($this->tokens as $key => $token) {
switch ($token[0]) {
case AT_START:
$out .= $template[0] . $this->_htmlsp($token[1], $plain) . $template[1];
$indent_level++;
if (!isset($in_at_out[$indent_level])) {
$in_at_out[$indent_level] = '';
}
$out = & $in_at_out[$indent_level];
break;
case SEL_START:
if ($this->parser->get_cfg('lowercase_s'))
$token[1] = strtolower($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 ($this->parser->get_cfg('case_properties') === 2) {
$token[1] = strtoupper($token[1]);
} elseif ($this->parser->get_cfg('case_properties') === 1) {
$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 ($this->_seeknocomment($key, 1) == SEL_END && $this->parser->get_cfg('remove_last_;')) {
$out .= str_replace(';', '', $template[6]);
} else {
$out .= $template[6];
}
break;
case SEL_END:
$out .= $template[7];
if ($this->_seeknocomment($key, 1) != AT_END)
$out .= $template[8];
break;
case AT_END:
if (strlen($template[10])) {
// indent the bloc we are closing
$out = str_replace("\n\n", "\r\n", $out); // don't fill empty lines
$out = str_replace("\n", "\n" . $template[10], $out);
$out = str_replace("\r\n", "\n\n", $out);
}
if ($indent_level > 1) {
$out = & $in_at_out[$indent_level-1];
}
else {
$out = & $output;
}
$out .= $template[10] . $in_at_out[$indent_level];
if ($this->_seeknocomment($key, 1) != AT_END) {
$out .= $template[9];
}
else {
$out .= rtrim($template[9]);
}
unset($in_at_out[$indent_level]);
$indent_level--;
break;
case IMPORTANT_COMMENT:
case COMMENT:
$out .= $template[11] . '/*' . $this->_htmlsp($token[1], $plain) . '*/' . $template[12];
break;
}
}
$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
* @param integer $key current position
* @param integer $move move this far
* @return mixed a token type
* @access private
* @version 1.0
*/
public 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 ($this->tokens[$i][0] == COMMENT) {
$move += 1;
continue;
}
return $this->tokens[$i][0];
}
}
/**
* Converts $this->css array to a raw array ($this->tokens)
* @param string $default_media default @media to add to selectors without any @media
* @access private
* @version 1.0
*/
public 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');
// important comment section ?
if (isset($this->css['!'])) {
$this->parser->_add_token(IMPORTANT_COMMENT, rtrim($this->css['!']), true);
unset($this->css['!']);
}
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))) {
$parts_to_open = explode('{', $medium);
foreach ($parts_to_open as $part) {
$this->parser->_add_token(AT_START, $part, 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 (strncmp($property,"//",2)!==0) {
$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))) {
$parts_to_close = explode('{', $medium);
foreach (array_reverse($parts_to_close) as $part) {
$this->parser->_add_token(AT_END, $part, true);
}
}
} elseif ($default_media) {
$this->parser->_add_token(AT_END, $default_media, true);
}
}
}
/**
* Same as htmlspecialchars, only that chars are not replaced if $plain !== true. This makes print_code() cleaner.
* @param string $string
* @param bool $plain
* @return string
* @see csstidy_print::_print()
* @access private
* @version 1.0
*/
public function _htmlsp($string, $plain) {
if (!$plain) {
return htmlspecialchars($string, ENT_QUOTES, 'utf-8');
}
return $string;
}
/**
* Get compression ratio
* @access public
* @return float
* @version 1.2
*/
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.
* @access public
* @return string
* @version 1.1
*/
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 ($diff == 0) {
return '+-' . $diff;
}
return $diff;
}
/**
* Get the size of either input or output CSS in KB
* @param string $loc default is "output"
* @access public
* @return integer
* @version 1.0
*/
public function size($loc = 'output') {
if ($loc === 'output' && !$this->output_css) {
$this->formatted();
}
if ($loc === 'input') {
return (strlen($this->input_css) / 1000);
} else {
return (strlen($this->output_css_plain) / 1000);
}
}
}

View File

@@ -0,0 +1,650 @@
<?php
/**
* 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 http://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
*
* @global array $data['csstidy']['whitespace']
* @version 1.0
*/
$data['csstidy']['whitespace'] = array(' ',"\n","\t","\r","\x0B");
/**
* All CSS tokens used by csstidy
*
* @global string $data['csstidy']['tokens']
* @version 1.0
*/
$data['csstidy']['tokens'] = '/@}{;:=\'"(,\\!$%&)*+.<>?[]^`|~';
/**
* All CSS units (CSS 3 units included)
*
* @see compress_numbers()
* @global array $data['csstidy']['units']
* @version 1.0
*/
$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
*
* @global array $data['csstidy']['at_rules']
* @version 1.1
*/
$data['csstidy']['at_rules'] = array('page' => 'is','font-face' => 'atis','charset' => 'iv', 'import' => 'iv','namespace' => 'iv','media' => 'at', 'supports' => 'at', 'keyframes' => 'at','-moz-keyframes' => 'at','-o-keyframes' => 'at','-webkit-keyframes' => 'at','-ms-keyframes' => 'at');
/**
* Properties that need a value with unit
*
* @todo CSS3 properties
* @see compress_numbers();
* @global array $data['csstidy']['unit_values']
* @version 1.2
*/
$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();
* @global array $data['csstidy']['color_values']
* @version 1.0
*/
$data['csstidy']['color_values'] = array();
$data['csstidy']['color_values'][] = 'background-color';
$data['csstidy']['color_values'][] = 'border-color';
$data['csstidy']['color_values'][] = 'border-top-color';
$data['csstidy']['color_values'][] = 'border-right-color';
$data['csstidy']['color_values'][] = 'border-bottom-color';
$data['csstidy']['color_values'][] = 'border-left-color';
$data['csstidy']['color_values'][] = 'color';
$data['csstidy']['color_values'][] = 'outline-color';
$data['csstidy']['color_values'][] = 'column-rule-color';
/**
* Default values for the background properties
*
* @todo Possibly property names will change during CSS3 development
* @global array $data['csstidy']['background_prop_default']
* @see dissolve_short_bg()
* @see merge_bg()
* @version 1.0
*/
$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
*
* @global array $data['csstidy']['font_prop_default']
* @see merge_fonts()
* @version 1.3
*/
$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
*
* @global array $data['csstidy']['replace_colors']
* @see cut_color()
* @version 1.0
*/
$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 devided into four properties and/or have four subvalues
*
* @global array $data['csstidy']['shorthands']
* @todo Are there new ones in CSS3?
* @see dissolve_4value_shorthands()
* @see merge_4value_shorthands()
* @version 1.0
*/
$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']['radius_shorthands']['border-radius'] = array('border-top-left-radius','border-top-right-radius','border-bottom-right-radius','border-bottom-left-radius');
$data['csstidy']['radius_shorthands']['-webkit-border-radius'] = array('-webkit-border-top-left-radius','-webkit-border-top-right-radius','-webkit-border-bottom-right-radius','-webkit-border-bottom-left-radius');
$data['csstidy']['radius_shorthands']['-moz-border-radius'] = array('-moz-border-radius-topleft','-moz-border-radius-topright','-moz-border-radius-bottomright','-moz-border-radius-bottomleft');
/**
* All CSS Properties. Needed for csstidy::property_is_next()
*
* @global array $data['csstidy']['all_properties']
* @version 1.1
* @see csstidy::property_is_next()
*/
$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-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']['fit'] = 'CSS3.0';
$data['csstidy']['all_properties']['fit-position'] = 'CSS3.0';
$data['csstidy']['all_properties']['flex-align'] = 'CSS3.0';
$data['csstidy']['all_properties']['flex-flow'] = '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']['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-columns'] = 'CSS3.0';
$data['csstidy']['all_properties']['grid-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']['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']['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']['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';
$data['csstidy']['all_properties']['--custom'] = 'CSS3.0';
/**
* An array containing all properties that can accept a quoted string as a value.
*
* @global array $data['csstidy']['quoted_string_properties']
*/
$data['csstidy']['quoted_string_properties'] = array('content', 'font-family', 'quotes');
/**
* An array containing all properties that can be defined multiple times without being overwritten.
*
* @global array $data['csstidy']['quoted_string_properties']
*/
$data['csstidy']['multiple_properties'] = array('background', 'background-image');
/**
* An array containing all predefined templates.
*
* @global array $data['csstidy']['predefined_templates']
* @version 1.0
* @see csstidy::load_template()
*/
$data['csstidy']['predefined_templates']['default'][0] = '<span class="at">'; //string before @rule
$data['csstidy']['predefined_templates']['default'][1] = '</span> <span class="format">{</span>'."\n"; //bracket after @-rule
$data['csstidy']['predefined_templates']['default'][2] = '<span class="selector">'; //string before selector
$data['csstidy']['predefined_templates']['default'][3] = '</span> <span class="format">{</span>'."\n"; //bracket after selector
$data['csstidy']['predefined_templates']['default'][4] = '<span class="property">'; //string before property
$data['csstidy']['predefined_templates']['default'][5] = '</span><span class="value">'; //string after property+before value
$data['csstidy']['predefined_templates']['default'][6] = '</span><span class="format">;</span>'."\n"; //string after value
$data['csstidy']['predefined_templates']['default'][7] = '<span class="format">}</span>'; //closing bracket - selector
$data['csstidy']['predefined_templates']['default'][8] = "\n\n"; //space between blocks {...}
$data['csstidy']['predefined_templates']['default'][9] = "\n".'<span class="format">}</span>'. "\n\n"; //closing bracket @-rule
$data['csstidy']['predefined_templates']['default'][10] = ''; //indent in @-rule
$data['csstidy']['predefined_templates']['default'][11] = '<span class="comment">'; // before comment
$data['csstidy']['predefined_templates']['default'][12] = '</span>'."\n"; // after comment
$data['csstidy']['predefined_templates']['default'][13] = "\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>'."\n"; // 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";

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,34 @@
<?php
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Abstract class for the Authentication in the API client
* @author Chris Chabot <chabotc@google.com>
*
*/
abstract class W3TCG_Google_Auth_Abstract
{
/**
* An utility function that first calls $this->auth->sign($request) and then
* executes makeRequest() on that signed request. Used for when a request
* should be authenticated
* @param W3TCG_Google_Http_Request $request
* @return W3TCG_Google_Http_Request $request
*/
abstract public function authenticatedRequest(W3TCG_Google_Http_Request $request);
abstract public function sign(W3TCG_Google_Http_Request $request);
}

View File

@@ -0,0 +1,100 @@
<?php
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* WARNING - this class depends on the Google App Engine PHP library
* which is 5.3 and above only, so if you include this in a PHP 5.2
* setup or one without 5.3 things will blow up.
*/
use google\appengine\api\app_identity\AppIdentityService;
/**
* Authentication via the Google App Engine App Identity service.
*/
class W3TCG_Google_Auth_AppIdentity extends W3TCG_Google_Auth_Abstract
{
const CACHE_PREFIX = "W3TCG_Google_Auth_AppIdentity::";
private $key = null;
private $client;
private $token = false;
private $tokenScopes = false;
public function __construct(W3TCG_Google_Client $client, $config = null)
{
$this->client = $client;
}
/**
* Retrieve an access token for the scopes supplied.
*/
public function authenticateForScope($scopes)
{
if ($this->token && $this->tokenScopes == $scopes) {
return $this->token;
}
$cacheKey = self::CACHE_PREFIX;
if (is_string($scopes)) {
$cacheKey .= $scopes;
} else if (is_array($scopes)) {
$cacheKey .= implode(":", $scopes);
}
$this->token = $this->client->getCache()->get($cacheKey);
if (!$this->token) {
$this->token = AppIdentityService::getAccessToken($scopes);
if ($this->token) {
$this->client->getCache()->set(
$cacheKey,
$this->token
);
}
}
$this->tokenScopes = $scopes;
return $this->token;
}
/**
* Perform an authenticated / signed apiHttpRequest.
* This function takes the apiHttpRequest, calls apiAuth->sign on it
* (which can modify the request in what ever way fits the auth mechanism)
* and then calls apiCurlIO::makeRequest on the signed request
*
* @param W3TCG_Google_Http_Request $request
* @return W3TCG_Google_Http_Request The resulting HTTP response including the
* responseHttpCode, responseHeaders and responseBody.
*/
public function authenticatedRequest(W3TCG_Google_Http_Request $request)
{
$request = $this->sign($request);
return $this->client->getIo()->makeRequest($request);
}
public function sign(W3TCG_Google_Http_Request $request)
{
if (!$this->token) {
// No token, so nothing to do.
return $request;
}
// Add the OAuth2 header to the request
$request->setRequestHeaders(
array('Authorization' => 'Bearer ' . $this->token['access_token'])
);
return $request;
}
}

View File

@@ -0,0 +1,134 @@
<?php
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Credentials object used for OAuth 2.0 Signed JWT assertion grants.
*
* @author Chirag Shah <chirags@google.com>
*/
class W3TCG_Google_Auth_AssertionCredentials
{
const MAX_TOKEN_LIFETIME_SECS = 3600;
public $serviceAccountName;
public $scopes;
public $privateKey;
public $privateKeyPassword;
public $assertionType;
public $sub;
/**
* @deprecated
* @link http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06
*/
public $prn;
private $useCache;
/**
* @param $serviceAccountName
* @param $scopes array List of scopes
* @param $privateKey
* @param string $privateKeyPassword
* @param string $assertionType
* @param bool|string $sub The email address of the user for which the
* application is requesting delegated access.
* @param bool useCache Whether to generate a cache key and allow
* automatic caching of the generated token.
*/
public function __construct(
$serviceAccountName,
$scopes,
$privateKey,
$privateKeyPassword = 'notasecret',
$assertionType = 'http://oauth.net/grant_type/jwt/1.0/bearer',
$sub = false,
$useCache = true
) {
$this->serviceAccountName = $serviceAccountName;
$this->scopes = is_string($scopes) ? $scopes : implode(' ', $scopes);
$this->privateKey = $privateKey;
$this->privateKeyPassword = $privateKeyPassword;
$this->assertionType = $assertionType;
$this->sub = $sub;
$this->prn = $sub;
$this->useCache = $useCache;
}
/**
* Generate a unique key to represent this credential.
* @return string
*/
public function getCacheKey()
{
if (!$this->useCache) {
return false;
}
$h = $this->sub;
$h .= $this->assertionType;
$h .= $this->privateKey;
$h .= $this->scopes;
$h .= $this->serviceAccountName;
return md5($h);
}
public function generateAssertion()
{
$now = time();
$jwtParams = array(
'aud' => W3TCG_Google_Auth_OAuth2::OAUTH2_TOKEN_URI,
'scope' => $this->scopes,
'iat' => $now,
'exp' => $now + self::MAX_TOKEN_LIFETIME_SECS,
'iss' => $this->serviceAccountName,
);
if ($this->sub !== false) {
$jwtParams['sub'] = $this->sub;
} else if ($this->prn !== false) {
$jwtParams['prn'] = $this->prn;
}
return $this->makeSignedJwt($jwtParams);
}
/**
* Creates a signed JWT.
* @param array $payload
* @return string The signed JWT.
*/
private function makeSignedJwt($payload)
{
$header = array('typ' => 'JWT', 'alg' => 'RS256');
$payload = json_encode($payload);
// Handle some overzealous escaping in PHP json that seemed to cause some errors
// with claimsets.
$payload = str_replace('\/', '/', $payload);
$segments = array(
W3TCG_Google_Utils::urlSafeB64Encode(json_encode($header)),
W3TCG_Google_Utils::urlSafeB64Encode($payload)
);
$signingInput = implode('.', $segments);
$signer = new W3TCG_Google_Signer_P12($this->privateKey, $this->privateKeyPassword);
$signature = $signer->sign($signingInput);
$segments[] = W3TCG_Google_Utils::urlSafeB64Encode($signature);
return implode(".", $segments);
}
}

View File

@@ -0,0 +1,20 @@
<?php
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class W3TCG_Google_Auth_Exception extends W3TCG_Google_Exception
{
}

View File

@@ -0,0 +1,67 @@
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Class to hold information about an authenticated login.
*
* @author Brian Eaton <beaton@google.com>
*/
class W3TCG_Google_Auth_LoginTicket
{
const USER_ATTR = "sub";
// Information from id token envelope.
private $envelope;
// Information from id token payload.
private $payload;
/**
* Creates a user based on the supplied token.
*
* @param string $envelope Header from a verified authentication token.
* @param string $payload Information from a verified authentication token.
*/
public function __construct($envelope, $payload)
{
$this->envelope = $envelope;
$this->payload = $payload;
}
/**
* Returns the numeric identifier for the user.
* @throws W3TCG_Google_Auth_Exception
* @return
*/
public function getUserId()
{
if (array_key_exists(self::USER_ATTR, $this->payload)) {
return $this->payload[self::USER_ATTR];
}
throw new W3TCG_Google_Auth_Exception("No user_id in token");
}
/**
* Returns attributes from the login ticket. This can contain
* various information about the user session.
* @return array
*/
public function getAttributes()
{
return array("envelope" => $this->envelope, "payload" => $this->payload);
}
}

View File

@@ -0,0 +1,618 @@
<?php
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Authentication class that deals with the OAuth 2 web-server authentication flow
*
* @author Chris Chabot <chabotc@google.com>
* @author Chirag Shah <chirags@google.com>
*
*/
class W3TCG_Google_Auth_OAuth2 extends W3TCG_Google_Auth_Abstract
{
const OAUTH2_REVOKE_URI = 'https://accounts.google.com/o/oauth2/revoke';
const OAUTH2_TOKEN_URI = 'https://accounts.google.com/o/oauth2/token';
const OAUTH2_AUTH_URL = 'https://accounts.google.com/o/oauth2/auth';
const CLOCK_SKEW_SECS = 300; // five minutes in seconds
const AUTH_TOKEN_LIFETIME_SECS = 300; // five minutes in seconds
const MAX_TOKEN_LIFETIME_SECS = 86400; // one day in seconds
const OAUTH2_ISSUER = 'accounts.google.com';
/** @var W3TCG_Google_Auth_AssertionCredentials $assertionCredentials */
private $assertionCredentials;
/**
* @var string The state parameters for CSRF and other forgery protection.
*/
private $state;
/**
* @var array The token bundle.
*/
private $token = array();
/**
* @var W3TCG_Google_Client the base client
*/
private $client;
/**
* Instantiates the class, but does not initiate the login flow, leaving it
* to the discretion of the caller.
*/
public function __construct(W3TCG_Google_Client $client)
{
$this->client = $client;
}
/**
* Perform an authenticated / signed apiHttpRequest.
* This function takes the apiHttpRequest, calls apiAuth->sign on it
* (which can modify the request in what ever way fits the auth mechanism)
* and then calls apiCurlIO::makeRequest on the signed request
*
* @param W3TCG_Google_Http_Request $request
* @return W3TCG_Google_Http_Request The resulting HTTP response including the
* responseHttpCode, responseHeaders and responseBody.
*/
public function authenticatedRequest(W3TCG_Google_Http_Request $request)
{
$request = $this->sign($request);
return $this->client->getIo()->makeRequest($request);
}
/**
* @param string $code
* @throws W3TCG_Google_Auth_Exception
* @return string
*/
public function authenticate($code)
{
if (strlen($code) == 0) {
throw new W3TCG_Google_Auth_Exception("Invalid code");
}
// We got here from the redirect from a successful authorization grant,
// fetch the access token
$request = new W3TCG_Google_Http_Request(
self::OAUTH2_TOKEN_URI,
'POST',
array(),
array(
'code' => $code,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->client->getClassConfig($this, 'redirect_uri'),
'client_id' => $this->client->getClassConfig($this, 'client_id'),
'client_secret' => $this->client->getClassConfig($this, 'client_secret')
)
);
$request->disableGzip();
$response = $this->client->getIo()->makeRequest($request);
if ($response->getResponseHttpCode() == 200) {
$this->setAccessToken($response->getResponseBody());
$this->token['created'] = time();
return $this->getAccessToken();
} else {
$decodedResponse = json_decode($response->getResponseBody(), true);
if ($decodedResponse != null && $decodedResponse['error']) {
$errorText = $decodedResponse['error'];
if (isset($decodedResponse['error_description'])) {
$errorText .= ": " . $decodedResponse['error_description'];
}
}
throw new W3TCG_Google_Auth_Exception(
sprintf(
"Error fetching OAuth2 access token, message: '%s'",
$errorText
),
$response->getResponseHttpCode()
);
}
}
/**
* Create a URL to obtain user authorization.
* The authorization endpoint allows the user to first
* authenticate, and then grant/deny the access request.
* @param string $scope The scope is expressed as a list of space-delimited strings.
* @return string
*/
public function createAuthUrl($scope)
{
$params = array(
'response_type' => 'code',
'redirect_uri' => $this->client->getClassConfig($this, 'redirect_uri'),
'client_id' => $this->client->getClassConfig($this, 'client_id'),
'scope' => $scope,
'access_type' => $this->client->getClassConfig($this, 'access_type'),
);
// Prefer prompt to approval prompt.
if ($this->client->getClassConfig($this, 'prompt')) {
$params = $this->maybeAddParam($params, 'prompt');
} else {
$params = $this->maybeAddParam($params, 'approval_prompt');
}
$params = $this->maybeAddParam($params, 'login_hint');
$params = $this->maybeAddParam($params, 'hd');
$params = $this->maybeAddParam($params, 'openid.realm');
$params = $this->maybeAddParam($params, 'include_granted_scopes');
// If the list of scopes contains plus.login, add request_visible_actions
// to auth URL.
$rva = $this->client->getClassConfig($this, 'request_visible_actions');
if (strpos($scope, 'plus.login') && strlen($rva) > 0) {
$params['request_visible_actions'] = $rva;
}
if (isset($this->state)) {
$params['state'] = $this->state;
}
return self::OAUTH2_AUTH_URL . "?" . http_build_query($params, '', '&');
}
/**
* @param string $token
* @throws W3TCG_Google_Auth_Exception
*/
public function setAccessToken($token)
{
$token = json_decode($token, true);
if ($token == null) {
throw new W3TCG_Google_Auth_Exception('Could not json decode the token');
}
if (! isset($token['access_token'])) {
throw new W3TCG_Google_Auth_Exception("Invalid token format");
}
$this->token = $token;
}
public function getAccessToken()
{
return json_encode($this->token);
}
public function getRefreshToken()
{
if (array_key_exists('refresh_token', $this->token)) {
return $this->token['refresh_token'];
} else {
return null;
}
}
public function setState($state)
{
$this->state = $state;
}
public function setAssertionCredentials(W3TCG_Google_Auth_AssertionCredentials $creds)
{
$this->assertionCredentials = $creds;
}
/**
* Include an accessToken in a given apiHttpRequest.
* @param W3TCG_Google_Http_Request $request
* @return W3TCG_Google_Http_Request
* @throws W3TCG_Google_Auth_Exception
*/
public function sign(W3TCG_Google_Http_Request $request)
{
// add the developer key to the request before signing it
if ($this->client->getClassConfig($this, 'developer_key')) {
$request->setQueryParam('key', $this->client->getClassConfig($this, 'developer_key'));
}
// Cannot sign the request without an OAuth access token.
if (null == $this->token && null == $this->assertionCredentials) {
return $request;
}
// Check if the token is set to expire in the next 30 seconds
// (or has already expired).
if ($this->isAccessTokenExpired()) {
if ($this->assertionCredentials) {
$this->refreshTokenWithAssertion();
} else {
if (! array_key_exists('refresh_token', $this->token)) {
throw new W3TCG_Google_Auth_Exception(
"The OAuth 2.0 access token has expired,"
." and a refresh token is not available. Refresh tokens"
." are not returned for responses that were auto-approved."
);
}
$this->refreshToken($this->token['refresh_token']);
}
}
// Add the OAuth2 header to the request
$request->setRequestHeaders(
array('Authorization' => 'Bearer ' . $this->token['access_token'])
);
return $request;
}
/**
* Fetches a fresh access token with the given refresh token.
* @param string $refreshToken
* @return void
*/
public function refreshToken($refreshToken)
{
$this->refreshTokenRequest(
array(
'client_id' => $this->client->getClassConfig($this, 'client_id'),
'client_secret' => $this->client->getClassConfig($this, 'client_secret'),
'refresh_token' => $refreshToken,
'grant_type' => 'refresh_token'
)
);
}
/**
* Fetches a fresh access token with a given assertion token.
* @param W3TCG_Google_Auth_AssertionCredentials $assertionCredentials optional.
* @return void
*/
public function refreshTokenWithAssertion($assertionCredentials = null)
{
if (!$assertionCredentials) {
$assertionCredentials = $this->assertionCredentials;
}
$cacheKey = $assertionCredentials->getCacheKey();
if ($cacheKey) {
// We can check whether we have a token available in the
// cache. If it is expired, we can retrieve a new one from
// the assertion.
$token = $this->client->getCache()->get($cacheKey);
if ($token) {
$this->setAccessToken($token);
}
if (!$this->isAccessTokenExpired()) {
return;
}
}
$this->refreshTokenRequest(
array(
'grant_type' => 'assertion',
'assertion_type' => $assertionCredentials->assertionType,
'assertion' => $assertionCredentials->generateAssertion(),
)
);
if ($cacheKey) {
// Attempt to cache the token.
$this->client->getCache()->set(
$cacheKey,
$this->getAccessToken()
);
}
}
private function refreshTokenRequest($params)
{
$http = new W3TCG_Google_Http_Request(
self::OAUTH2_TOKEN_URI,
'POST',
array(),
$params
);
$http->disableGzip();
$request = $this->client->getIo()->makeRequest($http);
$code = $request->getResponseHttpCode();
$body = $request->getResponseBody();
if (200 == $code) {
$token = json_decode($body, true);
if ($token == null) {
throw new W3TCG_Google_Auth_Exception("Could not json decode the access token");
}
if (! isset($token['access_token']) || ! isset($token['expires_in'])) {
throw new W3TCG_Google_Auth_Exception("Invalid token format");
}
if (isset($token['id_token'])) {
$this->token['id_token'] = $token['id_token'];
}
$this->token['access_token'] = $token['access_token'];
$this->token['expires_in'] = $token['expires_in'];
$this->token['created'] = time();
} else {
throw new W3TCG_Google_Auth_Exception("Error refreshing the OAuth2 token, message: '$body'", $code);
}
}
/**
* Revoke an OAuth2 access token or refresh token. This method will revoke the current access
* token, if a token isn't provided.
* @throws W3TCG_Google_Auth_Exception
* @param string|null $token The token (access token or a refresh token) that should be revoked.
* @return boolean Returns True if the revocation was successful, otherwise False.
*/
public function revokeToken($token = null)
{
if (!$token) {
if (!$this->token) {
// Not initialized, no token to actually revoke
return false;
} elseif (array_key_exists('refresh_token', $this->token)) {
$token = $this->token['refresh_token'];
} else {
$token = $this->token['access_token'];
}
}
$request = new W3TCG_Google_Http_Request(
self::OAUTH2_REVOKE_URI,
'POST',
array(),
"token=$token"
);
$request->disableGzip();
$response = $this->client->getIo()->makeRequest($request);
$code = $response->getResponseHttpCode();
if ($code == 200) {
$this->token = null;
return true;
}
return false;
}
/**
* Returns if the access_token is expired.
* @return bool Returns True if the access_token is expired.
*/
public function isAccessTokenExpired()
{
if (!$this->token || !isset($this->token['created'])) {
return true;
}
// If the token is set to expire in the next 30 seconds.
$expired = ($this->token['created']
+ ($this->token['expires_in'] - 30)) < time();
return $expired;
}
// Gets federated sign-on certificates to use for verifying identity tokens.
// Returns certs as array structure, where keys are key ids, and values
// are PEM encoded certificates.
private function getFederatedSignOnCerts()
{
return $this->retrieveCertsFromLocation(
$this->client->getClassConfig($this, 'federated_signon_certs_url')
);
}
/**
* Retrieve and cache a certificates file.
*
* @param $url string location
* @throws W3TCG_Google_Auth_Exception
* @return array certificates
*/
public function retrieveCertsFromLocation($url)
{
// If we're retrieving a local file, just grab it.
if ("http" != substr($url, 0, 4)) {
$file = file_get_contents($url);
if ($file) {
return json_decode($file, true);
} else {
throw new W3TCG_Google_Auth_Exception(
"Failed to retrieve verification certificates: '" .
$url . "'."
);
}
}
// This relies on makeRequest caching certificate responses.
$request = $this->client->getIo()->makeRequest(
new W3TCG_Google_Http_Request(
$url
)
);
if ($request->getResponseHttpCode() == 200) {
$certs = json_decode($request->getResponseBody(), true);
if ($certs) {
return $certs;
}
}
throw new W3TCG_Google_Auth_Exception(
"Failed to retrieve verification certificates: '" .
$request->getResponseBody() . "'.",
$request->getResponseHttpCode()
);
}
/**
* Verifies an id token and returns the authenticated apiLoginTicket.
* Throws an exception if the id token is not valid.
* The audience parameter can be used to control which id tokens are
* accepted. By default, the id token must have been issued to this OAuth2 client.
*
* @param $id_token
* @param $audience
* @return W3TCG_Google_Auth_LoginTicket
*/
public function verifyIdToken($id_token = null, $audience = null)
{
if (!$id_token) {
$id_token = $this->token['id_token'];
}
$certs = $this->getFederatedSignonCerts();
if (!$audience) {
$audience = $this->client->getClassConfig($this, 'client_id');
}
return $this->verifySignedJwtWithCerts($id_token, $certs, $audience, self::OAUTH2_ISSUER);
}
/**
* Verifies the id token, returns the verified token contents.
*
* @param $jwt string the token
* @param $certs array of certificates
* @param $required_audience string the expected consumer of the token
* @param [$issuer] the expected issues, defaults to Google
* @param [$max_expiry] the max lifetime of a token, defaults to MAX_TOKEN_LIFETIME_SECS
* @throws W3TCG_Google_Auth_Exception
* @return mixed token information if valid, false if not
*/
public function verifySignedJwtWithCerts(
$jwt,
$certs,
$required_audience,
$issuer = null,
$max_expiry = null
) {
if (!$max_expiry) {
// Set the maximum time we will accept a token for.
$max_expiry = self::MAX_TOKEN_LIFETIME_SECS;
}
$segments = explode(".", $jwt);
if (count($segments) != 3) {
throw new W3TCG_Google_Auth_Exception("Wrong number of segments in token: $jwt");
}
$signed = $segments[0] . "." . $segments[1];
$signature = W3TCG_Google_Utils::urlSafeB64Decode($segments[2]);
// Parse envelope.
$envelope = json_decode(W3TCG_Google_Utils::urlSafeB64Decode($segments[0]), true);
if (!$envelope) {
throw new W3TCG_Google_Auth_Exception("Can't parse token envelope: " . $segments[0]);
}
// Parse token
$json_body = W3TCG_Google_Utils::urlSafeB64Decode($segments[1]);
$payload = json_decode($json_body, true);
if (!$payload) {
throw new W3TCG_Google_Auth_Exception("Can't parse token payload: " . $segments[1]);
}
// Check signature
$verified = false;
foreach ($certs as $keyName => $pem) {
$public_key = new W3TCG_Google_Verifier_Pem($pem);
if ($public_key->verify($signed, $signature)) {
$verified = true;
break;
}
}
if (!$verified) {
throw new W3TCG_Google_Auth_Exception("Invalid token signature: $jwt");
}
// Check issued-at timestamp
$iat = 0;
if (array_key_exists("iat", $payload)) {
$iat = $payload["iat"];
}
if (!$iat) {
throw new W3TCG_Google_Auth_Exception("No issue time in token: $json_body");
}
$earliest = $iat - self::CLOCK_SKEW_SECS;
// Check expiration timestamp
$now = time();
$exp = 0;
if (array_key_exists("exp", $payload)) {
$exp = $payload["exp"];
}
if (!$exp) {
throw new W3TCG_Google_Auth_Exception("No expiration time in token: $json_body");
}
if ($exp >= $now + $max_expiry) {
throw new W3TCG_Google_Auth_Exception(
sprintf("Expiration time too far in future: %s", $json_body)
);
}
$latest = $exp + self::CLOCK_SKEW_SECS;
if ($now < $earliest) {
throw new W3TCG_Google_Auth_Exception(
sprintf(
"Token used too early, %s < %s: %s",
$now,
$earliest,
$json_body
)
);
}
if ($now > $latest) {
throw new W3TCG_Google_Auth_Exception(
sprintf(
"Token used too late, %s > %s: %s",
$now,
$latest,
$json_body
)
);
}
$iss = $payload['iss'];
if ($issuer && $iss != $issuer) {
throw new W3TCG_Google_Auth_Exception(
sprintf(
"Invalid issuer, %s != %s: %s",
$iss,
$issuer,
$json_body
)
);
}
// Check audience
$aud = $payload["aud"];
if ($aud != $required_audience) {
throw new W3TCG_Google_Auth_Exception(
sprintf(
"Wrong recipient, %s != %s:",
$aud,
$required_audience,
$json_body
)
);
}
// All good.
return new W3TCG_Google_Auth_LoginTicket($envelope, $payload);
}
/**
* Add a parameter to the auth params if not empty string.
*/
private function maybeAddParam($params, $name)
{
$param = $this->client->getClassConfig($this, $name);
if ($param != '') {
$params[$name] = $param;
}
return $params;
}
}

View File

@@ -0,0 +1,59 @@
<?php
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Simple API access implementation. Can either be used to make requests
* completely unauthenticated, or by using a Simple API Access developer
* key.
* @author Chris Chabot <chabotc@google.com>
* @author Chirag Shah <chirags@google.com>
*/
class W3TCG_Google_Auth_Simple extends W3TCG_Google_Auth_Abstract
{
private $key = null;
private $client;
public function __construct(W3TCG_Google_Client $client, $config = null)
{
$this->client = $client;
}
/**
* Perform an authenticated / signed apiHttpRequest.
* This function takes the apiHttpRequest, calls apiAuth->sign on it
* (which can modify the request in what ever way fits the auth mechanism)
* and then calls apiCurlIO::makeRequest on the signed request
*
* @param W3TCG_Google_Http_Request $request
* @return W3TCG_Google_Http_Request The resulting HTTP response including the
* responseHttpCode, responseHeaders and responseBody.
*/
public function authenticatedRequest(W3TCG_Google_Http_Request $request)
{
$request = $this->sign($request);
return $this->io->makeRequest($request);
}
public function sign(W3TCG_Google_Http_Request $request)
{
$key = $this->client->getClassConfig($this, 'developer_key');
if ($key) {
$request->setQueryParam('key', $key);
}
return $request;
}
}

View File

@@ -0,0 +1,53 @@
<?php
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Abstract storage class
*
* @author Chris Chabot <chabotc@google.com>
*/
abstract class W3TCG_Google_Cache_Abstract
{
abstract public function __construct(W3TCG_Google_Client $client);
/**
* Retrieves the data for the given key, or false if they
* key is unknown or expired
*
* @param String $key The key who's data to retrieve
* @param boolean|int $expiration Expiration time in seconds
*
*/
abstract public function get($key, $expiration = false);
/**
* Store the key => $value set. The $value is serialized
* by this function so can be of any type
*
* @param string $key Key of the data
* @param string $value data
*/
abstract public function set($key, $value);
/**
* Removes the key/data pair for the given $key
*
* @param String $key
*/
abstract public function delete($key);
}

View File

@@ -0,0 +1,70 @@
<?php
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A persistent storage class based on the APC cache, which is not
* really very persistent, as soon as you restart your web server
* the storage will be wiped, however for debugging and/or speed
* it can be useful, and cache is a lot cheaper then storage.
*
* @author Chris Chabot <chabotc@google.com>
*/
class W3TCG_Google_Cache_Apc extends W3TCG_Google_Cache_Abstract
{
public function __construct(W3TCG_Google_Client $client)
{
if (! function_exists('apc_add') ) {
throw new W3TCG_Google_Cache_Exception("Apc functions not available");
}
}
/**
* @inheritDoc
*/
public function get($key, $expiration = false)
{
$ret = apc_fetch($key);
if ($ret === false) {
return false;
}
if (is_numeric($expiration) && (time() - $ret['time'] > $expiration)) {
$this->delete($key);
return false;
}
return $ret['data'];
}
/**
* @inheritDoc
*/
public function set($key, $value)
{
$rc = apc_store($key, array('time' => time(), 'data' => $value));
if ($rc == false) {
throw new W3TCG_Google_Cache_Exception("Couldn't store data");
}
}
/**
* @inheritDoc
* @param String $key
*/
public function delete($key)
{
apc_delete($key);
}
}

View File

@@ -0,0 +1,20 @@
<?php
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class W3TCG_Google_Cache_Exception extends W3TCG_Google_Exception
{
}

View File

@@ -0,0 +1,142 @@
<?php
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* This class implements a basic on disk storage. While that does
* work quite well it's not the most elegant and scalable solution.
* It will also get you into a heap of trouble when you try to run
* this in a clustered environment.
*
* @author Chris Chabot <chabotc@google.com>
*/
class W3TCG_Google_Cache_File extends W3TCG_Google_Cache_Abstract
{
const MAX_LOCK_RETRIES = 10;
private $path;
private $fh;
public function __construct(W3TCG_Google_Client $client)
{
$this->path = $client->getClassConfig($this, 'directory');
}
public function get($key, $expiration = false)
{
$storageFile = $this->getCacheFile($key);
$data = false;
if (!file_exists($storageFile)) {
return false;
}
if ($expiration) {
$mtime = filemtime($storageFile);
if ((time() - $mtime) >= $expiration) {
$this->delete($key);
return false;
}
}
if ($this->acquireReadLock($storageFile)) {
$data = fread($this->fh, filesize($storageFile));
$data = unserialize($data);
$this->unlock($storageFile);
}
return $data;
}
public function set($key, $value)
{
$storageFile = $this->getWriteableCacheFile($key);
if ($this->acquireWriteLock($storageFile)) {
// We serialize the whole request object, since we don't only want the
// responseContent but also the postBody used, headers, size, etc.
$data = serialize($value);
$result = fwrite($this->fh, $data);
$this->unlock($storageFile);
}
}
public function delete($key)
{
$file = $this->getCacheFile($key);
if (file_exists($file) && !unlink($file)) {
throw new W3TCG_Google_Cache_Exception("Cache file could not be deleted");
}
}
private function getWriteableCacheFile($file)
{
return $this->getCacheFile($file, true);
}
private function getCacheFile($file, $forWrite = false)
{
return $this->getCacheDir($file, $forWrite) . '/' . md5($file);
}
private function getCacheDir($file, $forWrite)
{
// use the first 2 characters of the hash as a directory prefix
// this should prevent slowdowns due to huge directory listings
// and thus give some basic amount of scalability
$storageDir = $this->path . '/' . substr(md5($file), 0, 2);
if ($forWrite && ! is_dir($storageDir)) {
if (! mkdir($storageDir, 0755, true)) {
throw new W3TCG_Google_Cache_Exception("Could not create storage directory: $storageDir");
}
}
return $storageDir;
}
private function acquireReadLock($storageFile)
{
return $this->acquireLock(LOCK_SH, $storageFile);
}
private function acquireWriteLock($storageFile)
{
$rc = $this->acquireLock(LOCK_EX, $storageFile);
if (!$rc) {
$this->delete($storageFile);
}
return $rc;
}
private function acquireLock($type, $storageFile)
{
$mode = $type == LOCK_EX ? "w" : "r";
$this->fh = fopen($storageFile, $mode);
$count = 0;
while (!flock($this->fh, $type | LOCK_NB)) {
// Sleep for 10ms.
usleep(10000);
if (++$count < self::MAX_LOCK_RETRIES) {
return false;
}
}
return true;
}
public function unlock($storageFile)
{
if ($this->fh) {
flock($this->fh, LOCK_UN);
}
}
}

View File

@@ -0,0 +1,134 @@
<?php
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A persistent storage class based on the memcache, which is not
* really very persistent, as soon as you restart your memcache daemon
* the storage will be wiped.
*
* Will use either the memcache or memcached extensions, preferring
* memcached.
*
* @author Chris Chabot <chabotc@google.com>
*/
class W3TCG_Google_Cache_Memcache extends W3TCG_Google_Cache_Abstract
{
private $connection = false;
private $mc = false;
private $host;
private $port;
public function __construct(W3TCG_Google_Client $client)
{
if (!function_exists('memcache_connect') && !class_exists("Memcached")) {
throw new W3TCG_Google_Cache_Exception("Memcache functions not available");
}
if ($client->isAppEngine()) {
// No credentials needed for GAE.
$this->mc = new Memcached();
$this->connection = true;
} else {
$this->host = $client->getClassConfig($this, 'host');
$this->port = $client->getClassConfig($this, 'port');
if (empty($this->host) || (empty($this->port) && (string) $this->port != "0")) {
throw new W3TCG_Google_Cache_Exception("You need to supply a valid memcache host and port");
}
}
}
/**
* @inheritDoc
*/
public function get($key, $expiration = false)
{
$this->connect();
$ret = false;
if ($this->mc) {
$ret = $this->mc->get($key);
} else {
$ret = memcache_get($this->connection, $key);
}
if ($ret === false) {
return false;
}
if (is_numeric($expiration) && (time() - $ret['time'] > $expiration)) {
$this->delete($key);
return false;
}
return $ret['data'];
}
/**
* @inheritDoc
* @param string $key
* @param string $value
* @throws W3TCG_Google_Cache_Exception
*/
public function set($key, $value)
{
$this->connect();
// we store it with the cache_time default expiration so objects will at
// least get cleaned eventually.
$data = array('time' => time(), 'data' => $value);
$rc = false;
if ($this->mc) {
$rc = $this->mc->set($key, $data);
} else {
$rc = memcache_set($this->connection, $key, $data, false);
}
if ($rc == false) {
throw new W3TCG_Google_Cache_Exception("Couldn't store data in cache");
}
}
/**
* @inheritDoc
* @param String $key
*/
public function delete($key)
{
$this->connect();
if ($this->mc) {
$this->mc->delete($key, 0);
} else {
memcache_delete($this->connection, $key, 0);
}
}
/**
* Lazy initialiser for memcache connection. Uses pconnect for to take
* advantage of the persistence pool where possible.
*/
private function connect()
{
if ($this->connection) {
return;
}
if (class_exists("Memcached")) {
$this->mc = new Memcached();
$this->mc->addServer($this->host, $this->port);
$this->connection = true;
} else {
$this->connection = memcache_pconnect($this->host, $this->port);
}
if (! $this->connection) {
throw new W3TCG_Google_Cache_Exception("Couldn't connect to memcache server");
}
}
}

View File

@@ -0,0 +1,53 @@
<?php
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A blank storage class, for cases where caching is not
* required.
*/
class W3TCG_Google_Cache_Null extends W3TCG_Google_Cache_Abstract
{
public function __construct(W3TCG_Google_Client $client)
{
}
/**
* @inheritDoc
*/
public function get($key, $expiration = false)
{
return false;
}
/**
* @inheritDoc
*/
public function set($key, $value)
{
// Nop.
}
/**
* @inheritDoc
* @param String $key
*/
public function delete($key)
{
// Nop.
}
}

View File

@@ -0,0 +1,656 @@
<?php
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* The Google API Client
* http://code.google.com/p/google-api-php-client/
*
* @author Chris Chabot <chabotc@google.com>
* @author Chirag Shah <chirags@google.com>
*/
class W3TCG_Google_Client
{
const LIBVER = "1.1.0-beta";
const USER_AGENT_SUFFIX = "google-api-php-client/";
/**
* @var W3TCG_Google_Auth_Abstract $auth
*/
private $auth;
/**
* @var W3TCG_Google_IO_Abstract $io
*/
private $io;
/**
* @var W3TCG_Google_Cache_Abstract $cache
*/
private $cache;
/**
* @var W3TCG_Google_Config $config
*/
private $config;
/**
* @var boolean $deferExecution
*/
private $deferExecution = false;
/** @var array $scopes */
// Scopes requested by the client
protected $requestedScopes = array();
// definitions of services that are discovered.
protected $services = array();
// Used to track authenticated state, can't discover services after doing authenticate()
private $authenticated = false;
/**
* Construct the Google Client.
*
* @param $config W3TCG_Google_Config or string for the ini file to load
*/
public function __construct($config = null)
{
if (is_string($config) && strlen($config)) {
$config = new W3TCG_Google_Config($config);
} else if ( !($config instanceof W3TCG_Google_Config)) {
$config = new W3TCG_Google_Config();
if ($this->isAppEngine()) {
// Automatically use Memcache if we're in AppEngine.
$config->setCacheClass('W3TCG_Google_Cache_Memcache');
}
if (version_compare(phpversion(), "5.3.4", "<=") || $this->isAppEngine()) {
// Automatically disable compress.zlib, as currently unsupported.
$config->setClassConfig('W3TCG_Google_Http_Request', 'disable_gzip', true);
}
}
if ($config->getIoClass() == W3TCG_Google_Config::USE_AUTO_IO_SELECTION) {
if (function_exists('curl_version') && function_exists('curl_exec')) {
$config->setIoClass("W3TCG_Google_IO_Curl");
} else {
$config->setIoClass("W3TCG_Google_IO_Stream");
}
}
$this->config = $config;
}
/**
* Get a string containing the version of the library.
*
* @return string
*/
public function getLibraryVersion()
{
return self::LIBVER;
}
/**
* Attempt to exchange a code for an valid authentication token.
* Helper wrapped around the OAuth 2.0 implementation.
*
* @param $code string code from accounts.google.com
* @return string token
*/
public function authenticate($code)
{
$this->authenticated = true;
return $this->getAuth()->authenticate($code);
}
/**
* Set the auth config from the JSON string provided.
* This structure should match the file downloaded from
* the "Download JSON" button on in the Google Developer
* Console.
* @param string $json the configuration json
* @throws W3TCG_Google_Exception
*/
public function setAuthConfig($json)
{
$data = json_decode($json);
$key = isset($data->installed) ? 'installed' : 'web';
if (!isset($data->$key)) {
throw new W3TCG_Google_Exception("Invalid client secret JSON file.");
}
$this->setClientId($data->$key->client_id);
$this->setClientSecret($data->$key->client_secret);
if (isset($data->$key->redirect_uris)) {
$this->setRedirectUri($data->$key->redirect_uris[0]);
}
}
/**
* Set the auth config from the JSON file in the path
* provided. This should match the file downloaded from
* the "Download JSON" button on in the Google Developer
* Console.
* @param string $file the file location of the client json
*/
public function setAuthConfigFile($file)
{
$this->setAuthConfig(file_get_contents($file));
}
/**
* @throws W3TCG_Google_Auth_Exception
* @return array
* @visible For Testing
*/
public function prepareScopes()
{
if (empty($this->requestedScopes)) {
throw new W3TCG_Google_Auth_Exception("No scopes specified");
}
$scopes = implode(' ', $this->requestedScopes);
return $scopes;
}
/**
* Set the OAuth 2.0 access token using the string that resulted from calling createAuthUrl()
* or W3TCG_Google_Client#getAccessToken().
* @param string $accessToken JSON encoded string containing in the following format:
* {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",
* "expires_in":3600, "id_token":"TOKEN", "created":1320790426}
*/
public function setAccessToken($accessToken)
{
if ($accessToken == 'null') {
$accessToken = null;
}
$this->getAuth()->setAccessToken($accessToken);
}
/**
* Set the authenticator object
* @param W3TCG_Google_Auth_Abstract $auth
*/
public function setAuth(W3TCG_Google_Auth_Abstract $auth)
{
$this->config->setAuthClass(get_class($auth));
$this->auth = $auth;
}
/**
* Set the IO object
* @param W3TCG_Google_IO_Abstract $io
*/
public function setIo(W3TCG_Google_IO_Abstract $io)
{
$this->config->setIoClass(get_class($io));
$this->io = $io;
}
/**
* Set the Cache object
* @param W3TCG_Google_Cache_Abstract $cache
*/
public function setCache(W3TCG_Google_Cache_Abstract $cache)
{
$this->config->setCacheClass(get_class($cache));
$this->cache = $cache;
}
/**
* Construct the OAuth 2.0 authorization request URI.
* @return string
*/
public function createAuthUrl()
{
$scopes = $this->prepareScopes();
return $this->getAuth()->createAuthUrl($scopes);
}
/**
* Get the OAuth 2.0 access token.
* @return string $accessToken JSON encoded string in the following format:
* {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",
* "expires_in":3600,"id_token":"TOKEN", "created":1320790426}
*/
public function getAccessToken()
{
$token = $this->getAuth()->getAccessToken();
// The response is json encoded, so could be the string null.
// It is arguable whether this check should be here or lower
// in the library.
return (null == $token || 'null' == $token || '[]' == $token) ? null : $token;
}
/**
* Get the OAuth 2.0 refresh token.
* @return string $refreshToken refresh token or null if not available
*/
public function getRefreshToken()
{
return $this->getAuth()->getRefreshToken();
}
/**
* Returns if the access_token is expired.
* @return bool Returns True if the access_token is expired.
*/
public function isAccessTokenExpired()
{
return $this->getAuth()->isAccessTokenExpired();
}
/**
* Set OAuth 2.0 "state" parameter to achieve per-request customization.
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-3.1.2.2
* @param string $state
*/
public function setState($state)
{
$this->getAuth()->setState($state);
}
/**
* @param string $accessType Possible values for access_type include:
* {@code "offline"} to request offline access from the user.
* {@code "online"} to request online access from the user.
*/
public function setAccessType($accessType)
{
$this->config->setAccessType($accessType);
}
/**
* @param string $approvalPrompt Possible values for approval_prompt include:
* {@code "force"} to force the approval UI to appear. (This is the default value)
* {@code "auto"} to request auto-approval when possible.
*/
public function setApprovalPrompt($approvalPrompt)
{
$this->config->setApprovalPrompt($approvalPrompt);
}
/**
* Set the login hint, email address or sub id.
* @param string $loginHint
*/
public function setLoginHint($loginHint)
{
$this->config->setLoginHint($loginHint);
}
/**
* Set the application name, this is included in the User-Agent HTTP header.
* @param string $applicationName
*/
public function setApplicationName($applicationName)
{
$this->config->setApplicationName($applicationName);
}
/**
* Set the OAuth 2.0 Client ID.
* @param string $clientId
*/
public function setClientId($clientId)
{
$this->config->setClientId($clientId);
}
/**
* Set the OAuth 2.0 Client Secret.
* @param string $clientSecret
*/
public function setClientSecret($clientSecret)
{
$this->config->setClientSecret($clientSecret);
}
/**
* Set the OAuth 2.0 Redirect URI.
* @param string $redirectUri
*/
public function setRedirectUri($redirectUri)
{
$this->config->setRedirectUri($redirectUri);
}
/**
* If 'plus.login' is included in the list of requested scopes, you can use
* this method to define types of app activities that your app will write.
* You can find a list of available types here:
* @link https://developers.google.com/+/api/moment-types
*
* @param array $requestVisibleActions Array of app activity types
*/
public function setRequestVisibleActions($requestVisibleActions)
{
if (is_array($requestVisibleActions)) {
$requestVisibleActions = join(" ", $requestVisibleActions);
}
$this->config->setRequestVisibleActions($requestVisibleActions);
}
/**
* Set the developer key to use, these are obtained through the API Console.
* @see http://code.google.com/apis/console-help/#generatingdevkeys
* @param string $developerKey
*/
public function setDeveloperKey($developerKey)
{
$this->config->setDeveloperKey($developerKey);
}
/**
* Set the hd (hosted domain) parameter streamlines the login process for
* Google Apps hosted accounts. By including the domain of the user, you
* restrict sign-in to accounts at that domain.
* @param $hd string - the domain to use.
*/
public function setHostedDomain($hd)
{
$this->config->setHostedDomain($hd);
}
/**
* Set the prompt hint. Valid values are none, consent and select_account.
* If no value is specified and the user has not previously authorized
* access, then the user is shown a consent screen.
* @param $prompt string
*/
public function setPrompt($prompt)
{
$this->config->setPrompt($prompt);
}
/**
* openid.realm is a parameter from the OpenID 2.0 protocol, not from OAuth
* 2.0. It is used in OpenID 2.0 requests to signify the URL-space for which
* an authentication request is valid.
* @param $realm string - the URL-space to use.
*/
public function setOpenidRealm($realm)
{
$this->config->setOpenidRealm($realm);
}
/**
* If this is provided with the value true, and the authorization request is
* granted, the authorization will include any previous authorizations
* granted to this user/application combination for other scopes.
* @param $include boolean - the URL-space to use.
*/
public function setIncludeGrantedScopes($include)
{
$this->config->setIncludeGrantedScopes($include);
}
/**
* Fetches a fresh OAuth 2.0 access token with the given refresh token.
* @param string $refreshToken
*/
public function refreshToken($refreshToken)
{
$this->getAuth()->refreshToken($refreshToken);
}
/**
* Revoke an OAuth2 access token or refresh token. This method will revoke the current access
* token, if a token isn't provided.
* @throws W3TCG_Google_Auth_Exception
* @param string|null $token The token (access token or a refresh token) that should be revoked.
* @return boolean Returns True if the revocation was successful, otherwise False.
*/
public function revokeToken($token = null)
{
return $this->getAuth()->revokeToken($token);
}
/**
* Verify an id_token. This method will verify the current id_token, if one
* isn't provided.
* @throws W3TCG_Google_Auth_Exception
* @param string|null $token The token (id_token) that should be verified.
* @return W3TCG_Google_Auth_LoginTicket Returns an apiLoginTicket if the verification was
* successful.
*/
public function verifyIdToken($token = null)
{
return $this->getAuth()->verifyIdToken($token);
}
/**
* Verify a JWT that was signed with your own certificates.
*
* @param $id_token string The JWT token
* @param $cert_location array of certificates
* @param $audience string the expected consumer of the token
* @param $issuer string the expected issuer, defaults to Google
* @param [$max_expiry] the max lifetime of a token, defaults to MAX_TOKEN_LIFETIME_SECS
* @return mixed token information if valid, false if not
*/
public function verifySignedJwt($id_token, $cert_location, $audience, $issuer, $max_expiry = null)
{
$auth = new W3TCG_Google_Auth_OAuth2($this);
$certs = $auth->retrieveCertsFromLocation($cert_location);
return $auth->verifySignedJwtWithCerts($id_token, $certs, $audience, $issuer, $max_expiry);
}
/**
* @param $creds W3TCG_Google_Auth_AssertionCredentials
*/
public function setAssertionCredentials(W3TCG_Google_Auth_AssertionCredentials $creds)
{
$this->getAuth()->setAssertionCredentials($creds);
}
/**
* Set the scopes to be requested. Must be called before createAuthUrl().
* Will remove any previously configured scopes.
* @param array $scopes, ie: array('https://www.googleapis.com/auth/plus.login',
* 'https://www.googleapis.com/auth/moderator')
*/
public function setScopes($scopes)
{
$this->requestedScopes = array();
$this->addScope($scopes);
}
/**
* This functions adds a scope to be requested as part of the OAuth2.0 flow.
* Will append any scopes not previously requested to the scope parameter.
* A single string will be treated as a scope to request. An array of strings
* will each be appended.
* @param $scope_or_scopes string|array e.g. "profile"
*/
public function addScope($scope_or_scopes)
{
if (is_string($scope_or_scopes) && !in_array($scope_or_scopes, $this->requestedScopes)) {
$this->requestedScopes[] = $scope_or_scopes;
} else if (is_array($scope_or_scopes)) {
foreach ($scope_or_scopes as $scope) {
$this->addScope($scope);
}
}
}
/**
* Returns the list of scopes requested by the client
* @return array the list of scopes
*
*/
public function getScopes()
{
return $this->requestedScopes;
}
/**
* Declare whether batch calls should be used. This may increase throughput
* by making multiple requests in one connection.
*
* @param boolean $useBatch True if the batch support should
* be enabled. Defaults to False.
*/
public function setUseBatch($useBatch)
{
// This is actually an alias for setDefer.
$this->setDefer($useBatch);
}
/**
* Declare whether making API calls should make the call immediately, or
* return a request which can be called with ->execute();
*
* @param boolean $defer True if calls should not be executed right away.
*/
public function setDefer($defer)
{
$this->deferExecution = $defer;
}
/**
* Helper method to execute deferred HTTP requests.
*
* @param $request W3TCG_Google_Http_Request|Google_Http_Batch
* @throws W3TCG_Google_Exception
* @return object of the type of the expected class or array.
*/
public function execute($request)
{
if ($request instanceof W3TCG_Google_Http_Request) {
$request->setUserAgent(
$this->getApplicationName()
. " " . self::USER_AGENT_SUFFIX
. $this->getLibraryVersion()
);
if (!$this->getClassConfig("W3TCG_Google_Http_Request", "disable_gzip")) {
$request->enableGzip();
}
$request->maybeMoveParametersToBody();
return W3TCG_Google_Http_REST::execute($this, $request);
} else if ($request instanceof W3TCG_Google_Http_Batch) {
return $request->execute();
} else {
throw new W3TCG_Google_Exception("Do not know how to execute this type of object.");
}
}
/**
* Whether or not to return raw requests
* @return boolean
*/
public function shouldDefer()
{
return $this->deferExecution;
}
/**
* @return W3TCG_Google_Auth_Abstract Authentication implementation
*/
public function getAuth()
{
if (!isset($this->auth)) {
$class = $this->config->getAuthClass();
$this->auth = new $class($this);
}
return $this->auth;
}
/**
* @return W3TCG_Google_IO_Abstract IO implementation
*/
public function getIo()
{
if (!isset($this->io)) {
$class = $this->config->getIoClass();
$this->io = new $class($this);
}
return $this->io;
}
/**
* @return W3TCG_Google_Cache_Abstract Cache implementation
*/
public function getCache()
{
if (!isset($this->cache)) {
$class = $this->config->getCacheClass();
$this->cache = new $class($this);
}
return $this->cache;
}
/**
* Retrieve custom configuration for a specific class.
* @param $class string|object - class or instance of class to retrieve
* @param $key string optional - key to retrieve
* @return array
*/
public function getClassConfig($class, $key = null)
{
if (!is_string($class)) {
$class = get_class($class);
}
return $this->config->getClassConfig($class, $key);
}
/**
* Set configuration specific to a given class.
* $config->setClassConfig('W3TCG_Google_Cache_File',
* array('directory' => '/tmp/cache'));
* @param $class string|object - The class name for the configuration
* @param $config string key or an array of configuration values
* @param $value string optional - if $config is a key, the value
*
*/
public function setClassConfig($class, $config, $value = null)
{
if (!is_string($class)) {
$class = get_class($class);
}
$this->config->setClassConfig($class, $config, $value);
}
/**
* @return string the base URL to use for calls to the APIs
*/
public function getBasePath()
{
return $this->config->getBasePath();
}
/**
* @return string the name of the application
*/
public function getApplicationName()
{
return $this->config->getApplicationName();
}
/**
* Are we running in Google AppEngine?
* return bool
*/
public function isAppEngine()
{
return (isset($_SERVER['SERVER_SOFTWARE']) &&
strpos( sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ), 'Google App Engine') !== false);
}
}

View File

@@ -0,0 +1,94 @@
<?php
/**
* Extension to the regular W3TCG_Google_Model that automatically
* exposes the items array for iteration, so you can just
* iterate over the object rather than a reference inside.
*/
class W3TCG_Google_Collection extends W3TCG_Google_Model implements Iterator, Countable
{
protected $collection_key = 'items';
public function rewind()
{
if (isset($this->modelData[$this->collection_key])
&& is_array($this->modelData[$this->collection_key])) {
reset($this->modelData[$this->collection_key]);
}
}
public function current()
{
$this->coerceType($this->key());
if (is_array($this->modelData[$this->collection_key])) {
return current($this->modelData[$this->collection_key]);
}
}
public function key()
{
if (isset($this->modelData[$this->collection_key])
&& is_array($this->modelData[$this->collection_key])) {
return key($this->modelData[$this->collection_key]);
}
}
public function next()
{
return next($this->modelData[$this->collection_key]);
}
public function valid()
{
$key = $this->key();
return $key !== null && $key !== false;
}
public function count()
{
return count($this->modelData[$this->collection_key]);
}
public function offsetExists ($offset)
{
if (!is_numeric($offset)) {
return parent::offsetExists($offset);
}
return isset($this->modelData[$this->collection_key][$offset]);
}
public function offsetGet($offset)
{
if (!is_numeric($offset)) {
return parent::offsetGet($offset);
}
$this->coerceType($offset);
return $this->modelData[$this->collection_key][$offset];
}
public function offsetSet($offset, $value)
{
if (!is_numeric($offset)) {
return parent::offsetSet($offset, $value);
}
$this->modelData[$this->collection_key][$offset] = $value;
}
public function offsetUnset($offset)
{
if (!is_numeric($offset)) {
return parent::offsetUnset($offset);
}
unset($this->modelData[$this->collection_key][$offset]);
}
private function coerceType($offset)
{
$typeKey = $this->keyType($this->collection_key);
if (isset($this->$typeKey) && !is_object($this->modelData[$this->collection_key][$offset])) {
$type = $this->$typeKey;
$this->modelData[$this->collection_key][$offset] =
new $type($this->modelData[$this->collection_key][$offset]);
}
}
}

View File

@@ -0,0 +1,373 @@
<?php
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A class to contain the library configuration for the Google API client.
*/
class W3TCG_Google_Config
{
const GZIP_DISABLED = true;
const GZIP_ENABLED = false;
const GZIP_UPLOADS_ENABLED = true;
const GZIP_UPLOADS_DISABLED = false;
const USE_AUTO_IO_SELECTION = "auto";
protected $configuration;
/**
* Create a new W3TCG_Google_Config. Can accept an ini file location with the
* local configuration. For example:
* application_name="My App"
*
* @param [$ini_file_location] - optional - The location of the ini file to load
*/
public function __construct($ini_file_location = null)
{
$this->configuration = array(
// The application_name is included in the User-Agent HTTP header.
'application_name' => '',
// Which Authentication, Storage and HTTP IO classes to use.
'auth_class' => 'W3TCG_Google_Auth_OAuth2',
'io_class' => self::USE_AUTO_IO_SELECTION,
'cache_class' => 'W3TCG_Google_Cache_File',
// Don't change these unless you're working against a special development
// or testing environment.
'base_path' => 'https://www.googleapis.com',
// Definition of class specific values, like file paths and so on.
'classes' => array(
'W3TCG_Google_IO_Abstract' => array(
'request_timeout_seconds' => 100,
),
'W3TCG_Google_Http_Request' => array(
// Disable the use of gzip on calls if set to true. Defaults to false.
'disable_gzip' => self::GZIP_ENABLED,
// We default gzip to disabled on uploads even if gzip is otherwise
// enabled, due to some issues seen with small packet sizes for uploads.
// Please test with this option before enabling gzip for uploads in
// a production environment.
'enable_gzip_for_uploads' => self::GZIP_UPLOADS_DISABLED,
),
// If you want to pass in OAuth 2.0 settings, they will need to be
// structured like this.
'W3TCG_Google_Auth_OAuth2' => array(
// Keys for OAuth 2.0 access, see the API console at
// https://developers.google.com/console
'client_id' => '',
'client_secret' => '',
'redirect_uri' => '',
// Simple API access key, also from the API console. Ensure you get
// a Server key, and not a Browser key.
'developer_key' => '',
// Other parameters.
'hd' => '',
'prompt' => '',
'openid.realm' => '',
'include_granted_scopes' => '',
'login_hint' => '',
'request_visible_actions' => '',
'access_type' => 'online',
'approval_prompt' => 'auto',
'federated_signon_certs_url' =>
'https://www.googleapis.com/oauth2/v1/certs',
),
// Set a default directory for the file cache.
'W3TCG_Google_Cache_File' => array(
'directory' => sys_get_temp_dir() . '/W3TCG_Google_Client'
)
),
);
if ($ini_file_location) {
$ini = parse_ini_file($ini_file_location, true);
if (is_array($ini) && count($ini)) {
$this->configuration = array_merge($this->configuration, $ini);
}
}
}
/**
* Set configuration specific to a given class.
* $config->setClassConfig('W3TCG_Google_Cache_File',
* array('directory' => '/tmp/cache'));
* @param $class string The class name for the configuration
* @param $config string key or an array of configuration values
* @param $value string optional - if $config is a key, the value
*/
public function setClassConfig($class, $config, $value = null)
{
if (!is_array($config)) {
if (!isset($this->configuration['classes'][$class])) {
$this->configuration['classes'][$class] = array();
}
$this->configuration['classes'][$class][$config] = $value;
} else {
$this->configuration['classes'][$class] = $config;
}
}
public function getClassConfig($class, $key = null)
{
if (!isset($this->configuration['classes'][$class])) {
return null;
}
if ($key === null) {
return $this->configuration['classes'][$class];
} else {
return $this->configuration['classes'][$class][$key];
}
}
/**
* Return the configured cache class.
* @return string
*/
public function getCacheClass()
{
return $this->configuration['cache_class'];
}
/**
* Return the configured Auth class.
* @return string
*/
public function getAuthClass()
{
return $this->configuration['auth_class'];
}
/**
* Set the auth class.
*
* @param $class string the class name to set
*/
public function setAuthClass($class)
{
$prev = $this->configuration['auth_class'];
if (!isset($this->configuration['classes'][$class]) &&
isset($this->configuration['classes'][$prev])) {
$this->configuration['classes'][$class] =
$this->configuration['classes'][$prev];
}
$this->configuration['auth_class'] = $class;
}
/**
* Set the IO class.
*
* @param $class string the class name to set
*/
public function setIoClass($class)
{
$prev = $this->configuration['io_class'];
if (!isset($this->configuration['classes'][$class]) &&
isset($this->configuration['classes'][$prev])) {
$this->configuration['classes'][$class] =
$this->configuration['classes'][$prev];
}
$this->configuration['io_class'] = $class;
}
/**
* Set the cache class.
*
* @param $class string the class name to set
*/
public function setCacheClass($class)
{
$prev = $this->configuration['cache_class'];
if (!isset($this->configuration['classes'][$class]) &&
isset($this->configuration['classes'][$prev])) {
$this->configuration['classes'][$class] =
$this->configuration['classes'][$prev];
}
$this->configuration['cache_class'] = $class;
}
/**
* Return the configured IO class.
*
* @return string
*/
public function getIoClass()
{
return $this->configuration['io_class'];
}
/**
* Set the application name, this is included in the User-Agent HTTP header.
* @param string $name
*/
public function setApplicationName($name)
{
$this->configuration['application_name'] = $name;
}
/**
* @return string the name of the application
*/
public function getApplicationName()
{
return $this->configuration['application_name'];
}
/**
* Set the client ID for the auth class.
* @param $clientId string - the API console client ID
*/
public function setClientId($clientId)
{
$this->setAuthConfig('client_id', $clientId);
}
/**
* Set the client secret for the auth class.
* @param $secret string - the API console client secret
*/
public function setClientSecret($secret)
{
$this->setAuthConfig('client_secret', $secret);
}
/**
* Set the redirect uri for the auth class. Note that if using the
* Javascript based sign in flow, this should be the string 'postmessage'.
*
* @param $uri string - the URI that users should be redirected to
*/
public function setRedirectUri($uri)
{
$this->setAuthConfig('redirect_uri', $uri);
}
/**
* Set the app activities for the auth class.
* @param $rva string a space separated list of app activity types
*/
public function setRequestVisibleActions($rva)
{
$this->setAuthConfig('request_visible_actions', $rva);
}
/**
* Set the the access type requested (offline or online.)
* @param $access string - the access type
*/
public function setAccessType($access)
{
$this->setAuthConfig('access_type', $access);
}
/**
* Set when to show the approval prompt (auto or force)
* @param $approval string - the approval request
*/
public function setApprovalPrompt($approval)
{
$this->setAuthConfig('approval_prompt', $approval);
}
/**
* Set the login hint (email address or sub identifier)
* @param $hint string
*/
public function setLoginHint($hint)
{
$this->setAuthConfig('login_hint', $hint);
}
/**
* Set the developer key for the auth class. Note that this is separate value
* from the client ID - if it looks like a URL, its a client ID!
* @param $key string - the API console developer key
*/
public function setDeveloperKey($key)
{
$this->setAuthConfig('developer_key', $key);
}
/**
* Set the hd (hosted domain) parameter streamlines the login process for
* Google Apps hosted accounts. By including the domain of the user, you
* restrict sign-in to accounts at that domain.
* @param $hd string - the domain to use.
*/
public function setHostedDomain($hd)
{
$this->setAuthConfig('hd', $hd);
}
/**
* Set the prompt hint. Valid values are none, consent and select_account.
* If no value is specified and the user has not previously authorized
* access, then the user is shown a consent screen.
* @param $prompt string
*/
public function setPrompt($prompt)
{
$this->setAuthConfig('prompt', $prompt);
}
/**
* openid.realm is a parameter from the OpenID 2.0 protocol, not from OAuth
* 2.0. It is used in OpenID 2.0 requests to signify the URL-space for which
* an authentication request is valid.
* @param $realm string - the URL-space to use.
*/
public function setOpenidRealm($realm)
{
$this->setAuthConfig('openid.realm', $realm);
}
/**
* If this is provided with the value true, and the authorization request is
* granted, the authorization will include any previous authorizations
* granted to this user/application combination for other scopes.
* @param $include boolean - the URL-space to use.
*/
public function setIncludeGrantedScopes($include)
{
$this->setAuthConfig(
'include_granted_scopes',
$include ? "true" : "false"
);
}
/**
* @return string the base URL to use for API calls
*/
public function getBasePath()
{
return $this->configuration['base_path'];
}
/**
* Set the auth configuration for the current auth class.
* @param $key - the key to set
* @param $value - the parameter value
*/
private function setAuthConfig($key, $value)
{
if (!isset($this->configuration['classes'][$this->getAuthClass()])) {
$this->configuration['classes'][$this->getAuthClass()] = array();
}
$this->configuration['classes'][$this->getAuthClass()][$key] = $value;
}
}

View File

@@ -0,0 +1,20 @@
<?php
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class W3TCG_Google_Exception extends Exception
{
}

View File

@@ -0,0 +1,139 @@
<?php
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Chirag Shah <chirags@google.com>
*/
class W3TCG_Google_Http_Batch
{
/** @var string Multipart Boundary. */
private $boundary;
/** @var array service requests to be executed. */
private $requests = array();
/** @var W3TCG_Google_Client */
private $client;
private $expected_classes = array();
private $base_path;
public function __construct(W3TCG_Google_Client $client, $boundary = false)
{
$this->client = $client;
$this->base_path = $this->client->getBasePath();
$this->expected_classes = array();
$boundary = (false == $boundary) ? mt_rand() : $boundary;
$this->boundary = str_replace('"', '', $boundary);
}
public function add(W3TCG_Google_Http_Request $request, $key = false)
{
if (false == $key) {
$key = mt_rand();
}
$this->requests[$key] = $request;
}
public function execute()
{
$body = '';
/** @var W3TCG_Google_Http_Request $req */
foreach ($this->requests as $key => $req) {
$body .= "--{$this->boundary}\n";
$body .= $req->toBatchString($key) . "\n";
$this->expected_classes["response-" . $key] = $req->getExpectedClass();
}
$body = rtrim($body);
$body .= "\n--{$this->boundary}--";
$url = $this->base_path . '/batch';
$httpRequest = new W3TCG_Google_Http_Request($url, 'POST');
$httpRequest->setRequestHeaders(
array('Content-Type' => 'multipart/mixed; boundary=' . $this->boundary)
);
$httpRequest->setPostBody($body);
$response = $this->client->getIo()->makeRequest($httpRequest);
return $this->parseResponse($response);
}
public function parseResponse(W3TCG_Google_Http_Request $response)
{
$contentType = $response->getResponseHeader('content-type');
$contentType = explode(';', $contentType);
$boundary = false;
foreach ($contentType as $part) {
$part = (explode('=', $part, 2));
if (isset($part[0]) && 'boundary' == trim($part[0])) {
$boundary = $part[1];
}
}
$body = $response->getResponseBody();
if ($body) {
$body = str_replace("--$boundary--", "--$boundary", $body);
$parts = explode("--$boundary", $body);
$responses = array();
foreach ($parts as $part) {
$part = trim($part);
if (!empty($part)) {
list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2);
$metaHeaders = $this->client->getIo()->getHttpResponseHeaders($metaHeaders);
$status = substr($part, 0, strpos($part, "\n"));
$status = explode(" ", $status);
$status = $status[1];
list($partHeaders, $partBody) = $this->client->getIo()->ParseHttpResponse($part, false);
$response = new W3TCG_Google_Http_Request("");
$response->setResponseHttpCode($status);
$response->setResponseHeaders($partHeaders);
$response->setResponseBody($partBody);
// Need content id.
$key = $metaHeaders['content-id'];
if (isset($this->expected_classes[$key]) &&
strlen($this->expected_classes[$key]) > 0) {
$class = $this->expected_classes[$key];
$response->setExpectedClass($class);
}
try {
$response = W3TCG_Google_Http_REST::decodeHttpResponse($response);
$responses[$key] = $response;
} catch (W3TCG_Google_Service_Exception $e) {
// Store the exception as the response, so succesful responses
// can be processed.
$responses[$key] = $e;
}
}
}
return $responses;
}
return null;
}
}

View File

@@ -0,0 +1,182 @@
<?php
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Implement the caching directives specified in rfc2616. This
* implementation is guided by the guidance offered in rfc2616-sec13.
* @author Chirag Shah <chirags@google.com>
*/
class W3TCG_Google_Http_CacheParser
{
public static $CACHEABLE_HTTP_METHODS = array('GET', 'HEAD');
public static $CACHEABLE_STATUS_CODES = array('200', '203', '300', '301');
/**
* Check if an HTTP request can be cached by a private local cache.
*
* @static
* @param W3TCG_Google_Http_Request $resp
* @return bool True if the request is cacheable.
* False if the request is uncacheable.
*/
public static function isRequestCacheable(W3TCG_Google_Http_Request $resp)
{
$method = $resp->getRequestMethod();
if (! in_array($method, self::$CACHEABLE_HTTP_METHODS)) {
return false;
}
// Don't cache authorized requests/responses.
// [rfc2616-14.8] When a shared cache receives a request containing an
// Authorization field, it MUST NOT return the corresponding response
// as a reply to any other request...
if ($resp->getRequestHeader("authorization")) {
return false;
}
return true;
}
/**
* Check if an HTTP response can be cached by a private local cache.
*
* @static
* @param W3TCG_Google_Http_Request $resp
* @return bool True if the response is cacheable.
* False if the response is un-cacheable.
*/
public static function isResponseCacheable(W3TCG_Google_Http_Request $resp)
{
// First, check if the HTTP request was cacheable before inspecting the
// HTTP response.
if (false == self::isRequestCacheable($resp)) {
return false;
}
$code = $resp->getResponseHttpCode();
if (! in_array($code, self::$CACHEABLE_STATUS_CODES)) {
return false;
}
// The resource is uncacheable if the resource is already expired and
// the resource doesn't have an ETag for revalidation.
$etag = $resp->getResponseHeader("etag");
if (self::isExpired($resp) && $etag == false) {
return false;
}
// [rfc2616-14.9.2] If [no-store is] sent in a response, a cache MUST NOT
// store any part of either this response or the request that elicited it.
$cacheControl = $resp->getParsedCacheControl();
if (isset($cacheControl['no-store'])) {
return false;
}
// Pragma: no-cache is an http request directive, but is occasionally
// used as a response header incorrectly.
$pragma = $resp->getResponseHeader('pragma');
if ($pragma == 'no-cache' || strpos($pragma, 'no-cache') !== false) {
return false;
}
// [rfc2616-14.44] Vary: * is extremely difficult to cache. "It implies that
// a cache cannot determine from the request headers of a subsequent request
// whether this response is the appropriate representation."
// Given this, we deem responses with the Vary header as uncacheable.
$vary = $resp->getResponseHeader('vary');
if ($vary) {
return false;
}
return true;
}
/**
* @static
* @param W3TCG_Google_Http_Request $resp
* @return bool True if the HTTP response is considered to be expired.
* False if it is considered to be fresh.
*/
public static function isExpired(W3TCG_Google_Http_Request $resp)
{
// HTTP/1.1 clients and caches MUST treat other invalid date formats,
// especially including the value “0”, as in the past.
$parsedExpires = false;
$responseHeaders = $resp->getResponseHeaders();
if (isset($responseHeaders['expires'])) {
$rawExpires = $responseHeaders['expires'];
// Check for a malformed expires header first.
if (empty($rawExpires) || (is_numeric($rawExpires) && $rawExpires <= 0)) {
return true;
}
// See if we can parse the expires header.
$parsedExpires = strtotime($rawExpires);
if (false == $parsedExpires || $parsedExpires <= 0) {
return true;
}
}
// Calculate the freshness of an http response.
$freshnessLifetime = false;
$cacheControl = $resp->getParsedCacheControl();
if (isset($cacheControl['max-age'])) {
$freshnessLifetime = $cacheControl['max-age'];
}
$rawDate = $resp->getResponseHeader('date');
$parsedDate = strtotime($rawDate);
if (empty($rawDate) || false == $parsedDate) {
// We can't default this to now, as that means future cache reads
// will always pass with the logic below, so we will require a
// date be injected if not supplied.
throw new W3TCG_Google_Exception("All cacheable requests must have creation dates.");
}
if (false == $freshnessLifetime && isset($responseHeaders['expires'])) {
$freshnessLifetime = $parsedExpires - $parsedDate;
}
if (false == $freshnessLifetime) {
return true;
}
// Calculate the age of an http response.
$age = max(0, time() - $parsedDate);
if (isset($responseHeaders['age'])) {
$age = max($age, strtotime($responseHeaders['age']));
}
return $freshnessLifetime <= $age;
}
/**
* Determine if a cache entry should be revalidated with by the origin.
*
* @param W3TCG_Google_Http_Request $response
* @return bool True if the entry is expired, else return false.
*/
public static function mustRevalidate(W3TCG_Google_Http_Request $response)
{
// [13.3] When a cache has a stale entry that it would like to use as a
// response to a client's request, it first has to check with the origin
// server to see if its cached entry is still usable.
return self::isExpired($response);
}
}

View File

@@ -0,0 +1,295 @@
<?php
/**
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Chirag Shah <chirags@google.com>
*
*/
class W3TCG_Google_Http_MediaFileUpload
{
const UPLOAD_MEDIA_TYPE = 'media';
const UPLOAD_MULTIPART_TYPE = 'multipart';
const UPLOAD_RESUMABLE_TYPE = 'resumable';
/** @var string $mimeType */
private $mimeType;
/** @var string $data */
private $data;
/** @var bool $resumable */
private $resumable;
/** @var int $chunkSize */
private $chunkSize;
/** @var int $size */
private $size;
/** @var string $resumeUri */
private $resumeUri;
/** @var int $progress */
private $progress;
/** @var W3TCG_Google_Client */
private $client;
/** @var W3TCG_Google_Http_Request */
private $request;
/** @var string */
private $boundary;
/**
* Result code from last HTTP call
* @var int
*/
private $httpResultCode;
/**
* @param $mimeType string
* @param $data string The bytes you want to upload.
* @param $resumable bool
* @param bool $chunkSize File will be uploaded in chunks of this many bytes.
* only used if resumable=True
*/
public function __construct(
W3TCG_Google_Client $client,
W3TCG_Google_Http_Request $request,
$mimeType,
$data,
$resumable = false,
$chunkSize = false,
$boundary = false
) {
$this->client = $client;
$this->request = $request;
$this->mimeType = $mimeType;
$this->data = $data;
$this->size = strlen($this->data);
$this->resumable = $resumable;
if (!$chunkSize) {
$chunkSize = 256 * 1024;
}
$this->chunkSize = $chunkSize;
$this->progress = 0;
$this->boundary = $boundary;
// Process Media Request
$this->process();
}
/**
* Set the size of the file that is being uploaded.
* @param $size - int file size in bytes
*/
public function setFileSize($size)
{
$this->size = $size;
}
/**
* Return the progress on the upload
* @return int progress in bytes uploaded.
*/
public function getProgress()
{
return $this->progress;
}
/**
* Return the HTTP result code from the last call made.
* @return int code
*/
public function getHttpResultCode()
{
return $this->httpResultCode;
}
/**
* Send the next part of the file to upload.
* @param [$chunk] the next set of bytes to send. If false will used $data passed
* at construct time.
*/
public function nextChunk($chunk = false)
{
if (false == $this->resumeUri) {
$this->resumeUri = $this->getResumeUri();
}
if (false == $chunk) {
$chunk = substr($this->data, $this->progress, $this->chunkSize);
}
$lastBytePos = $this->progress + strlen($chunk) - 1;
$headers = array(
'content-range' => "bytes $this->progress-$lastBytePos/$this->size",
'content-type' => $this->request->getRequestHeader('content-type'),
'content-length' => $this->chunkSize,
'expect' => '',
);
$httpRequest = new W3TCG_Google_Http_Request(
$this->resumeUri,
'PUT',
$headers,
$chunk
);
if ($this->client->getClassConfig("W3TCG_Google_Http_Request", "enable_gzip_for_uploads")) {
$httpRequest->enableGzip();
} else {
$httpRequest->disableGzip();
}
$response = $this->client->getIo()->makeRequest($httpRequest);
$response->setExpectedClass($this->request->getExpectedClass());
$code = $response->getResponseHttpCode();
$this->httpResultCode = $code;
if (308 == $code) {
// Track the amount uploaded.
$range = explode('-', $response->getResponseHeader('range'));
$this->progress = $range[1] + 1;
// Allow for changing upload URLs.
$location = $response->getResponseHeader('location');
if ($location) {
$this->resumeUri = $location;
}
// No problems, but upload not complete.
return false;
} else {
return W3TCG_Google_Http_REST::decodeHttpResponse($response);
}
}
/**
* @param $meta
* @param $params
* @return array|bool
* @visible for testing
*/
private function process()
{
$postBody = false;
$contentType = false;
$meta = $this->request->getPostBody();
$meta = is_string($meta) ? json_decode($meta, true) : $meta;
$uploadType = $this->getUploadType($meta);
$this->request->setQueryParam('uploadType', $uploadType);
$this->transformToUploadUrl();
$mimeType = $this->mimeType ?
$this->mimeType :
$this->request->getRequestHeader('content-type');
if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) {
$contentType = $mimeType;
$postBody = is_string($meta) ? $meta : json_encode($meta);
} else if (self::UPLOAD_MEDIA_TYPE == $uploadType) {
$contentType = $mimeType;
$postBody = $this->data;
} else if (self::UPLOAD_MULTIPART_TYPE == $uploadType) {
// This is a multipart/related upload.
$boundary = $this->boundary ? $this->boundary : mt_rand();
$boundary = str_replace('"', '', $boundary);
$contentType = 'multipart/related; boundary=' . $boundary;
$related = "--$boundary\r\n";
$related .= "Content-Type: application/json; charset=UTF-8\r\n";
$related .= "\r\n" . json_encode($meta) . "\r\n";
$related .= "--$boundary\r\n";
$related .= "Content-Type: $mimeType\r\n";
$related .= "Content-Transfer-Encoding: base64\r\n";
$related .= "\r\n" . base64_encode($this->data) . "\r\n";
$related .= "--$boundary--";
$postBody = $related;
}
$this->request->setPostBody($postBody);
if (isset($contentType) && $contentType) {
$contentTypeHeader['content-type'] = $contentType;
$this->request->setRequestHeaders($contentTypeHeader);
}
}
private function transformToUploadUrl()
{
$base = $this->request->getBaseComponent();
$this->request->setBaseComponent($base . '/upload');
}
/**
* Valid upload types:
* - resumable (UPLOAD_RESUMABLE_TYPE)
* - media (UPLOAD_MEDIA_TYPE)
* - multipart (UPLOAD_MULTIPART_TYPE)
* @param $meta
* @return string
* @visible for testing
*/
public function getUploadType($meta)
{
if ($this->resumable) {
return self::UPLOAD_RESUMABLE_TYPE;
}
if (false == $meta && $this->data) {
return self::UPLOAD_MEDIA_TYPE;
}
return self::UPLOAD_MULTIPART_TYPE;
}
private function getResumeUri()
{
$result = null;
$body = $this->request->getPostBody();
if ($body) {
$headers = array(
'content-type' => 'application/json; charset=UTF-8',
'content-length' => W3TCG_Google_Utils::getStrLen($body),
'x-upload-content-type' => $this->mimeType,
'x-upload-content-length' => $this->size,
'expect' => '',
);
$this->request->setRequestHeaders($headers);
}
$response = $this->client->getIo()->makeRequest($this->request);
$location = $response->getResponseHeader('location');
$code = $response->getResponseHttpCode();
if (200 == $code && true == $location) {
return $location;
}
$message = $code;
$body = @json_decode($response->getResponseBody());
if (!empty( $body->error->errors ) ) {
$message .= ': ';
foreach ($body->error->errors as $error) {
$message .= "{$error->domain}, {$error->message};";
}
$message = rtrim($message, ';');
}
throw new W3TCG_Google_Exception("Failed to start the resumable upload (HTTP {$message})");
}
}

View File

@@ -0,0 +1,134 @@
<?php
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This class implements the RESTful transport of apiServiceRequest()'s
*
* @author Chris Chabot <chabotc@google.com>
* @author Chirag Shah <chirags@google.com>
*/
class W3TCG_Google_Http_REST
{
/**
* Executes a W3TCG_Google_Http_Request
*
* @param W3TCG_Google_Client $client
* @param W3TCG_Google_Http_Request $req
* @return array decoded result
* @throws W3TCG_Google_Service_Exception on server side error (ie: not authenticated,
* invalid or malformed post body, invalid url)
*/
public static function execute(W3TCG_Google_Client $client, W3TCG_Google_Http_Request $req)
{
$httpRequest = $client->getIo()->makeRequest($req);
$httpRequest->setExpectedClass($req->getExpectedClass());
return self::decodeHttpResponse($httpRequest);
}
/**
* Decode an HTTP Response.
* @static
* @throws W3TCG_Google_Service_Exception
* @param W3TCG_Google_Http_Request $response The http response to be decoded.
* @return mixed|null
*/
public static function decodeHttpResponse($response)
{
$code = $response->getResponseHttpCode();
$body = $response->getResponseBody();
$decoded = null;
if ((intVal($code)) >= 300) {
$decoded = json_decode($body, true);
$err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl();
if (isset($decoded['error']) &&
isset($decoded['error']['message']) &&
isset($decoded['error']['code'])) {
// if we're getting a json encoded error definition, use that instead of the raw response
// body for improved readability
$err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}";
} else {
$err .= ": ($code) $body";
}
$errors = null;
// Specific check for APIs which don't return error details, such as Blogger.
if (isset($decoded['error']) && isset($decoded['error']['errors'])) {
$errors = $decoded['error']['errors'];
}
throw new W3TCG_Google_Service_Exception($err, $code, null, $errors);
}
// Only attempt to decode the response, if the response code wasn't (204) 'no content'
if ($code != '204') {
$decoded = json_decode($body, true);
if ($decoded === null || $decoded === "") {
throw new W3TCG_Google_Service_Exception("Invalid json in service response: $body");
}
if ($response->getExpectedClass()) {
$class = $response->getExpectedClass();
$decoded = new $class($decoded);
}
}
return $decoded;
}
/**
* Parse/expand request parameters and create a fully qualified
* request uri.
* @static
* @param string $servicePath
* @param string $restPath
* @param array $params
* @return string $requestUrl
*/
public static function createRequestUri($servicePath, $restPath, $params)
{
$requestUrl = $servicePath . $restPath;
$uriTemplateVars = array();
$queryVars = array();
foreach ($params as $paramName => $paramSpec) {
if ($paramSpec['type'] == 'boolean') {
$paramSpec['value'] = ($paramSpec['value']) ? 'true' : 'false';
}
if ($paramSpec['location'] == 'path') {
$uriTemplateVars[$paramName] = $paramSpec['value'];
} else if ($paramSpec['location'] == 'query') {
if (isset($paramSpec['repeated']) && is_array($paramSpec['value'])) {
foreach ($paramSpec['value'] as $value) {
$queryVars[] = $paramName . '=' . rawurlencode($value);
}
} else {
$queryVars[] = $paramName . '=' . rawurlencode($paramSpec['value']);
}
}
}
if (count($uriTemplateVars)) {
$uriTemplateParser = new W3TCG_Google_Utils_URITemplate();
$requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars);
}
if (count($queryVars)) {
$requestUrl .= '?' . implode($queryVars, '&');
}
return $requestUrl;
}
}

View File

@@ -0,0 +1,474 @@
<?php
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* HTTP Request to be executed by IO classes. Upon execution, the
* responseHttpCode, responseHeaders and responseBody will be filled in.
*
* @author Chris Chabot <chabotc@google.com>
* @author Chirag Shah <chirags@google.com>
*
*/
class W3TCG_Google_Http_Request
{
const GZIP_UA = " (gzip)";
private $batchHeaders = array(
'Content-Type' => 'application/http',
'Content-Transfer-Encoding' => 'binary',
'MIME-Version' => '1.0',
);
protected $queryParams;
protected $requestMethod;
protected $requestHeaders;
protected $baseComponent = null;
protected $path;
protected $postBody;
protected $userAgent;
protected $canGzip = null;
protected $responseHttpCode;
protected $responseHeaders;
protected $responseBody;
protected $expectedClass;
public $accessKey;
public function __construct(
$url,
$method = 'GET',
$headers = array(),
$postBody = null
) {
$this->setUrl($url);
$this->setRequestMethod($method);
$this->setRequestHeaders($headers);
$this->setPostBody($postBody);
}
/**
* Misc function that returns the base url component of the $url
* used by the OAuth signing class to calculate the base string
* @return string The base url component of the $url.
*/
public function getBaseComponent()
{
return $this->baseComponent;
}
/**
* Set the base URL that path and query parameters will be added to.
* @param $baseComponent string
*/
public function setBaseComponent($baseComponent)
{
$this->baseComponent = $baseComponent;
}
/**
* Enable support for gzipped responses with this request.
*/
public function enableGzip()
{
$this->setRequestHeaders(array("Accept-Encoding" => "gzip"));
$this->canGzip = true;
$this->setUserAgent($this->userAgent);
}
/**
* Disable support for gzip responses with this request.
*/
public function disableGzip()
{
if (
isset($this->requestHeaders['accept-encoding']) &&
$this->requestHeaders['accept-encoding'] == "gzip"
) {
unset($this->requestHeaders['accept-encoding']);
}
$this->canGzip = false;
$this->userAgent = str_replace(self::GZIP_UA, "", $this->userAgent);
}
/**
* Can this request accept a gzip response?
* @return bool
*/
public function canGzip()
{
return $this->canGzip;
}
/**
* Misc function that returns an array of the query parameters of the current
* url used by the OAuth signing class to calculate the signature
* @return array Query parameters in the query string.
*/
public function getQueryParams()
{
return $this->queryParams;
}
/**
* Set a new query parameter.
* @param $key - string to set, does not need to be URL encoded
* @param $value - string to set, does not need to be URL encoded
*/
public function setQueryParam($key, $value)
{
$this->queryParams[$key] = $value;
}
/**
* @return string HTTP Response Code.
*/
public function getResponseHttpCode()
{
return (int) $this->responseHttpCode;
}
/**
* @param int $responseHttpCode HTTP Response Code.
*/
public function setResponseHttpCode($responseHttpCode)
{
$this->responseHttpCode = $responseHttpCode;
}
/**
* @return $responseHeaders (array) HTTP Response Headers.
*/
public function getResponseHeaders()
{
return $this->responseHeaders;
}
/**
* @return string HTTP Response Body
*/
public function getResponseBody()
{
return $this->responseBody;
}
/**
* Set the class the response to this request should expect.
*
* @param $class string the class name
*/
public function setExpectedClass($class)
{
$this->expectedClass = $class;
}
/**
* Retrieve the expected class the response should expect.
* @return string class name
*/
public function getExpectedClass()
{
return $this->expectedClass;
}
/**
* @param array $headers The HTTP response headers
* to be normalized.
*/
public function setResponseHeaders($headers)
{
$headers = W3TCG_Google_Utils::normalize($headers);
if ($this->responseHeaders) {
$headers = array_merge($this->responseHeaders, $headers);
}
$this->responseHeaders = $headers;
}
/**
* @param string $key
* @return array|boolean Returns the requested HTTP header or
* false if unavailable.
*/
public function getResponseHeader($key)
{
return isset($this->responseHeaders[$key])
? $this->responseHeaders[$key]
: false;
}
/**
* @param string $responseBody The HTTP response body.
*/
public function setResponseBody($responseBody)
{
$this->responseBody = $responseBody;
}
/**
* @return string $url The request URL.
*/
public function getUrl()
{
return $this->baseComponent . $this->path .
(count($this->queryParams) ?
"?" . $this->buildQuery($this->queryParams) :
'');
}
/**
* @return string $method HTTP Request Method.
*/
public function getRequestMethod()
{
return $this->requestMethod;
}
/**
* @return array $headers HTTP Request Headers.
*/
public function getRequestHeaders()
{
return $this->requestHeaders;
}
/**
* @param string $key
* @return array|boolean Returns the requested HTTP header or
* false if unavailable.
*/
public function getRequestHeader($key)
{
return isset($this->requestHeaders[$key])
? $this->requestHeaders[$key]
: false;
}
/**
* @return string $postBody HTTP Request Body.
*/
public function getPostBody()
{
return $this->postBody;
}
/**
* @param string $url the url to set
*/
public function setUrl($url)
{
if (substr($url, 0, 4) != 'http') {
// Force the path become relative.
if (substr($url, 0, 1) !== '/') {
$url = '/' . $url;
}
}
$parts = parse_url($url);
if (isset($parts['host'])) {
$this->baseComponent = sprintf(
"%s%s%s",
isset($parts['scheme']) ? $parts['scheme'] . "://" : '',
isset($parts['host']) ? $parts['host'] : '',
isset($parts['port']) ? ":" . $parts['port'] : ''
);
}
$this->path = isset($parts['path']) ? $parts['path'] : '';
$this->queryParams = array();
if (isset($parts['query'])) {
$this->queryParams = $this->parseQuery($parts['query']);
}
}
/**
* @param string $method Set he HTTP Method and normalize
* it to upper-case, as required by HTTP.
*
*/
public function setRequestMethod($method)
{
$this->requestMethod = strtoupper($method);
}
/**
* @param array $headers The HTTP request headers
* to be set and normalized.
*/
public function setRequestHeaders($headers)
{
$headers = W3TCG_Google_Utils::normalize($headers);
if ($this->requestHeaders) {
$headers = array_merge($this->requestHeaders, $headers);
}
$this->requestHeaders = $headers;
}
/**
* @param string $postBody the postBody to set
*/
public function setPostBody($postBody)
{
$this->postBody = $postBody;
}
/**
* Set the User-Agent Header.
* @param string $userAgent The User-Agent.
*/
public function setUserAgent($userAgent)
{
$this->userAgent = $userAgent;
if ($this->canGzip) {
$this->userAgent = $userAgent . self::GZIP_UA;
}
}
/**
* @return string The User-Agent.
*/
public function getUserAgent()
{
return $this->userAgent;
}
/**
* Returns a cache key depending on if this was an OAuth signed request
* in which case it will use the non-signed url and access key to make this
* cache key unique per authenticated user, else use the plain request url
* @return string The md5 hash of the request cache key.
*/
public function getCacheKey()
{
$key = $this->getUrl();
if (isset($this->accessKey)) {
$key .= $this->accessKey;
}
if (isset($this->requestHeaders['authorization'])) {
$key .= $this->requestHeaders['authorization'];
}
return md5($key);
}
public function getParsedCacheControl()
{
$parsed = array();
$rawCacheControl = $this->getResponseHeader('cache-control');
if ($rawCacheControl) {
$rawCacheControl = str_replace(', ', '&', $rawCacheControl);
parse_str($rawCacheControl, $parsed);
}
return $parsed;
}
/**
* @param string $id
* @return string A string representation of the HTTP Request.
*/
public function toBatchString($id)
{
$str = '';
$path = parse_url($this->getUrl(), PHP_URL_PATH) . "?" .
http_build_query($this->queryParams);
$str .= $this->getRequestMethod() . ' ' . $path . " HTTP/1.1\n";
foreach ($this->getRequestHeaders() as $key => $val) {
$str .= $key . ': ' . $val . "\n";
}
if ($this->getPostBody()) {
$str .= "\n";
$str .= $this->getPostBody();
}
$headers = '';
foreach ($this->batchHeaders as $key => $val) {
$headers .= $key . ': ' . $val . "\n";
}
$headers .= "Content-ID: $id\n";
$str = $headers . "\n" . $str;
return $str;
}
/**
* Our own version of parse_str that allows for multiple variables
* with the same name.
* @param $string - the query string to parse
*/
private function parseQuery($string)
{
$return = array();
$parts = explode("&", $string);
foreach ($parts as $part) {
list($key, $value) = explode('=', $part, 2);
$value = urldecode($value);
if (isset($return[$key])) {
if (!is_array($return[$key])) {
$return[$key] = array($return[$key]);
}
$return[$key][] = $value;
} else {
$return[$key] = $value;
}
}
return $return;
}
/**
* A version of build query that allows for multiple
* duplicate keys.
* @param $parts array of key value pairs
*/
private function buildQuery($parts)
{
$return = array();
foreach ($parts as $key => $value) {
if (is_array($value)) {
foreach ($value as $v) {
$return[] = urlencode($key) . "=" . urlencode($v);
}
} else {
$return[] = urlencode($key) . "=" . urlencode($value);
}
}
return implode('&', $return);
}
/**
* If we're POSTing and have no body to send, we can send the query
* parameters in there, which avoids length issues with longer query
* params.
*/
public function maybeMoveParametersToBody()
{
if ($this->getRequestMethod() == "POST" && empty($this->postBody)) {
$this->setRequestHeaders(
array(
"content-type" =>
"application/x-www-form-urlencoded; charset=UTF-8"
)
);
$this->setPostBody($this->buildQuery($this->queryParams));
$this->queryParams = array();
}
}
}

View File

@@ -0,0 +1,327 @@
<?php
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Abstract IO base class
*/
abstract class W3TCG_Google_IO_Abstract
{
const UNKNOWN_CODE = 0;
const FORM_URLENCODED = 'application/x-www-form-urlencoded';
private static $CONNECTION_ESTABLISHED_HEADERS = array(
"HTTP/1.0 200 Connection established\r\n\r\n",
"HTTP/1.1 200 Connection established\r\n\r\n",
);
private static $ENTITY_HTTP_METHODS = array("POST" => null, "PUT" => null);
/** @var W3TCG_Google_Client */
protected $client;
public function __construct(W3TCG_Google_Client $client)
{
$this->client = $client;
$timeout = $client->getClassConfig('W3TCG_Google_IO_Abstract', 'request_timeout_seconds');
if ($timeout > 0) {
$this->setTimeout($timeout);
}
}
/**
* Executes a W3TCG_Google_Http_Request and returns the resulting populated W3TCG_Google_Http_Request
* @param W3TCG_Google_Http_Request $request
* @return W3TCG_Google_Http_Request $request
*/
abstract public function executeRequest(W3TCG_Google_Http_Request $request);
/**
* Set options that update the transport implementation's behavior.
* @param $options
*/
abstract public function setOptions($options);
/**
* Set the maximum request time in seconds.
* @param $timeout in seconds
*/
abstract public function setTimeout($timeout);
/**
* Get the maximum request time in seconds.
* @return timeout in seconds
*/
abstract public function getTimeout();
/**
* Test for the presence of a cURL header processing bug
*
* The cURL bug was present in versions prior to 7.30.0 and caused the header
* length to be miscalculated when a "Connection established" header added by
* some proxies was present.
*
* @return boolean
*/
abstract protected function needsQuirk();
/**
* @visible for testing.
* Cache the response to an HTTP request if it is cacheable.
* @param W3TCG_Google_Http_Request $request
* @return bool Returns true if the insertion was successful.
* Otherwise, return false.
*/
public function setCachedRequest(W3TCG_Google_Http_Request $request)
{
// Determine if the request is cacheable.
if (W3TCG_Google_Http_CacheParser::isResponseCacheable($request)) {
$this->client->getCache()->set($request->getCacheKey(), $request);
return true;
}
return false;
}
/**
* Execute an HTTP Request
*
* @param W3TCG_Google_HttpRequest $request the http request to be executed
* @return W3TCG_Google_HttpRequest http request with the response http code,
* response headers and response body filled in
* @throws W3TCG_Google_IO_Exception on curl or IO error
*/
public function makeRequest(W3TCG_Google_Http_Request $request)
{
// First, check to see if we have a valid cached version.
$cached = $this->getCachedRequest($request);
if ($cached !== false && $cached instanceof W3TCG_Google_Http_Request) {
if (!$this->checkMustRevalidateCachedRequest($cached, $request)) {
return $cached;
}
}
if (array_key_exists($request->getRequestMethod(), self::$ENTITY_HTTP_METHODS)) {
$request = $this->processEntityRequest($request);
}
list($responseData, $responseHeaders, $respHttpCode) = $this->executeRequest($request);
if ($respHttpCode == 304 && $cached) {
// If the server responded NOT_MODIFIED, return the cached request.
$this->updateCachedRequest($cached, $responseHeaders);
return $cached;
}
if (!isset($responseHeaders['Date']) && !isset($responseHeaders['date'])) {
$responseHeaders['Date'] = date("r");
}
$request->setResponseHttpCode($respHttpCode);
$request->setResponseHeaders($responseHeaders);
$request->setResponseBody($responseData);
// Store the request in cache (the function checks to see if the request
// can actually be cached)
$this->setCachedRequest($request);
return $request;
}
/**
* @visible for testing.
* @param W3TCG_Google_Http_Request $request
* @return W3TCG_Google_Http_Request|bool Returns the cached object or
* false if the operation was unsuccessful.
*/
public function getCachedRequest(W3TCG_Google_Http_Request $request)
{
if (false === W3TCG_Google_Http_CacheParser::isRequestCacheable($request)) {
return false;
}
return $this->client->getCache()->get($request->getCacheKey());
}
/**
* @visible for testing
* Process an http request that contains an enclosed entity.
* @param W3TCG_Google_Http_Request $request
* @return W3TCG_Google_Http_Request Processed request with the enclosed entity.
*/
public function processEntityRequest(W3TCG_Google_Http_Request $request)
{
$postBody = $request->getPostBody();
$contentType = $request->getRequestHeader("content-type");
// Set the default content-type as application/x-www-form-urlencoded.
if (false == $contentType) {
$contentType = self::FORM_URLENCODED;
$request->setRequestHeaders(array('content-type' => $contentType));
}
// Force the payload to match the content-type asserted in the header.
if ($contentType == self::FORM_URLENCODED && is_array($postBody)) {
$postBody = http_build_query($postBody, '', '&');
$request->setPostBody($postBody);
}
// Make sure the content-length header is set.
if (!$postBody || is_string($postBody)) {
$postsLength = strlen($postBody);
$request->setRequestHeaders(array('content-length' => $postsLength));
}
return $request;
}
/**
* Check if an already cached request must be revalidated, and if so update
* the request with the correct ETag headers.
* @param W3TCG_Google_Http_Request $cached A previously cached response.
* @param W3TCG_Google_Http_Request $request The outbound request.
* return bool If the cached object needs to be revalidated, false if it is
* still current and can be re-used.
*/
protected function checkMustRevalidateCachedRequest($cached, $request)
{
if (W3TCG_Google_Http_CacheParser::mustRevalidate($cached)) {
$addHeaders = array();
if ($cached->getResponseHeader('etag')) {
// [13.3.4] If an entity tag has been provided by the origin server,
// we must use that entity tag in any cache-conditional request.
$addHeaders['If-None-Match'] = $cached->getResponseHeader('etag');
} elseif ($cached->getResponseHeader('date')) {
$addHeaders['If-Modified-Since'] = $cached->getResponseHeader('date');
}
$request->setRequestHeaders($addHeaders);
return true;
} else {
return false;
}
}
/**
* Update a cached request, using the headers from the last response.
* @param W3TCG_Google_HttpRequest $cached A previously cached response.
* @param mixed Associative array of response headers from the last request.
*/
protected function updateCachedRequest($cached, $responseHeaders)
{
if (isset($responseHeaders['connection'])) {
$hopByHop = array_merge(
self::$HOP_BY_HOP,
explode(
',',
$responseHeaders['connection']
)
);
$endToEnd = array();
foreach ($hopByHop as $key) {
if (isset($responseHeaders[$key])) {
$endToEnd[$key] = $responseHeaders[$key];
}
}
$cached->setResponseHeaders($endToEnd);
}
}
/**
* Used by the IO lib and also the batch processing.
*
* @param $respData
* @param $headerSize
* @return array
*/
public function parseHttpResponse($respData, $headerSize)
{
// check proxy header
foreach (self::$CONNECTION_ESTABLISHED_HEADERS as $established_header) {
if (stripos($respData, $established_header) !== false) {
// existed, remove it
$respData = str_ireplace($established_header, '', $respData);
// Subtract the proxy header size unless the cURL bug prior to 7.30.0
// is present which prevented the proxy header size from being taken into
// account.
if (!$this->needsQuirk()) {
$headerSize -= strlen($established_header);
}
break;
}
}
if ($headerSize) {
$responseBody = substr($respData, $headerSize);
$responseHeaders = substr($respData, 0, $headerSize);
} else {
$responseSegments = explode("\r\n\r\n", $respData, 2);
$responseHeaders = $responseSegments[0];
$responseBody = isset($responseSegments[1]) ? $responseSegments[1] :
null;
}
$responseHeaders = $this->getHttpResponseHeaders($responseHeaders);
return array($responseHeaders, $responseBody);
}
/**
* Parse out headers from raw headers
* @param rawHeaders array or string
* @return array
*/
public function getHttpResponseHeaders($rawHeaders)
{
if (is_array($rawHeaders)) {
return $this->parseArrayHeaders($rawHeaders);
} else {
return $this->parseStringHeaders($rawHeaders);
}
}
private function parseStringHeaders($rawHeaders)
{
$headers = array();
$responseHeaderLines = explode("\r\n", $rawHeaders);
foreach ($responseHeaderLines as $headerLine) {
if ($headerLine && strpos($headerLine, ':') !== false) {
list($header, $value) = explode(': ', $headerLine, 2);
$header = strtolower($header);
if (isset($headers[$header])) {
$headers[$header] .= "\n" . $value;
} else {
$headers[$header] = $value;
}
}
}
return $headers;
}
private function parseArrayHeaders($rawHeaders)
{
$header_count = count($rawHeaders);
$headers = array();
for ($i = 0; $i < $header_count; $i++) {
$header = $rawHeaders[$i];
// Times will have colons in - so we just want the first match.
$header_parts = explode(': ', $header, 2);
if (count($header_parts) == 2) {
$headers[$header_parts[0]] = $header_parts[1];
}
}
return $headers;
}
}

View File

@@ -0,0 +1,136 @@
<?php
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Curl based implementation of W3TCG_Google_IO.
*
* @author Stuart Langley <slangley@google.com>
*/
class W3TCG_Google_IO_Curl extends W3TCG_Google_IO_Abstract
{
// cURL hex representation of version 7.30.0
const NO_QUIRK_VERSION = 0x071E00;
private $options = array();
/**
* Execute an HTTP Request
*
* @param W3TCG_Google_HttpRequest $request the http request to be executed
* @return W3TCG_Google_HttpRequest http request with the response http code,
* response headers and response body filled in
* @throws W3TCG_Google_IO_Exception on curl or IO error
*/
public function executeRequest(W3TCG_Google_Http_Request $request)
{
$curl = curl_init();
if ($request->getPostBody()) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody());
}
$requestHeaders = $request->getRequestHeaders();
if ($requestHeaders && is_array($requestHeaders)) {
$curlHeaders = array();
foreach ($requestHeaders as $k => $v) {
$curlHeaders[] = "$k: $v";
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders);
}
curl_setopt($curl, CURLOPT_URL, $request->getUrl());
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod());
curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent());
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
if ($request->canGzip()) {
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
}
foreach ($this->options as $key => $var) {
curl_setopt($curl, $key, $var);
}
if (!isset($this->options[CURLOPT_CAINFO])) {
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem');
}
$response = curl_exec($curl);
if ($response === false) {
throw new W3TCG_Google_IO_Exception(curl_error($curl));
}
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
list($responseHeaders, $responseBody) = $this->parseHttpResponse($response, $headerSize);
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
return array($responseBody, $responseHeaders, $responseCode);
}
/**
* Set options that update the transport implementation's behavior.
* @param $options
*/
public function setOptions($options)
{
$this->options = $options + $this->options;
}
/**
* Set the maximum request time in seconds.
* @param $timeout in seconds
*/
public function setTimeout($timeout)
{
// Since this timeout is really for putting a bound on the time
// we'll set them both to the same. If you need to specify a longer
// CURLOPT_TIMEOUT, or a tigher CONNECTTIMEOUT, the best thing to
// do is use the setOptions method for the values individually.
$this->options[CURLOPT_CONNECTTIMEOUT] = $timeout;
$this->options[CURLOPT_TIMEOUT] = $timeout;
}
/**
* Get the maximum request time in seconds.
* @return timeout in seconds
*/
public function getTimeout()
{
return $this->options[CURLOPT_TIMEOUT];
}
/**
* Test for the presence of a cURL header processing bug
*
* {@inheritDoc}
*
* @return boolean
*/
protected function needsQuirk()
{
$ver = curl_version();
$versionNum = $ver['version_number'];
return $versionNum < W3TCG_Google_IO_Curl::NO_QUIRK_VERSION;
}
}

View File

@@ -0,0 +1,20 @@
<?php
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class W3TCG_Google_IO_Exception extends W3TCG_Google_Exception
{
}

View File

@@ -0,0 +1,209 @@
<?php
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Http Streams based implementation of W3TCG_Google_IO.
*
* @author Stuart Langley <slangley@google.com>
*/
class W3TCG_Google_IO_Stream extends W3TCG_Google_IO_Abstract
{
const TIMEOUT = "timeout";
const ZLIB = "compress.zlib://";
private $options = array();
private $trappedErrorNumber;
private $trappedErrorString;
private static $DEFAULT_HTTP_CONTEXT = array(
"follow_location" => 0,
"ignore_errors" => 1,
);
private static $DEFAULT_SSL_CONTEXT = array(
"verify_peer" => true,
);
/**
* Execute an HTTP Request
*
* @param W3TCG_Google_HttpRequest $request the http request to be executed
* @return W3TCG_Google_HttpRequest http request with the response http code,
* response headers and response body filled in
* @throws W3TCG_Google_IO_Exception on curl or IO error
*/
public function executeRequest(W3TCG_Google_Http_Request $request)
{
$default_options = stream_context_get_options(stream_context_get_default());
$requestHttpContext = array_key_exists('http', $default_options) ?
$default_options['http'] : array();
if ($request->getPostBody()) {
$requestHttpContext["content"] = $request->getPostBody();
}
$requestHeaders = $request->getRequestHeaders();
if ($requestHeaders && is_array($requestHeaders)) {
$headers = "";
foreach ($requestHeaders as $k => $v) {
$headers .= "$k: $v\r\n";
}
$requestHttpContext["header"] = $headers;
}
$requestHttpContext["method"] = $request->getRequestMethod();
$requestHttpContext["user_agent"] = $request->getUserAgent();
$requestSslContext = array_key_exists('ssl', $default_options) ?
$default_options['ssl'] : array();
if (!array_key_exists("cafile", $requestSslContext)) {
$requestSslContext["cafile"] = dirname(__FILE__) . '/cacerts.pem';
}
$options = array(
"http" => array_merge(
self::$DEFAULT_HTTP_CONTEXT,
$requestHttpContext
),
"ssl" => array_merge(
self::$DEFAULT_SSL_CONTEXT,
$requestSslContext
)
);
$context = stream_context_create($options);
$url = $request->getUrl();
if ($request->canGzip()) {
$url = self::ZLIB . $url;
}
// We are trapping any thrown errors in this method only and
// throwing an exception.
$this->trappedErrorNumber = null;
$this->trappedErrorString = null;
// START - error trap.
set_error_handler(array($this, 'trapError'));
$fh = fopen($url, 'r', false, $context);
restore_error_handler();
// END - error trap.
if ($this->trappedErrorNumber) {
throw new W3TCG_Google_IO_Exception(
sprintf(
"HTTP Error: Unable to connect: '%s'",
$this->trappedErrorString
),
$this->trappedErrorNumber
);
}
$response_data = false;
$respHttpCode = self::UNKNOWN_CODE;
if ($fh) {
if (isset($this->options[self::TIMEOUT])) {
stream_set_timeout($fh, $this->options[self::TIMEOUT]);
}
$response_data = stream_get_contents($fh);
fclose($fh);
$respHttpCode = $this->getHttpResponseCode($http_response_header);
}
if (false === $response_data) {
throw new W3TCG_Google_IO_Exception(
sprintf(
"HTTP Error: Unable to connect: '%s'",
$respHttpCode
),
$respHttpCode
);
}
$responseHeaders = $this->getHttpResponseHeaders($http_response_header);
return array($response_data, $responseHeaders, $respHttpCode);
}
/**
* Set options that update the transport implementation's behavior.
* @param $options
*/
public function setOptions($options)
{
$this->options = $options + $this->options;
}
/**
* Method to handle errors, used for error handling around
* stream connection methods.
*/
public function trapError($errno, $errstr)
{
$this->trappedErrorNumber = $errno;
$this->trappedErrorString = $errstr;
}
/**
* Set the maximum request time in seconds.
* @param $timeout in seconds
*/
public function setTimeout($timeout)
{
$this->options[self::TIMEOUT] = $timeout;
}
/**
* Get the maximum request time in seconds.
* @return timeout in seconds
*/
public function getTimeout()
{
return $this->options[self::TIMEOUT];
}
/**
* Test for the presence of a cURL header processing bug
*
* {@inheritDoc}
*
* @return boolean
*/
protected function needsQuirk()
{
return false;
}
protected function getHttpResponseCode($response_headers)
{
$header_count = count($response_headers);
for ($i = 0; $i < $header_count; $i++) {
$header = $response_headers[$i];
if (strncasecmp("HTTP", $header, strlen("HTTP")) == 0) {
$response = explode(' ', $header);
return $response[1];
}
}
return self::UNKNOWN_CODE;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,281 @@
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This class defines attributes, valid values, and usage which is generated
* from a given json schema.
* http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5
*
* @author Chirag Shah <chirags@google.com>
*
*/
class W3TCG_Google_Model implements ArrayAccess
{
protected $internal_gapi_mappings = array();
protected $modelData = array();
protected $processed = array();
/**
* Polymorphic - accepts a variable number of arguments dependent
* on the type of the model subclass.
*/
final public function __construct()
{
if (func_num_args() == 1 && is_array(func_get_arg(0))) {
// Initialize the model with the array's contents.
$array = func_get_arg(0);
$this->mapTypes($array);
}
$this->gapiInit();
}
/**
* Getter that handles passthrough access to the data array, and lazy object creation.
* @param string $key Property name.
* @return mixed The value if any, or null.
*/
public function __get($key)
{
$keyTypeName = $this->keyType($key);
$keyDataType = $this->dataType($key);
if (isset($this->$keyTypeName) && !isset($this->processed[$key])) {
if (isset($this->modelData[$key])) {
$val = $this->modelData[$key];
} else if (isset($this->$keyDataType) &&
($this->$keyDataType == 'array' || $this->$keyDataType == 'map')) {
$val = array();
} else {
$val = null;
}
if ($this->isAssociativeArray($val)) {
if (isset($this->$keyDataType) && 'map' == $this->$keyDataType) {
foreach ($val as $arrayKey => $arrayItem) {
$this->modelData[$key][$arrayKey] =
$this->createObjectFromName($keyTypeName, $arrayItem);
}
} else {
$this->modelData[$key] = $this->createObjectFromName($keyTypeName, $val);
}
} else if (is_array($val)) {
$arrayObject = array();
foreach ($val as $arrayIndex => $arrayItem) {
$arrayObject[$arrayIndex] =
$this->createObjectFromName($keyTypeName, $arrayItem);
}
$this->modelData[$key] = $arrayObject;
}
$this->processed[$key] = true;
}
return isset($this->modelData[$key]) ? $this->modelData[$key] : null;
}
/**
* Initialize this object's properties from an array.
*
* @param array $array Used to seed this object's properties.
* @return void
*/
protected function mapTypes($array)
{
// Hard initilise simple types, lazy load more complex ones.
foreach ($array as $key => $val) {
if ( !property_exists($this, $this->keyType($key)) &&
property_exists($this, $key)) {
$this->$key = $val;
unset($array[$key]);
} elseif (property_exists($this, $camelKey = W3TCG_Google_Utils::camelCase($key))) {
// For backwards compatibility, this checks if property exists as camelCase, leaving
// it in array as snake_case
$this->$camelKey = $val;
}
}
$this->modelData = $array;
}
/**
* Blank initialiser to be used in subclasses to do post-construction initialisation - this
* avoids the need for subclasses to have to implement the variadics handling in their
* constructors.
*/
protected function gapiInit()
{
return;
}
/**
* Create a simplified object suitable for straightforward
* conversion to JSON. This is relatively expensive
* due to the usage of reflection, but shouldn't be called
* a whole lot, and is the most straightforward way to filter.
*/
public function toSimpleObject()
{
$object = new stdClass();
// Process all other data.
foreach ($this->modelData as $key => $val) {
$result = $this->getSimpleValue($val);
if ($result !== null) {
$object->$key = $result;
}
}
// Process all public properties.
$reflect = new ReflectionObject($this);
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($props as $member) {
$name = $member->getName();
$result = $this->getSimpleValue($this->$name);
if ($result !== null) {
$name = $this->getMappedName($name);
$object->$name = $result;
}
}
return $object;
}
/**
* Handle different types of values, primarily
* other objects and map and array data types.
*/
private function getSimpleValue($value)
{
if ($value instanceof W3TCG_Google_Model) {
return $value->toSimpleObject();
} else if (is_array($value)) {
$return = array();
foreach ($value as $key => $a_value) {
$a_value = $this->getSimpleValue($a_value);
if ($a_value !== null) {
$key = $this->getMappedName($key);
$return[$key] = $a_value;
}
}
return $return;
}
return $value;
}
/**
* If there is an internal name mapping, use that.
*/
private function getMappedName($key)
{
if (isset($this->internal_gapi_mappings) &&
isset($this->internal_gapi_mappings[$key])) {
$key = $this->internal_gapi_mappings[$key];
}
return $key;
}
/**
* Returns true only if the array is associative.
* @param array $array
* @return bool True if the array is associative.
*/
protected function isAssociativeArray($array)
{
if (!is_array($array)) {
return false;
}
$keys = array_keys($array);
foreach ($keys as $key) {
if (is_string($key)) {
return true;
}
}
return false;
}
/**
* Given a variable name, discover its type.
*
* @param $name
* @param $item
* @return object The object from the item.
*/
private function createObjectFromName($name, $item)
{
$type = $this->$name;
return new $type($item);
}
/**
* Verify if $obj is an array.
* @throws W3TCG_Google_Exception Thrown if $obj isn't an array.
* @param array $obj Items that should be validated.
* @param string $method Method expecting an array as an argument.
*/
public function assertIsArray($obj, $method)
{
if ($obj && !is_array($obj)) {
throw new W3TCG_Google_Exception(
"Incorrect parameter type passed to $method(). Expected an array."
);
}
}
public function offsetExists($offset)
{
return isset($this->$offset) || isset($this->modelData[$offset]);
}
public function offsetGet($offset)
{
return isset($this->$offset) ?
$this->$offset :
$this->__get($offset);
}
public function offsetSet($offset, $value)
{
if (property_exists($this, $offset)) {
$this->$offset = $value;
} else {
$this->modelData[$offset] = $value;
$this->processed[$offset] = true;
}
}
public function offsetUnset($offset)
{
unset($this->modelData[$offset]);
}
protected function keyType($key)
{
return $key . "Type";
}
protected function dataType($key)
{
return $key . "DataType";
}
public function __isset($key)
{
return isset($this->modelData[$key]);
}
public function __unset($key)
{
unset($this->modelData[$key]);
}
}

View File

@@ -0,0 +1,39 @@
<?php
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class W3TCG_Google_Service
{
public $version;
public $servicePath;
public $availableScopes;
public $resource;
private $client;
public function __construct(W3TCG_Google_Client $client)
{
$this->client = $client;
}
/**
* Return the associated W3TCG_Google_Client class.
* @return W3TCG_Google_Client
*/
public function getClient()
{
return $this->client;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,51 @@
<?php
class W3TCG_Google_Service_Exception extends W3TCG_Google_Exception
{
/**
* Optional list of errors returned in a JSON body of an HTTP error response.
*/
protected $errors = array();
/**
* Override default constructor to add ability to set $errors.
*
* @param string $message
* @param int $code
* @param Exception|null $previous
* @param [{string, string}] errors List of errors returned in an HTTP
* response. Defaults to [].
*/
public function __construct(
$message,
$code = 0,
Exception $previous = null,
$errors = array()
) {
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
parent::__construct($message, $code, $previous);
} else {
parent::__construct($message, $code);
}
$this->errors = $errors;
}
/**
* An example of the possible errors returned.
*
* {
* "domain": "global",
* "reason": "authError",
* "message": "Invalid Credentials",
* "locationType": "header",
* "location": "Authorization",
* }
*
* @return [{string, string}] List of errors return in an HTTP response or [].
*/
public function getErrors()
{
return $this->errors;
}
}

View File

@@ -0,0 +1,409 @@
<?php
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
/**
* Service definition for Oauth2 (v2).
*
* <p>
* Lets you access OAuth2 protocol related APIs.
* </p>
*
* <p>
* For more information about this service, see the API
* <a href="https://developers.google.com/accounts/docs/OAuth2" target="_blank">Documentation</a>
* </p>
*
* @author Google, Inc.
*/
class W3TCG_Google_Service_Oauth2 extends W3TCG_Google_Service
{
/** Know your basic profile info and list of people in your circles.. */
const PLUS_LOGIN = "https://www.googleapis.com/auth/plus.login";
/** Know who you are on Google. */
const PLUS_ME = "https://www.googleapis.com/auth/plus.me";
/** View your email address. */
const USERINFO_EMAIL = "https://www.googleapis.com/auth/userinfo.email";
/** View your basic profile info. */
const USERINFO_PROFILE = "https://www.googleapis.com/auth/userinfo.profile";
public $userinfo;
public $userinfo_v2_me;
private $base_methods;
/**
* Constructs the internal representation of the Oauth2 service.
*
* @param W3TCG_Google_Client $client
*/
public function __construct(W3TCG_Google_Client $client)
{
parent::__construct($client);
$this->servicePath = '';
$this->version = 'v2';
$this->serviceName = 'oauth2';
$this->userinfo = new W3TCG_Google_Service_Oauth2_Userinfo_Resource(
$this,
$this->serviceName,
'userinfo',
array(
'methods' => array(
'get' => array(
'path' => 'oauth2/v2/userinfo',
'httpMethod' => 'GET',
'parameters' => array(),
),
)
)
);
$this->userinfo_v2_me = new W3TCG_Google_Service_Oauth2_UserinfoV2Me_Resource(
$this,
$this->serviceName,
'me',
array(
'methods' => array(
'get' => array(
'path' => 'userinfo/v2/me',
'httpMethod' => 'GET',
'parameters' => array(),
),
)
)
);
$this->base_methods = new W3TCG_Google_Service_Resource(
$this,
$this->serviceName,
'',
array(
'methods' => array(
'tokeninfo' => array(
'path' => 'oauth2/v2/tokeninfo',
'httpMethod' => 'POST',
'parameters' => array(
'access_token' => array(
'location' => 'query',
'type' => 'string',
),
'id_token' => array(
'location' => 'query',
'type' => 'string',
),
),
),
)
)
);
}
/**
* (tokeninfo)
*
* @param array $optParams Optional parameters.
*
* @opt_param string access_token
*
* @opt_param string id_token
*
* @return W3TCG_Google_Service_Oauth2_Tokeninfo
*/
public function tokeninfo($optParams = array())
{
$params = array();
$params = array_merge($params, $optParams);
return $this->base_methods->call('tokeninfo', array($params), "W3TCG_Google_Service_Oauth2_Tokeninfo");
}
}
/**
* The "userinfo" collection of methods.
* Typical usage is:
* <code>
* $oauth2Service = new W3TCG_Google_Service_Oauth2(...);
* $userinfo = $oauth2Service->userinfo;
* </code>
*/
class W3TCG_Google_Service_Oauth2_Userinfo_Resource extends W3TCG_Google_Service_Resource
{
/**
* (userinfo.get)
*
* @param array $optParams Optional parameters.
* @return W3TCG_Google_Service_Oauth2_Userinfoplus
*/
public function get($optParams = array())
{
$params = array();
$params = array_merge($params, $optParams);
return $this->call('get', array($params), "W3TCG_Google_Service_Oauth2_Userinfoplus");
}
}
/**
* The "v2" collection of methods.
* Typical usage is:
* <code>
* $oauth2Service = new W3TCG_Google_Service_Oauth2(...);
* $v2 = $oauth2Service->v2;
* </code>
*/
class W3TCG_Google_Service_Oauth2_UserinfoV2_Resource extends W3TCG_Google_Service_Resource
{
}
/**
* The "me" collection of methods.
* Typical usage is:
* <code>
* $oauth2Service = new W3TCG_Google_Service_Oauth2(...);
* $me = $oauth2Service->me;
* </code>
*/
class W3TCG_Google_Service_Oauth2_UserinfoV2Me_Resource extends W3TCG_Google_Service_Resource
{
/**
* (me.get)
*
* @param array $optParams Optional parameters.
* @return W3TCG_Google_Service_Oauth2_Userinfoplus
*/
public function get($optParams = array())
{
$params = array();
$params = array_merge($params, $optParams);
return $this->call('get', array($params), "W3TCG_Google_Service_Oauth2_Userinfoplus");
}
}
class W3TCG_Google_Service_Oauth2_Tokeninfo extends W3TCG_Google_Model
{
protected $internal_gapi_mappings = array(
"accessType" => "access_type",
"expiresIn" => "expires_in",
"issuedTo" => "issued_to",
"userId" => "user_id",
"verifiedEmail" => "verified_email",
);
public $accessType;
public $audience;
public $email;
public $expiresIn;
public $issuedTo;
public $scope;
public $userId;
public $verifiedEmail;
public function setAccessType($accessType)
{
$this->accessType = $accessType;
}
public function getAccessType()
{
return $this->accessType;
}
public function setAudience($audience)
{
$this->audience = $audience;
}
public function getAudience()
{
return $this->audience;
}
public function setEmail($email)
{
$this->email = $email;
}
public function getEmail()
{
return $this->email;
}
public function setExpiresIn($expiresIn)
{
$this->expiresIn = $expiresIn;
}
public function getExpiresIn()
{
return $this->expiresIn;
}
public function setIssuedTo($issuedTo)
{
$this->issuedTo = $issuedTo;
}
public function getIssuedTo()
{
return $this->issuedTo;
}
public function setScope($scope)
{
$this->scope = $scope;
}
public function getScope()
{
return $this->scope;
}
public function setUserId($userId)
{
$this->userId = $userId;
}
public function getUserId()
{
return $this->userId;
}
public function setVerifiedEmail($verifiedEmail)
{
$this->verifiedEmail = $verifiedEmail;
}
public function getVerifiedEmail()
{
return $this->verifiedEmail;
}
}
class W3TCG_Google_Service_Oauth2_Userinfoplus extends W3TCG_Google_Model
{
protected $internal_gapi_mappings = array(
"familyName" => "family_name",
"givenName" => "given_name",
"verifiedEmail" => "verified_email",
);
public $email;
public $familyName;
public $gender;
public $givenName;
public $hd;
public $id;
public $link;
public $locale;
public $name;
public $picture;
public $verifiedEmail;
public function setEmail($email)
{
$this->email = $email;
}
public function getEmail()
{
return $this->email;
}
public function setFamilyName($familyName)
{
$this->familyName = $familyName;
}
public function getFamilyName()
{
return $this->familyName;
}
public function setGender($gender)
{
$this->gender = $gender;
}
public function getGender()
{
return $this->gender;
}
public function setGivenName($givenName)
{
$this->givenName = $givenName;
}
public function getGivenName()
{
return $this->givenName;
}
public function setHd($hd)
{
$this->hd = $hd;
}
public function getHd()
{
return $this->hd;
}
public function setId($id)
{
$this->id = $id;
}
public function getId()
{
return $this->id;
}
public function setLink($link)
{
$this->link = $link;
}
public function getLink()
{
return $this->link;
}
public function setLocale($locale)
{
$this->locale = $locale;
}
public function getLocale()
{
return $this->locale;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function setPicture($picture)
{
$this->picture = $picture;
}
public function getPicture()
{
return $this->picture;
}
public function setVerifiedEmail($verifiedEmail)
{
$this->verifiedEmail = $verifiedEmail;
}
public function getVerifiedEmail()
{
return $this->verifiedEmail;
}
}

View File

@@ -0,0 +1,203 @@
<?php
/**
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Implements the actual methods/resources of the discovered Google API using magic function
* calling overloading (__call()), which on call will see if the method name (plus.activities.list)
* is available in this service, and if so construct an apiHttpRequest representing it.
*
* @author Chris Chabot <chabotc@google.com>
* @author Chirag Shah <chirags@google.com>
*
*/
class W3TCG_Google_Service_Resource
{
// Valid query parameters that work, but don't appear in discovery.
private $stackParameters = array(
'alt' => array('type' => 'string', 'location' => 'query'),
'fields' => array('type' => 'string', 'location' => 'query'),
'trace' => array('type' => 'string', 'location' => 'query'),
'userIp' => array('type' => 'string', 'location' => 'query'),
'userip' => array('type' => 'string', 'location' => 'query'),
'quotaUser' => array('type' => 'string', 'location' => 'query'),
'data' => array('type' => 'string', 'location' => 'body'),
'mimeType' => array('type' => 'string', 'location' => 'header'),
'uploadType' => array('type' => 'string', 'location' => 'query'),
'mediaUpload' => array('type' => 'complex', 'location' => 'query'),
);
/** @var W3TCG_Google_Service $service */
private $service;
/** @var W3TCG_Google_Client $client */
private $client;
/** @var string $serviceName */
private $serviceName;
/** @var string $resourceName */
private $resourceName;
/** @var array $methods */
private $methods;
public function __construct($service, $serviceName, $resourceName, $resource)
{
$this->service = $service;
$this->client = $service->getClient();
$this->serviceName = $serviceName;
$this->resourceName = $resourceName;
$this->methods = isset($resource['methods']) ?
$resource['methods'] :
array($resourceName => $resource);
}
/**
* TODO(ianbarber): This function needs simplifying.
* @param $name
* @param $arguments
* @param $expected_class - optional, the expected class name
* @return W3TCG_Google_Http_Request|expected_class
* @throws W3TCG_Google_Exception
*/
public function call($name, $arguments, $expected_class = null)
{
if (! isset($this->methods[$name])) {
throw new W3TCG_Google_Exception(
"Unknown function: " .
"{$this->serviceName}->{$this->resourceName}->{$name}()"
);
}
$method = $this->methods[$name];
$parameters = $arguments[0];
// postBody is a special case since it's not defined in the discovery
// document as parameter, but we abuse the param entry for storing it.
$postBody = null;
if (isset($parameters['postBody'])) {
if ($parameters['postBody'] instanceof W3TCG_Google_Model) {
// In the cases the post body is an existing object, we want
// to use the smart method to create a simple object for
// for JSONification.
$parameters['postBody'] = $parameters['postBody']->toSimpleObject();
} else if (is_object($parameters['postBody'])) {
// If the post body is another kind of object, we will try and
// wrangle it into a sensible format.
$parameters['postBody'] =
$this->convertToArrayAndStripNulls($parameters['postBody']);
}
$postBody = json_encode($parameters['postBody']);
unset($parameters['postBody']);
}
// TODO(ianbarber): optParams here probably should have been
// handled already - this may well be redundant code.
if (isset($parameters['optParams'])) {
$optParams = $parameters['optParams'];
unset($parameters['optParams']);
$parameters = array_merge($parameters, $optParams);
}
if (!isset($method['parameters'])) {
$method['parameters'] = array();
}
$method['parameters'] = array_merge(
$method['parameters'],
$this->stackParameters
);
foreach ($parameters as $key => $val) {
if ($key != 'postBody' && ! isset($method['parameters'][$key])) {
throw new W3TCG_Google_Exception("($name) unknown parameter: '$key'");
}
}
foreach ($method['parameters'] as $paramName => $paramSpec) {
if (isset($paramSpec['required']) &&
$paramSpec['required'] &&
! isset($parameters[$paramName])
) {
throw new W3TCG_Google_Exception("($name) missing required param: '$paramName'");
}
if (isset($parameters[$paramName])) {
$value = $parameters[$paramName];
$parameters[$paramName] = $paramSpec;
$parameters[$paramName]['value'] = $value;
unset($parameters[$paramName]['required']);
} else {
// Ensure we don't pass nulls.
unset($parameters[$paramName]);
}
}
$servicePath = $this->service->servicePath;
$url = W3TCG_Google_Http_REST::createRequestUri(
$servicePath,
$method['path'],
$parameters
);
$httpRequest = new W3TCG_Google_Http_Request(
$url,
$method['httpMethod'],
null,
$postBody
);
$httpRequest->setBaseComponent($this->client->getBasePath());
if ($postBody) {
$contentTypeHeader = array();
$contentTypeHeader['content-type'] = 'application/json; charset=UTF-8';
$httpRequest->setRequestHeaders($contentTypeHeader);
$httpRequest->setPostBody($postBody);
}
$httpRequest = $this->client->getAuth()->sign($httpRequest);
$httpRequest->setExpectedClass($expected_class);
if (isset($parameters['data']) &&
($parameters['uploadType']['value'] == 'media' || $parameters['uploadType']['value'] == 'multipart')) {
// If we are doing a simple media upload, trigger that as a convenience.
$mfu = new W3TCG_Google_Http_MediaFileUpload(
$this->client,
$httpRequest,
isset($parameters['mimeType']) ? $parameters['mimeType']['value'] : 'application/octet-stream',
$parameters['data']['value']
);
}
if ($this->client->shouldDefer()) {
// If we are in batch or upload mode, return the raw request.
return $httpRequest;
}
return $this->client->execute($httpRequest);
}
protected function convertToArrayAndStripNulls($o)
{
$o = (array) $o;
foreach ($o as $k => $v) {
if ($v === null) {
unset($o[$k]);
} elseif (is_object($v) || is_array($v)) {
$o[$k] = $this->convertToArrayAndStripNulls($o[$k]);
}
}
return $o;
}
}

View File

@@ -0,0 +1,29 @@
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Signs data.
*
* @author Brian Eaton <beaton@google.com>
*/
abstract class W3TCG_Google_Signer_Abstract
{
/**
* Signs data, returns the signature as binary data.
*/
abstract public function sign($data);
}

View File

@@ -0,0 +1,88 @@
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Signs data.
*
* Only used for testing.
*
* @author Brian Eaton <beaton@google.com>
*/
class W3TCG_Google_Signer_P12 extends W3TCG_Google_Signer_Abstract
{
// OpenSSL private key resource
private $privateKey;
// Creates a new signer from a .p12 file.
public function __construct($p12, $password)
{
if (!function_exists('openssl_x509_read')) {
throw new W3TCG_Google_Exception(
'The Google PHP API library needs the openssl PHP extension'
);
}
// If the private key is provided directly, then this isn't in the p12
// format. Different versions of openssl support different p12 formats
// and the key from google wasn't being accepted by the version available
// at the time.
if (!$password && strpos($p12, "-----BEGIN RSA PRIVATE KEY-----") !== false) {
$this->privateKey = openssl_pkey_get_private($p12);
} else {
// This throws on error
$certs = array();
if (!openssl_pkcs12_read($p12, $certs, $password)) {
throw new W3TCG_Google_Auth_Exception(
"Unable to parse the p12 file. " .
"Is this a .p12 file? Is the password correct? OpenSSL error: " .
openssl_error_string()
);
}
// TODO(beaton): is this part of the contract for the openssl_pkcs12_read
// method? What happens if there are multiple private keys? Do we care?
if (!array_key_exists("pkey", $certs) || !$certs["pkey"]) {
throw new W3TCG_Google_Auth_Exception("No private key found in p12 file.");
}
$this->privateKey = openssl_pkey_get_private($certs['pkey']);
}
if (!$this->privateKey) {
throw new W3TCG_Google_Auth_Exception("Unable to load private key");
}
}
public function __destruct()
{
if ($this->privateKey) {
openssl_pkey_free($this->privateKey);
}
}
public function sign($data)
{
if (version_compare(PHP_VERSION, '5.3.0') < 0) {
throw new W3TCG_Google_Auth_Exception(
"PHP 5.3.0 or higher is required to use service accounts."
);
}
$hash = defined("OPENSSL_ALGO_SHA256") ? OPENSSL_ALGO_SHA256 : "sha256";
if (!openssl_sign($data, $signature, $this->privateKey, $hash)) {
throw new W3TCG_Google_Auth_Exception("Unable to sign data");
}
return $signature;
}
}

View File

@@ -0,0 +1,151 @@
<?php
/**
* File: Utils.php
*
* @package W3TC
*/
/**
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Collection of static utility methods used for convenience across
* the client library.
*
* @author Chirag Shah <chirags@google.com>
*/
class W3TCG_Google_Utils {
/**
* URL safe base 64 encoder function
*
* @param string $data URL to be base 64 encoded.
*
* @return string
*/
public static function urlSafeB64Encode( $data ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
$b64 = base64_encode( $data ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
$b64 = str_replace(
array( '+', '/', '\r', '\n', '=' ),
array( '-', '_' ),
$b64
);
return $b64;
}
/**
* URL safe base 64 de-coder function
*
* @param string $b64 Base 64 encoded string.
*
* @return string
*/
public static function urlSafeB64Decode( $b64 ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
$b64 = str_replace(
array( '-', '_' ),
array( '+', '/' ),
$b64
);
return base64_decode( $b64 ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
}
/**
* Misc function used to count the number of bytes in a post body, in the
* world of multi-byte chars and the unpredictability of
* strlen/mb_strlen/sizeof, this is the only way to do that in a sane
* manner at the moment.
*
* This algorithm was originally developed for the
* Solar Framework by Paul M. Jones
*
* @link http://solarphp.com/
* @link http://svn.solarphp.com/core/trunk/Solar/Json.php
* @link http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Json/Decoder.php
* @param string $str String to get length of.
* @return int The number of bytes in a string.
*/
public static function getStrLen( $str ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
$strlen_var = strlen( $str );
$ret = 0;
for ( $count = 0; $count < $strlen_var; ++ $count ) {
$ordinal_value = ord( $str[ $ret ] );
switch ( true ) {
case ( ( $ordinal_value >= 0x20 ) && ( $ordinal_value <= 0x7F ) ):
// characters U-00000000 - U-0000007F (same as ASCII).
$ret ++;
break;
case ( ( $ordinal_value & 0xE0 ) === 0xC0 ):
// characters U-00000080 - U-000007FF, mask 110XXXXX.
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8.
$ret += 2;
break;
case ( ( $ordinal_value & 0xF0 ) === 0xE0 ):
// characters U-00000800 - U-0000FFFF, mask 1110XXXX.
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8.
$ret += 3;
break;
case ( ( $ordinal_value & 0xF8 ) === 0xF0 ):
// characters U-00010000 - U-001FFFFF, mask 11110XXX.
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8.
$ret += 4;
break;
case ( ( $ordinal_value & 0xFC ) === 0xF8 ):
// characters U-00200000 - U-03FFFFFF, mask 111110XX.
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8.
$ret += 5;
break;
case ( ( $ordinal_value & 0xFE ) === 0xFC ):
// characters U-04000000 - U-7FFFFFFF, mask 1111110X.
// see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8.
$ret += 6;
break;
default:
$ret ++;
}
}
return $ret;
}
/**
* Normalize all keys in an array to lower-case.
*
* @param array $arr Array to normalize all keys to lowercase.
* @return array Normalized array.
*/
public static function normalize( $arr ) {
if ( ! is_array( $arr ) ) {
return array();
}
$normalized = array();
foreach ( $arr as $key => $val ) {
$normalized[ strtolower( $key ) ] = $val;
}
return $normalized;
}
/**
* Convert a string to camelCase
*
* @param string $value String to convert to camelCase.
* @return string
*/
public static function camelCase( $value ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
$value = ucwords( str_replace( array( '-', '_' ), ' ', $value ) );
$value = str_replace( ' ', '', $value );
$value[0] = strtolower( $value[0] );
return $value;
}
}

View File

@@ -0,0 +1,333 @@
<?php
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Implementation of levels 1-3 of the URI Template spec.
* @see http://tools.ietf.org/html/rfc6570
*/
class W3TCG_Google_Utils_URITemplate
{
const TYPE_MAP = "1";
const TYPE_LIST = "2";
const TYPE_SCALAR = "4";
/**
* @var $operators array
* These are valid at the start of a template block to
* modify the way in which the variables inside are
* processed.
*/
private $operators = array(
"+" => "reserved",
"/" => "segments",
"." => "dotprefix",
"#" => "fragment",
";" => "semicolon",
"?" => "form",
"&" => "continuation"
);
/**
* @var reserved array
* These are the characters which should not be URL encoded in reserved
* strings.
*/
private $reserved = array(
"=", ",", "!", "@", "|", ":", "/", "?", "#",
"[", "]",'$', "&", "'", "(", ")", "*", "+", ";"
);
private $reservedEncoded = array(
"%3D", "%2C", "%21", "%40", "%7C", "%3A", "%2F", "%3F",
"%23", "%5B", "%5D", "%24", "%26", "%27", "%28", "%29",
"%2A", "%2B", "%3B"
);
public function parse($string, array $parameters)
{
return $this->resolveNextSection($string, $parameters);
}
/**
* This function finds the first matching {...} block and
* executes the replacement. It then calls itself to find
* subsequent blocks, if any.
*/
private function resolveNextSection($string, $parameters)
{
$start = strpos($string, "{");
if ($start === false) {
return $string;
}
$end = strpos($string, "}");
if ($end === false) {
return $string;
}
$string = $this->replace($string, $start, $end, $parameters);
return $this->resolveNextSection($string, $parameters);
}
private function replace($string, $start, $end, $parameters)
{
// We know a data block will have {} round it, so we can strip that.
$data = substr($string, $start + 1, $end - $start - 1);
// If the first character is one of the reserved operators, it effects
// the processing of the stream.
if (isset($this->operators[$data[0]])) {
$op = $this->operators[$data[0]];
$data = substr($data, 1);
$prefix = "";
$prefix_on_missing = false;
switch ($op) {
case "reserved":
// Reserved means certain characters should not be URL encoded
$data = $this->replaceVars($data, $parameters, ",", null, true);
break;
case "fragment":
// Comma separated with fragment prefix. Bare values only.
$prefix = "#";
$prefix_on_missing = true;
$data = $this->replaceVars($data, $parameters, ",", null, true);
break;
case "segments":
// Slash separated data. Bare values only.
$prefix = "/";
$data =$this->replaceVars($data, $parameters, "/");
break;
case "dotprefix":
// Dot separated data. Bare values only.
$prefix = ".";
$prefix_on_missing = true;
$data = $this->replaceVars($data, $parameters, ".");
break;
case "semicolon":
// Semicolon prefixed and separated. Uses the key name
$prefix = ";";
$data = $this->replaceVars($data, $parameters, ";", "=", false, true, false);
break;
case "form":
// Standard URL format. Uses the key name
$prefix = "?";
$data = $this->replaceVars($data, $parameters, "&", "=");
break;
case "continuation":
// Standard URL, but with leading ampersand. Uses key name.
$prefix = "&";
$data = $this->replaceVars($data, $parameters, "&", "=");
break;
}
// Add the initial prefix character if data is valid.
if ($data || ($data !== false && $prefix_on_missing)) {
$data = $prefix . $data;
}
} else {
// If no operator we replace with the defaults.
$data = $this->replaceVars($data, $parameters);
}
// This is chops out the {...} and replaces with the new section.
return substr($string, 0, $start) . $data . substr($string, $end + 1);
}
private function replaceVars(
$section,
$parameters,
$sep = ",",
$combine = null,
$reserved = false,
$tag_empty = false,
$combine_on_empty = true
) {
if (strpos($section, ",") === false) {
// If we only have a single value, we can immediately process.
return $this->combine(
$section,
$parameters,
$sep,
$combine,
$reserved,
$tag_empty,
$combine_on_empty
);
} else {
// If we have multiple values, we need to split and loop over them.
// Each is treated individually, then glued together with the
// separator character.
$vars = explode(",", $section);
return $this->combineList(
$vars,
$sep,
$parameters,
$combine,
$reserved,
false, // Never emit empty strings in multi-param replacements
$combine_on_empty
);
}
}
public function combine(
$key,
$parameters,
$sep,
$combine,
$reserved,
$tag_empty,
$combine_on_empty
) {
$length = false;
$explode = false;
$skip_final_combine = false;
$value = false;
// Check for length restriction.
if (strpos($key, ":") !== false) {
list($key, $length) = explode(":", $key);
}
// Check for explode parameter.
if ($key[strlen($key) - 1] == "*") {
$explode = true;
$key = substr($key, 0, -1);
$skip_final_combine = true;
}
// Define the list separator.
$list_sep = $explode ? $sep : ",";
if (isset($parameters[$key])) {
$data_type = $this->getDataType($parameters[$key]);
switch($data_type) {
case self::TYPE_SCALAR:
$value = $this->getValue($parameters[$key], $length);
break;
case self::TYPE_LIST:
$values = array();
foreach ($parameters[$key] as $pkey => $pvalue) {
$pvalue = $this->getValue($pvalue, $length);
if ($combine && $explode) {
$values[$pkey] = $key . $combine . $pvalue;
} else {
$values[$pkey] = $pvalue;
}
}
$value = implode($list_sep, $values);
if ($value == '') {
return '';
}
break;
case self::TYPE_MAP:
$values = array();
foreach ($parameters[$key] as $pkey => $pvalue) {
$pvalue = $this->getValue($pvalue, $length);
if ($explode) {
$pkey = $this->getValue($pkey, $length);
$values[] = $pkey . "=" . $pvalue; // Explode triggers = combine.
} else {
$values[] = $pkey;
$values[] = $pvalue;
}
}
$value = implode($list_sep, $values);
if ($value == '') {
return false;
}
break;
}
} else if ($tag_empty) {
// If we are just indicating empty values with their key name, return that.
return $key;
} else {
// Otherwise we can skip this variable due to not being defined.
return false;
}
if ($reserved) {
$value = str_replace($this->reservedEncoded, $this->reserved, $value);
}
// If we do not need to include the key name, we just return the raw
// value.
if (!$combine || $skip_final_combine) {
return $value;
}
// Else we combine the key name: foo=bar, if value is not the empty string.
return $key . ($value != '' || $combine_on_empty ? $combine . $value : '');
}
/**
* Return the type of a passed in value
*/
private function getDataType($data)
{
if (is_array($data)) {
reset($data);
if (key($data) !== 0) {
return self::TYPE_MAP;
}
return self::TYPE_LIST;
}
return self::TYPE_SCALAR;
}
/**
* Utility function that merges multiple combine calls
* for multi-key templates.
*/
private function combineList(
$vars,
$sep,
$parameters,
$combine,
$reserved,
$tag_empty,
$combine_on_empty
) {
$ret = array();
foreach ($vars as $var) {
$response = $this->combine(
$var,
$parameters,
$sep,
$combine,
$reserved,
$tag_empty,
$combine_on_empty
);
if ($response === false) {
continue;
}
$ret[] = $response;
}
return implode($sep, $ret);
}
/**
* Utility function to encode and trim values
*/
private function getValue($value, $length)
{
if ($length) {
$value = substr($value, 0, $length);
}
$value = rawurlencode($value);
return $value;
}
}

View File

@@ -0,0 +1,30 @@
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Verifies signatures.
*
* @author Brian Eaton <beaton@google.com>
*/
abstract class W3TCG_Google_Verifier_Abstract
{
/**
* Checks a signature, returns true if the signature is correct,
* false otherwise.
*/
abstract public function verify($data, $signature);
}

View File

@@ -0,0 +1,71 @@
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Verifies signatures using PEM encoded certificates.
*
* @author Brian Eaton <beaton@google.com>
*/
class W3TCG_Google_Verifier_Pem extends W3TCG_Google_Verifier_Abstract
{
private $publicKey;
/**
* Constructs a verifier from the supplied PEM-encoded certificate.
*
* $pem: a PEM encoded certificate (not a file).
* @param $pem
* @throws W3TCG_Google_Auth_Exception
* @throws W3TCG_Google_Exception
*/
public function __construct($pem)
{
if (!function_exists('openssl_x509_read')) {
throw new W3TCG_Google_Exception('Google API PHP client needs the openssl PHP extension');
}
$this->publicKey = openssl_x509_read($pem);
if (!$this->publicKey) {
throw new W3TCG_Google_Auth_Exception("Unable to parse PEM: $pem");
}
}
public function __destruct()
{
if ($this->publicKey) {
openssl_x509_free($this->publicKey);
}
}
/**
* Verifies the signature on data.
*
* Returns true if the signature is valid, false otherwise.
* @param $data
* @param $signature
* @throws W3TCG_Google_Auth_Exception
* @return bool
*/
public function verify($data, $signature)
{
$hash = defined("OPENSSL_ALGO_SHA256") ? OPENSSL_ALGO_SHA256 : "sha256";
$status = openssl_verify($data, $signature, $this->publicKey, $hash);
if ($status === -1) {
throw new W3TCG_Google_Auth_Exception('Signature verification error: ' . openssl_error_string());
}
return $status === 1;
}
}

View File

@@ -0,0 +1,416 @@
<?php
namespace W3TCL\Minify;
/**
* Class HTTP_ConditionalGet
* @package Minify
* @subpackage HTTP
*/
/**
* Implement conditional GET via a timestamp or hash of content
*
* E.g. Content from DB with update time:
* <code>
* list($updateTime, $content) = getDbUpdateAndContent();
* $cg = new HTTP_ConditionalGet(array(
* 'lastModifiedTime' => $updateTime
* ,'isPublic' => true
* ));
* $cg->sendHeaders();
* if ($cg->cacheIsValid) {
* exit();
* }
* echo $content;
* </code>
*
* E.g. Shortcut for the above
* <code>
* HTTP_ConditionalGet::check($updateTime, true); // exits if client has cache
* echo $content;
* </code>
*
* E.g. Content from DB with no update time:
* <code>
* $content = getContentFromDB();
* $cg = new HTTP_ConditionalGet(array(
* 'contentHash' => md5($content)
* ));
* $cg->sendHeaders();
* if ($cg->cacheIsValid) {
* exit();
* }
* echo $content;
* </code>
*
* E.g. Static content with some static includes:
* <code>
* // before content
* $cg = new HTTP_ConditionalGet(array(
* 'lastUpdateTime' => max(
* filemtime(__FILE__)
* ,filemtime('/path/to/header.inc')
* ,filemtime('/path/to/footer.inc')
* )
* ));
* $cg->sendHeaders();
* if ($cg->cacheIsValid) {
* exit();
* }
* </code>
* @package Minify
* @subpackage HTTP
* @author Stephen Clay <steve@mrclay.org>
*/
class HTTP_ConditionalGet {
/**
* Does the client have a valid copy of the requested resource?
*
* You'll want to check this after instantiating the object. If true, do
* not send content, just call sendHeaders() if you haven't already.
*
* @var bool
*/
public $cacheIsValid = null;
/**
* @param array $spec options
*
* 'isPublic': (bool) if false, the Cache-Control header will contain
* "private", allowing only browser caching. (default false)
*
* 'lastModifiedTime': (int) if given, both ETag AND Last-Modified headers
* will be sent with content. This is recommended.
*
* 'encoding': (string) if set, the header "Vary: Accept-Encoding" will
* always be sent and a truncated version of the encoding will be appended
* to the ETag. E.g. "pub123456;gz". This will also trigger a more lenient
* checking of the client's If-None-Match header, as the encoding portion of
* the ETag will be stripped before comparison.
*
* 'contentHash': (string) if given, only the ETag header can be sent with
* content (only HTTP1.1 clients can conditionally GET). The given string
* should be short with no quote characters and always change when the
* resource changes (recommend md5()). This is not needed/used if
* lastModifiedTime is given.
*
* 'eTag': (string) if given, this will be used as the ETag header rather
* than values based on lastModifiedTime or contentHash. Also the encoding
* string will not be appended to the given value as described above.
*
* 'invalidate': (bool) if true, the client cache will be considered invalid
* without testing. Effectively this disables conditional GET.
* (default false)
*
* 'maxAge': (int) if given, this will set the Cache-Control max-age in
* seconds, and also set the Expires header to the equivalent GMT date.
* After the max-age period has passed, the browser will again send a
* conditional GET to revalidate its cache.
*/
public function __construct($spec)
{
if (isset($spec['cacheHeaders']) && is_array($spec['cacheHeaders'])) {
$this->_cacheHeaders = $spec['cacheHeaders'];
}
$scope = ($this->_cacheHeaders['cacheheaders_enabled'] && $this->_cacheHeaders['cacheheaders'] != 'no_cache') ? 'public' : 'private';
$maxAge = 0;
$this->_headers['Pragma'] = $scope;
// For backwards compatibility (will be removed in the future)
if (isset($spec['setExpires'])
&& is_numeric($spec['setExpires'])
&& ! isset($spec['maxAge'])) {
$spec['maxAge'] = $spec['setExpires'] - ( isset( $_SERVER['REQUEST_TIME'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_TIME'] ) ) : '' );
}
if (isset($spec['maxAge']) && $this->_cacheHeaders['expires_enabled'] && $spec['maxAge']) {
$maxAge = $spec['maxAge'];
$this->_headers['Expires'] = self::gmtDate(
( isset( $_SERVER['REQUEST_TIME'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_TIME'] ) ) : '' ) + $spec['maxAge']
);
}
$etagAppend = '';
if (isset($spec['encoding'])) {
$this->_stripEtag = true;
$this->_headers['Vary'] = 'Accept-Encoding';
if ('' !== $spec['encoding']) {
if (0 === strpos($spec['encoding'], 'x-')) {
$spec['encoding'] = substr($spec['encoding'], 2);
}
$etagAppend = ';' . substr($spec['encoding'], 0, 2);
}
}
if (isset($spec['lastModifiedTime'])) {
$this->_setLastModified($spec['lastModifiedTime']);
if (isset($spec['eTag'])) { // Use it
$this->_setEtag($spec['eTag'], $scope);
} else { // base both headers on time
$this->_setEtag($spec['lastModifiedTime'] . $etagAppend, $scope);
}
} elseif (isset($spec['eTag'])) { // Use it
$this->_setEtag($spec['eTag'], $scope);
} elseif (isset($spec['contentHash'])) { // Use the hash as the ETag
$this->_setEtag($spec['contentHash'] . $etagAppend, $scope);
}
if ($this->_cacheHeaders['cacheheaders_enabled']) {
switch ($this->_cacheHeaders['cacheheaders']) {
case 'cache':
$this->_headers['Cache-Control'] = 'public';
break;
case 'cache_public_maxage':
$this->_headers['Cache-Control'] = "max-age={$maxAge}, public";
break;
case 'cache_validation':
$this->_headers['Cache-Control'] = 'public, must-revalidate, proxy-revalidate';
break;
case 'cache_noproxy':
$this->_headers['Cache-Control'] = 'private, must-revalidate';
break;
case 'cache_maxage':
$this->_headers['Cache-Control'] = "max-age={$maxAge}, {$scope}, must-revalidate, proxy-revalidate";
break;
case 'no_cache':
$this->_headers['Cache-Control'] = 'max-age=0, private, no-store, no-cache, must-revalidate';
break;
}
}
/**
* Disable caching for preview mode
*/
if (\W3TC\Util_Environment::is_preview_mode()) {
$this->_headers = array_merge($this->_headers, array(
'Pragma' => 'private',
'Cache-Control' => 'private'
));
}
// invalidate cache if disabled, otherwise check
$this->cacheIsValid = (isset($spec['invalidate']) && $spec['invalidate'])
? false
: $this->_isCacheValid();
}
/**
* Get array of output headers to be sent
*
* In the case of 304 responses, this array will only contain the response
* code header: array('_responseCode' => 'HTTP/1.0 304 Not Modified')
*
* Otherwise something like:
* <code>
* array(
* 'Cache-Control' => 'max-age=0, public'
* ,'ETag' => '"foobar"'
* )
* </code>
*
* @return array
*/
public function getHeaders()
{
return $this->_headers;
}
/**
* Set the Content-Length header in bytes
*
* With most PHP configs, as long as you don't flush() output, this method
* is not needed and PHP will buffer all output and set Content-Length for
* you. Otherwise you'll want to call this to let the client know up front.
*
* @param int $bytes
*
* @return int copy of input $bytes
*/
public function setContentLength($bytes)
{
return $this->_headers['Content-Length'] = $bytes;
}
/**
* Send headers
*
* @see getHeaders()
*
* Note this doesn't "clear" the headers. Calling sendHeaders() will
* call header() again (but probably have not effect) and getHeaders() will
* still return the headers.
*
* @return null
*/
public function sendHeaders()
{
$headers = $this->_headers;
if (array_key_exists('_responseCode', $headers)) {
// FastCGI environments require 3rd arg to header() to be set
list(, $code) = explode(' ', $headers['_responseCode'], 3);
header($headers['_responseCode'], true, $code);
unset($headers['_responseCode']);
}
foreach ($headers as $name => $val) {
header($name . ': ' . $val);
}
}
/**
* Exit if the client's cache is valid for this resource
*
* This is a convenience method for common use of the class
*
* @param int $lastModifiedTime if given, both ETag AND Last-Modified headers
* will be sent with content. This is recommended.
*
* @param bool $isPublic (default false) if true, the Cache-Control header
* will contain "public", allowing proxies to cache the content. Otherwise
* "private" will be sent, allowing only browser caching.
*
* @param array $options (default empty) additional options for constructor
*/
public static function check($lastModifiedTime = null, $isPublic = false, $options = array())
{
if (null !== $lastModifiedTime) {
$options['lastModifiedTime'] = (int)$lastModifiedTime;
}
$options['isPublic'] = (bool)$isPublic;
$cg = new HTTP_ConditionalGet($options);
$cg->sendHeaders();
if ($cg->cacheIsValid) {
exit();
}
}
/**
* Get a GMT formatted date for use in HTTP headers
*
* <code>
* header('Expires: ' . HTTP_ConditionalGet::gmtdate($time));
* </code>
*
* @param int $time unix timestamp
*
* @return string
*/
public static function gmtDate($time)
{
return gmdate('D, d M Y H:i:s \G\M\T', $time);
}
protected $_headers = array();
protected $_lmTime = null;
protected $_etag = null;
protected $_stripEtag = false;
protected $_cacheHeaders = array(
'use_etag' => true,
'expires_enabled' => true,
'cacheheaders_enabled' => true,
'cacheheaders' => 'cache_validation'
);
/**
* @param string $hash
*
* @param string $scope
*/
protected function _setEtag($hash, $scope)
{
$this->_etag = '"' . substr($scope, 0, 3) . $hash . '"';
if ($this->_cacheHeaders['use_etag'])
$this->_headers['ETag'] = $this->_etag;
}
/**
* @param int $time
*/
protected function _setLastModified($time)
{
$this->_lmTime = (int)$time;
$this->_headers['Last-Modified'] = self::gmtDate($time);
}
/**
* Determine validity of client cache and queue 304 header if valid
*
* @return bool
*/
protected function _isCacheValid()
{
if (null === $this->_etag) {
// lmTime is copied to ETag, so this condition implies that the
// server sent neither ETag nor Last-Modified, so the client can't
// possibly has a valid cache.
return false;
}
$isValid = ($this->resourceMatchedEtag() || $this->resourceNotModified());
if ($isValid) {
$this->_headers['_responseCode'] = 'HTTP/1.0 304 Not Modified';
}
return $isValid;
}
/**
* @return bool
*/
protected function resourceMatchedEtag()
{
if (!isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
return false;
}
$clientEtagList = sanitize_text_field( wp_unslash( $_SERVER['HTTP_IF_NONE_MATCH'] ) );
$clientEtags = explode(',', $clientEtagList);
$compareTo = $this->normalizeEtag($this->_etag);
foreach ($clientEtags as $clientEtag) {
if ($this->normalizeEtag($clientEtag) === $compareTo) {
// respond with the client's matched ETag, even if it's not what
// we would've sent by default
if ($this->_cacheHeaders['use_etag'])
$this->_headers['ETag'] = trim($clientEtag);
return true;
}
}
return false;
}
/**
* @param string $etag
*
* @return string
*/
protected function normalizeEtag($etag) {
$etag = trim($etag);
return $this->_stripEtag
? preg_replace('/;\\w\\w"$/', '"', $etag)
: $etag;
}
/**
* @return bool
*/
protected function resourceNotModified()
{
if (!isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
return false;
}
// strip off IE's extra data (semicolon)
list($ifModifiedSince) = explode(';', sanitize_text_field( wp_unslash( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ), 2);
if (strtotime($ifModifiedSince) >= $this->_lmTime) {
// Apache 2.2's behavior. If there was no ETag match, send the
// non-encoded version of the ETag value.
if ($this->_cacheHeaders['use_etag'])
$this->_headers['ETag'] = $this->normalizeEtag($this->_etag);
return true;
}
return false;
}
}

View File

@@ -0,0 +1,343 @@
<?php
namespace W3TCL\Minify;
/**
* Class HTTP_Encoder
* @package Minify
* @subpackage HTTP
*/
/**
* Encode and send gzipped/deflated content
*
* The "Vary: Accept-Encoding" header is sent. If the client allows encoding,
* Content-Encoding and Content-Length are added.
*
* <code>
* // Send a CSS file, compressed if possible
* $he = new HTTP_Encoder(array(
* 'content' => file_get_contents($cssFile)
* ,'type' => 'text/css'
* ));
* $he->encode();
* $he->sendAll();
* </code>
*
* <code>
* // Shortcut to encoding output
* header('Content-Type: text/css'); // needed if not HTML
* HTTP_Encoder::output($css);
* </code>
*
* <code>
* // Just sniff for the accepted encoding
* $encoding = HTTP_Encoder::getAcceptedEncoding();
* </code>
*
* For more control over headers, use getHeaders() and getData() and send your
* own output.
*
* Note: If you don't need header mgmt, use PHP's native gzencode, gzdeflate,
* and gzcompress functions for gzip, deflate, and compress-encoding
* respectively.
*
* @package Minify
* @subpackage HTTP
* @author Stephen Clay <steve@mrclay.org>
*/
class HTTP_Encoder {
/**
* Should the encoder allow HTTP encoding to IE6?
*
* If you have many IE6 users and the bandwidth savings is worth troubling
* some of them, set this to true.
*
* By default, encoding is only offered to IE7+. When this is true,
* getAcceptedEncoding() will return an encoding for IE6 if its user agent
* string contains "SV1". This has been documented in many places as "safe",
* but there seem to be remaining, intermittent encoding bugs in patched
* IE6 on the wild web.
*
* @var bool
*/
public static $encodeToIe6 = true;
/**
* Default compression level for zlib operations
*
* This level is used if encode() is not given a $compressionLevel
*
* @var int
*/
public static $compressionLevel = 6;
/**
* Get an HTTP Encoder object
*
* @param array $spec options
*
* 'content': (string required) content to be encoded
*
* 'type': (string) if set, the Content-Type header will have this value.
*
* 'method: (string) only set this if you are forcing a particular encoding
* method. If not set, the best method will be chosen by getAcceptedEncoding()
* The available methods are 'gzip', 'deflate', 'compress', and '' (no
* encoding)
*/
public function __construct($spec)
{
$this->_useMbStrlen = (function_exists('mb_strlen')
&& (ini_get('mbstring.func_overload') !== '')
&& ((int)ini_get('mbstring.func_overload') & 2));
$this->_content = $spec['content'];
$this->_headers['Content-Length'] = $this->_useMbStrlen
? (string)mb_strlen($this->_content, '8bit')
: (string)strlen($this->_content);
if (isset($spec['type'])) {
$this->_headers['Content-Type'] = $spec['type'];
}
if (isset($spec['method'])
&& in_array($spec['method'], array('gzip', 'deflate', '')))
{
$this->_encodeMethod = array($spec['method'], $spec['method']);
} else {
$this->_encodeMethod = self::getAcceptedEncoding();
}
}
/**
* Get content in current form
*
* Call after encode() for encoded content.
*
* @return string
*/
public function getContent()
{
return $this->_content;
}
/**
* Get array of output headers to be sent
*
* E.g.
* <code>
* array(
* 'Content-Length' => '615'
* ,'Content-Encoding' => 'x-gzip'
* ,'Vary' => 'Accept-Encoding'
* )
* </code>
*
* @return array
*/
public function getHeaders()
{
return $this->_headers;
}
/**
* Send output headers
*
* You must call this before headers are sent and it probably cannot be
* used in conjunction with zlib output buffering / mod_gzip. Errors are
* not handled purposefully.
*
* @see getHeaders()
*/
public function sendHeaders()
{
foreach ($this->_headers as $name => $val) {
header($name . ': ' . $val);
}
}
/**
* Send output headers and content
*
* A shortcut for sendHeaders() and echo getContent()
*
* You must call this before headers are sent and it probably cannot be
* used in conjunction with zlib output buffering / mod_gzip. Errors are
* not handled purposefully.
*/
public function sendAll()
{
$this->sendHeaders();
echo wp_kses(
$this->_content,
Util_Ui::get_allowed_html_for_wp_kses_from_content( $this->_content )
);
}
/**
* Determine the client's best encoding method from the HTTP Accept-Encoding
* header.
*
* If no Accept-Encoding header is set, or the browser is IE before v6 SP2,
* this will return ('', ''), the "identity" encoding.
*
* A syntax-aware scan is done of the Accept-Encoding, so the method must
* be non 0. The methods are favored in order of gzip, deflate, then
* compress. Deflate is always smallest and generally faster, but is
* rarely sent by servers, so client support could be buggier.
*
* @param bool $allowCompress allow the older compress encoding
*
* @param bool $allowDeflate allow the more recent deflate encoding
*
* @return array two values, 1st is the actual encoding method, 2nd is the
* alias of that method to use in the Content-Encoding header (some browsers
* call gzip "x-gzip" etc.)
*/
public static function getAcceptedEncoding($allowCompress = true, $allowDeflate = true)
{
// @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
if (! isset($_SERVER['HTTP_ACCEPT_ENCODING'])
|| \W3TC\Util_Environment::is_zlib_enabled()
|| headers_sent()
|| self::isBuggyIe())
{
return array('', '');
}
$ae = sanitize_text_field( wp_unslash( $_SERVER['HTTP_ACCEPT_ENCODING'] ) );
// brotli checks (quick)
if (0 === strpos($ae, 'br,')
|| strpos($ae, ', br') === strlen($ae) - 4
) {
if (function_exists('brotli_compress'))
return array('br', 'br');
}
// brotli checks (slow)
if (preg_match(
'@(?:^|,)\\s*((?:x-)?br)\\s*(?:$|,|;\\s*q=(?:0\\.|1))@'
,$ae
,$m)) {
if (function_exists('brotli_compress'))
return array('br', $m[1]);
}
// gzip checks (quick)
if (0 === strpos($ae, 'gzip,') // most browsers
|| 0 === strpos($ae, 'deflate, gzip,') // opera
) {
if (function_exists('gzencode'))
return array('gzip', 'gzip');
}
// gzip checks (slow)
if (preg_match(
'@(?:^|,)\\s*((?:x-)?gzip)\\s*(?:$|,|;\\s*q=(?:0\\.|1))@'
,$ae
,$m)) {
return array('gzip', $m[1]);
}
return array('', '');
}
/**
* Encode (compress) the content
*
* If the encode method is '' (none) or compression level is 0, or the 'zlib'
* extension isn't loaded, we return false.
*
* Then the appropriate gz_* function is called to compress the content. If
* this fails, false is returned.
*
* The header "Vary: Accept-Encoding" is added. If encoding is successful,
* the Content-Length header is updated, and Content-Encoding is also added.
*
* @param int $compressionLevel given to zlib functions. If not given, the
* class default will be used.
*
* @return bool success true if the content was actually compressed
*/
public function encode($compressionLevel = null)
{
if (! self::isBuggyIe()) {
$this->_headers['Vary'] = 'Accept-Encoding';
}
if (null === $compressionLevel) {
$compressionLevel = self::$compressionLevel;
}
if ('' === $this->_encodeMethod[0]
|| ($compressionLevel == 0)
|| !extension_loaded('zlib'))
{
return false;
}
if ($this->_encodeMethod[0] === 'br') {
$encoded = brotli_compress($this->_content);
} elseif ($this->_encodeMethod[0] === 'deflate') {
$encoded = gzdeflate($this->_content, $compressionLevel);
} elseif ($this->_encodeMethod[0] === 'gzip') {
$encoded = gzencode($this->_content, $compressionLevel);
} else {
$encoded = gzcompress($this->_content, $compressionLevel);
}
if (false === $encoded) {
return false;
}
$this->_headers['Content-Length'] = $this->_useMbStrlen
? (string)mb_strlen($encoded, '8bit')
: (string)strlen($encoded);
$this->_headers['Content-Encoding'] = $this->_encodeMethod[1];
$this->_content = $encoded;
return true;
}
/**
* Encode and send appropriate headers and content
*
* This is a convenience method for common use of the class
*
* @param string $content
*
* @param int $compressionLevel given to zlib functions. If not given, the
* class default will be used.
*
* @return bool success true if the content was actually compressed
*/
public static function output($content, $compressionLevel = null)
{
if (null === $compressionLevel) {
$compressionLevel = self::$compressionLevel;
}
$he = new HTTP_Encoder(array('content' => $content));
$ret = $he->encode($compressionLevel);
$he->sendAll();
return $ret;
}
/**
* Is the browser an IE version earlier than 6 SP2?
*
* @return bool
*/
public static function isBuggyIe()
{
if (empty($_SERVER['HTTP_USER_AGENT'])) {
return false;
}
$ua = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) );
// quick escape for non-IEs
if (0 !== strpos($ua, 'Mozilla/4.0 (compatible; MSIE ')
|| false !== strpos($ua, 'Opera')) {
return false;
}
// no regex = faaast
$version = (float)substr($ua, 30);
return self::$encodeToIe6
? ($version < 6 || ($version == 6 && false === strpos($ua, 'SV1')))
: ($version < 7);
}
protected $_content = '';
protected $_headers = array();
protected $_encodeMethod = array('', '');
protected $_useMbStrlen = false;
}

View File

@@ -0,0 +1,465 @@
<?php
namespace W3TCL\Minify;
/**
* JSMin.php - modified PHP implementation of Douglas Crockford's JSMin.
*
* <code>
* $minifiedJs = JSMin::minify($js);
* </code>
*
* This is a modified port of jsmin.c. Improvements:
*
* Does not choke on some regexp literals containing quote characters. E.g. /'/
*
* Spaces are preserved after some add/sub operators, so they are not mistakenly
* converted to post-inc/dec. E.g. a + ++b -> a+ ++b
*
* Preserves multi-line comments that begin with /*!
*
* PHP 5 or higher is required.
*
* Permission is hereby granted to use this version of the library under the
* same terms as jsmin.c, which has the following license:
*
* --
* Copyright (c) 2002 Douglas Crockford (www.crockford.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* The Software shall be used for Good, not Evil.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* --
*
* @package JSMin
* @author Ryan Grove <ryan@wonko.com> (PHP port)
* @author Steve Clay <steve@mrclay.org> (modifications + cleanup)
* @author Andrea Giammarchi <http://www.3site.eu> (spaceBeforeRegExp)
* @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
* @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)
* @license http://opensource.org/licenses/mit-license.php MIT License
* @link http://code.google.com/p/jsmin-php/
*/
class JSMin {
const ACTION_KEEP_A = 1;
const ACTION_DELETE_A = 2;
const ACTION_DELETE_A_B = 3;
protected $a = "\n";
protected $b = '';
protected $input = '';
protected $inputIndex = 0;
protected $inputLength = 0;
protected $lookAhead = null;
protected $output = '';
protected $lastByteOut = '';
protected $keptComment = '';
/**
* Minify Javascript.
*
* @param string $js Javascript to be minified
*
* @return string
*/
public static function minify($js)
{
$jsmin = new JSMin($js);
return $jsmin->min();
}
/**
* @param string $input
*/
public function __construct($input)
{
$this->input = $input;
}
/**
* Perform minification, return result
*
* @return string
*/
public function min()
{
if ($this->output !== '') { // min already run
return $this->output;
}
$mbIntEnc = null;
if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
$mbIntEnc = mb_internal_encoding();
mb_internal_encoding('8bit');
}
if (isset($this->input[0]) && $this->input[0] === "\xef") {
$this->input = substr($this->input, 3);
}
$this->input = str_replace("\r\n", "\n", $this->input);
$this->inputLength = strlen($this->input);
$this->action(self::ACTION_DELETE_A_B);
while ($this->a !== null) {
// determine next command
$command = self::ACTION_KEEP_A; // default
if ($this->isWhiteSpace($this->a)) {
if (($this->lastByteOut === '+' || $this->lastByteOut === '-')
&& ($this->b === $this->lastByteOut)) {
// Don't delete this space. If we do, the addition/subtraction
// could be parsed as a post-increment
} elseif (! $this->isAlphaNum($this->b)) {
$command = self::ACTION_DELETE_A;
}
} elseif ($this->isLineTerminator($this->a)) {
if ($this->isWhiteSpace($this->b)) {
$command = self::ACTION_DELETE_A_B;
// in case of mbstring.func_overload & 2, must check for null b,
// otherwise mb_strpos will give WARNING
} elseif ($this->b === null
|| (false === strpos('{[(+-!~#', $this->b)
&& ! $this->isAlphaNum($this->b))) {
$command = self::ACTION_DELETE_A;
}
} elseif (! $this->isAlphaNum($this->a)) {
if ($this->isWhiteSpace($this->b)
|| ($this->isLineTerminator($this->b)
&& (false === strpos('}])+-"\'`', $this->a)))) {
$command = self::ACTION_DELETE_A_B;
}
}
$this->action($command);
}
$this->output = trim($this->output);
if ($mbIntEnc !== null) {
mb_internal_encoding($mbIntEnc);
}
return $this->output;
}
/**
* ACTION_KEEP_A = Output A. Copy B to A. Get the next B.
* ACTION_DELETE_A = Copy B to A. Get the next B.
* ACTION_DELETE_A_B = Get the next B.
*
* @param int $command
* @throws UnterminatedRegExpException|UnterminatedStringException
*/
protected function action($command)
{
// make sure we don't compress "a + ++b" to "a+++b", etc.
if ($command === self::ACTION_DELETE_A_B
&& $this->b === ' '
&& ($this->a === '+' || $this->a === '-')) {
// Note: we're at an addition/substraction operator; the inputIndex
// will certainly be a valid index
if ($this->input[$this->inputIndex] === $this->a) {
// This is "+ +" or "- -". Don't delete the space.
$command = self::ACTION_KEEP_A;
}
}
switch ($command) {
case self::ACTION_KEEP_A: // 1
$this->output .= $this->a;
if ($this->keptComment) {
$this->output = rtrim($this->output, "\n");
$this->output .= $this->keptComment;
$this->keptComment = '';
}
$this->lastByteOut = $this->a;
// fallthrough intentional
case self::ACTION_DELETE_A: // 2
$this->a = $this->b;
if ($this->a === "'" || $this->a === '"' || $this->a === '`') { // string/template literal
$delimiter = $this->a;
$str = $this->a; // in case needed for exception
for(;;) {
$this->output .= $this->a;
$this->lastByteOut = $this->a;
$this->a = $this->get();
if ($this->a === $this->b) { // end quote
break;
}
if ($delimiter === '`' && $this->isLineTerminator($this->a)) {
// leave the newline
} elseif ($this->isEOF($this->a)) {
$byte = $this->inputIndex - 1;
throw new JSMin_UnterminatedStringException(
"JSMin: Unterminated String at byte {$byte}: {$str}");
}
$str .= $this->a;
if ($this->a === '\\') {
$this->output .= $this->a;
$this->lastByteOut = $this->a;
$this->a = $this->get();
$str .= $this->a;
}
}
}
// fallthrough intentional
case self::ACTION_DELETE_A_B: // 3
$this->b = $this->next();
if ($this->b === '/' && $this->isRegexpLiteral()) {
$this->output .= $this->a . $this->b;
$pattern = '/'; // keep entire pattern in case we need to report it in the exception
for(;;) {
$this->a = $this->get();
$pattern .= $this->a;
if ($this->a === '[') {
for(;;) {
$this->output .= $this->a;
$this->a = $this->get();
$pattern .= $this->a;
if ($this->a === ']') {
break;
}
if ($this->a === '\\') {
$this->output .= $this->a;
$this->a = $this->get();
$pattern .= $this->a;
}
if ($this->isEOF($this->a)) {
throw new JSMin_UnterminatedRegExpException(
"JSMin: Unterminated set in RegExp at byte "
. $this->inputIndex .": {$pattern}");
}
}
}
if ($this->a === '/') { // end pattern
break; // while (true)
} elseif ($this->a === '\\') {
$this->output .= $this->a;
$this->a = $this->get();
$pattern .= $this->a;
} elseif ($this->isEOF($this->a)) {
$byte = $this->inputIndex - 1;
throw new JSMin_UnterminatedRegExpException(
"JSMin: Unterminated RegExp at byte {$byte}: {$pattern}");
}
$this->output .= $this->a;
$this->lastByteOut = $this->a;
}
$this->b = $this->next();
}
// end case ACTION_DELETE_A_B
}
}
/**
* @return bool
*/
protected function isRegexpLiteral()
{
if (false !== strpos("(,=:[!&|?+-~*{;", $this->a)) {
// we can't divide after these tokens
return true;
}
// check if first non-ws token is "/" (see starts-regex.js)
$length = strlen($this->output);
if ($this->isWhiteSpace($this->a) || $this->isLineTerminator($this->a)) {
if ($length < 2) { // weird edge case
return true;
}
}
// if the "/" follows a keyword, it must be a regexp, otherwise it's best to assume division
$subject = $this->output . trim($this->a);
if (!preg_match('/(?:case|else|in|return|typeof)$/', $subject, $m)) {
// not a keyword
return false;
}
// can't be sure it's a keyword yet (see not-regexp.js)
$charBeforeKeyword = substr($subject, 0 - strlen($m[0]) - 1, 1);
if ($this->isAlphaNum($charBeforeKeyword)) {
// this is really an identifier ending in a keyword, e.g. "xreturn"
return false;
}
// it's a regexp. Remove unneeded whitespace after keyword
if ($this->isWhiteSpace($this->a) || $this->isLineTerminator($this->a)) {
$this->a = '';
}
return true;
}
/**
* Return the next character from stdin. Watch out for lookahead. If the character is a control character,
* translate it to a space or linefeed.
*
* @return string
*/
protected function get()
{
$c = $this->lookAhead;
$this->lookAhead = null;
if ($c === null) {
// getc(stdin)
if ($this->inputIndex < $this->inputLength) {
$c = $this->input[$this->inputIndex];
$this->inputIndex += 1;
} else {
$c = null;
}
}
if ($c === "\r") {
return "\n";
}
return $c;
}
/**
* Does $a indicate end of input?
*
* @param string $a
* @return bool
*/
protected function isEOF($a)
{
return $a === null || $this->isLineTerminator($a);
}
/**
* Get next char (without getting it). If is ctrl character, translate to a space or newline.
*
* @return string
*/
protected function peek()
{
$this->lookAhead = $this->get();
return $this->lookAhead;
}
/**
* Return true if the character is a letter, digit, underscore, dollar sign, or non-ASCII character.
*
* @param string $c
*
* @return bool
*/
protected function isAlphaNum($c)
{
return (preg_match('/^[a-z0-9A-Z_\\$\\\\]$/', $c) || ord($c) > 126);
}
/**
* Consume a single line comment from input (possibly retaining it)
*/
protected function consumeSingleLineComment()
{
$comment = '';
while (true) {
$get = $this->get();
$comment .= $get;
if ($this->isEOF($get)) {
// if IE conditional comment
if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
$this->keptComment .= "/{$comment}";
}
return;
}
}
}
/**
* Consume a multiple line comment from input (possibly retaining it)
*
* @throws UnterminatedCommentException
*/
protected function consumeMultipleLineComment()
{
$this->get();
$comment = '';
for(;;) {
$get = $this->get();
if ($get === '*') {
if ($this->peek() === '/') { // end of comment reached
$this->get();
if (0 === strpos($comment, '!')) {
// preserved by YUI Compressor
if (!$this->keptComment) {
// don't prepend a newline if two comments right after one another
$this->keptComment = "\n";
}
$this->keptComment .= "/*!" . substr($comment, 1) . "*/\n";
} else if (preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
// IE conditional
$this->keptComment .= "/*{$comment}*/";
}
return;
}
} elseif ($get === null) {
throw new JSMin_UnterminatedCommentException(
"JSMin: Unterminated comment at byte {$this->inputIndex}: /*{$comment}");
}
$comment .= $get;
}
}
/**
* Get the next character, skipping over comments. Some comments may be preserved.
*
* @return string
*/
protected function next()
{
$get = $this->get();
if ($get === '/') {
switch ($this->peek()) {
case '/':
$this->consumeSingleLineComment();
$get = "\n";
break;
case '*':
$this->consumeMultipleLineComment();
$get = ' ';
break;
}
}
return $get;
}
protected function isWhiteSpace($s) {
// https://www.ecma-international.org/ecma-262/#sec-white-space
return $s !== null && strpos(" \t\v\f", $s) !== false;
}
protected function isLineTerminator($s) {
// https://www.ecma-international.org/ecma-262/#sec-line-terminators
return $s !== null && strpos("\n\r", $s) !== false;
}
}
class JSMin_UnterminatedStringException extends \Exception {}
class JSMin_UnterminatedCommentException extends \Exception {}
class JSMin_UnterminatedRegExpException extends \Exception {}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,704 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify
* @package Minify
*/
/**
* Minify - Combines, minifies, and caches JavaScript and CSS files on demand.
*
* See README for usage instructions (for now).
*
* This library was inspired by {@link mailto:flashkot@mail.ru jscsscomp by Maxim Martynyuk}
* and by the article {@link http://www.hunlock.com/blogs/Supercharged_Javascript "Supercharged JavaScript" by Patrick Hunlock}.
*
* Requires PHP 5.1.0.
* Tested on PHP 5.1.6.
*
* @package Minify
* @author Ryan Grove <ryan@wonko.com>
* @author Stephen Clay <steve@mrclay.org>
* @copyright 2008 Ryan Grove, Stephen Clay. All rights reserved.
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @link http://code.google.com/p/minify/
*/
class Minify {
const VERSION = '2.1.7';
const TYPE_CSS = 'text/css';
const TYPE_HTML = 'text/html';
// there is some debate over the ideal JS Content-Type, but this is the
// Apache default and what Yahoo! uses..
const TYPE_JS = 'application/x-javascript';
const URL_DEBUG = 'http://code.google.com/p/minify/wiki/Debugging';
/**
* How many hours behind are the file modification times of uploaded files?
*
* If you upload files from Windows to a non-Windows server, Windows may report
* incorrect mtimes for the files. Immediately after modifying and uploading a
* file, use the touch command to update the mtime on the server. If the mtime
* jumps ahead by a number of hours, set this variable to that number. If the mtime
* moves back, this should not be needed.
*
* @var int $uploaderHoursBehind
*/
public static $uploaderHoursBehind = 0;
/**
* If this string is not empty AND the serve() option 'bubbleCssImports' is
* NOT set, then serve() will check CSS files for @import declarations that
* appear too late in the combined stylesheet. If found, serve() will prepend
* the output with this warning.
*
* @var string $importWarning
*/
public static $importWarning = "/* See http://code.google.com/p/minify/wiki/CommonProblems#@imports_can_appear_in_invalid_locations_in_combined_CSS_files */\n";
/**
* Has the DOCUMENT_ROOT been set in user code?
*
* @var bool
*/
public static $isDocRootSet = false;
/**
* Cache ID
* @var string
*/
protected static $_cacheId = null;
/**
* Specify a cache object (with identical interface as Minify_Cache_File) or
* a path to use with Minify_Cache_File.
*
* If not called, Minify will not use a cache and, for each 200 response, will
* need to recombine files, minify and encode the output.
*
* @param mixed $cache object with identical interface as Minify_Cache_File or
* a directory path, or null to disable caching. (default = '')
*
* @param bool $fileLocking (default = true) This only applies if the first
* parameter is a string.
*
* @return null
*/
public static function setCache($cache = '', $fileLocking = true)
{
if (is_string($cache)) {
self::$_cache = new Minify_Cache_File($cache, $fileLocking);
} else {
self::$_cache = $cache;
}
}
/**
* Serve a request for a minified file.
*
* Here are the available options and defaults in the base controller:
*
* 'isPublic' : send "public" instead of "private" in Cache-Control
* headers, allowing shared caches to cache the output. (default true)
*
* 'quiet' : set to true to have serve() return an array rather than sending
* any headers/output (default false)
*
* 'encodeOutput' : set to false to disable content encoding, and not send
* the Vary header (default true)
*
* 'encodeMethod' : generally you should let this be determined by
* HTTP_Encoder (leave null), but you can force a particular encoding
* to be returned, by setting this to 'gzip' or '' (no encoding)
*
* 'encodeLevel' : level of encoding compression (0 to 9, default 9)
*
* 'contentTypeCharset' : appended to the Content-Type header sent. Set to a falsey
* value to remove. (default 'utf-8')
*
* 'maxAge' : set this to the number of seconds the client should use its cache
* before revalidating with the server. This sets Cache-Control: max-age and the
* Expires header. Unlike the old 'setExpires' setting, this setting will NOT
* prevent conditional GETs. Note this has nothing to do with server-side caching.
*
* 'rewriteCssUris' : If true, serve() will automatically set the 'currentDir'
* minifier option to enable URI rewriting in CSS files (default true)
*
* 'bubbleCssImports' : If true, all @import declarations in combined CSS
* files will be move to the top. Note this may alter effective CSS values
* due to a change in order. (default false)
*
* 'debug' : set to true to minify all sources with the 'Lines' controller, which
* eases the debugging of combined files. This also prevents 304 responses.
* @see Minify_Lines::minify()
*
* 'minifiers' : to override Minify's default choice of minifier function for
* a particular content-type, specify your callback under the key of the
* content-type:
* <code>
* // call customCssMinifier($css) for all CSS minification
* $options['minifiers'][Minify::TYPE_CSS] = 'customCssMinifier';
*
* // don't minify Javascript at all
* $options['minifiers'][Minify::TYPE_JS] = '';
* </code>
*
* 'minifierOptions' : to send options to the minifier function, specify your options
* under the key of the content-type. E.g. To send the CSS minifier an option:
* <code>
* // give CSS minifier array('optionName' => 'optionValue') as 2nd argument
* $options['minifierOptions'][Minify::TYPE_CSS]['optionName'] = 'optionValue';
* </code>
*
* 'contentType' : (optional) this is only needed if your file extension is not
* js/css/html. The given content-type will be sent regardless of source file
* extension, so this should not be used in a Groups config with other
* Javascript/CSS files.
*
* Any controller options are documented in that controller's setupSources() method.
*
* @param mixed $controller instance of subclass of Minify_Controller_Base or string
* name of controller. E.g. 'Files'
*
* @param array $options controller/serve options
*
* @return null|array if the 'quiet' option is set to true, an array
* with keys "success" (bool), "statusCode" (int), "content" (string), and
* "headers" (array).
*
* @throws Exception
*/
public static function serve($controller, $options = array())
{
if (! self::$isDocRootSet && 0 === stripos(PHP_OS, 'win')) {
self::setDocRoot();
}
if (is_string($controller)) {
// make $controller into object
$class = '\W3TCL\Minify\Minify_Controller_' . $controller;
$controller = new $class();
/* @var Minify_Controller_Base $controller */
}
// set up controller sources and mix remaining options with
// controller defaults
$options = $controller->setupSources($options);
$options = $controller->analyzeSources($options);
self::$_options = $controller->mixInDefaultOptions($options);
// check request validity
if (! $controller->sources) {
// invalid request!
if (! self::$_options['quiet']) {
self::_errorExit(self::$_options['badRequestHeader'], self::URL_DEBUG);
} else {
list(,$statusCode) = explode(' ', self::$_options['badRequestHeader']);
return array(
'success' => false
,'statusCode' => (int)$statusCode
,'content' => ''
,'headers' => array()
);
}
}
self::$_controller = $controller;
if (self::$_options['debug']) {
self::_setupDebug($controller->sources);
self::$_options['maxAge'] = 0;
}
// determine encoding
if (self::$_options['encodeOutput']) {
$sendVary = true;
if (self::$_options['encodeMethod'] !== null) {
// controller specifically requested this
$contentEncoding = self::$_options['encodeMethod'];
} else {
// sniff request header
// depending on what the client accepts, $contentEncoding may be
// 'x-gzip' while our internal encodeMethod is 'gzip'. Calling
// getAcceptedEncoding(false, false) leaves out compress and deflate as options.
list(self::$_options['encodeMethod'], $contentEncoding) = HTTP_Encoder::getAcceptedEncoding(false, false);
$sendVary = ! HTTP_Encoder::isBuggyIe();
}
} else {
self::$_options['encodeMethod'] = ''; // identity (no encoding)
}
// check client cache
$cgOptions = array(
'lastModifiedTime' => self::$_options['lastModifiedTime']
,'cacheHeaders' => self::$_options['cacheHeaders']
,'isPublic' => self::$_options['isPublic']
,'encoding' => self::$_options['encodeMethod']
);
if (self::$_options['maxAge'] > 0) {
$cgOptions['maxAge'] = self::$_options['maxAge'];
} elseif (self::$_options['debug']) {
$cgOptions['invalidate'] = true;
}
$cg = new HTTP_ConditionalGet($cgOptions);
if ( $cg->cacheIsValid && !self::$_options['disable_304'] &&
!defined( 'W3TC_MINIFY_CONDITIONAL_OFF' ) ) {
// client's cache is valid
if (! self::$_options['quiet']) {
self::$lastServed = array(
'fullCacheId' =>self::_getCacheId() .
(self::$_options['encodeMethod']
? '.' . self::$_options['encodeMethod']
: '')
);
$cg->sendHeaders();
return;
} else {
return array(
'success' => true
,'statusCode' => 304
,'content' => ''
,'headers' => $cg->getHeaders()
);
}
} else {
// client will need output
$headers = $cg->getHeaders();
unset($cg);
}
if (self::$_options['contentType'] === self::TYPE_CSS
&& self::$_options['rewriteCssUris']) {
foreach($controller->sources as $key => $source) {
if (self::$_options['rewriteCssUris']
&& $source->filepath
&& !isset($source->minifyOptions['currentDir'])
&& !isset($source->minifyOptions['prependRelativePath'])
) {
$source->minifyOptions['currentDir'] = dirname($source->filepath);
}
$source->minifyOptions['processCssImports'] = self::$_options['processCssImports'];
}
}
// check server cache
$content = array();
if (null !== self::$_cache && ! self::$_options['debug']) {
// using cache
// the goal is to use only the cache methods to sniff the length and
// output the content, as they do not require ever loading the file into
// memory.
$cacheId = self::_getCacheId();
$fullCacheId = (self::$_options['encodeMethod'])
? $cacheId . '.' . self::$_options['encodeMethod']
: $cacheId;
// check cache for valid entry
$cacheIsReady = self::$_cache->isValid($fullCacheId, self::$_options['lastModifiedTime']);
if ($cacheIsReady) {
$cacheContentLength = self::$_cache->getSize($fullCacheId);
} else {
// generate & cache content
try {
$content = self::_combineMinify();
} catch (\Exception $e) {
self::$_controller->log($e->getMessage());
if (! self::$_options['quiet']) {
self::_errorExit(self::$_options['errorHeader'], self::URL_DEBUG);
}
throw $e;
}
self::$_cache->store($cacheId, $content);
if (function_exists('brotli_compress') && self::$_options['encodeMethod'] === 'br' && self::$_options['encodeOutput']) {
$compressed = $content;
$compressed['content'] = brotli_compress($content['content']);
self::$_cache->store($cacheId . '_' . self::$_options['encodeMethod'],
$compressed);
}
if (function_exists('gzencode') && self::$_options['encodeMethod'] && self::$_options['encodeMethod'] !== 'br' && self::$_options['encodeOutput']) {
$compressed = $content;
$compressed['content'] = gzencode($content['content'],
self::$_options['encodeLevel']);
self::$_cache->store($cacheId . '_' . self::$_options['encodeMethod'],
$compressed);
}
}
} else {
// no cache
$cacheIsReady = false;
try {
$content = self::_combineMinify();
} catch (\Exception $e) {
self::$_controller->log($e->getMessage());
if (! self::$_options['quiet']) {
self::_errorExit(self::$_options['errorHeader'], self::URL_DEBUG);
}
throw $e;
}
}
if (! $cacheIsReady && self::$_options['encodeMethod']) {
switch (self::$_options['encodeMethod']) {
case 'gzip':
$content['content'] = gzencode($content['content'], self::$_options['encodeLevel']);
break;
case 'deflate':
$content['content'] = gzdeflate($content['content'], self::$_options['encodeLevel']);
break;
case 'br':
$content['content'] = brotli_compress($content['content']);
break;
}
// still need to encode
}
// add headers
if ( !defined( 'W3TC_MINIFY_CONTENT_LENGTH_OFF' ) ) {
$headers['Content-Length'] = $cacheIsReady
? $cacheContentLength
: ((function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2))
? mb_strlen($content['content'], '8bit')
: strlen($content['content'])
);
}
$headers['Content-Type'] = self::$_options['contentTypeCharset']
? self::$_options['contentType'] . '; charset=' . self::$_options['contentTypeCharset']
: self::$_options['contentType'];
if (self::$_options['encodeMethod'] !== '') {
$headers['Content-Encoding'] = $contentEncoding;
}
if (self::$_options['encodeOutput'] && $sendVary) {
$headers['Vary'] = 'Accept-Encoding';
}
self::$lastServed = $content;
if (! self::$_options['quiet']) {
// output headers & content
foreach ($headers as $name => $val) {
header($name . ': ' . $val);
}
if ($cacheIsReady) {
self::$lastServed['fullCacheId'] = $fullCacheId;
self::$_cache->display($fullCacheId);
} else {
echo $content['content'];
}
} else {
if ($cacheIsReady)
$content = self::$_cache->fetch($fullCacheId);
return array(
'success' => true
,'statusCode' => 200
,'content' => $content['content']
,'headers' => $headers
);
}
}
private static $lastServed = array();
public static function getUsageStatistics() {
if (isset(self::$lastServed['fullCacheId']))
self::$lastServed = self::$_cache->fetch(self::$lastServed['fullCacheId']);
if (isset(self::$lastServed['content']) &&
isset(self::$lastServed['originalLength'])) {
return array(
'content_type' => self::$_options['contentType'],
'content_original_length' => self::$lastServed['originalLength'],
'content_output_length' => strlen(self::$lastServed['content'])
);
}
return array();
}
/**
* Return combined minified content for a set of sources
*
* No internal caching will be used and the content will not be HTTP encoded.
*
* @param array $sources array of filepaths and/or Minify_Source objects
*
* @param array $options (optional) array of options for serve. By default
* these are already set: quiet = true, encodeMethod = '', lastModifiedTime = 0.
*
* @return string
*/
public static function combine($sources, $options = array())
{
$cache = self::$_cache;
self::$_cache = null;
$options = array_merge(array(
'files' => (array)$sources
,'quiet' => true
,'encodeMethod' => ''
,'lastModifiedTime' => 0
), $options);
$out = self::serve('Files', $options);
self::$_cache = $cache;
return $out['content'];
}
/**
* Set $_SERVER['DOCUMENT_ROOT']. On IIS, the value is created from SCRIPT_FILENAME and SCRIPT_NAME.
*
* @param string $docRoot value to use for DOCUMENT_ROOT
*/
public static function setDocRoot( $docRoot = '' ) {
self::$isDocRootSet = true;
$server_software = isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : '';
if ( $docRoot ) {
$_SERVER['DOCUMENT_ROOT'] = $docRoot;
} elseif ( 0 === strpos( $server_software, 'Microsoft-IIS/' ) ) {
$_SERVER['DOCUMENT_ROOT'] = substr(
isset( $_SERVER['SCRIPT_FILENAME'] ) ? esc_url_raw( wp_unslash( $_SERVER['SCRIPT_FILENAME'] ) ) : '',
0,
strlen( isset( $_SERVER['SCRIPT_FILENAME'] ) ? esc_url_raw( wp_unslash( $_SERVER['SCRIPT_FILENAME'] ) ) : '' ) - strlen( isset( $_SERVER['SCRIPT_NAME'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SCRIPT_NAME'] ) ) : '' )
);
$_SERVER['DOCUMENT_ROOT'] = rtrim( isset( $_SERVER['DOCUMENT_ROOT'] ) ? esc_url_raw( wp_unslash( $_SERVER['DOCUMENT_ROOT'] ) ) : '', '\\' );
}
}
public static $recoverableError = null;
/**
* Any Minify_Cache_* object or null (i.e. no server cache is used)
*
* @var Minify_Cache_File
*/
private static $_cache = null;
/**
* Active controller for current request
*
* @var Minify_Controller_Base
*/
protected static $_controller = null;
/**
* Options for current request
*
* @var array
*/
protected static $_options = null;
/**
* @param string $header
*
* @param string $url
*/
protected static function _errorExit($header, $url)
{
$url = htmlspecialchars($url, ENT_QUOTES);
list(,$h1) = explode(' ', $header, 2);
$h1 = htmlspecialchars($h1);
// FastCGI environments require 3rd arg to header() to be set
list(, $code) = explode(' ', $header, 3);
header($header, true, $code);
header('Content-Type: text/html; charset=utf-8');
echo '<h1>' . esc_html( $h1 ) . '</h1>';
echo '<p>Please see <a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>.</p>';
exit();
}
/**
* Set up sources to use Minify_Lines
*
* @param Minify_Source[] $sources Minify_Source instances
*/
protected static function _setupDebug($sources)
{
foreach ($sources as $source) {
$source->minifier = array('\W3TCL\Minify\Minify_Lines', 'minify');
$id = $source->getId();
$source->minifyOptions = array(
'id' => (is_file($id) ? basename($id) : $id)
);
}
}
/**
* Combines sources and minifies the result.
*
* @return string
*
* @throws Exception
*/
protected static function _combineMinify()
{
$type = self::$_options['contentType']; // ease readability
// when combining scripts, make sure all statements separated and
// trailing single line comment is terminated
$implodeSeparator = ($type === self::TYPE_JS)
? "\n;"
: '';
// allow the user to pass a particular array of options to each
// minifier (designated by type). source objects may still override
// these
$defaultOptions = isset(self::$_options['minifierOptions'][$type])
? self::$_options['minifierOptions'][$type]
: array();
// if minifier not set, default is no minification. source objects
// may still override this
$defaultMinifier = isset(self::$_options['minifiers'][$type])
? self::$_options['minifiers'][$type]
: false;
// process groups of sources with identical minifiers/options
$content = array();
$originalLength = 0;
$i = 0;
$l = count(self::$_controller->sources);
$groupToProcessTogether = array();
$lastMinifier = null;
$lastOptions = null;
do {
// get next source
$source = null;
if ($i < $l) {
$source = self::$_controller->sources[$i];
/* @var Minify_Source $source */
$sourceContent = $source->getContent();
$originalLength += strlen($sourceContent);
// allow the source to override our minifier and options
$minifier = (null !== $source->minifier)
? $source->minifier
: $defaultMinifier;
$options = (null !== $source->minifyOptions)
? array_merge($defaultOptions, $source->minifyOptions)
: $defaultOptions;
}
// do we need to process our group right now?
if ($i > 0 // yes, we have at least the first group populated
&& (
! $source // yes, we ran out of sources
|| $type === self::TYPE_CSS // yes, to process CSS individually (avoiding PCRE bugs/limits)
|| $minifier !== $lastMinifier // yes, minifier changed
|| $options !== $lastOptions) // yes, options changed
)
{
// minify previous sources with last settings
$imploded = implode($implodeSeparator, $groupToProcessTogether);
$groupToProcessTogether = array();
if ($lastMinifier) {
try {
$content[] = call_user_func($lastMinifier, $imploded, $lastOptions);
} catch (\Exception $e) {
throw new \Exception("Exception in minifier: " . $e->getMessage());
}
} else {
$content[] = $imploded;
}
}
// add content to the group
if ($source) {
$groupToProcessTogether[] = $sourceContent;
$lastMinifier = $minifier;
$lastOptions = $options;
}
$i++;
} while ($source);
$content = implode($implodeSeparator, $content);
if ($type === self::TYPE_CSS && false !== strpos($content, '@import')) {
$content = self::_handleCssImports($content);
}
if ( $type === self::TYPE_CSS ) {
$content = apply_filters( 'w3tc_minify_css_content', $content, null, null );
}
if ( $type === self::TYPE_JS ) {
$content = apply_filters( 'w3tc_minify_js_content', $content, null, null );
}
// do any post-processing (esp. for editing build URIs)
if (self::$_options['postprocessorRequire']) {
require_once self::$_options['postprocessorRequire'];
}
if (self::$_options['postprocessor']) {
$content = call_user_func(self::$_options['postprocessor'], $content, $type);
}
return array(
'originalLength' => $originalLength,
'content' => $content
);
}
/**
* Sets cache ID
* @param string $cacheId
*/
public static function setCacheId($cacheId = null)
{
self::$_cacheId = $cacheId;
}
/**
* Returns cache ID
*/
public static function getCacheId()
{
return self::$_cacheId;
}
/**
* Make a unique cache id for for this request.
*
* Any settings that could affect output are taken into consideration
*
* @param string $prefix
*
* @return string
*/
protected static function _getCacheId($prefix = 'minify')
{
return (self::$_cacheId ? self::$_cacheId : md5(serialize(array(
Minify_Source::getDigest(self::$_controller->sources)
,self::$_options['minifiers']
,self::$_options['minifierOptions']
,self::$_options['postprocessor']
,self::$_options['bubbleCssImports']
,self::$_options['processCssImports']
))));
}
/**
* Bubble CSS @imports to the top or prepend a warning if an import is detected not at the top.
*
* @param string $css
*
* @return string
*/
protected static function _handleCssImports($css)
{
if (self::$_options['bubbleCssImports']) {
// bubble CSS imports
preg_match_all('/@import.*?;/', $css, $imports);
$css = implode('', $imports[0]) . preg_replace('/@import.*?;/', '', $css);
} else if ('' !== self::$importWarning) {
// remove comments so we don't mistake { in a comment as a block
$noCommentCss = preg_replace('@/\\*[\\s\\S]*?\\*/@', '', $css);
$lastImportPos = strrpos($noCommentCss, '@import');
$firstBlockPos = strpos($noCommentCss, '{');
if (false !== $lastImportPos
&& false !== $firstBlockPos
&& $firstBlockPos < $lastImportPos
) {
// { appears before @import : prepend warning
$css = self::$importWarning . $css;
}
}
return $css;
}
}

View File

@@ -0,0 +1,112 @@
<?php
/**
* File: Build.php
*
* NOTE: Fixes have been included in this file; look for "W3TC FIX".
*/
namespace W3TCL\Minify;
/**
* Class Minify_Build
* @package Minify
*/
/**
* Maintain a single last modification time for a group of Minify sources to
* allow use of far off Expires headers in Minify.
*
* <code>
* // in config file
* $groupSources = array(
* 'js' => array('file1.js', 'file2.js')
* ,'css' => array('file1.css', 'file2.css', 'file3.css')
* )
*
* // during HTML generation
* $jsBuild = new Minify_Build($groupSources['js']);
* $cssBuild = new Minify_Build($groupSources['css']);
*
* $script = "<script type='text/javascript' src='"
* . $jsBuild->uri('/min.php/js') . "'></script>";
* $link = "<link rel='stylesheet' type='text/css' href='"
* . $cssBuild->uri('/min.php/css') . "'>";
*
* // in min.php
* Minify::serve('Groups', array(
* 'groups' => $groupSources
* ,'setExpires' => (time() + 86400 * 365)
* ));
* </code>
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_Build {
/**
* Last modification time of all files in the build
*
* @var int
*/
public $lastModified = 0;
/**
* String to use as ampersand in uri(). Set this to '&' if
* you are not HTML-escaping URIs.
*
* @var string
*/
public static $ampersand = '&amp;';
/**
* Get a time-stamped URI
*
* <code>
* echo $b->uri('/site.js');
* // outputs "/site.js?1678242"
*
* echo $b->uri('/scriptaculous.js?load=effects');
* // outputs "/scriptaculous.js?load=effects&amp1678242"
* </code>
*
* @param string $uri
* @param boolean $forceAmpersand (default = false) Force the use of ampersand to
* append the timestamp to the URI.
* @return string
*/
public function uri($uri, $forceAmpersand = false) {
$sep = ($forceAmpersand || strpos($uri, '?') !== false)
? self::$ampersand
: '?';
return "{$uri}{$sep}{$this->lastModified}";
}
/**
* Create a build object
*
* @param array $sources array of Minify_Source objects and/or file paths
*
* @return null
*/
public function __construct($sources)
{
// W3TC FIX: Override $_SERVER['DOCUMENT_ROOT'] if enabled in settings.
$docroot = \W3TC\Util_Environment::document_root();
$max = 0;
foreach ((array)$sources as $source) {
if ($source instanceof Minify_Source) {
$max = max($max, $source->lastModified);
} elseif (is_string($source)) {
if (0 === strpos($source, '//')) {
$source = $docroot . substr($source, 1);
}
if (is_file($source)) {
$max = max($max, filemtime($source));
}
}
}
$this->lastModified = $max;
}
}

View File

@@ -0,0 +1,106 @@
<?php
/**
* File: CSS.php
*
* NOTE: Fixes have been included in this file; look for "W3TC FIX".
*/
namespace W3TCL\Minify;
/**
* Class Minify_CSS
* @package Minify
*/
/**
* Minify CSS
*
* This class uses Minify_CSS_Compressor and Minify_CSS_UriRewriter to
* minify CSS and rewrite relative URIs.
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
* @author http://code.google.com/u/1stvamp/ (Issue 64 patch)
*/
class Minify_CSS {
/**
* Minify a CSS string
*
* @param string $css
*
* @param array $options available options:
*
* 'preserveComments': (default true) multi-line comments that begin
* with "/*!" will be preserved with newlines before and after to
* enhance readability.
*
* 'removeCharsets': (default true) remove all @charset at-rules
*
* 'prependRelativePath': (default null) if given, this string will be
* prepended to all relative URIs in import/url declarations
*
* 'currentDir': (default null) if given, this is assumed to be the
* directory of the current CSS file. Using this, minify will rewrite
* all relative URIs in import/url declarations to correctly point to
* the desired files. For this to work, the files *must* exist and be
* visible by the PHP process.
*
* 'symlinks': (default = array()) If the CSS file is stored in
* a symlink-ed directory, provide an array of link paths to
* target paths, where the link paths are within the document root. Because
* paths need to be normalized for this to work, use "//" to substitute
* the doc root in the link paths (the array keys). E.g.:
* <code>
* array('//symlink' => '/real/target/path') // unix
* array('//static' => 'D:\\staticStorage') // Windows
* </code>
*
* 'docRoot': (default = $_SERVER['DOCUMENT_ROOT'])
* see Minify_CSS_UriRewriter::rewrite
*
* @return string
*/
public static function minify($css, $options = array())
{
// W3TC FIX: Override $_SERVER['DOCUMENT_ROOT'] if enabled in settings.
$docroot = \W3TC\Util_Environment::document_root();
$options = array_merge(array(
'compress' => true,
'removeCharsets' => true,
'preserveComments' => true,
'currentDir' => null,
'docRoot' => $docroot,
'prependRelativePath' => null,
'symlinks' => array(),
), $options);
if ($options['removeCharsets']) {
$css = preg_replace('/@charset[^;]+;\\s*/', '', $css);
}
if ($options['compress']) {
if (! $options['preserveComments']) {
$css = Minify_CSS_Compressor::process($css, $options);
} else {
$css = Minify_CommentPreserver::process(
$css
,array('\W3TCL\Minify\Minify_CSS_Compressor', 'process')
,array($options)
);
}
}
if (! $options['currentDir'] && ! $options['prependRelativePath']) {
return $css;
}
if ($options['currentDir']) {
return Minify_CSS_UriRewriter::rewrite(
$css
,$options);
} else {
return Minify_CSS_UriRewriter::prepend(
$css
,$options['prependRelativePath']
);
}
}
}

View File

@@ -0,0 +1,281 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_CSS_Compressor
* @package Minify
*/
/**
* Compress CSS
*
* This is a heavy regex-based removal of whitespace, unnecessary
* comments and tokens, and some CSS value minimization, where practical.
* Many steps have been taken to avoid breaking comment-based hacks,
* including the ie5/mac filter (and its inversion), but expect tricky
* hacks involving comment tokens in 'content' value strings to break
* minimization badly. A test suite is available.
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
* @author http://code.google.com/u/1stvamp/ (Issue 64 patch)
*/
class Minify_CSS_Compressor {
/**
* Minify a CSS string
*
* @param string $css
*
* @param array $options (currently ignored)
*
* @return string
*/
public static function process($css, $options = array())
{
$obj = new Minify_CSS_Compressor($options);
return $obj->_process($css);
}
/**
* @var array
*/
protected $_options = null;
/**
* Are we "in" a hack? I.e. are some browsers targetted until the next comment?
*
* @var bool
*/
protected $_inHack = false;
protected $_replacementHash = '';
protected $_placeholders = array();
/**
* Constructor
*
* @param array $options (currently ignored)
*/
private function __construct($options) {
$this->_options = $options;
}
/**
* Minify a CSS string
*
* @param string $css
*
* @return string
*/
protected function _process($css)
{
$this->_replacementHash = 'MINIFYCSS' . md5( isset( $_SERVER['REQUEST_TIME'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_TIME'] ) ) : '' );
$this->_placeholders = array();
$css = preg_replace_callback('~(".*"|\'.*\')~U', array($this, '_removeQuotesCB'), $css);
$css = str_replace("\r\n", "\n", $css);
// preserve empty comment after '>'
// http://www.webdevout.net/css-hacks#in_css-selectors
$css = preg_replace('@>/\\*\\s*\\*/@', '>/*keep*/', $css);
// preserve empty comment between property and value
// http://css-discuss.incutio.com/?page=BoxModelHack
$css = preg_replace('@/\\*\\s*\\*/\\s*:@', '/*keep*/:', $css);
$css = preg_replace('@:\\s*/\\*\\s*\\*/@', ':/*keep*/', $css);
// apply callback to all valid comments (and strip out surrounding ws
$css = preg_replace_callback('@\\s*/\\*([\\s\\S]*?)\\*/\\s*@'
,array($this, '_commentCB'), $css);
// remove ws around { } and last semicolon in declaration block
$css = preg_replace('/\\s*{\\s*/', '{', $css);
$css = preg_replace('/;?\\s*}\\s*/', '}', $css);
// remove ws surrounding semicolons
$css = preg_replace('/\\s*;\\s*/', ';', $css);
// remove ws around urls
$css = preg_replace('/
url\\( # url(
\\s*
([^\\)]+?) # 1 = the URL (really just a bunch of non right parenthesis)
\\s*
\\) # )
/x', 'url($1)', $css);
// remove ws between rules and colons
$css = preg_replace('/
\\s*
([{;]) # 1 = beginning of block or rule separator
\\s*
([\\*_]?[\\w\\-]+) # 2 = property (and maybe IE filter)
\\s*
:
\\s*
(\\b|[#\'"-]) # 3 = first character of a value
/x', '$1$2:$3', $css);
// remove ws in selectors
$css = preg_replace_callback('/
(?: # non-capture
\\s*
[^{}~>+,\\s]+ # selector part
\\s*
[,>+~] # combinators
)+
\\s*
[^{}~>+,\\s]+ # selector part
{ # open declaration block
/x'
,array($this, '_selectorsCB'), $css);
// minimize hex colors
$css = preg_replace('/([^=])#([a-f\\d])\\2([a-f\\d])\\3([a-f\\d])\\4([\\s;\\}])/i'
, '$1#$2$3$4$5', $css);
// remove spaces between font families
$css = preg_replace_callback('/font-family:([^;}]+)([;}])/'
,array($this, '_fontFamilyCB'), $css);
$css = preg_replace('/@import\\s+url/', '@import url', $css);
// replace any ws involving newlines with a single newline
$css = preg_replace('/[ \\t]*\\n+\\s*/', "\n", $css);
// separate common descendent selectors w/ newlines (to limit line lengths)
$css = preg_replace('/([\\w#\\.\\*]+)\\s+([\\w#\\.\\*]+){/', "$1\n$2{", $css);
// Use newline after 1st numeric value (to limit line lengths).
$css = preg_replace('/
((?:padding|margin|border|outline):\\d+(?:px|em)?) # 1 = prop : 1st numeric value
\\s+
/x'
,"$1\n", $css);
// prevent triggering IE6 bug: http://www.crankygeek.com/ie6pebug/
$css = preg_replace('/:first-l(etter|ine)\\{/', ':first-l$1 {', $css);
// fill placeholders
$css = str_replace(
array_keys($this->_placeholders)
, array_values($this->_placeholders)
, $css
);
if (isset($this->_options['stripCrlf']) && $this->_options['stripCrlf']) {
$css = preg_replace("~[\r\n]+~", ' ', $css);
} else {
$css = preg_replace("~[\r\n]+~", "\n", $css);
}
return trim($css);
}
/**
* Replace what looks like a set of selectors
*
* @param array $m regex matches
*
* @return string
*/
protected function _selectorsCB($m)
{
// remove ws around the combinators
return preg_replace('/\\s*([,>+~])\\s*/', '$1', $m[0]);
}
/**
* Process a comment and return a replacement
*
* @param array $m regex matches
*
* @return string
*/
protected function _commentCB($m)
{
$hasSurroundingWs = (trim($m[0]) !== $m[1]);
$m = $m[1];
// $m is the comment content w/o the surrounding tokens,
// but the return value will replace the entire comment.
if ($m === 'keep') {
return '/**/';
}
if ($m === '" "') {
// component of http://tantek.com/CSS/Examples/midpass.html
return '/*" "*/';
}
if (preg_match('@";\\}\\s*\\}/\\*\\s+@', $m)) {
// component of http://tantek.com/CSS/Examples/midpass.html
return '/*";}}/* */';
}
if ($this->_inHack) {
// inversion: feeding only to one browser
if (preg_match('@
^/ # comment started like /*/
\\s*
(\\S[\\s\\S]+?) # has at least some non-ws content
\\s*
/\\* # ends like /*/ or /**/
@x', $m, $n)) {
// end hack mode after this comment, but preserve the hack and comment content
$this->_inHack = false;
return "/*/{$n[1]}/**/";
}
}
if (substr($m, -1) === '\\') { // comment ends like \*/
// begin hack mode and preserve hack
$this->_inHack = true;
return '/*\\*/';
}
if ($m !== '' && $m[0] === '/') { // comment looks like /*/ foo */
// begin hack mode and preserve hack
$this->_inHack = true;
return '/*/*/';
}
if ($this->_inHack) {
// a regular comment ends hack mode but should be preserved
$this->_inHack = false;
return '/**/';
}
// Issue 107: if there's any surrounding whitespace, it may be important, so
// replace the comment with a single space
return $hasSurroundingWs // remove all other comments
? ' '
: '';
}
/**
* Process a font-family listing and return a replacement
*
* @param array $m regex matches
*
* @return string
*/
protected function _fontFamilyCB($m)
{
// Issue 210: must not eliminate WS between words in unquoted families
$pieces = preg_split('/(\'[^\']+\'|"[^"]+")/', $m[1], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$out = 'font-family:';
while (null !== ($piece = array_shift($pieces))) {
if ($piece[0] !== '"' && $piece[0] !== "'") {
$piece = preg_replace('/\\s+/', ' ', $piece);
$piece = preg_replace('/\\s?,\\s?/', ',', $piece);
}
$out .= $piece;
}
return $out . $m[2];
}
protected function _reservePlace($content) {
$placeholder = '"' . $this->_replacementHash . count($this->_placeholders) . '"';
$this->_placeholders[$placeholder] = $content;
return $placeholder;
}
protected function _removeQuotesCB($m) {
return $this->_reservePlace($m[1]);
}
}

View File

@@ -0,0 +1,463 @@
<?php
/**
* File: UriRewriter.php
*
* NOTE: Fixes have been included in this file; look for "W3TC FIX".
*/
namespace W3TCL\Minify;
/**
* Class Minify_CSS_UriRewriter
* @package Minify
*/
/**
* Rewrite file-relative URIs as root-relative in CSS files
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_CSS_UriRewriter {
/**
* rewrite() and rewriteRelative() append debugging information here
*
* @var string
*/
public static $debugText = '';
/**
* In CSS content, rewrite file relative URIs as root relative
*
* @param string $css
*
* @param string $currentDir The directory of the current CSS file.
*
* @param string $docRoot The document root of the web site in which
* the CSS file resides (default = $_SERVER['DOCUMENT_ROOT']).
*
* @param array $symlinks (default = array()) If the CSS file is stored in
* a symlink-ed directory, provide an array of link paths to
* target paths, where the link paths are within the document root. Because
* paths need to be normalized for this to work, use "//" to substitute
* the doc root in the link paths (the array keys). E.g.:
* <code>
* array('//symlink' => '/real/target/path') // unix
* array('//static' => 'D:\\staticStorage') // Windows
* </code>
*
* @return string
*/
public static function rewrite($css, $options) {
self::$_prependPath = null;
if (!isset($options['prependRelativePath']) && !isset($options['currentDir']))
return $css;
self::$_browserCacheId = (isset($options['browserCacheId']) ?
$options['browserCacheId'] : 0);
self::$_browserCacheExtensions =
(isset($options['browserCacheExtensions']) ?
$options['browserCacheExtensions'] : array());
// W3TC FIX: Override $_SERVER['DOCUMENT_ROOT'] if enabled in settings.
$docroot = \W3TC\Util_Environment::document_root();
if (isset($options['currentDir'])) {
$document_root = (isset($options['docRoot']) ? $options['docRoot'] : $docroot);
$symlinks = (isset($options['symlinks']) ? $options['symlinks'] : array());
$prependAbsolutePath = (isset($options['prependAbsolutePath']) ? $options['prependAbsolutePath'] : '');
$prependAbsolutePathCallback = (isset($options['prependAbsolutePathCallback']) ? $options['prependAbsolutePathCallback'] : '');
$css = self::_rewrite(
$css,
$options['currentDir'],
$prependAbsolutePath,
$prependAbsolutePathCallback,
$document_root,
$symlinks
);
} elseif (isset($options['prependRelativePath'])) {
$css = self::prepend(
$css,
$options['prependRelativePath']
);
}
return $css;
}
/**
* Rewrite file relative URIs as root relative in CSS files
*
* @param string $css
*
* @param string $currentDir The directory of the current CSS file.
*
* @param string $prependAbsolutePath
*
* @param string $prependAbsolutePathCallback
*
* @param string $docRoot The document root of the web site in which
* the CSS file resides (default = $_SERVER['DOCUMENT_ROOT']).
*
* @param array $symlinks (default = array()) If the CSS file is stored in
* a symlink-ed directory, provide an array of link paths to
* target paths, where the link paths are within the document root. Because
* paths need to be normalized for this to work, use "//" to substitute
* the doc root in the link paths (the array keys). E.g.:
* <code>
* array('//symlink' => '/real/target/path') // unix
* array('//static' => 'D:\\staticStorage') // Windows
* </code>
*
* @param int $browserCacheId
*
* @param array $browserCacheExtensions
*
* @return string
*/
private static function _rewrite($css, $currentDir,
$prependAbsolutePath = null, $prependAbsolutePathCallback = null,
$docRoot = null, $symlinks = array()) {
// W3TC FIX: Override $_SERVER['DOCUMENT_ROOT'] if enabled in settings.
$docroot = \W3TC\Util_Environment::document_root();
self::$_docRoot = self::_realpath(
$docRoot ? $docRoot : $docroot
);
self::$_currentDir = self::_realpath($currentDir);
self::$_prependAbsolutePath = $prependAbsolutePath;
self::$_prependAbsolutePathCallback = $prependAbsolutePathCallback;
self::$_symlinks = array();
// normalize symlinks
foreach ($symlinks as $link => $target) {
$link = ($link === '//')
? self::$_docRoot
: str_replace('//', self::$_docRoot . '/', $link);
$link = strtr($link, '/', DIRECTORY_SEPARATOR);
self::$_symlinks[$link] = self::_realpath($target);
}
self::$debugText .= "docRoot : " . self::$_docRoot . "\n"
. "currentDir : " . self::$_currentDir . "\n";
if (self::$_symlinks) {
self::$debugText .= "symlinks : " . var_export(self::$_symlinks, 1) . "\n";
}
self::$debugText .= "\n";
$css = self::_trimUrls($css);
// rewrite
$css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/'
,array(self::$className, '_processUriCB'), $css);
$css = preg_replace_callback('/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
,array(self::$className, '_processUriCB'), $css);
return $css;
}
/**
* In CSS content, prepend a path to relative URIs
*
* @param string $css
*
* @param string $path The path to prepend.
*
* @return string
*/
public static function prepend($css, $path)
{
self::$_prependPath = $path;
$css = self::_trimUrls($css);
// append
$css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/'
,array(self::$className, '_processUriCB'), $css);
$css = preg_replace_callback('/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
,array(self::$className, '_processUriCB'), $css);
return $css;
}
/**
* Get a root relative URI from a file relative URI
*
* <code>
* Minify_CSS_UriRewriter::rewriteRelative(
* '../img/hello.gif'
* , '/home/user/www/css' // path of CSS file
* , '/home/user/www' // doc root
* );
* // returns '/img/hello.gif'
*
* // example where static files are stored in a symlinked directory
* Minify_CSS_UriRewriter::rewriteRelative(
* 'hello.gif'
* , '/var/staticFiles/theme'
* , '/home/user/www'
* , array('/home/user/www/static' => '/var/staticFiles')
* );
* // returns '/static/theme/hello.gif'
* </code>
*
* @param string $uri file relative URI
*
* @param string $realCurrentDir realpath of the current file's directory.
*
* @param string $realDocRoot realpath of the site document root.
*
* @param array $symlinks (default = array()) If the file is stored in
* a symlink-ed directory, provide an array of link paths to
* real target paths, where the link paths "appear" to be within the document
* root. E.g.:
* <code>
* array('/home/foo/www/not/real/path' => '/real/target/path') // unix
* array('C:\\htdocs\\not\\real' => 'D:\\real\\target\\path') // Windows
* </code>
*
* @return string
*/
private static function rewriteRelative($uri, $realCurrentDir, $realDocRoot, $symlinks = array())
{
if ('/' === substr($uri, 0, 1)) { // root-relative
return $uri;
}
// prepend path with current dir separator (OS-independent)
$path = strtr($realCurrentDir, '/', DIRECTORY_SEPARATOR)
. DIRECTORY_SEPARATOR . strtr($uri, '/', DIRECTORY_SEPARATOR);
self::$debugText .= "file-relative URI : {$uri}\n"
. "path prepended : {$path}\n";
// "unresolve" a symlink back to doc root
foreach ($symlinks as $link => $target) {
if (0 === strpos($path, $target)) {
// replace $target with $link
$path = $link . substr($path, strlen($target));
self::$debugText .= "symlink unresolved : {$path}\n";
break;
}
}
// strip doc root
$path = substr($path, strlen($realDocRoot));
self::$debugText .= "docroot stripped : {$path}\n";
// fix to root-relative URI
$uri = strtr($path, '/\\', '//');
$uri = self::removeDots($uri);
self::$debugText .= "traversals removed : {$uri}\n\n";
return $uri;
}
/**
* Remove instances of "./" and "../" where possible from a root-relative URI
*
* @param string $uri
*
* @return string
*/
public static function removeDots($uri)
{
$uri = str_replace('/./', '/', $uri);
// inspired by patch from Oleg Cherniy
do {
$uri = preg_replace('@/[^/]+/\\.\\./@', '/', $uri, 1, $changed);
} while ($changed);
return $uri;
}
/**
* Defines which class to call as part of callbacks, change this
* if you extend Minify_CSS_UriRewriter
*
* @var string
*/
protected static $className = '\W3TCL\Minify\Minify_CSS_UriRewriter';
/**
* Get realpath with any trailing slash removed. If realpath() fails,
* just remove the trailing slash.
*
* @param string $path
*
* @return mixed path with no trailing slash
*/
protected static function _realpath($path)
{
$realPath = realpath($path);
if ($realPath !== false) {
$path = $realPath;
}
return rtrim($path, '/\\');
}
/**
* Directory of this stylesheet
*
* @var string
*/
private static $_currentDir = '';
/**
* DOC_ROOT
*
* @var string
*/
private static $_docRoot = '';
/**
* directory replacements to map symlink targets back to their
* source (within the document root) E.g. '/var/www/symlink' => '/var/realpath'
*
* @var array
*/
private static $_symlinks = array();
/**
* Path to prepend
*
* @var string
*/
private static $_prependPath = null;
/**
* @var string
*/
private static $_prependAbsolutePath = null;
/**
* @var string
*/
private static $_prependAbsolutePathCallback = null;
/**
* @var int
*/
private static $_browserCacheId = 0;
/**
* @var array
*/
private static $_browserCacheExtensions = array();
/**
* @param string $css
*
* @return string
*/
private static function _trimUrls($css)
{
return preg_replace('/
url\\( # url(
\\s*
([^\\)]+?) # 1 = URI (assuming does not contain ")")
\\s*
\\) # )
/x', 'url($1)', $css);
}
/**
* @param array $m
*
* @return string
*/
private static function _processUriCB($m)
{
// $m matched either '/@import\\s+([\'"])(.*?)[\'"]/' or '/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
$isImport = ($m[0][0] === '@');
// determine URI and the quote character (if any)
if ($isImport) {
$quoteChar = $m[1];
$uri = $m[2];
} else {
// $m[1] is either quoted or not
$quoteChar = ($m[1][0] === "'" || $m[1][0] === '"')
? $m[1][0]
: '';
$uri = ($quoteChar === '')
? $m[1]
: substr($m[1], 1, strlen($m[1]) - 2);
}
// analyze URI
if ( !empty($uri)
&& '/' !== substr($uri, 0, 1) // Root-relative (/).
&& false === strpos( $uri, '//' ) // Protocol/non-data (//).
&& 'data:' !== substr( $uri, 0, 5 ) // Data protocol.
&& '%' !== substr( $uri, 0, 1 ) // URL-encoded entity.
&& '#' !== substr( $uri, 0, 1 ) // URL fragment.
) {
// URI is file-relative: rewrite depending on options
if (self::$_prependPath === null) {
$uri = self::rewriteRelative($uri, self::$_currentDir, self::$_docRoot, self::$_symlinks);
if (self::$_prependAbsolutePath) {
$prependAbsolutePath = self::$_prependAbsolutePath;
} elseif (self::$_prependAbsolutePathCallback) {
$prependAbsolutePath = call_user_func(self::$_prependAbsolutePathCallback, $uri);
} else {
$prependAbsolutePath = '';
}
if ($prependAbsolutePath) {
$uri = rtrim($prependAbsolutePath, '/') . $uri;
}
} else {
if (!\W3TC\Util_Environment::is_url(self::$_prependPath)) {
$uri = self::$_prependPath . $uri;
if (substr($uri, 0, 1) === '/') {
$root = '';
$rootRelative = $uri;
$uri = $root . self::removeDots($rootRelative);
} elseif (preg_match('@^((https?\:)?//([^/]+))/@', $uri, $m) && (false !== strpos($m[3], '.'))) {
$root = $m[1];
$rootRelative = substr($uri, strlen($root));
$uri = $root . self::removeDots($rootRelative);
}
} else {
$parse_url = @parse_url(self::$_prependPath);
if ($parse_url && isset($parse_url['host'])) {
$scheme = array_key_exists('scheme', $parse_url) ? $parse_url['scheme'] : '';
$host = $parse_url['host'];
$port = (isset($parse_url['port']) && $parse_url['port'] != 80 ? ':' . (int) $parse_url['port'] : '');
$path = (!empty($parse_url['path']) ? $parse_url['path'] : '/');
$dir_css = preg_replace('~[^/]+$~', '', $path);
$dir_obj = preg_replace('~[^/]+$~', '', $uri);
$dir = (ltrim((strpos($dir_obj, '/') === 0 ? \W3TC\Util_Environment::realpath($dir_obj) : \W3TC\Util_Environment::realpath($dir_css . $dir_obj)), '/'));
$file = basename($uri);
$scheme_dot = ( empty( $scheme ) ? '' : $scheme . ':' );
$uri = sprintf('%s//%s%s/%s/%s', $scheme_dot, $host,
$port, $dir, $file);
}
}
}
if (self::$_browserCacheId && count(self::$_browserCacheExtensions)) {
$matches = null;
if (preg_match('~\.([a-z-_]+)(\?.*)?$~', $uri, $matches)) {
$extension = $matches[1];
if ($extension && in_array($extension, self::$_browserCacheExtensions)) {
$uri = \W3TC\Util_Environment::remove_query($uri);
$uri .= ( strpos( $uri, '?' ) !== false ? '&' : '?' ) . self::$_browserCacheId;
}
}
}
}
return $isImport
? "@import {$quoteChar}{$uri}{$quoteChar}"
: "url({$quoteChar}{$uri}{$quoteChar})";
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace W3TCL\Minify;
class Minify_CSSTidy {
public static function minify($css, $options = array()) {
$options = array_merge(array(
'remove_bslash' => true,
'compress_colors' => false,
'compress_font-weight' => false,
'lowercase_s' => false,
'optimise_shorthands' => 0,
'remove_last_;' => false,
'space_before_important' => false,
'case_properties' => 1,
'sort_properties' => false,
'sort_selectors' => false,
'merge_selectors' => 0,
'discard_invalid_selectors' => false,
'discard_invalid_properties' => false,
'css_level' => 'CSS3.0',
'preserve_css' => false,
'timestamp' => false,
'template' => 'default'
), $options);
set_include_path(get_include_path() . PATH_SEPARATOR . W3TC_LIB_DIR . '/CSSTidy');
require_once 'class.csstidy.php';
$csstidy = new \csstidy();
foreach ($options as $option => $value) {
$csstidy->set_cfg($option, $value);
}
$csstidy->load_template($options['template']);
$csstidy->parse($css);
$css = $csstidy->print->plain();
$css = Minify_CSS_UriRewriter::rewrite($css, $options);
return $css;
}
}

View File

@@ -0,0 +1,308 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_Cache_File
* @package Minify
*/
class Minify_Cache_File {
private $_path = null;
private $_exclude = null;
private $_locking = null;
private $_flushTimeLimit = null;
private $_flush_path = null;
public function __construct($path = '', $exclude = array(), $locking = false, $flushTimeLimit = 0, $flush_path = null) {
if (! $path) {
$path = self::tmp();
}
$this->_path = $path;
$this->_exclude = $exclude;
$this->_locking = $locking;
$this->_flushTimeLimit = $flushTimeLimit;
$this->_flush_path = (is_null($flush_path) ? $path : $flush_path);
if (!file_exists($this->_path .'/index.html')) {
if (!is_dir($this->_path))
\W3TC\Util_File::mkdir_from_safe($this->_path, W3TC_CACHE_DIR);
@file_put_contents($this->_path .'/index.html', '');
}
}
/**
* Write data to cache.
*
* @param string $id cache id (e.g. a filename)
*
* @param string $data
*
* @return bool success
*/
public function store($id, $data)
{
$path = $this->_path . '/' . $id;
$flag = $this->_locking ? LOCK_EX : null;
if (is_file($path)) {
@unlink($path);
}
if (!@file_put_contents($path, $data['content'], $flag)) {
// retry with make dir
\W3TC\Util_File::mkdir_from_safe(dirname($path), W3TC_CACHE_DIR);
if (!@file_put_contents($path, $data['content'], $flag))
return false;
}
if ( file_exists( $path . '_old' ) ) {
@unlink($path . '_old');
}
if ( file_exists( $path . '_meta_old' ) ) {
@unlink($path . '_meta_old');
}
$content = $data['content'];
unset($data['content']);
if (count($data) > 0)
@file_put_contents($path . '_meta', serialize($data), $flag);
// write control
$read = $this->fetch($id);
if (!isset($read['content']) || $content != $read['content']) {
@unlink($path);
return false;
}
return true;
}
/**
* Get the size of a cache entry
*
* @param string $id cache id (e.g. a filename)
*
* @return int size in bytes
*/
public function getSize($id)
{
return filesize($this->_path . '/' . $id);
}
/**
* Does a valid cache entry exist?
*
* @param string $id cache id (e.g. a filename)
*
* @param int $srcMtime mtime of the original source file(s)
*
* @return bool exists
*/
public function isValid($id, $srcMtime)
{
$file = $this->_path . '/' . $id;
return (is_file($file) && (filemtime($file) >= $srcMtime));
}
/**
* Send the cached content to output
*
* @param string $id cache id (e.g. a filename)
*/
public function display($id)
{
$path = $this->_path . '/' . $id;
$fp = @fopen($path, 'rb');
if ($fp) {
if ($this->_locking)
@flock($fp, LOCK_SH);
@fpassthru($fp);
if ($this->_locking)
@flock($fp, LOCK_UN);
@fclose($fp);
return true;
}
return false;
}
/**
* Fetch the cached content
*
* @param string $id cache id (e.g. a filename)
*
* @return string|false
*/
public function fetch($id)
{
$path = $this->_path . '/' . $id;
$data = @file_get_contents($path . '_meta');
if ( ! empty( $data ) ) {
$data = @unserialize($data);
if (!is_array($data))
$data = array();
} else {
$data = array();
}
if (is_readable($path)) {
if ($this->_locking) {
$fp = @fopen($path, 'rb');
if ($fp) {
@flock($fp, LOCK_SH);
$ret = @stream_get_contents($fp);
@flock($fp, LOCK_UN);
@fclose($fp);
$data['content'] = $ret;
return $data;
}
} else {
$data['content'] = @file_get_contents($path);
return $data;
}
} else {
$path_old = $path . '_old';
$too_old_time = time() - 30;
$file_time = @filemtime($path_old);
if ($file_time) {
if ($file_time > $too_old_time) {
// return old data
$data['content'] = @file_get_contents($path_old);
return $data;
}
@touch($path_old);
}
}
return false;
}
/**
* Returns the OS-specific directory for temporary files
*
* @author Paul M. Jones <pmjones@solarphp.com>
* @license http://opensource.org/licenses/bsd-license.php BSD
* @link http://solarphp.com/trac/core/browser/trunk/Solar/Dir.php
*
* @return string
*/
protected static function _tmp()
{
// non-Windows system?
if (strtolower(substr(PHP_OS, 0, 3)) != 'win') {
$tmp = empty($_ENV['TMPDIR']) ? getenv('TMPDIR') : $_ENV['TMPDIR'];
if ($tmp) {
return $tmp;
} else {
return '/tmp';
}
}
// Windows 'TEMP'
$tmp = empty($_ENV['TEMP']) ? getenv('TEMP') : $_ENV['TEMP'];
if ($tmp) {
return $tmp;
}
// Windows 'TMP'
$tmp = empty($_ENV['TMP']) ? getenv('TMP') : $_ENV['TMP'];
if ($tmp) {
return $tmp;
}
// Windows 'windir'
$tmp = empty($_ENV['windir']) ? getenv('windir') : $_ENV['windir'];
if ($tmp) {
return $tmp;
}
// final fallback for Windows
return getenv('SystemRoot') . '\\temp';
}
/**
* Flush cache
*
* @return bool
*/
public function flush() {
@set_time_limit($this->_flushTimeLimit);
return \W3TC\Util_File::emptydir($this->_flush_path, $this->_exclude);
}
/**
* Fetch the cache path used
*
* @return string
*/
public function getPath() {
return $this->_path;
}
/**
* Returns size statistics about cache files
*/
public function get_stats_size($timeout_time)
{
$dir = @opendir($this->_path);
$stats = array(
'css' => array(
'items' => 0,
'original_length' => 0,
'output_length' => 0
),
'js' => array(
'items' => 0,
'original_length' => 0,
'output_length' => 0
),
'timeout_occurred' => false
);
if (!$dir)
return $stats;
$n = 0;
while (!$stats['timeout_occurred'] &&
($entry = @readdir($dir)) !== false) {
$n++;
if ($n % 1000 == 0)
$stats['timeout_occurred'] |= (time() > $timeout_time);
if (substr($entry, -8) == '.js_meta' || substr($entry, -13) == '.js_gzip_meta')
$type = 'js';
else if (substr($entry, -9) == '.css_meta' || substr($entry, -14) == '.css_gzip_meta')
$type = 'css';
else
continue;
$full_path = $this->_path . DIRECTORY_SEPARATOR . $entry;
$data = @file_get_contents($full_path);
if (!$data)
continue;
$data = @unserialize($data);
if (!is_array($data) || !isset($data['originalLength']))
continue;
$stats[$type]['items']++;
$stats[$type]['original_length'] += (int)$data['originalLength'];
$stats[$type]['output_length'] +=
@filesize(substr($full_path, 0, strlen($full_path) - 5));
}
@closedir($dir);
return $stats;
}
}

View File

@@ -0,0 +1,126 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_Cache_APC
* @package Minify
*/
/**
* APC-based cache class for Minify
*
* <code>
* Minify::setCache(new Minify_Cache_APC());
* </code>
*
* @package Minify
* @author Chris Edwards
**/
class Minify_Cache_W3TCDerived {
private $_cache;
/**
* Create a Minify_Cache_APC object, to be passed to
* Minify::setCache().
*/
public function __construct($cache) {
$this->_cache = $cache;
}
/**
* Write data to cache.
*
* @param string $id cache id
*
* @param string $data
*
* @return bool success
*/
public function store($id, $data)
{
$data['created_time'] = isset( $_SERVER['REQUEST_TIME'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_TIME'] ) ) : '';
return $this->_cache->set($id, $data);
}
/**
* Get the size of a cache entry
*
* @param string $id cache id
*
* @return int size in bytes
*/
public function getSize($id)
{
$v = $this->fetch($id);
if (!isset($v['content'])) {
return false;
}
return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2))
? mb_strlen($v['content'], '8bit')
: strlen($v['content']);
}
/**
* Does a valid cache entry exist?
*
* @param string $id cache id
*
* @param int $srcMtime mtime of the original source file(s)
*
* @return bool exists
*/
public function isValid($id, $srcMtime)
{
$v = $this->fetch($id);
if (!isset($v['created_time']))
return false;
return ($v['created_time'] >= $srcMtime);
}
/**
* Send the cached content to output
*
* @param string $id cache id
*/
public function display($id)
{
$v = $this->fetch($id);
if ( isset( $v['content'] ) ) {
echo $v['content'];
}
}
private $loaded_id = null;
private $loaded_value = null;
/**
* Fetch the cached content
*
* @param string $id cache id
*
* @return string
*/
public function fetch($id)
{
if ($this->loaded_id == $id) {
return $this->loaded_value;
}
$v = $this->_cache->get($id);
if (!is_array($v) || !isset($v['content']))
return false;
$this->loaded_id = $id;
$this->loaded_value = $v;
return $this->loaded_value;
}
/**
* Flushes all data
*
* @return boolean
*/
function flush() {
return $this->_cache->flush();
}
}

View File

@@ -0,0 +1,142 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_Cache_ZendPlatform
* @package Minify
*/
/**
* ZendPlatform-based cache class for Minify
*
* Based on Minify_Cache_APC, uses output_cache_get/put (currently deprecated)
*
* <code>
* Minify::setCache(new Minify_Cache_ZendPlatform());
* </code>
*
* @package Minify
* @author Patrick van Dissel
*/
class Minify_Cache_ZendPlatform {
/**
* Create a Minify_Cache_ZendPlatform object, to be passed to
* Minify::setCache().
*
* @param int $expire seconds until expiration (default = 0
* meaning the item will not get an expiration date)
*
* @return null
*/
public function __construct($expire = 0)
{
$this->_exp = $expire;
}
/**
* Write data to cache.
*
* @param string $id cache id
*
* @param string $data
*
* @return bool success
*/
public function store( $id, $data ) {
return output_cache_put(
$id,
( isset( $_SERVER['REQUEST_TIME'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_TIME'] ) ) : '' ) . '|' . $data
);
}
/**
* Get the size of a cache entry
*
* @param string $id cache id
*
* @return int size in bytes
*/
public function getSize($id)
{
return $this->_fetch($id)
? strlen($this->_data)
: false;
}
/**
* Does a valid cache entry exist?
*
* @param string $id cache id
*
* @param int $srcMtime mtime of the original source file(s)
*
* @return bool exists
*/
public function isValid($id, $srcMtime)
{
$ret = ($this->_fetch($id) && ($this->_lm >= $srcMtime));
return $ret;
}
/**
* Send the cached content to output.
*
* @param string $id Cache id.
*/
public function display( $id ) {
echo $this->_fetch( $id ) ? $this->_data : '';
}
/**
* Fetch the cached content
*
* @param string $id cache id
*
* @return string
*/
public function fetch($id)
{
return $this->_fetch($id)
? $this->_data
: '';
}
private $_exp = null;
// cache of most recently fetched id
private $_lm = null;
private $_data = null;
private $_id = null;
/**
* Fetch data and timestamp from ZendPlatform, store in instance
*
* @param string $id
*
* @return bool success
*/
private function _fetch($id)
{
if ($this->_id === $id) {
return true;
}
$ret = output_cache_get($id, $this->_exp);
if (false === $ret) {
$this->_id = null;
return false;
}
list($this->_lm, $this->_data) = explode('|', $ret, 2);
$this->_id = $id;
return true;
}
}

View File

@@ -0,0 +1,144 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_ClosureCompiler
* @package Minify
*/
/**
* Compress Javascript using the Closure Compiler
*
* You must set $jarFile and $tempDir before calling the minify functions.
* Also, depending on your shell's environment, you may need to specify
* the full path to java in $javaExecutable or use putenv() to setup the
* Java environment.
*
* <code>
* Minify_ClosureCompiler::$jarFile = '/path/to/closure-compiler-20120123.jar';
* Minify_ClosureCompiler::$tempDir = '/tmp';
* $code = Minify_ClosureCompiler::minify(
* $code,
* array('compilation_level' => 'SIMPLE_OPTIMIZATIONS')
* );
*
* --compilation_level WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS
*
* </code>
*
* @todo unit tests, $options docs
* @todo more options support (or should just passthru them all?)
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
* @author Elan Ruusamäe <glen@delfi.ee>
*/
class Minify_ClosureCompiler {
/**
* Filepath of the Closure Compiler jar file. This must be set before
* calling minifyJs().
*
* @var string
*/
public static $jarFile = null;
/**
* Writable temp directory. This must be set before calling minifyJs().
*
* @var string
*/
public static $tempDir = null;
/**
* Filepath of "java" executable (may be needed if not in shell's PATH)
*
* @var string
*/
public static $javaExecutable = 'java';
/**
* Minify a Javascript string
*
* @param string $js
*
* @param array $options (verbose is ignored)
*
* @see https://code.google.com/p/closure-compiler/source/browse/trunk/README
*
* @return string
*/
public static function minify($js, $options = array())
{
self::_prepare();
if (! ($tmpFile = tempnam(self::$tempDir, 'cc_'))) {
throw new \Exception('Minify_ClosureCompiler : could not create temp file.');
}
file_put_contents($tmpFile, $js);
exec(self::_getCmd($options, $tmpFile), $output, $result_code);
unlink($tmpFile);
if ($result_code != 0) {
throw new \Exception('Minify_ClosureCompiler : Closure Compiler execution failed.');
}
return implode("\n", $output);
}
private static function _getCmd($userOptions, $tmpFile)
{
$o = array_merge(
array(
'charset' => 'utf-8',
'compilation_level' => 'SIMPLE_OPTIMIZATIONS',
),
$userOptions
);
$javaExecutable = self::$javaExecutable;
if ( false !== strpos(trim($javaExecutable), ' ') ) {
$javaExecutable = '"'.$javaExecutable.'"';
}
$cmd = $javaExecutable . ' -jar ' . escapeshellarg(self::$jarFile)
. (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $o['charset'])
? " --charset {$o['charset']}"
: '');
foreach (array('compilation_level') as $opt) {
if ($o[$opt]) {
$cmd .= " --{$opt} ". escapeshellarg($o[$opt]);
}
}
return $cmd . ' ' . escapeshellarg($tmpFile);
}
private static function _prepare()
{
if (! is_file(self::$jarFile)) {
throw new \Exception('Minify_ClosureCompiler : $jarFile('.self::$jarFile.') is not a valid file.');
}
if (! is_readable(self::$jarFile)) {
throw new \Exception('Minify_ClosureCompiler : $jarFile('.self::$jarFile.') is not readable.');
}
if (! is_dir(self::$tempDir)) {
throw new \Exception('Minify_ClosureCompiler : $tempDir('.self::$tempDir.') is not a valid direcotry.');
}
if (! is_writable(self::$tempDir)) {
throw new \Exception('Minify_ClosureCompiler : $tempDir('.self::$tempDir.') is not writable.');
}
}
public static function test(&$error) {
try {
self::minify('alert("ok");');
$error = 'OK';
return true;
} catch (\Exception $exception) {
$error = $exception->getMessage();
return false;
}
}
}
/* vim:ts=4:sw=4:et */

View File

@@ -0,0 +1,18 @@
<?php
namespace W3TCL\Minify;
/**
* Combine only minifier
*/
class Minify_CombineOnly {
/**
* Minifies content
* @param string $content
* @param array $options
* @return string
*/
public static function minify($content, $options = array()) {
$content = Minify_CSS_UriRewriter::rewrite($content, $options);
return $content;
}
}

View File

@@ -0,0 +1,90 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_CommentPreserver
* @package Minify
*/
/**
* Process a string in pieces preserving C-style comments that begin with "/*!"
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_CommentPreserver {
/**
* String to be prepended to each preserved comment
*
* @var string
*/
public static $prepend = "\n";
/**
* String to be appended to each preserved comment
*
* @var string
*/
public static $append = "\n";
/**
* Process a string outside of C-style comments that begin with "/*!"
*
* On each non-empty string outside these comments, the given processor
* function will be called. The comments will be surrounded by
* Minify_CommentPreserver::$preprend and Minify_CommentPreserver::$append.
*
* @param string $content
* @param callback $processor function
* @param array $args array of extra arguments to pass to the processor
* function (default = array())
* @return string
*/
public static function process($content, $processor, $args = array())
{
$ret = '';
while (true) {
list($beforeComment, $comment, $afterComment) = self::_nextComment($content);
if ('' !== $beforeComment) {
$callArgs = $args;
array_unshift($callArgs, $beforeComment);
$ret .= call_user_func_array($processor, $callArgs);
}
if (false === $comment) {
break;
}
$ret .= $comment;
$content = $afterComment;
}
return $ret;
}
/**
* Extract comments that YUI Compressor preserves.
*
* @param string $in input
*
* @return array 3 elements are returned. If a YUI comment is found, the
* 2nd element is the comment and the 1st and 3rd are the surrounding
* strings. If no comment is found, the entire string is returned as the
* 1st element and the other two are false.
*/
private static function _nextComment($in)
{
if (
false === ($start = strpos($in, '/*!'))
|| false === ($end = strpos($in, '*/', $start + 3))
) {
return array($in, false, false);
}
$ret = array(
substr($in, 0, $start)
,self::$prepend . '/*!' . substr($in, $start + 3, $end - $start - 1) . self::$append
);
$endChars = (strlen($in) - $end - 2);
$ret[] = (0 === $endChars)
? ''
: substr($in, -$endChars);
return $ret;
}
}

View File

@@ -0,0 +1,224 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_Controller_Base
* @package Minify
*/
/**
* Base class for Minify controller
*
* The controller class validates a request and uses it to create sources
* for minification and set options like contentType. It's also responsible
* for loading minifier code upon request.
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
abstract class Minify_Controller_Base {
/**
* Setup controller sources and set an needed options for Minify::source
*
* You must override this method in your subclass controller to set
* $this->sources. If the request is NOT valid, make sure $this->sources
* is left an empty array. Then strip any controller-specific options from
* $options and return it. To serve files, $this->sources must be an array of
* Minify_Source objects.
*
* @param array $options controller and Minify options
*
* @return array $options Minify::serve options
*/
abstract public function setupSources($options);
/**
* Get default Minify options for this controller.
*
* Override in subclass to change defaults
*
* @return array options for Minify
*/
public function getDefaultMinifyOptions() {
return array(
'isPublic' => true
,'encodeOutput' => function_exists('gzdeflate')
,'encodeMethod' => null // determine later
,'encodeLevel' => 9
,'minifierOptions' => array() // no minifier options
,'contentTypeCharset' => 'utf-8'
,'maxAge' => 1800 // 30 minutes
,'rewriteCssUris' => true
,'bubbleCssImports' => false
,'processCssImports' => false
,'quiet' => false // serve() will send headers and output
,'debug' => false
// if you override these, the response codes MUST be directly after
// the first space.
,'badRequestHeader' => 'HTTP/1.0 400 Bad Request'
,'errorHeader' => 'HTTP/1.0 500 Internal Server Error'
// callback function to see/modify content of all sources
,'postprocessor' => null
// file to require to load preprocessor
,'postprocessorRequire' => null
);
}
/**
* Get default minifiers for this controller.
*
* Override in subclass to change defaults
*
* @return array minifier callbacks for common types
*/
public function getDefaultMinifers() {
$ret[Minify::TYPE_JS] = array('\W3TCL\Minify\JSMin', 'minify');
$ret[Minify::TYPE_CSS] = array('\W3TCL\Minify\Minify_CSS', 'minify');
$ret[Minify::TYPE_HTML] = array('\W3TCL\Minify\Minify_HTML', 'minify');
return $ret;
}
/**
* Is a user-given file within an allowable directory, existing,
* and having an extension js/css/html/txt ?
*
* This is a convenience function for controllers that have to accept
* user-given paths
*
* @param string $file full file path (already processed by realpath())
*
* @param array $safeDirs directories where files are safe to serve. Files can also
* be in subdirectories of these directories.
*
* @return bool file is safe
*
* @deprecated use checkAllowDirs, checkNotHidden instead
*/
public static function _fileIsSafe($file, $safeDirs)
{
$pathOk = false;
foreach ((array)$safeDirs as $safeDir) {
if (strpos($file, $safeDir) === 0) {
$pathOk = true;
break;
}
}
$base = basename($file);
if (! $pathOk || ! is_file($file) || $base[0] === '.') {
return false;
}
list($revExt) = explode('.', strrev($base));
return in_array(strrev($revExt), array('js', 'css', 'html', 'txt'));
}
/**
* @param string $file
* @param array $allowDirs
* @param string $uri
* @return bool
* @throws Exception
*/
public static function checkAllowDirs($file, $allowDirs, $uri)
{
foreach ((array)$allowDirs as $allowDir) {
if (strpos($file, $allowDir) === 0) {
return true;
}
}
throw new \Exception("File '$file' is outside \$allowDirs. If the path is"
. " resolved via an alias/symlink, look into the \$min_symlinks option."
. " E.g. \$min_symlinks['/" . dirname($uri) . "'] = '" . dirname($file) . "';");
}
/**
* @param string $file
* @throws Exception
*/
public static function checkNotHidden($file)
{
$b = basename($file);
if (0 === strpos($b, '.')) {
throw new \Exception("Filename '$b' starts with period (may be hidden)");
}
}
/**
* instances of Minify_Source, which provide content and any individual minification needs.
*
* @var array
*
* @see Minify_Source
*/
public $sources = array();
/**
* Short name to place inside cache id
*
* The setupSources() method may choose to set this, making it easier to
* recognize a particular set of sources/settings in the cache folder. It
* will be filtered and truncated to make the final cache id <= 250 bytes.
*
* @var string
*/
public $selectionId = '';
/**
* Mix in default controller options with user-given options
*
* @param array $options user options
*
* @return array mixed options
*/
public final function mixInDefaultOptions($options)
{
$ret = array_merge(
$this->getDefaultMinifyOptions(), $options
);
if (! isset($options['minifiers'])) {
$options['minifiers'] = array();
}
$ret['minifiers'] = array_merge(
$this->getDefaultMinifers(), $options['minifiers']
);
return $ret;
}
/**
* Analyze sources (if there are any) and set $options 'contentType'
* and 'lastModifiedTime' if they already aren't.
*
* @param array $options options for Minify
*
* @return array options for Minify
*/
public final function analyzeSources($options = array())
{
if ($this->sources) {
if (! isset($options['contentType'])) {
$options['contentType'] = Minify_Source::getContentType($this->sources);
}
// last modified is needed for caching, even if setExpires is set
if (! isset($options['lastModifiedTime'])) {
$max = 0;
foreach ($this->sources as $source) {
$max = max($source->lastModified, $max);
}
$options['lastModifiedTime'] = $max;
}
}
return $options;
}
/**
* Send message to the Minify logger
*
* @param string $msg
*
* @return null
*/
public function log($msg) {
Minify_Logger::log($msg);
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_Controller_Files
* @package Minify
*/
/**
* Controller class for minifying a set of files
*
* E.g. the following would serve the minified Javascript for a site
* <code>
* Minify::serve('Files', array(
* 'files' => array(
* '//js/jquery.js'
* ,'//js/plugins.js'
* ,'/home/username/file.js'
* )
* ));
* </code>
*
* As a shortcut, the controller will replace "//" at the beginning
* of a filename with $_SERVER['DOCUMENT_ROOT'] . '/'.
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_Controller_Files extends Minify_Controller_Base {
/**
* Set up file sources
*
* @param array $options controller and Minify options
* @return array Minify options
*
* Controller options:
*
* 'files': (required) array of complete file paths, or a single path
*/
public function setupSources($options) {
// W3TC FIX: Override $_SERVER['DOCUMENT_ROOT'] if enabled in settings.
$docroot = \W3TC\Util_Environment::document_root();
// strip controller options
$files = $options['files'];
// if $files is a single object, casting will break it
if (is_object($files)) {
$files = array($files);
} elseif (! is_array($files)) {
$files = (array)$files;
}
unset($options['files']);
$sources = array();
foreach ($files as $file) {
if ($file instanceof Minify_Source) {
$sources[] = $file;
continue;
}
if (0 === strpos($file, '//')) {
if ( is_file( ABSPATH . substr($file, 1) ) ) {
$file = ABSPATH . substr( $file, 1 );
} else {
$file = $docroot . substr( $file, 1 );
}
}
$realPath = realpath($file);
if (is_file($realPath)) {
$sources[] = new Minify_Source(array(
'filepath' => $realPath
));
} else {
$this->log("The path \"{$file}\" could not be found (or was not a file)");
return $options;
}
}
if ($sources) {
$this->sources = $sources;
}
return $options;
}
}

View File

@@ -0,0 +1,102 @@
<?php
/**
* File: Groups.php
*
* NOTE: Fixes have been included in this file; look for "W3TC FIX".
*/
namespace W3TCL\Minify;
/**
* Class Minify_Controller_Groups
* @package Minify
*/
/**
* Controller class for serving predetermined groups of minimized sets, selected
* by PATH_INFO
*
* <code>
* Minify::serve('Groups', array(
* 'groups' => array(
* 'css' => array('//css/type.css', '//css/layout.css')
* ,'js' => array('//js/jquery.js', '//js/site.js')
* )
* ));
* </code>
*
* If the above code were placed in /serve.php, it would enable the URLs
* /serve.php/js and /serve.php/css
*
* As a shortcut, the controller will replace "//" at the beginning
* of a filename with $_SERVER['DOCUMENT_ROOT'] . '/'.
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_Controller_Groups extends Minify_Controller_Base {
/**
* Set up groups of files as sources
*
* @param array $options controller and Minify options
*
* 'groups': (required) array mapping PATH_INFO strings to arrays
* of complete file paths. @see Minify_Controller_Groups
*
* @return array Minify options
*/
public function setupSources($options) {
// strip controller options
$groups = $options['groups'];
unset($options['groups']);
// mod_fcgid places PATH_INFO in ORIG_PATH_INFO.
$pi = false;
if ( isset( $_SERVER['ORIG_PATH_INFO'] ) ) {
$pi = substr( sanitize_text_field( wp_unslash( $_SERVER['ORIG_PATH_INFO'] ) ), 1 );
} elseif ( isset( $_SERVER['PATH_INFO'] ) ) {
$pi = substr( sanitize_text_field( wp_unslash( $_SERVER['PATH_INFO'] ) ), 1 );
}
if (false === $pi || ! isset($groups[$pi])) {
// no PATH_INFO or not a valid group
$this->log("Missing PATH_INFO or no group set for \"$pi\"");
return $options;
}
$sources = array();
$files = $groups[$pi];
// if $files is a single object, casting will break it
if (is_object($files)) {
$files = array($files);
} elseif (! is_array($files)) {
$files = (array)$files;
}
// W3TC FIX: Override $_SERVER['DOCUMENT_ROOT'] if enabled in settings.
$docroot = \W3TC\Util_Environment::document_root();
foreach ($files as $file) {
if ($file instanceof Minify_Source) {
$sources[] = $file;
continue;
}
if (0 === strpos($file, '//')) {
$file = $docroot . substr($file, 1);
}
$realPath = realpath($file);
if (is_file($realPath)) {
$sources[] = new Minify_Source(array(
'filepath' => $realPath
));
} else {
$this->log("The path \"{$file}\" could not be found (or was not a file)");
return $options;
}
}
if ($sources) {
$this->sources = $sources;
}
return $options;
}
}

View File

@@ -0,0 +1,235 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_Controller_MinApp
* @package Minify
*
* NOTE: Fixes have been included in this file; look for "W3TC FIX".
*/
/**
* Controller class for requests to /min/index.php
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_Controller_MinApp extends Minify_Controller_Base {
/**
* Set up groups of files as sources
*
* @param array $options controller and Minify options
*
* @return array Minify options
*/
public function setupSources($options) {
// W3TC FIX: Override $_SERVER['DOCUMENT_ROOT'] if enabled in settings.
$docroot = \W3TC\Util_Environment::document_root();
// PHP insecure by default: realpath() and other FS functions can't handle null bytes.
foreach (array('g', 'b', 'f') as $key) {
if (isset($_GET[$key])) {
$_GET[$key] = str_replace("\x00", '', (string) sanitize_text_field( wp_unslash( $_GET[ $key ] ) ) );
}
}
// filter controller options
$cOptions = array_merge(
array(
'allowDirs' => '//'
,'groupsOnly' => false
,'groups' => array()
,'noMinPattern' => '@[-\\.]min\\.(?:js|css)$@i' // matched against basename
)
,(isset($options['minApp']) ? $options['minApp'] : array())
);
unset($options['minApp']);
$sources = array();
$this->selectionId = '';
$firstMissingResource = null;
if (isset($_GET['g'])) {
$g = sanitize_text_field( wp_unslash( $_GET['g'] ) );
// add group(s)
$this->selectionId .= 'g=' . $g;
$keys = explode(',', $g);
if ($keys != array_unique($keys)) {
$this->log("Duplicate group key found.");
return $options;
}
$keys = explode(',', $g);
foreach ($keys as $key) {
if (! isset($cOptions['groups'][$key])) {
$this->log("A group configuration for \"{$key}\" was not found");
return $options;
}
$files = $cOptions['groups'][$key];
// if $files is a single object, casting will break it
if (is_object($files)) {
$files = array($files);
} elseif (! is_array($files)) {
$files = (array)$files;
}
foreach ($files as $file) {
if ($file instanceof Minify_Source) {
$sources[] = $file;
continue;
}
if (0 === strpos($file, '//')) {
// W3TC FIX.
$file = $docroot . substr($file, 1);
}
$realpath = \W3TC\Util_Environment::realpath($file);
if ($realpath && is_file($realpath)) {
$sources[] = $this->_getFileSource($realpath, $cOptions);
} else {
$this->log("The path \"{$file}\" (realpath \"{$realpath}\") could not be found (or was not a file)");
if (null === $firstMissingResource) {
$firstMissingResource = basename($file);
continue;
} else {
$secondMissingResource = basename($file);
$this->log("More than one file was missing: '$firstMissingResource', '$secondMissingResource'");
return $options;
}
}
}
if ($sources) {
try {
$this->checkType($sources[0]);
} catch (\Exception $e) {
$this->log($e->getMessage());
return $options;
}
}
}
}
if (! $cOptions['groupsOnly'] && isset($_GET['f_array'])) {
$files = $_GET['f_array'];
$ext = isset( $_GET['ext'] ) ? sanitize_text_field( wp_unslash( $_GET['ext'] ) ) : '';
if (!empty($_GET['b'])) {
$b = sanitize_text_field( wp_unslash( $_GET['b'] ) );
// check for validity
if (preg_match('@^[^/]+(?:/[^/]+)*$@', $b)
&& false === strpos($b, '..')
&& $b !== '.') {
// valid base
$base = "/{$b}/";
} else {
$this->log("GET param 'b' invalid (see MinApp.php line 84)");
return $options;
}
} else {
$base = '/';
}
$allowDirs = array();
foreach ((array)$cOptions['allowDirs'] as $allowDir) {
// W3TC FIX.
$allowDirs[] = \W3TC\Util_Environment::realpath(str_replace('//', $docroot . '/', $allowDir));
}
$basenames = array(); // just for cache id
foreach ($files as $file) {
if ($file instanceof Minify_Source) {
$sources[] = $file;
continue;
}
$uri = $base . $file;
// W3TC FIX.
$path = $docroot . $uri;
$realpath = \W3TC\Util_Environment::realpath($path);
if (false === $realpath || ! is_file($realpath)) {
$this->log("The path \"{$path}\" (realpath \"{$realpath}\") could not be found (or was not a file)");
if (null === $firstMissingResource) {
$firstMissingResource = $uri;
continue;
} else {
$secondMissingResource = $uri;
$this->log("More than one file was missing: '$firstMissingResource', '$secondMissingResource`'");
return $options;
}
}
try {
parent::checkNotHidden($realpath);
parent::checkAllowDirs($realpath, $allowDirs, $uri);
} catch (\Exception $e) {
$this->log($e->getMessage());
return $options;
}
$sources[] = $this->_getFileSource($realpath, $cOptions);
$basenames[] = basename($realpath, $ext);
}
if ($this->selectionId) {
$this->selectionId .= '_f=';
}
$this->selectionId .= implode(',', $basenames) . $ext;
}
if ($sources) {
if (null !== $firstMissingResource) {
array_unshift($sources, new Minify_Source(array(
'id' => 'missingFile'
// should not cause cache invalidation
,'lastModified' => 0
// due to caching, filename is unreliable.
,'content' => "/* Minify: at least one missing file. See " . Minify::URL_DEBUG . " */\n"
,'minifier' => ''
)));
}
$this->sources = $sources;
} else {
$this->log("No sources to serve");
}
return $options;
}
/**
* @param string $file
*
* @param array $cOptions
*
* @return Minify_Source
*/
protected function _getFileSource($file, $cOptions)
{
$spec['filepath'] = $file;
if ($cOptions['noMinPattern'] && preg_match($cOptions['noMinPattern'], basename($file))) {
if (preg_match('~\.css$~i', $file)) {
$spec['minifyOptions']['compress'] = false;
} else {
$spec['minifier'] = '';
}
}
return new Minify_Source($spec);
}
protected $_type = null;
/**
* Make sure that only source files of a single type are registered
*
* @param string $sourceOrExt
*
* @throws Exception
*/
public function checkType($sourceOrExt)
{
if ($sourceOrExt === 'js') {
$type = Minify::TYPE_JS;
} elseif ($sourceOrExt === 'css') {
$type = Minify::TYPE_CSS;
} elseif ($sourceOrExt->contentType !== null) {
$type = $sourceOrExt->contentType;
} else {
return;
}
if ($this->_type === null) {
$this->_type = $type;
} elseif ($this->_type !== $type) {
throw new \Exception('Content-Type mismatch');
}
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_Controller_Page
* @package Minify
*/
/**
* Controller class for serving a single HTML page
*
* @link http://code.google.com/p/minify/source/browse/trunk/web/examples/1/index.php#59
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_Controller_Page extends Minify_Controller_Base {
/**
* Set up source of HTML content
*
* @param array $options controller and Minify options
* @return array Minify options
*
* Controller options:
*
* 'content': (required) HTML markup
*
* 'id': (required) id of page (string for use in server-side caching)
*
* 'lastModifiedTime': timestamp of when this content changed. This
* is recommended to allow both server and client-side caching.
*
* 'minifyAll': should all CSS and Javascript blocks be individually
* minified? (default false)
*
* @todo Add 'file' option to read HTML file.
*/
public function setupSources($options) {
if (isset($options['file'])) {
$sourceSpec = array(
'filepath' => $options['file']
);
$f = $options['file'];
} else {
// strip controller options
$sourceSpec = array(
'content' => $options['content']
,'id' => $options['id']
);
$f = $options['id'];
unset($options['content'], $options['id']);
}
// something like "builder,index.php" or "directory,file.html"
$this->selectionId = strtr(substr($f, 1 + strlen(dirname(dirname($f)))), '/\\', ',,');
if (isset($options['minifyAll'])) {
// this will be the 2nd argument passed to Minify_HTML::minify()
$sourceSpec['minifyOptions'] = array(
'cssMinifier' => array('\W3TCL\Minify\Minify_CSS', 'minify')
,'jsMinifier' => array('\W3TCL\Minify\JSMin', 'minify')
);
unset($options['minifyAll']);
}
$this->sources[] = new Minify_Source($sourceSpec);
$options['contentType'] = Minify::TYPE_HTML;
return $options;
}
}

View File

@@ -0,0 +1,131 @@
<?php
/**
* File: Version1.php
*
* NOTE: Fixes have been included in this file; look for "W3TC FIX".
*/
namespace W3TCL\Minify;
/**
* Class Minify_Controller_Version1
* @package Minify
*/
/**
* Controller class for emulating version 1 of minify.php (mostly a proof-of-concept)
*
* <code>
* Minify::serve('Version1');
* </code>
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_Controller_Version1 extends Minify_Controller_Base {
/**
* Set up groups of files as sources
*
* @param array $options controller and Minify options
* @return array Minify options
*
*/
public function setupSources($options) {
// PHP insecure by default: realpath() and other FS functions can't handle null bytes.
if (isset($_GET['files'])) {
$_GET['files'] = str_replace("\x00", '', (string) sanitize_text_field( wp_unslash( $_GET['files'] ) ) );
}
self::_setupDefines();
if (MINIFY_USE_CACHE) {
$cacheDir = defined('MINIFY_CACHE_DIR')
? MINIFY_CACHE_DIR
: '';
Minify::setCache($cacheDir);
}
$options['badRequestHeader'] = 'HTTP/1.0 404 Not Found';
$options['contentTypeCharset'] = MINIFY_ENCODING;
// The following restrictions are to limit the URLs that minify will
// respond to. Ideally there should be only one way to reference a file.
$files = isset( $_GET['files'] ) ? sanitize_text_field( wp_unslash( $_GET['files'] ) ) : '';
if (! isset($files)
// verify at least one file, files are single comma separated,
// and are all same extension
|| ! preg_match('/^[^,]+\\.(css|js)(,[^,]+\\.\\1)*$/', $files, $m)
// no "//" (makes URL rewriting easier)
|| strpos($files, '//') !== false
// no "\"
|| strpos($files, '\\') !== false
// no "./"
|| preg_match('/(?:^|[^\\.])\\.\\//', $files)
) {
return $options;
}
$files = explode(',', $files);
if (count($files) > MINIFY_MAX_FILES) {
return $options;
}
// W3TC FIX: Override $_SERVER['DOCUMENT_ROOT'] if enabled in settings.
$docroot = \W3TC\Util_Environment::document_root();
// strings for prepending to relative/absolute paths
$prependRelPaths = dirname( isset( $_SERVER['SCRIPT_FILENAME'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SCRIPT_FILENAME'] ) ) : '' )
. DIRECTORY_SEPARATOR;
$prependAbsPaths = $docroot;
$goodFiles = array();
$hasBadSource = false;
$allowDirs = isset($options['allowDirs'])
? $options['allowDirs']
: MINIFY_BASE_DIR;
foreach ($files as $file) {
// prepend appropriate string for abs/rel paths
$file = ($file[0] === '/' ? $prependAbsPaths : $prependRelPaths) . $file;
// make sure a real file!
$file = realpath($file);
// don't allow unsafe or duplicate files
if (parent::_fileIsSafe($file, $allowDirs)
&& !in_array($file, $goodFiles))
{
$goodFiles[] = $file;
$srcOptions = array(
'filepath' => $file
);
$this->sources[] = new Minify_Source($srcOptions);
} else {
$hasBadSource = true;
break;
}
}
if ($hasBadSource) {
$this->sources = array();
}
if (! MINIFY_REWRITE_CSS_URLS) {
$options['rewriteCssUris'] = false;
}
return $options;
}
private static function _setupDefines()
{
// W3TC FIX: Override $_SERVER['DOCUMENT_ROOT'] if enabled in settings.
$docroot = \W3TC\Util_Environment::document_root();
$defaults = array(
'MINIFY_BASE_DIR' => realpath($docroot)
,'MINIFY_ENCODING' => 'utf-8'
,'MINIFY_MAX_FILES' => 16
,'MINIFY_REWRITE_CSS_URLS' => true
,'MINIFY_USE_CACHE' => true
);
foreach ($defaults as $const => $val) {
if (! defined($const)) {
define($const, $val);
}
}
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace W3TCL\Minify;
/**
* Detect whether request should be debugged
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_DebugDetector {
public static function shouldDebugRequest($cookie, $get, $requestUri)
{
if (isset($get['debug'])) {
return true;
}
if (! empty($cookie['minifyDebug'])) {
foreach (preg_split('/\\s+/', $cookie['minifyDebug']) as $debugUri) {
$pattern = '@' . preg_quote($debugUri, '@') . '@i';
$pattern = str_replace(array('\\*', '\\?'), array('.*', '.'), $pattern);
if (preg_match($pattern, $requestUri)) {
return true;
}
}
}
return false;
}
}

View File

@@ -0,0 +1,390 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_HTML
* @package Minify
*/
/**
* Compress HTML
*
* This is a heavy regex-based removal of whitespace, unnecessary comments and
* tokens. IE conditional comments are preserved. There are also options to have
* STYLE and SCRIPT blocks compressed by callback functions.
*
* A test suite is available.
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_HTML {
/**
* @var boolean
*/
protected $_jsCleanComments = true;
/**
* @var string
*/
protected $_html = '';
/**
* "Minify" an HTML page
*
* @param string $html
*
* @param array $options
*
* 'cssMinifier' : (optional) callback function to process content of STYLE
* elements.
*
* 'jsMinifier' : (optional) callback function to process content of SCRIPT
* elements. Note: the type attribute is ignored.
*
* 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
* unset, minify will sniff for an XHTML doctype.
*
* @return string
*/
public static function minify($html, $options = array()) {
$min = new self($html, $options);
return $min->process();
}
/**
* Create a minifier object
*
* @param string $html
*
* @param array $options
*
* 'cssMinifier' : (optional) callback function to process content of STYLE
* elements.
*
* 'jsMinifier' : (optional) callback function to process content of SCRIPT
* elements. Note: the type attribute is ignored.
*
* 'jsCleanComments' : (optional) whether to remove HTML comments beginning and end of script block
*
* 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
* unset, minify will sniff for an XHTML doctype.
*
* @return null
*/
public function __construct($html, $options = array())
{
$this->_html = str_replace("\r\n", "\n", trim($html));
if (isset($options['xhtml'])) {
$this->_isXhtml = (bool)$options['xhtml'];
}
if (isset($options['cssMinifier'])) {
$this->_cssMinifier = $options['cssMinifier'];
}
if (isset($options['jsMinifier'])) {
$this->_jsMinifier = $options['jsMinifier'];
}
$this->_stripCrlf = (isset($options['stripCrlf']) ? (boolean) $options['stripCrlf'] : false) ;
$this->_ignoredComments = (isset($options['ignoredComments']) ? (array) $options['ignoredComments'] : array());
}
/**
* Minify the markeup given in the constructor
*
* @return string
*/
public function process()
{
if ($this->_isXhtml === null) {
$this->_isXhtml = (false !== strpos($this->_html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'));
}
$this->_replacementHash = 'MINIFYHTML' . md5( isset( $_SERVER['REQUEST_TIME'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_TIME'] ) ) : '' );
$this->_placeholders = array();
// replace dynamic tags
$this->_html = preg_replace_callback(
'~(<!--\s*m(func|clude)(.*)-->\s*<!--\s*/m(func|clude)\s*-->)~is'
,array($this, '_removeComment')
,$this->_html);
// replace SCRIPTs (and minify) with placeholders
$this->_html = preg_replace_callback(
'/(\\s*)<script(\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i'
,array($this, '_removeScriptCB')
,$this->_html);
// replace STYLEs (and minify) with placeholders
$this->_html = preg_replace_callback(
'/\\s*<style(\\b[^>]*>)([\\s\\S]*?)<\\/style>\\s*/i'
,array($this, '_removeStyleCB')
,$this->_html);
// remove HTML comments (not containing IE conditional comments).
$this->_html = preg_replace_callback(
'/<!--([\\s\\S]*?)-->/'
,array($this, '_commentCB')
,$this->_html);
// replace PREs with placeholders
$this->_html = preg_replace_callback('/\\s*<pre(\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
,array($this, '_removePreCB')
,$this->_html);
// replace TEXTAREAs with placeholders
$this->_html = preg_replace_callback(
'/\\s*<textarea(\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
,array($this, '_removeTextareaCB')
,$this->_html);
// trim each line.
// @todo take into account attribute values that span multiple lines.
$this->_html = preg_replace('/^\\s+|\\s+$/m', '', $this->_html);
// remove ws around block/undisplayed elements
$this->_html = preg_replace('/\\s+(<\\/?(?:area|article|aside|base(?:font)?|blockquote|body'
.'|canvas|caption|center|col(?:group)?|dd|dir|div|dl|dt|fieldset|figcaption|figure|footer|form'
.'|frame(?:set)?|h[1-6]|head|header|hgroup|hr|html|legend|link|main|map|menu|meta|nav'
.'|ol|opt(?:group|ion)|output|p|param|section|t(?:able|body|head|d|h||r|foot|itle)'
.'|ul|video)\\b[^>]*>)/i', '$1', $this->_html);
// remove whitespaces outside of all elements
$this->_html = preg_replace(
'/>((\\s)(?:\\s*))?([^<]+?)((\\s)(?:\\s*))?</'
,'>$2$3$5<'
,$this->_html);
// remove whitespaces before end of all empty elements
$this->_html = preg_replace(
'/\\s*\\/>/'
,'/>'
,$this->_html);
// remove trailing slash from void elements
$_html = preg_replace(
'~<(area|base|br|col|command|embed|hr|img|input|keygen|link|meta|param|source|track|wbr)(([^\'">]|\"[^\"]*\"|\'[^\']*\'|)*?)\\s*[/]?>~i'
,'<$1$2>'
,$this->_html);
// Avoid PREG_JIT_STACKLIMIT_ERROR. Thanks @ericek111 for https://github.com/BoldGrid/w3-total-cache/issues/190.
if ( preg_last_error() === PREG_NO_ERROR ) {
$this->_html = $_html;
}
unset( $_html );
// use newlines before 1st attribute in open tags (to limit line lengths)
$this->_html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $this->_html);
if ($this->_stripCrlf) {
$this->_html = preg_replace("~[\r\n]+~", ' ', $this->_html);
} else {
$this->_html = preg_replace("~[\r\n]+~", "\n", $this->_html);
}
// fill placeholders
$this->_html = str_replace(
array_keys($this->_placeholders)
,array_values($this->_placeholders)
,$this->_html
);
// issue 229: multi-pass to catch scripts that didn't get replaced in textareas
$this->_html = str_replace(
array_keys($this->_placeholders)
,array_values($this->_placeholders)
,$this->_html
);
// in HTML5, type attribute is unnecessary for JavaScript resources
// in HTML5, type attribute for style element is not needed and should be omitted
if (false !== stripos($this->_html, '<!doctype html>')) {
$this->_html = preg_replace(
'/<(script|style)([^>]*)\\stype=[\'"]?(text\\/javascript|text\\/css|application\\/javascript)[\'"]?([^>]*)>/i'
,'<$1$2$4>'
,$this->_html);
}
// unquote attribute values without spaces
$this->_html = preg_replace_callback(
'/(<([a-z\\-]+)\\s)\\s*([^>]+>)/m'
,array($this, '_removeAttributeQuotes')
,$this->_html);
return $this->_html;
}
protected function _commentCB($m)
{
return (0 === strpos($m[1], '[') || false !== strpos($m[1], '<![') || $this->_ignoredComment($m[1]))
? $m[0]
: '';
}
protected function _ignoredComment($comment)
{
foreach ($this->_ignoredComments as $ignoredComment) {
if (!empty($ignoredComment) && stristr($comment, $ignoredComment) !== false) {
return true;
}
}
return false;
}
protected function _reservePlace($content)
{
$placeholder = '%' . $this->_replacementHash . count($this->_placeholders) . '%';
$this->_placeholders[$placeholder] = $content;
return $placeholder;
}
protected $_isXhtml = null;
protected $_replacementHash = null;
protected $_placeholders = array();
protected $_cssMinifier = null;
protected $_jsMinifier = null;
protected $_stripCrlf = null;
protected $_ignoredComments = null;
protected function _removePreCB($m)
{
return $this->_reservePlace("<pre{$m[1]}");
}
protected function _removeTextareaCB($m)
{
return $this->_reservePlace("<textarea{$m[1]}");
}
protected function _removeStyleCB($m)
{
$openStyle = "<style{$m[1]}";
$css = $m[2];
// remove HTML comments
$css = preg_replace('/(?:^\\s*<!--|-->\\s*$)/', '', $css);
// remove CDATA section markers
$css = $this->_removeCdata($css);
// minify
$minifier = $this->_cssMinifier
? $this->_cssMinifier
: 'trim';
$css = call_user_func($minifier, $css);
return $this->_reservePlace($this->_needsCdata($css)
? "{$openStyle}/*<![CDATA[*/{$css}/*]]>*/</style>"
: "{$openStyle}{$css}</style>"
);
}
protected function _removeScriptCB($m)
{
$openScript = "<script{$m[2]}";
$js = $m[3];
$script_tag = "<script{$m[2]}>{$js}</script>";
$type = '';
if (preg_match('#type="([^"]+)"#i', $m[2], $matches)) {
$type = strtolower($matches[1]);
}
// whitespace surrounding? preserve at least one space
$ws1 = ($m[1] === '') ? '' : ' ';
$ws2 = ($m[4] === '') ? '' : ' ';
// remove HTML comments (and ending "//" if present)
if ($this->_jsCleanComments) {
$js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
}
// minify
$minifier = $this->_jsMinifier
? $this->_jsMinifier
: 'trim';
if (in_array($type, array('text/template', 'text/x-handlebars-template'))) {
$minifier = '';
}
$minifier = apply_filters('w3tc_minify_html_script_minifier', $minifier, $type, $script_tag);
if (empty($minifier)) {
$needsCdata = false;
} else {
// remove CDATA section markers
$js_old = $js;
$js = $this->_removeCdata($js);
$needsCdata = ( $js_old != $js );
$js = call_user_func($minifier, $js);
}
return $this->_reservePlace($needsCdata && $this->_needsCdata($js)
? "{$ws1}{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>{$ws2}"
: "{$ws1}{$openScript}{$js}</script>{$ws2}"
);
}
protected function _removeCdata($str)
{
if (false !== strpos($str, '<![CDATA[')) {
$str = str_replace('//<![CDATA[', '', $str);
$str = preg_replace('~/\*\s*<!\[CDATA\[\s*\*/~', '', $str);
$str = str_replace('<![CDATA[', '', $str);
$str = str_replace('//]]>', '', $str);
$str = preg_replace('~/\*\s*\]\]>\s*\*/~', '', $str);
$str = str_replace(']]>', '', $str);
}
return $str;
}
protected function _removeComment($m)
{
return $this->_reservePlace($m[1]);
}
protected function _needsCdata($str)
{
return ($this->_isXhtml && preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $str));
}
protected function _removeAttributeQuotes($m) {
// whatsapp/fb bots dont read meta tags without quotes well
if (strtolower($m[2]) != 'meta') {
$m[3] = preg_replace_callback( '~([a-z0-9\\-])=(?<quote>[\'"])([^"\'\\s=]*)\k<quote>(\\s|>|/>)~i',
array( $this, '_removeAttributeQuotesCallback'), $m[3] );
}
return $m[1] . $m[3];
}
public function _removeAttributeQuotesCallback( $m ) {
// empty tag values like <div data-value=""> to <div data-value
if ( $m[3] === '' ) {
return $m[1] . $m[4];
}
// 1. <a href=bla/>hi</a> is sometimes (XHTML? HTML5 specs doesnt allow that)
// parsed as <a href=bla></a>hi</a> by browsers
// avoid that by turning it to <a href=bla/ >hi</a>
// 2. auto-closing tags without space at the end e.g. <div data-value="aa"/>
// should have space after value <div data-value=aa />
// otherwise some browsers assume data-value="aa/"
if ( /* 1 */ $m[4] == '/>' ||
/* 2 */ ( $m[4] == '>' && substr( $m[3], -1, 1 ) == '/' ) ) {
return $m[1] . '=' . $m[3] . ' ' . $m[4];
}
return $m[1] . '=' . $m[3] . $m[4];
}
}

View File

@@ -0,0 +1,238 @@
<?php
/**
* File: Helper.php
*
* NOTE: Fixes have been included in this file; look for "W3TC FIX".
*/
namespace W3TCL\Minify;
/**
* Class Minify_HTML_Helper
* @package Minify
*/
/**
* Helpers for writing Minfy URIs into HTML
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_HTML_Helper {
public $rewriteWorks = true;
public $minAppUri = '/min';
public $groupsConfigFile = '';
/**
* Get an HTML-escaped Minify URI for a group or set of files
*
* @param string|array $keyOrFiles a group key or array of filepaths/URIs
* @param array $opts options:
* 'farExpires' : (default true) append a modified timestamp for cache revving
* 'debug' : (default false) append debug flag
* 'charset' : (default 'UTF-8') for htmlspecialchars
* 'minAppUri' : (default '/min') URI of min directory
* 'rewriteWorks' : (default true) does mod_rewrite work in min app?
* 'groupsConfigFile' : specify if different
* @return string
*/
public static function getUri($keyOrFiles, $opts = array())
{
$opts = array_merge(array( // default options
'farExpires' => true
,'debug' => false
,'charset' => 'UTF-8'
,'minAppUri' => '/min'
,'rewriteWorks' => true
,'groupsConfigFile' => ''
), $opts);
$h = new self;
$h->minAppUri = $opts['minAppUri'];
$h->rewriteWorks = $opts['rewriteWorks'];
$h->groupsConfigFile = $opts['groupsConfigFile'];
if (is_array($keyOrFiles)) {
$h->setFiles($keyOrFiles, $opts['farExpires']);
} else {
$h->setGroup($keyOrFiles, $opts['farExpires']);
}
$uri = $h->getRawUri($opts['farExpires'], $opts['debug']);
return htmlspecialchars($uri, ENT_QUOTES, $opts['charset']);
}
/**
* Get non-HTML-escaped URI to minify the specified files
*
* @param bool $farExpires
* @param bool $debug
* @return string
*/
public function getRawUri($farExpires = true, $debug = false)
{
$path = rtrim($this->minAppUri, '/') . '/';
if (! $this->rewriteWorks) {
$path .= '?';
}
if (null === $this->_groupKey) {
// @todo: implement shortest uri
$path = self::_getShortestUri($this->_filePaths, $path);
} else {
$path .= "g=" . $this->_groupKey;
}
if ($debug) {
$path .= "&debug";
} elseif ($farExpires && $this->_lastModified) {
$path .= "&" . $this->_lastModified;
}
return $path;
}
/**
* Set the files that will comprise the URI we're building
*
* @param array $files
* @param bool $checkLastModified
*/
public function setFiles($files, $checkLastModified = true)
{
// W3TC FIX: Override $_SERVER['DOCUMENT_ROOT'] if enabled in settings.
$docroot = \W3TC\Util_Environment::document_root();
$this->_groupKey = null;
if ($checkLastModified) {
$this->_lastModified = self::getLastModified($files);
}
// normalize paths like in /min/f=<paths>
foreach ($files as $k => $file) {
if (0 === strpos($file, '//')) {
$file = substr($file, 2);
} elseif (0 === strpos($file, '/')
|| 1 === strpos($file, ':\\')) {
$file = substr($file, strlen($docroot) + 1);
}
$file = strtr($file, '\\', '/');
$files[$k] = $file;
}
$this->_filePaths = $files;
}
/**
* Set the group of files that will comprise the URI we're building
*
* @param string $key
* @param bool $checkLastModified
*/
public function setGroup($key, $checkLastModified = true)
{
$this->_groupKey = $key;
if ($checkLastModified) {
if (! $this->groupsConfigFile) {
$this->groupsConfigFile = dirname(dirname(dirname(dirname(__FILE__)))) . '/groupsConfig.php';
}
if (is_file($this->groupsConfigFile)) {
$gc = (require $this->groupsConfigFile);
$keys = explode(',', $key);
foreach ($keys as $key) {
if (isset($gc[$key])) {
$this->_lastModified = self::getLastModified($gc[$key], $this->_lastModified);
}
}
}
}
}
/**
* Get the max(lastModified) of all files
*
* @param array|string $sources
* @param int $lastModified
* @return int
*/
public static function getLastModified($sources, $lastModified = 0)
{
// W3TC FIX: Override $_SERVER['DOCUMENT_ROOT'] if enabled in settings.
$docroot = \W3TC\Util_Environment::document_root();
$max = $lastModified;
foreach ((array)$sources as $source) {
if (is_object($source) && isset($source->lastModified)) {
$max = max($max, $source->lastModified);
} elseif (is_string($source)) {
if (0 === strpos($source, '//')) {
$source = $docroot . substr($source, 1);
}
if (is_file($source)) {
$max = max($max, filemtime($source));
}
}
}
return $max;
}
protected $_groupKey = null; // if present, URI will be like g=...
protected $_filePaths = array();
protected $_lastModified = null;
/**
* In a given array of strings, find the character they all have at
* a particular index
*
* @param array $arr array of strings
* @param int $pos index to check
* @return mixed a common char or '' if any do not match
*/
protected static function _getCommonCharAtPos($arr, $pos) {
if (!isset($arr[0][$pos])) {
return '';
}
$c = $arr[0][$pos];
$l = count($arr);
if ($l === 1) {
return $c;
}
for ($i = 1; $i < $l; ++$i) {
if ($arr[$i][$pos] !== $c) {
return '';
}
}
return $c;
}
/**
* Get the shortest URI to minify the set of source files
*
* @param array $paths root-relative URIs of files
* @param string $minRoot root-relative URI of the "min" application
* @return string
*/
protected static function _getShortestUri($paths, $minRoot = '/min/') {
$pos = 0;
$base = '';
while (true) {
$c = self::_getCommonCharAtPos($paths, $pos);
if ($c === '') {
break;
} else {
$base .= $c;
}
++$pos;
}
$base = preg_replace('@[^/]+$@', '', $base);
$uri = $minRoot . 'f=' . implode(',', $paths);
if (substr($base, -1) === '/') {
// we have a base dir!
$basedPaths = $paths;
$l = count($paths);
for ($i = 0; $i < $l; ++$i) {
$basedPaths[$i] = substr($paths[$i], strlen($base));
}
$base = substr($base, 0, strlen($base) - 1);
$bUri = $minRoot . 'b=' . $base . '&f=' . implode(',', $basedPaths);
$uri = strlen($uri) < strlen($bUri)
? $uri
: $bUri;
}
return $uri;
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace W3TCL\Minify;
class Minify_HTMLTidy {
public static function minify($content, $options = array()) {
$options = array_merge(array(
'clean' => false,
'hide-comments' => true,
'wrap' => 0,
'input-encoding' => 'utf8',
'output-encoding' => 'utf8',
'preserve-entities' => true
), $options, array(
'show-errors' => 0,
'show-warnings' => false,
'force-output' => true,
'tidy-mark' => false,
'output-xhtml' => false,
));
$tidy = new \tidy();
$tidy->parseString($content, $options);
$tidy->cleanRepair();
$content = $tidy->value;
return $content;
}
public static function minifyXhtml($html, $options = array()) {
$options = array_merge($options, array(
'output-xhtml' => true
));
return self::minify($html, $options);
}
public static function minifyXml($xml, $options = array()) {
$options = array_merge($options, array(
'input-xml' => true,
'output-xml' => true,
'add-xml-decl' => true
));
return self::minify($xml, $options);
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace W3TCL\Minify;
class Minify_IgnoredCommentPreserver {
protected $_replacementHash = '';
protected $_ignoredComments = array();
protected $_placeholders = array();
public function __construct() {
$this->_replacementHash = 'IgnoredCommentPreserver_' . md5(time());
}
public function setIgnoredComments($ignoredComments = array()) {
$this->_ignoredComments = $ignoredComments;
}
public function search($html) {
$html = preg_replace_callback('/<!--[\\s\\S]*?-->/',
array($this, '_callback'),
$html);
return $html;
}
public function replace($html) {
$html = str_replace(array_keys($this->_placeholders),
array_values($this->_placeholders),
$html);
return $html;
}
protected function _callback($match) {
list($comment) = $match;
if ($this->_isIgnoredComment($comment)) {
return $this->_reservePlace($comment);
}
return $comment;
}
protected function _isIgnoredComment(&$comment) {
foreach ($this->_ignoredComments as $ignoredComment) {
if ( ! empty( $ignoredComment ) && stristr($comment, $ignoredComment ) !== false) {
return true;
}
}
return false;
}
protected function _getPlaceholder() {
return '%%' . $this->_replacementHash . '_' . count($this->_placeholders) . '%%';
}
protected function _reservePlace(&$content) {
$placeholder = $this->_getPlaceholder();
$this->_placeholders[$placeholder] = &$content;
return $placeholder;
}
}

View File

@@ -0,0 +1,223 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_ImportProcessor
* @package Minify
*/
/**
* Linearize a CSS/JS file by including content specified by CSS import
* declarations. In CSS files, relative URIs are fixed.
*
* @imports will be processed regardless of where they appear in the source
* files; i.e. @imports commented out or in string content will still be
* processed!
*
* This has a unit test but should be considered "experimental".
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
* @author Simon Schick <simonsimcity@gmail.com>
*/
class Minify_ImportProcessor {
public static $filesIncluded = array();
public static function process($file)
{
self::$filesIncluded = array();
self::$_isCss = (strtolower(substr($file, -4)) === '.css');
$obj = new Minify_ImportProcessor(dirname($file));
return $obj->_getContent($file);
}
// allows callback funcs to know the current directory
private $_currentDir = null;
// allows callback funcs to know the directory of the file that inherits this one
private $_previewsDir = null;
// allows _importCB to write the fetched content back to the obj
private $_importedContent = '';
private static $_isCss = null;
/**
* @param String $currentDir
* @param String $previewsDir Is only used internally
*/
private function __construct($currentDir, $previewsDir = "")
{
$this->_currentDir = $currentDir;
$this->_previewsDir = $previewsDir;
}
private function _getContent($file, $is_imported = false)
{
$file = realpath($file);
if (! $file
|| in_array($file, self::$filesIncluded)
|| false === ($content = @file_get_contents($file))
) {
// file missing, already included, or failed read
return '';
}
self::$filesIncluded[] = realpath($file);
$this->_currentDir = dirname($file);
// remove UTF-8 BOM if present
if (pack("CCC",0xef,0xbb,0xbf) === substr($content, 0, 3)) {
$content = substr($content, 3);
}
// ensure uniform EOLs
$content = str_replace("\r\n", "\n", $content);
// process @imports
$content = preg_replace_callback(
'/
@import\\s+
(?:url\\(\\s*)? # maybe url(
[\'"]? # maybe quote
(.*?) # 1 = URI
[\'"]? # maybe end quote
(?:\\s*\\))? # maybe )
([a-zA-Z,\\s]*)? # 2 = media list
; # end token
/x'
,array($this, '_importCB')
,$content
);
// You only need to rework the import-path if the script is imported
if (self::$_isCss && $is_imported) {
// rewrite remaining relative URIs
$content = preg_replace_callback(
'/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
,array($this, '_urlCB')
,$content
);
}
return $this->_importedContent . $content;
}
private function _importCB($m)
{
$url = $m[1];
$mediaList = preg_replace('/\\s+/', '', $m[2]);
if (strpos($url, '://') > 0) {
// protocol, leave in place for CSS, comment for JS
return self::$_isCss
? $m[0]
: "/* Minify_ImportProcessor will not include remote content */";
}
if ('/' === $url[0]) {
// protocol-relative or root path
$url = ltrim($url, '/');
// W3TC FIX: Override $_SERVER['DOCUMENT_ROOT'] if enabled in settings.
$docroot = \W3TC\Util_Environment::document_root();
$file = realpath($docroot) . DIRECTORY_SEPARATOR
. strtr($url, '/', DIRECTORY_SEPARATOR);
} else {
// relative to current path
$file = $this->_currentDir . DIRECTORY_SEPARATOR
. strtr($url, '/', DIRECTORY_SEPARATOR);
}
$obj = new Minify_ImportProcessor(dirname($file), $this->_currentDir);
$content = $obj->_getContent($file, true);
if ('' === $content) {
// failed. leave in place for CSS, comment for JS
return self::$_isCss
? $m[0]
: "/* Minify_ImportProcessor could not fetch '{$file}' */";
}
return (!self::$_isCss || preg_match('@(?:^$|\\ball\\b)@', $mediaList))
? $content
: "@media {$mediaList} {\n{$content}\n}\n";
}
private function _urlCB($m)
{
// $m[1] is either quoted or not
$quote = ($m[1][0] === "'" || $m[1][0] === '"')
? $m[1][0]
: '';
$url = ($quote === '')
? $m[1]
: substr($m[1], 1, strlen($m[1]) - 2);
if ('/' !== $url[0]) {
if (false === strpos($url, '//') // protocol (non-data)
&& 0 !== strpos($url, 'data:')) { // data protocol
// probably starts with protocol, do not alter
} else {
// prepend path with current dir separator (OS-independent)
$path = $this->_currentDir
. DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR);
// update the relative path by the directory of the file that imported this one
$url = self::getPathDiff(realpath($this->_previewsDir), $path);
}
}
return "url({$quote}{$url}{$quote})";
}
/**
* @param string $from
* @param string $to
* @param string $ps
* @return string
*/
private function getPathDiff($from, $to, $ps = DIRECTORY_SEPARATOR)
{
$realFrom = $this->truepath($from);
$realTo = $this->truepath($to);
$arFrom = explode($ps, rtrim($realFrom, $ps));
$arTo = explode($ps, rtrim($realTo, $ps));
while (count($arFrom) && count($arTo) && ($arFrom[0] == $arTo[0]))
{
array_shift($arFrom);
array_shift($arTo);
}
return str_pad("", count($arFrom) * 3, '..' . $ps) . implode($ps, $arTo);
}
/**
* This function is to replace PHP's extremely buggy realpath().
* @param string $path The original path, can be relative etc.
* @return string The resolved path, it might not exist.
* @see http://stackoverflow.com/questions/4049856/replace-phps-realpath
*/
function truepath($path)
{
// whether $path is unix or not
$unipath = strlen($path) == 0 || substr($path, 0, 1) != '/';
// attempts to detect if path is relative in which case, add cwd
if (strpos($path, ':') === false && $unipath)
$path = $this->_currentDir . DIRECTORY_SEPARATOR . $path;
// resolve path parts (single dot, double dot and double delimiters)
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
$absolutes = array();
foreach ($parts as $part) {
if ('.' == $part)
continue;
if ('..' == $part) {
array_pop($absolutes);
} else {
$absolutes[] = $part;
}
}
$path = implode(DIRECTORY_SEPARATOR, $absolutes);
// resolve any symlinks
if (file_exists($path) && linkinfo($path) > 0)
$path = readlink($path);
// put initial separator that could have been lost
$path = !$unipath ? '/' . $path : $path;
return $path;
}
}

View File

@@ -0,0 +1,97 @@
<?php
namespace W3TCL\Minify;
abstract class Minify_Inline {
protected $_tag = '';
protected $_minifier = null;
protected $_minifierOptions = array();
//abstract static function minify($content, $minifier, $options = array());
public function setTag($tag) {
$this->_tag = $tag;
}
public function setMinifier($minifier) {
$this->_minifier = $minifier;
}
public function setMinifierOptions($minifierOptions = array()) {
$this->_minifierOptions = $minifierOptions;
}
public function doMinify($content) {
$search = '/(<' . $this->_tag . '\\b[^>]*?>)([\\s\\S]*?)(<\\/' . $this->_tag . '>)/i';
$content = preg_replace_callback($search, array($this, '_callback'), $content);
return $content;
}
protected function _callback($match) {
list(, $openTag, $content, $closeTag) = $match;
$content = $this->_process($openTag, $content, $closeTag);
return $content;
}
protected function _process($openTag, $content, $closeTag) {
$type = '';
if (preg_match('#type="([^"]+)"#i', $openTag, $matches)) {
$type = strtolower($matches[1]);
}
// minify
$minifier = $this->_minifier;
if (in_array($type, array('text/template', 'text/x-handlebars-template'))) {
$minifier = '';
}
$minifier = apply_filters('w3tc_minify_html_script_minifier', $minifier, $type, $openTag . $content . $closeTag);
if (empty($minifier)) {
$needsCdata = false;
} else {
// remove CDATA section markers
$content_old = $content;
$content = $this->_removeCdata($content);
$needsCdata = ( $content_old != $content );
$content = call_user_func($minifier, $content, $this->_minifierOptions);
}
if ($needsCdata && $this->_needsCdata($content)) {
$content = $this->_wrapCdata($content);
}
$content = $openTag . $content . $closeTag;
return $content;
}
protected function _needsCdata($content) {
return preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $content);
}
protected function _removeCdata($content) {
if (false !== strpos($content, '<![CDATA[')) {
$content = str_replace('//<![CDATA[', '', $content);
$content = str_replace('/*<![CDATA[*/', '', $content);
$content = str_replace('<![CDATA[', '', $content);
$content = str_replace('//]]>', '', $content);
$content = str_replace('/*]]>*/', '', $content);
$content = str_replace(']]>', '', $content);
}
return $content;
}
protected function _wrapCdata($content) {
$content = '/*<![CDATA[*/' . $content . '/*]]>*/';
return $content;
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace W3TCL\Minify;
if (!defined('W3TC')) {
die();
}
class Minify_Inline_CSS extends Minify_Inline {
public static function minify($content, $minifier, $options = array()) {
$inline = new self;
$inline->setTag('style');
$inline->setMinifier($minifier);
$inline->setMinifierOptions($options);
$content = $inline->doMinify($content);
return $content;
}
protected function _process($openTag, $content, $closeTag) {
$content = preg_replace('/(?:^\\s*<!--|-->\\s*$)/', '', $content);
return parent::_process($openTag, $content, $closeTag);
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace W3TCL\Minify;
if (!defined('W3TC')) {
die();
}
class Minify_Inline_JavaScript extends Minify_Inline {
public static function minify($content, $minifier, $options = array()) {
$inline = new self;
$inline->setTag('script');
$inline->setMinifier($minifier);
$inline->setMinifierOptions($options);
$content = $inline->doMinify($content);
return $content;
}
protected function _process($openTag, $content, $closeTag) {
$content = preg_replace('/(?:^\\s*(?:\\/\\/)?\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $content);
return parent::_process($openTag, $content, $closeTag);
}
}

View File

@@ -0,0 +1,159 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_JS_ClosureCompiler
* @package Minify
*/
/**
* Minify Javascript using Google's Closure Compiler API
*
* @link http://code.google.com/closure/compiler/
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*
* @todo can use a stream wrapper to unit test this?
*/
class Minify_JS_ClosureCompiler {
const URL = 'https://closure-compiler.appspot.com/compile';
/**
* Minify Javascript code via HTTP request to the Closure Compiler API
*
* @param string $js input code
* @param array $options unused at this point
* @return string
*/
public static function minify($js, array $options = array())
{
$obj = new self($options);
return $obj->min($js);
}
/**
*
* @param array $options
*
* fallbackFunc : default array($this, 'fallback');
*/
public function __construct(array $options = array())
{
$this->options = $options;
$this->_fallbackFunc = isset($options['fallbackMinifier'])
? $options['fallbackMinifier']
: array($this, '_fallback');
}
public function min($js)
{
if (trim($js) === '')
return $js;
$postBody = $this->_buildPostBody($js);
$bytes = (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2))
? mb_strlen($postBody, '8bit')
: strlen($postBody);
if ($bytes > 200000)
return $this->fail($js,
'File size is larger than Closure Compiler API limit (200000 bytes)');
$response = $this->_getResponse($postBody);
if (preg_match('/^Error\(\d\d?\):/', $response))
return $this->fail($response,
"Received errors from Closure Compiler API:\n$response");
return $response;
}
private function fail($js, $errorMessage) {
Minify::$recoverableError = $errorMessage;
$response = "/* " . $errorMessage . "\n(Using fallback minifier)\n*/\n";
if (is_callable($this->_fallbackFunc))
$response .= call_user_func($this->_fallbackFunc, $js);
else
$response .= $js;
return $response;
}
protected $_fallbackFunc = null;
protected $_options = array();
protected function _getResponse($postBody)
{
$allowUrlFopen = preg_match('/1|yes|on|true/i', ini_get('allow_url_fopen'));
if ($allowUrlFopen) {
$contents = file_get_contents(self::URL, false, stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\nConnection: close\r\n",
'content' => $postBody,
'max_redirects' => 0,
'timeout' => 15,
)
)));
} elseif (defined('CURLOPT_POST')) {
$ch = curl_init(self::URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
$contents = curl_exec($ch);
curl_close($ch);
} else {
throw new Minify_JS_ClosureCompiler_Exception(
"Could not make HTTP request: allow_url_open is false and cURL not available"
);
}
if (false === $contents) {
throw new Minify_JS_ClosureCompiler_Exception(
"No HTTP response from server"
);
}
return trim($contents);
}
protected function _buildPostBody($js, $returnErrors = false)
{
$a = array(
'js_code' => $js,
'output_info' => ($returnErrors ? 'errors' : 'compiled_code'),
'output_format' => 'text',
'compilation_level' =>
(isset($this->options['compilation_level']) ?
$this->options['compilation_level'] :
'SIMPLE_OPTIMIZATIONS')
);
if (isset($this->options['formatting']) && !empty($this->options['formatting']))
$a['formatting'] = $this->options['formatting'];
return http_build_query($a, null, '&');
}
/**
* Default fallback function if CC API fails
* @param string $js
* @return string
*/
protected function _fallback($js)
{
return JSMin::minify($js);
}
public static function test(&$error) {
try {
self::minify('alert("ok");');
$error = 'OK';
return true;
} catch (\Exception $exception) {
$error = $exception->getMessage();
return false;
}
}
}
class Minify_JS_ClosureCompiler_Exception extends \Exception {}

View File

@@ -0,0 +1,221 @@
<?php
/**
* File: Lines.php
*
* NOTE: Fixes have been included in this file; look for "W3TC FIX".
*/
namespace W3TCL\Minify;
/**
* Class Minify_Lines
* @package Minify
*/
/**
* Add line numbers in C-style comments for easier debugging of combined content
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
* @author Adam Pedersen (Issue 55 fix)
*/
class Minify_Lines
{
/**
* Add line numbers in C-style comments
*
* This uses a very basic parser easily fooled by comment tokens inside
* strings or regexes, but, otherwise, generally clean code will not be
* mangled. URI rewriting can also be performed.
*
* @param string $content
*
* @param array $options available options:
*
* 'id': (optional) string to identify file. E.g. file name/path
*
* 'currentDir': (default null) if given, this is assumed to be the
* directory of the current CSS file. Using this, minify will rewrite
* all relative URIs in import/url declarations to correctly point to
* the desired files, and prepend a comment with debugging information about
* this process.
*
* @return string
*/
public static function minify($content, $options = array())
{
$id = (isset($options['id']) && $options['id']) ? $options['id'] : '';
$content = str_replace("\r\n", "\n", $content);
$lines = explode("\n", $content);
$numLines = count($lines);
// determine left padding
$padTo = strlen((string) $numLines); // e.g. 103 lines = 3 digits
$inComment = false;
$i = 0;
$newLines = array();
while (null !== ($line = array_shift($lines))) {
if (('' !== $id) && (0 === $i % 50)) {
if ($inComment) {
array_push($newLines, '', "/* {$id} *|", '');
} else {
array_push($newLines, '', "/* {$id} */", '');
}
}
++$i;
$newLines[] = self::_addNote($line, $i, $inComment, $padTo);
$inComment = self::_eolInComment($line, $inComment);
}
$content = implode("\n", $newLines) . "\n";
// check for desired URI rewriting
if (isset($options['currentDir'])) {
Minify_CSS_UriRewriter::$debugText = '';
// W3TC FIX: Override $_SERVER['DOCUMENT_ROOT'] if enabled in settings.
$docroot = \W3TC\Util_Environment::document_root();
$docRoot = isset($options['docRoot']) ? $options['docRoot'] : $docroot;
$symlinks = isset($options['symlinks']) ? $options['symlinks'] : array();
$content = Minify_CSS_UriRewriter::rewrite($content, $options['currentDir'], $docRoot, $symlinks);
$content = "/* Minify_CSS_UriRewriter::\$debugText\n\n"
. Minify_CSS_UriRewriter::$debugText . "*/\n"
. $content;
}
return $content;
}
/**
* Is the parser within a C-style comment at the end of this line?
*
* @param string $line current line of code
*
* @param bool $inComment was the parser in a C-style comment at the
* beginning of the previous line?
*
* @return bool
*/
private static function _eolInComment($line, $inComment)
{
while (strlen($line)) {
if ($inComment) {
// only "*/" can end the comment
$index = self::_find($line, '*/');
if ($index === false) {
return true;
}
// stop comment and keep walking line
$inComment = false;
@$line = (string)substr($line, $index + 2);
continue;
}
// look for "//" and "/*"
$single = self::_find($line, '//');
$multi = self::_find($line, '/*');
if ($multi === false) {
return false;
}
if ($single === false || $multi < $single) {
// start comment and keep walking line
$inComment = true;
@$line = (string)substr($line, $multi + 2);
continue;
}
// a single-line comment preceeded it
return false;
}
return $inComment;
}
/**
* Prepend a comment (or note) to the given line
*
* @param string $line current line of code
*
* @param string $note content of note/comment
*
* @param bool $inComment was the parser in a comment at the
* beginning of the line?
*
* @param int $padTo minimum width of comment
*
* @return string
*/
private static function _addNote($line, $note, $inComment, $padTo)
{
if ($inComment) {
$line = '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' *| ' . $line;
} else {
$line = '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' */ ' . $line;
}
return rtrim($line);
}
/**
* Find a token trying to avoid false positives
*
* @param string $str String containing the token
* @param string $token Token being checked
* @return bool
*/
private static function _find($str, $token)
{
switch ($token) {
case '//':
$fakes = array(
'://' => 1,
'"//' => 1,
'\'//' => 1,
'".//' => 2,
'\'.//' => 2,
);
break;
case '/*':
$fakes = array(
'"/*' => 1,
'\'/*' => 1,
'"//*' => 2,
'\'//*' => 2,
'".//*' => 3,
'\'.//*' => 3,
'*/*' => 1,
'\\/*' => 1,
);
break;
default:
$fakes = array();
}
$index = strpos($str, $token);
$offset = 0;
while ($index !== false) {
foreach ($fakes as $fake => $skip) {
$check = substr($str, $index - $skip, strlen($fake));
if ($check === $fake) {
// move offset and scan again
$offset += $index + strlen($token);
$index = strpos($str, $token, $offset);
break;
}
}
// legitimate find
return $index;
}
return $index;
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_Logger
* @package Minify
*/
/**
* Message logging class
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*
* @todo lose this singleton! pass log object in Minify::serve and distribute to others
*/
class Minify_Logger {
/**
* Set logger object.
*
* The object should have a method "log" that accepts a value as 1st argument and
* an optional string label as the 2nd.
*
* @param mixed $obj or a "falsey" value to disable
* @return null
*/
public static function setLogger($obj = null) {
self::$_logger = $obj;
}
/**
* Pass a message to the logger (if set)
*
* @param string $msg message to log
* @return null
*/
public static function log($msg) {
if (is_callable(self::$_logger)) {
call_user_func(self::$_logger, $msg);
}
}
/**
* @var mixed logger object (like FirePHP) or null (i.e. no logger available)
*/
private static $_logger = null;
}

View File

@@ -0,0 +1,38 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_Packer
*
* To use this class you must first download the PHP port of Packer
* and place the file "class.JavaScriptPacker.php" in /lib (or your
* include_path).
* @link http://joliclic.free.fr/php/javascript-packer/en/
*
* Be aware that, as long as HTTP encoding is used, scripts minified with JSMin
* will provide better client-side performance, as they need not be unpacked in
* client-side code.
*
* @package Minify
*/
if (false === (@include 'class.JavaScriptPacker.php')) {
trigger_error(
'The script "class.JavaScriptPacker.php" is required. Please see: http:'
.'//code.google.com/p/minify/source/browse/trunk/min/lib/Minify/Packer.php'
,E_USER_ERROR
);
}
/**
* Minify Javascript using Dean Edward's Packer
*
* @package Minify
*/
class Minify_Packer {
public static function minify($code, $options = array())
{
// @todo: set encoding options based on $options :)
$packer = new JavascriptPacker($code, 'Normal', true, false);
return trim($packer->pack());
}
}

View File

@@ -0,0 +1,202 @@
<?php
/**
* File: Source.php
*
* NOTE: Fixes have been included in this file; look for "W3TC FIX".
*/
namespace W3TCL\Minify;
/**
* Class Minify_Source
* @package Minify
*/
/**
* A content source to be minified by Minify.
*
* This allows per-source minification options and the mixing of files with
* content from other sources.
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_Source {
/**
* @var int time of last modification
*/
public $lastModified = null;
/**
* @var callback minifier function specifically for this source.
*/
public $minifier = null;
/**
* @var array minification options specific to this source.
*/
public $minifyOptions = null;
/**
* @var string full path of file
*/
public $filepath = null;
/**
* @var string HTTP Content Type (Minify requires one of the constants Minify::TYPE_*)
*/
public $contentType = null;
/**
* Create a Minify_Source
*
* In the $spec array(), you can either provide a 'filepath' to an existing
* file (existence will not be checked!) or give 'id' (unique string for
* the content), 'content' (the string content) and 'lastModified'
* (unixtime of last update).
*
* As a shortcut, the controller will replace "//" at the beginning
* of a filepath with $_SERVER['DOCUMENT_ROOT'] . '/'.
*
* @param array $spec options
*/
public function __construct($spec)
{
if (isset($spec['filepath'])) {
if (0 === strpos($spec['filepath'], '//')) {
// W3TC FIX: Override $_SERVER['DOCUMENT_ROOT'] if enabled in settings.
$docroot = \W3TC\Util_Environment::document_root();
$spec['filepath'] = $docroot . substr($spec['filepath'], 1);
}
$segments = explode('.', $spec['filepath']);
$ext = strtolower(array_pop($segments));
switch ($ext) {
case 'js' : $this->contentType = 'application/x-javascript';
break;
case 'css' : $this->contentType = 'text/css';
break;
case 'htm' : // fallthrough
case 'html' : $this->contentType = 'text/html';
break;
}
$this->filepath = $spec['filepath'];
$this->_id = $spec['filepath'];
$this->lastModified = filemtime($spec['filepath'])
// offset for Windows uploaders with out of sync clocks
+ round(Minify::$uploaderHoursBehind * 3600);
} elseif (isset($spec['id'])) {
$this->_id = 'id::' . $spec['id'];
if (isset($spec['content'])) {
$this->_content = $spec['content'];
} else {
$this->_getContentFunc = $spec['getContentFunc'];
}
$this->lastModified = isset($spec['lastModified'])
? $spec['lastModified']
: time();
}
if (isset($spec['contentType'])) {
$this->contentType = $spec['contentType'];
}
if (isset($spec['minifier'])) {
$this->minifier = $spec['minifier'];
}
if (isset($spec['minifyOptions'])) {
$this->minifyOptions = $spec['minifyOptions'];
}
}
/**
* Get content
*
* @return string
*/
public function getContent()
{
if (isset($this->minifyOptions['processCssImports']) && $this->minifyOptions['processCssImports']) {
$content = Minify_ImportProcessor::process($this->filepath);
} else {
$content = (null !== $this->filepath)
? file_get_contents($this->filepath)
: ((null !== $this->_content)
? $this->_content
: call_user_func($this->_getContentFunc, $this->_id)
);
}
// remove UTF-8 BOM if present
return (pack("CCC",0xef,0xbb,0xbf) === substr($content, 0, 3))
? substr($content, 3)
: $content;
}
/**
* Get id
*
* @return string
*/
public function getId()
{
return $this->_id;
}
/**
* Verifies a single minification call can handle all sources
*
* @param array $sources Minify_Source instances
*
* @return bool true iff there no sources with specific minifier preferences.
*/
public static function haveNoMinifyPrefs($sources)
{
foreach ($sources as $source) {
if (null !== $source->minifier
|| null !== $source->minifyOptions) {
return false;
}
}
return true;
}
/**
* Get unique string for a set of sources
*
* @param array $sources Minify_Source instances
*
* @return string
*/
public static function getDigest($sources)
{
foreach ($sources as $source) {
$info[] = array(
$source->_id, $source->minifier, $source->minifyOptions
);
}
return md5(serialize($info));
}
/**
* Get content type from a group of sources
*
* This is called if the user doesn't pass in a 'contentType' options
*
* @param array $sources Minify_Source instances
*
* @return string content type. e.g. 'text/css'
*/
public static function getContentType($sources)
{
foreach ($sources as $source) {
if ($source->contentType !== null) {
return $source->contentType;
}
}
return 'text/plain';
}
protected $_content = null;
protected $_getContentFunc = null;
protected $_id = null;
}

View File

@@ -0,0 +1,382 @@
/*
* YUI Compressor
* http://developer.yahoo.com/yui/compressor/
* Author: Julien Lecomte - http://www.julienlecomte.net/
* Author: Isaac Schlueter - http://foohack.com/
* Author: Stoyan Stefanov - http://phpied.com/
* Copyright (c) 2011 Yahoo! Inc. All rights reserved.
* The copyrights embodied in the content of this file are licensed
* by Yahoo! Inc. under the BSD (revised) open source license.
*/
package com.yahoo.platform.yui.compressor;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
public class CssCompressor {
private StringBuffer srcsb = new StringBuffer();
public CssCompressor(Reader in) throws IOException {
// Read the stream...
int c;
while ((c = in.read()) != -1) {
srcsb.append((char) c);
}
}
// Leave data urls alone to increase parse performance.
protected String extractDataUrls(String css, ArrayList preservedTokens) {
int maxIndex = css.length() - 1;
int appendIndex = 0;
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile("url\\(\\s*([\"']?)data\\:");
Matcher m = p.matcher(css);
/*
* Since we need to account for non-base64 data urls, we need to handle
* ' and ) being part of the data string. Hence switching to indexOf,
* to determine whether or not we have matching string terminators and
* handling sb appends directly, instead of using matcher.append* methods.
*/
while (m.find()) {
int startIndex = m.start() + 4; // "url(".length()
String terminator = m.group(1); // ', " or empty (not quoted)
if (terminator.length() == 0) {
terminator = ")";
}
boolean foundTerminator = false;
int endIndex = m.end() - 1;
while(foundTerminator == false && endIndex+1 <= maxIndex) {
endIndex = css.indexOf(terminator, endIndex+1);
if ((endIndex > 0) && (css.charAt(endIndex-1) != '\\')) {
foundTerminator = true;
if (!")".equals(terminator)) {
endIndex = css.indexOf(")", endIndex);
}
}
}
// Enough searching, start moving stuff over to the buffer
sb.append(css.substring(appendIndex, m.start()));
if (foundTerminator) {
String token = css.substring(startIndex, endIndex);
token = token.replaceAll("\\s+", "");
preservedTokens.add(token);
String preserver = "url(___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___)";
sb.append(preserver);
appendIndex = endIndex + 1;
} else {
// No end terminator found, re-add the whole match. Should we throw/warn here?
sb.append(css.substring(m.start(), m.end()));
appendIndex = m.end();
}
}
sb.append(css.substring(appendIndex));
return sb.toString();
}
public void compress(Writer out, int linebreakpos)
throws IOException {
Pattern p;
Matcher m;
String css = srcsb.toString();
int startIndex = 0;
int endIndex = 0;
int i = 0;
int max = 0;
ArrayList preservedTokens = new ArrayList(0);
ArrayList comments = new ArrayList(0);
String token;
int totallen = css.length();
String placeholder;
css = this.extractDataUrls(css, preservedTokens);
StringBuffer sb = new StringBuffer(css);
// collect all comment blocks...
while ((startIndex = sb.indexOf("/*", startIndex)) >= 0) {
endIndex = sb.indexOf("*/", startIndex + 2);
if (endIndex < 0) {
endIndex = totallen;
}
token = sb.substring(startIndex + 2, endIndex);
comments.add(token);
sb.replace(startIndex + 2, endIndex, "___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + (comments.size() - 1) + "___");
startIndex += 2;
}
css = sb.toString();
// preserve strings so their content doesn't get accidentally minified
sb = new StringBuffer();
p = Pattern.compile("(\"([^\\\\\"]|\\\\.|\\\\)*\")|(\'([^\\\\\']|\\\\.|\\\\)*\')");
m = p.matcher(css);
while (m.find()) {
token = m.group();
char quote = token.charAt(0);
token = token.substring(1, token.length() - 1);
// maybe the string contains a comment-like substring?
// one, maybe more? put'em back then
if (token.indexOf("___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_") >= 0) {
for (i = 0, max = comments.size(); i < max; i += 1) {
token = token.replace("___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + i + "___", comments.get(i).toString());
}
}
// minify alpha opacity in filter strings
token = token.replaceAll("(?i)progid:DXImageTransform.Microsoft.Alpha\\(Opacity=", "alpha(opacity=");
preservedTokens.add(token);
String preserver = quote + "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___" + quote;
m.appendReplacement(sb, preserver);
}
m.appendTail(sb);
css = sb.toString();
// strings are safe, now wrestle the comments
for (i = 0, max = comments.size(); i < max; i += 1) {
token = comments.get(i).toString();
placeholder = "___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + i + "___";
// ! in the first position of the comment means preserve
// so push to the preserved tokens while stripping the !
if (token.startsWith("!")) {
preservedTokens.add(token);
css = css.replace(placeholder, "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___");
continue;
}
// \ in the last position looks like hack for Mac/IE5
// shorten that to /*\*/ and the next one to /**/
if (token.endsWith("\\")) {
preservedTokens.add("\\");
css = css.replace(placeholder, "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___");
i = i + 1; // attn: advancing the loop
preservedTokens.add("");
css = css.replace("___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_" + i + "___", "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___");
continue;
}
// keep empty comments after child selectors (IE7 hack)
// e.g. html >/**/ body
if (token.length() == 0) {
startIndex = css.indexOf(placeholder);
if (startIndex > 2) {
if (css.charAt(startIndex - 3) == '>') {
preservedTokens.add("");
css = css.replace(placeholder, "___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___");
}
}
}
// in all other cases kill the comment
css = css.replace("/*" + placeholder + "*/", "");
}
// Normalize all whitespace strings to single spaces. Easier to work with that way.
css = css.replaceAll("\\s+", " ");
// Remove the spaces before the things that should not have spaces before them.
// But, be careful not to turn "p :link {...}" into "p:link{...}"
// Swap out any pseudo-class colons with the token, and then swap back.
sb = new StringBuffer();
p = Pattern.compile("(^|\\})(([^\\{:])+:)+([^\\{]*\\{)");
m = p.matcher(css);
while (m.find()) {
String s = m.group();
s = s.replaceAll(":", "___YUICSSMIN_PSEUDOCLASSCOLON___");
s = s.replaceAll( "\\\\", "\\\\\\\\" ).replaceAll( "\\$", "\\\\\\$" );
m.appendReplacement(sb, s);
}
m.appendTail(sb);
css = sb.toString();
// Remove spaces before the things that should not have spaces before them.
css = css.replaceAll("\\s+([!{};:>+\\(\\)\\],])", "$1");
// bring back the colon
css = css.replaceAll("___YUICSSMIN_PSEUDOCLASSCOLON___", ":");
// retain space for special IE6 cases
css = css.replaceAll(":first\\-(line|letter)(\\{|,)", ":first-$1 $2");
// no space after the end of a preserved comment
css = css.replaceAll("\\*/ ", "*/");
// If there is a @charset, then only allow one, and push to the top of the file.
css = css.replaceAll("^(.*)(@charset \"[^\"]*\";)", "$2$1");
css = css.replaceAll("^(\\s*@charset [^;]+;\\s*)+", "$1");
// Put the space back in some cases, to support stuff like
// @media screen and (-webkit-min-device-pixel-ratio:0){
css = css.replaceAll("\\band\\(", "and (");
// Remove the spaces after the things that should not have spaces after them.
css = css.replaceAll("([!{}:;>+\\(\\[,])\\s+", "$1");
// remove unnecessary semicolons
css = css.replaceAll(";+}", "}");
// Replace 0(px,em,%) with 0.
css = css.replaceAll("([\\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)", "$1$2");
// Replace 0 0 0 0; with 0.
css = css.replaceAll(":0 0 0 0(;|})", ":0$1");
css = css.replaceAll(":0 0 0(;|})", ":0$1");
css = css.replaceAll(":0 0(;|})", ":0$1");
// Replace background-position:0; with background-position:0 0;
// same for transform-origin
sb = new StringBuffer();
p = Pattern.compile("(?i)(background-position|transform-origin|webkit-transform-origin|moz-transform-origin|o-transform-origin|ms-transform-origin):0(;|})");
m = p.matcher(css);
while (m.find()) {
m.appendReplacement(sb, m.group(1).toLowerCase() + ":0 0" + m.group(2));
}
m.appendTail(sb);
css = sb.toString();
// Replace 0.6 to .6, but only when preceded by : or a white-space
css = css.replaceAll("(:|\\s)0+\\.(\\d+)", "$1.$2");
// Shorten colors from rgb(51,102,153) to #336699
// This makes it more likely that it'll get further compressed in the next step.
p = Pattern.compile("rgb\\s*\\(\\s*([0-9,\\s]+)\\s*\\)");
m = p.matcher(css);
sb = new StringBuffer();
while (m.find()) {
String[] rgbcolors = m.group(1).split(",");
StringBuffer hexcolor = new StringBuffer("#");
for (i = 0; i < rgbcolors.length; i++) {
int val = Integer.parseInt(rgbcolors[i]);
if (val < 16) {
hexcolor.append("0");
}
hexcolor.append(Integer.toHexString(val));
}
m.appendReplacement(sb, hexcolor.toString());
}
m.appendTail(sb);
css = sb.toString();
// Shorten colors from #AABBCC to #ABC. Note that we want to make sure
// the color is not preceded by either ", " or =. Indeed, the property
// filter: chroma(color="#FFFFFF");
// would become
// filter: chroma(color="#FFF");
// which makes the filter break in IE.
// We also want to make sure we're only compressing #AABBCC patterns inside { }, not id selectors ( #FAABAC {} )
// We also want to avoid compressing invalid values (e.g. #AABBCCD to #ABCD)
p = Pattern.compile("(\\=\\s*?[\"']?)?" + "#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])" + "(:?\\}|[^0-9a-fA-F{][^{]*?\\})");
m = p.matcher(css);
sb = new StringBuffer();
int index = 0;
while (m.find(index)) {
sb.append(css.substring(index, m.start()));
boolean isFilter = (m.group(1) != null && !"".equals(m.group(1)));
if (isFilter) {
// Restore, as is. Compression will break filters
sb.append(m.group(1) + "#" + m.group(2) + m.group(3) + m.group(4) + m.group(5) + m.group(6) + m.group(7));
} else {
if( m.group(2).equalsIgnoreCase(m.group(3)) &&
m.group(4).equalsIgnoreCase(m.group(5)) &&
m.group(6).equalsIgnoreCase(m.group(7))) {
// #AABBCC pattern
sb.append("#" + (m.group(3) + m.group(5) + m.group(7)).toLowerCase());
} else {
// Non-compressible color, restore, but lower case.
sb.append("#" + (m.group(2) + m.group(3) + m.group(4) + m.group(5) + m.group(6) + m.group(7)).toLowerCase());
}
}
index = m.end(7);
}
sb.append(css.substring(index));
css = sb.toString();
// border: none -> border:0
sb = new StringBuffer();
p = Pattern.compile("(?i)(border|border-top|border-right|border-bottom|border-right|outline|background):none(;|})");
m = p.matcher(css);
while (m.find()) {
m.appendReplacement(sb, m.group(1).toLowerCase() + ":0" + m.group(2));
}
m.appendTail(sb);
css = sb.toString();
// shorter opacity IE filter
css = css.replaceAll("(?i)progid:DXImageTransform.Microsoft.Alpha\\(Opacity=", "alpha(opacity=");
// Remove empty rules.
css = css.replaceAll("[^\\}\\{/;]+\\{\\}", "");
// TODO: Should this be after we re-insert tokens. These could alter the break points. However then
// we'd need to make sure we don't break in the middle of a string etc.
if (linebreakpos >= 0) {
// Some source control tools don't like it when files containing lines longer
// than, say 8000 characters, are checked in. The linebreak option is used in
// that case to split long lines after a specific column.
i = 0;
int linestartpos = 0;
sb = new StringBuffer(css);
while (i < sb.length()) {
char c = sb.charAt(i++);
if (c == '}' && i - linestartpos > linebreakpos) {
sb.insert(i, '\n');
linestartpos = i;
}
}
css = sb.toString();
}
// Replace multiple semi-colons in a row by a single one
// See SF bug #1980989
css = css.replaceAll(";;+", ";");
// restore preserved comments and strings
for(i = 0, max = preservedTokens.size(); i < max; i++) {
css = css.replace("___YUICSSMIN_PRESERVED_TOKEN_" + i + "___", preservedTokens.get(i).toString());
}
// Trim the final string (for any leading or trailing white spaces)
css = css.trim();
// Write the output...
out.write(css);
}
}

View File

@@ -0,0 +1,172 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_YUI_CssCompressor
* @package Minify
*
* YUI Compressor
* Author: Julien Lecomte - http://www.julienlecomte.net/
* Author: Isaac Schlueter - http://foohack.com/
* Author: Stoyan Stefanov - http://phpied.com/
* Author: Steve Clay - http://www.mrclay.org/ (PHP port)
* Copyright (c) 2009 Yahoo! Inc. All rights reserved.
* The copyrights embodied in the content of this file are licensed
* by Yahoo! Inc. under the BSD (revised) open source license.
*/
/**
* Compress CSS (incomplete DO NOT USE)
*
* @see https://github.com/yui/yuicompressor/blob/master/src/com/yahoo/platform/yui/compressor/CssCompressor.java
*
* @package Minify
*/
class Minify_YUI_CssCompressor {
/**
* Minify a CSS string
*
* @param string $css
*
* @return string
*/
public function compress($css, $linebreakpos = 0)
{
$css = str_replace("\r\n", "\n", $css);
/**
* @todo comment removal
* @todo re-port from newer Java version
*/
// Normalize all whitespace strings to single spaces. Easier to work with that way.
$css = preg_replace('@\s+@', ' ', $css);
// Make a pseudo class for the Box Model Hack
$css = preg_replace("@\"\\\\\"}\\\\\"\"@", "___PSEUDOCLASSBMH___", $css);
// Remove the spaces before the things that should not have spaces before them.
// But, be careful not to turn "p :link {...}" into "p:link{...}"
// Swap out any pseudo-class colons with the token, and then swap back.
$css = preg_replace_callback("@(^|\\})(([^\\{:])+:)+([^\\{]*\\{)@", array($this, '_removeSpacesCB'), $css);
$css = preg_replace("@\\s+([!{};:>+\\(\\)\\],])@", "$1", $css);
$css = str_replace("___PSEUDOCLASSCOLON___", ":", $css);
// Remove the spaces after the things that should not have spaces after them.
$css = preg_replace("@([!{}:;>+\\(\\[,])\\s+@", "$1", $css);
// Add the semicolon where it's missing.
$css = preg_replace("@([^;\\}])}@", "$1;}", $css);
// Replace 0(px,em,%) with 0.
$css = preg_replace("@([\\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)@", "$1$2", $css);
// Replace 0 0 0 0; with 0.
$css = str_replace(":0 0 0 0;", ":0;", $css);
$css = str_replace(":0 0 0;", ":0;", $css);
$css = str_replace(":0 0;", ":0;", $css);
// Replace background-position:0; with background-position:0 0;
$css = str_replace("background-position:0;", "background-position:0 0;", $css);
// Replace 0.6 to .6, but only when preceded by : or a white-space
$css = preg_replace("@(:|\\s)0+\\.(\\d+)@", "$1.$2", $css);
// Shorten colors from rgb(51,102,153) to #336699
// This makes it more likely that it'll get further compressed in the next step.
$css = preg_replace_callback("@rgb\\s*\\(\\s*([0-9,\\s]+)\\s*\\)@", array($this, '_shortenRgbCB'), $css);
// Shorten colors from #AABBCC to #ABC. Note that we want to make sure
// the color is not preceded by either ", " or =. Indeed, the property
// filter: chroma(color="#FFFFFF");
// would become
// filter: chroma(color="#FFF");
// which makes the filter break in IE.
$css = preg_replace_callback("@([^\"'=\\s])(\\s*)#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])@", array($this, '_shortenHexCB'), $css);
// Remove empty rules.
$css = preg_replace("@[^\\}]+\\{;\\}@", "", $css);
$linebreakpos = isset($this->_options['linebreakpos'])
? $this->_options['linebreakpos']
: 0;
if ($linebreakpos > 0) {
// Some source control tools don't like it when files containing lines longer
// than, say 8000 characters, are checked in. The linebreak option is used in
// that case to split long lines after a specific column.
$i = 0;
$linestartpos = 0;
$sb = $css;
// make sure strlen returns byte count
$mbIntEnc = null;
if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
$mbIntEnc = mb_internal_encoding();
mb_internal_encoding('8bit');
}
$sbLength = strlen($css);
while ($i < $sbLength) {
$c = $sb[$i++];
if ($c === '}' && $i - $linestartpos > $linebreakpos) {
$sb = substr_replace($sb, "\n", $i, 0);
$sbLength++;
$linestartpos = $i;
}
}
$css = $sb;
// undo potential mb_encoding change
if ($mbIntEnc !== null) {
mb_internal_encoding($mbIntEnc);
}
}
// Replace the pseudo class for the Box Model Hack
$css = str_replace("___PSEUDOCLASSBMH___", "\"\\\\\"}\\\\\"\"", $css);
// Replace multiple semi-colons in a row by a single one
// See SF bug #1980989
$css = preg_replace("@;;+@", ";", $css);
// prevent triggering IE6 bug: http://www.crankygeek.com/ie6pebug/
$css = preg_replace('/:first-l(etter|ine)\\{/', ':first-l$1 {', $css);
// Trim the final string (for any leading or trailing white spaces)
$css = trim($css);
return $css;
}
protected function _removeSpacesCB($m)
{
return str_replace(':', '___PSEUDOCLASSCOLON___', $m[0]);
}
protected function _shortenRgbCB($m)
{
$rgbcolors = explode(',', $m[1]);
$hexcolor = '#';
for ($i = 0; $i < count($rgbcolors); $i++) {
$val = round($rgbcolors[$i]);
if ($val < 16) {
$hexcolor .= '0';
}
$hexcolor .= dechex($val);
}
return $hexcolor;
}
protected function _shortenHexCB($m)
{
// Test for AABBCC pattern
if ((strtolower($m[3])===strtolower($m[4])) &&
(strtolower($m[5])===strtolower($m[6])) &&
(strtolower($m[7])===strtolower($m[8]))) {
return $m[1] . $m[2] . "#" . $m[3] . $m[5] . $m[7];
} else {
return $m[0];
}
}
}

View File

@@ -0,0 +1,198 @@
<?php
namespace W3TCL\Minify;
/**
* Class Minify_YUICompressor
* @package Minify
*/
/**
* Compress Javascript/CSS using the YUI Compressor
*
* You must set $jarFile and $tempDir before calling the minify functions.
* Also, depending on your shell's environment, you may need to specify
* the full path to java in $javaExecutable or use putenv() to setup the
* Java environment.
*
* <code>
* Minify_YUICompressor::$jarFile = '/path/to/yuicompressor-2.4.6.jar';
* Minify_YUICompressor::$tempDir = '/tmp';
* $code = Minify_YUICompressor::minifyJs(
* $code
* ,array('nomunge' => true, 'line-break' => 1000)
* );
* </code>
*
* Note: In case you run out stack (default is 512k), you may increase stack size in $options:
* array('stack-size' => '2048k')
*
* @todo unit tests, $options docs
*
* @package Minify
* @author Stephen Clay <steve@mrclay.org>
*/
class Minify_YUICompressor {
/**
* Filepath of the YUI Compressor jar file. This must be set before
* calling minifyJs() or minifyCss().
*
* @var string
*/
public static $jarFile = null;
/**
* Writable temp directory. This must be set before calling minifyJs()
* or minifyCss().
*
* @var string
*/
public static $tempDir = null;
/**
* Filepath of "java" executable (may be needed if not in shell's PATH)
*
* @var string
*/
public static $javaExecutable = 'java';
/**
* Minify a Javascript string
*
* @param string $js
*
* @param array $options (verbose is ignored)
*
* @see http://www.julienlecomte.net/yuicompressor/README
*
* @return string
*/
public static function minifyJs($js, $options = array())
{
return self::_minify('js', $js, $options);
}
/**
* Minify a CSS string
*
* @param string $css
*
* @param array $options (verbose is ignored)
*
* @see http://www.julienlecomte.net/yuicompressor/README
*
* @return string
*/
public static function minifyCss($css, $options = array())
{
$css = self::_minify('css', $css, $options);
$css = Minify_CSS_UriRewriter::rewrite($css, $options);
return $css;
}
private static function _minify($type, $content, $options)
{
self::_prepare();
if (! ($tmpFile = tempnam(self::$tempDir, 'yuic_'))) {
throw new \Exception('Minify_YUICompressor : could not create temp file.');
}
file_put_contents($tmpFile, $content);
exec(self::_getCmd($options, $type, $tmpFile), $output, $result_code);
unlink($tmpFile);
if ($result_code != 0) {
throw new \Exception('Minify_YUICompressor : YUI compressor execution failed.');
}
return implode("\n", $output);
}
private static function _getCmd($userOptions, $type, $tmpFile)
{
if (!is_file(self::$javaExecutable)) {
throw new \Exception(sprintf('JAVA executable (%s) is not a valid file.', self::$javaExecutable));
}
if (!is_file(self::$jarFile)) {
throw new \Exception(sprintf('JAR file (%s) is not a valid file.', self::$jarFile));
}
$o = array_merge(
array(
'charset' => ''
,'line-break' => 5000
,'type' => $type
,'nomunge' => false
,'preserve-semi' => false
,'disable-optimizations' => false
,'stack-size' => ''
)
,$userOptions
);
$javaExecutable = self::$javaExecutable;
if ( false !== strpos(trim($javaExecutable), ' ') ) {
$javaExecutable = '"'. $javaExecutable . '"';
}
$cmd = $javaExecutable
. (!empty($o['stack-size'])
? ' -Xss' . $o['stack-size']
: '')
. ' -jar ' . escapeshellarg(self::$jarFile)
. " --type {$type}"
. (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $o['charset'])
? " --charset {$o['charset']}"
: '')
. (is_numeric($o['line-break']) && $o['line-break'] >= 0
? ' --line-break ' . (int)$o['line-break']
: '');
if ($type === 'js') {
foreach (array('nomunge', 'preserve-semi', 'disable-optimizations') as $opt) {
$cmd .= $o[$opt]
? " --{$opt}"
: '';
}
}
return $cmd . ' ' . escapeshellarg($tmpFile);
}
private static function _prepare()
{
if (! is_file(self::$jarFile)) {
throw new \Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not a valid file.');
}
if (! is_readable(self::$jarFile)) {
throw new \Exception('Minify_YUICompressor : $jarFile('.self::$jarFile.') is not readable.');
}
if (! is_dir(self::$tempDir)) {
throw new \Exception('Minify_YUICompressor : $tempDir('.self::$tempDir.') is not a valid direcotry.');
}
if (! is_writable(self::$tempDir)) {
throw new \Exception('Minify_YUICompressor : $tempDir('.self::$tempDir.') is not writable.');
}
}
public static function testJs(&$error) {
try {
Minify_YUICompressor::minifyJs('alert("ok");');
$error = 'OK';
return true;
} catch (\Exception $exception) {
$error = $exception->getMessage();
return false;
}
}
public static function testCss(&$error) {
try {
Minify_YUICompressor::minifyCss('p{color:red}');
$error = 'OK';
return true;
} catch (\Exception $exception) {
$error = $exception->getMessage();
return false;
}
}
}

View File

@@ -0,0 +1,436 @@
<?php
if (!defined('ABSPATH')) {
die();
}
require_once(W3TC_LIB_DIR . '/OAuth/W3tcOAuth.php');
require_once('W3tcWpHttpException.php');
/**
* NetDNA REST Client Library
*
* @copyright 2012
* @author Karlo Espiritu
* @version 1.0 2012-09-21
*/
class NetDNA {
public $alias;
public $key;
public $secret;
public $netdnarws_url = 'https://rws.netdna.com';
static public function create($authorization_key) {
$keys = explode('+', $authorization_key);
$alias = '';
$consumerkey = '';
$consumersecret = '';
if (sizeof($keys) == 3)
list($alias, $consumerkey, $consumersecret) = $keys;
$api = new NetDNA($alias, $consumerkey, $consumersecret);
return $api;
}
/**
* @param string $alias
* @param string $key
* @param string $secret
*/
public function __construct($alias, $key, $secret) {
$this->alias = $alias;
$this->key = $key;
$this->secret = $secret;
}
public function get_zone_domain($name) {
return $name . '.' . $this->alias . '.netdna-cdn.com';
}
public function is_valid() {
return !empty($this->alias) && !empty($this->key) &&
!empty($this->secret);
}
/**
* @param $selected_call
* @param $method_type
* @param $params
* @return string
* @throws W3tcWpHttpException
*/
private function execute($selected_call, $method_type, $params) {
//increase the http request timeout
add_filter('http_request_timeout', array($this, 'filter_timeout_time'));
add_filter('https_ssl_verify', array($this, 'https_ssl_verify'));
$consumer = new W3tcOAuthConsumer($this->key, $this->secret, NULL);
// the endpoint for your request
$endpoint = "$this->netdnarws_url/$this->alias$selected_call";
//parse endpoint before creating OAuth request
$parsed = parse_url($endpoint);
if (array_key_exists("parsed", $parsed)) {
parse_str($parsed['query'], $params);
}
//generate a request from your consumer
$req_req = W3tcOAuthRequest::from_consumer_and_token($consumer, NULL, $method_type, $endpoint, $params);
//sign your OAuth request using hmac_sha1
$sig_method = new W3tcOAuthSignatureMethod_HMAC_SHA1();
$req_req->sign_request($sig_method, $consumer, NULL);
$request = array();
$request['sslverify'] = false;
$request['method'] = $method_type;
if ($method_type == "POST" || $method_type == "PUT") {
$request['body'] = $req_req->to_postdata();
$request['headers']['Content-Type'] =
'application/x-www-form-urlencoded; charset=' . get_option('blog_charset');
$url = $req_req->get_normalized_http_url();
} else {
// notice GET, PUT and DELETE both needs to be passed in URL
$url = $req_req->to_url();
}
$response = wp_remote_request($url, $request);
$json_output = '';
if (!is_wp_error($response)) {
// make call
$result = wp_remote_retrieve_body($response);
$headers = wp_remote_retrieve_headers($response);
$response_code = wp_remote_retrieve_response_code($response);
// $json_output contains the output string
$json_output = $result;
} else {
$response_code = $response->get_error_code();
}
remove_filter('https_ssl_verify', array($this, 'https_ssl_verify'));
remove_filter('http_request_timeout', array($this, 'filter_timeout_time'));
// catch errors
if(is_wp_error($response)) {
throw new W3tcWpHttpException("ERROR: {$response->get_error_message()}, Output: $json_output", $response_code, null, $headers);
}
return $json_output;
}
/**
* @param $selected_call
* @param array $params
* @return string
* @throws W3tcWpHttpException
*/
public function get($selected_call, $params = array()){
return $this->execute($selected_call, 'GET', $params);
}
/**
* @param $selected_call
* @param array $params
* @return string
* @throws W3tcWpHttpException
*/
public function post($selected_call, $params = array()){
return $this->execute($selected_call, 'POST', $params);
}
/**
* @param $selected_call
* @param array $params
* @return string
* @throws W3tcWpHttpException
*/
public function put($selected_call, $params = array()){
return $this->execute($selected_call, 'PUT', $params);
}
/**
* @param $selected_call
* @param array $params
* @return string
* @throws W3tcWpHttpException
*/
public function delete($selected_call, $params = array()){
return $this->execute($selected_call, 'DELETE', $params);
}
/**
* Finds the zone id that matches the provided url.
* @param $url
* @return null|int
* @throws W3tcWpHttpException
*/
public function get_zone_id($url) {
$zone_id = null;
$pull_zones = json_decode($this->get('/zones/pull.json'));
if (preg_match("(200|201)", $pull_zones->code)) {
foreach ($pull_zones->data->pullzones as $zone) {
if (trim($zone->url, '/') != trim($url, '/'))
continue;
else {
$zone_id = $zone->id;
break;
}
}
} else
return null;
return $zone_id;
}
/**
* Retrieves statistics for the zone id
* @param $zone_id
* @return null|array
* @throws W3tcWpHttpException
*/
public function get_stats_per_zone($zone_id) {
$api_stats = json_decode($this->get("/reports/{$zone_id}/stats.json"), true);
if (preg_match("(200|201)", $api_stats['code'])) {
$summary = $api_stats['data']['summary'];
return $summary;
} else
return null;
}
/**
* Returns list of files for the zone id
* @param $zone_id
* @return null|array
* @throws W3tcWpHttpException
*/
public function get_list_of_file_types_per_zone($zone_id) {
$api_list = json_decode($this->get("/reports/pull/{$zone_id}/filetypes.json"), true);
if (preg_match("(200|201)", $api_list['code'])) {
$stats['total'] = $api_list['data']['total'];
foreach($api_list['data']['filetypes'] as $filetyp) {
$stats['filetypes'][] = $filetyp;
}
$stats['summary'] = $api_list['data']['summary'];
return $stats;
} else
return null;
}
/**
* Retrieves a list of popular files for zone id
*
* @param $zone_id
* @return null|array
* @throws W3tcWpHttpException
*/
public function get_list_of_popularfiles_per_zone($zone_id) {
$api_popularfiles = json_decode($this->get("/reports/{$zone_id}/popularfiles.json"), true);
if (preg_match("(200|201)", $api_popularfiles['code'])) {
$popularfiles = $api_popularfiles['data']['popularfiles'];
return $popularfiles;
} else
return null;
}
/**
* Retrieves an account connected with the authorization key
*
* @throws Exception
* @return null|string
*/
public function get_account() {
$api_account = json_decode($this->get("/account.json"), true);
if (preg_match("(200|201)", $api_account['code'])) {
$account = $api_account['data']['account'];
return $account;
} else
throw new Exception($this->error_message($api_account));
}
/**
* Retrieves a pull zone
* @param $zone_id
* @throws Exception
* @return null|string
*/
public function get_pull_zone($zone_id) {
$api_pull_zone = json_decode($this->get("/zones/pull.json/{$zone_id}"), true);
if (preg_match("(200|201)", $api_pull_zone['code'])) {
$pull_zone = $api_pull_zone['data']['pullzone'];
return $pull_zone;
} else
throw new Exception($this->error_message($api_pull_zone));
}
/**
* Creates a pull zone
* @param $zone
* @return mixed
* @throws Exception
*/
public function create_pull_zone($zone) {
$zone_data = json_decode($this->post('/zones/pull.json', $zone), true);
if (preg_match("(200|201)", $zone_data['code'])) {
return $zone_data['data']['pullzone'];
} else
throw new Exception($this->error_message($zone_data));
}
private function error_message($o) {
$m = isset( $o['error']['message'] ) ? $o['error']['message'] : '';
if ( isset( $o['data']['errors'] ) && is_array( $o['data']['errors'] ) ) {
foreach ( $o['data']['errors'] as $k => $v ) {
$m .= '. ' . $k . ': ' . $v;
}
}
return $m;
}
/**
* Retrieves pull zones
* @throws Exception
* @return array|null
*/
public function get_pull_zones() {
$pull_zones = json_decode($this->get('/zones/pull.json'), true);
$zones = array();
if (preg_match("(200|201)", $pull_zones['code'])) {
foreach ($pull_zones ['data']['pullzones'] as $zone) {
$zones[] = $zone;
}
} else {
throw new Exception($this->error_message($zone_data));
}
return $zones;
}
/**
* Increase http request timeout to 60 seconds
* @param int $time
* @return int
*/
public function filter_timeout_time($time) {
return 600;
}
/**
* Don't check certificate, some users have limited CA list
*/
public function https_ssl_verify($v) {
return false;
}
/**
* Update a pull zone
* @param $zone_id
* @param $zone
* @throws Exception
* @return
*/
public function update_pull_zone($zone_id, $zone) {
$zone_data = json_decode($this->put("/zones/pull.json/$zone_id", $zone), true);
if (preg_match("(200|201)", $zone_data['code'])) {
return $zone_data['data']['pullzone'];
} else {
throw new Exception($this->error_message($zone_data));
}
}
/**
* Creates custom domains
* @param $zone_id
* @throws Exception
* @return array|null
*/
public function create_custom_domain($zone_id, $custom_domain) {
$custom_domain = json_decode($this->post("/zones/pull/$zone_id/customdomains.json", array(
'custom_domain' => $custom_domain)), true);
if (preg_match("(200|201)", $custom_domain['code'])) {
return $custom_domain;
} else
throw $this->to_exception($custom_domain);
}
private function to_exception($response) {
$message = $response['error']['message'];
if ( isset( $response['data'] ) && isset( $response['data']['errors'] ) ) {
foreach ( $response['data']['errors'] as $field => $error ) {
if ( isset( $error['error'] ) )
$message .= '. ' . $field . ': ' . $error['error'];
else
$message .= '. ' . $field . ': ' . $error;
}
}
return new Exception($message);
}
/**
* Returns custom domains
* @param $zone_id
* @throws Exception
* @return array|null
*/
public function get_custom_domains($zone_id) {
$custom_domains = json_decode($this->get("/zones/pull/$zone_id/customdomains.json"), true);
$domains = array();
if (preg_match("(200|201)", $custom_domains['code'])) {
foreach ($custom_domains['data']['customdomains'] as $domain) {
$domains[] = $domain['custom_domain'];
}
} else
throw new Exception($this->error_message($custom_domains));
return $domains;
}
/**
* Returns the zone data for the provided zone id
*
* @param int $zone_id
* @throws Exception
* @return array
*/
public function get_zone($zone_id) {
$zone_data = json_decode($this->get("/zones/pull.json/$zone_id"), true);
if (preg_match("(200|201)", $zone_data['code'])) {
return $zone_data['data']['pullzone'];
} else
throw new Exception($this->error_message($zone_data));
}
/**
* Deletes files from cache
* @param $zone_id
* @param $files array of relative paths to files to delete
* Deletes whole zone if empty list passed
**/
public function cache_delete($zone_id, $files = array()) {
if (empty($files))
$params = array();
else
$params = array('files' => $files);
$response = json_decode($this->delete(
'/zones/pull.json/' . $zone_id . '/cache',
$params), true);
if (preg_match("(200|201)", $response['code'])) {
return true;
} else
throw $this->to_exception($response);
}
}

View File

@@ -0,0 +1,77 @@
<?php
class NetDNAPresentation {
public static function format_popular($popular_files) {
$formatted = array();
foreach ($popular_files as $file) {
// basename cannot be used, kills chinese chars and similar characters
$filename = substr($file['uri'], strrpos($file['uri'], '/')+1);
$formatted[] = array('file' => $filename, 'title' => $file['uri'],
'group' => self::get_file_group($file['uri']), 'hit' => $file['hit']);
}
return $formatted;
}
public static function group_hits_per_filetype_group($filetypes) {
$groups = array();
foreach ($filetypes as $file) {
$file_group = self::get_file_group($file['file_type']);
if (!isset($groups[$file_group]))
$groups[$file_group] = 0;
$groups[$file_group] += $file['hit'];
}
return $groups;
}
public static function get_file_group($uri) {
$uri_exploded = explode('.', $uri);
$ext = end($uri_exploded);
switch ($ext) {
case 'css':
case 'js':
return $ext;
break;
case 'png':
case 'tiff':
case 'gif':
case 'jpg':
case 'jpeg':
return 'images';
break;
default:
return 'misc';
break;
}
}
public static function get_file_group_color($group) {
switch ($group){
case 'css':
return '#739468';
case 'js':
return '#ffb05d';
case 'images':
return '#b080df';
default:
return '#4ba0fa';
}
}
public static function get_account_status($status) {
switch ($status) {
case 1:
return __('Pending', 'w3-total-cache');
case 2:
return __('Active', 'w3-total-cache');
case 3:
return __('Cancelled', 'w3-total-cache');
case 4:
return __('Suspended', 'w3-total-cache');
case 5:
return __('Fraud', 'w3-total-cache');
default:
return __('unknown', 'w3-total-cache');
}
}
}

View File

@@ -0,0 +1,20 @@
<?php
class W3tcWpHttpException extends Exception {
private $curl_headers;
public function __construct($message, $code = 0, Exception $previous = null, $headers = null) {
if (version_compare(PHP_VERSION, '5.3', '>='))
parent::__construct($message, (int)$code, $previous);
else
parent::__construct($message, (int)$code);
$this->curl_headers = $headers;
}
public function getHeaders() {
return $this->curl_headers;
}
}

View File

@@ -0,0 +1,207 @@
<?php
define('NEWRELIC_API_BASE', 'https://api.newrelic.com');
/**
* Interacts with the New Relic Connect API
* deprecated
* @link newrelic.github.com/newrelic_api/
*/
class NewRelicAPI {
private $_api_key;
/**
* @param string $api_key New Relic API Key
*/
function __construct($api_key) {
$this->_api_key = $api_key;
}
/**
* @param string $api_call_url url path with query string used to define what to get from the NR API
* @throws Exception
* @return bool
*/
private function _get($api_call_url) {
$defaults = array(
'headers'=>'x-api-key:'.$this->_api_key
);
$url = NEWRELIC_API_BASE . $api_call_url;
$response = wp_remote_get($url, $defaults);
if (is_wp_error($response)) {
throw new Exception('Could not get data');
} elseif ($response['response']['code'] == 200) {
$return = $response['body'];
} else {
switch ($response['response']['code']) {
case '403':
$message = __('Invalid API key', 'w3-total-cache');
break;
default:
$message = $response['response']['message'];
}
throw new Exception($message, $response['response']['code']);
}
return $return;
}
/**
* @param string $api_call_url url path with query string used to define what to get from the NR API
* @param array $params key value array.
* @throws Exception
* @return bool
*/
private function _put($api_call_url, $params) {
$defaults = array(
'method' => 'PUT',
'headers'=>'x-api-key:'.$this->_api_key,
'body' => $params
);
$url = NEWRELIC_API_BASE . $api_call_url;
$response = wp_remote_request($url, $defaults);
if (is_wp_error($response)) {
throw new Exception('Could not put data');
} elseif ($response['response']['code'] == 200) {
$return = true;
} else {
throw new Exception($response['response']['message'], $response['response']['code']);
}
return $return;
}
/**
* Get applications connected with the API key and provided account id.
* @param int $account_id
* @return array
*/
function get_applications($account_id) {
$applications = array();
if ($xml_string = $this->_get("/api/v1/accounts/{$account_id}/applications.xml")) {
$xml = simplexml_load_string($xml_string);
foreach($xml->application as $application) {
$applications[(int)$application->id] = (string)$application->name;
}
}
return $applications;
}
/**
* Get the application summary data for the provided application
* @param $application_id
* @return array array(metric name => metric value)
*/
function get_application_summary($account_id, $application_id) {
$summary = array();
if ($xml_string = $this->_get("/api/v1/accounts/{$account_id}/applications/{$application_id}/threshold_values.xml")) {
$xml = simplexml_load_string($xml_string);
foreach($xml->{'threshold_value'} as $value) {
$summary[(string)$value['name']] = (string)$value['formatted_metric_value'];
}
}
return $summary;
}
/**
* Return key value array with information connected to account.
* @return array|mixed|null
*/
function get_account() {
$account = null;
if ($xml_string = $this->_get('/api/v1/accounts.xml')) {
$xml = simplexml_load_string($xml_string);
foreach($xml->account as $account_values) {
$account=json_decode(json_encode((array) $account_values), 1);
break;
}
}
return $account;
}
/**
* Get key value array with application settings
* @param $application_id
* @return array|mixed
*/
function get_application_settings($account_id, $application_id) {
$settings = array();
if ($xml_string = $this->_get("/api/v1/accounts/{$account_id}/application_settings/{$application_id}.xml")) {
$xml = simplexml_load_string($xml_string);
$settings=json_decode(json_encode((array) $xml), 1);
}
return $settings;
}
/**
* Update application settings. verifies the keys in provided settings array is acceptable
* @param $application_id
* @param $settings
* @return bool
*/
function update_application_settings($account_id, $application_id, $settings) {
$supported = array('alerts_enabled', 'app_apdex_t','rum_apdex_t','rum_enabled');
$call = "/api/v1/accounts/{$account_id}/application_settings/{$application_id}.xml";
$params = array();
foreach($settings as $key => $value) {
if (in_array($key, $supported))
$params[$key] = $value;
}
return $this->_put($call, $params);
}
/**
* Returns the available metric names for provided application
* @param $application_id
* @param string $regex
* @param string $limit
* @return array|mixed
*/
function get_metric_names($application_id, $regex = '', $limit = '') {
$call = "/api/v1/agents/{$application_id}/metrics.json";
$callQS = '';
$qs = array();
if ($regex)
$qs[] = 're=' . urlencode($regex);
if ($limit)
$qs[] = 'limit=' . $limit;
if ($qs)
$callQS = '?' . implode('&', $qs);
$json = $this->_get($call . $callQS);
$metric_names=json_decode($json);
return $metric_names;
}
/**
* Gets the metric data for the provided metric names.
* @param string $account_id
* @param string $application_id
* @param string $begin XML date in GMT
* @param string $to XML date in GMT
* @param array $metrics
* @param string $field
* @param bool $summary if values should be merged or overtime
* @return array|mixed
*/
function get_metric_data($account_id, $application_id, $begin, $to, $metrics, $field, $summary = true) {
$metricParamArray = array();
foreach($metrics as $metric) {
$metricParamArray[] = 'metrics[]=' . urlencode($metric);
}
$metricQS = implode('&', $metricParamArray);
$fieldsQS = 'field=' . urlencode($field);
$agentQS = 'agent_id=' . urlencode($application_id);
$beginQS = 'begin=' . urlencode($begin);
$toQS = 'end=' . urlencode($to);
$summaryQS = $summary ? 'summary=1' : 'summary=0';
$command = $beginQS . '&' . $toQS . '&' . $metricQS . '&' . $fieldsQS . '&' . $agentQS . '&' . $summaryQS;
$json = $this->_get("/api/v1/accounts/{$account_id}/metrics/data.json?{$command}");
$metric_data=json_decode($json);
return $metric_data;
}
}

View File

@@ -0,0 +1,84 @@
<?php
/**
* Wraps around New Relic PHP Agent functions and makes sure the function exists.
*/
class NewRelicWrapper {
public static function set_appname($name, $license = '', $xmit =false) {
self::call('newrelic_set_appname',array($name, $license, $xmit));
}
public static function mark_as_background_job($flag = true) {
self::call('newrelic_background_job', $flag);
}
public static function disable_auto_rum() {
self::call('newrelic_disable_autorum');
}
public static function name_transaction($name) {
self::call('newrelic_name_transaction', $name);
}
public static function get_browser_timing_header() {
return self::call('newrelic_get_browser_timing_header');
}
public static function get_browser_timing_footer() {
return self::call('newrelic_get_browser_timing_footer');
}
public static function ignore_transaction(){
self::call('newrelic_ignore_transaction');
}
public static function ignore_apdex() {
self::call('newrelic_ignore_apdex');
}
public static function start_transaction ($appname, $license = '') {
$args = array();
$args[] = $appname;
if ($license)
$args[] = $license;
self::call('newrelic_start_transaction', $args);
}
public static function add_custom_parameter($name, $value) {
self::call('newrelic_add_custom_parameter', array($name, $value));
}
public static function capture_params($enable = true) {
self::call('newrelic_capture_params', $enable);
}
public static function custom_metric($name, $value) {
self::call('newrelic_custom_metric', array($name, $value));
}
public static function add_custom_tracer($function) {
self::call('newrelic_add_custom_tracer', $function);
}
public static function end_of_transaction() {
self::call('newrelic_end_of_transaction');
}
public static function end_transaction() {
self::call('newrelic_end_transaction');
}
public static function set_user_attributes($user = '', $account = '', $product = '') {
self::call('newrelic_set_user_attributes', array($user, $account, $product));
}
private static function call($function, $args = null) {
if (function_exists($function)) {
if ($args)
if (is_array($args))
return call_user_func_array($function, $args);
else
return call_user_func($function, $args);
else
return call_user_func($function);
}
}
}

View File

@@ -0,0 +1,993 @@
<?php
/*
$Id: class.nusoap_base.php,v 1.56 2010/04/26 20:15:08 snichol Exp $
NuSOAP - Web Services Toolkit for PHP
Copyright (c) 2002 NuSphere Corporation
This library 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.
This library 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 library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
The NuSOAP project home is:
http://sourceforge.net/projects/nusoap/
The primary support for NuSOAP is the Help forum on the project home page.
If you have any questions or comments, please email:
Dietrich Ayala
dietrich@ganx4.com
http://dietrich.ganx4.com/nusoap
NuSphere Corporation
http://www.nusphere.com
*/
/*
* Some of the standards implmented in whole or part by NuSOAP:
*
* SOAP 1.1 (http://www.w3.org/TR/2000/NOTE-SOAP-20000508/)
* WSDL 1.1 (http://www.w3.org/TR/2001/NOTE-wsdl-20010315)
* SOAP Messages With Attachments (http://www.w3.org/TR/SOAP-attachments)
* XML 1.0 (http://www.w3.org/TR/2006/REC-xml-20060816/)
* Namespaces in XML 1.0 (http://www.w3.org/TR/2006/REC-xml-names-20060816/)
* XML Schema 1.0 (http://www.w3.org/TR/xmlschema-0/)
* RFC 2045 Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies
* RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1
* RFC 2617 HTTP Authentication: Basic and Digest Access Authentication
*/
/* load classes
// necessary classes
require_once('class.soapclient.php');
require_once('class.soap_val.php');
require_once('class.soap_parser.php');
require_once('class.soap_fault.php');
// transport classes
require_once('class.soap_transport_http.php');
// optional add-on classes
require_once('class.xmlschema.php');
require_once('class.wsdl.php');
// server class
require_once('class.soap_server.php');*/
// class variable emulation
// cf. http://www.webkreator.com/php/techniques/php-static-class-variables.html
$GLOBALS['_transient']['static']['nusoap_base']['globalDebugLevel'] = 9;
/**
*
* nusoap_base
*
* @author Dietrich Ayala <dietrich@ganx4.com>
* @author Scott Nichol <snichol@users.sourceforge.net>
* @version $Id: class.nusoap_base.php,v 1.56 2010/04/26 20:15:08 snichol Exp $
* @access public
*/
class nusoap_base {
/**
* Identification for HTTP headers.
*
* @var string
* @access private
*/
var $title = 'NuSOAP';
/**
* Version for HTTP headers.
*
* @var string
* @access private
*/
var $version = '0.9.5';
/**
* CVS revision for HTTP headers.
*
* @var string
* @access private
*/
var $revision = '$Revision: 1.56 $';
/**
* Current error string (manipulated by getError/setError)
*
* @var string
* @access private
*/
var $error_str = '';
/**
* Current debug string (manipulated by debug/appendDebug/clearDebug/getDebug/getDebugAsXMLComment)
*
* @var string
* @access private
*/
var $debug_str = '';
/**
* toggles automatic encoding of special characters as entities
* (should always be true, I think)
*
* @var boolean
* @access private
*/
var $charencoding = true;
/**
* the debug level for this instance
*
* @var integer
* @access private
*/
var $debugLevel;
/**
* set schema version
*
* @var string
* @access public
*/
var $XMLSchemaVersion = 'http://www.w3.org/2001/XMLSchema';
/**
* charset encoding for outgoing messages
*
* @var string
* @access public
*/
var $soap_defencoding = 'ISO-8859-1';
//var $soap_defencoding = 'UTF-8';
/**
* namespaces in an array of prefix => uri
*
* this is "seeded" by a set of constants, but it may be altered by code
*
* @var array
* @access public
*/
var $namespaces = array(
'SOAP-ENV' => 'http://schemas.xmlsoap.org/soap/envelope/',
'xsd' => 'http://www.w3.org/2001/XMLSchema',
'xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'SOAP-ENC' => 'http://schemas.xmlsoap.org/soap/encoding/'
);
/**
* namespaces used in the current context, e.g. during serialization
*
* @var array
* @access private
*/
var $usedNamespaces = array();
/**
* XML Schema types in an array of uri => (array of xml type => php type)
* is this legacy yet?
* no, this is used by the nusoap_xmlschema class to verify type => namespace mappings.
* @var array
* @access public
*/
var $typemap = array(
'http://www.w3.org/2001/XMLSchema' => array(
'string'=>'string','boolean'=>'boolean','float'=>'double','double'=>'double','decimal'=>'double',
'duration'=>'','dateTime'=>'string','time'=>'string','date'=>'string','gYearMonth'=>'',
'gYear'=>'','gMonthDay'=>'','gDay'=>'','gMonth'=>'','hexBinary'=>'string','base64Binary'=>'string',
// abstract "any" types
'anyType'=>'string','anySimpleType'=>'string',
// derived datatypes
'normalizedString'=>'string','token'=>'string','language'=>'','NMTOKEN'=>'','NMTOKENS'=>'','Name'=>'','NCName'=>'','ID'=>'',
'IDREF'=>'','IDREFS'=>'','ENTITY'=>'','ENTITIES'=>'','integer'=>'integer','nonPositiveInteger'=>'integer',
'negativeInteger'=>'integer','long'=>'integer','int'=>'integer','short'=>'integer','byte'=>'integer','nonNegativeInteger'=>'integer',
'unsignedLong'=>'','unsignedInt'=>'','unsignedShort'=>'','unsignedByte'=>'','positiveInteger'=>''),
'http://www.w3.org/2000/10/XMLSchema' => array(
'i4'=>'','int'=>'integer','boolean'=>'boolean','string'=>'string','double'=>'double',
'float'=>'double','dateTime'=>'string',
'timeInstant'=>'string','base64Binary'=>'string','base64'=>'string','ur-type'=>'array'),
'http://www.w3.org/1999/XMLSchema' => array(
'i4'=>'','int'=>'integer','boolean'=>'boolean','string'=>'string','double'=>'double',
'float'=>'double','dateTime'=>'string',
'timeInstant'=>'string','base64Binary'=>'string','base64'=>'string','ur-type'=>'array'),
'http://soapinterop.org/xsd' => array('SOAPStruct'=>'struct'),
'http://schemas.xmlsoap.org/soap/encoding/' => array('base64'=>'string','array'=>'array','Array'=>'array'),
'http://xml.apache.org/xml-soap' => array('Map')
);
/**
* XML entities to convert
*
* @var array
* @access public
* @deprecated
* @see expandEntities
*/
var $xmlEntities = array('quot' => '"','amp' => '&',
'lt' => '<','gt' => '>','apos' => "'");
/**
* constructor
*
* @access public
*/
function __construct() {
$this->debugLevel = $GLOBALS['_transient']['static']['nusoap_base']['globalDebugLevel'];
}
/**
* gets the global debug level, which applies to future instances
*
* @return integer Debug level 0-9, where 0 turns off
* @access public
*/
function getGlobalDebugLevel() {
return $GLOBALS['_transient']['static']['nusoap_base']['globalDebugLevel'];
}
/**
* sets the global debug level, which applies to future instances
*
* @param int $level Debug level 0-9, where 0 turns off
* @access public
*/
function setGlobalDebugLevel($level) {
$GLOBALS['_transient']['static']['nusoap_base']['globalDebugLevel'] = $level;
}
/**
* gets the debug level for this instance
*
* @return int Debug level 0-9, where 0 turns off
* @access public
*/
function getDebugLevel() {
return $this->debugLevel;
}
/**
* sets the debug level for this instance
*
* @param int $level Debug level 0-9, where 0 turns off
* @access public
*/
function setDebugLevel($level) {
$this->debugLevel = $level;
}
/**
* adds debug data to the instance debug string with formatting
*
* @param string $string debug data
* @access private
*/
function debug($string){
if ($this->debugLevel > 0) {
$this->appendDebug($this->getmicrotime().' '.get_class($this).": $string\n");
}
}
/**
* adds debug data to the instance debug string without formatting
*
* @param string $string debug data
* @access public
*/
function appendDebug($string){
if ($this->debugLevel > 0) {
// it would be nice to use a memory stream here to use
// memory more efficiently
$this->debug_str .= $string;
}
}
/**
* clears the current debug data for this instance
*
* @access public
*/
function clearDebug() {
// it would be nice to use a memory stream here to use
// memory more efficiently
$this->debug_str = '';
}
/**
* gets the current debug data for this instance
*
* @return debug data
* @access public
*/
function &getDebug() {
// it would be nice to use a memory stream here to use
// memory more efficiently
return $this->debug_str;
}
/**
* gets the current debug data for this instance as an XML comment
* this may change the contents of the debug data
*
* @return debug data as an XML comment
* @access public
*/
function &getDebugAsXMLComment() {
// it would be nice to use a memory stream here to use
// memory more efficiently
while (strpos($this->debug_str, '--')) {
$this->debug_str = str_replace('--', '- -', $this->debug_str);
}
$ret = "<!--\n" . $this->debug_str . "\n-->";
return $ret;
}
/**
* expands entities, e.g. changes '<' to '&lt;'.
*
* @param string $val The string in which to expand entities.
* @access private
*/
function expandEntities($val) {
if ($this->charencoding) {
$val = str_replace('&', '&amp;', $val);
$val = str_replace("'", '&apos;', $val);
$val = str_replace('"', '&quot;', $val);
$val = str_replace('<', '&lt;', $val);
$val = str_replace('>', '&gt;', $val);
}
return $val;
}
/**
* returns error string if present
*
* @return mixed error string or false
* @access public
*/
function getError(){
if($this->error_str != ''){
return $this->error_str;
}
return false;
}
/**
* sets error string
*
* @return boolean $string error string
* @access private
*/
function setError($str){
$this->error_str = $str;
}
/**
* detect if array is a simple array or a struct (associative array)
*
* @param mixed $val The PHP array
* @return string (arraySimple|arrayStruct)
* @access private
*/
function isArraySimpleOrStruct($val) {
$keyList = array_keys($val);
foreach ($keyList as $keyListValue) {
if (!is_int($keyListValue)) {
return 'arrayStruct';
}
}
return 'arraySimple';
}
/**
* serializes PHP values in accordance w/ section 5. Type information is
* not serialized if $use == 'literal'.
*
* @param mixed $val The value to serialize
* @param string $name The name (local part) of the XML element
* @param string $type The XML schema type (local part) for the element
* @param string $name_ns The namespace for the name of the XML element
* @param string $type_ns The namespace for the type of the element
* @param array $attributes The attributes to serialize as name=>value pairs
* @param string $use The WSDL "use" (encoded|literal)
* @param boolean $soapval Whether this is called from soapval.
* @return string The serialized element, possibly with child elements
* @access public
*/
function serialize_val($val,$name=false,$type=false,$name_ns=false,$type_ns=false,$attributes=false,$use='encoded',$soapval=false) {
$this->debug("in serialize_val: name=$name, type=$type, name_ns=$name_ns, type_ns=$type_ns, use=$use, soapval=$soapval");
$this->appendDebug('value=' . $this->varDump($val));
$this->appendDebug('attributes=' . $this->varDump($attributes));
if (is_object($val) && get_class($val) == 'soapval' && (! $soapval)) {
$this->debug("serialize_val: serialize soapval");
$xml = $val->serialize($use);
$this->appendDebug($val->getDebug());
$val->clearDebug();
$this->debug("serialize_val of soapval returning $xml");
return $xml;
}
// force valid name if necessary
if (is_numeric($name)) {
$name = '__numeric_' . $name;
} elseif (! $name) {
$name = 'noname';
}
// if name has ns, add ns prefix to name
$xmlns = '';
if($name_ns){
$prefix = 'nu'.rand(1000,9999);
$name = $prefix.':'.$name;
$xmlns .= " xmlns:$prefix=\"$name_ns\"";
}
// if type is prefixed, create type prefix
if($type_ns != '' && $type_ns == $this->namespaces['xsd']){
// need to fix this. shouldn't default to xsd if no ns specified
// w/o checking against typemap
$type_prefix = 'xsd';
} elseif($type_ns){
$type_prefix = 'ns'.rand(1000,9999);
$xmlns .= " xmlns:$type_prefix=\"$type_ns\"";
}
// serialize attributes if present
$atts = '';
if($attributes){
foreach($attributes as $k => $v){
$atts .= " $k=\"".$this->expandEntities($v).'"';
}
}
// serialize null value
if (is_null($val)) {
$this->debug("serialize_val: serialize null");
if ($use == 'literal') {
// TODO: depends on minOccurs
$xml = "<$name$xmlns$atts/>";
$this->debug("serialize_val returning $xml");
return $xml;
} else {
if (isset($type) && isset($type_prefix)) {
$type_str = " xsi:type=\"$type_prefix:$type\"";
} else {
$type_str = '';
}
$xml = "<$name$xmlns$type_str$atts xsi:nil=\"true\"/>";
$this->debug("serialize_val returning $xml");
return $xml;
}
}
// serialize if an xsd built-in primitive type
if($type != '' && isset($this->typemap[$this->XMLSchemaVersion][$type])){
$this->debug("serialize_val: serialize xsd built-in primitive type");
if (is_bool($val)) {
if ($type == 'boolean') {
$val = $val ? 'true' : 'false';
} elseif (! $val) {
$val = 0;
}
} else if (is_string($val)) {
$val = $this->expandEntities($val);
}
if ($use == 'literal') {
$xml = "<$name$xmlns$atts>$val</$name>";
$this->debug("serialize_val returning $xml");
return $xml;
} else {
$xml = "<$name$xmlns xsi:type=\"xsd:$type\"$atts>$val</$name>";
$this->debug("serialize_val returning $xml");
return $xml;
}
}
// detect type and serialize
$xml = '';
switch(true) {
case (is_bool($val) || $type == 'boolean'):
$this->debug("serialize_val: serialize boolean");
if ($type == 'boolean') {
$val = $val ? 'true' : 'false';
} elseif (! $val) {
$val = 0;
}
if ($use == 'literal') {
$xml .= "<$name$xmlns$atts>$val</$name>";
} else {
$xml .= "<$name$xmlns xsi:type=\"xsd:boolean\"$atts>$val</$name>";
}
break;
case (is_int($val) || is_long($val) || $type == 'int'):
$this->debug("serialize_val: serialize int");
if ($use == 'literal') {
$xml .= "<$name$xmlns$atts>$val</$name>";
} else {
$xml .= "<$name$xmlns xsi:type=\"xsd:int\"$atts>$val</$name>";
}
break;
case (is_float($val)|| is_double($val) || $type == 'float'):
$this->debug("serialize_val: serialize float");
if ($use == 'literal') {
$xml .= "<$name$xmlns$atts>$val</$name>";
} else {
$xml .= "<$name$xmlns xsi:type=\"xsd:float\"$atts>$val</$name>";
}
break;
case (is_string($val) || $type == 'string'):
$this->debug("serialize_val: serialize string");
$val = $this->expandEntities($val);
if ($use == 'literal') {
$xml .= "<$name$xmlns$atts>$val</$name>";
} else {
$xml .= "<$name$xmlns xsi:type=\"xsd:string\"$atts>$val</$name>";
}
break;
case is_object($val):
$this->debug("serialize_val: serialize object");
if (get_class($val) == 'soapval') {
$this->debug("serialize_val: serialize soapval object");
$pXml = $val->serialize($use);
$this->appendDebug($val->getDebug());
$val->clearDebug();
} else {
if (! $name) {
$name = get_class($val);
$this->debug("In serialize_val, used class name $name as element name");
} else {
$this->debug("In serialize_val, do not override name $name for element name for class " . get_class($val));
}
foreach(get_object_vars($val) as $k => $v){
$pXml = isset($pXml) ? $pXml.$this->serialize_val($v,$k,false,false,false,false,$use) : $this->serialize_val($v,$k,false,false,false,false,$use);
}
}
if(isset($type) && isset($type_prefix)){
$type_str = " xsi:type=\"$type_prefix:$type\"";
} else {
$type_str = '';
}
if ($use == 'literal') {
$xml .= "<$name$xmlns$atts>$pXml</$name>";
} else {
$xml .= "<$name$xmlns$type_str$atts>$pXml</$name>";
}
break;
break;
case (is_array($val) || $type):
// detect if struct or array
$valueType = $this->isArraySimpleOrStruct($val);
if($valueType=='arraySimple' || preg_match('/^ArrayOf/',$type)){
$this->debug("serialize_val: serialize array");
$i = 0;
if(is_array($val) && count($val)> 0){
foreach($val as $v){
if(is_object($v) && get_class($v) == 'soapval'){
$tt_ns = $v->type_ns;
$tt = $v->type;
} elseif (is_array($v)) {
$tt = $this->isArraySimpleOrStruct($v);
} else {
$tt = gettype($v);
}
$array_types[$tt] = 1;
// TODO: for literal, the name should be $name
$xml .= $this->serialize_val($v,'item',false,false,false,false,$use);
++$i;
}
if(count($array_types) > 1){
$array_typename = 'xsd:anyType';
} elseif(isset($tt) && isset($this->typemap[$this->XMLSchemaVersion][$tt])) {
if ($tt == 'integer') {
$tt = 'int';
}
$array_typename = 'xsd:'.$tt;
} elseif(isset($tt) && $tt == 'arraySimple'){
$array_typename = 'SOAP-ENC:Array';
} elseif(isset($tt) && $tt == 'arrayStruct'){
$array_typename = 'unnamed_struct_use_soapval';
} else {
// if type is prefixed, create type prefix
if ($tt_ns != '' && $tt_ns == $this->namespaces['xsd']){
$array_typename = 'xsd:' . $tt;
} elseif ($tt_ns) {
$tt_prefix = 'ns' . rand(1000, 9999);
$array_typename = "$tt_prefix:$tt";
$xmlns .= " xmlns:$tt_prefix=\"$tt_ns\"";
} else {
$array_typename = $tt;
}
}
$array_type = $i;
if ($use == 'literal') {
$type_str = '';
} else if (isset($type) && isset($type_prefix)) {
$type_str = " xsi:type=\"$type_prefix:$type\"";
} else {
$type_str = " xsi:type=\"SOAP-ENC:Array\" SOAP-ENC:arrayType=\"".$array_typename."[$array_type]\"";
}
// empty array
} else {
if ($use == 'literal') {
$type_str = '';
} else if (isset($type) && isset($type_prefix)) {
$type_str = " xsi:type=\"$type_prefix:$type\"";
} else {
$type_str = " xsi:type=\"SOAP-ENC:Array\" SOAP-ENC:arrayType=\"xsd:anyType[0]\"";
}
}
// TODO: for array in literal, there is no wrapper here
$xml = "<$name$xmlns$type_str$atts>".$xml."</$name>";
} else {
// got a struct
$this->debug("serialize_val: serialize struct");
if(isset($type) && isset($type_prefix)){
$type_str = " xsi:type=\"$type_prefix:$type\"";
} else {
$type_str = '';
}
if ($use == 'literal') {
$xml .= "<$name$xmlns$atts>";
} else {
$xml .= "<$name$xmlns$type_str$atts>";
}
foreach($val as $k => $v){
// Apache Map
if ($type == 'Map' && $type_ns == 'http://xml.apache.org/xml-soap') {
$xml .= '<item>';
$xml .= $this->serialize_val($k,'key',false,false,false,false,$use);
$xml .= $this->serialize_val($v,'value',false,false,false,false,$use);
$xml .= '</item>';
} else {
$xml .= $this->serialize_val($v,$k,false,false,false,false,$use);
}
}
$xml .= "</$name>";
}
break;
default:
$this->debug("serialize_val: serialize unknown");
$xml .= 'not detected, got '.gettype($val).' for '.$val;
break;
}
$this->debug("serialize_val returning $xml");
return $xml;
}
/**
* serializes a message
*
* @param string $body the XML of the SOAP body
* @param mixed $headers optional string of XML with SOAP header content, or array of soapval objects for SOAP headers, or associative array
* @param array $namespaces optional the namespaces used in generating the body and headers
* @param string $style optional (rpc|document)
* @param string $use optional (encoded|literal)
* @param string $encodingStyle optional (usually 'http://schemas.xmlsoap.org/soap/encoding/' for encoded)
* @return string the message
* @access public
*/
function serializeEnvelope($body,$headers=false,$namespaces=array(),$style='rpc',$use='encoded',$encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'){
// TODO: add an option to automatically run utf8_encode on $body and $headers
// if $this->soap_defencoding is UTF-8. Not doing this automatically allows
// one to send arbitrary UTF-8 characters, not just characters that map to ISO-8859-1
$this->debug("In serializeEnvelope length=" . strlen($body) . " body (max 1000 characters)=" . substr($body, 0, 1000) . " style=$style use=$use encodingStyle=$encodingStyle");
$this->debug("headers:");
$this->appendDebug($this->varDump($headers));
$this->debug("namespaces:");
$this->appendDebug($this->varDump($namespaces));
// serialize namespaces
$ns_string = '';
foreach(array_merge($this->namespaces,$namespaces) as $k => $v){
$ns_string .= " xmlns:$k=\"$v\"";
}
if($encodingStyle) {
$ns_string = " SOAP-ENV:encodingStyle=\"$encodingStyle\"$ns_string";
}
// serialize headers
if($headers){
if (is_array($headers)) {
$xml = '';
foreach ($headers as $k => $v) {
if (is_object($v) && get_class($v) == 'soapval') {
$xml .= $this->serialize_val($v, false, false, false, false, false, $use);
} else {
$xml .= $this->serialize_val($v, $k, false, false, false, false, $use);
}
}
$headers = $xml;
$this->debug("In serializeEnvelope, serialized array of headers to $headers");
}
$headers = "<SOAP-ENV:Header>".$headers."</SOAP-ENV:Header>";
}
// serialize envelope
return
'<?xml version="1.0" encoding="'.$this->soap_defencoding .'"?'.">".
'<SOAP-ENV:Envelope'.$ns_string.">".
$headers.
"<SOAP-ENV:Body>".
$body.
"</SOAP-ENV:Body>".
"</SOAP-ENV:Envelope>";
}
/**
* formats a string to be inserted into an HTML stream
*
* @param string $str The string to format
* @return string The formatted string
* @access public
* @deprecated
*/
function formatDump($str){
$str = htmlspecialchars($str);
return nl2br($str);
}
/**
* contracts (changes namespace to prefix) a qualified name
*
* @param string $qname qname
* @return string contracted qname
* @access private
*/
function contractQname($qname){
// get element namespace
//$this->xdebug("Contract $qname");
if (strrpos($qname, ':')) {
// get unqualified name
$name = substr($qname, strrpos($qname, ':') + 1);
// get ns
$ns = substr($qname, 0, strrpos($qname, ':'));
$p = $this->getPrefixFromNamespace($ns);
if ($p) {
return $p . ':' . $name;
}
return $qname;
} else {
return $qname;
}
}
/**
* expands (changes prefix to namespace) a qualified name
*
* @param string $qname qname
* @return string expanded qname
* @access private
*/
function expandQname($qname){
// get element prefix
if(strpos($qname,':') && !preg_match('/^http:\/\//',$qname)){
// get unqualified name
$name = substr(strstr($qname,':'),1);
// get ns prefix
$prefix = substr($qname,0,strpos($qname,':'));
if(isset($this->namespaces[$prefix])){
return $this->namespaces[$prefix].':'.$name;
} else {
return $qname;
}
} else {
return $qname;
}
}
/**
* returns the local part of a prefixed string
* returns the original string, if not prefixed
*
* @param string $str The prefixed string
* @return string The local part
* @access public
*/
function getLocalPart($str){
if($sstr = strrchr($str,':')){
// get unqualified name
return substr( $sstr, 1 );
} else {
return $str;
}
}
/**
* returns the prefix part of a prefixed string
* returns false, if not prefixed
*
* @param string $str The prefixed string
* @return mixed The prefix or false if there is no prefix
* @access public
*/
function getPrefix($str){
if($pos = strrpos($str,':')){
// get prefix
return substr($str,0,$pos);
}
return false;
}
/**
* pass it a prefix, it returns a namespace
*
* @param string $prefix The prefix
* @return mixed The namespace, false if no namespace has the specified prefix
* @access public
*/
function getNamespaceFromPrefix($prefix){
if (isset($this->namespaces[$prefix])) {
return $this->namespaces[$prefix];
}
//$this->setError("No namespace registered for prefix '$prefix'");
return false;
}
/**
* returns the prefix for a given namespace (or prefix)
* or false if no prefixes registered for the given namespace
*
* @param string $ns The namespace
* @return mixed The prefix, false if the namespace has no prefixes
* @access public
*/
function getPrefixFromNamespace($ns) {
foreach ($this->namespaces as $p => $n) {
if ($ns == $n || $ns == $p) {
$this->usedNamespaces[$p] = $n;
return $p;
}
}
return false;
}
/**
* returns the time in ODBC canonical form with microseconds
*
* @return string The time in ODBC canonical form with microseconds
* @access public
*/
function getmicrotime() {
if (function_exists('gettimeofday')) {
$tod = gettimeofday();
$sec = $tod['sec'];
$usec = $tod['usec'];
} else {
$sec = time();
$usec = 0;
}
return strftime('%Y-%m-%d %H:%M:%S', $sec) . '.' . sprintf('%06d', $usec);
}
/**
* Returns a string with the output of var_dump
*
* @param mixed $data The variable to var_dump
* @return string The output of var_dump
* @access public
*/
function varDump($data) {
ob_start();
var_dump($data);
$ret_val = ob_get_contents();
ob_end_clean();
return $ret_val;
}
/**
* represents the object as a string
*
* @return string
* @access public
*/
function __toString() {
return $this->varDump($this);
}
}
// XML Schema Datatype Helper Functions
//xsd:dateTime helpers
/**
* convert unix timestamp to ISO 8601 compliant date string
*
* @param int $timestamp Unix time stamp
* @param boolean $utc Whether the time stamp is UTC or local
* @return mixed ISO 8601 date string or false
* @access public
*/
function timestamp_to_iso8601($timestamp,$utc=true){
$datestr = date('Y-m-d\TH:i:sO',$timestamp);
$pos = strrpos($datestr, "+");
if ($pos === FALSE) {
$pos = strrpos($datestr, "-");
}
if ($pos !== FALSE) {
if (strlen($datestr) == $pos + 5) {
$datestr = substr($datestr, 0, $pos + 3) . ':' . substr($datestr, -2);
}
}
if($utc){
$pattern = '/'.
'([0-9]{4})-'. // centuries & years CCYY-
'([0-9]{2})-'. // months MM-
'([0-9]{2})'. // days DD
'T'. // separator T
'([0-9]{2}):'. // hours hh:
'([0-9]{2}):'. // minutes mm:
'([0-9]{2})(\.[0-9]*)?'. // seconds ss.ss...
'(Z|[+\-][0-9]{2}:?[0-9]{2})?'. // Z to indicate UTC, -/+HH:MM:SS.SS... for local tz's
'/';
if(preg_match($pattern,$datestr,$regs)){
return sprintf('%04d-%02d-%02dT%02d:%02d:%02dZ',$regs[1],$regs[2],$regs[3],$regs[4],$regs[5],$regs[6]);
}
return false;
} else {
return $datestr;
}
}
/**
* convert ISO 8601 compliant date string to unix timestamp
*
* @param string $datestr ISO 8601 compliant date string
* @return mixed Unix timestamp (int) or false
* @access public
*/
function iso8601_to_timestamp($datestr){
$pattern = '/'.
'([0-9]{4})-'. // centuries & years CCYY-
'([0-9]{2})-'. // months MM-
'([0-9]{2})'. // days DD
'T'. // separator T
'([0-9]{2}):'. // hours hh:
'([0-9]{2}):'. // minutes mm:
'([0-9]{2})(\.[0-9]+)?'. // seconds ss.ss...
'(Z|[+\-][0-9]{2}:?[0-9]{2})?'. // Z to indicate UTC, -/+HH:MM:SS.SS... for local tz's
'/';
if(preg_match($pattern,$datestr,$regs)){
// not utc
if($regs[8] != 'Z'){
$op = substr($regs[8],0,1);
$h = substr($regs[8],1,2);
$m = substr($regs[8],strlen($regs[8])-2,2);
if($op == '-'){
$regs[4] = $regs[4] + $h;
$regs[5] = $regs[5] + $m;
} elseif($op == '+'){
$regs[4] = $regs[4] - $h;
$regs[5] = $regs[5] - $m;
}
}
return gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
// return strtotime("$regs[1]-$regs[2]-$regs[3] $regs[4]:$regs[5]:$regs[6]Z");
} else {
return false;
}
}
/**
* sleeps some number of microseconds
*
* @param string $usec the number of microseconds to sleep
* @access public
* @deprecated
*/
function usleepWindows($usec)
{
$start = gettimeofday();
do
{
$stop = gettimeofday();
$timePassed = 1000000 * ($stop['sec'] - $start['sec'])
+ $stop['usec'] - $start['usec'];
}
while ($timePassed < $usec);
}

View File

@@ -0,0 +1,88 @@
<?php
if (!defined('W3TC')) {
die();
}
/**
* Contains information for a SOAP fault.
* Mainly used for returning faults from deployed functions
* in a server instance.
* @author Dietrich Ayala <dietrich@ganx4.com>
* @version $Id: class.soap_fault.php,v 1.14 2007/04/11 15:49:47 snichol Exp $
* @access public
*/
class nusoap_fault extends nusoap_base {
/**
* The fault code (client|server)
* @var string
* @access private
*/
var $faultcode;
/**
* The fault actor
* @var string
* @access private
*/
var $faultactor;
/**
* The fault string, a description of the fault
* @var string
* @access private
*/
var $faultstring;
/**
* The fault detail, typically a string or array of string
* @var mixed
* @access private
*/
var $faultdetail;
/**
* constructor
*
* @param string $faultcode (SOAP-ENV:Client | SOAP-ENV:Server)
* @param string $faultactor only used when msg routed between multiple actors
* @param string $faultstring human readable error message
* @param mixed $faultdetail detail, typically a string or array of string
*/
function __construct($faultcode,$faultactor='',$faultstring='',$faultdetail=''){
parent::nusoap_base();
$this->faultcode = $faultcode;
$this->faultactor = $faultactor;
$this->faultstring = $faultstring;
$this->faultdetail = $faultdetail;
}
/**
* serialize a fault
*
* @return string The serialization of the fault instance.
* @access public
*/
function serialize(){
$ns_string = '';
foreach($this->namespaces as $k => $v){
$ns_string .= "\n xmlns:$k=\"$v\"";
}
$return_msg =
'<?xml version="1.0" encoding="'.$this->soap_defencoding.'"?>'.
'<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"'.$ns_string.">\n".
'<SOAP-ENV:Body>'.
'<SOAP-ENV:Fault>'.
$this->serialize_val($this->faultcode, 'faultcode').
$this->serialize_val($this->faultactor, 'faultactor').
$this->serialize_val($this->faultstring, 'faultstring').
$this->serialize_val($this->faultdetail, 'detail').
'</SOAP-ENV:Fault>'.
'</SOAP-ENV:Body>'.
'</SOAP-ENV:Envelope>';
return $return_msg;
}
}
/**
* Backward compatibility
*/
class soap_fault extends nusoap_fault {
}

View File

@@ -0,0 +1,641 @@
<?php
if (!defined('W3TC')) {
die();
}
/**
*
* nusoap_parser class parses SOAP XML messages into native PHP values
*
* @author Dietrich Ayala <dietrich@ganx4.com>
* @author Scott Nichol <snichol@users.sourceforge.net>
* @version $Id: class.soap_parser.php,v 1.42 2010/04/26 20:15:08 snichol Exp $
* @access public
*/
class nusoap_parser extends nusoap_base {
var $xml = '';
var $xml_encoding = '';
var $method = '';
var $root_struct = '';
var $root_struct_name = '';
var $root_struct_namespace = '';
var $root_header = '';
var $document = ''; // incoming SOAP body (text)
// determines where in the message we are (envelope,header,body,method)
var $status = '';
var $position = 0;
var $depth = 0;
var $default_namespace = '';
var $namespaces = array();
var $message = array();
var $parent = '';
var $fault = false;
var $fault_code = '';
var $fault_str = '';
var $fault_detail = '';
var $depth_array = array();
var $debug_flag = true;
var $soapresponse = NULL; // parsed SOAP Body
var $soapheader = NULL; // parsed SOAP Header
var $responseHeaders = ''; // incoming SOAP headers (text)
var $body_position = 0;
// for multiref parsing:
// array of id => pos
var $ids = array();
// array of id => hrefs => pos
var $multirefs = array();
// toggle for auto-decoding element content
var $decode_utf8 = true;
/**
* constructor that actually does the parsing
*
* @param string $xml SOAP message
* @param string $encoding character encoding scheme of message
* @param string $method method for which XML is parsed (unused?)
* @param string $decode_utf8 whether to decode UTF-8 to ISO-8859-1
* @access public
*/
function __construct($xml,$encoding='UTF-8',$method='',$decode_utf8=true){
parent::nusoap_base();
$this->xml = $xml;
$this->xml_encoding = $encoding;
$this->method = $method;
$this->decode_utf8 = $decode_utf8;
// Check whether content has been read.
if(!empty($xml)){
// Check XML encoding
$pos_xml = strpos($xml, '<?xml');
if ($pos_xml !== FALSE) {
$xml_decl = substr($xml, $pos_xml, strpos($xml, '?>', $pos_xml + 2) - $pos_xml + 1);
if (preg_match("/encoding=[\"']([^\"']*)[\"']/", $xml_decl, $res)) {
$xml_encoding = $res[1];
if (strtoupper($xml_encoding) != $encoding) {
$err = "Charset from HTTP Content-Type '" . $encoding . "' does not match encoding from XML declaration '" . $xml_encoding . "'";
$this->debug($err);
if ($encoding != 'ISO-8859-1' || strtoupper($xml_encoding) != 'UTF-8') {
$this->setError($err);
return;
}
// when HTTP says ISO-8859-1 (the default) and XML says UTF-8 (the typical), assume the other endpoint is just sloppy and proceed
} else {
$this->debug('Charset from HTTP Content-Type matches encoding from XML declaration');
}
} else {
$this->debug('No encoding specified in XML declaration');
}
} else {
$this->debug('No XML declaration');
}
$this->debug('Entering nusoap_parser(), length='.strlen($xml).', encoding='.$encoding);
// Create an XML parser - why not xml_parser_create_ns?
$this->parser = xml_parser_create($this->xml_encoding);
// Set the options for parsing the XML data.
//xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, $this->xml_encoding);
// Set the object for the parser.
xml_set_object($this->parser, $this);
// Set the element handlers for the parser.
xml_set_element_handler($this->parser, 'start_element','end_element');
xml_set_character_data_handler($this->parser,'character_data');
// Parse the XML file.
if(!xml_parse($this->parser,$xml,true)){
// Display an error message.
$err = sprintf('XML error parsing SOAP payload on line %d: %s',
xml_get_current_line_number($this->parser),
xml_error_string(xml_get_error_code($this->parser)));
$this->debug($err);
$this->debug("XML payload:\n" . $xml);
$this->setError($err);
} else {
$this->debug('in nusoap_parser ctor, message:');
$this->appendDebug($this->varDump($this->message));
$this->debug('parsed successfully, found root struct: '.$this->root_struct.' of name '.$this->root_struct_name);
// get final value
$this->soapresponse = $this->message[$this->root_struct]['result'];
// get header value
if($this->root_header != '' && isset($this->message[$this->root_header]['result'])){
$this->soapheader = $this->message[$this->root_header]['result'];
}
// resolve hrefs/ids
if(sizeof($this->multirefs) > 0){
foreach($this->multirefs as $id => $hrefs){
$this->debug('resolving multirefs for id: '.$id);
$idVal = $this->buildVal($this->ids[$id]);
if (is_array($idVal) && isset($idVal['!id'])) {
unset($idVal['!id']);
}
foreach($hrefs as $refPos => $ref){
$this->debug('resolving href at pos '.$refPos);
$this->multirefs[$id][$refPos] = $idVal;
}
}
}
}
xml_parser_free($this->parser);
} else {
$this->debug('xml was empty, didn\'t parse!');
$this->setError('xml was empty, didn\'t parse!');
}
}
/**
* start-element handler
*
* @param resource $parser XML parser object
* @param string $name element name
* @param array $attrs associative array of attributes
* @access private
*/
function start_element($parser, $name, $attrs) {
// position in a total number of elements, starting from 0
// update class level pos
$pos = $this->position++;
// and set mine
$this->message[$pos] = array('pos' => $pos,'children'=>'','cdata'=>'');
// depth = how many levels removed from root?
// set mine as current global depth and increment global depth value
$this->message[$pos]['depth'] = $this->depth++;
// else add self as child to whoever the current parent is
if($pos != 0){
$this->message[$this->parent]['children'] .= '|'.$pos;
}
// set my parent
$this->message[$pos]['parent'] = $this->parent;
// set self as current parent
$this->parent = $pos;
// set self as current value for this depth
$this->depth_array[$this->depth] = $pos;
// get element prefix
if(strpos($name,':')){
// get ns prefix
$prefix = substr($name,0,strpos($name,':'));
// get unqualified name
$name = substr(strstr($name,':'),1);
}
// set status
if ($name == 'Envelope' && $this->status == '') {
$this->status = 'envelope';
} elseif ($name == 'Header' && $this->status == 'envelope') {
$this->root_header = $pos;
$this->status = 'header';
} elseif ($name == 'Body' && $this->status == 'envelope'){
$this->status = 'body';
$this->body_position = $pos;
// set method
} elseif($this->status == 'body' && $pos == ($this->body_position+1)) {
$this->status = 'method';
$this->root_struct_name = $name;
$this->root_struct = $pos;
$this->message[$pos]['type'] = 'struct';
$this->debug("found root struct $this->root_struct_name, pos $this->root_struct");
}
// set my status
$this->message[$pos]['status'] = $this->status;
// set name
$this->message[$pos]['name'] = htmlspecialchars($name);
// set attrs
$this->message[$pos]['attrs'] = $attrs;
// loop through atts, logging ns and type declarations
$attstr = '';
foreach($attrs as $key => $value){
$key_prefix = $this->getPrefix($key);
$key_localpart = $this->getLocalPart($key);
// if ns declarations, add to class level array of valid namespaces
if($key_prefix == 'xmlns'){
if(preg_match('/^http:\/\/www.w3.org\/[0-9]{4}\/XMLSchema$/',$value)){
$this->XMLSchemaVersion = $value;
$this->namespaces['xsd'] = $this->XMLSchemaVersion;
$this->namespaces['xsi'] = $this->XMLSchemaVersion.'-instance';
}
$this->namespaces[$key_localpart] = $value;
// set method namespace
if($name == $this->root_struct_name){
$this->methodNamespace = $value;
}
// if it's a type declaration, set type
} elseif($key_localpart == 'type'){
if (isset($this->message[$pos]['type']) && $this->message[$pos]['type'] == 'array') {
// do nothing: already processed arrayType
} else {
$value_prefix = $this->getPrefix($value);
$value_localpart = $this->getLocalPart($value);
$this->message[$pos]['type'] = $value_localpart;
$this->message[$pos]['typePrefix'] = $value_prefix;
if(isset($this->namespaces[$value_prefix])){
$this->message[$pos]['type_namespace'] = $this->namespaces[$value_prefix];
} else if(isset($attrs['xmlns:'.$value_prefix])) {
$this->message[$pos]['type_namespace'] = $attrs['xmlns:'.$value_prefix];
}
// should do something here with the namespace of specified type?
}
} elseif($key_localpart == 'arrayType'){
$this->message[$pos]['type'] = 'array';
/* do arrayType ereg here
[1] arrayTypeValue ::= atype asize
[2] atype ::= QName rank*
[3] rank ::= '[' (',')* ']'
[4] asize ::= '[' length~ ']'
[5] length ::= nextDimension* Digit+
[6] nextDimension ::= Digit+ ','
*/
$expr = '/([A-Za-z0-9_]+):([A-Za-z]+[A-Za-z0-9_]+)\[([0-9]+),?([0-9]*)\]/';
if(preg_match($expr,$value,$regs)){
$this->message[$pos]['typePrefix'] = $regs[1];
$this->message[$pos]['arrayTypePrefix'] = $regs[1];
if (isset($this->namespaces[$regs[1]])) {
$this->message[$pos]['arrayTypeNamespace'] = $this->namespaces[$regs[1]];
} else if (isset($attrs['xmlns:'.$regs[1]])) {
$this->message[$pos]['arrayTypeNamespace'] = $attrs['xmlns:'.$regs[1]];
}
$this->message[$pos]['arrayType'] = $regs[2];
$this->message[$pos]['arraySize'] = $regs[3];
$this->message[$pos]['arrayCols'] = $regs[4];
}
// specifies nil value (or not)
} elseif ($key_localpart == 'nil'){
$this->message[$pos]['nil'] = ($value == 'true' || $value == '1');
// some other attribute
} elseif ($key != 'href' && $key != 'xmlns' && $key_localpart != 'encodingStyle' && $key_localpart != 'root') {
$this->message[$pos]['xattrs']['!' . $key] = $value;
}
if ($key == 'xmlns') {
$this->default_namespace = $value;
}
// log id
if($key == 'id'){
$this->ids[$value] = $pos;
}
// root
if($key_localpart == 'root' && $value == 1){
$this->status = 'method';
$this->root_struct_name = $name;
$this->root_struct = $pos;
$this->debug("found root struct $this->root_struct_name, pos $pos");
}
// for doclit
$attstr .= " $key=\"$value\"";
}
// get namespace - must be done after namespace atts are processed
if(isset($prefix)){
$this->message[$pos]['namespace'] = $this->namespaces[$prefix];
$this->default_namespace = $this->namespaces[$prefix];
} else {
$this->message[$pos]['namespace'] = $this->default_namespace;
}
if($this->status == 'header'){
if ($this->root_header != $pos) {
$this->responseHeaders .= "<" . (isset($prefix) ? $prefix . ':' : '') . "$name$attstr>";
}
} elseif($this->root_struct_name != ''){
$this->document .= "<" . (isset($prefix) ? $prefix . ':' : '') . "$name$attstr>";
}
}
/**
* end-element handler
*
* @param resource $parser XML parser object
* @param string $name element name
* @access private
*/
function end_element($parser, $name) {
// position of current element is equal to the last value left in depth_array for my depth
$pos = $this->depth_array[$this->depth--];
// get element prefix
if(strpos($name,':')){
// get ns prefix
$prefix = substr($name,0,strpos($name,':'));
// get unqualified name
$name = substr(strstr($name,':'),1);
}
// build to native type
if(isset($this->body_position) && $pos > $this->body_position){
// deal w/ multirefs
if(isset($this->message[$pos]['attrs']['href'])){
// get id
$id = substr($this->message[$pos]['attrs']['href'],1);
// add placeholder to href array
$this->multirefs[$id][$pos] = 'placeholder';
// add set a reference to it as the result value
$this->message[$pos]['result'] =& $this->multirefs[$id][$pos];
// build complexType values
} elseif($this->message[$pos]['children'] != ''){
// if result has already been generated (struct/array)
if(!isset($this->message[$pos]['result'])){
$this->message[$pos]['result'] = $this->buildVal($pos);
}
// build complexType values of attributes and possibly simpleContent
} elseif (isset($this->message[$pos]['xattrs'])) {
if (isset($this->message[$pos]['nil']) && $this->message[$pos]['nil']) {
$this->message[$pos]['xattrs']['!'] = null;
} elseif (isset($this->message[$pos]['cdata']) && trim($this->message[$pos]['cdata']) != '') {
if (isset($this->message[$pos]['type'])) {
$this->message[$pos]['xattrs']['!'] = $this->decodeSimple($this->message[$pos]['cdata'], $this->message[$pos]['type'], isset($this->message[$pos]['type_namespace']) ? $this->message[$pos]['type_namespace'] : '');
} else {
$parent = $this->message[$pos]['parent'];
if (isset($this->message[$parent]['type']) && ($this->message[$parent]['type'] == 'array') && isset($this->message[$parent]['arrayType'])) {
$this->message[$pos]['xattrs']['!'] = $this->decodeSimple($this->message[$pos]['cdata'], $this->message[$parent]['arrayType'], isset($this->message[$parent]['arrayTypeNamespace']) ? $this->message[$parent]['arrayTypeNamespace'] : '');
} else {
$this->message[$pos]['xattrs']['!'] = $this->message[$pos]['cdata'];
}
}
}
$this->message[$pos]['result'] = $this->message[$pos]['xattrs'];
// set value of simpleType (or nil complexType)
} else {
//$this->debug('adding data for scalar value '.$this->message[$pos]['name'].' of value '.$this->message[$pos]['cdata']);
if (isset($this->message[$pos]['nil']) && $this->message[$pos]['nil']) {
$this->message[$pos]['xattrs']['!'] = null;
} elseif (isset($this->message[$pos]['type'])) {
$this->message[$pos]['result'] = $this->decodeSimple($this->message[$pos]['cdata'], $this->message[$pos]['type'], isset($this->message[$pos]['type_namespace']) ? $this->message[$pos]['type_namespace'] : '');
} else {
$parent = $this->message[$pos]['parent'];
if (isset($this->message[$parent]['type']) && ($this->message[$parent]['type'] == 'array') && isset($this->message[$parent]['arrayType'])) {
$this->message[$pos]['result'] = $this->decodeSimple($this->message[$pos]['cdata'], $this->message[$parent]['arrayType'], isset($this->message[$parent]['arrayTypeNamespace']) ? $this->message[$parent]['arrayTypeNamespace'] : '');
} else {
$this->message[$pos]['result'] = $this->message[$pos]['cdata'];
}
}
/* add value to parent's result, if parent is struct/array
$parent = $this->message[$pos]['parent'];
if($this->message[$parent]['type'] != 'map'){
if(strtolower($this->message[$parent]['type']) == 'array'){
$this->message[$parent]['result'][] = $this->message[$pos]['result'];
} else {
$this->message[$parent]['result'][$this->message[$pos]['name']] = $this->message[$pos]['result'];
}
}
*/
}
}
// for doclit
if($this->status == 'header'){
if ($this->root_header != $pos) {
$this->responseHeaders .= "</" . (isset($prefix) ? $prefix . ':' : '') . "$name>";
}
} elseif($pos >= $this->root_struct){
$this->document .= "</" . (isset($prefix) ? $prefix . ':' : '') . "$name>";
}
// switch status
if ($pos == $this->root_struct){
$this->status = 'body';
$this->root_struct_namespace = $this->message[$pos]['namespace'];
} elseif ($pos == $this->root_header) {
$this->status = 'envelope';
} elseif ($name == 'Body' && $this->status == 'body') {
$this->status = 'envelope';
} elseif ($name == 'Header' && $this->status == 'header') { // will never happen
$this->status = 'envelope';
} elseif ($name == 'Envelope' && $this->status == 'envelope') {
$this->status = '';
}
// set parent back to my parent
$this->parent = $this->message[$pos]['parent'];
}
/**
* element content handler
*
* @param resource $parser XML parser object
* @param string $data element content
* @access private
*/
function character_data($parser, $data){
$pos = $this->depth_array[$this->depth];
if ($this->xml_encoding=='UTF-8'){
// TODO: add an option to disable this for folks who want
// raw UTF-8 that, e.g., might not map to iso-8859-1
// TODO: this can also be handled with xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, "ISO-8859-1");
if($this->decode_utf8){
$data = utf8_decode($data);
}
}
$this->message[$pos]['cdata'] .= $data;
// for doclit
if($this->status == 'header'){
$this->responseHeaders .= $data;
} else {
$this->document .= $data;
}
}
/**
* get the parsed message (SOAP Body)
*
* @return mixed
* @access public
* @deprecated use get_soapbody instead
*/
function get_response(){
return $this->soapresponse;
}
/**
* get the parsed SOAP Body (NULL if there was none)
*
* @return mixed
* @access public
*/
function get_soapbody(){
return $this->soapresponse;
}
/**
* get the parsed SOAP Header (NULL if there was none)
*
* @return mixed
* @access public
*/
function get_soapheader(){
return $this->soapheader;
}
/**
* get the unparsed SOAP Header
*
* @return string XML or empty if no Header
* @access public
*/
function getHeaders(){
return $this->responseHeaders;
}
/**
* decodes simple types into PHP variables
*
* @param string $value value to decode
* @param string $type XML type to decode
* @param string $typens XML type namespace to decode
* @return mixed PHP value
* @access private
*/
function decodeSimple($value, $type, $typens) {
// TODO: use the namespace!
if ((!isset($type)) || $type == 'string' || $type == 'long' || $type == 'unsignedLong') {
return (string) $value;
}
if ($type == 'int' || $type == 'integer' || $type == 'short' || $type == 'byte') {
return (int) $value;
}
if ($type == 'float' || $type == 'double' || $type == 'decimal') {
return (double) $value;
}
if ($type == 'boolean') {
if (strtolower($value) == 'false' || strtolower($value) == 'f') {
return false;
}
return (boolean) $value;
}
if ($type == 'base64' || $type == 'base64Binary') {
$this->debug('Decode base64 value');
return base64_decode($value);
}
// obscure numeric types
if ($type == 'nonPositiveInteger' || $type == 'negativeInteger'
|| $type == 'nonNegativeInteger' || $type == 'positiveInteger'
|| $type == 'unsignedInt'
|| $type == 'unsignedShort' || $type == 'unsignedByte') {
return (int) $value;
}
// bogus: parser treats array with no elements as a simple type
if ($type == 'array') {
return array();
}
// everything else
return (string) $value;
}
/**
* builds response structures for compound values (arrays/structs)
* and scalars
*
* @param integer $pos position in node tree
* @return mixed PHP value
* @access private
*/
function buildVal($pos){
if(!isset($this->message[$pos]['type'])){
$this->message[$pos]['type'] = '';
}
$this->debug('in buildVal() for '.$this->message[$pos]['name']."(pos $pos) of type ".$this->message[$pos]['type']);
// if there are children...
if($this->message[$pos]['children'] != ''){
$this->debug('in buildVal, there are children');
$children = explode('|',$this->message[$pos]['children']);
array_shift($children); // knock off empty
// md array
if(isset($this->message[$pos]['arrayCols']) && $this->message[$pos]['arrayCols'] != ''){
$r=0; // rowcount
$c=0; // colcount
foreach($children as $child_pos){
$this->debug("in buildVal, got an MD array element: $r, $c");
$params[$r][] = $this->message[$child_pos]['result'];
$c++;
if($c == $this->message[$pos]['arrayCols']){
$c = 0;
$r++;
}
}
// array
} elseif($this->message[$pos]['type'] == 'array' || $this->message[$pos]['type'] == 'Array'){
$this->debug('in buildVal, adding array '.$this->message[$pos]['name']);
foreach($children as $child_pos){
$params[] = &$this->message[$child_pos]['result'];
}
// apache Map type: java hashtable
} elseif($this->message[$pos]['type'] == 'Map' && $this->message[$pos]['type_namespace'] == 'http://xml.apache.org/xml-soap'){
$this->debug('in buildVal, Java Map '.$this->message[$pos]['name']);
foreach($children as $child_pos){
$kv = explode("|",$this->message[$child_pos]['children']);
$params[$this->message[$kv[1]]['result']] = &$this->message[$kv[2]]['result'];
}
// generic compound type
//} elseif($this->message[$pos]['type'] == 'SOAPStruct' || $this->message[$pos]['type'] == 'struct') {
} else {
// Apache Vector type: treat as an array
$this->debug('in buildVal, adding Java Vector or generic compound type '.$this->message[$pos]['name']);
if ($this->message[$pos]['type'] == 'Vector' && $this->message[$pos]['type_namespace'] == 'http://xml.apache.org/xml-soap') {
$notstruct = 1;
} else {
$notstruct = 0;
}
//
foreach($children as $child_pos){
if($notstruct){
$params[] = &$this->message[$child_pos]['result'];
} else {
if (isset($params[$this->message[$child_pos]['name']])) {
// de-serialize repeated element name into an array
if ((!is_array($params[$this->message[$child_pos]['name']])) || (!isset($params[$this->message[$child_pos]['name']][0]))) {
$params[$this->message[$child_pos]['name']] = array($params[$this->message[$child_pos]['name']]);
}
$params[$this->message[$child_pos]['name']][] = &$this->message[$child_pos]['result'];
} else {
$params[$this->message[$child_pos]['name']] = &$this->message[$child_pos]['result'];
}
}
}
}
if (isset($this->message[$pos]['xattrs'])) {
$this->debug('in buildVal, handling attributes');
foreach ($this->message[$pos]['xattrs'] as $n => $v) {
$params[$n] = $v;
}
}
// handle simpleContent
if (isset($this->message[$pos]['cdata']) && trim($this->message[$pos]['cdata']) != '') {
$this->debug('in buildVal, handling simpleContent');
if (isset($this->message[$pos]['type'])) {
$params['!'] = $this->decodeSimple($this->message[$pos]['cdata'], $this->message[$pos]['type'], isset($this->message[$pos]['type_namespace']) ? $this->message[$pos]['type_namespace'] : '');
} else {
$parent = $this->message[$pos]['parent'];
if (isset($this->message[$parent]['type']) && ($this->message[$parent]['type'] == 'array') && isset($this->message[$parent]['arrayType'])) {
$params['!'] = $this->decodeSimple($this->message[$pos]['cdata'], $this->message[$parent]['arrayType'], isset($this->message[$parent]['arrayTypeNamespace']) ? $this->message[$parent]['arrayTypeNamespace'] : '');
} else {
$params['!'] = $this->message[$pos]['cdata'];
}
}
}
$ret = is_array($params) ? $params : array();
$this->debug('in buildVal, return:');
$this->appendDebug($this->varDump($ret));
return $ret;
} else {
$this->debug('in buildVal, no children, building scalar');
$cdata = isset($this->message[$pos]['cdata']) ? $this->message[$pos]['cdata'] : '';
if (isset($this->message[$pos]['type'])) {
$ret = $this->decodeSimple($cdata, $this->message[$pos]['type'], isset($this->message[$pos]['type_namespace']) ? $this->message[$pos]['type_namespace'] : '');
$this->debug("in buildVal, return: $ret");
return $ret;
}
$parent = $this->message[$pos]['parent'];
if (isset($this->message[$parent]['type']) && ($this->message[$parent]['type'] == 'array') && isset($this->message[$parent]['arrayType'])) {
$ret = $this->decodeSimple($cdata, $this->message[$parent]['arrayType'], isset($this->message[$parent]['arrayTypeNamespace']) ? $this->message[$parent]['arrayTypeNamespace'] : '');
$this->debug("in buildVal, return: $ret");
return $ret;
}
$ret = $this->message[$pos]['cdata'];
$this->debug("in buildVal, return: $ret");
return $ret;
}
}
}
/**
* Backward compatibility
*/
class soap_parser extends nusoap_parser {
}

File diff suppressed because it is too large Load Diff

Some files were not shown because too many files have changed in this diff Show More