<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{

    function stripslashes_array($data) {
	    if (is_array($data)){
            foreach ($data as $key => $value){
          		$data[$key] = stripslashes_array($value);
        	}
        	return $data;
   		}else{
 		    return stripslashes($data);
   		}
    }
	//Prevent Magic Quotes from affecting scripts, regardless of server settings

	//Make sure when reading file data,
	//PHP doesn't "magically" mangle backslashes!

	set_magic_quotes_runtime(FALSE);

	if (get_magic_quotes_gpc()) {
       /*
       All these global variables are slash-encoded by default,
       because    magic_quotes_gpc is set by default!
       (And magic_quotes_gpc affects more than just $_GET, $_POST, and $_COOKIE)
       */
       $_SERVER = stripslashes_array($_SERVER);
       $_GET = stripslashes_array($_GET);
       $_POST = stripslashes_array($_POST);
       $_COOKIE = stripslashes_array($_COOKIE);
       $_FILES = stripslashes_array($_FILES);
       $_ENV = stripslashes_array($_ENV);
       $_REQUEST = stripslashes_array($_REQUEST);
       $HTTP_SERVER_VARS = stripslashes_array($HTTP_SERVER_VARS);
       $HTTP_GET_VARS = stripslashes_array($HTTP_GET_VARS);
       $HTTP_POST_VARS = stripslashes_array($HTTP_POST_VARS);
       $HTTP_COOKIE_VARS = stripslashes_array($HTTP_COOKIE_VARS);
       $HTTP_POST_FILES = stripslashes_array($HTTP_POST_FILES);
       $HTTP_ENV_VARS = stripslashes_array($HTTP_ENV_VARS);
       if (isset($_SESSION)) {    #These are unconfirmed (?)
          $_SESSION = stripslashes_array($_SESSION, '');
          $HTTP_SESSION_VARS = stripslashes_array($HTTP_SESSION_VARS, '');
       }
       /*
       The $GLOBALS array is also slash-encoded, but when all the above are
       changed, $GLOBALS is updated to reflect those changes.  (Therefore
       $GLOBALS should never be modified directly).  $GLOBALS also contains
       infinite recursion, so it's dangerous...
       */
    }
	$filename = stripslashes($_POST[txtFileName]);
	$filepath = "uploadsDir/templatefiles/" . $filename;
	$contentText = $_POST[contentSrc];
	$htmlStr = "<html><head><title>" . $filename . "</title></head><body>" . $contentText . "</body></html>";
	$handle = fopen($filepath, "x+");
	fwrite($handle, $htmlStr);
	fclose($handle);
    $scripts = "<script language='javascript'>alert('Template Saved Successfully...'); window.close();</script>";
    echo $scripts;
}
?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Save Template</title>
    <link href="EditorStyle.css" rel="stylesheet" type="text/css" />

    <script language="javascript">
        function chkFile()
        {
            var filname = document.getElementById("txtFileName").value;
            if(filname == "")
            {
                alert("Please enter a file name");
                return false;
            }    
            else
            {
                if(filname.lastIndexOf(".htm") == -1 && filname.lastIndexOf(".HTM") == -1 && filname.lastIndexOf(".html") == -1 && filname.lastIndexOf(".HTML") == -1)
                {
                    alert("Please enter file name with .htm or .html extension");
                    return false;
                }
            }            
        }
        
        function setText()
        {
            document.getElementById("contentSrc").value = window.opener.editFrame.document.body.innerHTML;
        }
    </script>
</head>

<body topmargin="0" onload="setText()" bottommargin="0" rightmargin="0" leftmargin="0" class='txtStyle'
    bgcolor='#99ccff'>
    <form name="form1" method="post">
   
    <table align="Center" cellpadding="2" cellspacing="2">
        <tr>

               <td colspan="2" align="center">
                   <b>SAVE TEMPLATE</b>
               </td>

        </tr>
        <tr>
            <td colspan="2" align="center">
                &nbsp;
            </td>
        </tr>
        <tr>
            <td align="left" class="txtStyle">
                Template File Name:
            </td>
            <td>
				<input type="text" id="txtFileName" name="txtFileName" class="txtStyle" width="214">
            </td>
        </tr>
        <tr>
            <td colspan="2" class="txtStyle">
                &nbsp;
            </td>
        </tr>
        <tr>
            <td colspan="2" align="right">
				<input type="submit" id="btnOK" class="buttonStyle2" value="Save" onClick="return chkFile()">
                &nbsp;
                <input type="button" id="btnCancel" class="buttonStyle2" value="Cancel" onclick="window.close()" />
            </td>
        </tr>
    </table>
     <input id="contentSrc" name="contentSrc" type="hidden" />
    </form>
</body>
</html>
