Automated License Generation
Previous Topic  

Typically a license key is emailed to a user after they have filled in their details on a web page, prior to downloading the trial software. We provide an application for Linux (and can provide builds for Windows servers and for various Linux fllavours), that is called licgen.exe. Here is a working example of how it can be called from a php script:

 

 

<?php

 

        // licgen.php

        // PHP Script to generate trial license keys for given email address. Trial starts at today's date.

 

// ***************************** functions *********************************

 

function send_email ( $email, $from, $subject, $body  ) {

/*         Sends html 'body' text in an html email

        Returns true, or false if not sent.

*/

    // message head

    $message = '

<html>

<head>

  <title>' ;

        $message .= $subject ;

          $message .=  '</title>

</head>

<body>' ;

    $message .= $body ;

        $message .= '</body>

</html>' ;

 

        // now the mail headers

        // To send HTML mail, the Content-type header must be set

        $headers  = "MIME-Version: 1.0\n";

        $headers .= "Content-type: text/html; charset=iso-8859-1\n";

        $headers .= ('From: ' . $from . "\n");

 

        $sent = mail ( $email, $subject, $message, $headers ) ;

        return $sent ;

}

 

function emailadr_invalid ( $em ) {

// Checks validity of email

// We need string followed by @ followed by string followed by . followed by string.

 

        $invalid = FALSE ;

        $apos = strpos ( $em, "@" ) ;

        if ( $apos === FALSE || $apos < 2 ) { // insist on at least two characters before

                $invalid = TRUE ;

        }

 

        else {

                // The part after the @ sign may contain more than one period (

                $dotpos = strpos ( $em, ".", $apos ) ;

                if ( $dotpos === FALSE ) {

                        $invalid = TRUE ;

                }

                else {

                          $dotpos2 = strpos ( $em, ".", $dotpos ) + $dotpos ;

                        if (  $dotpos2 === TRUE && $dotpos2 == $dotpos + 1 ) { // not two dots in a row

                                $invalid = TRUE ;

                        }

                        else

                        if ( $dotpos > strlen ($em) - 3 ) { // must be at least 2 characters after the dot

                                $invalid = TRUE ;

                        }

                        else

 

                        // filter typical spammer destinations

                        if ( strpos ( $em, "abuse@" ) !== FALSE ) {

                                $invalid = TRUE ;

                        }

                        else

                        if ( strpos ( $em, "root@" ) !== FALSE ) {

                                $invalid = TRUE ;

                        }

                        else

                        if ( strpos ( $em, "postmaster@" ) !== FALSE ) {

                                $invalid = TRUE ;

                        }

                        else

                        if ( strpos ( $em, "info@" ) !== FALSE ) {

                                $invalid = TRUE ;

                        }

                        else

                        if ( strpos ( $em, "support@" ) !== FALSE ) {

                                $invalid = TRUE ;

                        }

                        else

                        if ( strpos ( $em, "webmaster@" ) !== FALSE ) {

                                $invalid = TRUE ;

                        }

 

                        else

                        if ( strpos ( $em, " " ) !== FALSE ) {

                                $invalid = TRUE ;

                        }

                        else

                        if ( strpos ( $em, "&" ) !== FALSE ) {

                                $invalid = TRUE ;

                        }

                        else

                        if ( strpos ( $em, "*" ) !== FALSE ) {

                                $invalid = TRUE ;

                        }

                }

        }

        return $invalid ;

}

 

// **************************** main script *********************************

 

        error_reporting(0); // as technical errors can confuse novice users

 

        header('Content-Type: text/html'); // header for output results page

 

        // get entered emails from form, user made to enter same email twice

        $em = $_POST['Em'];

        $em2 = $_POST['Em2'];

 

        // -------------  email checks needed  ---------------------

 

         if ( strcmp ( $em, $em2 ) != 0 ) {

                echo ( "<P>The entered email addresses are different. Please enter the identical email address in each box. Click your browser Back button to return to the previous page to correct the spelling mistake.</P>");

                return ;

         }

 

        if ( strlen ( $em ) == 0 ) {

                echo ( '<P>Email address missing. Please enter your email address</P>');

                echo ( '<BR><P>Click your browser Back button to return to the previous page.</P>');

                return ;

        }

 

        if ( strpos ( $em, ' ' ) !== FALSE ) {

                echo ( '<P>Invalid email address - has a space in it</P>');

                echo ( '<BR><P>Click your browser Back button to return to the previous page.</P>');

                return ;

        }

 

        if ( strpos ( $em, '\t' ) !== FALSE ) {

                echo ( '<P>Invalid email address - has a tab space in it</P>');

                echo ( '<BR><P>Click your browser Back button to return to the previous page.</P>');

                return ;

        }

 

        if ( strpos ( $em, '@' ) === FALSE ) {

                echo ( '<P>Invalid email address - must have an @ in it</P>');

                echo ( '<BR><P>Click your browser Back button to return to the previous page.</P>');

                return ;

        }

 

        // misc email checks

        if ( emailadr_invalid ($em ) ) {

                echo ( '<P>Invalid email address</P>');

                echo ( '<BR><P>Click your browser Back button to return to the previous page and check email address for correctness or enter a different email address.</P>');

                return ;

        }

 

         // -------------- get today's date as YYYYMMDD -----------

 

        $today = getdate();

        $yearstr = $today['year'] ;

        $month = $today ['mon'];

        settype($month, "integer");

        if ( $month < 10 )

                $monthstr = '0' ;

        $monthstr .= $month ;

        $day = $today ['mday'];

        settype($day, "integer");

        if ( $day < 10 )

                $daystr = '0' ;

        $daystr .= $day ;

        $trial_start = $yearstr . '/' . $monthstr . '/' . $daystr ;

 

         // -------------- add your details here -----------

 

         $programmer_id = 'xxxxxxxx' ; // the id we have assigned you for License Generator

         $programmer_key = 'xxxxx-xxxxx-xxxxx-xxxxx' ; // the license key we have assigned you for License Generator

         $manufac_code = 'xxxxxxx' ; // the manufacturer code we have assigned you

         $prod_code = 'xxxxxxxxx' ; // your own unique code for the produxt ( value must not exceed hexadecimal equivalent of 0x7FFFFFF )

 

        // ----------- generate user license key for their email as their user id -----------

 

        // Usage: licgen programmer_id programmer_key manufacturer_code YYYY/MM/DD_trial_start unsigned_int_product_code user_id

 

        $lickey = exec("./licgen.exe $programmer_id $programmer_key $manufac_code $trial_start $prod_code $em");

 

        // email the license key

 

        $message = "<p>You recently requested a license key for a 30-day trial of Version Saver.</p>\r\n<p>Your license key is " . $lickey . "</p>\r\n<p>It should be entered with your user id, which is: " . $em . "</p>\r\n";

        $from = 'support@yourcompany.biz' ;

        $sent = send_email  ( $em, $from, 'License key for Product Name', $message ) ;

 

        // report any problem emailing

        if ( $sent == FALSE ) {

                echo ( '<P>Sorry. There was a problem emailing the license key.</P>');

                echo ( '<BR><P>Click your browser Back button to return to the previous page and check the email address.</P>');

                return ;

        }

 

        // else display download link

        echo ( '<p>Right-click the mouse <A HREF="http://www.yourcompany.biz/yourproduct.msi">here</A> and choose Save Target As .. from the popup meu, to download the installation file' . "\r\n" ) ;

        echo ( '<p>Go to the <A HREF="http://www.yourcompany.biz/vs/InstallingOnWindowsVista.html">Windows Vista installation instructions</A> to install the software on Windows Vista.</p>' . "\r\n" ) ;

        echo ( '<p>Go to the <A HREF="http://www.yourcompany.biz/vs/InstallingOnWindowsNOTVista.html">Windows XP and 2000 installation instructions</A> to install the software on Windows XP and 2000.</p>' . "\r\n" ) ;

?>