version_check.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * Version upgrade path manager for SyncTrayzor
  4. *
  5. * Clients request this with their current version, arch, and variant (portable, etc)
  6. * and this gives them a version to upgrade to (if any), along with the method of
  7. * upgrading to it (manual navigation to Github release page, automatic silent upgrade,
  8. * etc).
  9. *
  10. * $versions is a record of all of the current releases, which we might want to upgrade
  11. * people to. It has the structure:
  12. * [
  13. * version => [
  14. * variant => [
  15. * 'url' => [
  16. * arch => 'url',
  17. * ...
  18. * ],
  19. * ],
  20. * ...
  21. * 'release_notes' => release_notes,
  22. * ],
  23. * ...
  24. * ]
  25. *
  26. * version: version string e.g. '1.2.3'
  27. * variant: e.g. 'portable', 'installed'. Matched against the variant provided by the
  28. * client, or '*' can be used to specify a default.
  29. * arch: e.g. 'x86', 'x64'. Matched against the arch provided by the client, or '*'
  30. * can used to specify a default.
  31. * release_notes: Release notes to display to the user.
  32. *
  33. * $upgrades is a map of old_version => new_version, and specifies the formatter to
  34. * use to communicate with old_version. It also allows various overrides to be
  35. * specified (e.g. release notes)
  36. * It has the structure:
  37. * [
  38. * old_version => ['to' => new_version, 'formatter' => formatter_version, 'overrides' => [overrides]],
  39. * ...
  40. * ]
  41. *
  42. * old_version: version being upgraded from
  43. * new_version: version to upgrade to
  44. * formatter_version: formatter version to use (in $response_formatters)
  45. * overrides: optional overrides, used by the formatter
  46. */
  47. set_error_handler('error_handler');
  48. date_default_timezone_set('UCT');
  49. header('Content-Type: application/json');
  50. function error_handler($severity, $message, $filename, $lineno)
  51. {
  52. throw new ErrorException($message, 0, $severity, $filename, $lineno);
  53. }
  54. function get_with_wildcard($src, $value, $default = null)
  55. {
  56. if (isset($src[$value]))
  57. return $src[$value];
  58. if (isset($src['*']))
  59. return $src['*'];
  60. return $default;
  61. }
  62. $base = "https://synctrayzor.antonymale.co.uk/download";
  63. $versions = [
  64. '1.1.20' => [
  65. 'installed' => [
  66. 'direct_download_url' => [
  67. 'x64' => "$base/v{version}/SyncTrayzorSetup-x64.exe",
  68. 'x86' => "$base/v{version}/SyncTrayzorSetup-x86.exe",
  69. ],
  70. ],
  71. 'portable' => [
  72. 'direct_download_url' => [
  73. 'x64' => "$base/v{version}/SyncTrayzorPortable-x64.zip",
  74. 'x86' => "$base/v{version}/SyncTrayzorPortable-x86.zip",
  75. ],
  76. ],
  77. 'sha1sum_download_url' => "$base/v{version}/sha1sum.txt.asc",
  78. 'sha512sum_download_url' => "$base/v{version}/sha512sum.txt.asc",
  79. 'release_page_url' => 'https://github.com/canton7/SyncTrayzor/releases/tag/v{version}',
  80. 'release_notes' => "!!!!!\nYou must upgrade to v1.1.20 now, otherwise auto-upgrades will stop working!\n!!!!!\n\n- Fix to allow SyncTrayzor to download updates from GitHub, after they made changes\n- Disable the built-in filesystem watcher if Syncthing's watcher is enabled\n- Retry starting Syncthing if it crashed (#421)\n- Make it clearer that multiple items in the Conflict Resolver window can be selected at once (#359)\n- Expose the setting to change the location of syncthing.exe (#386)\n- Fix issue where \"Syncthing is starting\" would appear forever (#420)\n- Fix crash when pausing/unpausing devices (#435)\n- Fix a rare condition when trying to enable auto-start would crash (#407)\n- Remove support for debug facilities, since Syncthing has this built in",
  81. ]
  82. ];
  83. $upgrades = [
  84. '1.1.19' => ['to' => 'latest', 'formatter' => '5'],
  85. '1.1.18' => ['to' => 'latest', 'formatter' => '5'],
  86. '1.1.17' => ['to' => 'latest', 'formatter' => '5'],
  87. '1.1.16' => ['to' => 'latest', 'formatter' => '5'],
  88. '1.1.15' => ['to' => 'latest', 'formatter' => '5'],
  89. '1.1.14' => ['to' => 'latest', 'formatter' => '5'],
  90. '1.1.13' => ['to' => 'latest', 'formatter' => '5'],
  91. '1.1.12' => ['to' => 'latest', 'formatter' => '5'],
  92. '1.1.11' => ['to' => 'latest', 'formatter' => '5'],
  93. '1.1.10' => ['to' => 'latest', 'formatter' => '5'],
  94. '1.1.9' => ['to' => 'latest', 'formatter' => '5'],
  95. '1.1.8' => ['to' => 'latest', 'formatter' => '5'],
  96. '1.1.7' => ['to' => 'latest', 'formatter' => '4'],
  97. '1.1.6' => ['to' => 'latest', 'formatter' => '4'],
  98. '1.1.5' => ['to' => 'latest', 'formatter' => '4'],
  99. '1.1.4' => ['to' => 'latest', 'formatter' => '4'],
  100. '1.1.3' => ['to' => 'latest', 'formatter' => '4'],
  101. '1.1.2' => ['to' => 'latest', 'formatter' => '4'],
  102. '1.1.1' => ['to' => 'latest', 'formatter' => '3'],
  103. '1.1.0' => ['to' => 'latest', 'formatter' => '3'],
  104. '1.0.32' => ['to' => 'latest', 'formatter' => '3'],
  105. '1.0.31' => ['to' => 'latest', 'formatter' => '3'],
  106. '1.0.30' => ['to' => 'latest', 'formatter' => '3'],
  107. '1.0.29' => ['to' => 'latest', 'formatter' => '3'],
  108. '1.0.28' => ['to' => 'latest', 'formatter' => '3'],
  109. '1.0.27' => ['to' => 'latest', 'formatter' => '3'],
  110. '1.0.26' => ['to' => 'latest', 'formatter' => '3'],
  111. '1.0.25' => ['to' => 'latest', 'formatter' => '3'],
  112. '1.0.24' => ['to' => 'latest', 'formatter' => '3'],
  113. '1.0.23' => ['to' => 'latest', 'formatter' => '3'],
  114. '1.0.22' => ['to' => 'latest', 'formatter' => '2'],
  115. '1.0.21' => ['to' => 'latest', 'formatter' => '2'],
  116. '1.0.20' => ['to' => 'latest', 'formatter' => '2'],
  117. // 1.0.19 was never actually released, so no need to represent it
  118. '1.0.18' => ['to' => 'latest', 'formatter' => '2'],
  119. '1.0.17' => ['to' => 'latest', 'formatter' => '2'],
  120. '1.0.16' => ['to' => 'latest', 'formatter' => '2'],
  121. '1.0.15' => ['to' => 'latest', 'formatter' => '2'],
  122. '1.0.14' => ['to' => 'latest', 'formatter' => '2'],
  123. '1.0.13' => ['to' => 'latest', 'formatter' => '1'],
  124. '1.0.12' => ['to' => 'latest', 'formatter' => '1'],
  125. ];
  126. $response_formatters = [
  127. // 1.0.12 and 1.0.13 shouldn't download installers directly, as they doesn't know how to run them properly
  128. '1' => function($arch, $variant, $to_version, $to_version_info, $overrides)
  129. {
  130. $data = [
  131. 'version' => $to_version,
  132. 'direct_download_url' => null,
  133. 'release_page_url' => $to_version_info['release_page_url'],
  134. 'release_notes' => isset($overrides['release_notes']) ? $overrides['release_notes'] : $to_version_info['release_notes'],
  135. ];
  136. return $data;
  137. },
  138. // Prior to sha1sum_download_url
  139. '2' => function($arch, $variant, $to_version, $to_version_info, $overrides)
  140. {
  141. $variant_info = isset($overrides[$variant]) ? get_with_wildcard($overrides, $variant) : get_with_wildcard($to_version_info, $variant);
  142. $data = [
  143. 'version' => $to_version,
  144. 'release_page_url' => $to_version_info['release_page_url'],
  145. 'release_notes' => isset($overrides['release_notes']) ? $overrides['release_notes'] : $to_version_info['release_notes'],
  146. ];
  147. if ($variant == 'installed')
  148. {
  149. $data['direct_download_url'] = get_with_wildcard($variant_info['direct_download_url'], $arch);
  150. }
  151. return $data;
  152. },
  153. // Portable versions don't know how to handle directl downloads (or it's broken...)
  154. '3' => function($arch, $variant, $to_version, $to_version_info, $overrides)
  155. {
  156. $variant_info = isset($overrides[$variant]) ? get_with_wildcard($overrides, $variant) : get_with_wildcard($to_version_info, $variant);
  157. $data = [
  158. 'version' => $to_version,
  159. 'release_page_url' => $to_version_info['release_page_url'],
  160. 'release_notes' => isset($overrides['release_notes']) ? $overrides['release_notes'] : $to_version_info['release_notes'],
  161. ];
  162. if ($variant == 'installed')
  163. {
  164. $data['direct_download_url'] = get_with_wildcard($variant_info['direct_download_url'], $arch);
  165. $data['sha1sum_download_url'] = $to_version_info['sha1sum_download_url'];
  166. }
  167. return $data;
  168. },
  169. '4' => function($arch, $variant, $to_version, $to_version_info, $overrides)
  170. {
  171. $variant_info = isset($overrides[$variant]) ? get_with_wildcard($overrides, $variant) : get_with_wildcard($to_version_info, $variant);
  172. $data = [
  173. 'version' => $to_version,
  174. 'direct_download_url' => get_with_wildcard($variant_info['direct_download_url'], $arch),
  175. 'sha1sum_download_url' => $to_version_info['sha1sum_download_url'],
  176. 'release_page_url' => $to_version_info['release_page_url'],
  177. 'release_notes' => isset($overrides['release_notes']) ? $overrides['release_notes'] : $to_version_info['release_notes'],
  178. ];
  179. return $data;
  180. },
  181. // Learnt about sha512sum
  182. '5' => function($arch, $variant, $to_version, $to_version_info, $overrides)
  183. {
  184. $variant_info = isset($overrides[$variant]) ? get_with_wildcard($overrides, $variant) : get_with_wildcard($to_version_info, $variant);
  185. $data = [
  186. 'version' => $to_version,
  187. 'direct_download_url' => get_with_wildcard($variant_info['direct_download_url'], $arch),
  188. 'sha512sum_download_url' => $to_version_info['sha512sum_download_url'],
  189. 'release_page_url' => $to_version_info['release_page_url'],
  190. 'release_notes' => isset($overrides['release_notes']) ? $overrides['release_notes'] : $to_version_info['release_notes'],
  191. ];
  192. return $data;
  193. },
  194. ];
  195. $error = null;
  196. $loggable_error = null;
  197. $data = null;
  198. try
  199. {
  200. $version = isset($_GET['version']) ? $_GET['version'] : null;
  201. $arch = isset($_GET['arch']) ? $_GET['arch'] : null;
  202. $variant = isset($_GET['variant']) ? $_GET['variant'] : null;
  203. if (empty($version) || empty($arch) || empty($variant))
  204. {
  205. $error = ['code' => 1, 'message' => 'version, arch, or variant not specified'];
  206. }
  207. else if (isset($upgrades[$version]))
  208. {
  209. $to_version = $upgrades[$version]['to'];
  210. if ($to_version == 'latest')
  211. $to_version = array_keys($versions)[0];
  212. $formatter = $response_formatters[$upgrades[$version]['formatter']];
  213. $overrides = isset($upgrades[$version]['overrides']) ? $upgrades[$version]['overrides'] : [];
  214. array_walk_recursive($versions[$to_version], function(&$value, $key) use ($to_version) {
  215. $value = str_replace('{version}', $to_version, $value);
  216. });
  217. $to_version_info = $versions[$to_version];
  218. $data = $formatter($arch, $variant, $to_version, $to_version_info, $overrides);
  219. }
  220. }
  221. catch (Exception $e)
  222. {
  223. $error = ['code' => 2, 'message' => 'Unhandled error. Please try again later'];
  224. $loggable_error = $e->getMessage() . "\n" . $e->getTraceAsString();
  225. }
  226. $rsp = [];
  227. if ($data != null)
  228. $rsp['data'] = $data;
  229. if ($error != null)
  230. $rsp['error'] = $error;
  231. $output = json_encode($rsp, JSON_UNESCAPED_SLASHES | JSON_FORCE_OBJECT);
  232. $date = date('c');
  233. $fp = fopen('log.txt', 'a+');
  234. flock($fp, LOCK_EX);
  235. fputcsv($fp, [$date, $_SERVER['REMOTE_ADDR'], $version, $arch, $variant, $data == null ? "N" : "Y", $loggable_error]);
  236. fclose($fp);
  237. echo $output;