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