| Server IP : 167.99.224.18 / Your IP : 216.73.216.136 Web Server : Apache/2.4.41 (Ubuntu) System : Linux wordpress-ubuntu-s-1vcpu-1gb-nyc1-01 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 User : root ( 0) PHP Version : 8.0.25 Disable Function : 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_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/wp-content/plugins-old/matomo/plugins/WordPress/ |
Upload File : |
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\WordPress;
use Piwik\AuthResult;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Log\LoggerInterface;
use Piwik\Plugins\UsersManager\Model;
use Piwik\SettingsServer;
use Piwik\Tracker\TrackerConfig;
use WpMatomo\User;
if (!defined( 'ABSPATH')) {
exit; // if accessed directly
}
class Auth extends \Piwik\Plugins\Login\Auth
{
public function getName()
{
return 'WordPress';
}
public function authenticate()
{
// authenticate app password provided via Authorization header. for tracking,
// a dummy token_auth is still required.
$result = $this->authWithAppPassword();
if (!empty($result)) {
return $result;
}
// UI request authentication
$isUserLoggedIn = function_exists('is_user_logged_in') && is_user_logged_in();
if ($isUserLoggedIn) {
if (is_null($this->login) && empty($this->hashedPassword)) {
// api authentication using token
return parent::authenticate();
}
} else if ($this->isAppPasswordInTokenAuthAllowed()) {
$result = $this->authApiWithTokenAuthAppPassword();
if (!empty($result)) {
return $result;
}
}
$login = 'anonymous';
return new AuthResult(AuthResult::FAILURE, $login, $this->token_auth);
}
private function authWithAppPassword()
{
if (!function_exists('wp_validate_application_password')) {
return null;
}
$callback = function () { return true; };
add_filter('application_password_is_api_request', $callback);
try {
$loggedInUserId = wp_validate_application_password(false);
$isUserLoggedIn = $loggedInUserId !== false;
} finally {
remove_filter('application_password_is_api_request', $callback);
}
if (!$isUserLoggedIn) {
return null;
}
$login = User::get_matomo_user_login($loggedInUserId);
$userModel = new Model();
$matomoUser = $userModel->getUser($login);
if (empty($matomoUser)) {
return null;
}
$code = ((int) $matomoUser['superuser_access']) ? AuthResult::SUCCESS_SUPERUSER_AUTH_CODE : AuthResult::SUCCESS;
return new AuthResult($code, $login, $this->token_auth);
}
private function isAppPasswordInTokenAuthAllowed()
{
$wordPressConfig = Config::getInstance()->WordPress;
$allowed = !empty( $wordPressConfig['allow_app_password_as_token_auth'] ) && strval( $wordPressConfig['allow_app_password_as_token_auth'] ) === '1';
return $allowed;
}
private function authApiWithTokenAuthAppPassword()
{
$tokenAuth = $this->token_auth;
if (empty($tokenAuth)) {
return null;
}
$logger = StaticContainer::get(LoggerInterface::class);
if (!function_exists('wp_validate_application_password')) {
$logger->debug('WordPress\\Auth: wp_validate_application_password does not exist');
return null;
}
$parts = explode(':', $tokenAuth);
if (count($parts) !== 2) {
$logger->debug('WordPress\\Auth: app password provided in token_auth has incorrect format, expected "username:apppassword".');
return null;
}
if (
empty($_SERVER['REQUEST_METHOD'])
|| strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST'
) {
throw new \Exception('Invalid token auth or token auth was not provided as a POST parameter.');
}
[$user, $pass] = $parts;
$callback = function () { return true; };
add_filter('application_password_is_api_request', $callback);
try {
$authenticated = wp_authenticate_application_password(null, $user, $pass);
if (!($authenticated instanceof \WP_User)) {
return null;
}
$loggedInUserId = $authenticated->ID;
} finally {
remove_filter('application_password_is_api_request', $callback);
}
$login = User::get_matomo_user_login($loggedInUserId);
$userModel = new Model();
$matomoUser = $userModel->getUser($login);
if (empty($matomoUser)) {
return null;
}
$code = ((int) $matomoUser['superuser_access']) ? AuthResult::SUCCESS_SUPERUSER_AUTH_CODE : AuthResult::SUCCESS;
return new AuthResult($code, $login, $this->token_auth);
}
}