<?php
// This is a simple RSS bridge type script intended to generate a basic RSS feed for Firefox releases by scraping https://mozilla.org/firefox/notes/
// TODO: Add the contents of the page to the RSS entry

if($_GET['downloadsource'] == 'yes') { // just serve the source code as plain text and exit
  header('Content-Type: text/plain');
  echo file_get_contents('feed.php');
  die();
}

// In order to reduce the load this puts on both Mozilla's servers and mine, I cache the output of this script for 1 hour. (3600 seconds)
// I could cache it for longer, say a day, but that would significantly increase my chances of updating my browser before
// the release notes appear in my RSS reader, which is simply unacceptable.
$fname = "feed_cached.xml";
$cacheperiod = 3600;
$cache_too_old = (time() - filemtime($fname) > $cacheperiod);

if($cache_too_old) {
  $url = "https://mozilla.org/firefox/notes/";
  if(function_exists('curl_init')) {
    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    $str = curl_exec($ch);
    if(curl_error($ch)) {
      echo curl_error($ch);
      die();
    }
    curl_close($ch);
  } else { // If the cURL library isn't installed, just use file_get_contents()
    $str = file_get_contents($url);
  }

  preg_match('/(<div class="c-release-version">([0-9]|\.)+<\/div>)/', $str, $rcs); // extract the element containing the version number
  preg_match('/([0-9]|\.)+/', $rcs[0], $rs);                                       // extract the version number from that
  $rel = $rs[0];

  $link = "https://www.mozilla.org/en-US/firefox/".$rel."/releasenotes"; // construct a permalink to the patch notes for the current latest release in particular

  preg_match('/(<p class="c-release-date">.+<\/p>)/', $str, $odcs);                             // extract the element containing the release date
  $origdate = str_replace('</p>', "", str_replace('<p class="c-release-date">', "", $odcs[0])); // extract the date string
  $udate = strtotime($origdate) + 43200;                                                        // convert said date string to the unix timestamp for noon GMT on that date
  $rssdate = "D, d M Y H:i:s";
  $date = date($rssdate, $udate);                                                               // convert unix timestamp to the date format RSS uses

  $updatetime = date($rssdate, filemtime($fname)); // generating some data to fill out optional elements of the RSS feed.
  $ttl = $cacheperiod / 60;                        // This is the sole reason why a few variables exist.

  $output = "<rss version=\"2.0\"><channel><title>Firefox Release Notes</title><link>https://www.mozilla.org/firefox/releases/</link><description>Release notes for new versions of Firefox, updated every $ttl minutes</description><language>en-us</language><pubDate>$date</pubDate><lastBuildDate>$updatetime</lastBuildDate><docs>https://www.rssboard.org/rss-specification</docs><generator>niceopod's Firefox release notes RSS feed bridge</generator><ttl>$ttl</ttl><image><url>https://upload.wikimedia.org/wikipedia/commons/8/84/Mozilla_Firefox_3.5_logo.png</url><title>Firefox Release Notes</title><link>https://www.mozilla.org/firefox/releases/</link></image><item><title>Firefox $rel</title><pubDate>$date</pubDate><description>Release $rel of Firefox, from $origdate</description><link>$link</link><guid>$link</guid></item></channel></rss>";
  // Outputs an RSS feed with one entry. I wanted to do the latest ten, but the releases weren't in
  // chronological order on the release list page and I couldn't be bothered trying to reorder them,
  // plus I would have had to scrape all those pages for the release date too and fuck that lol
  file_put_contents($fname, $output);
} else {
  $output = file_get_contents($fname);
}

header('Content-Type: text/xml'); // Not sure if this is necessary but it feels like good practice and it gets Chromium to display it all pretty
echo $output;