<?php
/*

#-------------------------------------------------------------------------
# Module: Play - a module to enable multimedia inclusion in your pages
# Version: 0.1
#
# 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 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
#
#-------------------------------------------------------------------------

*/

//error_reporting(E_ALL);

class Play extends CMSModule
{

	function GetName() { return 'Play'; }
	function GetFriendlyName() {	return $this->Lang('friendlyname');}
	function GetVersion() { return '0.6.0';}
	function GetHelp() { return $this->Lang('help');}
	function GetAuthor() {return 'Dam';}
	function GetAuthorEmail() {return 'trigau@free.fr';}
	function GetChangeLog() {return $this->Lang('changelog');}
	function IsPluginModule() {return true;}
	function HasAdmin() {return true;}
	function GetAdminSection() { 	return 'content';}
	function GetAdminDescription() {return $this->Lang('admindescription');}
	function VisibleToAdminUser() { return $this->CheckPermission('Use Play'); }
	function MinimumCMSVersion() {return "1.2";}


	function SuppressAdminOutput(&$request)
	{
		global $action;
		if ($action=="playfile" || $action=="browsefiles" || $action=="browse" || $action=="grabpicture" || $action=="convertfile" || $action=="upload") return true;
		return false;
	}



	function ShowPlayerTemplate($id, $returnid, $template, $playerlocation, $recordids, $width, $height, $params = array())
	{

		global $CMS_ADMIN_PAGE;
		global $gCms;
		include_once dirname(__FILE__).'/class.record.php';

		$location = $playerlocation;
		if (!startswith($location, "modules/Play/"))
			$location = cms_join_path($gCms->config['uploads_url'],$location);
		elseif (isset($CMS_ADMIN_PAGE))
			$location = cms_join_path("..",$location);

		$recordids = explode(",", $recordids);
		$links = array();
		foreach ($recordids as $recordid)
		{
			$record = new pRecord();
			if (!$record->loaddb($recordid)) continue;
			$check = $record->checkfile();
			if ($check == "remote")
				$fileurl = $record->location;
			elseif ($check == "local")
				$fileurl = cms_join_path($gCms->config['uploads_url'],$record->location);
			$link = $fileurl;
			//$link = $this->createfrontendlink($id,$returnid,'playfile','',array('recordid'=>$recordid),'',true);
			$search = array("&amp;","&",",","=","?");
			$replace = array("%26","%26","%2C","%3D","%3F");
			$link = str_replace($search,$replace, $link);

			$links[$record->name] = $link;
		}

		$this->smarty->assign('location',$location);
		$this->smarty->assign('width',$width);
		$this->smarty->assign('height',$height);
		$this->smarty->assign('player_params',$params);
		$this->smarty->assign_by_ref('files',$links);
		if (count($links) > 0 )
		{
			$link = array_values($links);
			$link = $link[0];
			$this->smarty->assign('file',$link);
		}
		return $this->ProcessTemplateFromData($template);
	}

	function checkFFmpeg()
	{
		$ffmpeg = $this->getpreference('ffmpeg','ffmpeg');
		$cmd = "$ffmpeg -version 2>&1";
 		$result = shell_exec($cmd);
 		return (strpos(strtolower($result), "ffmpeg version")!==false);
	}

	function getThumb($record,$id,$returnid)
	{
		global $gCms;
		$dir = dirname($record->location);
		$filename = basename($record->location);
		$filename = "thumb_".substr($filename,0, strrpos($filename, '.')).".jpg";
		$picturepath = cms_join_path($gCms->config['uploads_path'],$dir,$filename);
		$pictureurl = cms_join_path($gCms->config['uploads_url'],$dir,$filename);
		if (!file_exists($picturepath))
		{
			include_once dirname(__FILE__).'/class.ffmpeg.php';
			$filepath = cms_join_path($gCms->config['uploads_path'],$record->location);
			$ffmpeg = new pFFmpeg($this->getpreference('ffmpeg','ffmpeg'));
			$ffmpeg->grabpicture($filepath,$picturepath,true,"90x60");
		}
		return $pictureurl;
	}

}

?>
