<?php

	/**
	 * Admin Modul für die Key Generierung 
	 */
	class wpsg_mod_keygen extends wpsg_mod_basic
	{
		
		var $id = 14;		
		var $lizenz = 3;
		var $inline = true;
		
		/**
		 * Costructor
		 */
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('Keygen', 'wpsg');
			$this->group = __('Sonstiges', 'wpsg');
			$this->desc = __('Erlaubt das generieren von Keys.', 'wpsg');
						
		} // public function __construct()

		public function settings_save()
		{
			
			if (file_exists($_FILES['testkey']['tmp_name']))
			{
				
				if (!class_exists('Crypt_RSA')) 
				{

					@set_include_path(WPSG_PATH.'/lib/phpseclib0.3.0/'.PATH_SEPARATOR.get_include_path());
					require_once(WPSG_PATH.'/lib/phpseclib0.3.0/Crypt/RSA.php');
						
				}
					 
				$rsa = new Crypt_RSA();
				$rsa->loadKey(file_get_contents(WPSG_PATH.'/lib/.htpublic'));
				 
				$arKey = @unserialize($rsa->decrypt(base64_decode(file_get_contents($_FILES['testkey']['tmp_name']))));
				
				$this->shop->addBackendMessage('nohspc_Testkey: <pre>'.print_r($arKey, 1).'</pre>');
				
			}
			
			if ($_REQUEST['wpsg_genkey'] == '1')
			{
			
				$_SESSION['wpsg_mod_keygen'] = wpsg_xss($_REQUEST['wpsg_mod_keygen']);
				
				if ($_REQUEST['wpsg_mod_keygen']['wpsg_mod_keygen_nr'] <= 0)
				{
					
					$this->shop->addBackendError(__('Bitte eine Schlüsselnummer angeben.', 'wpsg'));
					return;
					
				}
				
				if (!preg_match('/\d{2}\.\d{2}\.\d{4}/', $_REQUEST['wpsg_mod_keygen']['wpsg_mod_keygen_date']))
				{
					
					$this->shop->addBackendError(__('Bitte ein gültiges Gültigkeitsdatum angeben.', 'wpsg'));
					return;
					
				}
				
				if (!preg_match('/\d+\.\d+\.\d+/', $_REQUEST['wpsg_mod_keygen']['wpsg_mod_keygen_maxver']))
				{
					
					$this->shop->addBackendError(__('Bitte eine gültige Maximale Versionsnummer angeben.', 'wpsg'));
					return;
					
				}
				
				// Key generiere
				if (!class_exists('Crypt_RSA'))
				{
				
					@set_include_path(get_include_path().PATH_SEPARATOR.WPSG_PATH.'/lib/phpseclib0.3.0/');
					require_once(WPSG_PATH.'/lib/phpseclib0.3.0/Crypt/RSA.php');
					
				}
				
				$rsa = new Crypt_RSA();
				$rsa->loadKey(file_get_contents(WPSG_PATH.'/mods/mod_keygen/.htprivate'));
				
				$key = base64_encode($rsa->encrypt(serialize(array(
					"anwendung" => $_REQUEST['wpsg_mod_keygen']['wpsg_mod_keygen_anwendung'],
					"l" => $_REQUEST['wpsg_mod_keygen']['wpsg_mod_keygen_lizenz'],
					"nr" => $_REQUEST['wpsg_mod_keygen']['wpsg_mod_keygen_nr'],
					"date" => $_REQUEST['wpsg_mod_keygen']['wpsg_mod_keygen_date'],
					"version" => $_REQUEST['wpsg_mod_keygen']['wpsg_mod_keygen_maxver'],
					"mods" => $_REQUEST['wpsg_mod_keygen']['mods']
				))));
			 
				header('Content-type: text/html');
				header('Content-Disposition: attachment; filename="key.txt"');
				
				die($key);
				
			}
			
		} // public function settings_save()
		
		public function settings_edit()
		{
						
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_keygen/settings_edit.phtml');
			unset($_SESSION['wpsg_mod_keygen']['wpsg_mod_keygen_key']);
			
		}
		
	} // class wpsg_mod_keygen extends wpsg_mod_basic
		
?>