Browse Source

init

master
Simon Vieille 4 months ago
commit
7f228274e1
Signed by: Simon Vieille <simon@deblan.fr> GPG Key ID: 03383D15A1D31745
6 changed files with 196 additions and 0 deletions
  1. 1
    0
      .env.dist
  2. 2
    0
      .gitignore
  3. 15
    0
      composer.json
  4. 88
    0
      src/functions/simple-router.php
  5. 5
    0
      web/.htaccess
  6. 85
    0
      web/index.php

+ 1
- 0
.env.dist View File

@@ -0,0 +1 @@
API_HOSTNAME=api.example.com

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
/vendor
/.env

+ 15
- 0
composer.json View File

@@ -0,0 +1,15 @@
{
"autoload": {
"files": [
"src/functions/simple-router.php"
]
},
"require": {
"j0k3r/graby": "^2.0",
"php-http/guzzle6-adapter": "^2.0",
"pecee/simple-router": "4.2.0.6",
"fusonic/linq": "dev-master",
"fusonic/opengraph": "^1.0",
"vlucas/phpdotenv": "^3.4"
}
}

+ 88
- 0
src/functions/simple-router.php View File

@@ -0,0 +1,88 @@
<?php

use Pecee\SimpleRouter\SimpleRouter as Router;
use Pecee\Http\Url;
use Pecee\Http\Response;
use Pecee\Http\Request;

/**
* Get url for a route by using either name/alias, class or method name.
*
* The name parameter supports the following values:
* - Route name
* - Controller/resource name (with or without method)
* - Controller class name
*
* When searching for controller/resource by name, you can use this syntax "route.name@method".
* You can also use the same syntax when searching for a specific controller-class "MyController@home".
* If no arguments is specified, it will return the url for the current loaded route.
*
* @param string|null $name
* @param string|array|null $parameters
* @param array|null $getParams
* @return \Pecee\Http\Url
* @throws \InvalidArgumentException
*/
function url(?string $name = null, $parameters = null, ?array $getParams = null): Url
{
return Router::getUrl($name, $parameters, $getParams);
}

/**
* @return \Pecee\Http\Response
*/
function response(): Response
{
return Router::response();
}

/**
* @return \Pecee\Http\Request
*/
function request(): Request
{
return Router::request();
}

/**
* Get input class
* @param string|null $index Parameter index name
* @param string|null $defaultValue Default return value
* @param array ...$methods Default methods
* @return \Pecee\Http\Input\InputHandler|array|string|null
*/
function input($index = null, $defaultValue = null, ...$methods)
{
if ($index !== null) {
return request()->getInputHandler()->value($index, $defaultValue, ...$methods);
}

return request()->getInputHandler();
}

/**
* @param string $url
* @param int|null $code
*/
function redirect(string $url, ?int $code = null): void
{
if ($code !== null) {
response()->httpCode($code);
}

response()->redirect($url);
}

/**
* Get current csrf-token
* @return string|null
*/
function csrf_token(): ?string
{
$baseVerifier = Router::router()->getCsrfVerifier();
if ($baseVerifier !== null) {
return $baseVerifier->getTokenProvider()->getToken();
}

return null;
}

+ 5
- 0
web/.htaccess View File

@@ -0,0 +1,5 @@
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1

+ 85
- 0
web/index.php View File

@@ -0,0 +1,85 @@
<?php

use Graby\Graby;
use Fusonic\OpenGraph\Consumer;
use Pecee\SimpleRouter\SimpleRouter;

require __DIR__.'/../vendor/autoload.php';

$dotenv = Dotenv\Dotenv::create(__DIR__.'/../');
$dotenv->load();

function isValidUrl( ? string $url, ? string $hostname) : bool
{
if ($url === null) {
return false;
}

if (trim($url) === '') {
return false;
}

if (filter_var($url, FILTER_VALIDATE_URL) === false) {
return false;
}

echo '<pre>', var_dump($hostname), '</pre>';
die;

return parse_url($url)['host'] !== $hostname;
}

function graby(string $url) : array
{
$graby = new Graby();
$result = $graby->fetchContent($url);

if (isset($result['headers'])) {
unset($result['headers']);
}

return $result;
}

function opengraph(string $url) : array
{
$consumer = new Consumer();
$object = $consumer->loadUrl($url);

return json_decode(json_encode($object), true);
}

SimpleRouter::get('/do/{parser}', function ($parser) {
$url = input()->get('url')->value;

if (!isValidUrl($url, getenv('API_HOSTNAME'))) {
return response()
->httpCode(400)
->json(['error' => 'Invalid URL']);
}

$parsers = [
'graby' => 'graby',
'opengraph' => 'opengraph',
];

if (!isset($parsers[$parser])) {
return response()
->httpCode(400)
->json(['error' => 'Invalid parser']);
}

try {
$result = call_user_func_array($parsers[$parser], [$url]);

return response()
->httpCode(200)
->json($result);
} catch (\Exception $e) {
return response()
->httpCode(500)
->json(['error' => $e->getMessage()]);
}
});

SimpleRouter::start();

Loading…
Cancel
Save