Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Commit

Permalink
perf(ajax requests): to not instantiate WHMCS_ClientArea class
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiSchwarz-cnic committed Oct 11, 2021
1 parent b83e0d8 commit 6b4c2a5
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 55 deletions.
15 changes: 0 additions & 15 deletions modules/addons/ispapidomaincheck/init.inc.php

This file was deleted.

28 changes: 15 additions & 13 deletions modules/addons/ispapidomaincheck/ispapidomaincheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ function ispapidomaincheck_upgrade($vars)
*/
function ispapidomaincheck_clientarea($vars)
{
global $perf;

$perf["addon"] = [
"start" => microtime(true)
];
add_hook('ClientAreaHeadOutput', 1, function ($vars) {
$cfg = ispapidomaincheck_config();
$version = $cfg["version"];
Expand All @@ -102,12 +107,13 @@ function ispapidomaincheck_clientarea($vars)
<script>const wr = "{$wr}";</script>
<script src="{$wr}/modules/addons/ispapidomaincheck/lib/Client/assets/client.all.min.js?t={$version}"></script>
<link href="{$wr}/modules/addons/ispapidomaincheck/lib/Client/assets/client.all.min.css?t={$version}" rel="stylesheet" type="text/css" />
HTML;
});

//get default currency as fallback
$_SESSION["currency"] = DCHelper::GetCustomerCurrency();
if (!isset($_SESSION["currency"])) {
$_SESSION["currency"] = DCHelper::GetCustomerCurrency();
}

//save the language in the session if not already set
if (!isset($_SESSION["Language"])) {
Expand All @@ -117,12 +123,7 @@ function ispapidomaincheck_clientarea($vars)
//nodata=1 -> do not return data, we just update the chosen currency in session
//WHMCS cares automatically about handling currency when provided in request
if (isset($_REQUEST["nodata"]) && $_REQUEST["nodata"] == 1) {
//respond
header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Content-type: application/json; charset=utf-8');
die(json_encode(["success" => true, "msg" => "currency updated in session"]));
exit();
return json_encode(["success" => true, "msg" => "currency updated in session"]);
}

//init smarty and call admin dispatcher
Expand All @@ -137,12 +138,13 @@ function ispapidomaincheck_clientarea($vars)
$dispatcher = new ClientDispatcher();
$r = $dispatcher->dispatch($_REQUEST['action'], $vars, $smarty);
if ($_REQUEST['action']) {
//send json response headers
header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Content-type: application/json; charset=utf-8');
//do not echo as this would add template html code around!
die(json_encode($r));
global $perf;
$perf["addon"]["end"] = $perf["script"]["end"] = microtime(true);
$perf["addon"]["rt"] = $perf["addon"]["end"] - $perf["addon"]["start"];
$perf["script"]["rt"] = $perf["script"]["end"] - $perf["script"]["start"];
header("X_PERF: " . json_encode($perf));
return json_encode($r);
}
return $r;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ClientDispatcher
*/
public function dispatch($action, $args, $smarty)
{
global $perf;
$perf["dispatcher"] = ["start" => microtime(true)];
if (!$action) {
// Default to index if no action specified
$action = 'index';
Expand All @@ -30,10 +32,12 @@ public function dispatch($action, $args, $smarty)

// Verify requested action is valid and callable
if (is_callable([$controller, $action])) {
$perf["dispatcher"]["end"] = microtime(true);
$perf["dispatcher"]["rt"] = $perf["dispatcher"]["end"] - $perf["dispatcher"]["start"];
return $controller->$action($args, $smarty);
}
// action error
$smarty->assign("error", $args['_lang']['actionerror']);
$smarty->assign("error", $args['_lang']['actionerror']);
return $smarty->fetch('error.tpl');
}
}
6 changes: 6 additions & 0 deletions modules/addons/ispapidomaincheck/lib/Client/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public function index($vars, $smarty)
*/
public function checkdomains($vars, $smarty)
{
global $perf;
$perf["controller"] = [
"start" => microtime(true)
];
ignore_user_abort(false);
$data = json_decode(file_get_contents('php://input'), true); //convert JSON into array
$cmd = [
Expand Down Expand Up @@ -222,6 +226,8 @@ public function checkdomains($vars, $smarty)
file_put_contents($file, json_encode($current, JSON_PRETTY_PRINT));
}*/
ksort($res["results"]);
$perf["controller"]["end"] = microtime(true);
$perf["controller"]["rt"] = $perf["controller"]["end"] - $perf["controller"]["start"];
return $res;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1421,10 +1421,10 @@ const DomainSearch = function () {
cacheJobID: null,
maxCacheTTL: 600000, // 10 minutes in ms
base: 2,
initExp: 1,
maxExp: 4,
maxGroupsPerPage: 3,
maxEntriesPerPage: 14, // (2 + 4 + 8)
initExp: 0,
maxExp: 3,
//maxGroupsPerPage: 3,
maxEntriesPerPage: 15, // (1 + 2 + 4 + 8)
// to know if a searchresult corresponds to the search string
searchString: {IDN: '', PC: ''},
};
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const DomainSearch = function () {
cacheJobID: null,
maxCacheTTL: 600000, // 10 minutes in ms
base: 2,
initExp: 1,
maxExp: 4,
maxGroupsPerPage: 3,
maxEntriesPerPage: 14, // (2 + 4 + 8)
initExp: 0,
maxExp: 3,
//maxGroupsPerPage: 3,
maxEntriesPerPage: 15, // (1 + 2 + 4 + 8)
// to know if a searchresult corresponds to the search string
searchString: {IDN: '', PC: ''},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
use WHMCS\Module\Registrar\Ispapi\Helper;

$path = implode(DIRECTORY_SEPARATOR, [ROOTDIR,"modules","registrars","ispapi","lib","Helper.php"]);
if (file_exists($path)) {
require_once($path);
} else {
die('Please install the latest version of our <a href="https://github.com/hexonet/whmcs-ispapi-registrar/raw/master/whmcs-ispapi-registrar-latest.zip">ISPAPI Registrar Module</a>.');
if (!file_exists($path)) {
die("Please install the latest version of our <a href=\"https://github.com/hexonet/whmcs-ispapi-registrar/raw/master/whmcs-ispapi-registrar-latest.zip\">ISPAPI Registrar Module</a>.");
}
require_once($path);

/**
* PHP DCHelper Class
Expand Down
53 changes: 40 additions & 13 deletions mydomainchecker.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
<?php

use WHMCS\Module\Addon\ispapidomaincheck\DCHelper;

error_reporting(E_ALL);
ini_set("display_errors", 1);

// Handle "Transfer" button from the WHMCS default Homepage
if (!empty($_POST["transfer"])) {
header("Location: cart.php?a=add&domain=transfer&query=" . $_POST["domain"]);
exit();
}

global $perf;

define("CLIENTAREA", true);
use WHMCS\Module\Addon\ispapidomaincheck\DCHelper;

$perf = [
"script" => [
"start" => microtime(true),
]
];

// Find the correct path of the init.php file, based on the way we are integrating the module
// (via symlinks or copy/paste), the path is different.
$perf["init.php"] = ["start" => microtime(true)];
$isXHR = (
isset($_REQUEST["action"])
|| (
isset($_REQUEST["nodata"])
&& $_REQUEST["nodata"] == 1
)
);
require "init.php";

// load DCHelper class
$path = implode(DIRECTORY_SEPARATOR, [ROOTDIR, "modules", "addons", "ispapidomaincheck", "lib", "Common", "DCHelper.class.php"]);
if (!file_exists($path)) {
exit('Missing dependency `ISPAPI Registrar Module`. Please download and install it from <a href="https://github.com/hexonet/whmcs-ispapi-registrar/raw/master/whmcs-ispapi-registrar-latest.zip">github</a>.');
}
require_once($path);
$perf["init.php"]["end"] = microtime(true);
$perf["init.php"]["rt"] = $perf["init.php"]["end"] - $perf["init.php"]["start"];

$ca = new WHMCS_ClientArea();
$ca->setPageTitle(Lang::trans("domaintitle"));
$ca->addToBreadCrumb('index.php', Lang::trans('globalsystemname'));
$ca->addToBreadCrumb('mydomainchecker.php', Lang::trans("domaintitle"));
$ca->initPage();
// load DCHelper class
require_once(implode(DIRECTORY_SEPARATOR, [ROOTDIR, "modules", "addons", "ispapidomaincheck", "lib", "Common", "DCHelper.class.php"]));

// Include module file
$modulepath = implode(DIRECTORY_SEPARATOR, [ROOTDIR,"modules","addons","ispapidomaincheck","ispapidomaincheck.php"]);
Expand All @@ -36,7 +49,7 @@
// Call clientarea function
$modulevars = DCHelper::getAddOnConfigurationValue('ispapidomaincheck');
$language = (isset($_SESSION["Language"]) ? $_SESSION["Language"] : "english");
$langpath = ROOTDIR . DIRECTORY_SEPARATOR . "modules" . DIRECTORY_SEPARATOR . "addons" . DIRECTORY_SEPARATOR . "ispapidomaincheck" . DIRECTORY_SEPARATOR . "lang" . DIRECTORY_SEPARATOR;
$langpath = implode(DIRECTORY_SEPARATOR, [ROOTDIR, "modules", "addons", "ispapidomaincheck", "lang", ""]);
$file = $langpath . $language . ".php";
if (file_exists($file)) {
include($file);
Expand All @@ -49,6 +62,20 @@
$modulevars
);

//respond XHR Requests in JSON
if ($isXHR) {
header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Content-type: application/json; charset=utf-8');
echo $results;
exit();
}

$ca = new WHMCS_ClientArea();
$ca->setPageTitle(Lang::trans("domaintitle"));
$ca->addToBreadCrumb('index.php', Lang::trans('globalsystemname'));
$ca->addToBreadCrumb('mydomainchecker.php', Lang::trans("domaintitle"));
$ca->initPage();
if (is_array($results["vars"])) {
foreach ($results["vars"] as $k => $v) {
$smartyvalues[$k] = $v;
Expand Down

0 comments on commit 6b4c2a5

Please sign in to comment.