PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/administrator/components/com_artforms/lib/af.lib.rdf.php

https://github.com/nekrad/metux-joomla
PHP | 176 lines | 105 code | 37 blank | 34 comment | 18 complexity | 45dae3fbb3cf9b890a092026634cad69 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: af.lib.rdf.php v.2.1b7 2007-11-26 16:44:59Z GMT-3 $
  4. * @package ArtForms 2.1b7
  5. * @subpackage ArtForms Component
  6. * @original name code from RSS headline grabber
  7. * @original author Yelvington and Changes by Bob Zoller (bob@kludgebox.com)
  8. * @copyright Copyright (C) 2007 InterJoomla. All rights reserved.
  9. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL version 2, see LICENSE.txt
  10. * This version may have been modified pursuant to the
  11. * GNU General Public License, and as distributed it includes or is derivative
  12. * of works licensed under the GNU General Public License or other free
  13. * or open source software licenses.
  14. * See COPYRIGHT.txt for copyright notices and details.
  15. */
  16. defined( '_JEXEC' ) or die( 'Restricted access' );
  17. /**
  18. *RSS/XML news feed headline grabber
  19. */
  20. class afRDF {// A util Class for reading in RDF XML Headline style files...
  21. function createRDF($rdf, $agelimit = 30, $timeout = 10) {
  22. unlink(JPATH_SITE.DS.'cache'.DS.basename($rdf));
  23. $errno = '';
  24. $errstr = '';
  25. $cachefile = basename($rdf);
  26. // convert agelimit to seconds
  27. $agelimit *= 60;
  28. $timestamp = @filectime($cachefile);
  29. $age = time() - $timestamp;
  30. if($age > $agelimit) {
  31. $url = parse_url($rdf);
  32. $fp = fsockopen($url['host'], "80", $errno, $errstr, $timeout);
  33. if (!$fp) return; //just quit on error
  34. else {
  35. $local = @fopen(JPATH_SITE.DS.'cache'.DS.$cachefile, 'w');
  36. if (!$local) return; //just quit on error
  37. fputs($fp, "GET " . $url['path'] . " HTTP/1.1\r\nHost: " . $url['host'] . "\r\n\r\n");
  38. //while(!feof($fp))
  39. fwrite($local, fgets($fp, 128));
  40. fclose($local);
  41. }
  42. }
  43. }
  44. // This function will read the data in from the passed in URL if it is required.
  45. function getRDF($url, $timeToLive) {
  46. $timeToLive *= 60; // convert timeToLive into secs
  47. $basename = str_replace('?', '&', JPATH_SITE.DS.'cache'.DS.basename($url)) ; // Get the basename of URL for the cached filename. We do this here, cos we need to do it more than once
  48. $this->createRDF($url);
  49. $timestamp = @filectime($basename); // Get the timestamp of the file.
  50. $age = (time() - $timestamp); // Work out how old the file is that we have already..
  51. // If the file is too old, then we need to refresh it from the URL
  52. if($age > $timeToLive) {
  53. $ip = gethostbyname(substr($url,strpos($url,'jartforms'),strpos($url,'.com.ar')-3));
  54. if (strcasecmp($ip, substr($url,strpos($url,'jartforms'),strpos($url,'.com.ar')-3)) == 0) {
  55. return false;
  56. }
  57. $rdfHandle = @fopen($url,"r"); // Open the RDF file for reading
  58. if(!$rdfHandle)return false;
  59. $rdfData = fread($rdfHandle, 64000) ; // Read in the RDF data. 64K limit on filesize, should be enough.
  60. fclose($rdfHandle); // Close the Data feed
  61. // OK there is more recent news, so rewrite the cached news file..
  62. $localFile = fopen($basename, "w") ; // Open the local file for writing
  63. fwrite($localFile, $rdfData ) ; // Pump in all the data into the file.
  64. fclose($localFile) ; // Close the local file after writing to it
  65. } // end IF
  66. return true;
  67. } // end getRDF
  68. // Removes spurious tags from the link that we don't want
  69. function formatLink($item) {
  70. $link = ereg_replace(".*<link>","",$item); // Remove the leading <link> tags
  71. $link = ereg_replace("</link>.*","",$link); // Remove the closing </link> tags
  72. $title = ereg_replace(".*<title>","",$item); // Remove the leading <title> tags
  73. $title = ereg_replace("</title>.*","",$title); // Remove the closing </title> tags
  74. if ($title) // If we got anything left after all that trimming...
  75. // Choose how you want the link formatted here... This has no underline, and opens in a new window...
  76. echo "<a href=\"$link\" style=\"text-decoration:none\"
  77. target=\"_blank\">".htmlspecialchars($title, ENT_QUOTES)."</a><br />";
  78. } // end formatLink
  79. function displayRDF($rdffile, $randomise = TRUE, $numLinks = 3, $title) {
  80. $rdf = JPATH_SITE.DS.'cache'.DS.$rdffile;
  81. $localFile = fopen($rdf, "r"); // OK open up the local rdf file for reading
  82. clearstatcache() ; // Clear out the file size cache
  83. if (filesize($rdf) <= 0) {
  84. return false;
  85. }
  86. $rdfData = fread($localFile, filesize($rdf)); // Read in the data to memory
  87. fclose($localFile); // Close down the open file.
  88. // Get rid of all spurious leading and closing rdf data from the data in memory
  89. $rdfData = ereg_replace("<\?xml.*/image>","",$rdfData);
  90. $rdfData = ereg_replace("</rdf.*","",$rdfData);
  91. $rdfData = ereg_replace("[\r,\n]", "", $rdfData);
  92. $rdfData = chop($rdfData); // Strip any whitespace from the end of the string
  93. $rdfArray = explode("</item>",$rdfData); // Split up the string into an array to make it more manageable
  94. $max = sizeof($rdfArray); // See how many items we have got
  95. // Echo the font formatting... This is just HTML to make it look a little pretty
  96. if ($max > 1) {
  97. // echo "<p style=\"font-family: verdana, arial, helvetica;\">" ;
  98. // We need to do different stuff if we are to randomise the links...
  99. // The links will be randomised so we want a different message to the user....
  100. // The max -1 is to compensate for the 0 indexed array structure..
  101. if ($randomise) {
  102. echo "Displaying $numLinks (of " . ($max-1) . ") random headlines from $rdf... Updated every 30 minutes.. Refresh for some more!<br />";
  103. $links = array_rand( $rdfArray, $numLinks ); // OK select the keys of the links at random from the array..
  104. $upperLimit = $numLinks ; // Set this to the number of links to be displayed
  105. } else {
  106. if (!empty($title)) {
  107. echo $title;
  108. }
  109. $links = array_keys($rdfArray) ; // Give the keys to be displayed all the links we have parsed..
  110. $upperLimit = $max ; // Set the upper Limit to be all of the headlines
  111. }
  112. // Display the links...
  113. for ($i = 0 ; $i < $upperLimit ; $i++ ) {
  114. $this->formatLink($rdfArray[$links[$i]]);
  115. }
  116. // Close the font formatting like a good html coder ;)
  117. }
  118. return true;
  119. }
  120. function parseRSS($url)
  121. {
  122. @require_once(JPATH_SITE.DS.'includes'.DS.'domit'.DS.'xml_domit_rss.php');
  123. $rssdoc =& new xml_domit_rss_document($url);
  124. $numChannels = $rssdoc->getChannelCount();
  125. for ($i = 0; $i < $numChannels; $i++) {
  126. $currChannel =& $rssdoc->getChannel($i);
  127. //iterate through item elements
  128. $numItems = $currChannel->getItemCount();
  129. echo '<ul>';
  130. for ($j = 0; $j < $numItems; $j++) {
  131. $currItem =& $currChannel->getItem($j);
  132. //echo item info
  133. echo "<li><a href=\"" . $currItem->getLink() . "\">".$currItem->getTitle() . "</a></li>\n\n";
  134. }
  135. echo '</ul>';
  136. }
  137. }
  138. }
  139. ?>