<?php 

function setNitroPersistence($data) {
	if (!is_dir(NITRO_FOLDER.'data')) {
		mkdir(NITRO_FOLDER.'data');	
	}
	file_put_contents(NITRO_PERSISTENCE, base64_encode(json_encode($data)));
	
	return true;
}

function getNitroPersistence($setting = null) {
	if (file_exists(NITRO_PERSISTENCE) && empty($GLOBALS['nitroPersistence'])) {
		$data = file_get_contents(NITRO_PERSISTENCE);
		$data = base64_decode($data);
		$returnData = json_decode($data, true);
		$GLOBALS['nitroPersistence'] = $returnData;
	} else {
		if (!empty($GLOBALS['nitroPersistence'])) {
			$returnData = $GLOBALS['nitroPersistence'];
		} else {
			$returnData = false;	
		}
	}
	if (!empty($setting)) {
		if (!empty($returnData['Nitro'][$setting])) {
			$returnData = $returnData['Nitro'][$setting];
		}
	}
	return $returnData;
}

function getNitroSmushitPersistence() {
	$file = DIR_SYSTEM . 'nitro/data/smushit_persistence.tpl';
	$data = array(
		'smushed_images_count' => 0,
		'already_smushed_images_count' => 0,
		'total_images' => false,
		'kb_saved' => 0,
		'last_smush_timestamp' => 0
	);
	if (file_exists($file)) {
		$data = json_decode(file_get_contents($file), true);
	} else {
		file_put_contents($file, json_encode($data));
	}
	return $data;
}

function setNitroSmushitPersistence($data) {
	if (is_array($data)) {
		$file = DIR_SYSTEM . 'nitro/data/smushit_persistence.tpl';
		$old_data = getNitroSmushitPersistence();
		$new_data = array_merge($old_data, $data);
		file_put_contents($file, json_encode($new_data));
		return true;
	}
	return false;
}

function setGooglePageSpeedReport($data, $strategy) {
	if (!is_dir(NITRO_FOLDER.'data')) {
		mkdir(NITRO_FOLDER.'data');	
	}
	//if (is_array($data) || is_object($data)) {
		//$data = json_encode($data);
	//}
	file_put_contents(NITRO_FOLDER.'data/googlepagespeed-'.$strategy.'.tpl',base64_encode($data));
	return true;
}

function refreshGooglePageSpeedReport($strategies = array('mobile', 'desktop')) {
	foreach($strategies as $strategy) {
		if (file_exists(NITRO_FOLDER.'data/googlepagespeed-'.$strategy.'.tpl')) {
			if (!unlink(NITRO_FOLDER.'data/googlepagespeed-'.$strategy.'.tpl')) {
				return 'There was a permission issue - please make sure the file system/nitro/data/googlepagespeed.tpl has at least 644 permissions!';
			}
		}
	}
	return 'Google Page Speed Report was refreshed.';
}

function getGooglePageSpeedReport($setting = null, $strategies = array('mobile', 'desktop')) {
	foreach ($strategies as $strategy) {
		if (!file_exists(NITRO_FOLDER.'data/googlepagespeed-'.$strategy.'.tpl')) {
			// Fetch the report and save it
			$catalogURL = (defined('HTTP_SERVER') && HTTP_SERVER != '') ? dirname(HTTP_SERVER) : false;
			
			if (!$catalogURL) return false;
			
			$persistence = getNitroPersistence();
			$key = !empty($persistence['Nitro']['GooglePageSpeedApiKey']) ? $persistence['Nitro']['GooglePageSpeedApiKey'] : 'AIzaSyCxptR6CbHYrHkFfsO_XN3nkf6FjoQp2Mg';
			$url = "https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=".$catalogURL."&key=".$key."&strategy=".$strategy;
			$output = '';
			if (function_exists('curl_init')) {
				$ch = curl_init();  
				curl_setopt($ch, CURLOPT_URL, $url);  
				curl_setopt($ch, CURLOPT_HEADER, 0);  
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
				$output = curl_exec($ch);  
				curl_close($ch);
			} else if ((int)ini_get('allow_url_fopen')) {
				$output = file_get_contents($url);
			}
			if (!empty($output)) {
				setGooglePageSpeedReport($output, $strategy);
			}
		}
	}
	$returnData = false;
	foreach ($strategies as $strategy) {
		if (file_exists(NITRO_FOLDER.'data/googlepagespeed-'.$strategy.'.tpl')) {
			if (!is_array($returnData)) {
				$returnData = array();
			}
			$returnData[$strategy] = base64_decode(file_get_contents(NITRO_FOLDER.'data/googlepagespeed-'.$strategy.'.tpl'));
		}
	}
	if (!empty($setting)) {
		if (!empty($returnData['NitroCache'][$setting])) {
			$returnData = $returnData['NitroCache'][$setting];
		} else {
			$returnData = false;
		}
	}
	return $returnData;
}

function compressGzip($data, $level = 0) {
	$headers = array();
	if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false)) {
		$encoding = 'gzip';
	} 

	if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== false)) {
		$encoding = 'x-gzip';
	}

	if (!isset($encoding)) {
		return $data;
	}

	if (!extension_loaded('zlib') || ini_get('zlib.output_compression')) {
		return $data;
	}

	if (headers_sent()) {
		//return $data;
	}

	if (connection_status()) { 
		return $data;
	}
	
	//$headers[] = ('Content-Encoding: ' . $encoding);
	//foreach ($headers as $header) {
	//	header($header, true);
	//}
	return gzencode($data, (int)$level);
}

function minifyHTML($html) {

	include NITRO_FOLDER.'lib'.DS.'minifier'.DS.'HTMLMin.php';
	$htmlMinifier = new Nitro_Minify_HTML($html,array('jsCleanComments' => false));
	$html =  $htmlMinifier->process();
	//$html = str_replace(array("\n"), array(" "), trim($html));
	return $html;
}

function getSSLCachePrefix() {
	return isset($_SERVER['HTTPS']) && (($_SERVER['HTTPS'] == 'on') || ($_SERVER['HTTPS'] == '1')) ? '1-' : '0-';
}

?>