mirror of
https://github.com/antonkomarev/github-profile-views-counter.git
synced 2026-07-29 00:32:40 -04:00
@@ -14,31 +14,28 @@ declare(strict_types=1);
|
||||
use Dotenv\Exception\InvalidPathException;
|
||||
use Komarev\GitHubProfileViewsCounter\BadgeImageRendererService;
|
||||
use Komarev\GitHubProfileViewsCounter\CounterRepositoryFactory;
|
||||
use Komarev\GitHubProfileViewsCounter\Request;
|
||||
use Komarev\GitHubProfileViewsCounter\Username;
|
||||
|
||||
$appBasePath = realpath(__DIR__ . '/..');
|
||||
|
||||
require $appBasePath . '/vendor/autoload.php';
|
||||
|
||||
array_walk_recursive($_GET, function (&$input) {
|
||||
$input = htmlspecialchars($input, ENT_NOQUOTES, 'UTF-8', false);
|
||||
});
|
||||
$request = Request::of($_SERVER, $_GET);
|
||||
|
||||
$username = $_GET['username'] ?? '';
|
||||
$username = trim($username);
|
||||
$username = trim($request->username());
|
||||
|
||||
if ($username === '') {
|
||||
header('Location: https://github.com/antonkomarev/github-profile-views-counter');
|
||||
exit;
|
||||
}
|
||||
|
||||
$httpUserAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
|
||||
$isGitHubUserAgent = strpos($httpUserAgent, 'github-camo') === 0;
|
||||
$isGitHubUserAgent = strpos($request->userAgent(), 'github-camo') === 0;
|
||||
|
||||
$badgeLabel = $_GET['label'] ?? 'Profile views';
|
||||
$badgeMessageBackgroundFill = $_GET['color'] ?? 'blue';
|
||||
$badgeStyle = $_GET['style'] ?? 'flat';
|
||||
if (!in_array($badgeStyle, ['flat', 'flat-square', 'plastic', 'for-the-badge', 'pixel'])) {
|
||||
$badgeLabel = $request->badgeLabel() ?? 'Profile views';
|
||||
$badgeMessageBackgroundFill = $request->badgeColor() ?? 'blue';
|
||||
$badgeStyle = $request->badgeStyle() ?? 'flat';
|
||||
if (!in_array($badgeStyle, ['flat', 'flat-square', 'plastic', 'for-the-badge', 'pixel'], true)) {
|
||||
$badgeStyle = 'flat';
|
||||
}
|
||||
|
||||
|
||||
83
src/Request.php
Normal file
83
src/Request.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of GitHub Profile Views Counter.
|
||||
*
|
||||
* (c) Anton Komarev <anton@komarev.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Komarev\GitHubProfileViewsCounter;
|
||||
|
||||
final class Request
|
||||
{
|
||||
private string $userAgent;
|
||||
|
||||
private string $username;
|
||||
|
||||
private ?string $badgeLabel;
|
||||
|
||||
private ?string $badgeColor;
|
||||
|
||||
private ?string $badgeStyle;
|
||||
|
||||
public function __construct(
|
||||
string $userAgent,
|
||||
string $username,
|
||||
?string $badgeLabel,
|
||||
?string $badgeColor,
|
||||
?string $badgeStyle
|
||||
) {
|
||||
$this->userAgent = $userAgent;
|
||||
$this->username = $username;
|
||||
$this->badgeLabel = $badgeLabel;
|
||||
$this->badgeColor = $badgeColor;
|
||||
$this->badgeStyle = $badgeStyle;
|
||||
}
|
||||
|
||||
public static function of(
|
||||
array $server,
|
||||
array $get
|
||||
): self {
|
||||
array_walk_recursive($get, function (&$input) {
|
||||
$input = htmlspecialchars($input, ENT_NOQUOTES, 'UTF-8', false);
|
||||
});
|
||||
|
||||
return new self(
|
||||
$server['HTTP_USER_AGENT'] ?? '',
|
||||
$get['username'] ?? '',
|
||||
$get['label'] ?? null,
|
||||
$get['color'] ?? null,
|
||||
$get['style'] ?? null,
|
||||
);
|
||||
}
|
||||
|
||||
public function userAgent(): string
|
||||
{
|
||||
return $this->userAgent;
|
||||
}
|
||||
|
||||
public function username(): string
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function badgeLabel(): ?string
|
||||
{
|
||||
return $this->badgeLabel;
|
||||
}
|
||||
|
||||
public function badgeColor(): ?string
|
||||
{
|
||||
return $this->badgeColor;
|
||||
}
|
||||
|
||||
public function badgeStyle(): ?string
|
||||
{
|
||||
return $this->badgeStyle;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user