Vous êtes connecté en tant que anonymous Se Deconnecter
Browse code

Ajout de deux classe permettant de genrer des requète REST au sein de l'application

TODO: tester et mettre en place ces requètes dans le core du MVC afin qu'elles soient utilisable simplement et de facon protégé par un fichier de config

TODO: ajouter un plug-in symfony permettant de charger un utilisateur dans les apps a partir de l'authentification multiple

TODO: lire les documentation officielles provenant des 4 plate-formes tranquillement afin de comprendre commet doit on tester ces type d'auth quitte a créé un sous domaine particulier directement hebergé sur gittea
-->Sécuriser le serveur de dev

Emmanuel ROY authored on 02/12/2019 17:33:20
Showing 5 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,37 @@
1
+<?php
2
+
3
+
4
+namespace MVC\Classe;
5
+
6
+
7
+class Request
8
+{
9
+
10
+    public $method;
11
+    public $data;
12
+
13
+    public function __construct()
14
+    {
15
+        $this->method = $_SERVER['REQUEST_METHOD'];
16
+        $this->acceptResponse();
17
+    }
18
+
19
+    private function acceptResponse()
20
+    {
21
+        switch ($this->method) {
22
+            case 'GET':
23
+                break;
24
+            case 'POST':
25
+                break;
26
+            case 'PUT':
27
+                $this->data = json_decode(file_get_contents("php://input"), true);
28
+            case 'DELETE':
29
+                break;
30
+            default:
31
+                // Requête invalide
32
+                header("HTTP/1.0 405 Method Not Allowed");
33
+                break;
34
+        }
35
+    }
36
+
37
+}
0 38
\ No newline at end of file
1 39
new file mode 100644
... ...
@@ -0,0 +1,176 @@
1
+<?php
2
+
3
+
4
+namespace MVC\Classe;
5
+
6
+
7
+/**
8
+ * Class Response
9
+ *
10
+ * example use:
11
+ * $data = array('a','b','c');
12
+ *
13
+ * Three Way to send a request
14
+ *
15
+ * $request = new Response('http://myurl','mymethod');
16
+ * $request->addContent($data);
17
+ * $request->send();
18
+ *
19
+ * OR
20
+ *
21
+ * $request = new Response('http://myurl');
22
+ * (
23
+ * $request->createContext('mymethod')
24
+ * $request->addContent($data);
25
+ * $request->send();
26
+ * ) OR (
27
+ * $request->get($data);
28
+ * $request->post($data);
29
+ * $request->put($data);
30
+ * $request->delete($data);
31
+ *
32
+ *
33
+ * OR
34
+ *
35
+ * $request = new Response();
36
+ * $request->setUrl('http://myurl')->get($data)
37
+ * $request->setUrl('http://myurl')->post($data)
38
+ * $request->setUrl('http://myurl')->put($data)
39
+ * $request->setUrl('http://myurl')->delete($data)
40
+ *
41
+ * @package MVC\Classe
42
+ */
43
+class Response
44
+{
45
+    protected $url;
46
+    protected $options;
47
+
48
+    /**
49
+     * Response multi-constructor.
50
+     */
51
+    public function __construct()
52
+    {
53
+        $argumentFunction = func_get_args();
54
+        $nbParamsFunction = func_num_args();
55
+        if (method_exists($this, $function = '__construct' . $nbParamsFunction)) {
56
+            call_user_func_array(array($this, $function), $argumentFunction);
57
+        }
58
+    }
59
+
60
+    /**
61
+     * @return $this
62
+     */
63
+    public function __construct0()
64
+    {
65
+        return $this;
66
+    }
67
+
68
+    /**
69
+     * @param $url
70
+     * @return $this
71
+     */
72
+    public function __construct1($url)
73
+    {
74
+        $this->url = $url;
75
+        return $this;
76
+    }
77
+
78
+    /**
79
+     * Response constructor.
80
+     * @param $url URI
81
+     * @param $method POST,...
82
+     * @param $options
83
+     * @return $this
84
+     */
85
+    public function __construct2($url, $method)
86
+    {
87
+        $this->url = $url;
88
+
89
+        // utilisez 'http' même si vous envoyez la requête sur https:// ...
90
+        $this->options = array(
91
+            'http' => array(
92
+                'header' => "Content-type: application/x-www-form-urlencoded\r\n",
93
+                'method' => $method,
94
+            )
95
+        );
96
+        return $this;
97
+    }
98
+
99
+    public function setUrl($url)
100
+    {
101
+        $this->url = $url;
102
+        return $this;
103
+    }
104
+
105
+    public function setGetParamsUrl($url, $params = array())
106
+    {
107
+        $this->url = $url . (strpos($this->url, '?') ? '' : '?') . http_build_query($params);
108
+        return $this;
109
+    }
110
+
111
+    public function get($params = array())
112
+    {
113
+        return $this->replaceContext('GET')->addContent($params)->send();
114
+    }
115
+
116
+    public function send()
117
+    {
118
+
119
+        $context = stream_context_create($this->options);
120
+        $result = file_get_contents($this->url, false, $context);
121
+        if ($result === FALSE) {
122
+            /* Handle error */
123
+            return false;
124
+        } else {
125
+            return true;
126
+        }
127
+    }
128
+
129
+    /**
130
+     * @param $data Array
131
+     */
132
+    public function addContent($data)
133
+    {
134
+        //Exemple
135
+        //$this->data = array('name' => 'PEC', 'description' => 'Pencil 2H', 'price' => '2.25', 'category' => '9');
136
+        //'content' => http_build_query($data)
137
+        if (is_array($data)) {
138
+            $pContent = http_build_query($data);
139
+        }
140
+        $this->options['http']['content'] = $data;
141
+        return $this;
142
+    }
143
+
144
+    public function replaceContext($method)
145
+    {
146
+        return $this->createContext($method);
147
+    }
148
+
149
+    public function createContext($method)
150
+    {
151
+        // utilisez 'http' même si vous envoyez la requête sur https:// ...
152
+        $this->options = array(
153
+            'http' => array(
154
+                'header' => "Content-type: application/x-www-form-urlencoded\r\n",
155
+                'method' => $method,
156
+            )
157
+        );
158
+        return $this;
159
+    }
160
+
161
+    public function post($params = array())
162
+    {
163
+        return $this->replaceContext('POST')->addContent($params)->send();
164
+    }
165
+
166
+    public function put($params = array())
167
+    {
168
+        return $this->replaceContext('PUT')->addContent($params)->send();
169
+    }
170
+
171
+    public function delete($params = array())
172
+    {
173
+        return $this->replaceContext('DELETE')->addContent($params)->send();
174
+    }
175
+
176
+}
0 177
\ No newline at end of file
... ...
@@ -1,4 +1,4 @@
1
-
1
+<link rel="stylesheet" href="./../../dist/themes/default/style.min.css"/>
2 2
 <div id="wrapper">
3 3
     <div id="container">
4 4
         <div id="welcome">
... ...
@@ -5,22 +5,192 @@ namespace App\Controller;
5 5
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6 6
 use Symfony\Component\Routing\Annotation\Route;
7 7
 use Symfony\Component\HttpFoundation\Request;
8
+use Symfony\Component\HttpFoundation\Session\Session;
8 9
 
9 10
 class LuckyController extends AbstractController
10 11
 {
12
+
13
+    function recursiveObjectToJson($object)
14
+    {
15
+        $json = "";
16
+        var_dump($object);
17
+        echo "Iterating over: " . $object->count() . " values\n";
18
+        $iterator = $object->getIterator();
19
+        while ($iterator->valid()) {
20
+            print_r($iterator->key() . "=" . $iterator->current() . "\n");
21
+            $iterator->next();
22
+        }
23
+        print_r($iterator);
24
+        foreach ($iterator as $key => $value) {
25
+            print_r($key . $value);
26
+            if (is_object($value)) {
27
+                $json .= '{ "text" : "' . $key . ' - ' . get_class($value) . '", "children" : [';
28
+                $json .= $this->recursiveObjectToJson($value);
29
+                $json .= ']}';
30
+            } elseif (is_array($value)) {
31
+                $json .= '{ "text" : "' . $key . '", "children" : [';
32
+                $json .= $this->recursiveObjectToJson($value);
33
+                $json .= ']}';
34
+            } else {
35
+                $json .= '{ "text" : "' . $value . '" }';
36
+            }
37
+        }
38
+        return $json;
39
+    }
40
+
41
+    private function ObjectToJson($object)
42
+    {
43
+        $json = '{
44
+            "core" : {
45
+                "data" : [';
46
+        $json .= $this->recursiveObjectToJson($object);
47
+        $json .= ']}}';
48
+
49
+        return $json;
50
+    }
51
+
52
+    /*it comes from https://www.php.net/manual/en/function.var-dump.php */
53
+    public function dump_debug($input, $collapse = false)
54
+    {
55
+        $recursive = function ($data, $level = 0) use (&$recursive, $collapse) {
56
+            global $argv;
57
+
58
+            $isTerminal = isset($argv);
59
+
60
+            if (!$isTerminal && $level == 0 && !defined("DUMP_DEBUG_SCRIPT")) {
61
+                define("DUMP_DEBUG_SCRIPT", true);
62
+
63
+                echo '<script language="Javascript">function toggleDisplay(id) {';
64
+                echo 'var state = document.getElementById("container"+id).style.display;';
65
+                echo 'document.getElementById("container"+id).style.display = state == "inline" ? "none" : "inline";';
66
+                echo 'document.getElementById("plus"+id).style.display = state == "inline" ? "inline" : "none";';
67
+                echo '}</script>' . "\n";
68
+            }
69
+
70
+            $type = !is_string($data) && is_callable($data) ? "Callable" : ucfirst(gettype($data));
71
+            $type_data = null;
72
+            $type_color = null;
73
+            $type_length = null;
74
+
75
+            switch ($type) {
76
+                case "String":
77
+                    $type_color = "green";
78
+                    $type_length = strlen($data);
79
+                    $type_data = "\"" . htmlentities($data) . "\"";
80
+                    break;
81
+
82
+                case "Double":
83
+                case "Float":
84
+                    $type = "Float";
85
+                    $type_color = "#0099c5";
86
+                    $type_length = strlen($data);
87
+                    $type_data = htmlentities($data);
88
+                    break;
89
+
90
+                case "Integer":
91
+                    $type_color = "red";
92
+                    $type_length = strlen($data);
93
+                    $type_data = htmlentities($data);
94
+                    break;
95
+
96
+                case "Boolean":
97
+                    $type_color = "#92008d";
98
+                    $type_length = strlen($data);
99
+                    $type_data = $data ? "TRUE" : "FALSE";
100
+                    break;
101
+
102
+                case "NULL":
103
+                    $type_length = 0;
104
+                    break;
105
+
106
+                case "Array":
107
+                    $type_length = count($data);
108
+            }
109
+
110
+            if (in_array($type, array("Object", "Array"))) {
111
+                $notEmpty = false;
112
+
113
+                foreach ($data as $key => $value) {
114
+                    if (!$notEmpty) {
115
+                        $notEmpty = true;
116
+
117
+                        if ($isTerminal) {
118
+                            echo $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "\n";
119
+
120
+                        } else {
121
+                            $id = substr(md5(rand() . ":" . $key . ":" . $level), 0, 8);
122
+
123
+                            echo "<a href=\"javascript:toggleDisplay('" . $id . "');\" style=\"text-decoration:none\">";
124
+                            echo "<span style='color:#666666'>" . $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "</span>";
125
+                            echo "</a>";
126
+                            echo "<span id=\"plus" . $id . "\" style=\"display: " . ($collapse ? "inline" : "none") . ";\">&nbsp;&#10549;</span>";
127
+                            echo "<div id=\"container" . $id . "\" style=\"display: " . ($collapse ? "" : "inline") . ";\">";
128
+                            echo "<br />";
129
+                        }
130
+
131
+                        for ($i = 0; $i <= $level; $i++) {
132
+                            echo $isTerminal ? "|    " : "<span style='color:black'>|</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
133
+                        }
134
+
135
+                        echo $isTerminal ? "\n" : "<br />";
136
+                    }
137
+
138
+                    for ($i = 0; $i <= $level; $i++) {
139
+                        echo $isTerminal ? "|    " : "<span style='color:black'>|</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
140
+                    }
141
+
142
+                    echo $isTerminal ? "[" . $key . "] => " : "<span style='color:black'>[" . $key . "]&nbsp;=>&nbsp;</span>";
143
+
144
+                    call_user_func($recursive, $value, $level + 1);
145
+                }
146
+
147
+                if ($notEmpty) {
148
+                    for ($i = 0; $i <= $level; $i++) {
149
+                        echo $isTerminal ? "|    " : "<span style='color:black'>|</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
150
+                    }
151
+
152
+                    if (!$isTerminal) {
153
+                        echo "</div>";
154
+                    }
155
+
156
+                } else {
157
+                    echo $isTerminal ?
158
+                        $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "  " :
159
+                        "<span style='color:#666666'>" . $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "</span>&nbsp;&nbsp;";
160
+                }
161
+
162
+            } else {
163
+                echo $isTerminal ?
164
+                    $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "  " :
165
+                    "<span style='color:#666666'>" . $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "</span>&nbsp;&nbsp;";
166
+
167
+                if ($type_data != null) {
168
+                    echo $isTerminal ? $type_data : "<span style='color:" . $type_color . "'>" . $type_data . "</span>";
169
+                }
170
+            }
171
+
172
+            echo $isTerminal ? "\n" : "<br />";
173
+        };
174
+
175
+        call_user_func($recursive, $input);
176
+    }
177
+
11 178
     /**
12 179
      * @Route("/syf51", name="homepage")
13 180
      */
14 181
     public function indexAction(Request $request)
15 182
     {
16 183
         print_r("<pre>");
17
-        print_r($this->get('session'));
184
+        $session = $this->var_log($this->get('session'));
185
+        //$session = json_encode($this->get('session'));
186
+        print_r($session);
18 187
         print_r($_COOKIE);
19 188
         print_r($_SESSION);
189
+        print_r("</pre>");
20 190
         $_SESSION['test-user51'] = "user51";
21 191
         // replace this example code with whatever you need
22 192
         return $this->render('default/page.html.twig', [
23
-            'text' => 'homepage',
193
+            'text' => 'homepage', 'json' => $session
24 194
         ]);
25 195
     }
26 196
 
... ...
@@ -16,6 +16,8 @@
16 16
             </p>
17 17
         </div>
18 18
 
19
+        <div id="data" class="session"></div>
20
+
19 21
         <div id="next">
20 22
             <h2>What's next?</h2>
21 23
             <p>{{ text }}</p>
... ...
@@ -111,4 +113,10 @@
111 113
             animation: fade-in 1s .2s forwards;
112 114
         }
113 115
     }
114
-</style>
115 116
\ No newline at end of file
117
+</style>
118
+<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
119
+<script src="./../../dist/jstree.min.js"></script>
120
+<script>
121
+    // inline data demo
122
+    $('#session').jstree({{ json }});
123
+</script>
116 124
\ No newline at end of file