Skip to content

Instantly share code, notes, and snippets.

@Achterstraat Achterstraat/Vimexx.php

Last active Apr 26, 2020
Embed
What would you like to do?
<?php
/*
First, create API Projects at https://www.vimexx.nl/api
Second, replace all vars between [] with credentials from previous step!
Third, see below;
$vimexx = (new \Vimexx([account], 'wefact'));
var_export($vimexx->post('/domains'));
var_dump($vimexx->client('whmcs')->post('/domain/dns', ['sld' => 'achterstraat', 'tld' => 'com']));
var_export($vimexx->client('wefact')->post('/domains'));
*/
class Vimexx
{
public $debug;
public $test = false;
public $token = false;
public $accounts = [
[account] => [
'user' => '[emailaddress]',
'pass' => '[password]',
'clients' => [
'wefact' => [
'id' => [api-id],
'secret' => '[api-hash]',
'version' => '5.1.2'
],
'whmcs' => [
'id' => [api-id],
'secret' => '[api-hash]',
'version' => '7.7.1-release.1'
],
],
],
];
public $account;
public $requests = [
'wefact' => [
'DELETE' => [
'contact' => ['contact_id'],
],
'POST' => [
'contact' => ['contact_id'],
'contacts' => [],
'dns' => ['sld', 'tld'],
'domain' => ['sld', 'tld'],
'domains' => [],
'domain/available' => ['sld', 'tld'],
'domain/extend' => ['sld', 'tld', 'years'],
'domain/register' => ['sld', 'tld', 'nameservers', 'whois'],
'domain/token' => ['sld', 'tld'],
'domain/transfer' => ['sld', 'tld', 'nameservers', 'whois', 'epp'],
],
'PUT' => [
'contact' => ['contact'],
'contacts' => ['sld', 'tld', 'contacts'],
'dns' => ['sld', 'tld', 'dns_records'],
'domain' => ['sld', 'tld', 'auto_renew', 'lock'],
'nameservers' => ['sld', 'tld', 'nameservers', 'name'],
],
],
'whmcs' => [
'DELETE' => [
'domain' => ['sld', 'tld'],
],
'POST' => [
'domain/dns' => ['sld', 'tld'],
'domain/extend' => ['sld', 'tld'],
'domain/nameservers' => ['sld', 'tld'],
'domain/sync' => ['sld', 'tld'],
'domain/sync/transfer' => ['sld', 'tld'],
'domain/register' => ['sld', 'tld'],
'domain/token' => ['sld', 'tld'],
'domain/transfer' => ['sld', 'tld', 'epp'],
],
'PUT' => [
'domain/dns' => ['sld', 'tld', 'dns_records'],
'domain/nameservers' => ['sld', 'tld', 'nameservers', 'name'],
],
],
];
public $request;
public $versions = [
'wefact' => ['5.1.2', '5.0.0'],
'whmcs' => ['7.7.1-release.1', '7.6.1-release.1', '7.5.2-release.1'],
];
public $version;
function __construct($account = 0, $client = 'whmcs', $debug = false)
{
$this->debug = $debug;
if($this->account($account))
{
if($this->client($client))
{
return $this;
}
return ['error' => 'Unknown client!'];
}
return ['error' => 'Unknown account!'];
}
function __call($method, $args)
{
if(in_array($method, ['delete', 'get', 'post', 'put']))
{
array_unshift($args, strtoupper($method));
return call_user_func_array([$this, 'request'], $args);
}
}
public function account($id = 0)
{
if(array_key_exists($id, $this->accounts))
{
$this->account = [
'id' => $id,
'user' => $this->accounts[$id]['user'],
'pass' => $this->accounts[$id]['pass']
];
return $this;
}
return false;
}
public function client($client = 'whmcs')
{
$id = $this->account['id'];
if(array_key_exists($client, $this->accounts[$id]['clients']))
{
$this->account = array_merge($this->account, [
'client' => $client,
'credentials' => (empty($this->accounts[$id]['clients'][$client]) ? false : $this->accounts[$id]['clients'][$client]),
'version' => (in_array($this->accounts[$id]['clients'][$client]['version'], $this->versions) ? $this->accounts[$id]['clients'][$client]['version'] : $this->versions[$client][0]),
]);
return $this;
}
return false;
}
public function request($type = 'GET', $request = '/', $data = [])
{
if(!in_array($request, [null, '', '/']))
{
$auth = $this->uri('/auth/token', 'login');
$api = $this->uri('/'.$this->account['client'].$request);
list($scope, $request) = explode('/', substr($api, ($this->test ? 34 : 29)), 2);
if(array_key_exists($scope, $this->requests))
{
if(array_key_exists($type, $this->requests[$scope]))
{
if(array_key_exists($request, $this->requests[$scope][$type]))
{
$keys = array_keys($data);
$missing = array_diff($this->requests[$scope][$type][$request], $keys);
if(empty($missing))
{
$data = [
'body' => $data,
'version' => $this->account['version']
];
$token = $this->token($auth);
if(empty($token))
{
return ['error' => 'Token failed!'];
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Accept: application/json',
'Authorization: Bearer '.$token,
]);
curl_setopt($curl, CURLOPT_URL, $api);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $type);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$result = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if($this->debug)
{
var_dump([
'requesttype' => $type,
'url' => $api,
'postdata' => $data,
'result' => $result,
'httpcode' => $code
]);
}
$result = json_decode($result, true);
$result['code'] = $code;
if(in_array($code, [200, 201, 202, 400, 401, 404]))
{
return (!empty($result['result']) ? $result['data'] : false);
}
return null;
}
return ['error' => 'Missing '.implode(', ', $missing).' fields!'];
}
return ['error' => 'Undefined request!'];
}
return ['error' => 'Undefined method!'];
}
return ['error' => 'Undefined scope!'];
}
return (array_key_exists($this->account['client'], $this->requests) ? $this->requests[$this->account['client']] : array_keys($this->requests));
}
public function token($url)
{
if(empty($this->token))
{
$data = [
'grant_type' => 'password',
'client_id' => $this->account['credentials']['id'],
'client_secret' => $this->account['credentials']['secret'],
'username' => $this->account['user'],
'password' => $this->account['pass'],
'scope' => 'whmcs-access',
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Content-Type' => 'application/x-www-form-urlencoded',
'charset' => 'utf-8'
]);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
$json = json_decode($result, true);
if(empty($json['access_token']))
{
return false;
}
$this->token = $json['access_token'];
}
return $this->token;
}
public function uri($request = '', $type = 'api', $base = null)
{
$base = rtrim(empty($base) || !stripos($base, '://') ? 'https://api.vimexx.nl' : $base, '/');
switch($type)
{
case 'login': {
return $base.'/auth/token';
}
default: {
return $base.'/'.($this->test ? 'test' : '').'api/v1/'.(empty($request) ? '' : ltrim($request, '/'));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.