wwwgmo/src/managetable.php
2025-02-07 20:52:27 +01:00

312 lines
11 KiB
PHP

<?php
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
require_once 'config.php';
// conection db
$conn=require 'connect.php';
$defaultAction='add';
$defaultModCre=false; // display or not created/modified date
$defaultBtnMoreLess='More';
$firstName='';
$lastName='';
$email='';
$flagAction=$defaultAction;
$id='';
$modcre=$defaultModCre;
$btnMoreLess=$defaultBtnMoreLess;
$validEntry=true;
$beforeModifiedEmail='';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo $db_site_title." v".$site_version ?></title>
<!-- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/gmo.css">
</head>
<body>
<h2><?php echo $db_site_title ?></h2>
<?php echo $site_copyright; echo "<hr>"; ?>
<script>
function validate(msg)
{
conf = confirm("Are you sure you want to delete " + msg + " ?");
if (conf)
return true;
else
return false;
}
</script>
<br>
<h5>Add/Modify </h5>
<?php
function emailExistInDB($db, $table, $emailToVerif, $emailBeforeMod)
{
if ($emailToVerif != $emailBeforeMod) {
//echo "I connect to DB to verify<br>";
try{
/* Create a prepared statement */
$stmt = $db -> prepare("SELECT email FROM ". $table .";");
/* execute the query */
$stmt -> execute();
/* fetch all results */
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($res as $row){
extract($row);
//echo $email."<br>";
if ($emailToVerif == $email) {
echo $email." found in DB<br>";
$returnCode=true;
break;
}
else {
//echo $email." pas trouvé<br>";
$returnCode=false;
}
}
}
catch (PDOExecption $e){
echo $e->getMessage();
}
}
else {
$returnCode=false;
}
/*echo "Exit from DnsNameExistInDB function<br>";*/
return $returnCode; /* false $emailToVerif doesn't exist - true $emailToVerif exist */
}
// ADD/MODIFY RECORD
if (isset($_POST['send'])) {
/* set var from form */
$firstName = isset($_POST['firstName']) ? trim($_POST['firstName']) : "";
//$beforeModifieFirst = isset($_POST['firstNameBeforeModified']) ? trim($_POST['firstNameBeforeModified']) : "";
$lastName = isset($_POST['lastName']) ? trim($_POST['lastName']) : "";
//$beforeModifiedLastName = isset($_POST['lastNameBeforeModified']) ? trim($_POST['lastNameBeforeModified']) : "";
$email = isset($_POST['email']) ? trim($_POST['email']) : "";
$beforeModifiedEmail = isset($_POST['emailBeforeModified']) ? trim($_POST['emailBeforeModified']) : "";
$flagAction = isset($_POST['flagAction']) ? $_POST['flagAction'] : "";
$id = isset($_POST['id']) ? $_POST['id'] : "";
/* define the format of the DNS name and ip address */
$validEmail="/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/";
/* verify correct dnsname and ip address */
//echo "Verify email<br>";
if ($firstName == '' || $lastName == '' || !(preg_match($validEmail, $email)) || emailExistInDB($conn, $table, $email, $beforeModifiedEmail)) {
print"<div class='error'>One (or more) mandatory field(s) has (have) not been correctly filled in.<br>";
$validEntry=false;
if ($firstName == ''){
print "Mandatory 'Firstname' field.";
$validEntry=false;
}
elseif ($lastName == ''){
print "Mandatory 'Lastname' field.";
$validEntry=false;
}
elseif ($email == ''){
print "Mandatory 'EMail' field.";
$validEntry=false;
}
elseif (! preg_match($validEmail, $email)){
print "'".$email."' format invalid. ";
$validEntry=false;
}
if (emailExistInDB($conn, $table, $email, $beforeModifiedEmail)){
print "'".$email."' already exist. ";
$validEntry=false;
}
print"</div>";
}
/* All is ok we try to add record into DB */
if ($validEntry){
/*echo "Add/Update record into DB<br>"; */
if ($flagAction=="add"){
$stmt = $conn -> prepare("INSERT INTO $table (firstname, lastname, email) VALUES (:firstName, :lastName, :email)");
$stmt -> bindParam(':firstName', $firstName, PDO::PARAM_STR);
$stmt -> bindParam(':lastName', $lastName, PDO::PARAM_STR);
$stmt -> bindParam(':email', $email, PDO::PARAM_STR);
}
if ($flagAction=="modify"){
//$timestamp = date("Y-m-d h:i:s");
$stmt = $conn->prepare("UPDATE $table SET firstname = :firstName, lastname = :lastName, email = :email WHERE id =:id");
$stmt -> bindParam(':id', $id, PDO::PARAM_INT);
$stmt -> bindParam(':lastName', $lastName, PDO::PARAM_STR);
$stmt -> bindParam(':firstName', $firstName, PDO::PARAM_STR);
$stmt -> bindParam(':email', $email, PDO::PARAM_STR);
}
try{
/* execute the query */
$stmt -> execute();
}
catch (PDOExecption $e){
//echo $e->getMessage();
//print "<div class='error'>". $stmt -> errorInfo()[2]. " - ".$email."</div>";
print "<div class='error'> - ".$email."</div>";
}
}
$firstName='';
$lastName='';
$email='';
$flagAction=$defaultAction;
$id='';
}
// DELETE IP
if (isset($_POST['delete'])) {
$id = isset($_POST['id']) ? $_POST['id'] : "";
try {
$sql = "DELETE FROM $table WHERE id = :id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
$stmt->execute();
/* close connection */
$db = null;
}
catch (PDOExecption $e){
echo $e->getMessage();
}
}
// MODIFIER
if (isset($_POST['modify'])) {
$firstName = isset($_POST['firstName']) ? $_POST['firstName'] : "";
//$beforeModifyfirstName = isset($_POST['firstName']) ? $_POST['firstName'] : "";
$lastName = isset($_POST['lastName']) ? $_POST['lastName'] : "";
//$beforeModifylastName = isset($_POST['lastName']) ? $_POST['lastName'] : "";
$email = isset($_POST['email']) ? $_POST['email'] : "";
$beforeModifiedEmail = isset($_POST['email']) ? $_POST['email'] : "";
// $description = isset($_POST['description']) ? $_POST['description'] : "";
$flagAction = isset($_POST['flagAction']) ? $_POST['flagAction'] : "";
$id = isset($_POST['id']) ? $_POST['id'] : "";
}
//DISPLAY MORE
if (isset($_POST['displaymore'])) {
$modcre = isset($_POST['modcre']) ? $_POST['modcre'] : "";
if ($modcre){
$modcre=false;
$btnMoreLess='More';
}
elseif (! $modcre){
$modcre=true;
$btnMoreLess='Less';
}
}
?>
<!-- Formulaire pour la saisie et la modifcation -->
<form method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>">
<table border=0>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>EMail</th>
</tr>
<tr>
<?php
print '<td><input type="text" name="firstName" maxlength="30" value="'.$firstName.'" /></td>'.
'<td><input type="text" name="lastName" maxlength="30" value="'.$lastName.'" /></td>'.
'<td><input type="text" name="email" maxlength="100" value="'.$email.'" /></td>'.
'<td><input type="hidden" name="firstNameBeforeModified" maxlength="15" value="'.$firstName.'" /></td>'.
'<td><input type="hidden" name="lastNameBeforeModified" maxlength="100" value="'.$lastName.'" /></td>'.
'<td><input type="hidden" name="emailBeforeModified" maxlength="100" value="'.$email.'" /></td>'.
'<td><input type="hidden" name="flagAction" value="'.$flagAction.'" /></td>'.
'<td><input type="hidden" name="id" value="'.$id.'" /></td>'.
'<td><input type="hidden" name="modcre" value="'.$modcre.'"></td></tr><tr>';
//'<td><input type="submit" name="send" value="Ok" />&nbsp;<input type="submit" name="nextip" value="Next free IP">&nbsp;'.
//'<input type="reset" value="Cancel" />&nbsp;<input type="submit" value="Update DNS"</td>';
?>
</tr>
</table>
<table>
<tr>
<td><input type="submit" name="send" value="Ok" />
<input type="reset" value="Cancel" />&nbsp;<input type="submit" name="displaymore" value="<?php echo $btnMoreLess ?>" >
</td>
</tr>
</table>
</form>
<p></p>
<!-- Affiche la liste des adresses -->
<h5>EMails list</h5>
<?php
try{
//include('connexion.php');
//$db = new PDO('sqlite:'.$dbName);
/* Create a prepared statement - sort by ip address - whoau find on https://stackoverflow.com/questions/23092783/best-way-to-sort-by-ip-addresses-in-sql */
$stmt = $conn -> prepare("SELECT * FROM $table ORDER BY firstname");
//$stmt = $conn -> prepare("SELECT * FROM $table where firstname like 'T%'");
/* execute the query */
$stmt -> execute();
/* fetch all results */
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "<table border=1>".
"<tr>".
"<th>ID</th>".
"<th>Firstname</th>".
"<th>Lastname</th>".
"<th>EMail</th>";
if ($modcre){
echo "<th>Created</th>".
"<th>Modified</th>";
}
echo "<th>Actions</th>".
"</tr>";
foreach($res as $row){
extract($row);
echo "<tr>".
"<td>".$id."</td>".
"<td>".$firstname."</td>".
"<td>".$lastname."</td>".
"<td>".$email."</td>";
if ($modcre){
echo "<td>".$reg_date."</td>".
"<td>".$mod_date."</td>";
}
echo "<td>";
?>
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">
<?php
echo "<input type='hidden' name='firstName' value='".$firstname."'>".
"<input type='hidden' name='lastName' value='".$lastname."'>".
"<input type='hidden' name='email' value='".$email."'>".
"<input type='hidden' name='flagAction' value='modify'>".
"<input type='hidden' name='id' value='".$id."'>".
"<button type='submit' name='modify' title='Modify'><img src='images/pen.svg' alt='pen' /></button>&nbsp;".
"<button onclick='return validate(\"".$firstname." ".$lastname." (".$email.")\");' type='submit' name='delete' title='Delete'><img src='images/trash.svg' alt='trash' /></button>".
"</td></form></tr>";
}
echo "</table>";
/* close connection */
//$db = null;
}
catch (PDOExecption $e){
echo $e->getMessage();
}
?>
</body>
</html>