<?php
# ************** LICENCE ****************
/*
	Copyright (c) PORTANEO.

	This file is part of POSH (Portaneo Open Source Homepage) http://sourceforge.net/projects/posh/.

	POSH is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version

	POSH 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.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Posh.  If not, see <http://www.gnu.org/licenses/>.
*/
# ***************************************
# Encryption class
#
# !! be careful, this file must be saved under uft8 format, and display an e accentuated here : é
# ***************************************

class crypt {
    var $key;
    var $encryptData;
    var $decryptData;
	var $alpha;
    
	function crypt() {
		$this->alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?.;:=+-()/@%$*& ";
		$this->key = isset($_SESSION['encrypt_key'])?$_SESSION['encrypt_key']:'';
    }

    function encryptData($data, $key = '') {
		$this->key = !empty($key)?$key:$this->key;
		$coordData = array();
		$coordData_key = array();
		$newcoord=array();
		$encryptedData = '';
		$dataKey = "";
		$car=0;
		
		if ($data != "" && $this->key != "")
		{
			for ($i=0; $i<strlen($data); $i++)
			{
				$dataKey .= $this->key[$car];
				$car++;
				if ($car == strlen($this->key))
				$car=0;
			}
			for ($k=0; $k<strlen($data); $k++)
			{
				for ($p=0; $p<strlen($this->alpha); $p++)
				{
					if ($data[$k] == $this->alpha[$p])
					$coordData[$k] = $p;
					if ($dataKey[$k] == $this->alpha[$p])
					$coordData_key[$k] = $p;
				}
				$newcoord[$k] = (isset($coordData[$k])?$coordData[$k]:'') + (($coordData_key[$k])?$coordData_key[$k]:'');
				if ($newcoord[$k] > strlen($this->alpha)-1) {
					$newcoord[$k] = $newcoord[$k] - strlen($this->alpha);
				}
				$encryptedData .= $this->alpha[$newcoord[$k]];
			}
		}
		
		$this->encryptData = $encryptedData;
	}

    function getEncryptData() {
      return $this->encryptData;
    }

    function decryptData($v_data, $key) {
		$this->key = !empty($key)?$key:$this->key;
		$coordCrypt = array();
		$decryptData = "";
		$encryptedData = "";
		$car=0;
		$crypt=$v_data;
		if ($this->key != "" && $crypt != "")
		{
			for ($i=0; $i<strlen($crypt); $i++)
			{
				for ($b=0; $b<strlen($this->alpha); $b++)
				{
					if ($crypt[$i]==$this->alpha[$b])
						$coordCrypt[$i]=$b;
				}
			}
			for ($j=0; $j<strlen($crypt); $j++)
			{
				$encryptedData .= $this->key[$car];
				$car++;
				if ($car==strlen($this->key))
				$car=0;
			}
			for ($k=0; $k<strlen($encryptedData); $k++)
			{
				for ($u=0; $u<strlen($this->alpha); $u++)
				{
					if ($encryptedData[$k]==$this->alpha[$u])
						$coordCrypt[$k] = $coordCrypt[$k] - $u;
					if ($coordCrypt[$k] < 0)
						$coordCrypt[$k]= $coordCrypt[$k]+strlen($this->alpha);
				}
			}
			for ($m=0; $m<strlen($crypt); $m++)
			{
				$decryptData .= isset($this->alpha[$coordCrypt[$m]])?$this->alpha[$coordCrypt[$m]]:' ';
			}
		}
			
		$this->decryptData = $decryptData;
    }

    function getDecryptData() {
      return $this->decryptData;
    }

    // function __destruct() {
      // $this->key = '';
    // }
}
?>