Bug #30931

date() expects parameter 2 to be long, string given in class.tslib_content.php

Added by Simon Schaufelberger about 8 years ago. Updated about 1 year ago.

Status:
Closed
Priority:
Must have
Assignee:
-
Category:
Frontend
Target version:
-
Start date:
2011-10-14
Due date:
% Done:

100%

TYPO3 Version:
4.6
PHP Version:
5.2
Tags:
Complexity:
Is Regression:
Sprint Focus:

Description

I have the following typoscript (part of a sql string)

20 = TEXT
20.date = U
20.noTrimWrap = | AND date > ||

and i get the following warning message:

Warning: date() expects parameter 2 to be long, string given in /typo3/sysext/cms/tslib/class.tslib_content.php on line 2529

content should NEVER be a string by default here, it as written in the php docs:

string gmdate ( string $format [, int $timestamp = time() ] )

it shold be an int.

public function stdWrap_date($content = '', $conf = array()) {
    $content = ($conf['date.']['GMT'] ? gmdate($conf['date'], $content) : date($conf['date'], $content));
    return $content;
}

for me the code should be something like:

public function stdWrap_date($content = '', $conf = array()) {
    if ($content !== '') {
        $content = ($conf['date.']['GMT'] ? gmdate($conf['date'], $content) : date($conf['date'], $content));
    } else {
        $content = ($conf['date.']['GMT'] ? gmdate($conf['date']) : date($conf['date']));
    }
    return $content;
}

Related issues

Related to TYPO3 Core - Bug #38717: strftime() expects parameter 2 to be long, string given Closed 2012-07-06
Duplicated by TYPO3 Core - Bug #24276: PHP Warning may occur in backend UI with recent PHP version Closed 2010-12-02

Associated revisions

Revision 46cb802d (diff)
Added by Simon Schaufelberger about 8 years ago

[BUGFIX] date expects parameter 2 to be long, string given

Any zero length string value is replaced with the current timestamp.
(Just like the default value for the second parameter of date/gmdate.)

Change-Id: I44d095deb7b511b8b35730f2f031186aeaadb945
Fixes: #30931
Releases: 4.6, 4.5, 4.4
Reviewed-on: http://review.typo3.org/5821
Reviewed-by: Jigal van Hemert
Tested-by: Jigal van Hemert
Reviewed-by: Björn Pedersen
Reviewed-by: Simon Schaufelberger
Tested-by: Simon Schaufelberger

Revision 633eca81 (diff)
Added by Simon Schaufelberger about 8 years ago

[BUGFIX] date expects parameter 2 to be long, string given

Any zero length string value is replaced with the current timestamp.
(Just like the default value for the second parameter of date/gmdate.)

Change-Id: I231b704c3df181486c37497e0dc6f10baf1d267e
Fixes: #30931
Releases: 4.6, 4.5, 4.4
Reviewed-on: http://review.typo3.org/5970
Reviewed-by: Simon Schaufelberger
Tested-by: Simon Schaufelberger
Reviewed-by: Georg Ringer
Tested-by: Georg Ringer

Revision fea80ec1 (diff)
Added by Markus Klein about 8 years ago

[BUGFIX] date expects parameter 2 to be long, string given

Any zero length string value is replaced with the current timestamp.
(Just like the default value for the second parameter of date/gmdate.)

Change-Id: I0d4cef574028668b0736c8a13db0687f0be62b0e
Fixes: #30931
Releases: 4.6, 4.5, 4.4
Reviewed-on: http://review.typo3.org/5971
Reviewed-by: Simon Schaufelberger
Tested-by: Simon Schaufelberger
Reviewed-by: Jigal van Hemert
Tested-by: Jigal van Hemert

History

#1 Updated by Simon Schaufelberger about 8 years ago

a workaround would be:

20.value = UNIX_TIMESTAMP()

and remove the date line.

#2 Updated by Markus Klein about 8 years ago

What is your intention about assigning U to 20.date?

The documentation says:
The content should be data-type "UNIX-time".

I do agree to change the behavior to prevent this warning.
Can you push a patch to gerrit?

#3 Updated by Simon Schaufelberger about 8 years ago

My intention is to get the current timestamp which is only possible in typo3 with typoscript like that.

No behaviour needs to be changed, just the error handling... (if no "parameter"/$content is given)

Can you push a patch to gerrit?

yes, sure.

#4 Updated by Mr. Hudson about 8 years ago

  • Status changed from New to Under Review

Patch set 1 of change I44d095deb7b511b8b35730f2f031186aeaadb945 has been pushed to the review server.
It is available at http://review.typo3.org/5821

#5 Updated by Mr. Hudson about 8 years ago

Patch set 2 of change I44d095deb7b511b8b35730f2f031186aeaadb945 has been pushed to the review server.
It is available at http://review.typo3.org/5821

#6 Updated by Mr. Hudson about 8 years ago

Patch set 3 of change I44d095deb7b511b8b35730f2f031186aeaadb945 has been pushed to the review server.
It is available at http://review.typo3.org/5821

#7 Updated by Jigal van Hemert about 8 years ago

date() and gmdate() convert a big range of values to an integer:

integers : OK
floats : OK
NULL : OK ( => zero)
hex / decimal direct values : OK (like in date( 'U', 0xFF); )
strings:
- whitespace at beginning allowed (' 1234')
- trailing non-numeric garbage is ignored ( '1234abc' => 1234, but also '0xFFyyyy' => 255)
- +/- signs at beginning allowed ('+0' => 0

Next version will use a regular expression to check string values for validity.

#8 Updated by Mr. Hudson about 8 years ago

Patch set 4 of change I44d095deb7b511b8b35730f2f031186aeaadb945 has been pushed to the review server.
It is available at http://review.typo3.org/5821

#9 Updated by Simon Schaufelberger about 8 years ago

@Jigal: did/could you write some unit tests for that?

#10 Updated by Markus Klein about 8 years ago

Jigal, I'm sure you're referencing to this:
www.php.net/manual/en/language.types.integer.php

But:
  • I can't find any hint that date() is accepting floats.
  • Where does it say that leading white spaces are trimmed automatically?

I do believe a simple intval() or is_numeric() check would suffice.

#11 Updated by Jigal van Hemert about 8 years ago

I checked the internal source code of PHP where it uses a function to 'interpret' a parameter to a long (integer) value. This function is different than intval() or is_numeric() uses. For example date('U', '0xFFyyy') = 255 while intval('0xFFyyy') = 0 and is_numeric('0xFFyyy') = FALSE.

The problem with this issue is that we have to make sure that things that worked before will still work (especially for 4.5 and 4.4 branches), while getting rid of the ugly warnings.

Below a small test script to see what date() accepts and that the regexp matches this (at least for strings).

Unit tests would be very nice indeed.

<?php
$date = array('-2text1',0,'-0', 12345678, '0020', '0xFFyyy', '-0xEEyyy', '-1.0e4', '   12345.6', '12345,7', 12345.6, NULL, TRUE, ' +    1234567abc', 'plaap', '');
foreach ($date as $tryDate) {
    echo 'tryDate: ';
    var_export($tryDate);
    echo "<br />\nis_numeric: ";
    var_export(is_numeric($tryDate));
    echo "<br />\nintval: ";
    var_export(intval($tryDate));
    echo "<br />\nusing testAgainstRegexp: ";
    var_export(testAgainstRegexp($tryDate));

    echo "<br />\ndate('U' : ";
    $plaap = date('U', $tryDate);
    var_export($plaap);
    echo "<br />\ngmdate('U' : ";
    $plaap = gmdate('U', $tryDate);
    var_export($plaap);
    echo "<br />\n";
    echo "--------------------<br />\n";
}

    function testAgainstRegexp ($value) {
        return preg_match('/^[[:space:]]* #allow whitespace at beginning
                    (
                        [+-]?(\d+)(?![xX]) #(un)signed decimal which is not the start of a hexadecimal number
                        |
                        0[xX][0-9a-fA-F]+ #unsigned hexadecimal number
                        |
                        [+-]?((\d+|((\d*\.\d+)|(\d+\.\d*)))[eE][+-]?\d+) #floating point notation
                    )
                    /x', $value);    
    }
?>

#12 Updated by Dmitry Dulepov about 8 years ago

Do we really need that complexity? Why don't we simply do intval? What is the use case for having 0x23427235 as a string parameter?

I'd say go for a simple integer for now and expand that in future if there is a real use case for this. There is no need for complicating this more than necessary.

#13 Updated by Mr. Hudson about 8 years ago

Patch set 5 of change I44d095deb7b511b8b35730f2f031186aeaadb945 has been pushed to the review server.
It is available at http://review.typo3.org/5821

#14 Updated by Mr. Hudson about 8 years ago

Patch set 6 of change I44d095deb7b511b8b35730f2f031186aeaadb945 has been pushed to the review server.
It is available at http://review.typo3.org/5821

#15 Updated by Mr. Hudson about 8 years ago

Patch set 7 of change I44d095deb7b511b8b35730f2f031186aeaadb945 has been pushed to the review server.
It is available at http://review.typo3.org/5821

#16 Updated by Simon Schaufelberger about 8 years ago

  • Status changed from Under Review to Resolved
  • % Done changed from 0 to 100

#17 Updated by Benni Mack about 1 year ago

  • Status changed from Resolved to Closed

Also available in: Atom PDF