Bearsampp 2026.9.23
Loading...
Searching...
No Matches
class.action.checkVersion.php
Go to the documentation of this file.
1<?php
2/*
3 *
4 * * Copyright (c) 2022-2025 Bearsampp
5 * * License: GNU General Public License version 3 or later; see LICENSE.txt
6 * * Website: https://bearsampp.com
7 * * Github: https://github.com/Bearsampp
8 *
9 */
10
21{
23 const DISPLAY_OK = 'displayOk';
24
26 private $wbWindow;
28 private $wbImage;
32 private $wbLinkFull;
34 private $wbBtnOk;
35
43 private $didStartLoading = false;
44
50 public function __construct($args)
51 {
52 global $bearsamppCore, $bearsamppLang, $bearsamppWinbinder, $appGithubHeader;
53
54 // Check if we're being called from the version check menu item
55 $isMenuCheck = !empty($args[0]) && $args[0] == self::DISPLAY_OK;
56
57 // Start loading only if the exec file doesn't exist or if we're doing a menu check
58 if (!file_exists($bearsamppCore->getExec()) || $isMenuCheck) {
59 if ($isMenuCheck) {
60 Log::debug('ActionCheckVersion: Manual check detected, starting loading');
62 $this->didStartLoading = true;
63 }
64 $this->currentVersion = $bearsamppCore->getAppVersion();
65 Log::debug('ActionCheckVersion: Current version: ' . $this->currentVersion);
66
67 // Assuming getLatestVersion now returns an array with version and URL
69 Log::trace('ActionCheckVersion: GitHub version data: ' . var_export($githubVersionData, true));
70
71 if ($githubVersionData != null && isset($githubVersionData['version'], $githubVersionData['html_url'])) {
73 $this->githubLatestVersionUrl = $githubVersionData['html_url']; // URL of the latest version
74 Log::debug('ActionCheckVersion: GitHub latest version: ' . $githubLatestVersion);
75 if (version_compare($this->currentVersion, $githubLatestVersion, '<')) {
76 Log::debug('ActionCheckVersion: Update available, showing update window');
77 $this->showVersionUpdateWindow($bearsamppLang, $bearsamppWinbinder, $bearsamppCore, $githubLatestVersion);
78 } elseif ($isMenuCheck) {
79 Log::debug('ActionCheckVersion: Version is up to date, showing OK message box');
80 $this->showVersionOkMessageBox($bearsamppLang, $bearsamppWinbinder);
81 } else {
82 Log::debug('ActionCheckVersion: Version is up to date (background check)');
83 if ($this->didStartLoading) {
85 }
86 }
87 } elseif ($isMenuCheck) {
88 Log::debug('ActionCheckVersion: Failed to retrieve version data during manual check');
89 // If it's a menu check but we couldn't get version data, we must stop loading
90 if ($this->didStartLoading) {
92 }
93 Log::debug('ActionCheckVersion: Showing error message box');
94 $bearsamppWinbinder->messageBoxError(
95 'Failed to retrieve version data from GitHub during manual check.',
97 );
98 Log::debug('ActionCheckVersion: Error message box returned');
99 Log::error('Failed to retrieve version data from GitHub during manual check.');
100 } else {
101 Log::debug('ActionCheckVersion: Failed to retrieve version data (background check)');
102 // Not a menu check, only stop loading if we started it
103 if ($this->didStartLoading) {
105 }
106 }
107 }
108 }
109
118 private function showVersionUpdateWindow($lang, $winbinder, $core, $githubLatestVersion)
119 {
120 $labelFullLink = $lang->getValue(Lang::DOWNLOAD) . ' ' . APP_TITLE . ' ' . $githubLatestVersion;
121
122 $winbinder->reset();
123 $this->wbWindow = $winbinder->createAppWindow($lang->getValue(Lang::CHECK_VERSION_TITLE), 380, 170, WBC_NOTIFY, WBC_KEYDOWN | WBC_KEYUP);
124
125 $winbinder->createLabel($this->wbWindow, $lang->getValue(Lang::CHECK_VERSION_AVAILABLE_TEXT), 80, 35, 370, 120);
126
127 $this->wbLinkFull = $winbinder->createHyperLink($this->wbWindow, $labelFullLink, 80, 87, 200, 20, WBC_LINES | WBC_RIGHT);
128
129 $this->wbBtnOk = $winbinder->createButton($this->wbWindow, $lang->getValue(Lang::BUTTON_OK), 280, 103);
130 $this->wbImage = $winbinder->drawImage($this->wbWindow, Path::getImagesPath() . '/about.bmp');
131
133 $winbinder->setHandler($this->wbWindow, $this, 'processWindow');
134 $winbinder->mainLoop();
135 $winbinder->reset();
136 }
137
144 private function showVersionOkMessageBox($lang, $winbinder)
145 {
146 Log::debug('ActionCheckVersion: Calling Util::stopLoading() in showVersionOkMessageBox');
148 Log::debug('ActionCheckVersion: Calling messageBoxInfo');
149 $winbinder->messageBoxInfo(
150 $lang->getValue(Lang::CHECK_VERSION_LATEST_TEXT),
151 $lang->getValue(Lang::CHECK_VERSION_TITLE)
152 );
153 Log::debug('ActionCheckVersion: messageBoxInfo returned');
154 }
155
165 public function processWindow($window, $id, $ctrl, $param1, $param2)
166 {
167 global $bearsamppConfig, $bearsamppWinbinder;
168
169 switch ($id) {
170 case $this->wbLinkFull[WinBinder::CTRL_ID]:
171 $latestVersionInfo = Util::getLatestVersion(APP_GITHUB_LATEST_URL);
172 if ($latestVersionInfo && isset($latestVersionInfo['html_url'])) {
173 $browserPath = $bearsamppConfig->getBrowser();
174 if (!$bearsamppWinbinder->exec($browserPath, $latestVersionInfo['html_url'])) {
175 Log::error("Failed to open browser at path: $browserPath with URL: " . $latestVersionInfo['html_url']);
176 }
177 } else {
178 Log::error("Failed to retrieve latest version info or 'html_url' not set.");
179 }
180 break;
181 case IDCLOSE:
182 case $this->wbBtnOk[WinBinder::CTRL_ID]:
183 $bearsamppWinbinder->destroyWindow($window);
184 break;
185 default:
186 Log::error("Unhandled window control ID: $id");
187 }
188 }
189}
global $bearsamppLang
global $githubVersionData
$githubLatestVersion
global $bearsamppCore
showVersionUpdateWindow($lang, $winbinder, $core, $githubLatestVersion)
showVersionOkMessageBox($lang, $winbinder)
processWindow($window, $id, $ctrl, $param1, $param2)
const CHECK_VERSION_AVAILABLE_TEXT
const CHECK_VERSION_LATEST_TEXT
const DOWNLOAD
const BUTTON_OK
const CHECK_VERSION_TITLE
static debug($data, $file=null)
static trace($data, $file=null)
static error($data, $file=null)
static getImagesPath($aetrayPath=false)
static getLatestVersion($url)
static startLoading()
static stopLoading()
global $bearsamppConfig
Definition homepage.php:46
const APP_GITHUB_LATEST_URL
Definition root.php:26
const APP_TITLE
Definition root.php:19