2 /************************************************************************/
\r
4 /************************************************************************/
\r
5 /* Copyright (c) 2010 */
\r
6 /* Inclusive Design Institute */
\r
8 /* This program is free software. You can redistribute it and/or */
\r
9 /* modify it under the terms of the GNU General Public License */
\r
10 /* as published by the Free Software Foundation. */
\r
11 /************************************************************************/
\r
14 * Content Utility functions
\r
16 * @author Cindy Qi Li
\r
19 if (!defined('TR_INCLUDE_PATH')) exit;
\r
21 class ContentUtility {
\r
24 * This function cuts out html body
\r
26 * @param $text html text
\r
27 * @author Cindy Qi Li
\r
29 public static function getHtmlBody($text) {
\r
30 /* strip everything before <body> */
\r
31 $start_pos = strpos(strtolower($text), '<body');
\r
32 if ($start_pos !== false) {
\r
33 $start_pos += strlen('<body');
\r
34 $end_pos = strpos(strtolower($text), '>', $start_pos);
\r
35 $end_pos += strlen('>');
\r
37 $text = substr($text, $end_pos);
\r
40 /* strip everything after </body> */
\r
41 $end_pos = strpos(strtolower($text), '</body>');
\r
42 if ($end_pos !== false) {
\r
43 $text = trim(substr($text, 0, $end_pos));
\r
50 * This function cuts out requested tag information from html head
\r
52 * @param $text html text
\r
53 * @param $tags a string or an array of requested tags
\r
54 * @author Cindy Qi Li
\r
56 public static function getHtmlHeadByTag($text, $tags)
\r
58 $head = ContentUtility::getHtmlHead($text);
\r
61 if (!is_array($tags) && strlen(trim($tags)) > 0)
\r
63 $tags = array(trim($tags));
\r
65 foreach ($tags as $tag)
\r
67 $tag = strtolower($tag);
\r
69 /* strip everything before <{tag}> */
\r
70 $start_pos = stripos($head, '<'.$tag);
\r
73 while ($start_pos !== false)
\r
75 $temp_text = substr($temp_head, $start_pos);
\r
77 /* strip everything after </{tag}> or />*/
\r
78 $end_pos = stripos($temp_text, '</' . $tag . '>');
\r
80 if ($end_pos !== false)
\r
82 $end_pos += strlen('</' . $tag . '>');
\r
84 // add an empty line after each tag information
\r
85 $rtn_text .= trim(substr($temp_text, 0, $end_pos)) . '
\r
89 else // match /> as ending tag if </tag> is not found
\r
91 $end_pos = stripos($temp_text, '/>');
\r
93 if($end_pos === false && stripos($temp_text, $tag.'>')===false){
\r
94 //if /> is not found, then this is not a valid XHTML
\r
95 //text iff it's not tag>
\r
96 $end_pos = stripos($temp_text, '>');
\r
97 $end_pos += strlen('>');
\r
99 $end_pos += strlen('/>');
\r
101 // add an empty line after each tag information
\r
102 $rtn_text .= trim(substr($temp_text, 0, $end_pos)) . '
\r
107 // initialize vars for next round of matching
\r
108 $temp_head = substr($temp_text, $end_pos);
\r
109 $start_pos = stripos($temp_head, '<'.$tag);
\r
116 * This function cuts out html head
\r
118 * @param $text html text
\r
119 * @author Cindy Qi Li
\r
121 private static function getHtmlHead ($text) {
\r
122 /* strip everything before <head> */
\r
123 $start_pos = stripos($text, '<head');
\r
124 if ($start_pos !== false) {
\r
125 $start_pos += strlen('<head');
\r
126 $end_pos = stripos($text, '>', $start_pos);
\r
127 $end_pos += strlen('>');
\r
129 $text = substr($text, $end_pos);
\r
132 /* strip everything after </head> */
\r
133 $end_pos = stripos($text, '</head');
\r
134 if ($end_pos !== false) {
\r
135 $text = trim(substr($text, 0, $end_pos));
\r
140 private static function embedFLV($text) {
\r
141 global $content_base_href;
\r
143 // .flv - uses Flowplayer 3.0 from flowplayer.org (playing file via full URL)
\r
144 preg_match_all("#\[media[0-9a-z\|]*\]http://([\w\./-]+)\.flv\[/media\]#i",$text,$media_matches[0],PREG_SET_ORDER);
\r
145 $media_replace[0] ="<a class=\"flowplayerholder\"
\r
146 style=\"display:block;width:##WIDTH##px;height:##HEIGHT##px;\"
\r
147 href=\"http://##MEDIA1##.flv\">
\r
150 // .flv - uses Flowplayer 3.0 from flowplayer.org (playing file from AT_content_dir)
\r
151 preg_match_all("#\[media[0-9a-z\|]*\]([\w\./-]+)\.flv\[/media\]#i",$text,$media_matches[1],PREG_SET_ORDER);
\r
152 $media_replace[1] ="<a class=\"flowplayerholder\"
\r
153 style=\"display:block;width:##WIDTH##px;height:##HEIGHT##px;\"
\r
154 href=\"".TR_BASE_HREF."get.php/".$content_base_href."##MEDIA1##.flv\">
\r
158 // Executing the replace
\r
159 for ($i=0;$i<count($media_replace);$i++){
\r
160 foreach($media_matches[$i] as $media)
\r
162 if (is_array($media)) $has_flv = true;
\r
164 //find width and height for each matched media
\r
165 if (preg_match("/\[media\|([0-9]*)\|([0-9]*)\]*/", $media[0], $matches))
\r
167 $width = $matches[1];
\r
168 $height = $matches[2];
\r
176 //replace media tags with embedded media for each media tag
\r
177 $media_input = $media_replace[$i];
\r
178 $media_input = str_replace("##WIDTH##","$width",$media_input);
\r
179 $media_input = str_replace("##HEIGHT##","$height",$media_input);
\r
180 $media_input = str_replace("##MEDIA1##","$media[1]",$media_input);
\r
181 $media_input = str_replace("##MEDIA2##","$media[2]",$media_input);
\r
182 $text = str_replace($media[0],$media_input,$text);
\r
189 <script language="JavaScript">
\r
190 $f("a.flowplayerholder", "'.TR_BASE_HREF.'include/jscripts/flowplayer/flowplayer-3.1.2.swf", {
\r
191 clip: { autoPlay: false },
\r
207 public static function embedMedia($text) {
\r
208 if (preg_match("/\[media(\|[0-9]+\|[0-9]+)?\]*/", $text)==0){
\r
212 $media_matches = Array();
\r
215 First, we search though the text for all different kinds of media defined by media tags and store the results in $media_matches.
\r
217 Then the different replacements for the different media tags are stored in $media_replace.
\r
219 Lastly, we loop through all $media_matches / $media_replaces. (We choose $media_replace as index because $media_matches is multi-dimensioned.) It is important that for each $media_matches there is a $media_replace with the same index. For each media match we check the width/height, or we use the default value of 425x350. We then replace the height/width/media1/media2 parameter placeholders in $media_replace with the correct ones, before running a str_replace on $text, replacing the given media with its correct replacement.
\r
224 preg_match_all("#\[media[0-9a-z\|]*\]http://([a-z0-9\.]*)?youtube.com/watch\?v=([a-z0-9_-]+)\[/media\]#i",$text,$media_matches[1],PREG_SET_ORDER);
\r
225 $media_replace[1] = '<object width="##WIDTH##" height="##HEIGHT##"><param name="movie" value="http://##MEDIA1##youtube.com/v/##MEDIA2##"></param><embed src="http://##MEDIA1##youtube.com/v/##MEDIA2##" type="application/x-shockwave-flash" width="##WIDTH##" height="##HEIGHT##"></embed></object>';
\r
228 preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+).mpg\[/media\]#i",$text,$media_matches[2],PREG_SET_ORDER);
\r
229 $media_replace[2] = "<object data=\"##MEDIA1##.mpg\" type=\"video/mpeg\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.mpg\"><param name=\"autoplay\" value=\"false\"><param name=\"autoStart\" value=\"0\"><a href=\"##MEDIA1##.mpg\">##MEDIA1##.mpg</a></object>";
\r
232 preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+).avi\[/media\]#i",$text,$media_matches[3],PREG_SET_ORDER);
\r
233 $media_replace[3] = "<object data=\"##MEDIA1##.avi\" type=\"video/x-msvideo\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.avi\"><param name=\"autoplay\" value=\"false\"><param name=\"autoStart\" value=\"0\"><a href=\"##MEDIA1##.avi\">##MEDIA1##.avi</a></object>";
\r
236 preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+).wmv\[/media\]#i",$text,$media_matches[4],PREG_SET_ORDER);
\r
237 $media_replace[4] = "<object data=\"##MEDIA1##.wmv\" type=\"video/x-ms-wmv\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.wmv\"><param name=\"autoplay\" value=\"false\"><param name=\"autoStart\" value=\"0\"><a href=\"##MEDIA1##.wmv\">##MEDIA1##.wmv</a></object>";
\r
240 preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+).mov\[/media\]#i",$text,$media_matches[5],PREG_SET_ORDER);
\r
241 $media_replace[5] = "<object classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" codebase=\"http://www.apple.com/qtactivex/qtplugin.cab\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.mov\"><param name=\"controller\" value=\"true\"><param name=\"autoplay\" value=\"false\"><!--[if gte IE 7]> <!--><object type=\"video/quicktime\" data=\"##MEDIA1##.mov\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"controller\" value=\"true\"><param name=\"autoplay\" value=\"false\"><a href=\"##MEDIA1##.mov\">##MEDIA1##.mov</a></object><!--<![endif]--><!--[if lt IE 7]><a href=\"##MEDIA1##.mov\">##MEDIA1##.mov</a><![endif]--></object>";
\r
244 preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+).swf\[/media\]#i",$text,$media_matches[6],PREG_SET_ORDER);
\r
245 $media_replace[6] = "<object type=\"application/x-shockwave-flash\" data=\"##MEDIA1##.swf\" width=\"##WIDTH##\" height=\"##HEIGHT##\"> <param name=\"movie\" value=\"##MEDIA1##.swf\"><param name=\"loop\" value=\"false\"><a href=\"##MEDIA1##.swf\">##MEDIA1##.swf</a></object>";
\r
248 preg_match_all("#\[media[0-9a-z\|]*\](.+[^\s\"]+).mp3\[/media\]#i",$text,$media_matches[7],PREG_SET_ORDER);
\r
249 $media_replace[7] = "<object type=\"audio/mpeg\" data=\"##MEDIA1##.mp3\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.mp3\"><param name=\"autoplay\" value=\"false\"><param name=\"autoStart\" value=\"0\"><a href=\"##MEDIA1##.mp3\">##MEDIA1##.mp3</a></object>";
\r
252 preg_match_all("#\[media[0-9a-z\|]*\](.+[^\s\"]+).wav\[/media\]#i",$text,$media_matches[8],PREG_SET_ORDER);
\r
253 $media_replace[8] ="<object type=\"audio/x-wav\" data=\"##MEDIA1##.wav\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.wav\"><param name=\"autoplay\" value=\"false\"><param name=\"autoStart\" value=\"0\"><a href=\"##MEDIA1##.wav\">##MEDIA1##.wav</a></object>";
\r
256 preg_match_all("#\[media[0-9a-z\|]*\](.+[^\s\"]+).ogg\[/media\]#i",$text,$media_matches[9],PREG_SET_ORDER);
\r
257 $media_replace[9] ="<object type=\"application/ogg\" data=\"##MEDIA1##.ogg\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.ogg\"><a href=\"##MEDIA1##.ogg\">##MEDIA1##.ogg</a></object>";
\r
260 preg_match_all("#\[media[0-9a-z\|]*\](.+[^\s\"]+).mid\[/media\]#i",$text,$media_matches[10],PREG_SET_ORDER);
\r
261 $media_replace[10] ="<object type=\"application/x-midi\" data=\"##MEDIA1##.mid\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.mid\"><a href=\"##MEDIA1##.mid\">##MEDIA1##.mid</a></object>";
\r
263 $text = preg_replace("#\[media[0-9a-z\|]*\](.+[^\s\"]+).mid\[/media\]#i", "<object type=\"application/x-midi\" data=\"\\1.mid\" width=\"".$width."\" height=\"".$height."\"><param name=\"src\" value=\"\\1.mid\"><a href=\"\\1.mid\">\\1.mid</a></object>", $text);
\r
265 // Executing the replace
\r
266 for ($i=1;$i<=count($media_replace);$i++){
\r
267 foreach($media_matches[$i] as $media)
\r
270 //find width and height for each matched media
\r
271 if (preg_match("/\[media\|([0-9]*)\|([0-9]*)\]*/", $media[0], $matches))
\r
273 $width = $matches[1];
\r
274 $height = $matches[2];
\r
282 //replace media tags with embedded media for each media tag
\r
283 $media_input = $media_replace[$i];
\r
284 $media_input = str_replace("##WIDTH##","$width",$media_input);
\r
285 $media_input = str_replace("##HEIGHT##","$height",$media_input);
\r
286 $media_input = str_replace("##MEDIA1##","$media[1]",$media_input);
\r
287 $media_input = str_replace("##MEDIA2##","$media[2]",$media_input);
\r
288 $text = str_replace($media[0],$media_input,$text);
\r
292 $text = ContentUtility::embedFLV($text);
\r
297 private static function makeClickable($text) {
\r
298 $text = ContentUtility::embedMedia($text);
\r
300 // $text = eregi_replace("([[:space:]])(http[s]?)://([^[:space:]<]*)([[:alnum:]#?/&=])", "\\1<a href=\"\\2://\\3\\4\">\\3\\4</a>", $text);
\r
302 // $text = eregi_replace( '([_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.
\r
303 // '\@'.'[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.'(\.[a-zA-Z]{1,6})+)',
\r
304 // "<a href=\"mailto:\\1\">\\1</a>",
\r
307 $text = preg_replace("/([\s])(http[s]?):\/\/(.*)(\s|\$|<br\s\/\>)(.*)/U",
\r
308 "\\1<a href=\"\\2://\\3\">\\3</a>\\4\\5", $text);
\r
310 $text = preg_replace('/([_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.
\r
311 '\@'.'[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.'(\.[a-zA-Z]{1,6})+)/i',
\r
312 "<a href=\"mailto:\\1\">\\1</a>",
\r
317 private static function myCodes($text, $html = false) {
\r
318 global $_base_path;
\r
319 global $HTTP_USER_AGENT;
\r
321 if (substr($HTTP_USER_AGENT,0,11) == 'Mozilla/4.7') {
\r
322 $text = str_replace('[quote]','</p><p class="block">',$text);
\r
323 $text = str_replace('[/quote]','</p><p>',$text);
\r
325 $text = str_replace('[reply]','</p><p class="block">',$text);
\r
326 $text = str_replace('[/reply]','</p><p>',$text);
\r
328 $text = str_replace('[quote]','<blockquote>',$text);
\r
329 $text = str_replace('[/quote]','</blockquote><p>',$text);
\r
331 $text = str_replace('[reply]','</p><blockquote class="block"><p>',$text);
\r
332 $text = str_replace('[/reply]','</p></blockquote><p>',$text);
\r
335 $text = str_replace('[b]','<strong>',$text);
\r
336 $text = str_replace('[/b]','</strong>',$text);
\r
338 $text = str_replace('[i]','<em>',$text);
\r
339 $text = str_replace('[/i]','</em>',$text);
\r
341 $text = str_replace('[u]','<u>',$text);
\r
342 $text = str_replace('[/u]','</u>',$text);
\r
344 $text = str_replace('[center]','<center>',$text);
\r
345 $text = str_replace('[/center]','</center><p>',$text);
\r
348 $text = str_replace('[blue]','<span style="color: blue;">',$text);
\r
349 $text = str_replace('[/blue]','</span>',$text);
\r
351 $text = str_replace('[orange]','<span style="color: orange;">',$text);
\r
352 $text = str_replace('[/orange]','</span>',$text);
\r
354 $text = str_replace('[red]','<span style="color: red;">',$text);
\r
355 $text = str_replace('[/red]','</span>',$text);
\r
357 $text = str_replace('[purple]','<span style="color: purple;">',$text);
\r
358 $text = str_replace('[/purple]','</span>',$text);
\r
360 $text = str_replace('[green]','<span style="color: green;">',$text);
\r
361 $text = str_replace('[/green]','</span>',$text);
\r
363 $text = str_replace('[gray]','<span style="color: gray;">',$text);
\r
364 $text = str_replace('[/gray]','</span>',$text);
\r
366 $text = str_replace('[op]','<span class="bigspacer"></span> <a href="',$text);
\r
367 $text = str_replace('[/op]','">'._AT('view_entire_post').'</a>',$text);
\r
369 $text = str_replace('[head1]','<h2>',$text);
\r
370 $text = str_replace('[/head1]','</h2>',$text);
\r
372 $text = str_replace('[head2]','<h3>',$text);
\r
373 $text = str_replace('[/head2]','</h3>',$text);
\r
375 $text = str_replace('[cid]',$_base_path.'content.php?_cid='.$_SESSION['s_cid'],$text);
\r
377 global $sequence_links, $_course_id, $_content_id;
\r
378 if ($_course_id > 0 && !isset($sequence_links) && $_content_id > 0) {
\r
379 global $contentManager;
\r
380 $sequence_links = $contentManager->generateSequenceCrumbs($_content_id);
\r
382 if (isset($sequence_links['previous']) && $sequence_links['previous']['url']) {
\r
383 $text = str_replace('[pid]', $sequence_links['previous']['url'], $text);
\r
385 if (isset($sequence_links['next']) && $sequence_links['next']['url']) {
\r
386 $text = str_replace('[nid]', $sequence_links['next']['url'], $text);
\r
388 if (isset($sequence_links['resume']) && $sequence_links['resume']['url']) {
\r
389 $text = str_replace('[nid]', $sequence_links['resume']['url'], $text);
\r
391 if (isset($sequence_links['first']) && $sequence_links['first']['url']) {
\r
392 $text = str_replace('[fid]', $sequence_links['first']['url'], $text);
\r
395 /* contributed by Thomas M. Duffey <tduffey at homeboyz.com> */
\r
396 $html = !$html ? 0 : 1;
\r
398 // little hack added by greg to add syntax highlighting without using <?php \?\>
\r
400 $text = str_replace("[code]","[code]<?php",$text);
\r
401 $text = str_replace("[/code]","?>[/code]",$text);
\r
403 $text = preg_replace("/\[code\]\s*(.*)\s*\[\\/code\]/Usei", "highlight_code(fix_quotes('\\1'), $html)", $text);
\r
404 // now remove the <?php added above and leave the syntax colour behind.
\r
405 $text = str_replace("<?php", "", $text);
\r
406 $text = str_replace("?>", "", $text);
\r
411 private static function imageReplace($text) {
\r
412 /* image urls do not require http:// */
\r
414 // $text = eregi_replace("\[image(\|)?([[:alnum:][:space:]]*)\]" .
\r
416 // "([[:alnum:]#?/&=:\"'_.-]+)" .
\r
418 // "((\[/image\])|(.*\[/image\]))",
\r
419 // "<img src=\"\\3\" alt=\"\\2\" />",
\r
422 $text = preg_replace("/\[image(\|)?([a-zA-Z0-9\s]*)\]".
\r
424 "([a-zA-Z0-9\#\?\/\&\=\:\\\"\'\_\.\-]+)[\s]*".
\r
425 "((\[\/image\])|(.*\[\/image\]))/i",
\r
426 "<img src=\"\\3\" alt=\"\\2\" />",
\r
432 private static function formatFinalOutput($text, $nl2br = true) {
\r
433 global $_base_path;
\r
435 $text = str_replace('CONTENT_DIR/', '', $text);
\r
437 return nl2br(ContentUtility::imageReplace(ContentUtility::makeClickable(ContentUtility::myCodes(' '.$text, false))));
\r
440 return ContentUtility::imageReplace(ContentUtility::makeClickable(ContentUtility::myCodes(' '.$text, true)));
\r
444 * This function converts the input string into AContent html content string
\r
446 * @param $input: input string
\r
447 * $html: whether the input is in html
\r
448 * @return converted AContent html content string
\r
449 * @author Cindy Qi Li
\r
451 public static function formatContent($input, $html = 0) {
\r
452 global $_base_path, $_config;
\r
455 $input = str_replace('<', '<', $input);
\r
456 $input = str_replace('<?php', '<?php', $input); // for bug #2087
\r
457 } elseif ($html==2) {
\r
458 $output = '<iframe width="100%" frameborder="0" id="content_frame" marginheight="0" marginwidth="0" src="'.$input.'"></iframe>';
\r
459 $output .= '<script type="text/javascript">
\r
460 function resizeIframe() {
\r
461 var height = document.documentElement.clientHeight;
\r
463 // not sure how to get this dynamically
\r
464 height -= 20; /* whatever you set your body bottom margin/padding to be */
\r
466 document.getElementById(\'content_frame\').style.height = height +"px";
\r
469 document.getElementById(\'content_frame\').onload = resizeIframe;
\r
470 window.onresize = resizeIframe;
\r
475 /* Commented by Cindy Qi Li on Jan 12, 2010
\r
476 * AContent does not support glossary
\r
477 // do the glossary search and replace:
\r
478 if (is_array($glossary)) {
\r
479 foreach ($glossary as $k => $v) {
\r
480 $k = urldecode($k);
\r
481 $v = str_replace("\n", '<br />', $v);
\r
482 $v = str_replace("\r", '', $v);
\r
484 // escape special characters
\r
485 $k = preg_quote($k);
\r
487 $k = str_replace('<', '<', $k);
\r
488 $k = str_replace('/', '\/', $k);
\r
490 $original_term = $k;
\r
491 $term = $original_term;
\r
493 $term = '(\s*'.$term.'\s*)';
\r
494 $term = str_replace(' ','((<br \/>)*\s*)', $term);
\r
496 $def = htmlspecialchars($v, ENT_QUOTES, 'UTF-8');
\r
498 $input = preg_replace
\r
499 ("/(\[\?\])$term(\[\/\?\])/i",
\r
500 '<a href="'.$simple.'glossary.html#'.urlencode($original_term).'" target="body" class="at-term">\\2</a>',
\r
503 $input = preg_replace
\r
504 ("/(\[\?\])$term(\[\/\?\])/i",
\r
505 '\\2<sup><a class="tooltip" href="'.$_base_path.'glossary/index.php?g_cid='.$_SESSION['s_cid'].htmlentities(SEP).'w='.urlencode($original_term).'#term" title="'.addslashes($original_term).': '.$def.'"><span style="color: blue; text-decoration: none;font-size:small; font-weight:bolder;">?</span></a></sup>',$input);
\r
508 } else if (!$user_glossary) {
\r
509 $input = str_replace(array('[?]','[/?]'), '', $input);
\r
514 $input = str_replace('CONTENT_DIR', '', $input);
\r
516 if (isset($_config['latex_server']) && $_config['latex_server']) {
\r
517 // see: http://www.forkosh.com/mimetex.html
\r
518 $input = preg_replace('/\[tex\](.*?)\[\/tex\]/sie', "'<img src=\"'.\$_config['latex_server'].rawurlencode('$1').'\" align=\"middle\">'", $input);
\r
522 $x = ContentUtility::formatFinalOutput($input, false);
\r
526 $output = ContentUtility::formatFinalOutput($input);
\r
528 $output = '<p>'.$input.'</p>';
\r
534 * This function returns html string of "table of content"
\r
536 * @param: $content: a string
\r
537 * @return: a html string of "table of content"
\r
539 public static function getContentTable($content)
\r
541 preg_match_all("/<(h[\d]+)[^>]*>(.*)<\/(\s*)\\1(\s*)>/i", $content, $found_headers, PREG_SET_ORDER);
\r
543 if (count($found_headers) == 0) return array("", $content);
\r
546 $num_of_headers = 0;
\r
548 for ($i = 0; $i < count($found_headers); $i++)
\r
550 $div_id = "_header_" . $num_of_headers++;
\r
554 $content_table = "<div id=\"toc\">\n<fieldset id=\"toc\"><legend>". _AT("table_of_contents")."</legend>\n";
\r
557 $content = str_replace($found_headers[$i][0], '<div id="'.$div_id.'">'.$found_headers[$i][0].'</div>', $content);
\r
558 $content_table .= '<a href="'.$_SERVER["REQUEST_URI"].'#'.$div_id.'" class="'.$found_headers[$i][1].'">'. $found_headers[$i][2]."</a>\n";
\r
560 if ($i == count($found_headers) - 1)
\r
562 $content_table .= "</fieldset></div><br />";
\r
565 return array($content_table, $content);
\r
570 * This function returns an array of content tools' shortcuts
\r
572 * @param: $content_row: an array of the current content information
\r
573 * @return: an array of all the tool short cuts that apply to the current content or content folder
\r
575 public static function getToolShortcuts($content_row)
\r
577 global $_current_user, $_base_href, $contentManager, $_course_id;
\r
579 if (((!$content_row['content_parent_id'] && ($_SESSION['packaging'] == 'top'))
\r
580 || ($_SESSION['packaging'] == 'all'))
\r
581 || (isset($_current_user) && $_current_user->isAuthor($_course_id))) {
\r
583 $tool_shortcuts[] = array(
\r
584 'title' => _AT('export_content_in_cp'),
\r
585 'url' => $_base_href . 'home/ims/ims_export.php?_cid='.$content_row['content_id'],
\r
586 'icon' => $_base_href . 'themes/'.$_SESSION['prefs']['PREF_THEME'].'/images/export.png');
\r
587 $tool_shortcuts[] = array(
\r
588 'title' => _AT('export_content_in_cc'),
\r
589 'url' => $_base_href . 'home/imscc/ims_export.php?_cid='.$content_row['content_id'].'&to_a4a=1',
\r
590 'icon' => $_base_href . 'themes/'.$_SESSION['prefs']['PREF_THEME'].'/images/export_cc.png');
\r
593 if (isset($_current_user) && $_current_user->isAuthor($_course_id)) {
\r
594 if ($content_row['content_type'] == CONTENT_TYPE_CONTENT || $content_row['content_type'] == CONTENT_TYPE_WEBLINK) {
\r
595 $tool_shortcuts[] = array(
\r
596 'title' => _AT('edit_this_page'),
\r
597 'url' => $_base_href . 'home/editor/edit_content.php?_cid='.$content_row['content_id'],
\r
598 'icon' => $_base_href . 'images/medit.gif');
\r
601 $tool_shortcuts[] = array(
\r
602 'title' => _AT('add_sibling_folder'),
\r
603 'url' => $_base_href .
\r
604 'home/editor/edit_content_folder.php?pid='.$contentManager->_menu_info[$content_row['content_id']]['content_parent_id'].SEP.'_course_id='.$_course_id,
\r
605 'icon' => $_base_href . 'images/add_sibling_folder.gif');
\r
607 if ($content_row['content_type'] == CONTENT_TYPE_FOLDER || $content_row['content_type'] == CONTENT_TYPE_WEBLINK) {
\r
608 $tool_shortcuts[] = array(
\r
609 'title' => _AT('add_sub_folder'),
\r
610 'url' => $_base_href .
\r
611 'home/editor/edit_content_folder.php?_course_id='.$_course_id.SEP.'pid='.$content_row['content_id'],
\r
612 'icon' => $_base_href . 'images/add_sub_folder.gif');
\r
615 $tool_shortcuts[] = array(
\r
616 'title' => _AT('add_sibling_page'),
\r
617 'url' => $_base_href .
\r
618 'home/editor/edit_content.php?pid='.$contentManager->_menu_info[$content_row['content_id']]['content_parent_id'].SEP.'_course_id='.$_course_id,
\r
619 'icon' => $_base_href . 'images/add_sibling_page.gif');
\r
621 if ($content_row['content_type'] == CONTENT_TYPE_CONTENT) {
\r
622 $tool_shortcuts[] = array(
\r
623 'title' => _AT('delete_this_page'),
\r
624 'url' => $_base_href . 'home/editor/delete_content.php?_cid='.$content_row['content_id'],
\r
625 'icon' => $_base_href . 'images/page_delete.gif');
\r
627 else if ($content_row['content_type'] == CONTENT_TYPE_FOLDER) {
\r
628 $tool_shortcuts[] = array(
\r
629 'title' => _AT('add_sub_page'),
\r
630 'url' => $_base_href . 'home/editor/edit_content.php?_course_id='.$_course_id.SEP.'pid='.$content_row['content_id'],
\r
631 'icon' => $_base_href . 'images/add_sub_page.gif');
\r
633 $tool_shortcuts[] = array(
\r
634 'title' => _AT('delete_this_folder'),
\r
635 'url' => $_base_href . 'home/editor/delete_content.php?_cid='.$content_row['content_id'],
\r
636 'icon' => $_base_href . 'images/page_delete.gif');
\r
639 return $tool_shortcuts;
\r
641 // if (isset($_current_user) && $_current_user->isAuthor($_course_id)) {
\r
642 // $shortcuts[] = array('title' => _AT('add_sub_folder'), 'url' => $_base_href . 'home/editor/edit_content_folder.php?_course_id='.$_course_id.'pid='.$cid);
\r
644 //// $shortcuts[] = array('title' => _AT('add_top_page'), 'url' => $_base_href . 'home/editor/edit_content.php?_course_id='.$_course_id, 'icon' => $_base_href . 'images/page_add.gif');
\r
645 // if ($contentManager->_menu_info[$cid]['content_parent_id']) {
\r
646 // $shortcuts[] = array('title' => _AT('add_sibling_page'), 'url' => $_base_href .
\r
647 // 'home/editor/edit_content.php?_course_id='.$_course_id.SEP.'pid='.$contentManager->_menu_info[$cid]['content_parent_id'], 'icon' => $_base_href . 'images/page_add_sibling.gif');
\r
650 // $shortcuts[] = array('title' => _AT('add_sub_page'), 'url' => $_base_href . 'home/editor/edit_content.php?_course_id='.$_course_id.SEP.'pid='.$cid);
\r
651 // $shortcuts[] = array('title' => _AT('delete_this_folder'), 'url' => $_base_href . 'home/editor/delete_content.php?_cid='.$cid, 'icon' => $_base_href . 'images/page_delete.gif');
\r
656 * replace source object with alternatives according to user's preferences
\r
658 * @param $cid: content id.
\r
659 * @param $content: the original content page ($content_row['text'], from content.php).
\r
660 * @param $info_only: when "true", return the array of info (has_text_alternative, has_audio_alternative, has_visual_alternative, has_sign_lang_alternative)
\r
661 * @param $only_on_secondary_type:
\r
662 * @return string $content: the content page with the appropriated resources.
\r
663 * @see $db from include/vitals.inc.php
\r
664 * @author Cindy Qi Li
\r
666 public static function applyAlternatives($cid, $content, $info_only = false, $only_on_secondary_type = 0){
\r
667 global $db, $_course_id;
\r
669 include_once(TR_INCLUDE_PATH.'classes/DAO/DAO.class.php');
\r
672 $video_exts = array("mpg", "avi", "wmv", "mov", "swf", "mp3", "wav", "ogg", "mid", "mp4", "flv");
\r
673 $txt_exts = array("txt", "html", "htm");
\r
674 $image_exts = array("gif", "bmp", "png", "jpg", "jpeg", "png", "tif");
\r
675 $only_on_secondary_type = intval($only_on_secondary_type);
\r
677 // intialize the 4 returned values when $info_only is on
\r
680 $has_text_alternative = false;
\r
681 $has_audio_alternative = false;
\r
682 $has_visual_alternative = false;
\r
683 $has_sign_lang_alternative = false;
\r
686 if (!$info_only && !$only_on_secondary_type &&
\r
687 ($_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_TEXT']==0) &&
\r
688 ($_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_AUDIO']==0) &&
\r
689 ($_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_VISUAL']==0))
\r
691 //No user's preferences related to content format are declared
\r
695 return array($has_text_alternative, $has_audio_alternative, $has_visual_alternative, $has_sign_lang_alternative);
\r
699 // get all relations between primary resources and their alternatives
\r
700 $sql = "SELECT c.content_path, pr.resource, prt.type_id primary_type, sr.secondary_resource, srt.type_id secondary_type
\r
701 FROM ".TABLE_PREFIX."primary_resources pr, ".
\r
702 TABLE_PREFIX."primary_resources_types prt,".
\r
703 TABLE_PREFIX."secondary_resources sr,".
\r
704 TABLE_PREFIX."secondary_resources_types srt,".
\r
705 TABLE_PREFIX."content c
\r
706 WHERE pr.content_id=".$cid."
\r
707 AND pr.primary_resource_id = prt.primary_resource_id
\r
708 AND pr.primary_resource_id = sr.primary_resource_id
\r
709 AND sr.language_code='".$_SESSION['lang']."'
\r
710 AND sr.secondary_resource_id = srt.secondary_resource_id
\r
711 AND pr.content_id = c.content_id";
\r
712 if ($only_on_secondary_type > 0) {
\r
713 $sql .= " AND srt.type_id=".$only_on_secondary_type;
\r
715 $sql .= " ORDER BY pr.primary_resource_id, prt.type_id";
\r
717 $rows = $dao->execute($sql);
\r
719 if (!is_array($rows)) {
\r
723 return array($has_text_alternative, $has_audio_alternative, $has_visual_alternative, $has_sign_lang_alternative);
\r
727 foreach ($rows as $row)
\r
729 if ($info_only || $only_on_secondary_type ||
\r
730 ($_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_TEXT']==1 && $row['primary_type']==3 &&
\r
731 ($_SESSION['prefs']['PREF_ALT_TO_TEXT']=="audio" && $row['secondary_type']==1 ||
\r
732 $_SESSION['prefs']['PREF_ALT_TO_TEXT']=="visual" && $row['secondary_type']==4 ||
\r
733 $_SESSION['prefs']['PREF_ALT_TO_TEXT']=="sign_lang" && $row['secondary_type']==2)) ||
\r
735 ($_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_AUDIO']==1 && $row['primary_type']==1 &&
\r
736 ($_SESSION['prefs']['PREF_ALT_TO_AUDIO']=="visual" && $row['secondary_type']==4 ||
\r
737 $_SESSION['prefs']['PREF_ALT_TO_AUDIO']=="text" && $row['secondary_type']==3 ||
\r
738 $_SESSION['prefs']['PREF_ALT_TO_AUDIO']=="sign_lang" && $row['secondary_type']==2)) ||
\r
740 ($_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_VISUAL']==1 && $row['primary_type']==4 &&
\r
741 ($_SESSION['prefs']['PREF_ALT_TO_VISUAL']=="audio" && $row['secondary_type']==1 ||
\r
742 $_SESSION['prefs']['PREF_ALT_TO_VISUAL']=="text" && $row['secondary_type']==3 ||
\r
743 $_SESSION['prefs']['PREF_ALT_TO_VISUAL']=="sign_lang" && $row['secondary_type']==2))
\r
746 $ext = substr($row['secondary_resource'], strrpos($row['secondary_resource'], '.')+1);
\r
748 // alternative is video
\r
749 if (in_array($ext, $video_exts))
\r
750 $target = '[media]'.$row['secondary_resource'].'[/media]';
\r
751 // a text primary to be replaced by a visual alternative
\r
752 else if (in_array($ext, $txt_exts))
\r
754 if ($row['content_path'] <> '')
\r
755 $file_location = $row['content_path'].'/'.$row['secondary_resource'];
\r
757 $file_location = $row['secondary_resource'];
\r
759 $file = TR_CONTENT_DIR.$_SESSION['course_id'] . '/'.$file_location;
\r
760 $target = '<br />'.file_get_contents($file);
\r
762 // check whether html file
\r
763 if (preg_match('/.*\<html.*\<\/html\>.*/s', $target))
\r
764 { // is a html file, use iframe to display
\r
765 // get real path to the text file
\r
766 if (defined('TR_FORCE_GET_FILE') && TR_FORCE_GET_FILE) {
\r
767 $course_base_href = 'get.php/';
\r
769 $course_base_href = 'content/' . $_SESSION['course_id'] . '/';
\r
772 $file = TR_BASE_HREF . $course_base_href.$file_location;
\r
774 $target = '<iframe width="100%" frameborder="0" class="autoHeight" scrolling="auto" src="'.$file.'"></iframe>';
\r
777 { // is a text file, insert/replace into content
\r
778 $target = nl2br($target);
\r
781 else if (in_array($ext, $image_exts))
\r
782 $target = '<img border="0" alt="'._AT('alternate_text').'" src="'.$row['secondary_resource'].'"/>';
\r
785 $target = '<p><a href="'.$row['secondary_resource'].'">'.$row['secondary_resource'].'</a></p>';
\r
787 // replace or append the target alternative to the source
\r
788 if (($row['primary_type']==3 && $_SESSION['prefs']['PREF_ALT_TO_TEXT_APPEND_OR_REPLACE'] == 'replace') ||
\r
789 ($row['primary_type']==1 && $_SESSION['prefs']['PREF_ALT_TO_AUDIO_APPEND_OR_REPLACE']=='replace') ||
\r
790 ($row['primary_type']==4 && $_SESSION['prefs']['PREF_ALT_TO_VISUAL_APPEND_OR_REPLACE']=='replace'))
\r
791 $pattern_replace_to = '${1}'.$target.'${3}';
\r
793 $pattern_replace_to = '${1}${2}'.$target.'${3}';
\r
795 // *** Alternative replace/append starts from here ***
\r
796 $img_processed = false; // The indicator to tell the source image is found (or not)
\r
797 // and processed (or not) in an <img> tag. If found and processed,
\r
798 // SKIP the found/process for <a> tag because the source is a image
\r
799 // and <a> is very likely the tag wrapping around <img>
\r
801 // append/replace target alternative to [media]source[/media]
\r
802 if (preg_match("/".preg_quote("[media").".*".preg_quote("]".$row['resource']."[/media]", "/")."/sU", $content))
\r
805 $content = preg_replace("/(.*)(".preg_quote("[media").".*".preg_quote("]".$row['resource']."[/media]", "/").")(.*)/sU",
\r
806 $pattern_replace_to, $content);
\r
808 if ($row['secondary_type'] == 1) $has_audio_alternative = true;
\r
809 if ($row['secondary_type'] == 2) $has_sign_lang_alternative = true;
\r
810 if ($row['secondary_type'] == 3) $has_text_alternative = true;
\r
811 if ($row['secondary_type'] == 4) $has_visual_alternative = true;
\r
815 // append/replace target alternative to <img ... src="source" ...></a>
\r
816 if (preg_match("/\<img.*src=\"".preg_quote($row['resource'], "/")."\".*\/\>/sU", $content))
\r
818 $img_processed = true;
\r
820 $content = preg_replace("/(.*)(\<img.*src=\"".preg_quote($row['resource'], "/")."\".*\/\>)(.*)/sU",
\r
821 $pattern_replace_to, $content);
\r
823 if ($row['secondary_type'] == 1) $has_audio_alternative = true;
\r
824 if ($row['secondary_type'] == 2) $has_sign_lang_alternative = true;
\r
825 if ($row['secondary_type'] == 3) $has_text_alternative = true;
\r
826 if ($row['secondary_type'] == 4) $has_visual_alternative = true;
\r
830 // append/replace target alternative to <a>...source...</a> or <a ...source...>...</a>
\r
831 // skip this "if" when the source object has been processed in aboved <img> tag
\r
832 if (!$img_processed && preg_match("/\<a.*".preg_quote($row['resource'], "/").".*\<\/a\>/sU", $content))
\r
835 $content = preg_replace("/(.*)(\<a.*".preg_quote($row['resource'], "/").".*\<\/a\>)(.*)/sU",
\r
836 $pattern_replace_to, $content);
\r
838 if ($row['secondary_type'] == 1) $has_audio_alternative = true;
\r
839 if ($row['secondary_type'] == 2) $has_sign_lang_alternative = true;
\r
840 if ($row['secondary_type'] == 3) $has_text_alternative = true;
\r
841 if ($row['secondary_type'] == 4) $has_visual_alternative = true;
\r
845 // append/replace target alternative to <object ... source ...></object>
\r
846 if (preg_match("/\<object.*".preg_quote($row['resource'], "/").".*\<\/object\>/sU", $content))
\r
849 $content = preg_replace("/(.*)(\<object.*".preg_quote($row['resource'], "/").".*\<\/object\>)(.*)/sU",
\r
850 $pattern_replace_to, $content);
\r
852 if ($row['secondary_type'] == 1) $has_audio_alternative = true;
\r
853 if ($row['secondary_type'] == 2) $has_sign_lang_alternative = true;
\r
854 if ($row['secondary_type'] == 3) $has_text_alternative = true;
\r
855 if ($row['secondary_type'] == 4) $has_visual_alternative = true;
\r
859 // append/replace target alternative to <embed ... source ...>
\r
860 if (preg_match("/\<embed.*".preg_quote($row['resource'], "/").".*\>/sU", $content))
\r
863 $content = preg_replace("/(.*)(\<embed.*".preg_quote($row['resource'], "/").".*\>)(.*)/sU",
\r
864 $pattern_replace_to, $content);
\r
866 if ($row['secondary_type'] == 1) $has_audio_alternative = true;
\r
867 if ($row['secondary_type'] == 2) $has_sign_lang_alternative = true;
\r
868 if ($row['secondary_type'] == 3) $has_text_alternative = true;
\r
869 if ($row['secondary_type'] == 4) $has_visual_alternative = true;
\r
878 return array($has_text_alternative, $has_audio_alternative, $has_visual_alternative, $has_sign_lang_alternative);
\r
883 * This function save the last content_id accessed by current user on a course into db and set $_SESSION['s_cid']
\r
885 * @param: $content_id
\r
886 * @return: save $content_id, the last visited one of the current user, into db and session
\r
888 public static function saveLastCid($content_id)
\r
890 global $_course_id;
\r
892 if (!$content_id || !isset($_SESSION['user_id'])) return;
\r
894 include_once(TR_INCLUDE_PATH.'classes/DAO/UserCoursesDAO.class.php');
\r
896 $userCoursesDAO = new UserCoursesDAO();
\r
897 if ($userCoursesDAO->isExist($_SESSION['user_id'], $_course_id))
\r
899 $userCoursesDAO->UpdateLastCid($_SESSION['user_id'], $_course_id, $content_id);
\r
900 $_SESSION['s_cid'] = $content_id;
\r