📄 channelfile.php
字号:
<?php/** * PEAR_ChannelFile, the channel handling class * * PHP versions 4 and 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category pear * @package PEAR * @author Greg Beaver <cellog@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: ChannelFile.php,v 1.1.2.2 2006/05/22 10:19:33 cellog Exp $ * @link http://pear.php.net/package/PEAR * @since File available since Release 1.4.0a1 *//** * Needed for error handling */require_once 'PEAR/ErrorStack.php';require_once 'PEAR/XMLParser.php';require_once 'PEAR/Common.php';/** * Error code if the channel.xml <channel> tag does not contain a valid version */define('PEAR_CHANNELFILE_ERROR_NO_VERSION', 1);/** * Error code if the channel.xml <channel> tag version is not supported (version 1.0 is the only supported version, * currently */define('PEAR_CHANNELFILE_ERROR_INVALID_VERSION', 2);/** * Error code if parsing is attempted with no xml extension */define('PEAR_CHANNELFILE_ERROR_NO_XML_EXT', 3);/** * Error code if creating the xml parser resource fails */define('PEAR_CHANNELFILE_ERROR_CANT_MAKE_PARSER', 4);/** * Error code used for all sax xml parsing errors */define('PEAR_CHANNELFILE_ERROR_PARSER_ERROR', 5);/**#@+ * Validation errors *//** * Error code when channel name is missing */define('PEAR_CHANNELFILE_ERROR_NO_NAME', 6);/** * Error code when channel name is invalid */define('PEAR_CHANNELFILE_ERROR_INVALID_NAME', 7);/** * Error code when channel summary is missing */define('PEAR_CHANNELFILE_ERROR_NO_SUMMARY', 8);/** * Error code when channel summary is multi-line */define('PEAR_CHANNELFILE_ERROR_MULTILINE_SUMMARY', 9);/** * Error code when channel server is missing for xmlrpc or soap protocol */define('PEAR_CHANNELFILE_ERROR_NO_HOST', 10);/** * Error code when channel server is invalid for xmlrpc or soap protocol */define('PEAR_CHANNELFILE_ERROR_INVALID_HOST', 11);/** * Error code when a mirror name is invalid */define('PEAR_CHANNELFILE_ERROR_INVALID_MIRROR', 21);/** * Error code when a mirror type is invalid */define('PEAR_CHANNELFILE_ERROR_INVALID_MIRRORTYPE', 22);/** * Error code when an attempt is made to generate xml, but the parsed content is invalid */define('PEAR_CHANNELFILE_ERROR_INVALID', 23);/** * Error code when an empty package name validate regex is passed in */define('PEAR_CHANNELFILE_ERROR_EMPTY_REGEX', 24);/** * Error code when a <function> tag has no version */define('PEAR_CHANNELFILE_ERROR_NO_FUNCTIONVERSION', 25);/** * Error code when a <function> tag has no name */define('PEAR_CHANNELFILE_ERROR_NO_FUNCTIONNAME', 26);/** * Error code when a <validatepackage> tag has no name */define('PEAR_CHANNELFILE_ERROR_NOVALIDATE_NAME', 27);/** * Error code when a <validatepackage> tag has no version attribute */define('PEAR_CHANNELFILE_ERROR_NOVALIDATE_VERSION', 28);/** * Error code when a mirror does not exist but is called for in one of the set* * methods. */define('PEAR_CHANNELFILE_ERROR_MIRROR_NOT_FOUND', 32);/** * Error code when a server port is not numeric */define('PEAR_CHANNELFILE_ERROR_INVALID_PORT', 33);/** * Error code when <static> contains no version attribute */define('PEAR_CHANNELFILE_ERROR_NO_STATICVERSION', 34);/** * Error code when <baseurl> contains no type attribute in a <rest> protocol definition */define('PEAR_CHANNELFILE_ERROR_NOBASEURLTYPE', 35);/** * Error code when a mirror is defined and the channel.xml represents the __uri pseudo-channel */define('PEAR_CHANNELFILE_URI_CANT_MIRROR', 36);/** * Error code when ssl attribute is present and is not "yes" */define('PEAR_CHANNELFILE_ERROR_INVALID_SSL', 37);/**#@-*//** * Mirror types allowed. Currently only internet servers are recognized. */$GLOBALS['_PEAR_CHANNELS_MIRROR_TYPES'] = array('server');/** * The Channel handling class * * @category pear * @package PEAR * @author Greg Beaver <cellog@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version Release: @package_version@ * @link http://pear.php.net/package/PEAR * @since Class available since Release 1.4.0a1 */class PEAR_ChannelFile { /** * @access private * @var PEAR_ErrorStack * @access private */ var $_stack; /** * Supported channel.xml versions, for parsing * @var array * @access private */ var $_supportedVersions = array('1.0'); /** * Parsed channel information * @var array * @access private */ var $_channelInfo; /** * index into the subchannels array, used for parsing xml * @var int * @access private */ var $_subchannelIndex; /** * index into the mirrors array, used for parsing xml * @var int * @access private */ var $_mirrorIndex; /** * Flag used to determine the validity of parsed content * @var boolean * @access private */ var $_isValid = false; function PEAR_ChannelFile() { $this->_stack = &new PEAR_ErrorStack('PEAR_ChannelFile'); $this->_stack->setErrorMessageTemplate($this->_getErrorMessage()); $this->_isValid = false; } /** * @return array * @access protected */ function _getErrorMessage() { return array( PEAR_CHANNELFILE_ERROR_INVALID_VERSION => 'While parsing channel.xml, an invalid version number "%version% was passed in, expecting one of %versions%', PEAR_CHANNELFILE_ERROR_NO_VERSION => 'No version number found in <channel> tag', PEAR_CHANNELFILE_ERROR_NO_XML_EXT => '%error%', PEAR_CHANNELFILE_ERROR_CANT_MAKE_PARSER => 'Unable to create XML parser', PEAR_CHANNELFILE_ERROR_PARSER_ERROR => '%error%', PEAR_CHANNELFILE_ERROR_NO_NAME => 'Missing channel name', PEAR_CHANNELFILE_ERROR_INVALID_NAME => 'Invalid channel %tag% "%name%"', PEAR_CHANNELFILE_ERROR_NO_SUMMARY => 'Missing channel summary', PEAR_CHANNELFILE_ERROR_MULTILINE_SUMMARY => 'Channel summary should be on one line, but is multi-line', PEAR_CHANNELFILE_ERROR_NO_HOST => 'Missing channel server for %type% server', PEAR_CHANNELFILE_ERROR_INVALID_HOST => 'Server name "%server%" is invalid for %type% server', PEAR_CHANNELFILE_ERROR_INVALID_MIRROR => 'Invalid mirror name "%name%", mirror type %type%', PEAR_CHANNELFILE_ERROR_INVALID_MIRRORTYPE => 'Invalid mirror type "%type%"', PEAR_CHANNELFILE_ERROR_INVALID => 'Cannot generate xml, contents are invalid', PEAR_CHANNELFILE_ERROR_EMPTY_REGEX => 'packagenameregex cannot be empty', PEAR_CHANNELFILE_ERROR_NO_FUNCTIONVERSION => '%parent% %protocol% function has no version', PEAR_CHANNELFILE_ERROR_NO_FUNCTIONNAME => '%parent% %protocol% function has no name', PEAR_CHANNELFILE_ERROR_NOBASEURLTYPE => '%parent% rest baseurl has no type', PEAR_CHANNELFILE_ERROR_NOVALIDATE_NAME => 'Validation package has no name in <validatepackage> tag', PEAR_CHANNELFILE_ERROR_NOVALIDATE_VERSION => 'Validation package "%package%" has no version', PEAR_CHANNELFILE_ERROR_MIRROR_NOT_FOUND => 'Mirror "%mirror%" does not exist', PEAR_CHANNELFILE_ERROR_INVALID_PORT => 'Port "%port%" must be numeric', PEAR_CHANNELFILE_ERROR_NO_STATICVERSION => '<static> tag must contain version attribute', PEAR_CHANNELFILE_URI_CANT_MIRROR => 'The __uri pseudo-channel cannot have mirrors', PEAR_CHANNELFILE_ERROR_INVALID_SSL => '%server% has invalid ssl attribute "%ssl%" can only be yes or not present', ); } /** * @param string contents of package.xml file * @return bool success of parsing */ function fromXmlString($data) { if (preg_match('/<channel\s+version="([0-9]+\.[0-9]+)"/', $data, $channelversion)) { if (!in_array($channelversion[1], $this->_supportedVersions)) { $this->_stack->push(PEAR_CHANNELFILE_ERROR_INVALID_VERSION, 'error', array('version' => $channelversion[1])); return false; } $parser = new PEAR_XMLParser; $result = $parser->parse($data); if ($result !== true) { if ($result->getCode() == 1) { $this->_stack->push(PEAR_CHANNELFILE_ERROR_NO_XML_EXT, 'error', array('error' => $error)); } else { $this->_stack->push(PEAR_CHANNELFILE_ERROR_CANT_MAKE_PARSER, 'error'); } return false; } $this->_channelInfo = $parser->getData(); return true; } else { $this->_stack->push(PEAR_CHANNELFILE_ERROR_NO_VERSION, 'error', array('xml' => $data)); return false; } } /** * @return array */ function toArray() { if (!$this->_isValid && !$this->validate()) { return false; } return $this->_channelInfo; } /** * @param array * @static * @return PEAR_ChannelFile|false false if invalid */ function &fromArray($data, $compatibility = false, $stackClass = 'PEAR_ErrorStack') { $a = new PEAR_ChannelFile($compatibility, $stackClass); $a->_fromArray($data); if (!$a->validate()) { $a = false; return $a; } return $a; } /** * Unlike {@link fromArray()} this does not do any validation * @param array * @static * @return PEAR_ChannelFile */ function &fromArrayWithErrors($data, $compatibility = false, $stackClass = 'PEAR_ErrorStack') { $a = new PEAR_ChannelFile($compatibility, $stackClass); $a->_fromArray($data); return $a; } /** * @param array * @access private */ function _fromArray($data) { $this->_channelInfo = $data; } /** * Wrapper to {@link PEAR_ErrorStack::getErrors()} * @param boolean determines whether to purge the error stack after retrieving * @return array */ function getErrors($purge = false) { return $this->_stack->getErrors($purge); } /** * Unindent given string (?) * * @param string $str The string that has to be unindented. * @return string * @access private */ function _unIndent($str) { // remove leading newlines $str = preg_replace('/^[\r\n]+/', '', $str); // find whitespace at the beginning of the first line $indent_len = strspn($str, " \t"); $indent = substr($str, 0, $indent_len); $data = ''; // remove the same amount of whitespace from following lines foreach (explode("\n", $str) as $line) { if (substr($line, 0, $indent_len) == $indent) { $data .= substr($line, $indent_len) . "\n"; } } return $data; } /** * Parse a channel.xml file. Expects the name of * a channel xml file as input. * * @param string $descfile name of channel xml file * @return bool success of parsing */ function fromXmlFile($descfile) { if (!@is_file($descfile) || !is_readable($descfile) || (!$fp = @fopen($descfile, 'r'))) { require_once 'PEAR.php'; return PEAR::raiseError("Unable to open $descfile"); } // read the whole thing so we only get one cdata callback // for each block of cdata if (function_exists('file_get_contents')) { fclose($fp); $data = file_get_contents($descfile);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -