<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

if (!config::get('enabled')
	|| !config::get('sape_user')
	|| $_SERVER['SERVER_ADDR']==$_SERVER['REMOTE_ADDR']) return;

if (!defined('_SAPE_USER')) {
	define('_SAPE_USER', config::get('sape_user')); 
}

Hook::call('sape_before');

require_once dirname(__FILE__).'/sape.php'; 

class SAPE_client2 extends SAPE_client
{
	public $_db_path='';
	function __construct($options = null)
	{
		if ($options['db_path']) $this->_db_path=$options['db_path'];
		parent::SAPE_client($options);
	}
	
	function _get_db_file()
	{
		if (!$this->_db_path) $this->_db_path=dirname(__FILE__);
		if ($this->_multi_site) {
			return $this->_db_path . '/' . $this->_host . '.links.db';
		} else {
			return $this->_db_path . '/links.db';
		}
	}
}

class SAPE_articles2 extends SAPE_articles
{
	public $_db_path='';
	function __construct($options = null)
	{
		if ($options['db_path']) $this->_db_path=$options['db_path'];
		parent::SAPE_articles($options);
	}
	
	function _get_db_file()
	{
		if (!$this->_db_path) $this->_db_path=dirname(__FILE__);
		if ($this->_multi_site) {
			return $this->_db_path . '/' . $this->_host . '.links.db';
		} else {
			return $this->_db_path . '/links.db';
		}
	}
}

if (config::get('check_code')) $o['force_show_code']=true;
$o['charset'] = 'UTF-8';
//обходим проверку продажных ссылок
$tmp=explode('?', $_SERVER['REQUEST_URI']);
$o['request_uri']=$tmp[0];
$o['db_path']=LAB_PATH_DATA.'/cache';

if (config::get('host')) {
	$o['multi_site'] = true;
	$o['host'] = config::get('host');
}

$sape = new SAPE_client2($o);
//unset($o);
$links=array();
if ($sape->_links_page && !is_array($sape->_links_page)) $sape->_links_page=array($sape->_links_page);
if (is_array($sape->_links_page)) {
	$total=count($sape->_links_page);
	for ($i=1;$i<=$total;$i++) {
		$links[$i]=$sape->return_links(1);
	}
}

Tpl::i()->assign('sape', $links);


//$sape = new SAPE_articles2($o);
unset($o);

//Tpl::i()->assign('sape_article', $sape->process_request());

Hook::call('sape_after');