2009-06-19 07:39:16 +00:00
using System ;
using System.Web ;
using System.Web.UI ;
using System.IO ;
using System.Xml ;
using System.Text.RegularExpressions ;
using umbraco.presentation ;
using umbraco.cms.businesslogic.web ;
namespace umbraco
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
///
public partial class UmbracoDefault : Page
{
private Guid m_version = Guid . Empty ;
private string m_tmp = requestHandler . cleanUrl ( ) ;
private page m_umbPage = null ;
private requestHandler m_umbRequest = null ;
private bool m_validateRequest = true ;
/// <summary>
/// To turn off request validation set this to false before the PageLoad event. This equelevant to the validateRequest page directive
/// and has nothing to do with "normal" validation controls. Default value is true.
/// </summary>
public bool ValidateRequest
{
get { return m_validateRequest ; }
set { m_validateRequest = value ; }
}
protected override void Render ( HtmlTextWriter output )
{
// Get content
TextWriter tempWriter = new StringWriter ( ) ;
base . Render ( new HtmlTextWriter ( tempWriter ) ) ;
string pageContents = tempWriter . ToString ( ) ;
pageContents = template . ParseInternalLinks ( pageContents ) ;
2010-04-19 12:29:11 +00:00
// preview
if ( UmbracoContext . Current . InPreviewMode )
{
Trace . Write ( "Runtime Engine" , "Umbraco is running in preview mode." ) ;
int bodyPos = pageContents . ToLower ( ) . IndexOf ( "</body>" ) ;
if ( bodyPos > - 1 )
{
string htmlBadge =
String . Format ( UmbracoSettings . PreviewBadge ,
umbraco . IO . IOHelper . ResolveUrl ( umbraco . IO . SystemDirectories . Umbraco ) ,
umbraco . IO . IOHelper . ResolveUrl ( umbraco . IO . SystemDirectories . Umbraco_client ) ,
Server . UrlEncode ( UmbracoContext . Current . Request . Path )
) ;
// inject badge
pageContents =
pageContents . Substring ( 0 , bodyPos ) +
htmlBadge + pageContents . Substring ( bodyPos , pageContents . Length - bodyPos ) ;
}
}
2009-06-19 07:39:16 +00:00
output . Write ( pageContents ) ;
2010-03-25 10:14:54 +00:00
2009-06-19 07:39:16 +00:00
}
void Page_PreInit ( Object sender , EventArgs e )
{
Trace . Write ( "umbracoInit" , "handling request" ) ;
2010-01-28 12:51:46 +00:00
if ( UmbracoContext . Current = = null )
UmbracoContext . Current = new UmbracoContext ( HttpContext . Current ) ;
2010-04-19 12:29:11 +00:00
2009-06-19 07:39:16 +00:00
bool editMode = UmbracoContext . Current . LiveEditingContext . Enabled ;
2010-04-19 12:29:11 +00:00
2009-06-19 07:39:16 +00:00
if ( editMode )
ValidateRequest = false ;
if ( m_tmp ! = "" & & Request [ "umbPageID" ] = = null )
{
// Check numeric
string tryIntParse = m_tmp . Replace ( "/" , "" ) . Replace ( ".aspx" , string . Empty ) ;
int result ;
if ( int . TryParse ( tryIntParse , out result ) )
{
m_tmp = m_tmp . Replace ( ".aspx" , string . Empty ) ;
// Check for request
if ( ! string . IsNullOrEmpty ( Request [ "umbVersion" ] ) )
{
// Security check
BasePages . UmbracoEnsuredPage bp = new BasePages . UmbracoEnsuredPage ( ) ;
bp . ensureContext ( ) ;
m_version = new Guid ( Request [ "umbVersion" ] ) ;
}
}
}
else
{
if ( ! string . IsNullOrEmpty ( Request [ "umbPageID" ] ) )
2010-04-19 12:29:11 +00:00
{
2009-06-19 07:39:16 +00:00
int result ;
if ( int . TryParse ( Request [ "umbPageID" ] , out result ) )
{
m_tmp = Request [ "umbPageID" ] ;
}
}
}
if ( m_version ! = Guid . Empty )
{
HttpContext . Current . Items [ "pageID" ] = m_tmp . Replace ( "/" , "" ) ;
m_umbPage = new page ( int . Parse ( m_tmp . Replace ( "/" , "" ) ) , m_version ) ;
}
else
{
2010-02-10 14:00:47 +00:00
m_umbRequest = new requestHandler ( UmbracoContext . Current . GetXml ( ) , m_tmp ) ;
2009-06-19 07:39:16 +00:00
Trace . Write ( "umbracoInit" , "Done handling request" ) ;
if ( m_umbRequest . currentPage ! = null )
{
HttpContext . Current . Items [ "pageID" ] = m_umbRequest . currentPage . Attributes . GetNamedItem ( "id" ) . Value ;
// Handle edit
if ( editMode )
{
Document d = new Document ( int . Parse ( m_umbRequest . currentPage . Attributes . GetNamedItem ( "id" ) . Value ) ) ;
m_umbPage = new page ( d . Id , d . Version ) ;
}
else
m_umbPage = new page ( m_umbRequest . currentPage ) ;
}
}
// set the friendly path so it's used by forms
HttpContext . Current . RewritePath ( HttpContext . Current . Items [ requestModule . ORIGINAL_URL_CXT_KEY ] . ToString ( ) ) ;
if ( UmbracoSettings . UseAspNetMasterPages )
{
if ( m_umbPage ! = null )
this . MasterPageFile = template . GetMasterPageName ( m_umbPage . Template ) ;
initUmbracoPage ( ) ;
}
}
public Control pageContent = new Control ( ) ;
protected void Page_Load ( object sender , EventArgs e )
{
if ( ValidateRequest )
Request . ValidateInput ( ) ;
if ( ! String . IsNullOrEmpty ( Request [ "umbDebugShowTrace" ] ) )
{
if ( ! GlobalSettings . DebugMode )
{
Page . Trace . IsEnabled = false ;
}
}
else
Page . Trace . IsEnabled = false ;
2010-03-25 10:14:54 +00:00
2009-06-19 07:39:16 +00:00
}
#region Web Form Designer generated code
protected override void OnInit ( EventArgs e )
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent ( ) ;
if ( ! UmbracoSettings . UseAspNetMasterPages )
initUmbracoPage ( ) ;
base . OnInit ( e ) ;
}
private void initUmbracoPage ( )
{
if ( ! UmbracoSettings . EnableSplashWhileLoading | | ! content . Instance . isInitializing )
{
if ( m_umbPage ! = null )
{
// Add page elements to global items
try
{
2010-04-19 12:29:11 +00:00
2009-06-19 07:39:16 +00:00
System . Web . HttpContext . Current . Items . Add ( "pageElements" , m_umbPage . Elements ) ;
}
2010-04-19 12:29:11 +00:00
catch ( ArgumentException aex )
{
2009-06-19 07:39:16 +00:00
System . Web . HttpContext . Current . Items . Remove ( "pageElements" ) ;
System . Web . HttpContext . Current . Items . Add ( "pageElements" , m_umbPage . Elements ) ;
}
string tempCulture = m_umbPage . GetCulture ( ) ;
if ( tempCulture ! = "" )
{
System . Web . HttpContext . Current . Trace . Write ( "default.aspx" , "Culture changed to " + tempCulture ) ;
System . Threading . Thread . CurrentThread . CurrentCulture =
System . Globalization . CultureInfo . CreateSpecificCulture ( tempCulture ) ;
System . Threading . Thread . CurrentThread . CurrentUICulture = System . Threading . Thread . CurrentThread . CurrentCulture ;
}
if ( ! UmbracoSettings . UseAspNetMasterPages )
{
layoutControls . umbracoPageHolder pageHolder = new umbraco . layoutControls . umbracoPageHolder ( ) ;
pageHolder . ID = "umbPageHolder" ;
Page . Controls . Add ( pageHolder ) ;
m_umbPage . RenderPage ( m_umbPage . Template ) ;
layoutControls . umbracoPageHolder umbPageHolder =
( layoutControls . umbracoPageHolder ) Page . FindControl ( "umbPageHolder" ) ;
umbPageHolder . Populate ( m_umbPage ) ;
}
}
else
{
// If there's no published content, show friendly error
2010-03-31 09:39:36 +00:00
if ( umbraco . content . Instance . XmlContent . SelectSingleNode ( "/root/*" ) = = null )
2010-04-19 12:29:11 +00:00
Response . Redirect ( IO . SystemDirectories . Config + "/splashes/noNodes.aspx" ) ;
2009-06-19 07:39:16 +00:00
else
{
Response . StatusCode = 404 ;
Response . Write ( "<html><body><h1>Page not found</h1>" ) ;
if ( m_umbRequest ! = null )
HttpContext . Current . Response . Write ( "<h3>No umbraco document matches the url '" + HttpUtility . HtmlEncode ( Request . Url . ToString ( ) ) + "'</h3><p>umbraco tried this to match it using this xpath query'" + m_umbRequest . PageXPathQuery + "')" ) ;
else
HttpContext . Current . Response . Write ( "<h3>No umbraco document matches the url '" + HttpUtility . HtmlEncode ( Request . Url . ToString ( ) ) + "'</h3>" ) ;
Response . Write ( "</p>" ) ;
Response . Write ( "<p>This page can be replaced with a custom 404 page by adding the id of the umbraco document to show as 404 page in the /config/umbracoSettings.config file. Just add the id to the '/settings/content/errors/error404' element.</p>" ) ;
Response . Write ( "<p>For more information, visit <a href=\"http://umbraco.org/redir/custom-404\">information about custom 404</a> on the umbraco website.</p>" ) ;
Response . Write ( "<p style=\"border-top: 1px solid #ccc; padding-top: 10px\"><small>This page is intentionally left ugly ;-)</small></p>" ) ;
Response . Write ( "</body></html>" ) ;
}
}
}
else
{
2010-01-28 12:51:46 +00:00
Response . Redirect ( IO . SystemDirectories . Config + "/splashes/booting.aspx?orgUrl=" + Request . Url ) ;
2009-06-19 07:39:16 +00:00
}
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent ( )
{
}
#endregion
}
}