Despite the keywords PHP // Javascript // Load // URL showing lots of results on the web, I am not able to make things clear in my head. I am trying to learn with real cases studies, but I'm a bit lost right now.
I built a web interface for some command line scripts.
1 - The user enters a word in an HTML form,
2 - The form is submited to a PHP script,
3 - The script executes the command on the server and returns the result in the HTML page.
So far so good.
But the scripts may take a long time to process, so I would like to change the way the result is displayed.
I would like to use [ttyd][1], a terminal sharing utility that would allow me to get a beautiful result in realtime:
1 - The user enters a word in an HTML form,
2 - The form is submitted to a PHP script,
3 - The script executes the command on the server, opens a port, and wait for an HTTP connexion,
4 - The HTTP connexion is made and the result appears in an iframe.
and returns the result on the HTML page.
Here is my PHP sample:
<?php
require __DIR__ . "/vendor/autoload.php";
ini_set("display_errors", "1");
ini_set("display_startup_errors", "1");
error_reporting(E_ALL);
//Securize user inputs
function securize($p)
{
$v = htmlspecialchars($p); //Convertir les caractères spéciaux
$v = trim($v); //Supprimer les espaces dans la requête
$v = rtrim($v); //Supprimer les espaces à la fin de la requête
$v = strtolower($v); //Tout mettre en minuscule
$v = strip_tags($v); //Supprimer les balises html dans la requête
$v = stripslashes($v); //Supprimer les slash dans la requête
$v = stripcslashes($v); //Supprimer les backslash dans la requête
$v = escapeshellcmd($v); //Protège les caractères spéciaux du Shell
return $v;
}
//Tool selector
if (isset($_POST["search"])) {
$term = securize($_POST["search"]);
if (!empty($term)) {
$port = rand(5000, 6000); //Get a random port for ttyd
if ($_POST["tool"] == "ghunt") {
if (mailValidate($term)) {
$tool = "python3 ghunt.py email";
$cli = (craft($tool, $term, $port));
passthru($cli);
die();
} else {
echo "<script type='text/javascript'>alert('$term n\'est pas une adresse mail valide');</script>";
die();
}
}
}
// Mail validation
// Takes user input
function mailValidate($p)
{
if (filter_var($p, FILTER_VALIDATE_EMAIL)) {
$v = true;
} else {
$v = false;
}
return $v;
}
// Command prep
// Takes command line + User input + port
function craft($tool, $term, $port)
{
$v = "ttyd -o -R -p $port $tool $term";
return $v;
}
?>
So far, all my attempts failed.
I tried to set a PHP redirection header("Location: http://ipadress:$port/")
But the PHP script seems to stop right after executing the command.
So I also tried to use Javascript and Jquery with the load() function, but do not know where to put things.
Isn't PHP able to handle multiple "actions" like executing and redirect?
Here is a sample of my HTML form:
<body>
<form method="POST" action="search.php" target="iframe">
<fieldset>
<legend>Recherchez un mail, un téléphone ou un pseudo...</legend>
<input type="search" placeholder="mail, téléphone ou pseudo" name="search">
<br>
<div class="fields">
<fieldset>
<legend>Email</legend>
<input type="radio" name="tool" value="ghunt">
<label for="ghunt"><b><div class="tooltip">Ghunt<span class="tooltiptext">Informations d'un compte google</span></div></b></label>
</fieldset>
</div>
<input class="button" type="submit" name="submit" value="Seek">
</fieldset>
</form>
<iframe id="ttyd" name="iframe"> </iframe>
</body>
Many thanks for your lights [1]: https://github.com/tsl0922/ttyd