4525: Sanitize all input fields to disallow XSS. All the input field that most likel...
[acontent.git] / docs / file_manager / new.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 define('TR_INCLUDE_PATH', '../include/');
14 require(TR_INCLUDE_PATH.'vitals.inc.php');
15 require(TR_INCLUDE_PATH.'classes/FileUtility.class.php');
16
17 global $_course_id;
18 Utility::authenticate(TR_PRIV_ISAUTHOR_OF_CURRENT_COURSE);
19
20 $current_path = TR_CONTENT_DIR.$_course_id.'/';
21
22 $popup  = $_REQUEST['popup'];
23 $framed = $_REQUEST['framed'];
24
25
26 if (isset($_POST['cancel'])) {
27         $msg->addFeedback('CANCELLED');
28         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'framed='.$_POST['framed'].SEP.'popup='.$_POST['popup'].SEP.'_course_id='.$_course_id);
29         exit;
30 }
31
32 if (isset($_POST['submit_no'])) {
33         $msg->addFeedback('CANCELLED');
34         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'framed='.$_POST['framed'].SEP.'popup='.$_POST['popup'].SEP.'_course_id='.$_course_id);
35         exit;
36 }
37
38 if (isset($_POST['submit_yes'])) {
39         $filename = preg_replace("{[^a-zA-Z0-9_]}","_", trim($_POST['filename']));
40         $pathext  = $_POST['pathext'];
41
42         /* only html or txt extensions allowed */
43         if ($_POST['extension'] == 'html') {
44                 $extension = 'html';
45         } else {
46                 $extension = 'txt';
47         }
48         
49         if (FileUtility::course_realpath($current_path . $pathext . $filename.'.'.$extension) == FALSE) {
50                 $msg->addError('FILE_NOT_SAVED');
51                 /* take user to home page to avoid unspecified error warning */
52                 header('Location: index.php?pathext='.SEP.'framed='.$framed.SEP.'popup='.$popup.SEP.'_course_id='.$_course_id);
53                 exit;
54         }
55
56         if (($f = @fopen($current_path.$pathext.$filename.'.'.$extension,'w')) && @fwrite($f, stripslashes($_POST['body_text'])) !== FALSE && @fclose($f)){
57                 $msg->addFeedback('FILE_OVERWRITE');
58         } else {
59                 $msg->addError('CANNOT_OVERWRITE_FILE');
60         }
61         unset($_POST['newfile']);
62         header('Location: index.php?pathext='.$pathext.SEP.'framed='.$framed.SEP.'popup='.$popup.SEP.'_course_id='.$_course_id);
63         exit;
64 }
65
66 if (isset($_POST['savenewfile'])) {
67
68         if (isset($_POST['filename']) && ($_POST['filename'] != "")) {
69                 $filename     = preg_replace("{[^a-zA-Z0-9_]}","_", trim($_POST['filename']));
70                 $pathext      = $_POST['pathext'];
71                 $current_path = TR_CONTENT_DIR.$_course_id.'/';
72
73                 /* only html or txt extensions allowed */
74                 if ($_POST['extension'] == 'html') {
75                         $extension = 'html';
76                         $head_html = "<html>\n<head>\n<title>".$_POST['filename']."</title>\n<head>\n<body>";
77                         $foot_html ="\n</body>\n</html>";
78                 } else {
79                         $extension = 'txt';
80                 }
81
82                 if (!@file_exists($current_path.$pathext.$filename.'.'.$extension)) {
83                         $content = str_replace("\r\n", "\n", $head_html.$_POST['body_text'].$foot_html);
84                         
85                         if (FileUtility::course_realpath($current_path . $pathext . $filename.'.'.$extension) == FALSE) {
86                                 $msg->addError('FILE_NOT_SAVED');
87                                 /* take user to home page to avoid unspecified error warning */
88                                 header('Location: index.php?pathext='.SEP.'framed='.$framed.SEP.'popup='.$popup.SEP.'_course_id='.$_course_id);
89                                 exit;
90                         }
91
92                         if (($f = fopen($current_path.$pathext.$filename.'.'.$extension, 'w')) && (@fwrite($f, stripslashes($content)) !== false)  && (@fclose($f))) {
93                                 $msg->addFeedback(array('FILE_SAVED', $filename.'.'.$extension));
94                                 header('Location: index.php?pathext='.urlencode($_POST['pathext']).SEP.'popup='.$_POST['popup'].SEP.'_course_id='.$_course_id);
95                                 exit;
96                         } else {
97                                 $msg->addError('FILE_NOT_SAVED');
98                                 header('Location: index.php?pathext='.$pathext.SEP.'framed='.$framed.SEP.'popup='.$popup.SEP.'_course_id='.$_course_id);
99                                 exit;
100                         }
101                 }
102                 else {
103                         require(TR_INCLUDE_PATH.'header.inc.php');
104                         $pathext = $_POST['pathext']; 
105                         $popup   = $_POST['popup'];
106
107                         $_POST['newfile'] = "new";
108
109                         $hidden_vars['pathext']   = $pathext;
110                         $hidden_vars['filename']  = $filename;
111                         $hidden_vars['extension'] = $extension;
112                         $hidden_vars['_course_id'] = $_course_id;
113                         $hidden_vars['body_text'] = $_POST['body_text'];
114
115                         $hidden_vars['popup']  = $popup;
116                         $hidden_vars['framed'] = $framed;
117
118                         $msg->addConfirm(array('FILE_EXISTS', $filename.'.'.$extension), $hidden_vars);
119                         $msg->printConfirm();
120
121                         require(TR_INCLUDE_PATH.'footer.inc.php');
122                         exit;
123                 }
124         } else {
125                 $msg->addError(array('EMPTY_FIELDS', _AT('file_name')));
126         }
127 }
128
129 $onload="on_load()";
130
131 require(TR_INCLUDE_PATH.'header.inc.php');
132 require(TR_INCLUDE_PATH.'lib/tinymce.inc.php');
133
134 // set default body editor to tinymce editor
135 if (!isset($_POST['extension'])) $_POST['extension'] = 'html';
136
137 // load tinymce library
138 load_editor(true, false, "none");
139
140 $pathext = $_GET['pathext']; 
141 $popup   = $_GET['popup'];
142
143 $msg->printAll();
144
145 ?>
146         <form action="<?php echo $_SERVER['PHP_SELF'].'?_course_id='.$_course_id; ?>" method="post" name="form">
147         <input type="hidden" name="pathext" value="<?php echo $_REQUEST['pathext'] ?>" />
148         <input type="hidden" name="popup" value="<?php echo $popup; ?>" />
149
150         <div class="input-form">        
151         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('create_new_file'); ?></legend>
152                 <div class="row">
153                         <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="ctitle"><?php echo _AT('file_name');  ?></label><br />
154                         <input type="text" name="filename" id="ctitle" size="40" <?php if (isset($_POST['filename'])) echo 'value="'.AT_print($_POST['filename'], 'input.text').'"'?> />
155                 </div>
156
157                 <div class="row">
158                         <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><?php echo _AT('type'); ?><br />
159                         <input type="radio" name="extension" value="txt" id="text" <?php if ($_POST['extension'] == 'txt') { echo 'checked="checked"'; } ?> onclick="trans.editor.switch_content_type(this.value);" />
160                         <label for="text"><?php echo _AT('plain_text'); ?></label>
161         
162                         , <input type="radio" name="extension" value="html" id="html" <?php if ($_POST['extension'] == 'html') { echo 'checked="checked"'; } ?> onclick="trans.editor.switch_content_type(this.value);" />
163                         <label for="html"><?php echo _AT('html'); ?></label>
164                 </div>
165         
166                 <div class="row">
167                         <label for="body_text"><?php echo _AT('body');  ?></label><br />
168                         <textarea name="body_text" id="body_text" rows="25"><?php echo ContentManager::cleanOutput($_POST['body_text']); ?></textarea>
169                 </div>
170         
171                 <div class="row buttons">
172                         <input type="submit" name="savenewfile" value="<?php echo _AT('save'); ?>" accesskey="s" />
173                         <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>"  />             
174                 </div>
175         </fieldset>
176         </div>
177         </form>
178
179 <script type="text/javascript" language="javascript">
180 //<!--
181 function on_load()
182 {
183         document.form.filename.focus();
184         if (jQuery('#html').attr("checked")) { 
185                 tinyMCE.execCommand('mceAddControl', false, 'body_text');
186         }
187 }
188
189 trans.editor.switch_content_type = function (extension) {
190   if (extension === 'txt') { //text type
191     tinyMCE.execCommand('mceRemoveControl', false, 'body_text');
192   }
193   else { //html type
194     tinyMCE.execCommand('mceAddControl', false, 'body_text');
195   }
196 };
197
198 //-->
199 </script>
200
201 <?php require(TR_INCLUDE_PATH.'footer.inc.php'); ?>