Files
bm/public_html/public/cms/modules/fileblock.class.php
2025-09-24 13:26:28 +02:00

252 lines
7.0 KiB
PHP

<?php
/** File Block
*
* @version 1.9.0
* @since 2007-04-16
* @author martin lenzelbauer
*/
class FileBlock extends BuildingBlock{
var $file; //filename
var $displayName; //name that will be displayed
var $fileDir;
var $absoluteUrl;
/** C'tor
*/
//------------------------------------------------
function FileBlock($id=0){
//------------------------------------------------
parent::BuildingBlock($id);
$this->file = "";
$this->displayName = "";
$this->setDirectory(FILE_DIR);
$this->useAbsoluteUrl(false);
}
/** @see BuildingBlock::install()
*/
//-----------------------------------------------
function install(){
//-----------------------------------------------
$query = sprintf("CREATE TABLE IF NOT EXISTS bruckm_fileblock (
id INT not null AUTO_INCREMENT,
file VARCHAR(100) not null,
displayName VARCHAR(100) not null,
PRIMARY KEY(id)
)");
dbQuery($query);
}
/** @see BuildingBlock::load()
*/
//-------------------------------------------------
function load(){
//-------------------------------------------------
$query = sprintf("SELECT * FROM bruckm_fileblock WHERE id = %d", $this->id);
$result = dbQuery($query);
$line = mysqli_fetch_array($result, MYSQLI_ASSOC);
$this->file = $line['file'];
$this->displayName = $line['displayName'];
}
/** @see BuildingBlock::doSave()
*/
//-----------------------------------------------
function doSave(){
//-----------------------------------------------
$query = sprintf("UPDATE bruckm_fileblock SET file = %s, displayName = %s WHERE id = %d",
sqlstring($this->file),
sqlstring($this->displayName),
$this->id);
dbQuery($query);
}
/** @see BuildingBlock::doCreate()
*/
//-----------------------------------------------
function doCreate(){
//-----------------------------------------------
$query = sprintf("INSERT INTO bruckm_fileblock (file, displayName) VALUES (%s, %s)",
sqlstring($this->file),
sqlstring($this->displayName));
dbQuery($query);
return mysql_insert_id();
}
/** @see BuildingBlock::delete()
*/
//-------------------------------------------------
function delete(){
//-------------------------------------------------
$query = sprintf("DELETE FROM bruckm_fileblock WHERE id = %d", $this->id);
dbQuery($query);
@unlink($this->fileDir.$this->file);
}
/** @see BuildingBlock::update()
*/
//-----------------------------------------------
function update(){
//-----------------------------------------------
if(isset($_FILES["fileblock_file".$this->id]) && $_FILES["fileblock_file".$this->id]['error'] == UPLOAD_ERR_OK){
$this->file = $this->findName($this->fileDir, $_FILES["fileblock_file".$this->id]['name']);
move_uploaded_file($_FILES["fileblock_file".$this->id]["tmp_name"], $this->fileDir.$this->file);
}
$this->displayName = trim($_POST["fileblock_name".$this->id]);
if(empty($this->displayName)){
$this->displayName = $this->file;
}
}
/** @see BuildingBlock::printContent()
*/
//-----------------------------------------------
function printContent($position){
//-----------------------------------------------
$t = new Template(CMS_TEMPLATE_DIR."fileblock.html");
$t->setVar("ID", $this->id);
$t->setVar("POSITION", $position);
$t->setVar("DISPLAYNAME", htmlspecialchars($this->displayName));
if(!empty($this->file)){
$t->setVar("PATH", $this->fileDir.$this->file);
$t->removeBlock("UPLOAD");
}
else{
$t->removeBlock("FILE");
}
return $t->toString();
}
/** @see BuildingBlock::publish()
*/
//----------------------------------------------
function publish(){
//----------------------------------------------
if(!file_exists($this->fileDir.$this->file)){
logError(3, "File ".$this->file." doesn't exist", __FILE__, __LINE__);
return "";
}
if($this->absoluteUrl){
$f = '<div><a href="http://www.bruckmuehle.at/newsletter/' . $this->file . '" target="_blank">';
$f .= '<img src="http://bruckmuehle.at/bg/file1.png" alt="Datei" /> ' . $this->displayName . '</a></div>';
return $f;
}
else {
$f = new Template(TEMPLATE_DIR."subparts/file.html");
$f->setVar("ID", $this->id);
$f->setVar("PATH", $this->fileDir.$this->file);
$f->setVar("DISPLAY_NAME", htmlspecialchars($this->displayName));
return $f->toString();
}
}
/** @see BuildingBlock::publishAsPlainText()
*/
//----------------------------------------------
function publishAsPlainText(){
//----------------------------------------------
return "[Dateilink] http://www.bruckmuehle.at/newsletter/" . $this->file . "\r\n\r\n";
}
/** @see BuildingBlock::publishForNewsletter()
*/
//----------------------------------------------
function publishForNewsletter(){
//----------------------------------------------
$p = new Template(NEWSLETTER_DIR . "subparts/file.html");
$p->setVar("SRC", $this->file);
$p->setVar("ALT", htmlspecialchars($this->displayName));
return $p->toString();
}
// === ADDITIONAL METHODS ========================================================== //
/** finds a path that does not exist
* @param dir target directory
* @param name name of the source image
* @return filename in the target directory that does not exist
*/
//-----------------------------------------------
function findName($dir, $name){
//-----------------------------------------------
$name = strtolower($name);
$name = str_replace("ä", "ae", $name);
$name = str_replace("ö", "oe", $name);
$name = str_replace("ü", "ue", $name);
$name = str_replace("ß", "sz", $name);
$name = ereg_replace("[^A-Za-z0-9\.-_]", "", $name);
if(!file_exists($dir.$name)){
return $name;
}
//extract file extension
$dot = strrpos($name, ".");
$ext = substr($name, $dot+1);
$name = substr($name, 0, $dot);
//add 3-digit number
$count = 1;
while(file_exists(sprintf("%s%s%03d.%s", $dir, $name, $count, $ext))){
$count++;
}
return sprintf("%s%03d.%s", $name, $count, $ext);
}
/** sets flag for switching between relative paths and absolute url
* @param useAbsoluteUrl true to use absolute url
* false to use relative paths
*/
//---------------------------------------------
function useAbsoluteUrl($useAbsoluteUrl){
//---------------------------------------------
$this->absoluteUrl = $useAbsoluteUrl;
}
/** sets the output directory
*/
//-----------------------------------------------
function setDirectory($fileDir){
//-----------------------------------------------
$this->fileDir = $fileDir;
}
/** @see BuildingBlock::publishForPdf()
*/
//----------------------------------------------
function publishForPdf($pdf){
//----------------------------------------------
$url = "http://www.kulturhaus-bruckmuehle.at/files/" . $this->file;
$pdf->SetFont("Arial", "I", 12);
$pdf->SetTextColor(0, 0, 127);
$pdf->Write(5, "» " . $this->displayName, $url);
$pdf->Ln();
$pdf->Ln();
}
};
?>