Replaced get_browser with a MUCH faster library.

This commit is contained in:
flash 2020-05-25 14:33:52 +00:00
parent 174d25d40f
commit 58f22c9d74
5 changed files with 118 additions and 8 deletions

View file

@ -6,7 +6,8 @@
"geoip2/geoip2": "~2.0",
"twig/extensions": "^1.5",
"jublonet/codebird-php": "^3.1",
"chillerlan/php-qrcode": "^3.0"
"chillerlan/php-qrcode": "^3.0",
"whichbrowser/parser": "^2.0"
},
"autoload": {
"classmap": [

107
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "4e5f02da04a08b7717736b58d033f8d3",
"content-hash": "ce45fc99f5187b5f6e6e63b3bbd89ce9",
"packages": [
{
"name": "chillerlan/php-qrcode",
@ -684,6 +684,52 @@
"homepage": "https://github.com/maxmind/web-service-common-php",
"time": "2020-05-06T14:07:26+00:00"
},
{
"name": "psr/cache",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/cache.git",
"reference": "d11b50ad223250cf17b86e38383413f5a6764bf8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/cache/zipball/d11b50ad223250cf17b86e38383413f5a6764bf8",
"reference": "d11b50ad223250cf17b86e38383413f5a6764bf8",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Cache\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for caching libraries",
"keywords": [
"cache",
"psr",
"psr-6"
],
"time": "2016-08-06T20:24:11+00:00"
},
{
"name": "swiftmailer/swiftmailer",
"version": "v6.2.3",
@ -1158,6 +1204,65 @@
"templating"
],
"time": "2020-02-11T15:31:23+00:00"
},
{
"name": "whichbrowser/parser",
"version": "v2.0.42",
"source": {
"type": "git",
"url": "https://github.com/WhichBrowser/Parser-PHP.git",
"reference": "4899110cd2f87b01e04ced62dbb9dec541031dee"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/WhichBrowser/Parser-PHP/zipball/4899110cd2f87b01e04ced62dbb9dec541031dee",
"reference": "4899110cd2f87b01e04ced62dbb9dec541031dee",
"shasum": ""
},
"require": {
"php": ">=5.4.0",
"psr/cache": "^1.0"
},
"require-dev": {
"icomefromthenet/reverse-regex": "0.0.6.3",
"phpunit/php-code-coverage": "^2.2 || ^3.0",
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0",
"satooshi/php-coveralls": "^1.0",
"squizlabs/php_codesniffer": "2.5.*",
"symfony/yaml": "~2.8 || ~3.4 || ~4.2 || ~5.0"
},
"suggest": {
"cache/array-adapter": "Allows testing of the caching functionality"
},
"type": "library",
"autoload": {
"psr-4": {
"WhichBrowser\\": [
"src/",
"tests/src/"
]
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Niels Leenheer",
"email": "niels@leenheer.nl",
"role": "Developer"
}
],
"description": "Useragent sniffing library for PHP",
"homepage": "http://whichbrowser.net",
"keywords": [
"browser",
"sniffing",
"ua",
"useragent"
],
"time": "2020-02-12T10:54:23+00:00"
}
],
"packages-dev": [],

View file

@ -18,12 +18,12 @@ final class TwigMisuzu extends Twig_Extension {
new Twig_Filter('perms_check', 'perms_check'),
new Twig_Filter('bg_settings', 'user_background_settings_strings'),
new Twig_Filter('clamp', 'clamp'),
new Twig_Filter('as_platform', fn(string $userAgent) => (new \WhichBrowser\Parser($userAgent))->toString()),
];
}
public function getFunctions() {
return [
new Twig_Function('get_browser', 'get_browser'),
new Twig_Function('url_construct', 'url_construct'),
new Twig_Function('warning_has_duration', 'user_warning_has_duration'),
new Twig_Function('url', 'url'),

View file

@ -4,6 +4,7 @@ namespace Misuzu\Users;
use Misuzu\DB;
use Misuzu\Pagination;
use Misuzu\Net\IPAddress;
use WhichBrowser\Parser as UserAgentParser;
class UserLoginAttempt {
// Database fields
@ -16,6 +17,7 @@ class UserLoginAttempt {
private $user = null;
private $userLookedUp = false;
private $uaInfo = null;
public const TABLE = 'login_attempts';
private const QUERY_SELECT = 'SELECT %1$s FROM `' . DB::PREFIX . self::TABLE . '` AS '. self::TABLE;
@ -58,6 +60,11 @@ class UserLoginAttempt {
public function getUserAgent(): string {
return $this->attempt_user_agent;
}
public function getUserAgentInfo(): UserAgentParser {
if($this->uaInfo === null)
$this->uaInfo = new UserAgentParser($this->getUserAgent());
return $this->uaInfo;
}
public static function remaining(?string $remoteAddr = null): int {
$remoteAddr = $ipAddress ?? IPAddress::remote();

View file

@ -122,7 +122,6 @@
{% macro user_session(session, is_current_session) %}
{% from '_layout/input.twig' import input_hidden, input_csrf, input_checkbox_raw %}
{% set browser = get_browser(session.session_user_agent) %}
<div class="settings__session{% if is_current_session %} settings__session--current{% endif %}" id="session-{{ session.session_id }}">
<div class="settings__session__container">
@ -130,7 +129,7 @@
<div class="flag flag--{{ session.session_country|lower }} settings__session__flag" title="{{ session.session_country|country_name }}">{{ session.session_country }}</div>
<div class="settings__session__description">
{{ browser.browser }} on {{ browser.platform }}
{{ session.session_user_agent|default('')|as_platform }}
</div>
<form class="settings__session__actions" method="post" action="{{ url('settings-sessions') }}">
@ -211,15 +210,13 @@
{% endmacro %}
{% macro user_login_attempt(attempt) %}
{% set browser = get_browser(attempt.userAgent) %}
<div class="settings__login-attempt{% if not attempt.success %} settings__login-attempt--failed{% endif %}">
<div class="settings__login-attempt__container">
<div class="settings__login-attempt__important">
<div class="flag flag--{{ attempt.country|lower }} settings__login-attempt__flag" title="{{ attempt.countryName }}">{{ attempt.country }}</div>
<div class="settings__login-attempt__description">
{{ browser.browser }} on {{ browser.platform }}
{{ attempt.userAgentInfo.toString }}
</div>
</div>