.htaccess
1 year ago
Config.php
5 months ago
Locations.php
1 year ago
System.php
5 months ago
index.html
1 year ago
web.config
1 year ago
System.php
175 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Config; |
| 4 | |
| 5 | use DateTime; |
| 6 | use DateTimeZone; |
| 7 | use Exception; |
| 8 | use JetBackup\Cron\Cron; |
| 9 | use JetBackup\Entities\Util; |
| 10 | use JetBackup\Exception\IOException; |
| 11 | use JetBackup\Factory; |
| 12 | use JetBackup\IO\Execute; |
| 13 | use JetBackup\JetBackup; |
| 14 | use JetBackup\JetBackupLinux\JetBackupLinux; |
| 15 | use JetBackup\Wordpress\Wordpress; |
| 16 | use JetBackup\Wordpress\Helper; |
| 17 | |
| 18 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 19 | |
| 20 | class System { |
| 21 | |
| 22 | const NO_VALUE_STRING = "Cannot get value"; |
| 23 | const PHP_MIN_POST_MAX_SIZE = (8 * 1024 * 1024); |
| 24 | |
| 25 | public static function isWindowsOS(): bool { |
| 26 | // Check if PHP_OS_FAMILY is available (PHP 7.2+) |
| 27 | if(defined('PHP_OS_FAMILY')) return PHP_OS_FAMILY === 'Windows'; |
| 28 | |
| 29 | // Fallback to using PHP_OS for older versions |
| 30 | return str_starts_with(strtoupper(PHP_OS), 'WIN'); |
| 31 | } |
| 32 | |
| 33 | public static function getLastCron (): int { |
| 34 | $cron_last = Factory::getLocations()->getDataDir() . JetBackup::SEP . Cron::LAST_FILE; |
| 35 | if(!file_exists($cron_last)) return 0; |
| 36 | $_start_time = filemtime($cron_last); |
| 37 | $timeDifference = microtime(true) - $_start_time; |
| 38 | return (int) $timeDifference; |
| 39 | } |
| 40 | |
| 41 | public static function getRecommendSecurePath(): ?string { |
| 42 | // Check if the open_basedir directive is enabled |
| 43 | if (ini_get('open_basedir')) { |
| 44 | return '(Open basedir is enabled, cannot set secure path)'; |
| 45 | } |
| 46 | |
| 47 | // Attempt to fetch the user's home directory |
| 48 | $userHomeDir = getenv('HOME') ?: getenv('HOMEDRIVE') . getenv('HOMEPATH'); |
| 49 | |
| 50 | // Validate the user's home directory |
| 51 | if ($userHomeDir && is_dir($userHomeDir)) { |
| 52 | // Construct the secure path within the user's home directory |
| 53 | return $userHomeDir . JetBackup::SEP . Factory::getConfig()->getDataDirectory(); |
| 54 | } |
| 55 | |
| 56 | // If home directory is not valid, fallback to the previous solution |
| 57 | $_base_path = dirname(Factory::getWPHelper()->getWordPressHomedir()); |
| 58 | |
| 59 | // Check if this is nested inside another WordPress installation |
| 60 | if (file_exists($_base_path . JetBackup::SEP . 'wp-config.php')) { |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | // Return the secure path based on the parent directory of the WordPress home directory |
| 65 | return $_base_path . JetBackup::SEP . Factory::getConfig()->getDataDirectory(); |
| 66 | } |
| 67 | |
| 68 | private static function getFreeDiskSpace(): string { |
| 69 | try { |
| 70 | return function_exists('disk_free_space') ? Util::bytesToHumanReadable(disk_free_space( Factory::getWPHelper()->getWordPressHomedir())) : self::NO_VALUE_STRING; |
| 71 | } catch ( Exception $e) { |
| 72 | throw new IOException($e->getMessage()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private static function getOpenFilesLimit (): ?string { |
| 77 | return function_exists('posix_getrlimit') ? posix_getrlimit()['hard openfiles'] : self::NO_VALUE_STRING; |
| 78 | } |
| 79 | |
| 80 | private static function getPHPCliVersion () { |
| 81 | try { |
| 82 | |
| 83 | if(!Execute::run((Factory::getSettingsGeneral()->getPHPCLILocation() ?: 'php') . " -r 'print_r(phpversion());'", $output)) |
| 84 | return $output[0]; |
| 85 | |
| 86 | return self::NO_VALUE_STRING; |
| 87 | |
| 88 | } catch ( Exception $e) { |
| 89 | throw new IOException($e->getMessage()); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | private static function getPHPVersion (): string { |
| 94 | return defined('PHP_VERSION') ? PHP_VERSION : self::NO_VALUE_STRING; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @throws IOException |
| 99 | */ |
| 100 | public static function isPHPVersionCompatible() { |
| 101 | |
| 102 | if (($php_web_version = self::getPHPVersion()) && ($php_cli_version = self::getPHPCliVersion()) != self::NO_VALUE_STRING) { |
| 103 | return version_compare($php_web_version, $php_cli_version, '='); |
| 104 | } |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | public static function isDataDirSecured(): bool { |
| 110 | $homedir = Factory::getWPHelper()->getWordPressHomedir(); |
| 111 | $datadir = Factory::getLocations()->getDataDir(); |
| 112 | return !str_starts_with($datadir, $homedir); |
| 113 | } |
| 114 | |
| 115 | public static function isAlternateFolderSecured(?string $datadir): bool { |
| 116 | if (!$datadir) return false; |
| 117 | $homedir = Helper::getUserHomedir(); |
| 118 | if(!$homedir) $homedir = dirname(Wordpress::getAbsPath()); |
| 119 | return str_starts_with($datadir, $homedir); |
| 120 | } |
| 121 | |
| 122 | private static function getAvailableCli (): string { |
| 123 | if (!($list = Execute::getAvailable())) return self::NO_VALUE_STRING; |
| 124 | return implode(',', array_values($list)); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @throws IOException |
| 129 | * @throws Exception |
| 130 | */ |
| 131 | public static function getSystemInfo(): array { |
| 132 | |
| 133 | $dateTime = new DateTime('now', new DateTimeZone(Factory::getSettingsGeneral()->getTimeZone())); |
| 134 | |
| 135 | $output = [ |
| 136 | 'timezone' => Factory::getSettingsGeneral()->getTimeZone(), |
| 137 | 'show_time' => $dateTime->format('H:i:s'), |
| 138 | 'loaded_language' => Wordpress::getLocale(), |
| 139 | 'open_files_limit' => self::getOpenFilesLimit(), |
| 140 | 'memory_limit' => ini_get('memory_limit'), |
| 141 | 'max_execution_time' => ini_get('max_execution_time'), |
| 142 | 'post_max_size' => ini_get('post_max_size'), |
| 143 | 'upload_max_filesize' => ini_get('upload_max_filesize'), |
| 144 | 'wordpress_path' => Factory::getWPHelper()->getWordPressHomedir(), |
| 145 | 'jetbackup_data_dir' => Factory::getLocations()->getDataDir(), |
| 146 | 'php_version' => self::getPHPVersion(), |
| 147 | 'php_cli' => self::getPHPCliVersion(), |
| 148 | 'available_cli' => self::getAvailableCli(), |
| 149 | ]; |
| 150 | |
| 151 | if(Factory::getSettingsGeneral()->isDisplayLocalDiskSpaceEnabled()) $output['free_disk_space'] = self::getFreeDiskSpace(); |
| 152 | |
| 153 | return $output; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @throws IOException |
| 158 | */ |
| 159 | public static function getTotalAlerts(): int { |
| 160 | $alerts = 0; |
| 161 | if(!self::isDataDirSecured()) $alerts++; |
| 162 | if(ini_get('post_max_size') !== false && Util::humanReadableToBytes(ini_get('post_max_size')) < self::PHP_MIN_POST_MAX_SIZE) $alerts++; |
| 163 | if(!self::isPHPVersionCompatible()) $alerts++; |
| 164 | if(self::getLastCron() > 600) $alerts++; |
| 165 | if(!Factory::getSettingsAutomation()->isHeartbeatEnabled()) $alerts++; |
| 166 | if(!Factory::getSettingsAutomation()->isCronsEnabled()) $alerts++; |
| 167 | if(!defined('PDO::MYSQL_ATTR_USE_BUFFERED_QUERY')) $alerts++; |
| 168 | if(JetBackupLinux::isInstalled() && !Factory::getSettingsGeneral()->isJBIntegrationEnabled()) $alerts++; |
| 169 | return $alerts; |
| 170 | } |
| 171 | |
| 172 | public static function getServerExecutionTime() { |
| 173 | return ini_get('max_execution_time') ?: 60; |
| 174 | } |
| 175 | } |