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 Code:<?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;
}
?>




