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
This commit is contained in:
Emmanuel ROY 2019-12-02 17:33:20 +01:00
parent 66566367ef
commit e8953b5aae
5 changed files with 395 additions and 4 deletions

View file

@ -0,0 +1,37 @@
<?php
namespace MVC\Classe;
class Request
{
public $method;
public $data;
public function __construct()
{
$this->method = $_SERVER['REQUEST_METHOD'];
$this->acceptResponse();
}
private function acceptResponse()
{
switch ($this->method) {
case 'GET':
break;
case 'POST':
break;
case 'PUT':
$this->data = json_decode(file_get_contents("php://input"), true);
case 'DELETE':
break;
default:
// Requête invalide
header("HTTP/1.0 405 Method Not Allowed");
break;
}
}
}

View file

@ -0,0 +1,176 @@
<?php
namespace MVC\Classe;
/**
* Class Response
*
* example use:
* $data = array('a','b','c');
*
* Three Way to send a request
*
* $request = new Response('http://myurl','mymethod');
* $request->addContent($data);
* $request->send();
*
* OR
*
* $request = new Response('http://myurl');
* (
* $request->createContext('mymethod')
* $request->addContent($data);
* $request->send();
* ) OR (
* $request->get($data);
* $request->post($data);
* $request->put($data);
* $request->delete($data);
*
*
* OR
*
* $request = new Response();
* $request->setUrl('http://myurl')->get($data)
* $request->setUrl('http://myurl')->post($data)
* $request->setUrl('http://myurl')->put($data)
* $request->setUrl('http://myurl')->delete($data)
*
* @package MVC\Classe
*/
class Response
{
protected $url;
protected $options;
/**
* Response multi-constructor.
*/
public function __construct()
{
$argumentFunction = func_get_args();
$nbParamsFunction = func_num_args();
if (method_exists($this, $function = '__construct' . $nbParamsFunction)) {
call_user_func_array(array($this, $function), $argumentFunction);
}
}
/**
* @return $this
*/
public function __construct0()
{
return $this;
}
/**
* @param $url
* @return $this
*/
public function __construct1($url)
{
$this->url = $url;
return $this;
}
/**
* Response constructor.
* @param $url URI
* @param $method POST,...
* @param $options
* @return $this
*/
public function __construct2($url, $method)
{
$this->url = $url;
// utilisez 'http' même si vous envoyez la requête sur https:// ...
$this->options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => $method,
)
);
return $this;
}
public function setUrl($url)
{
$this->url = $url;
return $this;
}
public function setGetParamsUrl($url, $params = array())
{
$this->url = $url . (strpos($this->url, '?') ? '' : '?') . http_build_query($params);
return $this;
}
public function get($params = array())
{
return $this->replaceContext('GET')->addContent($params)->send();
}
public function send()
{
$context = stream_context_create($this->options);
$result = file_get_contents($this->url, false, $context);
if ($result === FALSE) {
/* Handle error */
return false;
} else {
return true;
}
}
/**
* @param $data Array
*/
public function addContent($data)
{
//Exemple
//$this->data = array('name' => 'PEC', 'description' => 'Pencil 2H', 'price' => '2.25', 'category' => '9');
//'content' => http_build_query($data)
if (is_array($data)) {
$pContent = http_build_query($data);
}
$this->options['http']['content'] = $data;
return $this;
}
public function replaceContext($method)
{
return $this->createContext($method);
}
public function createContext($method)
{
// utilisez 'http' même si vous envoyez la requête sur https:// ...
$this->options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => $method,
)
);
return $this;
}
public function post($params = array())
{
return $this->replaceContext('POST')->addContent($params)->send();
}
public function put($params = array())
{
return $this->replaceContext('PUT')->addContent($params)->send();
}
public function delete($params = array())
{
return $this->replaceContext('DELETE')->addContent($params)->send();
}
}

View file

@ -1,4 +1,4 @@
<link rel="stylesheet" href="./../../dist/themes/default/style.min.css"/>
<div id="wrapper">
<div id="container">
<div id="welcome">

View file

@ -5,22 +5,192 @@ namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
class LuckyController extends AbstractController
{
function recursiveObjectToJson($object)
{
$json = "";
var_dump($object);
echo "Iterating over: " . $object->count() . " values\n";
$iterator = $object->getIterator();
while ($iterator->valid()) {
print_r($iterator->key() . "=" . $iterator->current() . "\n");
$iterator->next();
}
print_r($iterator);
foreach ($iterator as $key => $value) {
print_r($key . $value);
if (is_object($value)) {
$json .= '{ "text" : "' . $key . ' - ' . get_class($value) . '", "children" : [';
$json .= $this->recursiveObjectToJson($value);
$json .= ']}';
} elseif (is_array($value)) {
$json .= '{ "text" : "' . $key . '", "children" : [';
$json .= $this->recursiveObjectToJson($value);
$json .= ']}';
} else {
$json .= '{ "text" : "' . $value . '" }';
}
}
return $json;
}
private function ObjectToJson($object)
{
$json = '{
"core" : {
"data" : [';
$json .= $this->recursiveObjectToJson($object);
$json .= ']}}';
return $json;
}
/*it comes from https://www.php.net/manual/en/function.var-dump.php */
public function dump_debug($input, $collapse = false)
{
$recursive = function ($data, $level = 0) use (&$recursive, $collapse) {
global $argv;
$isTerminal = isset($argv);
if (!$isTerminal && $level == 0 && !defined("DUMP_DEBUG_SCRIPT")) {
define("DUMP_DEBUG_SCRIPT", true);
echo '<script language="Javascript">function toggleDisplay(id) {';
echo 'var state = document.getElementById("container"+id).style.display;';
echo 'document.getElementById("container"+id).style.display = state == "inline" ? "none" : "inline";';
echo 'document.getElementById("plus"+id).style.display = state == "inline" ? "inline" : "none";';
echo '}</script>' . "\n";
}
$type = !is_string($data) && is_callable($data) ? "Callable" : ucfirst(gettype($data));
$type_data = null;
$type_color = null;
$type_length = null;
switch ($type) {
case "String":
$type_color = "green";
$type_length = strlen($data);
$type_data = "\"" . htmlentities($data) . "\"";
break;
case "Double":
case "Float":
$type = "Float";
$type_color = "#0099c5";
$type_length = strlen($data);
$type_data = htmlentities($data);
break;
case "Integer":
$type_color = "red";
$type_length = strlen($data);
$type_data = htmlentities($data);
break;
case "Boolean":
$type_color = "#92008d";
$type_length = strlen($data);
$type_data = $data ? "TRUE" : "FALSE";
break;
case "NULL":
$type_length = 0;
break;
case "Array":
$type_length = count($data);
}
if (in_array($type, array("Object", "Array"))) {
$notEmpty = false;
foreach ($data as $key => $value) {
if (!$notEmpty) {
$notEmpty = true;
if ($isTerminal) {
echo $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "\n";
} else {
$id = substr(md5(rand() . ":" . $key . ":" . $level), 0, 8);
echo "<a href=\"javascript:toggleDisplay('" . $id . "');\" style=\"text-decoration:none\">";
echo "<span style='color:#666666'>" . $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "</span>";
echo "</a>";
echo "<span id=\"plus" . $id . "\" style=\"display: " . ($collapse ? "inline" : "none") . ";\">&nbsp;&#10549;</span>";
echo "<div id=\"container" . $id . "\" style=\"display: " . ($collapse ? "" : "inline") . ";\">";
echo "<br />";
}
for ($i = 0; $i <= $level; $i++) {
echo $isTerminal ? "| " : "<span style='color:black'>|</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
}
echo $isTerminal ? "\n" : "<br />";
}
for ($i = 0; $i <= $level; $i++) {
echo $isTerminal ? "| " : "<span style='color:black'>|</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
}
echo $isTerminal ? "[" . $key . "] => " : "<span style='color:black'>[" . $key . "]&nbsp;=>&nbsp;</span>";
call_user_func($recursive, $value, $level + 1);
}
if ($notEmpty) {
for ($i = 0; $i <= $level; $i++) {
echo $isTerminal ? "| " : "<span style='color:black'>|</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
}
if (!$isTerminal) {
echo "</div>";
}
} else {
echo $isTerminal ?
$type . ($type_length !== null ? "(" . $type_length . ")" : "") . " " :
"<span style='color:#666666'>" . $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "</span>&nbsp;&nbsp;";
}
} else {
echo $isTerminal ?
$type . ($type_length !== null ? "(" . $type_length . ")" : "") . " " :
"<span style='color:#666666'>" . $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "</span>&nbsp;&nbsp;";
if ($type_data != null) {
echo $isTerminal ? $type_data : "<span style='color:" . $type_color . "'>" . $type_data . "</span>";
}
}
echo $isTerminal ? "\n" : "<br />";
};
call_user_func($recursive, $input);
}
/**
* @Route("/syf51", name="homepage")
*/
public function indexAction(Request $request)
{
print_r("<pre>");
print_r($this->get('session'));
$session = $this->var_log($this->get('session'));
//$session = json_encode($this->get('session'));
print_r($session);
print_r($_COOKIE);
print_r($_SESSION);
print_r("</pre>");
$_SESSION['test-user51'] = "user51";
// replace this example code with whatever you need
return $this->render('default/page.html.twig', [
'text' => 'homepage',
'text' => 'homepage', 'json' => $session
]);
}

View file

@ -16,6 +16,8 @@
</p>
</div>
<div id="data" class="session"></div>
<div id="next">
<h2>What's next?</h2>
<p>{{ text }}</p>
@ -111,4 +113,10 @@
animation: fade-in 1s .2s forwards;
}
}
</style>
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="./../../dist/jstree.min.js"></script>
<script>
// inline data demo
$('#session').jstree({{ json }});
</script>