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

PHP Cross Reference of TXP stable 4.0.6

title

Body

[close]

/textpattern/publish/ -> atom.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/atom.php $
  10  $LastChangedRevision: 2774 $
  11  
  12  */
  13  
  14  
  15  // -------------------------------------------------------------
  16  	function atom()
  17      {
  18          global $thisarticle;
  19          set_error_handler('feedErrorHandler');
  20          ob_clean();
  21          extract($GLOBALS['prefs']);
  22          define("t_texthtml",' type="text/html"');
  23          define("t_text",' type="text"');
  24          define("t_html",' type="html"');
  25          define("t_xhtml",' type="xhtml"');
  26          define('t_appxhtml',' type="xhtml"');
  27          define("r_relalt",' rel="alternate"');
  28          define("r_relself",' rel="self"');
  29  
  30          extract(doSlash(gpsa(array('category','section','limit','area'))));
  31  
  32          $last = fetch('unix_timestamp(val)','txp_prefs','name','lastmod');
  33  
  34          $sitename .= ($section) ? ' - '.fetch_section_title($section) : '';
  35          $sitename .= ($category) ? ' - '.fetch_category_title($category) : '';
  36  
  37          $pub = safe_row("RealName, email", "txp_users", "privs=1");
  38  
  39          $out[] = tag(htmlspecialchars($sitename),'title',t_text);
  40          $out[] = tag(htmlspecialchars($site_slogan),'subtitle',t_text);
  41          $out[] = '<link'.r_relself.' href="'.pagelinkurl(array('atom'=>1,'area'=>$area,'section'=>$section,'category'=>$category,'limit'=>$limit)).'" />';
  42          $out[] = '<link'.r_relalt.t_texthtml.' href="'.hu.'" />';
  43          $articles = array();
  44  
  45          //Atom feeds with mail or domain name
  46          $dn = explode('/',$siteurl);
  47          $mail_or_domain = ($use_mail_on_feeds_id)? eE($blog_mail_uid):$dn[0];
  48          $out[] = tag('tag:'.$mail_or_domain.','.$blog_time_uid.':'.$blog_uid.(($section)? '/'.$section:'').(($category)? '/'.$category:''),'id');
  49  
  50          $out[] = tag('Textpattern','generator',
  51              ' uri="http://textpattern.com/" version="'.$version.'"');
  52          $out[] = tag(safe_strftime("w3cdtf",$last),'updated');
  53  
  54  
  55          $auth[] = tag($pub['RealName'],'name');
  56          $auth[] = ($include_email_atom) ? tag(eE($pub['email']),'email') : '';
  57          $auth[] = tag(hu,'uri');
  58  
  59          $out[] = tag(n.t.t.join(n.t.t,$auth).n,'author');
  60          $out[] = callback_event('atom_head');
  61  
  62          if (!$area or $area=='article') {
  63  
  64              $sfilter = ($section) ? "and Section = '".$section."'" : '';
  65              $cfilter = ($category)
  66                  ? "and (Category1='".$category."' or Category2='".$category."')":'';
  67              $limit = ($limit) ? $limit : $rss_how_many;
  68              $limit = intval(min($limit,max(100,$rss_how_many)));
  69  
  70              $frs = safe_column("name", "txp_section", "in_rss != '1'");
  71  
  72              $query = array();
  73              foreach($frs as $f) $query[] = "and Section != '".doSlash($f)."'";
  74              $query[] = $sfilter;
  75              $query[] = $cfilter;
  76  
  77              $rs = safe_rows_start(
  78                  "*,
  79                  ID as thisid,
  80                  unix_timestamp(Posted) as uPosted,
  81                  unix_timestamp(LastMod) as uLastMod",
  82                  "textpattern",
  83                  "Status=4 and Posted <= now() ".
  84                      join(' ',$query).
  85                      "order by Posted desc limit $limit"
  86              );
  87              if ($rs) {
  88                  while ($a = nextRow($rs)) {
  89  
  90                      extract($a);
  91                      populateArticleData($a);
  92                      $cb = callback_event('atom_entry');
  93                      $e = array();
  94  
  95                      $a['posted'] = $uPosted;
  96  
  97                      if ($show_comment_count_in_feed)
  98                          $count = ($comments_count > 0) ? ' ['.$comments_count.']' : '';
  99                      else $count = '';
 100  
 101                      $thisauthor = get_author_name($AuthorID);
 102  
 103                      $e['thisauthor'] = tag(n.t.t.t.tag(htmlspecialchars($thisauthor),'name').n.t.t,'author');
 104  
 105                      $e['issued'] = tag(safe_strftime('w3cdtf',$uPosted),'published');
 106                      $e['modified'] = tag(safe_strftime('w3cdtf',$uLastMod),'updated');
 107  
 108                      $escaped_title = htmlspecialchars($Title);
 109                      $e['title'] = tag($escaped_title.$count,'title',t_html);
 110  
 111                      $permlink = permlinkurl($a);
 112                      $e['link'] = '<link'.r_relalt.t_texthtml.' href="'.$permlink.'" />';
 113  
 114                      $e['id'] = tag('tag:'.$mail_or_domain.','.$feed_time.':'.$blog_uid.'/'.$uid,'id');
 115  
 116                      $e['category1'] = (trim($Category1) ? '<category term="'.htmlspecialchars($Category1).'" />' : '');
 117                      $e['category2'] = (trim($Category2) ? '<category term="'.htmlspecialchars($Category2).'" />' : '');
 118  
 119                      $summary = trim(replace_relative_urls(parse($thisarticle['excerpt']), $permlink));
 120                      $content = trim(replace_relative_urls(parse($thisarticle['body']), $permlink));
 121  
 122                      if ($syndicate_body_or_excerpt) {
 123                          # short feed: use body as summary if there's no excerpt
 124                          if (!trim($summary))
 125                              $summary = $content;
 126                          $content = '';
 127                      }
 128  
 129                      if (trim($content))
 130                          $e['content'] = tag(n.escape_cdata($content).n,'content',t_html);
 131  
 132                      if (trim($summary))
 133                          $e['summary'] = tag(n.escape_cdata($summary).n,'summary',t_html);
 134  
 135                      $articles[$ID] = tag(n.t.t.join(n.t.t,$e).n.$cb,'entry');
 136  
 137                      $etags[$ID] = strtoupper(dechex(crc32($articles[$ID])));
 138                      $dates[$ID] = $uLastMod;
 139                  }
 140              }
 141          } elseif ($area=='link') {
 142  
 143              $cfilter = ($category) ? "category='".$category."'" : '1';
 144              $limit = ($limit) ? $limit : $rss_how_many;
 145              $limit = intval(min($limit,max(100,$rss_how_many)));
 146  
 147              $rs = safe_rows_start("*", "txp_link", "$cfilter order by date desc, id desc limit $limit");
 148  
 149              if ($rs) {
 150                  while ($a = nextRow($rs)) {
 151                      extract($a);
 152  
 153                      $e['title'] = tag(htmlspecialchars($linkname),'title',t_html);
 154                      $e['content'] = tag(n.htmlspecialchars($description).n,'content',t_html);
 155  
 156                      $url = (preg_replace("/^\/(.*)/","https?://$siteurl/$1",$url));
 157                      $url = preg_replace("/&((?U).*)=/","&amp;\\1=",$url);
 158                      $e['link'] = '<link'.r_relalt.t_texthtml.' href="'.$url.'" />';
 159  
 160                      $e['issued'] = tag(safe_strftime('w3cdtf', strtotime($date)),'published');
 161                      $e['modified'] = tag(gmdate('Y-m-d\TH:i:s\Z',strtotime($date)),'updated');
 162                      $e['id'] = tag('tag:'.$mail_or_domain.','.safe_strftime( '%Y-%m-%d', strtotime( $date)).':'.$blog_uid.'/'.$id,'id');
 163  
 164                      $articles[$id] = tag(n.t.t.join(n.t.t,$e).n,'entry');
 165  
 166                      $etags[$id] = strtoupper(dechex(crc32($articles[$id])));
 167                      $dates[$id] = $date;
 168  
 169                  }
 170              }
 171          }
 172  
 173          if (!$articles) {
 174              if ($section) {
 175                  if (safe_field('name', 'txp_section', "name = '$section'") == false) {
 176                      txp_die(gTxt('404_not_found'), '404');
 177                  }
 178              } elseif ($category) {
 179                  switch ($area) {
 180                      case 'link':
 181                              if (safe_field('id', 'txp_category', "name = '$category' and type = 'link'") == false) {
 182                                  txp_die(gTxt('404_not_found'), '404');
 183                              }
 184                      break;
 185  
 186                      case 'article':
 187                      default:
 188                              if (safe_field('id', 'txp_category', "name = '$category' and type = 'article'") == false) {
 189                                  txp_die(gTxt('404_not_found'), '404');
 190                              }
 191                      break;
 192                  }
 193              }
 194          } else {
 195              //turn on compression if we aren't using it already
 196              if (extension_loaded('zlib') && ini_get("zlib.output_compression") == 0 && ini_get('output_handler') != 'ob_gzhandler' && !headers_sent()) {
 197                  // make sure notices/warnings/errors don't fudge up the feed
 198                  // when compression is used
 199                  $buf = '';
 200                  while ($b = @ob_get_clean())
 201                      $buf .= $b;
 202                  @ob_start('ob_gzhandler');
 203                  echo $buf;
 204              }
 205  
 206              handle_lastmod();
 207              $hims = serverset('HTTP_IF_MODIFIED_SINCE');
 208              $imsd = ($hims) ? strtotime($hims) : 0;
 209  
 210              if (is_callable('apache_request_headers')) {
 211                  $headers = apache_request_headers();
 212                  if (isset($headers["A-IM"])) {
 213                      $canaim = strpos($headers["A-IM"], "feed");
 214                  } else {
 215                      $canaim = false;
 216                  }
 217              } else {
 218                  $canaim = false;
 219              }
 220  
 221              $hinm = stripslashes(serverset('HTTP_IF_NONE_MATCH'));
 222  
 223              $cutarticles = false;
 224  
 225              if ($canaim !== false) {
 226                  foreach($articles as $id=>$thing) {
 227                      if (strpos($hinm, $etags[$id])) {
 228                          unset($articles[$id]);
 229                          $cutarticles = true;
 230                          $cut_etag = true;
 231                      }
 232  
 233                      if ($dates[$id] < $imsd) {
 234                          unset($articles[$id]);
 235                          $cutarticles = true;
 236                          $cut_time = true;
 237                      }
 238                  }
 239              }
 240  
 241              if (isset($cut_etag) && isset($cut_time)) {
 242                  header("Vary: If-None-Match, If-Modified-Since");
 243              } else if (isset($cut_etag)) {
 244                  header("Vary: If-None-Match");
 245              } else if (isset($cut_time)) {
 246                  header("Vary: If-Modified-Since");
 247              }
 248  
 249              $etag = @join("-",$etags);
 250  
 251              if (strstr($hinm, $etag)) {
 252                  txp_status_header('304 Not Modified');
 253                  exit(0);
 254              }
 255  
 256              if ($etag) header('ETag: "'.$etag.'"');
 257  
 258              if ($cutarticles) {
 259                  //header("HTTP/1.1 226 IM Used");
 260                  //This should be used as opposed to 200, but Apache doesn't like it.
 261                  //http://intertwingly.net/blog/2004/09/11/Vary-ETag/ says that the status code should be 200.
 262                  header("Cache-Control: no-store, im");
 263                  header("IM: feed");
 264              }
 265  
 266          }
 267  
 268          $out = array_merge($out, $articles);
 269  
 270          header('Content-type: application/atom+xml; charset=utf-8');
 271          return chr(60).'?xml version="1.0" encoding="UTF-8"?'.chr(62).n.
 272              '<feed xml:lang="'.$language.'" xmlns="http://www.w3.org/2005/Atom">'.join(n,$out).'</feed>';
 273      }
 274  
 275  
 276  // DEPRECATED FUNCTIONS
 277  // these are included only for backwards compatibility with older plugins
 278  // see the above code for more appropriate ways of handling feed content
 279  
 280  	function safe_hed($toUnicode) {
 281  
 282          if (version_compare(phpversion(), "5.0.0", ">=")) {
 283              $str =  html_entity_decode($toUnicode, ENT_QUOTES, "UTF-8");
 284          } else {
 285              $trans_tbl = get_html_translation_table(HTML_ENTITIES);
 286              foreach($trans_tbl as $k => $v) {
 287                  $ttr[$v] = utf8_encode($k);
 288              }
 289              $str = strtr($toUnicode, $ttr);
 290          }
 291          return $str;
 292      }
 293  
 294    function fixup_for_feed($toFeed, $permalink) {
 295  
 296        // fix relative urls
 297        $txt = str_replace('href="/','href="'.hu.'/',$toFeed);
 298        $txt = preg_replace("/href=\\\"#(.*)\"/","href=\"".$permalink."#\\1\"",$txt);
 299       // This was removed as entities shouldn't be stripped in Atom feeds
 300       // when the content type is html. Leaving it commented out as a reminder.
 301        //$txt = safe_hed($txt);
 302  
 303          // encode and entify
 304          $txt = preg_replace(array('/</','/>/',"/'/",'/"/'), array('&#60;','&#62;','&#039;','&#34;'), $txt);
 305          $txt = preg_replace("/&(?![#0-9]+;)/i",'&amp;', $txt);
 306       return $txt;
 307  
 308    }
 309  
 310  
 311  ?>


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