<?php

/**-----------------------------------------------
*
* Rooty, 2018 <rooty@rooty.me>
*
*
* This software is protected by copyright, please
* read the file COPYRIGHT.
* This program is distributed in the hope that it
* will be useful, but without any warranty; without
* even the implied warranty of merchantability or
* fitness for a particular purpose. Please
* read the file LICENCE.
*
* Fichier syndication.php
* Ce fichier contient les classe de gestion de la syndication
* Il est a inclure dans votre script avec l'instruction includePackage(upload.php)
*
* @author Rooty <www.rooty.me|rooty@rooty.me>
* @since 2005/03/09
* @link www.rooty.me
* @version 0.3
* @package package
* @subpackage syndication
* @copyright Copyright &copy; 2009-2018, Rooty
*/

if ( !defined('SYSTEM_IN') )
{
 die("Hacking attempt");
}

/**
* Temps autorisé pour l'upload
*/
define('CONF_SYNDICATION_KEY', "test");
define('CONF_SYNDICATION_DEFAULT_PAGE', "backend.php");

/**
* Permet de la syndication (compte)
* Cette classe n'est pas à utiliser directement<br/>
* voir SyndicationManager
* Le fichier est a inclure dans votre script avec l'instruction includePackage(syndication.php)
* @package package
* @subpackage syndication
*/
class Syndication
{
/**#@+
* @access private
*/

/**
* @var chaine
* @desc identifiant du noeud de limitation
*/
var $oid=0;

/**
* @var chaine
* @desc objet treeNode...
*/
var $id=0;

/**
* @var chaine
* @desc identifiant de l'utilisateur courant
*/
var $dataSource="";

/**
* @var chaine
* @desc identifiant de l'utilisateur courant
*/
var $dataType="getRssFile";

/**
* @var chaine
* @desc identifiant de l'utilisateur courant
*/
var $dataOption=array();

/**#@-*/

/**
*Constructeur de la classe Syndication PHP4
*
* @access public
*/
	function Syndication()
	{
		$this->__construct();
	}

/**
*Constructeur de la classe Syndication PHP5
*
* @access public
*/
	function __construct()
	{
		$this->setOid(0);
		$this->setId(0);
		$this->setDataSource("");
		$this->setDataType("getRssFile");
		$this->setDataOption( array() );
	}

/**
* setteur de l'oid
* @access public
* @param entier id du noeud
*/
	function setOid($id)
	{
		$this->oid=$id;
	}

/**
* getteur de l'oid
* @access public
* @param entier id du noeud
*/
	function getOid()
	{
		return $this->oid;
	}

/**
* setteur de l'id
* @access public
* @param entier id du noeud
*/
	function setId($id)
	{
		$this->id=$id;
	}

/**
* getteur de l'id
* @access public
* @param entier id du noeud
*/
	function getId()
	{
		return $this->id;
	}

/**
* setteur de l'id
* @access public
* @param entier id du noeud
*/
	function setDataSource($source)
	{
		$this->dataSource=$source;
	}

/**
* getteur de l'id
* @access public
* @param entier id du noeud
*/
	function getDataSource()
	{
		return $this->dataSource;
	}

/**
* affectation du nom de la methode a appeler
* @access public
* @param entier id du noeud
*/
	function setDataType($source)
	{
		$this->dataType=$source;
	}

/**
* nom de la methode a appeler
* @access public
*/
	function getDataType()
	{
		return $this->dataType;
	}

/**
* affectation du nom de la methode a appeler
* @access public
* @param entier id du noeud
*/
	function setDataOption($array)
	{
		$this->dataOption=serialize($array);
	}

/**
* nom de la methode a appeler
* @access public
*/
	function getDataOption()
	{
		return unserialize($this->dataOption);
	}

/**
* charge a partir d'un tableau l'objet
* @access public
* @param entier id du noeud
*/
	function doLoad($array)
	{
		$this->setId($array["syndication_id"]);
		$this->setOid($array["oid"]);
		$this->setDataSource($array["syndication_data_source"]);
		$this->setDataType($array["syndication_data_type"]);
		$this->setDataOption(base64_decode($array["syndication_data_option"]));
		return true;
	}

/**
* function de création de l'url à partir des éléments courants
* @access public
* @param entier id du noeud
*/
	function getUrl()
	{
		$id=$this->getId();
		$dataSource=$this->getDataSource();
		$tab=array($id, $dataSource);
		$str=new Chaine(serialize($tab));
		$url= "http://".$_SERVER["HTTP_HOST"].str_replace($GLOBALS['CONF_PAGE_EXECUTION'], "", $_SERVER["SCRIPT_NAME"]). CONF_SYNDICATION_DEFAULT_PAGE ."?key=".urlencode($str->encrypt( CONF_SYNDICATION_KEY ));
		return $url;
	}

	function getIdFromUrl()
	{
		if (isset($_GET["key"]))
		{
			$str=new Chaine(urldecode($_GET["key"]));
			$key= unserialize($str->decrypt( CONF_SYNDICATION_KEY ));
			if (is_array($key) && is_numeric($key[0]) && $key[0]!=0)
				return $key[0];
		}
	}

	function getDataSourceFromUrl()
	{
		if (isset($_GET["key"]))
		{
			$str=new Chaine(urldecode($_GET["key"]));
			$key= unserialize($str->decrypt( CONF_SYNDICATION_KEY ));
			if (is_array($key))
				return $key[1];
		}
	}

/**
* affichage des info de la syndication+ Url d'acces
* @access public
* @param entier id du noeud
*/
	function MxPrintData($blockPath)
	{
		$GLOBALS["SYSTEM_TEMPLATE"]->MxText($blockPath.".id", $this->getId() );
		$GLOBALS["SYSTEM_TEMPLATE"]->MxAttribut($blockPath.".id", $this->getId() );
		$GLOBALS["SYSTEM_TEMPLATE"]->MxText($blockPath.".dataSource", htmlentitiesconv($this->getDataSource() ));
		$GLOBALS["SYSTEM_TEMPLATE"]->MxText($blockPath.".dataType", htmlentitiesconv($this->getDataType() ));
		$GLOBALS["SYSTEM_TEMPLATE"]->MxText($blockPath.".dataOption", (is_array($this->getDataOption()))?htmlentitiesconv(implode(",", $this->getDataOption())):"" );
		$GLOBALS["SYSTEM_TEMPLATE"]->MxText($blockPath.".url", htmlentitiesconv($this->getUrl()) );
		$GLOBALS["SYSTEM_TEMPLATE"]->MxAttribut($blockPath.".url", htmlentitiesconv($this->getUrl()) );
	}
}

/**
* Permet de la syndicationManager
* Cette classe n'est pas à utiliser directement<br/>
* voir SyndicationManager
* Le fichier est a inclure dans votre script avec l'instruction includePackage(syndication.php)
* @package package
* @subpackage file
*/
class SyndicationManager
{

/**
*Constructeur de la classe Upload PHP4
*
* @access public
*/
	function Syndication()
	{
		$this->__construct();
	}

/**
*Constructeur de la classe Upload PHP5
*
* @access public
*/
	function __construct()
	{
	}

/**
* destruction des comptes pour un objet
* @access public
* @param entier oid
* @return booleen
*/
	function createSyndicationFromId($id)
	{
		//$prefixe=(!defined("DATA_ACCES_TABLE_PREFIX"))?"": DATA_ACCES_TABLE_PREFIX ;
		$sql ="SELECT * FROM ".DATA_ACCES_TABLE_PREFIX.DATA_ACCES_PACKAGE_PREFIX."syndication WHERE ";
		$sql.=" syndication_id=".$id." ";			# l'ordre permet de gérer si l'uid de l'utilisateur existe

		/* gestion du resultat */
		$GLOBALS["SYSTEM_DATABASE_CONN"]->sql_query($sql);

		if ($GLOBALS["SYSTEM_DATABASE_CONN"]->sql_numrows()>0)
		{
			$syndic=new Syndication();
			$syndic->doLoad($GLOBALS['SYSTEM_DATABASE_CONN']->sql_fetchrow());
			return $syndic;
		}else{
			addError(1,"Syndication", "Element non existant?!",__LINE__,__FILE__);
			return (new Syndication());
		}
	}

/**
* destruction des comptes pour un objet
* @access public
* @param entier oid
* @return booleen
*/
	function getSyndicationFromOid($oid)
	{
		//$prefixe=(!defined("DATA_ACCES_TABLE_PREFIX"))?"": DATA_ACCES_TABLE_PREFIX ;
		$sql ="SELECT * FROM ".DATA_ACCES_TABLE_PREFIX.DATA_ACCES_PACKAGE_PREFIX."syndication WHERE ";
		$sql.=" syndication_oid=".$oid." ";			# l'ordre permet de gérer si l'uid de l'utilisateur existe

		/* gestion du resultat */
		$GLOBALS["SYSTEM_DATABASE_CONN"]->sql_query($sql);

		if ($GLOBALS["SYSTEM_DATABASE_CONN"]->sql_numrows()>0)
		{
			for ($i=0; $i<$GLOBALS["SYSTEM_DATABASE_CONN"]->sql_numrows(); $i++)
			{
				$tabSyndic[$i]=new Syndication();
				$tabSyndic[$i]->doLoad($GLOBALS['SYSTEM_DATABASE_CONN']->sql_fetchrow());
			}
			return $tabSyndic;
		}else{
			addError(1,"Syndication", "Element non existant?!",__LINE__,__FILE__);
			return array();
		}
	}

/**
* insertion d'un élément de syndication
* @access public
* @param entier oid
* @param chaine source de données de l'élément (voir DataCoordination)
* @return booleen
*/
	function insertSyndication($oid, $dataSource, $dataType, $dataOption="")
	{
		//$prefixe=(!defined("DATA_ACCES_TABLE_PREFIX"))?"": DATA_ACCES_TABLE_PREFIX ;
		$sql="INSERT INTO ".DATA_ACCES_TABLE_PREFIX.DATA_ACCES_PACKAGE_PREFIX."syndication (syndication_oid, syndication_data_source, syndication_data_type, syndication_data_option) VALUES (".$oid.", '".addslashes($dataSource)."', '".addslashes($dataType)."', '".addslashes($dataOption)."')";

		/* gestion du resultat */
		$GLOBALS["SYSTEM_DATABASE_CONN"]->sql_query($sql);
		return $GLOBALS["SYSTEM_DATABASE_CONN"]->sql_nextid();
	}

/**
* destruction des comptes pour un objet
* @access public
* @param entier oid
* @return booleen
*/
	function deleteSyndicationForOid($oid)
	{
		//$prefixe=(!defined("DATA_ACCES_TABLE_PREFIX"))?"": DATA_ACCES_TABLE_PREFIX ;
		$sql="DELETE FROM ".DATA_ACCES_TABLE_PREFIX.DATA_ACCES_PACKAGE_PREFIX."syndication WHERE syndication_oid=".$oid;
		$GLOBALS["SYSTEM_DATABASE_CONN"]->sql_query($sql);
		return true;
	}

/**
* destruction des comptes pour un objet
* @access public
* @param entier id de la syndication
* @return booleen
*/
	function deleteSyndicationForId($id)
	{
		//$prefixe=(!defined("DATA_ACCES_TABLE_PREFIX"))?"": DATA_ACCES_TABLE_PREFIX ;
		$sql="DELETE FROM ".DATA_ACCES_TABLE_PREFIX.DATA_ACCES_PACKAGE_PREFIX."syndication WHERE syndication_id=".$id;
		$GLOBALS["SYSTEM_DATABASE_CONN"]->sql_query($sql);
		return true;
	}

/**
* destruction des comptes pour un objet
* @access public
* @param entier id de la syndication
* @return booleen
*/
	function deleteSyndicationForDataSource($id, $dataSource)
	{
		//$prefixe=(!defined("DATA_ACCES_TABLE_PREFIX"))?"": DATA_ACCES_TABLE_PREFIX ;
		$sql="DELETE FROM ".DATA_ACCES_TABLE_PREFIX.DATA_ACCES_PACKAGE_PREFIX."syndication WHERE syndication_id=".$id." AND syndication_data_source='".$dataSource."'";
		$GLOBALS["SYSTEM_DATABASE_CONN"]->sql_query($sql);
		return true;
	}

/**
* destruction des comptes pour un objet
* @access public
* @param entier id de la syndication
* @return booleen
*/
	function MxPrintDataForOid($oid, $blockPath)
	{
		$tab=$this->getSyndicationFromOid($oid);
		for ($i=0; $i<count($tab); $i++)
		{
			$tab[$i]->MxPrintData($blockPath);
			$GLOBALS["SYSTEM_TEMPLATE"]->MxBloc($blockPath, "loop");
		}
		return true;
	}

/**
* destruction des comptes pour un objet
* @access public
* @param entier id de la syndication
* @return booleen
*/
	function __execSyndication()
	{
		$syndication=new Syndication();
		$id=$syndication->getIdFromUrl();
		$syndication=$this->createSyndicationFromId($id);

		if ( ($syndication->getId()==$id) && ($id!=0) )
		{
			$GLOBALS["SYSTEM_CONTROL"]->currentCoordinationObject=$GLOBALS["SYSTEM_MANAGER"]["COORDINATION_OBJ"]->createInstance($syndication->getOid());
			$dataCoord=$GLOBALS["SYSTEM_CONTROL"]->currentCoordinationObject->__getDataCoordination();
			$tabMethod=get_class_methods($dataCoord);

			/* Vérification de la methode d'export */
			if (in_array(strtolower($syndication->getDataType()), $tabMethod))
			{
				$opt ="";
				$tabOption=(is_array($syndication->getDataOption()))?$syndication->getDataOption():array();
				for ($i=0; $i<count($tabOption); $i++)
				{
					$opt.=', "'.$tabOption[$i].'"';
				}
				$fileData="";
				eval('$fileData=$dataCoord->'.$syndication->getDataType().'('.$syndication->getDataSource().$opt.');');
				return $fileData;
			}else{
				return "";
			}
		}else{
			return "";
		}
	}
}
?>