yiisoft/yii

PHP 7.4 CClientScript.php fail when is not an array

JeffPrixla1986 opened this issue · 6 comments

What steps will reproduce the problem?

Php 7.4, $package['baseUrl'] as bool(false)

What is the expected result?

View generate coorectly, that's the way it worked on PHP5.6 before I updated to PHP7.4.

What do you get instead?

Error: Trying to access array offset on value of type bool

Additional info

Q A
Yii version 1.1.22
PHP version 7.4
Operating system Linux (Debian Buster)

Issue is linked to file: ./framework/web/CClientScript.php : starting at line 540

Current code in release:

			if($baseUrl==='' || $baseUrl[0]!=='/' && strpos($baseUrl,'://')===false)
				$baseUrl=Yii::app()->getRequest()->getBaseUrl().'/'.$baseUrl;
			$baseUrl=rtrim($baseUrl,'/');

My update proposal fixing the issue:

			if(is_array($baseUrl))
			{
				if($baseUrl==='' || $baseUrl[0]!=='/' && strpos($baseUrl,'://')===false)
					$baseUrl=Yii::app()->getRequest()->getBaseUrl().'/'.$baseUrl;
				$baseUrl=rtrim($baseUrl,'/');
			}
			else
			{
				if($baseUrl==='' && strpos($baseUrl,'://')===false)
					$baseUrl=Yii::app()->getRequest()->getBaseUrl().'/'.$baseUrl;
				$baseUrl=rtrim($baseUrl,'/');
			}

I use my fix in production and it works correctly, maybe there is a better way to solve it.

That's definitely a bug. Would you like to submit a pull request with a unit test and the fix?

Thanks for your quick answer, I made the pull request #4330

That's incorrect fix.
Code $baseUrl[0] !== '/' is for string value, not array.

Fix should check if $baseUrl is string type or convert it to string before if block.

Why do you have false as baseUrl? I don't see that using false was documented anywhere.

@rob006 Good question.

@JeffPrixla1986 Can you give as some code example which causes error?

This does not look like a bug, but incorrect usage.

If you want to declare a package without a baseUrl, use null instead of false. For details see: https://www.yiiframework.com/doc/api/1.1/CClientScript#packages-detail