Browse Source

init

master
Simon Vieille 3 weeks 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 @@
1
+API_HOSTNAME=api.example.com

+ 2
- 0
.gitignore View File

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

+ 15
- 0
composer.json View File

@@ -0,0 +1,15 @@
1
+{
2
+    "autoload": {
3
+        "files": [
4
+            "src/functions/simple-router.php"
5
+        ]
6
+    },
7
+    "require": {
8
+        "j0k3r/graby": "^2.0",
9
+        "php-http/guzzle6-adapter": "^2.0",
10
+        "pecee/simple-router": "4.2.0.6",
11
+        "fusonic/linq": "dev-master",
12
+        "fusonic/opengraph": "^1.0",
13
+        "vlucas/phpdotenv": "^3.4"
14
+    }
15
+}

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

@@ -0,0 +1,88 @@
1
+<?php
2
+
3
+use Pecee\SimpleRouter\SimpleRouter as Router;
4
+use Pecee\Http\Url;
5
+use Pecee\Http\Response;
6
+use Pecee\Http\Request;
7
+
8
+/**
9
+ * Get url for a route by using either name/alias, class or method name.
10
+ *
11
+ * The name parameter supports the following values:
12
+ * - Route name
13
+ * - Controller/resource name (with or without method)
14
+ * - Controller class name
15
+ *
16
+ * When searching for controller/resource by name, you can use this syntax "route.name@method".
17
+ * You can also use the same syntax when searching for a specific controller-class "MyController@home".
18
+ * If no arguments is specified, it will return the url for the current loaded route.
19
+ *
20
+ * @param string|null $name
21
+ * @param string|array|null $parameters
22
+ * @param array|null $getParams
23
+ * @return \Pecee\Http\Url
24
+ * @throws \InvalidArgumentException
25
+ */
26
+function url(?string $name = null, $parameters = null, ?array $getParams = null): Url
27
+{
28
+    return Router::getUrl($name, $parameters, $getParams);
29
+}
30
+
31
+/**
32
+ * @return \Pecee\Http\Response
33
+ */
34
+function response(): Response
35
+{
36
+    return Router::response();
37
+}
38
+
39
+/**
40
+ * @return \Pecee\Http\Request
41
+ */
42
+function request(): Request
43
+{
44
+    return Router::request();
45
+}
46
+
47
+/**
48
+ * Get input class
49
+ * @param string|null $index Parameter index name
50
+ * @param string|null $defaultValue Default return value
51
+ * @param array ...$methods Default methods
52
+ * @return \Pecee\Http\Input\InputHandler|array|string|null
53
+ */
54
+function input($index = null, $defaultValue = null, ...$methods)
55
+{
56
+    if ($index !== null) {
57
+        return request()->getInputHandler()->value($index, $defaultValue, ...$methods);
58
+    }
59
+
60
+    return request()->getInputHandler();
61
+}
62
+
63
+/**
64
+ * @param string $url
65
+ * @param int|null $code
66
+ */
67
+function redirect(string $url, ?int $code = null): void
68
+{
69
+    if ($code !== null) {
70
+        response()->httpCode($code);
71
+    }
72
+
73
+    response()->redirect($url);
74
+}
75
+
76
+/**
77
+ * Get current csrf-token
78
+ * @return string|null
79
+ */
80
+function csrf_token(): ?string
81
+{
82
+    $baseVerifier = Router::router()->getCsrfVerifier();
83
+    if ($baseVerifier !== null) {
84
+        return $baseVerifier->getTokenProvider()->getToken();
85
+    }
86
+
87
+    return null;
88
+}

+ 5
- 0
web/.htaccess View File

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

+ 85
- 0
web/index.php View File

@@ -0,0 +1,85 @@
1
+<?php
2
+
3
+use Graby\Graby;
4
+use Fusonic\OpenGraph\Consumer;
5
+use Pecee\SimpleRouter\SimpleRouter;
6
+
7
+require __DIR__.'/../vendor/autoload.php';
8
+
9
+$dotenv = Dotenv\Dotenv::create(__DIR__.'/../');
10
+$dotenv->load();
11
+
12
+function isValidUrl( ? string $url, ? string $hostname) : bool
13
+{
14
+    if ($url === null) {
15
+        return false;
16
+    }
17
+
18
+    if (trim($url) === '') {
19
+        return false;
20
+    }
21
+
22
+    if (filter_var($url, FILTER_VALIDATE_URL) === false) {
23
+        return false;
24
+    }
25
+
26
+    echo '<pre>', var_dump($hostname), '</pre>';
27
+    die;
28
+
29
+    return parse_url($url)['host'] !== $hostname;
30
+}
31
+
32
+function graby(string $url) : array
33
+{
34
+    $graby = new Graby();
35
+    $result = $graby->fetchContent($url);
36
+
37
+    if (isset($result['headers'])) {
38
+        unset($result['headers']);
39
+    }
40
+
41
+    return $result;
42
+}
43
+
44
+function opengraph(string $url) : array
45
+{
46
+    $consumer = new Consumer();
47
+    $object = $consumer->loadUrl($url);
48
+
49
+    return json_decode(json_encode($object), true);
50
+}
51
+
52
+SimpleRouter::get('/do/{parser}', function ($parser) {
53
+    $url = input()->get('url')->value;
54
+
55
+    if (!isValidUrl($url, getenv('API_HOSTNAME'))) {
56
+        return response()
57
+            ->httpCode(400)
58
+            ->json(['error' => 'Invalid URL']);
59
+    }
60
+
61
+    $parsers = [
62
+        'graby' => 'graby',
63
+        'opengraph' => 'opengraph',
64
+    ];
65
+
66
+    if (!isset($parsers[$parser])) {
67
+        return response()
68
+            ->httpCode(400)
69
+            ->json(['error' => 'Invalid parser']);
70
+    }
71
+
72
+    try {
73
+        $result = call_user_func_array($parsers[$parser], [$url]);
74
+
75
+        return response()
76
+            ->httpCode(200)
77
+            ->json($result);
78
+    } catch (\Exception $e) {
79
+        return response()
80
+            ->httpCode(500)
81
+            ->json(['error' => $e->getMessage()]);
82
+    }
83
+});
84
+
85
+SimpleRouter::start();

Loading…
Cancel
Save