Changeset 505


Ignore:
Timestamp:
Mar 23, 2015 10:06:42 PM (9 years ago)
Author:
anonymous
Message:

Added hyperlinkTxt(). Fixed setParam() to apply some settings during runtime.

Location:
trunk/lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/App.inc.php

    r504 r505  
    131131        'error_reporting' => E_ALL,
    132132
    133         // Don't display errors by default; it is preferable to log them to a file.
     133        // Don't display errors by default; it is preferable to log them to a file. For CLI scripts, set this to the string 'stderr'.
    134134        'display_errors' => false,
    135135
     
    224224            // Merge new parameters with old overriding only those passed.
    225225            $this->_params = array_merge($this->_params, $param);
     226
     227            if ($this->running) {
     228                // Params that require processing if changed during runtime.
     229                foreach ($param as $key => $val) {
     230                    switch ($key) {
     231                    case 'session_name':
     232                        session_name($val);
     233                        break;
     234
     235                    case 'session_use_cookies':
     236                        ini_set('session.use_cookies', $val);
     237                        break;
     238
     239                    case 'error_reporting':
     240                        ini_set('error_reporting', $val);
     241                        break;
     242
     243                    case 'display_errors':
     244                        ini_set('display_errors', $val);
     245                        break;
     246
     247                    case 'log_errors':
     248                        ini_set('log_errors', true);
     249                        break;
     250
     251                    case 'log_directory':
     252                        if (is_dir($val) && is_writable($val)) {
     253                            ini_set('error_log', $val . '/' . $this->getParam('php_error_log'));
     254                        }
     255                        break;
     256                    }
     257                }
     258            }
    226259        }
    227260    }
  • trunk/lib/Utilities.inc.php

    r502 r505  
    135135
    136136/**
    137  * Returns text with appropriate html translations.
     137 * Returns text with appropriate html translations (a smart wrapper for htmlspecialchars()).
    138138 *
    139139 * @param  string $text             Text to clean.
     
    208208
    209209    return preg_replace($search, $replace, $text);
     210}
     211
     212/*
     213* Finds all URLs in text and hyperlinks them.
     214*
     215* @access   public
     216* @param    string  $text   Text to search for URLs.
     217* @param    mixed   $length Number of characters to truncate URL, or NULL to disable truncating.
     218* @param    string  $delim  Delimiter to append, indicate truncation.
     219* @return   string          Same input text, but URLs hyperlinked.
     220* @author   Quinn Comendant <quinn@strangecode.com>
     221* @version  1.0
     222* @since    22 Mar 2015 23:29:04
     223*/
     224function hyperlinkTxt($text, $length=null, $delim='
')
     225{
     226    return preg_replace_callback(
     227        // Inspired by @stephenhay's regex from https://mathiasbynens.be/demo/url-regex
     228        // Here we capture the full URL into the first match and only the first X characters into the second match.
     229        sprintf('@\b(?<!")(?<!\')(?<!=)(((?:https?|s?ftps?)://[^\s/$.?#].[^\s]{0,%s})[^\s]*)@iS', $length),
     230        // Use an anonymous function to decide when to append the delim.
     231        // Also encode special chars with oTxt().
     232        function ($m) use ($length, $delim) {
     233            if (is_null($length) || $m[1] == $m[2]) {
     234                // If not truncating, or URL was not truncated.
     235                return sprintf('<a href="%s">%s</a>', oTxt($m[1]), oTxt($m[1]));
     236            } else {
     237                // Truncated URL.
     238                return sprintf('<a href="%s">%s%s</a>', oTxt($m[1]), oTxt(trim($m[2])), $delim);
     239            }
     240        },
     241        $text
     242    );
    210243}
    211244
Note: See TracChangeset for help on using the changeset viewer.