]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-11048 + MDL-11297 improved tag normalisation, more security
authorskodak <skodak>
Sun, 16 Sep 2007 18:41:57 +0000 (18:41 +0000)
committerskodak <skodak>
Sun, 16 Sep 2007 18:41:57 +0000 (18:41 +0000)
lib/moodlelib.php
tag/lib.php

index a55e79f9b5d9ec3e9c01aa4f42c93c50c1960d2c..d29c2c9f552c02c2aa143663dfe2780e7727f8c3 100644 (file)
@@ -256,6 +256,11 @@ define ('BLOG_COURSE_LEVEL', 3);
 define ('BLOG_SITE_LEVEL', 4);
 define ('BLOG_GLOBAL_LEVEL', 5);
 
+/**
+ * Tag constanst
+ */
+define('TAG_MAX_LENGTH', 50);
+
 
 
 /// PARAMETER HANDLING ////////////////////////////////////////////////////
@@ -555,7 +560,10 @@ function clean_param($param, $type) {
             $param = preg_replace("/[\\x{80}-\\x{bf}\\x{d7}\\x{f7}]/u", '', $param);
             //cleanup the spaces
             $param = preg_replace('/ +/', ' ', $param);
-            return trim($param);
+            $param = trim($param);
+            $textlib = new textlib();
+            return $textlib->substr($param, 0, TAG_MAX_LENGTH);
+            
 
         case PARAM_TAGLIST:
             $tags = explode(',', $param);
index 653466a0ae189f1b7a09b9f3d230ce9a2ed11b79..b973ed0642e6b337617168097d5b7e8e0976dc57 100644 (file)
@@ -1,7 +1,6 @@
 <?php
     
 define('DEFAULT_TAG_TABLE_FIELDS', 'id, tagtype, name, rawname, flag');
-define('MAX_TAG_LENGTH',50);
 
 /**
  * Creates tags
@@ -956,7 +955,7 @@ function tag_instance_table_cleanup() {
 
 
 /**
- * Function that normalizes a tag name
+ * Function that normalizes a list of tag names
  * 
  * Ex: tag_normalize('bANAana')   -> returns 'banana'
  *        tag_normalize('lots    of    spaces') -> returns 'lots of spaces'
@@ -969,40 +968,13 @@ function tag_instance_table_cleanup() {
  */
 
 function tag_normalize($tag_names_csv, $lowercase=true) {
+    $tag_names_csv = clean_param($tag_names_csv, PARAM_TAGLIST);
 
-    $textlib = textlib_get_instance();
-    
-    $tags = explode(',', $tag_names_csv);
-    
-    if (sizeof($tags) > 1) {
-
-        foreach ($tags as $key => $tag) {
-            $tags[$key] = tag_normalize($tag);
-        }
-
-        return implode(',' , $tags);
-
-    }
-
-    // only one tag was passed
-    else {
-
-        if ($lowercase){
-            $value = moodle_strtolower($tag_names_csv);
-        }
-        else {
-            $value = $tag_names_csv;
-        }
-
-        //$value = preg_replace('|[^\w ]|i', '', strtolower(trim($tag_names_csv)));
-        $value = preg_replace('|[\,\!\@\#\$\%\^\&\*\(\)\-\+\=\~\`\\"\'\_.\[\]\{\}\:\;\?\ยด\^\\\/\<\>\|]|i', '', trim($value));
-
-        //removes excess white spaces
-        $value = preg_replace('/\s\s+/', ' ', $value);
-
-        return $textlib->substr($value,0,MAX_TAG_LENGTH);
+    if ($lowercase){
+        $tag_names_csv = moodle_strtolower($tag_names_csv);
     }
 
+    return $tag_names_csv;
 }
 
 function tag_flag_inappropriate($tag_names_or_ids_csv){