HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/RMourik/bassol.nl/wwwroot/CMSModules/AdminControls/Controls/MetaFiles/File.ascx.cs
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using System.Linq;

using CMS.ExtendedControls;
using CMS.Helpers;
using CMS.IO;
using CMS.Base;
using CMS.SiteProvider;
using CMS.Membership;
using CMS.UIControls;
using CMS.FormControls;
using CMS.DataEngine;

public partial class CMSModules_AdminControls_Controls_MetaFiles_File : ReadOnlyFormEngineUserControl
{
    #region "Variables"

    private bool mAlreadyUploadedDontDelete;
    private string baseUrl = null;

    private int width = 0;
    private int height = 0;

    protected bool columnUpdateVisible = false;

    #endregion


    #region "Properties"

    /// <summary>
    /// Object id.
    /// </summary>
    public int ObjectID
    {
        get
        {
            return ValidationHelper.GetInteger(ViewState["ObjectID"], 0);
        }
        set
        {
            ViewState["ObjectID"] = value;
        }
    }


    /// <summary>
    /// Object type.
    /// </summary>
    public string ObjectType
    {
        get
        {
            return ValidationHelper.GetString(ViewState["ObjectType"], "");
        }
        set
        {
            ViewState["ObjectType"] = value;
        }
    }


    /// <summary>
    /// Site id.
    /// </summary>
    public int SiteID
    {
        get
        {
            return ValidationHelper.GetInteger(ViewState["SiteID"], 0);
        }
        set
        {
            ViewState["SiteID"] = value;
        }
    }


    /// <summary>
    /// Attachment category/group
    /// </summary>
    public string Category
    {
        get
        {
            return ValidationHelper.GetString(ViewState["Category"], "");
        }
        set
        {
            ViewState["Category"] = value;
        }
    }


    /// <summary>
    /// Returns true if saving of the file failed.
    /// </summary>
    public bool SavingFailed
    {
        get
        {
            return ValidationHelper.GetBoolean(ViewState["SavingFailed"], false);
        }
    }


    /// <summary>
    /// Returns true if deleting of the file failed.
    /// </summary>
    public bool DeletingFailed
    {
        get
        {
            return ValidationHelper.GetBoolean(ViewState["DeletingFailed"], false);
        }
    }


    /// <summary>
    /// Gets or sets the semicolon-separated list of allowed file extensions (without dots).
    /// </summary>
    public string AllowedExtensions
    {
        get
        {
            return ValidationHelper.GetString(ViewState["AllowedExtensions"], null);
        }
        set
        {
            ViewState["AllowedExtensions"] = value;
        }
    }


    /// <summary>
    /// Indicates if uploader is enabled.
    /// </summary>
    public override bool Enabled
    {
        get
        {
            return uploader.Enabled;
        }
        set
        {
            uploader.Enabled = value;
        }
    }


    /// <summary>
    /// Returns the currently posted file or null when no file posted.
    /// </summary>
    public HttpPostedFile PostedFile
    {
        get
        {
            return uploader.PostedFile;
        }
    }


    /// <summary>
    /// Currently handled meta file.
    /// </summary>
    public MetaFileInfo CurrentlyHandledMetaFile
    {
        get;
        set;
    }


    /// <summary>
    /// Allow modify flag.
    /// </summary>
    protected bool AllowModify
    {
        get
        {
            return Enabled && UserInfoProvider.IsAuthorizedPerObject(ObjectType, ObjectID, PermissionsEnum.Modify, SiteContext.CurrentSiteName, MembershipContext.AuthenticatedUser);
        }
    }

    #endregion


    #region "Events"

    /// <summary>
    /// Occurs after the meta file was uploaded.
    /// </summary>
    public event EventHandler OnAfterUpload;


    /// <summary>
    /// Raises the OnAfterUpload event.
    /// </summary>
    private void RaiseOnAfterUpload()
    {
        if (OnAfterUpload != null)
        {
            OnAfterUpload(this, EventArgs.Empty);
        }
    }


    /// <summary>
    /// Occurs after the meta file was deleted.
    /// </summary>
    public event EventHandler OnAfterDelete;


    /// <summary>
    /// Raises the OnAfterDelete event.
    /// </summary>
    private void RaiseOnAfterDelete()
    {
        if (OnAfterDelete != null)
        {
            OnAfterDelete(this, EventArgs.Empty);
        }
    }

    #endregion


    #region "Methods"

    protected void Page_Load(object sender, EventArgs e)
    {
        // Register dialog script for Image Editor
        ScriptHelper.RegisterDialogScript(Page);
        ScriptHelper.RegisterTooltip(Page);
        ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "OpenImageEditor",
                                               ScriptHelper.GetScript(String.Format(@"
function OpenImageEditor(query) {{ 
    modalDialog('{0}/CMSModules/Content/CMSDesk/Edit/ImageEditor.aspx' + query, 'EditImage', 905, 670); 
    return false; 
}}", URLHelper.GetFullApplicationUrl())));
        ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "OpenEditor",
                                               ScriptHelper.GetScript(String.Format(@"
function OpenEditor(query) {{ 
    modalDialog('{0}/CMSModules/AdminControls/Controls/MetaFiles/MetaDataEditor.aspx' + query, 'EditMetadata', 500, 350); 
    return false; 
}} ", URLHelper.GetFullApplicationUrl())));
        // Register javascript 'postback' function
        ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "PostBack", ScriptHelper.GetScript(String.Format(@"
function UpdatePage(){{ 
    {0}; 
}}", Page.ClientScript.GetPostBackEventReference(hdnPostback, ""))));


        // Refresh script
        string script = String.Format(@"
function InitRefresh_{0}(msg, fullRefresh, action, fileId)
{{
    if((msg != null) && (msg != '')){{ 
        alert(msg); action='error'; 
    }}
    var hidden = document.getElementById('{1}');
    if (hidden) {{
        hidden.value = fileId;
    }}
    if(fullRefresh){{
        {2}
    }}
    else {{
        {3}
    }}
}}
function ConfirmDelete() {{
    return confirm({4});
}}
 ",
            ClientID,
            hdnField.ClientID,
            ControlsHelper.GetPostBackEventReference(hdnFullPostback, ""),
            ControlsHelper.GetPostBackEventReference(hdnPostback, ""),
            ScriptHelper.GetString(GetString("general.confirmdelete")));

        ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "MetafileScripts_" + ClientID, ScriptHelper.GetScript(script));

        BasicInit();
    }


    private void BasicInit()
    {
        // Init actions even if control is disabled. Enabled may be set late in some cases (team development).
        gridFile.OnAction += gridFile_OnAction;
        gridFile.OnExternalDataBound += gridFile_OnExternalDataBound;

        uploader.OnUploadFile += uploader_OnUploadFile;
        uploader.OnDeleteFile += uploader_OnDeleteFile;

        // Initialize UniGrid only if ObjectID is present
        if (ObjectID > 0)
        {
            gridFile.IsLiveSite = IsLiveSite;
            gridFile.WhereCondition = MetaFileInfoProvider.GetWhereCondition(ObjectID, ObjectType, Category, null);
            gridFile.StopProcessing = StopProcessing;
            gridFile.GridView.CssClass += " table-width-30";

            pnlGrid.Visible = true;
            pnlAttachmentList.CssClass = "AttachmentsList SingleAttachment";

            // Hide update column if modify not enabled
            gridFile.GridColumns.Columns[0].Visible = AllowModify;
        }
        else
        {
            pnlGrid.Visible = false;
        }
    }


    private void SetupControls()
    {
        ClearControl();

        if ((ObjectID > 0) && (ObjectType != "") && (Category != ""))
        {
            gridFile.ReloadData();
        }

        if (ObjectID > 0)
        {
            if (AllowModify)
            {
                // Initialize button for adding metafile
                newMetafileElem.ObjectID = ObjectID;
                newMetafileElem.ObjectType = ObjectType;
                newMetafileElem.Category = Category;
                newMetafileElem.ParentElemID = ClientID;
                newMetafileElem.SiteID = SiteID;
                newMetafileElem.InnerLoadingElementClass = "NewAttachmentLoading";
                if (AllowedExtensions != null)
                {
                    newMetafileElem.AllowedExtensions = AllowedExtensions;
                }
            }
            newMetafileElem.ForceLoad = true;
            newMetafileElem.Text = GetString("attach.uploadfile");
            newMetafileElem.InnerElementClass = "NewAttachment";
            newMetafileElem.IsLiveSite = IsLiveSite;
            newMetafileElem.SourceType = MediaSourceEnum.MetaFile;
            newMetafileElem.Visible = true;

            newMetafileElem.Enabled = AllowModify;
            plcOldUploader.Visible = false;
        }
        else
        {
            newMetafileElem.Visible = false;
            plcUploader.Visible = false;
            plcOldUploader.Visible = true;
        }
    }


    /// <summary>
    /// Reloads file uploader.
    /// </summary>
    public void ReloadData()
    {
        BasicInit();
        SetupControls();
    }


    /// <summary>
    /// Inits uploaded file name based on file name and GUID.
    /// </summary>
    /// <param name="fileName">File name</param>
    /// <param name="fileGuid">File GUID</param>
    public void InitUploader(string fileName, Guid fileGuid)
    {
        uploader.CurrentFileName = Path.GetFileName(fileName);
        uploader.CurrentFileUrl = "~/CMSPages/GetMetaFile.aspx?fileguid=" + fileGuid;
    }


    protected object gridFile_OnExternalDataBound(object sender, string sourceName, object parameter)
    {
        GridViewRow gvr;
        DataRowView drv;
        string fileGuid;

        switch (sourceName.ToLowerCSafe())
        {
            case "edit":
                if (sender is CMSAccessibleButton)
                {
                    gvr = (GridViewRow)parameter;
                    drv = (DataRowView)gvr.DataItem;

                    fileGuid = ValidationHelper.GetString(drv["MetaFileGUID"], "");
                    string fileExtension = ValidationHelper.GetString(drv["MetaFileExtension"], "");

                    // Initialize properties
                    CMSGridActionButton btnImageEditor = (CMSGridActionButton)sender;
                    btnImageEditor.Visible = true;

                    // Display button only if 'Modify' is allowed
                    if (AllowModify)
                    {
                        string query = String.Format("?refresh=1&metafileguid={0}&clientid={1}", fileGuid, ClientID);
                        query = URLHelper.AddUrlParameter(query, "hash", QueryHelper.GetHash(query));

                        // Display button only if metafile is in supported image format
                        if (ImageHelper.IsSupportedByImageEditor(fileExtension))
                        {
                            // Initialize button with script
                            btnImageEditor.OnClientClick = String.Format("OpenImageEditor({0}); return false;", ScriptHelper.GetString(query));
                        }
                        // Non-image metafile
                        else
                        {
                            // Initialize button with script
                            btnImageEditor.OnClientClick = String.Format("OpenEditor({0}); return false;", ScriptHelper.GetString(query));
                        }
                    }
                    else
                    {
                        btnImageEditor.Enabled = false;
                    }
                }
                break;

            case "delete":
                if (sender is CMSGridActionButton)
                {
                    CMSGridActionButton btnDelete = ((CMSGridActionButton)sender);
                    if (!AllowModify)
                    {
                        btnDelete.Enabled = false;
                    }
                }
                break;

            case "name":
                drv = (DataRowView)parameter;

                string fileName = ValidationHelper.GetString(DataHelper.GetDataRowViewValue(drv, "MetaFileName"), string.Empty);
                fileGuid = ValidationHelper.GetString(DataHelper.GetDataRowViewValue(drv, "MetaFileGUID"), string.Empty);
                string fileExt = ValidationHelper.GetString(DataHelper.GetDataRowViewValue(drv, "MetaFileExtension"), string.Empty);

                bool isImage = ImageHelper.IsImage(fileExt);
                string fileUrl = String.Format("{0}?fileguid={1}&chset={2}", URLHelper.GetAbsoluteUrl("~/CMSPages/GetMetaFile.aspx"), fileGuid, Guid.NewGuid());

                // Tooltip
                string title = ValidationHelper.GetString(DataHelper.GetDataRowViewValue(drv, "MetaFileTitle"), string.Empty);
                
                string description = ValidationHelper.GetString(DataHelper.GetDataRowViewValue(drv, "MetaFileDescription"), string.Empty);
                int imageWidth = ValidationHelper.GetInteger(DataHelper.GetDataRowViewValue(drv, "MetaFileImageWidth"), 0);
                int imageHeight = ValidationHelper.GetInteger(DataHelper.GetDataRowViewValue(drv, "MetaFileImageHeight"), 0);
                string tooltip = UIHelper.GetTooltipAttributes(fileUrl, imageWidth, imageHeight, title, fileName, fileExt, description, null, 300);

                // Icon
                string iconTag = UIHelper.GetFileIcon(Page,fileExt, tooltip: fileName);
                if (isImage)
                {
                    return String.Format("<a href=\"#\" onclick=\"javascript: window.open('{0}'); return false;\" class=\"cms-icon-link\"><span id=\"{1}\" {2}>{3}{4}</span></a>", fileUrl, fileGuid, tooltip, iconTag, fileName);
                }
                else
                {
                    return String.Format("<a href=\"{0}\" class=\"cms-icon-link\"><span id=\"{1}\" {2}>{3}{4}</span></a>", fileUrl, fileGuid, tooltip, iconTag, fileName);
                }

            case "size":
                return DataHelper.GetSizeString(ValidationHelper.GetLong(parameter, 0));

            case "update":
                {
                    drv = (DataRowView)parameter;

                    Panel pnlBlock = new Panel
                        {
                            ID = "pnlBlock"
                        };

                    string siteName = null;

                    if (SiteID > 0)
                    {
                        SiteInfo si = SiteInfoProvider.GetSiteInfo(SiteID);
                        if (si != null)
                        {
                            siteName = si.SiteName;
                        }
                    }
                    else
                    {
                        siteName = SiteContext.CurrentSiteName;
                    }

                    // Add update control
                    // Dynamically load uploader control
                    var dfuElem = Page.LoadUserControl("~/CMSModules/Content/Controls/Attachments/DirectFileUploader/DirectFileUploader.ascx") as DirectFileUploader;
                    if (dfuElem != null)
                    {
                        dfuElem.ID = "dfuElem" + ObjectID;
                        dfuElem.SourceType = MediaSourceEnum.MetaFile;
                        dfuElem.ControlGroup = "Uploader_" + ObjectID;
                        dfuElem.DisplayInline = true;
                        dfuElem.ForceLoad = true;
                        dfuElem.MetaFileID = ValidationHelper.GetInteger(drv["MetaFileID"], 0);
                        dfuElem.ObjectID = ObjectID;
                        dfuElem.ObjectType = ObjectType;
                        dfuElem.Category = Category;
                        dfuElem.ParentElemID = ClientID;
                        dfuElem.ShowIconMode = true;
                        dfuElem.InsertMode = false;
                        dfuElem.ParentElemID = ClientID;
                        dfuElem.IncludeNewItemInfo = true;
                        dfuElem.SiteID = SiteID;
                        dfuElem.IsLiveSite = IsLiveSite;

                        // Setting of the direct single mode
                        dfuElem.UploadMode = MultifileUploaderModeEnum.DirectSingle;
                        dfuElem.Width = 16;
                        dfuElem.Height = 16;
                        dfuElem.MaxNumberToUpload = 1;

                        if (AllowedExtensions != null)
                        {
                            dfuElem.AllowedExtensions = AllowedExtensions;
                        }

                        pnlBlock.Controls.Add(dfuElem);
                    }

                    // Setup external edit
                    ExternalEditHelper.LoadExternalEditControl(pnlBlock, FileTypeEnum.MetaFile, siteName, new DataRowContainer(drv), IsLiveSite);

                    return pnlBlock;
                }
        }
        return parameter;
    }


    protected void gridFile_OnAction(string actionName, object actionArgument)
    {
        switch (actionName.ToLowerCSafe())
        {
            case "delete":
                try
                {
                    // Delete the meta file
                    int metaFileId = ValidationHelper.GetInteger(actionArgument, 0);
                    MetaFileInfoProvider.DeleteMetaFileInfo(metaFileId);

                    RaiseOnAfterDelete();
                }
                catch (Exception ex)
                {
                    ShowError(ex.Message);
                }
                break;
        }
    }


    protected void uploader_OnUploadFile(object sender, EventArgs e)
    {
        UploadFile();
    }


    protected void uploader_OnDeleteFile(object sender, EventArgs e)
    {
        // Careful with upload and delete in on postback - ignore delete request
        if (mAlreadyUploadedDontDelete)
        {
            return;
        }

        try
        {
            using (DataSet ds = MetaFileInfoProvider.GetMetaFiles(ObjectID, ObjectType, Category, null, null))
            {
                if (!DataHelper.DataSourceIsEmpty(ds))
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        var mfi = new MetaFileInfo(dr);
                        if (mfi.MetaFileName.ToLowerCSafe() == uploader.CurrentFileName.ToLowerCSafe())
                        {
                            MetaFileInfoProvider.DeleteMetaFileInfo(mfi.MetaFileID);
                        }
                    }
                }
            }

            RaiseOnAfterDelete();

            SetupControls();
        }
        catch (Exception ex)
        {
            ViewState["DeletingFailed"] = true;
            ShowError(ex.Message);
            SetupControls();
        }
    }


    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        SetupControls();

        if (!AllowModify)
        {
            // Hide the update column
            gridFile.GridView.Columns[1].Visible = false;
        }

        if (!string.IsNullOrEmpty(baseUrl))
        {
            ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "OpenEditor",
                                                   ScriptHelper.GetScript(String.Format(@"
function OpenEditor(queryString) {{ 
    modalDialog('{0}{1}' + queryString, 'editorDialog', {2}, {3}); 
    return false; 
}}", URLHelper.GetFullApplicationUrl(), baseUrl, width, height)));
        }

        if (ObjectID > 0)
        {
            bool gridHasData = !DataHelper.DataSourceIsEmpty(gridFile.GridView.DataSource);

            // Ensure uploader button
            plcUploader.Visible = !gridHasData;
        }
    }


    /// <summary>
    /// Returns true if entered data is valid. If data is invalid, it returns false and displays an error message.
    /// </summary>
    public override bool IsValid()
    {
        if ((plcOldUploader.Visible) && (uploader.PostedFile != null))
        {
            string extensions = string.IsNullOrEmpty(AllowedExtensions) ? SettingsKeyInfoProvider.GetValue(SiteContext.CurrentSiteName + ".CMSUploadExtensions") : AllowedExtensions;
            if (extensions != String.Empty)
            {
                string extension = Path.GetExtension(uploader.PostedFile.FileName).TrimStart('.').ToLowerCSafe();
                string haystack = String.Format(";{0};", extensions);
                string needle = String.Format(";{0};", extension);

                if (!haystack.Contains(needle))
                {
                    string format = GetString("attach.notallowedextension");
                    ValidationError = String.Format(format, extension, extensions);
                    return false;
                }
            }
        }

        return true;
    }


    /// <summary>
    /// Uploads file.
    /// </summary>
    public void UploadFile()
    {
        if ((uploader.PostedFile != null) && (ObjectID > 0))
        {
            try
            {
                MetaFileInfo existing = null;

                // Check if uploaded file already exists and delete it
                DataSet ds = MetaFileInfoProvider.GetMetaFiles(ObjectID, ObjectType, Category, null, null);
                if (!DataHelper.DataSourceIsEmpty(ds))
                {
                    // Get existing record ID and delete it
                    existing = new MetaFileInfo(ds.Tables[0].Rows[0]);
                    MetaFileInfoProvider.DeleteMetaFileInfo(existing);
                }

                // Create new meta file
                MetaFileInfo mfi = new MetaFileInfo(uploader.PostedFile, ObjectID, ObjectType, Category);
                if (existing != null)
                {
                    // Preserve GUID
                    mfi.MetaFileGUID = existing.MetaFileGUID;
                    mfi.MetaFileTitle = existing.MetaFileTitle;
                    mfi.MetaFileDescription = existing.MetaFileDescription;
                }
                mfi.MetaFileSiteID = SiteID;

                // Save to the database
                MetaFileInfoProvider.SetMetaFileInfo(mfi);

                CurrentlyHandledMetaFile = mfi;
                RaiseOnAfterUpload();

                SetupControls();
            }
            catch (Exception ex)
            {
                ShowError(ex.Message);
                ViewState["SavingFailed"] = true;
                SetupControls();
            }

            // File was uploaded, do not delete in one postback
            mAlreadyUploadedDontDelete = true;
        }
    }


    /// <summary>
    /// Clears the content (file name & file URL) of the control.
    /// </summary>
    public void ClearControl()
    {
        uploader.CurrentFileName = string.Empty;
        uploader.CurrentFileUrl = string.Empty;
    }


    protected void hdnPostback_Click(object sender, EventArgs e)
    {
        try
        {
            int fileId = ValidationHelper.GetInteger(hdnField.Value, 0);
            if (fileId > 0)
            {
                CurrentlyHandledMetaFile = MetaFileInfoProvider.GetMetaFileInfo(fileId);
                RaiseOnAfterUpload();

                gridFile.ReloadData();
                updPanel.Update();
            }
        }
        catch (Exception ex)
        {
            ShowError(ex.Message);
        }
    }

    #endregion
}