<?php
  /**
   * Example use of the Dinkey Pro/FD API in PHP.
   *
   * Copyright 2020 Microcosm Ltd.
   **/

  // Possible flags values, as documented in the user manual
  define('DEC_ONE_EXEC', 1);
  define('DEC_MANY_EXECS', 2);
  define('START_NET_USER', 4);
  define('STOP_NET_USER', 8);
  define('USE_FUNCTION_ARGUMENT', 16);
  define('CHECK_LOCAL_FIRST', 32);
  define('CHECK_NETWORK_FIRST', 64);
  define('USE_ALT_LICENCE_NAME', 128);
  define('DONT_SET_MAXDAYS_EXPIRY', 256);
  define('MATCH_DONGLE_NUMBER', 512);
  define('DONT_RETURN_FD_DRIVE', 1024);

  // Possible dongle type values
  define('DONGLE_TYPE_PRO', 1);
  define('DONGLE_TYPE_FD', 2);

  // Possible dongle model values
  define('DONGLE_MODEL_LITE', 1); // Lite
  define('DONGLE_MODEL_PLUS', 2); // Plus
  define('DONGLE_MODEL_NET5', 4); // Net (5 users)
  define('DONGLE_MODEL_NETU', 7); // Net (unlimited users)

  // Possible software key type values
  define('SOFTWARE_KEY_TYPE_NONE', 0);
  define('SOFTWARE_KEY_TYPE_TEMPORARY', 1);
  define('SOFTWARE_KEY_TYPE_DEMO', 2);

  // These output variables are passed by reference and set by the API functions
  $algorithmResult = 0;
  $readStringOut = "";
  $extErr = 0;
  $type = 0;
  $model = 0;
  $sdsn = 0;
  $dongleNum = 0;
  $updateNum = 0;
  $dataAreaSize = 0;
  $maxAlgNum = 0;
  $execs = 0;
  $expDay = 0;
  $expMonth = 0;
  $expYear = 0;
  $features = 0;
  $netUsers = 0;
  $prodCode = "";
  $fdDrive = "";
  $softwareKeyType = 0;
  $swkeyExpDay = 0;
  $swkeyExpMonth = 0;
  $swkeyExpYear = 0;

  // Below are several examples that each demonstrate different API functionality.
  // Uncomment the appropriate lines to execute the examples.

  /**
   * An example of a basic protection check that decrements the execution counter by 1
   * and starts a new network user
   */
  $retCode = DDSimpleCheck(DEC_ONE_EXEC|START_NET_USER);
  $algorithmResult = null;
  $readStringOut = null;

  /**
   * An example of a basic protection check against a different licence to the one the PHP extension is locked to,
   * in this example a licence with name "alt"
   */
  //$retCode = DDSimpleCheckEx(USE_ALT_LICENCE_NAME, 0, "alt");
  //$algorithmResult = null;
  //$readStringOut = null;

  /*
   * An example of a protection check that executes the first algorithm stored in the dongle.
   * The best way to use this feature is to identify lines of code in your program
   * that can be represented by algorithms stored in the dongle,
   * and replace these lines with calls to the API.
   * Lite dongle users cannot change the algorithm stored in the dongles,
   * so should calculate the algorithm in their own code and compare the result with the one returned by the API.
   */
  //$retCode = DDSimpleCheckAlg(0, 1, 1,2,3,4,5,6,7,8);
  //DDGetAlgAnswer($algorithmResult);
  //$readStringOut = null;

  /**
   * An example of a protection check that executes the second algorithm stored in the dongle,
   * and also decrements the execution counter by 5
   */
  //$retCode = DDSimpleCheckAlgEx(DEC_MANY_EXECS, 5, "", 2, 1,2,3,4,5,6,7,8);
  //DDGetAlgAnswer($algorithmResult);
  //$readStringOut = null;

  /**
   * An example of a protection check that also writes data to the dongle's secure data area.
   * This feature is not supported by Lite dongles.
   * Make sure you specify a large enough data area size when adding protection with DinkeyAdd!
   */
  //$retCode = DDWriteString(0, 0, "abcdef");
  //$algorithmResult = null;
  //$readStringOut = null;

  /**
   * An example of a protection check that also reads data from the dongle's secure data area.
   * This feature is not supported by Lite dongles.
   * Make sure you specify a large enough data area size when adding protection with DinkeyAdd!
   */
  //$retCode = DDReadString(0, 2, 3, $readStringOut);
  //$algorithmResult = null;
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Dinkey Pro/FD PHP API Sample Code</title>
  </head>
  <body style="font-family: monospace;">
    <?php if ($retCode != 0): ?>
      <p>
        Protection check failed
      </p>
      <p>
        Error Code: <?php echo $retCode; ?><br>
        Extended Error Code: <?php DDGetExtendedError($extErr); echo $extErr; ?><br>
      </p>
    <?php else: ?>
      <p>
        Protection check succeeded
      </p>
      <p>
        <?php if ($algorithmResult !== null): ?>
          Algorithm result: <?php echo $algorithmResult; ?><br>
        <?php endif; ?>

        <?php if ($readStringOut !== null): ?>
          Data read: <?php echo $readStringOut; ?><br>
        <?php endif; ?>

        SDSN: <?php DDGetSDSN($sdsn); echo $sdsn; ?><br>
        Product Code: <?php DDGetProductCode($prodCode); echo $prodCode; ?><br>
        Dongle Number: <?php DDGetDongleNumber($dongleNum); echo $dongleNum; ?><br>

        Dongle Type: <?php DDGetType($type); echo $type; ?><br>

        <?php if ($type == DONGLE_TYPE_FD): ?>
          Flash Drive Path: <?php DDGetFDDrive($fdDrive); echo $fdDrive; ?><br>
        <?php endif; ?>

        Dongle Model: <?php DDGetModel($model); echo $model; ?><br>

        <?php if ($model > DONGLE_MODEL_LITE): ?>
          Update Number: <?php DDGetUpdateNumber($updateNum); echo $updateNum; ?><br>
          Data Area Size: <?php DDGetDataAreaSize($dataAreaSize); echo $dataAreaSize; ?><br>
          Max Algorithm Number: <?php DDGetMaxAlgNum($maxAlgNum); echo $maxAlgNum; ?><br>
          Executions: <?php DDGetExecs($execs); echo $execs; ?><br>
          Expiry Day: <?php DDGetExpDay($expDay); echo $expDay; ?><br>
          Expiry Month: <?php DDGetExpMonth($expMonth); echo $expMonth; ?><br>
          Expiry Year: <?php DDGetExpYear($expYear); echo $expYear; ?><br>
          Features: <?php DDGetFeatures($features); echo $features; ?><br>
        <?php endif; ?>

        <?php if ($model > DONGLE_MODEL_PLUS): ?>
          Maximum Simultaneous Network Users: <?php DDGetNetUsers($netUsers); echo $netUsers; ?><br>
        <?php endif; ?>

        <?php if ($softwareKeyType != SOFTWARE_KEY_TYPE_NONE): ?>
          Software Key Type: <?php DDGetSWKeyType($softwareKeyType); echo $softwareKeyType; ?><br>
          Software Key Expiry Day: <?php DDGetSWKeyExpDay($swkeyExpDay); echo $swkeyExpDay; ?><br>
          Software Key Expiry Month: <?php DDGetSWKeyExpMonth($swkeyExpMonth); echo $swkeyExpMonth; ?><br>
          Software Key Expiry Year: <?php DDGetSWKeyExpYear($swkeyExpYear); echo $swkeyExpYear; ?><br>
        <?php endif; ?>

    <?php endif; ?>
  </body>
</html>
