@@ -106,8 +106,21 @@ module.exports = class Helpers {
106106 return ! Helpers . HasExpired ( licenseKey ) ;
107107 }
108108
109+
109110 /**
110- * Check if the license has a certain feature enabled (i.e. set to true).
111+ * Check if the license has a certain feature enabled (i.e. set to true). If the feature parameter is an integer, then
112+ * this method will check against the 8 built in feature flags (F1..F8). If feature parameter is a string, the method
113+ * will use a built in data object to determine if a certain feature exists. Below is more information about that:
114+ *
115+ * <strong>Formatting: </strong> The name of the data object should be 'cryptolens_features' and it should be structured as a JSON array.
116+ *
117+ * For example, <pre>["f1", "f2"]</pre><p>means f1 and f2 are true. You can also have feature bundling, eg. <pre>["f1", ["f2",["voice","image"]]]</pre>
118+ * which means that f1 and f2 are true, as well as f2.voice and f2.image. You can set any depth, eg. you can have
119+ * <pre>["f1", ["f2",[["voice",["all"]], "image"]]]</pre> means f2.voice.all is true as well as f2.voice and f2.
120+ * The dots symbol is used to specify the "sub-features".
121+
122+ * Read more here: https://help.cryptolens.io/web-interface/feature-templates
123+ *
111124 * @param licenseKey a license key object.
112125 * @param feature The feature, eg 1 to 8.
113126 * @return If the feature is set to true, true is returned and false otherwise.
@@ -118,24 +131,81 @@ module.exports = class Helpers {
118131 return false ;
119132 }
120133
121- if ( feature === 1 && licenseKey . F1 )
122- return true ;
123- if ( feature === 2 && licenseKey . F2 )
124- return true ;
125- if ( feature === 3 && licenseKey . F3 )
126- return true ;
127- if ( feature === 4 && licenseKey . F4 )
128- return true ;
129- if ( feature === 5 && licenseKey . F5 )
130- return true ;
131- if ( feature === 6 && licenseKey . F6 )
132- return true ;
133- if ( feature === 7 && licenseKey . F7 )
134- return true ;
135- if ( feature === 8 && licenseKey . F8 )
134+ if ( ! isNaN ( feature ) ) {
135+ if ( feature === 1 && licenseKey . F1 )
136136 return true ;
137+ if ( feature === 2 && licenseKey . F2 )
138+ return true ;
139+ if ( feature === 3 && licenseKey . F3 )
140+ return true ;
141+ if ( feature === 4 && licenseKey . F4 )
142+ return true ;
143+ if ( feature === 5 && licenseKey . F5 )
144+ return true ;
145+ if ( feature === 6 && licenseKey . F6 )
146+ return true ;
147+ if ( feature === 7 && licenseKey . F7 )
148+ return true ;
149+ if ( feature === 8 && licenseKey . F8 )
150+ return true ;
137151
138- return false ;
152+ return false ;
153+ }
154+
155+ if ( ! licenseKey [ "DataObjects" ] ) {
156+ return false ;
157+ }
158+
159+ var features = null ;
160+
161+ for ( const dobj of licenseKey [ "DataObjects" ] ) {
162+ if ( dobj [ "Name" ] == 'cryptolens_features' ) {
163+ features = dobj [ "StringValue" ] ;
164+ break ;
165+ }
166+ }
167+
168+ if ( features == null ) {
169+ return false ;
170+ }
171+
172+ var array = JSON . parse ( features ) ;
173+
174+ var featurePath = feature . split ( "\." ) ;
175+
176+ var found = false ;
177+
178+ for ( let i = 0 ; i < featurePath . length ; i ++ ) {
179+ found = false ;
180+
181+ var index = - 1 ;
182+
183+ for ( let j = 0 ; j < array . length ; j ++ ) {
184+
185+ if ( ! Array . isArray ( array [ j ] ) && array [ j ] == featurePath [ i ] ) {
186+ found = true ;
187+ break ;
188+ } else if ( Array . isArray ( array [ j ] ) && array [ j ] [ 0 ] == featurePath [ i ] ) {
189+ found = true ;
190+ index = j ;
191+ break ;
192+ }
193+ }
194+ if ( ! found ) {
195+ return false ;
196+ }
197+
198+ if ( i + 1 < featurePath . length && index != - 1 ) {
199+ array = array [ index ] [ 1 ] ;
200+ }
201+
202+ }
203+
204+ if ( ! found ) {
205+ return false ;
206+ }
207+
208+ return true ;
139209 }
140210
141211
0 commit comments