| Server IP : 38.190.208.107 / Your IP : 216.73.217.46 Web Server : nginx/1.28.1 System : Linux ht2026040333114 6.1.0-10-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.37-1 (2023-07-03) x86_64 User : root ( 0) PHP Version : 8.2.28 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv,eval,assert,passthru,system,exec,shell_exec,popen,proc_open MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /www/wwwroot/wp.3/wp-content/plugins/wp-statistics/src/Traits/ |
Upload File : |
<?php
namespace WP_Statistics\Traits;
use Exception;
use WP_STATISTICS\User;
use WP_Statistics\Utils\Request;
trait AjaxUtilityTrait
{
/**
* Verifies if the current request is an AJAX request.
*
* If the request is not an AJAX request, it will exit the script.
*
* @return void
*/
protected function verifyAjaxRequest()
{
if (!Request::isFrom('ajax')) {
die(esc_html__('Request is not a valid AJAX request. Please try again!', 'wp-statistics'));
}
}
/**
* Verify nonce in the request.
*
* This function will check if the given nonce is valid for the given action.
* If the nonce is invalid or expired, it will return a 403 error response with a message.
*
* @param int|string $action The action name to check the nonce against. Defaults to -1.
* @param string $field The field name to check for the nonce. Defaults to '_wpnonce'.
*
* @throws Exception If the nonce is invalid or expired.
*/
protected function verifyNonce($action = -1, $field = '_wpnonce')
{
$nonce = Request::get($field);
if (!wp_verify_nonce($nonce, $action)) {
throw new Exception(esc_html__('The request does not contain a valid nonce. Please try again.', 'wp-statistics'), 403);
}
}
/**
* Checks if the current user has the specified capability.
*
* If the user does not have the capability, it will return a 403 error response with a message.
*
* @param string $cap The capability to check.
*/
protected function checkCapability($cap)
{
if (!User::Access($cap)) {
throw new Exception(esc_html__('You do not have permission to perform this action. Please contact an administrator.', 'wp-statistics'), 403);
}
}
/**
* Checks if the AJAX request is valid and comes from the admin dashboard.
*
* @param string $action The action name to check.
* @param string $field The field name to check.
*
* @throws Exception If the request is invalid or expired nonce.
*/
protected function checkAdminReferrer($action = -1, $field = '_wpnonce')
{
$nonce = Request::get($field);
$adminUrl = strtolower(admin_url());
$referer = strtolower(wp_get_referer());
if (!wp_verify_nonce($nonce, $action) || strpos($referer, $adminUrl) !== 0) {
throw new Exception(esc_html__('The request does not come from the admin dashboard or is invalid.', 'wp-statistics'), 403);
}
}
}