📄 xml_domit_rss_shared.php
字号:
$this->indexNumerically(); if (isset($this->DOMIT_RSS_numericalIndexer[$index])) { return $this->DOMIT_RSS_numericalIndexer[$index]; } else { xml_domit_rss_exception::raiseException(DOMIT_RSS_ELEMENT_NOT_FOUND_ERR, 'Element ' . $index . ' not present.'); } } //getElementAt /** * Populates an integer-based index for elements if one isn't already present. */ function indexNumerically() { if (!isset($this->DOMIT_RSS_numericalIndexer)) { $counter = 0; foreach ($this->DOMIT_RSS_indexer as $key => $value) { $this->DOMIT_RSS_numericalIndexer[$counter] =& $this->DOMIT_RSS_indexer[$key]; $counter++; } } } //indexNumerically /** * Gets the text of the specified element * @param string The name of the requested element * @return string The element text, or an empty string */ function getElementText($elementName) { $elementName = strtolower($elementName); return $this->_getElementText($elementName, $this->DOMIT_RSS_indexer); } //getElementText /** * Gets the text at the specified index * @param int The index of the requested element * @return string The element text, or an empty string */ function getElementTextAt($index) { $this->indexNumerically(); return $this->_getElementText($index, $this->DOMIT_RSS_numericalIndexer); } //getElementTextAt /** * Gets the text at the specified index * @param mixed The index or name of the requested element * @param array The indexing array from which to extract data * @return string The element text, or an empty string */ function _getElementText($index, &$myArray) { if (isset($myArray[$index])) { $element =& $myArray[$index]; $result = ''; if (is_array($element)) { //do nothing; data for domit_rss_channels, domit_rss_items, //and domit_rss_categories should be extracted with their own methods } else { switch (strtolower(get_class($element))) { case 'xml_domit_rss_simpleelement': $result = $element->getElementText(); break; case 'xml_domit_rss_collection': $result = $element->getElementText(); break; case 'domit_element': $total = $element->childCount; for ($i = 0; $i < $total; $i++) { $currNode =& $element->childNodes[$i]; if ($currNode->nodeType == DOMIT_CDATA_SECTION_NODE) { $result .= $currNode->nodeValue; } else { $result .= $currNode->toString(); } } break; } } return $result; } return ''; } //_getElementText} //xml_domit_rss_elementindexer/*** A base class for DOMIT! RSS and DOMIT! RSS Lite documents** @package domit-rss* @author John Heinstein <johnkarl@nbnet.nb.ca>*/class xml_domit_rss_base_document extends xml_domit_rss_elementindexer { /** @var array An array of item elements (only present in some RSS formats) */ var $domit_rss_items = array(); /** @var array An array of existing channel elements */ var $domit_rss_channels = array(); /** @var array An array of existing category elements */ var $domit_rss_categories = array(); /** @var boolean True if caching is enabled */ var $cacheEnabled = true; /** @var Object A reference to the file caching object */ var $cache; /** @var boolean True if PEAR:Cache_Lite is to be used instead of php_text_cache */ var $useCacheLite = false; /** @var boolean True if php_http_client_generic is to be used instead of PHP get_file_contents */ var $doUseHTTPClient = false; /** @var string The name of the current parser - either 'DOMIT_RSS' or 'DOMIT_RSS_LITE' */ var $parser; /** @var object A reference to a http connection or proxy, if one is required */ var $httpConnection = null; /** @var int The timeout value for an http connection */ var $rssTimeout = 0; /** * Constructor * @param string Path to the rss file * @param string Directory in which cache files are to be stored * @param int Expiration time (in seconds) for the cache file * @return mixed Null if an url was not provided, true if an url was provided and parsing was successful, false otherwise */ function xml_domit_rss_base_document ($url = '', $cacheDir = './', $cacheTime = 3600) { $success = null; $this->createDocument(); if ($url != '') { //if rss data is from filesystem if (substr($url, 0, 4) != "http") { $rssText = $this->getTextFromFile($url); $this->parseRSS($rssText); } else { $this->createDefaultCache($cacheDir, $cacheTime); $success = $this->loadRSS($url, $cacheDir, $cacheTime); } } return $success; } //xml_domit_rss_base_document /** * Specifies the default timeout value for connecting to a host * @param int The number of seconds to timeout when attempting to connect to a server */ function setRSSTimeout($rssTimeout) { $this->rssTimeout = $rssTimeout; if (!$this->useCacheLite && !($this->cache == null)) { $this->cache->setTimeout($rssTimeout); } } //setRSSTimeout /** * Specifies the parameters of the http conection used to obtain the xml data * @param string The ip address or domain name of the connection * @param string The path of the connection * @param int The port that the connection is listening on * @param int The timeout value for the connection * @param string The user name, if authentication is required * @param string The password, if authentication is required */ function setConnection($host, $path = '/', $port = 80, $timeout = 0, $user = null, $password = null) { require_once(DOMIT_RSS_INCLUDE_PATH . 'php_http_client_generic.php'); $this->httpConnection = new php_http_client_generic($host, $path, $port, $timeout, $user, $password); } //setConnection /** * Specifies basic authentication for an http connection * @param string The user name * @param string The password */ function setAuthorization($user, $password) { $this->httpConnection->setAuthorization($user, $password); } //setAuthorization /** * Specifies that a proxy is to be used to obtain the xml data * @param string The ip address or domain name of the proxy * @param string The path to the proxy * @param int The port that the proxy is listening on * @param int The timeout value for the connection * @param string The user name, if authentication is required * @param string The password, if authentication is required */ function setProxyConnection($host, $path = '/', $port = 80, $timeout = 0, $user = null, $password = null) { require_once(DOMIT_RSS_INCLUDE_PATH . 'php_http_proxy.php'); $this->httpConnection = new php_http_proxy($host, $path, $port, $timeout, $user, $password); } //setProxyConnection /** * Specifies a user name and password for the proxy * @param string The user name * @param string The password */ function setProxyAuthorization($user, $password) { $this->httpConnection->setProxyAuthorization($user, $password); } //setProxyAuthorization /** * Specifies whether an HTTP client should be used to establish a connection * @param boolean True if an HTTP client is to be used to establish the connection */ function useHTTPClient($truthVal) { $this->doUseHTTPClient = $truthVal; } //useHTTPClient /** * Returns the name of the parser *@return string Either 'DOMIT_RSS' or 'DOMIT_RSS_LITE' */ function parsedBy() { return $this->parser; } //parsedBy /** * Creates an empty DOMIT! document to contain the RSS nodes */ function createDocument() { require_once(DOMIT_RSS_INCLUDE_PATH . 'xml_domit_include.php'); $this->node = new DOMIT_Document(); $this->node->resolveErrors(true); } //createDocument /** * Substitutes PEAR::Cache_Lite for the default php_text_cache * @param boolean True if Cache Lite is to be used * @param string Absolute or relative path to the Cache Lite library * @param string Directory for cache files * @param int Expiration time for a cache file */ function useCacheLite($doUseCacheLite, $pathToLibrary = './Lite.php', $cacheDir = './', $cacheTime = 3600) { $this->useCacheLite = $doUseCacheLite; if ($doUseCacheLite) { if (!file_exists($pathToLibrary)) { $this->useCacheLite(false); } else { require_once($pathToLibrary); $cacheOptions = array('cacheDir' => $cacheDir, 'lifeTime' => $cacheTime); $this->cache = new Cache_Lite($cacheOptions); } } else { $this->createDefaultCache($cacheDir, $cacheTime); } } //useCacheLite /** * Instantiates a default cache (php_text_cache) * @param string Directory for cache files * @param int Expiration time for a cache file */ function createDefaultCache($cacheDir = './', $cacheTime = 3600) { require_once(DOMIT_RSS_INCLUDE_PATH . 'php_text_cache.php'); $this->cache = new php_text_cache($cacheDir, $cacheTime, $this->rssTimeout); } //initDefaultCache /** * Disables caching mechanism */ function disableCache() { $this->cacheEnabled = false; } //initDefaultCache /** * Loads and parses the RSS at the specified url * @param string The url of the RSS feed * @return boolean True if parsing is successful */ function loadRSS($url) { if (substr($url, 0, 4) != "http") { $rssText = $this->getTextFromFile($url); return $this->parseRSS($rssText); } else { if ($this->cacheEnabled && !isset($this->cache)) { $this->createDefaultCache(); $this->cache->httpConnection =& $this->httpConnection; } $success = $this->loadRSSData($url); if ($success) { $this->_init(); } return $success; } } //loadRSS /** * Parses the RSS text provided * @param string The RSS text * @return boolean True if parsing is successful */ function parseRSS($rssText) { if ($this->cacheEnabled && !isset($this->cache)) $this->createDefaultCache(); $success = $this->parseRSSData($rssText); if ($success) { $this->_init(); } return $success; } //parseRSS /** * Retrieves the RSS data from the url/cache file and parses * @param string The url for the RSS data * @return boolean True if parsing is successful */ function loadRSSData($url) { $rssText = $this->getDataFromCache($url); return $this->parseRSSData($rssText); } //loadRSSData /** * Retrieves the RSS data from the url/cache file * @param string The url for the RSS data * @return string The RSS data */ function getDataFromCache($url) { if ($this->cacheEnabled) { if ($this->useCacheLite) { if ($rssText = $this->cache->get($url)) { return $rssText; } else { $rssText = $this->getTextFromFile($url);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -