init
This commit is contained in:
當前提交
7f228274e1
|
@ -0,0 +1,2 @@
|
|||
/vendor
|
||||
/.env
|
|
@ -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"
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
RewriteEngine on
|
||||
RewriteCond %{SCRIPT_FILENAME} !-f
|
||||
RewriteCond %{SCRIPT_FILENAME} !-d
|
||||
RewriteCond %{SCRIPT_FILENAME} !-l
|
||||
RewriteRule ^(.*)$ index.php/$1
|
|
@ -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…
Reference in New Issue