/includes/qcodo/_core/framework/QRssFeed.class.php

https://github.com/quinta/qcodo · PHP · 272 lines · 222 code · 34 blank · 16 comment · 21 complexity · a96a9624c5553984b3989b0766069216 MD5 · raw file

  1. <?php
  2. class QRssFeed extends QBaseClass {
  3. // Required RSS 2 Fields
  4. protected $strTitle;
  5. protected $strLink;
  6. protected $strDescription;
  7. // Optional RSS 2 Fields
  8. // TODO: Fields that are commented out are currently not supported because they are either
  9. // complex RSS tags that need additional coding to implement and/or they are rarely used by readers
  10. protected $strLanguage;
  11. protected $strCopyright;
  12. protected $strManagingEditor;
  13. protected $strWebMaster;
  14. protected $dttPubDate;
  15. protected $dttLastBuildDate;
  16. // protected $strCategory;
  17. protected $strGenerator;
  18. protected $strDocs = 'http://blogs.law.harvard.edu/tech/rss';
  19. // protected $strCloud;
  20. protected $strTtl;
  21. protected $objImage;
  22. // protected $strRating;
  23. // protected $strTextInput;
  24. // protected $strSkipHours;
  25. // protected $strSkipDays;
  26. protected $objItemArray = array();
  27. public function __construct($strTitle, $strLink, $strDescription) {
  28. $this->strTitle = $strTitle;
  29. $this->strLink = $strLink;
  30. $this->strDescription = $strDescription;
  31. $this->strGenerator = 'Qcodo Development Framework ' . QCODO_VERSION;
  32. }
  33. public function GetXml() {
  34. $strToReturn = "<rss version=\"2.0\">\r\n<channel>\r\n";
  35. $strToReturn .= sprintf(" <title>%s</title>\r\n", $this->strTitle);
  36. $strToReturn .= sprintf(" <link>%s</link>\r\n", $this->strLink);
  37. $strToReturn .= sprintf(" <description>%s</description>\r\n", $this->strDescription);
  38. if ($this->strLanguage)
  39. $strToReturn .= sprintf(" <language>%s</language>\r\n", $this->strLanguage);
  40. if ($this->strCopyright)
  41. $strToReturn .= sprintf(" <copyright>%s</copyright>\r\n", $this->strCopyright);
  42. if ($this->strManagingEditor)
  43. $strToReturn .= sprintf(" <managingEditor>%s</managingEditor>\r\n", $this->strManagingEditor);
  44. if ($this->strWebMaster)
  45. $strToReturn .= sprintf(" <webMaster>%s</webMaster>\r\n", $this->strWebMaster);
  46. if ($this->dttPubDate)
  47. $strToReturn .= sprintf(" <pubDate>%s</pubDate>\r\n", $this->dttPubDate->__toString(QDateTime::FormatRfc822));
  48. if ($this->dttLastBuildDate)
  49. $strToReturn .= sprintf(" <lastBuildDate>%s</lastBuildDate>\r\n", $this->dttLastBuildDate->__toString(QDateTime::FormatRfc822));
  50. if ($this->strGenerator)
  51. $strToReturn .= sprintf(" <generator>%s</generator>\r\n", $this->strGenerator);
  52. if ($this->strDocs)
  53. $strToReturn .= sprintf(" <docs>%s</docs>\r\n", $this->strDocs);
  54. if ($this->strTtl)
  55. $strToReturn .= sprintf(" <ttl>%s</ttl>\r\n", $this->strTtl);
  56. if ($this->objImage)
  57. $strToReturn .= $this->objImage->GetXml($this->strTitle, $this->strLink);
  58. foreach ($this->objItemArray as $objItem)
  59. $strToReturn .= $objItem->GetXml();
  60. $strToReturn .= "</channel>\r\n</rss>\r\n";
  61. return $strToReturn;
  62. }
  63. public function Run() {
  64. ob_clean();
  65. header('Content-type: text/xml');
  66. if (QApplication::$EncodingType)
  67. printf("<?xml version=\"1.0\" encoding=\"%s\" ?>\r\n", QApplication::$EncodingType);
  68. else
  69. _p("<?xml version=\"1.0\" ?>\r\n", false);
  70. _p($this->GetXml(), false);
  71. }
  72. public function AddItem(QRssItem $objItem) {
  73. array_push($this->objItemArray, $objItem);
  74. }
  75. public function __get($strName) {
  76. try {
  77. switch ($strName) {
  78. case 'Title': return $this->strTitle;
  79. case 'Link': return $this->strLink;
  80. case 'Description': return $this->strDescription;
  81. case 'Language': return $this->strLanguage;
  82. case 'Copyright': return $this->strCopyright;
  83. case 'ManagingEditor': return $this->strManagingEditor;
  84. case 'WebMaster': return $this->strWebMaster;
  85. case 'PubDate': return $this->dttPubDate;
  86. case 'LastBuildDate': return $this->dttLastBuildDate;
  87. case 'Generator': return $this->strGenerator;
  88. case 'Docs': return $this->strDocs;
  89. case 'Ttl': return $this->strTtl;
  90. case 'Image': return $this->objImage;
  91. default: return parent::__get($strName);
  92. }
  93. } catch (QCallerException $objExc) {
  94. $objExc->IncrementOffset();
  95. throw $objExc;
  96. }
  97. }
  98. public function __set($strName, $mixValue) {
  99. try {
  100. switch ($strName) {
  101. case 'Title': return ($this->strTitle = QType::Cast($mixValue, QType::String));
  102. case 'Link': return ($this->strLink = QType::Cast($mixValue, QType::String));
  103. case 'Description': return ($this->strDescription = QType::Cast($mixValue, QType::String));
  104. case 'Language': return ($this->strLanguage = QType::Cast($mixValue, QType::String));
  105. case 'Copyright': return ($this->strCopyright = QType::Cast($mixValue, QType::String));
  106. case 'ManagingEditor': return ($this->strManagingEditor = QType::Cast($mixValue, QType::String));
  107. case 'WebMaster': return ($this->strWebMaster = QType::Cast($mixValue, QType::String));
  108. case 'PubDate': return ($this->dttPubDate = QType::Cast($mixValue, QType::DateTime));
  109. case 'LastBuildDate': return ($this->dttLastBuildDate = QType::Cast($mixValue, QType::DateTime));
  110. case 'Generator': return ($this->strGenerator = QType::Cast($mixValue, QType::String));
  111. case 'Docs': return ($this->strDocs= QType::Cast($mixValue, QType::String));
  112. case 'Ttl': return ($this->strTtl = QType::Cast($mixValue, QType::String));
  113. case 'Image': return ($this->objImage = QType::Cast($mixValue, 'QRssImage'));
  114. default: return parent::__set($strName, $mixValue);
  115. }
  116. } catch (QCallerException $objExc) {
  117. $objExc->IncrementOffset();
  118. throw $objExc;
  119. }
  120. }
  121. }
  122. class QRssImage extends QBaseClass {
  123. protected $strUrl;
  124. protected $strTitle;
  125. protected $strLink;
  126. public function __construct($strUrl, $strTitle = null, $strLink = null) {
  127. $this->strUrl = $strUrl;
  128. $this->strTitle = $strTitle;
  129. $this->strLink = $strLink;
  130. }
  131. public function GetXml($strTitle, $strLink) {
  132. $strToReturn = " <image>\r\n";
  133. $strToReturn .= sprintf(" <url>%s</url>\r\n", $this->strUrl);
  134. $strToReturn .= sprintf(" <title>%s</title>\r\n", ($this->strTitle) ? $this->strTitle : $strTitle);
  135. $strToReturn .= sprintf(" <link>%s</link>\r\n", ($this->strLink) ? $this->strLink : $strLink);
  136. $objImageSize = @getimagesize($this->strUrl);
  137. if ($objImageSize) {
  138. $strToReturn .= sprintf(" <width>%s</width>\r\n", $objImageSize[0]);
  139. $strToReturn .= sprintf(" <height>%s</height>\r\n", $objImageSize[1]);
  140. }
  141. $strToReturn .= " </image>\r\n";
  142. return $strToReturn;
  143. }
  144. }
  145. class QRssCategory extends QBaseClass {
  146. protected $strCategory;
  147. protected $strDomain;
  148. public function __construct($strCategory, $strDomain = null) {
  149. $this->strCategory = $strCategory;
  150. $this->strDomain = $strDomain;
  151. }
  152. public function GetXml() {
  153. if ($this->strDomain)
  154. return sprintf(" <category domain=\"%s\">%s</category>\r\n", $this->strDomain, $this->strCategory);
  155. else
  156. return sprintf(" <category>%s</category>\r\n", $this->strCategory);
  157. }
  158. }
  159. class QRssItem extends QBaseClass {
  160. // Required RSS 2 Item Fields
  161. protected $strTitle;
  162. protected $strLink;
  163. protected $strDescription;
  164. // Optional RSS 2 Item Fields
  165. // TODO: Fields that are commented out are currently not supported because they are either
  166. // complex RSS tags that need additional coding to implement and/or they are rarely used by readers
  167. protected $strAuthor;
  168. protected $objCategoryArray = array();
  169. protected $strComments;
  170. // protected $strEnclosure;
  171. protected $strGuid;
  172. protected $blnGuidPermaLink;
  173. protected $dttPubDate;
  174. // protected $strSource;
  175. public function __construct($strTitle, $strLink, $strDescription = null) {
  176. $this->strTitle = $strTitle;
  177. $this->strLink = $strLink;
  178. $this->strDescription = $strDescription;
  179. }
  180. public function GetXml() {
  181. $strToReturn = " <item>\r\n";
  182. $strToReturn .= sprintf(" <title>%s</title>\r\n", QString::XmlEscape($this->strTitle));
  183. $strToReturn .= sprintf(" <link>%s</link>\r\n", QString::XmlEscape($this->strLink));
  184. $strToReturn .= sprintf(" <description>%s</description>\r\n", QString::XmlEscape($this->strDescription));
  185. if ($this->strAuthor)
  186. $strToReturn .= sprintf(" <author>%s</author>\r\n", QString::XmlEscape($this->strAuthor));
  187. foreach ($this->objCategoryArray as $objCategory)
  188. $strToReturn .= $objCategory->GetXml();
  189. if ($this->strComments)
  190. $strToReturn .= sprintf(" <comments>%s</comments>\r\n", QString::XmlEscape($this->strComments));
  191. if ($this->strGuid)
  192. $strToReturn .= sprintf(" <guid isPermaLink=\"%s\">%s</guid>\r\n", ($this->blnGuidPermaLink) ? 'true' : 'false', $this->strGuid);
  193. if ($this->dttPubDate)
  194. $strToReturn .= sprintf(" <pubDate>%s</pubDate>\r\n", $this->dttPubDate->__toString(QDateTime::FormatRfc822));
  195. $strToReturn .= " </item>\r\n";
  196. return $strToReturn;
  197. }
  198. public function AddCategory(QRssCategory $objCategory) {
  199. array_push($this->objCategoryArray, $objCategory);
  200. }
  201. public function __get($strName) {
  202. try {
  203. switch ($strName) {
  204. case 'Title': return $this->strTitle;
  205. case 'Link': return $this->strLink;
  206. case 'Description': return $this->strDescription;
  207. case 'Author': return $this->strAuthor;
  208. case 'Comments': return $this->strComments;
  209. case 'Guid': return $this->strGuid;
  210. case 'GuidPermaLink': return $this->blnGuidPermaLink;
  211. case 'PubDate': return $this->dttPubDate;
  212. default: return parent::__get($strName);
  213. }
  214. } catch (QCallerException $objExc) {
  215. $objExc->IncrementOffset();
  216. throw $objExc;
  217. }
  218. }
  219. public function __set($strName, $mixValue) {
  220. try {
  221. switch ($strName) {
  222. case 'Title': return ($this->strTitle = QType::Cast($mixValue, QType::String));
  223. case 'Link': return ($this->strLink = QType::Cast($mixValue, QType::String));
  224. case 'Description': return ($this->strDescription = QType::Cast($mixValue, QType::String));
  225. case 'Author': return ($this->strAuthor = QType::Cast($mixValue, QType::String));
  226. case 'Comments': return ($this->strComments = QType::Cast($mixValue, QType::String));
  227. case 'Guid': return ($this->strGuid = QType::Cast($mixValue, QType::String));
  228. case 'GuidPermaLink': return ($this->blnGuidPermaLink = QType::Cast($mixValue, QType::Boolean));
  229. case 'PubDate': return ($this->dttPubDate = QType::Cast($mixValue, QType::DateTime));
  230. default: return parent::__set($strName, $mixValue);
  231. }
  232. } catch (QCallerException $objExc) {
  233. $objExc->IncrementOffset();
  234. throw $objExc;
  235. }
  236. }
  237. }
  238. ?>