[ Index ]

PHP Cross Reference of Textpattern 4.0.8

title

Body

[close]

/textpattern/publish/ -> rss.php (source)

   1  <?php
   2  
   3  /*
   4      This is Textpattern
   5      Copyright 2005 by Dean Allen - all rights reserved.
   6  
   7      Use of this software denotes acceptance of the Textpattern license agreement
   8  
   9  $HeadURL: https://textpattern.googlecode.com/svn/releases/4.0.8/source/textpattern/publish/rss.php $
  10  $LastChangedRevision: 3029 $
  11  
  12  */
  13  
  14  
  15  // -------------------------------------------------------------
  16  	function rss()
  17      {
  18          global $prefs,$thisarticle;
  19          set_error_handler('feedErrorHandler');
  20          ob_clean();
  21          extract($prefs);
  22  
  23          extract(doSlash(gpsa(array('category','section','limit','area'))));
  24  
  25          $sitename .= ($section) ? ' - '.fetch_section_title($section) : '';
  26          $sitename .= ($category) ? ' - '.fetch_category_title($category) : '';
  27          $dn = explode('/',$siteurl);
  28          $mail_or_domain = ($use_mail_on_feeds_id)? eE($blog_mail_uid):$dn[0];
  29  
  30          $out[] = tag('http://textpattern.com/?v='.$version, 'generator');
  31          $out[] = tag(doSpecial($sitename),'title');
  32          $out[] = tag(hu,'link');
  33          $out[] = '<atom:link href="'.pagelinkurl(array('rss'=>1,'area'=>$area,'section'=>$section,'category'=>$category,'limit'=>$limit)).'" rel="self" type="application/rss+xml" />';
  34          $out[] = tag(doSpecial($site_slogan),'description');
  35          $last = fetch('unix_timestamp(val)','txp_prefs','name','lastmod');
  36          $out[] = tag(safe_strftime('rfc822',$last),'pubDate');
  37          $out[] = callback_event('rss_head');
  38  
  39          $articles = array();
  40  
  41          if (!$area or $area=='article') {
  42  
  43              $sfilter = ($section) ? "and Section = '".$section."'" : '';
  44              $cfilter = ($category)
  45                  ? "and (Category1='".$category."' or Category2='".$category."')":'';
  46              $limit = ($limit) ? $limit : $rss_how_many;
  47              $limit = intval(min($limit,max(100,$rss_how_many)));
  48  
  49              $frs = safe_column("name", "txp_section", "in_rss != '1'");
  50              if ($frs) foreach($frs as $f) $query[] = "and Section != '".doSlash($f)."'";
  51              $query[] = $sfilter;
  52              $query[] = $cfilter;
  53  
  54              $expired = ($publish_expired_articles) ? '' : ' and (now() <= Expires or Expires = '.NULLDATETIME.') ';
  55              $rs = safe_rows_start(
  56                  "*, unix_timestamp(Posted) as uPosted, unix_timestamp(LastMod) as uLastMod, unix_timestamp(Expires) as uExpires, ID as thisid",
  57                  "textpattern",
  58                  "Status = 4 ".join(' ',$query).
  59                  "and Posted < now()".$expired."order by Posted desc limit $limit"
  60              );
  61  
  62              if($rs) {
  63                  while ($a = nextRow($rs)) {
  64                      extract($a);
  65                      populateArticleData($a);
  66  
  67                      $cb = callback_event('rss_entry');
  68  
  69                      $a['posted'] = $uPosted;
  70  
  71                      $permlink = permlinkurl($a);
  72                      $summary = trim(replace_relative_urls(parse($thisarticle['excerpt']), $permlink));
  73                      $content = trim(replace_relative_urls(parse($thisarticle['body']), $permlink));
  74  
  75                      if ($syndicate_body_or_excerpt) {
  76                          # short feed: use body as summary if there's no excerpt
  77                          if (!trim($summary))
  78                              $summary = $content;
  79                          $content = '';
  80                      }
  81  
  82                      if ($show_comment_count_in_feed) {
  83                          $count = ($comments_count > 0) ? ' ['.$comments_count.']' : '';
  84                      } else $count = '';
  85  
  86                      $Title = escape_title(strip_tags($Title)).$count;
  87  
  88                      $thisauthor = get_author_name($AuthorID);
  89  
  90                      $item = tag($Title,'title').n.
  91                          (trim($summary) ? tag(n.escape_cdata($summary).n,'description').n : '').
  92                          (trim($content) ? tag(n.escape_cdata($content).n,'content:encoded').n : '').
  93                          tag($permlink,'link').n.
  94                          tag(safe_strftime('rfc822',$a['posted']),'pubDate').n.
  95                          tag(htmlspecialchars($thisauthor),'dc:creator').n.
  96                          tag('tag:'.$mail_or_domain.','.$feed_time.':'.$blog_uid.'/'.$uid,'guid', ' isPermaLink="false"').n.
  97                          $cb;
  98  
  99  
 100  
 101                      $articles[$ID] = tag($item,'item');
 102  
 103                      $etags[$ID] = strtoupper(dechex(crc32($articles[$ID])));
 104                      $dates[$ID] = $uPosted;
 105  
 106                  }
 107  
 108              }
 109          } elseif ($area=='link') {
 110  
 111              $cfilter = ($category) ? "category='$category'" : '1';
 112              $limit = ($limit) ? $limit : $rss_how_many;
 113              $limit = intval(min($limit,max(100,$rss_how_many)));
 114  
 115              $rs = safe_rows_start("*, unix_timestamp(date) as uDate", "txp_link", "$cfilter order by date desc limit $limit");
 116  
 117              if ($rs) {
 118                  while ($a = nextRow($rs)) {
 119                      extract($a);
 120                      $item =
 121                          tag(doSpecial($linkname),'title').n.
 122                          tag(doSpecial($description),'description').n.
 123                          tag(doSpecial($url),'link').n.
 124                          tag(safe_strftime('rfc822',$uDate),'pubDate');
 125                      $articles[$id] = tag($item,'item');
 126  
 127                      $etags[$id] = strtoupper(dechex(crc32($articles[$id])));
 128                      $dates[$id] = $date;
 129                  }
 130              }
 131          }
 132  
 133          if (!$articles) {
 134              if ($section) {
 135                  if (safe_field('name', 'txp_section', "name = '$section'") == false) {
 136                      txp_die(gTxt('404_not_found'), '404');
 137                  }
 138              } elseif ($category) {
 139                  switch ($area) {
 140                      case 'link':
 141                              if (safe_field('id', 'txp_category', "name = '$category' and type = 'link'") == false) {
 142                                  txp_die(gTxt('404_not_found'), '404');
 143                              }
 144                      break;
 145  
 146                      case 'article':
 147                      default:
 148                              if (safe_field('id', 'txp_category', "name = '$category' and type = 'article'") == false) {
 149                                  txp_die(gTxt('404_not_found'), '404');
 150                              }
 151                      break;
 152                  }
 153              }
 154          } else {
 155              //turn on compression if we aren't using it already
 156              if (extension_loaded('zlib') && ini_get("zlib.output_compression") == 0 && ini_get('output_handler') != 'ob_gzhandler' && !headers_sent()) {
 157                  // make sure notices/warnings/errors don't fudge up the feed
 158                  // when compression is used
 159                  $buf = '';
 160                  while ($b = @ob_get_clean())
 161                      $buf .= $b;
 162                  @ob_start('ob_gzhandler');
 163                  echo $buf;
 164              }
 165  
 166              handle_lastmod();
 167              $hims = serverset('HTTP_IF_MODIFIED_SINCE');
 168              $imsd = ($hims) ? strtotime($hims) : 0;
 169  
 170              if (is_callable('apache_request_headers')) {
 171                  $headers = apache_request_headers();
 172                  if (isset($headers["A-IM"])) {
 173                      $canaim = strpos($headers["A-IM"], "feed");
 174                  } else {
 175                      $canaim = false;
 176                  }
 177              } else {
 178                  $canaim = false;
 179              }
 180  
 181              $hinm = stripslashes(serverset('HTTP_IF_NONE_MATCH'));
 182  
 183              $cutarticles = false;
 184  
 185              if ($canaim !== false) {
 186                  foreach($articles as $id=>$thing) {
 187                      if (strpos($hinm, $etags[$id]) !== false) {
 188                          unset($articles[$id]);
 189                          $cutarticles = true;
 190                          $cut_etag = true;
 191                      }
 192  
 193                      if ($dates[$id] < $imsd) {
 194                          unset($articles[$id]);
 195                          $cutarticles = true;
 196                          $cut_time = true;
 197                      }
 198                  }
 199              }
 200  
 201              if (isset($cut_etag) && isset($cut_time)) {
 202                  header("Vary: If-None-Match, If-Modified-Since");
 203              } else if (isset($cut_etag)) {
 204                  header("Vary: If-None-Match");
 205              } else if (isset($cut_time)) {
 206                  header("Vary: If-Modified-Since");
 207              }
 208  
 209              $etag = @join("-",$etags);
 210  
 211              if (strstr($hinm, $etag)) {
 212                  txp_status_header('304 Not Modified');
 213                  exit(0);
 214              }
 215  
 216              if ($cutarticles) {
 217                  //header("HTTP/1.1 226 IM Used");
 218                  //This should be used as opposed to 200, but Apache doesn't like it.
 219                  //http://intertwingly.net/blog/2004/09/11/Vary-ETag/ says that the status code should be 200.
 220                  header("Cache-Control: no-store, im");
 221                  header("IM: feed");
 222              }
 223          }
 224  
 225          $out = array_merge($out, $articles);
 226  
 227          header("Content-Type: application/rss+xml; charset=utf-8");
 228          if (isset($etag)) header('ETag: "'.$etag.'"');
 229          return
 230              '<?xml version="1.0" encoding="utf-8"?>'.n.
 231              '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom">'.n.
 232              tag(join(n,$out),'channel').n.
 233              '</rss>';
 234      }
 235  
 236  
 237  // DEPRECATED FUNCTIONS
 238  // included for backwards compatibility with older plugins only
 239  	function rss_safe_hed($toUnicode) {
 240  
 241          if (version_compare(phpversion(), "5.0.0", ">=")) {
 242              $str =  html_entity_decode($toUnicode, ENT_QUOTES, "UTF-8");
 243          } else {
 244              $trans_tbl = get_html_translation_table(HTML_ENTITIES);
 245              foreach($trans_tbl as $k => $v) {
 246                  $ttr[$v] = utf8_encode($k);
 247              }
 248              $str = strtr($toUnicode, $ttr);
 249          }
 250          return $str;
 251      }
 252  
 253  ?>


Generated: Thu May 21 23:03:01 2009 Cross-referenced by PHPXref 0.7