tools-update.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /* ------------------------------------------------------------------------------------
  3. * Goosle - The fast, privacy oriented search tool that just works.
  4. *
  5. * COPYRIGHT NOTICE
  6. * Copyright 2023-2024 Arnan de Gans. All Rights Reserved.
  7. *
  8. * COPYRIGHT NOTICES AND ALL THE COMMENTS SHOULD REMAIN INTACT.
  9. * By using this code you agree to indemnify Arnan de Gans from any
  10. * liability that might arise from its use.
  11. ------------------------------------------------------------------------------------ */
  12. /*--------------------------------------
  13. // Do periodic update check
  14. --------------------------------------*/
  15. function check_update() {
  16. $cache_file = ABSPATH.'cache/version.data';
  17. // Currently installed version
  18. $current_version = "1.5";
  19. if(!is_file($cache_file)) {
  20. // Create update cache file if it doesn't exist
  21. $version = array('current' => $current_version, 'latest' => '0.0', 'checked' => 0, 'url' => '');
  22. file_put_contents($cache_file, serialize($version));
  23. } else {
  24. // Get update information
  25. $version = unserialize(file_get_contents($cache_file));
  26. }
  27. // Update check, every week
  28. if($version['checked'] < time() - 604800) {
  29. $response = do_curl_request(
  30. 'https://api.github.com/repos/adegans/goosle/releases/latest', // (string) Where?
  31. array('Accept: application/json, */*;q=0.7', 'User-Agent: goosle/'.$version['current'].';'), // (array) User agent + Headers
  32. 'get', // (string) post/get
  33. null // (assoc array|null) Post body
  34. );
  35. $json_response = json_decode($response, true);
  36. // Got a response? Store it!
  37. if(!empty($json_response)) {
  38. // Update version info
  39. $version = array('current' => $version['current'], 'latest' => $json_response['tag_name'], 'checked' => time(), 'url' => $json_response['html_url']);
  40. file_put_contents($cache_file, serialize($version));
  41. }
  42. }
  43. }
  44. /*--------------------------------------
  45. // Show version in footer
  46. --------------------------------------*/
  47. function show_version() {
  48. $cache_file = ABSPATH.'cache/version.data';
  49. if(is_file($cache_file)) {
  50. // Get update information
  51. $version = unserialize(file_get_contents($cache_file));
  52. // TODO: Remove in a future version
  53. if(!isset($version['current'])) $version['current'] = "1.5";
  54. // Format current version for footer
  55. $show_version = "<a href=\"https://github.com/adegans/Goosle/\" target=\"_blank\">Goosle ".$version['current']."</a>.";
  56. // Check if a newer version is available and add it to the version display
  57. if(version_compare($version['current'], $version['latest'], '<')) {
  58. $show_version .= " <a href=\"".$version['url']."\" target=\"_blank\" class=\"update\">Version ".$version['latest']." is available!</a>";
  59. }
  60. } else {
  61. // If the update cache doesn't exist...
  62. $show_version = "<a href=\"https://github.com/adegans/Goosle/\" target=\"_blank\">Goosle</a>.";
  63. }
  64. return $show_version;
  65. }
  66. ?>