<?php
/**
 * @version		3.0.0
 * @package		JomLand TicketCenter
 * @copyright	Copyright (C) 2010 JomLand.com. All rights reserved.
 * @link 		www.jomland.com
 * @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');

jimport('joomla.html.pane');

class JLTicketPayment
{

	public $payment_params = array();

	function __construct(){
			
	}

	function getConfig($payment_class){
		$json_path = JPATH_COMPONENT_ADMINISTRATOR.DS.'models'.DS.'payment'.DS.strtolower($payment_class).'.cfg.php';

		if (file_exists($json_path)) {
			require_once $json_path ;
		} else {
			return;
		}

		$this->payment_params = json_decode($payment_config_json);

		return $this->payment_params;
	}//eof getConfig


	function setConfig($payment_class,$config=array()){
		$json_file = JPATH_COMPONENT.DS.'models'.DS.'payment'.DS.strtolower($payment_class).'.cfg.php';

		$cfg ='';
		$cfg = "<?php defined('_JEXEC') or die('Restricted access');"."\n".
		$cfg .= '$payment_config_json=\''.json_encode($config)."';";

		$this->writeFile($json_file, $cfg );

		//jimport ('');
		return;
	}//eof getConfig

	function writeFile($file, &$buffer, $use_streams=false)
	{
		// If the destination directory doesn't exist we need to create it
		if (!file_exists(dirname($file))) {
			jimport('joomla.filesystem.folder');
			JFolder::create(dirname($file));
		}

		if($use_streams) {
			$stream =& JFactory::getStream();
			$stream->set('chunksize', (1024 * 1024 * 1024)); // beef up the chunk size to a meg
			if(!$stream->writeFile($file, $buffer)) {
				JError::raiseWarning(21, 'JFile::write('. $file.'): '. $stream->getError());
				return false;
			}
			return true;
		} else {
			// Initialise variables.
			jimport('joomla.client.helper');
			$FTPOptions = JClientHelper::getCredentials('ftp');

			if ($FTPOptions['enabled'] == 1) {
				// Connect the FTP client
				jimport('joomla.client.ftp');
				$ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);

				// Translate path for the FTP account and use FTP write buffer to file
				$file = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
				$ret = $ftp->write($file, $buffer);
			} else {
				 
				$file = JPath::clean($file);
				JPath::setPermissions($file,'0666');
				$ret = file_put_contents($file, $buffer);
				JPath::setPermissions($file,'0644');
			}
			return $ret;
		}
	}// EOF writeFile

}//EOC