<?PHP

/**************************************
    Webutler V3.0 - www.webutler.de
    Copyright (c) 2008 - 2014
    Autor: Sven Zinke
    Free for any use
    Lizenz: GPL
**************************************/

require_once dirname(dirname(dirname(__FILE__))).'/includes/loader.php';

require_once $webutler_config['server_path'].'/admin/browser/assets/functions.php';

$webutlermedia = new WebutlerMBrowserClass;
$webutlermedia->config = $webutler_config;
$webutlermedia->MediaDirectory = $webutlermedia->config['server_path'].'/content/media/';

if(!$webutlermedia->checkadmin())
    exit('no access');

require_once $webutlermedia->config['server_path'].'/admin/browser/lang/'.$_SESSION['loggedin']['userlang'].'.php';

if(isset($_SESSION['browseruploadfile'])) {
    unset($_SESSION['browseruploadfile']);
}

$webutlermedia->type = '';
$webutlermedia->urltype = '';

if(!isset($_GET['type']) || $_GET['type'] == '') {
	if(isset($_GET['types']) && in_array($_GET['types'], $webutlermedia->AllowedTypes))
		$webutlermedia->type = $_GET['types'];
	else
		$webutlermedia->type = 'file';
	
	$webutlermedia->urltype = 'types='.$webutlermedia->type;
}
else {
	if(in_array($_GET['type'], $webutlermedia->AllowedTypes) && $_GET['type'] != 'file') {
		$webutlermedia->type = $_GET['type'];
		$webutlermedia->urltype = 'type='.$webutlermedia->type;
	}
}

if($webutlermedia->type == '') {
	echo _WBLANGADMIN_BROWSER_WRONGTYP_;
	exit;
}

if(!file_exists($webutlermedia->MediaDirectory.$webutlermedia->type.$webutlermedia->actualfolder())) {
	echo _WBLANGADMIN_BROWSER_WRONGPATH_;
	exit;
}

$webutlermedia->getthumb = (isset($_GET['vorschau']) && $webutlermedia->type == 'image') ? '&vorschau=1' : '';

if(isset($_POST['renamepopup_submit']) && $_POST['renamepopup_oldname'] != '' && $_POST['renamepopup_newname'] != '') {
    $renameoldname = preg_replace('/[^a-z0-9_\.]/', '', strtolower($_POST['renamepopup_oldname']));
    $renamenewname = preg_replace('/[^a-z0-9_]/', '', strtolower($_POST['renamepopup_newname']));
    $webutlermedia->renamefileto($renameoldname, $renamenewname);
    unset($_POST);
}

if(isset($_POST['cutoutfile']) && $_POST['cutoutfile'] != '') {
    $filetype = $webutlermedia->type;
    $filevalue = preg_replace('/[^a-z0-9_\|\/\.]/', '', $_POST['cutoutfile']);
    $_SESSION['cut'.$filetype] = $filevalue;
    unset($_SESSION['copy'.$filetype]);
    unset($_POST['cutoutfile']);
}

if(isset($_POST['copyfile']) && $_POST['copyfile'] != '') {
    $filetype = $webutlermedia->type;
    $filevalue = preg_replace('/[^a-z0-9_\|\/\.]/', '', $_POST['copyfile']);
    $_SESSION['copy'.$filetype] = $filevalue;
    unset($_SESSION['cut'.$filetype]);
    unset($_POST['copyfile']);
}

if(isset($_POST['insert']) && $_POST['insert'] == $webutlermedia->type) {
    if(isset($_SESSION['cut'.$webutlermedia->type]) && $_SESSION['cut'.$webutlermedia->type] != '') {
    	$webutlermedia->copyfilefromto($_SESSION['cut'.$webutlermedia->type], true);
        unset($_SESSION['cut'.$webutlermedia->type]);
    }
    if(isset($_SESSION['copy'.$webutlermedia->type]) && $_SESSION['copy'.$webutlermedia->type] != '') {
    	$webutlermedia->copyfilefromto($_SESSION['copy'.$webutlermedia->type]);
        unset($_SESSION['copy'.$webutlermedia->type]);
    }
    unset($_POST['insert']);
}

if(isset($_GET['delete']) && file_exists($webutlermedia->fileactionpath().$_GET['delete'])) {
	$webutlermedia->delete($_GET['delete']);
}

if(isset($_POST['foldername'])) {
	$webutlermedia->makefolder($webutlermedia->cleanpathfromget($_POST['foldername']));
}

if(isset($_GET['upload']) && $_GET['upload'] == 'newfile') {
	$webutlermedia->uploadfile($_GET['filename'], $_GET['filetype'], (isset($_GET['imgwidth']) ? $_GET['imgwidth'] : ''));
	exit;
}

$_SESSION['browseruploadfile']['tmpdir'] = $_SERVER['TMP'];

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?PHP echo _WBLANGADMIN_BROWSER_WINTITLE_; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-style-type" content="text/css" />
<meta http-equiv="content-script-type" content="text/javascript" />
<meta http-equiv="imagetoolbar" content="no" />
<meta name="robots" content="noindex, nofollow" />
<link type="text/css" rel="stylesheet" href="assets/styles.css" />
<link type="text/css" rel="stylesheet" href="assets/progress.css" />
<link type="text/css" rel="stylesheet" href="<?PHP echo $webutlermedia->config['homepage']; ?>/admin/system/css/loading.css" />
<script type="text/javascript" src="assets/jscript.js"></script>
<script type="text/javascript">
/* <![CDATA[ */
    function DeleteFile( FileName )
    {
    	if (confirm('<?PHP echo _WBLANGADMIN_BROWSER_DELALERT1_; ?> ' + FileName + ' <?PHP echo _WBLANGADMIN_BROWSER_DELALERT2_; ?>'))
    	{
    		window.top.location.href = 'index.php?<?PHP echo $webutlermedia->urltype.'&actualfolder='.$webutlermedia->makeurlfolder($webutlermedia->actualfolder()).$webutlermedia->getthumb.$webutlermedia->cke_getvars(); ?>&delete=' + FileName;
    	}
    }
    
    var imageeditorWindowWidth = '<?PHP echo $webutlermedia->config['imageeditor_wh'][0]; ?>';
    var imageeditorWindowHeight = '<?PHP echo $webutlermedia->config['imageeditor_wh'][1]; ?>';
    
    window.onresize = function() {
        setheights();
    }
    
    window.onload = function() {
        if(window.name == '')
            window.name = 'CKBrowseDialog';
        setheights();
        <?PHP echo $webutlermedia->jsalert; ?>
    }
/* ]]> */
</script>
</head>
<body scroll="no">
<div id="webutler_loadingscreen"></div>
<div id="uploadscreen">
    <div id="actiontext"><?PHP echo _WBLANGADMIN_BROWSER_UPLOADPROGRESS_; ?></div>
    <div id="actionprogress">
		<div id="prozent">
			<?PHP echo _WBLANGADMIN_BROWSER_UPLOADSTATUS_; ?>: <span id="prozentbar"></span>
		</div>
		<div id="progress">
			<div id="progressbar" style="display: none;"></div>
		</div>
		<div id="uploadcancel"><?PHP echo _WBLANGADMIN_BROWSER_UPLOADCANCEL_; ?></div>
	</div>
</div>
<div id="renamebg"></div>
<div id="renamepopup">
    <div id="renamepopup_close"><img alt="" src="images/close.gif" onclick="renamepopupclose()" /></div>
    <div id="renamepopup_text"><?PHP echo _WBLANGADMIN_BROWSER_RENAMEFILE_; ?></div>
    <div id="renamepopup_frame">
        <form method="post">
            <input type="hidden" name="renamepopup_oldname" id="renamepopup_oldname" value="" />
            <div id="renamepopup_newvalue">
            	<table border="0" cellspacing="0" cellpadding="0">
            	  <tr>
                	<td><input type="text" name="renamepopup_newname" id="renamepopup_newname" value="" /></td>
                	<td id="renamepopup_exttd"><span id="renamepopup_oldext"></span></td>
            	  </tr>
            	</table>
                
            </div>
            <!-- input type="text" class="intext" name="renamepopup_newname" id="renamepopup_newname" value="" /><span id="renamepopup_oldext"></span><br / -->
            <input type="submit" class="inbutton" name="renamepopup_submit" id="renamepopup_submit" value="<?PHP echo _WBLANGADMIN_BROWSER_BUTTON_SAVE_; ?>" />
        </form>
    </div>
</div>
<table style="width: 100%; height: 100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td style="height: 40px; background-color: #DEDEDE; border-bottom: 1px solid #a6a6a6">
	<table style="width: 100%; height: 20px" border="0" cellspacing="10" cellpadding="0">
	  <tr>
		<?PHP
		if(!isset($_GET['type']) || $_GET['type'] == '') {
			echo '<td style="width: 178px">';
			echo '<select class="SelectResource" id="ResourceSelector" onchange="location.href = this.options[this.selectedIndex].value;" onmouseover="title = this.options[this.selectedIndex].title;">';
			echo '<option value="index.php?types=file&actualfolder='.$webutlermedia->makeurlfolder("/").$webutlermedia->cke_getvars().'" title="'.$webutlermedia->resourcetitle('file').'"';
				if($webutlermedia->type == 'file') {
					echo ' selected="selected"';
				}
				echo '>'.$webutlermedia->resourcetype('file').'</option>';
			echo '<option value="index.php?types=image&actualfolder='.$webutlermedia->makeurlfolder("/").$webutlermedia->cke_getvars().'" title="'.$webutlermedia->resourcetitle('image').'"';
				if($webutlermedia->type == 'image') {
					echo ' selected="selected"';
				}
				echo '>'.$webutlermedia->resourcetype('image').'</option>';
			echo '<option value="index.php?types=flash&actualfolder='.$webutlermedia->makeurlfolder("/").$webutlermedia->cke_getvars().'" title="'.$webutlermedia->resourcetitle('flash').'"';
				if($webutlermedia->type == 'flash') {
					echo ' selected="selected"';
				}
				echo '>'.$webutlermedia->resourcetype('flash').'</option>';
			echo '<option value="index.php?types=track&actualfolder='.$webutlermedia->makeurlfolder("/").$webutlermedia->cke_getvars().'" title="'.$webutlermedia->resourcetitle('track').'"';
				if($webutlermedia->type == 'track') {
					echo ' selected="selected"';
				}
				echo '>'.$webutlermedia->resourcetype('track').'</option>';
			echo '</select>';
			echo '</td>';
		}
		else
		{
    		if($webutlermedia->type == 'flash' || $webutlermedia->type == 'track') {
    			echo '<td style="width: 178px">';
    			echo '<select class="SelectResource" id="ResourceSelector" onchange="location.href = this.options[this.selectedIndex].value;" onmouseover="title = this.options[this.selectedIndex].title;">';
    			echo '<option value="index.php?type=flash&actualfolder='.$webutlermedia->makeurlfolder("/").$webutlermedia->cke_getvars().'" title="'.$webutlermedia->resourcetitle('flash').'"';
    				if($webutlermedia->type == 'flash') {
    					echo ' selected="selected"';
    				}
    				echo '>'.$webutlermedia->resourcetype('flash').'</option>';
    			echo '<option value="index.php?type=track&actualfolder='.$webutlermedia->makeurlfolder("/").$webutlermedia->cke_getvars().'" title="'.$webutlermedia->resourcetitle('track').'"';
    				if($webutlermedia->type == 'track') {
    					echo ' selected="selected"';
    				}
    				echo '>'.$webutlermedia->resourcetype('track').'</option>';
    			echo '</select>';
    			echo '</td>';
    		}
            else {
			    echo '<td style="width: 178px" class="ResourceType" title="'.$webutlermedia->resourcetitle('image').'">'.$webutlermedia->resourcetype('image').'</td>';
			}
		}
		?>
		<td style="vertical-align: bottom; padding-left: 2px">
			<?PHP echo _WBLANGADMIN_BROWSER_CURRENTPATH_; ?>: <strong><?PHP echo $webutlermedia->actualfolder(); ?></strong>
		</td>
	  </tr>
	</table>
	</td>
  </tr>
  <tr>
    <td style="background-color: #F7F7F7; vertical-align: top">
	<table style="width: 100%; height: 100%" border="0" cellspacing="10" cellpadding="0">
	  <tr>
		<td style="width: 178px; vertical-align: top; border: 1px solid #373737; background-color: #ffffff">
		  <div style="width: 178px; height: 100%; overflow: auto" id="folderswin">
            <table style="margin: 5px" border="0" cellspacing="0" cellpadding="0">
            <?PHP
                if($webutlermedia->cleanpathfromget($webutlermedia->actualfolder()) != "/")
                {
                	echo '<tr>
                    	<td style="width: 21px"><img alt="" src="images/FolderUp.gif" style="margin-right: 5px" /></td>
                    	<td style="word-space: nowrap"><a href="index.php?'.$webutlermedia->urltype.'&actualfolder='.$webutlermedia->makeurlfolder($webutlermedia->lastfolder()).$webutlermedia->getthumb.$webutlermedia->cke_getvars().'" style="font-weight: bold; color: #b51719">&laquo; '._WBLANGADMIN_BROWSER_BUTTON_BACK_.'</a></td>
                    	</tr>';
                	
                	$datei = $webutlermedia->MediaDirectory.$webutlermedia->type.$webutlermedia->lastfolder();
                	
                	if(is_dir($datei))
                	{
                		$verzeichnis = opendir($datei);
                		while(false !== ($inhalt = readdir($verzeichnis)))
                		{
                			if($inhalt != "." && $inhalt != ".." && $inhalt != ".box")
                			{
                				if(is_dir($datei."/".$inhalt."/"))
                				{
                        			if(($inhalt == 'tpl_icons' && ($webutlermedia->config['admin_erweitert'] != '1' || (isset($_SESSION['loggedin']['userisadmin']) && $_SESSION['loggedin']['userisadmin'] == '1'))) || $inhalt != 'tpl_icons')
                        			{
                    					echo '<tr>';
                    					if(str_replace('/', '', str_replace($webutlermedia->lastfolder(), '', $webutlermedia->actualfolder())) == $inhalt)
                    					{
                    						echo '<td style="width: 21px"><img alt="" src="images/FolderOpened.gif" style="margin-right: 5px" /></td>
                                              <td style="word-space: nowrap">'.$inhalt.'</td>';
                    					}
                    					else
                    					{
                    						echo '<td style="width: 16px"><img alt="" src="images/Folder.gif" style="margin-right: 5px" /></td>
                                              <td style="word-space: nowrap"><a href="index.php?'.$webutlermedia->urltype.'&actualfolder='.$webutlermedia->makeurlfolder($webutlermedia->lastfolder().$inhalt.'/').$webutlermedia->getthumb.$webutlermedia->cke_getvars().'">'.$inhalt.'</a></td>';
                    					}
                    					echo '</tr>';
                        			}
                				}
                			}
                		}
                		closedir($verzeichnis);
                	}
                }
                clearstatcache();
            ?>
            </table>
          </div>
		</td>
		<td style="vertical-align: top; border: 1px solid #373737; background-color: #ffffff">
		  <div style="width: 100%; height: 100%; overflow: auto" id="fileswin">
            <table style="width: 98%; margin: 3px" width="98%" border="0" cellspacing="1" cellpadding="1">
                <?PHP
                if($webutlermedia->type == 'image') {
                    echo '<tr>';
                    if(isset($_GET['vorschau'])) {
                        echo '<td colspan="8" style="text-align: right;"><a href="index.php?'.$webutlermedia->urltype.'&actualfolder='.$webutlermedia->makeurlfolder($webutlermedia->actualfolder()).$webutlermedia->cke_getvars().'" style="font-weight: bold; text-decoration: underline">'._WBLANGADMIN_BROWSER_LISTVIEW_.'</a></td>';
                    }
                    else {
                        echo '<td colspan="8" style="text-align: right; padding-bottom: 3px"><a href="index.php?'.$webutlermedia->urltype.'&vorschau=1&actualfolder='.$webutlermedia->makeurlfolder($webutlermedia->actualfolder()).$webutlermedia->cke_getvars().'" style="font-weight: bold; text-decoration: underline">'._WBLANGADMIN_BROWSER_THUMBVIEW_.'</a></td>';
                    }
                    echo '</tr>';
                }
                
                $directory = $webutlermedia->MediaDirectory.$webutlermedia->type.$webutlermedia->cleanpathfromget($webutlermedia->actualfolder());
                echo $webutlermedia->get_folders($directory);
                echo $webutlermedia->get_files($directory);
                if((isset($_SESSION['cut'.$webutlermedia->type]) && $_SESSION['cut'.$webutlermedia->type] != '') || (isset($_SESSION['copy'.$webutlermedia->type]) && $_SESSION['copy'.$webutlermedia->type] != ''))
                {
                    echo '<tr>';
        			echo '<td colspan="8" style="text-align: right"><form method="post"><input type="hidden" name="insert" value="'.$webutlermedia->type.'" /><input type="image" src="images/insert.png" style="margin: 0px" /></form></td>';
                    echo '</tr>';
                }
                ?>
            </table>
          </div>
		</td>
	  </tr>
	</table>
	</td>
  </tr>
  <tr>
    <td style="height: 70px; background-color: #DEDEDE; border-top: 1px solid #a6a6a6">
	<table style="width: 100%; height: 50px" border="0" cellspacing="10" cellpadding="0">
	  <tr>
		<td style="text-align: right">
          <?PHP $disablefortpl = ($webutlermedia->actualfolder() == '/tpl_icons/') ? ' disabled="disabled"' : ''; ?>
		  <form action="index.php?<?PHP echo $webutlermedia->urltype.'&actualfolder='.$webutlermedia->makeurlfolder($webutlermedia->actualfolder()).$webutlermedia->getthumb.$webutlermedia->cke_getvars(); ?>" method="post">
        	<table border="0" cellspacing="0" cellpadding="0" align="right">
        	  <tr>
        		<td><?PHP echo _WBLANGADMIN_BROWSER_NEWFOLDER_; ?></td>
                <td><input type="text" name="foldername" class="intext" style="margin-left: 5px; width: 220px" value=""<?PHP echo $disablefortpl; ?> /></td>
                <td><input type="submit" class="inbutton" value="<?PHP echo _WBLANGADMIN_BROWSER_BUTTON_ADD_; ?>"<?PHP echo $disablefortpl; ?> /></td>
        	  </tr>
        	</table>
		  </form>
		</td>
	  </tr>
	  <tr>
		<td style="text-align: right">
        	<table border="0" cellspacing="0" cellpadding="0" align="right">
        	  <tr>
        		<td><?PHP
    				if($webutlermedia->type == 'image') {
                        echo '<div class="fakeupload"><input type="file" name="uploadfile" id="uploadfile" size="41" style="margin-right: 5px" onchange="setuploadpath(this)" /><input type="text" class="fakefield" /><input type="text" class="fakebutton" value="'._WBLANGADMIN_BROWSER_BUTTON_CHOOSE_.'" /></div></td><td>&nbsp;/ '._WBLANGADMIN_BROWSER_SCAL_.'<input type="text" id="imgwidth" name="imgwidth" size="4" maxlength="4" value="'.$webutlermedia->config['imgthumbsize'].'" class="intext" /> px';
    				}
    				else {
                        echo '<div class="fakeupload"><input type="file" id="uploadfile" name="uploadfile" size="41" onchange="setuploadpath(this)" /><input type="text" class="fakefield" /><input type="text" class="fakebutton" value="'._WBLANGADMIN_BROWSER_BUTTON_CHOOSE_.'" /></div>';
    				}
    			?></td>
                <td>
					<input type="hidden" id="actionpath" value="index.php?upload=newfile&<?PHP echo $webutlermedia->urltype.'&actualfolder='.$webutlermedia->makeurlfolder($webutlermedia->actualfolder()); ?>" />
					<input type="button" class="inbutton" value="<?PHP echo _WBLANGADMIN_BROWSER_MEDIAUPLOAD_; ?>" onclick="streamfileupload()" />
				</td>
        	  </tr>
        	</table>
		</td>
	  </tr>
	</table>
	</td>
  </tr>
</table>
</body>
</html>
