Skip to content

Commit 2e864f0

Browse files
authored
Merge pull request #14 from Ente/v1.2
v1.2
2 parents d5408ef + 823b2d0 commit 2e864f0

22 files changed

+400
-126
lines changed

CHANGELOG.md

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

3+
## v1.2
4+
5+
* All API classes can now directly be used from the Cryptolens base class
6+
7+
* Analytics class no longer extends Cryptolens base class
8+
* removed php closing tag within classes and excess empty lines
9+
* added documentation for internal functions
10+
* quality improvements
11+
* added php class headers
12+
313
## v1.1
414

515
* added support to User authentication endpoints `login`, `register`, `associate`, `dissociate`, `getUsers`, `changePassword`, `resetPasswordToken`, `removeUser` <!-- I simply forgot this endpoint group exists aswell or it has been added newly. -->

Cryptolens.php

Lines changed: 138 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
11
<?php
2+
/**
3+
* Cryptolens_PHP_Client
4+
*
5+
* Namespace for all classes implementing API endpoints
6+
*/
27
namespace Cryptolens_PHP_Client {
8+
use Cryptolens_PHP_Client\Analytics;
9+
use Cryptolens_PHP_Client\Auth;
10+
use Cryptolens_PHP_Client\Customer;
11+
use Cryptolens_PHP_Client\Data;
12+
use Cryptolens_PHP_Client\Endpoints;
13+
use Cryptolens_PHP_Client\Key;
14+
use Cryptolens_PHP_Client\License;
15+
use Cryptolens_PHP_Client\Message;
16+
use Cryptolens_PHP_Client\PaymentForm;
17+
use Cryptolens_PHP_Client\Reseller;
18+
use Cryptolens_PHP_Client\Results;
19+
use Cryptolens_PHP_Client\Subscription;
20+
use Cryptolens_PHP_Client\User;
321
/**
422
* Cryptolens main class
5-
*
623
* This is the cryptolens main class containing an auto loader. It is also holding the sensitive login information needed for the API.
724
* You can define the output to either JSON or Arrays (PHP)
25+
*
26+
* @author Bryan Böhnke-Avan <bryan@openducks.org>
27+
* @license MIT
28+
* @since v0.1
29+
* @link https://app.cryptolens.io/docs/api/v3
830
*/
931
class Cryptolens {
1032

@@ -43,11 +65,30 @@ class Cryptolens {
4365

4466
public static int $output;
4567

68+
private $analytics;
69+
private $auth;
70+
private $customer;
71+
private $data;
72+
private $endpoints;
73+
private $key;
74+
private $license;
75+
private $message;
76+
private $paymentForm;
77+
private $product;
78+
private $reseller;
79+
private $subscription;
80+
private $user;
81+
4682

47-
private function set_token(string $token): void{
83+
/**
84+
* `setToken()` - Setter for API token
85+
* @param string $token The API token to set
86+
* @return void
87+
*/
88+
private function setToken(string $token): void{
4889
$this->token = $token;
4990
}
50-
private function set_product_id($product_id){
91+
private function setProductId($product_id){
5192
$this->productId = $product_id;
5293
}
5394

@@ -56,23 +97,40 @@ private function set_product_id($product_id){
5697
*/
5798
public function __construct($token, $productid, $output = self::CRYPTOLENS_OUTPUT_PHP){
5899
self::$output = $output;
59-
$this->set_token($token);
60-
$this->set_product_id($productid);
100+
$this->setToken($token);
101+
$this->setProductId($productid);
61102
}
62103

63104

64-
public function get_token(){
105+
/**
106+
* `getToken()` - Getter for API token
107+
* @return string Returns the API Authentication token
108+
*/
109+
public function getToken(){
65110
return $this->token;
66111
}
67112

68-
public function get_product_id(){
113+
/**
114+
* `getProductId()` - Getter for product ID
115+
* @return int Returns the product ID
116+
*/
117+
public function getProductId(){
69118
return $this->productId;
70119
}
71120

72-
public function set_output($output){
121+
/**
122+
* `setOutput()` - Setter for output mode, either `Cryptolens::CRYPTOLENS_OUTPUT_PHP` (1) or `Cryptolens::CRYPTOLENS_OUTPUT_JSON` (2)
123+
* @param mixed $output
124+
* @return void
125+
*/
126+
public function setOutput($output){
73127
$this->output = $output;
74128
}
75129

130+
/**
131+
* `loader()` - Loads all required sub classes
132+
* @return void `require_once`s the API classes
133+
*/
76134
public static function loader(){
77135
require_once dirname(__FILE__) . "/classes/Helper.cryptolens.php";
78136
require_once dirname(__FILE__) . "/classes/Errors.cryptolens.php";
@@ -89,11 +147,17 @@ public static function loader(){
89147
require_once dirname(__FILE__) . "/classes/Customer.cryptolens.php";
90148
require_once dirname(__FILE__) . "/classes/Analytics.cryptolens.php";
91149
require_once dirname(__FILE__) . "/classes/User.cryptolens.php";
92-
@require_once dirname(__FILE__) . "/classes/License.cryptolens.php";
150+
require_once dirname(__FILE__) . "/classes/License.cryptolens.php";
93151

94152

95153
}
96154

155+
/**
156+
* `outputHelper` - Returns the corresponding data type, either JSON or an PHP array
157+
* @param mixed $data The data returned checked by `Helper::connection(...)`
158+
* @param int $error
159+
* @return array|bool|string
160+
*/
97161
public static function outputHelper($data, int $error = 0){
98162
if(self::$output == self::CRYPTOLENS_OUTPUT_PHP){
99163
if($error == 1){
@@ -113,11 +177,73 @@ public static function outputHelper($data, int $error = 0){
113177
}
114178
return json_encode($data);
115179
}
180+
return (array) $data;
116181
}
117182

118-
}
119-
}
183+
public function analytics(): Analytics {
184+
if(!$this->analytics) $this->analytics = new Analytics($this);
185+
return $this->analytics;
186+
}
187+
188+
public function auth(): Auth {
189+
if(!$this->auth) $this->auth = new Auth($this);
190+
return $this->auth;
191+
}
192+
193+
public function customer(): Customer {
194+
if(!$this->customer || $this->customer === null) $this->customer = new Customer($this);
195+
return $this->customer;
196+
}
120197

198+
public function data(): Data{
199+
if(!$this->data) $this->data = new Data($this);
200+
return $this->data;
201+
}
202+
203+
public function endpoints(): Endpoints {
204+
if(!$this->endpoints) $this->endpoints = new Endpoints;
205+
return $this->endpoints;
206+
}
207+
208+
public function key(): Key {
209+
if(!$this->key) $this->key = new Key($this);
210+
return $this->key;
211+
}
212+
213+
public function license(bool $publicKeyIsXMLFormat = false, ?string $publicKeyFile = null): License {
214+
if(!$this->license) $this->license = new License($publicKeyIsXMLFormat, $publicKeyFile);
215+
return $this->license;
216+
}
217+
218+
public function message(?string $channel = null): Message {
219+
if(!$this->message) $this->message = new Message($this, $channel);
220+
return $this->message;
221+
}
222+
223+
public function paymentForm(): PaymentForm {
224+
if(!$this->paymentForm) $this->paymentForm = new PaymentForm($this);
225+
return $this->paymentForm;
226+
}
227+
228+
public function product(): Product {
229+
if(!$this->product) $this->product = new Product($this);
230+
return $this->product;
231+
}
232+
233+
public function reseller(): Reseller {
234+
if(!$this->reseller) $this->reseller = new Reseller($this);
235+
return $this->reseller;
236+
}
121237

238+
public function subscription(): Subscription {
239+
if(!$this->subscription) $this->subscription = new Subscription($this);
240+
return $this->subscription;
241+
}
242+
243+
public function user(): User {
244+
if(!$this->user) $this->user = new User($this);
245+
return $this->user;
246+
}
122247

123-
?>
248+
}
249+
}

Cryptolens_old.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
<?php
22

3-
function cryptolens_activate($token, $product_id, $key, $machine_code)
3+
/**
4+
* `cryptolens_activate` - Standalone function to activate a key
5+
* @param mixed $token The token to authenticate against the API
6+
* @param mixed $product_id Your product ID
7+
* @param mixed $key The key to activate
8+
* @param mixed $machine_code optional machine code to bind the key to
9+
* @return bool Either true on success or false on failure
10+
*
11+
* @author Cryptolens AB
12+
* @link https://github.com/cryptolens/cryptolens-php
13+
* @deprecated You should use the library. This functions exits only for backwards compatibility.
14+
*/
15+
function cryptolens_activate(string $token, string $product_id, string $key, ?string $machine_code)
416
{
517
$params =
618
array(

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Cryptolens PHP
22

3-
This repository contains functions for interacting with the Cryptolens
4-
Web API from PHP. Currently most endpoints are supported and more are following.
3+
This repository contains functions for interacting with the Cryptolens Web API from PHP.
4+
All endpoints are supported. This API client uses Cryptolens Web API 3.
55

6-
For more information about the API and possible values and types, visit https://app.cryptolens.io/docs/api/v3, the official API documentation
6+
For more information about the API and possible values and types, visit https://app.cryptolens.io/docs/api/v3, the official API documentation.
77

88
To use the library, you can `require_once` the `loader.php` which loads all other classes automatically or use composer where you just have to `require` the composer `autoload.php`. Currently, this library is not safe to use for CLI.
99
Inside your script you need to `use` the classes, here is an example:
@@ -24,6 +24,11 @@ $k = new Key($c);
2424
$key = "XXXXX-XXXXX-XXXXX-XXXXX";
2525
$machine_id = $k->getMachineId();
2626
print_r("Key 'activate':" . var_dump($k->activate($key, $machine_id)));
27+
28+
// OR
29+
30+
$c->key()->activate($key, $c->key()->getMachineId());
31+
2732
?>
2833
```
2934

classes/Analytics.cryptolens.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
<?php
2-
32
namespace Cryptolens_PHP_Client {
4-
class Analytics extends Cryptolens {
3+
/**
4+
* Analytics
5+
*
6+
* Allows the use of all Analytics endpoints
7+
*
8+
* @author Bryan Böhnke-Avan <bryan@openducks.org>
9+
* @license MIT
10+
* @since v0.9
11+
* @link https://app.cryptolens.io/docs/api/v3/AI
12+
*/
13+
class Analytics {
514
private Cryptolens $cryptolens;
615

716
private string $group;
@@ -38,7 +47,7 @@ public function register_event(string $key, string $machineid, string $featurena
3847
"Currency" => $currency,
3948
"Metadata" => $metadata
4049
);
41-
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), $key, $machineid, $additional_flags);
50+
$parms = Helper::build_params($this->cryptolens->getToken(), $this->cryptolens->getProductId(), $key, $machineid, $additional_flags);
4251
$c = Helper::connection($parms, "registerEvent", $this->group);
4352
if($c == true){
4453
if(Helper::check_rm($c)){
@@ -71,7 +80,7 @@ public function register_events(string $key = null, string $machineid = null, ar
7180
"MachineCode" => $machineid,
7281
"Events" => $events
7382
);
74-
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), $key, $machineid, $additional_flags);
83+
$parms = Helper::build_params($this->cryptolens->getToken(), $this->cryptolens->getProductId(), $key, $machineid, $additional_flags);
7584
$c = Helper::connection($parms, "registerEvents", $this->group);
7685
if($c == true){
7786
if(Helper::check_rm($c)){
@@ -102,7 +111,7 @@ public function get_events(int $limit = 10, int $startingafter = null, int $endi
102111
if(isset($key) && !isset($product_id)){
103112
return false;
104113
}
105-
$parms = Helper::build_params($this->cryptolens->get_token(), $product_id ?? $this->cryptolens->get_product_id(), $key, null, array("Limit" => $limit, "StartingAfter" => $startingafter, "EndingBefore" => $endingbefore, "Time" => $time, "Metadata" => $metadata));
114+
$parms = Helper::build_params($this->cryptolens->getToken(), $product_id ?? $this->cryptolens->getProductId(), $key, null, array("Limit" => $limit, "StartingAfter" => $startingafter, "EndingBefore" => $endingbefore, "Time" => $time, "Metadata" => $metadata));
106115
$c = Helper::connection($parms, "getEvents", $this->group);
107116
if($c == true){
108117
if(Helper::check_rm($c)){
@@ -128,7 +137,7 @@ public function get_object_log(int $limit = 10, int $startingafter = null){
128137
if($limit > 100){
129138
return false;
130139
}
131-
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, array("Limit" => $limit, "StartingAfter" => $startingafter));
140+
$parms = Helper::build_params($this->cryptolens->getToken(), $this->cryptolens->getProductId(), null, null, array("Limit" => $limit, "StartingAfter" => $startingafter));
132141
$c = Helper::connection($parms, "getObjectLog", $this->group);
133142
if($c == true){
134143
if(Helper::check_rm($c)){
@@ -163,7 +172,7 @@ public function get_web_api_log(int $product_id = null, string $key = null, $lim
163172
return false;
164173
}
165174

166-
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), null, null, array("ProductId" => $product_id, "Key" => $key, "Limit" => $limit, "StartingAfter" => $startingafter, "EndingBefore" => $endingbefore, "AnomalyClassification" => $anomalyClassification));
175+
$parms = Helper::build_params($this->cryptolens->getToken(), $this->cryptolens->getProductId(), null, null, array("ProductId" => $product_id, "Key" => $key, "Limit" => $limit, "StartingAfter" => $startingafter, "EndingBefore" => $endingbefore, "AnomalyClassification" => $anomalyClassification));
167176
$c = Helper::connection($parms, "getWebAPILog", $this->group);
168177
if($c == true){
169178
if(Helper::check_rm($c)){
@@ -177,6 +186,3 @@ public function get_web_api_log(int $product_id = null, string $key = null, $lim
177186
}
178187
}
179188
}
180-
181-
182-
?>

classes/Auth.cryptolens.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
<?php
22
namespace Cryptolens_PHP_Client {
3+
/**
4+
* Auth
5+
*
6+
* Allows the use of all Auth endpoints
7+
*
8+
* @author Bryan Böhnke-Avan <bryan@openducks.org
9+
* @license MIT
10+
* @since v0.4
11+
* @link https://app.cryptolens.io/docs/api/v3/AuthMethods
12+
*/
313
class Auth {
414

515
private Cryptolens $cryptolens;
@@ -18,7 +28,7 @@ public function __construct($cryptolens){
1828
* @return array|bool Returns an array on success, false on failure
1929
*/
2030
public function key_lock($key){
21-
$parms = Helper::build_params($this->cryptolens->get_token(), $this->cryptolens->get_product_id(), $key);
31+
$parms = Helper::build_params($this->cryptolens->getToken(), $this->cryptolens->getProductId(), $key);
2232
$c = Helper::connection($parms, "keyLock", $this->group);
2333
if($c == true){
2434
if(Helper::check_rm($c)){
@@ -33,6 +43,3 @@ public function key_lock($key){
3343
}
3444
}
3545
}
36-
37-
38-
?>

0 commit comments

Comments
 (0)