63a0c6e471a4a18a6dd2bf46fb50765fd160f205
[acontent.git] / docs / home / editor / editor_tab_functions.inc.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 if (!defined('TR_INCLUDE_PATH')) { exit; }
14
15 function in_array_cin($strItem, $arItems)
16 {
17    foreach ($arItems as $key => $strValue)
18    {
19        if (strtoupper($strItem) == strtoupper($strValue))
20        {
21                    return $key;
22        }
23    }
24    return false;
25
26
27
28 function get_tabs() {
29         //these are the _AT(x) variable names and their include file
30         /* tabs[tab_id] = array(tab_name, file_name,                accesskey) */
31         $tabs[0] = array('content',                     'edit.inc.php',          'n');
32         $tabs[1] = array('metadata',                    'properties.inc.php',    'p');
33         $tabs[2] = array('alternative_content', 'alternatives.inc.php',  'l');  
34         $tabs[3] = array('tests',               'tests.inc.php',         't');  
35         
36         return $tabs;
37 }
38
39
40 function output_tabs($current_tab, $changes) {
41         global $_base_path;
42         $tabs = get_tabs();
43         $num_tabs = count($tabs);
44 ?>
45         <table class="etabbed-table" border="0" cellpadding="0" cellspacing="0" width="95%">
46         <tr>            
47                 <?php 
48                 for ($i=0; $i < $num_tabs; $i++): 
49                         if ($current_tab == $i):?>
50                                 <td class="editor_tab_selected">
51                                         <?php if ($changes[$i]): ?>
52                                                 <img src="<?php echo $_base_path; ?>images/changes_bullet.gif" alt="<?php echo _AT('usaved_changes_made'); ?>" height="12" width="15" />
53                                         <?php endif; ?>
54                                         <?php echo _AT($tabs[$i][0]); ?>
55                                 </td>
56                                 <td class="tab-spacer">&nbsp;</td>
57                         <?php else: ?>
58                                 <td class="editor_tab">
59                                         <?php if ($changes[$i]): ?>
60                                                 <img src="<?php echo $_base_path; ?>images/changes_bullet.gif" alt="<?php echo _AT('usaved_changes_made'); ?>" height="12" width="15" />
61                                         <?php endif; ?>
62
63                                         <?php echo '<input type="submit" name="button_'.$i.'" value="'._AT($tabs[$i][0]).'" title="'._AT($tabs[$i][0]).' - alt '.$tabs[$i][2].'" class="editor_buttontab" accesskey="'.$tabs[$i][2].'" onmouseover="this.style.cursor=\'pointer\';" '.$clickEvent.' />'; ?>
64                                 </td>
65                                 <td class="tab-spacer">&nbsp;</td>
66                         <?php endif; ?>
67                 <?php endfor; ?>
68                 <td >&nbsp;</td>
69         </tr>
70         </table>
71 <?php }
72 /**
73  * Strips all tags and encodes special characters in the URL
74  * Returns false if the URL is invalid
75  * 
76  * @param string $url
77  * @return mixed - returns a stripped and encoded URL or false if URL is invalid
78  */
79 function isValidURL($url) {
80     if (substr($url,0,4) === 'http') {
81         return filter_var(filter_var($url, FILTER_SANITIZE_STRING), FILTER_VALIDATE_URL);
82     }
83     return false;
84 }
85
86 /*
87  * Parse the primary resources out of the content and save into db.
88  * Clean up the removed primary resources from db.
89  * @param: $cid: content id
90  * @param: $content
91  * @return: none
92  */
93 function populate_a4a($cid, $content, $formatting){
94         global $my_files, $content_base_href, $contentManager;
95         
96         // Defining alternatives is only available for content type "html".
97         // But don't clean up the a4a tables at other content types in case the user needs them back at html.
98         if ($formatting <> 1) return;
99
100         include_once(TR_INCLUDE_PATH.'classes/A4a/A4a.class.php');
101         include_once(TR_INCLUDE_PATH.'classes/XML/XML_HTMLSax/XML_HTMLSax.php');        /* for XML_HTMLSax */
102         include_once(TR_INCLUDE_PATH.'classes/ContentOutputParser.class.php');  /* for parser */
103         
104         // initialize content_base_href; used in format_content
105         if (!isset($content_base_href)) {
106                 $content_row = $contentManager->getContentPage($cid);
107                 // return if the cid is not found
108                 if (!is_array($content_row)) {
109                         return;
110                 }
111                 $content_base_href = $content_row["content_path"].'/';
112         }
113         
114         $body = ContentUtility::formatContent($content, $formatting);
115     
116         $handler = new ContentOutputParser();
117         $parser = new XML_HTMLSax();
118         $parser->set_object($handler);
119         $parser->set_element_handler('openHandler','closeHandler');
120         
121         $my_files               = array();
122         $parser->parse($body);
123         $my_files = array_unique($my_files);
124         
125         foreach ($my_files as $file) {
126                 /* filter out full urls */
127                 $url_parts = @parse_url($file);
128                 
129                 // file should be relative to content
130                 if ((substr($file, 0, 1) == '/')) {
131                         continue;
132                 }
133                 
134                 // The URL of the movie from youtube.com has been converted above in embed_media().
135                 // For example:  http://www.youtube.com/watch?v=a0ryB0m0MiM is converted to
136                 // http://www.youtube.com/v/a0ryB0m0MiM to make it playable. This creates the problem
137                 // that the parsed-out url (http://www.youtube.com/v/a0ryB0m0MiM) does not match with
138                 // the URL saved in content table (http://www.youtube.com/watch?v=a0ryB0m0MiM).
139                 // The code below is to convert the URL back to original.
140                 $file = ContentUtility::convertYoutubePlayURLToWatchURL($file);
141                 
142                 $resources[] = convertAmp($file);  // converts & to &amp;
143         }
144     
145     $a4a = new A4a($cid);
146     $db_primary_resources = $a4a->getPrimaryResources();
147     
148     // clean up the removed resources
149     foreach ($db_primary_resources  as $primary_rid=>$db_resource){
150         //if this file from our table is not found in the $resource, then it's not used.
151         if(count($resources) == 0 || !in_array($db_resource['resource'], $resources)){
152                 $a4a->deletePrimaryResource($primary_rid);
153         }
154     }
155     
156     if (count($resources) == 0) return;
157
158         // insert the new resources
159     foreach($resources as $primary_resource)
160         {
161                 if (!$a4a->getPrimaryResourceByName($primary_resource)){
162                         $a4a->setPrimaryResource($cid, $primary_resource, $_SESSION['lang']);
163                 }
164         }
165 }
166
167 // save all changes to the DB
168 function save_changes($redir, $current_tab) {
169         global $contentManager, $addslashes, $msg, $_course_id, $_content_id;
170         
171         $_POST['pid']   = intval($_POST['pid']);
172         $_POST['_cid']  = intval($_POST['_cid']);
173         
174         $_POST['alternatives'] = intval($_POST['alternatives']);
175         
176         $_POST['title'] = trim($_POST['title']);
177         $_POST['head']  = trim($_POST['head']);
178         $_POST['use_customized_head']   = isset($_POST['use_customized_head'])?$_POST['use_customized_head']:0;
179         $_POST['body_text']     = trim($_POST['body_text']);
180         $_POST['weblink_text'] = trim($_POST['weblink_text']);
181         $_POST['formatting'] = intval($_POST['formatting']);
182         $_POST['keywords']      = trim($_POST['keywords']);
183         $_POST['test_message'] = trim($_POST['test_message']);
184
185         //if weblink is selected, use it
186         if ($_POST['formatting']==CONTENT_TYPE_WEBLINK) {
187             $url = $_POST['weblink_text'];
188             $validated_url = isValidURL($url);
189         if (!validated_url || $validated_url !== $url) {
190                $msg->addError(array('INVALID_INPUT', _AT('weblink')));
191             } else {
192                     $_POST['body_text'] = $url;
193                     $content_type_pref = CONTENT_TYPE_WEBLINK;
194             }
195         } else {
196                 $content_type_pref = CONTENT_TYPE_CONTENT;
197         }
198
199         /*if (!($release_date = generate_release_date())) {
200                 $msg->addError('BAD_DATE');
201         }*/
202
203 //      if ($_POST['title'] == '') {
204 //              $msg->addError(array('EMPTY_FIELDS', _AT('title')));
205 //      }
206                 
207 //      if (!$msg->containsErrors()) {
208         $orig_body_text = $_POST['body_text'];  // used to populate a4a tables
209 //              $_POST['title']                 = $addslashes($_POST['title']);
210 //              $_POST['body_text']             = $addslashes($_POST['body_text']);
211 //              $_POST['head']                  = $addslashes($_POST['head']);
212 //              $_POST['keywords']              = $addslashes($_POST['keywords']);
213 //              $_POST['test_message']  = $addslashes($_POST['test_message']);          
214
215                 // add or edit content
216                 if ($_POST['_cid']) {
217                         /* editing an existing page */
218                         $err = $contentManager->editContent($_POST['_cid'], $_POST['title'], $_POST['body_text'], 
219                                                             $_POST['keywords'], $_POST['formatting'], 
220                                                             $_POST['head'], $_POST['use_customized_head'], 
221                                                             $_POST['test_message']);
222                         $cid = $_POST['_cid'];
223                 } else {
224                         /* insert new */
225                         $cid = $contentManager->addContent($_course_id,
226                                                                                                   $_POST['pid'],
227                                                                                                   $_POST['ordering'],
228                                                                                                   $_POST['title'],
229                                                                                                   $_POST['body_text'],
230                                                                                                   $_POST['keywords'],
231                                                                                                   $_POST['related'],
232                                                                                                   $_POST['formatting'],
233                                                                                                   $_POST['head'],
234                                                                                                   $_POST['use_customized_head'],
235                                                                                                   $_POST['test_message'],
236                                                                                                   $content_type_pref);
237                         $_POST['_cid']    = $cid;
238                         $_REQUEST['_cid'] = $cid;
239                 }
240         // re-populate a4a tables based on the new content
241                 populate_a4a($cid, $orig_body_text, $_POST['formatting']);
242                 if ($cid == 0) return;
243 //      }
244
245         /* insert glossary terms */
246         /*
247         if (is_array($_POST['glossary_defs']) && ($num_terms = count($_POST['glossary_defs']))) {
248                 global $glossary, $glossary_ids, $msg;
249
250                 foreach($_POST['glossary_defs'] as $w => $d) {
251                         $old_w = $w;
252                         $key = in_array_cin($w, $glossary_ids);
253                         $w = urldecode($w);
254                         $d = $addslashes($d);
255
256                         if (($key !== false) && (($glossary[$old_w] != $d) || isset($_POST['related_term'][$old_w])) ) {
257                                 $w = addslashes($w);
258                                 $related_id = intval($_POST['related_term'][$old_w]);
259                                 $sql = "UPDATE ".TABLE_PREFIX."glossary SET definition='$d', related_word_id=$related_id WHERE word_id=$key AND course_id=$_SESSION[course_id]";
260                                 $result = mysql_query($sql, $db);
261                                 $glossary[$old_w] = $d;
262                         } else if ($key === false && ($d != '')) {
263                                 $w = addslashes($w);
264                                 $related_id = intval($_POST['related_term'][$old_w]);
265                                 $sql = "INSERT INTO ".TABLE_PREFIX."glossary VALUES (NULL, $_SESSION[course_id], '$w', '$d', $related_id)";
266
267                                 $result = mysql_query($sql, $db);
268                                 $glossary[$old_w] = $d;
269                         }
270                 }
271         }*/
272         if (isset($_GET['tab'])) {
273                 $current_tab = intval($_GET['tab']);
274         }
275         if (isset($_POST['current_tab'])) {
276                 $current_tab = intval($_POST['current_tab']);
277         }
278
279         // adapted content: save primary content type
280         if (isset($_POST['use_post_for_alt']))
281         {
282                 include_once(TR_INCLUDE_PATH.'classes/DAO/PrimaryResourcesTypesDAO.class.php');
283                 $primaryResourcesTypesDAO = new PrimaryResourcesTypesDAO();
284                 
285                 // 1. delete old primary content type
286                 $sql = "DELETE FROM ".TABLE_PREFIX."primary_resources_types
287                          WHERE primary_resource_id in 
288                                (SELECT DISTINCT primary_resource_id 
289                                   FROM ".TABLE_PREFIX."primary_resources
290                                  WHERE content_id=".$cid."
291                                    AND language_code='".$_SESSION['lang']."')";
292                 $primaryResourcesTypesDAO->execute($sql);
293                 
294                 // 2. insert the new primary content type
295                 $sql = "SELECT pr.primary_resource_id, rt.type_id
296                           FROM ".TABLE_PREFIX."primary_resources pr, ".
297                                  TABLE_PREFIX."resource_types rt
298                          WHERE pr.content_id = ".$cid."
299                            AND pr.language_code = '".$_SESSION['lang']."'";
300                 $all_types_rows = $primaryResourcesTypesDAO->execute($sql);
301                 
302                 if (is_array($all_types_rows)) {
303                         foreach ($all_types_rows as $type) {
304                                 if (isset($_POST['alt_'.$type['primary_resource_id'].'_'.$type['type_id']]))
305                                 {
306                                         $primaryResourcesTypesDAO->Create($type['primary_resource_id'], $type['type_id']);
307 //                                      $sql = "INSERT INTO ".TABLE_PREFIX."primary_resources_types (primary_resource_id, type_id)
308 //                                              VALUES (".$type['primary_resource_id'].", ".$type['type_id'].")";
309 //                                      $result = mysql_query($sql, $db);
310                                 }
311                         }
312                 }
313         }
314         
315         include_once(TR_INCLUDE_PATH.'classes/DAO/ContentTestsAssocDAO.class.php');
316         $contentTestsAssocDAO = new ContentTestsAssocDAO();
317         $test_rows = $contentTestsAssocDAO->getByContent($_POST['_cid']);
318 //      $sql = 'SELECT * FROM '.TABLE_PREFIX."content_tests_assoc WHERE content_id=$_POST[cid]";
319 //      $result = mysql_query($sql, $db);
320         $db_test_array = array();
321         if (is_array($test_rows)) {
322                 foreach ($test_rows as $row) {
323                         $db_test_array[] = $row['test_id'];
324                 }
325         }
326
327         if (is_array($_POST['tid']) && sizeof($_POST['tid']) > 0){
328                 $toBeDeleted = array_diff($db_test_array, $_POST['tid']);
329                 $toBeAdded = array_diff($_POST['tid'], $db_test_array);
330                 //Delete entries
331                 if (!empty($toBeDeleted)){
332                         $tids = implode(",", $toBeDeleted);
333                         $sql = 'DELETE FROM '. TABLE_PREFIX . "content_tests_assoc WHERE content_id=$_POST[cid] AND test_id IN ($tids)";
334                         $contentTestsAssocDAO->execute($sql);
335                 }
336         
337                 //Add entries
338                 if (!empty($toBeAdded)){
339                         foreach ($toBeAdded as $i => $tid){
340                                 $tid = intval($tid);
341 //                              $sql = 'INSERT INTO '. TABLE_PREFIX . "content_tests_assoc SET content_id=$_POST[cid], test_id=$tid";
342 //                              $result = mysql_query($sql, $db);
343                                 if ($contentTestsAssocDAO->Create($_POST['_cid'], $tid) === false){
344                                         $msg->addError('DB_NOT_UPDATED');
345                                 }
346                         }
347                 }
348         } else {
349                 //All tests has been removed.
350                 $contentTestsAssocDAO->DeleteByContentID($_POST['_cid']);
351 //              $sql = 'DELETE FROM '. TABLE_PREFIX . "content_tests_assoc WHERE content_id=$_POST[cid]";
352 //              $result = mysql_query($sql, $db);
353         }
354         //End Add test
355
356         //TODO*******************BOLOGNA****************REMOVE ME**************/
357 /*
358         if(isset($_SESSION['associated_forum']) && !$msg->containsErrors()){
359                 if($_SESSION['associated_forum']=='none'){
360                         $sql = "DELETE FROM ".TABLE_PREFIX."content_forums_assoc WHERE content_id='$_POST[cid]'";
361                         mysql_query($sql,$db);
362                 } else {
363                         $sql = "DELETE FROM ".TABLE_PREFIX."content_forums_assoc WHERE content_id='$_POST[cid]'";
364                         mysql_query($sql,$db);
365                         $associated_forum = $_SESSION['associated_forum'];
366                         for($i=0; $i<count($associated_forum); $i++){
367                                 $sql="INSERT INTO ".TABLE_PREFIX."content_forums_assoc SET content_id='$_POST[cid]',forum_id='$associated_forum[$i]'";
368                                 mysql_query($sql,$db);
369                         }
370                 }
371                 unset($_SESSION['associated_forum']);
372         }
373 */
374         if (!$msg->containsErrors() && $redir) {
375                 $_SESSION['save_n_close'] = $_POST['save_n_close'];
376                 
377                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
378                 header('Location: '.basename($_SERVER['PHP_SELF']).'?_cid='.$cid.SEP.'close='.$addslashes($_POST['save_n_close']).SEP.'tab='.$addslashes($_POST['current_tab']).SEP.'displayhead='.$addslashes($_POST['displayhead']).SEP.'alternatives='.$addslashes($_POST['alternatives']));
379                 exit;
380         } else {
381                 return;
382         }
383 }
384 /*
385 function generate_release_date($now = false) {
386         if ($now) {
387                 $day  = date('d');
388                 $month= date('m');
389                 $year = date('Y');
390                 $hour = date('H');
391                 $min  = 0;
392         } else {
393                 $day    = intval($_POST['day']);
394                 $month  = intval($_POST['month']);
395                 $year   = intval($_POST['year']);
396                 $hour   = intval($_POST['hour']);
397                 $min    = intval($_POST['min']);
398         }
399
400         if (!checkdate($month, $day, $year)) {
401                 return false;
402         }
403
404         if (strlen($month) == 1){
405                 $month = "0$month";
406         }
407         if (strlen($day) == 1){
408                 $day = "0$day";
409         }
410         if (strlen($hour) == 1){
411                 $hour = "0$hour";
412         }
413         if (strlen($min) == 1){
414                 $min = "0$min";
415         }
416         $release_date = "$year-$month-$day $hour:$min:00";
417         
418         return $release_date;
419 }
420 */
421 function check_for_changes($row, $row_alternatives) {
422         global $contentManager, $cid, $glossary, $glossary_ids_related, $addslashes;
423
424         $changes = array();
425
426         if ($row && strcmp(trim($addslashes($_POST['title'])), addslashes($row['title']))) {
427                 $changes[0] = true;
428         } else if (!$row && $_POST['title']) {
429                 $changes[0] = true;
430         }
431
432         if ($row && strcmp($addslashes(trim($_POST['head'])), trim(addslashes($row['head'])))) {
433                 $changes[0] = true;
434         } else if (!$row && $_POST['head']) {
435                 $changes[0] = true;
436         }
437
438         if ($row && strcmp($addslashes(trim($_POST['body_text'])), trim(addslashes($row['text'])))) {
439                 $changes[0] = true;
440         } else if (!$row && $_POST['body_text']) {
441                 $changes[0] = true;
442         }
443         
444     if ($row && strcmp($addslashes(trim($_POST['weblink_text'])), trim(addslashes($row['text'])))) {
445         $changes[0] = true;
446     } else if (!$row && $_POST['weblink_text']) {
447         $changes[0] = true;
448     }
449
450         /* use customized head: */
451         if ($row && isset($_POST['use_customized_head']) && ($_POST['use_customized_head'] != $row['use_customized_head'])) {
452                 $changes[0] = true;
453         }
454
455         /* formatting: */
456         if ($row && strcmp(trim($_POST['formatting']), $row['formatting'])) {
457                 $changes[0] = true;
458         } else if (!$row && $_POST['formatting']) {
459                 $changes[0] = true;
460         }
461
462         /* release date: */
463 //      if ($row && strcmp(substr(generate_release_date(), 0, -2), substr($row['release_date'], 0, -2))) {
464 //              /* the substr was added because sometimes the release_date in the db has the seconds field set, which we dont use */
465 //              /* so it would show a difference, even though it should actually be the same, so we ignore the seconds with the -2 */
466 //              /* the seconds gets added if the course was created during the installation process. */
467 //              $changes[1] = true;
468 //      } else if (!$row && strcmp(generate_release_date(), generate_release_date(true))) {
469 //              $changes[1] = true;
470 //      }
471
472         /* related content: */
473 //      $row_related = $contentManager->getRelatedContent($cid);
474 //
475 //      if (is_array($_POST['related']) && is_array($row_related)) {
476 //              $sum = array_sum(array_diff($_POST['related'], $row_related));
477 //              $sum += array_sum(array_diff($row_related, $_POST['related']));
478 //              if ($sum > 0) {
479 //                      $changes[1] = true;
480 //              }
481 //      } else if (!is_array($_POST['related']) && !empty($row_related)) {
482 //              $changes[1] = true;
483 //      }
484
485         /* keywords */
486         if ($row && strcmp(trim($_POST['keywords']), $row['keywords'])) {
487                 $changes[1] = true;
488         }  else if (!$row && $_POST['keywords']) {
489                 $changes[1] = true;
490         }
491
492
493         /* glossary */
494 //      if (is_array($_POST['glossary_defs'])) {
495 //              global $glossary_ids;
496 //              foreach ($_POST['glossary_defs'] as $w => $d) {
497 //
498 //                      $key = in_array_cin($w, $glossary_ids);
499 //                      if ($key === false) {
500 //                              /* new term */
501 //                              $changes[2] = true;
502 //                              break;
503 //                      } else if ($cid && ($d &&($d != $glossary[$glossary_ids[$key]]))) {
504 //                              /* changed term */
505 //                              $changes[2] = true;
506 //                              break;
507 //                      }
508 //              }
509 //
510 //              if (is_array($_POST['related_term'])) {
511 //                      foreach($_POST['related_term'] as $term => $r_id) {
512 //                              if ($glossary_ids_related[$term] != $r_id) {
513 //                                      $changes[2] = true;
514 //                                      break;
515 //                              }
516 //                      }
517 //              }
518 //      }
519
520         /* adapted content */
521         if (isset($_POST['use_post_for_alt']))
522         {
523                 foreach ($_POST as $alt_id => $alt_value) {
524                         if (substr($alt_id, 0 ,4) == 'alt_' && $alt_value != $row_alternatives[$alt_id]){
525                                 $changes[2] = true;
526                                 break;
527                         }
528                 }
529         }
530         
531         /* test & survey */     
532         if ($row && isset($_POST['test_message']) && $_POST['test_message'] != $row['test_message']){
533                 $changes[3] = true;
534         }
535         
536         $content_tests = $contentManager->getContentTestsAssoc($cid);
537         
538         if (isset($_POST['visited_tests'])) {
539                 if (!is_array($content_tests) && is_array($_POST['tid'])) {
540                         $changes[3] = true;
541                 }
542                 if (is_array($content_tests)) {
543                         for ($i = 0; $i < count($content_tests); $i++) {
544                                 if ($content_tests[$i]['test_id'] <> $_POST['tid'][$i]) {
545                                         $changes[3] = true;
546                                         break;
547                                 }
548                         }
549                 }
550         }
551
552         return $changes;
553 }
554
555 function paste_from_file() {
556         global $msg;
557         
558         include_once(TR_INCLUDE_PATH.'../home/classes/ContentUtility.class.php');
559         if ($_FILES['uploadedfile_paste']['name'] == '')        {
560                 $msg->addError('FILE_NOT_SELECTED');
561                 return;
562         }
563         if ($_FILES['uploadedfile_paste']['name']
564                 && (($_FILES['uploadedfile_paste']['type'] == 'text/plain')
565                         || ($_FILES['uploadedfile_paste']['type'] == 'text/html')) )
566                 {
567
568                 $path_parts = pathinfo($_FILES['uploadedfile_paste']['name']);
569                 $ext = strtolower($path_parts['extension']);
570
571                 if (in_array($ext, array('html', 'htm'))) {
572                         $_POST['body_text'] = file_get_contents($_FILES['uploadedfile_paste']['tmp_name']);
573
574                         /* get the <title></title> of this page                         */
575
576                         $start_pos      = strpos(strtolower($_POST['body_text']), '<title>');
577                         $end_pos        = strpos(strtolower($_POST['body_text']), '</title>');
578
579                         if (($start_pos !== false) && ($end_pos !== false)) {
580                                 $start_pos += strlen('<title>');
581                                 $_POST['title'] = trim(substr($_POST['body_text'], $start_pos, $end_pos-$start_pos));
582                         }
583                         unset($start_pos);
584                         unset($end_pos);
585
586                         $_POST['head'] = ContentUtility::getHtmlHeadByTag($_POST['body_text'], array("link", "style", "script")); 
587                         if (strlen(trim($_POST['head'])) > 0)   
588                                 $_POST['use_customized_head'] = 1;
589                         else
590                                 $_POST['use_customized_head'] = 0;
591                         
592                         $_POST['body_text'] = ContentUtility::getHtmlBody($_POST['body_text']); 
593
594                         $msg->addFeedback('FILE_PASTED');
595                 } else if ($ext == 'txt') {
596                         $_POST['body_text'] = file_get_contents($_FILES['uploadedfile_paste']['tmp_name']);
597                         //LAW
598 //                      debug($_POST);
599                         $msg->addFeedback('FILE_PASTED');
600
601                 }
602         } else {
603                 $msg->addError('BAD_FILE_TYPE');
604         }
605
606         return;
607 }
608
609 //for accessibility checker
610 function write_temp_file() {
611         global $_POST, $msg;
612
613         if (defined('TR_FORCE_GET_FILE') && TR_FORCE_GET_FILE) {
614                 $content_base = 'get.php/';
615         } else {
616                 $content_base = 'content/' . $_SESSION['course_id'] . '/';
617         }
618
619         if ($_POST['content_path']) {
620                 $content_base .= $_POST['content_path'] . '/';
621         }
622
623         $file_name = $_POST['_cid'].'.html';
624
625         if ($handle = fopen(TR_CONTENT_DIR . $file_name, 'wb+')) {
626 //              $temp_content = '<h2>'.TR_print(stripslashes($_POST['title']), 'content.title').'</h2>';
627 //
628 //              if ($_POST['body_text'] != '') {
629 //                      $temp_content .= format_content(stripslashes($_POST['body_text']), $_POST['formatting'], $_POST['glossary_defs']);
630 //              }
631 //              $temp_title = $_POST['title'];
632 //
633 //              $html_template = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
634 //                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
635 //              <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
636 //              <head>
637 //                      <base href="{BASE_HREF}" />
638 //                      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
639 //                      <title>{TITLE}</title>
640 //                      <meta name="Generator" content="ATutor accessibility checker file - can be deleted">
641 //              </head>
642 //              <body>
643 //              {CONTENT}
644 //              </body>
645 //              </html>';
646 //
647 //              $page_html = str_replace(       array('{BASE_HREF}', '{TITLE}', '{CONTENT}'),
648 //                                                                      array($content_base, $temp_title, $temp_content),
649 //                                                                      $html_template);
650                 
651                 if (!@fwrite($handle, stripslashes($_POST['body_text']))) {
652                         $msg->addError('FILE_NOT_SAVED');       
653            }
654         } else {
655                 $msg->addError('FILE_NOT_SAVED');
656         }
657         $msg->printErrors();
658 }
659 ?>