<?php
/**
* @version 1.0.0
* @package RSFiles! 1.0.0
* @copyright (C) 2009 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.controller');
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.path');
jimport('joomla.mail.helper');

class rsfilesControllerFiles extends rsfilesController
{
	function __construct()
	{
		parent::__construct();
	}
	
	
	function upload()
	{
		global $mainframe;
		$db = JFactory::getDBO();
		$user = JFactory::getUser();
		$files = JRequest::get('files');
		$post = JRequest::get('post');
		$rsfilesConfig = RsfilesHelper::getConfig();
		$path = $post['folder'];
		$path1 = empty($path) ? '' : DS.$path;
		$fullpath = $rsfilesConfig['download_folder'].$path1;
		$files = $files['files'];
		$Itemid = JRequest::getVar('Itemid','','request');
		$itemid = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
		$model = $this->getModel('files');
		$row =& JTable::getInstance('Rsfiles_Files','Table');
		
		//check to see if the upload is enabled
		if($rsfilesConfig['enable_upload'] == 0) $mainframe->redirect(JRoute::_('index.php?option=com_rsfiles&view=files'.$itemid,false),JText::_('RSFL_UPLOAD_DENIED'));
		//check to see if the user has permission to upload
		if (!$model->getPermissionMaintenance()) $mainframe->redirect(JRoute::_('index.php?option=com_rsfiles&view=files'.$itemid,false),JText::_('RSFL_UPLOAD_DENIED'));
		
		$rsfilesConfig['allowed_files'] = str_replace("\r",'',$rsfilesConfig['allowed_files']);
		$allowed_extensions = explode("\n",$rsfilesConfig['allowed_files']);
		$j = 0;
		
		if($user->id != 0)
		{
			$db->setQuery("SELECT CanPerforMaintenance,CanDownload,CanView FROM #__rsfiles_files WHERE FilePath = '".$db->getEscaped(urldecode($fullpath))."' ");
			$permissions = $db->loadObject();
		} else 
		{
			$permissions = new stdClass();
			$permissions->CanPerforMaintenance = '';
			$permissions->CanDownload = '';
			$permissions->CanView = '';
		}
		
		$filepaths = array();
		
		foreach($files['tmp_name'] as $i=>$tmp)
		{
			if(!in_array(JFile::getExt($files['name'][$i]),$allowed_extensions)) continue;
			if($files['size'][$i] > ($rsfilesConfig['max_upl_size']*1024)) continue;
			JFile::upload($tmp, urldecode($fullpath.DS.JFile::getName($files['name'][$i])));
			$row->IdFile = null;
			$row->FilePath = urldecode($fullpath.DS.JFile::getName($files['name'][$i]));
			$row->DateAdded = time();
			$row->ModifiedDate = time();
			$row->DownloadMethod = 0;
			$row->CanDownload = $permissions->CanDownload; 	 
			$row->CanPerforMaintenance = $permissions->CanPerforMaintenance; 	 
			$row->CanView = $permissions->CanView;
			$row->IdUser = $user->id;
			if(is_file($fullpath.DS.JFile::getName($files['name'][$i])))
				$row->hash = md5_file($fullpath.DS.JFile::getName($files['name'][$i]));
			$row->hits = 0;
			$row->published = 1;
			$row->store();
			$j++;
			$filepaths[] = '<a href="'.JURI::root().'index.php?option=com_rsfiles&view=files&layout=download&path='.urldecode($path.DS.JFile::getName($files['name'][$i])).'">'.JURI::root().'index.php?option=com_rsfiles&view=files&layout=download&path='.urldecode($path.DS.JFile::getName($files['name'][$i])).'</a>';
		}
		
		
		if($rsfilesConfig['email_upload_enable'] == 1)
		{
			$to = $rsfilesConfig['email_upload_to'];
			$from = $rsfilesConfig['email_from'];
			$fromname = $rsfilesConfig['email_from_name'];
			$reply = $rsfilesConfig['email_reply'];
			$replyname = $rsfilesConfig['email_reply_name'];
			$subject = $rsfilesConfig['email_upload_subject'];
			$mode = $rsfilesConfig['email_upload_mode'];
			$cc = !empty($rsfilesConfig['email_cc']) ? $rsfilesConfig['email_cc'] : null;
			$bcc = !empty($rsfilesConfig['email_bcc']) ? $rsfilesConfig['email_bcc'] : null;
			$message = $rsfilesConfig['email_upload_message'];
			
			$filepaths = implode('<br/>',$filepaths);
			$bad = array('{username}','{files}');
			$good = array($user->name,$filepaths);
			$message = str_replace($bad,$good,$message);
			
			if(JMailHelper::isEmailAddress($to))
				JUtility::sendMail($from , $fromname , $to , $subject , $message , $mode , $cc , $bcc , null , $reply , $replyname);
		}
		
		$msg = JText::_('RSFL_UPLOAD_SUCCESS');
		if(count($files['tmp_name']) != $j)
			$msg = JText::_('RSFL_UPLOAD_SUCCESS_WITH_EXCEPTION');
		
		$redirect = empty($path) ? '' : '&folder='.$path;
		
		$this->setRedirect(JRoute::_('index.php?option=com_rsfiles&view=files'.$redirect.$itemid,false),$msg);
	}
	
	function newfolder()
	{
		global $mainframe;
		$db = JFactory::getDBO();
		$user = JFactory::getUser();
		$post = JRequest::get('post');
		$folder = $post['folder'];
		$folder1 = empty($folder) ? '' : DS.$folder;
		$dirname = $post['newfolder'];
		$rsfilesConfig = RsfilesHelper::getConfig();
		$fullpath = $rsfilesConfig['download_folder'].$folder1;
		$Itemid = JRequest::getVar('Itemid','','request');
		$itemid = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
		$model = $this->getModel('files');
		$row =& JTable::getInstance('Rsfiles_Files','Table');
		
		//check to see if the user has permission to create a new folder
		if (!$model->getPermissionMaintenance()) $mainframe->redirect(JRoute::_('index.php?option=com_rsfiles&view=files'.$itemid,false));
		
		if($user->id != 0)
		{
			$db->setQuery("SELECT CanPerforMaintenance FROM #__rsfiles_files WHERE FilePath = '".$db->getEscaped(urldecode($fullpath))."' ");
			$permission = $db->loadResult();
		} else $permission = '';
		
		if(strlen($dirname) > 1)
			$result = JFolder::create(urldecode($fullpath).DS.$dirname);
		if($result == true)
		{
			$row->IdFile = null;
			$row->FilePath = urldecode($fullpath.DS.$dirname);
			$row->DateAdded = time();
			$row->ModifiedDate = time();
			$row->DownloadMethod = 0;
			$row->CanDownload = 0; 	 
			$row->CanView = 0;
			$row->CanPerforMaintenance = $permission;
			$row->hits = 0;
			$row->published = 1;
			$row->store();
			$msg = JText::_('RSFL_FOLDER_CREATE_OK');
		}
		else
			$msg = JText::_('RSFL_FOLDER_CREATE_FAILED');
		$redirect = empty($folder) ? '' : '&folder='.$folder;
		$this->setRedirect(JRoute::_('index.php?option=com_rsfiles&view=files'.$redirect.$itemid,false),$msg);
	}
	
	function remove()
	{
		global $mainframe;
		$db = JFactory::getDBO();
		$rsfilesConfig = RsfilesHelper::getConfig();
		$model = $this->getModel('files');
		$get = JRequest::get('get');
		$folder = @$get['folder'];
		$fullpath = $rsfilesConfig['download_folder'].DS.$folder;
		$Itemid = JRequest::getVar('Itemid','','request');
		$itemid = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
		
		//do not erase the root
		if($fullpath == $rsfilesConfig['download_folder'].DS) return;
		//do not delete outside the root
		if (strpos(realpath($fullpath), $rsfilesConfig['download_folder']) !== 0) $mainframe->redirect(JRoute::_('index.php?option=com_rsfiles&view=files'.$itemid,false));
		
		$ext = end(explode(DS,$fullpath));
		if(JFile::stripExt($ext) == '') $mainframe->redirect(JRoute::_('index.php?option=com_rsfiles&view=files'.$itemid,false));
		
		//check to see if the user has permission to delete
		if (!$model->getPermissionMaintenance()) $mainframe->redirect(JRoute::_('index.php?option=com_rsfiles&view=files'.$itemid,false));
		
		if(is_dir($fullpath))
		{
			$subfolders = JFolder::folders($fullpath,'.',true,true);
			$subfiles = JFolder::files($fullpath,'.',true,true);
			$subs = array_merge($subfolders,$subfiles);
			foreach($subs as $sub)
			{
				$db->setQuery("DELETE FROM #__rsfiles_files WHERE FilePath = '".$db->getEscaped($sub)."'");
				$db->query();
			}
			JFolder::delete($fullpath);
			$msg = JText::_('RSFL_DELETE_FOLDER_SUCCESS');
		}
		
		if(is_file($fullpath))
		{
			JFile::delete($fullpath);
			$msg = JText::_('RSFL_DELETE_FILE_SUCCESS');
		}
		
		$db->setQuery("DELETE FROM #__rsfiles_files WHERE FilePath = '".$db->getEscaped($fullpath)."'");
		$db->query();
		
		$path = explode(DS,$folder);
		array_pop($path);
		$path = implode(DS,$path);
		
		$path = empty($path) ? '' : '&folder='.urlencode($path);
		
		$this->setRedirect(JRoute::_('index.php?option=com_rsfiles&view=files'.$path.$itemid,false),$msg);
	}	
	
	function removefile()
	{
		global $mainframe;
		$db = JFactory::getDBO();
		$rsfilesConfig = RsfilesHelper::getConfig();
		$model = $this->getModel('files');
		$path = JRequest::getVar('path','','request');
		$fullpath = $rsfilesConfig['download_folder'].DS.$path;
		$Itemid = JRequest::getVar('Itemid','','request');
		$itemid = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
		
		//do not delete outside the root
		if (strpos(realpath($fullpath), $rsfilesConfig['download_folder']) !== 0) $mainframe->redirect(JRoute::_('index.php?option=com_rsfiles&view=files'.$itemid,false));
		//check to see if the user has permission to delete
		if (!$model->getPermissionMaintenance()) $mainframe->redirect(JRoute::_('index.php?option=com_rsfiles&view=files'.$itemid,false));
		
		$ext = end(explode(DS,$fullpath));
		if(JFile::stripExt($ext) == '') $mainframe->redirect(JRoute::_('index.php?option=com_rsfiles&view=files'.$itemid,false));
		
		$result = JFile::delete($fullpath);
		
		if($result == true)
		{
			$db->setQuery("DELETE FROM #__rsfiles_files WHERE FilePath = '".$db->getEscaped($fullpath)."'");
			$db->query();
			$msg = JText::_('RSFL_DELETE_FILE_SUCCESS');
		} else $msg = JText::_('RSFL_DELETE_FILE_ERROR');
		
		$redirect = explode(DS,$path);
		if(count($redirect>0))
			array_pop($redirect);
		$redirect = implode(DS,$redirect);
		$redirect = empty($redirect) ? '' : '&folder='.urlencode($redirect);
		$this->setRedirect(JRoute::_('index.php?option=com_rsfiles&view=files'.$redirect.$itemid,false),$msg);
	}
	
	function emailsend()
	{
		global $mainframe;
		$db = JFactory::getDBO();
		$rsfilesConfig = RsfilesHelper::getConfig();
		$path = JRequest::getVar('path','','request');
		$fullpath = $rsfilesConfig['download_folder'].DS.$path;
		$Itemid = JRequest::getVar('Itemid','','request');
		$itemid = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
		$to = JRequest::getVar('rsfl_email','','post');
		
		$from = $rsfilesConfig['email_from'];
		$fromname = $rsfilesConfig['email_from_name'];
		$reply = $rsfilesConfig['email_reply'];
		$replyname = $rsfilesConfig['email_reply_name'];
		$subject = $rsfilesConfig['email_download_subject'];
		$mode = $rsfilesConfig['email_download_mode'];
		$cc = !empty($rsfilesConfig['email_cc']) ? $rsfilesConfig['email_cc'] : null;
		$bcc = !empty($rsfilesConfig['email_bcc']) ? $rsfilesConfig['email_bcc'] : null;
		$message = $rsfilesConfig['email_download_message'];
		
		$dpath = base64_encode($path);
		$downloadurl = '<a target="_blank" href="'.JURI::root().'index.php?option=com_rsfiles&task=download&path='.$dpath.'&email=1'.$itemid.'">link</a>';
		
		$bad = array('{email}','{downloadurl}');
		$good = array($to , $downloadurl);
		
		$message = str_replace($bad,$good,$message);
		
		if(JMailHelper::isEmailAddress($to))
		{
			JUtility::sendMail($from , $fromname , $to , $subject , $message , $mode , $cc , $bcc , null , $reply , $replyname);
			$msg = JText::_('RSFL_MAIL_SENT');
		} else $msg = JText::_('RSFL_MAIL_ERROR');
		
		
		$this->setRedirect(JRoute::_('index.php?option=com_rsfiles&view=files&layout=download&path='.urlencode($path).$itemid,false),$msg);
	}
	
	function uploadflash()
	{		 
		global $mainframe;
		$db 				= JFactory::getDBO();
		$user 				= JFactory::getUser();
		$files				= JRequest::get('files');
		$rsfilesConfig 		= RsfilesHelper::getConfig();
		$model 				= $this->getModel('files');
		$row 				= & JTable::getInstance('Rsfiles_Files','Table');
		$file				= $files['Filedata']; 
		$fieldName			= $file['name'];
		$fileTemp 			= $file['tmp_name'];
		$path 				= JRequest::getVar('folder','','request');
		$path1 				= empty($path) ? '' : DS.$path;
		$fullpath 			= $rsfilesConfig['download_folder'].$path1;
		
		
		//check to see if the upload is enabled
		if($rsfilesConfig['enable_upload'] == 0) return;
		//check to see if the user has permission to upload
		if (!$model->getPermissionMaintenance()) return;
		
		$rsfilesConfig['allowed_files'] = str_replace("\r",'',$rsfilesConfig['allowed_files']);
		$allowed_extensions = explode("\n",$rsfilesConfig['allowed_files']);
		
		if($user->id != 0)
		{
			$db->setQuery("SELECT CanPerforMaintenance FROM #__rsfiles_files WHERE FilePath = '".$db->getEscaped(urldecode($fullpath))."' ");
			$permission = $db->loadResult();
		} else $permission = '';
		
		if(!in_array(JFile::getExt($file['name']),$allowed_extensions)) return;
		if($file['size'] > ($rsfilesConfig['max_upl_size']*1024)) return;
		JFile::upload($file['tmp_name'], urldecode($fullpath.DS.JFile::getName($file['name'])));
		$row->IdFile = null;
		$row->FilePath = urldecode($fullpath.DS.JFile::getName($file['name']));
		$row->DateAdded = time();
		$row->ModifiedDate = time();
		$row->DownloadMethod = 0;
		$row->CanDownload = 0; 	 
		$row->CanPerforMaintenance = $permission; 	 
		$row->CanView = 0;
		$row->IdUser = $user->id;
		if(is_file($fullpath.DS.JFile::getName($file['name'])))
			$row->hash = md5_file($fullpath.DS.JFile::getName($file['name']));
		$row->hits = 0;
		$row->store();
		
		$filepath = '<a href="'.JURI::root().'index.php?option=com_rsfiles&view=files&layout=download&path='.urldecode($path.DS.JFile::getName($file['name'])).'">'.JURI::root().'index.php?option=com_rsfiles&view=files&layout=download&path='.urldecode($path.DS.JFile::getName($file['name'])).'</a>';

		
		
		if($rsfilesConfig['email_upload_enable'] == 1)
		{
			$to = $rsfilesConfig['email_upload_to'];
			$from = $rsfilesConfig['email_from'];
			$fromname = $rsfilesConfig['email_from_name'];
			$reply = $rsfilesConfig['email_reply'];
			$replyname = $rsfilesConfig['email_reply_name'];
			$subject = $rsfilesConfig['email_upload_subject'];
			$mode = $rsfilesConfig['email_upload_mode'];
			$cc = !empty($rsfilesConfig['email_cc']) ? $rsfilesConfig['email_cc'] : null;
			$bcc = !empty($rsfilesConfig['email_bcc']) ? $rsfilesConfig['email_bcc'] : null;
			$message = $rsfilesConfig['email_upload_message'];
			
			$bad = array('{username}','{files}');
			$good = array($user->name,$filepath);
			$message = str_replace($bad,$good,$message);
			
			if(JMailHelper::isEmailAddress($to))
				JUtility::sendMail($from , $fromname , $to , $subject , $message , $mode , $cc , $bcc , null , $reply , $replyname);
		}
	}
	
	function report()
	{		
		$db 	= JFactory::getDBO();
		$user 	= JFactory::getUser();
		$post 	= JRequest::get('post');
		$config = RsfilesHelper::getConfig();
		$ip		= $_SERVER['REMOTE_ADDR'];
		
		$fullpath = $config['download_folder'].DS.urldecode($post['path']);
		$db->setQuery("SELECT IdFile FROM #__rsfiles_files WHERE FilePath = '".$db->getEscaped($fullpath)."'");
		$idfile = $db->loadResult();
		
		if (empty($idfile))
		{
			$db->setQuery("INSERT INTO #__rsfiles_files SET FilePath = '".$db->getEscaped($fullpath)."' , DateAdded = '".time()."' , ModifiedDate = '".time()."' , DownloadMethod = 0 , CanPerforMaintenance = '' ,CanDownload = 0 , CanView = 0");
			$db->query();
			$idfile = $db->insertid();
		}
		
		$db->setQuery("INSERT INTO #__rsfiles_reports SET IdFile = ".$idfile.", ReportMessage = '".$db->getEscaped($post['report'])."' , uid = ".$user->id." , ip = '".$_SERVER['REMOTE_ADDR']."' , `date` = '".time()."'");
		$db->query();
		
		if($rsfilesConfig['email_report_enable'] == 1)
		{
			$filename = '<a href="'.JRoute::_('index.php?option=com_rsfiles&view=files&layout=download&path='.$post['path'],false).'">'.JRoute::_('index.php?option=com_rsfiles&view=files&layout=download&path='.$post['path'],false).'</a>';
			$to = $rsfilesConfig['email_report_to'];
			$from = $rsfilesConfig['email_from'];
			$fromname = $rsfilesConfig['email_from_name'];
			$reply = $rsfilesConfig['email_reply'];
			$replyname = $rsfilesConfig['email_reply_name'];
			$subject = $rsfilesConfig['email_report_subject'];
			$mode = $rsfilesConfig['email_report_mode'];
			$cc = !empty($rsfilesConfig['email_cc']) ? $rsfilesConfig['email_cc'] : null;
			$bcc = !empty($rsfilesConfig['email_bcc']) ? $rsfilesConfig['email_bcc'] : null;
			$message = $rsfilesConfig['email_report_message'];
			
			$bad = array('{username}','{ip}','{report}','{filename}');
			$good = array($user->username,$ip,$post['report'],$filename);
			$message = str_replace($bad,$good,$message);
			
			JUtility::sendMail($from , $fromname , $to , $subject , $message , $mode , $cc , $bcc , null , $reply , $replyname);
		}
		exit();
	}
	
	function bookmark()
	{
		global $mainframe;
		$session = JFactory::getSession();
		
		$config = RsfilesHelper::getConfig();
		$file = JRequest::getVar('file');		
		$fullpath = urldecode($config['download_folder'].DS.$file);
		
		if (!file_exists($fullpath)) { echo JText::_('RSFL_PATH_DOESNT_EXIST',true); exit(); }
		if (strpos(realpath($fullpath), $config['download_folder']) !== 0) { echo JText::_('RSFL_FILE_UNREACHABLE',true); exit(); }
		
		$bookmarks = $session->get('rsfl_bookmarks');
		$bookmarks[] = $fullpath;
		$session->set('rsfl_bookmarks',$bookmarks);
		echo JText::_('RSFL_FILE_BOOKMARKED',true); exit();
	}
	
	function processbookmarks()
	{		
		global $mainframe;
		jimport('joomla.filesystem.archive');
		jimport('joomla.filesystem.file');
		
		$config	= RsfilesHelper::getConfig();
		$sitec	= new JConfig;
		$cids 	= JRequest::getVar('cid');
		$action = JRequest::getWord('action');		
		$adapter =& JArchive::getAdapter('zip');
		
		if (empty($cids)) return;
		
		if ($action == 'download')
		{
			$files = array();
			foreach($cids as $cid)
			{
				$cid = urldecode($cid);
				$file = $config['download_folder'].DS.$cid;
				$data = JFile::read($file);
				$files[] = array('name' => JFile::getName($file), 'data' => $data);
			}

			if (!empty($files))
			{
				$filename = 'download_package_'.time().'.zip';
				$zip = $adapter->create($sitec->tmp_path.DS.$filename,$files);
				
				if ($zip)
				{
					@ob_end_clean();
					$filename = basename($filename);
					header("Cache-Control: public, must-revalidate");
					header('Cache-Control: pre-check=0, post-check=0, max-age=0');
					header("Pragma: no-cache");
					header("Expires: 0"); 
					header("Content-Description: File Transfer");
					header("Expires: Sat, 01 Jan 2000 01:00:00 GMT");
					header("Content-Type: application/octet-stream");
					header("Content-Length: ".(string) filesize($sitec->tmp_path.DS.$filename));
					header('Content-Disposition: attachment; filename="'.$filename.'"');
					header("Content-Transfer-Encoding: binary\n");
					$this->readfile_chunked($sitec->tmp_path.DS.$filename);
					exit();
				}
			}
		}
	}
	
	function readfile_chunked($filename,$retbytes=true)
	{
		$chunksize = 1*(1024*1024); // how many bytes per chunk
		$buffer = '';
		$cnt =0;
		$handle = fopen($filename, 'rb');
		if ($handle === false) {
			return false;
		}
		while (!feof($handle)) {
			$buffer = fread($handle, $chunksize);
			echo $buffer;
			if ($retbytes) {
				$cnt += strlen($buffer);
			}
		}
	   $status = fclose($handle);
	   if ($retbytes && $status) {
			return $cnt; // return num. bytes delivered like readfile() does.
		}
		return $status;
	}
	
	function deletebookmark()
	{
		global $mainframe;
		
		$session = JFactory::getSession();
		$config = RsfilesHelper::getConfig();
		$path = JRequest::getVar('path');
		
		$bookmarks = $session->get('rsfl_bookmarks');
		foreach($bookmarks as $i => $bookmark)
			if ($bookmark == $config['download_folder'].DS.urldecode($path)) unset($bookmarks[$i]);
		
		
		$session->clear('rsfl_bookmarks');
		$session->set('rsfl_bookmarks',$bookmarks);
		$this->setRedirect(JRoute::_('index.php?option=com_rsfiles&view=files&layout=bookmarks',false));
	}
	
	function save()
	{
		jimport('joomla.filesystem.file');
		
		$db = JFactory::getDBO();
		$model = $this->getModel('files');
		$post = JRequest::get('post',JREQUEST_ALLOWRAW);
		$files = JRequest::get('files');
		$thumb = $files['thumb'];
		$config = RsfilesHelper::getConfig();
		$row =& JTable::getInstance('Rsfiles_Files','Table');
		$Itemid = JRequest::getVar('Itemid','','request');
		$itemid = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
		
		$fullpath = $config['download_folder'].DS.urldecode($post['FilePath']);
		
		//check if the file already is in the database
		$db->setQuery("SELECT IdFile FROM #__rsfiles_files WHERE FilePath = '".$db->getEscaped($fullpath)."' ");
		$id = $db->loadResult();
		
		$row->load($id);
		$row->bind($post);
		
		$row->DateAdded = strtotime($row->DateAdded);
		$row->FilePath = $fullpath;		
		$row->store();
		
		$extension = JFile::getExt($thumb['name']);
		$thumb['name'] = JPATH_ROOT.DS.'components'.DS.'com_rsfiles'.DS.'images'.DS.'thumbs'.DS.'files'.DS.$row->IdFile;
		if(!$thumb['error'] && !empty($thumb['tmp_name']))
		{
			//create the thumbnail
			$uploaded = RsfilesHelper::createThumb($thumb['tmp_name'], $thumb['name'], $config['thumb_width'],$extension);
			
			if($uploaded)
			{
				$db->setQuery("UPDATE #__rsfiles_files SET FileThumb = '".JFile::getName($thumb['name'].'.'.$extension)."' WHERE IdFile ='".$row->IdFile."'");
				$db->query();
			}
		}
		
		$this->setRedirect(JRoute::_('index.php?option=com_rsfiles&view=files&layout=download&path='.$post['FilePath'].$itemid,false));
	}
	
	function deletethumb()
	{
		jimport('joomla.filesystem.file');
		$db = JFactory::getDBO();
		$cid = JRequest::getInt('cid');
		$config = RsfilesHelper::getConfig();
		$Itemid = JRequest::getVar('Itemid','','request');
		$itemid = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
		
		//see if the file has a thumb
		$db->setQuery("SELECT FilePath,FileThumb FROM #__rsfiles_files WHERE IdFile = '".$cid."'");
		$thumb = $db->loadObject();
		
		if(!empty($thumb))
		{
			$db->setQuery("UPDATE #__rsfiles_files SET FileThumb = '' WHERE IdFile=".$cid);
			$db->query();
			JFile::delete(JPATH_SITE.DS.'components'.DS.'com_rsfiles'.DS.'images'.DS.'thumbs'.DS.'files'.DS.$thumb->FileThumb);
		}
		
		$path = str_replace($config['download_folder'].DS,'',$thumb->FilePath);
		
		$this->setRedirect(JRoute::_('index.php?option=com_rsfiles&view=files&layout=edit&path='.urlencode($path).$itemid,false));
	}
	
}
?>