Add files via upload

bugfix
This commit is contained in:
Daniel Intrieri 2024-01-25 16:16:59 +01:00 committed by GitHub
parent 9d0d1144e2
commit eae43dfe06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 77432 additions and 0 deletions

512
funzioni.php Normal file
View File

@ -0,0 +1,512 @@
<?php
function visualizzapdfcorrente($wpdb,$etichetta) {
$query = "SELECT * FROM {$wpdb->prefix}listapdf";
$datacorrenteunix = strtotime(date('Y-m-d'));
$query = "SELECT * FROM {$wpdb->prefix}listapdf";
$results = $wpdb->get_results($query);
$controllo = false;
foreach ($results as $result) {
$dataconfrontoiniziounix = strtotime($result->data_partenza);
$dataconfrontofineunix = strtotime($result->data_fine);
if ($datacorrenteunix <= $dataconfrontofineunix){
if ($datacorrenteunix >= $dataconfrontoiniziounix){
$controllo = true;
$nomefileprescelto = $result->nome_file;
$urlfileprescelto = $result->url_completo;
}
}
}
if ($controllo){
$urlcomp = get_home_url().'/wp-content/plugins/ieimenupdf/menu/';
$finalurl = $urlcomp.$urlfileprescelto;
if (!is_string($etichetta)){
$contenutohtml = generahtmlmodal($finalurl);
} else {
$contenutohtml = generahtmlmodalridotto($finalurl,$etichetta);
}
return array("nome"=>$nomefileprescelto, "url"=>$finalurl,'html'=>$contenutohtml,'controllo'=>$controllo);
} else {
return array ('controllo'=>$controllo);
}
}
function generacssmodal (){
return '<style>
.pdf-container {
overflow-y: auto;
overflow-x:hidden;
max-height: 100%; /* Adjust as needed */
width: 100%;
margin-bottom: 20px;
}
@media only screen and (max-width: 600px) {
canvas {
margin-top:50px;
}
.button-container {
display: flex;
justify-content: space-between;
padding: 10px;
}
}
@media only screen and (min-width: 601px) {
.button-container {
text-align:center;
}
}
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.7);
justify-content: center;
align-items: center;
z-index:50 !important;
}
/* Contenuto modal */
.modal-content {
background-color: #fff;
width: 100%;
height: 100%;
position: relative;
}
/* Tasto chiudi */
.close {
background-color:black;
padding:10px;
position: absolute;
top: 10px;
right: 10px;
font-size: 20px;
cursor: pointer;
color: white;
}
.bottonebello {
background-color: #04AA6D; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
-webkit-transition-duration: 0.4s; /* Safari */
transition-duration: 0.4s;
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
}
#prev {
padding: 10px;
background-color: #04AA6D; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
-webkit-transition-duration: 0.4s; /* Safari */
transition-duration: 0.4s;
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
}
#next {
padding: 10px;
background-color: #04AA6D; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
-webkit-transition-duration: 0.4s; /* Safari */
transition-duration: 0.4s;
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
}
.bottonebello:hover {
box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24),0 17px 50px 0 rgba(0,0,0,0.19);
}
</style>
';
}
function generajsmodal() {
return '
<script>
// Open the modal
function openModal() {
document.getElementById("pdfModal").style.display = "flex";
}
// Close the modal
function closeModal() {
document.getElementById("pdfModal").style.display = "none";
}
</script>
<script>
document.addEventListener("DOMContentLoaded", function() {
var elements = document.querySelectorAll(".is-layout-constrained");
elements.forEach(function(element) {
element.classList.remove("is-layout-constrained");
});
});
</script>
<script>
function stopScrollPropagation(event) {
// Questo evita che lo scroll sul modale vada sulla pagina
event.stopPropagation();
}
</script>
';
}
function generahtmlmodal($filepdf){
$urlcomp = get_home_url().'/wp-content/plugins/ieimenupdf/';
return '
<div style="text-align:center;z-index:5000;">
<button class="bottonebello" onclick="openModal()">Apri il Menu</button>
</div>
<!-- Modale PDF -->
<div style="background-color:white;" id="pdfModal" class="modal" onscroll="stopScrollPropagation(event)">
<div class="modal-content">
<!-- Close button -->
<span class="close" onclick="closeModal()">&times;</span>
<!-- METTERE QUI IL VISUALIZZATORE -->
<script src="'.$urlcomp.'pdf.js" type="module"></script>
<script type="module">
// If absolute URL from the remote server is provided, configure the CORS
// header on that server.
var url = "'.$filepdf.'";
// Loaded via <script> tag, create shortcut to access PDF.js exports.
var { pdfjsLib } = globalThis;
// The workerSrc property shall be specified.
pdfjsLib.GlobalWorkerOptions.workerSrc = "'.$urlcomp.'pdf.worker.js";
var pdfDoc = null,
pageNum = 1,
pageRendering = false,
pageNumPending = null,
scale = 3.0,
canvas = document.getElementById("the-canvas"),
ctx = canvas.getContext("2d");
/**
* Get page info from document, resize canvas accordingly, and render page.
* @param num Page number.
*/
function renderPage(num) {
pageRendering = true;
// Using promise to fetch the page
pdfDoc.getPage(num).then(function(page) {
var viewport = page.getViewport({scale: scale});
canvas.height = viewport.height;
canvas.width = viewport.width;
// Render PDF page into canvas context
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
var renderTask = page.render(renderContext);
// Wait for rendering to finish
renderTask.promise.then(function() {
pageRendering = false;
if (pageNumPending !== null) {
// New page rendering is pending
renderPage(pageNumPending);
pageNumPending = null;
}
});
});
// Update page counters
document.getElementById("page_num").textContent = num;
}
/**
* If another page rendering in progress, waits until the rendering is
* finised. Otherwise, executes rendering immediately.
*/
function queueRenderPage(num) {
if (pageRendering) {
pageNumPending = num;
} else {
renderPage(num);
}
}
/**
* Displays previous page.
*/
function onPrevPage() {
if (pageNum <= 1) {
return;
}
pageNum--;
queueRenderPage(pageNum);
}
document.getElementById("prev").addEventListener("click", onPrevPage);
/**
* Displays next page.
*/
function onNextPage() {
if (pageNum >= pdfDoc.numPages) {
return;
}
pageNum++;
queueRenderPage(pageNum);
}
document.getElementById("next").addEventListener("click", onNextPage);
/**
* Asynchronously downloads PDF.
*/
pdfjsLib.getDocument(url).promise.then(function(pdfDoc_) {
pdfDoc = pdfDoc_;
document.getElementById("page_count").textContent = pdfDoc.numPages;
// Initial/first page rendering
renderPage(pageNum);
});
</script>
<div class="pdf-container" style="overflow:scroll;">
<canvas style="width:100%;" id="the-canvas"></canvas>
<div class="button-container">
<button id="prev">Indietro</button>
<button id="next">Avanti</button>
</div>
<br>
<div style="text-align:center;">
<span>Pagina: <span id="page_num"></span> / <span id="page_count"></span></span>
</div>
</div> <!-- FINE VISUALIZZATORE -->
</div>
</div>
';
}
function generahtmlmodalridotto($filepdf,$etichetta){
$urlcomp = get_home_url().'/wp-content/plugins/ieimenupdf/';
return '
<div style="z-index:5000;">
<div style="text-align:center;margin-bottom:12px;"><a style="font-size:20px;text-transform: uppercase;cursor: pointer;" onclick="openModal()">'.$etichetta.'</a></div>
<!-- Modale PDF -->
<div style="background-color:white;" id="pdfModal" class="modal" onscroll="stopScrollPropagation(event)">
<div class="modal-content">
<!-- Close button -->
<span class="close" onclick="closeModal()">&times;</span>
<!-- METTERE QUI IL VISUALIZZATORE -->
<script src="'.$urlcomp.'pdf.js" type="module"></script>
<script type="module">
// If absolute URL from the remote server is provided, configure the CORS
// header on that server.
var url = "'.$filepdf.'";
// Loaded via <script> tag, create shortcut to access PDF.js exports.
var { pdfjsLib } = globalThis;
// The workerSrc property shall be specified.
pdfjsLib.GlobalWorkerOptions.workerSrc = "'.$urlcomp.'pdf.worker.js";
var pdfDoc = null,
pageNum = 1,
pageRendering = false,
pageNumPending = null,
scale = 3.0,
canvas = document.getElementById("the-canvas"),
ctx = canvas.getContext("2d");
/**
* Get page info from document, resize canvas accordingly, and render page.
* @param num Page number.
*/
function renderPage(num) {
pageRendering = true;
// Using promise to fetch the page
pdfDoc.getPage(num).then(function(page) {
var viewport = page.getViewport({scale: scale});
canvas.height = viewport.height;
canvas.width = viewport.width;
// Render PDF page into canvas context
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
var renderTask = page.render(renderContext);
// Wait for rendering to finish
renderTask.promise.then(function() {
pageRendering = false;
if (pageNumPending !== null) {
// New page rendering is pending
renderPage(pageNumPending);
pageNumPending = null;
}
});
});
// Update page counters
document.getElementById("page_num").textContent = num;
}
/**
* If another page rendering in progress, waits until the rendering is
* finised. Otherwise, executes rendering immediately.
*/
function queueRenderPage(num) {
if (pageRendering) {
pageNumPending = num;
} else {
renderPage(num);
}
}
/**
* Displays previous page.
*/
function onPrevPage() {
if (pageNum <= 1) {
return;
}
pageNum--;
queueRenderPage(pageNum);
}
document.getElementById("prev").addEventListener("click", onPrevPage);
/**
* Displays next page.
*/
function onNextPage() {
if (pageNum >= pdfDoc.numPages) {
return;
}
pageNum++;
queueRenderPage(pageNum);
}
document.getElementById("next").addEventListener("click", onNextPage);
/**
* Asynchronously downloads PDF.
*/
pdfjsLib.getDocument(url).promise.then(function(pdfDoc_) {
pdfDoc = pdfDoc_;
document.getElementById("page_count").textContent = pdfDoc.numPages;
// Initial/first page rendering
renderPage(pageNum);
});
</script>
<div class="pdf-container" style="overflow:scroll;">
<canvas style="width:100%;" id="the-canvas"></canvas>
<div class="button-container">
<button id="prev">Indietro</button>
<button id="next">Avanti</button>
</div>
<br>
<div style="text-align:center;">
<span>Pagina: <span id="page_num"></span> / <span id="page_count"></span></span>
</div>
</div> <!-- FINE VISUALIZZATORE -->
</div>
</div>
</div>
';
}

185
iei-menupdf.php Normal file
View File

@ -0,0 +1,185 @@
<?php
include("funzioni.php");
/**
* Plugin Name: Iei MenuPdf
* Description: Crea uno shortcode con il pdf del tuo Menu
* Version: 0.0.1
* Text Domain: options-plugin
*/
if (!defined("ABSPATH")) {
exit; // esci se questo file viene letto direttamente
}
if (!class_exists("ieimenupdf")) {
class ieimenupdf
{
public function __construct()
{
add_action('admin_menu', array($this, 'crea_voci'));
//questo si triggera quando il plugin viene attivato ed aggiunge una nuova tabella nel database
register_activation_hook(__FILE__, array($this, 'plugin_activation'));
//questo aggiunge la possibilità di avere uno shortcode
add_action('init', array($this, 'register_shortcodes'));
add_action('init', array($this, 'register_shortcodes_a'));
}
public function crea_voci()
{
// Add main menu entry
add_menu_page(
'IEI Menu',
'Il tuo menu',
'manage_options',
'iei-menu-dashboard',
array($this, 'iei_menu_dashboard_page'),
'dashicons-food', // Icon for the menu entry (replace with your preferred icon)
20 // Position on the menu
);
add_submenu_page(
'iei-menu-dashboard',
'Internet & Idee',
'Internet & Idee',
'manage_options',
'iei-menu-external',
array($this, 'iei_menu_external_page')
);
}
// Callback functions per menu e submenu del plugin
public function iei_menu_dashboard_page()
{
// Main dashboard page content
echo '<h2>Il Tuo Menu</h2><br>';
echo '<br>Per visualizzare il tuo menu in una pagina, usa questo shortcode: <br> <input style="margin-top:10px;" type="text" value="[ieimenupdf_content]"/>';
include("listafile.php");
}
public function iei_menu_external_page()
{
echo '<h2>Internet & Idee</h2>';
echo '<p>Questo plugin è stato sviluppato da <a target="_blank" href="https://internet-idee.net">Internet & Idee</a></p>';
echo '<img style="width:250px;" src="https://www.internet-idee.net/assets/img/colore.svg"/>';
// Contenuto
}
// LAVORAZIONE PER CREAZIONE TABELLA NEL DATABASE ALL'ATTIVAZIONE DEL PLUGIN
public function plugin_activation()
{
$this->create_database_table();
}
public function create_database_table()
{
global $wpdb;
$table_name = $wpdb->prefix . 'listapdf';
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
nome_file varchar(255) NOT NULL,
url_completo varchar(255) NOT NULL,
data_partenza date NOT NULL,
data_fine date NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
// FINE LAVORAZIONE PER CREAZIONE TABELLA NEL DATABASE
//GESTIONE SHORTCODE
public function ieimenupdf_shortcode() {
// Qui mettere lo shortcode
global $wpdb;
$etichetta = 0;
$arrayrisultati = visualizzapdfcorrente($wpdb,$etichetta);
if (isset($arrayrisultati)){
return $arrayrisultati['html'];} else {
return 'Nessun menu abilitato';
}
}
public function register_shortcodes() {
add_shortcode('ieimenupdf_content', array($this, 'ieimenupdf_shortcode'));
}
//GESTIONE SHORTCODE
//GESTIONE SHORTCODE TAG A
public function ieimenupdf_shortcode_a($atts) {
//Accesso ad array attributi per prendermi il nome etichetta
$atts = shortcode_atts(
array(
'etichetta' => 'Menu',
// Add more attributes if needed
),
$atts,
'ieimenupdf_content_a' // Shortcode di riferimento
);
// Qui mettere lo shortcode
global $wpdb;
$etichetta = $atts['etichetta'];
$arrayrisultati = visualizzapdfcorrente($wpdb,$etichetta);
if (isset($arrayrisultati)){
return $arrayrisultati['html'];} else {
return 'Nessun menu abilitato';
}
}
public function register_shortcodes_a() {
add_shortcode('ieimenupdf_content_a', array($this, 'ieimenupdf_shortcode_a'));
}
//GESTIONE SHORTCODE TAG A
}//FINE CLASSE
new ieimenupdf();
// AGGIUNGO CSS A HEADER DI WORDPRESS
function custom_header_content() {
// Add your custom content here
$urlcomp = get_home_url().'/wp-content/plugins/ieimenupdf/';
echo '<script src="'.$urlcomp.'pdf.js"></script>';
echo generacssmodal();
echo generajsmodal();
}
add_action('wp_head', 'custom_header_content');
}

3
index.php Normal file
View File

@ -0,0 +1,3 @@
<?php
die("silence is golden");

233
listafile.php Normal file
View File

@ -0,0 +1,233 @@
<style>
table {
margin-top:50px;
border-collapse: collapse !important;
border-spacing: 0 !important;
width: 100% !important;
border: 1px solid #ddd !important;
background-color:white;
}
th, td {
text-align: left !important;
padding: 8px !important;
}
tr:nth-child(even){background-color: #f2f2f2 !important}
</style>
<?php
//caricamento globale per db wordpress per tutte le operazioni
global $wpdb;
//impostazione variabile per tutte le operazioni di lettura/scrittura su directory
$target_dir = $_SERVER['DOCUMENT_ROOT']."/wp-content/plugins/ieimenupdf/menu/";
?>
<?php
// QUI MI CONTROLLO L'ELIMINAZIONE
if (isset($_GET['elimina'])){
$datoelimina = $_GET['elimina'];
//CARICO E CONTROLLO DAL DATABASE
// QUERY SUL DATABASE
$query = "SELECT * FROM {$wpdb->prefix}listapdf WHERE id='$datoelimina'";
// ESECUZIONE QUERY
$results = $wpdb->get_results($query);
foreach ($results as $result) {
$urlfile = $result->url_completo;
}
//ELIMINO IL FILE DALLA DIRECTORY
unlink($target_dir.$urlfile);
// QUERY SUL DATABASE PER ELIMINAZIONE RIFERIMENTO FILE
$query = "DELETE FROM {$wpdb->prefix}listapdf WHERE id='$datoelimina'";
// ESECUZIONE QUERY
$results = $wpdb->get_results($query);
}
// QUI MI CONTROLLO L'ELIMINAZIONE
?>
<?php
// QUI AVVIENE IL CARICAMENTO DI UN FILE
$esitofile = "";
if(isset($_POST["invia"])) {
$campoinizio = $_POST['datainizio'];
$campofine = $_POST['datafine'];
if (!empty($campoinizio)&& !empty($campofine)) {
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$uploadOk = 1;
// Controlla se il file esiste
if (file_exists($target_file)) {
$esitofile = 'Esiste già un file con questo nome. Scegliere un altro nome oppure eliminare quello esistente dalla lista.';
$uploadOk = 0;
}
if ($uploadOk){
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$nomefile = htmlspecialchars( basename( $_FILES["fileToUpload"]["name"]));
$esitofile = "Il file ". $nomefile . " è stato caricato.";
// QUERY SUL DATABASE PER INSERIMENTO FILE
$query = "INSERT INTO {$wpdb->prefix}listapdf (nome_file, url_completo, data_partenza, data_fine) VALUES ('$nomefile','$nomefile','$campoinizio','$campofine')";
// ESECUZIONE QUERY
$results = $wpdb->get_results($query);
}else {
$esitofile = 'file non caricato';
}
}
} else {
$esitofile = 'Assicurati di aver compilato tutti i campi';
}
}
// QUI AVVIENE IL CARICAMENTO DI UN FILE
?>
<?php
$urlcomp = get_home_url();
echo '<link rel="stylesheet" href="'.$urlcomp.'/wp-content/plugins/ieimenupdf/elementi/style.css">';
?>
<?php
// QUERY SUL DATABASE
$query = "SELECT * FROM {$wpdb->prefix}listapdf";
// ESECUZIONE QUERY
$results = $wpdb->get_results($query);
?>
<hr>
<h2>Carica un nuovo menu</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?page=iei-menu-dashboard" method="post" enctype="multipart/form-data">
<label for="pdfFile">Scegli file PDF:</label>
<input type="file" id="fileToUpload" name="fileToUpload" accept="application/pdf">
<label for="datainizio">Imposta data inizio</label>
<input type="date" id="datainizio" name="datainizio">
<label for="datafine">Imposta data fine</label>
<input type="date" id="datafine" name="datafine">
<input type="submit" name="invia" value="Carica">
</form>
<p style="color:red;font-weight:bold;"><?php echo $esitofile; ?></p>
<hr>
<div style="overflow-x:auto;">
<table>
<tr>
<th>Nome del File</th>
<th>Data Partenza</th>
<th>Data Fine</th>
<th>/</th>
</tr>
<?php
foreach ($results as $result) {
echo '<tr><td>'.$result->nome_file.'</td>';
echo '<td>'.$result->data_partenza.'</td>';
echo '<td>'.$result->data_fine.'</td>';
echo '<td><button onclick="elimina('.$result->id.')">elimina</button></td></tr>';
}
?>
</table>
</div>
<div id="infobox" style="margin-top:30px;">
<?php
$etichetta = 0;
$arrayrisultati = visualizzapdfcorrente($wpdb,$etichetta);
if ($arrayrisultati['controllo']){
echo '<b>In questo momento il menu attivo è: '.$arrayrisultati['nome'].'</b><br>';
echo '<b>Per vederlo nel browser clicca qui: <a target="_blank" href="'.$arrayrisultati['url'].'">VEDI MENU PDF</a>';
} else {
echo '<b>Non esiste un menu attivo in questo momento, controlla le date dei file caricati oppure carica un nuovo file.</b>';
}
?>
</div>
<script>
function elimina(elemento){
updateURLParameter('elimina', elemento);
}
function updateURLParameter(key, value) {
// Get the current URL
var url = window.location.href;
// Check if the parameter already exists in the URL
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
var separator = url.indexOf('?') !== -1 ? "&" : "?";
if (url.match(re)) {
// If the parameter exists, remove it
url = url.replace(re, '$1' + key + "="+ value +'$2');
} else {
// If the parameter doesn't exist, add it
url = url + separator + key + '=' + value;
}
// Refresh the page with the updated URL
window.location.href = url;
}
</script>

18146
pdf.js Normal file

File diff suppressed because it is too large Load Diff

58353
pdf.worker.js vendored Normal file

File diff suppressed because one or more lines are too long