Skip to content

Commit

Permalink
Merge pull request #12 from Ente/v1.0
Browse files Browse the repository at this point in the history
v1.0 - adding support to Data endpoints and offline license validation
  • Loading branch information
Ente authored Sep 9, 2024
2 parents 6dc9dab + 2d89057 commit 1f338da
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v1.0

* added support to Data endpoints `addDataObject`, `listDataObject`, `incrementIntValue`, `decrementIntValue`, `setStringValue`, `setIntValue`, `removeDataObject`, `uploadValues`
* added support for License file validation `License.cryptolens.php`: `verifyLicenseKey`, `verifyLicenseFromFileContent`. See `README.md` for configuration

## v0.9

* added error handler class `Errors.cryptolens.php` which will now return you the respectful errors and logs them into files.
Expand Down
3 changes: 3 additions & 0 deletions Cryptolens.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Cryptolens {
public const CRYPTOLENS_ANALYTICS = "Analytics";

public const CRYPTOLENS_LICENSE = "License";

public const CRYPTOLENS_DATA = "Data";

private string $token;

Expand Down Expand Up @@ -80,6 +82,7 @@ public static function loader(){
require_once dirname(__FILE__) . "/classes/Auth.cryptolens.php";
require_once dirname(__FILE__) . "/classes/Product.cryptolens.php";
require_once dirname(__FILE__) . "/classes/PaymentForm.cryptolens.php";
require_once dirname(__FILE__) . "/classes/Data.cryptolens.php";
require_once dirname(__FILE__) . "/classes/Message.cryptolens.php";
require_once dirname(__FILE__) . "/classes/Reseller.cryptolens.php";
require_once dirname(__FILE__) . "/classes/Subscription.cryptolens.php";
Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,37 @@ In a real values for you can be obtained as follows:
* You can generate a machine ID for the PHP instance with the builtin `Key::getMachineId()` funtion. Please read the function's documentation for more understanding of the calculation of the machine ID.
* In an upcoming release this library should be able to also validate license files

### Offline license validation

The API also allows you to validate license files via the `License.cryptolens.php` file.
To properly configure this function, please convert the XML-styled public key into PEM format (PKC#1) and save it into the `classes/*` directory as `key.pub` (if `License(<Cryptolens $cryptolens>, <string $pathToKey>)` $pathToKey is set, the path is overwritten by specified one).

You can convert your key on a site like this one [here](https://the-x.cn/en-US/certificate/XmlToPem.aspx) or [using this repository](https://github.com/MisterDaneel/PemToXml)

You can then validate a license key like this:

```php
<?php

require_once "/path/to/autoloader.php";
use Cryptolens_PHP_Client\Cryptolens;
use Cryptolens_PHP_Client\License;

$c = new Cryptolens("YOUR_TOKEN", 12345, Cryptolens::CRYPTOLENS_OUTPUT_PHP);
$l = new License($c); // to specify custom key file location other than /classes/key.pub specify the FULL path as License($c, "/var/www/my/public/key.pub")

$fromString = $l->validateLicense("BASE64-ENCODED-STRING", "BASE64-ENCODED-STRING") // licenseKey and signature
$fromFileContent = $l->validateLicenseFromFileContent(file_get_contents("license.skm")); // License file as string
$fromFile = $l->validateLicenseFromFile("license.skm")

if($fromFile){
echo "Signature successfully validated";
} else {
echo "Could not verify signature";
}

```

## Installation

You can either clone this repository and require the `loader.php` (which contains a autoloader) or use composer via console:
Expand Down Expand Up @@ -121,6 +152,11 @@ to automatically load the required classes.
* Payment Form
* [x] create_session
* Analytics
* [x] register_event
* [x] register_events
* [x] get_events
* [x] get_object_log
* [x] get_web_api_log
* Message
* [x] get_messages
* [x] create_message
Expand Down
231 changes: 231 additions & 0 deletions classes/Data.cryptolens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
<?php
namespace Cryptolens_PHP_Client {
class Data {

private Cryptolens $cryptolens;

private string $group;

public function __construct(Cryptolens $cryptolens){
$this->cryptolens = $cryptolens;
$this->group = CRYPTOLENS::CRYPTOLENS_DATA;
}

/**
* `addDataObject()` - Adds a new Data Object to either a license key, product or your entire account.
* The Cryptolens API provides 3 ways to add a Data object for each type (license, product or account).
*
* @param string $name Name of the DataObject, up to 100 characters
* @param string $value A string to store, up to 10k characters
* @param string $int optional integer to store, default = 0
* @param int $referencerType Where to assign the data object: license key (2), product (1) or User (0), default = 0
* @param string $referencerId ID of the referencer, e.g. the license key or product ID. Not required if "User" (0) is referencerType
* @param bool $checkForDuplicates If set to true, checking for data objects with the same name
* @link https://app.cryptolens.io/docs/api/v3/addDataObject
* @return array|bool
*/
public function addDataObject(string $name, string $value, int $int = 0, int $referencerType = 0, string $referencerId = "0", bool $checkForDuplicates = false){
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, [
"Name" => $name,
"StringValue" => $value,
"IntValue" => $int,
"ReferencerType" => $referencerType,
"ReferencerId" => $referencerId,
"CheckForDuplicates" => $checkForDuplicates
]);

$c = Helper::connection($parms, "addDataObject", $this->group);
if($c == true){
if(Helper::check_rm($c)){
return Cryptolens::outputHelper($c);
} else {
return Cryptolens::outputHelper($c, 1);
}
} else {
return false;
}
}

/**
* `listDataObjects()` - Returns all data objects associated with a key, product or account
* @param int $referencerType Where to assign the data object: license key (2), product (1) or User (0), default = 0
* @param string $referencerId ID of the referencer, e.g. the license key or product ID. Not required if "User" (0) is referencerType
* @param string $contains A string, if set only returns Data object where "name" == $contains, default = ""
* @param bool $showAll If set to `true` if returns both license key, product and account specific data objects all at once. Within response it contains the referencerType and Id.
* @link https://app.cryptolens.io/docs/api/v3/ListDataObjects
* @return array|bool Array on success and false on failure
*/
public function listDataObjects(int $referencerType = 0, string $referencerId = "0", string $contains = "", bool $showAll = true){
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, [
"ReferencerType" => $referencerType,
"ReferencerId" => $referencerId,
"Contains" => $contains,
"ShowAll" => $showAll
]);
$c = Helper::connection($parms, "listDataObjects", $this->group);
if($c == true){
if(Helper::check_rm($c)){
return Cryptolens::outputHelper($c);
} else {
return Cryptolens::outputHelper($c, 1);
}
} else {
return false;
}
}

/**
* `incrementIntValue()` - Increment the Int value of an Data Object
* @param int $id Unique ID of the data object
* @param int $value The value to be incremented on top (if e.g. set to 5 and your existing int is 2 the new one will be 7)
* @param bool $enableBound If set to true, you can set a upper bound, e.g. 10 - if you reach 10 an error occurs.
* @param int $bond Upper bound to enforce if $enableBond has been enabled
* @link https://app.cryptolens.io/docs/api/v3/incrementIntValue
* @return array|bool Array on success and false on failure
*/
public function incrementIntValue(int $id, int $value = 0, bool $enableBound = false, int $bound = 0){
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, [
"Id" => $id,
"IntValue" => $value,
"EnableBound" => $enableBound,
"Bound" => $bound
]);
$c = Helper::connection($parms, "incrementIntValue", $this->group);
if($c == true){
if(Helper::check_rm($c)){
return Cryptolens::outputHelper($c);
} else {
return Cryptolens::outputHelper($c, 1);
}
} else {
return false;
}
}

/**
* `decrementIntValue()` - Increment the Int value of an Data Object
* @param int $id Unique ID of the data object
* @param int $value The value to decrement(if e.g. set to 2 and your existing int is 5 the new one will be 3)
* @param bool $enableBound If set to true, you can set a lower bound, e.g. 10 - if you reach 10 an error occurs.
* @param int $bond Lower bound to enforce if $enableBond has been enabled
* @link https://app.cryptolens.io/docs/api/v3/decrementIntValue
* @return array|bool Array on success and false on failure
*/
public function decrementIntValue(int $id, int $value = 0, bool $enableBound = false, int $bound = 0){
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, [
"Id" => $id,
"IntValue" => $value,
"EnableBound" => $enableBound,
"Bound" => $bound
]);
$c = Helper::connection($parms, "decrementIntValue", $this->group);
if($c == true){
if(Helper::check_rm($c)){
return Cryptolens::outputHelper($c);
} else {
return Cryptolens::outputHelper($c, 1);
}
} else {
return false;
}
}

/**
* `setStringValue()` - Set the string value of a data object
* @param int $id Unique ID of the data object
* @param string $value String value to set
* @link https://app.cryptolens.io/docs/api/v3/setStringValue
* @return array|bool
*/
public function setStringValue(int $id, string $value = null){
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, [
"Id" => $id,
"StringValue" => $value
]);
$c = Helper::connection($parms, "setStringValue", $this->group);
if($c == true){
if(Helper::check_rm($c)){
return Cryptolens::outputHelper($c);
} else {
return Cryptolens::outputHelper($c, 1);
}
} else {
return false;
}
}

/**
* `setIntValue()` - Set the int value of a data object
* @param int $id Unique ID of the data object
* @param string $value String value to set
* @link https://app.cryptolens.io/docs/api/v3/setIntValue
* @return array|bool
*/
public function setIntValue(int $id, int $value = null){
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, [
"Id" => $id,
"IntValue" => $value
]);
$c = Helper::connection($parms, "setIntValue", $this->group);
if($c == true){
if(Helper::check_rm($c)){
return Cryptolens::outputHelper($c);
} else {
return Cryptolens::outputHelper($c, 1);
}
} else {
return false;
}
}

/**
* `removeDataObject()` - Remove a data object
* @param int $id Unique ID of the data object
* @link https://app.cryptolens.io/docs/api/v3/removeDataObject
* @return array|bool
*/
public function removeDataObject(int $id){
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, [
"Id" => $id
]);
$c = Helper::connection($parms, "removeDataObject", $this->group);
if($c == true){
if(Helper::check_rm($c)){
return Cryptolens::outputHelper($c);
} else {
return Cryptolens::outputHelper($c, 1);
}
} else {
return false;
}
}

/**
* `uploadValues()` - Upload values from a Cryptolens log file to either increment or decrement a data object
* @param int $id Unique ID of the data object
* @param string $key Serial Key
* @param string $data String in base64 created by Cryptolens SDK that has recorded data object usage.
* @link https://app.cryptolens.io/docs/api/v3/removeDataObject
* @return array|bool
*/
public function uploadValues(int $id, string $key, string $data){
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), $key, null, [
"Id" => $id,
"Data" => $data
]);
$c = Helper::connection($parms, "removeDataObject", $this->group);
if($c == true){
if(Helper::check_rm($c)){
return Cryptolens::outputHelper($c);
} else {
return Cryptolens::outputHelper($c, 1);
}
} else {
return false;
}
}
}
}


?>
14 changes: 12 additions & 2 deletions classes/Endpoints.cryptolens.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@ class Endpoints {
"registerEvents" => "https://api.cryptolens.io/api/ai/RegisterEvents",
"getEvents" => "https://api.cryptolens.io/api/ai/GetEvents",
"getObjectLog" => "https://api.cryptolens.io/api/ai/GetObjectLog",
"getWebAPILog" => "https://api.cryptolens.io/api/ai/GetWebAPILog"
"getWebAPILog" => "https://api.cryptolens.io/api/ai/GetWebAPILog",
# Data
"addDataObject" => "https://api.cryptolens.io/api/data/AddDataObject",
"listDataObjects" => "https://api.cryptolens.io/api/data/ListDataObjects",
"incrementIntValue" => "https://api.cryptolens.io/api/data/IncrementIntValue",
"decramentIntValue" => "https://api.cryptolens.io/api/data/DecramentIntValue",
"setStringValue" => "https://api.cryptolens.io/api/data/SetStringValue",
"setIntValue" => "https://api.cryptolens.io/api/data/SetIntValue",
"removeDataObject" => "https://api.cryptolens.io/api/data/RemoveDataObject",
"uploadValues" => "https://api.cryptolens.io/api/data/UploadValuesToKey"
];

public static array $no_response_check = [
Expand All @@ -59,7 +68,8 @@ class Endpoints {
"getCustomers",
"getEvents",
"getWebAPILog",
"getObjectsLog"
"getObjectsLog",
"listDataObjects"
];

public static function get_endpoint($function_name){
Expand Down
Loading

0 comments on commit 1f338da

Please sign in to comment.