remove old readme
[atutor.git] / docs / mods / _core / file_manager / new.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2010                                      */
6 /* Inclusive Design Institute                                   */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id$
14
15 define('AT_INCLUDE_PATH', '../../../include/');
16 require(AT_INCLUDE_PATH.'vitals.inc.php');
17 require_once(AT_INCLUDE_PATH.'../mods/_core/file_manager/filemanager.inc.php');
18
19 if (!authenticate(AT_PRIV_FILES,AT_PRIV_RETURN)) {
20         authenticate(AT_PRIV_CONTENT);
21 }
22
23
24 $current_path = AT_CONTENT_DIR.$_SESSION['course_id'].'/';
25
26 $popup  = $_REQUEST['popup'];
27 $framed = $_REQUEST['framed'];
28
29
30 if (isset($_POST['cancel'])) {
31         $msg->addFeedback('CANCELLED');
32         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'framed='.$_POST['framed'].SEP.'popup='.$_POST['popup']);
33         exit;
34 }
35
36 if (isset($_POST['submit_no'])) {
37         $msg->addFeedback('CANCELLED');
38         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'framed='.$_POST['framed'].SEP.'popup='.$_POST['popup']);
39         exit;
40 }
41
42 if (isset($_POST['submit_yes'])) {
43         $filename = preg_replace("{[^a-zA-Z0-9_]}","_", trim($_POST['filename']));
44         $pathext  = $_POST['pathext'];
45
46         /* only html or txt extensions allowed */
47         if ($_POST['extension'] == 'html') {
48                 $extension = 'html';
49         } else {
50                 $extension = 'txt';
51         }
52         
53         if (course_realpath($current_path . $pathext . $filename.'.'.$extension) == FALSE) {
54                 $msg->addError('FILE_NOT_SAVED');
55                 /* take user to home page to avoid unspecified error warning */
56                 header('Location: index.php?pathext='.SEP.'framed='.$framed.SEP.'popup='.$popup);
57                 exit;
58         }
59
60         if (($f = @fopen($current_path.$pathext.$filename.'.'.$extension,'w')) && @fwrite($f, stripslashes($_POST['body_text'])) !== FALSE && @fclose($f)){
61                 $msg->addFeedback('FILE_OVERWRITE');
62         } else {
63                 $msg->addError('CANNOT_OVERWRITE_FILE');
64         }
65         unset($_POST['newfile']);
66         header('Location: index.php?pathext='.$pathext.SEP.'framed='.$framed.SEP.'popup='.$popup);
67         exit;
68 }
69
70 if (isset($_POST['savenewfile'])) {
71
72         if (isset($_POST['filename']) && ($_POST['filename'] != "")) {
73                 $filename     = preg_replace("{[^a-zA-Z0-9_]}","_", trim($_POST['filename']));
74                 $pathext      = $_POST['pathext'];
75                 $current_path = AT_CONTENT_DIR.$_SESSION['course_id'].'/';
76
77                 /* only html or txt extensions allowed */
78                 if ($_POST['extension'] == 'html') {
79                         $extension = 'html';
80                         $head_html = "<html>\n<head>\n<title>".$_POST['filename']."</title>\n<head>\n<body>";
81                         $foot_html ="\n</body>\n</html>";
82                 } else {
83                         $extension = 'txt';
84                 }
85
86                 if (!@file_exists($current_path.$pathext.$filename.'.'.$extension)) {
87                         $content = str_replace("\r\n", "\n", $head_html.$_POST['body_text'].$foot_html);
88                         
89                         if (course_realpath($current_path . $pathext . $filename.'.'.$extension) == FALSE) {
90                                 $msg->addError('FILE_NOT_SAVED');
91                                 /* take user to home page to avoid unspecified error warning */
92                                 header('Location: index.php?pathext='.SEP.'framed='.$framed.SEP.'popup='.$popup);
93                                 exit;
94                         }
95
96                         if (($f = fopen($current_path.$pathext.$filename.'.'.$extension, 'w')) && (@fwrite($f, stripslashes($content)) !== false)  && (@fclose($f))) {
97                                 $msg->addFeedback(array('FILE_SAVED', $filename.'.'.$extension));
98                                 header('Location: index.php?pathext='.urlencode($_POST['pathext']).SEP.'popup='.$_POST['popup']);
99                                 exit;
100                         } else {
101                                 $msg->addError('FILE_NOT_SAVED');
102                                 header('Location: index.php?pathext='.$pathext.SEP.'framed='.$framed.SEP.'popup='.$popup);
103                                 exit;
104                         }
105                 }
106                 else {
107                         require(AT_INCLUDE_PATH.'header.inc.php');
108                         $pathext = $_POST['pathext']; 
109                         $popup   = $_POST['popup'];
110
111                         $_POST['newfile'] = "new";
112
113                         $hidden_vars['pathext']   = $pathext;
114                         $hidden_vars['filename']  = $filename;
115                         $hidden_vars['extension'] = $extension;
116                         $hidden_vars['body_text'] = $_POST['body_text'];
117
118                         $hidden_vars['popup']  = $popup;
119                         $hidden_vars['framed'] = $framed;
120
121                         $msg->addConfirm(array('FILE_EXISTS', $filename.'.'.$extension), $hidden_vars);
122                         $msg->printConfirm();
123
124                         require(AT_INCLUDE_PATH.'footer.inc.php');
125                         exit;
126                 }
127         } else {
128                 $msg->addError(array('EMPTY_FIELDS', _AT('file_name')));
129         }
130 }
131
132 $onload="on_load()";
133
134 require(AT_INCLUDE_PATH.'header.inc.php');
135 require(AT_INCLUDE_PATH.'lib/tinymce.inc.php');
136
137 if (!isset($_REQUEST['setvisual']) && !isset($_REQUEST['settext'])) {
138         if ($_SESSION['prefs']['PREF_CONTENT_EDITOR'] == 1) {
139                 $_POST['formatting'] = 1;
140                 $_REQUEST['settext'] = 0;
141                 $_REQUEST['setvisual'] = 0;
142
143         } else if ($_SESSION['prefs']['PREF_CONTENT_EDITOR'] == 2) {
144                 $_POST['formatting'] = 1;
145                 $_POST['settext'] = 0;
146                 $_POST['setvisual'] = 1;
147
148         } else { // else if == 0
149                 $_POST['formatting'] = 0;
150                 $_REQUEST['settext'] = 0;
151                 $_REQUEST['setvisual'] = 0;
152         }
153 }
154
155 // load tinymce library
156 load_editor(false, false, "none");
157
158 $pathext = $_GET['pathext']; 
159 $popup   = $_GET['popup'];
160
161 $msg->printAll();
162 if (!$_POST['extension']) {
163         $_POST['extension'] = 'txt';
164         $_POST['formatting'] = 0;
165 }else if($_POST['extension'] == "html"){
166         $_POST['formatting'] = 1;
167 }
168
169 ?>
170         <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
171         <input type="hidden" name="pathext" value="<?php echo $_REQUEST['pathext'] ?>" />
172         <input type="hidden" name="popup" value="<?php echo $popup; ?>" />
173
174         <div class="input-form">        
175         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('create_new_file'); ?></legend>
176                 <div class="row">
177                         <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><label for="ctitle"><?php echo _AT('file_name');  ?></label><br />
178                         <input type="text" name="filename" id="ctitle" size="40" <?php if (isset($_POST['filename'])) echo 'value="'.$_POST['filename'].'"'?> />
179                 </div>
180
181                 <div class="row">
182                         <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><?php echo _AT('type'); ?><br />
183                         <input type="radio" name="extension" value="txt" id="text" <?php if ($_POST['formatting'] == 0) { echo 'checked="checked"'; } ?> onclick="javascript: document.form.setvisualbutton.disabled=true;" <?php if ($_POST['setvisual'] && !$_POST['settext']) { echo 'disabled="disabled"'; } ?> />
184                         <label for="text"><?php echo _AT('plain_text'); ?></label>
185         
186                         , <input type="radio" name="extension" value="html" id="html" <?php if ($_POST['formatting'] ==1 || $_POST['setvisual']) { echo 'checked="checked"'; } ?> onclick="javascript: document.form.setvisualbutton.disabled=false;"/>
187                         <label for="html"><?php echo _AT('html'); ?></label>
188         
189                         <input type="hidden" name="setvisual" value="<?php echo $_POST['setvisual']; ?>" />
190                         <input type="hidden" name="settext" value="<?php echo $_POST['settext']; ?>" />
191                         <input type="button" name="setvisualbutton" value="<?php echo _AT('switch_visual'); ?>" onClick="switch_body_editor()" />
192                 </div>
193         
194                 <div class="row">
195                         <label for="body_text"><?php echo _AT('body');  ?></label><br />
196                         <textarea name="body_text" id="body_text" rows="25"><?php echo ContentManager::cleanOutput($_POST['body_text']); ?></textarea>
197                 </div>
198         
199                 <div class="row buttons">
200                         <input type="submit" name="savenewfile" value="<?php echo _AT('save'); ?>" accesskey="s" />
201                         <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>"  />             
202                 </div>
203         </fieldset>
204         </div>
205         </form>
206
207         <script type="text/javascript" language="javascript">
208         //<!--
209         function on_load()
210         {
211                 if (document.getElementById("text").checked)
212                         document.form.setvisualbutton.disabled = true;
213                         
214                 if (document.form.setvisual.value==1)
215                 {
216                         tinyMCE.execCommand('mceAddControl', false, 'body_text');
217                         document.form.extension[0].disabled = "disabled";
218                         document.form.setvisualbutton.value = "<?php echo _AT('switch_text'); ?>";
219                 }
220                 else
221                 {
222                         document.form.setvisualbutton.value = "<?php echo _AT('switch_visual'); ?>";
223                 }
224         }
225         
226         // switch between text, visual editor for "body text"
227         function switch_body_editor()
228         {
229                 if (document.form.setvisualbutton.value=="<?php echo _AT('switch_visual'); ?>")
230                 {
231                         tinyMCE.execCommand('mceAddControl', false, 'body_text');
232                         document.form.setvisual.value=1;
233                         document.form.settext.value=0;
234                         document.form.extension[0].disabled = "disabled";
235                         document.form.setvisualbutton.value = "<?php echo _AT('switch_text'); ?>";
236                 }
237                 else
238                 {
239                         tinyMCE.execCommand('mceRemoveControl', false, 'body_text');
240                         document.form.setvisual.value=0;
241                         document.form.settext.value=1;
242                         document.form.extension[0].disabled = "";
243                         document.form.setvisualbutton.value = "<?php echo _AT('switch_visual'); ?>";
244                 }
245         }
246         //-->
247         </script>
248
249 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>