Bearsampp 2026.9.23
Loading...
Searching...
No Matches
ajax.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2026 Bearsampp
4 * License: GNU General Public License version 3 or later; see LICENSE.txt
5 * Author: Bear
6 * Website: https://bearsampp.com
7 * Github: https://github.com/Bearsampp
8 */
9
38$ajaxProcRaw = isset($_POST['proc']) ? $_POST['proc'] : '';
40 'summary',
41 'latestversion',
42 'apache',
43 'mailpit',
44 'memcached',
45 'mariadb',
46 'mysql',
47 'nodejs',
48 'php',
49 'postgresql',
50 'xlight',
51 'reloadstatus',
52);
53
54if (in_array($ajaxProcRaw, $ajaxReadOnlyProcs, true)) {
55 require_once __DIR__ . '/../../classes/class.log.php';
57 ob_start();
58
59 register_shutdown_function(function () use ($ajaxProcRaw) {
60 try {
61 $output = ob_get_contents();
62 if ($output === false) {
63 $output = '';
64 }
65 $fingerprint = md5($output);
66
67 if (!class_exists('Path')) {
68 // Bootstrap failed before Path was available -> keep the entries.
70
71 return;
72 }
73 $fingerprintFile = Path::getTmpPath() . '/homepage-ajax-state-' . md5($ajaxProcRaw) . '.md5';
74 $stored = @file_get_contents($fingerprintFile);
75
76 if ($stored !== false && trim($stored) === $fingerprint) {
77 // Homepage state unchanged -> discard this poll's log entries.
79 } else {
80 // State changed (service started/stopped, version changed, ...) ->
81 // write this poll's log entries and remember the new state.
82 @file_put_contents($fingerprintFile, $fingerprint, LOCK_EX);
84 }
85 } catch (Exception $e) {
86 // On any failure, commit (write) the entries rather than lose them.
88 }
89 });
90}
91
96include_once __DIR__ . '/../../root.php';
97
105 'summary' => __DIR__ . '/ajax/ajax.summary.php',
106 'latestversion' => __DIR__ . '/ajax/ajax.latestversion.php',
107 'apache' => __DIR__ . '/ajax/ajax.apache.php',
108 'mailpit' => __DIR__ . '/ajax/ajax.mailpit.php',
109 'memcached' => __DIR__ . '/ajax/ajax.memcached.php',
110 'mariadb' => __DIR__ . '/ajax/ajax.mariadb.php',
111 'mysql' => __DIR__ . '/ajax/ajax.mysql.php',
112 'nodejs' => __DIR__ . '/ajax/ajax.nodejs.php',
113 'php' => __DIR__ . '/ajax/ajax.php.php',
114 'postgresql' => __DIR__ . '/ajax/ajax.postgresql.php',
115 'xlight' => __DIR__ . '/ajax/ajax.xlight.php',
116 'quickpick' => __DIR__ . '/ajax/ajax.quickpick.php',
117 'toggleenhancedquickpick' => __DIR__ . '/ajax/ajax.toggle.enhancedquickpick.php',
118 'applymoduleconfig' => __DIR__ . '/ajax/ajax.apply.moduleconfig.php',
119 'reloadstatus' => __DIR__ . '/ajax/ajax.reload.status.php',
120 'clearcache' => __DIR__ . '/ajax/ajax.clearcache.php'
121];
122
130$proc = UtilInput::cleanPostVar('proc', 'text'); // Ensure 'proc' is cleaned and read correctly
131
138 'quickpick', // Installs modules
139 'toggleenhancedquickpick', // Changes configuration
140 'applymoduleconfig', // Applies configuration changes
141 'clearcache' // Clears cache files
142];
143
150if (in_array($proc, $csrfProtectedEndpoints, true)) {
151 // State-changing endpoints must be reached via POST only
152 if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
153 http_response_code(405);
154 header('Content-Type: application/json');
155 header('Cache-Control: no-store');
156 echo json_encode(['error' => 'Method not allowed']);
157 exit;
158 }
159
160 Csrf::init();
161
162 if (!Csrf::validateRequest()) {
163 http_response_code(403);
164 header('Content-Type: application/json');
165 header('Cache-Control: no-store');
166 echo json_encode([
167 'error' => 'CSRF validation failed',
168 'message' => 'Invalid or expired security token. Please refresh the page and try again.'
169 ]);
170 exit;
171 }
172
173 // Release the session lock so long-running handlers (module downloads /
174 // extraction) do not block other requests sharing this session.
175 session_write_close();
176}
177
183if (isset($procMap[$proc]) && file_exists($procMap[$proc])) {
187 include $procMap[$proc];
188} else {
194 $errorMessage = 'Invalid proc parameter';
195 if (!empty($proc)) {
196 $errorMessage .= ': "' . htmlspecialchars($proc) . '" is not a valid procedure';
197 } else {
198 $errorMessage .= ': no procedure was specified';
199 }
200 echo json_encode(['error' => $errorMessage]);
201}
$csrfProtectedEndpoints
Definition ajax.php:137
$procMap
Definition ajax.php:104
$ajaxProcRaw
Definition ajax.php:38
$proc
Definition ajax.php:130
$ajaxReadOnlyProcs
Definition ajax.php:39
static validateRequest($removeAfterValidation=false)
static init()
static startSilentBuffer()
static rollbackSilentBuffer()
static commitSilentBuffer()
static getTmpPath($aetrayPath=false)
static cleanPostVar($name, $type='text')