✏️ 正在编辑: XmlrpcEncoder.php
路径:
/home/eblama1/sms.karnplayinland.com/classes/PHPCompatibility/Xmlrpc/XmlrpcEncoder.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php /** * XML-RPC encoder * * Main features: * - support for <nil /> and <ex:nil /> * - implements true, XML compliant, HTML numeric entities conversion * - support for CDATA values * * @package Comodojo Spare Parts * @author Marco Giovinazzi <marco.giovinazzi@comodojo.org> * @license MIT * @link https://github.com/comodojo/xmlrpc/ * * @copyright 2020 François Jacquet * Note: Fixed encodeCall() to handle associative arrays. */ class XmlrpcEncoder { /** * Request/Response encoding * * @var string */ private $encoding; /** * References of special values (base64, datetime, cdata) * * @var array */ private $special_types = array(); /** * ex:nil switch (for apache rpc compatibility) * * @var bool */ private $use_ex_nil = false; /** * Constructor method */ final public function __construct( $encoding = '' ) { $this->encoding = $encoding ? $encoding : 'utf-8'; } /** * Set encoding * * @param string $encoding * * @return \Comodojo\Xmlrpc\XmlrpcEncoder */ final public function setEncoding($encoding) { $this->encoding = $encoding; return $this; } /** * Get encoding * * @return string */ final public function getEncoding() { return $this->encoding; } /** * Handle base64 and datetime values * * @param mixed $value The referenced value * @param string $type The type of value * * @return \Comodojo\Xmlrpc\XmlrpcEncoder */ final public function setValueType(&$value, $type) { if ( empty($value) || !in_array(strtolower($type), array("base64", "datetime", "cdata")) ) throw new Exception("Invalid value type"); $this->special_types[$value] = strtolower($type); return $this; } /** * Use <ex:nil /> instead of <nil /> (apache xmlrpc compliant) * * @param bool $mode * * @return \Comodojo\Xmlrpc\XmlrpcEncoder */ final public function useExNil($mode = true) { $this->use_ex_nil = filter_var($mode, FILTER_VALIDATE_BOOLEAN); return $this; } /** * Encode an xmlrpc call * * It expects an array of values as $data and will try to encode it as a valid xmlrpc call. * * @param string $method * @param array $data * * @return string xmlrpc formatted call * * @throws \Exception * @throws \Exception */ public function encodeCall($method, $data = array()) { $xml = new XMLWriter(); $xml->openMemory(); $xml->setIndent(false); $xml->startDocument('1.0', $this->encoding); $xml->startElement("methodCall"); $xml->writeElement("methodName", trim($method)); $xml->startElement("params"); $has_param_value_elements = false; if ( is_array( $data ) && isset( $data[0] ) && is_array( $data[0] ) ) { $data = $data[0]; $xml->startElement("param"); $xml->startElement("value"); $has_param_value_elements = true; } try { $this->encodeValue($xml, $data); } catch (Exception $xe) { throw $xe; } if ( $has_param_value_elements ) { $xml->endElement(); $xml->endElement(); } $xml->endElement(); $xml->endElement(); $xml->endDocument(); return trim($xml->outputMemory()); } /** * Encode a value using XMLWriter object $xml * * @param XMLWriter $xml * @param mixed $value * * @throws \Exception */ private function encodeValue(XMLWriter $xml, $value) { if ( $value === null ) $xml->writeRaw($this->use_ex_nil === true ? '<ex:nil />' : '<nil />'); else if ( is_array($value) ) { if ( !self::catchStruct($value) ) $this->encodeArray($xml, $value); else $this->encodeStruct($xml, $value); } else if ( @array_key_exists($value, $this->special_types) ) { switch ( $this->special_types[$value] ) { case 'base64': $xml->writeElement("base64", $value); break; case 'datetime': $xml->writeElement("dateTime.iso8601", self::timestampToIso8601Time($value)); break; case 'cdata': $xml->writeCData($value); break; } } else if ( is_bool($value) ) { $xml->writeElement("boolean", $value ? 1 : 0); } else if ( is_double($value) ) { $xml->writeElement("double", $value); } else if ( is_integer($value) ) { $xml->writeElement("int", $value); } else if ( is_object($value) ) { $this->encodeObject($xml, $value); } else if ( is_string($value) ) { $value = htmlentities($value, ENT_QUOTES, $this->encoding, false); $string = preg_replace_callback('/&([a-zA-Z][a-zA-Z0-9]+);/S', [self::class, 'numericEntities'], $value); $xml->writeRaw("<string>".$string."</string>"); } else throw new Exception("Unknown type for encoding"); } /** * Encode an array using XMLWriter object $xml * * @param XMLWriter $xml * @param mixed $value */ private function encodeArray(XMLWriter $xml, $value) { $xml->startElement("array"); $xml->startElement("data"); foreach ( $value as $entry ) { $xml->startElement("value"); $this->encodeValue($xml, $entry); $xml->endElement(); } $xml->endElement(); $xml->endElement(); } /** * Encode an object using XMLWriter object $xml * * @param XMLWriter $xml * @param mixed $value * * @throws \Exception */ private function encodeObject(XMLWriter $xml, $value) { if ( $value instanceof \DateTime ) $xml->writeElement("dateTime.iso8601", self::timestampToIso8601Time($value->format('U'))); else throw new Exception("Unknown type for encoding"); } /** * Encode a struct using XMLWriter object $xml * * @param XMLWriter $xml * @param mixed $value * * @throws \Exception */ private function encodeStruct(XMLWriter $xml, $value) { $xml->startElement("struct"); foreach ( $value as $k => $v ) { $xml->startElement("member"); $xml->writeElement("name", $k); $xml->startElement("value"); $this->encodeValue($xml, $v); $xml->endElement(); $xml->endElement(); } $xml->endElement(); } /** * Return true if $value is a struct, false otherwise * * @param mixed $value * * @return bool */ private static function catchStruct($value) { $values = count($value); for ( $i = 0; $i < $values; $i++ ) if ( !array_key_exists($i, $value) ) return true; return false; } /** * Convert timestamp to Iso8601 * * @param int $timestamp * * @return string Iso8601 formatted date */ private static function timestampToIso8601Time($timestamp) { return date("Ymd\TH:i:s", $timestamp); } /** * Recode named entities into numeric ones * * @param mixed $matches * * @return string Iso8601 formatted date */ private static function numericEntities($matches) { static $table = array( 'quot' => '"', 'amp' => '&', 'lt' => '<', 'gt' => '>', 'OElig' => 'Œ', 'oelig' => 'œ', 'Scaron' => 'Š', 'scaron' => 'š', 'Yuml' => 'Ÿ', 'circ' => 'ˆ', 'tilde' => '˜', 'ensp' => ' ', 'emsp' => ' ', 'thinsp' => ' ', 'zwnj' => '‌', 'zwj' => '‍', 'lrm' => '‎', 'rlm' => '‏', 'ndash' => '–', 'mdash' => '—', 'lsquo' => '‘', 'rsquo' => '’', 'sbquo' => '‚', 'ldquo' => '“', 'rdquo' => '”', 'bdquo' => '„', 'dagger' => '†', 'Dagger' => '‡', 'permil' => '‰', 'lsaquo' => '‹', 'rsaquo' => '›', 'euro' => '€', 'fnof' => 'ƒ', 'Alpha' => 'Α', 'Beta' => 'Β', 'Gamma' => 'Γ', 'Delta' => 'Δ', 'Epsilon' => 'Ε', 'Zeta' => 'Ζ', 'Eta' => 'Η', 'Theta' => 'Θ', 'Iota' => 'Ι', 'Kappa' => 'Κ', 'Lambda' => 'Λ', 'Mu' => 'Μ', 'Nu' => 'Ν', 'Xi' => 'Ξ', 'Omicron' => 'Ο', 'Pi' => 'Π', 'Rho' => 'Ρ', 'Sigma' => 'Σ', 'Tau' => 'Τ', 'Upsilon' => 'Υ', 'Phi' => 'Φ', 'Chi' => 'Χ', 'Psi' => 'Ψ', 'Omega' => 'Ω', 'alpha' => 'α', 'beta' => 'β', 'gamma' => 'γ', 'delta' => 'δ', 'epsilon' => 'ε', 'zeta' => 'ζ', 'eta' => 'η', 'theta' => 'θ', 'iota' => 'ι', 'kappa' => 'κ', 'lambda' => 'λ', 'mu' => 'μ', 'nu' => 'ν', 'xi' => 'ξ', 'omicron' => 'ο', 'pi' => 'π', 'rho' => 'ρ', 'sigmaf' => 'ς', 'sigma' => 'σ', 'tau' => 'τ', 'upsilon' => 'υ', 'phi' => 'φ', 'chi' => 'χ', 'psi' => 'ψ', 'omega' => 'ω', 'thetasym' => 'ϑ', 'upsih' => 'ϒ', 'piv' => 'ϖ', 'bull' => '•', 'hellip' => '…', 'prime' => '′', 'Prime' => '″', 'oline' => '‾', 'frasl' => '⁄', 'weierp' => '℘', 'image' => 'ℑ', 'real' => 'ℜ', 'trade' => '™', 'alefsym' => 'ℵ', 'larr' => '←', 'uarr' => '↑', 'rarr' => '→', 'darr' => '↓', 'harr' => '↔', 'crarr' => '↵', 'lArr' => '⇐', 'uArr' => '⇑', 'rArr' => '⇒', 'dArr' => '⇓', 'hArr' => '⇔', 'forall' => '∀', 'part' => '∂', 'exist' => '∃', 'empty' => '∅', 'nabla' => '∇', 'isin' => '∈', 'notin' => '∉', 'ni' => '∋', 'prod' => '∏', 'sum' => '∑', 'minus' => '−', 'lowast' => '∗', 'radic' => '√', 'prop' => '∝', 'infin' => '∞', 'ang' => '∠', 'and' => '∧', 'or' => '∨', 'cap' => '∩', 'cup' => '∪', 'int' => '∫', 'there4' => '∴', 'sim' => '∼', 'cong' => '≅', 'asymp' => '≈', 'ne' => '≠', 'equiv' => '≡', 'le' => '≤', 'ge' => '≥', 'sub' => '⊂', 'sup' => '⊃', 'nsub' => '⊄', 'sube' => '⊆', 'supe' => '⊇', 'oplus' => '⊕', 'otimes' => '⊗', 'perp' => '⊥', 'sdot' => '⋅', 'lceil' => '⌈', 'rceil' => '⌉', 'lfloor' => '⌊', 'rfloor' => '⌋', 'lang' => '〈', 'rang' => '〉', 'loz' => '◊', 'spades' => '♠', 'clubs' => '♣', 'hearts' => '♥', 'diams' => '♦', 'nbsp' => ' ', 'iexcl' => '¡', 'cent' => '¢', 'pound' => '£', 'curren' => '¤', 'yen' => '¥', 'brvbar' => '¦', 'sect' => '§', 'uml' => '¨', 'copy' => '©', 'ordf' => 'ª', 'laquo' => '«', 'not' => '¬', 'shy' => '­', 'reg' => '®', 'macr' => '¯', 'deg' => '°', 'plusmn' => '±', 'sup2' => '²', 'sup3' => '³', 'acute' => '´', 'micro' => 'µ', 'para' => '¶', 'middot' => '·', 'cedil' => '¸', 'sup1' => '¹', 'ordm' => 'º', 'raquo' => '»', 'frac14' => '¼', 'frac12' => '½', 'frac34' => '¾', 'iquest' => '¿', 'Agrave' => 'À', 'Aacute' => 'Á', 'Acirc' => 'Â', 'Atilde' => 'Ã', 'Auml' => 'Ä', 'Aring' => 'Å', 'AElig' => 'Æ', 'Ccedil' => 'Ç', 'Egrave' => 'È', 'Eacute' => 'É', 'Ecirc' => 'Ê', 'Euml' => 'Ë', 'Igrave' => 'Ì', 'Iacute' => 'Í', 'Icirc' => 'Î', 'Iuml' => 'Ï', 'ETH' => 'Ð', 'Ntilde' => 'Ñ', 'Ograve' => 'Ò', 'Oacute' => 'Ó', 'Ocirc' => 'Ô', 'Otilde' => 'Õ', 'Ouml' => 'Ö', 'times' => '×', 'Oslash' => 'Ø', 'Ugrave' => 'Ù', 'Uacute' => 'Ú', 'Ucirc' => 'Û', 'Uuml' => 'Ü', 'Yacute' => 'Ý', 'THORN' => 'Þ', 'szlig' => 'ß', 'agrave' => 'à', 'aacute' => 'á', 'acirc' => 'â', 'atilde' => 'ã', 'auml' => 'ä', 'aring' => 'å', 'aelig' => 'æ', 'ccedil' => 'ç', 'egrave' => 'è', 'eacute' => 'é', 'ecirc' => 'ê', 'euml' => 'ë', 'igrave' => 'ì', 'iacute' => 'í', 'icirc' => 'î', 'iuml' => 'ï', 'eth' => 'ð', 'ntilde' => 'ñ', 'ograve' => 'ò', 'oacute' => 'ó', 'ocirc' => 'ô', 'otilde' => 'õ', 'ouml' => 'ö', 'divide' => '÷', 'oslash' => 'ø', 'ugrave' => 'ù', 'uacute' => 'ú', 'ucirc' => 'û', 'uuml' => 'ü', 'yacute' => 'ý', 'thorn' => 'þ', 'yuml' => 'ÿ' ); // cleanup invalid entities return isset($table[$matches[1]]) ? $table[$matches[1]] : ''; } }
💾 保存文件
← 返回文件管理器