Posted By

shadevampire on 05/31/11


Tagged

get link download from automatically latest Sparkle


Versions (?)

Automatically get latest download link from Sparkle in PHP


 / Published in: PHP
 

URL: http://www.stevestreeting.com/2011/05/19/automatically-get-latest-download-link-from-sparkle-in-php/

  1. <?php
  2.  
  3. // We're looking for the latest link link this:
  4. // <pubDate>Thu, 14 Oct 2010 10:34:03 +0100</pubDate>
  5. // <enclosure url="http://downloads.whatever.com/MyDownload.dmg" sparkle:version="1.0.0.5"
  6.  
  7. class AppCastXMLParser
  8. {
  9. var $tag_name;
  10. var $tag_data;
  11. var $tag_prev_name;
  12. var $tag_parent_name;
  13. var $latest_date;
  14. var $use_next_url;
  15. var $download_url;
  16. function AppCastXMLParser ()
  17. {
  18. $tag_name = NULL;
  19. $tag_data = array ();
  20. $tag_prev_name = NULL;
  21. $tag_parent_name = NULL;
  22.  
  23. $latest_date = NULL;
  24. $use_next_url = true;
  25. $download_url = NULL;
  26. }
  27. function startElement ($parser, $name, $attrs)
  28. {
  29. if ($this->tag_name != NULL)
  30. {
  31. $this->tag_parent_name = $this->tag_name;
  32. }
  33. $this->tag_name = $name;
  34.  
  35. if ($name == "enclosure" && $this->use_next_url)
  36. {
  37. $this->download_url = $attrs['url'];
  38. $this->use_next_url = false;
  39. }
  40.  
  41. }
  42. function endElement ($parser, $name)
  43. {
  44. if ($this->tag_name == NULL)
  45. {
  46. $this->tag_parent_name = NULL;
  47. }
  48. $this->tag_name = NULL;
  49. $this->tag_prev_name = NULL;
  50. }
  51. function characterData ($parser, $data)
  52. {
  53. if ($this->tag_name == $this->tag_prev_name)
  54. {
  55. $data = $this->tag_data[$this->tag_name] . $data;
  56. }
  57. $this->tag_data[$this->tag_name] = $data;
  58. if ($this->tag_parent_name != NULL)
  59. {
  60. $this->tag_data[$this->tag_parent_name . "." . $this->tag_name] = $data;
  61. }
  62. $this->tag_prev_name = $this->tag_name;
  63.  
  64. if ($this->tag_name == "pubDate")
  65. {
  66. if ($thisdate = strtotime($this->tag_data[$this->tag_name]))
  67. {
  68. if ($this->latest_date == NULL || $this->latest_date < $thisdate)
  69. {
  70. $this->use_next_url = true;
  71. $this->latest_date = $thisdate;
  72. }
  73. }
  74. }
  75.  
  76. }
  77. function parse ($data)
  78. {
  79. $xml_parser = xml_parser_create ();
  80. xml_set_object ($xml_parser, $this);
  81. xml_parser_set_option ($xml_parser, XML_OPTION_CASE_FOLDING, false);
  82. xml_set_element_handler ($xml_parser, "startElement", "endElement");
  83. xml_set_character_data_handler ($xml_parser, "characterData");
  84. $success = xml_parse ($xml_parser, $data, true);
  85. if (!$success)
  86. {
  87. $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));
  88. }
  89. xml_parser_free ($xml_parser);
  90. return ($success);
  91. }
  92. function getElement ($tag)
  93. {
  94. return ($this->tag_data[$tag]);
  95. }
  96. }
  97.  
  98. function getLatestDownloadURL()
  99. {
  100.  
  101. $appcastparser = new AppCastXMLParser ();
  102.  
  103. $appcast = $contents=file_get_contents("http://www.myapp.com/path/to/SparkleAppcast.xml");
  104.  
  105. if ($appcastparser->parse ($appcast))
  106. {
  107. return $appcastparser->download_url;
  108. }
  109. else
  110. {
  111. return NULL;
  112. }
  113. }
  114.  
  115. $url = getLatestDownloadURL();
  116.  
  117. if ($url)
  118. {
  119. echo "<p>Thank you for downloading MyRandomApp, your download should start automatically.</p>";
  120. echo "<p>If your download doesn't start within 5 seconds, you can use this <a href=\"$url\">direct link</a>.</p>\n";
  121.  
  122. echo <<<EXCERPT
  123. <script type="text/javascript">
  124. <!--
  125. function godownload()
  126. {
  127. window.location = "$url"
  128. }
  129. setTimeout("godownload()", 2000)
  130. //-->
  131. </script>
  132. EXCERPT;
  133.  
  134. }
  135.  
  136. ?>

Report this snippet  

You need to login to post a comment.