private void ucAsyncControl_OnFinished(object sender, EventArgs e)
    {
        ScriptHelper.RegisterStartupScript(this, typeof(string), "StartTimer", "StopTimer();", true);
        ucActivityBar.Visible = false;
        pnlContent.Visible    = true;
        btnOk.Visible         = false;

        string targetUrl     = GetTargetUrl();
        string resultMessage = "ExportObject.BackupFinished";

        if (!IsBackupMode)
        {
            resultMessage = "ExportObject.lblResult";
            if (targetUrl != null)
            {
                btnDownload.OnClientClick = "window.open(" + ScriptHelper.GetString(targetUrl) + "); return false;";
                btnDownload.Visible       = true;
            }
        }

        string path        = ImportExportHelper.GetSiteUtilsFolderRelativePath() + "Export/" + txtFileName.Text;
        string storageName = (StorageHelper.IsExternalStorage(path)) ? GetString("Export.StorageProviderName." + StorageHelper.GetStorageProvider(path).Name) : "";

        lblResult.Text = String.Format(GetString(resultMessage), ExportedObjectDisplayName, storageName, path);
    }
    /// <summary>
    /// Testing '~/CMSSiteUtils/TestFiles/TestModify.txt' modification.
    /// </summary>
    protected void ModifyTest(string path)
    {
        string filePath   = path + "TestModify.txt";
        string strOK      = GetString("General.OK");
        string strFailed  = GetString("System_Files.Failed");
        string strSkipped = GetString("System_Files.ExternalStorage.ModifySkipped");
        string targetPath = ImportExportHelper.GetSiteUtilsFolderRelativePath();

        ltlInfo.Text += "<br /><br />";

        // Skip test on external storage
        if (StorageHelper.IsExternalStorage(filePath))
        {
            ltlInfo.Text += strSkipped;
            return;
        }

        // Path is full path
        if (targetPath == null)
        {
            targetPath = ImportExportHelper.GetSiteUtilsFolder() + "TestFiles\\";
        }
        else
        {
            targetPath = targetPath + "TestFiles/";
        }
        ltlInfo.Text += GetString("System_Files.Modifying") + " '" + targetPath + "TestModify.txt' - ";

        FileInfo fi = FileInfo.New(filePath);

        // If the file exists, try to append some text
        if (fi.Exists)
        {
            try
            {
                using (StreamWriter sw = fi.CreateText())
                {
                    sw.WriteLine(ltlInfo.Text.Replace("<br />", "\n") + strOK);
                }
                ltlInfo.Text += strOK;
            }
            catch
            {
                ltlInfo.Text += strFailed;
            }
        }
        else
        {
            ltlInfo.Text += strFailed;
        }
    }
Ejemplo n.º 3
0
    // Load imports lsit
    private void LoadImports()
    {
        if (lstImports.Items.Count == 0)
        {
            List <string> files = null;

            // Get import packages
            try
            {
                files = ImportProvider.GetImportFilesList();
            }
            catch
            {
                // Show error and log exception
                string path = ImportExportHelper.GetSiteUtilsFolderRelativePath();
                if (path == null)
                {
                    path = ImportExportHelper.GetSiteUtilsFolder();
                }

                lblError.Text = GetString("importconfiguration.securityerror").Replace("{0}", path + "Import");
            }

            if ((files != null) && (files.Count != 0))
            {
                lstImports.Enabled    = true;
                lstImports.DataSource = files;
                lstImports.DataBind();
            }
            else
            {
                lstImports.Enabled = false;
            }
        }

        if (!RequestHelper.IsPostBack())
        {
            try
            {
                lstImports.SelectedIndex = 0;
            }
            catch
            {
            }
        }
    }
    /// <summary>
    /// Create&Delete directory/file test.
    /// </summary>
    /// <param name="path">Path where to create subdirectory</param>
    /// <param name="di">Parent directory info</param>
    protected void CreateDeleteTest(string path, DirectoryInfo di)
    {
        FileInfo      fi  = null;
        DirectoryInfo sdi = null;

        string   strCreating = GetString("System_Files.Creating");
        string   strDeleting = GetString("System_Files.Deleting");
        string   strOK       = GetString("General.OK");
        string   strFailed   = GetString("System_Files.Failed");
        string   targetPath  = ImportExportHelper.GetSiteUtilsFolderRelativePath();
        DateTime dt          = DateTime.Now;
        // Create subdirectory name
        string subdir = "TestFolder" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() +
                        dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString();

        // Path is full path
        if (targetPath == null)
        {
            targetPath = ImportExportHelper.GetSiteUtilsFolder() + DirectoryHelper.CombinePath("TestFiles", subdir) + "\\";
        }
        else
        {
            targetPath = targetPath + "TestFiles/" + subdir + "/";
        }


        ltlInfo.Text += strCreating + " '" + targetPath + "' - ";
        try
        {
            // Create subdirectory
            sdi           = di.CreateSubdirectory(subdir);
            ltlInfo.Text += strOK;
        }
        catch
        {
            ltlInfo.Text += strFailed;
        }

        // Create test file name
        string fileName = "TestFile" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() +
                          dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString() + ".txt";

        ltlInfo.Text += "<br /><br />" + strCreating + " '" + targetPath + fileName + "' - ";
        try
        {
            // Create test file
            fi = FileInfo.New(path + subdir + "/" + fileName);
            using (StreamWriter sw = fi.CreateText())
            {
                sw.WriteLine("Edited - " + dt.ToString());
            }
            ltlInfo.Text += strOK;
        }
        catch
        {
            ltlInfo.Text += strFailed;
        }

        ltlInfo.Text += "<br /><br />" + strDeleting + " '" + targetPath + fileName + "' - ";
        try
        {
            // Delete test file
            fi.Delete();
            ltlInfo.Text += strOK;
        }
        catch
        {
            ltlInfo.Text += strFailed;
        }


        ltlInfo.Text += "<br /><br />" + strDeleting + " '" + targetPath + "' - ";
        try
        {
            // Delete subdirectory
            sdi.Delete();
            ltlInfo.Text += strOK;
        }
        catch
        {
            ltlInfo.Text += strFailed;
        }
    }
Ejemplo n.º 5
0
    /// <summary>
    /// Button Ok click.
    /// </summary>
    /// <param name="sender">Sender</param>
    /// <param name="e">Arguments</param>
    protected void btnOk_Click(object sender, EventArgs e)
    {
        // Validate file name
        string fileName     = txtFileName.Text.Trim();
        string errorMessage = new Validator().NotEmpty(fileName, GetString("lincense.export.filenameempty")).IsFileName(fileName, GetString("license.export.notvalidfilename")).Result;

        if (!string.IsNullOrEmpty(errorMessage))
        {
            ShowError(errorMessage);
        }
        else
        {
            try
            {
                // Create writers
                string path = ImportExportHelper.GetSiteUtilsFolder() + "Export\\" + fileName;
                DirectoryHelper.EnsureDiskPath(path, SettingsKeyProvider.WebApplicationPhysicalPath);

                using (FileStream file = FileStream.New(path, FileMode.Create))
                {
                    using (StreamWriter sw = StreamWriter.New(file))
                    {
                        // Array list for duplicity checking
                        ArrayList allSites = new ArrayList();

                        // Get all sites
                        DataSet sites = SiteInfoProvider.GetSites(null, null, "SiteID,SiteDomainName");
                        if (!DataHelper.DataSourceIsEmpty(sites))
                        {
                            foreach (DataRow dr in sites.Tables[0].Rows)
                            {
                                // Get domain
                                string domain = ValidationHelper.GetString(dr["SiteDomainName"], "");
                                if (!string.IsNullOrEmpty(domain))
                                {
                                    domain = GetDomain(domain);
                                    // Add to file
                                    if (!allSites.Contains(domain))
                                    {
                                        sw.WriteLine(domain);
                                        allSites.Add(domain);
                                    }

                                    // Add all domain aliases
                                    DataSet aliases = SiteDomainAliasInfoProvider.GetDomainAliases("SiteID=" + ValidationHelper.GetString(dr["SiteID"], ""), null, "SiteDomainAliasName");
                                    if (!DataHelper.DataSourceIsEmpty(aliases))
                                    {
                                        foreach (DataRow drAlias in aliases.Tables[0].Rows)
                                        {
                                            // Get domain
                                            domain = ValidationHelper.GetString(drAlias["SiteDomainAliasName"], "");
                                            if (!string.IsNullOrEmpty(domain))
                                            {
                                                domain = GetDomain(domain);
                                                // Add to file
                                                if (!allSites.Contains(domain))
                                                {
                                                    sw.WriteLine(domain);
                                                    allSites.Add(domain);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Output
                string downloadPath = ImportExportHelper.GetSiteUtilsFolderRelativePath();
                string location     = null;

                // Path is relative path
                if (downloadPath != null)
                {
                    string externalUrl = null;
                    string fullPath    = downloadPath + "Export/" + fileName;

                    // Handle external storage URL
                    if (StorageHelper.IsExternalStorage(fullPath))
                    {
                        externalUrl = File.GetFileUrl(fullPath, CMSContext.CurrentSiteName);
                    }

                    // Handle default URL
                    if (string.IsNullOrEmpty(externalUrl))
                    {
                        location = URLHelper.ResolveUrl(downloadPath + "Export/" + fileName);
                    }
                    else
                    {
                        location = externalUrl;
                    }

                    location = String.Format("<a href=\"{0}\" target=\"_blank\">{0}</a>", location);
                }
                else
                {
                    location = "<b>" + path + "</b>";
                }

                ShowConfirmation(GetString("license.export.exported"));
                ShowInformation(String.Format(GetString("license.export.download"), location));

                plcTextBox.Visible = false;
            }
            catch (Exception ex)
            {
                ShowError(ex.Message);
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        backup = QueryHelper.GetBoolean("backup", false);

        // Check permissions
        if (backup)
        {
            if (!CMSContext.CurrentUser.IsAuthorizedPerResource("cms.globalpermissions", "BackupObjects", CMSContext.CurrentSiteName))
            {
                RedirectToCMSDeskAccessDenied("cms.globalpermissions", "BackupObjects");
            }
        }
        else if (!CMSContext.CurrentUser.IsAuthorizedPerResource("cms.globalpermissions", "ExportObjects", CMSContext.CurrentSiteName))
        {
            RedirectToCMSDeskAccessDenied("cms.globalpermissions", "ExportObjects");
        }

        // Register script for pendingCallbacks repair
        ScriptHelper.FixPendingCallbacks(Page);

        // Async control events binding
        ucAsyncControl.OnFinished += ucAsyncControl_OnFinished;
        ucAsyncControl.OnError    += ucAsyncControl_OnError;

        if (!IsCallback)
        {
            try
            {
                // Delete temporary files
                ExportProvider.DeleteTemporaryFiles();
            }
            catch (Exception ex)
            {
                DisplayError(ex);
            }

            if (backup)
            {
                SetTitle("CMSModules/CMS_ImportExport/backupobject.png", GetString("BackupObject.Title"), null, null);
            }
            else
            {
                SetTitle("CMSModules/CMS_ImportExport/exportobject.png", GetString("ExportObject.Title"), null, null);
            }

            // Display BETA warning
            lblBeta.Visible = CMSContext.IsBetaVersion();
            lblBeta.Text    = string.Format(GetString("export.BETAwarning"), CMSContext.GetFriendlySystemVersion(false));

            // Get data from parameters
            objectId   = ValidationHelper.GetInteger(Request.QueryString["objectId"], 0);
            objectType = ValidationHelper.GetString(Request.QueryString["objectType"], "");

            // Get the object
            infoObj = CMSObjectHelper.GetReadOnlyObject(objectType);

            if (infoObj == null)
            {
                plcExportDetails.Visible = false;
                lblIntro.Text            = GetString("ExportObject.ObjectTypeNotFound");
                lblIntro.CssClass        = "ErrorLabel";
                return;
            }

            // Get exported object
            exportObj = infoObj.GetObject(objectId);
            if (exportObj == null)
            {
                plcExportDetails.Visible = false;
                lblIntro.Text            = GetString("ExportObject.ObjectNotFound");
                lblIntro.CssClass        = "ErrorLabel";
                btnOk.Visible            = false;
                return;
            }

            // Store display name
            exportObjectDisplayName = HTMLHelper.HTMLEncode(exportObj.ObjectDisplayName);

            if (backup)
            {
                lblIntro.Text = string.Format(GetString("BackupObject.Intro"), ResHelper.LocalizeString(exportObjectDisplayName));
            }
            else
            {
                lblIntro.Text = string.Format(GetString("ExportObject.Intro"), ResHelper.LocalizeString(exportObjectDisplayName));
            }

            btnOk.Click += btnOk_Click;

            if (!RequestHelper.IsPostBack())
            {
                lblIntro.Visible    = true;
                lblFileName.Visible = true;
                txtFileName.Text    = GetExportFileName(exportObj, backup);
                if (backup)
                {
                    btnOk.Text = GetString("General.backup");
                }
                else
                {
                    btnOk.Text = GetString("General.export");
                }
                btnCancel.Text = GetString("General.Close");
            }

            string path = null;
            if (backup)
            {
                path         = ImportExportHelper.GetObjectBackupFolder(exportObj);
                targetFolder = Server.MapPath(path);

                targetUrl = ResolveUrl(path) + "/" + txtFileName.Text;
            }
            else
            {
                targetFolder = ImportExportHelper.GetSiteUtilsFolder() + "Export";
                path         = ImportExportHelper.GetSiteUtilsFolderRelativePath();
                if (path != null)
                {
                    string externalUrl = null;
                    string fullPath    = path + "Export/" + txtFileName.Text;

                    // Handle external storage URL
                    if (StorageHelper.IsExternalStorage(fullPath))
                    {
                        externalUrl = File.GetFileUrl(fullPath, CMSContext.CurrentSiteName);
                    }

                    // Ensure default target URL if not set
                    if (string.IsNullOrEmpty(externalUrl))
                    {
                        targetUrl = ResolveUrl(path) + "Export/" + txtFileName.Text;
                    }
                    else
                    {
                        targetUrl = externalUrl;
                    }
                }
                else
                {
                    targetUrl = null;
                }
            }
        }
    }
    /// <summary>
    /// Button Ok click.
    /// </summary>
    /// <param name="sender">Sender</param>
    /// <param name="e">Arguments</param>
    protected void btnOk_Click(object sender, EventArgs e)
    {
        // Validate file name
        string fileName     = txtFileName.Text.Trim();
        string errorMessage = new Validator().NotEmpty(fileName, GetString("lincense.export.filenameempty")).IsFileName(fileName, GetString("license.export.notvalidfilename")).Result;

        if (!string.IsNullOrEmpty(errorMessage))
        {
            ShowError(errorMessage);
        }
        else
        {
            try
            {
                // Create writers
                string path = ImportExportHelper.GetSiteUtilsFolder() + "Export\\" + fileName;
                DirectoryHelper.EnsureDiskPath(path, SystemContext.WebApplicationPhysicalPath);

                using (FileStream file = FileStream.New(path, FileMode.Create))
                {
                    using (StreamWriter sw = StreamWriter.New(file))
                    {
                        // Array list for duplicity checking
                        ArrayList allSites = new ArrayList();

                        // Get all sites
                        DataSet sites = SiteInfoProvider.GetSites().Columns("SiteID,SiteDomainName");
                        if (!DataHelper.DataSourceIsEmpty(sites))
                        {
                            foreach (DataRow dr in sites.Tables[0].Rows)
                            {
                                // Get domain
                                string domain = ValidationHelper.GetString(dr["SiteDomainName"], "");
                                if (!string.IsNullOrEmpty(domain))
                                {
                                    domain = GetDomain(domain);
                                    // Add to file
                                    if (!allSites.Contains(domain))
                                    {
                                        sw.WriteLine(domain);
                                        allSites.Add(domain);
                                    }

                                    // Add all domain aliases
                                    DataSet aliases = SiteDomainAliasInfoProvider.GetDomainAliases()
                                                      .Column("SiteDomainAliasName")
                                                      .Where("SiteID", QueryOperator.Equals, ValidationHelper.GetInteger(dr["SiteID"], 0));

                                    if (!DataHelper.DataSourceIsEmpty(aliases))
                                    {
                                        foreach (DataRow drAlias in aliases.Tables[0].Rows)
                                        {
                                            // Get domain
                                            domain = ValidationHelper.GetString(drAlias["SiteDomainAliasName"], "");
                                            if (!string.IsNullOrEmpty(domain))
                                            {
                                                domain = GetDomain(domain);
                                                // Add to file
                                                if (!allSites.Contains(domain))
                                                {
                                                    sw.WriteLine(domain);
                                                    allSites.Add(domain);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Output
                string url          = ImportExportHelper.GetExportPackageUrl(fileName, SiteContext.CurrentSiteName);
                string downloadLink = (url != null) ? String.Format("<a href=\"{0}\" target=\"_blank\">{1}</a>", url, GetString("license.export.clicktodownload")) : "";
                string storageName  = (StorageHelper.IsExternalStorage(path)) ? GetString("Export.StorageProviderName." + StorageHelper.GetStorageProvider(path).Name) : "";
                string relativePath = ImportExportHelper.GetSiteUtilsFolderRelativePath() + "Export/" + txtFileName.Text;

                ShowConfirmation(GetString("license.export.exported"));
                ShowInformation(String.Format(GetString("license.export.download"), storageName, relativePath, downloadLink));

                plcTextBox.Visible = false;
            }
            catch (Exception ex)
            {
                ShowError(ex.Message);
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        backup = QueryHelper.GetBoolean("backup", false);

        // Check permissions
        if (backup)
        {
            if (!MembershipContext.AuthenticatedUser.IsAuthorizedPerResource("cms.globalpermissions", "BackupObjects", SiteContext.CurrentSiteName))
            {
                RedirectToAccessDenied("cms.globalpermissions", "BackupObjects");
            }
        }
        else if (!MembershipContext.AuthenticatedUser.IsAuthorizedPerResource("cms.globalpermissions", "ExportObjects", SiteContext.CurrentSiteName))
        {
            RedirectToAccessDenied("cms.globalpermissions", "ExportObjects");
        }

        // Register script for pendingCallbacks repair
        ScriptHelper.FixPendingCallbacks(Page);

        // Async control events binding
        ucAsyncControl.OnFinished += ucAsyncControl_OnFinished;
        ucAsyncControl.OnError    += ucAsyncControl_OnError;

        if (!RequestHelper.IsCallback())
        {
            try
            {
                // Delete temporary files
                ExportProvider.DeleteTemporaryFiles();
            }
            catch (Exception ex)
            {
                DisplayError(ex);
            }

            if (backup)
            {
                SetTitle(GetString("BackupObject.Title"));
            }
            else
            {
                SetTitle(GetString("ExportObject.Title"));
            }

            // Display BETA warning
            lblBeta.Visible = CMSVersion.IsBetaVersion();
            lblBeta.Text    = string.Format(GetString("export.BETAwarning"), CMSVersion.GetFriendlySystemVersion(false));

            // Get data from parameters
            objectId   = QueryHelper.GetInteger("objectId", 0);
            objectType = QueryHelper.GetString("objectType", "");

            // Get the object
            infoObj = ModuleManager.GetReadOnlyObject(objectType);

            if (infoObj == null)
            {
                plcExportDetails.Visible = false;
                lblIntro.Text            = GetString("ExportObject.ObjectTypeNotFound");
                lblIntro.CssClass        = "ErrorLabel";
                return;
            }

            // Get exported object
            exportObj = infoObj.GetObject(objectId);
            if (exportObj == null)
            {
                plcExportDetails.Visible = false;
                lblIntro.Text            = GetString("ExportObject.ObjectNotFound");
                lblIntro.CssClass        = "ErrorLabel";
                btnOk.Visible            = false;
                return;
            }

            // Check permissions
            var info = (BaseInfo)exportObj;
            if (!CurrentUser.IsGlobalAdministrator)
            {
                try
                {
                    if (info.Generalized.ObjectSiteID > 0)
                    {
                        CurrentUser.IsAuthorizedPerObject(PermissionsEnum.Read, info,
                                                          SiteInfoProvider.GetSiteName(info.Generalized.ObjectSiteID), true);
                    }
                    else if ((info.TypeInfo.SiteBindingObject != null) && (info.AssignedSites[CurrentSiteName] == null))
                    {
                        // Do not allow to clone objects with site binding which are not assigned to current site
                        RedirectToAccessDenied(info.TypeInfo.ModuleName, PermissionsEnum.Read.ToString());
                    }
                    else
                    {
                        CurrentUser.IsAuthorizedPerObject(PermissionsEnum.Read, info, CurrentSiteName, true);
                    }
                }
                catch (PermissionCheckException ex)
                {
                    RedirectToAccessDenied(ex.ModuleName, ex.PermissionFailed);
                }
            }

            // Store display name
            exportObjectDisplayName = HTMLHelper.HTMLEncode(ResHelper.LocalizeString(exportObj.ObjectDisplayName));

            lblIntro.Text = string.Format(GetString(backup ? "BackupObject.Intro" : "ExportObject.Intro"), exportObjectDisplayName);

            btnOk.Click += btnOk_Click;

            if (!RequestHelper.IsPostBack())
            {
                lblIntro.Visible    = true;
                lblFileName.Visible = true;
                txtFileName.Text    = GetExportFileName(exportObj, backup);
            }

            btnOk.Text = GetString(backup ? "General.backup" : "General.export");

            string path;
            if (backup)
            {
                path         = ImportExportHelper.GetObjectBackupFolder(exportObj);
                targetFolder = Server.MapPath(path);

                targetUrl = ResolveUrl(path) + "/" + txtFileName.Text;
            }
            else
            {
                targetFolder = ImportExportHelper.GetSiteUtilsFolder() + "Export";
                path         = ImportExportHelper.GetSiteUtilsFolderRelativePath();
                if (path != null)
                {
                    string externalUrl = null;
                    string fullPath    = path + "Export/" + txtFileName.Text;

                    // Handle external storage URL
                    if (StorageHelper.IsExternalStorage(fullPath))
                    {
                        externalUrl = File.GetFileUrl(fullPath, SiteContext.CurrentSiteName);
                    }

                    // Ensure default target URL if not set
                    if (string.IsNullOrEmpty(externalUrl))
                    {
                        targetUrl = ResolveUrl(path) + "Export/" + txtFileName.Text;
                    }
                    else
                    {
                        targetUrl = externalUrl;
                    }
                }
                else
                {
                    targetUrl = null;
                }
            }
        }
    }