Skip to content

Commit

Permalink
feat: dark visitors analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricerenck committed Jun 11, 2024
1 parent df17085 commit 5acdd5a
Show file tree
Hide file tree
Showing 466 changed files with 5,691 additions and 48,667 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@
/~
/content/phpunit/.lock
/site/config
/.ddev
20 changes: 6 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
"psr-4": {
"mauricerenck\\DarkVisitors\\": "plugin"
},
"classmap": [
"plugin"
]
"classmap": ["plugin"]
},
"require": {
"getkirby/composer-installer": "^1.2",
"php": ">=8.0.0"
"php": ">=8.0.0",
"amphp/amp": "^3.0"
},
"require-dev": {
"getkirby/cms": "^4",
Expand All @@ -34,17 +33,10 @@
}
},
"scripts": {
"start": [
"Composer\\Config::disableProcessTimeout",
"php -S localhost:8000 kirby/router.php"
],
"start": ["Composer\\Config::disableProcessTimeout", "php -S localhost:8000 kirby/router.php"],
"test": "vendor/bin/phpunit --testdox --colors=always tests",
"build-test-package": "git archive HEAD -o dark-visitors.zip --worktree-attributes",
"build-composer": "composer install --no-dev --optimize-autoloader",
"build-release": [
"composer test",
"composer install --no-dev --optimize-autoloader",
"npm run build"
]
"build-release": ["composer install --no-dev --optimize-autoloader"]
}
}
}
155 changes: 154 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Kirby;
use Kirby\Http\Response;
use Amp;

@include_once __DIR__ . '/vendor/autoload.php';

Expand Down Expand Up @@ -39,5 +40,32 @@
return new Response(join("\n", $robotTxt), 'text/plain', 200);
},
],
[
'pattern' => '(:any)',
'method' => 'GET',
'action' => function () {
$request = kirby()->request();
$api = new Api();

if (option('mauricerenck.dark-visitors.analytics', false)) {
$requestPath = $request->path();
$requestMethod = $request->method();
$requestHeaders = $request->headers();

try {
$future = Amp\async(function () use ($api, $requestPath, $requestMethod, $requestHeaders) {
$result = $api->trackAgent($requestPath, $requestMethod, $requestHeaders);
return $result;
});

$future->await();
} catch (\Throwable $e) {
echo 'Error' . $e->getMessage();
}
}

$this->next();
},
],
],
]);
15 changes: 15 additions & 0 deletions plugin/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,19 @@ public function getLatestAgents()

return $request->content();
}

public function trackAgent($requestPath, $requestMethod, $requestHeaders)
{
$body = [
'request_path' => $requestPath,
'request_method' => $requestMethod,
'request_headers' => $requestHeaders,
];

return Remote::request('https://api.darkvisitors.com/visits', [
'method' => 'POST',
'headers' => ['Content-Type: application/json', 'Authorization: Bearer ' . $this->token],
'data' => json_encode($body),
]);
}
}
41 changes: 41 additions & 0 deletions vendor/amphp/amp/src/Cancellation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types=1);

namespace Amp;

/**
* Cancellations are simple objects that allow registering handlers to subscribe to cancellation requests.
*/
interface Cancellation
{
/**
* Subscribes a new handler to be invoked on a cancellation request.
*
* This handler might be invoked immediately in case the cancellation has already been requested. Any unhandled
* exceptions will be thrown into the event loop.
*
* @param \Closure(CancelledException) $callback Callback to be invoked on a cancellation request. Will receive a
* `CancelledException` as first argument that may be used to fail the operation.
*
* @return string Identifier that can be used to cancel the subscription.
*/
public function subscribe(\Closure $callback): string;

/**
* Unsubscribes a previously registered handler.
*
* The handler will no longer be called as long as this method isn't invoked from a subscribed callback.
*/
public function unsubscribe(string $id): void;

/**
* Returns whether cancellation has been requested yet.
*/
public function isRequested(): bool;

/**
* Throws the `CancelledException` if cancellation has been requested, otherwise does nothing.
*
* @throws CancelledException
*/
public function throwIfRequested(): void;
}
17 changes: 17 additions & 0 deletions vendor/amphp/amp/src/CancelledException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Amp;

/**
* Will be thrown in case an operation is cancelled.
*
* @see Cancellation
* @see DeferredCancellation
*/
class CancelledException extends \Exception
{
public function __construct(?\Throwable $previous = null)
{
parent::__construct("The operation was cancelled", 0, $previous);
}
}
26 changes: 26 additions & 0 deletions vendor/amphp/amp/src/Closable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace Amp;

interface Closable
{
/**
* Closes the resource, marking it as unusable.
* Whether pending operations are aborted or not is implementation dependent.
*/
public function close(): void;

/**
* Returns whether this resource has been closed.
*
* @return bool `true` if closed, otherwise `false`.
*/
public function isClosed(): bool;

/**
* Registers a callback that is invoked when this resource is closed.
*
* @param \Closure():void $onClose
*/
public function onClose(\Closure $onClose): void;
}
Loading

0 comments on commit 5acdd5a

Please sign in to comment.