defined('WPINC') || exit();
const CLOUD_SERVER = 'https://api.quic.cloud';
const CLOUD_IPS = 'https://quic.cloud/ips';
const CLOUD_SERVER_DASH = 'https://my.quic.cloud';
const CLOUD_SERVER_WP = 'https://wpapi.quic.cloud';
const SVC_D_NODES = 'd/nodes';
const SVC_D_SYNC_CONF = 'd/sync_conf';
const SVC_D_USAGE = 'd/usage';
const SVC_D_SETUP_TOKEN = 'd/get_token';
const SVC_D_DEL_CDN_DNS = 'd/del_cdn_dns';
const SVC_PAGE_OPTM = 'page_optm';
const SVC_QUEUE = 'queue';
const SVC_IMG_OPTM = 'img_optm';
const SVC_HEALTH = 'health';
const BM_IMG_OPTM_PRIO = 16;
const BM_IMG_OPTM_JUMBO_GROUP = 32;
const IMG_OPTM_JUMBO_GROUP = 1000;
const IMG_OPTM_DEFAULT_GROUP = 200;
const IMGOPTM_TAKEN = 'img_optm-taken';
const TTL_NODE = 3; // Days before node expired
const EXPIRATION_REQ = 300; // Seconds of min interval between two unfinished requests
const EXPIRATION_TOKEN = 900; // Min intval to request a token 15m
const TTL_IPS = 3; // Days for node ip list cache
const API_REPORT = 'wp/report';
const API_VER = 'ver_check';
const API_BETA_TEST = 'beta_test';
private static $CENTER_SVC_SET = array(
private static $WP_SVC_SET = array(self::API_NEWS, self::API_VER, self::API_BETA_TEST);
// No api key needed for these services
private static $_PUB_SVC_SET = array(self::API_NEWS, self::API_REPORT, self::API_VER, self::API_BETA_TEST);
private static $_QUEUE_SVC_SET = array(self::SVC_UCSS, self::SVC_VPI);
public static $SERVICES_LOAD_CHECK = array(
public static $SERVICES = array(
const TYPE_CLEAR_PROMO = 'clear_promo';
const TYPE_REDETECT_CLOUD = 'redetect_cloud';
const TYPE_CLEAR_CLOUD = 'clear_cloud';
const TYPE_GEN_KEY = 'gen_key';
const TYPE_LINK = 'link';
const TYPE_SYNC_USAGE = 'sync_usage';
public function __construct()
$this->_setup_token = $this->conf(self::O_QC_TOKEN);
$this->_summary = self::get_summary();
private function _api_key()
return $this->conf(self::O_API_KEY);
* Show latest commit version always if is on dev
public function check_dev_version()
if (!preg_match('/[^\d\.]/', Core::VER)) {
$last_check = empty($this->_summary['last_request.' . self::API_VER]) ? 0 : $this->_summary['last_request.' . self::API_VER];
if (time() - $last_check > 86400) {
$auto_v = self::version_check('dev');
if (!empty($auto_v['dev'])) {
self::save_summary(array('version.dev' => $auto_v['dev']));
if (empty($this->_summary['version.dev'])) {
self::debug('Latest dev version ' . $this->_summary['version.dev']);
if (version_compare($this->_summary['version.dev'], Core::VER, '<=')) {
require_once LSCWP_DIR . 'tpl/banner/new_version_dev.tpl.php';
public static function version_check($src = false)
'v' => defined('LSCWP_CUR_V') ? LSCWP_CUR_V : '',
if (defined('LITESPEED_ERR')) {
$req_data['err'] = base64_encode(!is_string(LITESPEED_ERR) ? \json_encode(LITESPEED_ERR) : LITESPEED_ERR);
$data = self::post(self::API_VER, $req_data);
if (empty($this->_summary['news.new'])) {
if (!empty($this->_summary['news.plugin']) && Activation::cls()->dash_notifier_is_plugin_active($this->_summary['news.plugin'])) {
require_once LSCWP_DIR . 'tpl/banner/cloud_news.tpl.php';
private function _update_news()
if (!empty($this->_summary['news.utime']) && time() - $this->_summary['news.utime'] < 86400 * 7) {
self::save_summary(array('news.utime' => time()));
$data = self::get(self::API_NEWS);
if (empty($data['id'])) {
if (!empty($this->_summary['news.id']) && $this->_summary['news.id'] == $data['id']) {
$this->_summary['news.id'] = $data['id'];
$this->_summary['news.plugin'] = !empty($data['plugin']) ? $data['plugin'] : '';
$this->_summary['news.title'] = !empty($data['title']) ? $data['title'] : '';
$this->_summary['news.content'] = !empty($data['content']) ? $data['content'] : '';
$this->_summary['news.zip'] = !empty($data['zip']) ? $data['zip'] : '';
$this->_summary['news.new'] = 1;
if ($this->_summary['news.plugin']) {
$plugin_info = Activation::cls()->dash_notifier_get_plugin_info($this->_summary['news.plugin']);
if ($plugin_info && !empty($plugin_info->name)) {
$this->_summary['news.plugin_name'] = $plugin_info->name;
* Check if contains a package in a service or not
public function has_pkg($service, $pkg)
if (!empty($this->_summary['usage.' . $service]['pkgs']) && $this->_summary['usage.' . $service]['pkgs'] & $pkg) {
* Get allowance of current service
public function allowance($service, &$err = false)
// Only auto sync usage at most one time per day
if (empty($this->_summary['last_request.' . self::SVC_D_USAGE]) || time() - $this->_summary['last_request.' . self::SVC_D_USAGE] > 86400) {
if (in_array($service, array(self::SVC_CCSS, self::SVC_UCSS, self::SVC_VPI))) {
$service = self::SVC_PAGE_OPTM;
if (empty($this->_summary['usage.' . $service])) {
$usage = $this->_summary['usage.' . $service];
// Image optm is always free
if ($service == self::SVC_IMG_OPTM) {
$allowance_max = self::IMG_OPTM_DEFAULT_GROUP;
if (!empty($usage['pkgs']) && $usage['pkgs'] & self::BM_IMG_OPTM_JUMBO_GROUP) {
$allowance_max = self::IMG_OPTM_JUMBO_GROUP;
$allowance = $usage['quota'] - $usage['used'];
if ($allowance_max && $allowance_max < $allowance) {
$allowance = $allowance_max;
// Daily limit @since 4.2
if (isset($usage['remaining_daily_quota']) && $usage['remaining_daily_quota'] >= 0 && $usage['remaining_daily_quota'] < $allowance) {
$allowance = $usage['remaining_daily_quota'];
$err = 'out_of_daily_quota';
// Check Pay As You Go balance
if (empty($usage['pag_bal'])) {
if ($allowance_max && $allowance_max < $usage['pag_bal']) {
return $usage['pag_bal'];
* Sync Cloud usage summary data
public function sync_usage()
$usage = $this->_post(self::SVC_D_USAGE);
self::debug('sync_usage ' . \json_encode($usage));
foreach (self::$SERVICES as $v) {
$this->_summary['usage.' . $v] = !empty($usage[$v]) ? $usage[$v] : false;
* Clear all existing cloud nodes for future reconnect
public function clear_cloud()
foreach (self::$SERVICES as $service) {
if (isset($this->_summary['server.' . $service])) {
unset($this->_summary['server.' . $service]);
if (isset($this->_summary['server_date.' . $service])) {
unset($this->_summary['server_date.' . $service]);
self::debug('Cleared all local service node caches');
* ping clouds to find the fastest node
public function detect_cloud($service, $force = false)
if (in_array($service, self::$CENTER_SVC_SET)) {
return self::CLOUD_SERVER;
if (in_array($service, self::$WP_SVC_SET)) {
return self::CLOUD_SERVER_WP;
// Check if the stored server needs to be refreshed
!empty($this->_summary['server.' . $service]) &&
!empty($this->_summary['server_date.' . $service]) &&
$this->_summary['server_date.' . $service] > time() - 86400 * self::TTL_NODE
return $this->_summary['server.' . $service];
if (!$service || !in_array($service, self::$SERVICES)) {
$msg = __('Cloud Error', 'litespeed-cache') . ': ' . $service;
Admin_Display::error($msg);
// Send request to Quic Online Service
$json = $this->_post(self::SVC_D_NODES, array('svc' => $this->_maybe_queue($service)));
// Check if get list correctly
if (empty($json['list']) || !is_array($json['list'])) {
self::debug('request cloud list failed: ', $json);
$msg = __('Cloud Error', 'litespeed-cache') . ": [Service] $service [Info] " . \json_encode($json);
Admin_Display::error($msg);
foreach ($json['list'] as $v) {
// Exclude possible failed 503 nodes
if (!empty($this->_summary['disabled_node']) && !empty($this->_summary['disabled_node'][$v]) && time() - $this->_summary['disabled_node'][$v] < 86400) {
$speed_list[$v] = Utility::ping($v);
self::debug('nodes are in 503 failed nodes');
self::debug('failed to ping all clouds');
// Random pick same time range ip (230ms 250ms)
$range_len = strlen($min);
$range_num = substr($min, 0, 1);
foreach ($speed_list as $node => $speed) {
if (strlen($speed) == $range_len && substr($speed, 0, 1) == $range_num) {
// Append the lower speed ones
elseif ($speed < $min * 4) {
$msg = __('Cloud Error', 'litespeed-cache') . ": [Service] $service [Info] " . __('No available Cloud Node.', 'litespeed-cache');
Admin_Display::error($msg);
self::debug('Closest nodes list', $valid_clouds);
if (in_array($service, self::$SERVICES_LOAD_CHECK)) {
$valid_cloud_loads = array();
foreach ($valid_clouds as $k => $v) {
$response = wp_remote_get($v, array('timeout' => 5, 'sslverify' => true));
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
self::debug('failed to do load checker: ' . $error_message);
$curr_load = \json_decode($response['body'], true);
if (!empty($curr_load['_res']) && $curr_load['_res'] == 'ok' && isset($curr_load['load'])) {
$valid_cloud_loads[$v] = $curr_load['load'];
if (!$valid_cloud_loads) {
$msg = __('Cloud Error', 'litespeed-cache') . ": [Service] $service [Info] " . __('No available Cloud Node after checked server load.', 'litespeed-cache');
Admin_Display::error($msg);
self::debug('Closest nodes list after load check', $valid_cloud_loads);
$qualified_list = array_keys($valid_cloud_loads, min($valid_cloud_loads));
$qualified_list = $valid_clouds;
$closest = $qualified_list[array_rand($qualified_list)];
self::debug('Chose node: ' . $closest);
// store data into option locally
$this->_summary['server.' . $service] = $closest;
$this->_summary['server_date.' . $service] = time();
return $this->_summary['server.' . $service];
* May need to convert to queue service
private function _maybe_queue($service)
if (in_array($service, self::$_QUEUE_SVC_SET)) {