On utilise AJAX pour le téléversement

This commit is contained in:
Matthieu Patout 2021-02-02 12:46:36 +01:00
parent d829d5016e
commit 823f13f39e
2 changed files with 97 additions and 128 deletions

View File

@ -0,0 +1,97 @@
<?php
// We have a ZIP file !
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];
$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
break;
}
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
echo "Votre fichier n'est pas une archive ZIP.";
exit();
}
// Path and name
$filenoext = basename ($filename, '.zip');
$filenoext = basename ($filenoext, '.ZIP');
$targetdir = $path . $filenoext;
$targetzip = $path . $filename;
// Uncompress ZIP then remove file
if(move_uploaded_file($source, $targetzip)) {
$zip = new ZipArchive();
$x = $zip->open($targetzip);
if ($x === true) {
$zip->extractTo('.');
$zip->close();
unlink($targetzip);
}
} else {
echo nl2br("Une erreur vient de se produire lors du téléversement de votre fichier.\r\n");
exit();
}
// Download latest CMS zip with cURL
$ch = curl_init();
$source = "https://ouvaton.coop/oci/framagrav-themes.zip";
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
// If any cURL error stop processing
if(curl_errno($ch)) {
echo 'Erreur cURL : '.curl_error($ch);
curl_close($ch);
exit;
}
// Check HTTP code
switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
case 200:
// Create CMS zip
$destination = "themes.zip";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
curl_close($ch);
// Unzip archive file
$file = 'themes.zip';
$zip = new ZipArchive;
if ($zip->open($file) === TRUE) {
$zip->extractTo('.');
$zip->close();
} else {
echo nl2br("Une erreur lors de la décompression de l'archive vient de se produire, contactez l'assistance !\r\n");
exit;
}
// Zip archive is not needed anymore
unlink('themes.zip');
// Website URI
$host = $_SERVER['HTTP_HOST'];
// Set URI
$grav_custom = "custom_base_url: 'https://".$host."'";
$set_grav_custom = file_put_contents('user/config/system.yaml', $grav_custom.PHP_EOL , FILE_APPEND | LOCK_EX);
// Remove scripts
unlink('oci-framagrav.php');
unlink(__FILE__);
echo "L'installation est terminée !";
exit;
default:
// If any HTTP error stop processing
echo nl2br("Code HTTP : ".$http_code."\r\n");
curl_close($ch);
exit;
}
?>

View File

@ -1,128 +0,0 @@
<?php
function dir_is_empty($dirname)
{
// Check if directory is empty but ignore install script
if (!is_dir($dirname)) return false;
foreach (scandir($dirname) as $file) {
if (!in_array($file, array('.','..','oci-framagrav.php'))) return false;
}
return true;
}
// If install directory is not empty stop processing
$dest = dirname(__FILE__).'/';
if (dir_is_empty($dest) === false) {
echo nl2br("Le dossier httpdocs/ doit être vide pour y migrer votre Framasite.\r\n");
exit();
}
// We have a ZIP file !
if($_FILES["zip_file"]["name"]) {
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];
$name = explode(".", $filename);
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
break;
}
}
$continue = strtolower($name[1]) == 'zip' ? true : false;
if(!$continue) {
$message = "Votre fichier n'est pas une archive ZIP.";
}
/* PHP current path */
$filenoext = basename ($filename, '.zip'); // absolute path (lowercase)
$filenoext = basename ($filenoext, '.ZIP'); // absolute path (uppercase)
$targetdir = $path . $filenoext; // target directory
$targetzip = $path . $filename; // target zip file
/* here it is really happening */
if(move_uploaded_file($source, $targetzip)) {
$zip = new ZipArchive();
$x = $zip->open($targetzip); // open the zip file to extract
if ($x === true) {
$zip->extractTo('.'); // place in the directory with same name
$zip->close();
unlink($targetzip);
}
} else {
echo nl2br("Une erreur vient de se produire lors du téléversement de votre fichier.\r\n");
exit();
}
// Download latest CMS zip with cURL
$ch = curl_init();
$source = "https://ouvaton.coop/oci/framagrav-themes.zip";
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
// If any cURL error stop processing
if(curl_errno($ch)) {
echo nl2br("Une erreur vient de se produire. Rechargez la page pour essayer à nouveau, ou contactez l'assistance si le problème persiste.\r\n");
echo 'Erreur cURL : '.curl_error($ch);
curl_close($ch);
exit;
}
// Check HTTP code
switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
case 200:
// Create CMS zip
$destination = "themes.zip";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
curl_close($ch);
// Unzip archive file
$file = 'themes.zip';
$zip = new ZipArchive;
if ($zip->open($file) === TRUE) {
$zip->extractTo('.');
$zip->close();
} else {echo nl2br("Une erreur lors de la décompression de l'archive vient de se produire, contactez l'assistance !\r\n");}
// Zip archive is not needed anymore
unlink('themes.zip');
// Redirecting user to CMS install page
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
header("Location: http://$host$uri/");
// Remove script
unlink(__FILE__);
exit;
default:
// If any HTTP error stop processing
echo nl2br("Une erreur vient de se produire. Rechargez la page pour essayer à nouveau, ou contactez l'assistance si le problème persiste.\r\n");
echo nl2br("Code HTTP : ".$http_code."\r\n");
curl_close($ch);
exit;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Migration de votre Framasite chez Ouvaton</title>
</head>
<body>
<?php if($message) echo "<p>$message</p>"; ?>
<form enctype="multipart/form-data" method="post" action="">
<label>Sélectionnez l'archive ZIP de votre site : <input type="file" name="zip_file" /></label>
<br />
<input type="submit" name="submit" value="Envoyer" />
</form>
</body>
</html>