make_clickable(): improve regular expression of converting URL to ignore the beginnin...
[acontent.git] / docs / include / lib / output.inc.php
index e4760d4..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);
@@ -302,10 +302,10 @@ function AT_date($format='%Y-%M-%d', $timestamp = '', $formTR_type=TR_DATE_MYSQL
        /**
        *       Transforms text based on formatting preferences.  Original $input is also changed (passed by reference).
        *       Can be called as:
-       *       1) $output = TR_print($input, $name);
+       *       1) $output = AT_print($input, $name);
        *          echo $output;
        *
-       *       2) echo TR_print($input, $name); // prefered method
+       *       2) echo AT_print($input, $name); // prefered method
        *
        * @access       public
        * @param        string $input                   text being transformed
@@ -317,7 +317,7 @@ function AT_date($format='%Y-%M-%d', $timestamp = '', $formTR_type=TR_DATE_MYSQL
        * @see          query_bit()                             in include/vitals.inc.php
        * @author       Joel Kronenberg
        */
-       function TR_print($input, $name, $runtime_html = true) {
+       function AT_print($input, $name, $runtime_html = true) {
                global $_field_formatting;
 
                if (!isset($_field_formatting[$name])) {
@@ -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;