Zen Cart Logo
Forums / All Other Contributions/Addons / software license key files

software license key files

Locked

Views: 3,741

Results 1 to 5 of 5
This thread is locked. New replies are disabled.
19 Nov 2008, 6:32 PM
#1
dause avatar

dause

New Zenner

Join Date:
Sep 2008
Posts:
33
Plugin Contributions:
0

software license key files

Hi,

I want to provide my customers with a link on the order page that will allow them to download the license key files for their purchased software products.
The key files are generated on the fly using the customer account info.

As a first step I have written the script that delivers the key file when accessed through a html link from the order page.

The major part of the script retrieves the session information and customer info from the data base.
This portion of the code is based on the ipn_application_top.php from the current ZC distribution. I basically copied it into a new file and deleted everything that seemed irrelevant to my aims.

Please have a look and send me your comments. Thanks!

<?php
/**
 * license_key.php
 * creates a downloadable software key file
 *  
 * This script is the first step towards a comprehensive 
 * software licensing scheme that will be neatly integrated
 * with zen cart.
 * 
 * Usage: 
 * <a href='path/to/license_key.php'>key file</a>
 * put the link on the order page or on a dedicated 
 * download page.
 * (make sure it is only included when the item has 
 * been purchased)
 * 
 * coming features:
 * (1) integration with the shop (how to link to this script from 
 *     the order page or from a dedicated downloads page.)
 * (2) include some more security checks. 
 * (3) send an email notification to the shop owner whenever 
 *     a license is created.
 * (4) send key file as an email attachment immedeately 
 *     after the payment is cleared.
 *   
 * @package general
 * @copyright Copyright 2008 Andreas Sumerauer
 * @copyright Portions Copyright 2003 osCommerce, 2003-2005 Zen Cart Development Team
 * @license [url]http://www.zen-cart.com/license/2_0.txt[/url] GNU Public License V2.0
 * @version $Id: license_key.php 2008-11-19 ansum 
 */


/**
 * create_plugin_license()
 * Replace the dummy code in the function body with your own sophisticated encryption scheme.
 *
 * @param The example uses customer name and email to create a unique personalized key file.
 * @param You might want to use a different identifier like a machine ID for example.
 * @param Use the product_id parameter to generate licenses for different software products 
 * @return key code
 */
function create_plugin_license($Fname, $Lname, $Email, $product_id){
	$keycode  = str_pad ( $Fname, 32,'~')."\n";
	$keycode .= str_pad ( $Lname, 32,'~')."\n";
	$keycode .= str_pad ( $Email, 32,'~')."\n";
	return $keycode;
}

/**
 * boolean used to see if we are in the admin script, set to false here.
 */
define('IS_ADMIN_FLAG', false);

error_reporting(0);
$show_all_errors = false;
$current_page_base = 'plugin_license';
@ini_set("arg_separator.output","&");

// Set the local configuration parameters
if (file_exists('includes/local/configure.php')) {
	include('includes/local/configure.php');
}
// include server parameters
if (file_exists('includes/configure.php')) {
	include('includes/configure.php');
}

require('includes/classes/class.base.php');

// The mailer classes are not used at the moment.
// A coming version will send a notification 
// to the shopowner whenever a license is created
//
// require('includes/classes/class.phpmailer.php');
// require('includes/classes/class.smtp.php');
// require(DIR_WS_FUNCTIONS . 'functions_email.php');

require('includes/classes/db/' .DB_TYPE . '/query_factory.php');
$db = new queryFactory();
if ( !$db->connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD, DB_DATABASE, USE_PCONNECT, false) ) {
	die('Cannot connect to database. Please notify webmaster.');
	exit;
}

// set the type of request (secure or not)
$request_type = (strtolower($_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == '1' || strstr(strtoupper($_SERVER['HTTP_X_FORWARDED_BY']),'SSL') || strstr(strtoupper($_SERVER['HTTP_X_FORWARDED_HOST']),'SSL'))  ? 'SSL' : 'NONSSL';

// include the list of project database tables
require(DIR_WS_INCLUDES . 'database_tables.php');

// define general functions used application-wide
require(DIR_WS_FUNCTIONS . 'functions_general.php');

// define how the session functions will be used
require(DIR_WS_FUNCTIONS . 'sessions.php');

// set the session name and save path
zen_session_name('zenid');
zen_session_save_path(SESSION_WRITE_DIRECTORY);

// set the session cookie parameters
session_set_cookie_params(0, '/', (zen_not_null($current_domain) ? $current_domain : ''));

// set the session ID if it exists
if (isset($_POST[zen_session_name()])) {
	zen_session_id($_POST[zen_session_name()]);
} elseif ( ($request_type == 'SSL') && isset($_GET[zen_session_name()]) ) {
	zen_session_id($_GET[zen_session_name()]);
}

// start the session
$session_started = false;
if (SESSION_FORCE_COOKIE_USE == 'True') {
	zen_setcookie('cookie_test', 'please_accept_for_session', time()+60*60*24*30, '/', (zen_not_null($current_domain) ? $current_domain : ''));

	if (isset($_COOKIE['cookie_test'])) {
		zen_session_start();
		$session_started = true;
	}
} elseif (SESSION_BLOCK_SPIDERS == 'True') {
	$user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
	$spider_flag = false;

	if (zen_not_null($user_agent)) {
		$spiders = file(DIR_WS_INCLUDES . 'spiders.txt');

		for ($i=0, $n=sizeof($spiders); $i<$n; $i++) {
			if (zen_not_null($spiders[$i])) {
				if (is_integer(strpos($user_agent, trim($spiders[$i])))) {
					$spider_flag = true;
					break;
				}
			}
		}
	}

	if ($spider_flag == false) {
		zen_session_start();
		$session_started = true;
	}
} else {
	zen_session_start();
	$session_started = true;
}

$language_page_directory = DIR_WS_LANGUAGES . $_SESSION['language'] . '/';

if (array_key_exists('customer_id', $_SESSION)){
	$customer_query = "select customers_firstname, customers_lastname, customers_email_address
                                from " . TABLE_CUSTOMERS . "
                                where customers_id = '" . (int)$_SESSION['customer_id'] . "'";

	$customer = $db->Execute($customer_query);
	
	$Fname = $customer->fields['customers_firstname'];
	$Lname = $customer->fields['customers_lastname'];
	$Email = $customer->fields['customers_email_address'];
	$product_id = 74;
	$keyFileName = "license.key";
	
	// php header will make sure that the key file is not displayed by the browser 
	// but is instead made available as a download with the correct filename.
	header("Content-type: application/octet-stream");
	header("Content-Disposition: attachment; filename=\"" . $keyFileName . "\"");
	echo create_plugin_license($Fname, $Lname, $Email, $product_id);
	
}else{
	// better use zen_redirect() here?
	header( "HTTP/1.1 301 Moved Permanently" );
	header( "Location: " . HTTP_SERVER . DIR_WS_CATALOG );
	header( "Connection: close" );
	exit;
}
?>
20 Nov 2008, 10:52 AM
#2
hugo13 avatar

hugo13

Zen Follower

Join Date:
Apr 2004
Location:
vienna
Posts:
176
Plugin Contributions:
4

Re: software license key files

I did a similar thing, but used the observer NOTIFY_CHECKOUT_PROCESS_AFTER_ORDER_CREATE_ADD_PRODUCTS

at this point i create a license-file (manipulate the downloadable-file) & store it in the download-area & make an entry into the TABLE_ORDERS_PRODUCTS_DOWNLOAD-table

a config-flag checks ( 'RL_LICENSE_PRODUCTS', '1,4,6,8' ): is the basket-product a producht which should generate a lic-file

after that process, the customer can download the file, if status = 3 (delivered)

20 Nov 2008, 3:20 PM
#3
dause avatar

dause

New Zenner

Join Date:
Sep 2008
Posts:
33
Plugin Contributions:
0

Re: software license key files

hugo13:

I did a similar thing, but used the observer NOTIFY_CHECKOUT_PROCESS_AFTER_ORDER_CREATE_ADD_PRODUCTS

at this point i create a license-file (manipulate the downloadable-file) & store it in the download-area & make an entry into the TABLE_ORDERS_PRODUCTS_DOWNLOAD-table

Thanks Rainer for the comments and for sharing your code.
I think I will stick with my approach and create the license on the fly. This way I don't have to care about what happens if two or more customers want to download a license for the same product at the same time. (plus it saves completely from having to manage the download files)

a config-flag checks ( 'RL_LICENSE_PRODUCTS', '1,4,6,8' ): is the basket-product a product which should generate a lic-file

I'll just evaluate the download file name to check wether a product is a content download or a key file. Example: When the script finds a download file name 'BagPipes.license' it will create a key file for the BagPipes software. If the .license extension is not present it will look for a file in the downloads folder. Of course I will have to stick to the naming convention but I don't see that as a restriction.
Another benefit of having the license creation tied to the attribute name instead of the product as a whole is that I can easily offer different license levels - like one for the pro version and another one for a cheaper or free light version of the same program. One could even offer an alternative boxed version of the product that does not require a license file at all.
(I'll soon add some code examples to illustrate the approach.)

Andreas

25 Nov 2008, 9:39 PM
#4
nomadgimp_com avatar

nomadgimp_com

New Zenner

Join Date:
Nov 2008
Posts:
4
Plugin Contributions:
0

Re: software license key files

Wow, okay, this sounds like exactly what I have been looking for, but it's a bit over my head. Is there a module/addon available for what you're talking about? Or can I copy some of your code? (not that I know exactly which parts to copy, but i might eventually figure it out). I am trying to basically do the same thing, but only sell a license, and possibly not bother with the download, allowing them to download the program from sites like cnet or versiontracker. At any rate, I'll continue to look for more info on this, but it seems like you're doing what I want to be doing. A little extra bump in the right direction would be great (I'm not totally inept, but I'm not a php programmer either).

27 Nov 2008, 1:15 AM
#5
nomadgimp_com avatar

nomadgimp_com

New Zenner

Join Date:
Nov 2008
Posts:
4
Plugin Contributions:
0

Re: software license key files

Okay, so after working through this a little bit more, I think I get what is going on... but 1 question: when a customer changes her/his email, won't they get another key? so basically a customer could generate unlimited keys if they wanted to keep changing their email in their account profile, and distribute them?
Other than that, not YET having tested this, I think it's really great...
thoughts? (I have also read the really long thread on this topic, but I like your code the most so far).
O

(sorry for sounding so lost on my previous post, a bit sick, and tired too. So, just completely ignore it i guess, unless their is another resource for this somewhere I don't know about (doubtful). Thank You so much Andreas! Thanks for your code!