95e6240fb536ee42691309a366002d551512bd74
[acontent.git] / docs / file_manager / edit.php
1 <?php\r
2 /************************************************************************/\r
3 /* AContent                                                             */\r
4 /************************************************************************/\r
5 /* Copyright (c) 2010                                                   */\r
6 /* Inclusive Design Institute                                           */\r
7 /*                                                                      */\r
8 /* This program is free software. You can redistribute it and/or        */\r
9 /* modify it under the terms of the GNU General Public License          */\r
10 /* as published by the Free Software Foundation.                        */\r
11 /************************************************************************/\r
12 \r
13 define('TR_INCLUDE_PATH', '../include/');\r
14 require_once(TR_INCLUDE_PATH.'vitals.inc.php');\r
15 require_once(TR_INCLUDE_PATH.'classes/FileUtility.class.php');\r
16 \r
17 global $_course_id;\r
18 Utility::authenticate(TR_PRIV_ISAUTHOR_OF_CURRENT_COURSE);\r
19 $editable_file_types = array('txt', 'html', 'htm', 'xml', 'css', 'asc', 'csv', 'sql');\r
20 \r
21 $current_path = TR_CONTENT_DIR.$_course_id.'/';\r
22 \r
23 $popup  = $_REQUEST['popup'];\r
24 $framed = $_REQUEST['framed'];\r
25 $file    = $_REQUEST['file'];\r
26 $pathext = $_REQUEST['pathext']; \r
27 \r
28 if (isset($_POST['cancel'])) {\r
29         $msg->addFeedback('CANCELLED');\r
30         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'framed='.$_POST['framed'].SEP.'popup='.$_POST['popup'].SEP.'_course_id='.$_course_id);\r
31         exit;\r
32 }\r
33 \r
34 if (isset($_POST['save'])) {\r
35         $content = str_replace("\r\n", "\n", $stripslashes($_POST['body_text']));\r
36         $file = $_POST['file'];\r
37 \r
38         if (FileUtility::course_realpath($current_path . $pathext . $file) == FALSE) {\r
39                 $msg->addError('FILE_NOT_SAVED');\r
40         } else {\r
41                 if (($f = @fopen($current_path.$pathext.$file, 'w')) && (@fwrite($f, $content) !== false) && @fclose($f)) {\r
42                         $msg->addFeedback(array('FILE_SAVED', $file));\r
43                         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'framed='.$_POST['framed'].SEP.'popup='.$_POST['popup'].SEP.'_course_id='.$_course_id);\r
44                         exit;\r
45                 } else {\r
46                         $msg->addError('FILE_NOT_SAVED');\r
47                 }\r
48         }\r
49         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'framed='.$_POST['framed'].SEP.'popup='.$_POST['popup'].SEP.'_course_id='.$_course_id);\r
50         exit;\r
51 }\r
52 \r
53 \r
54 $path_parts = pathinfo($current_path.$pathext.$file);\r
55 $ext = strtolower($path_parts['extension']);\r
56 \r
57 // open file to edit\r
58 $real = realpath($current_path . $pathext . $file);\r
59 \r
60 if (FileUtility::course_realpath($current_path . $pathext . $file) == FALSE) {\r
61         // error: File does not exist\r
62         $msg->addError('FILE_NOT_EXIST');\r
63         header('Location: index.php?pathext='.$pathext.SEP.'framed='.$framed.SEP.'popup='.$popup.SEP.'_course_id='.$_course_id);\r
64         exit;\r
65 } else if (is_dir($current_path.$pathext.$file)) {\r
66         // error: cannot edit folder\r
67         $msg->addError('BAD_FILE_TYPE');\r
68         header('Location: index.php?pathext='.$pathext.SEP.'framed='.$framed.SEP.'popup='.$popup.SEP.'_course_id='.$_course_id);\r
69         exit;\r
70 } else if (!is_readable($current_path.$pathext.$file)) {\r
71         // error: File cannot open file\r
72         $msg->addError(array('CANNOT_OPEN_FILE', $file));\r
73         header('Location: index.php?pathext='.$pathext.SEP.'framed='.$framed.SEP.'popup='.$popup.SEP.'_course_id='.$_course_id);\r
74         exit;\r
75 } else if (in_array($ext, $editable_file_types)) {\r
76         $_POST['body_text'] = file_get_contents($current_path.$pathext.$file);\r
77 } else {\r
78         //error: bad file type\r
79         $msg->addError('BAD_FILE_TYPE');\r
80         header('Location: index.php?pathext='.$pathext.SEP.'framed='.$framed.SEP.'popup='.$popup.SEP.'_course_id='.$_course_id);\r
81         exit;\r
82 }\r
83 \r
84 $onload = "on_load();";\r
85 require(TR_INCLUDE_PATH.'header.inc.php');\r
86 require(TR_INCLUDE_PATH.'lib/tinymce.inc.php');\r
87 \r
88 // load tinymce library\r
89 load_editor(true, false, "none");\r
90 \r
91 if (!isset($_POST['extension'])) {\r
92         if ($ext == 'html' || $ext == 'htm')\r
93                 $_POST['extension'] = 'html';\r
94         else\r
95                 $_POST['extension'] = 'txt';\r
96 }\r
97 ?>\r
98 \r
99 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">\r
100 <input type="hidden" name="pathext" value="<?php echo AT_print($pathext, 'input.hidden'); ?>" />\r
101 <input type="hidden" name="framed" value="<?php echo AT_print($framed, 'input.hidden'); ?>" />\r
102 <input type="hidden" name="popup" value="<?php echo AT_print($popup, 'input.hidden'); ?>" />\r
103 <input type="hidden" name="file" value="<?php echo AT_print($file, 'input.hidden'); ?>" />\r
104 <input type="hidden" name="_course_id" value="<?php echo AT_print($_course_id, 'input.hidden'); ?>" />\r
105 <input type="submit" name="submit" style="display:none;"/>\r
106 <div class="input-form">\r
107         <div class="row">\r
108                 <h3><?php echo AT_print($file, 'input.h3'); ?></h3>\r
109         </div>\r
110         <div class="row">\r
111                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><?php echo _AT('type'); ?><br />\r
112                 <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);" />\r
113                 <label for="text"><?php echo _AT('plain_text'); ?></label>\r
114 \r
115                 , <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);" />\r
116                 <label for="html"><?php echo _AT('html'); ?></label>\r
117         </div>\r
118         <div class="row">\r
119                 <label for="body_text"><?php echo _AT('body'); ?></label><br />\r
120                 <textarea  name="body_text" id="body_text" rows="25"><?php echo htmlspecialchars($_POST['body_text']); ?></textarea>\r
121         </div>\r
122 \r
123         <div class="row buttons">\r
124                 <input type="submit" name="save" value="<?php echo _AT('save'); ?>" accesskey="s" />\r
125                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />\r
126         </div>\r
127 </div>\r
128 </form>\r
129 \r
130 <script type="text/javascript" language="javascript">\r
131 //<!--\r
132 function on_load()\r
133 {\r
134         if (jQuery('#html').attr("checked")) { \r
135                 tinyMCE.execCommand('mceAddControl', false, 'body_text');\r
136         }\r
137 }\r
138 \r
139 trans.editor.switch_content_type = function (extension) {\r
140   if (extension === 'txt') { //text type\r
141     tinyMCE.execCommand('mceRemoveControl', false, 'body_text');\r
142   }\r
143   else { //html type\r
144     tinyMCE.execCommand('mceAddControl', false, 'body_text');\r
145   }\r
146 };\r
147 \r
148 //-->\r
149 </script>\r
150 <?php require(TR_INCLUDE_PATH.'footer.inc.php'); ?>\r