remove old readme
[atutor.git] / docs / mods / _core / editor / editor_tabs / pastefromfile.php
1 <?php
2 define('AT_INCLUDE_PATH', '../../../../include/');
3 require(AT_INCLUDE_PATH.'vitals.inc.php');
4 ?>
5 <html>
6     <head>
7         <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
8         <title>Paste from file tool</title>
9         <script src="<?php echo $_base_path; ?>jscripts/infusion/InfusionAll.js" type="text/javascript"></script>
10         <script src="<?php echo $this->base_path; ?>jscripts/ATutor.js" type="text/javascript"></script>
11         <script type="text/javascript">
12         ATutor = ATutor || {};
13         ATutor.mods = ATutor.mods || {};
14         ATutor.mods.editor = ATutor.mods.editor || {};
15
16         var errorStringPrefix = '<div id="error"><h4><?php echo _AT('the_follow_errors_occurred'); ?></h4><ul><li>';      
17         var errorStringSuffix = '</li></ul></div>';      
18
19         (function () {
20             
21             ATutor.mods.editor.insertErrorMsg = function (errorString) {
22                 jQuery("#subnavlistcontainer", window.opener.document).before(errorStringPrefix + errorString + errorStringSuffix);    
23             };
24
25             ATutor.mods.editor.removeErrorMsg = function () {
26                 jQuery("#error", window.opener.document).remove();
27             };
28
29             ATutor.mods.editor.testStuff = function () {
30                 var body = ATutor.mods.editor.stuff;
31                 body = body.replace(/\\/g,"");
32                 alert(body);
33                 jQuery("#body_text", window.opener.document).val(body);
34                                 
35             };
36             
37             ATutor.mods.editor.pasteFromFile = function (body, title, head) {
38                 body = body.replace(/\\/g,"");
39                 if (jQuery("#html", window.opener.document).attr("checked") && 
40                    (<?php echo $_SESSION['prefs']['PREF_CONTENT_EDITOR']; ?> !== 1)) {
41                         window.opener.tinyMCE.activeEditor.setContent(body);
42                 } else {  
43                     jQuery("#body_text", window.opener.document).val(body);
44                 }
45                 
46                 if (title != "") {
47                     jQuery("#ctitle",window.opener.document).val(title);
48                 }
49                 if (head != "") {
50                     jQuery("#head", window.opener.document).html(head);
51                     jQuery("#use_customized_head", window.opener.document).attr("checked", true);
52                 }
53             };
54         })();
55         </script>
56     </head>
57 <?php
58
59 class FileData
60 {
61     
62     private $title = "";
63     private $head = "";
64     private $body = "";
65     private $errorMsg = "";
66     
67     public function getTitle() {
68         return $this->title;
69     }
70     
71     public function setTitle($value) {
72         $this->title = $value;
73     }
74
75     public function getHead() {
76         return $this->head;
77     }
78     
79     public function setHead($value) {
80         $this->head = $value;
81     }
82       
83     public function getBody() {
84         return $this->body;
85     }
86     
87     public function setBody($value) {
88         $this->body = $value;
89     }
90
91     public function getErrorMsg() {
92         return $this->errorMsg;
93     }
94     
95     public function setErrorMsg($value) {
96         $this->errorMsg = $value;
97     }
98     
99 }
100
101 /**
102  * Paste_from_file
103  * Parses a named uploaded file of html or txt type
104  * The function identifies title, head and body for html files,
105  * or body for text files.
106  * 
107  * @return FileData object
108  */
109 function paste_from_file() {
110     $fileData = new FileData();
111     if ($_FILES['uploadedfile_paste']['name'] == '') {
112         $fileData->setErrorMsg(_AT('AT_ERROR_FILE_NOT_SELECTED'));
113     } elseif (($_FILES['uploadedfile_paste']['type'] == 'text/plain')
114             || ($_FILES['uploadedfile_paste']['type'] == 'text/html') ) {
115
116         $path_parts = pathinfo($_FILES['uploadedfile_paste']['name']);
117         $ext = strtolower($path_parts['extension']);
118
119         if (in_array($ext, array('html', 'htm'))) {
120             $contents = file_get_contents($_FILES['uploadedfile_paste']['tmp_name']);
121
122             /* get the <title></title> of this page             */
123             $start_pos  = strpos(strtolower($contents), '<title>');
124             $end_pos    = strpos(strtolower($contents), '</title>');
125
126             if (($start_pos !== false) && ($end_pos !== false)) {
127                 $start_pos += strlen('<title>');
128                 $fileData->setTitle(trim(substr($contents, $start_pos, $end_pos-$start_pos)));
129             }
130             unset($start_pos);
131             unset($end_pos);
132
133             $fileData->setHead(trim(get_html_head_by_tag($contents, array("link", "style", "script"))));
134             $fileData->setBody(trim(get_html_body($contents))); 
135         } else if ($ext == 'txt') {
136             $fileData->setBody(trim(file_get_contents($_FILES['uploadedfile_paste']['tmp_name'])));
137         } 
138      } else {
139         $fileData->setErrorMsg(_AT('AT_ERROR_BAD_FILE_TYPE'));
140      }
141      return $fileData;
142 }
143
144 if (isset($_POST['submit_file']))
145 {
146         echo '<script type="text/javascript">';
147     echo 'ATutor.mods.editor.removeErrorMsg();';
148         $fileData = paste_from_file();
149         $errorMessage = $fileData->getErrorMsg();
150         if ($errorMessage == "") {
151         echo 'ATutor.mods.editor.pasteFromFile('.json_encode($fileData->getBody()).','.json_encode($fileData->getTitle()).','.json_encode($fileData->getHead()).');';
152     } else {
153        echo 'ATutor.mods.editor.insertErrorMsg("'.$errorMessage.'");';
154     }
155     echo "window.close();";
156         echo '</script>';
157 }
158
159 ?>
160     <body>
161         <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form" enctype="multipart/form-data">
162                <input type="file" name="uploadedfile_paste" id="uploadedfile" class="formfield" size="20" />
163            <input type="submit" name="submit_file" id="submit_file" value="<?php echo _AT('paste'); ?>" class="button" />
164         </form>
165     </body>
166 </html>