Skip to content

Commit 1f338da

Browse files
authored
Merge pull request #12 from Ente/v1.0
v1.0 - adding support to Data endpoints and offline license validation
2 parents 6dc9dab + 2d89057 commit 1f338da

8 files changed

Lines changed: 386 additions & 5 deletions

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v1.0
4+
5+
* added support to Data endpoints `addDataObject`, `listDataObject`, `incrementIntValue`, `decrementIntValue`, `setStringValue`, `setIntValue`, `removeDataObject`, `uploadValues`
6+
* added support for License file validation `License.cryptolens.php`: `verifyLicenseKey`, `verifyLicenseFromFileContent`. See `README.md` for configuration
7+
38
## v0.9
49

510
* added error handler class `Errors.cryptolens.php` which will now return you the respectful errors and logs them into files.

Cryptolens.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class Cryptolens {
3232
public const CRYPTOLENS_ANALYTICS = "Analytics";
3333

3434
public const CRYPTOLENS_LICENSE = "License";
35+
36+
public const CRYPTOLENS_DATA = "Data";
3537

3638
private string $token;
3739

@@ -80,6 +82,7 @@ public static function loader(){
8082
require_once dirname(__FILE__) . "/classes/Auth.cryptolens.php";
8183
require_once dirname(__FILE__) . "/classes/Product.cryptolens.php";
8284
require_once dirname(__FILE__) . "/classes/PaymentForm.cryptolens.php";
85+
require_once dirname(__FILE__) . "/classes/Data.cryptolens.php";
8386
require_once dirname(__FILE__) . "/classes/Message.cryptolens.php";
8487
require_once dirname(__FILE__) . "/classes/Reseller.cryptolens.php";
8588
require_once dirname(__FILE__) . "/classes/Subscription.cryptolens.php";

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,37 @@ In a real values for you can be obtained as follows:
6363
* 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.
6464
* In an upcoming release this library should be able to also validate license files
6565

66+
### Offline license validation
67+
68+
The API also allows you to validate license files via the `License.cryptolens.php` file.
69+
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).
70+
71+
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)
72+
73+
You can then validate a license key like this:
74+
75+
```php
76+
<?php
77+
78+
require_once "/path/to/autoloader.php";
79+
use Cryptolens_PHP_Client\Cryptolens;
80+
use Cryptolens_PHP_Client\License;
81+
82+
$c = new Cryptolens("YOUR_TOKEN", 12345, Cryptolens::CRYPTOLENS_OUTPUT_PHP);
83+
$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")
84+
85+
$fromString = $l->validateLicense("BASE64-ENCODED-STRING", "BASE64-ENCODED-STRING") // licenseKey and signature
86+
$fromFileContent = $l->validateLicenseFromFileContent(file_get_contents("license.skm")); // License file as string
87+
$fromFile = $l->validateLicenseFromFile("license.skm")
88+
89+
if($fromFile){
90+
echo "Signature successfully validated";
91+
} else {
92+
echo "Could not verify signature";
93+
}
94+
95+
```
96+
6697
## Installation
6798

6899
You can either clone this repository and require the `loader.php` (which contains a autoloader) or use composer via console:
@@ -121,6 +152,11 @@ to automatically load the required classes.
121152
* Payment Form
122153
* [x] create_session
123154
* Analytics
155+
* [x] register_event
156+
* [x] register_events
157+
* [x] get_events
158+
* [x] get_object_log
159+
* [x] get_web_api_log
124160
* Message
125161
* [x] get_messages
126162
* [x] create_message

classes/Data.cryptolens.php

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
<?php
2+
namespace Cryptolens_PHP_Client {
3+
class Data {
4+
5+
private Cryptolens $cryptolens;
6+
7+
private string $group;
8+
9+
public function __construct(Cryptolens $cryptolens){
10+
$this->cryptolens = $cryptolens;
11+
$this->group = CRYPTOLENS::CRYPTOLENS_DATA;
12+
}
13+
14+
/**
15+
* `addDataObject()` - Adds a new Data Object to either a license key, product or your entire account.
16+
* The Cryptolens API provides 3 ways to add a Data object for each type (license, product or account).
17+
*
18+
* @param string $name Name of the DataObject, up to 100 characters
19+
* @param string $value A string to store, up to 10k characters
20+
* @param string $int optional integer to store, default = 0
21+
* @param int $referencerType Where to assign the data object: license key (2), product (1) or User (0), default = 0
22+
* @param string $referencerId ID of the referencer, e.g. the license key or product ID. Not required if "User" (0) is referencerType
23+
* @param bool $checkForDuplicates If set to true, checking for data objects with the same name
24+
* @link https://app.cryptolens.io/docs/api/v3/addDataObject
25+
* @return array|bool
26+
*/
27+
public function addDataObject(string $name, string $value, int $int = 0, int $referencerType = 0, string $referencerId = "0", bool $checkForDuplicates = false){
28+
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, [
29+
"Name" => $name,
30+
"StringValue" => $value,
31+
"IntValue" => $int,
32+
"ReferencerType" => $referencerType,
33+
"ReferencerId" => $referencerId,
34+
"CheckForDuplicates" => $checkForDuplicates
35+
]);
36+
37+
$c = Helper::connection($parms, "addDataObject", $this->group);
38+
if($c == true){
39+
if(Helper::check_rm($c)){
40+
return Cryptolens::outputHelper($c);
41+
} else {
42+
return Cryptolens::outputHelper($c, 1);
43+
}
44+
} else {
45+
return false;
46+
}
47+
}
48+
49+
/**
50+
* `listDataObjects()` - Returns all data objects associated with a key, product or account
51+
* @param int $referencerType Where to assign the data object: license key (2), product (1) or User (0), default = 0
52+
* @param string $referencerId ID of the referencer, e.g. the license key or product ID. Not required if "User" (0) is referencerType
53+
* @param string $contains A string, if set only returns Data object where "name" == $contains, default = ""
54+
* @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.
55+
* @link https://app.cryptolens.io/docs/api/v3/ListDataObjects
56+
* @return array|bool Array on success and false on failure
57+
*/
58+
public function listDataObjects(int $referencerType = 0, string $referencerId = "0", string $contains = "", bool $showAll = true){
59+
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, [
60+
"ReferencerType" => $referencerType,
61+
"ReferencerId" => $referencerId,
62+
"Contains" => $contains,
63+
"ShowAll" => $showAll
64+
]);
65+
$c = Helper::connection($parms, "listDataObjects", $this->group);
66+
if($c == true){
67+
if(Helper::check_rm($c)){
68+
return Cryptolens::outputHelper($c);
69+
} else {
70+
return Cryptolens::outputHelper($c, 1);
71+
}
72+
} else {
73+
return false;
74+
}
75+
}
76+
77+
/**
78+
* `incrementIntValue()` - Increment the Int value of an Data Object
79+
* @param int $id Unique ID of the data object
80+
* @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)
81+
* @param bool $enableBound If set to true, you can set a upper bound, e.g. 10 - if you reach 10 an error occurs.
82+
* @param int $bond Upper bound to enforce if $enableBond has been enabled
83+
* @link https://app.cryptolens.io/docs/api/v3/incrementIntValue
84+
* @return array|bool Array on success and false on failure
85+
*/
86+
public function incrementIntValue(int $id, int $value = 0, bool $enableBound = false, int $bound = 0){
87+
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, [
88+
"Id" => $id,
89+
"IntValue" => $value,
90+
"EnableBound" => $enableBound,
91+
"Bound" => $bound
92+
]);
93+
$c = Helper::connection($parms, "incrementIntValue", $this->group);
94+
if($c == true){
95+
if(Helper::check_rm($c)){
96+
return Cryptolens::outputHelper($c);
97+
} else {
98+
return Cryptolens::outputHelper($c, 1);
99+
}
100+
} else {
101+
return false;
102+
}
103+
}
104+
105+
/**
106+
* `decrementIntValue()` - Increment the Int value of an Data Object
107+
* @param int $id Unique ID of the data object
108+
* @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)
109+
* @param bool $enableBound If set to true, you can set a lower bound, e.g. 10 - if you reach 10 an error occurs.
110+
* @param int $bond Lower bound to enforce if $enableBond has been enabled
111+
* @link https://app.cryptolens.io/docs/api/v3/decrementIntValue
112+
* @return array|bool Array on success and false on failure
113+
*/
114+
public function decrementIntValue(int $id, int $value = 0, bool $enableBound = false, int $bound = 0){
115+
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, [
116+
"Id" => $id,
117+
"IntValue" => $value,
118+
"EnableBound" => $enableBound,
119+
"Bound" => $bound
120+
]);
121+
$c = Helper::connection($parms, "decrementIntValue", $this->group);
122+
if($c == true){
123+
if(Helper::check_rm($c)){
124+
return Cryptolens::outputHelper($c);
125+
} else {
126+
return Cryptolens::outputHelper($c, 1);
127+
}
128+
} else {
129+
return false;
130+
}
131+
}
132+
133+
/**
134+
* `setStringValue()` - Set the string value of a data object
135+
* @param int $id Unique ID of the data object
136+
* @param string $value String value to set
137+
* @link https://app.cryptolens.io/docs/api/v3/setStringValue
138+
* @return array|bool
139+
*/
140+
public function setStringValue(int $id, string $value = null){
141+
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, [
142+
"Id" => $id,
143+
"StringValue" => $value
144+
]);
145+
$c = Helper::connection($parms, "setStringValue", $this->group);
146+
if($c == true){
147+
if(Helper::check_rm($c)){
148+
return Cryptolens::outputHelper($c);
149+
} else {
150+
return Cryptolens::outputHelper($c, 1);
151+
}
152+
} else {
153+
return false;
154+
}
155+
}
156+
157+
/**
158+
* `setIntValue()` - Set the int value of a data object
159+
* @param int $id Unique ID of the data object
160+
* @param string $value String value to set
161+
* @link https://app.cryptolens.io/docs/api/v3/setIntValue
162+
* @return array|bool
163+
*/
164+
public function setIntValue(int $id, int $value = null){
165+
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, [
166+
"Id" => $id,
167+
"IntValue" => $value
168+
]);
169+
$c = Helper::connection($parms, "setIntValue", $this->group);
170+
if($c == true){
171+
if(Helper::check_rm($c)){
172+
return Cryptolens::outputHelper($c);
173+
} else {
174+
return Cryptolens::outputHelper($c, 1);
175+
}
176+
} else {
177+
return false;
178+
}
179+
}
180+
181+
/**
182+
* `removeDataObject()` - Remove a data object
183+
* @param int $id Unique ID of the data object
184+
* @link https://app.cryptolens.io/docs/api/v3/removeDataObject
185+
* @return array|bool
186+
*/
187+
public function removeDataObject(int $id){
188+
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, [
189+
"Id" => $id
190+
]);
191+
$c = Helper::connection($parms, "removeDataObject", $this->group);
192+
if($c == true){
193+
if(Helper::check_rm($c)){
194+
return Cryptolens::outputHelper($c);
195+
} else {
196+
return Cryptolens::outputHelper($c, 1);
197+
}
198+
} else {
199+
return false;
200+
}
201+
}
202+
203+
/**
204+
* `uploadValues()` - Upload values from a Cryptolens log file to either increment or decrement a data object
205+
* @param int $id Unique ID of the data object
206+
* @param string $key Serial Key
207+
* @param string $data String in base64 created by Cryptolens SDK that has recorded data object usage.
208+
* @link https://app.cryptolens.io/docs/api/v3/removeDataObject
209+
* @return array|bool
210+
*/
211+
public function uploadValues(int $id, string $key, string $data){
212+
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), $key, null, [
213+
"Id" => $id,
214+
"Data" => $data
215+
]);
216+
$c = Helper::connection($parms, "removeDataObject", $this->group);
217+
if($c == true){
218+
if(Helper::check_rm($c)){
219+
return Cryptolens::outputHelper($c);
220+
} else {
221+
return Cryptolens::outputHelper($c, 1);
222+
}
223+
} else {
224+
return false;
225+
}
226+
}
227+
}
228+
}
229+
230+
231+
?>

classes/Endpoints.cryptolens.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,16 @@ class Endpoints {
4545
"registerEvents" => "https://api.cryptolens.io/api/ai/RegisterEvents",
4646
"getEvents" => "https://api.cryptolens.io/api/ai/GetEvents",
4747
"getObjectLog" => "https://api.cryptolens.io/api/ai/GetObjectLog",
48-
"getWebAPILog" => "https://api.cryptolens.io/api/ai/GetWebAPILog"
48+
"getWebAPILog" => "https://api.cryptolens.io/api/ai/GetWebAPILog",
49+
# Data
50+
"addDataObject" => "https://api.cryptolens.io/api/data/AddDataObject",
51+
"listDataObjects" => "https://api.cryptolens.io/api/data/ListDataObjects",
52+
"incrementIntValue" => "https://api.cryptolens.io/api/data/IncrementIntValue",
53+
"decramentIntValue" => "https://api.cryptolens.io/api/data/DecramentIntValue",
54+
"setStringValue" => "https://api.cryptolens.io/api/data/SetStringValue",
55+
"setIntValue" => "https://api.cryptolens.io/api/data/SetIntValue",
56+
"removeDataObject" => "https://api.cryptolens.io/api/data/RemoveDataObject",
57+
"uploadValues" => "https://api.cryptolens.io/api/data/UploadValuesToKey"
4958
];
5059

5160
public static array $no_response_check = [
@@ -59,7 +68,8 @@ class Endpoints {
5968
"getCustomers",
6069
"getEvents",
6170
"getWebAPILog",
62-
"getObjectsLog"
71+
"getObjectsLog",
72+
"listDataObjects"
6373
];
6474

6575
public static function get_endpoint($function_name){

0 commit comments

Comments
 (0)