using System;
using System.Collections;
using System.Web.UI;
using CMS.ExtendedControls;
using CMS.GlobalHelper;
using CMS.IO;
using CMS.SettingsProvider;
using CMS.UIControls;
public partial class CMSModules_Content_Controls_Dialogs_Properties_FileSystemPathProperties : ItemProperties
{
#region "Variables"
private FileSystemDialogConfiguration mDialogConfig = null;
#endregion
#region "Properties"
///
/// Gets or sets if selected item is file or folder.
///
public bool IsFile
{
get
{
return ValidationHelper.GetBoolean(ViewState["IsFile"], true);
}
set
{
ViewState["IsFile"] = value;
}
}
///
/// File system dialog configuration.
///
public FileSystemDialogConfiguration DialogConfig
{
get
{
return mDialogConfig;
}
set
{
mDialogConfig = value;
}
}
#endregion
#region "Page events"
///
/// Page load.
///
protected void Page_Load(object sender, EventArgs e)
{
// Get refference causing postback to hidden button
string postBackRef = ControlsHelper.GetPostBackEventReference(hdnButton, "");
string postBackKeyDownRef = "var keynum;if(window.event){keynum = event.keyCode;}else if(event.which){keynum = event.which;}if(keynum == 13){" + postBackRef + "; return false;}";
txtPathText.Attributes["onkeydown"] = postBackKeyDownRef;
}
///
/// Page prerender.
///
protected void Page_Prerender(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(txtPathText.Text))
{
if (lblError.Text == String.Empty)
{
lblEmpty.Text = DialogConfig.ShowFolders ? GetString("dialogs.filesystem.nofolderselected") : NoSelectionText;
lblEmpty.Visible = true;
plnPathUpdate.Visible = false;
}
else
{
plnPathUpdate.Visible = true;
lblEmpty.Visible = false;
}
}
else
{
plnPathUpdate.Visible = true;
lblEmpty.Visible = false;
}
}
///
/// Click action on hidden button.
///
protected void hdnButton_Click(object sender, EventArgs e)
{
if (Validate())
{
// Get selected item information
Hashtable properties = GetItemProperties();
// Get JavaScript for inserting the item
string script = CMSDialogHelper.GetFileSystemItem(properties);
if (!string.IsNullOrEmpty(script))
{
ScriptManager.RegisterStartupScript(Page, typeof(Page), "insertItemScript", script, true);
}
}
}
#endregion
#region "Overridden methods"
///
/// Loads the properties into control.
///
/// Properties to be loaded
public override void LoadItemProperties(Hashtable properties)
{
if (properties != null)
{
string filePath = ValidationHelper.GetString(properties[DialogParameters.ITEM_PATH], "");
string fileSize = ValidationHelper.GetString(properties[DialogParameters.ITEM_SIZE], "");
bool showSize = ValidationHelper.GetBoolean(properties[DialogParameters.ITEM_ISFILE], true);
bool isPathRelative = ValidationHelper.GetBoolean(properties[DialogParameters.ITEM_RELATIVEPATH], true);
IsFile = showSize;
if (isPathRelative)
{
filePath = URLHelper.UnMapPath(filePath);
if (filePath == "~")
{
filePath += "/";
}
}
txtPathText.Text = filePath;
if (showSize)
{
plcFileSize.Visible = true;
lblFileSizeText.Text = fileSize;
}
else
{
plcFileSize.Visible = false;
}
}
}
///
/// Returns all parameters of the selected item as name – value collection.
///
public override Hashtable GetItemProperties()
{
Hashtable retval = new Hashtable();
string path = txtPathText.Text.Trim();
retval[DialogParameters.ITEM_PATH] = path;
retval[DialogParameters.ITEM_RESOLVED_PATH] = URLHelper.ResolveUrl(path);
retval[DialogParameters.ITEM_SIZE] = lblFileSizeText.Text.Trim();
retval[DialogParameters.ITEM_ISFILE] = ValidationHelper.IsFileName(txtPathText.Text.Trim());
retval[DialogParameters.EDITOR_CLIENTID] = QueryHelper.GetString(DialogParameters.EDITOR_CLIENTID, "");
return retval;
}
///
/// Validates From, Cc and Bcc e-mails.
///
public override bool Validate()
{
string errorMessage = "";
try
{
if (string.IsNullOrEmpty(txtPathText.Text))
{
errorMessage += GetString("dialogs.filesystem.requirepath");
}
if (IsFile)
{
string ext = (txtPathText.Text.Contains(".") ? txtPathText.Text.Substring(txtPathText.Text.LastIndexOfCSafe(".")) : String.Empty);
if (!(File.Exists(txtPathText.Text.Trim()) || FileHelper.FileExists(txtPathText.Text.Trim())))
{
errorMessage += GetString("general.filedoesntexist");
}
else if (!IsAllowedExtension(ext) || IsExcludedExtension(ext))
{
if (!String.IsNullOrEmpty(DialogConfig.AllowedExtensions))
{
string allowedExt = ";" + DialogConfig.AllowedExtensions + ";";
if (!String.IsNullOrEmpty(DialogConfig.ExcludedExtensions))
{
foreach (string excludedExt in DialogConfig.ExcludedExtensions.Split(';'))
{
allowedExt = allowedExt.Replace(";" + excludedExt + ";", ";");
}
}
errorMessage += GetString("dialogs.filesystem.NotAllowedExtension").Replace("%%extensions%%", FormatExtensions(allowedExt));
}
else
{
errorMessage += GetString("dialogs.filesystem.ExcludedExtension").Replace("%%extensions%%", FormatExtensions(DialogConfig.ExcludedExtensions));
}
}
}
else
{
if (!((Directory.Exists(txtPathText.Text.Trim())) || (FileHelper.DirectoryExists(txtPathText.Text.Trim()))) && (!IsFile))
{
errorMessage += GetString("general.folderdoesntexist");
}
else if (!IsAllowedAndNotExcludedFolder(txtPathText.Text))
{
errorMessage += GetString("dialogs.filesystem.NotAllowedFolder");
}
}
}
catch (Exception ex)
{
errorMessage += ex.Message;
}
errorMessage = errorMessage.Trim();
if (errorMessage != "")
{
lblError.Text = errorMessage;
lblError.Visible = true;
plcFileSize.Visible = false;
plnPathUpdate.Update();
return false;
}
return true;
}
///
/// Clears the properties form.
///
public override void ClearProperties(bool hideProperties)
{
txtPathText.Text = "";
lblFileSizeText.Text = "";
}
#endregion
#region "Helper methods"
///
/// Check if manually typed extension is allowed.
///
/// File extension
/// True if allowed, false otherwise
private bool IsAllowedExtension(string extension)
{
if (String.IsNullOrEmpty(DialogConfig.AllowedExtensions))
{
return true;
}
else
{
string extensions = ";" + DialogConfig.AllowedExtensions.ToLowerCSafe() + ";";
if (extensions.Contains(";" + extension.ToLowerCSafe().TrimStart('.') + ";"))
{
return true;
}
}
return false;
}
///
/// Check if manually typed extension is excluded.
///
/// File extension
/// True if excluded, false otherwise
private bool IsExcludedExtension(string extension)
{
if (String.IsNullOrEmpty(DialogConfig.ExcludedExtensions))
{
return false;
}
else
{
string extensions = ";" + DialogConfig.ExcludedExtensions.ToLowerCSafe() + ";";
if (extensions.Contains(";" + extension.ToLowerCSafe().TrimStart('.') + ";"))
{
return true;
}
}
return false;
}
///
/// Check if folder is allowed and not excluded.
///
/// Folder to check
/// True if folder isallowed and not excluded otherwise false
private bool IsAllowedAndNotExcludedFolder(string folder)
{
bool isAllowed = false;
bool isExcluded = false;
string startPath = DialogConfig.StartingPath;
// Resolve relative URL with ~
if (startPath.StartsWithCSafe("~"))
{
startPath = ResolveUrl(startPath);
}
// Map to server if not network path
if (!startPath.Contains("\\\\"))
{
startPath = Server.MapPath(startPath);
}
startPath = DirectoryHelper.EnsurePathBackSlash(startPath.ToLowerCSafe());
string folderName = DirectoryHelper.EnsurePathBackSlash(folder.ToLowerCSafe());
try
{
folderName = Server.MapPath(folderName);
}
catch
{
}
folderName = folderName.ToLowerCSafe();
// Check if folder is allowed
if (String.IsNullOrEmpty(DialogConfig.AllowedFolders))
{
isAllowed = true;
}
else
{
foreach (string path in DialogConfig.AllowedFolders.ToLowerCSafe().Split(';'))
{
if (folderName.StartsWithCSafe(startPath + path))
{
isAllowed = true;
}
}
}
// Check if folder isn't excluded
if (!String.IsNullOrEmpty(DialogConfig.ExcludedFolders))
{
foreach (string path in DialogConfig.ExcludedFolders.ToLowerCSafe().Split(';'))
{
if (folderName.StartsWithCSafe(startPath + path))
{
isExcluded = true;
}
}
}
return (isAllowed) && (!isExcluded);
}
///
/// Format extensions.
///
/// Extensions string to be displayed
private static string FormatExtensions(string extensions)
{
string ext = ";" + extensions.Trim(';');
return ext.Replace(";", ";.").TrimStart(';').Replace(";", ", ");
}
#endregion
}