From e84105c3bda99ab9be1e613377153d2be2b144c5 Mon Sep 17 00:00:00 2001 From: Llewellyn van der Merwe Date: Tue, 18 Jun 2019 16:12:49 +0200 Subject: [PATCH] Adapted the safeName to a global safeFieldName method so to use it all over JCB to safely build field names. gh-427 --- README.md | 2 +- admin/README.txt | 2 +- admin/helpers/compiler/a_Get.php | 61 ++++-------------------------- admin/helpers/componentbuilder.php | 52 ++++++++++++++++++++++++- admin/models/field.php | 4 +- site/helpers/componentbuilder.php | 52 ++++++++++++++++++++++++- 6 files changed, 110 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 73b0a6185..ad18f9cb6 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ TODO + *Version*: 2.9.20 + *Copyright*: Copyright (C) 2015 - 2019 Vast Development Method. All rights reserved. + *License*: GNU General Public License version 2 or later; see LICENSE.txt -+ *Line count*: **206865** ++ *Line count*: **206959** + *Field count*: **1141** + *File count*: **1346** + *Folder count*: **209** diff --git a/admin/README.txt b/admin/README.txt index 73b0a6185..ad18f9cb6 100644 --- a/admin/README.txt +++ b/admin/README.txt @@ -150,7 +150,7 @@ TODO + *Version*: 2.9.20 + *Copyright*: Copyright (C) 2015 - 2019 Vast Development Method. All rights reserved. + *License*: GNU General Public License version 2 or later; see LICENSE.txt -+ *Line count*: **206865** ++ *Line count*: **206959** + *Field count*: **1141** + *File count*: **1346** + *Folder count*: **209** diff --git a/admin/helpers/compiler/a_Get.php b/admin/helpers/compiler/a_Get.php index 7f323bfc1..4283c3073 100644 --- a/admin/helpers/compiler/a_Get.php +++ b/admin/helpers/compiler/a_Get.php @@ -689,13 +689,6 @@ class Get */ public $minify = 0; - /** - * field name builder switch - * - * @var int - */ - public $fieldNameBuilder = 0; - /** * Is Tidy Enabled * @@ -757,8 +750,6 @@ class Get $this->params = JComponentHelper::getParams('com_componentbuilder'); // set the minfy switch of the JavaScript $this->minify = (isset($config['minify']) && $config['minify'] != 2) ? $config['minify'] : $this->params->get('minify', 0); - // field name builder switch between conventions - $this->fieldNameBuilder = $this->params->get('field_name_builder', 0); // change this to 1 for testing the new convention // set the global language $this->langTag = $this->params->get('language', $this->langTag); // setup the main language array @@ -2863,7 +2854,7 @@ class Get * * @param object $field The field object * @param string $listViewName The list view name - * @param string $amicably The peaceful resolve + * @param string $amicably The peaceful resolve (for fields in subforms in same view :) * * @return string Success returns field name * @@ -2881,9 +2872,9 @@ class Get return 'error'; } // set the type name - $type_name = $this->safeName($field['settings']->type_name); + $type_name = ComponentbuilderHelper::safeFieldName($field['settings']->type_name); // set the name of the field - $name = $this->safeName($field['settings']->name); + $name = ComponentbuilderHelper::safeFieldName($field['settings']->name); // check that we have the poperties if (ComponentbuilderHelper::checkArray($field['settings']->properties)) { @@ -2917,7 +2908,7 @@ class Get { // set other category details $this->catOtherName[$listViewName] = array( - 'name' => $this->safeName($otherName), + 'name' => ComponentbuilderHelper::safeFieldName($otherName), 'views' => ComponentbuilderHelper::safeString($otherViews), 'view' => ComponentbuilderHelper::safeString($otherView) ); @@ -2937,7 +2928,7 @@ class Get else { // get value from xml - $xml = $this->safeName($this->setPlaceholders(ComponentbuilderHelper::getBetween($field['settings']->xml, 'name="', '"'), $this->placeholders)); + $xml = ComponentbuilderHelper::safeFieldName($this->setPlaceholders(ComponentbuilderHelper::getBetween($field['settings']->xml, 'name="', '"'), $this->placeholders)); // check if a value was found if (ComponentbuilderHelper::checkString($xml)) { @@ -2960,44 +2951,6 @@ class Get return $name; } - /** - * Making field names safe - * - * @input string The you would like to make safe - * - * @returns string on success - **/ - protected function safeName($string, $spacer = '_') - { - // use the new convention - if (1 == $this->fieldNameBuilder) - { - // 0nly continue if we have a string - if (ComponentbuilderHelper::checkString($string)) - { - // check that the first character is not a number - if (is_numeric(substr($string, 0, 1))) - { - $string = ComponentbuilderHelper::replaceNumbers($string); - } - // remove all other strange characters - $string = trim($string); - $string = preg_replace('/'.$spacer.'+/', ' ', $string); - $string = preg_replace('/\s+/', ' ', $string); - // remove all and keep only characters and numbers - $string = preg_replace("/[^A-Za-z0-9 ]/", '', $string); - // replace white space with underscore (SAFEST OPTION) - $string = preg_replace('/\s+/', $spacer, $string); - // default is to return lower - return strtolower($string); - } - // not a string - return ''; - } - // use the default (original behaviour/convention) - return ComponentbuilderHelper::safeString($string); - } - /** * Count how many times the same field is used per view * @@ -3041,13 +2994,13 @@ class Get { $counter = 1; // set the unique name - $uniqueName = $this->safeName($name . '_' . $counter); + $uniqueName = ComponentbuilderHelper::safeFieldName($name . '_' . $counter); while (isset($this->uniqueNames[$view]['names'][$uniqueName])) { // increment the number $counter++; // try again - $uniqueName = $this->safeName($name . '_' . $counter); + $uniqueName = ComponentbuilderHelper::safeFieldName($name . '_' . $counter); } // set the new name number $this->uniqueNames[$view]['names'][$uniqueName] = $counter; diff --git a/admin/helpers/componentbuilder.php b/admin/helpers/componentbuilder.php index 5db1f8202..2fb2979ad 100644 --- a/admin/helpers/componentbuilder.php +++ b/admin/helpers/componentbuilder.php @@ -124,6 +124,54 @@ abstract class ComponentbuilderHelper 'JPATH_THEMES' => JPATH_THEMES ); + /** + * The field builder switch + **/ + protected static $fieldNameBuilder = false; + + /** + * Making field names safe + * + * @input string The you would like to make safe + * + * @returns string on success + **/ + public static function safeFieldName($string, $spacer = '_') + { + // get global value + if (self::$fieldNameBuilder === false) + { + self::$fieldNameBuilder = JComponentHelper::getParams('com_componentbuilder')->get('field_name_builder', 0); // change this to 1 for testing the new convention + } + // use the new convention + if (1 == self::$fieldNameBuilder) + { + // 0nly continue if we have a string + if (self::checkString($string)) + { + // check that the first character is not a number + if (is_numeric(substr($string, 0, 1))) + { + $string = self::replaceNumbers($string); + } + // remove all other strange characters + $string = trim($string); + $string = preg_replace('/'.$spacer.'+/', ' ', $string); + $string = preg_replace('/\s+/', ' ', $string); + // remove all and keep only characters and numbers + $string = preg_replace("/[^A-Za-z0-9 ]/", '', $string); + // replace white space with underscore (SAFEST OPTION) + $string = preg_replace('/\s+/', $spacer, $string); + // default is to return lower + return strtolower($string); + } + // not a string + return ''; + } + // use the default (original behaviour/convention) + return self::safeString($string); + } + /* * Get the Array of Existing Validation Rule Names * @@ -1200,13 +1248,13 @@ abstract class ComponentbuilderHelper } else { - $name = self::safeString(self::getBetween($field->xml,'name="','"')); + $name = self::safeFieldName(self::getBetween($field->xml,'name="','"')); } // use field core name only if not found in xml if (!self::checkString($name)) { - $name = self::safeString($field->name);; + $name = self::safeFieldName($field->name); } return array('name' => $name, 'type' => $field->type_name); } diff --git a/admin/models/field.php b/admin/models/field.php index a0809d2b6..b467b9b67 100644 --- a/admin/models/field.php +++ b/admin/models/field.php @@ -923,8 +923,6 @@ class ComponentbuilderModelField extends JModelAdmin // make sure we have the correct values if (ComponentbuilderHelper::checkArray($property) && isset($property['name']) && ComponentbuilderHelper::checkString($property['name']) && (isset($property['value']) || 'default' === $property['name'])) { - // fix the name (TODO) - // $property['name'] = ComponentbuilderHelper::safeString($property['name']); // some fixes, just in case (more can be added) switch ($property['name']) { @@ -937,7 +935,7 @@ class ComponentbuilderModelField extends JModelAdmin } else { - $property['value'] = ComponentbuilderHelper::safeString($property['value']); + $property['value'] = ComponentbuilderHelper::safeFieldName($property['value']); } break; case 'type': diff --git a/site/helpers/componentbuilder.php b/site/helpers/componentbuilder.php index 11092047b..ec7f161ff 100644 --- a/site/helpers/componentbuilder.php +++ b/site/helpers/componentbuilder.php @@ -124,6 +124,54 @@ abstract class ComponentbuilderHelper 'JPATH_THEMES' => JPATH_THEMES ); + /** + * The field builder switch + **/ + protected static $fieldNameBuilder = false; + + /** + * Making field names safe + * + * @input string The you would like to make safe + * + * @returns string on success + **/ + public static function safeFieldName($string, $spacer = '_') + { + // get global value + if (self::$fieldNameBuilder === false) + { + self::$fieldNameBuilder = JComponentHelper::getParams('com_componentbuilder')->get('field_name_builder', 0); // change this to 1 for testing the new convention + } + // use the new convention + if (1 == self::$fieldNameBuilder) + { + // 0nly continue if we have a string + if (self::checkString($string)) + { + // check that the first character is not a number + if (is_numeric(substr($string, 0, 1))) + { + $string = self::replaceNumbers($string); + } + // remove all other strange characters + $string = trim($string); + $string = preg_replace('/'.$spacer.'+/', ' ', $string); + $string = preg_replace('/\s+/', ' ', $string); + // remove all and keep only characters and numbers + $string = preg_replace("/[^A-Za-z0-9 ]/", '', $string); + // replace white space with underscore (SAFEST OPTION) + $string = preg_replace('/\s+/', $spacer, $string); + // default is to return lower + return strtolower($string); + } + // not a string + return ''; + } + // use the default (original behaviour/convention) + return self::safeString($string); + } + /* * Get the Array of Existing Validation Rule Names * @@ -1200,13 +1248,13 @@ abstract class ComponentbuilderHelper } else { - $name = self::safeString(self::getBetween($field->xml,'name="','"')); + $name = self::safeFieldName(self::getBetween($field->xml,'name="','"')); } // use field core name only if not found in xml if (!self::checkString($name)) { - $name = self::safeString($field->name);; + $name = self::safeFieldName($field->name); } return array('name' => $name, 'type' => $field->type_name); }