Ejemplo n.º 1
0
        private void GetFileContents(bool authenticated, Stream stream, HttpContext context, SPSite site, SPWeb web)
        {
            SPFile file = null;

            try
            {
                file = web.GetFile(this.VirtualPath);
            }
            catch { };

            if (file != null && file.Exists)
            {
                try
                {
                    byte[] binFile = null;
                    string content = string.Empty;

                    if (file.InDocumentLibrary)
                    {
                        SPListItem listItem = file.ListItemAllFields;

                        int  versionId  = 0;
                        bool versioning = file.DocumentLibrary.EnableVersioning;
                        SPFileVersionCollection versions = null;

                        if (versioning)
                        {
                            if (versioning && !authenticated && file.Level != SPFileLevel.Published && file.MajorVersion > 0)
                            {
                                versions = file.Versions;

                                try
                                {
                                    foreach (SPFileVersion version in versions)
                                    {
                                        if (version.Level == SPFileLevel.Published)
                                        {
                                            if (versionId <= version.ID)
                                            {
                                                versionId = version.ID;
                                            }
                                        }
                                    }
                                }
                                catch
                                {
                                }
                            }
                        }

                        string url = file.ServerRelativeUrl;

                        NameValueCollection queryString = context.Request.QueryString;
                        StringBuilder       builder     = new StringBuilder();
                        bool NoKeys = !queryString.HasKeys();

                        if (versionId > 0)
                        {
                            bool NoVersion = true;

                            foreach (string key in queryString.AllKeys)
                            {
                                if (key != null)
                                {
                                    //NoKeys = false;
                                    var value = queryString[key];
                                    if (key != "PageVersion")
                                    {
                                        builder.Append((NoKeys) ? "?" : "&" + key + "=" + value);
                                    }
                                    else
                                    {
                                        NoVersion = false;
                                    }
                                }
                            }

                            if (NoVersion)
                            {
                                builder.Append((NoKeys) ? "?" : "&");
                                builder.Append("PageVersion=" + versionId.ToString());
                                url = context.Request.Url.AbsolutePath + builder.ToString();
                                context.RewritePath(url);
                            }
                        }

                        /*
                         * else if (file.MajorVersion > 0 && !authenticated)
                         * {
                         *  int count = versions.Count;
                         *
                         *  for (int i = count; i > 0; i--)
                         *  {
                         *      try
                         *      {
                         *          if (versions[i].Level == SPFileLevel.Published)
                         *          {
                         *              builder.Append((NoKeys) ? "?" : "&");
                         *              builder.Append("PageVersion=" + versions[i].ID.ToString());
                         *              url = context.Request.Url.AbsolutePath + builder.ToString();
                         *              context.RewritePath(url);
                         *              break;
                         *          }
                         *      }
                         *      catch { };
                         *
                         *  }
                         * }
                         */

                        if (versionId == 0 && !authenticated && versioning && file.Level != SPFileLevel.Published)// && !(file.MajorVersion > 0))
                        {
                            WebSiteControllerRule rule = WebSiteControllerConfig.GetRule("ErrorCode", "403");
                            string page = rule.Properties["ErrorPage"].ToString();
                            context.Response.Redirect("~/" + page + "?aspxerrorpath=" + url, false);
                        }

                        if (listItem != null)
                        {
                            PublishingPageDesignFieldValue value = null;
                            SPFile pageLayoutFile = null;
                            SPWeb  rootWeb        = null;
                            try
                            {
                                if (listItem.Fields.Contains(BuildFieldId.PublishingPageDesign))
                                {
                                    value = listItem[BuildFieldId.PublishingPageDesign] as PublishingPageDesignFieldValue;
                                }
                                if (value != null)
                                {
                                    rootWeb = site.RootWeb;

                                    if (rootWeb.ServerRelativeUrl != "/")
                                    {
                                        string rootsite = SPContext.Current.Site.RootWeb.Url.Replace(SPContext.Current.Site.ServerRelativeUrl, string.Empty);
                                        site    = new SPSite(rootsite);
                                        rootWeb = site.OpenWeb();
                                    }

                                    pageLayoutFile = rootWeb.GetFile(value.Id);

                                    if (pageLayoutFile != null && !pageLayoutFile.Exists)
                                    {
                                        pageLayoutFile = rootWeb.GetFile(value.Url);
                                    }
                                }
                            }
                            catch { };


                            if (pageLayoutFile != null && pageLayoutFile.Exists)
                            {
                                if (pageLayoutFile.Item.HasPublishedVersion && (rootWeb != null && rootWeb.Exists))
                                {
                                    string slayout = rootWeb.GetFileAsString(pageLayoutFile.Url);
                                    binFile = Encoding.ASCII.GetBytes(slayout);

                                    //binFile = pageLayoutFile.OpenBinary();
                                }
                                else
                                {
                                    throw (new HttpException((int)HttpStatusCode.Forbidden, this.VirtualPath));
                                }
                            }
                            else
                            {
                                string sfile = web.GetFileAsString(file.Url);
                                binFile = Encoding.ASCII.GetBytes(sfile);

                                //binFile = file.OpenBinary();
                            }
                        }
                        else
                        {
                            throw (new HttpException((int)HttpStatusCode.Gone, this.VirtualPath));
                        }
                    }
                    else
                    {
                        binFile = file.OpenBinary();
                    }
                    //}
                    //else
                    //{
                    //    throw (new HttpException((int)HttpStatusCode.Forbidden, ""));
                    //}

                    if (binFile != null && binFile.Length > 0)
                    {
                        MemoryStream m      = new MemoryStream(binFile);
                        StreamReader reader = new StreamReader(m);
                        content = reader.ReadToEnd();
                        reader.Close();
                        //m.Close();

                        StreamWriter writer = new StreamWriter(stream);

                        writer.Write(content);
                        writer.Flush();
                        stream.Seek(0, SeekOrigin.Begin);
                    }
                }
                catch (HttpException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(ex.Source, TraceSeverity.High, EventSeverity.Error), TraceSeverity.High, ex.Message, ex.Data);
                    throw (new HttpException((int)HttpStatusCode.NotImplemented, ""));
                }
                finally
                {
                }
            }
            else
            {
                throw (new HttpException((int)HttpStatusCode.NotFound, this.VirtualPath));
            }
        }
Ejemplo n.º 2
0
        public void RaisePostBackEvent(string eventArgument)
        {
            SPContext.Current.Web.AllowUnsafeUpdates = true;
            Guid uniqueId = Guid.Empty;

            try
            {
                string[] eventArg = eventArgument.Split(new string[1] {
                    "#;"
                }, StringSplitOptions.None);

                SPWeb      rootWeb = SPContext.Current.Site.RootWeb;
                SPListItem item    = SPContext.Current.ListItem;

                if (!string.IsNullOrEmpty(eventArg[1]))
                {
                    PageLayout layout = servicePageLayouts.GetPageLayout(new Guid(eventArg[1]));

                    if (item != null && layout != null)
                    {
                        SPFile currentFile = item.File;
                        bool   checkout    = false;
                        if (currentFile != null && currentFile.Exists)
                        {
                            if (currentFile.CheckOutType == SPFile.SPCheckOutType.None && currentFile.RequiresCheckout)
                            {
                                currentFile.CheckOut();
                                checkout = true;
                            }
                        }

                        SPUser            suser     = null;
                        SPListItemVersion spversion = null;

                        SPListItemVersionCollection versions = item.Versions;
                        if (versions.Count > 0)
                        {
                            foreach (SPListItemVersion version in versions)
                            {
                                if (version.IsCurrentVersion)
                                {
                                    suser     = version.CreatedBy.User;
                                    spversion = version;
                                    item      = version.ListItem;
                                    break;
                                }
                            }
                        }

                        /*
                         * else
                         * {
                         *  listItem = file.ListItemAllFields;
                         * }
                         */

                        using (SPSite site = new SPSite(SPContext.Current.Web.Url, suser.UserToken))
                        {
                            SPWeb      spweb  = site.OpenWeb();
                            SPFile     spfile = spweb.GetFile(currentFile.UniqueId);
                            SPListItem spitem = spfile.Item.Versions.GetVersionFromLabel(spversion.VersionLabel).ListItem;


                            PublishingPageDesignFieldValue value = spitem[BuildFieldId.PublishingPageDesign] as PublishingPageDesignFieldValue;
                            value.Id    = layout.UniqueId;
                            value.Title = layout.Title;
                            value.Url   = layout.Url;

                            spitem[BuildFieldId.PublishingPageDesign] = value;

                            try
                            {
                                spitem.Fields[BuildFieldId.PublishingPageDesign].Update(true);
                            }
                            catch (Exception)
                            {
                            }

                            try
                            {
                                spitem.Update();
                            }
                            catch (Exception)
                            {
                            }

                            try
                            {
                                spitem.SystemUpdate(false);
                            }
                            catch (Exception)
                            {
                            }

                            try
                            {
                                spfile.Update();
                            }
                            catch (Exception)
                            {
                            }
                        }

                        uniqueId = layout.UniqueId;

                        //currentFile.Update();

                        if (checkout)
                        {
                            currentFile.CheckIn("Layout Changed", SPCheckinType.OverwriteCheckIn);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
                //result = ex;
            }
            SPContext.Current.Web.AllowUnsafeUpdates = false;

            url = base.parentStateControl.ContextUri;


            if (IsEditMode(this.Page))
            {
                url = WebPageStateControl.AddQueryStringParameter(WebPageStateControl.AddQueryStringParameter(url, "ControlMode", "Edit"), "DisplayMode", "Design");
            }

            url = WebPageStateControl.AddQueryStringParameter(url, "PageLayout", DateTime.Now.Ticks.ToString());

            base.parentStateControl.EnsureItemSavedIfEditMode(true);
            this.RefreshPageState();
            this.OnPageStateChanged();

            //base.ClearChildControlState();


            HtmlMeta refresh = new HtmlMeta();

            refresh.HttpEquiv = "refresh";
            refresh.Content   = "0;URL=" + url + "";
            this.Page.Header.Controls.Add(refresh);
            this.Page.Header.Controls.Add(new LiteralControl(Environment.NewLine));

            /*
             * if (IsEditMode(this.Page))
             * {
             *  SPUtility.Redirect("~/" + url, SPRedirectFlags.Trusted,HttpContext.Current);
             * }
             * else
             * {
             *  SPUtility.Redirect("~/" + base.parentStateControl.ContextUri, SPRedirectFlags.Trusted, HttpContext.Current);
             * }
             */
        }