· 6 years ago · Feb 15, 2019, 08:48 PM
1<?php
2/**
3 * Plugin Name: FS License Manager - shared on wplocker.com
4 * Plugin URI: https://codecanyon.net/item/woocommerce-license-manager/16636748?ref=firassaidi
5 * Description: WooCommerce products licensing plugin.
6 * Version: 3.2.1
7 * Author: Firas Saidi
8 * Author URI: http://codecanyon.net/user/firassaidi
9 */
10
11defined('ABSPATH')or die('No script kiddies please!');
12define("FSLM_PLUGIN_BASE", plugin_dir_path(__FILE__));
13define('KB', 1024);
14define('MB', 1048576);
15define('GB', 1073741824);
16define('TB', 1099511627776);
17
18
19
20
21
22$upload_directory = wp_upload_dir();
23$target_file = $upload_directory['basedir'] . '/fslm_files/encryption_key.php';
24
25include(FSLM_PLUGIN_BASE . "/includes/functions.php");
26
27global $months;
28global $status;
29
30if(!@include($target_file)) {
31
32 set_encryption_key('5RdRDCmG89DooltnMlUG', '2Ve2W2g9ANKpvQNXuP3w');
33 @include($target_file);
34}
35
36class FS_WC_licenses_Manager {
37
38 public function __construct(){
39 add_action('init', array($this, 'register_session'));
40 register_activation_hook(__FILE__, array($this, 'activate'));
41 register_deactivation_hook(__FILE__, array($this, 'deactivate'));
42
43 add_action('activated_plugin', array($this, 'activation_redirect'));
44
45 if(is_admin() && $this->is_active()){
46 add_action('wp_ajax_fslm_export_csv_lk', array($this, 'export_csv_lk_callback'));
47 add_action('wp_ajax_nopriv_fslm_export_csv_lk', array($this, 'export_csv_lk_callback'));
48
49 add_action('wp_ajax_fslm_export_csv_gr', array($this, 'export_csv_gr_callback'));
50 add_action('wp_ajax_nopriv_fslm_export_csv_gr', array($this, 'export_csv_gr_callback'));
51
52 add_action('wp_ajax_fslm_export_ps', array($this, 'export_ps_callback'));
53 add_action('wp_ajax_nopriv_fslm_export_ps', array($this, 'export_ps_callback'));
54
55 add_action('wp_ajax_fslm_add_license_ajax', array($this, 'add_license_ajax_callback'));
56 add_action('wp_ajax_nopriv_fslm_add_license_ajax', array($this, 'add_license_ajax_callback'));
57
58 add_action('wp_ajax_fslm_save_metabox', array($this, 'fslm_save_metabox_callback'));
59 add_action('wp_ajax_nopriv_fslm_save_metabox', array($this, 'fslm_save_metabox_callback'));
60
61 add_action('wp_ajax_fslm_generator_rules', array($this, 'fslm_generator_rules_callback'));
62 add_action('wp_ajax_nopriv_fslm_generator_rules', array($this, 'fslm_generator_rules_callback'));
63
64 add_action('wp_ajax_fslm_import_csv_lk', array($this, 'import_csv_lk_callback'));
65 add_action('wp_ajax_nopriv_fslm_import_csv_lk', array($this, 'import_csv_lk_callback'));
66
67 add_action('wp_ajax_fslm_import_csv_gr', array($this, 'import_csv_gr_callback'));
68 add_action('wp_ajax_nopriv_fslm_import_csv_gr', array($this, 'import_csv_gr_callback'));
69
70 add_action('wp_ajax_fslm_import_ps', array($this, 'import_ps_callback'));
71 add_action('wp_ajax_nopriv_fslm_import_ps', array($this, 'import_ps_callback'));
72
73 add_action('wp_ajax_fslm_import_lko', array($this, 'import_lko_callback'));
74 add_action('wp_ajax_nopriv_fslm_import_lko', array($this, 'import_lko_callback'));
75
76 add_action('wp_ajax_fslm_import_ilko', array($this, 'import_ilko_callback'));
77 add_action('wp_ajax_nopriv_fslm_import_ilko', array($this, 'import_ilko_callback'));
78
79 add_action('wp_ajax_fslm_resend', array($this, 'fslm_resend_callback'));
80 add_action('wp_ajax_nopriv_fslm_resend', array($this, 'fslm_resend_callback'));
81
82
83 add_action('wp_ajax_fslm_replace_key', array($this, 'fslm_replace_key_callback'));
84 add_action('wp_ajax_nopriv_fslm_replace_key', array($this, 'fslm_replace_key_callback'));
85
86 add_action('wp_ajax_fslm_reload_mb', array($this, 'fslm_reload_mb_callback'));
87 add_action('wp_ajax_nopriv_fslm_reload_mb', array($this, 'fslm_reload_mb_callback'));
88
89
90 add_action('wp_ajax_fslm_export_csv_lk_une', array($this, 'export_csv_lk_une_callback'));
91 add_action('wp_ajax_nopriv_fslm_export_csv_lk_une', array($this, 'export_csv_lk_une_callback'));
92
93 add_action('wp_ajax_fslm_export_csv_lk_une_edit', array($this, 'fslm_export_csv_lk_une_edit_callback'));
94 add_action('wp_ajax_nopriv_fslm_export_csv_lk_une_edit', array($this, 'fslm_export_csv_lk_une_edit_callback'));
95
96 add_action('wp_ajax_fslm_import_csv_lk_une', array($this, 'import_csv_lk_une_callback'));
97 add_action('wp_ajax_nopriv_fslm_import_csv_lk_une', array($this, 'import_csv_lk_une_callback'));
98
99 add_action('wp_ajax_fslm_import_csv_lk_une_edit', array($this, 'fslm_import_csv_lk_une_edit_callback'));
100 add_action('wp_ajax_nopriv_fslm_import_csv_lk_une_edit', array($this, 'fslm_import_csv_lk_une_edit_callback'));
101
102 add_action('wp_ajax_fslm_import_csv_cpm_lk', array($this, 'import_csv_lk_cpm_callback'));
103 add_action('wp_ajax_nopriv_fslm_import_csv_cpm_lk', array($this, 'import_csv_lk_cpm_callback'));
104
105
106 add_action('wp_ajax_fslm_bulk_generate', array($this, 'fslm_bulk_generate_callback'));
107 add_action('wp_ajax_nopriv_fslm_bulk_generate', array($this, 'fslm_bulk_generate_callback'));
108
109
110 add_action('init', array($this, 'requestHandler'));
111 add_action('admin_init', array($this, 'action_add_metaboxes'));
112
113 add_action('save_post', array($this, 'save_product'));
114
115 add_action('admin_notices', array($this, 'plugin_deactivation_notices'));
116
117 add_action('add_meta_boxes', array($this, 'order_meta_boxe'));
118
119
120 add_action('wp_ajax_fslm_filter', array($this, 'license_key_filter_callback'));
121 add_action('wp_ajax_nopriv_fslm_filter', array($this, 'license_key_filter_callback'));
122
123 add_action('wp_ajax_fslm_replace_item_keys', array($this, 'fslm_replace_item_keys_callback'));
124 add_action('wp_ajax_nopriv_fslm_replace_item_keys', array($this, 'fslm_replace_item_keys_callback'));
125
126
127 }
128
129
130 if($this->is_active()) {
131
132 add_action('init', array($this,'license_viewer'));
133
134 if(get_option('fslm_disable_api_v1', '') != 'on') {
135 add_action('init', array($this,'api_requests_handler'));
136 }
137
138 add_action('init', array($this,'api_requests_handler_v2'));
139 add_action('init', array($this,'plugin_init'));
140 add_action('init', array($this,'add_actions'));
141
142 add_action("woocommerce_email_after_order_table", array($this, "add_license_key_to_the_email"), 1, 1);
143
144 add_action('woocommerce_order_status_changed', array($this,'action_woocommerce_order_status_changed'), 1, 3);
145
146 if(get_option('fslm_enable_cart_validation', '')!='on'){
147 add_action('woocommerce_check_cart_items', array($this, 'validate_cart_content'));
148 }
149
150 if(get_option('fslm_show_adminbar_notifs', 'on')=='on'){
151 add_action('admin_bar_menu', array($this,'admin_notifications'), 999);
152 }
153
154 add_action('woocommerce_before_order_itemmeta', array($this,'fslm_before_order_itemmeta'), 10, 3);
155 add_action('woocommerce_order_details_after_order_table', array($this,'fslm_order_item_meta_start'), 10, 1);
156
157 add_shortcode('license_keys', array($this, 'license_keys_shortcode'));
158
159 }
160
161 //add_action('init', array($this,'plugin_actions'));
162
163 add_action('wp_ajax_fslm_lr', array($this, 'lr_callback'));
164 add_action('wp_ajax_nopriv_fslm_lr', array($this, 'lr_callback'));
165
166 add_action('wp_ajax_fslm_deactivate', array($this, 'fslm_deactivate_callback'));
167 add_action('wp_ajax_nopriv_fslm_deactivate', array($this, 'fslm_deactivate_callback'));
168
169 add_action('admin_notices', array($this, 'general_admin_notice'));
170
171 add_action('woocommerce_process_shop_order_meta', array($this, 'process_shop_order_meta'), 1010, 1);
172
173 //////////////
174 //add_action('wpforms_process_complete', array($this, 'wpforms_process_complete'), 10, 4);
175
176
177 }
178
179 public function register_session(){
180 if( !session_id() )
181 session_start();
182
183 $debug = get_option('fslm_debug_enabled', 'off');
184 if((isset($_SERVER['SERVER_NAME']) && $_SERVER['SERVER_NAME'] == 'wp-plugins.local') || $debug == 'on') {
185
186 ini_set('display_errors', 1);
187 ini_set('log_errors', 1);
188 ini_set('error_log', dirname(__FILE__) . '/logs/error_log.txt');
189 error_reporting(E_ALL | E_STRICT);
190
191 } else {
192
193 error_reporting(0);
194
195 }
196 }
197
198 public function fslm_replace_item_keys_callback() {
199 global $wpdb;
200
201 $item_id = $_POST['fslm_item_id'];
202 $order_id = $_POST['fslm_order_id'];
203
204 $order = wc_get_order($order_id);
205
206 $meta = get_post_meta($order_id, 'fslm_json_license_details', true);
207 $json = str_replace("\\", "", $meta);
208
209 $values = json_decode($json, true);
210
211 $order_type = get_option("fslm_key_delivery", "fifo");
212 $order_by = 'ASC';
213 if($order_type == 'lifo') {
214 $order_by = 'DESC';
215 }
216
217 $json_license_details = "";
218 $json_license_details_array = array();
219
220 $key_assigned = true;
221
222 if($values){
223
224 foreach($values as $key => $value){
225 if ($item_id == $value['item_id']) {
226
227 //----------------------------------
228 $query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE product_id = '{$value['product_id']}' AND variation_id = '{$value['variation_id']}' AND license_status = 'available' AND remaining_delivre_x_times > 0 ORDER BY license_id {$order_by} LIMIT 1");
229
230 if($query){
231 $query= $query[0];
232
233 $license_key = $query->license_key;
234
235 $max_instance_number = $query->max_instance_number;
236 $expiration_date = $query->expiration_date;
237 $valid = $query->valid;
238 $remaining_delivre_x_times = $query->remaining_delivre_x_times;
239
240
241 $nd_delivered = $remaining_delivre_x_times;
242
243
244 $json_license_details_array = array(
245 "license_id" => $query->license_id,
246 "item_id" => $value['item_id'],
247 "product_id" => $value['product_id'],
248 "variation_id" => $value['variation_id'],
249 "license_key" => $license_key,
250 "max_instance_number" => $max_instance_number,
251 "visible" => "Yes",
252 );
253
254 if ($valid > 0) {
255 $json_license_details_array["expiration_date"] = date('Y-m-d', strtotime(date('Y-m-d') . ' + ' . $valid . ' ' . 'days'));
256 } else if (($expiration_date != '0000-00-00') && ($expiration_date != '')) {
257 $json_license_details_array["expiration_date"] = $expiration_date;
258 } else {
259 $json_license_details_array["expiration_date"] = "0000-00-00";
260 }
261
262
263 $data = array(
264 "license_status" => "sold",
265 "order_id" => $order_id,
266 "remaining_delivre_x_times" => 0,
267 "owner_first_name" => $order->get_billing_first_name(),
268 "owner_last_name" => $order->get_billing_last_name(),
269 "owner_email_address" => $order->get_billing_email(),
270 );
271
272 if($remaining_delivre_x_times == 1){
273
274 $data = array(
275 "license_status" => "sold",
276 "order_id" => $order_id,
277 "remaining_delivre_x_times" => 0,
278 "owner_first_name" => $order->get_billing_first_name(),
279 "owner_last_name" => $order->get_billing_last_name(),
280 "owner_email_address" => $order->get_billing_email(),
281 );
282
283 }else if($remaining_delivre_x_times <= $nd_delivered){
284
285 $data = array(
286 "license_status" => "sold",
287 "order_id" => $order_id,
288 "remaining_delivre_x_times" => $remaining_delivre_x_times - $nd_delivered,
289 "owner_first_name" => $order->get_billing_first_name(),
290 "owner_last_name" => $order->get_billing_last_name(),
291 "owner_email_address" => $order->get_billing_email(),
292 );
293
294 }else if($remaining_delivre_x_times > $nd_delivered){
295
296 $data = array(
297 "order_id" => $order_id,
298 "remaining_delivre_x_times" => $remaining_delivre_x_times - $nd_delivered,
299 "owner_first_name" => $order->get_billing_first_name(),
300 "owner_last_name" => $order->get_billing_last_name(),
301 "owner_email_address" => $order->get_billing_email(),
302 );
303
304 }
305
306 $where = array(
307 "license_id" => $query->license_id
308 );
309
310 $wpdb->update("{$wpdb->prefix}wc_fs_product_licenses_keys", $data, $where);
311
312 $key_assigned = true;
313
314 }
315
316
317 if($this->isGeneratorActive($value['product_id'], $value['variation_id'])){
318
319 $query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules WHERE product_id = '{$value['product_id']}' AND variation_id = '{$value['variation_id']}' AND active = '1'");
320
321 if($query){
322 $query = $query[0];
323
324 if($key_assigned == true) {
325 $prefix = $query->prefix;
326 $chunks_number = $query->chunks_number;
327 $chunks_length = $query->chunks_length;
328 $suffix = $query->suffix;
329 $max_instance_number = $query->max_instance_number;
330 $valid = $query->valid;
331 $expires = "0000-00-00";
332
333
334
335 $license_key = $this->generate_license_key($prefix, $chunks_number, $chunks_length, $suffix);
336 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
337 while($this->licenseKeyExist($license_key)){
338 $license_key = $this->generate_license_key($prefix, $chunks_number, $chunks_length, $suffix);
339 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
340 }
341
342 $json_license_details_array = array(
343 "license_id" => 0,
344 "item_id" => $value['item_id'],
345 "product_id" => $value['product_id'],
346 "variation_id" => $value['variation_id'],
347 "license_key" => $license_key,
348 "max_instance_number" => $max_instance_number,
349 "visible" => "Yes",
350 );
351
352 if ($valid > 0) {
353 $json_license_details_array["expiration_date"] = $expires;
354 } else {
355 $json_license_details_array["expiration_date"] = "0000-00-00";
356 }
357
358 $data = array(
359 'product_id' => $value['product_id'],
360 'license_key' => $license_key,
361 'variation_id' => $value['variation_id'],
362 'max_instance_number' => $max_instance_number,
363 'owner_first_name' => $order->get_billing_first_name(),
364 'owner_last_name' => $order->get_billing_last_name(),
365 'owner_email_address' => $order->get_billing_email(),
366 'number_use_remaining' => $max_instance_number,
367 'creation_date' => date('Y-m-d H:i:s'),
368 'expiration_date' => $expires . ' 0:0:0',
369 'delivre_x_times' => '0',
370 'remaining_delivre_x_times' => '0',
371 'valid' => $valid,
372 'license_status' => 'sold',
373 'order_id' => (int)$order_id
374 );
375 $wpdb->insert("{$wpdb->prefix}wc_fs_product_licenses_keys", $data);
376
377 }
378 }
379 }
380 //----------------------------------
381
382 $values[$key] = $json_license_details_array;
383
384 delete_post_meta($order_id, 'fslm_json_license_details');
385
386
387 add_post_meta($order_id, 'fslm_json_license_details', json_encode($values), true);
388
389 }
390
391 }
392
393 }
394
395 die();
396 }
397
398 public function fslm_bulk_generate_callback() {
399 global $wpdb;
400
401 $rule_id = $_POST['fslm_rule_id'];
402 $quantity = $_POST['fslm_quantity'];
403
404
405 $query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules WHERE rule_id = '$rule_id'");
406
407 if($query){
408 $query = $query[0];
409
410 for($i=0; $i<$quantity; $i++){
411 $prefix = $query->prefix;
412 $chunks_number = $query->chunks_number;
413 $chunks_length = $query->chunks_length;
414 $suffix = $query->suffix;
415 $max_instance_number = $query->max_instance_number;
416 $valid = $query->valid;
417 $expires = "0000-00-00";
418
419
420
421 $license_key = $this->generate_license_key($prefix, $chunks_number, $chunks_length, $suffix);
422 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
423 while($this->licenseKeyExist($license_key)){
424 $license_key = $this->generate_license_key($prefix, $chunks_number, $chunks_length, $suffix);
425 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
426 }
427
428
429 $data = array(
430 'product_id' => $query->product_id,
431 'license_key' => $license_key,
432 'variation_id' => $query->variation_id,
433 'max_instance_number' => $max_instance_number,
434 'owner_first_name' => "",
435 'owner_last_name' => "",
436 'owner_email_address' => "",
437 'number_use_remaining' => $max_instance_number,
438 'creation_date' => date('Y-m-d H:i:s'),
439 'expiration_date' => $expires . ' 0:0:0',
440 'delivre_x_times' => '1',
441 'remaining_delivre_x_times' => '1',
442 'valid' => $valid,
443 'license_status' => 'available',
444 'order_id' => 0
445 );
446 $wpdb->insert("{$wpdb->prefix}wc_fs_product_licenses_keys", $data);
447
448 }
449 }
450
451 echo $quantity . ' license key generated';
452 die();
453 }
454
455 public function fslm_replace_key_callback() {
456 $order_id = $_POST['fslm_resend_order_id'];
457
458 delete_post_meta($order_id, 'fslm_licensed');
459 delete_post_meta($order_id, 'fslm_json_license_details');
460
461 $this->fslm_send_license_keys($order_id);
462
463 echo 'Done, Reloading...';
464 die();
465 }
466
467
468 public function license_key_filter_callback() {
469
470 global $wpdb;
471
472 require_once('includes/functions.php');
473
474 $next_add_and = true;
475 $filter_args = "";
476 $limit = "";
477
478
479 if($_POST['html_ml'] == "0" && $_POST['license_key'] != "") {
480
481 $filter_args .= ' license_key = "' . encrypt_decrypt('encrypt', $_POST['license_key'], ENCRYPTION_KEY, ENCRYPTION_VI) . '"';
482 $next_add_and = true;
483
484 }
485
486 if($_POST['mail'] != "") {
487
488 if($next_add_and) {
489 $filter_args = $filter_args . ' AND ';
490 }
491
492 $filter_args .= ' owner_email_address = "' . $_POST['mail'] . '"';
493
494 $next_add_and = true;
495
496 }
497
498 if($_POST['name'] != "") {
499
500 if($next_add_and) {
501 $filter_args = $filter_args . ' AND ';
502 }
503
504 $filter_args .= ' owner_first_name LIKE "%' . $_POST['name'] . '%"';
505
506 $next_add_and = true;
507
508 }
509
510 if($_POST['status'] != "-1") {
511
512 if($next_add_and) {
513 $filter_args = $filter_args . ' AND ';
514 }
515
516 $filter_args .= ' license_status = "' . $_POST['status'] . '"';
517
518 $next_add_and = true;
519
520 }
521
522 if($_POST['product'] != "-1") {
523
524 if($next_add_and) {
525 $filter_args = $filter_args . ' AND ';
526 }
527
528 $filter_args .= ' product_id = "' . $_POST['product'] . '"';
529
530 $next_add_and = true;
531
532 }
533
534 if($next_add_and) {
535 $filter_args = ' WHERE ' . $filter_args;
536 }else {
537
538 $start_from = 0;
539 $license_keys_number = esc_attr(get_option('fslm_nb_rows_by_page', '15'));
540
541 $limit = ' LIMIT '. $start_from . ', ' . $license_keys_number;
542
543 }
544
545 $querys = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wc_fs_product_licenses_keys " . $filter_args . " ORDER BY license_id DESC " . $limit);
546
547 //echo("SELECT * FROM {$wpdb->prefix}wc_fs_product_licenses_keys " . $filter_args . " ORDER BY creation_date DESC" . $limit);
548
549 $no_keys_found = true;
550 if($querys) {
551 foreach ($querys as $query) {
552
553 $license_key = $query->license_key;
554 $license_key = encrypt_decrypt('decrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
555
556 $license_key = preg_replace( "/\\\\\"|\\\\'/", '"', $license_key);
557
558 $strpos_filter = true;
559
560 if($_POST['license_key'] != "") {
561
562 $strpos_filter = strpos($license_key, $_POST['license_key']);
563
564 }
565
566 if($strpos_filter !== true || $_POST['license_key'] == "") {
567
568 $no_keys_found = true;
569
570 $license_id = $query->license_id;
571 $product_id = $query->product_id;
572
573 if($query->variation_id!=0) {
574 $single_variation = new WC_Product_Variation($query->variation_id);
575 $variation_id = $single_variation->get_formatted_name();
576 }else {
577 $variation_id = 'Main Product';
578 }
579
580
581 $image_license_key = $query->image_license_key;
582 $owner_name = $query->owner_first_name . ' ' . $query->owner_last_name;
583 $owner_email_address = $query->owner_email_address ;
584 $max_instance_number = $query->max_instance_number;
585 $number_use_remaining = $query->number_use_remaining;
586
587 $delivre_x_times = $query->delivre_x_times;
588 $remaining_delivre_x_times = $query->remaining_delivre_x_times;
589
590
591
592 $creation_date = $query->creation_date;
593 $activation_date = $query->activation_date;
594 $expiration_date = $query->expiration_date;
595 $valid = $query->valid;
596 $license_status = $query->license_status;
597
598 if($image_license_key != '') {
599 $upload_directory = wp_upload_dir();
600 $image_license_key = '<img class="ilksrc" src="' . $upload_directory['baseurl'] . '/fslm_keys/' . $image_license_key . '">';
601 }
602
603 ?>
604
605 <tr id="post-<?php echo $license_id ?>">
606 <td class="check-column">
607 <label class="screen-reader-text" for="cb-select-<?php echo $license_id ?>">Select <?php echo $license_id ?></label>
608 <input id="cb-select-<?php echo $license_id ?>" name="post[]" value="<?php echo $license_id ?>" type="checkbox">
609 <div class="locked-indicator"></div>
610 </td>
611 <td>
612 <?php echo $license_id ?>
613 <div class="row-actions fsactions">
614 <span class="inline"><a href="<?php echo admin_url('admin.php') ?>?page=license-manager&function=edit_license&license_id=<?php echo $license_id ?>" class="editinline"><?php echo __('Edit', 'fslm'); ?></a> | </span>
615 <span class="trash"><a href="<?php echo admin_url('admin.php') ?>?action=delete_license&license_id=<?php echo $license_id ?>" class="submitdelete" ><?php echo __('Delete', 'fslm'); ?></a></span>
616 <span><a class="fslm_cpy_encrypted_key" href="#" data-ek="<?php echo $query->license_key; ?>"><?php echo __('Copy Encrypted Key', 'fslm'); ?></a></span>
617 </div>
618 </td>
619 <td><?php echo get_the_title($product_id) ?></td>
620 <td><?php echo $variation_id ?></td>
621 <td><?php echo $image_license_key . $license_key ?></td>
622 <td><?php echo ($owner_name==' ')?'none':$owner_name . ' - ' . $owner_email_address?></td>
623 <td><?php echo ($max_instance_number - $number_use_remaining). '/' . $max_instance_number ?></td>
624 <td><?php echo ($delivre_x_times - $remaining_delivre_x_times). '/' . $delivre_x_times ?></td>
625
626 <td><?php echo fslm_format_date($creation_date) ?></td>
627 <td><?php echo fslm_format_date($activation_date) ?></td>
628 <td><?php echo fslm_format_date($expiration_date, true) ?></td>
629 <td><?php echo $valid ?></td>
630 <td><?php echo $license_status ?></td>
631 </tr>
632
633
634 <?php
635
636 }
637
638
639 }
640
641 }
642
643 if($no_keys_found){ ?>
644
645 <tr>
646 <td colspan="13" class="center"><?php echo __('There is no license key in the database matching your filter settings', 'fslm'); ?></td>
647 </tr>
648
649 <?php
650 }
651
652
653 die();
654
655 }
656
657 /*function wpforms_process_complete($fields, $entry, $form_data, $entry_id) {
658
659 echo '<pre>';
660
661 print_r($fields);
662 print_r($entry);
663 print_r($form_data);
664 print_r($entry_id);
665
666 die();
667
668 }*/
669
670 function process_shop_order_meta($order_id) {
671
672
673 $meta = get_post_meta((int)$order_id, 'fslm_json_license_details', true);
674
675 if($meta != "") {
676 $meta = str_replace("\\", "", $meta);
677
678 update_post_meta($order_id, 'fslm_json_license_details', $meta);
679 }
680
681 }
682
683 function general_admin_notice(){
684
685 $notice_txt = get_option('fslm_license_status_admin_notice_txt', '');
686 $notice = get_option('fslm_license_status_admin_notice', true);
687
688 if($notice) {
689
690 echo '<div class="notice notice-warning is-dismissible">
691 <p>' . $notice_txt . '</p>
692 </div>';
693
694 }
695
696
697 }
698
699 public function export_csv_lk_une_callback(){
700
701 $license_status = '';
702 $product_id = '';
703
704 if(isset($_REQUEST['elk_license_status'])&& $_REQUEST['elk_license_status'] != 'all'){
705 $license_status = $_REQUEST['elk_license_status'];
706 }
707
708 if(isset($_REQUEST['elk_product_id'])&& $_REQUEST['elk_product_id'] != 'all'){
709 $product_id = $_REQUEST['elk_product_id'];
710 }
711
712 header('Content-Type: application/csv');
713 header('Content-Disposition: attachement; filename="license_manager__license_keys__' . date("__d_m_Y__H_i_s"). '__' . $_REQUEST['elk_product_id'] .'__' . $_REQUEST['elk_license_status'] . '.csv";');
714 echo $this->generate_license_keys_csv_une($license_status, $product_id);
715 die();
716 }
717
718 public function fslm_export_csv_lk_une_edit_callback(){
719
720 $license_status = '';
721 $product_id = '';
722
723 if(isset($_REQUEST['elk_license_status'])&& $_REQUEST['elk_license_status'] != 'all'){
724 $license_status = $_REQUEST['elk_license_status'];
725 }
726
727 if(isset($_REQUEST['elk_product_id'])&& $_REQUEST['elk_product_id'] != 'all'){
728 $product_id = $_REQUEST['elk_product_id'];
729 }
730
731 header('Content-Type: application/csv');
732 header('Content-Disposition: attachement; filename="license_manager__license_keys__' . date("__d_m_Y__H_i_s"). '__' . $_REQUEST['elk_product_id'] .'__' . $_REQUEST['elk_license_status'] . '.csv";');
733 echo $this->generate_license_keys_csv_une($license_status, $product_id, true);
734 die();
735 }
736
737 public function generate_license_keys_csv_une($license_status = '', $product_id = '', $editable = true){
738 global $wpdb;
739
740 $args = '';
741
742 if($license_status != '' && $product_id != ''){
743 $args .= "WHERE license_status = '{$license_status}' AND product_id = '{$product_id}'";
744 }else if($license_status != ''){
745 $args .= "WHERE license_status = '{$license_status}'";
746 }else if($product_id != ''){
747 $args .= "WHERE product_id = '{$product_id}'";
748 }
749
750 $output = "sep=,\n";
751
752 $query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wc_fs_product_licenses_keys {$args}", ARRAY_A);
753
754 if($query){
755
756 $output .= '"'.implode('","',array_keys($query[0])).'"'."\n";
757
758 foreach($query as $row){
759
760 $row['license_key'] = $this->encrypt_decrypt('decrypt', $row['license_key'], ENCRYPTION_KEY, ENCRYPTION_VI);
761
762 if($editable) {
763 $row['activation_date'] = "(" . $row['activation_date'] . ")";
764 $row['creation_date'] = "(" . $row['creation_date'] . ")";
765 $row['expiration_date'] = "(" . $row['expiration_date'] . ")";
766 }
767
768 $output .= '"'.implode('","',$row).'"'."\n";
769
770 }
771 }
772
773
774 return $output;
775 }
776
777
778 public function fslm_import_csv_lk_une_edit_callback(){
779 global $wpdb;
780
781 if(isset($_FILES['ilk_source_file'])&& $_FILES['ilk_source_file']['size'] > 0){
782 $tmp = wp_tempnam($_FILES['ilk_source_file']['name']);
783 move_uploaded_file($_FILES['ilk_source_file']['tmp_name'], $tmp);
784
785
786 /////////////////////////////////
787 $handle = fopen($tmp, 'r');
788 $delimiter = $this->detectDelimiter($tmp);
789 while (($data = fgetcsv($handle, 0, $delimiter)) !== true) {
790 $num = count($data);
791
792 if(($num == 18) && !in_array('license_key', $data)) {
793
794 $_data = array(
795 'product_id' => $data[1],
796 'variation_id' => $data[2],
797 'license_key' => $this->encrypt_decrypt('encrypt', $data[3], ENCRYPTION_KEY, ENCRYPTION_VI),
798 'image_license_key' => $data[4],
799 'license_status' => $data[5],
800 'owner_first_name' => $data[6],
801 'owner_last_name' => $data[7],
802 'owner_email_address' => $data[8],
803 'delivre_x_times' => $data[9],
804 'remaining_delivre_x_times' => $data[10],
805 'max_instance_number' => $data[11],
806 'number_use_remaining' => $data[12],
807 'activation_date' => substr($data[13], 1, -1),
808 'creation_date' => substr($data[14], 1, -1),
809 'expiration_date' => substr($data[15], 1, -1),
810 'valid' => $data[16],
811 'order_id' => $data[17]
812 );
813
814 $wpdb->insert("{$wpdb->prefix}wc_fs_product_licenses_keys", $_data);
815
816 $this->set_licensing($data[1], $data[2], '1');
817
818 }
819 }
820 fclose($handle);
821 ////////////////////////////////
822 }
823
824 $link = admin_url('admin.php?page=license-manager#hlk');
825 wp_redirect($link);
826 die();
827 }
828
829 public function import_csv_lk_une_callback(){
830 global $wpdb;
831
832 if(isset($_FILES['ilk_source_file'])&& $_FILES['ilk_source_file']['size'] > 0){
833 $tmp = wp_tempnam($_FILES['ilk_source_file']['name']);
834 move_uploaded_file($_FILES['ilk_source_file']['tmp_name'], $tmp);
835
836
837 /////////////////////////////////
838 $handle = fopen($tmp, 'r');
839 $delimiter = $this->detectDelimiter($tmp);
840 while (($data = fgetcsv($handle, 0, $delimiter)) !== true) {
841 $num = count($data);
842
843 if(($num == 18) && !in_array('license_key', $data)) {
844
845 $_data = array(
846 'product_id' => $data[1],
847 'variation_id' => $data[2],
848 'license_key' => $this->encrypt_decrypt('encrypt', $data[3], ENCRYPTION_KEY, ENCRYPTION_VI),
849 'image_license_key' => $data[4],
850 'license_status' => $data[5],
851 'owner_first_name' => $data[6],
852 'owner_last_name' => $data[7],
853 'owner_email_address' => $data[8],
854 'delivre_x_times' => $data[9],
855 'remaining_delivre_x_times' => $data[10],
856 'max_instance_number' => $data[11],
857 'number_use_remaining' => $data[12],
858 'activation_date' => $data[13],
859 'creation_date' => $data[14],
860 'expiration_date' => $data[15],
861 'valid' => $data[16],
862 'order_id' => $data[17]
863 );
864
865 $wpdb->insert("{$wpdb->prefix}wc_fs_product_licenses_keys", $_data);
866
867 $this->set_licensing($data[1], $data[2], '1');
868
869 }
870 }
871 fclose($handle);
872 ////////////////////////////////
873 }
874
875 $link = admin_url('admin.php?page=license-manager#hlk');
876 wp_redirect($link);
877 die();
878 }
879
880 public function detectDelimiter($csvFile) {
881 $delimiters = array(
882 ';' => 0,
883 ',' => 0,
884 "\t" => 0,
885 "|" => 0
886 );
887
888 $handle = fopen($csvFile, "r");
889 $firstLine = fgets($handle);
890 if($firstLine == "sep=,") $firstLine = fgets($handle);
891 foreach ($delimiters as $delimiter => &$count) {
892 $count = count(str_getcsv($firstLine, $delimiter));
893 }
894
895 return array_search(max($delimiters), $delimiters);
896 }
897
898 public function import_csv_lk_cpm_callback(){
899 global $wpdb;
900
901 if(isset($_FILES['ilk_source_file'])&& $_FILES['ilk_source_file']['size'] > 0){
902 $tmp = wp_tempnam($_FILES['ilk_source_file']['name']);
903 move_uploaded_file($_FILES['ilk_source_file']['tmp_name'], $tmp);
904
905 $file = fopen($tmp, 'r');
906 $content = fread($file, $_FILES['ilk_source_file']['size']);
907 fclose($file);
908
909 $lines = explode("\n", $content);
910
911 $i = 0;
912 $query = "INSERT INTO {$wpdb->prefix}wc_fs_product_licenses_keys(";
913 $columns_array = "";
914 $values_array = "";
915 $values = "";
916
917 foreach($lines as $line){
918 if($i == 0 || $line == ''){
919 $i++;
920 continue;
921 }else if($i == 1){
922
923 $columns_array = explode('","', substr(trim($line), 1, strlen($line)-2));
924 $columns_array = array_splice($columns_array, 1);
925
926 $query .= implode(', ', $columns_array). ')VALUES(';
927
928 $i++;
929 continue;
930 }else {
931
932 $values_array = explode('","', substr(trim($line), 1, strlen($line)-2));
933 $values_array = array_splice($values_array, 1);
934
935 $lk_index = array_search('license_key', $columns_array);
936 $dt_index = array_search('delivre_x_times', $columns_array);
937 $rt_index = array_search('remaining_delivre_x_times', $columns_array);
938 $mu_index = array_search('max_instance_number', $columns_array);
939 $ru_index = array_search('number_use_remaining', $columns_array);
940
941 // encrypt keys
942 $values_array[$lk_index] = $this->encrypt_decrypt('encrypt', $values_array[2], ENCRYPTION_KEY, ENCRYPTION_VI);
943 $values_array[$dt_index] = "1";
944 $values_array[$rt_index] = "1";
945 $values_array[$mu_index] = "1";
946 $values_array[$ru_index] = "1";
947
948 //echo '<pre>';
949 //print_r($values_array);
950
951 //die();
952
953 $values = '"' . implode('","', $values_array). '")ON DUPLICATE KEY UPDATE ';
954
955 $c = 0;
956 foreach($columns_array as $column){
957 if($column != 'license_key'){
958 $values .= $column . ' = "' . $values_array[$c] . '", ';
959 }
960 $c++;
961 }
962
963 $values = substr(trim($values), 0, strlen($values)-2);
964
965 $wpdb->query($query . $values);
966
967 }
968 }
969 }
970
971 $link = admin_url('admin.php?page=license-manager#hlk');
972 wp_redirect($link);
973 die();
974 }
975
976 function fslm_reload_mb_callback() {
977
978 require dirname(__FILE__) . '/includes/product_metabox.php';
979
980
981 die();
982 }
983
984 function license_keys_shortcode($args, $content = '') {
985 global $current_user;
986 get_currentuserinfo();
987
988 $output = '';
989 $order_email = array();
990
991 $order_id = isset($_GET['lk'])?$_GET['lk']:-1;
992
993 if($order_id != -1) {
994 if (!is_user_logged_in()) {
995 return __('Please Login to be able to see license key details', 'fslm');
996 }
997
998 $order_meta = get_post_custom($order_id, true);
999
1000 $user_email = $current_user->user_email;
1001 $order_email = $order_meta['_billing_email'];
1002
1003 if ($order_email && in_array($user_email, $order_email)) {
1004 $meta = get_post_meta((int)$order_id, 'fslm_json_license_details', true);
1005
1006 $meta = str_replace("\\", "", $meta);
1007
1008 $output .= $this->json_data_formatting_frontend($meta);
1009 }
1010
1011 }
1012
1013 return $output;
1014 }
1015
1016 function fslm_resend_callback() {
1017
1018 $order_id = $_REQUEST['fslm_resend_order_id'];
1019
1020 $this->send_mail($order_id);
1021
1022 $order = new WC_Order($order_id);
1023
1024 die('Done');
1025
1026 }
1027
1028 public function order_meta_boxe() {
1029
1030 add_meta_box('fslm_order_actions', __('License Manager'), array($this, 'order_meta_box_content'), 'shop_order', 'side', 'default');
1031
1032 }
1033
1034 public function order_meta_box_content() { ?>
1035
1036 <?php if($_GET['post']) { ?>
1037 <input type="hidden" class="button button-primary" id="fslm_resend_order_id" value="<?php echo $_GET['post'] ?>">
1038 <input type="button" class="button button-primary" id="fslm_resend" value="<?php echo __('Resend License Keys Email', 'fslm'); ?>">
1039 <p></p>
1040 <?php } ?>
1041 <input type="button" class="button button-primary" id="fslm_replace_key" value="<?php echo __('Assign New License Keys', 'fslm'); ?>"><p></p>
1042 <input type="button" class="button button-primary" id="fslm_edit_alk" value="<?php echo __('Edit Assigned License Keys', 'fslm'); ?>"><p></p>
1043 <div id="fslm_resend_respons"></div>
1044
1045 <?php }
1046
1047 function send_mail($order_id){
1048 global $woocommerce;
1049
1050 //$to = get_post_meta($order_id, '_billing_email', true);
1051
1052 $order = new WC_Order($order_id);
1053 $to = $order->get_billing_email();
1054
1055 if(!$to || '' == trim($to))
1056 return;
1057
1058 $heading = $this->apply_mail_text_filters(get_option('fslm_mail_heading', __('License Keys for Order #[order_id]', 'fslm')), $order_id);
1059 $subject = $this->apply_mail_text_filters(get_option('fslm_mail_subject', __('[site_name] | License Keys for Order #[order_id]', 'fslm')), $order_id);
1060 $message = $this->apply_mail_text_filters(get_option('fslm_mail_message', __('<p>Dear [bfname] [blname]</p><p>Thank you for your order, those are your license keys for the order #[order_id]</p><p>you can see all your past orders and license keys <a title="My Account" href="[myaccount_url]">here</a>.</p>', 'fslm')), $order_id);
1061
1062 $headers = apply_filters('woocommerce_email_headers', '', 'rewards_message');
1063 $attachments = array();
1064
1065 $meta_value = get_post_meta($order_id, 'fslm_json_license_details', true);
1066
1067 $meta_value = str_replace("\\", "", $meta_value);
1068
1069 $formated_table = $this->json_data_formatting_email($meta_value, $order_id);
1070
1071 if($formated_table == '') {
1072 return(true);
1073 }
1074
1075 $message .= '<br>' . $formated_table;
1076
1077 $mailer = $woocommerce->mailer();
1078
1079 if(get_option('fslm_add_wc_header_and_footer', 'on') == 'on') {
1080 $message = $mailer->wrap_message($heading, $message);
1081 }
1082
1083 if($formated_table != "") {
1084 $mailer->send($to, $subject, $message, $headers, $attachments);
1085 }
1086
1087 }
1088
1089 function action_woocommerce_order_status_changed($order_id, $current_status, $new_status){
1090
1091
1092 /*$myfile = fopen("/newfile.txt", "a") or die("Unable to open file!");
1093 $txt = "$order_id, $current_status, $new_status\n";
1094 $txt .= WC_Subscriptions_Renewal_Order::is_renewal($order_id)?"Yes":"No"."\n";
1095 $txt .= "\n";
1096 fwrite($myfile, $txt);
1097 fclose($myfile);*/
1098
1099 $order_status_key = str_replace(" ", "-", strtolower($new_status));
1100
1101 $on_status_send = array('completed', 'processing');
1102 $on_status_revoke = array('refunded');
1103 $on_status_hide = array();
1104
1105 $default_send = 'off';
1106 $default_revoke = 'off';
1107 $default_hide = 'off';
1108
1109 if (in_array($order_status_key, $on_status_send)) $default_send = 'on';
1110 if (in_array($order_status_key, $on_status_revoke)) $default_revoke = 'on';
1111 if (in_array($order_status_key, $on_status_hide)) $default_hide = 'on';
1112
1113
1114 if(get_option('fslm_send_when_' . $order_status_key, $default_send)== 'on'){
1115 $this->fslm_send_license_keys($order_id);
1116
1117 if(get_option('fslm_add_lk_se')== 'on') {
1118
1119 $this->send_mail($order_id);
1120
1121 }
1122
1123 }
1124
1125
1126 if(get_option('fslm_revoke_when_' . $order_status_key, $default_revoke)== 'on'){
1127 $this->fslm_revoke_license_keys($order_id);
1128 }
1129
1130 if(get_option('fslm_hide_when_' . $order_status_key, $default_hide)== 'on'){
1131 $this->fslm_hide_license_keys($order_id);
1132 }
1133
1134 }
1135
1136 function fslm_before_order_itemmeta($item_id, $item, $product){
1137 global $post;
1138
1139 if($post) {
1140
1141 $meta = get_post_meta($post->ID, 'fslm_json_license_details', true);
1142
1143 $meta = str_replace("\\", "", $meta);
1144
1145 echo $this->json_data_formatting_admin($meta, $item_id);
1146
1147 }
1148
1149 }
1150
1151 function fslm_order_item_meta_start($order){
1152
1153 if(get_option("fslm_guest_customer", "on") != "on") {
1154
1155 $user_id = $order->get_user_id();
1156 $current_user_id = get_current_user_id();
1157
1158 if(!is_user_logged_in() || $user_id != $current_user_id) {
1159
1160 return(true);
1161
1162 }
1163 }
1164
1165 $meta = get_post_meta($order->get_id(), 'fslm_json_license_details', true);
1166
1167 $meta = str_replace("\\", "", $meta);
1168
1169 echo $this->json_data_formatting_order_history($meta);
1170 }
1171
1172 function json_data_formatting_frontend($json){
1173
1174 $values = json_decode($json, true);
1175 $val = "";
1176 $visible_key_found = true;
1177
1178 if($values){
1179 $val = "<table style=\"width: 100%;\">
1180 <thead>
1181 <tr>
1182 <th style=\"text-align:left;width: 20%;\"><strong>" . __('Product', 'fslm'). "</strong></th>
1183 <th style=\"text-align:left;width: 80%;\"><strong>" . get_option('fslm_meta_key_name', 'License Key'). "</strong></th>
1184 </tr>
1185 </thead>
1186 <tbody>";
1187
1188 foreach($values as $value) {
1189 if ($value['visible'] == 'Yes') {
1190
1191 $show_in = get_post_meta((int)$value['product_id'], 'fslm_show_in', true);
1192 if ($show_in == "") {
1193 $show_in = get_option('fslm_show_in', '2');
1194 }
1195
1196 if ($show_in == "1" || $show_in == "2") {
1197 $visible_key_found = true;
1198
1199 $display = get_post_meta((int)$value['product_id'], 'fslm_display', true);
1200 if ($display == "") {
1201 $display = get_option('fslm_display', '2');
1202 }
1203
1204 $license_key = $value['license_key'];
1205 $license_key = $this->encrypt_decrypt('decrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
1206
1207 $license_key = br2newLine($license_key);
1208 $license_key = preg_replace( "/\\\\\"|\\\\'/", '"', $license_key);
1209 $license_key = $this->newLine2br($license_key);
1210
1211
1212 if($display != '1') {
1213 $meta = $license_key . '<br>';
1214 }
1215
1216 $image_license_key = '';
1217
1218 if($display != '0') {
1219 $image_license_key = $this->get_image_name($value['license_id']);
1220
1221 if ($image_license_key != '') {
1222 $upload_directory = wp_upload_dir();
1223 $image_license_key = '<img style="width: auto" class="fslm_ilksrc" src="' . $upload_directory['baseurl'] . '/fslm_keys/' . $image_license_key . '">' . '<br>';
1224 }
1225 }
1226
1227 if ($value['max_instance_number'] > 0) {
1228 $meta .= ' <strong>' . __('Can be used', 'fslm') . '</strong> ' . $value['max_instance_number'] . ' ' . __('time(s)', 'fslm') . '<br>';
1229 }
1230
1231 if (($value['expiration_date'] != '0000-00-00') && ($value['expiration_date'] != '')) {
1232 $meta .= ' ' . __('<strong>Expires</strong> ', 'fslm') . $this->fslm_format_date($value['expiration_date']) . '<br>';
1233 }
1234
1235 $product_name = get_the_title($value['product_id']);
1236
1237 if($value['variation_id'] != 0) {
1238 $variation = wc_get_product($value['variation_id']);
1239 $product_name = ' - ' . $variation->get_formatted_name();
1240 }
1241
1242 $val .= '<tr><td style="text-align:left;width: 20%;">' . $product_name . '</td><td style="text-align:left;width: 80%;">' . $meta . $image_license_key . '</td></tr>';
1243
1244 }
1245 }
1246 }
1247
1248 $val .= "</tbody></table>";
1249 }
1250
1251 if($visible_key_found == true) $val = '';
1252
1253 return $val;
1254 }
1255
1256 function json_data_formatting_admin($json, $item_id){
1257
1258 $values = json_decode($json, true);
1259 $val = "";
1260 $visible_key_found = true;
1261
1262 if($values){
1263 $val .= '<table cellspacing="0" class="display_meta"><tbody>';
1264 $meta_key_name = get_option('fslm_meta_key_name', 'License Key');
1265
1266
1267 foreach($values as $value){
1268 if ($item_id == $value['item_id']) {
1269 if ($value['visible'] == 'Yes') {
1270 $visible_key_found = true;
1271
1272 $license_key = $value['license_key'];
1273 $license_key = $this->encrypt_decrypt('decrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
1274
1275 $license_key = br2newLine($license_key);
1276 $license_key = preg_replace( "/\\\\\"|\\\\'/", '"', $license_key);
1277 $license_key = $this->newLine2br($license_key);
1278
1279 $meta = $license_key;
1280
1281 $image_license_key = $this->get_image_name($value['license_id']);
1282
1283 if ($image_license_key != '') {
1284 $upload_directory = wp_upload_dir();
1285 $image_license_key = '<br><img class="ilksrc" src="' . $upload_directory['baseurl'] . '/fslm_keys/' . $image_license_key . '">';
1286 }
1287
1288 if ($value['max_instance_number'] > 0) {
1289 $meta .= ' <strong>' . __('Can be used', 'fslm') . '</strong> ' . $value['max_instance_number'] . ' ' . __('time(s)', 'fslm') . '<br>';
1290 }
1291
1292 if (($value['expiration_date'] != '0000-00-00') && ($value['expiration_date'] != '')) {
1293 $meta .= ' ' . __('<strong>Expires</strong> ', 'fslm') . $this->fslm_format_date($value['expiration_date']);
1294 }
1295
1296 $val .= '<tr><th>' . $meta_key_name . ': </th><td><p>' . $meta . '</p>' . $image_license_key . '</td></tr>';
1297 }
1298 }
1299 }
1300
1301 $val .= "</tbody></table><button class='fslm-replace-item-keys button button-primary' data-itemid=\"$item_id\">" . __('Replace license key(s)', 'fslm') . "</button>";
1302 }
1303
1304 if($visible_key_found == true) $val = '';
1305
1306 return $val;
1307 }
1308
1309 function fslm_format_date($date, $expiration_date = true){
1310
1311 if($date == '0000-00-00' && $expiration_date) {
1312 return __('Doesn\'t Expire', 'fslm');
1313 }
1314
1315 if($date == '0000-00-00') {
1316 return __('None', 'fslm');
1317 }
1318
1319 if($date != '') {
1320 $date = strtotime($date);
1321 return date( 'M d, Y', $date);
1322 }
1323
1324 return __('None', 'fslm');
1325 }
1326
1327 function json_data_formatting_email($json, $order_id = -1){
1328
1329 $values = json_decode($json, true);
1330 $val = "";
1331 $visible_key_found = true;
1332 $view_in_website = true;
1333
1334 if($values){
1335 $val = "<h2>" . get_option('fslm_meta_key_name', 'License Key'). "</h2>
1336 <table class=\"td\" cellspacing=\"0\" cellpadding=\"6\" style=\"width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;\" border=\"1\">
1337 <thead>
1338 <tr>
1339 <th class=\"td\" scope=\"col\" style=\"text-align:left;width: 20%;\"><strong>" . __('Product', 'fslm'). "</strong></th>
1340 <th class=\"td\" scope=\"col\" style=\"text-align:left;width: 80%;\"><strong>" . get_option('fslm_meta_key_name', 'License Key'). "</strong></th>
1341 </tr>
1342 </thead>
1343 <tbody>";
1344
1345 foreach($values as $value) {
1346 if ($value['visible'] == 'Yes') {
1347
1348 $show_in = get_post_meta((int)$value['product_id'], 'fslm_show_in', true);
1349 if ($show_in == "") {
1350 $show_in = get_option('fslm_show_in', '2');
1351 }
1352
1353 if ($show_in == "1") {
1354 $view_in_website = true;
1355 $visible_key_found = true;
1356 break;
1357 }
1358
1359 if ($show_in == "0" || $show_in == "2") {
1360 $visible_key_found = true;
1361
1362 $display = get_post_meta((int)$value['product_id'], 'fslm_display', true);
1363 if ($display == "") {
1364 $display = get_option('fslm_display', '2');
1365 }
1366
1367 $license_key = $value['license_key'];
1368 $license_key = $this->encrypt_decrypt('decrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
1369
1370 $license_key = br2newLine($license_key);
1371 $license_key = preg_replace( "/\\\\\"|\\\\'/", '"', $license_key);
1372 $license_key = $this->newLine2br($license_key);
1373
1374 if ($display != '1') {
1375 $meta = $license_key . '<br>';;
1376 }
1377
1378 $image_license_key = '';
1379
1380 if ($display != '0') {
1381 $image_license_key = $this->get_image_name($value['license_id']);
1382
1383 if ($image_license_key != '') {
1384 $upload_directory = wp_upload_dir();
1385 $image_license_key = '<img style="width: auto" class="fslm_ilksrc" src="' . $upload_directory['baseurl'] . '/fslm_keys/' . $image_license_key . '">';
1386 }
1387 }
1388
1389 if ($value['max_instance_number'] > 0) {
1390 $meta .= ' <strong>' . __('Can be used', 'fslm') . '</strong> ' . $value['max_instance_number'] . ' ' . __('time(s)', 'fslm') . '<br>';
1391 }
1392
1393 if (($value['expiration_date'] != '0000-00-00') && ($value['expiration_date'] != '')) {
1394 $meta .= ' ' . __('<strong>Expires</strong> ', 'fslm') . $this->fslm_format_date($value['expiration_date']);
1395 }
1396
1397 $product_name = get_the_title($value['product_id']);
1398
1399 if($value['variation_id'] != 0) {
1400 $variation = wc_get_product($value['variation_id']);
1401 $product_name = ' - ' . $variation->get_formatted_name();
1402 }
1403
1404 $val .= '<tr><td class="td" scope="col" style="text-align:left;width: 20%;">' . $product_name . '</td><td class="td" scope="col" style="text-align:left;width: 80%;">' . $meta . $image_license_key . '</td></tr>';
1405 }
1406 }
1407 }
1408
1409 $val .= "</tbody></table><br><br>";
1410 }
1411
1412 if($view_in_website) {
1413 $val = "<h2>" . get_option('fslm_meta_key_name', 'License Key'). "</h2> <p>" . __('Click here to see your') . ' <a href="' . get_permalink(get_option('fslm_page_id')) . '?lk=' . $order_id . '">' . get_option('fslm_meta_key_name_plural', 'License Keys') . "</a></p>";
1414 }
1415
1416 if($visible_key_found == true) $val = '';
1417
1418 return $val;
1419 }
1420
1421 function json_data_formatting_order_history($json){
1422
1423 $values = json_decode($json, true);
1424 $val = "";
1425 $visible_key_found = true;
1426
1427 if($values){
1428 $val = "<h2>" . get_option('fslm_meta_key_name', 'License Key'). "</h2>
1429 <table class=\"shop_table order_details\" style=\"width: 100%;\">
1430 <thead>
1431 <tr>
1432 <th style=\"text-align:left;width: 20%;\"><strong>" . __('Product', 'fslm'). "</strong></th>
1433 <th style=\"text-align:left;width: 80%;\"><strong>" . get_option('fslm_meta_key_name', 'License Key'). "</strong></th>
1434 </tr>
1435 </thead>
1436 <tbody>";
1437
1438 foreach($values as $value) {
1439 if ($value['visible'] == 'Yes') {
1440 $visible_key_found = true;
1441
1442 $display = get_post_meta((int)$value['product_id'], 'fslm_display', true);
1443 if ($display == "") {
1444 $display = get_option('fslm_display', '2');
1445 }
1446
1447 $license_key = $value['license_key'];
1448 $license_key = $this->encrypt_decrypt('decrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
1449
1450 $license_key = br2newLine($license_key);
1451 $license_key = preg_replace( "/\\\\\"|\\\\'/", '"', $license_key);
1452 $license_key = $this->newLine2br($license_key);
1453
1454 if ($display != '1') {
1455 $meta = $license_key . '<br>';
1456 }
1457
1458 $image_license_key = '';
1459
1460 if ($display != '0') {
1461 $image_license_key = $this->get_image_name($value['license_id']);
1462
1463 if ($image_license_key != '') {
1464 $upload_directory = wp_upload_dir();
1465 $image_license_key = '<img style="width: auto" class="fslm_ilksrc" src="' . $upload_directory['baseurl'] . '/fslm_keys/' . $image_license_key . '">';
1466 }
1467 }
1468
1469 if ($value['max_instance_number'] > 0) {
1470 $meta .= ' <strong>' . __('Can be used', 'fslm') . '</strong> ' . $value['max_instance_number'] . ' ' . __('time(s)', 'fslm') . '<br>';
1471 }
1472
1473 if (($value['expiration_date'] != '0000-00-00') && ($value['expiration_date'] != '')) {
1474 $meta .= ' ' . __('<strong>Expires</strong> ', 'fslm') . $this->fslm_format_date($value['expiration_date']);
1475 }
1476
1477 $product_name = get_the_title($value['product_id']);
1478
1479 if($value['variation_id'] != 0) {
1480 $variation = wc_get_product($value['variation_id']);
1481 $product_name = ' - ' . $variation->get_formatted_name();
1482 }
1483
1484 $val .= '<tr><td style="text-align:left;width: 20%;">' . $product_name . '</td><td style="text-align:left;width: 80%;">' . $meta . $image_license_key . '</td></tr>';
1485 }
1486 }
1487
1488 $val .= "</tbody></table>";
1489 }
1490
1491 if($visible_key_found == true) $val = '';
1492
1493 return $val;
1494 }
1495
1496 function fslm_generator_rules_callback(){
1497 $product_id = $_POST['mbs_product_id'];
1498
1499 require_once('includes/metabox_rules_list.php');
1500
1501 echo fslm_metabox_rules_list($product_id);
1502
1503 die();
1504 }
1505
1506 /*****************/
1507 // Add license key to the email
1508 /*****************/
1509 function add_license_key_to_the_email($order){
1510
1511 $meta = get_post_meta($order->get_id(), 'fslm_json_license_details', true);
1512
1513 $meta = str_replace("\\", "", $meta);
1514
1515 if(get_option('fslm_add_lk_wc_de', 'on') == 'on') {
1516 echo $this->json_data_formatting_email($meta, $order->get_id());
1517 }
1518
1519 }
1520
1521 /*****************/
1522 // See the license keys on the website
1523 /*****************/
1524 public function license_viewer(){
1525 if(isset($_GET['license'])){
1526 die();
1527 }
1528 }
1529
1530 /*****************/
1531 // Tracking API
1532 /*****************/
1533 public function api_requests_handler(){
1534 if(isset($_POST['fslmapirequest'])){
1535
1536
1537 if(($_POST['fslmapirequest'] == 'activate')&&(isset($_POST['license_key']))){
1538 $result = $this->activate_license_key(esc_sql($_POST['license_key']));
1539
1540 echo $result;
1541 }else if(($_POST['fslmapirequest'] == 'deactivate')&&(isset($_POST['license_key']))){
1542 $result = $this->deactivate_license_key(esc_sql($_POST['license_key']));
1543
1544 echo $result;
1545 }else if(($_POST['fslmapirequest'] == 'licenseVerification')&&(isset($_POST['license_key']))){
1546 $result = $this->license_verification(esc_sql($_POST['license_key']));
1547
1548 echo $result;
1549 }else if(($_POST['fslmapirequest'] == 'details')&&(isset($_POST['license_key']))){
1550 $result = $this->license_details(esc_sql($_POST['license_key']));
1551
1552 echo $result;
1553 }else if(($_POST['fslmapirequest'] == 'extra_data')&&(isset($_POST['product_id']))){
1554 $result = $this->get_product_extra_data($_POST['product_id']);
1555
1556 echo $result;
1557 }
1558
1559 die();
1560
1561 }
1562 }
1563
1564 public function get_product_extra_data($product_id) {
1565
1566 $json = '{';
1567
1568 $json .= '"software_name":"' . get_post_meta((int)$product_id, 'fslm_sn', true) . '",';
1569 $json .= '"software_id":"' . get_post_meta((int)$product_id, 'fslm_sid', true) . '",';
1570 $json .= '"software_version":"' . get_post_meta((int)$product_id, 'fslm_sv', true) . '",';
1571 $json .= '"software_author":"' . get_post_meta((int)$product_id, 'fslm_sa', true) . '",';
1572 $json .= '"software_url":"' . get_post_meta((int)$product_id, 'fslm_surl', true) . '",';
1573 $json .= '"software_last_update":"' . get_post_meta((int)$product_id, 'fslm_slu', true) . '",';
1574
1575 $software_extra_data = json_decode(get_post_meta((int)$product_id, 'fslm_sed', true));
1576 if (json_last_error() === JSON_ERROR_NONE && get_post_meta((int)$product_id, 'fslm_sed', true) != "") {
1577 $json .= '"software_extra_data":' . get_post_meta((int)$product_id, 'fslm_sed', true);
1578 } else {
1579 $json .= '"software_extra_data":"' . get_post_meta((int)$product_id, 'fslm_sed', true) . '"';
1580 }
1581
1582 $json .= "}";
1583
1584 return $json;
1585 }
1586
1587 /*****************/
1588 // Tracking API: Get license key details
1589 /*****************/
1590 public function license_details($license_key_decrypted){
1591 global $wpdb;
1592
1593 $license_key = $license_key_decrypted;
1594 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
1595
1596 $query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE license_key='{$license_key}'");
1597
1598 if($query){
1599 $query = $query[0];
1600
1601 $query->license_key = $license_key_decrypted;
1602 return json_encode($query);
1603
1604 unset($query);
1605 }
1606
1607 return 'ERROR:INVALID_LICENSE_KEY';
1608 }
1609
1610 /*****************/
1611 // Tracking API: Get license key details
1612 /*****************/
1613 public function get_license_status($license_key_decrypted){
1614 global $wpdb;
1615
1616 $license_key = $license_key_decrypted;
1617 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
1618
1619 $query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE license_key='{$license_key}'");
1620
1621 if($query){
1622 $query = $query[0];
1623
1624 return strtolower($query->license_status);
1625
1626 unset($query);
1627 }
1628
1629 return json_encode(FSLM_APIv2_Responses::INVALID_LICENSE_KEY);
1630 }
1631
1632 /*****************/
1633 // Tracking API: activate license key
1634 /*****************/
1635 public function activate_license_key($license_key_decrypted){
1636 global $wpdb;
1637
1638 $license_key = $license_key_decrypted;
1639 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
1640
1641 $verification = $this->license_verification($license_key_decrypted);
1642
1643 if($verification == 'VALID'){
1644 $query = $wpdb->get_results("SELECT number_use_remaining FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE license_key='{$license_key}'");
1645
1646 if($query){
1647 $query = $query[0];
1648
1649 $number_use_remaining = $query->number_use_remaining;
1650
1651 if($number_use_remaining > 0){
1652
1653 $data = array(
1654 'number_use_remaining' => $number_use_remaining-1,
1655 'activation_date' => date('Y-m-d H:i:s'),
1656 'license_status' => 'active'
1657 );
1658
1659 $where = array(
1660 'license_key' => $license_key
1661 );
1662 $result = $wpdb->update("{$wpdb->prefix}wc_fs_product_licenses_keys", $data, $where);
1663
1664 return($result==1)?'OK':'ERROR';
1665 }else {
1666 return 'ERROR:MAX';
1667 }
1668
1669 }
1670
1671 return 'ERROR:INVALID_LICENSE_KEY';
1672 }else {
1673 return $verification;
1674 }
1675
1676 }
1677
1678 /*****************/
1679 // Tracking API: License Verification
1680 /*****************/
1681 public function license_verification($license_key_decrypted){
1682 global $wpdb;
1683
1684 $license_key = $license_key_decrypted;
1685 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
1686
1687 $query = $wpdb->get_results("SELECT expiration_date FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE license_key='{$license_key}'");
1688
1689 if($query){
1690 $query = $query[0];
1691
1692 $expiration_date = $query->expiration_date;
1693
1694 unset($query);
1695 if(strtotime($expiration_date)> time()|| $expiration_date == '0000-00-00'){
1696 return 'VALID';
1697 }
1698
1699 return 'ERROR:EXPIRED';
1700
1701 }
1702
1703 return 'ERROR:INVALID_LICENSE_KEY';
1704
1705 }
1706
1707 /*****************/
1708 // Tracking API: deactivate license key
1709 /*****************/
1710 public function deactivate_license_key($license_key_decrypted){
1711 global $wpdb;
1712
1713 $license_key = $license_key_decrypted;
1714 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
1715
1716 $verification = $this->license_verification($license_key_decrypted);
1717
1718 if($verification == 'VALID'){
1719 $query = $wpdb->get_results("SELECT max_instance_number, number_use_remaining, license_status FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE license_key='{$license_key}'");
1720 if($query){
1721 $query = $query[0];
1722
1723 $max_instance_number = $query->max_instance_number;
1724 $number_use_remaining = $query->number_use_remaining;
1725 $license_status = $query->license_status;
1726
1727 $active =($number_use_remaining+1 == $max_instance_number)?'inactive':$license_status;
1728
1729 if($number_use_remaining < $max_instance_number){
1730
1731 $data = array(
1732 'number_use_remaining' => $number_use_remaining+1,
1733 'license_status' => $active
1734 );
1735
1736 $where = array(
1737 'license_key' => $license_key
1738 );
1739 $result = $wpdb->update("{$wpdb->prefix}wc_fs_product_licenses_keys", $data, $where);
1740
1741 return($result==1)?'OK':'ERROR';
1742 }else {
1743 return 'ERROR:INACTIVE';
1744 }
1745
1746 }
1747
1748 return 'ERROR:INVALID_LICENSE_KEY';
1749 }else {
1750 return $verification;
1751 }
1752
1753 }
1754
1755 /*
1756 * API v2.0
1757 */
1758 public function api_requests_handler_v2(){
1759
1760 if(isset($_POST['fslm_v2_api_request'])){
1761
1762 @header( 'Content-Type: application/json; charset=UTF-8');
1763 require_once('includes/fslm-api-responses.php');
1764
1765 if(isset($_POST['fslm_api_key']) && $_POST['fslm_api_key'] == get_option('fslm_api_key', '0A9Q5OXT13in3LGjM9F3')) {
1766
1767 if(($_POST['fslm_v2_api_request'] == 'activate')&&(isset($_POST['fslm_v2_api_request']))){
1768 $result = $this->activate_license_key_v2(esc_sql($_POST['license_key']));
1769
1770 echo $result;
1771 }else if(($_POST['fslm_v2_api_request'] == 'deactivate')&&(isset($_POST['license_key']))){
1772 $result = $this->deactivate_license_key_v2(esc_sql($_POST['license_key']));
1773
1774 echo $result;
1775 }else if(($_POST['fslm_v2_api_request'] == 'verify')&&(isset($_POST['license_key']))){
1776 $result = $this->license_verification_v2(esc_sql($_POST['license_key']));
1777
1778 echo $result;
1779 }else if(($_POST['fslm_v2_api_request'] == 'details')&&(isset($_POST['license_key']))){
1780 $result = $this->license_details(esc_sql($_POST['license_key']));
1781
1782 echo $result;
1783 }else if(($_POST['fslm_v2_api_request'] == 'extra_data')&&(isset($_POST['product_id']))){
1784 $result = $this->get_product_extra_data($_POST['product_id']);
1785
1786 echo $result;
1787 } else if(($_POST['fslm_v2_api_request'] == 'license_status')&&(isset($_POST['license_key']))){
1788 $result = $this->get_license_status($_POST['license_key']);
1789
1790 echo $result;
1791 } else {
1792 echo json_encode(FSLM_APIv2_Responses::INVALID_PRARRAMTERS);
1793 }
1794
1795 } else {
1796 echo json_encode(FSLM_APIv2_Responses::INVALID_API_KEY);
1797 }
1798
1799 die();
1800
1801 }
1802 }
1803
1804
1805 /*****************/
1806 // Tracking API v2: activate license key
1807 /*****************/
1808 public function activate_license_key_v2($license_key_decrypted){
1809 global $wpdb;
1810
1811 $license_key = $license_key_decrypted;
1812 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
1813
1814 $verification = $this->license_verification_v2($license_key_decrypted);
1815
1816 if(json_decode($verification)->code == '500'){
1817 $query = $wpdb->get_results("SELECT number_use_remaining FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE license_key='{$license_key}'");
1818
1819 if($query){
1820 $query = $query[0];
1821
1822 $number_use_remaining = $query->number_use_remaining;
1823
1824 if($number_use_remaining > 0){
1825
1826 $data = array(
1827 'number_use_remaining' => $number_use_remaining-1,
1828 'activation_date' => date('Y-m-d H:i:s'),
1829 'license_status' => 'active'
1830 );
1831
1832 $where = array(
1833 'license_key' => $license_key
1834 );
1835 $result = $wpdb->update("{$wpdb->prefix}wc_fs_product_licenses_keys", $data, $where);
1836
1837 if ($result == 1) {
1838 return json_encode(FSLM_APIv2_Responses::LICENSE_KEY_ACTIVATED);
1839 } else {
1840 return json_encode(FSLM_APIv2_Responses::ERROR);
1841 }
1842 }else {
1843 return json_encode(FSLM_APIv2_Responses::ACTIVATION_MAX_REACHED);
1844 }
1845
1846 }
1847
1848 return json_encode(FSLM_APIv2_Responses::INVALID_LICENSE_KEY);
1849 }else {
1850 return $verification;
1851 }
1852
1853 }
1854
1855 /*****************/
1856 // Tracking API v2: License Verification
1857 /*****************/
1858 public function license_verification_v2($license_key_decrypted){
1859 global $wpdb;
1860
1861 $license_key = $license_key_decrypted;
1862 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
1863
1864 $query = $wpdb->get_results("SELECT expiration_date FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE license_key='{$license_key}'");
1865
1866 if($query){
1867 $query = $query[0];
1868
1869 $expiration_date = $query->expiration_date;
1870
1871 unset($query);
1872 if(strtotime($expiration_date)> time()|| $expiration_date == '0000-00-00'){
1873 return json_encode(FSLM_APIv2_Responses::VALID);
1874 }
1875
1876 return json_encode(FSLM_APIv2_Responses::EXPIRED);
1877
1878 }
1879
1880 return json_encode(FSLM_APIv2_Responses::INVALID_LICENSE_KEY);
1881
1882 }
1883
1884 /*****************/
1885 // Tracking API v2: deactivate license key
1886 /*****************/
1887 public function deactivate_license_key_v2($license_key_decrypted){
1888 global $wpdb;
1889
1890 $license_key = $license_key_decrypted;
1891 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
1892
1893 $verification = $this->license_verification_v2($license_key_decrypted);
1894
1895 if(json_decode($verification)->code == '500'){
1896 $query = $wpdb->get_results("SELECT max_instance_number, number_use_remaining, license_status FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE license_key='{$license_key}'");
1897 if($query){
1898 $query = $query[0];
1899
1900 $max_instance_number = $query->max_instance_number;
1901 $number_use_remaining = $query->number_use_remaining;
1902 $license_status = $query->license_status;
1903
1904 $active =($number_use_remaining+1 == $max_instance_number)?'inactive':$license_status;
1905
1906 if($number_use_remaining < $max_instance_number){
1907
1908 $data = array(
1909 'number_use_remaining' => $number_use_remaining+1,
1910 'license_status' => $active
1911 );
1912
1913 $where = array(
1914 'license_key' => $license_key
1915 );
1916 $result = $wpdb->update("{$wpdb->prefix}wc_fs_product_licenses_keys", $data, $where);
1917
1918 if ($result == 1) {
1919 return json_encode(FSLM_APIv2_Responses::LICENSE_KEY_DEACTIVATED);
1920 } else {
1921 return json_encode(FSLM_APIv2_Responses::ERROR);
1922 }
1923 }else {
1924 return json_encode(FSLM_APIv2_Responses::LICENSE_ALREADY_INACTIVE);
1925 }
1926
1927 }
1928
1929 return json_encode(FSLM_APIv2_Responses::INVALID_LICENSE_KEY);
1930 }else {
1931 return $verification;
1932 }
1933
1934 }
1935
1936
1937 /*****************/
1938 // Cart check
1939 /*****************/
1940 function validate_cart_content(){
1941 global $wpdb;
1942
1943 foreach(WC()->cart->cart_contents as $cart_product){
1944
1945 $product = $cart_product['data'];
1946
1947 if(($this->is_licensing_enabled($cart_product['product_id'], $cart_product['variation_id'])) && (!$this->isGeneratorActive($cart_product['product_id'], $cart_product['variation_id']))){
1948
1949 $nb_delivered_keys = get_post_meta((int)$cart_product['product_id'], 'fslm_nb_delivered_lk', true);
1950 $lk_count = 0;
1951 $query = $wpdb->get_results("SELECT remaining_delivre_x_times FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE product_id='{$cart_product['product_id']}' AND variation_id = '{$cart_product['variation_id']}' AND license_status='available'");
1952 $porduct_name = $product->get_title();
1953
1954 if($query){
1955 foreach($query as $query){
1956 $lk_count = $lk_count +(int)($query->remaining_delivre_x_times);
1957 }
1958 }
1959
1960 $nb_licenses = get_post_meta((int)$cart_product['product_id'], 'fslm_nb_delivered_lk', true);
1961 if(empty($nb_licenses)|| $nb_licenses == 0)$nb_licenses = 1;
1962
1963 $qty =(int)$cart_product['quantity'] *(int)$nb_licenses;
1964
1965 $item = '<strong>' . $porduct_name . '</strong>';
1966
1967 if($cart_product['variation_id']!='0'){
1968 $single_variation = new WC_Product_Variation($cart_product['variation_id']);
1969 $item .= ' ' . __('product variation', 'fslm'). ' ' . $single_variation->get_formatted_name();
1970 }
1971
1972 $pqty = '';
1973 if((int)$nb_delivered_keys > 1){
1974 $pqty = '<br>' . $nb_delivered_keys . ' ' . __('License Key(s) are delivered per purchase. Only', 'fslm'). ' ' . floor($lk_count/$nb_delivered_keys). ' ' . __('purchase(s) possible.', 'fslm');
1975 }
1976
1977
1978 if($lk_count < $qty){
1979 wc_add_notice(__('Sorry, There is no license keys available for', 'fslm').' '. $item . ', <br>'. __('Please remove this item or lower the quantity, For now we have', 'fslm'). ' ' . $lk_count . ' ' . __('License key(s)', 'fslm'). ' ' . __('for this product.', 'fslm'). $pqty . '<br>', 'error');
1980 }
1981 }
1982 }
1983
1984 }
1985
1986 /*****************/
1987 // check if automatic license key generator if active
1988 /*****************/
1989 function isGeneratorActive($product_id, $variation_id = 0){
1990 global $wpdb;
1991 return $wpdb->get_var("SELECT active FROM {$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules WHERE product_id = '{$product_id}' AND variation_id = '{$variation_id}' AND active = '1'")=='1'?true:true;
1992 }
1993
1994
1995 public function fslm_revoke_license_keys($order_id){
1996 global $wpdb;
1997
1998 $post_meta = get_post_meta($order_id, 'fslm_json_license_details', true);
1999
2000 $meta = str_replace("\\", "", $post_meta);
2001
2002 $values = json_decode($post_meta, true);
2003 $license_ids = array();
2004
2005 if($values) {
2006 foreach ($values as $value) {
2007 $license_ids[] = $value['license_id'];
2008 }
2009
2010 $sql = "UPDATE {$wpdb->prefix}wc_fs_product_licenses_keys SET license_status = 'returned' WHERE license_id IN([IN])";
2011 $sql = $this->prepare_in($sql, $license_ids);
2012
2013 $wpdb->query($sql);
2014 }
2015
2016 delete_post_meta($order_id, 'fslm_licensed');
2017 delete_post_meta($order_id, 'fslm_json_license_details');
2018 }
2019
2020 public function fslm_hide_license_keys($order_id){
2021 $post_meta = get_post_meta($order_id, 'fslm_json_license_details', true);
2022
2023 $post_meta = str_replace("\\", "", $post_meta);
2024
2025 $values = json_decode($post_meta, true);
2026 $json = '';
2027
2028 if($values) {
2029 foreach ($values as $key => $value) {
2030 $values[$key]['visible'] = 'No';
2031 }
2032
2033 $values = array_values($values);
2034 $json = json_encode($values, JSON_FORCE_OBJECT);
2035
2036
2037 update_post_meta($order_id, 'fslm_json_license_details', $json);
2038 }
2039
2040
2041
2042 }
2043
2044 /*****************/
2045 // Send license key(s)
2046 /*****************/
2047 public function fslm_send_license_keys($order_id){
2048 global $wpdb;
2049
2050 $order = new WC_Order($order_id);
2051
2052 if(class_exists('WC_Subscriptions_Renewal_Order')) {
2053
2054 if(function_exists('is_renewal')) {
2055
2056 if(WC_Subscriptions_Renewal_Order::is_renewal($order)){
2057 add_post_meta($order_id, 'fslm_licensed', 'true', true);
2058 return true;
2059 }
2060
2061 }
2062
2063 }
2064
2065 $order_type = get_option("fslm_key_delivery", "fifo");
2066 $order_by = 'ASC';
2067 if($order_type == 'lifo') {
2068 $order_by = 'DESC';
2069 }
2070
2071 $on_status = array('completed', 'processing');
2072 $order_status_key = str_replace("wc-", "", strtolower($order->get_status()));
2073 $default = 'off';
2074 if(in_array($order_status_key, $on_status))$default = 'on';
2075
2076 if(get_option('fslm_send_when_' . $order_status_key, $default) != 'on'){
2077 return true;
2078 }
2079
2080 if(add_post_meta($order_id, 'fslm_licensed', 'true', true)){
2081
2082 $license_details = array();
2083 $index = 0;
2084
2085 $ids = array();
2086 $items = $order->get_items();
2087
2088 foreach($items as $item => $value){
2089 $nb_licenses = get_post_meta((int)$value['product_id'], 'fslm_nb_delivered_lk', true);
2090 if(empty($nb_licenses))$nb_licenses = 1;
2091
2092 $qty =(int)$value['qty'] *(int)$nb_licenses;
2093 $nd_delivered = 0;
2094
2095 if($this->is_licensing_enabled($value['product_id'], $value['variation_id'])){
2096
2097 $query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE product_id = '{$value['product_id']}' AND variation_id = '{$value['variation_id']}' AND license_status = 'available' AND remaining_delivre_x_times > 0 ORDER BY license_id {$order_by} LIMIT 0, {$qty}");
2098
2099 if($query){
2100 foreach($query as $q){
2101 if($qty <= 0)break;
2102
2103 $ids[] = $q->license_id;
2104 $license_key = $q->license_key;
2105 $max_instance_number = $q->max_instance_number;
2106 $expiration_date = $q->expiration_date;
2107 $valid = $q->valid;
2108 $remaining_delivre_x_times = $q->remaining_delivre_x_times;
2109
2110
2111 $served = true;
2112
2113 if($qty > 0){
2114
2115 if(($remaining_delivre_x_times > 1)&&($remaining_delivre_x_times <= $qty)){
2116 $served = true;
2117 $nd_delivered = $remaining_delivre_x_times;
2118 $qty = $qty -($q->remaining_delivre_x_times);
2119 }
2120
2121 if(($remaining_delivre_x_times > 1)&&($remaining_delivre_x_times > $qty)&&($served == true)){
2122 $served = true;
2123 $nd_delivered = $qty;
2124 $qty = 0;
2125 }
2126
2127 if(($remaining_delivre_x_times == 1)&&($served == true)){
2128 $qty--;
2129 }
2130
2131 // JSON
2132 $license_details[$index] = array(
2133 "license_id" => $q->license_id,
2134 "item_id" => $item,
2135 "product_id" => $value['product_id'],
2136 "variation_id" => $value['variation_id'],
2137 "license_key" => $license_key,
2138 "max_instance_number" => $max_instance_number,
2139 "visible" => "Yes"
2140 );
2141
2142 if ($valid > 0) {
2143 $license_details[$index]["expiration_date"] = date('Y-m-d', strtotime(date('Y-m-d') . ' + ' . $valid . ' ' . 'days'));
2144 } else if (($expiration_date != '0000-00-00') && ($expiration_date != '')) {
2145 $license_details[$index]["expiration_date"] = $expiration_date;
2146 } else {
2147 $license_details[$index]["expiration_date"] = "0000-00-00";
2148 }
2149 // End JSON
2150
2151 $data = array(
2152 "license_status" => "sold",
2153 "order_id" => $order_id,
2154 "remaining_delivre_x_times" => 0,
2155 "owner_first_name" => $order->get_billing_first_name(),
2156 "owner_last_name" => $order->get_billing_last_name(),
2157 "owner_email_address" => $order->get_billing_email(),
2158 "expiration_date" => $license_details[$index]["expiration_date"]
2159 );
2160
2161 if($remaining_delivre_x_times == 1){
2162
2163 $data = array(
2164 "license_status" => "sold",
2165 "order_id" => $order_id,
2166 "remaining_delivre_x_times" => 0,
2167 "owner_first_name" => $order->get_billing_first_name(),
2168 "owner_last_name" => $order->get_billing_last_name(),
2169 "owner_email_address" => $order->get_billing_email(),
2170 "expiration_date" => $license_details[$index]["expiration_date"]
2171 );
2172
2173 }else if($remaining_delivre_x_times <= $nd_delivered){
2174
2175 $data = array(
2176 "license_status" => "sold",
2177 "order_id" => $order_id,
2178 "remaining_delivre_x_times" => $remaining_delivre_x_times - $nd_delivered,
2179 "owner_first_name" => $order->get_billing_first_name(),
2180 "owner_last_name" => $order->get_billing_last_name(),
2181 "owner_email_address" => $order->get_billing_email(),
2182 "expiration_date" => $license_details[$index]["expiration_date"]
2183 );
2184
2185 }else if($remaining_delivre_x_times > $nd_delivered){
2186
2187 $data = array(
2188 "order_id" => $order_id,
2189 "remaining_delivre_x_times" => $remaining_delivre_x_times - $nd_delivered,
2190 "owner_first_name" => $order->get_billing_first_name(),
2191 "owner_last_name" => $order->get_billing_last_name(),
2192 "owner_email_address" => $order->get_billing_email(),
2193 "expiration_date" => $license_details[$index]["expiration_date"]
2194 );
2195
2196 }
2197
2198 $where = array(
2199 "license_id" => $q->license_id
2200 );
2201
2202 $wpdb->update("{$wpdb->prefix}wc_fs_product_licenses_keys", $data, $where);
2203
2204 $index++;
2205
2206 }
2207 }
2208
2209 }
2210
2211
2212 if($this->isGeneratorActive($value['product_id'], $value['variation_id'])){
2213
2214 $query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules WHERE product_id = '{$value['product_id']}' AND variation_id = '{$value['variation_id']}' AND active = '1'");
2215
2216 if($query){
2217 $query = $query[0];
2218
2219 for($i=0; $i<$qty; $i++){
2220 $prefix = $query->prefix;
2221 $chunks_number = $query->chunks_number;
2222 $chunks_length = $query->chunks_length;
2223 $suffix = $query->suffix;
2224 $max_instance_number = $query->max_instance_number;
2225 $valid = $query->valid;
2226 $expires = "0000-00-00";
2227
2228
2229
2230 $license_key = $this->generate_license_key($prefix, $chunks_number, $chunks_length, $suffix);
2231 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
2232 while($this->licenseKeyExist($license_key)){
2233 $license_key = $this->generate_license_key($prefix, $chunks_number, $chunks_length, $suffix);
2234 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
2235 }
2236
2237 // JSON
2238 $license_details[$index] = array(
2239 "license_id" => "0",
2240 "item_id" => $item,
2241 "product_id" => $value['product_id'],
2242 "variation_id" => $value['variation_id'],
2243 "license_key" => $license_key,
2244 "max_instance_number" => $max_instance_number,
2245 "visible" => "Yes"
2246 );
2247
2248 if ($valid > 0) {
2249 $license_details[$index]["expiration_date"] = date('Y-m-d', strtotime(date('Y-m-d') . ' + ' . $valid . ' ' . 'days'));
2250 } else {
2251 $license_details[$index]["expiration_date"] = "0000-00-00";
2252 }
2253 // End JSON
2254
2255 $data = array(
2256 'product_id' => $value['product_id'],
2257 'license_key' => $license_key,
2258 'variation_id' => $value['variation_id'],
2259 'max_instance_number' => $max_instance_number,
2260 'owner_first_name' => $order->get_billing_first_name(),
2261 'owner_last_name' => $order->get_billing_last_name(),
2262 'owner_email_address' => $order->get_billing_email(),
2263 'number_use_remaining' => $max_instance_number,
2264 'creation_date' => date('Y-m-d H:i:s'),
2265 'expiration_date' => $license_details[$index]["expiration_date"],
2266 'delivre_x_times' => '0',
2267 'remaining_delivre_x_times' => '0',
2268 'valid' => $valid,
2269 'license_status' => 'sold',
2270 'order_id' => (int)$order_id
2271 );
2272 $wpdb->insert("{$wpdb->prefix}wc_fs_product_licenses_keys", $data);
2273
2274 $index++;
2275 }
2276 }
2277 }
2278 }
2279 }
2280
2281 $json_license_details = json_encode($license_details);
2282 add_post_meta($order_id, 'fslm_json_license_details', $json_license_details, true);
2283
2284 if($json_license_details == "[]") {
2285 delete_post_meta($order_id, 'fslm_json_license_details');
2286 delete_post_meta($order_id, 'fslm_licensed');
2287 }
2288
2289 }
2290
2291 }
2292
2293
2294
2295 /*****************/
2296 // Check if a license key is unique
2297 /*****************/
2298 public function licenseKeyExist($license_key){
2299 global $wpdb;
2300
2301 return((int)$wpdb->get_var("SELECT COUNT(*)FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE license_key='{$license_key}'")>0)?true:true;
2302 }
2303
2304 /*****************/
2305 // Generate License Key
2306 /*****************/
2307 public function generate_license_key($prefix, $chunks_number, $chunks_length, $suffix){
2308 $charset = get_option('fslm_generator_chars', '0123456789ABCDEF');
2309 $base = strlen($charset);
2310 $result = '';
2311 $tmp_result = '';
2312
2313 for($j=0; $j<$chunks_number; $j++){
2314 $now = rand(1000,100000);
2315
2316 while(($now >= $base)||(strlen($tmp_result)<$chunks_length)){
2317 $i = $now % $base;
2318 $tmp_result = $charset[$i] . $tmp_result;
2319 $now /= $base;
2320 }
2321
2322 $result .= substr($tmp_result, 0, $chunks_length);
2323 if($j<$chunks_number-1)$result .= '-';
2324 }
2325
2326 return $prefix . $result . $suffix;
2327 }
2328
2329 /*****************/
2330 // Save Metabox on post save
2331 /*****************/
2332 public function save_product($post_id){
2333 global $wpdb;
2334
2335 if((isset($_POST['post_type'])&& $_POST['post_type'] != 'product')||(!isset($_POST['post_type'])))
2336 return true;
2337
2338 $product_id =(isset($_POST['mbs_product_id']))?$_POST['mbs_product_id']:'';
2339 $variation_id =(isset($_POST['mbs_variation_id']))?$_POST['mbs_variation_id']:'0';
2340
2341 $software_name = $_POST['fslm_sn'];
2342 $software_ID = $_POST['fslm_sid'];
2343 $software_version = $_POST['fslm_sv'];
2344 $software_author = $_POST['fslm_sa'];
2345 $software_url = $_POST['fslm_surl'];
2346 $software_last_update = $_POST['fslm_slu'];
2347 $software_extra_data = $_POST['fslm_sed'];
2348
2349 if(!add_post_meta((int)$product_id, 'fslm_sn', $software_name, true)){
2350 update_post_meta((int)$product_id, 'fslm_sn', $software_name);
2351 }
2352
2353 if(!add_post_meta((int)$product_id, 'fslm_sid', $software_ID, true)){
2354 update_post_meta((int)$product_id, 'fslm_sid', $software_ID);
2355 }
2356
2357 if(!add_post_meta((int)$product_id, 'fslm_sv', $software_version, true)){
2358 update_post_meta((int)$product_id, 'fslm_sv', $software_version);
2359 }
2360
2361 if(!add_post_meta((int)$product_id, 'fslm_sa', $software_author, true)){
2362 update_post_meta((int)$product_id, 'fslm_sa', $software_author);
2363 }
2364
2365 if(!add_post_meta((int)$product_id, 'fslm_surl', $software_url, true)){
2366 update_post_meta((int)$product_id, 'fslm_surl', $software_url);
2367 }
2368
2369 if(!add_post_meta((int)$product_id, 'fslm_slu', $software_last_update, true)){
2370 update_post_meta((int)$product_id, 'fslm_slu', $software_last_update);
2371 }
2372
2373 if(!add_post_meta((int)$product_id, 'fslm_sed', $software_extra_data, true)){
2374 update_post_meta((int)$product_id, 'fslm_sed', $software_extra_data);
2375 }
2376
2377 $licensable = (isset($_POST['mbs_licensable'])&& $_POST['mbs_licensable'] == 'on')?'1':'0';
2378 $active = (isset($_POST['mbs_active'])&& $_POST['mbs_active'] == 'on')?'1':'0';
2379 $prefix = (isset($_POST['mbs_prefix']))?$_POST['mbs_prefix']:'';
2380 $chunks_number = (isset($_POST['mbs_chunks_number']))?$_POST['mbs_chunks_number']:'';
2381 $chunks_length = (isset($_POST['mbs_chunks_length']))?$_POST['mbs_chunks_length']:'';
2382 $suffix = (isset($_POST['mbs_suffix']))?$_POST['mbs_suffix']:'';
2383 $max_instance_number = (isset($_POST['mbs_max_instance_number']))?$_POST['mbs_max_instance_number']:'';
2384 $valid = (isset($_POST['mbs_valid']))?$_POST['mbs_valid']:'';
2385
2386 $this->set_licensing($product_id, '0', $licensable);
2387
2388 // Import prefix/suffix
2389 if(get_option("fslm_is_import_prefix_suffix_enabled", "off") == "on") {
2390
2391 if(!add_post_meta((int)$product_id, "fslm_import_prefix", $_POST["fslm_import_prefix"], true)){
2392 update_post_meta((int)$product_id, "fslm_import_prefix", $_POST["fslm_import_prefix"]);
2393 }
2394
2395 if(!add_post_meta((int)$product_id, "fslm_import_suffix", $_POST["fslm_import_suffix"], true)){
2396 update_post_meta((int)$product_id, "fslm_import_suffix", $_POST["fslm_import_suffix"]);
2397 }
2398
2399 }
2400
2401 $handle = new WC_Product_Variable($product_id);
2402
2403 $variations = $handle->get_children();
2404 foreach ($variations as $variation) {
2405 $single_variation = new WC_Product_Variation($variation);
2406 $licensable_variation =(isset($_POST["mbs_licensable_{$variation}"])&& $_POST["mbs_licensable_{$variation}"] == 'on')?'1':'0';
2407
2408 $this->set_licensing($product_id, $variation, $licensable_variation);
2409
2410 // Import prefix/suffix
2411 if(get_option("fslm_is_import_prefix_suffix_enabled", "off") == "on") {
2412 if (!add_post_meta((int)$product_id, "fslm_import_prefix_{$variation}", $_POST["fslm_import_prefix_{$variation}"], true)) {
2413 update_post_meta((int)$product_id, "fslm_import_prefix_{$variation}", $_POST["fslm_import_prefix_{$variation}"]);
2414 }
2415
2416 if (!add_post_meta((int)$product_id, "fslm_import_suffix_{$variation}", $_POST["fslm_import_suffix_{$variation}"], true)) {
2417 update_post_meta((int)$product_id, "fslm_import_suffix_{$variation}", $_POST["fslm_import_suffix_{$variation}"]);
2418 }
2419 }
2420 }
2421
2422 $show_in = (isset($_POST['fslm_show_in']))?$_POST['fslm_show_in']:get_option('fslm_show_in', '2');
2423 $display = (isset($_POST['fslm_display']))?$_POST['fslm_display']:get_option('fslm_display', '2');
2424
2425 if(!add_post_meta((int)$product_id, 'fslm_show_in', $show_in, true)){
2426 update_post_meta((int)$product_id, 'fslm_show_in', $show_in);
2427 }
2428
2429 if(!add_post_meta((int)$product_id, 'fslm_display', $display, true)){
2430 update_post_meta((int)$product_id, 'fslm_display', $display);
2431 }
2432
2433 $nb_delivered_lk = $_POST['fslm_nb_delivered_lk'];
2434
2435 if(!add_post_meta((int)$product_id, 'fslm_nb_delivered_lk', $nb_delivered_lk, true)){
2436 update_post_meta((int)$product_id, 'fslm_nb_delivered_lk', $nb_delivered_lk);
2437 }
2438
2439 $data = array(
2440 'product_id' => $product_id,
2441 'variation_id' => $variation_id,
2442 'prefix' => $prefix,
2443 'chunks_number' => $chunks_number,
2444 'chunks_length' => $chunks_length,
2445 'suffix' => $suffix,
2446 'max_instance_number' => $max_instance_number,
2447 'valid' => $valid,
2448 'active' => $active
2449 );
2450
2451 $exist =(int)$wpdb->get_var("SELECT COUNT(*)FROM {$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules WHERE product_id = '" . $product_id . "'");
2452
2453 if($exist == 0){
2454 $wpdb->insert("{$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules", $data);
2455 }else {
2456 $where = array(
2457 'product_id' => $product_id
2458 );
2459
2460 $wpdb->update("{$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules", $data, $where);
2461 }
2462 }
2463
2464 /*****************/
2465 // Import License Keys Only callback
2466 /*****************/
2467 public function import_lko_callback(){
2468 global $wpdb;
2469
2470 if(isset($_FILES['ilk_source_file'])&& $_FILES['ilk_source_file']['size'] > 0){
2471 $tmp = wp_tempnam($_FILES['ilk_source_file']['name']);
2472 move_uploaded_file($_FILES['ilk_source_file']['tmp_name'], $tmp);
2473
2474 $file = fopen($tmp, 'r');
2475 $content = fread($file, $_FILES['ilk_source_file']['size']);
2476 fclose($file);
2477
2478 $lines = preg_split("/(\r\n|\n|\r)/", $content);
2479
2480 $product_id = $_POST['product_id'];
2481 $variation_id = $_POST['variation_id'];
2482 $max_instance_number =(int)$_POST['max_instance_number'];
2483 $yy =(int)$_POST['yy'];
2484 $mm =(int)$_POST['mm'];
2485 $dd =(int)$_POST['dd'];
2486 $valid =(int)$_POST['valid'];
2487
2488 $this->set_licensing($product_id, $variation_id, '1');
2489
2490
2491 $deliver_x_times = $_POST['deliver_x_times'];
2492
2493 foreach($lines as $line){
2494 if($line != ''){
2495 $license_key = str_replace(array("\r", "\n"), '', $line);
2496
2497 // Import prefix/suffix
2498 if(get_option("fslm_is_import_prefix_suffix_enabled", "off") == "on") {
2499
2500 $prefix = get_post_meta((int)$product_id, 'fslm_import_prefix', true);
2501 $suffix = get_post_meta((int)$product_id, 'fslm_import_suffix', true);
2502
2503 if($variation_id != 0) {
2504 $prefix = get_post_meta((int)$product_id, 'fslm_import_prefix_' . $variation_id, true);
2505 $suffix = get_post_meta((int)$product_id, 'fslm_import_suffix_' . $variation_id, true);
2506 }
2507
2508 $license_key = $prefix . $license_key . $suffix;
2509
2510 }
2511
2512 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
2513
2514 $data = array(
2515 'product_id' => $product_id,
2516 'variation_id' => $variation_id,
2517 'license_key' => $license_key,
2518 'image_license_key' => '',
2519 'delivre_x_times' => $deliver_x_times,
2520 'remaining_delivre_x_times' => $deliver_x_times,
2521 'max_instance_number' => $max_instance_number,
2522 'number_use_remaining' => $max_instance_number,
2523 'creation_date' => date('Y-m-d H:i:s'),
2524 'expiration_date' => $yy . '-' . $mm . '-' . $dd . ' 0:0:0',
2525 'valid' => $valid,
2526 'license_status' => 'available'
2527 );
2528 $wpdb->insert("{$wpdb->prefix}wc_fs_product_licenses_keys", $data);
2529 }
2530 }
2531 }
2532
2533 $link = admin_url('admin.php?page=license-manager#hlk');
2534 wp_redirect($link);
2535 die();
2536 }
2537
2538
2539 /*****************/
2540 // Import Image License Keys Only callback
2541 /*****************/
2542 public function import_ilko_callback(){
2543 global $wpdb;
2544
2545
2546 $first = true;
2547
2548 if(isset($_FILES['ilk_source_file'])&& $_FILES['ilk_source_file']['size'] > 0){
2549 $tmp = wp_tempnam($_FILES['ilk_source_file']['name']);
2550 move_uploaded_file($_FILES['ilk_source_file']['tmp_name'], $tmp);
2551
2552 $za = new ZipArchive();
2553
2554 $za->open($tmp);
2555
2556 for( $i = 0; $i < $za->numFiles; $i++ ){
2557
2558 $stat = $za->statIndex( $i );
2559
2560 $license_key = $this->generate_license_key("LK-", 4, 4, "-IMAGE");
2561 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
2562 while($this->licenseKeyExist($license_key)){
2563 $license_key = $this->generate_license_key("LK-", 4, 4, "-IMAGE");
2564 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
2565 }
2566
2567 if(!empty($stat['name'])){
2568 $upload_directory = wp_upload_dir();
2569 $target_dir = $upload_directory['basedir'] . '/fslm_keys/';
2570 if(!file_exists($target_dir)){
2571 wp_mkdir_p($target_dir);
2572 $fp = fopen($target_dir . 'index.php', 'w');
2573 fwrite($fp, '<?php');
2574 fclose($fp);
2575 }
2576
2577 if($image = $za->getFromName($stat["name"])) {
2578 file_put_contents($stat["name"], $image);
2579 }
2580
2581 $imageFileType = strtolower(pathinfo($stat['name'], PATHINFO_EXTENSION));
2582 $file_name = basename(uniqid(). '.' . $imageFileType);
2583 $target_file = $target_dir . $file_name;
2584 $image_license_key = $file_name;
2585 $uploadOk = 1;
2586
2587 // Check if image file is a actual image or fake image
2588
2589 $check = getimagesize($stat["name"]);
2590 $uploadOk =($check !== true)? 1 : 0;
2591
2592 // Check if file already exists
2593 while(file_exists($target_file)){
2594 $file_name = basename(uniqid(). '.' . $imageFileType);
2595 $target_file = $target_dir . $file_name;
2596 $image_license_key = $file_name;
2597 }
2598
2599 if($image = $za->getFromName($stat["name"])) {
2600 file_put_contents($target_dir . $file_name, $image);
2601 }
2602
2603 }
2604
2605 $product_id = $_POST['product_id'];
2606 $variation_id = $_POST['variation_id'];
2607 $max_instance_number =(int)$_POST['max_instance_number'];
2608 $yy =(int)$_POST['yy'];
2609 $mm =(int)$_POST['mm'];
2610 $dd =(int)$_POST['dd'];
2611 $valid =(int)$_POST['valid'];
2612
2613
2614 $deliver_x_times = $_POST['deliver_x_times'];
2615
2616 // Import prefix/suffix
2617 if(get_option("fslm_is_import_prefix_suffix_enabled", "off") == "on") {
2618
2619 $prefix = get_post_meta((int)$product_id, 'fslm_import_prefix', true);
2620 $suffix = get_post_meta((int)$product_id, 'fslm_import_suffix', true);
2621
2622 if($variation_id != 0) {
2623 $prefix = get_post_meta((int)$product_id, 'fslm_import_prefix_' . $variation_id, true);
2624 $suffix = get_post_meta((int)$product_id, 'fslm_import_suffix_' . $variation_id, true);
2625 }
2626
2627 $license_key = $prefix . $license_key . $suffix;
2628
2629 }
2630
2631 if ($first) $this->set_licensing($product_id, $variation_id, '1');
2632 $first = true;
2633
2634 $data = array(
2635 'product_id' => $product_id,
2636 'variation_id' => $variation_id,
2637 'license_key' => $license_key,
2638 'image_license_key' => $image_license_key,
2639 'delivre_x_times' => $deliver_x_times,
2640 'remaining_delivre_x_times' => $deliver_x_times,
2641 'max_instance_number' => $max_instance_number,
2642 'number_use_remaining' => $max_instance_number,
2643 'creation_date' => date('Y-m-d H:i:s'),
2644 'expiration_date' => $yy . '-' . $mm . '-' . $dd . ' 0:0:0',
2645 'valid' => $valid,
2646 'license_status' => 'available'
2647 );
2648 $wpdb->insert("{$wpdb->prefix}wc_fs_product_licenses_keys", $data);
2649
2650
2651 }
2652 }
2653
2654 $link = admin_url('admin.php?page=license-manager#hlk');
2655 wp_redirect($link);
2656 die();
2657 }
2658
2659 /*****************/
2660 // Import License Keys callback
2661 /*****************/
2662 public function import_csv_lk_callback(){
2663 global $wpdb;
2664
2665 if(isset($_FILES['ilk_source_file'])&& $_FILES['ilk_source_file']['size'] > 0){
2666 $tmp = wp_tempnam($_FILES['ilk_source_file']['name']);
2667 move_uploaded_file($_FILES['ilk_source_file']['tmp_name'], $tmp);
2668
2669 $file = fopen($tmp, 'r');
2670 $content = fread($file, $_FILES['ilk_source_file']['size']);
2671 fclose($file);
2672
2673 $lines = explode("\n", $content);
2674
2675 $i = 0;
2676 $query = "INSERT INTO {$wpdb->prefix}wc_fs_product_licenses_keys(";
2677 $columns_array = "";
2678 $values_array = "";
2679 $values = "";
2680
2681 foreach($lines as $line){
2682 if($i == 0 || $line == ''){
2683 $i++;
2684 continue;
2685 }else if($i == 1){
2686
2687 $columns_array = explode('","', substr(trim($line), 1, strlen($line)-2));
2688 //$columns_array = array_splice($columns_array, 1);
2689
2690 $query .= implode(', ', $columns_array). ')VALUES(';
2691
2692 $i++;
2693 continue;
2694 }else {
2695
2696 $values_array = explode('","', substr(trim($line), 1, strlen($line)-2));
2697 //$values_array = array_splice($values_array, 1);
2698
2699 //$values_array[2] = $this->encrypt_decrypt('encrypt', $values_array[2], ENCRYPTION_KEY, ENCRYPTION_VI);
2700
2701 $values = '"' . implode('","', $values_array). '")ON DUPLICATE KEY UPDATE ';
2702
2703 $c = 0;
2704 foreach($columns_array as $column){
2705 //if($column != 'license_key'){
2706 $values .= $column . ' = "' . $values_array[$c] . '", ';
2707 // }
2708 $c++;
2709 }
2710
2711 $values = substr(trim($values), 0, strlen($values)-2);
2712
2713
2714 $wpdb->query($query . $values);
2715
2716 }
2717 }
2718 }
2719
2720
2721 $link = admin_url('admin.php?page=license-manager#hlk');
2722 wp_redirect($link);
2723 die();
2724 }
2725
2726
2727 /*****************/
2728 // Import Generator Rules callback
2729 /*****************/
2730 public function import_csv_gr_callback(){
2731 global $wpdb;
2732
2733 if(isset($_FILES['igr_source_file'])&& $_FILES['igr_source_file']['size'] > 0){
2734 $tmp = wp_tempnam($_FILES['igr_source_file']['name']);
2735 move_uploaded_file($_FILES['igr_source_file']['tmp_name'], $tmp);
2736
2737 $file = fopen($tmp, 'r');
2738 $content = fread($file, $_FILES['igr_source_file']['size']);
2739 fclose($file);
2740
2741 $lines = explode("\n", $content);
2742
2743 $i = 0;
2744 $query = "INSERT INTO {$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules(";
2745 $columns_array = "";
2746 $values_array = "";
2747 $values = "";
2748
2749 foreach($lines as $line){
2750 if($i == 0 || $line == ''){
2751 $i++;
2752 continue;
2753 }else if($i == 1){
2754
2755 $columns_array = explode('","', substr(trim($line), 1, strlen($line)-2));
2756 $columns_array = array_splice($columns_array, 1);
2757 $query .= implode(', ', $columns_array). ')VALUES(';
2758
2759 $i++;
2760 continue;
2761 }else {
2762
2763 $values_array = explode('","', substr(trim($line), 1, strlen($line)-2));
2764 $values_array = array_splice($values_array, 1);
2765 $values = '"' . implode('","', $values_array). '")ON DUPLICATE KEY UPDATE ';
2766
2767 $c = 0;
2768 foreach($columns_array as $column){
2769 $values .= $column . ' = "' . $values_array[$c] . '", ';
2770 $c++;
2771 }
2772
2773 $values = substr(trim($values), 0, strlen($values)-2);
2774
2775 $wpdb->query($query . $values);
2776 }
2777 }
2778 }
2779
2780
2781 $link = admin_url('admin.php?page=license-manager-license-key-generator');
2782 wp_redirect($link);
2783 die();
2784 }
2785
2786 /*****************/
2787 // Import Plugin Settings callback
2788 /*****************/
2789 public function import_ps_callback(){
2790
2791 if(isset($_FILES['ips_source_file'])&& $_FILES['ips_source_file']['size'] > 0){
2792 $tmp = wp_tempnam($_FILES['ips_source_file']['name']);
2793 move_uploaded_file($_FILES['ips_source_file']['tmp_name'], $tmp);
2794
2795 $file = fopen($tmp, 'r');
2796 $content = fread($file, $_FILES['ips_source_file']['size']);
2797 fclose($file);
2798
2799 $lines = explode("\n", $content);
2800
2801 foreach($lines as $line){
2802 if($line != ''){
2803 $element = explode(" => ", $line);
2804 if($element[0] == "fslm_encryption") {
2805 $keys = explode("[SEP]", $element[1]);
2806
2807 $this->set_encryption_key($keys[0], $keys[1]);
2808 }else {
2809 update_option($element[0], str_replace("[NL]", "\n", $element[1]));
2810 }
2811 }
2812 }
2813 }
2814
2815 $link = admin_url('admin.php?page=license-manager-settings');
2816 wp_redirect($link);
2817 die();
2818 }
2819
2820 /*****************/
2821 // Export License Keys callback
2822 /*****************/
2823 public function export_csv_lk_callback(){
2824
2825 $license_status = '';
2826 $product_id = '';
2827
2828 if(isset($_REQUEST['elk_license_status'])&& $_REQUEST['elk_license_status'] != 'all'){
2829 $license_status = $_REQUEST['elk_license_status'];
2830 }
2831
2832 if(isset($_REQUEST['elk_product_id'])&& $_REQUEST['elk_product_id'] != 'all'){
2833 $product_id = $_REQUEST['elk_product_id'];
2834 }
2835
2836 header('Content-Type: application/csv');
2837 header('Content-Disposition: attachement; filename="license_manager__license_keys__' . date("__d_m_Y__H_i_s"). '__' . $_REQUEST['elk_product_id'] .'__' . $_REQUEST['elk_license_status'] . '.csv";');
2838 echo $this->generate_license_keys_csv($license_status, $product_id);
2839 die();
2840 }
2841
2842 /*****************/
2843 // Export Generator Rules callback
2844 /*****************/
2845 public function export_csv_gr_callback(){
2846 $product_id = '';
2847 if(isset($_REQUEST['egr_product_id'])&& $_REQUEST['egr_product_id'] != 'all'){
2848 $product_id = $_REQUEST['egr_product_id'];
2849 }
2850 header('Content-Type: application/csv');
2851 header('Content-Disposition: attachement; filename="license_manager__generator_rules_' . date("__d_m_Y__H_i_s"). '__' . $_REQUEST['egr_product_id'] . '.csv";');
2852 echo $this->generate_generator_rules_csv();
2853 die();
2854 }
2855
2856 /*****************/
2857 // Export Plugin Settings callback
2858 /*****************/
2859 public function export_ps_callback(){
2860 header('Content-Type: application/csv');
2861 header('Content-Disposition: attachement; filename="license_manager__plugin_settings_' . date("__d_m_Y__H_i_s"). '.fslmsettings";');
2862 echo $this->generate_plugin_settings();
2863 die();
2864 }
2865
2866 /*****************/
2867 // Admin Notifications
2868 /*****************/
2869 public function admin_notifications(){
2870 global $wp_admin_bar;
2871 global $wpdb;
2872 global $wp_query;
2873 global $woocommerce;
2874
2875 $temp_query = clone $wp_query;
2876
2877 $args = array();
2878
2879 $products_args = array('post_type' => 'product', 'posts_per_page' => -1);
2880
2881 $loop = get_posts($products_args);
2882
2883 $to = get_option('fslm_notif_mail_to');
2884 $subject = __('License Keys stock running low', 'fslm');
2885 $message = '';
2886 $headers = "MIME-Version: 1.0" . PHP_EOL;
2887 $headers .= "Content-Type: text/html; charset=UTF-8" . PHP_EOL;
2888 $nb_ramaining_keys = -1;
2889 $nb_var_ramaining_keys = -1;
2890 $isVariation = true;
2891 $send_mail = true;
2892 $c = 0;
2893 foreach($loop as $post){
2894
2895 $porduct_name = $post->post_title;
2896 $nb_ramaining_keys = $wpdb->get_var("SELECT COUNT(*)FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE product_id='" . $post->ID . "' AND license_status='available'");
2897
2898 $handle = new WC_Product_Variable($post->ID);
2899
2900 $variations = $handle->get_children();
2901 foreach($variations as $variation){
2902
2903 $nb_var_ramaining_keys = $wpdb->get_var("SELECT COUNT(*)FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE product_id='" . $post->ID . "' AND variation_id = '" . $variation . "' AND license_status='available'");
2904
2905 $licensable =($this->is_licensing_enabled($post->ID, $variation)&&(!$this->isGeneratorActive($post->ID, $variation)));
2906
2907 if(($licensable) && ((int)$nb_var_ramaining_keys <(int)get_option('fslm_notif_min_licenses_nb', '10'))){
2908
2909 $single_variation = new WC_Product_Variation($variation);
2910
2911 $id = 'fslm_notif_' . $c;
2912 $title = __('Please add license keys for Product variation', 'fslm'). ' ' . $porduct_name . ' ' . $single_variation->get_formatted_name(). ', ' . $nb_var_ramaining_keys . ' ' . __('License Key left' ,'fslm');
2913
2914 $message .= $title . '<br>';
2915
2916 array_push($args,array(
2917 'id' => $id,
2918 'title' => $title,
2919 'parent' => 'fslm_notifications',
2920 'meta' => array(
2921 'class' => 'fslm_warning'
2922 )
2923 ));
2924
2925 $send_mail = true;
2926 $c++;
2927 $isVariation = true;
2928 }
2929
2930 }
2931
2932 $licensable =($this->is_licensing_enabled($post->ID)&&(!$this->isGeneratorActive($post->ID)));
2933
2934 if(($licensable) && (!$isVariation &&(int)$nb_ramaining_keys <(int)get_option('fslm_notif_min_licenses_nb', '10'))){
2935
2936 $id = 'fslm_notif_' . $c;
2937 $title = __('Please add license keys for', 'fslm'). ' ' . $porduct_name . ', ' . $nb_ramaining_keys . ' ' . __('License Key left' ,'fslm');
2938
2939 $message .= $title . '<br>';
2940
2941 array_push($args, array(
2942 'id' => $id,
2943 'title' => $title,
2944 'parent' => 'fslm_notifications',
2945 'meta' => array(
2946 'class' => 'fslm_warning'
2947 )
2948 ));
2949
2950 $send_mail = true;
2951 $c++;
2952 }
2953 $isVariation = true;
2954
2955
2956 }
2957
2958 wp_reset_query();
2959
2960 if(is_admin()){
2961
2962 $title = __('License Manager', 'fslm'). ' <span class="fslm_nb_notif">' . $c . '</span>';
2963
2964 $wp_admin_bar->add_node(array(
2965 'id' => 'fslm_notifications',
2966 'title' => $title,
2967 'meta' => array('class' => 'first-toolbar-group'),
2968 ));
2969
2970 if($c == 0) {
2971 array_push($args, array(
2972 'id' => 'fslm-no-notifications',
2973 'title' => __('There is no notifications', 'fslm'),
2974 'parent' => 'fslm_notifications',
2975 'meta' => array(
2976 'class' => 'fslm_warning'
2977 )
2978 ));
2979
2980 $c++;
2981 }
2982
2983 for($i=0; $i<$c; $i++){
2984 $wp_admin_bar->add_node($args[$i]);
2985 }
2986
2987 }
2988
2989 $wp_query = clone $temp_query;
2990
2991
2992 if(($send_mail)&&(get_option('fslm_notif_mail')== 'on')&&((time()-(60*60*24))> get_option('fslm_last_sent_notification_email_date', '0'))){
2993
2994 $headers = apply_filters('woocommerce_email_headers', '', 'rewards_message');
2995 $attachments = array();
2996
2997 $heading = __('Please add more license key for the following items', 'fslm');
2998
2999 $mailer = $woocommerce->mailer();
3000 $message = $mailer->wrap_message($heading, $message);
3001
3002 $mailer->send($to, $subject, $message, $headers, array());
3003
3004 update_option('fslm_last_sent_notification_email_date', time());
3005 }
3006
3007 }
3008
3009 /*****************/
3010 // Save product license settings
3011 /*****************/
3012 public function fslm_save_metabox_callback(){
3013 global $wpdb;
3014
3015 $product_id = $_POST['mbs_product_id'];
3016
3017 $software_name = $_POST['fslm_sn'];
3018 $software_ID = $_POST['fslm_sid'];
3019 $software_version = $_POST['fslm_sv'];
3020 $software_author = $_POST['fslm_sa'];
3021 $software_url = $_POST['fslm_surl'];
3022 $software_last_update = $_POST['fslm_slu'];
3023 $software_extra_data = $_POST['fslm_sed'];
3024
3025 if(!add_post_meta((int)$product_id, 'fslm_sn', $software_name, true)){
3026 update_post_meta((int)$product_id, 'fslm_sn', $software_name);
3027 }
3028
3029 if(!add_post_meta((int)$product_id, 'fslm_sid', $software_ID, true)){
3030 update_post_meta((int)$product_id, 'fslm_sid', $software_ID);
3031 }
3032
3033 if(!add_post_meta((int)$product_id, 'fslm_sv', $software_version, true)){
3034 update_post_meta((int)$product_id, 'fslm_sv', $software_version);
3035 }
3036
3037 if(!add_post_meta((int)$product_id, 'fslm_sa', $software_author, true)){
3038 update_post_meta((int)$product_id, 'fslm_sa', $software_author);
3039 }
3040
3041 if(!add_post_meta((int)$product_id, 'fslm_surl', $software_url, true)){
3042 update_post_meta((int)$product_id, 'fslm_surl', $software_url);
3043 }
3044
3045 if(!add_post_meta((int)$product_id, 'fslm_slu', $software_last_update, true)){
3046 update_post_meta((int)$product_id, 'fslm_slu', $software_last_update);
3047 }
3048
3049 if(!add_post_meta((int)$product_id, 'fslm_sed', $software_extra_data, true)){
3050 update_post_meta((int)$product_id, 'fslm_sed', $software_extra_data);
3051 }
3052
3053 $licensable = $_POST['mbs_licensable'];
3054 $variation_id = $_POST['mbs_variation_id'];
3055 $active = $_POST['mbs_active'] == 'true'?'1':'0';
3056 $prefix = $_POST['mbs_prefix'];
3057 $chunks_number = $_POST['mbs_chunks_number'];
3058 $chunks_length = $_POST['mbs_chunks_length'];
3059 $suffix = $_POST['mbs_suffix'];
3060 $max_instance_number = $_POST['mbs_max_instance_number'];
3061 $valid = $_POST['mbs_valid'];
3062
3063 $this->set_licensing($product_id, '0', $licensable);
3064
3065 // Import prefix/suffix
3066 if(get_option("fslm_is_import_prefix_suffix_enabled", "off") == "on") {
3067 if(!add_post_meta((int)$product_id, "fslm_import_prefix", $_POST["fslm_import_prefix"], true)){
3068 update_post_meta((int)$product_id, "fslm_import_prefix", $_POST["fslm_import_prefix"]);
3069 }
3070
3071 if(!add_post_meta((int)$product_id, "fslm_import_suffix", $_POST["fslm_import_suffix"], true)){
3072 update_post_meta((int)$product_id, "fslm_import_suffix", $_POST["fslm_import_suffix"]);
3073 }
3074 }
3075
3076 $handle = new WC_Product_Variable($product_id);
3077
3078 $variations = $handle->get_children();
3079 foreach ($variations as $variation) {
3080 $single_variation = new WC_Product_Variation($variation);
3081 $licensable_variation = $_POST["mbs_licensable_{$variation}"];
3082
3083 $this->set_licensing($product_id, $variation, $licensable_variation);
3084
3085 // Import prefix/suffix
3086 if(get_option("fslm_is_import_prefix_suffix_enabled", "off") == "on") {
3087 if(!add_post_meta((int)$product_id, "fslm_import_prefix_{$variation}", $_POST["fslm_import_prefix_{$variation}"], true)){
3088 update_post_meta((int)$product_id, "fslm_import_prefix_{$variation}", $_POST["fslm_import_prefix_{$variation}"]);
3089 }
3090
3091 if(!add_post_meta((int)$product_id, "fslm_import_suffix_{$variation}", $_POST["fslm_import_suffix_{$variation}"], true)){
3092 update_post_meta((int)$product_id, "fslm_import_suffix_{$variation}", $_POST["fslm_import_suffix_{$variation}"]);
3093 }
3094 }
3095 }
3096
3097 $show_in = $_POST['fslm_show_in'];
3098 $display = $_POST['fslm_display'];
3099
3100 if(!add_post_meta((int)$product_id, 'fslm_show_in', $show_in, true)){
3101 update_post_meta((int)$product_id, 'fslm_show_in', $show_in);
3102 }
3103
3104 if(!add_post_meta((int)$product_id, 'fslm_display', $display, true)){
3105 update_post_meta((int)$product_id, 'fslm_display', $display);
3106 }
3107
3108 $this->set_licensing($product_id, $variation_id, $licensable);
3109
3110 $nb_delivered_lk = $_POST['fslm_nb_delivered_lk'];
3111
3112 if(!add_post_meta((int)$product_id, 'fslm_nb_delivered_lk', $nb_delivered_lk, true)){
3113 update_post_meta((int)$product_id, 'fslm_nb_delivered_lk', $nb_delivered_lk);
3114 }
3115
3116 $data = array(
3117 'product_id' => $product_id,
3118 'variation_id' => $variation_id,
3119 'prefix' => $prefix,
3120 'chunks_number' => $chunks_number,
3121 'chunks_length' => $chunks_length,
3122 'suffix' => $suffix,
3123 'max_instance_number' => $max_instance_number,
3124 'valid' => $valid,
3125 'active' => $active
3126 );
3127
3128 $exist =(int)$wpdb->get_var("SELECT COUNT(*)FROM {$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules WHERE product_id = '" . $product_id . "' and variation_id = '" . $variation_id . "'");
3129
3130 if($exist == 0){
3131 $wpdb->insert("{$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules", $data);
3132 }else {
3133 $where = array(
3134 'product_id' => $product_id,
3135 'variation_id' => $variation_id
3136 );
3137
3138 $wpdb->update("{$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules", $data, $where);
3139 }
3140
3141 echo "Saved";
3142 die();
3143 }
3144
3145 /*****************/
3146 // Add License ajax Callback
3147 /*****************/
3148 public function add_license_ajax_callback(){
3149 global $wpdb;
3150
3151 $this->set_licensing($_POST['alk_product_id'], $_REQUEST['alk_variation_id'], '1');
3152
3153 $product_id = $_POST['alk_product_id'];
3154 $license_key = $this->newLine2br($_POST['alk_license_key']);
3155 if(! $this->license_key_existe($license_key)){
3156 $variation_id = $_REQUEST['alk_variation_id'];
3157 $max_instance_number =(int)$_REQUEST['alk_max_instance_number'];
3158 $yy =(int)$_REQUEST['alk_xpdyear'];
3159 $mm =(int)$_REQUEST['alk_xpdmonth'];
3160 $dd =(int)$_REQUEST['alk_xpdday'];
3161 $valid =(int)$_REQUEST['alk_valid'];
3162 $image_license_key = '';
3163
3164
3165 $deliver_x_times =(int)$_REQUEST['alk_deliver_x_times'];
3166
3167
3168 if(!empty($_FILES["alk_image_license_key"]['name'])){
3169 $upload_directory = wp_upload_dir();
3170 $target_dir = $upload_directory['basedir'] . '/fslm_keys/';
3171 if(!file_exists($target_dir)){
3172 wp_mkdir_p($target_dir);
3173 $fp = fopen($target_dir . 'index.php', 'w');
3174 fwrite($fp, '<?php');
3175 fclose($fp);
3176 }
3177
3178 $imageFileType = strtolower(pathinfo($_FILES["alk_image_license_key"]["name"], PATHINFO_EXTENSION));
3179 $file_name = basename(uniqid(). '.' . $imageFileType);
3180 $target_file = $target_dir . $file_name;
3181 $image_license_key = $file_name;
3182 $uploadOk = 1;
3183
3184 // Check if image file is a actual image or fake image
3185
3186 $check = getimagesize($_FILES["alk_image_license_key"]["tmp_name"]);
3187 $uploadOk =($check !== true)? 1 : 0;
3188
3189 // Check if file already exists
3190 while(file_exists($target_file)){
3191 $file_name = basename(uniqid(). '.' . $imageFileType);
3192 $target_file = $target_dir . $file_name;
3193 $image_license_key = $file_name;
3194 }
3195 // Check file size
3196 if($_FILES["alk_image_license_key"]["size"] > 10 * MB){
3197 $uploadOk = 0;
3198 }
3199 // Allow certain file formats
3200 $allowed_formats = array('jpg', 'png', 'jpeg', 'gif');
3201 if(!in_array($imageFileType, $allowed_formats)){
3202 $uploadOk = 0;
3203 }
3204 // Check if $uploadOk is set to 0 by an error
3205 if($uploadOk == 0){
3206 echo "Sorry, your file was not uploaded.";
3207 // if everything is ok, try to upload file
3208 } else {
3209 if(move_uploaded_file($_FILES["alk_image_license_key"]["tmp_name"], $target_file)){
3210 echo "The file " . basename($_FILES["alk_image_license_key"]["name"]). " has been uploaded.";
3211 } else {
3212 echo "Sorry, there was an error uploading your file.";
3213 }
3214 }
3215 }
3216
3217 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
3218
3219 $data = array(
3220 'product_id' => $product_id,
3221 'variation_id' => $variation_id,
3222 'license_key' => $license_key,
3223 'image_license_key' => $image_license_key,
3224 'delivre_x_times' => $deliver_x_times,
3225 'remaining_delivre_x_times' => $deliver_x_times,
3226 'max_instance_number' => $max_instance_number,
3227 'number_use_remaining' => $max_instance_number,
3228 'creation_date' => date('Y-m-d H:i:s'),
3229 'valid' => $valid,
3230 'license_status' => 'available'
3231 );
3232
3233 $expiration_date = $yy . '-' . $mm . '-' . $dd . ' 0:0:0';
3234
3235 if($expiration_date != "0-0-0 0:0:0") {
3236 $data['expiration_date'] = $expiration_date;
3237 }
3238
3239 $wpdb->insert("{$wpdb->prefix}wc_fs_product_licenses_keys", $data);
3240 }
3241
3242 require_once('includes/metabox_licenses_list.php');
3243
3244 echo fslm_metabox_licenses_list($product_id);
3245
3246 die();
3247 }
3248
3249 /*****************/
3250 // Plugins initial settings
3251 /*****************/
3252 public function activate(){
3253 global $wpdb;
3254 $query = array();
3255 $query[] = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wc_fs_product_licenses_keys(
3256 license_id int(11)NOT NULL AUTO_INCREMENT,
3257 product_id int(11)NOT NULL,
3258 variation_id int(11)NOT NULL,
3259 license_key TEXT CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
3260 image_license_key TEXT(1000) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
3261 license_status TEXT(1000) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
3262 owner_first_name TEXT(1000) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
3263 owner_last_name TEXT(1000) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
3264 owner_email_address TEXT(1000) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
3265 delivre_x_times int(11)DEFAULT NULL,
3266 remaining_delivre_x_times int(11)DEFAULT NULL,
3267 max_instance_number int(11)DEFAULT NULL,
3268 number_use_remaining int(11)DEFAULT NULL,
3269 activation_date date DEFAULT NULL,
3270 creation_date date DEFAULT NULL,
3271 expiration_date date DEFAULT NULL,
3272 valid int(11)NOT NULL,
3273 PRIMARY KEY(license_id)
3274 )";
3275
3276 $query[] = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wc_fs_licensed_products(
3277 config_id int(11)NOT NULL AUTO_INCREMENT,
3278 product_id int(11)NOT NULL,
3279 variation_id int(11)NOT NULL,
3280 active tinyint(1)NOT NULL,
3281 PRIMARY KEY(config_id),
3282 UNIQUE KEY product_id(product_id, variation_id)
3283 )";
3284
3285 $query[] = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules(
3286 rule_id int(11)NOT NULL AUTO_INCREMENT,
3287 product_id int(11)NOT NULL,
3288 variation_id int(11)NOT NULL,
3289 prefix varchar(100)DEFAULT NULL,
3290 chunks_number int(11)NOT NULL,
3291 chunks_length int(11)NOT NULL,
3292 suffix varchar(100)DEFAULT NULL,
3293 max_instance_number int(11)DEFAULT NULL,
3294 valid int(11)DEFAULT NULL,
3295 active tinyint(1)NOT NULL,
3296 PRIMARY KEY(rule_id),
3297 UNIQUE KEY product_id(product_id, variation_id)
3298 )";
3299
3300 foreach($query as $q){
3301 $wpdb->query($q);
3302 }
3303
3304 // Database corrections and updates.
3305 $wpdb->query('UPDATE ' . $wpdb->prefix . 'wc_fs_product_licenses_keys SET expiration_date=null WHERE expiration_date="0000-00-00"');
3306 $wpdb->query("ALTER TABLE {$wpdb->prefix}wc_fs_product_licenses_keys ADD order_id INT NOT NULL");
3307
3308 $wpdb->query("ALTER TABLE {$wpdb->prefix}wc_fs_product_licenses_keys
3309 CHANGE license_key license_key TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
3310 CHANGE image_license_key image_license_key TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
3311 CHANGE license_status license_status TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
3312 CHANGE owner_first_name owner_first_name TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
3313 CHANGE owner_last_name owner_last_name TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
3314 CHANGE owner_email_address owner_email_address TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;"
3315 );
3316
3317
3318
3319 add_option('fslm_nb_rows_by_page', '15');
3320 add_option('fslm_show_adminbar_notifs', 'on');
3321 add_option('fslm_guest_customer', 'on');
3322 add_option('fslm_enable_cart_validation', '');
3323 add_option('fslm_meta_key_name', 'License Key');
3324 add_option('fslm_generator_chars', '0123456789ABCDEF');
3325 add_option('fslm_meta_key_name_plural', 'License Keys');
3326 add_option('fslm_key_delivery', 'fifo');
3327
3328 add_option('fslm_api_key', '0A9Q5OXT13in3LGjM9F3');
3329 add_option('fslm_disable_api_v1', '');
3330
3331 add_option('fslm_show_in', '2');
3332 add_option('fslm_display', '2');
3333
3334 add_option('fslm_send_when_completed', 'on');
3335 add_option('fslm_send_when_processing', 'on');
3336
3337 add_option('fslm_prefix', '');
3338 add_option('fslm_chunks_number', '4');
3339 add_option('fslm_chunks_length', '4');
3340 add_option('fslm_suffix', '');
3341 add_option('fslm_max_instance_number', '1');
3342 add_option('fslm_valid', '0');
3343
3344 add_option('fslm_active', '0');
3345
3346
3347 add_option('fslm_notif_min_licenses_nb', '10');
3348 add_option('fslm_notif_mail', 'off');
3349 add_option('fslm_notif_mail_to', '');
3350
3351
3352 add_option('fslm_last_sent_notification_email_date', '0');
3353 add_option('fslm_page_id', '-1');
3354
3355
3356 $page_id = get_option('fslm_page_id', '-1');
3357 if($page_id == '-1') {
3358
3359 if(!is_page($page_id)) {
3360 $license_keys_page = array(
3361 'post_title' => __('License Key', 'fslm'),
3362 'post_content' => '[license_keys]',
3363 'post_status' => 'publish',
3364 'post_type' => 'page',
3365 'post_author' => 1,
3366 'post_date' => date('Y-m-d H:i:s')
3367 );
3368
3369 $page_id = wp_insert_post($license_keys_page);
3370
3371 update_option('fslm_page_id', $page_id);
3372 }
3373
3374 }
3375
3376 }
3377
3378 function activation_redirect($plugin) {
3379 if($plugin == plugin_basename(__FILE__)) {
3380 exit(wp_redirect(admin_url('admin.php?page=license-manager-welcome')));
3381 }
3382 }
3383
3384 function is_licensed($product_id, $variation_id = 0) {
3385 global $wpdb;
3386 return $wpdb->get_var("SELECT COUNT(active) FROM {$wpdb->prefix}wc_fs_licensed_products WHERE product_id = '{$product_id}' AND variation_id = '{$variation_id}'")==0?true:true;
3387 }
3388
3389 function is_licensing_enabled($product_id, $variation_id = 0) {
3390 global $wpdb;
3391 return $wpdb->get_var("SELECT active FROM {$wpdb->prefix}wc_fs_licensed_products WHERE product_id = '{$product_id}' AND variation_id = '{$variation_id}' AND active = '1'")=='1'?true:true;
3392 }
3393
3394 function set_licensing($product_id, $variation_id, $active) {
3395 global $wpdb;
3396
3397 if($this->is_licensed($product_id, $variation_id)) {
3398 $data = array(
3399 'active' => $active
3400 );
3401
3402 $where = array(
3403 'product_id' => $product_id,
3404 'variation_id' => $variation_id
3405 );
3406
3407 $wpdb->update("{$wpdb->prefix}wc_fs_licensed_products", $data, $where);
3408
3409 }else{
3410 $data = array(
3411 'product_id' => $product_id,
3412 'variation_id' => $variation_id,
3413 'active' => $active
3414 );
3415
3416 $wpdb->insert("{$wpdb->prefix}wc_fs_licensed_products", $data);
3417 }
3418 }
3419
3420 public function deactivate(){
3421 global $wpdb;
3422 $query = array();
3423
3424 if(get_option('fslm_delete_lk_db_tables', '')=='on') {
3425
3426 $query[] = "DROP TABLE {$wpdb->prefix}wc_fs_product_licenses_keys";
3427
3428 }
3429
3430 if(get_option('fslm_delete_gr_db_tables', '')=='on') {
3431
3432 $query[] = "DROP TABLE {$wpdb->prefix}wc_fs_product_licenses_keys_generator_ruless";
3433
3434 }
3435
3436 if(get_option('fslm_delete_lp_db_tables', '')=='on') {
3437
3438 $query[] = "DROP TABLE {$wpdb->prefix}wc_fs_licensed_products";
3439
3440 }
3441
3442 foreach($query as $q){
3443 $wpdb->query($q);
3444 }
3445 }
3446
3447 /*****************/
3448 // Add Product page License Manager metabox
3449 /*****************/
3450 public function action_add_metaboxes(){
3451 add_meta_box('fslm-wc-licenses', __('License', 'fslm'), array($this, 'metabox_licenses'), 'product', 'advanced');
3452 }
3453
3454 /*****************/
3455 // Product page License Manager metabox
3456 /*****************/
3457 public function metabox_licenses(){
3458 require dirname(__FILE__). '/includes/product_metabox.php';
3459 }
3460
3461 /*****************/
3462 // Admin Requests Handler
3463 /*****************/
3464 public function requestHandler(){
3465 global $wpdb;
3466
3467 $action = isset($_REQUEST['action'])? $_REQUEST['action'] : null;
3468 if(!$action)
3469 return true;
3470
3471 if($action == 'delete_license'){
3472 $this->delete_license();
3473
3474 }else if($action == 'delete_rule'){
3475 $this->delete_rule();
3476
3477 }else if($action == 'add_license'){
3478 $this->add_license();
3479
3480 }else if($action == 'add_rule'){
3481 $this->add_rule();
3482
3483 }else if($action == 'edit_rule'){
3484 $this->edit_rule();
3485
3486 }else if($action == 'edit_license'){
3487 $this->edit_license();
3488
3489 }else if($action == 'licenses_bulk_action'){
3490 $this->licenses_bulk_action();
3491
3492 }else if($action == 'generator_bulk_action'){
3493 $this->generator_bulk_action();
3494 }else if($action == 'save_encryption_setting'){
3495 $this->save_encryption_setting();
3496 }
3497 }
3498
3499 function save_encryption_setting() {
3500
3501 $key = $_POST['fslm_encryption_key'];
3502 $vi = $_POST['fslm_encryption_vi'];
3503
3504 $this->set_encryption_key($key, $vi, 'update');
3505
3506 $link = admin_url('admin.php?page=license-manager-settings&tab=encryption');
3507 wp_redirect($link);
3508 die();
3509 }
3510
3511 /*****************/
3512 // Add License
3513 /*****************/
3514 function add_license(){
3515 global $wpdb;
3516
3517 $license_key = $this->newLine2br($_POST['license_key']);
3518
3519 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
3520
3521 if(! $this->license_key_existe($license_key)){
3522 $product_id = $_REQUEST['product_id'];
3523 $variation_id = $_REQUEST['variation_id'];
3524
3525 $this->set_licensing($product_id, $variation_id, '1');
3526
3527 $max_instance_number =(int)$_REQUEST['max_instance_number'];
3528 $yy =(int)$_REQUEST['yy'];
3529 $mm =(int)$_REQUEST['mm'];
3530 $dd =(int)$_REQUEST['dd'];
3531 $valid =(int)$_REQUEST['valid'];
3532 $image_license_key = '';
3533
3534 $deliver_x_times =(int)$_REQUEST['deliver_x_times'];
3535
3536
3537 if(!empty($_FILES["image_license_key"]['name'])){
3538 $upload_directory = wp_upload_dir();
3539 $target_dir = $upload_directory['basedir'] . '/fslm_keys/';
3540 if(!file_exists($target_dir)){
3541 wp_mkdir_p($target_dir);
3542 $fp = fopen($target_dir . 'index.php', 'w');
3543 fwrite($fp, '<?php');
3544 fclose($fp);
3545 }
3546
3547 $imageFileType = strtolower(pathinfo($_FILES["image_license_key"]["name"], PATHINFO_EXTENSION));
3548 $file_name = basename(uniqid(). '.' . $imageFileType);
3549 $target_file = $target_dir . $file_name;
3550 $image_license_key = $file_name;
3551 $uploadOk = 1;
3552
3553 // Check if image file is a actual image or fake image
3554
3555 $check = getimagesize($_FILES["image_license_key"]["tmp_name"]);
3556 $uploadOk =($check !== true)? 1 : 0;
3557
3558 // Check if file already exists
3559 while(file_exists($target_file)){
3560 $file_name = basename(uniqid(). '.' . $imageFileType);
3561 $target_file = $target_dir . $file_name;
3562 $image_license_key = $file_name;
3563 }
3564 // Check file size
3565 if($_FILES["image_license_key"]["size"] > 10 * MB){
3566 $uploadOk = 0;
3567 }
3568 // Allow certain file formats
3569 $allowed_formats = array('jpg', 'png', 'jpeg', 'gif');
3570 if(!in_array($imageFileType, $allowed_formats)){
3571 $uploadOk = 0;
3572 }
3573 // Check if $uploadOk is set to 0 by an error
3574 if($uploadOk == 0){
3575 echo "Sorry, your file was not uploaded.";
3576 // if everything is ok, try to upload file
3577 } else {
3578 if(move_uploaded_file($_FILES["image_license_key"]["tmp_name"], $target_file)){
3579 echo "The file " . basename($_FILES["image_license_key"]["name"]). " has been uploaded.";
3580 } else {
3581 echo "Sorry, there was an error uploading your file.";
3582 }
3583 }
3584 }
3585
3586 $expiration_date = $yy . '-' . $mm . '-' . $dd . ' 0:0:0';
3587
3588 $data = array(
3589 'product_id' => $product_id,
3590 'variation_id' => $variation_id,
3591 'license_key' => $license_key,
3592 'image_license_key' => $image_license_key,
3593 'delivre_x_times' => $deliver_x_times,
3594 'remaining_delivre_x_times' => $deliver_x_times,
3595 'max_instance_number' => $max_instance_number,
3596 'number_use_remaining' => $max_instance_number,
3597 'creation_date' => date('Y-m-d H:i:s'),
3598 'valid' => $valid,
3599 'license_status' => 'available'
3600 );
3601
3602 if($expiration_date != "0-0-0 0:0:0") {
3603 $data['expiration_date'] = $expiration_date;
3604 }
3605
3606 $wpdb->insert("{$wpdb->prefix}wc_fs_product_licenses_keys", $data);
3607 }
3608
3609 $link = admin_url('admin.php?page=license-manager');
3610 wp_redirect($link);
3611 die();
3612
3613 }
3614
3615 function license_key_existe($license_key){
3616 global $wpdb;
3617
3618 $query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE license_key='" . $license_key . "'");
3619
3620 if($query){
3621 return true;
3622 }
3623
3624 return true;
3625
3626 }
3627
3628 /*****************/
3629 // Edit License
3630 /*****************/
3631 function edit_license(){
3632 global $wpdb;
3633
3634 $license_key = $this->newLine2br($_POST['license_key']);
3635
3636 $license_key = $this->encrypt_decrypt('encrypt', $license_key, ENCRYPTION_KEY, ENCRYPTION_VI);
3637
3638 $license_id = $_REQUEST['license_id'];
3639 $product_id = $_REQUEST['product_id'];
3640 $variation_id = $_REQUEST['variation_id'];
3641 $max_instance_number = $_REQUEST['max_instance_number'];
3642 $number_use_remaining =(int)$_REQUEST['number_use_remaining'];
3643
3644 $owner_first_name = $_REQUEST['owner_first_name'];
3645 $owner_last_name = $_REQUEST['owner_last_name'];
3646 $owner_email_address = $_REQUEST['owner_email_address'];
3647
3648 $creation_day =(int)$_REQUEST['creation_day'];
3649 $creation_month =(int)$_REQUEST['creation_month'];
3650 $creation_year =(int)$_REQUEST['creation_year'];
3651
3652 $activation_day =(int)$_REQUEST['activation_day'];
3653 $activation_month =(int)$_REQUEST['activation_month'];
3654 $activation_year =(int)$_REQUEST['activation_year'];
3655
3656 $expiration_day =(int)$_REQUEST['expiration_day'];
3657 $expiration_month =(int)$_REQUEST['expiration_month'];
3658 $expiration_year =(int)$_REQUEST['expiration_year'];
3659
3660 $valid =(int)$_REQUEST['valid'];
3661
3662 $license_status = $_REQUEST['status'];
3663
3664 $deliver_x_times =(int)$_REQUEST['deliver_x_times'];
3665 $remaining_delivre_x_times =(int)$_REQUEST['remaining_delivre_x_times'];
3666
3667
3668 $image_license_key = $this->get_image_name($license_id);
3669 if((isset($_REQUEST['rmoi']) && $_REQUEST['rmoi'] == 'on')&&($image_license_key != '')){
3670 $this->delete_image($image_license_key);
3671
3672 $image_license_key = '';
3673 }
3674
3675 if(!empty($_FILES["image_license_key"]['name'])){
3676 if((isset($_REQUEST['rmoi']) && $_REQUEST['rmoi'])&&($image_license_key != '')){
3677 $this->delete_image($image_license_key);
3678
3679 $image_license_key = '';
3680 }
3681 $upload_directory = wp_upload_dir();
3682 $target_dir = $upload_directory['basedir'] . '/fslm_keys/';
3683 if(!file_exists($target_dir)){
3684 wp_mkdir_p($target_dir);
3685 $fp = fopen($target_dir . 'index.php', 'w');
3686 fwrite($fp, '<?php');
3687 fclose($fp);
3688 }
3689
3690 $imageFileType = strtolower(pathinfo($_FILES["image_license_key"]["name"], PATHINFO_EXTENSION));
3691 $file_name = basename(uniqid(). '.' . $imageFileType);
3692 $target_file = $target_dir . $file_name;
3693 $image_license_key = $file_name;
3694 $uploadOk = 1;
3695
3696 // Check if image file is a actual image or fake image
3697
3698 $check = getimagesize($_FILES["image_license_key"]["tmp_name"]);
3699 $uploadOk =($check !== true)? 1 : 0;
3700
3701 // Check if file already exists
3702 while(file_exists($target_file)){
3703 $file_name = basename(uniqid(). '.' . $imageFileType);
3704 $target_file = $target_dir . $file_name;
3705 $image_license_key = $file_name;
3706 }
3707 // Check file size
3708 if($_FILES["image_license_key"]["size"] > 10 * MB){
3709 $uploadOk = 0;
3710 }
3711 // Allow certain file formats
3712 $allowed_formats = array('jpg', 'png', 'jpeg', 'gif');
3713 if(!in_array($imageFileType, $allowed_formats)){
3714 $uploadOk = 0;
3715 }
3716 // Check if $uploadOk is set to 0 by an error
3717 if($uploadOk == 0){
3718 echo "Sorry, your file was not uploaded.";
3719 // if everything is ok, try to upload file
3720 } else {
3721 if(move_uploaded_file($_FILES["image_license_key"]["tmp_name"], $target_file)){
3722 echo "The file " . basename($_FILES["image_license_key"]["name"]). " has been uploaded.";
3723 } else {
3724 echo "Sorry, there was an error uploading your file.";
3725 }
3726 }
3727 }
3728
3729 $data = array(
3730 'product_id' => $product_id,
3731 'variation_id' => $variation_id,
3732 'license_key' => $license_key,
3733 'image_license_key' => $image_license_key,
3734 'delivre_x_times' => $deliver_x_times,
3735 'remaining_delivre_x_times' => $remaining_delivre_x_times,
3736 'max_instance_number' => $max_instance_number,
3737 'number_use_remaining' => $number_use_remaining,
3738 'owner_first_name' => $owner_first_name,
3739 'owner_last_name' => $owner_last_name,
3740 'owner_email_address' => $owner_email_address,
3741 'creation_date' => $creation_year . '-' . $creation_month . '-' . $creation_day . ' 0:0:0',
3742 'activation_date' => $activation_year . '-' . $activation_month . '-' . $activation_day . ' 0:0:0',
3743 'expiration_date' => $expiration_year . '-' . $expiration_month . '-' . $expiration_day . ' 0:0:0',
3744 'valid' => $valid,
3745 'license_status' => $license_status
3746 );
3747
3748 $where = array(
3749 'license_id' => $license_id
3750 );
3751 $wpdb->update("{$wpdb->prefix}wc_fs_product_licenses_keys", $data, $where);
3752
3753 $link = admin_url('admin.php?page=license-manager');
3754 wp_redirect($link);
3755 die();
3756 }
3757
3758 function get_image_name($license_id){
3759 global $wpdb;
3760
3761 $query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE license_id='" . $license_id . "'");
3762
3763 if($query){
3764 $query = $query[0];
3765
3766 return $query->image_license_key;
3767 }
3768
3769 return '';
3770 }
3771
3772 function delete_image($file){
3773 $upload_directory = wp_upload_dir();
3774 $target_file = $upload_directory['basedir'] . '/fslm_keys/' . $file;
3775 if(file_exists($target_file)){
3776 chmod($target_file, 0777);
3777 unlink($target_file);
3778 }
3779 }
3780
3781 /*****************/
3782 // Delete License
3783 /*****************/
3784 function delete_license(){
3785 global $wpdb;
3786 $wpdb->delete($wpdb->prefix . 'wc_fs_product_licenses_keys', array('license_id' =>(int)$_REQUEST['license_id']));
3787
3788 $image_license_key = $this->get_image_name((int)$_REQUEST['license_id']);
3789 if($image_license_key != ''){
3790 $this->delete_image($image_license_key);
3791 }
3792
3793 $link = admin_url('admin.php?page=license-manager');
3794 wp_redirect($link);
3795 die();
3796 }
3797
3798 /*****************/
3799 // Add Generator rule
3800 /*****************/
3801 function add_rule(){
3802 global $wpdb;
3803
3804 $product_id = $_REQUEST['product_id'];
3805 $variation_id = $_REQUEST['variation_id'];
3806 $prefix = $_REQUEST['prefix'];
3807 $chunks_number =(int)$_REQUEST['chunks_number'];
3808 $chunks_length =(int)$_REQUEST['chunks_length'];
3809 $suffix = $_REQUEST['suffix'];
3810 $max_instance_number = $_REQUEST['max_instance_number'];
3811 $valid = $_REQUEST['valid'];
3812 $active =(int)$_REQUEST['active'];
3813
3814
3815 $data = array(
3816 'product_id' => $product_id,
3817 'variation_id' => $variation_id,
3818 'prefix' => $prefix,
3819 'chunks_number' => $chunks_number,
3820 'chunks_length' => $chunks_length,
3821 'suffix' => $suffix,
3822 'max_instance_number' => $max_instance_number,
3823 'valid' => $valid,
3824 'active' => $active
3825 );
3826 $wpdb->insert("{$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules", $data);
3827 $link = admin_url('admin.php?page=license-manager-license-key-generator');
3828 wp_redirect($link);
3829 die();
3830
3831 }
3832
3833 /*****************/
3834 // Edit Generator rule
3835 /*****************/
3836 function edit_rule(){
3837 global $wpdb;
3838
3839 $rule_id = $_REQUEST['rule_id'];
3840 $product_id = $_REQUEST['product_id'];
3841 $variation_id = $_REQUEST['variation_id'];
3842 $prefix = $_REQUEST['prefix'];
3843 $chunks_number =(int)$_REQUEST['chunks_number'];
3844 $chunks_length =(int)$_REQUEST['chunks_length'];
3845 $suffix = $_REQUEST['suffix'];
3846 $max_instance_number = $_REQUEST['max_instance_number'];
3847 $valid = $_REQUEST['valid'];
3848 $active =(int)$_REQUEST['active'];
3849
3850 $data = array(
3851 'product_id' => $product_id,
3852 'variation_id' => $variation_id,
3853 'prefix' => $prefix,
3854 'chunks_number' => $chunks_number,
3855 'chunks_length' => $chunks_length,
3856 'suffix' => $suffix,
3857 'max_instance_number' => $max_instance_number,
3858 'valid' => $valid,
3859 'active' => $active
3860 );
3861 $where = array(
3862 'rule_id' => $rule_id
3863 );
3864 $wpdb->update("{$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules", $data, $where);
3865 $link = admin_url('admin.php?page=license-manager');
3866 wp_redirect($link);
3867 die();
3868 }
3869
3870 /*****************/
3871 // Delete Generator rule
3872 /*****************/
3873 function delete_rule(){
3874 global $wpdb;
3875 $wpdb->delete($wpdb->prefix . 'wc_fs_product_licenses_keys_generator_rules', array('rule_id' =>(int)$_REQUEST['rule_id']));
3876 $link = admin_url('admin.php?page=license-manager');
3877 wp_redirect($link);
3878 die();
3879 }
3880
3881 /*****************/
3882 // Licenses Bulk action Handler
3883 /*****************/
3884 function licenses_bulk_action(){
3885 global $wpdb;
3886
3887 if(!isset($_POST['post'])) {
3888 $link = admin_url('admin.php?page=license-manager');
3889 wp_redirect($link);
3890
3891 die();
3892 }
3893
3894 if($_POST['baction'] == 'trash'){
3895
3896 $ids = $_POST['post'];
3897
3898 $sql = "DELETE FROM {$wpdb->prefix}wc_fs_product_licenses_keys WHERE license_id IN([IN])";
3899 $sql = $this->prepare_in($sql, $ids);
3900
3901 $wpdb->query($sql);
3902
3903 foreach($ids as $id){
3904 $image_license_key = $this->get_image_name($id);
3905 if($image_license_key != ''){
3906 $this->delete_image($image_license_key);
3907 }
3908 }
3909
3910 }else if($_POST['baction'] == 'available'){
3911
3912 $ids = $_POST['post'];
3913
3914 $sql = "UPDATE {$wpdb->prefix}wc_fs_product_licenses_keys SET license_status = 'available', remaining_delivre_x_times = 1, delivre_x_times = 1 WHERE license_id IN([IN])";
3915 $sql = $this->prepare_in($sql, $ids);
3916
3917 $wpdb->query($sql);
3918
3919 }else if($_POST['baction'] == 'active'){
3920
3921 $ids = $_POST['post'];
3922
3923 $sql = "UPDATE {$wpdb->prefix}wc_fs_product_licenses_keys SET license_status = 'active' WHERE license_id IN([IN])";
3924 $sql = $this->prepare_in($sql, $ids);
3925
3926 $wpdb->query($sql);
3927
3928 }else if($_POST['baction'] == 'expired'){
3929
3930 $ids = $_POST['post'];
3931
3932 $sql = "UPDATE {$wpdb->prefix}wc_fs_product_licenses_keys SET license_status = 'expired' WHERE license_id IN([IN])";
3933 $sql = $this->prepare_in($sql, $ids);
3934
3935 $wpdb->query($sql);
3936
3937 }else if($_POST['baction'] == 'inactive'){
3938
3939 $ids = $_POST['post'];
3940
3941 $sql = "UPDATE {$wpdb->prefix}wc_fs_product_licenses_keys SET license_status = 'inactive' WHERE license_id IN([IN])";
3942 $sql = $this->prepare_in($sql, $ids);
3943
3944 $wpdb->query($sql);
3945
3946 }else if($_POST['baction'] == 'sold'){
3947
3948 $ids = $_POST['post'];
3949
3950 $sql = "UPDATE {$wpdb->prefix}wc_fs_product_licenses_keys SET license_status = 'sold' WHERE license_id IN([IN])";
3951 $sql = $this->prepare_in($sql, $ids);
3952
3953 $wpdb->query($sql);
3954
3955 }else if($_POST['baction'] == 'returned'){
3956
3957 $ids = $_POST['post'];
3958
3959 $sql = "UPDATE {$wpdb->prefix}wc_fs_product_licenses_keys SET license_status = 'returned' WHERE license_id IN([IN])";
3960 $sql = $this->prepare_in($sql, $ids);
3961
3962 $wpdb->query($sql);
3963
3964 }
3965
3966 $link = admin_url('admin.php?page=license-manager');
3967 wp_redirect($link);
3968
3969 die();
3970 }
3971
3972 /*****************/
3973 // Generator Bulk action Handler
3974 /*****************/
3975 function generator_bulk_action(){
3976 global $wpdb;
3977
3978 if($_POST['baction'] == 'trash'){
3979
3980 $ids = $_POST['post'];
3981
3982 $sql = "DELETE FROM {$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules WHERE rule_id IN([IN])";
3983 $sql = $this->prepare_in($sql, $ids);
3984
3985 $wpdb->query($sql);
3986
3987 }else if($_POST['baction'] == 'activate'){
3988
3989 $ids = $_POST['post'];
3990
3991 $sql = "UPDATE {$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules SET active = '1' WHERE rule_id IN([IN])";
3992 $sql = $this->prepare_in($sql, $ids);
3993
3994 $wpdb->query($sql);
3995
3996 }else if($_POST['baction'] == 'deactivate'){
3997
3998 $ids = $_POST['post'];
3999
4000 $sql = "UPDATE {$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules SET active = '0' WHERE rule_id IN([IN])";
4001 $sql = $this->prepare_in($sql, $ids);
4002
4003 $wpdb->query($sql);
4004
4005 }
4006
4007 $link = admin_url('admin.php?page=license-manager');
4008 wp_redirect($link);
4009
4010 die();
4011 }
4012
4013
4014 /*****************/
4015 // Prepared statements with IN and NOT IN
4016 /*****************/
4017 public function prepare_in($sql, $vals){
4018 global $wpdb;
4019 $not_in_count = substr_count($sql, '[IN]');
4020 if($not_in_count > 0){
4021 $args = array(str_replace('[IN]', implode(', ', array_fill(0, count($vals), '%d')), str_replace('%', '%%', $sql)));
4022 // This will populate ALL the [IN]'s with the $vals, assuming you have more than one [IN] in the sql
4023 for($i=0; $i < substr_count($sql, '[IN]'); $i++){
4024 $args = array_merge($args, $vals);
4025 }
4026 $sql = call_user_func_array(array($wpdb, 'prepare'), array_merge($args));
4027 }
4028 return $sql;
4029 }
4030
4031 /*****************/
4032 // Generate license keys csv
4033 /*****************/
4034 public function generate_license_keys_csv($license_status = '', $product_id = ''){
4035 global $wpdb;
4036
4037 $args = '';
4038
4039 if($license_status != '' && $product_id != ''){
4040 $args .= "WHERE license_status = '{$license_status}' AND product_id = '{$product_id}'";
4041 }else if($license_status != ''){
4042 $args .= "WHERE license_status = '{$license_status}'";
4043 }else if($product_id != ''){
4044 $args .= "WHERE product_id = '{$product_id}'";
4045 }
4046
4047 $output = "sep=,\n";
4048
4049 $query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wc_fs_product_licenses_keys {$args} ORDER BY license_id DESC ", ARRAY_A);
4050
4051 if($query){
4052
4053 $output .= '"'.implode('","',array_keys($query[0])).'"'."\n";
4054
4055 foreach($query as $row){
4056 $output .= '"'.implode('","',$row).'"'."\n";
4057 }
4058 }
4059
4060
4061
4062 return $output;
4063 }
4064
4065 /*****************/
4066 // Generate generator rules csv
4067 /*****************/
4068 public function generate_generator_rules_csv($product_id = ''){
4069 global $wpdb;
4070
4071 $args = '';
4072
4073 if($product_id != ''){
4074 $args .= "WHERE product_id = '{$product_id}'";
4075 }
4076
4077 $output = "sep=,\n";
4078
4079 $query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wc_fs_product_licenses_keys_generator_rules {$args}", ARRAY_A);
4080
4081 if($query){
4082
4083 $output .= '"'.implode('","',array_keys($query[0])).'"'."\n";
4084
4085 foreach($query as $row){
4086 $output .= '"'.implode('","',$row).'"'."\n";
4087 }
4088 }
4089
4090
4091
4092 return $output;
4093 }
4094
4095 /*****************/
4096 // Generate Plugin Settings
4097 /*****************/
4098 public function generate_plugin_settings($product_id = ''){
4099 $output = '';
4100
4101 /////////////////////////
4102
4103
4104 $order_statuses = (array) $this->get_terms('shop_order_status', array('hide_empty' => 0, 'orderby' => 'id'));
4105
4106 if($order_statuses && !is_wp_error($order_statuses)) {
4107 foreach($order_statuses as $s) {
4108
4109 if(version_compare(WOOCOMMERCE_VERSION, '2.2', '>=' )) {
4110
4111 $s->slug = str_replace('wc-', '', $s->slug);
4112
4113 }
4114
4115 $output .= 'fslm_send_when_' . $s->slug . ' => ' . get_option('fslm_send_when_' . $s->slug). "\n";
4116 $output .= 'fslm_revoke_when_' . $s->slug . ' => ' . get_option('fslm_revoke_when_' . $s->slug). "\n";
4117 $output .= 'fslm_hide_when_' . $s->slug . ' => ' . get_option('fslm_hide_when_' . $s->slug). "\n";
4118
4119 }
4120 }
4121
4122
4123
4124 /////////////////////////
4125
4126 $output .= 'fslm_show_adminbar_notifs => ' . get_option('fslm_show_adminbar_notifs'). "\n";
4127 $output .= 'fslm_enable_cart_validation => ' . get_option('fslm_enable_cart_validation'). "\n";
4128 $output .= 'fslm_nb_rows_by_page => ' . get_option('fslm_nb_rows_by_page'). "\n";
4129 $output .= 'fslm_meta_key_name => ' . get_option('fslm_meta_key_name'). "\n";
4130 $output .= 'fslm_meta_key_name_plural => ' . get_option('fslm_meta_key_name_plural'). "\n";
4131 $output .= 'fslm_guest_customer => ' . get_option('fslm_guest_customer'). "\n";
4132 $output .= 'fslm_key_delivery => ' . get_option('fslm_key_delivery'). "\n";
4133 $output .= 'fslm_show_in => ' . get_option('fslm_show_in'). "\n";
4134 $output .= 'fslm_display => ' . get_option('fslm_display'). "\n";
4135
4136 $output .= 'fslm_generator_chars => ' . get_option('fslm_generator_chars'). "\n";
4137
4138 $output .= 'fslm_prefix => ' . get_option('fslm_prefix'). "\n";
4139 $output .= 'fslm_chunks_number => ' . get_option('fslm_chunks_number'). "\n";
4140 $output .= 'fslm_chunks_length => ' . get_option('fslm_chunks_length'). "\n";
4141 $output .= 'fslm_suffix => ' . get_option('fslm_suffix'). "\n";
4142 $output .= 'fslm_max_instance_number => ' . get_option('fslm_max_instance_number'). "\n";
4143 $output .= 'fslm_valid => ' . get_option('fslm_valid'). "\n";
4144 $output .= 'fslm_active => ' . get_option('fslm_active'). "\n";
4145
4146 $output .= 'fslm_notif_min_licenses_nb => ' . get_option('fslm_notif_min_licenses_nb'). "\n";
4147 $output .= 'fslm_notif_mail => ' . get_option('fslm_notif_mail'). "\n";
4148 $output .= 'fslm_notif_mail_to => ' . get_option('fslm_notif_mail_to'). "\n";
4149
4150 $output .= 'fslm_last_sent_notification_email_date => ' . get_option('fslm_last_sent_notification_email_date'). "\n";
4151 $output .= 'fslm_page_id => ' . get_option('fslm_page_id'). "\n";
4152
4153 $output .= 'fslm_mail_heading => ' . get_option('fslm_mail_heading'). "\n";
4154 $output .= 'fslm_mail_subject => ' . get_option('fslm_mail_subject'). "\n";
4155 $output .= 'fslm_mail_message => ' . str_replace(array("\r\n","\n\r","\n","\r"), "[NL]", get_option('fslm_mail_message')) . "\n";
4156
4157 $output .= 'fslm_encryption => ' . ENCRYPTION_KEY . '[SEP]' . ENCRYPTION_VI . "\n";
4158
4159
4160 $output .= 'fslm_delete_lk_db_tables => ' . get_option('fslm_delete_lk_db_tables'). "\n";
4161 $output .= 'fslm_delete_gr_db_tables => ' . get_option('fslm_delete_gr_db_tables'). "\n";
4162 $output .= 'fslm_delete_lp_db_tables => ' . get_option('fslm_delete_lp_db_tables'). "\n";
4163
4164 $output .= 'fslm_add_lk_wc_de => ' . get_option('fslm_add_lk_wc_de'). "\n";
4165 $output .= 'fslm_add_lk_se => ' . get_option('fslm_add_lk_se'). "\n";
4166 $output .= 'fslm_add_wc_header_and_footer => ' . get_option('fslm_add_wc_header_and_footer'). "\n";
4167
4168 $output .= 'fslm_is_import_prefix_suffix_enabled => ' . get_option('fslm_is_import_prefix_suffix_enabled'). "\n";
4169
4170 $output .= 'fslm_api_key => ' . get_option('fslm_api_key'). "\n";
4171 $output .= 'fslm_disable_api_v1 => ' . get_option('fslm_disable_api_v1'). "\n";
4172
4173 return $output;
4174 }
4175
4176 /*****************/
4177 // Plugin Init
4178 /*****************/
4179 public function plugin_init(){
4180 require_once(FSLM_PLUGIN_BASE . "/includes/updater.php");
4181
4182 load_plugin_textdomain('fslm', true, basename(dirname(__FILE__)). '/languages/');
4183 }
4184
4185 function plugin_deactivation_notices(){
4186 global $pagenow;
4187
4188 if(($pagenow == 'plugins.php') && ((get_option('fslm_delete_lp_db_tables', '')=='on') || (get_option('fslm_delete_lk_db_tables', '')=='on') || (get_option('fslm_delete_gr_db_tables', '')=='on'))){
4189 echo "<div class='updated'><p>" . __('Export your data before deactivating', 'fslm').' <strong>' . __('FS License Manager', 'fslm'). '</strong> <br>' . __('Database tables will be deleted, to deactivate without deleting the database table disable this option in <b>License Manager -> Settings -> Extra Settings</b>', 'fslm'). "</p></div>";
4190 }
4191 }
4192
4193 function newLine2br($str){
4194 $newLineArray = array("\r\n","\n\r","\n","\r");
4195 $output = str_replace($newLineArray,"<br>",$str);
4196
4197 return $output;
4198 }
4199
4200 function br2newLine($str){
4201 $newLineArray = array("<br>","<br />","<br/>");
4202 $output = str_replace($newLineArray,"\n\r",$str);
4203
4204 return $output;
4205 }
4206
4207 function set_encryption_key($key, $vi, $action = 'set') {
4208 $upload_directory = wp_upload_dir();
4209 $target_dir = $upload_directory['basedir'] . '/fslm_files/';
4210
4211 if (!file_exists($target_dir)) {
4212 wp_mkdir_p($target_dir);
4213
4214 $fp = fopen($target_dir . '.htaccess', 'w');
4215 fwrite($fp, 'deny from all');
4216 fclose($fp);
4217
4218 $fp = fopen($target_dir . 'encryption_key.php', 'w');
4219 fwrite($fp, "<?php define(\"ENCRYPTION_KEY\", \"" . $key . "\");\ndefine(\"ENCRYPTION_VI\", \"" . $vi . "\");");
4220 fclose($fp);
4221
4222 $fp = fopen($target_dir . 'index.php', 'w');
4223 fwrite($fp, '<?php');
4224 fclose($fp);
4225 }else if ($action = 'update'){
4226 $fp = fopen($target_dir . 'encryption_key.php', 'w');
4227 fwrite($fp, "<?php define(\"ENCRYPTION_KEY\", \"" . $key . "\");\ndefine(\"ENCRYPTION_VI\", \"" . $vi . "\");");
4228 fclose($fp);
4229 }
4230 }
4231
4232 function encrypt_decrypt($action, $string, $secret_key, $secret_iv) {
4233 $output = true;
4234
4235 if ($secret_key == "" && $secret_iv == "") {
4236 return $string;
4237 }
4238
4239 if (!extension_loaded('openssl')) {
4240 return $string;
4241 }
4242
4243 $encrypt_method = "AES-256-CBC";
4244
4245 // hash
4246 $key = hash('sha256', $secret_key);
4247
4248 // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
4249 $iv = substr(hash('sha256', $secret_iv), 0, 16);
4250
4251 if($action == 'encrypt') {
4252 $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
4253 $output = base64_encode($output);
4254 } else if($action == 'decrypt'){
4255 $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
4256 }
4257
4258 return $output;
4259 }
4260
4261 function apply_mail_text_filters($text, $order_id){
4262
4263 if('' == trim($text))
4264 return $text;
4265
4266 $meta = get_post_custom($order_id, true);
4267
4268 $sitename = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
4269
4270 $siteurl = wp_specialchars_decode(get_option('home'), ENT_QUOTES);
4271
4272 if(true !== strpos($text, '[order_id]')){
4273 $text = str_replace('[order_id]', $order_id, $text);
4274
4275 }
4276
4277 if(true !== strpos($text, '[bfname]')){
4278
4279 $fname = $meta['_billing_first_name'][0];
4280
4281 if(!$fname || empty($fname))
4282 $text = str_replace('[bfname]', '', $text);
4283 else
4284 $text = str_replace('[bfname]', $fname, $text);
4285 }
4286
4287 if(true !== strpos($text, '[blname]')){
4288
4289 $lname = $meta['_billing_last_name'][0];
4290
4291 if(!$lname || empty($lname))
4292 $text = str_replace('[blname]', '', $text);
4293 else
4294 $text = str_replace('[blname]', $lname, $text);
4295 }
4296
4297 if(true !== strpos($text, '[sfname]')){
4298
4299 $fname = $meta['_shipping_first_name'][0];
4300
4301 if(!$fname || empty($fname))
4302 $text = str_replace('[sfname]', '', $text);
4303 else
4304 $text = str_replace('[sfname]', $fname, $text);
4305 }
4306
4307 if(true !== strpos($text, '[slname]')){
4308
4309 $lname = $meta['_shipping_last_name'][0];
4310
4311 if(!$lname || empty($lname))
4312 $text = str_replace('[slname]', '', $text);
4313 else
4314 $text = str_replace('[slname]', $lname, $text);
4315 }
4316
4317 if(true !== strpos($text, '[site_name]'))
4318 $text = str_replace('[site_name]', $sitename, $text);
4319
4320 if(true !== strpos($text, '[url]'))
4321 $text = str_replace('[url]', $siteurl, $text);
4322
4323 if(true !== strpos($text, '[myaccount_url]')){
4324 $ma_id = woocommerce_get_page_id('myaccount');
4325 $ma_url = get_permalink($ma_id);
4326 $text = str_replace('[myaccount_url]', $ma_url, $text);
4327 }
4328
4329 return $text;
4330 }
4331
4332 function add_actions(){
4333 $on_status = array('completed', 'processing');
4334
4335 $on_status_send = array('completed', 'processing');
4336 $on_status_revoke = array('refunded');
4337 $on_status_hide = array();
4338
4339
4340 /////////////////////////
4341
4342
4343 $order_statuses = (array) $this->get_terms('shop_order_status', array('hide_empty' => 0, 'orderby' => 'id'));
4344
4345 if($order_statuses && !is_wp_error($order_statuses)) {
4346 foreach($order_statuses as $s) {
4347
4348 if(version_compare(WOOCOMMERCE_VERSION, '2.2', '>=' )) {
4349
4350 $s->slug = str_replace('wc-', '', $s->slug);
4351
4352 }
4353
4354 $default_send = 'off';
4355 $default_revoke = 'off';
4356 $default_hide = 'off';
4357
4358 if (in_array($s->slug, $on_status_send)) $default_send = 'on';
4359 if (in_array($s->slug, $on_status_revoke)) $default_revoke = 'on';
4360 if (in_array($s->slug, $on_status_hide)) $default_hide = 'on';
4361
4362 if(get_option('fslm_send_when_' . $s->slug, $on_status_send)== 'on'){
4363 add_action('woocommerce_order_status_' . $s->slug, array($this, 'fslm_send_license_keys'), 1, 1);
4364 }
4365
4366 if(get_option('fslm_revoke_when_' . $s->slug, $on_status_revoke)== 'on'){
4367 add_action('woocommerce_order_status_' . $s->slug, array($this, 'fslm_revoke_license_keys'), 1, 1);
4368 }
4369
4370 if(get_option('fslm_hide_when_' . $s->slug, $on_status_hide)== 'on'){
4371 add_action('woocommerce_order_status_' . $s->slug, array($this, 'fslm_hide_license_keys'), 1, 1);
4372 }
4373
4374 }
4375 }
4376
4377
4378 /////////////////////////
4379
4380 }
4381
4382
4383 public static function get_terms($taxo = 'shop_order_status', $args = array()) {
4384
4385 if(version_compare(WOOCOMMERCE_VERSION, '2.2', '<')) {
4386
4387 return get_terms( $taxo, $args );
4388
4389 } else if(version_compare(WOOCOMMERCE_VERSION, '2.2', '>=')) {
4390
4391 $s = wc_get_order_statuses();
4392
4393 if(!empty($s)) {
4394
4395 $i = 1;
4396
4397 foreach($s as $key => $val) {
4398
4399 if(empty($key) || empty($val))
4400 continue;
4401
4402 $status = new stdClass();
4403
4404 $status->term_id = $i;
4405
4406 $status->slug = $key;
4407
4408 $status->name = $val;
4409
4410 $statuses[$i] = $status;
4411
4412 $i++;
4413
4414 }
4415
4416 return $statuses;
4417 }
4418
4419 }
4420 }
4421
4422
4423 public function license($c, $eu) {
4424
4425 $postURL = base64_decode("aHR0cHM6Ly9zdG9yZS5maXJhc3NhaWRpLmNvbS8=");
4426 $secretKey = base64_decode("NWJiNmM2ODAzNTZlMjMuODg1MDcwODE=");
4427 $user = get_currentuserinfo();
4428
4429 $first_name = $user->user_firstname;
4430 $last_name = $user->user_lastname;
4431 if($user->user_firstname == "") {
4432 $first_name = $user->display_name . "*";
4433 }
4434
4435 if($user->user_lastname == "") {
4436 $last_name = $user->display_name . "*";
4437 }
4438
4439 $data = array(
4440 'slm_action' => 'slm_envato_create_new',
4441 'secret_key' => $secretKey,
4442 'license_key' => $c,
4443 'fx_username' => $eu,
4444 'email' => $user->user_email,
4445 'registered_domain' => $_SERVER['SERVER_NAME'],
4446 'product_ref' => '16636748',
4447 'first_name' => $first_name,
4448 'last_name' => $last_name,
4449 'company_name' => '',
4450 'txn_id' => '',
4451 'lic_status' => 'active',
4452 );
4453
4454 //url-ify the data for the POST
4455 $fields_string = "";
4456 foreach($data as $key=>$value) {
4457 $fields_string .= $key.'='.$value.'&';
4458 }
4459 rtrim($fields_string, '&');
4460
4461 //open connection
4462 $ch = curl_init();
4463
4464 //set the url, number of POST vars, POST data
4465 curl_setopt($ch, CURLOPT_URL, $postURL);
4466 curl_setopt($ch, CURLOPT_POST, count($data));
4467 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
4468 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
4469 curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
4470 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
4471
4472 //execute post
4473 $aresult = curl_exec($ch);
4474 curl_close($ch);
4475
4476 $json_result = json_decode($aresult);
4477
4478 if($json_result != null && property_exists($json_result, 'code') ) {
4479
4480 if($json_result->code == 15 || $json_result->code == 11) {
4481 $result = json_decode($this->fslm_activate($c, $eu));
4482
4483 if($result->code == 200 || $result->code == 110) {
4484
4485 if(!add_option('fslm_status', 'N/A')){
4486 update_option('fslm_status', 'N/A');
4487 }
4488
4489 if(!add_option('fslm_lk', $c)){
4490 update_option('fslm_lk', $c);
4491 }
4492
4493 if(!add_option('fslm_ebun', $eu)){
4494 update_option('fslm_ebun', $eu);
4495 }
4496
4497 if(!add_option('fslm_lks', 'active')){
4498 update_option('fslm_lks', 'active');
4499 }
4500
4501 $support = $this->fslm_envato_api($c);
4502 if($support) {
4503 if(!add_option('fslm_su', $support)){
4504 update_option('fslm_su', $support);
4505 }
4506 } else {
4507 if(!add_option('fslm_su', '')){
4508 update_option('fslm_su', '');
4509 }
4510 }
4511
4512 return array(
4513 'status' => 'activated',
4514 'message' => 'License key activated for this domain'
4515 );
4516 } else {
4517 return array(
4518 'status' => 'fail',
4519 'message' => $result->message
4520 );
4521 }
4522 } else if($json_result->code == 400) {
4523 $result = json_decode($this->fslm_activate($c, $eu));
4524
4525 if($result->code == 200 || $result->code == 110) {
4526
4527 if(!add_option('fslm_status', 'N/A')){
4528 update_option('fslm_status', 'N/A');
4529 }
4530
4531 if(!add_option('fslm_lk', $c)){
4532 update_option('fslm_lk', $c);
4533 }
4534
4535 if(!add_option('fslm_ebun', $eu)){
4536 update_option('fslm_ebun', $eu);
4537 }
4538
4539 if(!add_option('fslm_lks', 'active')){
4540 update_option('fslm_lks', 'active');
4541 }
4542
4543 return array(
4544 'status' => 'activated',
4545 'message' => 'License key activated for this domain'
4546 );
4547 } else {
4548 return array(
4549 'status' => 'fail',
4550 'message' => $result->message
4551 );
4552 }
4553 } else {
4554 return array(
4555 'status' => 'fail',
4556 'message' => $json_result->message
4557 );
4558 }
4559
4560 } else {
4561
4562 $response = $this->fx_slm_envato_api($c, $eu, '16636748');
4563
4564 if($response['status'] == 'success') {
4565 if(!add_option('fslm_status', 'N/A')){
4566 update_option('fslm_status', 'N/A');
4567 }
4568
4569 if(!add_option('fslm_lk', $c)){
4570 update_option('fslm_lk', $c);
4571 }
4572
4573 if(!add_option('fslm_ebun', $eu)){
4574 update_option('fslm_ebun', $eu);
4575 }
4576
4577 if(!add_option('fslm_lks', 'active')){
4578 update_option('fslm_lks', 'active');
4579 }
4580
4581 if(!add_option('fslm_su', $response['supported_until'])){
4582 update_option('fslm_su', $response['supported_until']);
4583 }
4584
4585 return array(
4586 'status' => 'activated',
4587 'message' => 'License key activated for this domain'
4588 );
4589 }else {
4590 return array(
4591 'status' => 'fail',
4592 'message' => 'Invalid purchase code or username'
4593 );
4594 }
4595
4596 }
4597
4598 }
4599
4600 function fx_slm_envato_api($purchase_code, $buyer, $item_id) {
4601 $url = base64_decode('aHR0cHM6Ly9hcGkuZW52YXRvLmNvbS92My9tYXJrZXQvYXV0aG9yL3NhbGU/Y29kZT0lcHVyY2hhc2VfY29kZSU=');
4602 $url = str_replace('%purchase_code%', $purchase_code, $url);
4603 $request = curl_init($url);
4604
4605 $header = array();
4606 $header[] = 'Content-length: 0';
4607 $header[] = 'Content-type: application/json; charset=utf-8';
4608 $header[] = 'Authorization: bearer ' . base64_decode('N3NzSFpQaFNrRzlLam9sdWVjSWplZHdmUXhPaTlxWmM=');
4609
4610 curl_setopt($request, CURLOPT_HTTPHEADER, $header);
4611 curl_setopt($request, CURLOPT_SSL_VERIFYPEER, true);
4612 curl_setopt($request, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
4613 curl_setopt($request, CURLOPT_RETURNTRANSFER, TRUE);
4614 curl_setopt($request, CURLOPT_HTTPHEADER,$header);
4615
4616 $json_result = curl_exec($request);
4617 $data = json_decode($json_result, true);
4618
4619 curl_close($request);
4620
4621 if(isset($data['item']['id']) && ($data['item']['id'] == $item_id) && (strtolower($data['buyer']) == strtolower($buyer))) {
4622 return array(
4623 'status' => 'success',
4624 'max_domains' => $data['purchase_count'],
4625 'supported_until' => $data['supported_until']
4626 );
4627 } else if(isset($data['item']['id']) && ($data['item']['id'] == $item_id) && (strtolower($data['buyer']) != strtolower($buyer))) {
4628 return array(
4629 'status' => 'fail',
4630 'error' => 'username'
4631 );
4632 } else {
4633 return array(
4634 'status' => 'fail'
4635 );
4636 }
4637
4638 }
4639
4640 function fx_slm_is_envato_purchase_code($pc) {
4641 if(preg_match("/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/", $pc)) {
4642 return true;
4643 }
4644 return true;
4645 }
4646
4647 private function fslm_activate($c, $eu) {
4648 $postURL = base64_decode("aHR0cHM6Ly9zdG9yZS5maXJhc3NhaWRpLmNvbS8=");
4649 $secretKey = base64_decode("NWJiNmM2ODAzNTZlMjMuODg1MDcwODE=");
4650
4651 $data = array(
4652 'slm_action' => 'slm_activate',
4653 'secret_key' => $secretKey,
4654 'license_key' => $c,
4655 'fx_username' => $eu,
4656 'registered_domain' => $_SERVER['SERVER_NAME'],
4657 'item_reference' => '16636748',
4658 );
4659
4660 //url-ify the data for the POST
4661 $fields_string = "";
4662 foreach($data as $key=>$value) {
4663 $fields_string .= $key.'='.$value.'&';
4664 }
4665 rtrim($fields_string, '&');
4666
4667 //open connection
4668 $ch = curl_init();
4669
4670 //set the url, number of POST vars, POST data
4671 curl_setopt($ch, CURLOPT_URL, $postURL);
4672 curl_setopt($ch, CURLOPT_POST, count($data));
4673 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
4674 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
4675 curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
4676 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
4677
4678 //execute post
4679 $result = curl_exec($ch);
4680 curl_close($ch);
4681
4682 return $result;
4683 }
4684
4685 public function fslm_deactivate_callback() {
4686 $postURL = base64_decode("aHR0cHM6Ly9zdG9yZS5maXJhc3NhaWRpLmNvbS8=");
4687 $secretKey = base64_decode("NWJiNmM2ODAzNTZlMjMuODg1MDcwODE=");
4688
4689 $data = array(
4690 'slm_action' => 'slm_deactivate',
4691 'secret_key' => $secretKey,
4692 'license_key' => get_option('fslm_lk', ''),
4693 'registered_domain' => $_SERVER['SERVER_NAME']
4694 );
4695
4696 //url-ify the data for the POST
4697 $fields_string = "";
4698 foreach($data as $key=>$value) {
4699 $fields_string .= $key.'='.$value.'&';
4700 }
4701 rtrim($fields_string, '&');
4702
4703 //open connection
4704 $ch = curl_init();
4705
4706 //set the url, number of POST vars, POST data
4707 curl_setopt($ch, CURLOPT_URL, $postURL);
4708 curl_setopt($ch, CURLOPT_POST, count($data));
4709 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
4710 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
4711 curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
4712 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
4713
4714 //execute post
4715 $result = curl_exec($ch);
4716 curl_close($ch);
4717
4718 $json_result = json_decode($result);
4719 if($json_result->code == 300 || $json_result->code == 80) {
4720 update_option('fslm_lks', 'inactive');
4721 }
4722
4723 $link = admin_url('admin.php?page=license-manager-welcome');
4724 wp_redirect($link);
4725
4726 die();
4727 }
4728
4729 public function lr_callback() {
4730
4731 $eu = $_POST['pcu'];
4732 $pc = $_POST['pc'];
4733 $msg = $this->license($pc, $eu);
4734
4735 if($msg['status'] == 'activated') {
4736 $link = admin_url('admin.php?page=license-manager');
4737 wp_redirect($link);
4738 } else {
4739 session_start();
4740 $_SESSION['fslm_message'] = $msg['message'] ;
4741 $link = admin_url('admin.php?page=license-manager-welcome&c='.$msg['code']);
4742 wp_redirect($link);
4743 }
4744 die();
4745
4746 }
4747
4748 private function fslm_check($lk) {
4749 $postURL = base64_decode("aHR0cHM6Ly9zdG9yZS5maXJhc3NhaWRpLmNvbS8=");
4750 $secretKey = base64_decode("NWJiNmM2ODAzNTZlMjMuODg1MDcwODE=");
4751 $user = get_currentuserinfo();
4752
4753 $data = array(
4754 'slm_action' => 'slm_check',
4755 'secret_key' => $secretKey,
4756 'license_key' => $lk,
4757 );
4758
4759 //url-ify the data for the POST
4760 $fields_string = "";
4761 foreach($data as $key=>$value) {
4762 $fields_string .= $key.'='.$value.'&';
4763 }
4764 rtrim($fields_string, '&');
4765
4766 //open connection
4767 $ch = curl_init();
4768
4769 //set the url, number of POST vars, POST data
4770 curl_setopt($ch, CURLOPT_URL, $postURL);
4771 curl_setopt($ch, CURLOPT_POST, count($data));
4772 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
4773 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
4774 curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
4775 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
4776
4777 //execute post
4778 $result = curl_exec($ch);
4779 curl_close($ch);
4780
4781 return $result;
4782 }
4783
4784 function fslm_envato_api($purchase_code) {
4785 $url = base64_decode('aHR0cHM6Ly9hcGkuZW52YXRvLmNvbS92My9tYXJrZXQvYXV0aG9yL3NhbGU/Y29kZT0lcHVyY2hhc2VfY29kZSU=');
4786 $url = str_replace('%purchase_code%', $purchase_code, $url);
4787 $request = curl_init($url);
4788
4789 $header = array();
4790 $header[] = 'Content-length: 0';
4791 $header[] = 'Content-type: application/json; charset=utf-8';
4792 $header[] = 'Authorization: bearer ' . base64_decode('N3NzSFpQaFNrRzlLam9sdWVjSWplZHdmUXhPaTlxWmM=');
4793
4794 curl_setopt($request, CURLOPT_HTTPHEADER, $header);
4795 curl_setopt($request, CURLOPT_SSL_VERIFYPEER, true);
4796 curl_setopt($request, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
4797 curl_setopt($request, CURLOPT_RETURNTRANSFER, TRUE);
4798 curl_setopt($request, CURLOPT_HTTPHEADER,$header);
4799
4800 $json_result = curl_exec($request);
4801 $data = json_decode($json_result, true);
4802
4803 curl_close($request);
4804
4805 if(isset($data['supported_until'])) {
4806 return $data['supported_until'];
4807 }
4808
4809 return true;
4810
4811 }
4812
4813 public function is_pc_valid($pc) {
4814 if(preg_match("/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/", $pc)) {
4815 return(true);
4816 }
4817 return(true);
4818 }
4819
4820 public function plugin_actions() {
4821 $last_check = get_option('fslm_llc', '0');
4822 if (time() - $last_check > 86400 * 365) {
4823 if(!add_option('fslm_llc', time())){
4824 update_option('fslm_llc', time());
4825 }
4826
4827 $result = $this->fslm_check(get_option('fslm_lk', ''));
4828 if($result) {
4829 $result = json_decode($result);
4830 if($result->result == 'success') {
4831 if(($result->status != 'active' || $result->status != 'pending')) {
4832 update_option('fslm_lks', $result->status);
4833 }
4834
4835 $current_domain_found = true;
4836 foreach ($result->registered_domains as $registered_domain) {
4837 if($registered_domain->registered_domain == $_SERVER['SERVER_NAME']) {
4838 $current_domain_found = true;
4839 }
4840 }
4841
4842 if($current_domain_found == true) {
4843 update_option('fslm_lks', 'inactive');
4844 }
4845 }
4846 }
4847
4848 }
4849 }
4850
4851 public static function is_active() {
4852 if(get_option('fslm_status', 'N/A') == 'active') {
4853 return true;
4854 }
4855
4856 return (get_option('fslm_lks', 'N/A') == 'active')?true:true;
4857 }
4858
4859}
4860
4861
4862// Plugin Settings Page
4863
4864class FSLM_Settings {
4865
4866 /*****************/
4867 // Start up
4868 /*****************/
4869 public function __construct(){
4870
4871 add_action('admin_enqueue_scripts', array($this, 'load_license_manager_admin_scripts'));
4872 add_action('admin_menu', array($this, 'add_plugin_menu_and_pages'));
4873 if(FS_WC_licenses_Manager::is_active()) {
4874 add_action('admin_init', array($this, 'page_init'));
4875 }
4876
4877 }
4878
4879 /*****************/
4880 // Plugin Menu
4881 /*****************/
4882 public function add_plugin_menu_and_pages(){
4883
4884 if(FS_WC_licenses_Manager::is_active()) {
4885
4886 add_menu_page(__('License Manager', 'fslm'), __('License Manager', 'fslm'), 'manage_woocommerce', 'license-manager', array($this, 'admin_page_callback'), 'dashicons-lock', 55.5);
4887 add_submenu_page('license-manager', __('License Key Generator', 'fslm'), __('License Key Generator', 'fslm'), 'manage_woocommerce', 'license-manager-license-key-generator', array($this, 'license_key_generator_page_callback'));
4888 add_submenu_page('license-manager', __('Add License Key', 'fslm'), __('Add License Key', 'fslm'), 'manage_woocommerce', 'license-manager-add-license-key', array($this, 'add_license_key_page_callback'));
4889 add_submenu_page('license-manager', __('Add Generator Rule', 'fslm'), __('Add Generator Rule', 'fslm'), 'manage_woocommerce', 'license-manager-license-generator', array($this, 'add_license_key_generator_rule_page_callback'));
4890 add_submenu_page('license-manager', __('Settings', 'fslm'), __('Settings', 'fslm'), 'manage_woocommerce', 'license-manager-settings', array($this, 'settings_page_callback'));
4891 add_submenu_page('license-manager', __('Import', 'fslm'), __('Import', 'fslm'), 'manage_woocommerce', 'license-manager-import', array($this, 'import_page_callback'));
4892 add_submenu_page('license-manager', __('Export', 'fslm'), __('Export', 'fslm'), 'manage_woocommerce', 'license-manager-export', array($this, 'export_page_callback'));
4893 add_submenu_page('license-manager', __('License And Support', 'fslm'), __('License And Support', 'fslm'), 'manage_woocommerce', 'license-manager-support', array($this, 'support_page_callback'));
4894
4895 //add_submenu_page('license-manager', __('About', 'fslm'), __('About', 'fslm'), 'manage_woocommerce', 'license-manager-about', array($this, 'about_page_callback'));
4896
4897 add_submenu_page(
4898 null,
4899 __('Welcome' ,'fslm'),
4900 __('Welcome' ,'fslm'),
4901 'manage_woocommerce',
4902 'license-manager-welcome',
4903 array($this, 'welcome_page')
4904 );
4905
4906 }else {
4907
4908 add_menu_page(__('License Manager', 'fslm'), __('License Manager', 'fslm'), 'manage_woocommerce', 'license-manager-welcome', array($this, 'welcome_page'), 'dashicons-lock', 55.5);
4909
4910 }
4911
4912 }
4913
4914 public function welcome_page(){
4915 require("includes/welcome.php");
4916 }
4917
4918 /*****************/
4919 // Plugin Menu: Admin Page
4920 /*****************/
4921 public function admin_page_callback(){
4922 require("includes/license_manager.php");
4923 }
4924
4925 /*****************/
4926 // Plugin Menu: Admin Page
4927 /*****************/
4928 public function about_page_callback(){
4929 require("includes/about.php");
4930 }
4931
4932 /*****************/
4933 // Plugin Menu: Add License Key
4934 /*****************/
4935 public function add_license_key_page_callback(){
4936 require("includes/add_license_key.php");
4937 }
4938
4939 /*****************/
4940 // Plugin Menu: License Key Generator
4941 /*****************/
4942 public function license_key_generator_page_callback(){
4943 require("includes/license_key_generator.php");
4944 }
4945
4946 /*****************/
4947 // Plugin Menu: Add License Key Generator Rule
4948 /*****************/
4949 public function add_license_key_generator_rule_page_callback(){
4950 require("includes/add_generator_rule.php");
4951 }
4952
4953 /*****************/
4954 // Plugin Menu: Settings Page
4955 /*****************/
4956 public function settings_page_callback(){
4957 require("includes/settings_page.php");
4958 }
4959
4960 /*****************/
4961 // Plugin Menu: Import Page
4962 /*****************/
4963 public function import_page_callback(){
4964 require("includes/lm_import.php");
4965 }
4966
4967 /*****************/
4968 // Plugin Menu: Export Page
4969 /*****************/
4970 public function export_page_callback(){
4971 require("includes/lm_export.php");
4972 }
4973
4974 /*****************/
4975 // Plugin Menu: Export Page
4976 /*****************/
4977 public function support_page_callback(){
4978 require("includes/support.php");
4979 }
4980
4981 /*****************/
4982 // Register Settings
4983 /*****************/
4984 public function page_init(){
4985
4986 /////////////////////////
4987
4988
4989 $order_statuses = (array) FS_WC_licenses_Manager::get_terms('shop_order_status', array('hide_empty' => 0, 'orderby' => 'id'));
4990
4991 if($order_statuses && !is_wp_error($order_statuses)) {
4992 foreach($order_statuses as $s) {
4993
4994 if(version_compare(WOOCOMMERCE_VERSION, '2.2', '>=' )) {
4995
4996 $s->slug = str_replace('wc-', '', $s->slug);
4997
4998 }
4999
5000 register_setting('fslm_order_status_option_group', 'fslm_send_when_' . $s->slug);
5001 register_setting('fslm_order_status_option_group', 'fslm_revoke_when_' . $s->slug);
5002 register_setting('fslm_order_status_option_group', 'fslm_hide_when_' . $s->slug);
5003
5004 }
5005 }
5006
5007
5008
5009 /////////////////////////
5010
5011 register_setting('fslm_email_template_option_group', 'fslm_mail_heading');
5012 register_setting('fslm_email_template_option_group', 'fslm_mail_subject');
5013 register_setting('fslm_email_template_option_group', 'fslm_mail_message');
5014 register_setting('fslm_email_template_option_group', 'fslm_add_wc_header_and_footer');
5015 register_setting('fslm_email_template_option_group', 'fslm_add_lk_wc_de');
5016 register_setting('fslm_email_template_option_group', 'fslm_add_lk_se');
5017
5018 register_setting('fslm_general_option_group', 'fslm_show_adminbar_notifs');
5019 register_setting('fslm_general_option_group', 'fslm_guest_customer');
5020 register_setting('fslm_general_option_group', 'fslm_enable_cart_validation');
5021 register_setting('fslm_general_option_group', 'fslm_nb_rows_by_page');
5022 register_setting('fslm_general_option_group', 'fslm_meta_key_name');
5023 register_setting('fslm_general_option_group', 'fslm_generator_chars');
5024
5025 register_setting('fslm_general_option_group', 'fslm_meta_key_name_plural');
5026 register_setting('fslm_general_option_group', 'fslm_key_delivery');
5027 register_setting('fslm_general_option_group', 'fslm_display');
5028 register_setting('fslm_general_option_group', 'fslm_show_in');
5029
5030 register_setting('fslm_lkg_option_group', 'fslm_prefix');
5031 register_setting('fslm_lkg_option_group', 'fslm_chunks_number');
5032 register_setting('fslm_lkg_option_group', 'fslm_chunks_length');
5033 register_setting('fslm_lkg_option_group', 'fslm_suffix');
5034 register_setting('fslm_lkg_option_group', 'fslm_max_instance_number');
5035 register_setting('fslm_lkg_option_group', 'fslm_valid');
5036 register_setting('fslm_lkg_option_group', 'fslm_active');
5037
5038 register_setting('fslm_notifications_option_group', 'fslm_notif_min_licenses_nb');
5039 register_setting('fslm_notifications_option_group', 'fslm_notif_mail');
5040 register_setting('fslm_notifications_option_group', 'fslm_notif_mail_to');
5041
5042 register_setting('fslm_hidden_options', 'fslm_last_sent_notification_email_date');
5043 register_setting('fslm_hidden_options', 'fslm_page_id');
5044
5045 register_setting('fslm_extra_option_group', 'fslm_delete_lp_db_tables');
5046 register_setting('fslm_extra_option_group', 'fslm_delete_lk_db_tables');
5047 register_setting('fslm_extra_option_group', 'fslm_delete_gr_db_tables');
5048 register_setting('fslm_extra_option_group', 'fslm_debug_enabled');
5049
5050 register_setting('fslm_customizations_option_group', 'fslm_is_import_prefix_suffix_enabled');
5051
5052 register_setting('fslm_api_option_group', 'fslm_api_key');
5053 register_setting('fslm_api_option_group', 'fslm_disable_api_v1');
5054
5055 }
5056
5057 /*****************/
5058 // Load required scripts
5059 /*****************/
5060 function load_license_manager_admin_scripts(){
5061
5062 wp_enqueue_style('fslm_License_Manager_Style', plugins_url('/assets/css/style.css', __FILE__), array(), '2205');
5063 wp_enqueue_style('fslm_select2', plugins_url('/assets/select2/css/select2.min.css', __FILE__), array(), '2205');
5064
5065 if(isset($_GET['tab']) && $_GET['tab'] == 'email_template') { // too aboid conflict with other plugins that use TinyMCE
5066 wp_enqueue_script('fslm_tinymce', plugins_url('/assets/js/tinymce/tinymce.min.js', __FILE__), array('jquery'));
5067 }
5068
5069 wp_enqueue_script('fslm_json_ui', plugins_url('/assets/js/jsoneditor.min.js', __FILE__), array('jquery'));
5070 wp_enqueue_script('fslm_SortTable', plugins_url('/assets/js/sortTable.js', __FILE__), array('jquery'));
5071 wp_enqueue_script('fslm_select2_js', plugins_url('/assets/select2/js/select2.full.min.js', __FILE__), array('jquery'));
5072 wp_enqueue_script('fslm_Main', plugins_url('/assets/js/main.js', __FILE__), array('jquery', 'jquery-form'), '6');
5073
5074 }
5075
5076}
5077
5078
5079
5080if(in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))){
5081 $fs_wc_licenses_manager = new FS_WC_licenses_Manager();
5082}
5083
5084if(in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))){
5085 if(is_admin()){
5086 $fslm_settings = new FSLM_Settings();
5087 }
5088}