tl_host = $host;
} else {
$this->tl_host = $_SERVER['HTTP_HOST'];
}
$this->tl_host = preg_replace('{^https?://}i', '', $this->tl_host);
$this->tl_host = preg_replace('{^www\.}i', '', $this->tl_host);
$this->tl_host = strtolower( $this->tl_host);
if (isset($options['is_static']) && $options['is_static']) {
$this->tl_is_static = true;
}
if (isset($options['request_uri']) && strlen($options['request_uri']) != 0) {
$this->tl_request_uri = $options['request_uri'];
} else {
if ($this->tl_is_static) {
$this->tl_request_uri = preg_replace( '{\?.*$}', '', $_SERVER['REQUEST_URI']);
$this->tl_request_uri = preg_replace( '{/+}', '/', $this->tl_request_uri);
} else {
$this->tl_request_uri = $_SERVER['REQUEST_URI'];
}
}
$this->tl_request_uri = rawurldecode($this->tl_request_uri);
if (isset($options['multi_site']) && $options['multi_site'] == true) {
$this->tl_multi_site = true;
}
if ((isset($options['verbose']) && $options['verbose']) ||
isset($this->tl_links['__trustlink_debug__'])) {
$this->tl_verbose = true;
}
if (isset($options['charset']) && strlen($options['charset']) != 0) {
$this->tl_charset = $options['charset'];
}
if (isset($options['fetch_remote_type']) && strlen($options['fetch_remote_type']) != 0) {
$this->tl_fetch_remote_type = $options['fetch_remote_type'];
}
if (isset($options['socket_timeout']) && is_numeric($options['socket_timeout']) && $options['socket_timeout'] > 0) {
$this->tl_socket_timeout = $options['socket_timeout'];
}
if ((isset($options['force_show_code']) && $options['force_show_code']) ||
isset($this->tl_links['__trustlink_debug__'])) {
$this->tl_force_show_code = true;
}
if (!defined('TRUSTLINK_USER')) {
return $this->raise_error("Constant TRUSTLINK_USER is not defined.");
}
if (isset($_SERVER['HTTP_TRUSTLINK']) && $_SERVER['HTTP_TRUSTLINK']==TRUSTLINK_USER){
$this->tl_test=true;
$this->tl_isrobot=true;
$this->tl_verbose = true;
}
$this->load_links();
}
function load_links() {
if ($this->tl_multi_site) {
$this->tl_links_db_file = dirname(__FILE__) . '/trustlink.' . $this->tl_host . '.links.db';
} else {
$this->tl_links_db_file = dirname(__FILE__) . '/trustlink.links.db';
}
if (!is_file($this->tl_links_db_file)) {
if (@touch($this->tl_links_db_file, time() - $this->tl_cache_lifetime)) {
@chmod($this->tl_links_db_file, 0666);
} else {
return $this->raise_error("There is no file " . $this->tl_links_db_file . ". Fail to create. Set mode to 777 on the folder.");
}
}
if (!is_writable($this->tl_links_db_file)) {
return $this->raise_error("There is no permissions to write: " . $this->tl_links_db_file . "! Set mode to 777 on the folder.");
}
@clearstatcache();
if (filemtime($this->tl_links_db_file) < (time()-$this->tl_cache_lifetime) ||
(filemtime($this->tl_links_db_file) < (time()-$this->tl_cache_reloadtime) && filesize($this->tl_links_db_file) == 0)) {
@touch($this->tl_links_db_file, time());
$path = '/' . TRUSTLINK_USER . '/' . strtolower( $this->tl_host ) . '/' . strtoupper( $this->tl_charset);
if ($links = $this->fetch_remote_file($this->tl_server, $path)) {
if (substr($links, 0, 12) == 'FATAL ERROR:' && $this->tl_debug) {
$this->raise_error($links);
} else if (@unserialize($links) !== false) {
$this->lc_write($this->tl_links_db_file, $links);
} else if ($this->tl_debug) {
$this->raise_error("Cans't unserialize received data.");
}
}
}
$links = $this->lc_read($this->tl_links_db_file);
$this->tl_file_change_date = gmstrftime ("%d.%m.%Y %H:%M:%S",filectime($this->tl_links_db_file));
$this->tl_file_size = strlen( $links);
if (!$links) {
$this->tl_links = array();
if ($this->tl_debug)
$this->raise_error("Empty file.");
} else if (!$this->tl_links = @unserialize($links)) {
$this->tl_links = array();
if ($this->tl_debug)
$this->raise_error("Can't unserialize data from file.");
}
if (isset($this->tl_links['__trustlink_delimiter__'])) {
$this->tl_links_delimiter = $this->tl_links['__trustlink_delimiter__'];
}
if ($this->tl_test)
{
if (isset($this->tl_links['__test_tl_link__']) && is_array($this->tl_links['__test_tl_link__']))
for ($i=0;$i<$this->tl_test_count;$i++)
$this->tl_links_page[$i]=$this->tl_links['__test_tl_link__'];
} else {
$tl_links_temp=array();
foreach($this->tl_links as $key=>$value){
$tl_links_temp[rawurldecode($key)]=$value;
}
$this->tl_links=$tl_links_temp;
//if ($this->tl_request_uri[strlen($this->tl_request_uri)-1]=='/') $this->tl_request_uri=substr($this->tl_request_uri,0,-1);
$this->tl_links_page=array();
if (array_key_exists($this->tl_request_uri, $this->tl_links) && is_array($this->tl_links[$this->tl_request_uri])) {
$this->tl_links_page = array_merge($this->tl_links_page, $this->tl_links[$this->tl_request_uri]);
}
if (array_key_exists($this->tl_request_uri.'/', $this->tl_links) && is_array($this->tl_links[$this->tl_request_uri.'/'])) {
$this->tl_links_page =array_merge($this->tl_links_page, $this->tl_links[$this->tl_request_uri.'/']);
}
}
$this->tl_links_count = count($this->tl_links_page);
}
function fetch_remote_file($host, $path) {
$user_agent = 'Trustlink Client PHP ' . $this->tl_version;
@ini_set('allow_url_fopen', 1);
@ini_set('default_socket_timeout', $this->tl_socket_timeout);
@ini_set('user_agent', $user_agent);
if (
$this->tl_fetch_remote_type == 'file_get_contents' || (
$this->tl_fetch_remote_type == '' && function_exists('file_get_contents') && ini_get('allow_url_fopen') == 1
)
) {
if ($data = @file_get_contents('http://' . $host . $path)) {
return $data;
}
} elseif (
$this->tl_fetch_remote_type == 'curl' || (
$this->tl_fetch_remote_type == '' && function_exists('curl_init')
)
) {
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->tl_socket_timeout);
@curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
if ($data = @curl_exec($ch)) {
return $data;
}
@curl_close($ch);
}
} else {
$buff = '';
$fp = @fsockopen($host, 80, $errno, $errstr, $this->tl_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("Can't connect to server: " . $host . $path);
}
function lc_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("Can't get data from the file: " . $filename);
}
function lc_write($filename, $data) {
$fp = @fopen($filename, 'wb');
if ($fp) {
@flock($fp, LOCK_EX);
$length = strlen($data);
@fwrite($fp, $data, $length);
@flock($fp, LOCK_UN);
@fclose($fp);
if (md5($this->lc_read($filename)) != md5($data)) {
return $this->raise_error("Integrity was violated while writing to file: " . $filename);
}
return true;
}
return $this->raise_error("Can't write to file: " . $filename);
}
function raise_error($e) {
$this->tl_error = '';
return false;
}
function build_links()
{
$links = $this->tl_links_page;
$result = '';
if (isset($this->tl_links['__trustlink_start__']) && strlen($this->tl_links['__trustlink_start__']) != 0 &&
(in_array($_SERVER['REMOTE_ADDR'], $this->tl_links['__trustlink_robots__']) || $this->tl_force_show_code)
) {
$result .= $this->tl_links['__trustlink_start__'];
}
if (isset($this->tl_links['__trustlink_robots__']) && in_array($_SERVER['REMOTE_ADDR'], $this->tl_links['__trustlink_robots__']) || $this->tl_verbose) {
if ($this->tl_error != '' && $this->tl_debug) {
$result .= $this->tl_error;
}
$result .= '\n";
$result .= "\n';
}
$tpl_filename = dirname(__FILE__)."/".$this->tl_template.".tpl.html";
$tpl = $this->lc_read($tpl_filename);
if (!$tpl)
return $this->raise_error("Template file not found");
if (!preg_match("/<{block}>(.+)<{\/block}>/is", $tpl, $block))
return $this->raise_error("Wrong template format: no <{block}><{/block}> tags");
$tpl = str_replace($block[0], "%s", $tpl);
$block = $block[0];
$blockT = substr($block, 9, -10);
if (strpos($blockT, '<{link}>')===false)
return $this->raise_error("Wrong template format: no <{link}> tag.");
if (strpos($blockT, '<{text}>')===false)
return $this->raise_error("Wrong template format: no <{text}> tag.");
if (strpos($blockT, '<{host}>')===false)
return $this->raise_error("Wrong template format: no <{host}> tag.");
foreach ($links as $link)
{
//$link is array('anchor'=>$anchor,'url'=>$url,'text'=>$text
if (!is_array($link)) {
return $this->raise_error("link must be an array");
} elseif (!isset($link['anchor']) || !isset($link['url']) || !isset($link['text'])) {
return $this->raise_error("format of link must be an array('anchor'=>\$anchor,'url'=>\$url,'text'=>\$text");
} elseif (!($parsed=@parse_url($link['url'])) || !isset($parsed['host'])) {
return $this->raise_error("wrong format of url: ".$link['url']);
}
if (($level=count(explode(".",$parsed['host'])))<2) {
return $this->raise_error("wrong host: ".$parsed['host']." in url ".$link['url']);
}
$host=strtolower(($level>2 && strpos(strtolower($parsed['host']),'www.')===0)?substr($parsed['host'],4):$parsed['host']);
$block = str_replace("<{text}>", $link['text'], $blockT);
$block = str_replace("<{link}>", ''.$link['anchor'].'', $block);
$block = str_replace("<{host}>", $host, $block);
$text .= $block;
}
if (is_array($links) && (count($links)>0)){
$tpl = sprintf($tpl, $text);
$result .= $tpl;
}
if (isset($this->tl_links['__trustlink_end__']) && strlen($this->tl_links['__trustlink_end__']) != 0 &&
(in_array($_SERVER['REMOTE_ADDR'], $this->tl_links['__trustlink_robots__']) || $this->tl_force_show_code)
) {
$result .= $this->tl_links['__trustlink_end__'];
}
if ($this->tl_test && !$this->tl_isrobot)
$result = '';
return $result;
}
}
?>