📄 common.php
字号:
/** * Returns information about a package file. Expects the contents * of a package xml file as input. * * @param string $data name of package xml file * * @return array array with package information * * @access public * */ function infoFromString($data) { require_once('PEAR/Dependency.php'); if (PEAR_Dependency::checkExtension($error, 'xml')) { return $this->raiseError($error); } $xp = @xml_parser_create(); if (!$xp) { return $this->raiseError('Unable to create XML parser'); } xml_set_object($xp, $this); xml_set_element_handler($xp, '_element_start', '_element_end'); xml_set_character_data_handler($xp, '_pkginfo_cdata'); xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, false); $this->element_stack = array(); $this->pkginfo = array('provides' => array()); $this->current_element = false; unset($this->dir_install); $this->pkginfo['filelist'] = array(); $this->filelist =& $this->pkginfo['filelist']; $this->dir_names = array(); $this->in_changelog = false; $this->d_i = 0; $this->cdata = ''; $this->_validPackageFile = false; if (!xml_parse($xp, $data, 1)) { $code = xml_get_error_code($xp); $msg = sprintf("XML error: %s at line %d", xml_error_string($code), xml_get_current_line_number($xp)); xml_parser_free($xp); return $this->raiseError($msg, $code); } xml_parser_free($xp); if (!$this->_validPackageFile) { return $this->raiseError('Invalid Package File, no <package> tag'); } foreach ($this->pkginfo as $k => $v) { if (!is_array($v)) { $this->pkginfo[$k] = trim($v); } } return $this->pkginfo; } // }}} // {{{ infoFromAny() /** * Returns package information from different sources * * This method is able to extract information about a package * from a .tgz archive or from a XML package definition file. * * @access public * @param string Filename of the source ('package.xml', '<package>.tgz') * @return string */ function infoFromAny($info) { if (is_string($info) && file_exists($info)) { $tmp = substr($info, -4); if ($tmp == '.xml') { $info = $this->infoFromDescriptionFile($info); } elseif ($tmp == '.tar' || $tmp == '.tgz') { $info = $this->infoFromTgzFile($info); } else { $fp = fopen($info, "r"); $test = fread($fp, 5); fclose($fp); if ($test == "<?xml") { $info = $this->infoFromDescriptionFile($info); } else { $info = $this->infoFromTgzFile($info); } } if (PEAR::isError($info)) { return $this->raiseError($info); } } return $info; } // }}} // {{{ xmlFromInfo() /** * Return an XML document based on the package info (as returned * by the PEAR_Common::infoFrom* methods). * * @param array $pkginfo package info * * @return string XML data * * @access public */ function xmlFromInfo($pkginfo) { static $maint_map = array( "handle" => "user", "name" => "name", "email" => "email", "role" => "role", ); $ret = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"; $ret .= "<!DOCTYPE package SYSTEM \"http://pear.php.net/dtd/package-1.0\">\n"; $ret .= "<package version=\"1.0\"> <name>$pkginfo[package]</name> <summary>".htmlspecialchars($pkginfo['summary'])."</summary> <description>".htmlspecialchars($pkginfo['description'])."</description> <maintainers>"; foreach ($pkginfo['maintainers'] as $maint) { $ret .= " <maintainer>\n"; foreach ($maint_map as $idx => $elm) { $ret .= " <$elm>"; $ret .= htmlspecialchars($maint[$idx]); $ret .= "</$elm>\n"; } $ret .= " </maintainer>\n"; } $ret .= " </maintainers>\n"; $ret .= $this->_makeReleaseXml($pkginfo); if (@sizeof($pkginfo['changelog']) > 0) { $ret .= " <changelog>\n"; foreach ($pkginfo['changelog'] as $oldrelease) { $ret .= $this->_makeReleaseXml($oldrelease, true); } $ret .= " </changelog>\n"; } $ret .= "</package>\n"; return $ret; } // }}} // {{{ _makeReleaseXml() /** * Generate part of an XML description with release information. * * @param array $pkginfo array with release information * @param bool $changelog whether the result will be in a changelog element * * @return string XML data * * @access private */ function _makeReleaseXml($pkginfo, $changelog = false) { // XXX QUOTE ENTITIES IN PCDATA, OR EMBED IN CDATA BLOCKS!! $indent = $changelog ? " " : ""; $ret = "$indent <release>\n"; if (!empty($pkginfo['version'])) { $ret .= "$indent <version>$pkginfo[version]</version>\n"; } if (!empty($pkginfo['release_date'])) { $ret .= "$indent <date>$pkginfo[release_date]</date>\n"; } if (!empty($pkginfo['release_license'])) { $ret .= "$indent <license>$pkginfo[release_license]</license>\n"; } if (!empty($pkginfo['release_state'])) { $ret .= "$indent <state>$pkginfo[release_state]</state>\n"; } if (!empty($pkginfo['release_notes'])) { $ret .= "$indent <notes>".htmlspecialchars($pkginfo['release_notes'])."</notes>\n"; } if (!empty($pkginfo['release_warnings'])) { $ret .= "$indent <warnings>".htmlspecialchars($pkginfo['release_warnings'])."</warnings>\n"; } if (isset($pkginfo['release_deps']) && sizeof($pkginfo['release_deps']) > 0) { $ret .= "$indent <deps>\n"; foreach ($pkginfo['release_deps'] as $dep) { $ret .= "$indent <dep type=\"$dep[type]\" rel=\"$dep[rel]\""; if (isset($dep['version'])) { $ret .= " version=\"$dep[version]\""; } if (isset($dep['optional'])) { $ret .= " optional=\"$dep[optional]\""; } if (isset($dep['name'])) { $ret .= ">$dep[name]</dep>\n"; } else { $ret .= "/>\n"; } } $ret .= "$indent </deps>\n"; } if (isset($pkginfo['configure_options'])) { $ret .= "$indent <configureoptions>\n"; foreach ($pkginfo['configure_options'] as $c) { $ret .= "$indent <configureoption name=\"". htmlspecialchars($c['name']) . "\""; if (isset($c['default'])) { $ret .= " default=\"" . htmlspecialchars($c['default']) . "\""; } $ret .= " prompt=\"" . htmlspecialchars($c['prompt']) . "\""; $ret .= "/>\n"; } $ret .= "$indent </configureoptions>\n"; } if (isset($pkginfo['provides'])) { foreach ($pkginfo['provides'] as $key => $what) { $ret .= "$indent <provides type=\"$what[type]\" "; $ret .= "name=\"$what[name]\" "; if (isset($what['extends'])) { $ret .= "extends=\"$what[extends]\" "; } $ret .= "/>\n"; } } if (isset($pkginfo['filelist'])) { $ret .= "$indent <filelist>\n"; foreach ($pkginfo['filelist'] as $file => $fa) { @$ret .= "$indent <file role=\"$fa[role]\""; if (isset($fa['baseinstalldir'])) { $ret .= ' baseinstalldir="' . htmlspecialchars($fa['baseinstalldir']) . '"'; } if (isset($fa['md5sum'])) { $ret .= " md5sum=\"$fa[md5sum]\""; } if (isset($fa['platform'])) { $ret .= " platform=\"$fa[platform]\""; } if (!empty($fa['install-as'])) { $ret .= ' install-as="' . htmlspecialchars($fa['install-as']) . '"'; } $ret .= ' name="' . htmlspecialchars($file) . '"'; if (empty($fa['replacements'])) { $ret .= "/>\n"; } else { $ret .= ">\n"; foreach ($fa['replacements'] as $r) { $ret .= "$indent <replace"; foreach ($r as $k => $v) { $ret .= " $k=\"" . htmlspecialchars($v) .'"'; } $ret .= "/>\n"; } @$ret .= "$indent </file>\n"; } } $ret .= "$indent </filelist>\n"; } $ret .= "$indent </release>\n"; return $ret; } // }}} // {{{ validatePackageInfo() /** * Validate XML package definition file. * * @param string $info Filename of the package archive or of the * package definition file * @param array $errors Array that will contain the errors * @param array $warnings Array that will contain the warnings * @param string $dir_prefix (optional) directory where source files * may be found, or empty if they are not available * @access public * @return boolean */ function validatePackageInfo($info, &$errors, &$warnings, $dir_prefix = '') { if (PEAR::isError($info = $this->infoFromAny($info))) { return $this->raiseError($info); } if (!is_array($info)) { return false; } $errors = array(); $warnings = array(); if (!isset($info['package'])) { $errors[] = 'missing package name'; } elseif (!$this->validPackageName($info['package'])) { $errors[] = 'invalid package name'; } $this->_packageName = $pn = $info['package']; if (empty($info['summary'])) { $errors[] = 'missing summary'; } elseif (strpos(trim($info['summary']), "\n") !== false) { $warnings[] = 'summary should be on a single line'; } if (empty($info['description'])) { $errors[] = 'missing description'; } if (empty($info['release_license'])) { $errors[] = 'missing license'; } if (!isset($info['version'])) { $errors[] = 'missing version'; } elseif (!$this->validPackageVersion($info['version'])) { $errors[] = 'invalid package version'; } if (empty($info['release_state'])) { $errors[] = 'missing release state'; } elseif (!in_array($info['release_state'], PEAR_Common::getReleaseStates())) { $errors[] = "invalid release state `$info[release_state]', should be one of: " . implode(' ', PEAR_Common::getReleaseStates()); } if (empty($info['release_date'])) { $errors[] = 'missing release date'; } elseif (!preg_match('/^\d{4}-\d\d-\d\d$/', $info['release_date'])) { $errors[] = "invalid release date `$info[release_date]', format is YYYY-MM-DD"; } if (empty($info['release_notes'])) { $errors[] = "missing release notes"; } if (empty($info['maintainers'])) { $errors[] = 'no maintainer(s)'; } else { $i = 1; foreach ($info['maintainers'] as $m) { if (empty($m['handle'])) { $errors[] = "maintainer $i: missing handle"; } if (empty($m['role'])) { $errors[] = "maintainer $i: missing role"; } elseif (!in_array($m['role'], PEAR_Common::getUserRoles())) { $errors[] = "maintainer $i: invalid role `$m[role]', should be one of: " . implode(' ', PEAR_Common::getUserRoles()); } if (empty($m['name'])) { $errors[] = "maintainer $i: missing name"; } if (empty($m['email'])) { $errors[] = "maintainer $i: missing email"; } $i++; } } if (!empty($info['deps'])) { $i = 1; foreach ($info['deps'] as $d) { if (empty($d['type'])) { $errors[] = "dependency $i: missing type"; } elseif (!in_array($d['type'], PEAR_Common::getDependencyTypes())) { $errors[] = "dependency $i: invalid type, should be one of: " . implode(' ', PEAR_Common::getDependencyTypes()); } if (empty($d['rel'])) { $errors[] = "dependency $i: missing relation"; } elseif (!in_array($d['rel'], PEAR_Common::getDependencyRelations())) { $errors[] = "dependency $i: invalid relation, should be one of: " . implode(' ', PEAR_Common::getDependencyRelations()); } if (!empty($d['optional'])) { if (!in_array($d['optional'], array('yes', 'no'))) { $errors[] = "dependency $i: invalid relation optional attribute, should be one of: yes no"; } } if ($d['rel'] != 'has' && empty($d['version'])) { $warnings[] = "dependency $i: missing version"; } elseif ($d['rel'] == 'has' && !empty($d['version'])) { $warnings[] = "dependency $i: version ignored for `has' dependencies"; } if ($d['type'] == 'php' && !empty($d['name'])) { $warnings[] = "dependency $i: name ignored for php type dependencies"; } elseif ($d['type'] != 'php' && empty($d['name'])) { $errors[] = "dependency $i: missing name"; } $i++; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -