<?php

    lt_include( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );
    
    /**
	* offers features to insert SAPE codes in template.
	*/
    class PluginSapeClient extends PluginBase
    {
        var $pluginEnabled;
		var $sapeuser;
		var $sape;
        
        function PluginSapeClient( $source = "" )
        {
			$this->PluginBase($source);
			
            $this->id = "sapeclient";
            $this->author = "ModernStudio";
            $this->desc = "This plugin offers features to insert SAPE codes in template";
            $this->version = "20081119";
  
            $this->locales = Array( "en_UK" , "ru_RU" );
            
			if( $source == "admin" )
				$this->initAdmin();
        }

		function initAdmin()
		{
			$this->registerAdminAction( "sapeclient", "PluginSapeClientConfigAction" );
			$this->registerAdminAction( "updatesapeclient", "PluginSapeClientUpdateConfigAction" );
			
			$menu =& Menu::getMenu();
            $this->addMenuEntry( "/menu/controlCenter/manageSettings", "SapeClient", "?op=sapeclient");
		}

		function register()
		{
		    $blogSettings = $this->blogInfo->getSettings();
		    $this->pluginEnabled = $blogSettings->getValue( "plugin_sapeclient_enabled" );
		    $this->sapeuser = $blogSettings->getValue( "plugin_sapeclient_sapeuser" );

	    }
	    
	    function isEnabled()
	    {
	        return $this->pluginEnabled;
	    }
        
    /**
		* Returns sape code
		*/
        function getSapeLinks($n=0){
            $sapeCode = "";
	    $n = intval($n);
	    if ($n > 5) {
		$n = 5;
	    }
            if( $this->isEnabled() ){
		define('_SAPE_USER', $this->sapeuser); 
		require_once($_SERVER['DOCUMENT_ROOT']."/sape/sape.php");
		$o['multi_site'] = true;
		$o['host'] = $_SERVER['HTTP_HOST'];
		$o['charset'] = 'UTF-8';
		$sape = new SAPE_client($o);
		if ($n) {
                    $sapeCode = $sape->return_links($n);
		} else {
                    $sapeCode = $sape->return_links();
		}
		unset($o);
            }
	    return $sapeCode;
        }
        
    }
?>
