<!--#include virtual="/NR/System/WBC/Internals/Main/WBCApp.inc"-->
<!--#include virtual="/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/ResourceBrowser/Common/ResourceBrowserCommon.inc" -->
<!--#include virtual="/NR/System/WBC/Customizable/WBCFuncPagesConfig/UIStringConstants/CommonUIStrings.inc" -->

<%
' ************************************************************************
' File:		/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/ResourceBrowser/Authoring/UploadAcceptor.asp
' Author:	Microsoft Corporation.
' Purpose:	Process the uploaded desktop resource and add it to a
'			placeholder
'
' Query String Parameters:
'	[API-Generated-Query-Params]: This set of parameters is generated by
'								invoking the API method Posting.QueryString
'								in the previous ASP page. It contains all the
'								necessary information to be interpreted by the
'								API to set the AutoSession.This object in this
'								ASP page.
'	IDS_QPARAM_PHNAME: Placeholder Name.
'	IDS_QPARAM_PHTYPE: Placeholder Type.
'	                   Valid values are IDS_PHTYPE_MULTIPURPOSE,
'	                   IDS_PHTYPE_SINGLEATTACHMENT, IDS_PHTYPE_SINGLEIMAGE,
'	                   IDS_PHTYPE_SINGLELINETEXT, IDS_PHTYPE_MULTILINETEXT and
'	                   IDS_PHTYPE_THINEDITIE.
'	IDS_QPARAM_RESTYPE: Resource Type.
'	                    Valid values are IDS_RESTYPE_ATTACHMENT, IDS_RESTYPE_IMAGE
'	                    and IDS_RESTYPE_VIDEO.
'	IDS_QPARAM_ATTACHICON:Indicates whether the resource is to be shown as icon
'							in the case that it is an attachment.
'							Possible values are {True, False}.
'	IDS_QPARAM_IMG: Determines whether the <img> or <embed> tag will be used
'					to insert the resource into the placeholder content,
'					in the case that it is a video.
'
' Form Field Names:
'	NC_Href: The hyperlink reference URL for an image resource.
'	NC_AltText: The ALT text for an image resource.
'	NC_DispText: The display text for an attachment resource.
'
' ************************************************************************


Dim g_pThisPosting
Dim g_strPhName
Dim g_strPhType
Dim g_strResType
Dim g_strBlobUrl
Dim g_strPhHref
Dim g_strPhAltText
Dim g_bAttachIcon
Dim g_strPhDispText
Dim g_bImg
Dim g_lScriptTimeOut
Dim g_bBuffer

if AutoSession.UserCanAuthor = false then
	response.End
end if

g_lScriptTimeOut = Server.ScriptTimeOut
g_bBuffer = Response.Buffer

Server.ScriptTimeOut = 10000
Response.Buffer = true

Function GetPhName()
	On Error Resume Next
	Dim pPlaceholder

	Set pPlaceholder = AutoSession.ThisPosting.Placeholders( CStr(Request.QueryString(IDS_QPARAM_PHNAME)) )
	If (pPlaceholder Is Nothing) Then
		GetPhName = ""
	Else
		GetPhName = CStr(Request.QueryString(IDS_QPARAM_PHNAME))
	End If
End Function

Function GetPhType()
	' Restrict possible value of g_strPhType to guard against malicious input
	Select Case Request.QueryString(IDS_QPARAM_PHTYPE)
		Case IDS_PHTYPE_MULTIPURPOSE
			GetPhType = IDS_PHTYPE_MULTIPURPOSE
		Case IDS_PHTYPE_SINGLEATTACHMENT
			GetPhType = IDS_PHTYPE_SINGLEATTACHMENT
		Case IDS_PHTYPE_SINGLEIMAGE
			GetPhType = IDS_PHTYPE_SINGLEIMAGE
		Case IDS_PHTYPE_THINEDITIE
			GetPhType = IDS_PHTYPE_THINEDITIE
		Case Else
			GetPhType = ""
	End Select
End Function

Function GetResType()
	Select Case Request.QueryString(IDS_QPARAM_RESTYPE)
		Case IDS_RESTYPE_ATTACHMENT
			GetResType = IDS_RESTYPE_ATTACHMENT
		Case IDS_RESTYPE_IMAGE
			GetResType = IDS_RESTYPE_IMAGE
		Case IDS_RESTYPE_VIDEO
			GetResType = IDS_RESTYPE_VIDEO
		Case Else
			GetResType = ""
	End Select
End Function


Set g_pThisPosting = AutoSession.ThisPosting
g_strPhName = GetPhName()
g_strPhType = GetPhType()
g_strResType = GetResType()


' Routine that reads all the content and throws it away so we can do a clean exit
sub EndResponse
	Dim varContent
	while true
		Dim nCount
		nCount = 100000
		varContent = Request.BinaryRead(nCount)
		if nCount = 0 then
			Response.End
		end if
	wend
end sub

'
'	Query String Parameter Validation
'
If (g_strPhName = "") or (g_strPhType = "") or (g_strResType = "") Then
	Response.Write(IDS_INCORRECT_PARM)
	EndResponse
End If


'
'	Validate whether placeholder allows images or attachments upload
'
Dim g_pPlaceholder
Dim g_bAllowUpload

g_bAllowUpload = false
Set g_pPlaceholder = g_pThisPosting.Placeholders(g_strPhName)
If g_pPlaceholder.XIsSingleImage Then
	If g_pPlaceholder.XAllowsImageUpload Then
		g_bAllowUpload = true
	End If
ElseIf g_pPlaceholder.XIsSingleAttachment Or g_pPlaceholder.XIsSingleVideo Then
	If g_pPlaceholder.XAllowsAttachmentUpload Then
		g_bAllowUpload = true
	End If
ElseIf g_pPlaceholder.XIsMultipurpose Then
	If g_strResType = IDS_RESTYPE_IMAGE And g_pPlaceholder.XAllowsImageUpload Then
		g_bAllowUpload = true
	ElseIf (g_strResType = IDS_RESTYPE_ATTACHMENT Or g_strResType = IDS_RESTYPE_VIDEO) And _
			g_pPlaceholder.XAllowsAttachmentUpload Then
		g_bAllowUpload = true
	End If
End If

If NOT g_bAllowUpload Then
	Response.Write(IDS_PLACEHOLDER_NO_UPLOAD)
	EndResponse
End If

' create the HttpUpload.UploadHandler object to handle the multipart form post
Dim g_pFormReader
Set g_pFormReader = GetInitializedFormReaderSafe( True )

' turns mutli-part post form data into a BlobURL
g_strBlobUrl = AcceptFileUpload( g_pFormReader, "NC_FileData" )

' retrieves resource properties set by user
g_strPhHRef = CStr(g_pFormReader.Form("NC_HRef"))
g_strPhAltText = CStr(g_pFormReader.Form("NC_AltText"))
g_strPhDispText = CStr(g_pFormReader.Form("NC_DispText"))
g_bAttachIcon = CBool(Request.QueryString(IDS_QPARAM_ATTACHICON))
g_bImg = CBool(Request.QueryString(IDS_QPARAM_IMG))


Server.ScriptTimeOut = g_lScriptTimeOut
Response.Buffer = g_bBuffer


' ---- [Private] Functions to accept binary upload from client, and return the resulting URL
Function AcceptFileUpload( pFormReader, strFormFieldName )
	Err.Clear()
	On Error Resume Next

	' figure out where to create the temporary file
	Dim strUploadTempDir

	strUploadTempDir = GetSCAUpLoadDirectory()
	If (Err.Number <> 0) Then
		Response.Write("<br><h3>There was an error while retrieving the upload file directory from Server Configuration Application.</h3>")
		Response.Write("<b>&nbsp;&nbsp;Error: </b><em>" & Err.Description & "</em>")
		Response.Write("<h4>&nbsp;&nbsp;<a href='javascript:history.back()'>Please try the uploading again.</a></h4>")
		EndResponse
	End If


	' the mime type suggested by the user
	Dim strSuggestedMime
	strSuggestedMime = pFormReader.Form("NC_Mime")

	' get HttpUpload.UploadHandler to accept file, which will create a file in the server
	Dim strTempFilename
	strTempFilename = pFormReader.Files(strFormFieldName).Save( strUploadTempDir, strSuggestedMime )
	If (Err.Number <> 0) Then
		Response.Write("NC_Mime: " & Server.HTMLEncode(pFormReader.Form("NC_Mime")) )
		Response.Write("<br><h3>There was an error while reading the uploaded file.</h3>")
		Response.Write("<b>&nbsp;&nbsp;Error: </b><em>" & Err.Description & "</em>")
		Response.Write("<h4>&nbsp;&nbsp;<a href='javascript:history.back()'>Please try the uploading again.</a></h4>")
		EndResponse
	End If

	' forward that to the ROM to turn the file into Blob managed resource
	Dim strURLToUploadedFile
	strURLToUploadedFile = AutoSession.AcceptBinaryFile( strTempFilename )
	If (Err.Number <> 0) Then
		Response.Write("<br><h3>There was an error inserting the file into the database.</h3>")
		Response.Write("<b>&nbsp;&nbsp;Error: </b><em>" & AutoSession.LastError.Description & "</em><br>")
		Response.Write("<small>Internal Details: " & AutoSession.LastError.InternalDetails & "</small>")
		Response.Write("<h4>&nbsp;&nbsp;<a href='javascript:history.back()'>Please try the uploading again.</a></h4>")
		EndResponse
	End If

	' finally, clean up the temporary file
	Dim objFileSystemObject
	set objFileSystemObject = CreateObject( "Scripting.FileSystemObject" )
	objFileSystemObject.DeleteFile( strTempFilename )

	' return the BlobURL
	AcceptFileUpload = strURLToUploadedFile
End function

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="expires" content="-1">
<meta name="Author" content="Microsoft Corporation.">
<meta name="Description" content="Process the uploaded desktop resource and add it to a	placeholder">
<meta name="robots" content="NOINDEX,NOFOLLOW">
<meta http-equiv="Content-Type" content="text/html;charset=<%= g_pThisPosting.Charset%>">
<title><%= IDS_UI_BROWSER_TITLE %></title>
<% Call EmbedWBCHeadSection %>
<script language="JavaScript"
		src="ResourceBrowserClient.js"
		type="text/javascript"></script>
<script language="JavaScript"
		src="/NR/System/WBC/Internals/WBCFuncPages/Cross-Browser/ResourceBrowser/Common/ResourceBrowserCommon.js"
		type="text/javascript"></script>
</head>

<body>

<form name="NC_LocalForm">

<%
    Dim pASPUtils
    Set pASPUtils = GetInitializedASPUtils()

    Dim lCodePage
	lCodePage = g_pThisPosting.CodePage

	If g_strResType = IDS_RESTYPE_IMAGE Then
		Dim strEncodedAltText
		strEncodedAltText = pASPUtils.HTMLEncode(g_strPhALTText)
%>

	<input name="NC_HRef" type="hidden" value="<%= Server.HTMLEncode(g_strPhHRef) %>">
	<input name="NC_AltText" type="hidden" value="<% Call pASPUtils.MBCSWrite( strEncodedAltText, lCodePage ) %>">
	<script language="JavaScript" type="text/javascript">
	<!--
		WBC_procsImage("<%= g_strPhName %>", "<%= g_strPhType %>", "<%= g_strBlobUrl %>");
	// -->
	</script>

<%	ElseIf g_strResType = IDS_RESTYPE_VIDEO Then %>

	<script language="JavaScript" type="text/javascript">
	<!--
		WBC_procsVideo("<%= g_strPhName %>", "<%= g_strPhType %>", "<%= g_strBlobUrl %>", <%= Bool2LCaseStr(g_bImg) %>);
	// -->
	</script>

<%
	ElseIf g_strResType = IDS_RESTYPE_ATTACHMENT Then
		If g_strPhDispText = "" Then
			g_strPhDispText = Mid(g_strBlobUrl, InstrRev(g_strBlobUrl, "/") + 1)
		End If

		Dim strEncodedDispText
		strEncodedDispText = pASPUtils.HTMLEncode(g_strPhDispText)
%>

	<input name="NC_DispText" type="hidden" value="<% Call pASPUtils.MBCSWrite( strEncodedDispText, lCodePage ) %>">
	<script language="JavaScript" type="text/javascript">
	<!--
		WBC_procsAttachment("<%= g_strPhName %>", "<%= g_strPhType %>", "<%= g_strBlobUrl %>", <%= Bool2LCaseStr(g_bAttachIcon) %>);
	// -->
	</script>

<% End If %>

</form>

</body>
</html>