0
\$\begingroup\$

I'm workin on this code that will be a licensing class integrated with an electron app. For now it's a simple object who will generate and check for a licence stored inside the electron app folder. I want to improve it before implementing it inside my projects. Using this class it's simple: It will check for a valid licence file inside a predefined folder, if the file not exist, the class will generate one for using a trial version of the app. It's not perfect, but for now it work as expected. Any suggestion will be appreciated.

<?php
if(!defined('ABSPATH')){
  define('ABSPATH', __DIR__);
}


class Activator{

  private $uuid;
  private $licence_code;
  private $machine_uuid;
  private $current_date;
  private $start_date;
  private $end_date;
  private $timezone;

  public static function generateLicence($uuid){

    if(!file_exists(ABSPATH.'/DataStorage/.licence')){
      $timezone    = new DateTimeZone('Europe/Rome');
      $start_date  = new DateTime("now", $timezone);
      $end_date  = new DateTime("+30 days", $timezone);
      $machine_uuid = $uuid;
      $licence_code = hash('sha384', $machine_uuid);

        $licence_file = array(
          'uuid' => $machine_uuid,
          'activation_date' => $start_date->format('d-M-Y'),
          'expire_date' => $end_date->format('d-M-Y'),
          'trial_version' => true,
          'licence_code' => $licence_code
        );

        $w = file_put_contents(ABSPATH.'/DataStorage/.licence' ,json_encode($licence_file));
        echo $w;
    }

  }

  public function checkLicence($uuid){
    if(!file_exists(ABSPATH.'/DataStorage/.licence')){

      return;
    }
    else{
      $current_date  = new DateTime('now');
      $licence_file = json_decode(file_get_contents(ABSPATH.'/DataStorage/.licence'), true);
      $licence_code = $licence_file['licence_code'];
      $licence_check = hash_equals($licence_code, hash('sha384', $uuid));
        if($licence_check){
          if($current_date->format('d-M-Y') != $licence_file['expire_date']){
            return 'OK';
          }
          else{
            return 'EXPIRED';
          }
        }
    }
  }

}
// usage example with an AJAX call:
if(isset($_POST['uid'])){
  $system_id = $_POST['uid'];
  echo Activator::checkLicence($system_id);

}

?> 
\$\endgroup\$
  • 1
    \$\begingroup\$ The problem with this is it relies on the current time of the computer and I could simply change the time back to 1970, does the rest of the app depend on the internet or not ? Because if so you might be better of getting the time of a remote server or even better doing the check on your server (as they could inspect the web request and put a site returning 1970 again) and returning some string you could hash and then verify (pushing a new expected hash to the client program every update) \$\endgroup\$ – Dan Nov 12 '18 at 12:47
  • \$\begingroup\$ @Dan for now I'm testing it inside the server I've included inside the electron app. The final version will use the server date to generate a license, but I'm not sure about this. The app will work also offline so the check will be done only when the app start or if there is any update available \$\endgroup\$ – user9741470 Nov 12 '18 at 12:52
  • \$\begingroup\$ Oh i see they request to this form (you can put php in electron) ! So the steps are license file gets uploaded -> you check license file -> return an OK response, Are you putting any mechanism to stop me putting a fake site between the app and your server and just returning OK \$\endgroup\$ – Dan Nov 12 '18 at 12:56
  • \$\begingroup\$ Not for now, but this will be a check that I want to implement \$\endgroup\$ – user9741470 Nov 12 '18 at 13:01
  • \$\begingroup\$ So just for absolute clarity the user uploads his license file to your server and the code here runs on your server ? \$\endgroup\$ – Dan Nov 12 '18 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.