<?php
// phpcs:disable WordPress.WP.AlternativeFunctions, WordPress.Security.NonceVerification -- this file is standalone and doesn't run in the context of WordPress

header('Content-Type: text/css');


$file = !empty($_GET['file']) && in_array($_GET['file'], array('style.css', 'style.dev.css', 'style-cpt.css')) ? $_GET['file'] : 'style.css'; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- checked against fixed values
$origFile = __DIR__.'/../../../../themes/Divi/'.$file;
$origFileSize = @filesize($origFile); // phpcs:ignore PHPCS_SecurityAudit.BadFunctions.FilesystemFunctions.WarnFilesystem -- file name is checked against fixed values
$currentVersion = $origFileSize;
	
$css = @file_get_contents(__DIR__.'/divi-'.$file); // phpcs:ignore HPCS_SecurityAudit.BadFunctions.FilesystemFunctions.WarnFilesystem -- not a remote file; file name is checked against fixed values
if (!empty($css)) {
	$spacePos = strpos($css, ' ', 3);
	if ($spacePos) {
		$cachedVersion = substr($css, 3, $spacePos - 3);
		if ($cachedVersion == $currentVersion) {
			echo($css); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSS output
			exit;
		}
	}
}

$css = file_get_contents($origFile); // phpcs:ignore PHPCS_SecurityAudit.BadFunctions.FilesystemFunctions.WarnFilesystem -- not a remote file; file name is checked against fixed values

/* dsx-exclude */
@include(__DIR__.'/options.php');
/* /dsx-exclude */
/* dsx-execute
@include(__DIR__.'/../options.php');
/dsx-execute */
/* dsx-execute */
if (empty($diviOptions)) {
/* /dsx-execute */
/* dsx-exclude */
	echo($css); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSS output
/* /dsx-exclude */
/* dsx-execute */
	exit;
}
/* /dsx-execute */

$css = preg_replace('/(url\\(["\']?)/i', '$1../../../../themes/Divi/', $css);

$from = array();
$to = array();

/* dsx-execute */
$breakpoints = array(1406, 1099, 981, 801, 768, 480, 381);
foreach ($breakpoints as $i => $breakpoint) {
	$option = 'swtch-mobile-'.($i + 1);
	if (!empty($diviOptions[$option]) && is_numeric($diviOptions[$option])) {
		$optionValue = $diviOptions[$option];
/* /dsx-execute */
/* dsx-include
		$optionValue = '^($optionValue)^';
/dsx-include */
		$from[] = '(min-width:'.$breakpoint.'px)';
		$to[] = '(min-width:'.$optionValue.'px)';
		$from[] = '(max-width:'.($breakpoint - 1).'px)';
		$to[] = '(max-width:'.($optionValue - 1).'px)';
/* dsx-execute */
	}
}
/* /dsx-execute */

if (!empty($from)) {
	$css = str_ireplace($from, $to, $css);
}

if (file_exists(__DIR__.'/style.css')) { // phpcs:ignore PHPCS_SecurityAudit.BadFunctions.FilesystemFunctions.WarnFilesystem
	$css .= "\n\n".file_get_contents(__DIR__.'/style.css'); // phpcs:ignore PHPCS_SecurityAudit.BadFunctions.FilesystemFunctions.WarnFilesystem
}

$css = '/* '.$currentVersion.' Modified by the Divi Switch plugin by Divi Space to implement user customization(s) - '.date('r').' */'."\n".$css;
file_put_contents(__DIR__.'/divi-'.$file, $css); 
echo($css); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- CSS output
?>