| [ Index ] |
|
Code source de Joomla 1.0.13 |
1 <?php 2 /** 3 * @package domit-rss 4 * @version 0.51 5 * @copyright (C) 2004 John Heinstein. All rights reserved 6 * @license http://www.gnu.org/copyleft/lesser.html LGPL License 7 * @author John Heinstein <johnkarl@nbnet.nb.ca> 8 * @link http://www.engageinteractive.com/domitrss/ DOMIT! RSS Home Page 9 * DOMIT! RSS is Free Software 10 **/ 11 12 /** channel constant */ 13 define('DOMIT_RSS_ELEMENT_CHANNEL', 'channel'); 14 /** item constant */ 15 define('DOMIT_RSS_ELEMENT_ITEM', 'item'); 16 /** title constant */ 17 define('DOMIT_RSS_ELEMENT_TITLE', 'title'); 18 /** link constant */ 19 define('DOMIT_RSS_ELEMENT_LINK', 'link'); 20 /** description constant */ 21 define('DOMIT_RSS_ELEMENT_DESCRIPTION', 'description'); 22 23 /** version constant */ 24 define('DOMIT_RSS_ATTR_VERSION', 'version'); 25 26 /** name of array containing list of existing RSS items */ 27 define('DOMIT_RSS_ARRAY_ITEMS', 'item'); //formerly named 'domit_rss_items' 28 /** name of array containing list of existing RSS channels */ 29 define('DOMIT_RSS_ARRAY_CHANNELS', 'channel'); //formerly named 'domit_rss_channels' 30 /** name of array containing list of existing RSS categories */ 31 define('DOMIT_RSS_ARRAY_CATEGORIES', 'category'); //formerly named 'domit_rss_categories' 32 33 /** DOMIT RSS error, attempt to call an abstract method */ 34 define('DOMIT_RSS_ABSTRACT_METHOD_INVOCATION_ERR', 101); 35 /** DOMIT RSS error, specified element not present */ 36 define('DOMIT_RSS_ELEMENT_NOT_FOUND_ERR', 102); 37 /** DOMIT RSS error, specified attribute not present */ 38 define('DOMIT_RSS_ATTR_NOT_FOUND_ERR', 103); 39 /** DOMIT RSS error, parsing failed */ 40 define('DOMIT_RSS_PARSING_ERR', 104); 41 42 //DOMIT! RSS Error Modes 43 /** continue on error */ 44 define('DOMIT_RSS_ONERROR_CONTINUE', 1); 45 /** die on error */ 46 define('DOMIT_RSS_ONERROR_DIE', 2); 47 /** die on error */ 48 define('DOMIT_RSS_ONERROR_RETURN', 3); 49 50 /** 51 * The base class of all DOMIT! RSS objects 52 * 53 * @package domit-rss 54 * @author John Heinstein <johnkarl@nbnet.nb.ca> 55 */ 56 class xml_domit_rss_base { 57 /** @var Object The underlying DOMIT! node of the element */ 58 var $node = null; 59 /** @var array A list of valid RSS defined child elements */ 60 var $rssDefinedElements = array(); 61 62 /** 63 * Retrieves the underlying DOMIT node 64 * @return Object The underlying DOMIT node 65 */ 66 function getNode() { 67 return $this->node; 68 } //getNode 69 70 /** 71 * Retrieves the text of the named attribute, checking first if the attribute exists 72 * @param string The attribute name 73 * @return string The attribute value, or an empty string 74 */ 75 function getAttribute($attr) { 76 if ($this->node->hasAttribute($attr)) { 77 return $this->node->getAttribute($attr); 78 } 79 80 return ""; 81 } //getAttribute 82 83 /** 84 * Checks whether the named attribute exists 85 * @param string The attribute name 86 * @return boolean True if the attribute exists 87 */ 88 function hasAttribute($attr) { 89 return (($this->node->nodeType == DOMIT_ELEMENT_NODE) && $this->node->hasAttribute($attr)); 90 } //hasAttribute 91 92 /** 93 * Tests whether the named element is predefined by the RSS spec 94 * @param string The element name 95 * @return boolean True if the element is predefined by the RSS spec 96 */ 97 function isRSSDefined($elementName) { 98 $isDefined = false; 99 100 foreach ($this->rssDefinedElements as $key => $value) { 101 if ($elementName == $value) { 102 $isDefined = true; 103 break; 104 } 105 } 106 107 return $isDefined; 108 } //isRSSDefined 109 110 /** 111 * Tests whether the named element has a single child text node 112 * @param string The element name 113 * @return boolean True if the named element has a single child text node 114 */ 115 function isSimpleRSSElement($elementName) { 116 $elementName = strtolower($elementName); 117 118 if (isset($this->DOMIT_RSS_indexer[$elementName])) { 119 return (get_class($this->getElement($elementName)) == 'xml_domit_rss_simpleelement'); 120 } 121 else { 122 return false; 123 } 124 } //isSimpleRSSElement 125 126 /** 127 * Generates a string representation of the node and its children 128 * @param boolean True if HTML readable output is desired 129 * @param boolean True if illegal xml characters in text nodes and attributes should be converted to entities 130 * @return string The string representation 131 */ 132 function get($htmlSafe = false, $subEntities = false) { 133 return $this->node->toString($htmlSafe, $subEntities); 134 } //toString 135 136 /** 137 * Generates a normalized (formatted for readability) representation of the node and its children 138 * @param boolean True if HTML readable output is desired 139 * @param boolean True if illegal xml characters in text nodes and attributes should be converted to entities 140 * @return string The formatted string representation 141 */ 142 function toNormalizedString($htmlSafe = false, $subEntities = false) { 143 return $this->node->toNormalizedString($htmlSafe, $subEntities); 144 } //toNormalizedString 145 } //xml_domit_rss_base 146 147 148 /** 149 * Represents a collection of custom RSS elements, e.g. a set of dc:creator entries 150 * 151 * @package domit-rss 152 * @subpackage domit-rss-main 153 * @author John Heinstein <johnkarl@nbnet.nb.ca> 154 */ 155 class xml_domit_rss_collection extends xml_domit_rss_elementindexer { 156 /** @var array An array holding the collection of custom elements */ 157 var $elements = array(); 158 /** @var int The number of custom elements in the collection */ 159 var $elementCount = 0; 160 161 /** 162 * Adds a custom RSS element (DOM node) to the collection 163 * @param Object A DOM node representing a custom RSS element 164 */ 165 function addElement(&$node) { 166 $this->elements[] =& $node; 167 $this->elementCount++; 168 } //addElement 169 170 /** 171 * Retrieves the element at the specified index 172 * @param int The index of the requested custom RSS element 173 * @return Object The DOMIT node representing the requested element 174 */ 175 function &getElementAt($index) { 176 return $this->elements[$index]; 177 } //getElementAt 178 179 /** 180 * Retrieves the element at the specified index (alias for getElementAt) 181 * @param int The index of the requested custom RSS element 182 * @return Object The DOMIT node representing the requested element 183 */ 184 function &getElement($index) { 185 return $this->getElementAt($index); 186 } //getElement 187 188 /** 189 * Returns the number of elements in the collection 190 * @return int The number of members in the collection 191 */ 192 function getElementCount() { 193 return $this->elementCount; 194 } //getElementCount 195 196 /** 197 * Gets a text representation of the collection (applies the toString method to each member and concatenates) 198 * @return string The element text 199 */ 200 function getElementText() { 201 $total = $this->getElementCount(); 202 $result = ''; 203 204 for ($i = 0; $i < $total; $i++) { 205 $result .= $currElement->toString(); 206 } 207 208 return $result; 209 } //getElementText 210 } //xml_domit_rss_collection 211 212 213 /** 214 * Provides indexing functionality to RSS classes 215 * 216 * @package domit-rss 217 * @author John Heinstein <johnkarl@nbnet.nb.ca> 218 */ 219 class xml_domit_rss_elementindexer extends xml_domit_rss_base { 220 /** @var Array Name based index to RSS elements */ 221 var $DOMIT_RSS_indexer = array(); 222 /** @var Array Numerical index to RSS elements; for optimization purposes, only set if requested by getElementAt */ 223 var $DOMIT_RSS_numericalIndexer; 224 225 /** 226 * Performs generic initialization of elements 227 */ 228 function _init(){ 229 $total = $this->node->childCount; 230 231 for($i = 0; $i < $total; $i++) { 232 $currNode =& $this->node->childNodes[$i]; 233 //$this->DOMIT_RSS_indexer[$currNode->nodeName] =& $currNode; 234 $this->addIndexedElement($currNode); 235 } 236 } //_init 237 238 /** 239 * Adds a custom element (one not defined by the RSS specs, e..g., dc:creator) to the indexer 240 * @param Object A DOMIT! node representing the custom element 241 */ 242 function addIndexedElement(&$node) { 243 $tagName = strtolower($node->nodeName); 244 245 if (isset($this->DOMIT_RSS_indexer[$tagName])) { 246 if (strtolower(get_class($this->DOMIT_RSS_indexer[$tagName])) == 'domit_element') { 247 $collection = new xml_domit_rss_collection(); 248 $collection->addElement($this->DOMIT_RSS_indexer[$tagName]); 249 $collection->addElement($node); 250 $this->DOMIT_RSS_indexer[$tagName] =& $collection; 251 } 252 else { 253 //Don't think I need this case??? 254 //$this->DOMIT_RSS_indexer[$tagName]->addElement($node); 255 } 256 } 257 else { 258 $this->DOMIT_RSS_indexer[$tagName] =& $node; 259 } 260 } //addIndexedElement 261 262 /** 263 * Indicates whether the requested element is actually a collection of elements of the same type 264 * @param string The name of the requested element 265 * @return boolean True if a collection of elements exists 266 */ 267 function isCollection($elementName) { 268 $elementName = strtolower($elementName); 269 270 if (isset($this->DOMIT_RSS_indexer[$elementName])) { 271 return (get_class($this->DOMIT_RSS_indexer[$elementName]) == 'xml_domit_rss_collection'); 272 } 273 else { 274 return false; 275 } 276 } //isCollection 277 278 /** 279 * Indicates whether the requested element is a DOMIT! node 280 * @param string The name of the requested element 281 * @return boolean True if the requested element is a DOMIT! node 282 */ 283 function isNode($elementName) { 284 $elementName = strtolower($elementName); 285 286 if (isset($this->DOMIT_RSS_indexer[$elementName])) { 287 return (strtolower(get_class($this->DOMIT_RSS_indexer[$elementName])) == 'domit_element'); 288 } 289 else { 290 return false; 291 } 292 } //isNode 293 294 /** 295 * Indicates whether the requested element is a DOMIT! node (alias for isNode) 296 * @param string The name of the requested element 297 * @return boolean True if the requested element is a DOMIT! node 298 */ 299 function isCustomRSSElement($elementName) { 300 return isNode($elementName); 301 } //isCustomRSSElement 302 303 /** 304 * Gets a named list of existing elements as a child of the current element 305 * @return array A named list of existing elements 306 */ 307 function getElementList() { 308 return array_keys($this->DOMIT_RSS_indexer); 309 } //getElementList 310 311 /** 312 * Indicates whether a particular element exists 313 * @param string The name of the requested element 314 * @return boolean True if an element with the specified name exists 315 */ 316 function hasElement($elementName) { 317 return isset($this->DOMIT_RSS_indexer[strtolower($elementName)]); 318 } //hasElement 319 320 /** 321 * Gets a reference to an element with the specified name 322 * @param string The name of the requested element 323 * @return mixed A reference to an element with the specified name, or the text of the element if it is a text node 324 */ 325 function &getElement($elementName) { 326 $elementName = strtolower($elementName); 327 328 if (isset($this->DOMIT_RSS_indexer[$elementName])) { 329 return $this->DOMIT_RSS_indexer[$elementName]; 330 } 331 else { 332 xml_domit_rss_exception::raiseException(DOMIT_RSS_ELEMENT_NOT_FOUND_ERR, 333 'Element ' . $elementName . ' not present.'); 334 } 335 } //getElement 336 337 /** 338 * Gets a reference to an element at the specified index 339 * @param int The index of the requested element 340 * @return mixed A reference to an element at the specified index, or the text of the element if it is a text node 341 */ 342 function &getElementAt($index) { 343 $this->indexNumerically(); 344 345 if (isset($this->DOMIT_RSS_numericalIndexer[$index])) { 346 return $this->DOMIT_RSS_numericalIndexer[$index]; 347 } 348 else { 349 xml_domit_rss_exception::raiseException(DOMIT_RSS_ELEMENT_NOT_FOUND_ERR, 350 'Element ' . $index . ' not present.'); 351 } 352 } //getElementAt 353 354 /** 355 * Populates an integer-based index for elements if one isn't already present. 356 */ 357 function indexNumerically() { 358 if (!isset($this->DOMIT_RSS_numericalIndexer)) { 359 $counter = 0; 360 361 foreach ($this->DOMIT_RSS_indexer as $key => $value) { 362 $this->DOMIT_RSS_numericalIndexer[$counter] =& $this->DOMIT_RSS_indexer[$key]; 363 $counter++; 364 } 365 } 366 } //indexNumerically 367 368 /** 369 * Gets the text of the specified element 370 * @param string The name of the requested element 371 * @return string The element text, or an empty string 372 */ 373 function getElementText($elementName) { 374 $elementName = strtolower($elementName); 375 return $this->_getElementText($elementName, $this->DOMIT_RSS_indexer); 376 } //getElementText 377 378 /** 379 * Gets the text at the specified index 380 * @param int The index of the requested element 381 * @return string The element text, or an empty string 382 */ 383 function getElementTextAt($index) { 384 $this->indexNumerically(); 385 386 return $this->_getElementText($index, $this->DOMIT_RSS_numericalIndexer); 387 } //getElementTextAt 388 389 /** 390 * Gets the text at the specified index 391 * @param mixed The index or name of the requested element 392 * @param array The indexing array from which to extract data 393 * @return string The element text, or an empty string 394 */ 395 function _getElementText($index, &$myArray) { 396 if (isset($myArray[$index])) { 397 $element =& $myArray[$index]; 398 $result = ''; 399 400 if (is_array($element)) { 401 //do nothing; data for domit_rss_channels, domit_rss_items, 402 //and domit_rss_categories should be extracted with their own methods 403 } 404 else { 405 switch (strtolower(get_class($element))) { 406 case 'xml_domit_rss_simpleelement': 407 $result = $element->getElementText(); 408 break; 409 410 case 'xml_domit_rss_collection': 411 $result = $element->getElementText(); 412 break; 413 414 case 'domit_element': 415 $total = $element->childCount; 416 417 for ($i = 0; $i < $total; $i++) { 418 $currNode =& $element->childNodes[$i]; 419 420 if ($currNode->nodeType == DOMIT_CDATA_SECTION_NODE) { 421 $result .= $currNode->nodeValue; 422 } 423 else { 424 $result .= $currNode->toString(); 425 } 426 } 427 break; 428 } 429 } 430 431 return $result; 432 } 433 434 return ''; 435 } //_getElementText 436 } //xml_domit_rss_elementindexer 437 438 439 /** 440 * A base class for DOMIT! RSS and DOMIT! RSS Lite documents 441 * 442 * @package domit-rss 443 * @author John Heinstein <johnkarl@nbnet.nb.ca> 444 */ 445 class xml_domit_rss_base_document extends xml_domit_rss_elementindexer { 446 /** @var array An array of item elements (only present in some RSS formats) */ 447 var $domit_rss_items = array(); 448 /** @var array An array of existing channel elements */ 449 var $domit_rss_channels = array(); 450 /** @var array An array of existing category elements */ 451 var $domit_rss_categories = array(); 452 /** @var boolean True if caching is enabled */ 453 var $cacheEnabled = true; 454 /** @var Object A reference to the file caching object */ 455 var $cache; 456 /** @var boolean True if PEAR:Cache_Lite is to be used instead of php_text_cache */ 457 var $useCacheLite = false; 458 /** @var boolean True if php_http_client_generic is to be used instead of PHP get_file_contents */ 459 var $doUseHTTPClient = false; 460 /** @var string The name of the current parser - either 'DOMIT_RSS' or 'DOMIT_RSS_LITE' */ 461 var $parser; 462 /** @var object A reference to a http connection or proxy, if one is required */ 463 var $httpConnection = null; 464 /** @var int The timeout value for an http connection */ 465 var $rssTimeout = 0; 466 467 /** 468 * Constructor 469 * @param string Path to the rss file 470 * @param string Directory in which cache files are to be stored 471 * @param int Expiration time (in seconds) for the cache file 472 * @return mixed Null if an url was not provided, true if an url was provided and parsing was successful, false otherwise 473 */ 474 function xml_domit_rss_base_document ($url = '', $cacheDir = './', $cacheTime = 3600) { 475 $success = null; 476 $this->createDocument(); 477 478 if ($url != '') { //if rss data is from filesystem 479 if (substr($url, 0, 4) != "http") { 480 $rssText = $this->getTextFromFile($url); 481 $this->parseRSS($rssText); 482 } 483 else { 484 $this->createDefaultCache($cacheDir, $cacheTime); 485 $success = $this->loadRSS($url, $cacheDir, $cacheTime); 486 } 487 } 488 489 return $success; 490 } //xml_domit_rss_base_document 491 492 /** 493 * Specifies the default timeout value for connecting to a host 494 * @param int The number of seconds to timeout when attempting to connect to a server 495 */ 496 function setRSSTimeout($rssTimeout) { 497 $this->rssTimeout = $rssTimeout; 498 499 if (!$this->useCacheLite && !($this->cache == null)) { 500 $this->cache->setTimeout($rssTimeout); 501 } 502 } //setRSSTimeout 503 504 /** 505 * Specifies the parameters of the http conection used to obtain the xml data 506 * @param string The ip address or domain name of the connection 507 * @param string The path of the connection 508 * @param int The port that the connection is listening on 509 * @param int The timeout value for the connection 510 * @param string The user name, if authentication is required 511 * @param string The password, if authentication is required 512 */ 513 function setConnection($host, $path = '/', $port = 80, $timeout = 0, $user = null, $password = null) { 514 require_once (DOMIT_RSS_INCLUDE_PATH . 'php_http_client_generic.php'); 515 516 $this->httpConnection = new php_http_client_generic($host, $path, $port, $timeout, $user, $password); 517 } //setConnection 518 519 /** 520 * Specifies basic authentication for an http connection 521 * @param string The user name 522 * @param string The password 523 */ 524 function setAuthorization($user, $password) { 525 $this->httpConnection->setAuthorization($user, $password); 526 } //setAuthorization 527 528 /** 529 * Specifies that a proxy is to be used to obtain the xml data 530 * @param string The ip address or domain name of the proxy 531 * @param string The path to the proxy 532 * @param int The port that the proxy is listening on 533 * @param int The timeout value for the connection 534 * @param string The user name, if authentication is required 535 * @param string The password, if authentication is required 536 */ 537 function setProxyConnection($host, $path = '/', $port = 80, $timeout = 0, $user = null, $password = null) { 538 require_once (DOMIT_RSS_INCLUDE_PATH . 'php_http_proxy.php'); 539 540 $this->httpConnection = new php_http_proxy($host, $path, $port, $timeout, $user, $password); 541 } //setProxyConnection 542 543 /** 544 * Specifies a user name and password for the proxy 545 * @param string The user name 546 * @param string The password 547 */ 548 function setProxyAuthorization($user, $password) { 549 $this->httpConnection->setProxyAuthorization($user, $password); 550 } //setProxyAuthorization 551 552 /** 553 * Specifies whether an HTTP client should be used to establish a connection 554 * @param boolean True if an HTTP client is to be used to establish the connection 555 */ 556 function useHTTPClient($truthVal) { 557 $this->doUseHTTPClient = $truthVal; 558 } //useHTTPClient 559 560 /** 561 * Returns the name of the parser 562 *@return string Either 'DOMIT_RSS' or 'DOMIT_RSS_LITE' 563 */ 564 function parsedBy() { 565 return $this->parser; 566 } //parsedBy 567 568 /** 569 * Creates an empty DOMIT! document to contain the RSS nodes 570 */ 571 function createDocument() { 572 require_once (DOMIT_RSS_INCLUDE_PATH . 'xml_domit_include.php'); 573 $this->node = new DOMIT_Document(); 574 $this->node->resolveErrors(true); 575 } //createDocument 576 577 /** 578 * Substitutes PEAR::Cache_Lite for the default php_text_cache 579 * @param boolean True if Cache Lite is to be used 580 * @param string Absolute or relative path to the Cache Lite library 581 * @param string Directory for cache files 582 * @param int Expiration time for a cache file 583 */ 584 function useCacheLite($doUseCacheLite, $pathToLibrary = './Lite.php', $cacheDir = './', $cacheTime = 3600) { 585 $this->useCacheLite = $doUseCacheLite; 586 587 if ($doUseCacheLite) { 588 if (!file_exists($pathToLibrary)) { 589 $this->useCacheLite(false); 590 } 591 else { 592 require_once($pathToLibrary); 593 594 $cacheOptions = array('cacheDir' => $cacheDir, 'lifeTime' => $cacheTime); 595 $this->cache = new Cache_Lite($cacheOptions); 596 } 597 } 598 else { 599 $this->createDefaultCache($cacheDir, $cacheTime); 600 } 601 } //useCacheLite 602 603 /** 604 * Instantiates a default cache (php_text_cache) 605 * @param string Directory for cache files 606 * @param int Expiration time for a cache file 607 */ 608 function createDefaultCache($cacheDir = './', $cacheTime = 3600) { 609 require_once (DOMIT_RSS_INCLUDE_PATH . 'php_text_cache.php'); 610 $this->cache = new php_text_cache($cacheDir, $cacheTime, $this->rssTimeout); 611 } //initDefaultCache 612 613 /** 614 * Disables caching mechanism 615 */ 616 function disableCache() { 617 $this->cacheEnabled = false; 618 } //initDefaultCache 619 620 /** 621 * Loads and parses the RSS at the specified url 622 * @param string The url of the RSS feed 623 * @return boolean True if parsing is successful 624 */ 625 function loadRSS($url) { 626 if (substr($url, 0, 4) != "http") { 627 $rssText = $this->getTextFromFile($url); 628 return $this->parseRSS($rssText); 629 } 630 else { 631 if ($this->cacheEnabled && !isset($this->cache)) { 632 $this->createDefaultCache(); 633 $this->cache->httpConnection =& $this->httpConnection; 634 } 635 636 $success = $this->loadRSSData($url); 637 638 if ($success) { 639 $this->_init(); 640 } 641 642 return $success; 643 } 644 } //loadRSS 645 646 /** 647 * Parses the RSS text provided 648 * @param string The RSS text 649 * @return boolean True if parsing is successful 650 */ 651 function parseRSS($rssText) { 652 if ($this->cacheEnabled && !isset($this->cache)) $this->createDefaultCache(); 653 $success = $this->parseRSSData($rssText); 654 655 if ($success) { 656 $this->_init(); 657 } 658 659 return $success; 660 } //parseRSS 661 662 /** 663 * Retrieves the RSS data from the url/cache file and parses 664 * @param string The url for the RSS data 665 * @return boolean True if parsing is successful 666 */ 667 function loadRSSData($url) { 668 $rssText = $this->getDataFromCache($url); 669 return $this->parseRSSData($rssText); 670 } //loadRSSData 671 672 /** 673 * Retrieves the RSS data from the url/cache file 674 * @param string The url for the RSS data 675 * @return string The RSS data 676 */ 677 function getDataFromCache($url) { 678 if ($this->cacheEnabled) { 679 if ($this->useCacheLite) { 680 if ($rssText = $this->cache->get($url)) { 681 return $rssText; 682 } 683 else { 684 $rssText = $this->getTextFromFile($url); 685 if ($rssText != '') $this->cache->save($rssText, $url); 686 return $rssText; 687 } 688 } 689 else { 690 $this->cache->useHTTPClient($this->doUseHTTPClient); 691 return $this->cache->getData($url); 692 } 693 } 694 else { 695 return $this->getTextFromFile($url); 696 } 697 } //getDataFromCache 698 699 /** 700 * Parses the RSS data provided 701 * @param string The the RSS data 702 * @return boolean True if parsing is successful 703 */ 704 function parseRSSData($rssText) { 705 if ($rssText != '') { 706 return $this->fromString($rssText); 707 } 708 else { 709 return false; 710 } 711 } //parseRSSData 712 713 /** 714 * Reads in RSS text from a file and parses it 715 * @param string The file path 716 * @return boolean True if parsing is successful 717 */ 718 function &fromFile($filename) { 719 $success = $this->node->loadXML($filename, false); 720 return $success; 721 } //fromFile 722 723 /** 724 * Reads in RSS text from a string and parses it 725 * @param string The RSS text 726 * @return boolean True if parsing is successful 727 */ 728 function &fromString($rssText) { 729 $success = $this->node->parseXML($rssText, false); 730 return $success; 731 } //fromString 732 733 /** 734 * Establishes a connection, given an url 735 * @param string The url of the data 736 */ 737 function establishConnection($url) { 738 require_once (DOMIT_RSS_INCLUDE_PATH . 'php_http_client_generic.php'); 739 740 $host = php_http_connection::formatHost($url); 741 $host = substr($host, 0, strpos($host, '/')); 742 743 $this->setConnection($host, '/', 80, $this->rssTimeout); 744 } //establishConnection 745 746 /** 747 * Get text from an url or file 748 * @param string The url or file path 749 * @return string The text contained in the url or file, or an empty string 750 */ 751 function getTextFromFile($filename) { 752 $fileContents = ''; 753 754 if ($this->doUseHTTPClient) { 755 $this->establishConnection($filename); 756 $response =& $this->httpConnection->get($filename); 757 758 if ($response != null) { 759 $fileContents = $response->getResponse(); 760 } 761 } 762 else { 763 if (function_exists('file_get_contents')) { 764 $fileContents = @file_get_contents($filename); 765 } 766 else { 767 require_once (DOMIT_RSS_INCLUDE_PATH . 'php_file_utilities.php'); 768 $fileContents =& php_file_utilities::getDataFromFile($filename, 'r'); 769 } 770 771 if ($fileContents == '') { 772 $this->establishConnection($filename); 773 $response =& $this->httpConnection->get($filename); 774 775 if ($response != null) { 776 $fileContents = $response->getResponse(); 777 } 778 } 779 } 780 781 return $fileContents; 782 } //getTextFromFile 783 784 /** 785 * Gets a reference to the underlying DOM document 786 * @return Object A reference to the underlying DOM document 787 */ 788 function &getDocument() { 789 return $this->node; 790 } //getDocument 791 792 /** 793 * Gets a reference to the root DOM element 794 * @return Object A reference to the root DOM element 795 */ 796 function &getNode() { 797 return $this->node->documentElement; 798 } //getNode 799 800 /** 801 * Forces channel elements that are external to a channel to be referenced as subelements of that channel 802 */ 803 function handleChannelElementsEmbedded() { 804 if (count($this->domit_rss_items) > 0) { 805 foreach ($this->domit_rss_channels as $key => $value) { 806 $this->domit_rss_channels[$key]->domit_rss_items =& $this->domit_rss_items; 807 808 if (count($this->DOMIT_RSS_indexer) > 0) { 809 foreach ($this->DOMIT_RSS_indexer as $ikey => $ivalue) { 810 if ($ikey != DOMIT_RSS_ARRAY_CHANNELS) { 811 $this->domit_rss_channels[$key]->DOMIT_RSS_indexer[$ikey] =& $this->DOMIT_RSS_indexer[$ikey]; 812 unset($this->DOMIT_RSS_indexer[$ikey]); 813 } 814 } 815 } 816 } 817 } 818 } //handleChannelElementsEmbedded 819 820 /** 821 * Returns the version of RSS used to format the data 822 * @return string The version of RSS used to format the data 823 */ 824 function getRSSVersion() { 825 $version = $this->node->documentElement->getAttribute(DOMIT_RSS_ATTR_VERSION); 826 827 if ($version == '') { 828 $xmlns = $this->node->documentElement->getAttribute('xmlns'); 829 $total = strlen($xmlns); 830 831 if (substr($xmlns, $total) == '/') { 832 $total--; 833 } 834 835 for ($i = ($total - 1); $i > -1; $i--) { 836 $currentChar = substr($xmlns, $i); 837 838 if ($currentChar == '/') { 839 break; 840 } 841 else { 842 $version = $currentChar . $version; 843 } 844 } 845 846 } 847 848 return $version; 849 } //getRSSVersion 850 851 /** 852 * Returns the number of channels in the document 853 * @return int The number of channels in the document 854 */ 855 function getChannelCount() { 856 return count($this->domit_rss_channels); 857 } //getChannelCount() 858 859 /** 860 * Returns a reference to the channel located at the specified index 861 * @return Object A reference to the channel located at the specified index 862 */ 863 function &getChannel($index) { 864 return $this->domit_rss_channels[$index]; 865 } //getChannel 866 } //xml_domit_rss_base_document 867 868 /** 869 * Represents a simple RSS element, without attributes and only a single child text node 870 * 871 * @package domit-rss 872 * @subpackage domit-rss-main 873 * @author John Heinstein <johnkarl@nbnet.nb.ca> 874 */ 875 class xml_domit_rss_simpleelement extends xml_domit_rss_elementindexer { 876 /** 877 * Constructor 878 * @param Object A DOM node containing element data 879 */ 880 function xml_domit_rss_simpleelement(&$element) { 881 $this->node =& $element; 882 } //xml_domit_rss_simpleelement 883 884 /** 885 * Gets the text of the element 886 * @return string The element text 887 */ 888 function getElementText() { 889 $element =& $this->node; 890 $result = ''; 891 $total = $element->childCount; 892 893 for ($i = 0; $i < $total; $i++) { 894 $currNode =& $element->childNodes[$i]; 895 896 if ($currNode->nodeType == DOMIT_CDATA_SECTION_NODE) { 897 $result .= $currNode->nodeValue; 898 } 899 else { 900 $result .= $currNode->toString(); 901 } 902 } 903 904 return $result; 905 } //getElementText 906 } //xml_domit_rss_simpleelement 907 908 /** 909 * @global object Reference to custom error handler for DOMIT RSS Exception class 910 */ 911 $GLOBALS['DOMIT_RSS_Exception_errorHandler'] = null; 912 /** 913 * @global int Error mode; specifies whether to die on error or simply return 914 */ 915 //$GLOBALS['DOMIT_RSS_Exception_mode'] = DOMIT_RSS_ONERROR_RETURN; 916 // fixes bug identified here: sarahk.pcpropertymanager.com/blog/using-domit-rss/225/ 917 $GLOBALS['DOMIT_RSS_Exception_mode'] = 1; 918 919 /** 920 * @global string Log file for errors 921 */ 922 $GLOBALS['DOMIT_RSS_Exception_log'] = null; 923 924 925 /** 926 * A DOMIT! RSS exception handling class 927 * 928 * @package domit-rss 929 * @author John Heinstein <johnkarl@nbnet.nb.ca> 930 */ 931 class xml_domit_rss_exception { 932 /** 933 * Raises the specified exception 934 * @param int The error number 935 * @param string A string explanation of the error 936 */ 937 function raiseException($errorNum, $errorString) { 938 if ($GLOBALS['DOMIT_RSS_Exception_errorHandler'] != null) { 939 call_user_func($GLOBALS['DOMIT_RSS_Exception_errorHandler'], $errorNum, $errorString); 940 } 941 else { 942 $errorMessageText = $errorNum . ' ' . $errorString; 943 $errorMessage = 'Error: ' . $errorMessageText; 944 945 if ((!isset($GLOBALS['DOMIT_RSS_ERROR_FORMATTING_HTML'])) || 946 ($GLOBALS['DOMIT_RSS_ERROR_FORMATTING_HTML'] == true)) { 947 $errorMessage = "<p><pre>" . $errorMessage . "</pre></p>"; 948 } 949 950 //log error to file 951 if ((isset($GLOBALS['DOMIT_RSS_Exception_log'])) && 952 ($GLOBALS['DOMIT_RSS_Exception_log'] != null)) { 953 require_once (DOMIT_RSS_INCLUDE_PATH . 'php_file_utilities.php'); 954 955 $logItem = "\n" . date('Y-m-d H:i:s') . 'DOMIT! RSS Error ' . $errorMessageText; 956 php_file_utilities::putDataToFile($GLOBALS['DOMIT_RSS_Exception_log'], $logItem, 'a'); 957 } 958 959 switch ($GLOBALS['DOMIT_RSS_Exception_mode']) { 960 case DOMIT_RSS_ONERROR_CONTINUE: 961 return; 962 break; 963 964 case DOMIT_RSS_ONERROR_DIE: 965 die($errorMessage); 966 break; 967 } 968 } 969 } //raiseException 970 971 /** 972 * custom handler for DOM RSS errors 973 * @param object A reference to the custom error handler 974 */ 975 function setErrorHandler($method) { 976 $GLOBALS['DOMIT_RSS_Exception_errorHandler'] =& $method; 977 978 require_once (DOMIT_RSS_INCLUDE_PATH . 'php_http_exceptions.php'); 979 $GLOBALS['HTTP_Exception_errorHandler'] =& $method; 980 981 require_once (DOMIT_RSS_INCLUDE_PATH . 'xml_domit_shared.php'); 982 $GLOBALS['HTTP_Exception_errorHandler'] =& $method; 983 } //setErrorHandler 984 985 /** 986 * Set error mode 987 * @param int The DOMIT RSS error mode 988 */ 989 function setErrorMode($mode) { 990 $GLOBALS['DOMIT_RSS_Exception_mode'] = $mode; 991 992 require_once (DOMIT_RSS_INCLUDE_PATH . 'php_http_exceptions.php'); 993 require_once (DOMIT_RSS_INCLUDE_PATH . 'xml_domit_shared.php'); 994 995 if ($mode == DOMIT_RSS_ONERROR_CONTINUE) { 996 $GLOBALS['HTTP_Exception_mode'] = HTTP_ONERROR_CONTINUE; 997 $GLOBALS['DOMIT_DOMException_mode'] = DOMIT_ONERROR_CONTINUE; 998 } 999 else { 1000 $GLOBALS['HTTP_Exception_mode'] = HTTP_ONERROR_DIE; 1001 $GLOBALS['DOMIT_DOMException_mode'] = DOMIT_ONERROR_DIE; 1002 } 1003 } //setErrorMode 1004 1005 /** 1006 * Set error mode 1007 * @param boolean True if errors should be logged 1008 * @param string Absolute or relative path to log file 1009 */ 1010 function setErrorLog($doLogErrors, $logfile) { 1011 require_once (DOMIT_RSS_INCLUDE_PATH . 'php_http_exceptions.php'); 1012 1013 if ($doLogErrors) { 1014 $GLOBALS['DOMIT_RSS_Exception_log'] = $logfile; 1015 $GLOBALS['HTTP_Exception_log'] = $logfile; 1016 $GLOBALS['DOMIT_Exception_log'] = $logfile; 1017 } 1018 else { 1019 $GLOBALS['DOMIT_RSS_Exception_log'] = null; 1020 $GLOBALS['HTTP_Exception_log'] = null; 1021 $GLOBALS['DOMIT_Exception_log'] = null; 1022 } 1023 } //setErrorLog 1024 } //xml_domit_rss_exception 1025 1026 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Wed Nov 21 14:43:32 2007 | par Balluche grâce à PHPXref 0.7 |
|