|
deny from all
SetCursorPosition($_GET['SSISTART']);
if(isset($_GET['SSICOUNT']))
echo $sl->GetLinks($_GET['SSICOUNT']);
else
echo $sl->GetLinks();
?>
register_function("sl_get_links", "print_sl_links");
function print_sl_links($params)
{
global $sl;
if(isset($params['start']))
$sl->SetCursorPosition($params['start']);
if(isset($params['count']))
return $sl->GetLinks($params['count']);
else
return $sl->GetLinks();
}
?>
GetLinks();
?>
"mainsite.ru", "sitealias2.ru" => "mainsite.ru")
var $password = '3059289d7abf865b5e31ab48a061a763'; // Пароль
var $encoding = 'WINDOWS-1251'; // Необходимая вам кодировка. (WINDOWS-1251, UTF-8, KOI8-R)
var $server = 'show.setlinks.ru'; // сервер с которого берутся коды ссылок
var $cachetimeout = 600; // Время обновления кэша в секундах
var $errortimeout = 60; // Период обновления кэша после ошибки в секундах
var $cachedir = ''; // Директория куда будет сохраняться кэш(если пусто, то будет сохранен в папке со скриптом), в конце обязателен слэш "/"
var $cachetype = 'FILE'; // тип кэша. (FILE, MYSQL)
var $connecttype = ''; // тип соединения с сервером setlinks. (CURL - использовать библиотеку CURL, SOCKET - использовать сокеты, NONE - не соединяться с сервером, использовать данные кэша)
// если $connecttype пусто, то тип соединения определяется автоматом
var $sockettimeout = 10; // Ожидание кода, секунд
var $indexfile = '^/index.(html|htm|php|phtml|asp)$'; // фильтр индексной страницы
var $use_safe_method = true; // защита от проверки на продажность ссылок, читать тут http://forum.setlinks.ru/showthread.php?p=1506#post1495
var $allow_url_params = ""; // параметры которые могут появлятся в урле через пробел "mod id username"
var $show_comment = true; // если true, то выводить коментарий всем, а не только индексаторам
var $show_errors = true; // выводить или нет ошибки
}
?>
Config = new SLConfig();
if(!empty($uri))
$this->uri = $uri;
else
$this->uri = (isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $HTTP_SERVER_VARS['REQUEST_URI']);
if (strlen(session_id()) > 0) {
$session = session_name()."=".session_id();
$this->uri = str_replace(array('?'.$session,'&'.$session), '', $this->uri);
}
if(empty($this->uri) || (!empty($this->Config->indexfile) && preg_match("!".$this->Config->indexfile."!", $this->uri)))
$this->uri = '/';
if($this->Config->connecttype == 'CURL' AND !function_exists('curl_init')) $this->Error('CURL not found! Библиотека CURL не обнаружена!');
else if($this->Config->connecttype == 'SOCKET' AND !function_exists('fsockopen')) $this->Error('fsockopen not found! Внешние соединения не поддерживаются Вашим хостингом!');
else if($this->Config->connecttype == 'NONE');
else {
if(function_exists('curl_init')) $this->Config->connecttype = 'CURL';
else if(function_exists('fsockopen')) $this->Config->connecttype = 'SOCKET';
else $this->Config->connecttype = 'NONE';
}
$this->host = $_SERVER['HTTP_HOST'];
if(substr($this->host,0,4) == 'www.')
$this->host = substr($this->host, 4);
if(isset($this->Config->aliases[$this->host]))
$this->host = $this->Config->aliases[$this->host];
if(empty($this->Config->cachedir))
$this->Config->cachedir = dirname(__FILE__)."/cache/";
if(!is_dir($this->Config->cachedir))
$this->Error("Can't open cache dir!");
else if(!is_writable($this->Config->cachedir))
$this->Error("Cache dir: Permission denied!");
if(empty($this->cachefile))
$this->cachefile = strtolower($this->host).'.links';
if($this->Config->use_safe_method && trim($this->Config->allow_url_params) !='') {
$prms = explode(" ", $this->Config->allow_url_params);
foreach($prms as $p) $this->_safe_params[] = sprintf("%u", crc32($p));
}
}
function SaveLinksToCache($links, $info)
{
if(count($info)!=6) return false;
unset($info[1]);
if($this->Config->cachetype == "MYSQL") {
mysql_query("replace into sl_params values
('".$this->host."_lcc', '".time()."'),
('".$this->host."_scc', '".intval($info[0])."'),
('".$this->host."_delim', '".$this->MysqlEscapeString($info[2])."'),
('".$this->host."_sid', '".intval($info[3])."')") or $this->Error(mysql_error());
mysql_query("delete from sl_cache where sid='".intval($info[3])."'") or $this->Error(mysql_error());
foreach($links AS $val) {
if(count($val) > 1) {
$id = $val[0];
unset($val[0]);
mysql_query("replace into sl_cache values (".intval($info[3]).", '".$this->MysqlEscapeString($id)."', '".$this->MysqlEscapeString(implode("t", $val))."')") or $this->Error(mysql_error());
}
}
return true;
} else if($this->Config->cachetype == "FILE") {
$h = @fopen($this->Config->cachedir.$this->cachefile, "w+");
if($h) {
$info[6] = "0000000000";
@fwrite($h, time()."t".implode("t", $info)."n");
foreach($links AS $val) {
if(count($val)>1)
@fwrite($h, implode("t", $val)."n");
}
@fclose($h);
return true;
} else $this->Error('Can't open cache file!');
} else $this->Error("Cache type unknown!");
return false;
}
function IsCached()
{
if($this->Config->cachetype == "MYSQL") {
$res = @mysql_query("select param_name, param_value from sl_params where param_name like '".$this->host."_%'") or $this->Error(mysql_error());
while($line = mysql_fetch_assoc($res)) {
if($line['param_name'] == $this->host."_scc")
$this->servercachetime = $line['param_value'];
else if($line['param_name'] == $this->host."_lcc")
$this->cachetime = min(time()+24*60*60, $line['param_value']);
else if($line['param_name'] == $this->host."_delim")
$this->delimiter = $line['param_value'];
else if($line['param_name'] == $this->host."_errtime")
$this->errortime = $line['param_value'];
}
} else if($this->Config->cachetype == "FILE") {
if(!is_file($this->Config->cachedir.$this->cachefile)) return false;
$h = @fopen($this->Config->cachedir.$this->cachefile, "r");
if($h) {
$info = explode("t", @fgets($h));
$this->cachetime = min(time()+24*60*60, $info[0]);
$this->servercachetime = $info[1];
$this->delimiter = $info[2];
$this->_safe_params = explode(' ',$info[4]);
$this->_servers = explode(' ',$info[5]);
$this->errortime = $info[6];
@fclose($h);
}
}
if( ($this->cachetime + $this->Config->cachetimeout > time()) || ($this->errortime + $this->Config->errortimeout > time()) )
return true;
return false;
}
function GetLinks($countlinks=0, $delimiter=false)
{
static $firstlink=true;
if(!$this->IsCached()) {
if(!$this->DownloadLinks()) {
if($this->Config->cachetype == "MYSQL") {
mysql_query("replace into sl_params values ('".$this->host."_errtime', '".time()."')") or $this->Error(mysql_error());
} else if($this->Config->cachetype == "FILE" && file_exists($this->Config->cachedir.$this->cachefile)) {
$h = fopen($this->Config->cachedir.$this->cachefile, "r+");
if($h) {
$str = fgets($h);
if(strlen($str) > 25) {
fseek($h, strlen($str)-11);
fwrite($h, time());
}
fclose($h);
}
}
}
}
$pageid = sprintf("%u", crc32($this->host . $this->uri));
if($this->links === false) {
if($this->Config->cachetype == "MYSQL") {
$res = mysql_query("select param_value from sl_params where param_name='".$this->host."_delim'") or $this->Error(mysql_error());
$line = mysql_fetch_assoc($res);
$this->delimiter = $line['param_value'];
$res = mysql_query("select param_value from sl_params where param_name='".$this->host."_sid'") or $this->Error(mysql_error());
$sid = mysql_fetch_assoc($res);
$res = mysql_query("select links from sl_cache where sid='".intval($sid['param_value'])."' and id='".$this->MysqlEscapeString($pageid)."'") or $this->Error(mysql_error());
if(mysql_num_rows($res) == 1) {
$this->links = mysql_fetch_assoc($res);
$this->links = explode("t", $this->links['links']);
} else {
$this->links = Array();
}
} else if($this->Config->cachetype == "FILE") {
$h = @fopen($this->Config->cachedir.$this->cachefile, "r");
if($h) {
$info = explode("t", @fgets($h));
$this->servercachetime = $info[0];
$this->cachetime = $info[1];
$this->delimiter = $info[2];
$this->_safe_params = explode(' ',$info[4]);
$this->_servers = explode(' ',$info[5]);
$this->links = Array();
while(!feof($h)) {
$links = explode("t", @fgets($h));
$page_ids = explode(" ", $links[0]);
if($page_ids[0] == -1 || $page_ids[0] == $pageid || ( $this->Config->use_safe_method && $page_ids[1] == $this->SafeUrlCrc32('http://'.$this->host.$this->uri))) {
unset($links[0]);
$this->links = array_merge ($this->links, array_values($links));
}
}
@fclose($h);
}
}
}
$returnlinks = Array();
$cnt = count($this->links);
if($countlinks > 0) $cnt = min($cnt, $this->curlink+$countlinks);
for(; $this->curlink < $cnt; $this->curlink++) {
$returnlinks[] = $this->links[$this->curlink];
}
$user_ip = (isset($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['HTTP_X_REAL_IP'] : $_SERVER['REMOTE_ADDR']);
if($this->Config->show_comment || in_array($user_ip, $this->_servers))
$this->_show_comment = true;
else
$this->_show_comment = false;
$retstring = (($firstlink && $this->_show_comment) ? '' : '').implode(($delimiter===false ? $this->delimiter : $delimiter), $returnlinks);
$firstlink = false;
return $retstring;
}
function DownloadLinks()
{
$page = '';
$path = "/?host=".$this->host."&k=".$this->Config->encoding."&p=".$this->Config->password."&v=".VERSION.($this->Config->use_safe_method ? "&safe" :"");
if($this->Config->connecttype == "CURL") {
$curl = curl_init($this->Config->server.$path);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->Config->sockettimeout);
curl_setopt($curl, CURLOPT_TIMEOUT, $this->Config->sockettimeout);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($curl);
if(curl_error($curl) OR curl_getinfo($curl, CURLINFO_HTTP_CODE) != '200')
{
curl_close($curl);
return false;
}
curl_close($curl);
} else if($this->Config->connecttype == "SOCKET") {
$fp = @fsockopen($this->Config->server, 80);
if(!$fp) {
return false;
} else {
fputs($fp,"GET ".$path.
" HTTP/1.0rnHost: ".$this->Config->server."rnConnection: Closernrn");
socket_set_timeout($fp, $this->Config->sockettimeout);
$page = '';
while(!feof($fp)){
$page .= fread($fp, 2048);
}
$status = socket_get_status($fp);
fclose($fp);
if($status['unread_bytes'] == 0 && $status['timed_out'] != 1) {
$page = substr($page, strpos($page,"rnrn")+4);
} else return false;
}
} else return false;
$page = trim($page);
if(strlen($page) < 20) return false;
$this->SaveLinks($page);
return true;
}
function SaveLinks($page)
{
$info = explode("t", substr($page, 0, strpos($page,"n")));
if($this->Config->password == $info[1]) {
$this->servercachetime = $info[0];
$this->cachetime = time();
$this->delimiter = $info[2];
$this->_safe_params = explode(" ", $info[4]);
$this->_servers = explode(" ", $info[5]);
$this->errortime = 0;
if(isset($info[4]) && $info[4]!='')
$this->_safe_params = explode(" ", $info[4]);
else
$this->_safe_params = Array();
$page = explode("n", substr($page, strpos($page,"n")+1));
foreach($page as $key=>$val)
$page[$key] = explode("t", $val);
if(!$this->SaveLinksToCache($page, $info)) $this->Error('Can't write cache!');
else return true;
} else
$this->Error('Incorrect password!');
return false;
}
function MysqlEscapeString($string)
{
if(get_magic_quotes_gpc())
$string = stripslashes($string);
if(function_exists('mysql_real_escape_string'))
return mysql_real_escape_string($string);
return mysql_escape_string($string);
}
function SetCursorPosition($position)
{
$this->curlink = max(intval($position)-1, 0);
}
function Error($error)
{
if($this->Config->show_errors) print('SetLinks error: '.$error." n");
}
function SafeUrlCrc32($url){
$url = parse_url(trim($url));
if(isset($url['query'])) {
$params = $this->GetQueryParams($url['query']);
if($params !== false) {
ksort($params, SORT_STRING);
$params_string = Array();
foreach($params as $name=>$value) {
if(in_array(sprintf("%u", crc32($name)), $this->_safe_params)) {
if($value === false)
$params_string[]=$name;
else
$params_string[]=$name.'='.$value;
}
}
$params_string = implode('&', $params_string);
}
}
if(isset($url['host']))
$url['host'] = preg_replace('/^(:?www.)/i', '', strtolower($url['host']));
if(!isset($url['path'])) $url['path'] = "/";
if(isset($params_string) && $params_string != '')
$url['query'] = '?'.$params_string;
else
$url['query'] = '';
return sprintf("%u", crc32($url['host'].$url['path'].$url['query']));
}
function GetQueryParams($query){
if(is_null($query) || trim($query)=='') return false;
$params = explode('&', $query);
$out_params=Array();
foreach($params as $val) {
$delimiter_position = strpos($val,'=');
if($delimiter_position === false && $val!='') {
$out_params[$val] = false;
} else if($delimiter_position == 0){
// no name...
} else {
$name = substr($val, 0, $delimiter_position);
$value = substr($val, $delimiter_position+1);
$out_params[strval($name)] = $value;
}
}
return $out_params;
}
// end of class
}
?>
|
|