Bearsampp 2026.5.5
Loading...
Searching...
No Matches
root.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2022 - 2024 Bearsampp
4 * License: GNU General Public License version 3 or later; see LICENSE.txt
5 * Website: https://bearsampp.com
6 * Github: https://github.com/Bearsampp
7 */
8
12const APP_AUTHOR_NAME = 'N6REJ';
13const APP_TITLE = 'Bearsampp';
14const APP_WEBSITE = 'https://bearsampp.com';
15const APP_LICENSE = 'GPL3 License';
16const APP_GITHUB_USER = 'Bearsampp';
17const APP_GITHUB_REPO = 'Bearsampp';
18const APP_GITHUB_USERAGENT = 'Bearsampp';
19const APP_GITHUB_LATEST_URL = 'https://api.github.com/repos/' . APP_GITHUB_USER . '/' . APP_GITHUB_REPO . '/releases/latest';
20const RETURN_TAB = ' ';
21
22// Membership Pro API key & URL
23const QUICKPICK_API_KEY = '4abe15e5-95f2-4663-ad12-eadb245b28b4';
24const QUICKPICK_API_URL = 'https://bearsampp.com/index.php?option=com_osmembership&task=api.get_active_plan_ids&api_key=';
25
26// URL where quickpick-releases.json lives
27const QUICKPICK_JSON_URL = 'https://raw.githubusercontent.com/Bearsampp/Bearsampp/main/core/resources/quickpick-releases.json';
28
32if (isset($_SERVER['argv']) && isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] === 'startup') {
33 $flagFile = sys_get_temp_dir() . '/bearsampp_no_admin.lock';
34
35 // Quick exit if we already determined no admin recently
36 if (file_exists($flagFile) && (time() - filemtime($flagFile)) < 10) {
37 $currentPid = getmypid();
38 $killCmd = 'powershell.exe -WindowStyle Hidden -Command "Stop-Process -Id ' . (int)$currentPid . ' -Force -ErrorAction SilentlyContinue; Stop-Process -Name bearsampp -Force -ErrorAction SilentlyContinue"';
39 try {
40 // WScript.Shell.Run with style=0 (hidden) avoids cmd.exe flash that popen causes
41 $wshKill = new COM('WScript.Shell');
42 $wshKill->Run($killCmd, 0, false);
43 } catch (Exception $e) {
44 pclose(popen($killCmd, 'r'));
45 }
46 exit(1);
47 }
48
49 // FAST elevation check - use WScript.Shell.Run with hidden window (style=0) to avoid console flash
50 $isElevated = false;
51 try {
52 $wsh = new COM('WScript.Shell');
53 // intWindowStyle=0 = completely hidden (no window, no taskbar entry), bWaitOnReturn=true
54 // net session exits 0 when elevated, non-zero when access denied
55 $exitCode = $wsh->Run('net session', 0, true);
56 $isElevated = ($exitCode === 0);
57 } catch (Exception $e) {
58 // COM unavailable — fall back to shell_exec (may flash briefly)
59 $netOutput = @shell_exec('net session 2>&1');
60 if ($netOutput !== null) {
61 if (stripos($netOutput, 'Access is denied') === false &&
62 stripos($netOutput, 'System error 5') === false &&
63 $netOutput !== '') {
64 $isElevated = true;
65 }
66 }
67 }
68
69 if (!$isElevated) {
70 // Create flag file immediately
71 @file_put_contents($flagFile, time());
72
73 // Load language file for error message
74 $langFile = dirname(__FILE__) . '/langs/english.lang';
75 $langData = @parse_ini_file($langFile);
76
77 // Get localized messages
78 $title = isset($langData['errorAdminRequiredTitle']) ? $langData['errorAdminRequiredTitle'] : 'Administrator Rights Required';
79 $messageText = isset($langData['errorAdminRequiredText']) ? $langData['errorAdminRequiredText'] : '%s requires administrator privileges to install and manage Windows services.@nl@@nl@Please right-click on bearsampp.exe and select "Run as administrator" to start the application.';
80
81 // Replace placeholders
82 $messageText = sprintf($messageText, APP_TITLE);
83 $messageText = str_replace('@nl@', '|||NEWLINE|||', $messageText);
84
85 // Split by newline placeholder
86 $messageParts = explode('|||NEWLINE|||', $messageText);
87
88 // Create PowerShell script that handles everything (no window flashing)
89 $psFile = sys_get_temp_dir() . '/bearsampp_admin_error.ps1';
90 $flagFilePs = str_replace('/', '\\', $flagFile);
91 $psFilePs = str_replace('/', '\\', $psFile);
92
93 // Escape for PowerShell
94 $title = str_replace("'", "''", $title);
95 $currentPid = getmypid();
96
97 // PowerShell script that shows message FIRST, then kills processes
98 $psContent = "# Show error message using Windows Forms\n";
99 $psContent .= "Add-Type -AssemblyName System.Windows.Forms\n";
100
101 // Build message from parts
102 $psContent .= "\$messageParts = @(\n";
103 $lastIndex = count($messageParts) - 1;
104 foreach ($messageParts as $index => $part) {
105 $part = str_replace("'", "''", trim($part));
106 // Don't add comma after last item
107 $comma = ($index < $lastIndex) ? ',' : '';
108 $psContent .= " '" . $part . "'" . $comma . "\n";
109 }
110 $psContent .= ")\n";
111 $psContent .= "\$message = \$messageParts -join [Environment]::NewLine\n";
112 $psContent .= "[System.Windows.Forms.MessageBox]::Show(\$message, '" . $title . "', [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error) | Out-Null\n";
113 $psContent .= "\n";
114 $psContent .= "# After user clicks OK, kill only Bearsampp-related processes\n";
115 $psContent .= "Stop-Process -Id " . (int)$currentPid . " -Force -ErrorAction SilentlyContinue\n";
116 $psContent .= "Stop-Process -Name bearsampp -Force -ErrorAction SilentlyContinue\n";
117 $psContent .= "\n";
118 $psContent .= "# Clean up\n";
119 $psContent .= "Remove-Item -Path '" . $flagFilePs . "' -Force -ErrorAction SilentlyContinue\n";
120 $psContent .= "Remove-Item -Path '" . $psFilePs . "' -Force -ErrorAction SilentlyContinue\n";
121
122 @file_put_contents($psFile, $psContent);
123
124 // Launch PowerShell to show the error message — use COM Run(style=0) to avoid cmd.exe flash
125 if (isset($wsh)) {
126 $wsh->Run('powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File "' . $psFile . '"', 0, false);
127 } else {
128 pclose(popen('start "" powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File "' . $psFile . '"', 'r'));
129 }
130
131 // Exit immediately - PowerShell will handle showing the message and cleanup
132 exit(1);
133 } else {
134 // We're elevated, clean up any old flag
135 @unlink($flagFile);
136 }
137}
138
143require_once dirname(__FILE__) . '/classes/class.root.php';
144$bearsamppRoot = new Root(dirname(__FILE__));
145$bearsamppRoot->register();
146
151$bearsamppAction->process();
152
156if ($bearsamppRoot->isRoot()) {
158}
159
164$locale = $bearsamppLang->getValue('locale');
global $bearsamppLang
global $bearsamppRoot
static stopLoading()
const QUICKPICK_JSON_URL
Definition root.php:27
const APP_GITHUB_USERAGENT
Definition root.php:18
const APP_AUTHOR_NAME
Definition root.php:12
const APP_WEBSITE
Definition root.php:14
$locale
Definition root.php:164
const APP_GITHUB_USER
Definition root.php:16
const APP_GITHUB_REPO
Definition root.php:17
const QUICKPICK_API_URL
Definition root.php:24
const APP_LICENSE
Definition root.php:15
const APP_GITHUB_LATEST_URL
Definition root.php:19
const QUICKPICK_API_KEY
Definition root.php:23
const RETURN_TAB
Definition root.php:20
$bearsamppAction
Definition root.php:150
const APP_TITLE
Definition root.php:13