Custom Code self reference check is off #365

Closed
opened 2019-01-13 17:53:24 +00:00 by AlexDings · 2 comments
AlexDings commented 2019-01-13 17:53:24 +00:00 (Migrated from github.com)

Steps to reproduce the issue

Create a custom code with the name "printSomething"
Type [CUSTOMCODE=printSomethingHelper] inside as if you wanted to include that custom code inside "printSomething".

Expected result

It saves, as there is no loop or self-reference.

Actual result

It removes [CUSTOMCODE=printSomethingHelper] and gives the following warning:

Warning
Placeholder Removed!
This custom code can only be used in other custom code, not in it self! Since that will cause a infinite loop in the compiler.

Additional comments

In my testing, this happens when the first part of the custom code name matches. For example with printSomethingHelper being included in printSomething.

### Steps to reproduce the issue Create a custom code with the name "printSomething" Type [CUSTOMCODE=printSomethingHelper] inside as if you wanted to include that custom code inside "printSomething". ### Expected result It saves, as there is no loop or self-reference. ### Actual result It removes [CUSTOMCODE=printSomethingHelper] and gives the following warning: ~~~~ Warning Placeholder Removed! This custom code can only be used in other custom code, not in it self! Since that will cause a infinite loop in the compiler. ~~~~ ### Additional comments In my testing, this happens when the first part of the custom code name matches. For example with printSomethingHelper being included in printSomething.

Okay that warning is triggered by this little script on line 829 of the model of the custom_code view:

if (isset($data['code']) && ($placholders = ComponentbuilderHelper::getAllBetween($data['code'], '[CUSTOM' . 'CODE=', ']'))
	&& ComponentbuilderHelper::checkArray($placholders))
{
	// make sure custom code as Hash (automation)  target does not have other custom code placeholders
	if (isset($data['target']) && 1 == $data['target'])
	{
		foreach ($placholders as $placholder)
		{
			$data['code'] = str_replace('[CUSTOM' . 'CODE=' . $placholder . ']', '', $data['code']);
		}
		// set title
		$title = (count($placholders) == 1) ? JText::_('COM_COMPONENTBUILDER_PLACEHOLDER_REMOVED') : JText::_('COM_COMPONENTBUILDER_PLACEHOLDERS_REMOVED');
		// show message that we have had to remove the custom placeholders
		JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_HTHREESHTHREEPCUSTOM_CODE_CAN_ONLY_BE_USED_IN_OTHER_CUSTOM_CODE_IF_SET_AS_BJCB_MANUALB_YOU_CAN_NOT_ADD_THEM_TO_EMHASH_AUTOMATIONEM_CODE_AT_THIS_POINTP', $title), 'Warning');
	}
	// make sure that the same custom code is not added to itself
	else
	{
		foreach ($placholders as $placholder)
		{
			if (strpos($placholder, $data['function_name']) !== false)
			{
				$data['code'] = str_replace('[CUSTOM' . 'CODE=' . $placholder . ']', '', $data['code']);
				// show message that we have had to remove the custom placeholders
				JFactory::getApplication()->enqueueMessage(JText::_('COM_COMPONENTBUILDER_HTHREEPLACEHOLDER_REMOVEDHTHREEPBTHISB_CUSTOM_CODE_CAN_ONLY_BE_USED_IN_BOTHERB_CUSTOM_CODE_NOT_IN_IT_SELF_SINCE_THAT_WILL_CAUSE_A_INFINITE_LOOP_IN_THE_COMPILERP'), 'Warning');
				// stop the loop :)
				break;
			}
		}
	}
}

So first we check is there placeholders and then we on line 833 check that the method is not a Hash (automation) since that implementation, still does not allow custom code to be used in it, but the error should read Custom code can only be used in other custom code if set as <b>JCB (manual)</b>, you can not add them to <em>Hash (Automation)</em> code at this point. which is not the error you gave.

So that means we are on line 847 which loops the placeholders to see if it is the same as the current function name.... hmmm and here I see we can get negative/true results due to the strpos method. Okay got it... this is a bug, I will push out a fix, thanks!

Okay that warning is triggered by this little script on [line 829](https://github.com/vdm-io/Joomla-Component-Builder/blob/staging/admin/models/custom_code.php#L829) of the model of the custom_code view: ``` if (isset($data['code']) && ($placholders = ComponentbuilderHelper::getAllBetween($data['code'], '[CUSTOM' . 'CODE=', ']')) && ComponentbuilderHelper::checkArray($placholders)) { // make sure custom code as Hash (automation) target does not have other custom code placeholders if (isset($data['target']) && 1 == $data['target']) { foreach ($placholders as $placholder) { $data['code'] = str_replace('[CUSTOM' . 'CODE=' . $placholder . ']', '', $data['code']); } // set title $title = (count($placholders) == 1) ? JText::_('COM_COMPONENTBUILDER_PLACEHOLDER_REMOVED') : JText::_('COM_COMPONENTBUILDER_PLACEHOLDERS_REMOVED'); // show message that we have had to remove the custom placeholders JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_HTHREESHTHREEPCUSTOM_CODE_CAN_ONLY_BE_USED_IN_OTHER_CUSTOM_CODE_IF_SET_AS_BJCB_MANUALB_YOU_CAN_NOT_ADD_THEM_TO_EMHASH_AUTOMATIONEM_CODE_AT_THIS_POINTP', $title), 'Warning'); } // make sure that the same custom code is not added to itself else { foreach ($placholders as $placholder) { if (strpos($placholder, $data['function_name']) !== false) { $data['code'] = str_replace('[CUSTOM' . 'CODE=' . $placholder . ']', '', $data['code']); // show message that we have had to remove the custom placeholders JFactory::getApplication()->enqueueMessage(JText::_('COM_COMPONENTBUILDER_HTHREEPLACEHOLDER_REMOVEDHTHREEPBTHISB_CUSTOM_CODE_CAN_ONLY_BE_USED_IN_BOTHERB_CUSTOM_CODE_NOT_IN_IT_SELF_SINCE_THAT_WILL_CAUSE_A_INFINITE_LOOP_IN_THE_COMPILERP'), 'Warning'); // stop the loop :) break; } } } } ``` So first we check is there placeholders and then we on [line 833](https://github.com/vdm-io/Joomla-Component-Builder/blob/staging/admin/models/custom_code.php#L832) check that the method is not a `Hash (automation)` since that implementation, still does not allow custom code to be used in it, but the error should read `Custom code can only be used in other custom code if set as <b>JCB (manual)</b>, you can not add them to <em>Hash (Automation)</em> code at this point.` which is not the error you gave. So that means we are on [line 847](https://github.com/vdm-io/Joomla-Component-Builder/blob/staging/admin/models/custom_code.php#L847) which loops the placeholders to see if it is the same as the current function name.... hmmm and here I see we can get negative/true results due to the `strpos` method. Okay got it... this is a bug, I will push out a fix, thanks!

Please test the staging branch to see if the fix really resolved the issue for you.

Please test the staging branch to see if the fix really resolved the issue for you.
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: joomla/Component-Builder#365
No description provided.