PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Networking » Net_FTP » Bug #1176

Net_FTP::ls can return dates in the future

Details

Submitted2004-04-10 16:25 UTC
Fromphilip at philipnet dot com
Assignedtoby
StatusClosed
PackageNet_FTP
PHP Version4.3.4
OSLinux 2.4.25
Roadmaps(Not assigned)

Comments

[2004-04-10 16:25 UTC] philip at philipnet dot com

Description:
------------
Net_FTP::ls can return a timestamp value for a file which
is in the future. This only occurs if the FTP site does
not
send Year information for its files and the files have a
later date (wtr. month, day etc.) than the current date
and
time.

Reproduce code:
---------------
Apologies for the length

<?php
require_once 'Net/FTP.php';
$ftp = new Net_FTP( );
$ftp -> connect( 'www.xmlsoft.org');
$ftp -> login ( 'anonymous');
$dir = $ftp->ls( '/');
$theone = array( );
foreach ($dir as $entry) {
echo $entry["name"];
if (ereg("libxml2-[0-9].[0-9].[0-9].tar.gz", $entry["name"])) {
echo " ".$theone["date"] ." ".$entry["date"];
if ($theone["date"] < $entry["date"]) {
$theone = $entry;
echo " match";
}
}
echo "\n";
}
if ($theone["name"]=="") {
echo "Unable to find file which maches ".$file."\n";
exit (1);
}
echo "The latest release is ".$theone["name"]."\n";
?>

Expected result:
----------------
Doing a Net_FTP::ls to ftp://www.xmlsort.org/ should return
a timestamp indicating that libxml2-2.68.tar.gz is later
than libxml2-2.6.4.tar.gz, but it doesn't.

Below is my proposed solution.

It checks for timestamps from the future and recalculates
the value if so.
There might be a quicker way to do it - by subtracting the
equivalent of a year from $out, but you would have to take
leap years into account.

Actual result:
--------------
Proposed solution:

function _parse_Date ( $date ) {
// Sep 10 22:06 => Sep 10, <year> 22:06
if (preg_match("/([A-Za-z]+)[ ]+([0-9]+)[
]+([0-9]+):([0-9]+)/", $date, $res)) {
$year = date("Y");
$month = $res[1];
$day = $res[2];
$hour = $res[3];
$minute = $res[4];
$date = "$month $day, $year $hour:$minute";
$out = strtotime($date);
if ($out > time())
{
// We can't have a file with a
timestamp in the future
$year = date("Y")-;
$month = $res[1];
$day = $res[2];
$hour = $res[3];
$minute = $res[4];
$date = "$month $day, $year
$hour:$minute";
$out = strtotime($date);
}
}
else
{
$out = strtotime($date);
}
if (!$out) {
return $this->raiseError("Dateconversion
faild.", 0);
}
return $out;
}

[2004-04-10 18:36 UTC] philip at philipnet dot com

Opps!

$year = date("Y")-;

should be:
$year = date("Y")-1;

[2004-04-12 09:50 UTC] philip at philipnet dot com

And the duplicate assignments don't need to be there as
well!