http://atutor.ca/atutor/mantis/view.php?id=4574
[acontent.git] / docs / home / classes / ContentUtility.class.php
index 112b733..21e8e4d 100644 (file)
@@ -204,6 +204,33 @@ class ContentUtility {
                return $text;           \r
        }\r
        \r
+    /*\r
+     * This function converts the youtube playable url used in <object> tag (for instance: http://www.youtube.com/v/a0ryB0m0MiM)\r
+     * to youtube url that is used to browse (for instance: http://www.youtube.com/watch?v=a0ryB0m0MiM)\r
+     * @param: youtube playable URL. For instance, http://www.youtube.com/v/a0ryB0m0MiM\r
+     * @return: if the param is a youtube playable url, return the according youtube URL used to browse. \r
+     *          For instance: http://www.youtube.com/watch?v=a0ryB0m0MiM\r
+     *          Otherwise, return the original send-in parameter.\r
+     */\r
+    function convertYoutubePlayURLToWatchURL($youtube_playURL) {\r
+        return preg_replace("/(http:\/\/[a-z0-9\.]*)?youtube.com\/v\/(.*)/",\r
+                            "\\1youtube.com/watch?v=\\2", $youtube_playURL);\r
+    }\r
+\r
+    /*\r
+     * This function converts the youtube url that is used to browse (for instance: http://www.youtube.com/watch?v=a0ryB0m0MiM)\r
+     * to youtube playable url used in <object> tag (for instance: http://www.youtube.com/v/a0ryB0m0MiM)\r
+     * @param: the youtube URL used to browse. \r
+     *         For instance: http://www.youtube.com/watch?v=a0ryB0m0MiM\r
+     * @return: if the param is a youtube url used to browse, return the according youtube playable URL. \r
+     *          For instance, http://www.youtube.com/v/a0ryB0m0MiM\r
+     *          Otherwise, return the original send-in parameter.\r
+     */\r
+    function convertYoutubeWatchURLToPlayURL($youtube_watchURL) {\r
+        return preg_replace("/(http:\/\/[a-z0-9\.]*)?youtube.com\/watch\?v=(.*)/",\r
+                            "\\1youtube.com/v/\\2", $youtube_watchURL);\r
+    }\r
+\r
        public static function embedMedia($text) {\r
                if (preg_match("/\[media(\|[0-9]+\|[0-9]+)?\]*/", $text)==0){\r
                        return $text;\r
@@ -215,19 +242,14 @@ class ContentUtility {
                \r
                $media_matches = Array();\r
                \r
-               /*\r
-                       First, we search though the text for all different kinds of media defined by media tags and store the results in $media_matches.\r
-                       \r
-                       Then the different replacements for the different media tags are stored in $media_replace.\r
-                       \r
-                       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
-                       \r
-               */\r
+               // First, we search though the text for all different kinds of media defined by media tags and store the results in $media_matches.\r
+        // Then the different replacements for the different media tags are stored in $media_replace.\r
+        // 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
                \r
                // youtube videos\r
-               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
-               $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
-                       \r
+        preg_match_all("#\[media[0-9a-z\|]*\]http://([a-z0-9\.]*)?youtube.com/watch\?v=(.*)\[/media\]#iU",$text,$media_matches[1],PREG_SET_ORDER);\r
+        $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
+                               \r
                // .mpg\r
                preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+).mpg\[/media\]#i",$text,$media_matches[2],PREG_SET_ORDER);\r
                $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
@@ -240,29 +262,25 @@ class ContentUtility {
                preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+).wmv\[/media\]#i",$text,$media_matches[4],PREG_SET_ORDER);\r
                $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
                \r
-               // .mov\r
-               preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+).mov\[/media\]#i",$text,$media_matches[5],PREG_SET_ORDER);\r
-               $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
-               \r
                // .swf\r
-               preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+).swf\[/media\]#i",$text,$media_matches[6],PREG_SET_ORDER);\r
-               $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
-               \r
-               // .mp3\r
-               preg_match_all("#\[media[0-9a-z\|]*\](.+[^\s\"]+).mp3\[/media\]#i",$text,$media_matches[7],PREG_SET_ORDER);\r
-               $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
+               preg_match_all("#\[media[0-9a-z\|]*\]([.\w\d]+[^\s\"]+).swf\[/media\]#i",$text,$media_matches[5],PREG_SET_ORDER);\r
+               $media_replace[5] = "<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
                \r
                // .wav\r
-               preg_match_all("#\[media[0-9a-z\|]*\](.+[^\s\"]+).wav\[/media\]#i",$text,$media_matches[8],PREG_SET_ORDER);\r
-               $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
+               preg_match_all("#\[media[0-9a-z\|]*\](.+[^\s\"]+).wav\[/media\]#i",$text,$media_matches[6],PREG_SET_ORDER);\r
+               $media_replace[6] ="<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
                \r
                // .ogg\r
-               preg_match_all("#\[media[0-9a-z\|]*\](.+[^\s\"]+).ogg\[/media\]#i",$text,$media_matches[9],PREG_SET_ORDER);\r
-               $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
+               preg_match_all("#\[media[0-9a-z\|]*\](.+[^\s\"]+).ogg\[/media\]#i",$text,$media_matches[7],PREG_SET_ORDER);\r
+               $media_replace[7] ="<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
+\r
+        // .ogm\r
+        preg_match_all("#\[media[0-9a-z\|]*\](.+[^\s\"]+).ogm\[/media\]#i",$text,$media_matches[8],PREG_SET_ORDER);\r
+        $media_replace[8] ="<object type=\"application/ogm\" data=\"##MEDIA1##.ogm\" width=\"##WIDTH##\" height=\"##HEIGHT##\"><param name=\"src\" value=\"##MEDIA1##.ogm\"><a href=\"##MEDIA1##.ogg\">##MEDIA1##.ogm</a></object>";\r
                \r
                // .mid\r
-               preg_match_all("#\[media[0-9a-z\|]*\](.+[^\s\"]+).mid\[/media\]#i",$text,$media_matches[10],PREG_SET_ORDER);\r
-               $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
+               preg_match_all("#\[media[0-9a-z\|]*\](.+[^\s\"]+).mid\[/media\]#i",$text,$media_matches[9],PREG_SET_ORDER);\r
+               $media_replace[9] ="<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
                \r
                $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
        \r
@@ -535,8 +553,8 @@ class ContentUtility {
                                                        $input);\r
                                } else {\r
                                        $input = preg_replace\r
-                                                       ("/(\[\?\])$term(\[\/\?\])/i",\r
-                                                       '\\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
+                                               ("/(\[\?\])$term(\[\/\?\])/i",\r
+                                               '\\2<sup><a class="tooltip" href="'.$_base_path.'mods/_core/glossary/index.php?g_cid='.$_SESSION['s_cid'].htmlentities(SEP).'w='.urlencode($original_term).'#term" title="'.addslashes($original_term).': '.$def.'">?</a></sup>',$input);\r
                                }\r
                        }\r
                } else if (!$user_glossary) {\r
@@ -703,7 +721,19 @@ class ContentUtility {
                include_once(TR_INCLUDE_PATH.'classes/DAO/DAO.class.php');\r
                $dao = new DAO();\r
                \r
-               $video_exts = array("mpg", "avi", "wmv", "mov", "swf", "mp3", "wav", "ogg", "mid", "mp4", "flv");\r
+               // All a4a resources have "&" converted to "&amp;". To match content with resources, \r
+        // need the same conversion on content. \r
+        $content = convertAmp($content);\r
+        \r
+        // keep &lt; (content saved in plain text format) as it is instead of &amp;lt;\r
+        $content = str_replace('&amp;lt;', '&lt;', $content);\r
+        \r
+        $video_exts = array("mpg", "avi", "wmv", "mov", "swf", "mp4", "flv");\r
+        \r
+        $audio_exts = array("mp3", "wav", "ogg", "mid");\r
+        $audio_width = 425;\r
+        $audio_height = 27;\r
+\r
                $txt_exts = array("txt", "html", "htm");\r
                $image_exts = array("gif", "bmp", "png", "jpg", "jpeg", "png", "tif");\r
                $only_on_secondary_type = intval($only_on_secondary_type);\r
@@ -729,9 +759,16 @@ class ContentUtility {
                                return array($has_text_alternative, $has_audio_alternative, $has_visual_alternative, $has_sign_lang_alternative);\r
                        }\r
                }\r
-               \r
                // get all relations between primary resources and their alternatives\r
-               $sql = "SELECT c.content_path, pr.resource, prt.type_id primary_type, sr.secondary_resource, srt.type_id secondary_type\r
+        $sql = "SELECT DISTINCT c.content_path, pr.resource, ";\r
+        \r
+        // ignore primary resource type if the request is on particular secondary type.\r
+        // otherwise, if the primary resource is defined with multiple primary types, \r
+        // the primary resource would be replaced/appended multiple times.\r
+        if ($only_on_secondary_type == 0) {\r
+            $sql .= "prt.type_id primary_type, ";\r
+        }\r
+        $sql .= "sr.secondary_resource, srt.type_id secondary_type\r
                          FROM ".TABLE_PREFIX."primary_resources pr, ".\r
                                 TABLE_PREFIX."primary_resources_types prt,".\r
                                 TABLE_PREFIX."secondary_resources sr,".\r
@@ -758,7 +795,18 @@ class ContentUtility {
                        }\r
                }\r
                \r
-               foreach ($rows as $row) \r
+               foreach ($rows as $row) {\r
+            $alternative_rows[] = $row;\r
+            \r
+            $youtube_playURL = ContentUtility::convertYoutubeWatchURLToPlayURL($row['resource']);\r
+            \r
+            if ($row['resource'] <> $youtube_playURL) {\r
+                $row['resource'] = $youtube_playURL;\r
+                $alternative_rows[] = $row;\r
+            }\r
+        }\r
+\r
+        foreach ($alternative_rows as $row) \r
                {\r
                        if ($info_only || $only_on_secondary_type ||\r
                            ($_SESSION['prefs']['PREF_USE_ALTERNATIVE_TO_TEXT']==1 && $row['primary_type']==3 &&\r
@@ -780,8 +828,18 @@ class ContentUtility {
                                $ext = substr($row['secondary_resource'], strrpos($row['secondary_resource'], '.')+1);\r
                                \r
                                // alternative is video\r
-                               if (in_array($ext, $video_exts))\r
-                                       $target = '[media]'.$row['secondary_resource'].'[/media]';\r
+                               if (in_array($ext, $video_exts)|| in_array($ext, $audio_exts) || \r
+                    preg_match("/http:\/\/.*youtube.com\/watch.*/", $row['secondary_resource'])) {\r
+                    if (in_array($ext, $audio_exts)) {\r
+                        // display audio medias in a smaller width/height (425 * 27)\r
+                        // A hack for now to handle audio media player size\r
+                        $target = '[media|'.$audio_width.'|'.$audio_height.']'.$row['secondary_resource'].'[/media]';\r
+                    } else {\r
+                        // use default media size for video medias\r
+                        $target = '[media]'.$row['secondary_resource'].'[/media]';\r
+                    }\r
+                    $target = ContentUtility::embedFLV(ContentUtility::embedMedia($target));\r
+                }\r
                                // a text primary to be replaced by a visual alternative \r
                                else if (in_array($ext, $txt_exts))\r
                                {\r
@@ -822,19 +880,17 @@ class ContentUtility {
                                if (($row['primary_type']==3 && $_SESSION['prefs']['PREF_ALT_TO_TEXT_APPEND_OR_REPLACE'] == 'replace') ||\r
                                        ($row['primary_type']==1 && $_SESSION['prefs']['PREF_ALT_TO_AUDIO_APPEND_OR_REPLACE']=='replace') ||\r
                                        ($row['primary_type']==4 && $_SESSION['prefs']['PREF_ALT_TO_VISUAL_APPEND_OR_REPLACE']=='replace'))\r
-                                       $pattern_replace_to = '${1}'.$target.'${3}';\r
-                               else\r
-                                       $pattern_replace_to = '${1}${2}'.$target.'${3}';\r
-       \r
-                               // *** Alternative replace/append starts from here ***\r
-                               $img_processed = false;    // The indicator to tell the source image is found (or not) \r
-                                                          // and processed (or not) in an <img> tag. If found and processed, \r
-                                                          // SKIP the found/process for <a> tag because the source is a image\r
-                                                          // and <a> is very likely the tag wrapping around <img>\r
+                                       $pattern_replace_to = '${1}'."\n".$target."\n".'${3}';\r
+                else\r
+                    $pattern_replace_to = '${1}${2}'."<br /><br />\n".$target."\n".'${3}';\r
 \r
-                               // append/replace target alternative to [media]source[/media]\r
-                               if (preg_match("/".preg_quote("[media").".*".preg_quote("]".$row['resource']."[/media]", "/")."/sU", $content))\r
-                               {\r
+                // *** Alternative replace/append starts from here ***\r
+                $processed = false;    // one primary resource is only processed once \r
+                \r
+                // append/replace target alternative to [media]source[/media]\r
+                if (!$processed && preg_match("/".preg_quote("[media").".*".preg_quote("]".$row['resource']."[/media]", "/")."/sU", $content))\r
+                {\r
+                    $processed = true;\r
                                        if (!$info_only) {\r
                                                $content = preg_replace("/(.*)(".preg_quote("[media").".*".preg_quote("]".$row['resource']."[/media]", "/").")(.*)/sU", \r
                                             $pattern_replace_to, $content);\r
@@ -847,9 +903,9 @@ class ContentUtility {
                                }\r
                                \r
                                // append/replace target alternative to <img ... src="source" ...></a>\r
-                               if (preg_match("/\<img.*src=\"".preg_quote($row['resource'], "/")."\".*\/\>/sU", $content))\r
-                               {\r
-                                       $img_processed = true;\r
+                               if (!$processed && preg_match("/\<img.*src=\"".preg_quote($row['resource'], "/")."\".*\/\>/sU", $content))\r
+                {\r
+                    $processed = true;\r
                                        if (!$info_only) {\r
                                                $content = preg_replace("/(.*)(\<img.*src=\"".preg_quote($row['resource'], "/")."\".*\/\>)(.*)/sU", \r
                                                        $pattern_replace_to, $content);\r
@@ -861,12 +917,12 @@ class ContentUtility {
                                        }\r
                                }\r
                                \r
-                               // append/replace target alternative to <a>...source...</a> or <a ...source...>...</a>\r
-                               // skip this "if" when the source object has been processed in aboved <img> tag\r
-                               if (!$img_processed && preg_match("/\<a.*".preg_quote($row['resource'], "/").".*\<\/a\>/sU", $content))\r
-                               {\r
-                                       if (!$info_only) {\r
-                                               $content = preg_replace("/(.*)(\<a.*".preg_quote($row['resource'], "/").".*\<\/a\>)(.*)/sU", \r
+                               // append/replace target alternative to <object ... source ...></object>\r
+                if (!$processed && preg_match("/\<object.*".preg_quote($row['resource'], "/").".*\<\/object\>/sU", $content))\r
+                {\r
+                    $processed = true;\r
+                    if (!$info_only) {\r
+                        $content = preg_replace("/(.*)(\<object.*".preg_quote($row['resource'], "/").".*\<\/object\>)(.*)/sU", \r
                                                        $pattern_replace_to, $content);\r
                                        } else {\r
                                                if ($row['secondary_type'] == 1) $has_audio_alternative = true;\r
@@ -876,11 +932,13 @@ class ContentUtility {
                                        }\r
                                }\r
        \r
-                               // append/replace target alternative to <object ... source ...></object>\r
-                               if (preg_match("/\<object.*".preg_quote($row['resource'], "/").".*\<\/object\>/sU", $content))\r
-                               {\r
-                                       if (!$info_only) {\r
-                                               $content = preg_replace("/(.*)(\<object.*".preg_quote($row['resource'], "/").".*\<\/object\>)(.*)/sU", \r
+                               // append/replace target alternative to <a>...source...</a> or <a ...source...>...</a>\r
+                // skip this "if" when the source object has been processed in aboved <img> tag\r
+                if (!$processed && preg_match("/\<a.*".preg_quote($row['resource'], "/").".*\<\/a\>/sU", $content))\r
+                {\r
+                    $processed = true;\r
+                    if (!$info_only) {\r
+                        $content = preg_replace("/(.*)(\<a.*".preg_quote($row['resource'], "/").".*\<\/a\>)(.*)/sU",\r
                                                        $pattern_replace_to, $content);\r
                                        } else {\r
                                                if ($row['secondary_type'] == 1) $has_audio_alternative = true;\r
@@ -891,8 +949,9 @@ class ContentUtility {
                                }\r
        \r
                                // append/replace target alternative to <embed ... source ...>\r
-                               if (preg_match("/\<embed.*".preg_quote($row['resource'], "/").".*\>/sU", $content))\r
-                               {\r
+                               if (!$processed && preg_match("/\<embed.*".preg_quote($row['resource'], "/").".*\>/sU", $content))\r
+                {\r
+                    $processed = true;\r
                                        if (!$info_only) {\r
                                                $content = preg_replace("/(.*)(\<embed.*".preg_quote($row['resource'], "/").".*\>)(.*)/sU", \r
                                                        $pattern_replace_to, $content);\r