remove old readme
[atutor.git] / docs / mods / _core / file_manager / edit.php
1 <?php\r
2 /****************************************************************/\r
3 /* ATutor                                                                                                               */\r
4 /****************************************************************/\r
5 /* Copyright (c) 2002-2010                                      */\r
6 /* Inclusive Design Institute                                   */\r
7 /* http://atutor.ca                                                                                             */\r
8 /*                                                              */\r
9 /* This program is free software. You can redistribute it and/or*/\r
10 /* modify it under the terms of the GNU General Public License  */\r
11 /* as published by the Free Software Foundation.                                */\r
12 /****************************************************************/\r
13 // $Id$\r
14 \r
15 define('AT_INCLUDE_PATH', '../../../include/');\r
16 require(AT_INCLUDE_PATH.'vitals.inc.php');\r
17 require_once(AT_INCLUDE_PATH.'../mods/_core/file_manager/filemanager.inc.php');\r
18 \r
19 \r
20 if (!authenticate(AT_PRIV_FILES,AT_PRIV_RETURN)) {\r
21         authenticate(AT_PRIV_CONTENT);\r
22 }\r
23 \r
24 $current_path = AT_CONTENT_DIR.$_SESSION['course_id'].'/';\r
25 \r
26 $popup  = $_REQUEST['popup'];\r
27 $framed = $_REQUEST['framed'];\r
28 $file    = $_REQUEST['file'];\r
29 $pathext = $_REQUEST['pathext']; \r
30 \r
31 if (isset($_POST['cancel'])) {\r
32         $msg->addFeedback('CANCELLED');\r
33         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'framed='.$_POST['framed'].SEP.'popup='.$_POST['popup']);\r
34         exit;\r
35 }\r
36 \r
37 if (isset($_POST['save'])) {\r
38         $content = str_replace("\r\n", "\n", $stripslashes($_POST['body_text']));\r
39         $file = $_POST['file'];\r
40 \r
41         if (course_realpath($current_path . $pathext . $file) == FALSE) {\r
42                 $msg->addError('FILE_NOT_SAVED');\r
43         } else {\r
44                 if (($f = @fopen($current_path.$pathext.$file, 'w')) && (@fwrite($f, $content) !== false) && @fclose($f)) {\r
45                         $msg->addFeedback(array('FILE_SAVED', $file));\r
46                         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'framed='.$_POST['framed'].SEP.'popup='.$_POST['popup']);\r
47                         exit;\r
48                 } else {\r
49                         $msg->addError('FILE_NOT_SAVED');\r
50                 }\r
51         }\r
52         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'framed='.$_POST['framed'].SEP.'popup='.$_POST['popup']);\r
53         exit;\r
54 }\r
55 \r
56 \r
57 $path_parts = pathinfo($current_path.$pathext.$file);\r
58 $ext = strtolower($path_parts['extension']);\r
59 \r
60 // open file to edit\r
61 $real = realpath($current_path . $pathext . $file);\r
62 \r
63 if (course_realpath($current_path . $pathext . $file) == FALSE) {\r
64         // error: File does not exist\r
65         $msg->addError('FILE_NOT_EXIST');\r
66         header('Location: index.php?pathext='.$pathext.SEP.'framed='.$framed.SEP.'popup='.$popup);\r
67         exit;\r
68 } else if (is_dir($current_path.$pathext.$file)) {\r
69         // error: cannot edit folder\r
70         $msg->addError('BAD_FILE_TYPE');\r
71         header('Location: index.php?pathext='.$pathext.SEP.'framed='.$framed.SEP.'popup='.$popup);\r
72         exit;\r
73 } else if (!is_readable($current_path.$pathext.$file)) {\r
74         // error: File cannot open file\r
75         $msg->addError(array('CANNOT_OPEN_FILE', $file));\r
76         header('Location: index.php?pathext='.$pathext.SEP.'framed='.$framed.SEP.'popup='.$popup);\r
77         exit;\r
78 } else if (in_array($ext, $editable_file_types)) {\r
79         $_POST['body_text'] = file_get_contents($current_path.$pathext.$file);\r
80 } else {\r
81         //error: bad file type\r
82         $msg->addError('BAD_FILE_TYPE');\r
83         header('Location: index.php?pathext='.$pathext.SEP.'framed='.$framed.SEP.'popup='.$popup);\r
84         exit;\r
85 }\r
86 \r
87 require(AT_INCLUDE_PATH.'header.inc.php');\r
88 require(AT_INCLUDE_PATH.'lib/tinymce.inc.php');\r
89 \r
90 if (!isset($_REQUEST['setvisual']) && !isset($_REQUEST['settext'])) {\r
91         if ($_SESSION['prefs']['PREF_CONTENT_EDITOR'] == 1) {\r
92                 $_POST['formatting'] = 1;\r
93                 $_REQUEST['settext'] = 0;\r
94                 $_REQUEST['setvisual'] = 0;\r
95 \r
96         } else if ($_SESSION['prefs']['PREF_CONTENT_EDITOR'] == 2) {\r
97                 $_POST['formatting'] = 1;\r
98                 $_POST['settext'] = 0;\r
99                 $_POST['setvisual'] = 1;\r
100 \r
101         } else { // else if == 0\r
102                 $_POST['formatting'] = 0;\r
103                 $_REQUEST['settext'] = 0;\r
104                 $_REQUEST['setvisual'] = 0;\r
105         }\r
106 }\r
107 if (($_POST['setvisual'] && !$_POST['settext']) || $_GET['setvisual']) {\r
108         load_editor(false, 'body_text');\r
109 }\r
110 \r
111 \r
112 ?>\r
113 \r
114 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">\r
115 <input type="hidden" name="pathext" value="<?php echo $pathext; ?>" />\r
116 <input type="hidden" name="framed"  value="<?php echo $framed; ?>" />\r
117 <input type="hidden" name="popup"   value="<?php echo $popup; ?>" />\r
118 <input type="hidden" name="file"    value="<?php echo $file; ?>" />\r
119 <input type="submit" name="submit" style="display:none;"/>\r
120 <div class="input-form">\r
121         <div class="row">\r
122                 <h3><?php echo $file; ?></h3>\r
123         </div>\r
124                 <div class="row">\r
125                 <?php\r
126                         if (($_POST['setvisual'] && !$_POST['settext']) || $_GET['setvisual']){\r
127                                 echo '<input type="hidden" name="setvisual" value="'.$_POST['setvisual'].'" />';\r
128                                 echo '<input type="submit" name="settext" value="'._AT('switch_text').'" />';\r
129                         } else {\r
130                                 echo '<input type="submit" name="setvisual" value="'._AT('switch_visual').'" />';\r
131                         }\r
132                 ?>\r
133         </div>\r
134         <div class="row">\r
135                 <label for="body_text"><?php echo _AT('body'); ?></label><br />\r
136                 <textarea  name="body_text" id="body_text" rows="25"><?php echo htmlspecialchars($_POST['body_text']); ?></textarea>\r
137         </div>\r
138 \r
139         <div class="row buttons">\r
140                 <input type="submit" name="save" value="<?php echo _AT('save'); ?>" accesskey="s" />\r
141                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />\r
142         </div>\r
143 </div>\r
144 </form>\r
145 \r
146 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>