<?php
/*****************************************************************
* Spacemarc News
* Version: 1.2.0
* Author and copyright (C): Marcello Vitagliano
* Web site: http://www.spacemarc.it
* License: GNU General Public License
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Current file: getpdf.php
*****************************************************************/
header('Content-type: text/html; charset=ISO-8859-1');
//includo i file di configurazione
require_once (dirname(__FILE__) . '/config.php');
require_once (dirname(__FILE__) . '/pdf/fpdf.php');
// apro la connessione a mysql
$db = mysql_connect($db_host, $db_user, $db_password) or die("Impossibile connettersi a MySQL<br />Numero errore: " . mysql_errno() . "<br />Tipo di errore: " . mysql_error());
mysql_select_db($db_name, $db) or die("Impossibile selezionare il database $db_name<br />Numero errore: " . mysql_errno() . "<br />Tipo di errore: " . mysql_error());
//seleziono l'ID minore e maggiore delle news
$result = mysql_query("SELECT MIN(id) AS MinID, MAX(id) AS MaxID FROM $tab_news");
$row = @mysql_fetch_array($result);
//se l'id della notizia c'è ed è di tipo numerico ed è presente in tabella procedo
if (isset($_GET['id']) && preg_match('/^[0-9]{1,8}$/', $_GET['id']) && $_GET['id'] <= $row['MaxID'] && $_GET['id'] >= $row['MinID']) {
$result2 = mysql_query("SELECT nt.id, nt.titolo, nu.nome_cognome, nt.testo, nt.data_pubb, nt.letture, nt.stampe FROM $tab_news nt JOIN $tab_utenti nu ON nu.user_id=nt.user_id WHERE nt.id=" . intval($_GET['id']));
$row2 = mysql_fetch_array($result2);
$titolo = html_entity_decode($row2['titolo'], ENT_QUOTES, 'ISO-8859-1');
$testo = nl2br(html_entity_decode($row2['testo'], ENT_QUOTES, 'ISO-8859-1'));
$autore = html_entity_decode($row2['nome_cognome'], ENT_QUOTES, 'ISO-8859-1');
//estraggo alcune configurazioni
$result3 = mysql_query("SELECT url_sito, formato_data FROM $tab_config");
$row3 = mysql_fetch_array($result3);
$url_sito = $row3['url_sito'];
switch ($row3['formato_data']) {
case 1:
$formato_data = strftime("%a %d %b %Y %H:%M", $row2['data_pubb']);
break;
case 2:
$formato_data = html_entity_decode(str_replace("ì", "ì", strftime("%A %d %B %Y %H:%M", $row2['data_pubb'])));
break;
case 3:
$formato_data = strftime("%d/%m/%Y %H:%M", $row2['data_pubb']);
break;
case 4:
$formato_data = strftime("%d %B %Y %H:%M", $row2['data_pubb']);
break;
case 5:
$formato_data = strftime("%d %b %Y %H:%M", $row2['data_pubb']);
break;
}
//sostituisco i codici di formattazione
$testo_cerca = array(
"{’}",
"{€}",
"{<br />}",
"{\[ul]}",
"{\[li]}",
"{\[/li]}",
"{\[/ul]}",
"{&}",
"{\[img]}",
"{\[/img]}",
"{\[b]}",
"{\[i]}",
"{\[u]}",
"{\[/b]}",
"{\[/i]}",
"{\[/u]}",
"{\[email\](\r\n|\r|\n)*([a-zA-Z0-9\._-]+@(([a-zA-Z0-9_-])+\.)+[a-z]{2,6})\[/email\]}siU",
"{\[email=(\w[\w\-\.\+]*?@\w[\w\-\.]*?\w\.[a-zA-Z]{2,4})\](.+)?\[\/email\]}siU",
"{(\[)(url)(])((http|ftp|https)://)([^;<>\*\(\)\"\s]*)(\[/url\])}siU",
"{(\[)(url)(=)(['\"]?)((http|ftp|https)://)([^;<>\*\(\)\"\s]*)(\\4])(.*)(\[/url\])}siU",
"{(\[)(callto)(])((callto):)([^;<>\*\(\)\"\s]*)(\[/callto\])}siU",
"{(\[)(callto)(=)(['\"]?)((callto):)([^;<>\*\(\)\"\s]*)(\\4])(.*)(\[/callto\])}siU",
"{(\[)(size)(=)(['\"]?)([0-9]*)(\\4])(.*)(\[/size\])}siU",
"{\[quote\](\r\n|\r|\n)*(.+)\[/quote\]}siU",
"{\[code\](\r\n|\r|\n)*(.+)\[/code\]}siU",
"{\[yt\]([0-9A-Za-z-_]{11})\[/yt]}siU"
);
$testo_sostituisci = array(
"'",
"E",
"",
"",
"\n#",
"",
"",
"&",
"[Foto: ",
"]",
"",
"",
"",
"",
"",
"",
"\\2",
"\\2 [mailto: \\1]",
"\\4\\6",
"\\9 [url: \\5\\7]",
"\\4\\6",
"\\9 [\\5\\7]",
"\\7",
"[Citazione]\n\\2\n[/Citazione]",
"[Codice]\n\\2\n[/Codice]",
"[Video: youtube.com/watch?v=\\1]"
);
$testoPdf = preg_replace($testo_cerca, $testo_sostituisci, $testo);
class PDF extends FPDF {
function ChapterTitle() {
global $titolo, $autore, $url_sito, $news_dir, $row2, $img_dir, $formato_data;
$titoloChars = array(
'’' => '\'',
'€' => 'E'
);
$titoloPdf = strtr($titolo, $titoloChars);
//inserire il logo del proprio sito
$this->Image($img_dir . '/logonews.gif', 11, 8, 38, 7);
$this->Ln(8);
//font e posizione della cella contenente il titolo
$this->SetFont('Times', 'B', 9);
$this->Cell(1);
//posizione del titolo
$this->MultiCell(0, 4, $titoloPdf, 0);
$this->Ln(5);
//font e posizione delle info sulla notizia
$this->SetFont('Times', '', 9);
$this->SetFillColor(225, 225, 225);
$this->Cell(0, 4, "di $autore - $formato_data - Letture: " . $row2['letture'] . " - Stampe: " . $row2['stampe'] . " - $url_sito/$news_dir/view.php?id=" . $row2['id'] . "", 0, 1, "L", 1);
$this->Ln(8);
//metadati
$this->Settitle($titoloPdf);
$this->SetAuthor($autore);
}
function PrintChapter() {
$this->ChapterTitle();
}
//footer
function Footer() {
//Paginazione a 15 mm dal bordo inferiore della pagina
$this->SetY(-15);
$this->SetTextColor(120, 120, 120);
$this->SetFont('Arial', '', 8);
$this->Cell(0, 10, 'Pagina ' . $this->PageNo() . ' di {nb}', 0, 0, 'C');
}
}
//istanzio la classe e invio il pdf al browser
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->PrintChapter();
$pdf->MultiCell(0, 4, $testoPdf, 0);
$pdf->Output("News_" . $row2['id'] . ".pdf", 'D'); //Nome del file; D=scarica il file; I=apre il file nel browser attraverso l'apposito plugin
//chiudo la connessione a mysql
mysql_close($db);
}
else {
die("Notizia inesistente");
}
?>