Internal Server Error when Using the Content Password Joomla Plugin

We have discussed the Content Password plugin previously in a post explaining how to password protect a page in Joomla. Many of our readers have used it and have praised it. One of our customers, however, told us that he was seeing an “Internal Server Error” message when he uses this plugin on some of his pages.

We investigated this issue immediately – at first we were not able to understand what the problem was. His usage of the plugin was as instructed: he was correctly encapsulating the password protected part with {password} and {/password}. The weird thing is that the plugin was working on some of the pages and not working on some other pages. It was really odd!

We then started debugging Joomla by adding this code die(test); to the beginning of the contentpassword.php file located under /plugins/content/, and then moving this code to the subsequent line until the die message disappeared and we saw the error again. This method allowed us locate the exact source of this error, it was the function call responsible for replacing the password protected content with the form to enter the password (line 155 of the plugin):

$row->$replace_type = preg_replace(
$this->_passwordregex,
'',
preg_replace('%\{password[^\}]*(.(?!\{password))*?\{/password\}%s', str_replace("\\", "\\\\", $passform), $row->$replace_type)

We did some research and some tests and we discovered that the function preg_replace has some limitations regarding the size of the text and the complexity of the query. We don’t have a scientific formula that allows one to exactly calculate the boundaries of this function, but we’re sure that there are boundaries!

In any case, we decided to solve this problem by replacing the line with our own code. So we wrote the following function and we added it to the contentpassword.php file as a method to the plgContentContentpassword class:

	function _replacePasswordPlaceholder($content, $form){
		$arrContent = explode('password}', $content);
		if (count($arrContent) < 3)
			return $content;
		for ($i = 0; $i < count($arrContent) - 1; $i = $i+2){
			if (($arrContent[$i][strlen($arrContent[$i]) - 1] == '{') && ($arrContent[$i+1][strlen($arrContent[$i+1]) - 1] == '/') && ($arrContent[$i+1][strlen($arrContent[$i+1]) - 2] == '{')){
				$strContent .= substr ($arrContent[$i], 0, strlen($arrContent[$i]) - 1).$form;
			}
			else
				$strContent .= implode('password}', array($arrContent[$i], $arrContent[$i+1]));
		}
		$strContent .= $arrContent[$i];
		return $strContent;
	}

The above function just takes the content and the form as parameters, and replaces all occurrences of text encapsulated between {password} and {/password} with the standard password protection form. It then returns the modified text.

After adding the function, we replaced the code in line 155 with this code:

$row->$replace_type = $this->_replacePasswordPlaceholder($row->$replace_type, $passform);

We then tested the password protected page again and it worked! We were happy and our client was happy!

If you're having a problem with the Content Password plugin, or with any other Joomla plugin, then why don't you contact us? We work with enthusiasm, we're very friendly, we don't charge much, and we love Joomla!

No comments yet.

Leave a comment