_host = $host; } else { $this->_host = $_SERVER['HTTP_HOST']; } $this->_host = preg_replace('/^http:\/\//', '', $this->_host); $this->_host = preg_replace('/^www\./', '', $this->_host); // Какая страница? if (isset($options['request_uri']) && strlen($options['request_uri'])) { $this->_request_uri = $options['request_uri']; } else { $this->_request_uri = $_SERVER['REQUEST_URI']; } // На случай, если хочется много сайтов в одной папке if (isset($options['multi_site']) && $options['multi_site'] == true) { $this->_multi_site = true; } // Сообщать об ошибках if (isset($options['verbose']) && $options['verbose'] == true) { $this->_verbose = true; } // Кодировка if (isset($options['charset']) && strlen($options['charset'])) { $this->_charset = $options['charset']; } else { $this->_charset = 'windows-1251'; } if (isset($options['fetch_remote_type']) && strlen($options['fetch_remote_type'])) { $this->_fetch_remote_type = $options['fetch_remote_type']; } if (isset($options['socket_timeout']) && is_numeric($options['socket_timeout']) && $options['socket_timeout'] > 0) { $this->_socket_timeout = $options['socket_timeout']; } // Всегда выводить чек-код if (isset($options['force_show_code']) && $options['force_show_code'] == true) { $this->_force_show_code = true; } // Выводить информацию о дебаге if (isset($options['debug']) && $options['debug'] == true) { $this->_debug = true; } if (!defined('_SAPE_USER')) { return $this->raise_error('Не задана константа _SAPE_USER'); } // Определяем наш ли робот if (isset($_COOKIE['sape_cookie']) && ($_COOKIE['sape_cookie'] == _SAPE_USER)) { $this->_is_our_bot = true; if (isset($_COOKIE['sape_debug']) && ($_COOKIE['sape_debug'] == 1)){ $this->_debug = true; } } else { $this->_is_our_bot = false; } } /* * Функция для подключения к удалённому серверу */ function fetch_remote_file($host, $path) { $user_agent = $this->_user_agent.' '.$this->_version; @ini_set('allow_url_fopen', 1); @ini_set('default_socket_timeout', $this->_socket_timeout); @ini_set('user_agent', $user_agent); if ( $this->_fetch_remote_type == 'file_get_contents' || ( $this->_fetch_remote_type == '' && function_exists('file_get_contents') && ini_get('allow_url_fopen') == 1 ) ) { $this->_fetch_remote_type = 'file_get_contents'; if ($data = @file_get_contents('http://' . $host . $path)) { return $data; } } elseif ( $this->_fetch_remote_type == 'curl' || ( $this->_fetch_remote_type == '' && function_exists('curl_init') ) ) { $this->_fetch_remote_type = 'curl'; if ($ch = @curl_init()) { @curl_setopt($ch, CURLOPT_URL, 'http://' . $host . $path); @curl_setopt($ch, CURLOPT_HEADER, false); @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->_socket_timeout); @curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); if ($data = @curl_exec($ch)) { return $data; } @curl_close($ch); } } else { $this->_fetch_remote_type = 'socket'; $buff = ''; $fp = @fsockopen($host, 80, $errno, $errstr, $this->_socket_timeout); if ($fp) { @fputs($fp, "GET {$path} HTTP/1.0\r\nHost: {$host}\r\n"); @fputs($fp, "User-Agent: {$user_agent}\r\n\r\n"); while (!@feof($fp)) { $buff .= @fgets($fp, 128); } @fclose($fp); $page = explode("\r\n\r\n", $buff); return $page[1]; } } return $this->raise_error('Не могу подключиться к серверу: ' . $host . $path.', type: '.$this->_fetch_remote_type); } /* * Функция чтения из локального файла */ function _read($filename) { $fp = @fopen($filename, 'rb'); @flock($fp, LOCK_SH); if ($fp) { clearstatcache(); $length = @filesize($filename); $mqr = @get_magic_quotes_runtime(); @set_magic_quotes_runtime(0); if ($length) { $data = @fread($fp, $length); } else { $data = ''; } @set_magic_quotes_runtime($mqr); @flock($fp, LOCK_UN); @fclose($fp); return $data; } return $this->raise_error('Не могу считать данные из файла: ' . $filename); } /* * Функция записи в локальный файл */ function _write($filename, $data) { $fp = @fopen($filename, 'ab'); if ($fp) { if (flock($fp, LOCK_EX|LOCK_NB)) { $length = strlen($data); ftruncate($fp, 0); @fwrite($fp, $data, $length); @flock($fp, LOCK_UN); @fclose($fp); if (md5($this->_read($filename)) != md5($data)) { @unlink($filename); return $this->raise_error('Нарушена целостность данных при записи в файл: ' . $filename); } } else { return false; } return true; } return $this->raise_error('Не могу записать данные в файл: ' . $filename); } /* * Функция обработки ошибок */ function raise_error($e) { $this->_error = '
SAPE ERROR: ' . $e . '
'; if ($this->_verbose == true) { print $this->_error; } return false; } function load_data() { $this->_db_file = $this->_get_db_file(); if (!is_file($this->_db_file)) { // Пытаемся создать файл. if (@touch($this->_db_file)) { @chmod($this->_db_file, 0666); // Права доступа } else { return $this->raise_error('Нет файла ' . $this->_db_file . '. Создать не удалось. Выставите права 777 на папку.'); } } if (!is_writable($this->_db_file)) { return $this->raise_error('Нет доступа на запись к файлу: ' . $this->_db_file . '! Выставите права 777 на папку.'); } @clearstatcache(); $data = $this->_read($this->_db_file); if ( !$this->_is_our_bot && ( filemtime($this->_db_file) < (time()-$this->_cache_lifetime) || filesize($this->_db_file) == 0 || @unserialize($data) == false ) ) { // Чтобы не повесить площадку клиента и чтобы не было одновременных запросов @touch($this->_db_file, (time() - $this->_cache_lifetime + $this->_cache_reloadtime)); $path = $this->_get_dispenser_path(); if (strlen($this->_charset)) { $path .= '&charset=' . $this->_charset; } foreach ($this->_server_list as $i => $server){ if ($data = $this->fetch_remote_file($server, $path)) { if (substr($data, 0, 12) == 'FATAL ERROR:') { $this->raise_error($data); } else { // [псевдо]проверка целостности: $hash = @unserialize($data); if ($hash != false) { // попытаемся записать кодировку в кеш $hash['__sape_charset__'] = $this->_charset; $hash['__last_update__'] = time(); $hash['__multi_site__'] = $this->_multi_site; $hash['__fetch_remote_type__'] = $this->_fetch_remote_type; $hash['__php_version__'] = phpversion(); $hash['__server_software__'] = $_SERVER['SERVER_SOFTWARE']; $data_new = @serialize($hash); if ($data_new) { $data = $data_new; } $this->_write($this->_db_file, $data); break; } } } } } // Убиваем PHPSESSID if (strlen(session_id())) { $session = session_name() . '=' . session_id(); $this->_request_uri = str_replace(array('?'.$session,'&'.$session), '', $this->_request_uri); } $this->set_data(@unserialize($data)); } } class SAPE_client extends SAPE_base { var $_links_delimiter = ''; var $_links = array(); var $_links_page = array(); var $_user_agent = 'SAPE_Client PHP'; function SAPE_client($options = null) { parent::SAPE_base($options); $this->load_data(); } /* * Ccылки можно показывать по частям */ function return_links($n = null, $offset = 0) { if (is_array($this->_links_page)) { $total_page_links = count($this->_links_page); if (!is_numeric($n) || $n > $total_page_links) { $n = $total_page_links; } $links = array(); for ($i = 1; $i <= $n; $i++) { if ($offset > 0 && $i <= $offset) { array_shift($this->_links_page); } else { $links[] = array_shift($this->_links_page); } } $html = join($this->_links_delimiter, $links); // если запрошена определенная кодировка, и известна кодировка кеша, и они разные, конвертируем в заданную if ( strlen($this->_charset) > 0 && strlen($this->_sape_charset) > 0 && $this->_sape_charset != $this->_charset && function_exists('iconv') ) { $new_html = @iconv($this->_sape_charset, $this->_charset, $html); if ($new_html) { $html = $new_html; } } if ($this->_is_our_bot) { $html = '