[ Index ]

PHP Cross Reference of MyBB 1.8.38

title

Body

[close]

/inc/ -> class_feedgeneration.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.8
   4   * Copyright 2014 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://www.mybb.com
   7   * License: http://www.mybb.com/about/license
   8   *
   9   */
  10  
  11  class FeedGenerator
  12  {
  13      /**
  14       * The type of feed to generate.
  15       *
  16       * @var string
  17       */
  18      public $feed_format = 'rss2.0';
  19  
  20      /**
  21       * The feed to output.
  22       *
  23       * @var string
  24       */
  25      public $feed = "";
  26  
  27      /**
  28       * Array of all of the items
  29       *
  30       * @var array
  31       */
  32      public $items = array();
  33  
  34      /**
  35       * Array of the channel information.
  36       *
  37       * @var array
  38       */
  39      public $channel = array();
  40  
  41      /**
  42       * Set the type of feed to be used.
  43       *
  44       * @param string $feed_format The feed type.
  45       */
  46  	function set_feed_format($feed_format)
  47      {
  48          if($feed_format == 'json')
  49          {
  50              $this->feed_format = 'json';
  51          }
  52          elseif($feed_format == 'atom1.0')
  53          {
  54              $this->feed_format = 'atom1.0';
  55          }
  56          else
  57          {
  58              $this->feed_format = 'rss2.0';
  59          }
  60      }
  61  
  62      /**
  63       * Sets the channel information for the RSS feed.
  64       *
  65       * @param array $channel The channel information
  66       */
  67  	function set_channel($channel)
  68      {
  69          $this->channel = $channel;
  70      }
  71  
  72      /**
  73       * Adds an item to the RSS feed.
  74       *
  75       * @param array $item The item.
  76       */
  77  	function add_item($item)
  78      {
  79          $this->items[] = $item;
  80      }
  81  
  82      /** 
  83       * Generate the feed.
  84       *
  85       */
  86  	function generate_feed()
  87      {
  88          global $lang;
  89  
  90          // First, add the feed metadata.
  91          switch($this->feed_format)
  92          {
  93              // Ouput JSON formatted feed.
  94              case "json":
  95                  $this->feed .= "{\n\t\"version\": ".json_encode('https://jsonfeed.org/version/1').",\n";
  96                  $this->feed .= "\t\"title\": \"".$this->channel['title']."\",\n";
  97                  $this->feed .= "\t\"home_page_url\": ".json_encode($this->channel['link']).",\n";
  98                  $this->feed .= "\t\"feed_url\": ".json_encode($this->channel['link']."syndication.php").",\n";
  99                  $this->feed .= "\t\"description\": ".json_encode($this->channel['description']).",\n";
 100                  $this->feed .= "\t\"items\": [\n";
 101                  $serial = 0;
 102                  break;
 103              // Ouput Atom 1.0 formatted feed.
 104              case "atom1.0":
 105                  $this->channel['date'] = gmdate("Y-m-d\TH:i:s\Z", $this->channel['date']);
 106                  $this->feed .= "<?xml version=\"1.0\" encoding=\"{$lang->settings['charset']}\"?>\n";
 107                  $this->feed .= "<feed xmlns=\"http://www.w3.org/2005/Atom\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n";
 108                  $this->feed .= "\t<title type=\"html\"><![CDATA[".$this->sanitize_content($this->channel['title'])."]]></title>\n";
 109                  $this->feed .= "\t<subtitle type=\"html\"><![CDATA[".$this->sanitize_content($this->channel['description'])."]]></subtitle>\n";
 110                  $this->feed .= "\t<link rel=\"self\" href=\"{$this->channel['link']}syndication.php\"/>\n";
 111                  $this->feed .= "\t<id>{$this->channel['link']}</id>\n";
 112                  $this->feed .= "\t<link rel=\"alternate\" type=\"text/html\" href=\"{$this->channel['link']}\"/>\n";
 113                  $this->feed .= "\t<updated>{$this->channel['date']}</updated>\n";
 114                  $this->feed .= "\t<generator uri=\"https://mybb.com\">MyBB</generator>\n";
 115                  break;
 116              // The default is the RSS 2.0 format.
 117              default:
 118                  $this->channel['date'] = gmdate("D, d M Y H:i:s O", $this->channel['date']);
 119                  $this->feed .= "<?xml version=\"1.0\" encoding=\"{$lang->settings['charset']}\"?>\n";
 120                  $this->feed .= "<rss version=\"2.0\" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n";
 121                  $this->feed .= "\t<channel>\n";
 122                  $this->feed .= "\t\t<title><![CDATA[".$this->sanitize_content($this->channel['title'])."]]></title>\n";
 123                  $this->feed .= "\t\t<link>".$this->channel['link']."</link>\n";
 124                  $this->feed .= "\t\t<description><![CDATA[".$this->sanitize_content($this->channel['description'])."]]></description>\n";
 125                  $this->feed .= "\t\t<pubDate>".$this->channel['date']."</pubDate>\n";
 126                  $this->feed .= "\t\t<generator>MyBB</generator>\n";
 127          }
 128  
 129          // Now loop through all of the items and add them to the feed.
 130          foreach($this->items as $item)
 131          {
 132              if(!$item['date'])
 133              {
 134                  $item['date'] = TIME_NOW;
 135              }
 136              switch($this->feed_format)
 137              {
 138                  // Output JSON formatted feed.
 139                  case "json":
 140                      ++$serial;
 141                      $end = $serial < count($this->items) ? "," : "";
 142                      $item_id = explode('tid=', $item['link']);
 143                      if(empty($item['updated']))
 144                      {
 145                          $item['updated'] = $item['date'];
 146                      }
 147                      $this->feed .= "\t\t{\n";
 148                      $this->feed .= "\t\t\t\"id\": \"".end($item_id)."\",\n";
 149                      $this->feed .= "\t\t\t\"url\": ".json_encode($item['link']).",\n";
 150                      $this->feed .= "\t\t\t\"title\": ".json_encode($item['title']).",\n";
 151                      if(!empty($item['author']))
 152                      {
 153                          $this->feed .= "\t\t\t\"author\": {\n\t\t\t\t\"name\": ".json_encode($item['author']['name']).",\n";
 154                          $this->feed .= "\t\t\t\t\"url\": ".json_encode($this->channel['link']."member.php?action=profile&uid=".$item['author']['uid'])."\n";
 155                          $this->feed .= "\t\t\t},\n";
 156                      }
 157                      $this->feed .= "\t\t\t\"content_html\": ".json_encode($item['description']).",\n";
 158                      $this->feed .= "\t\t\t\"date_published\": \"".date('c', $item['date'])."\",\n";
 159                      $this->feed .= "\t\t\t\"date_modified \": \"".date('c', $item['updated'])."\"\n";
 160                      $this->feed .= "\t\t}".$end."\n";
 161                      break;
 162                  // Output Atom 1.0 formatted feed.
 163                  case "atom1.0":
 164                      $item['date'] = date("Y-m-d\TH:i:s\Z", $item['date']);
 165                      $this->feed .= "\t<entry xmlns=\"http://www.w3.org/2005/Atom\">\n";
 166                      if(!empty($item['author']))
 167                      {
 168                          $author = "<a href=\"".$this->channel['link']."member.php?action=profile&uid=".$item['author']['uid']."\">".$item['author']['name']."</a>";
 169                          $this->feed .= "\t\t<author>\n";
 170                          $this->feed .= "\t\t\t<name type=\"html\" xml:space=\"preserve\"><![CDATA[".$this->sanitize_content($author)."]]></name>\n";
 171                          $this->feed .= "\t\t</author>\n";
 172                      }
 173                      $this->feed .= "\t\t<published>{$item['date']}</published>\n";
 174                      if(empty($item['updated']))
 175                      {
 176                          $item['updated'] = $item['date'];
 177                      }
 178                      else
 179                      {
 180                          $item['updated'] = date("Y-m-d\TH:i:s\Z", $item['updated']);
 181                      }
 182                      $this->feed .= "\t\t<updated>{$item['updated']}</updated>\n";
 183                      $this->feed .= "\t\t<link rel=\"alternate\" type=\"text/html\" href=\"{$item['link']}\" />\n";
 184                      $this->feed .= "\t\t<id>{$item['link']}</id>\n";
 185                      $this->feed .= "\t\t<title xml:space=\"preserve\"><![CDATA[".$this->sanitize_content($item['title'])."]]></title>\n";
 186                      $this->feed .= "\t\t<content type=\"html\" xml:space=\"preserve\" xml:base=\"{$item['link']}\"><![CDATA[".$this->sanitize_content($item['description'])."]]></content>\n";
 187                      $this->feed .= "\t\t<draft xmlns=\"http://purl.org/atom-blog/ns#\">false</draft>\n";
 188                      $this->feed .= "\t</entry>\n";
 189                      break;
 190  
 191                  // The default is the RSS 2.0 format.
 192                  default:
 193                      $item['date'] = date("D, d M Y H:i:s O", $item['date']);
 194                      $this->feed .= "\t\t<item>\n";
 195                      $this->feed .= "\t\t\t<title><![CDATA[".$this->sanitize_content($item['title'])."]]></title>\n";
 196                      $this->feed .= "\t\t\t<link>".$item['link']."</link>\n";
 197                      $this->feed .= "\t\t\t<pubDate>".$item['date']."</pubDate>\n";
 198                      if(!empty($item['author']))
 199                      {
 200                          $author = "<a href=\"".$this->channel['link']."member.php?action=profile&uid=".$item['author']['uid']."\">".$item['author']['name']."</a>";
 201                          $this->feed .= "\t\t\t<dc:creator><![CDATA[".$this->sanitize_content($author)."]]></dc:creator>\n";
 202                      }
 203                      $this->feed .= "\t\t\t<guid isPermaLink=\"false\">".$item['link']."</guid>\n";
 204                      $this->feed .= "\t\t\t<description><![CDATA[".$item['description']."]]></description>\n";
 205                      $this->feed .= "\t\t\t<content:encoded><![CDATA[".$item['description']."]]></content:encoded>\n";
 206                      $this->feed .= "\t\t</item>\n";
 207                      break;
 208              }
 209          }
 210  
 211          // Now, neatly end the feed.
 212          switch($this->feed_format)
 213          {
 214              case "json":
 215                  $this->feed .= "\t]\n}";
 216                  break;
 217              case "atom1.0":
 218                  $this->feed .= "</feed>";
 219                  break;
 220              default:
 221                  $this->feed .= "\t</channel>\n";
 222                  $this->feed .= "</rss>";
 223          }
 224      }
 225  
 226      /**
 227       * Sanitize content suitable for RSS feeds.
 228       *
 229       * @param  string $string The string we wish to sanitize.
 230       * @return string The cleaned string.
 231       */
 232  	function sanitize_content($content)
 233      {
 234          $content = preg_replace("#&[^\s]([^\#])(?![a-z1-4]{1,10});#i", "&#x26;$1", $content);
 235          $content = str_replace("]]>", "]]]]><![CDATA[>", $content);
 236  
 237          return $content;
 238      }
 239  
 240      /**
 241      * Output the feed.
 242      */
 243  	function output_feed()
 244      {
 245          global $lang;
 246          // Send an appropriate header to the browser.
 247          switch($this->feed_format)
 248          {
 249              case "json":
 250                  header("Content-Type: application/json; charset=\"{$lang->settings['charset']}\"");
 251                  break;
 252              case "atom1.0":
 253                  header("Content-Type: application/atom+xml; charset=\"{$lang->settings['charset']}\"");
 254                  break;
 255              default:
 256                  header("Content-Type: text/xml; charset=\"{$lang->settings['charset']}\"");
 257          }
 258  
 259          // If the feed hasn't been generated, do so.
 260          if($this->feed)
 261          {
 262              echo $this->feed;
 263          }
 264          else
 265          {
 266              $this->generate_feed();
 267              echo $this->feed;
 268          }
 269      }
 270  }


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref