rss-inc.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. function generateRssFile($newsPath, $destPath)
  3. {
  4. if (!file_exists($newsPath)) {
  5. return 'News file does not exists';
  6. }
  7. $xmlFile = simplexml_load_file($newsPath);
  8. // édition du début du fichier XML
  9. $xml = '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">';
  10. $xml .= '<channel>';
  11. $xml .= '<title>Freesynd News</title>';
  12. $xml .= '<link>http://freesynd.sourceforge.net/</link>';
  13. $xml .= '<description>Recent news from the Freesynd project.</description>';
  14. $lastBuildDate = date(DATE_RSS, filemtime($newsPath));
  15. $xml .= '<lastBuildDate>' . $lastBuildDate . '</lastBuildDate>';
  16. foreach ($xmlFile->item as $item) {
  17. $xml .= '<item>';
  18. // News title
  19. $xml .= '<title>' . htmlentities($item['title']) . '</title>';
  20. // link
  21. $xml .= '<link>http://freesynd.sourceforge.net/index.php#n' . $item['id'] . '</link>';
  22. // News content
  23. $xml .= '<description>' . $item['rssDesc'] . '</description>';
  24. $dtTime = DateTime::createFromFormat('Y-m-d', $item['date']);
  25. $xml .= '<pubDate>' . $dtTime->format("D, d M Y") .' 09:00:01 GMT</pubDate>';
  26. $xml .= '<guid>http://freesynd.sourceforge.net/index.php#n' . $item['id'] . '</guid>';
  27. $xml .= '</item>';
  28. }
  29. // édition de la fin du fichier XML
  30. $xml .= '</channel>';
  31. $xml .= '</rss>';
  32. // écriture dans le fichier
  33. $fp = fopen($destPath, 'w+');
  34. if (!$fp) {
  35. return "Cannot open news file";
  36. }
  37. if(fputs($fp, $xml) == false) {
  38. return "Cannot write to file " . $destPath;
  39. }
  40. fclose($fp);
  41. return FALSE;
  42. }
  43. ?>