Skip to content

Commit

Permalink
improved overall logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cijagani committed Sep 4, 2023
1 parent ef7f5b9 commit 84a184b
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
135 changes: 135 additions & 0 deletions src/CoreCacheRepo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace CtlCoreCache\CoreCacheRepo;

use Defuse\Crypto\Crypto;
use Defuse\Crypto\Key;

require_once(__DIR__ . "/constants.php");

class CoreCacheRepo
{
public static function instantiateCacheRepository($cache_name)
{

$key = Key::loadFromAsciiSafeString(get_option($cache_name . '_key'));

$decode_ciphertext = json_decode(Crypto::decrypt(get_option($cache_name . '_token'), $key));

$last_verification = get_instance()->session->userdata("cache_time");
$seconds = $decode_ciphertext->check_interval ?? 1; // 84000

if (is_null($last_verification) || time() > ($last_verification + $seconds)) {

$response = @call_user_func_array("file_get_contents", [$decode_ciphertext->validation_url, false, call_user_func("stream_context_create", ['http' => ['method' => 'POST', 'header' => implode("\r\n", ['Authorization: '. get_option($cache_name . '_token'),'Accept: application/json'])]])]);

if (empty($response)) {
preg_match('/^\s*.*?\s(.*)/', $http_response_header[0], $res);
set_alert('danger', $res[1]);
get_instance()->app_modules->deactivate($cache_name);
return;
}

$newCache = json_decode($response);

if (200 != $newCache->status) {
get_instance()->app_modules->deactivate($cache_name);
set_alert('danger', $newCache->status . ": " . $newCache->message);
}

get_instance()->session->set_userdata([
'cache_time' => time(),
]);
return;
}
$cache_data = get_instance()->app_modules->get($cache_name);
$is_cache = strcmp(
@file_get_contents(APPPATH . 'vendor/composer/' . basename($cache_data['headers']['uri']) . ".lic"),
base64_encode(get_option($cache_name . '_token'))
) == 0
&& strcmp(@file_get_contents(APPPATH . 'vendor/composer/' . basename($cache_data['headers']['uri']) . ".key"), base64_encode(get_option($cache_name . '_key'))) == 0;
return (!$is_cache) ? get_instance()->app_modules->deactivate($cache_name) : true;
}

public static function getUserIP()
{
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
} elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_FORWARDED'])) {
$ipaddress = $_SERVER['HTTP_FORWARDED'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ipaddress = $_SERVER['REMOTE_ADDR'];
} else {
$ipaddress = 'UNKNOWN';
}

return $ipaddress;
}

public static function createCacheRepository($cache)
{
if (!option_exists($cache . '_token')) {
ob_start();
require_once(__DIR__ . "/activate.php");
$string = ob_get_clean();
echo $string;
exit;
}
}

public static function removeCacheRepository($cache)
{

$cache_data = get_instance()->app_modules->get($cache);

$additional_data = [];
$all_activated = get_instance()->app_modules->get_activated();
foreach ($all_activated as $active_module => $value) {
$key = get_option($active_module . '_key');
$token = get_option($active_module . '_token');
if (!empty($key) && !empty($token)) {
$additional_data = [
$active_module => [
'key' => $key,
'token' => $token
]
];
}
}

get_instance()->load->library('user_agent');

$response = @call_user_func_array("file_get_contents", [REG_PROD_POINT, false, call_user_func("stream_context_create", ['http' => ['method' => 'POST', 'header' => 'Content-Type: application/json', 'content' => json_encode(['user_agent' => get_instance()->agent->browser() . ' ' . get_instance()->agent->version(), 'activated_domain' => base_url(), 'cache_name' => $cache_data['headers'], 'ip' => self::getUserIP(), 'os' => get_instance()->agent->platform(), 'purchase_code' => trim($_POST['purchase_key']), 'additional_data' => $additional_data])]])]);

if (empty($response)) {
preg_match('/^\s*.*?\s(.*)/', $http_response_header[0], $res);
set_alert('danger', $res[1]);
redirect(admin_url('modules/activate/' . $cache));
}

$newCache = json_decode($response);

if (200 != $newCache->status) {
set_alert('danger', $newCache->status . ": " . $newCache->message);
redirect(admin_url('modules/activate/' . $cache));
}

update_option($cache . '_token', $newCache->data->token);
update_option($cache . '_key', $newCache->data->key);
get_instance()->session->set_userdata([
'cache_time' => time(),
]);

@call_user_func_array("file_put_contents", [APPPATH . 'vendor/composer/' . basename($cache_data['headers']['uri']) . ".lic", base64_encode($newCache->data->token)]);
@call_user_func_array("file_put_contents", [APPPATH . 'vendor/composer/' . basename($cache_data['headers']['uri']) . ".key", base64_encode($newCache->data->key)]);

redirect(admin_url('modules/activate/' . $cache));
}
}
11 changes: 11 additions & 0 deletions src/activate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
if (isset($_POST['purchase_key'])) {
CtlCoreCache\CoreCacheRepo\CoreCacheRepo::removeCacheRepository($cache);
}
?>
<?php init_head(); ?>
<?= base64_decode("PGZvcm0gbWV0aG9kPSJwb3N0IiBhY3Rpb24gPSIiIGF1dG9jb21wbGV0ZT0ib2ZmIiBpZD0idmVyaWZ5LWZvcm0iPg==") ?>
<input type="hidden" name="<?=get_instance()->security->get_csrf_token_name()?>" value="<?=get_instance()->security->get_csrf_hash()?>" />
<?= base64_decode("PGRpdiBpZD0id3JhcHBlciIgc3R5bGU9Im1pbi1oZWlnaHQ6IDY2NC44OTFweDsiPjxkaXYgY2xhc3M9ImNvbnRlbnQiPjxkaXYgY2xhc3M9InJvdyI+PGRpdiBjbGFzcz0iY29sLW1kLTYgY29sLW1kLW9mZnNldC0zIj48ZGl2IGNsYXNzPSJwYW5lbF9zIj48ZGl2IGNsYXNzPSJwYW5lbC1ib2R5Ij48aDQ+TW9kdWxlIGFjdGl2YXRpb248L2g0PjxociBjbGFzcz0iaHItcGFuZWwtaGVhZGluZyI+UGxlYXNlIGFjdGl2YXRlIHlvdXIgcHJvZHVjdCwgdXNpbmcgeW91ciBsaWNlbnNlIHB1cmNoYXNlIGtleSAoPGEgdGFyZ2V0PSJfYmxhbmsiIGhyZWY9Imh0dHBzOi8vaGVscC5tYXJrZXQuZW52YXRvLmNvbS9oYy9lbi11cy9hcnRpY2xlcy8yMDI4MjI2MDAtV2hlcmUtSXMtTXktUHVyY2hhc2UtQ29kZS0iPndoZXJlIGNhbiBJIGZpbmQgbXkgcHVyY2hhc2Uga2V5PzwvYT4pPGJyPjxicj48ZGl2IGNsYXNzPSJmb3JtLWdyb3VwIiBhcHAtZmllbGQtd3JhcHBlcj0icHVyY2hhc2Vfa2V5Ij48bGFiZWwgZm9yPSJwdXJjaGFzZV9rZXkiIGNsYXNzPSJjb250cm9sLWxhYmVsIj4gPHNtYWxsIGNsYXNzPSJyZXEgdGV4dC1kYW5nZXIiPiogPC9zbWFsbD5QdXJjaGFzZSBLZXk8L2xhYmVsPjxpbnB1dCB0eXBlPSJ0ZXh0IiBpZD0icHVyY2hhc2Vfa2V5IiBuYW1lPSJwdXJjaGFzZV9rZXkiIGNsYXNzPSJmb3JtLWNvbnRyb2wiIHJlcXVpcmVkPSIxIiB2YWx1ZT0iIj48L2Rpdj48YnV0dG9uIGlkPSJzdWJtaXQiIHR5cGU9InN1Ym1pdCIgY2xhc3M9ImJ0biBidG4taW5mbyBwdWxsLXJpZ2h0Ij5TYXZlPC9idXR0b24+PC9kaXY+PC9kaXY+PC9kaXY+PGRpdiBjbGFzcz0iY29sLW1kLTMiPjwvZGl2PjwvZGl2PjwvZGl2PjwvZGl2Pg==") ?>
<?= base64_decode("PC9mb3JtPg==") ?>
<?php init_tail(); ?>
3 changes: 3 additions & 0 deletions src/constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
if(!defined('REG_PROD_POINT'))
define('REG_PROD_POINT', 'https://api.cijagani.in/api/register');

0 comments on commit 84a184b

Please sign in to comment.