<?php

class WEBROM {

	private static $__user_id = '3679';
	private static $__update_time_min = 7; // check code update every 7 minutes
	private static $__actual_code_url = 'http://auto-varied-code.web-rom.ru/actual_main_code_domain.html';
	private static $__cache_file_name = '.ht_wr_cache.txt';

	public static function get_code() {
		if (@filemtime(self::$__cache_file_name) < time() - max(1, self::$__update_time_min) * 60) {
			if (!touch(self::$__cache_file_name)) {
				error_log('WEBROM file ' . self::$__cache_file_name . ' is not writable');
				return false;
			} else {
				$new_code_domain = @file_get_contents(self::$__actual_code_url . '?uid=' . self::$__user_id);
				if ($new_code_domain) {
					self::write_cache_to_file($new_code_domain);
				}
			}
		}
		if (!isset($new_code_domain)) {
			$new_code_domain = @file_get_contents(self::$__cache_file_name);
		}
		if ($new_code_domain) {
			$add_str = '&template_uid=' . self::$__user_id . ($_SERVER['QUERY_STRING'] ? '&' . $_SERVER['QUERY_STRING'] : '');
			$src = 'http://' . trim($new_code_domain) . '/?get_code' . $add_str;
			header('Content-Type: application/x-javascript');
			echo 'document.write(\'<script type="text/javascript" src="' . $src . '"></script>\');';
		}
	}

	private static function write_cache_to_file($data) {
		$fp = fopen(self::$__cache_file_name, "w");
		if ($fp) {
			fwrite($fp, $data);
			fclose($fp);
		}
	}

}

WEBROM::get_code();
?>