<?
class VideoGallery {
private $video_path = '';
private $video_url = '';
private $thumb_sizes = array();
const err_no_title = 'ERR_NO_TITLE';
const err_no_video_file = 'ERR_NO_VIDEO_FILE';
const err_no_thumb = 'ERR_NO_THUMB';
const err_thumb_not_image = 'ERR_THUMB_NOT_IMAGE';
const err_loading_thumb = 'ERR_LOADING_THUMB';
const err_loading_video = 'ERR_LOADING_VIDEO_FILE';
const err_incorrect_video_file = 'ERR_INCORRECT_VIDEO_FILE';
private $lastError = 0;
function __construct() {
$this->video_path = WEBPATH.'media/';
$this->video_url = WEBURL.'media/';
$this->thumb_sizes = array(
1 => array('hash'=>'', 'size'=>''),
2 => array('hash'=>'small_', 'size'=>'120x80')
);
}
function deleteVideo($id) {
$data = $this->getVideo($id);
if (!empty($data)) {
foreach ($this->thumb_sizes as $index=>$thumb) {
unlink ($data['picture'.$index.'_path']);
}
unlink ($data['video_file_path']);
$DB = Database::getInstance();
$DB->Exec("DELETE FROM videos WHERE id = ".$id);
return true;
}
return false;
}
function editVideo ($id, $title, $descr) {
$title = mb_substr($title, 0, 30);
if (!$title) {
$this->lastError = self::err_no_title;
return false;
}
$DB = Database::getInstance();
$DB->Exec("UPDATE videos SET title = '".$DB->Escape($title)."', description='".$DB->Escape($descr)."' WHERE id = ".$id);
return true;
}
function uploadVideo($title, $file, $thumb, $descr) {
require_once ADMPATH.'includes/plugins/getid3/getid3.php';
$return = true;
$title = mb_substr($title, 0, 30);
if (!is_array($_FILES[$thumb]) || empty($_FILES[$thumb]['name'])) {
$this->lastError = self::err_no_thumb;
return false;
}
if (!is_array($_FILES[$file]) || empty($_FILES[$file]['name'])) {
$this->lastError = self::err_no_video_file;
return false;
}
if (!$title) {
$this->lastError = self::err_no_title;
return false;
}
$hash = Text::getRandomHash();
$video_file = $hash.'.flv';
$thumb_file = 'tmp_'.$hash.'.jpg';
$video_upload = Filesystem::uploadFile ($file, $this->video_path, $video_file);
$thumb_upload = Filesystem::uploadFile ($thumb, $this->video_path, $thumb_file);
if (!$video_upload) {
$this->lastError = self::err_loading_video;
if ($thumb_upload) {
unlink ($this->video_path.$thumb_file);
}
return false;
} else {
chmod($this->video_path.$video_file,0777);
$id3 = new getID3;
$data = $id3->analyze($this->video_path.$video_file);
if (isset($data['video']) && $data['fileformat'] == 'flv') {
$video_data = $data['video'];
$video_w = $video_data['resolution_x'];
$video_h = $video_data['resolution_y'];
} else {
if ($thumb_upload) {
unlink ($this->video_path.$thumb_file);
}
unlink ($this->video_path.$video_file);
$this->lastError = self::err_incorrect_video_file;
return false;
}
}
if (!$thumb_upload) {
$this->lastError = self::err_loading_thumb;
if ($video_upload) {
unlink ($this->video_path.$video_file);
}
return false;
}
if (getimagesize($this->video_path.$thumb_file)) {
foreach ($this->thumb_sizes as $index => $thumb_data) {
$dst = str_replace('tmp_','',$thumb_file);
if ($thumb_data['size']) {
list ($w, $h) = explode('x',$thumb_data['size']);
} else {
$w = $video_w;
$h = $video_h;
}
$dst_file = $this->video_path.$thumb_data['hash'].$dst;
Image:: imageResize ($this->video_path.$thumb_file,$dst_file,$w,$h,true,100);
chmod($dst_file,0777);
}
unlink ($this->video_path.$thumb_file);
} else {
unlink ($this->video_path.$thumb_file);
unlink ($this->video_path.$video_file);
$this->lastError = self::err_thumb_not_image;
return false;
}
$DB = Database::getInstance();
$DB->Exec("INSERT INTO videos (title, hash,w, h, description) VALUES ('".$DB->Escape($title)."','".$hash."',".$video_w.",".$video_h.",'".$DB->Escape($descr)."')");
return true;
}
function getThumbnail($hash, $index) {
return $this->video_url.$this->thumb_sizes[$index]['hash'].$hash.'.jpg';
}
function getVideoList ($pg=0, $rpp=12) {
$result = array();
$DB = Database::getInstance();
$get = $DB->Exec("SELECT * FROM videos");
$rows = $DB->Count($get);
if ($rows) {
for ($i=0;$i<$rows;$i++) {
$item = $DB->Fetch($get);
$result[] = $this->populateVideoData($item);
}
}
return $result;
}
function getLastError() {
return $this->lastError;
}
private function populateVideoData ($item) {
foreach ($this->thumb_sizes as $index => $data){
$item['picture'.$index] = $this->getThumbnail($item['hash'],$index);
$item['picture'.$index.'_path'] = $this->video_path.$data['hash'].$item['hash'].'.jpg';
}
$item['video_file'] = $this->video_url.$item['hash'].'.flv';
$item['video_file_path'] = $this->video_path.$item['hash'].'.flv';
return $item;
}
function getVideo ($id) {
$DB = Database::getInstance();
$video = $DB->GetRow ('videos',$id);
if (!empty($video)) {
return $this->populateVideoData($video);
} else {
return array();
}
}
function __destruct() {
}
}
?>