<?php 
/*
function ean ($message,$bol) {
	
	if($bol){$message1=substr($message,0,strlen($message)-1);}
	else    {$message1=$message;}
	  	
  $checksum = 0;
  foreach (str_split(strrev($message1)) as $pos => $val) {
    
	$checksum += $val * (3 -( 2 * ($pos % 2))); 
  }
	$checksum=((10 - ($checksum % 10)) % 10);
	
  
  if($bol){
		if($checksum==substr($message,strlen($message)-1,1)){$xchek=1;}
		else{$xchek=0;}}
  else{$xchek=$checksum;}
  
  
  return $xchek;
}
*/
class ean{
	var $check='';
	
	function ean(){}
	
	function val($message)
	{
		
		$aux=$this->get(substr($message,0,strlen($message)-1));
		
		if($aux==substr($message,strlen($message)-1,1)){$xchek=1;}
		else{$xchek=0;}
		return $xchek;
	}
	
	function get($message)
	{
		$checksum = 0;
		foreach (str_split(strrev($message)) as $pos => $val) {
    		$checksum += $val * (3 -( 2 * ($pos % 2))); 
  		}
		$checksum=((10 - ($checksum % 10)) % 10);
		//$this->check=$checksum;
		return $checksum;
	}
	
	function set($message)
	{
		if(strlen($message)>=13 || strlen($message)<1)
		{
			return 0;
		}

		if(strlen($message)==12)
		{
			$message.=$this->get($message);
			return $message;
		}
		
		if(strlen($message)<12)
		{
			$message='1'.$message;
			$aux=strlen($message);
			for($i=$aux;$i<12;$i++){$message.='0';}
			if(strlen($message)==12)
			{
				$message.=$this->get($message);
				return $message;
			}
		}
	}
}
?>