getClientIp(); $file = (sys_get_temp_dir() ?: '/tmp') . '/fs_tools_throttle_' . sha1($ip) . '.json'; $events = []; if (file_exists($file)) { $events = json_decode(file_get_contents($file), true) ?: []; } // Keep only timestamps inside the window $filtered = []; for ($i = 0; $i < count($events); $i++) { if (($now - $events[$i]) < $window_seconds) { $filtered[] = $events[$i]; } } $events = $filtered; // Too many requests. if (count($events) >= $max_requests) { return true; } // Add current request $events[] = $now; file_put_contents($file, json_encode($events), LOCK_EX); } // Get app key //function getAppKey($root_dir, $check_cache = true) function getEnvVar($var_name, $root_dir) { // First check APP_KEY in cache // if ($check_cache && file_exists($root_dir.'bootstrap/cache/config.php')) { // $config = include $root_dir.'bootstrap/cache/config.php'; // if (!empty($config)) { // if (!empty($config['app']['key'])) { // return $config['app']['key']; // } else { // return ''; // } // } // } // Read .env file into $_ENV try { $dotenv = new Dotenv\Dotenv($root_dir); // If using load() if $_ENV['APP_KEY'] was present in .env before it will not be updated when reading $dotenv->overload(); } catch (\Exception $e) { // Do nothing } if (!empty($_ENV[$var_name])) { return $_ENV[$var_name]; } else { return ''; } } // Set PHP_PATH in the .env file if PHP on your server can not be executed via "php" console command. // https://github.com/freescout-help-desk/freescout/issues/5507 define('PHP_PATH', trim(getEnvVar('PHP_PATH', $root_dir))); function clearCache($root_dir, $php_path) { if (file_exists($root_dir.'bootstrap/cache/config.php')) { unlink($root_dir.'bootstrap/cache/config.php'); } if (file_exists($root_dir.'bootstrap/cache/services.php')) { unlink($root_dir.'bootstrap/cache/services.php'); } if (file_exists($root_dir.'bootstrap/cache/packages.php')) { unlink($root_dir.'bootstrap/cache/packages.php'); } if (file_exists($root_dir.'bootstrap/cache/routes.php')) { unlink($root_dir.'bootstrap/cache/routes.php'); } return shell_exec($php_path.' '.$root_dir.'artisan freescout:clear-cache'); } function hashSecret($secret, $root_dir) { $salt = getEnvVar('APP_KEY', $root_dir); return (string)sha1($secret.$salt); } $logged_in = false; $alerts = []; $errors = []; $app_key = trim($_POST['app_key'] ?? ''); $db_username = trim($_POST['db_username'] ?? ''); $db_password = trim($_POST['db_password'] ?? ''); if (!empty($_POST)) { if (throttle()) { http_response_code(429); exit('Too many requests. Please wait.'); } // First check DB credentials. $env_db_username = trim(getEnvVar('DB_USERNAME', $root_dir)); $env_db_password = trim(getEnvVar('DB_PASSWORD', $root_dir)); if (($db_username !== $env_db_username && $db_username != hashSecret($env_db_username, $root_dir)) || ($db_password !== $env_db_password && $db_password != hashSecret($env_db_password, $root_dir)) ) { $alerts[] = [ 'type' => 'danger', 'text' => 'Invalid DB Username or Password', ]; } else { $logged_in = true; } if ($logged_in && $app_key) { $php_path = PHP_PATH ?: 'php'; // https://github.com/freescout-help-desk/freescout/security/advisories/GHSA-jx2w-fhmw-rg39 if (!empty($_POST['php_path'])) { $php_path = trim($_POST['php_path']); $php_path = preg_replace("#[ ;\$<>:&\|`\t\r\n]#", '', $php_path); if (!$php_path) { $php_path = 'php'; } // Is allowed PHP directory. if (!PHP_PATH) { $real_php_dir = dirname(realpath($php_path)); if ($real_php_dir && !in_array($real_php_dir, ALLOWED_PHP_DIRS, true)) { $errors['php_path'] = 'Directory is not allowed. Allowed directories: '.implode(', ', ALLOWED_PHP_DIRS).". You may need to set the variable PHP_PATH in .env"; } } // Sanitize path. // https://github.com/freescout-helpdesk/freescout/security/advisories/GHSA-7p9x-ch4c-vqj9 if (empty($errors['php_path'])) { if (!file_exists($php_path) || !stristr($php_path, 'php')) { $errors['php_path'] = 'Invalid Path ('.$php_path.' does not exist or "php" not found in PHP_PATH'; } } } if (empty($errors)) { if (trim($app_key) !== trim(getEnvVar('APP_KEY', $root_dir))) { $errors['app_key'] = 'Invalid App Key'; } else { if (!function_exists('shell_exec')) { $alerts[] = [ 'type' => 'danger', 'text' => 'shell_exec function is unavailable. Can not run updating.', ]; } else { // Make sure that it's actually $php_path points to PHP executable and not something else. $version_output = shell_exec($php_path.' -r "echo phpversion();"'); if (!preg_match("#^\d+\.\d+\.\d+#", $version_output)) { if ($php_path != 'php') { // $alerts[] = [ // 'type' => 'danger', // 'text' => 'Invalid Path to PHP: '.$php_path, // ]; $errors['php_path'] = 'Path to invalid PHP (Wrong or empty php version: "'. $version_output. '")'; } else { $alerts[] = [ 'type' => 'danger', 'text' => '"php" command could not be executed. You may need to set the variable PHP_PATH in .env', ]; } } if (!count($alerts) && empty($errors)) { if ($_POST['action'] == 'cc') { $cc_output = clearCache($root_dir, $php_path); $alerts[] = [ 'type' => 'success', 'text' => 'Cache cleared:
'.htmlspecialchars($cc_output).'
', ]; } else { try { // First check PHP version. if (!version_compare($version_output, '7.1', '>=')) { $alerts[] = [ 'type' => 'danger', 'text' => 'Incorrect PHP version (7.1+ is required):

'.htmlspecialchars($version_output).'
', ]; } else { if ($_POST['action'] == 'update') { // Update Now $output = shell_exec($php_path.' '.$root_dir.'artisan freescout:update --force'); if (strstr($output, 'Broadcasting queue restart signal')) { $alerts[] = [ 'type' => 'success', 'text' => 'Updating finished:
'.htmlspecialchars($output).'
', ]; } else { $alerts[] = [ 'type' => 'danger', 'text' => 'Something went wrong... Please download the latest version and extract it into your application folder replacing existing files. After that click "Migrate DB" button.

'.htmlspecialchars($output).'
', ]; } } else { // Migreate DB $output = shell_exec($php_path.' '.$root_dir.'artisan migrate --force'); $alerts[] = [ 'type' => 'success', 'text' => 'Migrating finished:

'.htmlspecialchars($output).'
', ]; } } } catch (\Exception $e) { $alerts[] = [ 'type' => 'danger', 'text' => 'Error occurred: '.htmlspecialchars($e->getMessage()), ]; } } } } } } } } ?> FreeScout Tools

FreeScout Tools