<?php

function &nitro_get_global_cdn_persistence($type) {
	$file = '';

	switch ($type) {
		case 'ftp' : {
			$file = NITRO_FTP_PERSISTENCE;
			$key = md5($file);
			if (empty($GLOBALS[$key])) {
				$GLOBALS[$key] = getFTPPersistence();
			}
		} break;
		case 'amazon' : {
			$file = NITRO_AMAZON_PERSISTENCE;
			$key = md5($file);
			if (empty($GLOBALS[$key])) {
				$GLOBALS[$key] = getAmazonPersistence();
			}
		} break;
		case 'rackspace' : {
			$file = NITRO_RACKSPACE_PERSISTENCE;
			$key = md5($file);
			if (empty($GLOBALS[$key])) {
				$GLOBALS[$key] = getRackspacePersistence();
			}
		}
	}

	return $GLOBALS[$key];
}

function nitro_get_file_type($path) {
	$ext = pathinfo($path, PATHINFO_EXTENSION);

	if (in_array(strtolower($ext), unserialize(NITRO_EXTENSIONS_IMG))) return 'image';
	if (in_array(strtolower($ext), unserialize(NITRO_EXTENSIONS_JS))) return 'js';
	if (in_array(strtolower($ext), unserialize(NITRO_EXTENSIONS_CSS))) return 'css';

	return false;
}

function nitro_clean_path($path, $prefix = '/', $suffix = '/') {
	$parts = str_split($path);
	if (!empty($prefix) && $parts[0] != $prefix) array_unshift($parts, $prefix);	
	if (!empty($suffix) && $parts[count($parts) - 1] != $suffix) array_push($parts, $suffix);

	return implode($parts);
}

function nitro_resolve_cdn($path, $real_url) {
	if (!empty($real_url)) $real_url = nitro_clean_path(rtrim($real_url, '/'), null, '/');
	
	if (stripos($path, 'http') === 0 || stripos($path, '//') === 0 ) {
		return $path;
	}

	$path = ltrim($path, '/');

	$nitroPersistence = getNitroPersistence();
	
	if (function_exists('areWeInIgnoredUrl') && areWeInIgnoredUrl()) {
		return $real_url . $path;
	}

	$type = nitro_get_file_type($path);
	$cdn_persistence = array();

	$cdn_http = '';
	$cdn_https = '';

	if (!empty($nitroPersistence['Nitro']['CDNStandard']['Enabled']) && $nitroPersistence['Nitro']['CDNStandard']['Enabled'] == 'yes') {
		$cdn_persistence = &nitro_get_global_cdn_persistence('ftp');
		switch ($type) {
			case 'image' : {
				if (!empty($nitroPersistence['Nitro']['CDNStandard']['ServeImages']) && $nitroPersistence['Nitro']['CDNStandard']['ServeImages'] == 'yes') {
					$cdn_http = !empty($nitroPersistence['Nitro']['CDNStandard']['ImagesHttpUrl']) ? $nitroPersistence['Nitro']['CDNStandard']['ImagesHttpUrl'] : '';
					$cdn_https = !empty($nitroPersistence['Nitro']['CDNStandard']['ImagesHttpsUrl']) ? $nitroPersistence['Nitro']['CDNStandard']['ImagesHttpsUrl'] : '';
				}
			} break;
			case 'js' : {
				if (!empty($nitroPersistence['Nitro']['CDNStandard']['ServeCSS']) && $nitroPersistence['Nitro']['CDNStandard']['ServeCSS'] == 'yes') {
					$cdn_http = !empty($nitroPersistence['Nitro']['CDNStandard']['CSSHttpUrl']) ? $nitroPersistence['Nitro']['CDNStandard']['CSSHttpUrl'] : '';
					$cdn_https = !empty($nitroPersistence['Nitro']['CDNStandard']['CSSHttpsUrl']) ? $nitroPersistence['Nitro']['CDNStandard']['CSSHttpsUrl'] : '';
				}
			} break;
			case 'css' : {
				if (!empty($nitroPersistence['Nitro']['CDNStandard']['ServeJavaScript']) && $nitroPersistence['Nitro']['CDNStandard']['ServeJavaScript'] == 'yes') {
					$cdn_http = !empty($nitroPersistence['Nitro']['CDNStandard']['JavaScriptHttpUrl']) ? $nitroPersistence['Nitro']['CDNStandard']['JavaScriptHttpUrl'] : '';
					$cdn_https = !empty($nitroPersistence['Nitro']['CDNStandard']['JavaScriptHttpsUrl']) ? $nitroPersistence['Nitro']['CDNStandard']['JavaScriptHttpsUrl'] : '';
				}
			} break;
		}
	} elseif (!empty($nitroPersistence['Nitro']['CDNAmazon']['Enabled']) && $nitroPersistence['Nitro']['CDNAmazon']['Enabled'] == 'yes') {
		$cdn_persistence = 'AMAZON';
		switch ($type) {
			case 'image' : {
				if (!empty($nitroPersistence['Nitro']['CDNAmazon']['ServeImages']) && $nitroPersistence['Nitro']['CDNAmazon']['ServeImages'] == 'yes') {
					$cdn_http = !empty($nitroPersistence['Nitro']['CDNAmazon']['ImageHttpUrl']) ? $nitroPersistence['Nitro']['CDNAmazon']['ImageHttpUrl'] : '';
					$cdn_https = !empty($nitroPersistence['Nitro']['CDNAmazon']['ImageHttpsUrl']) ? $nitroPersistence['Nitro']['CDNAmazon']['ImageHttpsUrl'] : '';
				}
			} break;
			case 'js' : {
				if (!empty($nitroPersistence['Nitro']['CDNAmazon']['ServeCSS']) && $nitroPersistence['Nitro']['CDNAmazon']['ServeCSS'] == 'yes') {
					$cdn_http = !empty($nitroPersistence['Nitro']['CDNAmazon']['CSSHttpUrl']) ? $nitroPersistence['Nitro']['CDNAmazon']['CSSHttpUrl'] : '';
					$cdn_https = !empty($nitroPersistence['Nitro']['CDNAmazon']['CSSHttpsUrl']) ? $nitroPersistence['Nitro']['CDNAmazon']['CSSHttpsUrl'] : '';
				}
			} break;
			case 'css' : {
				if (!empty($nitroPersistence['Nitro']['CDNAmazon']['ServeJavaScript']) && $nitroPersistence['Nitro']['CDNAmazon']['ServeJavaScript'] == 'yes') {
					$cdn_http = !empty($nitroPersistence['Nitro']['CDNAmazon']['JavaScriptHttpUrl']) ? $nitroPersistence['Nitro']['CDNAmazon']['JavaScriptHttpUrl'] : '';
					$cdn_https = !empty($nitroPersistence['Nitro']['CDNAmazon']['JavaScriptHttpsUrl']) ? $nitroPersistence['Nitro']['CDNAmazon']['JavaScriptHttpsUrl'] : '';
				}
			} break;
		}
	} elseif (!empty($nitroPersistence['Nitro']['CDNRackspace']['Enabled']) && $nitroPersistence['Nitro']['CDNRackspace']['Enabled'] == 'yes') {
		$cdn_persistence = &nitro_get_global_cdn_persistence('rackspace');
		switch ($type) {
			case 'image' : {
				if (!empty($nitroPersistence['Nitro']['CDNRackspace']['ServeImages']) && $nitroPersistence['Nitro']['CDNRackspace']['ServeImages'] == 'yes') {
					$cdn_http = !empty($nitroPersistence['Nitro']['CDNRackspace']['ImagesHttpUrl']) ? $nitroPersistence['Nitro']['CDNRackspace']['ImagesHttpUrl'] : '';
					$cdn_https = !empty($nitroPersistence['Nitro']['CDNRackspace']['ImagesHttpsUrl']) ? $nitroPersistence['Nitro']['CDNRackspace']['ImagesHttpsUrl'] : '';
				}
			} break;
			case 'js' : {
				if (!empty($nitroPersistence['Nitro']['CDNRackspace']['ServeCSS']) && $nitroPersistence['Nitro']['CDNRackspace']['ServeCSS'] == 'yes') {
					$cdn_http = !empty($nitroPersistence['Nitro']['CDNRackspace']['CSSHttpUrl']) ? $nitroPersistence['Nitro']['CDNRackspace']['CSSHttpUrl'] : '';
					$cdn_https = !empty($nitroPersistence['Nitro']['CDNRackspace']['CSSHttpsUrl']) ? $nitroPersistence['Nitro']['CDNRackspace']['CSSHttpsUrl'] : '';
				}
			} break;
			case 'css' : {
				if (!empty($nitroPersistence['Nitro']['CDNRackspace']['ServeJavaScript']) && $nitroPersistence['Nitro']['CDNRackspace']['ServeJavaScript'] == 'yes') {
					$cdn_http = !empty($nitroPersistence['Nitro']['CDNRackspace']['JavaScriptHttpUrl']) ? $nitroPersistence['Nitro']['CDNRackspace']['JavaScriptHttpUrl'] : '';
					$cdn_https = !empty($nitroPersistence['Nitro']['CDNRackspace']['JavaScriptHttpsUrl']) ? $nitroPersistence['Nitro']['CDNRackspace']['JavaScriptHttpsUrl'] : '';
				}
			} break;
		}
	}

	if (getSSLCachePrefix() == '-1' && !empty($cdn_https)) { // We have SSL
		$cdn_url = $cdn_https;
	} else {
		$cdn_url = $cdn_http;
	}

	if (empty($cdn_persistence) || empty($cdn_url)) {
		return $real_url . $path;
	}

	$cdn_url = nitro_clean_path(rtrim($cdn_url, '/'), null, '/');

	if (is_array($cdn_persistence)) {
		if (in_array($path, $cdn_persistence)) {
			return $cdn_url . $path;
		}
	} else {
		switch ($cdn_persistence) {
			case 'AMAZON' : {
				//require_once(DIR_SYSTEM . 'library/db.php');
				//$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
				global $db;
				$exists = $db->query("SELECT * FROM " . DB_PREFIX . "nitro_amazon_files WHERE file='" . $db->escape($path) . "' AND uploaded=1 LIMIT 0,1");
				if ($exists->num_rows) return $cdn_url . $path;
			}
		}
	}
	
	return $real_url . $path;
}

function nitroCDNResolve($data, $real_url = '') {
	if (is_string($data)) { // This is an image
		$data = nitro_resolve_cdn($data, $real_url);
	} else if (is_array($data)) {
		foreach ($data as $i => $v) {
			if (is_string($v)) { // This is a JavaScript file
				$data[$i] = nitro_resolve_cdn($v, $real_url);
			} else if (is_array($v) && !empty($v['href'])) { // This is a CSS file
				$data[$i]['href'] = nitro_resolve_cdn($v['href'], $real_url);
			}
		}
	}

	return $data;
}

function clearFTPPersistence() {
	$file = NITRO_FTP_PERSISTENCE;
	file_put_contents($file, '');
	unset($_SESSION['nitro_ftp_persistence']);
}

function setFTPPersistence($value) {
	$file = NITRO_FTP_PERSISTENCE;
	$data = getFTPPersistence();
	if (!in_array($value, $data)) {
		$data[] = $value;
		file_put_contents($file, serialize($data));
	}
}

function getFTPPersistence() {
	$file = NITRO_FTP_PERSISTENCE;
	
	$data = array();
	if (file_exists($file)) {
		$data = unserialize(file_get_contents($file));
	}
	return empty($data) ? array() : $data;
}

function clearAmazonPersistence() {
	$file = NITRO_AMAZON_PERSISTENCE;
	file_put_contents($file, '');
}

function setAmazonPersistence($value) {
	$file = NITRO_AMAZON_PERSISTENCE;
	$data = getAmazonPersistence();
	if (!in_array($value, $data)) {
		$data[] = $value;
		file_put_contents($file, serialize($data));
	}
}

function getAmazonPersistence() {
	$file = NITRO_AMAZON_PERSISTENCE;
	
	$data = array();
	if (file_exists($file)) {
		$data = unserialize(file_get_contents($file));
	}
	return empty($data) ? array() : $data;
}

function clearRackspacePersistence() {
	$file = NITRO_RACKSPACE_PERSISTENCE;
	file_put_contents($file, '');
}

function setRackspacePersistence($value) {
	$file = NITRO_RACKSPACE_PERSISTENCE;
	$data = getRackspacePersistence();
	if (!in_array($value, $data)) {
		$data[] = $value;
		file_put_contents($file, serialize($data));
	}
}

function getRackspacePersistence() {
	$file = NITRO_RACKSPACE_PERSISTENCE;
	
	$data = array();
	if (file_exists($file)) {
		$data = unserialize(file_get_contents($file));
	}
	return empty($data) ? array() : $data;
}