/**
  * Parse raw listing entry.
  *
  * @param   string $raw a single line
  * @param   peer.ftp.FtpConnection $connection
  * @param   string $base default "/"
  * @param   util.Date $ref default NULL
  * @return  peer.ftp.FtpEntry
  */
 public function entryFrom($raw, FtpConnection $conn = null, $base = '/', Date $ref = null)
 {
     sscanf($raw, '%s %d %s %s %d %s %d %[^ ] %[^$]', $permissions, $numlinks, $user, $group, $size, $month, $day, $date, $filename);
     // Only qualify filenames if they appear unqualified in the listing
     if ('/' !== $filename[0]) {
         $filename = $base . $filename;
     }
     // Create a directory or an entry
     if ('d' === $permissions[0]) {
         $e = new FtpDir($filename, $conn);
     } else {
         $e = new FtpFile($filename, $conn);
     }
     // If the entry contains a timestamp, the year is omitted, "Apr 4 20:16"
     // instead of "Apr 4 2009". This compact format is used when the file
     // time is within six months* from the current date, in either direction!
     //
     // *] #define SIXMONTHS       ((365 / 2) * 86400) := 15724800
     //    See http://svn.freebsd.org/base/projects/releng_7_xen/bin/ls/print.c
     if (strstr($date, ':')) {
         $ref || ($ref = Date::now());
         $d = new Date($month . ' ' . $day . ' ' . $ref->getYear() . ' ' . $date);
         if ($d->getTime() - $ref->getTime() > 15724800) {
             $d = DateUtil::addMonths($d, -12);
         }
     } else {
         $d = new Date($month . ' ' . $day . ' ' . $date);
     }
     try {
         $e->setPermissions(substr($permissions, 1));
         $e->setNumlinks($numlinks);
         $e->setUser($user);
         $e->setGroup($group);
         $e->setSize($size);
         $e->setDate($d);
     } catch (\lang\IllegalArgumentException $e) {
         throw new \lang\FormatException('Cannot parse "' . $raw . '": ' . $e->getMessage());
     }
     return $e;
 }
Ejemplo n.º 2
0
 /**
  * LIST: This command causes a list of file names and file details 
  * to be sent from the FTP site to the client.
  *
  * @param   peer.Socket socket
  * @param   string params
  */
 public function onList($socket, $params)
 {
     if (!($dataSocket = $this->openDatasock($socket))) {
         return;
     }
     // Split options from arguments
     if (substr($params, 0, 1) === '-') {
         list($options, $params) = explode(' ', substr($params, 1), 2);
         $this->cat && $this->cat->debug('+++ Options:', $options);
     } else {
         $options = '';
     }
     if (!($entry = $this->storage->lookup($socket->hashCode(), $params))) {
         $this->answer($socket, 550, $params . ': No such file or directory');
         $dataSocket->close();
         unset($dataSocket);
         $this->cat && $this->cat->debug($socket, $this->datasock[$socket->hashCode()]);
         return;
     }
     // Invoke interceptor
     if (!$this->checkInterceptors($socket, $entry, 'onRead')) {
         $dataSocket->close();
         return;
     }
     $this->answer($socket, 150, sprintf('Opening %s mode data connection for filelist', $this->sessions[$socket->hashCode()]->typeName()));
     // If a collection was specified, list its elements, otherwise,
     // list the single element
     if ($entry instanceof StorageCollection && !strstr($options, 'd')) {
         $elements = $entry->elements();
     } else {
         $elements = [$entry];
     }
     $before6Months = DateUtil::addMonths(Date::now(), -6)->getTime();
     for ($i = 0, $s = sizeof($elements); $i < $s; $i++) {
         $buf = sprintf('%s  %2d %s  %s  %8d %s %s', $this->permissionString($elements[$i]->getPermissions()), $elements[$i]->numLinks(), $elements[$i]->getOwner(), $elements[$i]->getGroup(), $elements[$i]->getSize(), date($elements[$i]->getModifiedStamp() < $before6Months ? 'M d  Y' : 'M d H:i', $elements[$i]->getModifiedStamp()), $elements[$i]->getName());
         $this->cat && $this->cat->debug('    ', $buf);
         $dataSocket->write($buf . $this->eol($this->sessions[$socket->hashCode()]->getType()));
     }
     $dataSocket->close();
     $this->answer($socket, 226, 'Transfer complete');
 }
 public function testNonLeapYear()
 {
     $date = \util\Date::create(1999, 2, 1, 0, 0, 0, new TimeZone('Europe/Berlin'));
     $this->assertEquals(\util\Date::create(1999, 3, 1, 0, 0, 0, new TimeZone('Europe/Berlin')), DateUtil::addMonths($date, 1));
     $this->assertEquals(\util\Date::create(1999, 3, 3, 0, 0, 0, new TimeZone('Europe/Berlin')), DateUtil::addDays($date, 30));
 }