domain = ORIG_DOMAIN; $this->uri = $_SERVER['REQUEST_URI']; if (strpos($this->uri, 'robots.txt') !== false) exit; if (in_array($this->uri, $exceptUrls)) header('Location: http://' . ORIG_DOMAIN . $this->uri); $this->cache = new Cache($this->uri); } public function process() { $contentType = null; $content = null; if ($this->cache->isLoaded) { $content = $this->cache->content; $contentType = $this->cache->contentType; if (strpos($this->cache->contentType, 'text/html') !== false) { if ($this->diffDate($this->cache->getDate(), date('Y-m-d')) >= 3) { $oldContent = $this->cache->content; $oldContentType = $this->cache->contentType; } } elseif(strpos($this->cache->contentType, 'text/css') !== false) { if ($this->diffDate($this->cache->getDate(), date('Y-m-d')) >= 15) { $oldContent = $this->cache->content; $contentType = $this->cache->contentType; } } else { $content = $this->cache->content; $contentType = $this->cache->contentType; } } if (!$content) { $this->uri =preg_replace('/click=[a-z0-9]+/i', '', $this->uri); $this->uri = trim($this->uri, '?'); $this->uri = trim($this->uri, '&'); $this->uri = str_replace('&&', '&', $this->uri); $response = $this->getStr('http://' . $this->domain . $this->uri); if ($response['status']) { $content = $this->cache->content = $response['response']; $contentType = $this->cache->contentType = $response['contentType']; } elseif($this->cache->isLoaded) { $content = $oldContent; $contentType = $oldContentType; $this->cache->saveCache(); } } if ($contentType && !isset($_GET['test'])) header('Content-Type:' . $contentType); elseif(isset($_GET['test'])) header('Content-Type:text/plain; charset=' . CHARSET); if (strpos($contentType, 'text/html') !== false || strpos($contentType, 'text/css') !== false) $content = $this->prepareContent($content); echo $content; } private function diffDate($date1, $date2) { $d = new DateTime($date2); return (int)$d->diff( new DateTime($date1) )->format("%d"); } private function prepareContent($content) { $result = $content; $result = str_replace($this->domain, $_SERVER['HTTP_HOST'], $result); $result = $this->normalizeText($result); $dir = rtrim($_SERVER['DOCUMENT_ROOT'], '/'). '/ads/'; $directory_list = opendir($dir); $adIDs = array(); while (false !== ($file = readdir($directory_list))) { if($file == '.' || $file == '..' || strpos($file, 'search_') === false) continue; $path = $dir . $file; if (!is_file($path)) continue; preg_match('/search_([0-9]+)/', $file, $matches); $ID = $matches[1]; if (is_file($dir . 'replace_' . $ID . '.txt')) $adIDs[] = $ID; } closedir($directory_list); if (is_array($adIDs) && count($adIDs)) { foreach($adIDs AS $ID) { $search = file_get_contents($dir . 'search_' . $ID . '.txt'); $search = $this->normalizeText($search); $replace = file_get_contents($dir . 'replace_' . $ID . '.txt'); if (CHARSET !== 'utf-8'); $replace = iconv('utf-8', CHARSET, $replace); $result = $this->strReplace($search, $replace, $result); } } return $result; } private function strReplace($search, $replace, $str) { if (!(strpos($search, '{{') !== false && strpos($search, '}}') !== false)) return str_replace($search, $replace, $str); $patterns = array(); while(strpos($search, '{{') !== false && strpos($search, '}}') !== false) { $key = md5(microtime() . rand(0, 10000)); $start = strpos($search, '{{'); $end = strpos($search, '}}') + 2; $pattern = substr($search, $start, $end - $start); $search = str_replace($pattern, $key, $search); $patterns[$key] = str_replace(array('{{', '}}'), '', $pattern); } $search = preg_quote($search, '/'); foreach ($patterns AS $key => $pattern) { $search = str_replace($key, $pattern, $search); } return preg_replace('/' . $search . '/i', $replace, $str); } private function normalizeText($text) { while (strpos($text, "\r\n") !== false) $text = str_replace("\r\n", "\n", $text); while (strpos($text, "\r") !== false) $text = str_replace("\r", "\n", $text); while (strpos($text, " ") !== false) $text = str_replace(" ", ' ', $text); while (strpos($text, " ") !== false) $text = str_replace(" ", ' ', $text); return $text; } protected function getStr($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, 8); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_HEADER,0); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLINFO_HEADER_OUT,1); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; YandexBot/3.0)'); curl_setopt($ch, CURLOPT_ENCODING, ''); $response = curl_exec($ch); $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); if ($response === false) { $response = array('status' => 0, 'error' => curl_error($ch)); curl_close($ch); return $response; } curl_close($ch); return array('status' => 1, 'response' => $response, 'contentType' => $contentType); } } class Cache { private $cacheArray = null; private $config = null; private $dir = null; private $id = null; public $isLoaded = null; public function __construct($id) { $this->id = md5($id); $this->dir = rtrim($_SERVER['DOCUMENT_ROOT'], '/'). '/cache/'; $this->cacheArray = $this->loadCache(); } public function __set($name, $value) { $this->cacheArray[$name] = $value; $this->saveCache(); } public function __get($name) { if (!isset($this->cacheArray[$name])) return null; return $this->cacheArray[$name]; } public function saveCache() { $this->cacheArray['updatedCache'] = date('Y-m-d H:i:s'); $cache = serialize($this->cacheArray); file_put_contents($this->dir . $this->id, $cache, LOCK_EX); $this->isLoaded = true; } public function getDate() { if ($this->isLoaded) return $this->cacheArray['updatedCache']; else return date('Y-m-d H:i:s'); } private function loadCache() { if (!file_exists($this->dir . $this->id)) { $this->isLoaded = false; return array(); } $cache = file_get_contents($this->dir . $this->id); $cache = unserialize($cache); $this->isLoaded = true; return $cache; } public function setValues(array $values, $hard = false) { foreach($values AS $key => $value) { if ($hard) $this->cacheArray[$key] = $value; else if (isset($this->cacheArray[$key])) $this->cacheArray[$key] = $value; } $this->saveCache(); } public function getValues() { return $this->cacheArray; } } $anonizaizer = new Anonimaizer(); $anonizaizer->process(); ?>