2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2010 */
6 /* Inclusive Design Institute */
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 /************************************************************************/
13 define('TR_INCLUDE_PATH', '../../include/');
14 require(TR_INCLUDE_PATH.'vitals.inc.php');
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>';
28 trans.editor.insertErrorMsg = function (errorString) {
29 jQuery("#subnavlistcontainer", window.opener.document).before(errorStringPrefix + errorString + errorStringSuffix);
32 trans.editor.removeErrorMsg = function () {
33 jQuery("#error", window.opener.document).remove();
36 trans.editor.testStuff = function () {
37 var body = trans.editor.stuff;
38 body = body.replace(/\\/g,"");
40 jQuery("#body_text", window.opener.document).val(body);
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);
50 jQuery("#body_text", window.opener.document).val(body);
54 jQuery("#ctitle",window.opener.document).val(title);
57 jQuery("#head", window.opener.document).html(head);
58 jQuery("#use_customized_head", window.opener.document).attr("checked", true);
72 private $errorMsg = "";
74 public function getTitle() {
78 public function setTitle($value) {
79 $this->title = $value;
82 public function getHead() {
86 public function setHead($value) {
90 public function getBody() {
94 public function setBody($value) {
98 public function getErrorMsg() {
99 return $this->errorMsg;
102 public function setErrorMsg($value) {
103 $this->errorMsg = $value;
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.
114 * @return FileData object
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') ) {
123 $path_parts = pathinfo($_FILES['uploadedfile_paste']['name']);
124 $ext = strtolower($path_parts['extension']);
126 if (in_array($ext, array('html', 'htm'))) {
127 $contents = file_get_contents($_FILES['uploadedfile_paste']['tmp_name']);
129 /* get the <title></title> of this page */
130 $start_pos = strpos(strtolower($contents), '<title>');
131 $end_pos = strpos(strtolower($contents), '</title>');
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)));
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'])));
146 $fileData->setErrorMsg(_AT('TR_ERROR_BAD_FILE_TYPE'));
151 if (isset($_POST['submit_file']))
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()).');';
160 echo 'trans.editor.insertErrorMsg("'.$errorMessage.'");';
162 echo "window.close();";
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" />