Illegal string offset in PHP 5.4 - php

The following code works in a computer with PHP 5.3 and it doesn't in PHP 5.4:
function __clone() {
$this->changed = TRUE;
foreach ($this->conditions as $key => $condition) {
if (
$condition['field']
instanceOf QueryConditionInterface) {
$this->conditions[$key]['field'] = clone($condition['field']);
}
}
}
$condition in both cases doesn't have the 'field' offset, but in PHP 5.3 the library continues working without complain, however in PHP 5.4 gives this warning message:
Warning: Illegal string offset 'field' in DatabaseCondition->__clone()
And short after, the library (from Drupal6), stops working.
Any idea how to fix this?
Should I use an isset($condition['field']) even if it's a core library of the framework?

Related

PhpStorm override function

Return type of explode function has changed in PHP 8.0.
With PhpStorm 2022.1.3 + Php Inspections (EA Extended) 4.0.7.1 inspect code (PHP 7.2 level), I get many:
[EA] '$xyz' may not support offset operations (or its type not annotated properly: [bool, array]).
In PhpStorm configuration, is there a way to override default explode function?
I tried to add .phpstorm.meta.php/explode.php, but this is not working. I think it's wrong.
<?php
namespace PHPSTORM_META {
override( explode,
function explode(string $separator, string $string, int $limit): array {
return [];
}
);
}
I ask that because, I get the previous "error" with the following code with PHP 7.2-7.4, and not with PHP 8.0 (for $gps[0] and $gps[1]):
$gps = explode(',', $result['GPS']);
if (count($gps) == 2) {
$items['lat'] = $gps[0];
$items['lng'] = $gps[1];
}

PHP Deprecated: Automatic conversion of false to array is deprecated adodb-mssqlnative.inc.php on line 154

We are upgrading PHP to version 8.1. Using MS Sql Server DB. It all seems to work correctly but I see repeated messages in the log file:
[03-Feb-2022 11:51:18 America/New_York] PHP Deprecated: Automatic conversion of false to array is deprecated in C:...\includes\adodb\drivers\adodb-mssqlnative.inc.php on line 154
I've updated adodb to version version 5.22 but that did not stop the messges from logging. The ini file has
extension=php_sqlsrv_81_nts_x64.dll
extension=php_pdo_sqlsrv_81_nts_x64.dll
Does anyone know how to fix this problem?
This is a known issue of backwards incompatibility, affecting the ADOdb library version 5.22.1 and older. PHP 8.1 warns you on autovivification of false-y values and some future version of PHP will throw an error when you do it.
PHP natively allows for autovivification (auto-creation of arrays from falsey values). This feature is very useful and used in a lot of PHP projects, especially if the variable is undefined. However, there is a little oddity that allows creating an array from a false and null value.
And they give this example
// From false
$arr = false;
$arr[] = 2;
I went and found the file in question and this is the function it's in
function ServerInfo() {
global $ADODB_FETCH_MODE;
static $arr = false;
if (is_array($arr))
return $arr;
if ($this->fetchMode === false) {
$savem = $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_NUM;
} elseif ($this->fetchMode >=0 && $this->fetchMode <=2) {
$savem = $this->fetchMode;
} else
$savem = $this->SetFetchMode(ADODB_FETCH_NUM);
$arrServerInfo = sqlsrv_server_info($this->_connectionID);
$ADODB_FETCH_MODE = $savem;
$arr['description'] = $arrServerInfo['SQLServerName'].' connected to '.$arrServerInfo['CurrentDatabase'];
$arr['version'] = $arrServerInfo['SQLServerVersion'];//ADOConnection::_findvers($arr['description']);
return $arr;
}
The problem is that it starts with
static $arr = false;
and then tries to autovivificate a non-array (line 154 in your error)
$arr['description'] = $arrServerInfo['SQLServerName'].' connected to '.$arrServerInfo['CurrentDatabase'];
You should be able to fix it (in theory) by making sure it's an array (something they should have done anyways). Add this above that line to make it one before it tries to append
if(!is_array($arr)) $arr = [];

Guide a newbie: PHP 7 warning: Warning: count(): Parameter must be an array or an object that implements Countable

After upgrading to PHP 7 I get the following error message in a really old WP theme. Unfortunately changing the theme is not an option right now, so I have to try to find a fix. I can kind of read PHP, but have a hard time writing it on my own, so I'd love some pointers on how to change this.
The Error message:
Warning: count(): Parameter must be an array or an object that implements Countable
The code that needs to implement Countable
if (defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE) {
$settings = $this['option']->get('warp_theme_options', array());
} else {
$settings = ($file = $this['path']->path('template:config')) ? file_get_contents($file) : array();
}
// set config or load defaults
if (count($settings)) {
$this->config = $this['data']->create($settings);
} else {
$this->config = $this['data']->create(file_get_contents($this['path']->path('template:config.default')));
}
From trying to read up on this I think the error is that count no longer allows Null? But how would I change the code if statement above to work then?

Codeigniter showing error: mysql_real_escape_string() expects parameter 2 to be resource, boolean given

I am working on a project where it has a simple enquiry form. The model code is as follows:
public function insenqm($name, $email, $enqtype, $comments) {
$data = array(
'name' => $name,
'email' => $email,
'enqtype' => $enqtype,
'comments' => $comments
);
$this->db->insert('enquiry', $data);
}
First I thought that the data is not entering the model function insenqm, but it is entering the function and not getting inserted in the database. And instead showing error as follows:
A PHP Error was encountered
Severity: Warning
Message: mysql_real_escape_string() expects parameter 2 to be resource, boolean given
Filename: mysql/mysql_driver.php
Line Number: 314
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /home/webtrees123/public_html/system/core/Exceptions.php:186)
Filename: helpers/url_helper.php
Line Number: 543
When I googled for the error, it showed that the keyword mysql has been depricated and suggested to use mysqli. However, it is working perfectly on my local machine, but showing this error on server. You can see the error by visiting this link and the filling the enquiry form at the end of the web page.
Does anyone have any suggestion on why the error is coming? If yes please reply to this question.
All positive suggestions are welcomed.
I had this error occur in Code Igniter and saw it multiple times in my logs file after I upgrade to PHP 5.4 on my server.
This error is a typical thing that happens when you upgrade to PHP 5.4 to get the new fun stuff.
Hopefully you have your error logging activated so you can see it.
This error is due to the following function in the mysql driver.
Assuming Code Igniter: 2.2.6, the latest version:
function escape_str($str, $like = FALSE)
{
if (is_array($str))
{
foreach ($str as $key => $val)
{
$str[$key] = $this->escape_str($val, $like);
}
return $str;
}
$str = mysql_real_escape_string($str, $this->conn_id);
// escape LIKE condition wildcards
if ($like === TRUE)
{
$str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
}
return $str;
}
It will also occur if you use the mysqli driver (a setup in your database), so using the other driver will not solve it, because there is a similar function.
The error results from the fact that $this->escape_str() is called without a connection to mysql, so the error occurs, because $this->conn_id is not a resource.
There is also a similar error when using an older version of CI that uses mysql_escape_string($string)
Because I'm using PHP 5.4 and that error would not be a warning, it would be 8192 E_DEPRECATED!
That would give you some hint as to what version of CI 2 you are using.
In any event the solution is the same:
In any case, the error can be fixed with this:
Here's what I changed this function to
function escape_str($str, $like = FALSE)
{
if (is_array($str))
{
foreach ($str as $key => $val)
{
$str[$key] = $this->escape_str($val, $like);
}
return $str;
}
if (is_resource($this->conn_id)) {
$str = mysql_real_escape_string($str, $this->conn_id);
//log_message('error', 'is_resource is true');
} else {
//log_message('error', 'string to convert is '.$str);
$str = addslashes($str);
}
// escape LIKE condition wildcards
if ($like === TRUE)
{
$str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
}
return $str;
}
You'll notice I've left some log_message() calls in there when I did my testing to see what this function was escaping, which I've commented out above.
The latest code from Version of CI 2 will still not contain this fix.
CI 2 is not under active development.
Lastly, I'd like to point out this post for reference:
http://forum.codeigniter.com/thread-62431.html
mentions that there is a character set MySQL injection technique which may warrant upgrade to 3.0.
Not sure if that is the case in this fix, though.
Someone else could look into how this function is being called and fix the caller, rather than this function.
Here's the link to the mysql driver code in version 2.2.3 on github.

CakePHP cakeshell errors "A Notice: Uninitialized string offset: 0 in"

I am trying to run my cake shell script but the output looks like the following:
-bash-3.2$ ../cake/console/cake audit
../cake/console/cake: line 30:/root/site/app: is a directory
Array
(
[0] => /root/site/cake/console/cake.php
[1] => -working
[2] =>
[3] => audit
)
Notice: Uninitialized string offset: 0 in /root/site/cake/console/cake.php on line 550
What am I doing wrong? Here are the contents of this file:
cake.php
function __parseParams($params) {
$count = count($params);
for ($i = 0; $i < $count; $i++) {
if (isset($params[$i])) {
if ($params[$i]{0} === '-') {
$key = substr($params[$i], 1);
$this->params[$key] = true;
unset($params[$i]);
if (isset($params[++$i])) {
if ($params[$i]{0} !== '-') {//This is line 550
$this->params[$key] = str_replace('"', '', $params[$i]);
unset($params[$i]);
} else {
$i--;
$this->__parseParams($params);
}
}
} else {
$this->args[] = $params[$i];
unset($params[$i]);
}
}
}
}
Focus on the first error
Whenever debugging something that's broken, it's a good idea to focus on the first error and not the fallout from it. The first error message is this line:
line 30:/root/site/app: is a directory
It comes from the cake bash script, before calling php. That line in the most recent 1.3 version is blank, so it's not obvious what specific version of cake you are using, but it isn't the latest 1.3 release.
The consequence of the above error is that the following is the command called:
exec php -q "/root/site/cake/console/"cake.php -working "" "audit"
^^
The parameters passed to cake.php specify that the working directory is an empty string, something which is abnormal and later causes an undefined index error.
Upgrading cures all ailes
Most likely, this specific error can be solved by copying cake.php from the latest version of the same release cycle you are using.
Also consider simply upgrading CakePHP itself to the latest release (from the same major version in use) which will likely fix this specific problem, and others - especially relevant if there have been security releases, which recently there have been.

Categories