Skip to content

Commit

Permalink
Publishable config
Browse files Browse the repository at this point in the history
  • Loading branch information
hisorange committed Feb 27, 2020
1 parent ce7c22e commit 1109cc7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,34 @@ Every call on the **Browser** facade is proxied to a result object, so the follo
| Browser::isIEVersion() | Compares to a given IE version | _(boolean)_ |
| Browser::isEdge() | Is this a microsoft edge browser. | _(boolean)_ |

### Configuration, personalization

---

If You are using Laravel then after installation run the following command:

```sh
// Will copy a config file to ~/app/config/browser-detect.php
php artisan vendor:publish
```

For standalone mode to apply Your custom configuration:

```php
use hisorange\BrowserDetect\Parser;

$browser = new Parser(null, null, [
'cache' => [
'interval' => 86400 // This will overide the default configuration.
]
]);

$result = $browser->detect();
```

Since the package aims to be easy to use, there is not much to configure.
But You can customize the cache and security values.

### Advanced Usage Information

---
Expand Down
20 changes: 20 additions & 0 deletions config/browser-detect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
return [
'cache' => [
/**
* Interval in seconds, as how long a result should be cached.
*/
'interval' => 10080,
/**
* Cache prefix, the user agent string will be hashed and appended at the end.
*/
'prefix' => 'bd4_'
],
'security' => [
/**
* Byte length where the header is cut off, if some attacker sends a long header
* then the library will make a cut this byte point.
*/
'max-header-length' => 2048,
]
];
25 changes: 21 additions & 4 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class Parser implements ParserInterface
*/
protected $runtime;

/**
* Parsing configurations.
*
* @var array
*/
protected $config;

/**
* Singleton used in standalone mode.
*
Expand All @@ -46,8 +53,9 @@ class Parser implements ParserInterface
*
* @param CacheManager $cache
* @param Request $request
* @param array $config
*/
public function __construct($cache = null, $request = null)
public function __construct($cache = null, $request = null, array $config = [])
{
if ($cache !== null) {
if ($cache instanceof CacheManager) {
Expand All @@ -65,6 +73,11 @@ public function __construct($cache = null, $request = null)
}
}

$this->config = array_merge_recursive(
require(__DIR__ . '/../config/browser-detect.php'),
$config
);

$this->runtime = [];
}

Expand Down Expand Up @@ -115,7 +128,11 @@ public static function __callStatic(string $method, array $params)
public function detect(): ResultInterface
{
// Cuts the agent string at 2048 byte, anything longer will be a DoS attack.
$userAgentString = substr($this->getUserAgentString(), 0, 2048);
$userAgentString = substr(
$this->getUserAgentString(),
0,
$this->config['security']['max-header-length']
);

return $this->parse($userAgentString);
}
Expand Down Expand Up @@ -147,7 +164,7 @@ public function parse(string $agent): ResultInterface
if ($this->cache !== null) {
$result = $this->cache->remember(
$key,
10080,
$this->config['cache']['interval'],
function () use ($agent) {
return $this->process($agent);
}
Expand All @@ -170,7 +187,7 @@ function () use ($agent) {
*/
protected function makeHashKey(string $agent): string
{
return 'bd4_' . md5($agent);
return $this->config['cache']['prefix'] . md5($agent);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class ServiceProvider extends BaseServiceProvider
public function boot(): void
{
$this->registerDirectives();

$this->publishes([
__DIR__ . '/../config/browser-detect.php' => config_path('browser-detect.php'),
]);
}

/**
Expand Down Expand Up @@ -67,7 +71,7 @@ function ($fn) {
public function register(): void
{
$this->app->singleton('browser-detect', function ($app) {
return new Parser($app->make('cache'), $app->make('request'));
return new Parser($app->make('cache'), $app->make('request'), $app->make('config')['browser-detect'] ?? []);
});
}
}

0 comments on commit 1109cc7

Please sign in to comment.