Bearsampp 2026.7.11
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{
22 const DISPLAY_OK = 'displayOk';
23
24 private $wbWindow;
25 private $wbImage;
27 private $wbLinkFull;
28 private $wbBtnOk;
29
33 private $didStartLoading = false;
34
40 public function __construct($args)
41 {
42 global $bearsamppCore, $bearsamppLang, $bearsamppWinbinder, $appGithubHeader;
43
44 // Check if we're being called from the version check menu item
45 $isMenuCheck = !empty($args[0]) && $args[0] == self::DISPLAY_OK;
46
47 // Start loading only if the exec file doesn't exist or if we're doing a menu check
48 if (!file_exists($bearsamppCore->getExec()) || $isMenuCheck) {
49 if ($isMenuCheck) {
50 Log::debug('ActionCheckVersion: Manual check detected, starting loading');
52 $this->didStartLoading = true;
53 }
54 $this->currentVersion = $bearsamppCore->getAppVersion();
55 Log::debug('ActionCheckVersion: Current version: ' . $this->currentVersion);
56
57 // Assuming getLatestVersion now returns an array with version and URL
59 Log::debug('ActionCheckVersion: GitHub version data: ' . var_export($githubVersionData, true));
60
61 if ($githubVersionData != null && isset($githubVersionData['version'], $githubVersionData['html_url'])) {
63 $this->githubLatestVersionUrl = $githubVersionData['html_url']; // URL of the latest version
64 Log::debug('ActionCheckVersion: GitHub latest version: ' . $githubLatestVersion);
65 if (version_compare($this->currentVersion, $githubLatestVersion, '<')) {
66 Log::debug('ActionCheckVersion: Update available, showing update window');
67 $this->showVersionUpdateWindow($bearsamppLang, $bearsamppWinbinder, $bearsamppCore, $githubLatestVersion);
68 } elseif ($isMenuCheck) {
69 Log::debug('ActionCheckVersion: Version is up to date, showing OK message box');
70 $this->showVersionOkMessageBox($bearsamppLang, $bearsamppWinbinder);
71 } else {
72 Log::debug('ActionCheckVersion: Version is up to date (background check)');
73 if ($this->didStartLoading) {
75 }
76 }
77 } elseif ($isMenuCheck) {
78 Log::debug('ActionCheckVersion: Failed to retrieve version data during manual check');
79 // If it's a menu check but we couldn't get version data, we must stop loading
80 if ($this->didStartLoading) {
82 }
83 Log::debug('ActionCheckVersion: Showing error message box');
84 $bearsamppWinbinder->messageBoxError(
85 'Failed to retrieve version data from GitHub during manual check.',
87 );
88 Log::debug('ActionCheckVersion: Error message box returned');
89 Log::error('Failed to retrieve version data from GitHub during manual check.');
90 } else {
91 Log::debug('ActionCheckVersion: Failed to retrieve version data (background check)');
92 // Not a menu check, only stop loading if we started it
93 if ($this->didStartLoading) {
95 }
96 }
97 }
98 }
99
108 private function showVersionUpdateWindow($lang, $winbinder, $core, $githubLatestVersion)
109 {
110 $labelFullLink = $lang->getValue(Lang::DOWNLOAD) . ' ' . APP_TITLE . ' ' . $githubLatestVersion;
111
112 $winbinder->reset();
113 $this->wbWindow = $winbinder->createAppWindow($lang->getValue(Lang::CHECK_VERSION_TITLE), 380, 170, WBC_NOTIFY, WBC_KEYDOWN | WBC_KEYUP);
114
115 $winbinder->createLabel($this->wbWindow, $lang->getValue(Lang::CHECK_VERSION_AVAILABLE_TEXT), 80, 35, 370, 120);
116
117 $this->wbLinkFull = $winbinder->createHyperLink($this->wbWindow, $labelFullLink, 80, 87, 200, 20, WBC_LINES | WBC_RIGHT);
118
119 $this->wbBtnOk = $winbinder->createButton($this->wbWindow, $lang->getValue(Lang::BUTTON_OK), 280, 103);
120 $this->wbImage = $winbinder->drawImage($this->wbWindow, Path::getImagesPath() . '/about.bmp');
121
123 $winbinder->setHandler($this->wbWindow, $this, 'processWindow');
124 $winbinder->mainLoop();
125 $winbinder->reset();
126 }
127
134 private function showVersionOkMessageBox($lang, $winbinder)
135 {
136 Log::debug('ActionCheckVersion: Calling Util::stopLoading() in showVersionOkMessageBox');
138 Log::debug('ActionCheckVersion: Calling messageBoxInfo');
139 $winbinder->messageBoxInfo(
140 $lang->getValue(Lang::CHECK_VERSION_LATEST_TEXT),
141 $lang->getValue(Lang::CHECK_VERSION_TITLE)
142 );
143 Log::debug('ActionCheckVersion: messageBoxInfo returned');
144 }
145
155 public function processWindow($window, $id, $ctrl, $param1, $param2)
156 {
157 global $bearsamppConfig, $bearsamppWinbinder;
158
159 switch ($id) {
160 case $this->wbLinkFull[WinBinder::CTRL_ID]:
162 if ($latestVersionInfo && isset($latestVersionInfo['html_url'])) {
163 $browserPath = $bearsamppConfig->getBrowser();
164 if (!$bearsamppWinbinder->exec($browserPath, $latestVersionInfo['html_url'])) {
165 Log::error("Failed to open browser at path: $browserPath with URL: " . $latestVersionInfo['html_url']);
166 }
167 } else {
168 Log::error("Failed to retrieve latest version info or 'html_url' not set.");
169 }
170 break;
171 case IDCLOSE:
172 case $this->wbBtnOk[WinBinder::CTRL_ID]:
173 $bearsamppWinbinder->destroyWindow($window);
174 break;
175 default:
176 Log::error("Unhandled window control ID: $id");
177 }
178 }
179}
global $bearsamppLang
global $githubVersionData
$githubLatestVersion
global $bearsamppCore
showVersionUpdateWindow($lang, $winbinder, $core, $githubLatestVersion)
showVersionOkMessageBox($lang, $winbinder)
processWindow($window, $id, $ctrl, $param1, $param2)
static getLatestVersion($url)
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 error($data, $file=null)
static getImagesPath($aetrayPath=false)
static startLoading()
static stopLoading()
global $bearsamppConfig
Definition homepage.php:41
const APP_GITHUB_LATEST_URL
Definition root.php:19
const APP_TITLE
Definition root.php:13