<?php

/*
Objects :
	- browse files in CMSMS uploads directory and display them.
	- for each file corresponding to the good file type, provide a way to send its location to opener window
*/


// 1- load input parameters
if (!isset($gCms)) exit;
if (! $this->CheckPermission('Use Play'))
	return $this->DisplayErrorPage($id, $params, $returnid,$this->Lang('accessdenied'));

$uploadsdir = $gCms->config['uploads_path'];
$currentsubdir = (isset($params['curdir']) ? $params['curdir'] : "");
$currentpath = DIRECTORY_SEPARATOR.join_path($uploadsdir, $currentsubdir);

if (!is_dir($currentpath)) {
	$currentsubdir = "";
	$currentpath = join_path($uploadsdir, $currentsubdir);
}

$browsefiles = (isset($params['browse']) ? $params['browse'] : 'any');
$module_url = $gCms->config['root_url'].'/modules/Play';

// 2- load path to current dir
$recursivepath = array();
$explodedpath = explode(DIRECTORY_SEPARATOR,$currentsubdir);
$tmpcurrent = "";
foreach ($explodedpath as $dir) {
	if ($dir=="") continue;
	$tmpcurrent = join_path($tmpcurrent,$dir);
	$tmp = new stdclass();
	$tmp->name = $dir;
	$tmp->href = $this->createlink($id,'browse',$returnid, $dir,array('curdir'=>$tmpcurrent,'browse'=>$browsefiles),'',true);
	$recursivepath[] = $tmp;
}
	$tmp = new stdclass();
	$tmp->name = $this->lang('root');
	$tmp->href = $this->createlink($id,'browse',$returnid, $dir,array('curdir'=>'','browse'=>$browsefiles),'',true);
	array_unshift($recursivepath,$tmp);


// 3- load dirs and files contained in current dir
$tmpdirs = array();
$tmpfiles = array();
$dirs = array();
$files = array();
browse($currentpath, $tmpdirs, $tmpfiles);
if ($currentsubdir!="")
	array_unshift($tmpdirs,"..");

foreach ($tmpdirs as $dir) {
	$tmp = new stdclass();
	$tmp->name = $dir;
	$tmp->href = $this->createlink($id,'browse',$returnid, $dir,array('curdir'=>join_path($currentsubdir,$dir),'browse'=>$browsefiles),"",true);
	if ($dir=="..")
		$tmp->imgsrc = $module_url.'/images/upfolder.gif';
	else
		$tmp->imgsrc = $module_url.'/images/folder.gif';
	$dirs[] = $tmp;
}

include_once(dirname(__FILE__).'/class.ffmpeg.php');
foreach ($tmpfiles as $file) {
	$filepath = join_path($uploadsdir,$currentsubdir,$file);
	$MMfile = new pMMfile(join_path($uploadsdir,$currentsubdir,$file));

	$tmp = new stdclass();
	$tmp->name = $file;

	$extension = substr($tmp->name,strrpos(".",$tmp->name)+1);

	if ($MMfile->isplayable())
		$tmp->filetype = "flash";	
	elseif ($MMfile->hasvideo())
		$tmp->filetype = "video";
	elseif ($MMfile->hasaudio())
		$tmp->filetype = "audio";
	else
		$tmp->filetype = "";

	switch ($tmp->filetype) {
		case "video" : $tmp->imgsrc = $module_url.'/images/video.gif'; break;
		case "audio" : $tmp->imgsrc = $module_url.'/images/audio.gif'; break;
		case "flash" : $tmp->imgsrc = $module_url.'/images/flash.gif'; break;
		case "" : $tmp->imgsrc = $module_url.'/images/anyfile.gif'; break;
	}

	$tmp->filesize = $MMfile->filesize();

	if 	(($browsefiles=="flash" && endswith(strtolower($file),".swf"))
		|| ($browsefiles=="flvmp3" && ($MMfile->fileformat()=="mp3" || $MMfile->fileformat()=="flv" || $MMfile->fileformat()==false))
		|| ($browsefiles=="any")) {
		$tmp->href = "#";
		$tmp->onclick = "sendfile('".join_path($currentsubdir,$file)."');";
	}

	$tmp->action = "";
	if ($browsefiles=="flvmp3" && ! $MMfile->isplayable()) {
		if ($MMfile->hasaudio()) {
			$img = '<img alt="'.$this->lang('converttomp3').'" title="'.$this->lang('converttomp3').'" src="'.$module_url.'/images/import_audio.png" />';
			$tmp->action .= $this->createlink($id,'convertfile',$returnid, $img,array('file'=>$file,'curdir'=>$currentsubdir, 'browse'=>$browsefiles,'format'=>'mp3'));
		}
		if ($MMfile->hasvideo()) {
			$img = '<img alt="'.$this->lang('converttoflv').'" title="'.$this->lang('converttoflv').'" src="'.$module_url.'/images/import_video.png" />';
			$tmp->action .= $this->createlink($id,'convertfile',$returnid, $img,array('file'=>$file,'curdir'=>$currentsubdir, 'browse'=>$browsefiles,'format'=>'flv'));
		}
		
	}

	$files[] = $tmp;
}

$this->smarty->assign('titletext',$this->lang('browser'));
$this->smarty->assign('id',$id);
$this->smarty->assign('nametext',$this->lang('name'));
$this->smarty->assign('filesizetext',$this->lang('filesize'));
$this->smarty->assign_by_ref('recursivepath',$recursivepath);
$this->smarty->assign_by_ref('dirs',$dirs);
$this->smarty->assign_by_ref('files',$files);

if (isset($params['error']))
	$this->smarty->assign('error','<div id="error">'.$params['error'].'</div>');

$this->smarty->assign('formstart',$this->CreateFormStart($id, 'upload', $returnid, 'post', 'multipart/form-data', false, '', array('curdir'=>$currentsubdir,'browse'=>$browsefiles)));
$this->smarty->assign('formend',$this->CreateFormEnd());
$this->smarty->assign('uploadinput',$this->CreateInputFile($id, 'upload','',35));
$this->smarty->assign('submit',$this->CreateInputSubmit($id, 'submit', $this->lang('upload')));

$this->smarty->assign('allowconvert',false);//$this->checkffmpeg());
$this->smarty->assign('convertcheckbox',$this->CreateInputCheckbox($id, 'convert','yes'));
$this->smarty->assign('convertlabel',$this->lang('convertuploadedfile'));
$this->smarty->assign('cancel',$this->CreateInputSubmit($id, 'back', $this->lang('cancel'),'onclick="window.close();return false;"'));

echo $this->processtemplate('browse.tpl');




/*********************************************************************************************/
//  Miscellaneous usefull functions

function browse($dir, &$outputdirs, &$outputfiles) {
	$outputdirs = array();
	$outputfiles = array();
	if (!is_dir($dir)) return false;
	if (! $dh = opendir($dir)) return false;
	while (($file = readdir($dh)) !== false) {
		$filepath = join_path($dir,$file);
		if (is_dir($filepath) && $file!="." && $file!="..")
			$outputdirs[] = $file;
		elseif (is_file($filepath) && !startswith($file,".") && !startswith($file,"thumb_"))
			$outputfiles[] = $file;
	}
	closedir($dh);
	sort($outputdirs);
	sort($outputfiles);
	return true;
}

function join_path()
{
	$args = func_get_args();
	$input = explode(DIRECTORY_SEPARATOR,implode(DIRECTORY_SEPARATOR,$args));
	$output = array();
	foreach ($input as $element) {
		switch ($element) {
			case "." : case "": break;
			case ".." : @array_pop($output); break;
			default : $output[] = $element;
		}
	}
	$output = implode(DIRECTORY_SEPARATOR,$output);
	if (count($args)>0 && startswith($args[0],DIRECTORY_SEPARATOR))
		$output = DIRECTORY_SEPARATOR.$output;
	return $output;
}


?>