l'upload fonctionne ( pas la partie bdd, juste l'upload :( )
This commit is contained in:
parent
9e8b5ec335
commit
02b2a1946e
5 changed files with 162 additions and 23 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
||||||
test_creds.php
|
test_creds.php
|
||||||
|
archives
|
21
api.php
21
api.php
|
@ -56,6 +56,25 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (isset($_GET["chercher"])) {
|
||||||
|
// Example URL: /api/chercher?rech=math&duree=30&tags=algebre,geometrie
|
||||||
|
|
||||||
|
$query = isset($_GET["req"]) ? $_GET["req"] : "";
|
||||||
|
$length = isset($_GET["duree"]) ? $_GET["duree"] : "";
|
||||||
|
$tags = isset($_GET["duree"]) ? explode(",", $_GET["tags"]) : [];
|
||||||
|
|
||||||
|
try {
|
||||||
|
$results = searchExercises($query, $length, $tags);
|
||||||
|
echo json_encode(["status" => "1", "results" => $results]);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
echo json_encode(["status" => "0", "msg" => $e->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exit;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -75,5 +94,7 @@
|
||||||
echo(json_encode(["status"=> "2","msg"=> "Opération inconnue."]));
|
echo(json_encode(["status"=> "2","msg"=> "Opération inconnue."]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exit;
|
||||||
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
127
bdd.php
127
bdd.php
|
@ -6,6 +6,37 @@ $servername = "127.0.0.1";
|
||||||
$username = "root";
|
$username = "root";
|
||||||
$password = "";
|
$password = "";
|
||||||
$dbname = "archivinsa";
|
$dbname = "archivinsa";
|
||||||
|
|
||||||
|
|
||||||
|
// Liste des extensions autorisées pour les images
|
||||||
|
$image_extensions = [
|
||||||
|
'jpg',
|
||||||
|
'jpeg',
|
||||||
|
'png',
|
||||||
|
'gif',
|
||||||
|
'bmp',
|
||||||
|
'tiff',
|
||||||
|
'tif',
|
||||||
|
'webp',
|
||||||
|
'svg',
|
||||||
|
'ico',
|
||||||
|
'raw'];
|
||||||
|
|
||||||
|
// Liste des extensions autorisées pour les fichiers PDF
|
||||||
|
$pdf_extensions = ['pdf'];
|
||||||
|
|
||||||
|
// Liste des extensions autorisées pour les fichiers de présentation (par exemple, PowerPoint)
|
||||||
|
$presentation_extensions = ['ppt', 'pptx','odp','pptm','ppsx'];
|
||||||
|
|
||||||
|
// Fusionner les listes en une seule liste
|
||||||
|
$ext_autorisees = array_merge($imageExtensions, $pdfExtensions, $presentationExtensions);
|
||||||
|
|
||||||
|
function check_ext($filename) {
|
||||||
|
$extension = pathinfo($filename, PATHINFO_EXTENSION);
|
||||||
|
return in_array(strtolower($extension), $GLOBALS["ext_autorisees"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function ajouter_doc($request){
|
function ajouter_doc($request){
|
||||||
|
|
||||||
$conn = new mysqli($GLOBALS["servername"], $GLOBALS["username"], $GLOBALS["password"], $GLOBALS["dbname"]);
|
$conn = new mysqli($GLOBALS["servername"], $GLOBALS["username"], $GLOBALS["password"], $GLOBALS["dbname"]);
|
||||||
|
@ -30,32 +61,49 @@ function ajouter_doc($request){
|
||||||
|
|
||||||
function saveFilesFromPost($postData,$id_ensemble,$conn) {
|
function saveFilesFromPost($postData,$id_ensemble,$conn) {
|
||||||
// Check if the $_POST variable is set and contains files
|
// Check if the $_POST variable is set and contains files
|
||||||
if (isset($postData['files']) && is_array($postData['files'])) {
|
echo(print_r($_FILES,true));
|
||||||
|
if (isset($_FILES['fichiers']) && is_array($_FILES['fichiers'])) {
|
||||||
// Directory to save the files
|
// Directory to save the files
|
||||||
$uploadDir = 'archives/';
|
// /!\ A CHANGER EN PROD /!\
|
||||||
|
$uploadDir = '/opt/lampp/htdocs/annales/archives/';
|
||||||
|
|
||||||
// Iterate through each file in the $_POST['files'] array
|
|
||||||
foreach ($postData['files'] as $file) {
|
// Iterate through each file in the $_FILES array
|
||||||
|
foreach ($_FILES as $file) {
|
||||||
// Extract file information
|
// Extract file information
|
||||||
|
if (isset($file['name'])){
|
||||||
$fileName = $file['name'];
|
$fileName = $file['name'];
|
||||||
$fileData = $file['data'];
|
if(!check_ext($fileName)){
|
||||||
|
echo(json_encode(["status"=>"0","msg"=>"Error saving file '$uniqueFileName'"]));
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
// Decode base64 encoded file data
|
}else{
|
||||||
$fileData = base64_decode($fileData);
|
echo("WTFFF");
|
||||||
|
print_r($file);
|
||||||
|
}
|
||||||
|
|
||||||
// Create a unique filename to avoid overwriting existing files
|
// Create a unique filename to avoid overwriting existing files
|
||||||
$uniqueFileName = uniqid() . '_' . $fileName;
|
$uniqueFileName = uniqid() . '_' . htmlspecialchars($fileName);
|
||||||
|
|
||||||
// Define the path to save the file
|
// Define the path to save the file
|
||||||
$filePath = $uploadDir . $uniqueFileName;
|
$filePath = $uploadDir . $uniqueFileName;
|
||||||
|
|
||||||
|
//echo($filePath."\n");
|
||||||
|
|
||||||
|
|
||||||
// Save the file
|
// Save the file
|
||||||
if (file_put_contents($filePath, $fileData) !== false) {
|
if (move_uploaded_file($file['tmp_name'], $filePath)) {
|
||||||
echo(json_encode(["status"=>"1","msg" =>"File '$uniqueFileName' has been saved successfully."]));
|
echo(json_encode(["status"=>"1","msg" =>"File '$uniqueFileName' has been saved successfully."]));
|
||||||
} else {
|
} else {
|
||||||
echo(json_encode(["status"=>"0","msg"=>"Error saving file '$uniqueFileName'"]));
|
echo(json_encode(["status"=>"0","msg"=>"Error saving file '$uniqueFileName'"]));
|
||||||
|
exit;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
try{
|
||||||
//update the database
|
//update the database
|
||||||
$safe_titre = htmlspecialchars($postData['titre']);
|
$safe_titre = htmlspecialchars($postData['titre']);
|
||||||
$safe_type = htmlspecialchars($postData['type']);
|
$safe_type = htmlspecialchars($postData['type']);
|
||||||
|
@ -64,10 +112,71 @@ function saveFilesFromPost($postData,$id_ensemble,$conn) {
|
||||||
$sql="INSERT INTO documents (titre,type,upload_path,commentaire_auteur,ensemble_id) VALUES(?,?,?,?,?)";
|
$sql="INSERT INTO documents (titre,type,upload_path,commentaire_auteur,ensemble_id) VALUES(?,?,?,?,?)";
|
||||||
$conn->execute_query($sql, array("titre"=> $safe_titre,"type"=>$safe_type,"upload_path"=> $uploadDir,"commentaire_auteur"=>"","ensemble_id"=>$id_ensemble));
|
$conn->execute_query($sql, array("titre"=> $safe_titre,"type"=>$safe_type,"upload_path"=> $uploadDir,"commentaire_auteur"=>"","ensemble_id"=>$id_ensemble));
|
||||||
|
|
||||||
|
}catch(Exception $e){
|
||||||
|
echo(json_encode(['status'=> '0','msg'=>$e]));
|
||||||
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
echo(json_encode(["status"=>"2","msg"=>"No files in the POST data."]));
|
echo(json_encode(["status"=>"2","msg"=>"No files in the POST data."]));
|
||||||
|
exit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function searchExercises($query, $length, $tags)
|
||||||
|
{
|
||||||
|
$conn = new mysqli($GLOBALS["servername"], $GLOBALS["username"], $GLOBALS["password"], $GLOBALS["dbname"]);
|
||||||
|
|
||||||
|
if ($conn->connect_error) {
|
||||||
|
throw new Exception("Connection failed: " . $conn->connect_error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build the SQL query based on the search parameters
|
||||||
|
$sql = "SELECT * FROM exercices";
|
||||||
|
|
||||||
|
if (!empty($query) || !empty($length) || !empty($tags)) {
|
||||||
|
$sql .= " WHERE";
|
||||||
|
}
|
||||||
|
|
||||||
|
$conditions = [];
|
||||||
|
|
||||||
|
if (!empty($query)) {
|
||||||
|
$conditions[] = "titre LIKE '%$query%'";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($length)) {
|
||||||
|
$conditions[] = "duree = $length";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($tags)) {
|
||||||
|
$tagConditions = array_map(function ($tag) {
|
||||||
|
return "EXISTS (SELECT 1 FROM exercices_themes et, themes t WHERE et.exercice_id = e.id AND et.theme_id = t.id AND t.name = '$tag')";
|
||||||
|
}, $tags);
|
||||||
|
|
||||||
|
$conditions[] = implode(" AND ", $tagConditions);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql .= implode(" AND ", $conditions);
|
||||||
|
|
||||||
|
// Execute the query
|
||||||
|
$result = $conn->query($sql);
|
||||||
|
|
||||||
|
if (!$result) {
|
||||||
|
throw new Exception("Error executing search query: " . $conn->error);
|
||||||
|
}
|
||||||
|
|
||||||
|
$exercises = [];
|
||||||
|
|
||||||
|
while ($row = $result->fetch_assoc()) {
|
||||||
|
$exercises[] = $row;
|
||||||
|
}
|
||||||
|
|
||||||
|
$conn->close();
|
||||||
|
|
||||||
|
return $exercises;
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
|
@ -27,6 +27,8 @@ D'autres fonctionnalités seront ajoutées petit à petit. (si vous avez des sug
|
||||||
| titre | VARCHAR(255) | NOT NULL |
|
| titre | VARCHAR(255) | NOT NULL |
|
||||||
| commentaire_auteur | TEXT | |
|
| commentaire_auteur | TEXT | |
|
||||||
| document_id | INT | FOREIGN KEY (document_id) REFERENCES documents(id) |
|
| document_id | INT | FOREIGN KEY (document_id) REFERENCES documents(id) |
|
||||||
|
| duree | INT | |
|
||||||
|
(la durée est en secondes)
|
||||||
|
|
||||||
### Table: ensemble
|
### Table: ensemble
|
||||||
|
|
||||||
|
|
|
@ -8,12 +8,9 @@
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<!-- Input to choose files -->
|
<!-- Input to choose files -->
|
||||||
|
|
||||||
|
<form id="uploadForm">
|
||||||
<input type="file" id="fileInput" multiple>
|
<input type="file" id="fileInput" multiple>
|
||||||
<button onclick="uploadFiles()">Upload Files</button>
|
|
||||||
|
|
||||||
<!-- Button to open the camera -->
|
|
||||||
<button onclick="openCamera()">Open Camera</button>
|
|
||||||
|
|
||||||
<input type="text" placeholder="titre" id="titre"></input>
|
<input type="text" placeholder="titre" id="titre"></input>
|
||||||
|
|
||||||
<select id="select_type">
|
<select id="select_type">
|
||||||
|
@ -22,6 +19,14 @@
|
||||||
|
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<button type="button" onclick="uploadFiles()">Upload File</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<!-- Button to open the camera -->
|
||||||
|
<button onclick="openCamera()">Open Camera</button>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
function uploadFiles() {
|
function uploadFiles() {
|
||||||
const fileInput = document.getElementById('fileInput');
|
const fileInput = document.getElementById('fileInput');
|
||||||
|
@ -29,12 +34,12 @@ function uploadFiles() {
|
||||||
// Create FormData object to append files
|
// Create FormData object to append files
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
|
|
||||||
formData.append("type",document.getElementById("select_type").getAttribute("value"));
|
formData.append("type",document.getElementById("select_type").value);
|
||||||
formData.append("titre",document.getElementById("titre").getAttribute("value"));
|
formData.append("titre",document.getElementById("titre").value);
|
||||||
|
|
||||||
// Append each selected file to the FormData
|
// Append each selected file to the FormData
|
||||||
for (const file of fileInput.files) {
|
for (const file of fileInput.files) {
|
||||||
formData.append('files[]', file);
|
formData.append('fichiers', file);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make a POST request using Fetch API
|
// Make a POST request using Fetch API
|
||||||
|
@ -42,7 +47,7 @@ function uploadFiles() {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: formData
|
body: formData
|
||||||
})
|
})
|
||||||
.then(response => response.json())
|
.then(response => response.text())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
console.log(data);
|
console.log(data);
|
||||||
// Handle the response from the server
|
// Handle the response from the server
|
||||||
|
@ -85,12 +90,13 @@ function openCamera() {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
files: [{ name: 'camera_image.jpg', data: imageDataUrl.split(',')[1] }]
|
fichiers: [{ name: 'camera_image.jpg', data: imageDataUrl.split(',')[1] }]
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.then(response => response.json())
|
.then(response => response.text())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
console.log(data);
|
console.log(data);
|
||||||
|
|
||||||
// Handle the response from the server
|
// Handle the response from the server
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
|
Loading…
Reference in a new issue