#1252 few suggestions for sanitizer class

XOOPS_2.6.x
open
nobody
None
1
2013-01-31
2013-01-31
Alain91
No

results from unit tests on methods in this class

Index: module.textsanitizer.php
===================================================================
--- module.textsanitizer.php    (revision 596)
+++ module.textsanitizer.php    (working copy)
@@ -103,7 +103,7 @@
     {
         if (is_array($config_custom)) {
             foreach ($config_custom as $key => $val) {
-                if (is_array($config_default[$key])) {
+                if (array_key_exists($key,$config_default) AND is_array($config_default[$key])) {
                     $config_default[$key] = self::mergeConfig($config_default[$key], $config_custom[$key]);
                 } else {
                     $config_default[$key] = $val;
@@ -196,7 +196,7 @@
      */
     public $config = array();

-    public function __construct()
+    protected function __construct()
     {
         $this->path_basic = XOOPS_ROOT_PATH . '/class/textsanitizer';
         $this->path_plugin = XOOPS_ROOT_PATH . '/Frameworks/textsanitizer';
@@ -318,11 +318,21 @@
     static function truncate($text)
     {
         $instance = MyTextSanitizer::getInstance();
-        if (empty($text) || empty($instance->config['truncate_length']) || strlen($text) < $instance->config['truncate_length']) {
+       $truncate_length = empty($instance->config['truncate_length']) ? 0 : intval($instance->config['truncate_length']);
+       $length_text = strlen($text);
+        if (empty($text) || empty($truncate_length) || $length_text <= $truncate_length) {
             return $text;
         }
-        $len = floor($instance->config['truncate_length'] / 2);
-        $ret = substr($text, 0, $len) . ' ... ' . substr($text, 5 - $len);
+       $center = ' ... ';
+       $length_center = strlen($center);
+       if ($length_text <= $length_center) {
+           return $text;
+       }
+       if (($truncate_length - $length_center) <= 2) {
+           return substr($text, 0, $truncate_length) ;
+       }
+        $len = intval(($truncate_length - $length_center) / 2);
+        $ret = substr($text, 0, $len) . $center . substr($text, -$len);
         return $ret;
     }

@@ -331,43 +341,41 @@
      *
      * @param string $text
      * @param int $allowimage Allow images in the text?
-     *                                                           On FALSE, uses links to images.
+     * On FALSE, uses links to images.
      * @return string
      */
     public function xoopsCodeDecode(&$text, $allowimage = 1)
     {
         $patterns = array();
         $replacements = array();
-        $patterns[] = "/\[siteurl=(['\"]?)([^\"'<>]*)\\1](.*)\[\/siteurl\]/sU";
+        $patterns[] = "/\[siteurl=(['\"]?)([^\"'<>]*)\\1\](.*)\[\/siteurl\]/sU";
         $replacements[] = '<a href="' . XOOPS_URL . '/\\2" title="">\\3</a>';
-        $patterns[] = "/\[url=(['\"]?)(http[s]?:\/\/[^\"'<>]*)\\1](.*)\[\/url\]/sU";
+        $patterns[] = "/\[url=(['\"]?)((?:ht|f)tps?:\/\/[^\"'<>]*)\\1\](.*)\[\/url\]/sU";
         $replacements[] = '<a href="\\2" rel="external" title="">\\3</a>';
-        $patterns[] = "/\[url=(['\"]?)(ftp?:\/\/[^\"'<>]*)\\1](.*)\[\/url\]/sU";
-        $replacements[] = '<a href="\\2" rel="external" title="">\\3</a>';
-        $patterns[] = "/\[url=(['\"]?)([^'\"<>]*)\\1](.*)\[\/url\]/sU";
+        $patterns[] = "/\[url=(['\"]?)([^'\"<>]*)\\1\](.*)\[\/url\]/sU";
         $replacements[] = '<a href="http://\\2" rel="external" title="">\\3</a>';
-        $patterns[] = "/\[color=(['\"]?)([a-zA-Z0-9]*)\\1](.*)\[\/color\]/sU";
+        $patterns[] = "/\[color=(['\"]?)([a-zA-Z0-9]*)\\1\](.*)\[\/color\]/sU";
         $replacements[] = '<span style="color: #\\2;">\\3</span>';
-        $patterns[] = "/\[size=(['\"]?)([a-z0-9-]*)\\1](.*)\[\/size\]/sU";
+        $patterns[] = "/\[size=(['\"]?)([a-zA-Z0-9-]*)\\1\](.*)\[\/size\]/sU";
         $replacements[] = '<span style="font-size: \\2;">\\3</span>';
-        $patterns[] = "/\[font=(['\"]?)([^;<>\*\(\)\"']*)\\1](.*)\[\/font\]/sU";
+        $patterns[] = "/\[font=(['\"]?)([^;<>\*\(\)\"']*)\\1\](.*)\[\/font\]/sU";
         $replacements[] = '<span style="font-family: \\2;">\\3</span>';
-        $patterns[] = "/\[email]([^;<>\*\(\)\"']*)\[\/email\]/sU";
+        $patterns[] = "/\[email\]([^;<>\*\(\)\"']*)\[\/email\]/sU";
         $replacements[] = '<a href="mailto:\\1" title="">\\1</a>';

-        $patterns[] = "/\[b](.*)\[\/b\]/sU";
+        $patterns[] = "/\[b\](.*)\[\/b\]/sU";
         $replacements[] = '<strong>\\1</strong>';
-        $patterns[] = "/\[i](.*)\[\/i\]/sU";
+        $patterns[] = "/\[i\](.*)\[\/i\]/sU";
         $replacements[] = '<em>\\1</em>';
-        $patterns[] = "/\[u](.*)\[\/u\]/sU";
+        $patterns[] = "/\[u\](.*)\[\/u\]/sU";
         $replacements[] = '<u>\\1</u>';
-        $patterns[] = "/\[d](.*)\[\/d\]/sU";
+        $patterns[] = "/\[d\](.*)\[\/d\]/sU";
         $replacements[] = '<del>\\1</del>';
-        $patterns[] = "/\[center](.*)\[\/center\]/sU";
+        $patterns[] = "/\[center\](.*)\[\/center\]/sU";
         $replacements[] = '<div style="text-align: center;">\\1</div>';
-        $patterns[] = "/\[left](.*)\[\/left\]/sU";
+        $patterns[] = "/\[left\](.*)\[\/left\]/sU";
         $replacements[] = '<div style="text-align: left;">\\1</div>';
-        $patterns[] = "/\[right](.*)\[\/right\]/sU";
+        $patterns[] = "/\[right\](.*)\[\/right\]/sU";
         $replacements[] = '<div style="text-align: right;">\\1</div>';

         $this->text = $text;
@@ -453,7 +461,7 @@
     }

     /**
-     * if magic_quotes_gpc is on, stirip back slashes
+     * if magic_quotes_gpc is on, strip back slashes
      *
      * @param string $text
      * @return string
@@ -658,6 +666,7 @@
     public function executeExtension($name)
     {
         $extension = $this->loadExtension($name);
+       if (!$extension) return false;
         $args = array_slice(func_get_args(), 1);
         return call_user_func_array(array($extension , 'load'), array_merge(array(&$this), $args));
     }
@@ -674,7 +683,7 @@
     {
         $ret = $this->executeExtension('textfilter', $text, $force);
         if ($ret === false) {
-            return $text;
+            return $text; // alain91: bizarre de retourner le texte inchange
         }
         return $ret;
     }

Discussion