/ Published in: PHP
URL: http://www.stevestreeting.com/2011/05/19/automatically-get-latest-download-link-from-sparkle-in-php/
Expand |
Embed | Plain Text
<?php // We're looking for the latest link link this: // <pubDate>Thu, 14 Oct 2010 10:34:03 +0100</pubDate> // <enclosure url="http://downloads.whatever.com/MyDownload.dmg" sparkle:version="1.0.0.5" class AppCastXMLParser { var $tag_name; var $tag_data; var $tag_prev_name; var $tag_parent_name; var $latest_date; var $use_next_url; var $download_url; function AppCastXMLParser () { $tag_name = NULL; $tag_prev_name = NULL; $tag_parent_name = NULL; $latest_date = NULL; $use_next_url = true; $download_url = NULL; } function startElement ($parser, $name, $attrs) { if ($this->tag_name != NULL) { $this->tag_parent_name = $this->tag_name; } $this->tag_name = $name; if ($name == "enclosure" && $this->use_next_url) { $this->download_url = $attrs['url']; $this->use_next_url = false; } } function endElement ($parser, $name) { if ($this->tag_name == NULL) { $this->tag_parent_name = NULL; } $this->tag_name = NULL; $this->tag_prev_name = NULL; } function characterData ($parser, $data) { if ($this->tag_name == $this->tag_prev_name) { $data = $this->tag_data[$this->tag_name] . $data; } $this->tag_data[$this->tag_name] = $data; if ($this->tag_parent_name != NULL) { $this->tag_data[$this->tag_parent_name . "." . $this->tag_name] = $data; } $this->tag_prev_name = $this->tag_name; if ($this->tag_name == "pubDate") { { if ($this->latest_date == NULL || $this->latest_date < $thisdate) { $this->use_next_url = true; $this->latest_date = $thisdate; } } } } function parse ($data) { if (!$success) { $this->tag_data['error'] = sprintf ("XML error: %s at line %d", xml_error_string(xml_get_error_code ($xml_parser)), xml_get_current_line_number ($xml_parser)); } return ($success); } function getElement ($tag) { return ($this->tag_data[$tag]); } } function getLatestDownloadURL() { $appcastparser = new AppCastXMLParser (); if ($appcastparser->parse ($appcast)) { return $appcastparser->download_url; } else { return NULL; } } $url = getLatestDownloadURL(); if ($url) { echo "<p>Thank you for downloading MyRandomApp, your download should start automatically.</p>"; echo "<p>If your download doesn't start within 5 seconds, you can use this <a href=\"$url\">direct link</a>.</p>\n"; echo <<<EXCERPT <script type="text/javascript"> <!-- function godownload() { window.location = "$url" } setTimeout("godownload()", 2000) //--> </script> EXCERPT; } ?>
You need to login to post a comment.
