make_clickable(): improve regular expression of converting URL to ignore the beginnin...
[acontent.git] / docs / include / lib / output.inc.php
index 3b54b05..6aa0d81 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /************************************************************************/
-/* Transformable                                                        */
+/* AContent                                                             */
 /************************************************************************/
-/* Copyright (c) 2009                                                   */
-/* Adaptive Technology Resource Centre / University of Toronto          */
+/* Copyright (c) 2010                                                   */
+/* Inclusive Design Institute                                           */
 /*                                                                      */
 /* This program is free software. You can redistribute it and/or        */
 /* modify it under the terms of the GNU General Public License          */
@@ -166,7 +166,7 @@ function _AT() {
        TR_DATE_UNIX_TIMESTAMP:         seconds since epoch
        TR_DATE_INDEX_VALUE:            0-x, index into a date array
 */
-function AT_date($format='%Y-%M-%d', $timestamp = '', $formTR_type=TR_DATE_MYSQL_DATETIME) {   
+function AT_date($format='%Y-%M-%d', $timestamp = '', $format_type=TR_DATE_MYSQL_DATETIME) {   
        static $day_name_ext, $day_name_con, $month_name_ext, $month_name_con;
        global $_config;
 
@@ -214,7 +214,7 @@ function AT_date($format='%Y-%M-%d', $timestamp = '', $formTR_type=TR_DATE_MYSQL
                                                                'date_dec');
        }
 
-       if ($formTR_type == TR_DATE_INDEX_VALUE) {
+       if ($format_type == TR_DATE_INDEX_VALUE) {
                // apply timezone offset
                apply_timezone($timestamp);
        
@@ -231,11 +231,11 @@ function AT_date($format='%Y-%M-%d', $timestamp = '', $formTR_type=TR_DATE_MYSQL
 
        if ($timestamp == '') {
                $timestamp = time();
-               $formTR_type = TR_DATE_UNIX_TIMESTAMP;
+               $format_type = TR_DATE_UNIX_TIMESTAMP;
        }
 
        /* convert the date to a Unix timestamp before we do anything with it */
-       if ($formTR_type == TR_DATE_MYSQL_DATETIME) {
+       if ($format_type == TR_DATE_MYSQL_DATETIME) {
                $year   = substr($timestamp,0,4);
                $month  = substr($timestamp,5,2);
                $day    = substr($timestamp,8,2);
@@ -244,7 +244,7 @@ function AT_date($format='%Y-%M-%d', $timestamp = '', $formTR_type=TR_DATE_MYSQL
                $sec    = substr($timestamp,17,2);
                $timestamp      = mktime($hour, $min, $sec, $month, $day, $year);
 
-       } else if ($formTR_type == TR_DATE_MYSQL_TIMESTAMP_14) {
+       } else if ($format_type == TR_DATE_MYSQL_TIMESTAMP_14) {
            $year               = substr($timestamp,0,4);
            $month              = substr($timestamp,4,2);
            $day                = substr($timestamp,6,2);
@@ -669,12 +669,23 @@ function embed_media($text) {
 function make_clickable($text) {
        $text = embed_media($text);
 
-       $text = preg_replace("/([\s])(http[s]?):\/\/([\^\s\<]*)([a-zA-Z0-9\#\?\/\&\=])/i", 
-                            "\\1<a href=\"\\2://\\3\\4\">\\3\\4</a>", $text);
+       // The next 3 preg_replace convert plain text URL to clickable URL.
+       // limited conversion. It doesn't cover the case when the stuff in front of the URL is not a word. For example:
+       // <p>http://google.ca</p>
+       // "http://google.ca"  
+       // 1. remove the spaces in [media] tag, otherwise, the next line converts URL inside [media] into <a> tag
+       $text = preg_replace("/(\[media\])([\s]*)(.*)(\[\/media\])/", '$1$3$4', $text);
+       $text = preg_replace("/(\[media\])(.*)([\s]*)(\[\/media\])/U", '$1$2$4', $text);
+       // 2. convert URL
+       $text = preg_replace('/(^|[\n ])([\w]*?[\"]*)((?<!(\[media\]))http(s)?:\/\/[\w]+[^ \,\"\n\r\t\)<]*)/is', 
+                            '$1$2<a href="$3">$3</a>', $text);
        
-       $text = preg_replace('/([_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.
-                                               '\@'.'[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.'(\.[a-zA-Z]{1,6})+)/i',
-                                               "<a href=\"mailto:\\1\">\\1</a>",
+       // convert email address to clickable URL that pops up "send email" interface with the address filled in
+       $text = preg_replace('/(?|<a href="mailto[\s]*:[\s]*([_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.'\@'
+                            .'[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.'(\.[a-zA-Z]{1,6})+)">(.*)<\/a>'
+                            .'|((((([_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.'\@'
+                            .'[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*'.'(\.[a-zA-Z]{1,6})+))))))/i',
+                                               "<a href=\"mailto:\\1\">\\5</a>",
                                                $text);
        
        return $text;