ouvsnippets/oci-framasite/oci-framagrav-uploader.php

98 lines
2.6 KiB
PHP
Raw Normal View History

<?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;
}
?>