import { createElement } from './dom';
import { getValue, containerObject, setValue, isNullOrUndefined } from './util';
export const componentList: string[] = ['grid', 'pivotview', 'treegrid', 'spreadsheet', 'rangeNavigator', 'DocumentEditor', 'listbox', 'inplaceeditor', 'PdfViewer', 'richtexteditor', 'DashboardLayout', 'chart', 'stockChart', 'circulargauge', 'diagram', 'heatmap', 'lineargauge', 'maps', 'slider', 'smithchart', 'barcode', 'sparkline', 'treemap', 'bulletChart', 'kanban', 'daterangepicker', 'schedule', 'gantt', 'signature', 'query-builder', 'drop-down-tree', 'carousel', 'filemanager', 'uploader', 'accordion', 'tab', 'treeview'];
const bypassKey: number[] = [115, 121, 110, 99, 102, 117, 115, 105,
111, 110, 46, 105, 115, 76, 105, 99, 86, 97, 108,
105, 100, 97, 116, 101, 100];
let accountURL: string;
/**
* License validation module
*
* @private
*/
class LicenseValidator {
private isValidated: boolean = false;
public isLicensed: boolean = true;
public version: string = '29';
public platform: RegExp = /JavaScript|ASPNET|ASPNETCORE|ASPNETMVC|FileFormats|essentialstudio/i;
private errors: IErrorType = {
noLicense: 'This application was built using a trial version of Syncfusion® Essential Studio®.' +
' To remove the license validation message permanently, a valid license key must be included.',
trailExpired: 'This application was built using a trial version of Syncfusion® Essential Studio®.' +
' To remove the license validation message permanently, a valid license key must be included.',
versionMismatched: 'The included Syncfusion® license key is invalid.',
platformMismatched: 'The included Syncfusion® license key is invalid.',
invalidKey: 'The included Syncfusion® license key is invalid.'
};
public minVersion: number | null = null;
constructor(key?: string) {
this.manager.setKey(key);
}
/**
* To manage licensing operation.
*/
private manager: { setKey: Function, getKey: Function } = (() => {
let licKey: string = null;
/**
* Sets the license key.
*
* @param {string} key - Specifies the license key.
* @returns {void}
*/
function set(key: string): void { licKey = key; }
/**
* Gets the license key.
*
* @returns {string} -Gets the license key.
*/
function get(): string { return licKey; }
return {
setKey: set,
getKey: get
};
})();
/**
* To manage npx licensing operation.
*/
private npxManager: { getKey: Function } = (() => {
const npxLicKey: string = 'npxKeyReplace';
/**
* Gets the license key.
*
* @returns {string} - Gets the license key.
*/
function get(): string { return npxLicKey; }
return {
getKey: get
};
})();
/**
* To validate the provided license key.
*
* @returns {boolean} ?
*/
public validate(): boolean {
const contentKey: number[] = [115, 121, 110, 99, 102, 117, 115, 105, 111, 110, 46,
108, 105, 99, 101, 110, 115, 101, 67, 111, 110, 116, 101, 110, 116];
const URLKey: number[] = [115, 121, 110, 99, 102, 117, 115, 105, 111, 110, 46,
99, 108, 97, 105, 109, 65, 99, 99, 111, 117, 110, 116, 85, 82, 76];
if (!this.isValidated && (containerObject && !getValue(convertToChar(bypassKey), containerObject) && !getValue('Blazor', containerObject))) {
let validateMsg: string;
let validateURL: string;
if ((this.manager && this.manager.getKey()) || (this.npxManager && this.npxManager.getKey() !== 'npxKeyReplace')) {
const result: IValidator[] = this.getInfoFromKey();
if (result && result.length) {
for (const res of result) {
if (!this.platform.test(res.platform) || res.invalidPlatform) {
validateMsg = this.errors.platformMismatched;
}
else {
if (((res.minVersion >= res.lastValue) && (res.minVersion !== res.lastValue)) ||
(res.lastValue < parseInt(this.version, 10))) {
validateMsg = this.errors.versionMismatched;
validateMsg = validateMsg.replace('##LicenseVersion', res.version);
validateMsg = validateMsg.replace('##Requireversion', this.version + '.x');
}
else {
if (res.lastValue == null || isNaN(res.lastValue)) {
validateMsg = this.errors.versionMismatched;
validateMsg = validateMsg.replace('##LicenseVersion', res.version);
validateMsg = validateMsg.replace('##Requireversion', this.version + '.x');
}
}
if (res.expiryDate) {
const expDate: Date = new Date(res.expiryDate);
const currDate: Date = new Date();
if (expDate !== currDate && expDate < currDate) {
validateMsg = this.errors.trailExpired;
} else {
break;
}
}
}
}
} else {
validateMsg = this.errors.invalidKey;
}
} else {
const licenseContent: string = getValue(convertToChar(contentKey), containerObject);
validateURL = getValue(convertToChar(URLKey), containerObject);
if (licenseContent && licenseContent !== '') {
validateMsg = licenseContent;
} else {
validateMsg = this.errors.noLicense;
}
}
if (validateMsg && typeof document !== 'undefined' && !isNullOrUndefined(document)) {
accountURL = (validateURL && validateURL !== '') ? validateURL : 'https://www.syncfusion.com/account/claim-license-key?pl=SmF2YVNjcmlwdA==&vs=Mjk=&utm_source=es_license_validation_banner&utm_medium=listing&utm_campaign=license-information';
const errorDiv: HTMLElement = createElement('div', {
innerHTML: `` + validateMsg + ' ' + 'Claim your free account'
});
errorDiv.setAttribute('style', `position: fixed;
top: 10px;
left: 10px;
right: 10px;
font-size: 14px;
background: #EEF2FF;
color: #222222;
z-index: 999999999;
text-align: left;
border: 1px solid #EEEEEE;
padding: 10px 11px 10px 50px;
border-radius: 8px;
font-family: Helvetica Neue, Helvetica, Arial;`);
document.body.appendChild(errorDiv);
this.isLicensed = false;
}
this.isValidated = true;
setValue(convertToChar(bypassKey), this.isValidated, containerObject);
}
return this.isLicensed;
}
private getDecryptedData(key: string): string {
try {
return atob(key);
}
catch (error) {
return '';
}
}
/**
* Get license information from key.
*
* @returns {IValidator} - Get license information from key.
*/
private getInfoFromKey(): IValidator[] {
try {
let licKey: string = '';
const pkey: number[] = [5439488, 7929856, 5111808, 6488064, 4587520, 7667712, 5439488,
6881280, 5177344, 7208960, 4194304, 4456448, 6619136, 7733248, 5242880, 7077888,
6356992, 7602176, 4587520, 7274496, 7471104, 7143424];
let decryptedStr: string[] = [];
const resultArray: IValidator[] = [];
let invalidPlatform: boolean = false;
let isNpxKey: boolean = false;
if (this.manager.getKey()) {
licKey = this.manager.getKey();
} else {
isNpxKey = true;
licKey = this.npxManager.getKey().split('npxKeyReplace')[1];
}
const licKeySplit: string[] = licKey.split(';');
for (const lKey of licKeySplit) {
const decodeStr: string = this.getDecryptedData(lKey);
if (!decodeStr) {
continue;
}
let k: number = 0;
let buffr: string = '';
if (!isNpxKey) {
for (let i: number = 0; i < decodeStr.length; i++, k++) {
if (k === pkey.length) { k = 0; }
const c: number = decodeStr.charCodeAt(i);
buffr += String.fromCharCode(c ^ (pkey[parseInt(k.toString(), 10)] >> 16));
}
} else {
const charKey: string = decodeStr[decodeStr.length - 1];
const decryptedKey: number[] = [];
for (let i: number = 0; i < decodeStr.length; i++) {
decryptedKey[parseInt(i.toString(), 10)] = decodeStr[parseInt(i.toString(), 10)].charCodeAt(0)
- charKey.charCodeAt(0);
}
for (let i: number = 0; i < decryptedKey.length; i++) {
buffr += String.fromCharCode(decryptedKey[parseInt(i.toString(), 10)]);
}
}
if (this.platform.test(buffr)) {
decryptedStr = buffr.split(';');
invalidPlatform = false;
// checked the length to verify the key in proper strucutre
if (decryptedStr.length > 3) {
const minVersion: number = parseInt(decryptedStr[1].split('.')[0], 10);
const lastValue: number = parseInt(decryptedStr[4], 10);
resultArray.push({
platform: decryptedStr[0],
version: decryptedStr[1],
expiryDate: decryptedStr[2],
lastValue: lastValue,
minVersion: minVersion
});
}
} else if (buffr && buffr.split(';').length > 3) {
invalidPlatform = true;
}
}
if (invalidPlatform && !resultArray.length) {
return [{ invalidPlatform: invalidPlatform }];
} else {
return resultArray.length ? resultArray : null;
}
} catch (error) {
return null;
}
}
}
let licenseValidator: LicenseValidator = new LicenseValidator();
/**
* Converts the given number to characters.
*
* @param {number} cArr - Specifies the license key as number.
* @returns {string} ?
*/
function convertToChar(cArr: number[]): string {
let ret: string = '';
for (const arr of cArr) {
ret += String.fromCharCode(arr);
}
return ret;
}
/**
* To set license key.
*
* @param {string} key - license key
* @returns {void}
*/
export function registerLicense(key: string): void {
licenseValidator = new LicenseValidator(key);
}
export const validateLicense: Function = (key?: string): boolean => {
if (key) {
registerLicense(key);
}
return licenseValidator.validate();
};
export const getVersion: Function = (): string => {
return licenseValidator.version;
};
// Method for create overlay over the sample
export const createLicenseOverlay: Function = (): void => {
const bannerTemplate: string = `