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

471 lines
15 KiB
PHP

<?php
/** Video Block
*
* @version 1.0.0
* @since 2007-07-26
* @author martin lenzelbauer
*/
class VideoBlock extends BuildingBlock{
var $file; //file name
var $preview; //preview image
var $type; //video type ("real", "quicktime", "wmv", "unknown")
var $width;
var $height;
var $videoDir; //upload directory for videos
var $previewDir; //upload directory for preview images
var $maxWidth; //maximum width for the preview image
var $maxHeight; //maximum height for the preview image
/** C'tor
*/
//------------------------------------------------
function VideoBlock($id=0){
//------------------------------------------------
parent::BuildingBlock($id);
$this->file = "";
$this->preview = "";
$this->type = "unknown";
$this->width = 320;
$this->height = 240;
$this->maxWidth = 500;
$this->maxHeight = 500;
$this->setUploadDirectories(VIDEO_DIR, IMG_DIR);
}
/** @see BuildingBlock::install()
*/
//-----------------------------------------------
function install(){
//-----------------------------------------------
$query = sprintf("CREATE TABLE IF NOT EXISTS bruckm_videoblock (
id INT not null AUTO_INCREMENT,
file VARCHAR(128) not null,
preview VARCHAR(128) not null,
type ENUM('real','quicktime','windowsmedia','flash','unknown'),
width INT not null,
height INT not null,
PRIMARY KEY(id)
)");
$result = dbQuery($query);
}
/** @see BuildingBlock::load()
*/
//-------------------------------------------------
function load(){
//-------------------------------------------------
$query = sprintf("SELECT * FROM bruckm_videoblock WHERE id = %d", $this->id);
$result = dbQuery($query);
$line = mysqli_fetch_array($result, MYSQLI_ASSOC);
$this->file = $line['file'];
$this->preview = $line['preview'];
$this->type = $line['type'];
$this->width = $line['width'];
$this->height = $line['height'];
}
/** @see BuildingBlock::doSave()
*/
//-----------------------------------------------
function doSave(){
//-----------------------------------------------
$query = sprintf("UPDATE bruckm_videoblock SET file = %s, preview = %s, type = %s, width = %d, height = %d WHERE id = %d",
sqlstring($this->file),
sqlstring($this->preview),
sqlstring($this->type),
sqlnum($this->width),
sqlnum($this->height),
$this->id);
dbQuery($query);
}
/** @see BuildingBlock::doCreate()
*/
//-----------------------------------------------
function doCreate(){
//-----------------------------------------------
$query = sprintf("INSERT INTO bruckm_videoblock (file, type, width, height) VALUES (%s, %s, %d, %d)",
sqlstring($this->file),
sqlstring($this->type),
sqlnum($this->width),
sqlnum($this->height));
dbQuery($query);
return mysql_insert_id();
}
/** @see BuildingBlock::delete()
*/
//-------------------------------------------------
function delete(){
//-------------------------------------------------
$query = sprintf("DELETE FROM bruckm_videoblock WHERE id = %d", $this->id);
dbQuery($query);
@unlink($this->videoDir.$this->file);
@unlink($this->previewDir.$this->preview);
}
/** @see BuildingBlock::update()
*/
//-----------------------------------------------
function update(){
//-----------------------------------------------
$this->width = $_POST["videoblock_width".$this->id];
$this->height = $_POST["videoblock_height".$this->id];
if(isset($_FILES["videoblock_file".$this->id]) && $_FILES["videoblock_file".$this->id]['error'] == UPLOAD_ERR_OK){
$this->file = $this->findName($this->videoDir, $_FILES["videoblock_file".$this->id]['name']);
$this->uploadVideo($_FILES["videoblock_file".$this->id]);
}
if(isset($_FILES["videoblock_preview".$this->id]) && $_FILES["videoblock_preview".$this->id]['error'] == UPLOAD_ERR_OK){
$this->preview = $this->findName($this->previewDir, $_FILES["videoblock_preview".$this->id]['name']);
$this->uploadPreview($_FILES["videoblock_preview".$this->id]);
}
}
/** @see BuildingBlock::printContent()
*/
//-----------------------------------------------
function printContent($position){
//-----------------------------------------------
$t = new Template(CMS_TEMPLATE_DIR."videoblock.html");
$t->setVar("ID", $this->id);
$t->setVar("POSITION", $position);
$t->setVar("WIDTH", $this->width);
$t->setVar("HEIGHT", $this->height);
if(!empty($this->file) && file_exists($this->videoDir.$this->file)){
if ($this->type == "real") {
$t->setVar("VIDEO", '<a href="../video/show.php?id=' . $this->id . '" target="_blank">Video ansehen</a>');
$t->removeBlock("UPLOADVIDEO");
}
else if ($this->type == "quicktime") {
$t->setVar("VIDEO", '<a href="../video/show.php?id=' . $this->id . '" target="_blank">Video ansehen</a>');
$t->removeBlock("UPLOADVIDEO");
}
else if ($this->type == "windowsmedia") {
$t->setVar("VIDEO", '<a href="../video/show.php?id=' . $this->id . '" target="_blank">Video ansehen</a>');
$t->removeBlock("UPLOADVIDEO");
}
else if ($this->type == "flash") {
$t->setVar("VIDEO", '<a href="../video/show.php?id=' . $this->id . '" target="_blank">Video ansehen</a>');
$t->removeBlock("UPLOADVIDEO");
}
else {
$t->setVar("VIDEO", '<span style="color:#ff0000">Unbekannter Dateityp</span>');
}
}
else{
$t->removeBlock("VIDEO");
}
if(!empty($this->preview) && file_exists($this->previewDir.$this->preview)){
$t->setVar("PREVIEW", $this->previewDir . $this->preview);
$t->removeBlock("UPLOADPREVIEW");
}
else{
$t->removeBlock("PREVIEW");
}
return $t->toString();
}
/** @see BuildingBlock::publish()
*/
//----------------------------------------------
function publish(){
//----------------------------------------------
if(!file_exists($this->videoDir.$this->file)){
logError(3, "Video ".$this->videoDir.$this->file." doesn't exist", __FILE__, __LINE__);
return "";
}
$out = '<div><a href="video/show.php?id=' . $this->id . '" target="_blank">';
if(!empty($this->preview)){
$out .= '<img src="images/' . $this->preview . '" alt="Video &ouml;ffnen" />';
}
else{
$out .= 'Video: ' . $this->file;
}
$out .= '</a></div>';
return $out;
}
// === ADDITIONAL METHODS ======================================================== //
/** shows the video in a separate window
*/
//------------------------------------------------
function show(){
//------------------------------------------------
$t = new Template("video.html");
if ($this->type == "real") {
$t->setVar("VIDEO", $this->printReal($this->file));
}
else if ($this->type == "quicktime") {
$t->setVar("VIDEO", $this->printQuicktime($this->file));
}
else if ($this->type == "windowsmedia") {
$t->setVar("VIDEO", $this->printWindowsMedia($this->file));
}
else if ($this->type == "flash") {
$t->setVar("VIDEO", $this->printFlash($this->file));
}
else {
$t->setVar("VIDEO", "Video nicht gefunden");
}
$t->parse();
}
/** prints the source code for embedding a real video
* @param path path
*/
//------------------------------------------------
function printReal($path) {
//------------------------------------------------
$width = $this->width;
$height = $this->height;
$v .= '<object id="video' . $this->id . '" classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="' . $width . '" height="' . $height . '">';
$v .= '<param name="controls" value="videoWindow' . $this->id . '">';
$v .= '<param console="controls" value="videoConsole' . $this->id . '">';
$v .= '<param name="src" value="' . $path . '">';
$v .= '<param name="autostart" value="true">';
$v .= '<embed type="video/x-pn-realvideo-plugin" src="' . $path . '" controls="ImageWindow,ControlPanel" console="videoConsole"' . $this->id . '" ';
$v .= 'width="' . $width . '" height="' . $height . '" />';
$v .= '</object>';
return $v;
}
/** prints the source code for embedding a quicktime video
* @param path path
*/
//------------------------------------------------
function printQuicktime($path) {
//------------------------------------------------
$width = $this->width;
$height = $this->height + 16;
$v = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="' . $width . '" height="' . $height . '">';
$v .= '<param name="src" value="' . $path . '">';
$v .= '<param name="controller" value="true">';
$v .= '<param name="autoplay" value="false">';
$v .= '<embed type="video/quicktime" src="' . $path . '" pluginspage="http://www.apple.com/quicktime/download/" width="' . $width . '" height="' . $height . '" ';
$v .= 'autoplay="false" controller="true" />';
$v .= '</object>';
return $v;
}
/** prints the source code for embedding a windows media video
* @param path path
*/
//------------------------------------------------
function printWindowsMedia($path) {
//------------------------------------------------
$width = $this->width;
$height = $this->height + 46;
$v = '<object classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" type="application/x-oleobject" width="' . $width . '" height="' . $height . '">';
$v .= '<param name="url" value="' . $path . '">';
$v .= '<param name="autostart" value="false">';
$v .= '<param name="ShowControls" value="true">';
$v .= '<param name="ShowStatusBar" value="false">';
$v .= '<param name="ShowDisplay" value="false">';
$v .= '<embed type="video/x-ms-wmv" src="' . $path . '" width="' . $width . '" height="' . $height . '" autostart="0" ';
$v .= 'ShowControls="1" ShowStatusBar="0" ShowDisplay="0" />';
$v .= '</object>';
return $v;
}
/** prints the source code for embedding a flash video
* @param path path
*/
//------------------------------------------------
function printFlash($path) {
//------------------------------------------------
$width = $this->width;
$height = $this->height;
$v = '<object type="application/x-shockwave-flash" data="flvplayer.swf" ';
$v .= 'width="' . $this->width . '" height="' . $this->height . '" id="flv' . $this->id . '" align="top">';
$v .= '<param name="movie" value="flvplayer.swf" />';
$v .= '<param name="flashvars" value="flv=' . $path .'" />';
$v .= '<param name="quality" value="high" />';
$v .= '<param name="bgcolor" value="#ffffff" />';
$v .= '<param name="scale" value="noscale" />';
$v .= '<param name="quality" value="high" />';
$v .= '<p><a href="http://www.adobe.com/go/EN_US-H-GET-FLASH">Download Flash Player</a></p>';
$v .= '</object>';
return $v;
}
/** uploads a video file
* @param file $_FILES file data
*/
//------------------------------------------------
function uploadVideo($file){
//------------------------------------------------
$path = $this->videoDir . $this->file;
if(file_exists($path)){
@unlink($path);
}
move_uploaded_file($file['tmp_name'], $path);
@chmod($path, 0777);
$type = substr($path, strrpos($path, ".") + 1);
if ($type == "mov" || $type == "qt") {
$this->type = "quicktime";
}
else if ($type == "rm" || $type == "rmv" || $type == "rmvb") {
$this->type = "real";
}
else if ($type == "wmv") {
$this->type = "windowsmedia";
}
else if ($type == "flv") {
$this->type = "flash";
}
else {
$this->type = "unknown";
}
}
/** uploads the preview image
* @param file $_FILES file data
*/
//------------------------------------------------
function uploadPreview($file){
//------------------------------------------------
$path = $this->previewDir . $this->preview;
if(file_exists($path)){
@unlink($path);
}
//resize image
$imgSize = getImageSize($file['tmp_name']);
if($imgSize[0] > $this->width || $imgSize[1] > $this->height){
switch($imgSize[2]){
case 1: //gif
$original = imageCreateFromGif($file['tmp_name']);
break;
case 2: //jpg
$original = imageCreateFromJpeg($file['tmp_name']);
break;
case 3: //png
$original = imageCreateFromPng($file['tmp_name']);
break;
}
$origWidth = $imgSize[0];
$origHeight = $imgSize[1];
$width = $origWidth;
$height = $origHeight;
if($width > $this->maxWidth){
$fact = $this->maxWidth / $origWidth;
$width *= $fact;
$height *= $fact;
}
if($height > $this->maxHeight){
$fact = $this->maxHeight / $height;
$width *= $fact;
$height *= $fact;
}
$small = imageCreateTrueColor($width,$height);
imageCopyResampled($small, $original, 0, 0, 0, 0, $width, $height, $origWidth, $origHeight);
switch($imgSize[2]){
case 1:
if(!@imageGif($small, $path)){
logError(4, "Could not create gif image: $path", __FILE__, __LINE__);
return "";
}
break;
case 2:
if(!@imageJpeg($small, $path, 100)){
logError(4, "Could not create jpg image: $path", __FILE__, __LINE__);
return "";
}
break;
case 3:
if(!@imagePng($small, $path)){
logError(4, "Could not create png image: $path", __FILE__, __LINE__);
return "";
}
break;
}
imageDestroy($small);
imageDestroy($original);
@chmod($path, 0777);
}
else{
move_uploaded_file($file['tmp_name'], $path);
@chmod($path, 0777);
}
}
/** sets the output directories
*/
//-----------------------------------------------
function setUploadDirectories($video, $preview){
//-----------------------------------------------
$this->videoDir = $video;
$this->previewDir = $preview;
}
/** 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);
}
/** returns video description as xml
* @return xml string
*/
//---------------------------------------------------
function toXml(){
//---------------------------------------------------
$xml = sprintf("<video file=\"%s\" />", $this->file);
return $xml;
}
};
?>