Kaspersky KSS
This class enables you to send SOAP requests to the Kaspersky KSS api.
<?php
/**
* Generates requests to Kaspersky Subscription Service 3.0
*/
class KasperskyApi
{
const KASPERSKY_NAMESPACE = 'http://schemas.kaspersky.com/services/subscription/2.0/';
/**
* Set credentials as private:
*/
private $kss_user = false;
private $kss_pass = false;
private $kss_wsdl = false;
private $pem_cert = false;
private $pem_pass = false;
/**
* Set private variables:
*/
private $request_actions = array(); // Contains array with all actions that will be part of the request. NOTE: the variable is set to empty array when a SOAP request is sent.
private $params = array(); // The request action variables. NOTE: the variable is set to empty array when the request action is set.
private $TransactionId = false; // The ID of the transaction.
private $trace = true; // Trace the SOAP request or not. Use set_trace() function.
/**
* Set public variables. NOTE this variables are used only to provide information outside of the class.
*/
public $trace_data = array(); // If trace is set and SOAP request is done this variable will contain all of the debug information.
public $errors = array(); // Contains the errors of the last SOAP request if there are any.
/**
* Set credentials for KSS and the complete path to the WSDL
* @param string $TransactionId - Unique transaction identifier used for the request-response association. Generated by Service Provider upon sending the request. See section 6.2, Transaction ID format.
* @param string $kss_user - Username required for the SOAP request (provided by Kaspersky).
* @param string $kss_pass - Password required for the SOAP request (provided by Kaspersky).
* @param string $kss_wsdl - The complete path to the WSDL file (provided by Kaspersky).
* @param string $pem_pass - The pem certificate password.
* @param string $pem_cert - The pem certificate file.
*/
public function __construct($TransactionId, $kss_user, $kss_pass, $kss_wsdl, $pem_pass, $pem_cert)
{
$this->TransactionId = $TransactionId;
$this->kss_user = $kss_user;
$this->kss_pass = $kss_pass;
$this->kss_wsdl = $kss_wsdl;
$this->pem_cert = $pem_cert;
$this->pem_pass = $pem_pass;
}
/**
* Set SOAP trace value.
* NOTE: Use to debug
* @param int $trace - The SOAP trace parameter
*/
public function set_trace($trace)
{
$this->trace = (int) $trace;
}
/**
* Returns the given date in the format required by Kaspersky.
* @param $datetime - The required date in format "Y-m-d H:i:s" for the current timezone.
* @return $kaspersky_time - The date in the format required by Kaspersky.
*/
private function get_kaspersky_date($datetime = false)
{
$gmt_offset = date('Z') / 3600;
$kaspersky_time = date('Y-m-d\TH:i:s', strtotime($datetime . '-' . $gmt_offset . ' hours'));
if ($gmt_offset > 0) {
$kaspersky_time .= '+' . sprintf("%02d", $gmt_offset) . ':00';
} elseif ($gmt_offset < 0) {
$kaspersky_time .= '-' . sprintf("%02d", abs($gmt_offset)) . ':00';
} else {
$kaspersky_time .= 'Z';
}
return $kaspersky_time;
}
/**
* Sent SOAP request to Kaspersky:
* @return $result - The result of the SOAP request as returned by Kaspersky
*/
private function send_soap_request()
{
$this->errors = array();
$SubscriptionRequest = array();
$soap_params = array(
'TransactionId' => $this->TransactionId,
'Timestamp' => $this->get_kaspersky_date(),
'SubscriptionRequest' => $this->request_actions,
);
$this->request_actions = array();
$client = new \SoapClient($this->kss_wsdl, array(
'trace' => $this->trace,
'local_cert' => $this->pem_cert,
'passphrase' => $this->pem_pass,
'cache_wsdl' => WSDL_CACHE_NONE,
'connection_timeout' => 15,
'verifypeer' => false,
'verifyhost' => false,
));
$auth = array(
'UserName' => $this->kss_user,
'Password' => $this->kss_pass,
);
$header = new \SoapHeader(self::KASPERSKY_NAMESPACE, 'AccessInfo', $auth, false);
$client->__setSoapHeaders($header);
$microtime = microtime(true);
$result = null;
try {
$result = $client->SubscriptionRequest($soap_params);
} catch (\SoapFault $fault) {
$this->kss_error_log("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})");
}
if ($this->trace) {
// Debug mode
$this->trace_data = array();
$this->trace_data['soap_params'] = $soap_params;
$this->trace_data['LastRequestHeaders'] = $client->__getLastRequestHeaders();
$this->trace_data['LastRequest'] = $client->__getLastRequest();
$this->trace_data['LastResponse'] = $client->__getLastResponse();
$microtime = microtime(true) - $microtime;
if ($microtime > 1) {
$this->kss_log('KSS slow response ' . $microtime, 'KSSv3API');
$this->kss_log($this->trace_data, 'KSSv3API');
$this->kss_log($result, 'KSSv3API');
}
}
if (is_array($result) || is_object($result)) {
foreach ($result as $key => $value) {
if (strpos($key, 'Error')) {
$this->errors[$key] = $value;
$this->kss_error_log('Kaspersky response error serialized: [' . serialize($value) . ']');
}
}
}
return $result;
}
/**
* Sets actions that will be added to the next SOAP request
* @param string $action_type - The action type as described in Kaspersky documentation.
*/
private function set_action($action_type)
{
if (!isset($this->request_actions[$action_type])) {
$this->request_actions[$action_type] = array();
}
$action_data = array(
'UnitId' => count($this->request_actions[$action_type]) + 1,
);
foreach ($this->params as $key => $value) {
$action_data[$key] = $value;
}
$this->request_actions[$action_type][] = $action_data;
$this->params = array();
}
/**
* Activate subscription for the specified Subscriber with defined parameters (subscription until the specified date/time or open-ended subscription, subscription for selected product, etc.).
* Every Subscriber must have a unique identifier (SubscriberId) on the Service Provider’s side.
* @param string $SubscriberId - Unique identifier of the subscriber. See section 6.1, Subscriber ID format.
* @param string $LicenseCount - Number of licenses for subscription. Licenses count cannot be changed during the subscription period. 1 is selected by default if the value is not specified. Acceptable values are defined for each service provider individually.
* @param string $StartTime - Date/timek since when the subscription will be activated (date/time of subscription purchase). See section 6.3, Timestamp format.
* @param string $EndTime - Date/time of subscription expiration (last day of subscription maintenance inclusive). If the subscription is activated for an indefinite period, then "indefinite" must be specified in this field. Value "indefinite" or Date/time as specified in section 6.3, Timestamp format.
* @param string $ProductId - Unique identifier of the application for which the subscription is being activated. Application type cannot be changed during the subscription period. Acceptable values are defined for each service provider individually.
* @param string $ActivationType - Type of activation. Always "Standard".
* @return Object - The result of the SOAP request.
*/
public function activate($SubscriberId, $LicenseCount = '1', $StartTime = false, $EndTime = 'indefinite', $ProductId = 'KISA', $ActivationType = 'Standard')
{
$this->params['SubscriberId'] = $SubscriberId;
$this->params['StartTime'] = $this->get_kaspersky_date($StartTime);
$this->params['EndTime'] = ((int) $EndTime) ? $this->get_kaspersky_date($EndTime) : $EndTime; // NOTE: If string starts with int (like the date format) the Kaspersky format will be set.
$this->params['LicenseCount'] = $LicenseCount;
$this->params['ProductId'] = $ProductId;
$this->params['ActivationType'] = $ActivationType;
$this->set_action('Activate');
return $this->send_soap_request();
}
/**
* Renew subscription. This implies extending the subscription for the specified Subscriber (SubscriberId) to the defined date/time or indefinitely.
* @param string $SubscriberId - Unique identifier of the subscriber. See section 6.1, Subscriber ID format.
* @param string $EndTime - Date/time of subscription expiration (last day of subscription maintenance inclusive). If the subscription is activated for an indefinite period, then "indefinite" must be specified in this field. Value "indefinite" or Date/time as specified in section 6.3, Timestamp format.
*/
public function renew($SubscriberId, $EndTime = 'indefinite')
{
$this->params['SubscriberId'] = $SubscriberId;
$this->params['EndTime'] = ((int) $EndTime) ? $this->get_kaspersky_date($EndTime) : $EndTime; // NOTE: If string starts with int (like the date format) the Kaspersky format will be set.
$this->set_action('Renew');
return $this->send_soap_request();
}
/**
* Soft-cancel subscription. This implies cancelling of a subscription for the specified Subscriber (SubscriberId) since the defined date with a grace period.
* @param string $SubscriberId - Unique identifier of the subscriber. See section 6.1, Subscriber ID format.
* @param string $EndTime - Date/time of subscription expiration (last day of subscription maintenance inclusive). If the subscription is activated for an indefinite period, then "indefinite" must be specified in this field. Value "indefinite" or Date/time as specified in section 6.3, Timestamp format.
*/
public function soft_cancel($SubscriberId, $EndTime = false)
{
$this->params['SubscriberId'] = $SubscriberId;
$this->params['EndTime'] = $this->get_kaspersky_date($EndTime);
$this->set_action('SoftCancel');
return $this->send_soap_request();
}
/**
* Hard-cancel subscription. This implies cancelling of a subscription for the specified Subscriber (SubscriberId) since the defined date.
* @param string $SubscriberId - Unique identifier of the subscriber. See section 6.1, Subscriber ID format.
* @param string $EndTime - Date/time of subscription expiration (last day of subscription maintenance inclusive). If the subscription is activated for an indefinite period, then "indefinite" must be specified in this field. Value "indefinite" or Date/time as specified in section 6.3, Timestamp format.
*/
public function hard_cancel($SubscriberId, $EndTime = false)
{
$this->params['SubscriberId'] = $SubscriberId;
$this->params['EndTime'] = $this->get_kaspersky_date($EndTime);
$this->set_action('HardCancel');
return $this->send_soap_request();
}
/**
* Pause subscription. This implies bgpv2/third_party_libs/kasperskyputting a subscription on a pause for the specified Subscriber (SubscriberId) since the defined date.
* @param string $SubscriberId - Unique identifier of the subscriber. See section 6.1, Subscriber ID format.
* @param string $PauseTime - Date/time of subscription pause (last day of subscription inclusive). See section 6.3, Timestamp format.
*/
public function pause($SubscriberId, $PauseTime = false)
{
$this->params['SubscriberId'] = $SubscriberId;
$this->params['PauseTime'] = $this->get_kaspersky_date($PauseTime);
$this->set_action('Pause');
return $this->send_soap_request();
}
/**
* Resume subscription. This implies resuming a paused subscription for the specified Subscriber (SubscriberId).
* @param string $SubscriberId - Unique identifier of the subscriber. See section 6.1, Subscriber ID format.
*/
public function resume($SubscriberId)
{
$this->params['SubscriberId'] = $SubscriberId;
$this->set_action('Resume');
return $this->send_soap_request();
}
/**
* Getting subscription info. This implies obtaining information on subscription for the specified Subscriber (SubscriberId).
* @param string $SubscriberId - Unique identifier of the subscriber. See section 6.1, Subscriber ID format.
*/
public function get_subscription_info($SubscriberId)
{
// WARNING: Insufficient data in the KSS 3.0 documentation to implement this function!
$this->params['SubscriberId'] = $SubscriberId;
$this->params['InfoSection'] = 'Subscription';
$this->set_action('GetInfo');
return $this->send_soap_request();
}
/**
* Used to debug at this point
*/
public function kss_log($log)
{
if( class_exists('Log') ) {
$log = is_string($log) ? $log: serialize($log);
Log::info($log);
}
}
/**
* Used to log errors
*/
public function kss_error_log($log)
{
if( class_exists('Log') ) {
$log = is_string($log) ? $log: serialize($log);
Log::alert($log);
}
}
/**
* Encripts the activation code
*/
public function kaspersky_encrypt($kasperskyCode, $jarFile, $xorKey)
{
return `java -jar {$jarFile} {$kasperskyCode} {$xorKey}`;
}
/**
*
*/
public function getDownloadUrl($activationCodeEncrypted, $referrer)
{
// Generate the url for kaspersky
$downloadURL = 'https://play.google.com/store/apps/details?id=com.kms.free&referrer=' . $referrer . urlencode( '=' . $activationCodeEncrypted );
return file_get_contents( 'http://tinyurl.com/api-create.php?url=' . urlencode( $downloadURL ) );
}
}
Download...