Location: PHPKode > scripts > sellaOTP > sellaotp/sella_OTP.class.php
<? 
/*
/************************************************************************/
/* sella_OTP.class                                                      */
/* ========                                                             */
/*                                                                      */
/* Copyright (c) 2002 by Angelo Gelmi                                   */
/* http://www.web3king.com/sellaOTP 									*/
/* hide@address.com								                        */
/*                                                                      */
/* This program 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 2 of the License.       */
/************************************************************************/
/*
//METHODS
==========
function read_answer()		//Assign to general properties the values returned from Sella Bank
function make_query($id)	//Prepare url to be passed to banca sella
function prepare_otp_ric()	//Take one OTP password from archive
function verify_otp()		//Verify the OTP returned from Sella Bank [return 0 if OK, 1 if KO]
function convertcomma2point($amount)	//Convert Number with comma into number with point
function show_report()		//Show the table with the passed returned values
function show_form($action)	//Show the form of submit values for payment

function SetStore($stmethod) //Set Storing method for OTP  [file/sql]
function SetUrl($url)		 //Set url to Sella Bank (Default is https://ecomm.sella.it/gestpay/pagam.asp)
function SetA($a)			 
function SetB($b)
function SetSepSella($separatoregenerale) //Set general Sella Bank separator (Default is *P1*) 
function SetSepValue($sepvalue)	//Set value separator (Default is =) 	
*/

class sella_OTP
{

//proprieta' private
var $sep;              //current page number
var $sep2;             //current number of objects
var $a;				   //A parameter
var $b;			       //B parameter
var $amount;		   //AMOUNT
var $error_log;		   //Error 
var $url;			   //url (default is set https://ecomm.sella.it/gestpay/pagam.asp 
var $root;				// /home/mhd/www.dominio.com/htdocs/basi
var $store;		       //type of OTP storing (file/sql)

// Both Sent and Received Variabiles
//Mandatories
var $PAY1_SHOPTRANSACTIONID;
var $PAY1_UICCODE;
var $PAY1_AMOUNT;
var $PAY1_OTP;
//Optionals
var $PAY1_IDLANGUAGE;
var $LING;

// Return Variabiles
var $PAY1_CHNAME;
var $PAY1_CHEMAIL;
var $PAY1_TRANSACTIONRESULT;
var $PAY1_AUTHORIZATIONCODE;
var $PAY1_BANKTRANSACTIONID;
var $PAY1_ERRORCODE;
var $PAY1_ERRORDESCRIPTION;
var $PAY1_ALERTCODE;
var $PAY1_ALERTDESCRIPTION;


//Assign to general properties the values returned from Sella Bank
function read_answer(){
	//Set default value if missing
	if (empty($this->sep))
		$this->SetSepSella('');
	if (empty($this->sep2))
		$this->SetSepValue('');
		
	$parametri=explode("$this->sep","$this->b");
	foreach ($parametri as $i){	
		list($key,$value)=explode($this->sep2,$i);
		$chiavi["$key"]="$value";	
		$this->$key=$value;
	}
}

//Prepare url to be passed to banca sella
function make_query($id){
	//Set default value if missing
	if (empty($this->sep))
		$this->SetSepSella('');
	if (empty($this->sep2))
		$this->SetSepValue('');
	
	//if PAY1_AMOUNT is empty exit returning 1 
	if (empty($this->PAY1_AMOUNT))
		return 1;
	else //convert comma into point
		$this->convertcomma2point($this->PAY1_AMOUNT);
	
	if (empty($this->PAY1_UICCODE))
		$this->PAY1_UICCODE="242";
	
	//if PAY1_SHOPTRANSACTIONID is empty exit returning 2 	
	if (empty($this->PAY1_SHOPTRANSACTIONID))
		return 2;
		
	$this->prepare_otp_ric();
	
	$this->a="$id";
	$this->b="PAY1_UICCODE=$this->PAY1_UICCODE".$this->sep."PAY1_AMOUNT=$this->PAY1_AMOUNT".$this->sep."PAY1_SHOPTRANSACTIONID=$this->PAY1_SHOPTRANSACTIONID".$this->sep."PAY1_OTP=$this->PAY1_OTP".$this->sep."PAY1_IDLANGUAGE=$this->PAY1_IDLANGUAGE";
	if (isset($this->LING))
		$this->b=$this->b."*P1*LING=".$this->LING;
	else
		$this->b=$this->b."*P1*LING=2";
}
//Take one OTP password from archive
function prepare_otp_ric(){
	if ($this->store=="sql"){
		//find one time password from database
		$sql="select * from otpric where stato='F'";
		$res=mysql_query($sql);
		if (!$res){
			$this->error_log="No more otp free, please contact webmaster";
		}else{
			$otp=mysql_fetch_array($res);
			//set otp status as locked  
			$sql="update otpric set stato='L' where otp='$otp[otp]'";
			$res=mysql_query($sql);
			//assign the value taken to property PAY1_OTP
			$this->PAY1_OTP=$otp[otp];
		}
	}elseif($this->store=="file"){
		//find one time password from file
		// file must be 666 and in a protected directory or even better out from website filesystem
		$otp=file("/siti/Web3king/sellaOTP/otp.ric.php");
		$this->PAY1_OTP=array_shift($otp);
		$this->PAY1_OTP=substr($this->PAY1_OTP,0,32);
		$fp = fopen ("/siti/Web3king/sellaOTP/otp.ric.php", "w");
		foreach ($otp as $riga){
			$res=fwrite($fp,$riga);
		}
		fclose($fp);	
		
		
	}else{
		$this->error_log="Hasn't been specified storing method";
		exit;
	}
}

//Verify the OTP returned from Sella Bank [return 0 if OK, 1 if KO]
function verify_otp(){
	if ($this->store=="sql"){
		//find one time password from database
		$sql="select * from otpris where otp='$this->PAY1_OTP';";
		$ret=mysql_query($sql);
		$otp=mysql_fetch_array($ret);
		$rit=mysql_num_rows($ret);
		
		//if no record found 
		if (!$rit){
			$this->error_log="Not valid OTP from Sella Bank";
			return 1;
		}else{
			//assign the value taken to property PAY1_OTP
			$this->PAY1_OTP=$otp[otp];
			return 0;
		}
	}elseif($this->store=="file"){
	   //find one time password from file
	   $otp = file  ("/siti/Web3king/sellaOTP/otp.ris.php");
       $fp  = fopen ("/siti/Web3king/sellaOTP/otp.ris.php", "r");
	   $s=0;
		foreach ($otp as $riga){
			$this->PAY1_OTP=substr($this->PAY1_OTP,0,32);
			$riga=substr($riga,0,32);
			if ($riga==$this->PAY1_OTP){
				$c=1;
				//$otp[$s]="";
			}else{
				//$res=fwrite($fp,$riga."\n");
			}
			$s++;
		}
		fclose($fp);
		if ($c){
				return 0;
			}else{
				
				$this->error_log="Not valid OTP from Sella Bank";
				return 1;
		}
	}else{
		$this->error_log="Hasn't been specified storing method";
		return 2;	
	}
}

function del_otp($otp){
	if ($this->store=="sql"){
			//delete otp from database
			$sql="delete from otpris where otp='".$otp."';";
			$res=mysql_query($sql);
	}elseif ($this->store=="file"){
			 //delete one time password from file
		   	   $otp=file("/home/mhd/www.dominio.biz/htdocs/basi/otp.ris.php");
		       $fp = fopen ("/home/mhd/www.dominio.biz/htdocs/basi/otp.ris.php", "w");
			   $s=0;

			foreach ($otp as $riga){
			$this->PAY1_OTP=substr($this->PAY1_OTP,0,32);
			$riga=substr($riga,0,32);
			if ($riga==$this->PAY1_OTP){
				$otp[$s]="";
			}else{
				$res=fwrite($fp,$riga."\n");
			}
			$s++;
		}
		fclose($fp);
	}
}

//Convert Number with comma into number with point
function convertcomma2point($amount){
	$this->PAY1_AMOUNT=ereg_replace( ",", ".", $amount ); 
}

//Show the table with the passed returned values
function show_report(){
$table="<table border=1 cellspacing=1 cellpadding=1><tr><td>Nome Variabile</td><td>Valore passato</td></tr>";
$table.="<tr><td>PAY1_SHOPTRANSACTIONID</td><td>$this->PAY1_SHOPTRANSACTIONID</td></tr>";
$table.="<tr><td>PAY1_UICCODE</td><td>$this->PAY1_UICCODE</td></tr>";
$table.="<tr><td>PAY1_AMOUNT</td><td>$this->PAY1_AMOUNT</td></tr>";
$table.="<tr><td>PAY1_CHNAME</td><td>$this->PAY1_CHNAME</td></tr>";
$table.="<tr><td>PAY1_CHEMAIL</td><td>$this->PAY1_CHEMAIL</td></tr>";
$table.="<tr><td>PAY1_OTP</td><td>$this->PAY1_OTP</td></tr>";
$table.="<tr><td>PAY1_TRANSACTIONRESULT</td><td>$this->PAY1_TRANSACTIONRESULT</td></tr>";
$table.="<tr><td>PAY1_AUTHORIZATIONCODE</td><td>$this->PAY1_AUTHORIZATIONCODE</td></tr>";
$table.="<tr><td>PAY1_BANKTRANSACTIONID</td><td>$this->PAY1_BANKTRANSACTIONID</td></tr>";
$table.="<tr><td>PAY1_ERRORCODE</td><td>$this->PAY1_ERRORCODE</td></tr>";
$table.="<tr><td>PAY1_ERRORDESCRIPTION</td><td>$this->PAY1_ERRORDESCRIPTION</td></tr>";
$table.="<tr><td>PAY1_ALERTCODE</td><td>$this->PAY1_ALERTCODE</td></tr>";
$table.="<tr><td>PAY1_ALERTDESCRIPTION</td><td>$this->PAY1_ALERTDESCRIPTION</td></tr>";
$table.="</table>";
print $table;
}

//Show the form of submit values for payment
function show_form($method){
if (!isset($method))
	$method="post";
if ($this->LING==1)
	$cc="avanti.gif";
else
	$cc="continue.gif";
	
if ($method=="post")
{
  $table="<form action=".$this->url." method=$method>
		<input type=hidden name=\"a\" value = \"".$this->a." \"> 
		<input type=hidden name=\"b\" value = \"".$this->b." \">
		<table align=center><tr><td><input type=image border=0 src=".$cc." name=Submit2></td></table></form>";
}else{
	$table="<table align=center><tr><td><a href=".$this->url."?a=".$this->a."&b=".$this->b."><img border=0 src=".$cc."  name=Submit2></a></td></table>";
		}
print $table;
}

//Set general Sella Bank separator (Default is *P1*) 
function SetSepSella($separatoregenerale)
{
	if (empty($separatoregenerale))
		$this->sep="*P1*";
	else
		$this->sep=$separatoregenerale;
}

//Set value separator (Default is =) 
function SetSepValue($sepvalue)
{
	if (empty($sepvalue))
		$this->sep2="=";
	else
		$this->sep2=$sepvalue;
}

//Set Storing method for OTP  [file/sql]
function SetStore($stmethod)
{
	if (empty($stmethod))
		$this->store="file";
	else
		$this->store=$stmethod;
}

//Set url to Sella Bank (Default is https://ecomm.sella.it/gestpay/pagam.asp) 
function SetUrl($url)
{
	if (empty($url))
		$this->url="https://ecomm.sella.it/gestpay/pagam.asp";
	else
		$this->url=$url;
}

function SetA($a)
{
	$this->a=$a;
}

function SetB($b)
{
	$this->b=$b;
}

//fine classe
}
</script>
Return current item: sellaOTP