[ Index ] [ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]

PHP Cross Reference of TXP stable 4.0.6

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: http://svn.textpattern.com/releases/4.0.6/source/textpattern/publish/rss.php $
  10  $LastChangedRevision: 2793 $
  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              $rs = safe_rows_start(
  55                  "*, unix_timestamp(Posted) as uPosted, ID as thisid",
  56                  "textpattern",
  57                  "Status = 4 ".join(' ',$query).
  58                  "and Posted < now() order by Posted desc limit $limit"
  59              );
  60  
  61              if($rs) {
  62                  while ($a = nextRow($rs)) {
  63                      extract($a);
  64                      populateArticleData($a);
  65  
  66                      $cb = callback_event('rss_entry');
  67  
  68                      $a['posted'] = $uPosted;
  69  
  70                      $permlink = permlinkurl($a);
  71                      $summary = trim(replace_relative_urls(parse($thisarticle['excerpt']), $permlink));
  72                      $content = trim(replace_relative_urls(parse($thisarticle['body']), $permlink));
  73  
  74                      if ($syndicate_body_or_excerpt) {
  75                          # short feed: use body as summary if there's no excerpt
  76                          if (!trim($summary))
  77                              $summary = $content;
  78                          $content = '';
  79                      }
  80  
  81                      if ($show_comment_count_in_feed) {
  82                          $count = ($comments_count > 0) ? ' ['.$comments_count.']' : '';
  83                      } else $count = '';
  84  
  85                      $Title = htmlspecialchars(strip_tags($Title)).$count;
  86  
  87                      $thisauthor = get_author_name($AuthorID);
  88  
  89                      $item = tag($Title,'title').n.
  90                          (trim($summary) ? tag(n.escape_cdata($summary).n,'description').n : '').
  91                          (trim($content) ? tag(n.escape_cdata($content).n,'content:encoded').n : '').
  92                          tag($permlink,'link').n.
  93                          tag(safe_strftime('rfc822',$a['posted']),'pubDate').n.
  94                          tag(htmlspecialchars($thisauthor),'dc:creator').n.
  95                          tag('tag:'.$mail_or_domain.','.$feed_time.':'.$blog_uid.'/'.$uid,'guid', ' isPermaLink="false"').n.
  96                          $cb;
  97  
  98  
  99  
 100                      $articles[$ID] = tag($item,'item');
 101  
 102                      $etags[$ID] = strtoupper(dechex(crc32($articles[$ID])));
 103                      $dates[$ID] = $uPosted;
 104  
 105                  }
 106  
 107              }
 108          } elseif ($area=='link') {
 109  
 110              $cfilter = ($category) ? "category='$category'" : '1';
 111              $limit = ($limit) ? $limit : $rss_how_many;
 112              $limit = intval(min($limit,max(100,$rss_how_many)));
 113  
 114              $rs = safe_rows_start("*, unix_timestamp(date) as uDate", "txp_link", "$cfilter order by date desc limit $limit");
 115  
 116              if ($rs) {
 117                  while ($a = nextRow($rs)) {
 118                      extract($a);
 119                      $item =
 120                          tag(doSpecial($linkname),'title').n.
 121                          tag(doSpecial($description),'description').n.
 122                          tag(doSpecial($url),'link').n.
 123                          tag(safe_strftime('rfc822',$uDate),'pubDate');
 124                      $articles[$id] = tag($item,'item');
 125  
 126                      $etags[$id] = strtoupper(dechex(crc32($articles[$id])));
 127                      $dates[$id] = $date;
 128                  }
 129              }
 130          }
 131  
 132          if (!$articles) {
 133              if ($section) {
 134                  if (safe_field('name', 'txp_section', "name = '$section'") == false) {
 135                      txp_die(gTxt('404_not_found'), '404');
 136                  }
 137              } elseif ($category) {
 138                  switch ($area) {
 139                      case 'link':
 140                              if (safe_field('id', 'txp_category', "name = '$category' and type = 'link'") == false) {
 141                                  txp_die(gTxt('404_not_found'), '404');
 142                              }
 143                      break;
 144  
 145                      case 'article':
 146                      default:
 147                              if (safe_field('id', 'txp_category', "name = '$category' and type = 'article'") == false) {
 148                                  txp_die(gTxt('404_not_found'), '404');
 149                              }
 150                      break;
 151                  }
 152              }
 153          } else {
 154              //turn on compression if we aren't using it already
 155              if (extension_loaded('zlib') && ini_get("zlib.output_compression") == 0 && ini_get('output_handler') != 'ob_gzhandler' && !headers_sent()) {
 156                  // make sure notices/warnings/errors don't fudge up the feed
 157                  // when compression is used
 158                  $buf = '';
 159                  while ($b = @ob_get_clean())
 160                      $buf .= $b;
 161                  @ob_start('ob_gzhandler');
 162                  echo $buf;
 163              }
 164  
 165              handle_lastmod();
 166              $hims = serverset('HTTP_IF_MODIFIED_SINCE');
 167              $imsd = ($hims) ? strtotime($hims) : 0;
 168  
 169              if (is_callable('apache_request_headers')) {
 170                  $headers = apache_request_headers();
 171                  if (isset($headers["A-IM"])) {
 172                      $canaim = strpos($headers["A-IM"], "feed");
 173                  } else {
 174                      $canaim = false;
 175                  }
 176              } else {
 177                  $canaim = false;
 178              }
 179  
 180              $hinm = stripslashes(serverset('HTTP_IF_NONE_MATCH'));
 181  
 182              $cutarticles = false;
 183  
 184              if ($canaim !== false) {
 185                  foreach($articles as $id=>$thing) {
 186                      if (strpos($hinm, $etags[$id]) !== false) {
 187                          unset($articles[$id]);
 188                          $cutarticles = true;
 189                          $cut_etag = true;
 190                      }
 191  
 192                      if ($dates[$id] < $imsd) {
 193                          unset($articles[$id]);
 194                          $cutarticles = true;
 195                          $cut_time = true;
 196                      }
 197                  }
 198              }
 199  
 200              if (isset($cut_etag) && isset($cut_time)) {
 201                  header("Vary: If-None-Match, If-Modified-Since");
 202              } else if (isset($cut_etag)) {
 203                  header("Vary: If-None-Match");
 204              } else if (isset($cut_time)) {
 205                  header("Vary: If-Modified-Since");
 206              }
 207  
 208              $etag = @join("-",$etags);
 209  
 210              if (strstr($hinm, $etag)) {
 211                  txp_status_header('304 Not Modified');
 212                  exit(0);
 213              }
 214  
 215              if ($cutarticles) {
 216                  //header("HTTP/1.1 226 IM Used");
 217                  //This should be used as opposed to 200, but Apache doesn't like it.
 218                  //http://intertwingly.net/blog/2004/09/11/Vary-ETag/ says that the status code should be 200.
 219                  header("Cache-Control: no-store, im");
 220                  header("IM: feed");
 221              }
 222          }
 223  
 224          $out = array_merge($out, $articles);
 225  
 226          header("Content-Type: application/rss+xml; charset=utf-8");
 227          if ($etag) header('ETag: "'.$etag.'"');
 228          return
 229              '<?xml version="1.0" encoding="utf-8"?>'.n.
 230              '<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.
 231              tag(join(n,$out),'channel').n.
 232              '</rss>';
 233      }
 234  
 235  
 236  // DEPRECATED FUNCTIONS
 237  // included for backwards compatibility with older plugins only
 238  	function rss_safe_hed($toUnicode) {
 239  
 240          if (version_compare(phpversion(), "5.0.0", ">=")) {
 241              $str =  html_entity_decode($toUnicode, ENT_QUOTES, "UTF-8");
 242          } else {
 243              $trans_tbl = get_html_translation_table(HTML_ENTITIES);
 244              foreach($trans_tbl as $k => $v) {
 245                  $ttr[$v] = utf8_encode($k);
 246              }
 247              $str = strtr($toUnicode, $ttr);
 248          }
 249          return $str;
 250      }
 251  
 252  ?>


Generated: Mon Feb 18 03:42:45 2008 Cross-referenced by PHPXref 0.7