Subversion Repositories Applications.papyrus

Compare Revisions

Ignore whitespace Rev 319 → Rev 320

/trunk/api/pear/Net/Socket.php
New file
0,0 → 1,528
<?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Stig Bakken <ssb@php.net> |
// | Chuck Hagenbuch <chuck@horde.org> |
// +----------------------------------------------------------------------+
//
// $Id: Socket.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
 
require_once 'PEAR.php';
 
define('NET_SOCKET_READ', 1);
define('NET_SOCKET_WRITE', 2);
define('NET_SOCKET_ERROR', 3);
 
/**
* Generalized Socket class.
*
* @version 1.1
* @author Stig Bakken <ssb@php.net>
* @author Chuck Hagenbuch <chuck@horde.org>
*/
class Net_Socket extends PEAR {
 
/**
* Socket file pointer.
* @var resource $fp
*/
var $fp = null;
 
/**
* Whether the socket is blocking. Defaults to true.
* @var boolean $blocking
*/
var $blocking = true;
 
/**
* Whether the socket is persistent. Defaults to false.
* @var boolean $persistent
*/
var $persistent = false;
 
/**
* The IP address to connect to.
* @var string $addr
*/
var $addr = '';
 
/**
* The port number to connect to.
* @var integer $port
*/
var $port = 0;
 
/**
* Number of seconds to wait on socket connections before assuming
* there's no more data. Defaults to no timeout.
* @var integer $timeout
*/
var $timeout = false;
 
/**
* Number of bytes to read at a time in readLine() and
* readAll(). Defaults to 2048.
* @var integer $lineLength
*/
var $lineLength = 2048;
 
/**
* Connect to the specified port. If called when the socket is
* already connected, it disconnects and connects again.
*
* @param string $addr IP address or host name.
* @param integer $port TCP port number.
* @param boolean $persistent (optional) Whether the connection is
* persistent (kept open between requests
* by the web server).
* @param integer $timeout (optional) How long to wait for data.
* @param array $options See options for stream_context_create.
*
* @access public
*
* @return boolean | PEAR_Error True on success or a PEAR_Error on failure.
*/
function connect($addr, $port = 0, $persistent = null, $timeout = null, $options = null)
{
if (is_resource($this->fp)) {
@fclose($this->fp);
$this->fp = null;
}
 
if (!$addr) {
return $this->raiseError('$addr cannot be empty');
} elseif (strspn($addr, '.0123456789') == strlen($addr) ||
strstr($addr, '/') !== false) {
$this->addr = $addr;
} else {
$this->addr = @gethostbyname($addr);
}
 
$this->port = $port % 65536;
 
if ($persistent !== null) {
$this->persistent = $persistent;
}
 
if ($timeout !== null) {
$this->timeout = $timeout;
}
 
$openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen';
$errno = 0;
$errstr = '';
if ($options && function_exists('stream_context_create')) {
if ($this->timeout) {
$timeout = $this->timeout;
} else {
$timeout = 0;
}
$context = stream_context_create($options);
$fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context);
} else {
if ($this->timeout) {
$fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout);
} else {
$fp = @$openfunc($this->addr, $this->port, $errno, $errstr);
}
}
 
if (!$fp) {
return $this->raiseError($errstr, $errno);
}
 
$this->fp = $fp;
 
return $this->setBlocking($this->blocking);
}
 
/**
* Disconnects from the peer, closes the socket.
*
* @access public
* @return mixed true on success or an error object otherwise
*/
function disconnect()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
 
@fclose($this->fp);
$this->fp = null;
return true;
}
 
/**
* Find out if the socket is in blocking mode.
*
* @access public
* @return boolean The current blocking mode.
*/
function isBlocking()
{
return $this->blocking;
}
 
/**
* Sets whether the socket connection should be blocking or
* not. A read call to a non-blocking socket will return immediately
* if there is no data available, whereas it will block until there
* is data for blocking sockets.
*
* @param boolean $mode True for blocking sockets, false for nonblocking.
* @access public
* @return mixed true on success or an error object otherwise
*/
function setBlocking($mode)
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
 
$this->blocking = $mode;
socket_set_blocking($this->fp, $this->blocking);
return true;
}
 
/**
* Sets the timeout value on socket descriptor,
* expressed in the sum of seconds and microseconds
*
* @param integer $seconds Seconds.
* @param integer $microseconds Microseconds.
* @access public
* @return mixed true on success or an error object otherwise
*/
function setTimeout($seconds, $microseconds)
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
 
return socket_set_timeout($this->fp, $seconds, $microseconds);
}
 
/**
* Returns information about an existing socket resource.
* Currently returns four entries in the result array:
*
* <p>
* timed_out (bool) - The socket timed out waiting for data<br>
* blocked (bool) - The socket was blocked<br>
* eof (bool) - Indicates EOF event<br>
* unread_bytes (int) - Number of bytes left in the socket buffer<br>
* </p>
*
* @access public
* @return mixed Array containing information about existing socket resource or an error object otherwise
*/
function getStatus()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
 
return socket_get_status($this->fp);
}
 
/**
* Get a specified line of data
*
* @access public
* @return $size bytes of data from the socket, or a PEAR_Error if
* not connected.
*/
function gets($size)
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
 
return @fgets($this->fp, $size);
}
 
/**
* Read a specified amount of data. This is guaranteed to return,
* and has the added benefit of getting everything in one fread()
* chunk; if you know the size of the data you're getting
* beforehand, this is definitely the way to go.
*
* @param integer $size The number of bytes to read from the socket.
* @access public
* @return $size bytes of data from the socket, or a PEAR_Error if
* not connected.
*/
function read($size)
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
 
return @fread($this->fp, $size);
}
 
/**
* Write a specified amount of data.
*
* @param string $data Data to write.
* @param integer $blocksize Amount of data to write at once.
* NULL means all at once.
*
* @access public
* @return mixed true on success or an error object otherwise
*/
function write($data, $blocksize = null)
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
 
if (is_null($blocksize) && !OS_WINDOWS) {
return fwrite($this->fp, $data);
} else {
if (is_null($blocksize)) {
$blocksize = 1024;
}
 
$pos = 0;
$size = strlen($data);
while ($pos < $size) {
$written = @fwrite($this->fp, substr($data, $pos, $blocksize));
if ($written === false) {
return false;
}
$pos += $written;
}
 
return $pos;
}
}
 
/**
* Write a line of data to the socket, followed by a trailing "\r\n".
*
* @access public
* @return mixed fputs result, or an error
*/
function writeLine($data)
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
 
return fwrite($this->fp, $data . "\r\n");
}
 
/**
* Tests for end-of-file on a socket descriptor.
*
* @access public
* @return bool
*/
function eof()
{
return (is_resource($this->fp) && feof($this->fp));
}
 
/**
* Reads a byte of data
*
* @access public
* @return 1 byte of data from the socket, or a PEAR_Error if
* not connected.
*/
function readByte()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
 
return ord(@fread($this->fp, 1));
}
 
/**
* Reads a word of data
*
* @access public
* @return 1 word of data from the socket, or a PEAR_Error if
* not connected.
*/
function readWord()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
 
$buf = @fread($this->fp, 2);
return (ord($buf[0]) + (ord($buf[1]) << 8));
}
 
/**
* Reads an int of data
*
* @access public
* @return integer 1 int of data from the socket, or a PEAR_Error if
* not connected.
*/
function readInt()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
 
$buf = @fread($this->fp, 4);
return (ord($buf[0]) + (ord($buf[1]) << 8) +
(ord($buf[2]) << 16) + (ord($buf[3]) << 24));
}
 
/**
* Reads a zero-terminated string of data
*
* @access public
* @return string, or a PEAR_Error if
* not connected.
*/
function readString()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
 
$string = '';
while (($char = @fread($this->fp, 1)) != "\x00") {
$string .= $char;
}
return $string;
}
 
/**
* Reads an IP Address and returns it in a dot formated string
*
* @access public
* @return Dot formated string, or a PEAR_Error if
* not connected.
*/
function readIPAddress()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
 
$buf = @fread($this->fp, 4);
return sprintf("%s.%s.%s.%s", ord($buf[0]), ord($buf[1]),
ord($buf[2]), ord($buf[3]));
}
 
/**
* Read until either the end of the socket or a newline, whichever
* comes first. Strips the trailing newline from the returned data.
*
* @access public
* @return All available data up to a newline, without that
* newline, or until the end of the socket, or a PEAR_Error if
* not connected.
*/
function readLine()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
 
$line = '';
$timeout = time() + $this->timeout;
while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) {
$line .= @fgets($this->fp, $this->lineLength);
if (substr($line, -1) == "\n") {
return rtrim($line, "\r\n");
}
}
return $line;
}
 
/**
* Read until the socket closes, or until there is no more data in
* the inner PHP buffer. If the inner buffer is empty, in blocking
* mode we wait for at least 1 byte of data. Therefore, in
* blocking mode, if there is no data at all to be read, this
* function will never exit (unless the socket is closed on the
* remote end).
*
* @access public
*
* @return string All data until the socket closes, or a PEAR_Error if
* not connected.
*/
function readAll()
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
 
$data = '';
while (!feof($this->fp)) {
$data .= @fread($this->fp, $this->lineLength);
}
return $data;
}
 
/**
* Runs the equivalent of the select() system call on the socket
* with a timeout specified by tv_sec and tv_usec.
*
* @param integer $state Which of read/write/error to check for.
* @param integer $tv_sec Number of seconds for timeout.
* @param integer $tv_usec Number of microseconds for timeout.
*
* @access public
* @return False if select fails, integer describing which of read/write/error
* are ready, or PEAR_Error if not connected.
*/
function select($state, $tv_sec, $tv_usec = 0)
{
if (!is_resource($this->fp)) {
return $this->raiseError('not connected');
}
 
$read = null;
$write = null;
$except = null;
if ($state & NET_SOCKET_READ) {
$read[] = $this->fp;
}
if ($state & NET_SOCKET_WRITE) {
$write[] = $this->fp;
}
if ($state & NET_SOCKET_ERROR) {
$except[] = $this->fp;
}
if (false === ($sr = stream_select($read, $write, $except, $tv_sec, $tv_usec))) {
return false;
}
 
$result = 0;
if (count($read)) {
$result |= NET_SOCKET_READ;
}
if (count($write)) {
$result |= NET_SOCKET_WRITE;
}
if (count($except)) {
$result |= NET_SOCKET_ERROR;
}
return $result;
}
 
}
/trunk/api/pear/Net/FTP.php
New file
0,0 → 1,2116
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
 
/**
* Net_FTP main file.
*
* This file must be included to use the Net_FTP package.
*
* PHP versions 4 and 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @category Networking
* @package FTP
* @author Tobias Schlitt <toby@php.net>
* @copyright 1997-2005 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: FTP.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
* @link http://pear.php.net/package/Net_FTP
* @since File available since Release 0.0.1
*/
 
require_once 'PEAR.php';
 
/**
* Option to let the ls() method return only files.
*
* @since 1.3
* @name NET_FTP_FILES_ONLY
* @see Net_FTP::ls()
*/
define('NET_FTP_FILES_ONLY', 0, true);
 
/**
* Option to let the ls() method return only directories.
*
* @since 1.3
* @name NET_FTP_DIRS_ONLY
* @see Net_FTP::ls()
*/
define('NET_FTP_DIRS_ONLY', 1, true);
 
/**
* Option to let the ls() method return directories and files (default).
*
* @since 1.3
* @name NET_FTP_DIRS_FILES
* @see Net_FTP::ls()
*/
define('NET_FTP_DIRS_FILES', 2, true);
 
/**
* Option to let the ls() method return the raw directory listing from ftp_rawlist().
*
* @since 1.3
* @name NET_FTP_RAWLIST
* @see Net_FTP::ls()
*/
define('NET_FTP_RAWLIST', 3, true);
 
 
/**
* Error code to indicate a failed connection
* This error code indicates, that the connection you tryed to set up
* could not be established. Check your connection settings (host & port)!
*
* @since 1.3
* @name NET_FTP_ERR_CONNECT_FAILED
* @see Net_FTP::connect()
*/
define('NET_FTP_ERR_CONNECT_FAILED', -1);
 
/**
* Error code to indicate a failed login
* This error code indicates, that the login to the FTP server failed. Check
* your user data (username & password).
*
* @since 1.3
* @name NET_FTP_ERR_LOGIN_FAILED
* @see Net_FTP::login()
*/
define('NET_FTP_ERR_LOGIN_FAILED', -2);
 
/**
* Error code to indicate a failed directory change
* The cd() method failed. Ensure that the directory you wanted to access exists.
*
* @since 1.3
* @name NET_FTP_ERR_DIRCHANGE_FAILED
* @see Net_FTP::cd()
*/
define('NET_FTP_ERR_DIRCHANGE_FAILED', 2); // Compatibillity reasons!
 
/**
* Error code to indicate that Net_FTP could not determine the current path
* The cwd() method failed and could not determine the path you currently reside
* in on the FTP server.
*
* @since 1.3
* @name NET_FTP_ERR_DETERMINEPATH_FAILED
* @see Net_FTP::pwd()
*/
define('NET_FTP_ERR_DETERMINEPATH_FAILED', 4); // Compatibillity reasons!
 
/**
* Error code to indicate that the creation of a directory failed
* The directory you tryed to create could not be created. Check the
* access rights on the parent directory!
*
* @since 1.3
* @name NET_FTP_ERR_CREATEDIR_FAILED
* @see Net_FTP::mkdir()
*/
define('NET_FTP_ERR_CREATEDIR_FAILED', -4);
 
/**
* Error code to indicate that the EXEC execution failed.
* The execution of a command using EXEC failed. Ensure, that your
* FTP server supports the EXEC command.
*
* @since 1.3
* @name NET_FTP_ERR_EXEC_FAILED
* @see Net_FTP::execute()
*/
define('NET_FTP_ERR_EXEC_FAILED', -5);
 
/**
* Error code to indicate that the SITE command failed.
* The execution of a command using SITE failed. Ensure, that your
* FTP server supports the SITE command.
*
* @since 1.3
* @name NET_FTP_ERR_SITE_FAILED
* @see Net_FTP::site()
*/
define('NET_FTP_ERR_SITE_FAILED', -6);
 
/**
* Error code to indicate that the CHMOD command failed.
* The execution of CHMOD failed. Ensure, that your
* FTP server supports the CHMOD command and that you have the appropriate
* access rights to use CHMOD.
*
* @since 1.3
* @name NET_FTP_ERR_CHMOD_FAILED
* @see Net_FTP::chmod()
*/
define('NET_FTP_ERR_CHMOD_FAILED', -7);
 
/**
* Error code to indicate that a file rename failed
* The renaming of a file on the server failed. Ensure that you have the
* appropriate access rights to rename the file.
*
* @since 1.3
* @name NET_FTP_ERR_RENAME_FAILED
* @see Net_FTP::rename()
*/
define('NET_FTP_ERR_RENAME_FAILED', -8);
 
/**
* Error code to indicate that the MDTM command failed
* The MDTM command is not supported for directories. Ensure that you gave
* a file path to the mdtm() method, not a directory path.
*
* @since 1.3
* @name NET_FTP_ERR_MDTMDIR_UNSUPPORTED
* @see Net_FTP::mdtm()
*/
define('NET_FTP_ERR_MDTMDIR_UNSUPPORTED', -9);
 
/**
* Error code to indicate that the MDTM command failed
* The MDTM command failed. Ensure that your server supports the MDTM command.
*
* @since 1.3
* @name NET_FTP_ERR_MDTM_FAILED
* @see Net_FTP::mdtm()
*/
define('NET_FTP_ERR_MDTM_FAILED', -10);
 
/**
* Error code to indicate that a date returned by the server was misformated
* A date string returned by your server seems to be missformated and could not be
* parsed. Check that the server is configured correctly. If you're sure, please
* send an email to the auhtor with a dumped output of $ftp->ls('./', NET_FTP_RAWLIST);
* to get the date format supported.
*
* @since 1.3
* @name NET_FTP_ERR_DATEFORMAT_FAILED
* @see Net_FTP::mdtm(), Net_FTP::ls()
*/
define('NET_FTP_ERR_DATEFORMAT_FAILED', -11);
 
/**
* Error code to indicate that the SIZE command failed
* The determination of the filesize of a file failed. Ensure that your server supports the
* SIZE command.
*
* @since 1.3
* @name NET_FTP_ERR_SIZE_FAILED
* @see Net_FTP::size()
*/
define('NET_FTP_ERR_SIZE_FAILED', -12);
 
/**
* Error code to indicate that a local file could not be overwritten
* You specified not to overwrite files. Therefore the local file has not been
* overwriten. If you want to get the file overwriten, please set the option to
* do so.
*
* @since 1.3
* @name NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN
* @see Net_FTP::get(), Net_FTP::getRecursive()
*/
define('NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN', -13);
 
/**
* Error code to indicate that a local file could not be overwritten
* Also you specified to overwrite the local file you want to download to,
* it has not been possible to do so. Check that you have the appropriate access
* rights on the local file to overwrite it.
*
* @since 1.3
* @name NET_FTP_ERR_OVERWRITELOCALFILE_FAILED
* @see Net_FTP::get(), Net_FTP::getRecursive()
*/
define('NET_FTP_ERR_OVERWRITELOCALFILE_FAILED', -14);
 
/**
* Error code to indicate that the file you wanted to upload does not exist
* The file you tried to upload does not exist. Ensure that it exists.
*
* @since 1.3
* @name NET_FTP_ERR_LOCALFILENOTEXIST
* @see Net_FTP::put(), Net_FTP::putRecursive()
*/
define('NET_FTP_ERR_LOCALFILENOTEXIST', -15);
 
/**
* Error code to indicate that a remote file could not be overwritten
* You specified not to overwrite files. Therefore the remote file has not been
* overwriten. If you want to get the file overwriten, please set the option to
* do so.
*
* @since 1.3
* @name NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN
* @see Net_FTP::put(), Net_FTP::putRecursive()
*/
define('NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN', -16);
 
/**
* Error code to indicate that the upload of a file failed
* The upload you tried failed. Ensure that you have appropriate access rights
* to upload the desired file.
*
* @since 1.3
* @name NET_FTP_ERR_UPLOADFILE_FAILED
* @see Net_FTP::put(), Net_FTP::putRecursive()
*/
define('NET_FTP_ERR_UPLOADFILE_FAILED', -17);
 
/**
* Error code to indicate that you specified an incorrect directory path
* The remote path you specified seems not to be a directory. Ensure that
* the path you specify is a directory and that the path string ends with
* a /.
*
* @since 1.3
* @name NET_FTP_ERR_REMOTEPATHNODIR
* @see Net_FTP::putRecursive(), Net_FTP::getRecursive()
*/
define('NET_FTP_ERR_REMOTEPATHNODIR', -18);
 
/**
* Error code to indicate that you specified an incorrect directory path
* The local path you specified seems not to be a directory. Ensure that
* the path you specify is a directory and that the path string ends with
* a /.
*
* @since 1.3
* @name NET_FTP_ERR_LOCALPATHNODIR
* @see Net_FTP::putRecursive(), Net_FTP::getRecursive()
*/
define('NET_FTP_ERR_LOCALPATHNODIR', -19);
 
/**
* Error code to indicate that a local directory failed to be created
* You tried to create a local directory through getRecursive() method,
* which has failed. Ensure that you have the appropriate access rights
* to create it.
*
* @since 1.3
* @name NET_FTP_ERR_CREATELOCALDIR_FAILED
* @see Net_FTP::getRecursive()
*/
define('NET_FTP_ERR_CREATELOCALDIR_FAILED', -20);
 
/**
* Error code to indicate that the provided hostname was incorrect
* The hostname you provided was invalid. Ensure to provide either a
* full qualified domain name or an IP address.
*
* @since 1.3
* @name NET_FTP_ERR_HOSTNAMENOSTRING
* @see Net_FTP::setHostname()
*/
define('NET_FTP_ERR_HOSTNAMENOSTRING', -21);
 
/**
* Error code to indicate that the provided port was incorrect
* The port number you provided was invalid. Ensure to provide either a
* a numeric port number greater zero.
*
* @since 1.3
* @name NET_FTP_ERR_PORTLESSZERO
* @see Net_FTP::setPort()
*/
define('NET_FTP_ERR_PORTLESSZERO', -22);
 
/**
* Error code to indicate that you provided an invalid mode constant
* The mode constant you provided was invalid. You may only provide
* FTP_ASCII or FTP_BINARY.
*
* @since 1.3
* @name NET_FTP_ERR_NOMODECONST
* @see Net_FTP::setMode()
*/
define('NET_FTP_ERR_NOMODECONST', -23);
 
/**
* Error code to indicate that you provided an invalid timeout
* The timeout you provided was invalid. You have to provide a timeout greater
* or equal to zero.
*
* @since 1.3
* @name NET_FTP_ERR_TIMEOUTLESSZERO
* @see Net_FTP::Net_FTP(), Net_FTP::setTimeout()
*/
define('NET_FTP_ERR_TIMEOUTLESSZERO', -24);
 
/**
* Error code to indicate that you provided an invalid timeout
* An error occured while setting the timeout. Ensure that you provide a
* valid integer for the timeount and that your PHP installation works
* correctly.
*
* @since 1.3
* @name NET_FTP_ERR_SETTIMEOUT_FAILED
* @see Net_FTP::Net_FTP(), Net_FTP::setTimeout()
*/
define('NET_FTP_ERR_SETTIMEOUT_FAILED', -25);
 
/**
* Error code to indicate that the provided extension file doesn't exist
* The provided extension file does not exist. Ensure to provided an
* existant extension file.
*
* @since 1.3
* @name NET_FTP_ERR_EXTFILENOTEXIST
* @see Net_FTP::getExtensionFile()
*/
define('NET_FTP_ERR_EXTFILENOTEXIST', -26);
 
/**
* Error code to indicate that the provided extension file is not readable
* The provided extension file is not readable. Ensure to have sufficient
* access rights for it.
*
* @since 1.3
* @name NET_FTP_ERR_EXTFILEREAD_FAILED
* @see Net_FTP::getExtensionFile()
*/
define('NET_FTP_ERR_EXTFILEREAD_FAILED', -27);
 
/**
* Error code to indicate that the deletion of a file failed
* The specified file could not be deleted. Ensure to have sufficient
* access rights to delete the file.
*
* @since 1.3
* @name NET_FTP_ERR_EXTFILEREAD_FAILED
* @see Net_FTP::rm()
*/
define('NET_FTP_ERR_DELETEFILE_FAILED', -28);
 
/**
* Error code to indicate that the deletion of a directory faild
* The specified file could not be deleted. Ensure to have sufficient
* access rights to delete the file.
*
* @since 1.3
* @name NET_FTP_ERR_EXTFILEREAD_FAILED
* @see Net_FTP::rm()
*/
define('NET_FTP_ERR_DELETEDIR_FAILED', -29);
 
/**
* Error code to indicate that the directory listing failed
* PHP could not list the directory contents on the server. Ensure
* that your server is configured appropriate.
*
* @since 1.3
* @name NET_FTP_ERR_RAWDIRLIST_FAILED
* @see Net_FTP::ls()
*/
define('NET_FTP_ERR_RAWDIRLIST_FAILED', -30);
 
/**
* Error code to indicate that the directory listing failed
* The directory listing format your server uses seems not to
* be supported by Net_FTP. Please send the output of the
* call ls('./', NET_FTP_RAWLIST); to the author of this
* class to get it supported.
*
* @since 1.3
* @name NET_FTP_ERR_DIRLIST_UNSUPPORTED
* @see Net_FTP::ls()
*/
define('NET_FTP_ERR_DIRLIST_UNSUPPORTED', -31);
 
/**
* Error code to indicate failed disconnecting
* This error code indicates, that disconnection was not possible.
*
* @since 1.3
* @name NET_FTP_ERR_DISCONNECT_FAILED
* @see Net_FTP::disconnect()
*/
define('NET_FTP_ERR_DISCONNECT_FAILED', -32);
 
/**
* Error code to indicate that the username you provided was invalid.
* Check that you provided a non-empty string as the username.
*
* @since 1.3
* @name NET_FTP_ERR_USERNAMENOSTRING
* @see Net_FTP::setUsername()
*/
define('NET_FTP_ERR_USERNAMENOSTRING', -33);
 
/**
* Error code to indicate that the username you provided was invalid.
* Check that you provided a non-empty string as the username.
*
* @since 1.3
* @name NET_FTP_ERR_PASSWORDNOSTRING
* @see Net_FTP::setPassword()
*/
define('NET_FTP_ERR_PASSWORDNOSTRING', -33);
 
/**
* Class for comfortable FTP-communication
*
* This class provides comfortable communication with FTP-servers. You may do everything
* enabled by the PHP-FTP-extension and further functionalities, like recursive-deletion,
* -up- and -download. Another feature is to create directories recursively.
*
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @category Networking
* @package FTP
* @author Tobias Schlitt <toby@php.net>
* @copyright 1997-2005 The PHP Group
* @version Release: 1.3.0
* @link http://pear.php.net/package/Net_FTP
* @since 0.0.1
* @access public
*/
class Net_FTP extends PEAR
{
/**
* The host to connect to
*
* @access private
* @var string
*/
var $_hostname;
 
/**
* The port for ftp-connection (standard is 21)
*
* @access private
* @var int
*/
var $_port = 21;
 
/**
* The username for login
*
* @access private
* @var string
*/
var $_username;
 
/**
* The password for login
*
* @access private
* @var string
*/
var $_password;
 
/**
* Determine whether to use passive-mode (true) or active-mode (false)
*
* @access private
* @var bool
*/
var $_passv;
 
/**
* The standard mode for ftp-transfer
*
* @access private
* @var int
*/
var $_mode = FTP_BINARY;
 
/**
* This holds the handle for the ftp-connection
*
* @access private
* @var resource
*/
var $_handle;
 
/**
* Contains the timeout for FTP operations
*
* @access private
* @var int
* @since 1.3
*/
var $_timeout = 90;
/**
* Saves file-extensions for ascii- and binary-mode
*
* The array contains 2 sub-arrays ("ascii" and "binary"), which both contain
* file-extensions without the "." (".php" = "php").
*
* @access private
* @var array
*/
var $_file_extensions;
 
/**
* ls match
* Matches the ls entries against a regex and maps the resulting array to speaking names
*
* @access private
* @var array
* @since 1.3
*/
var $_ls_match = array(
'unix' => array(
'pattern' => '/(?:(d)|.)([rwxt-]+)\s+(\w+)\s+(\w+)\s+(\w+)\s+(\w+)\s+(\S+\s+\S+\s+\S+)\s+(.+)/',
'map' => array('name'=>8,'size'=>6,'rights'=>2,'user'=>4,'group'=>5,
'files_inside'=>3,'date'=>7,'is_dir'=>1)
),
'windows' => array(
'pattern' => '/(.+)\s+(.+)\s+((<DIR>)|[0-9]+)\s+(.+)/',
'map' => array('name'=>5,'date'=>1,'is_dir'=>3)
)
);
/**
* matcher
* Stores the matcher for the current connection
*
* @access private
* @var array
* @since 1.3
*/
var $_matcher = null;
/**
* Holds all Net_FTP_Observer objects
* that wish to be notified of new messages.
*
* @var array
* @access private
* @since 1.3
*/
var $_listeners = array();
 
/**
* This generates a new FTP-Object. The FTP-connection will not be established, yet.
* You can leave $host and $port blank, if you want. The $host will not be set
* and the $port will be left at 21. You have to set the $host manualy before
* trying to connect or with the connect() method.
*
* @access public
* @param string $host (optional) The hostname
* @param int $port (optional) The port
* @param int $timeout (optional) Sets the standard timeout
* @return void
* @see Net_FTP::setHostname(), Net_FTP::setPort(), Net_FTP::connect()
*/
function Net_FTP($host = null, $port = null, $timeout = 90)
{
$this->PEAR();
if (isset($host)) {
$this->setHostname($host);
}
if (isset($port)) {
$this->setPort($port);
}
$this->_timeout = $timeout;
$this->_file_extensions[FTP_ASCII] = array();
$this->_file_extensions[FTP_BINARY] = array();
}
 
/**
* This function generates the FTP-connection. You can optionally define a
* hostname and/or a port. If you do so, this data is stored inside the object.
*
* @access public
* @param string $host (optional) The Hostname
* @param int $port (optional) The Port
* @return mixed True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_CONNECT_FAILED
*/
function connect($host = null, $port = null)
{
$this->_matcher = null;
if (isset($host)) {
$this->setHostname($host);
}
if (isset($port)) {
$this->setPort($port);
}
$handle = @ftp_connect($this->getHostname(), $this->getPort(), $this->_timeout);
if (!$handle) {
return $this->raiseError("Connection to host failed", NET_FTP_ERR_CONNECT_FAILED);
} else {
$this->_handle =& $handle;
return true;
}
}
 
/**
* This function close the FTP-connection
*
* @access public
* @return bool|PEAR_Error Returns true on success, PEAR_Error on failure
*/
function disconnect()
{
$res = @ftp_close($this->_handle);
if (!$res) {
return PEAR::raiseError('Disconnect failed.', NET_FTP_ERR_DISCONNECT_FAILED);
}
return true;
}
 
/**
* This logges you into the ftp-server. You are free to specify username and password
* in this method. If you specify it, the values will be taken into the corresponding
* attributes, if do not specify, the attributes are taken.
*
* @access public
* @param string $username (optional) The username to use
* @param string $password (optional) The password to use
* @return mixed True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_LOGIN_FAILED
*/
function login($username = null, $password = null)
{
if (!isset($username)) {
$username = $this->getUsername();
} else {
$this->setUsername($username);
}
 
if (!isset($password)) {
$password = $this->getPassword();
} else {
$this->setPassword($password);
}
 
$res = @ftp_login($this->_handle, $username, $password);
 
if (!$res) {
return $this->raiseError("Unable to login", NET_FTP_ERR_LOGIN_FAILED);
} else {
return true;
}
}
 
/**
* This changes the currently used directory. You can use either an absolute
* directory-path (e.g. "/home/blah") or a relative one (e.g. "../test").
*
* @access public
* @param string $dir The directory to go to.
* @return mixed True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_DIRCHANGE_FAILED
*/
function cd($dir)
{
$erg = @ftp_chdir($this->_handle, $dir);
if (!$erg) {
return $this->raiseError("Directory change failed", NET_FTP_ERR_DIRCHANGE_FAILED);
} else {
return true;
}
}
 
/**
* Show's you the actual path on the server
* This function questions the ftp-handle for the actual selected path and returns it.
*
* @access public
* @return mixed The actual path or PEAR::Error
* @see NET_FTP_ERR_DETERMINEPATH_FAILED
*/
function pwd()
{
$res = @ftp_pwd($this->_handle);
if (!$res) {
return $this->raiseError("Could not determine the actual path.", NET_FTP_ERR_DETERMINEPATH_FAILED);
} else {
return $res;
}
}
 
/**
* This works similar to the mkdir-command on your local machine. You can either give
* it an absolute or relative path. The relative path will be completed with the actual
* selected server-path. (see: pwd())
*
* @access public
* @param string $dir Absolute or relative dir-path
* @param bool $recursive (optional) Create all needed directories
* @return mixed True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_CREATEDIR_FAILED
*/
function mkdir($dir, $recursive = false)
{
$dir = $this->_construct_path($dir);
$savedir = $this->pwd();
$this->pushErrorHandling(PEAR_ERROR_RETURN);
$e = $this->cd($dir);
$this->popErrorHandling();
if ($e === true) {
$this->cd($savedir);
return true;
}
$this->cd($savedir);
if ($recursive === false){
$res = @ftp_mkdir($this->_handle, $dir);
if (!$res) {
return $this->raiseError("Creation of '$dir' failed", NET_FTP_ERR_CREATEDIR_FAILED);
} else {
return true;
}
} else {
if(strpos($dir, '/') === false) {
return $this->mkdir($dir,false);
}
$pos = 0;
$res = $this->mkdir(dirname($dir), true);
$res = $this->mkdir($dir, false);
if ($res !== true) {
return $res;
}
return true;
}
}
 
/**
* This method tries executing a command on the ftp, using SITE EXEC.
*
* @access public
* @param string $command The command to execute
* @return mixed The result of the command (if successfull), otherwise PEAR::Error
* @see NET_FTP_ERR_EXEC_FAILED
*/
function execute($command)
{
$res = @ftp_exec($this->_handle, $command);
if (!$res) {
return $this->raiseError("Execution of command '$command' failed.", NET_FTP_ERR_EXEC_FAILED);
} else {
return $res;
}
}
 
/**
* Execute a SITE command on the server
* This method tries to execute a SITE command on the ftp server.
*
* @access public
* @param string $command The command with parameters to execute
* @return mixed True if successful, otherwise PEAR::Error
* @see NET_FTP_ERR_SITE_FAILED
*/
function site($command)
{
$res = @ftp_site($this->_handle, $command);
if (!$res) {
return $this->raiseError("Execution of SITE command '$command' failed.", NET_FTP_ERR_SITE_FAILED);
} else {
return $res;
}
}
 
/**
* This method will try to chmod the file specified on the server
* Currently, you must give a number as the the permission argument (777 or
* similar). The file can be either a relative or absolute path.
* NOTE: Some servers do not support this feature. In that case, you will
* get a PEAR error object returned. If successful, the method returns true
*
* @access public
* @param mixed $target The file or array of files to set permissions for
* @param integer $permissions The mode to set the file permissions to
* @return mixed True if successful, otherwise PEAR::Error
* @see NET_FTP_ERR_CHMOD_FAILED
*/
function chmod($target, $permissions)
{
// If $target is an array: Loop through it.
if (is_array($target)) {
 
for ($i = 0; $i < count($target); $i++) {
$res = $this->chmod($target[$i], $permissions);
if (PEAR::isError($res)) {
return $res;
} // end if isError
} // end for i < count($target)
 
} else {
 
$res = $this->site("CHMOD " . $permissions . " " . $target);
if (!$res) {
return PEAR::raiseError("CHMOD " . $permissions . " " . $target . " failed", NET_FTP_ERR_CHMOD_FAILED);
} else {
return $res;
}
 
} // end if is_array
 
} // end method chmod
 
/**
* This method will try to chmod a folder and all of its contents
* on the server. The target argument must be a folder or an array of folders
* and the permissions argument have to be an integer (i.e. 777).
* The file can be either a relative or absolute path.
* NOTE: Some servers do not support this feature. In that case, you
* will get a PEAR error object returned. If successful, the method
* returns true
*
* @access public
* @param mixed $target The folder or array of folders to
* set permissions for
* @param integer $permissions The mode to set the folder
* and file permissions to
* @return mixed True if successful, otherwise PEAR::Error
* @see NET_FTP_ERR_CHMOD_FAILED, NET_FTP_ERR_DETERMINEPATH_FAILED, NET_FTP_ERR_RAWDIRLIST_FAILED, NET_FTP_ERR_DIRLIST_UNSUPPORTED
*/
function chmodRecursive($target, $permissions)
{
static $dir_permissions;
 
if(!isset($dir_permissions)){ // Making directory specific permissions
$dir_permissions = $this->_makeDirPermissions($permissions);
}
 
// If $target is an array: Loop through it
if (is_array($target)) {
 
for ($i = 0; $i < count($target); $i++) {
$res = $this->chmodRecursive($target[$i], $permissions);
if (PEAR::isError($res)) {
return $res;
} // end if isError
} // end for i < count($target)
 
} else {
 
$remote_path = $this->_construct_path($target);
 
// Chmod the directory itself
$result = $this->chmod($remote_path, $dir_permissions);
 
if (PEAR::isError($result)) {
return $result;
}
 
// If $remote_path last character is not a slash, add one
if (substr($remote_path, strlen($remote_path)-1) != "/") {
 
$remote_path .= "/";
}
 
$dir_list = array();
$mode = NET_FTP_DIRS_ONLY;
$dir_list = $this->ls($remote_path, $mode);
foreach ($dir_list as $dir_entry) {
 
$remote_path_new = $remote_path.$dir_entry["name"]."/";
 
// Chmod the directory we're about to enter
$result = $this->chmod($remote_path_new, $dir_permissions);
 
if (PEAR::isError($result)) {
return $result;
}
 
$result = $this->chmodRecursive($remote_path_new, $permissions);
 
if (PEAR::isError($result)) {
return $result;
}
 
} // end foreach dir_list as dir_entry
 
$file_list = array();
$mode = NET_FTP_FILES_ONLY;
$file_list = $this->ls($remote_path, $mode);
 
foreach ($file_list as $file_entry) {
 
$remote_file = $remote_path.$file_entry["name"];
 
$result = $this->chmod($remote_file, $permissions);
 
if (PEAR::isError($result)) {
return $result;
}
 
} // end foreach $file_list
 
} // end if is_array
 
return true; // No errors
 
} // end method chmodRecursive
 
/**
* Rename or move a file or a directory from the ftp-server
*
* @access public
* @param string $remote_from The remote file or directory original to rename or move
* @param string $remote_to The remote file or directory final to rename or move
* @return bool $res True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_RENAME_FAILED
*/
 
function rename ($remote_from, $remote_to)
{
$res = @ftp_rename($this->_handle, $remote_from, $remote_to);
if(!$res) {
return $this->raiseError("Could not rename ".$remote_from." to ".$remote_to." !", NET_FTP_ERR_RENAME_FAILED);
}
return true;
}
 
/**
* This will return logical permissions mask for directory.
* if directory have to be writeable it have also be executable
*
* @access private
* @param string $permissions File permissions in digits for file (i.e. 666)
* @return string File permissions in digits for directory (i.e. 777)
*/
 
function _makeDirPermissions($permissions){
$permissions = (string)$permissions;
 
for($i = 0; $i < strlen($permissions); $i++){ // going through (user, group, world)
if((int)$permissions{$i} & 4 and !((int)$permissions{$i} & 1)){ // Read permission is set
// but execute not yet
(int)$permissions{$i} = (int)$permissions{$i} + 1; // Adding execute flag
}
}
 
return (string)$permissions;
}
 
/**
* This will return the last modification-time of a file. You can either give this
* function a relative or an absolute path to the file to check.
* NOTE: Some servers will not support this feature and the function works
* only on files, not directories! When successful,
* it will return the last modification-time as a unix-timestamp or, when $format is
* specified, a preformated timestring.
*
* @access public
* @param string $file The file to check
* @param string $format (optional) The format to give the date back
* if not set, it will return a Unix timestamp
* @return mixed Unix timestamp, a preformated date-string or PEAR::Error
* @see NET_FTP_ERR_MDTMDIR_UNSUPPORTED, NET_FTP_ERR_MDTM_FAILED, NET_FTP_ERR_DATEFORMAT_FAILED
*/
function mdtm($file, $format = null)
{
$file = $this->_construct_path($file);
if ($this->_check_dir($file)) {
return $this->raiseError("Filename '$file' seems to be a directory.", NET_FTP_ERR_MDTMDIR_UNSUPPORTED);
}
$res = @ftp_mdtm($this->_handle, $file);
if ($res == -1) {
return $this->raiseError("Could not get last-modification-date of '$file'.", NET_FTP_ERR_MDTM_FAILED);
}
if (isset($format)) {
$res = date($format, $res);
if (!$res) {
return $this->raiseError("Date-format failed on timestamp '$res'.", NET_FTP_ERR_DATEFORMAT_FAILED);
}
}
return $res;
}
 
/**
* This will return the size of a given file in bytes. You can either give this function
* a relative or an absolute file-path. NOTE: Some servers do not support this feature!
*
* @access public
* @param string $file The file to check
* @return mixed Size in bytes or PEAR::Error
* @see NET_FTP_ERR_SIZE_FAILED
*/
function size($file)
{
$file = $this->_construct_path($file);
$res = @ftp_size($this->_handle, $file);
if ($res == -1) {
return $this->raiseError("Could not determine filesize of '$file'.", NET_FTP_ERR_SIZE_FAILED);
} else {
return $res;
}
}
 
/**
* This method returns a directory-list of the current directory or given one.
* To display the current selected directory, simply set the first parameter to null
* or leave it blank, if you do not want to use any other parameters.
* <BR><BR>
* There are 4 different modes of listing directories. Either to list only
* the files (using NET_FTP_FILES_ONLY), to list only directories (using
* NET_FTP_DIRS_ONLY) or to show both (using NET_FTP_DIRS_FILES, which is default).
* <BR><BR>
* The 4th one is the NET_FTP_RAWLIST, which returns just the array created by the
* ftp_rawlist()-function build into PHP.
* <BR><BR>
* The other function-modes will return an array containing the requested data.
* The files and dirs are listed in human-sorted order, but if you select
* NET_FTP_DIRS_FILES the directories will be added above the files,
* but although both sorted.
* <BR><BR>
* All elements in the arrays are associative arrays themselves. The have the following
* structure:
* <BR><BR>
* Dirs:<BR>
* ["name"] => string The name of the directory<BR>
* ["rights"] => string The rights of the directory (in style "rwxr-xr-x")<BR>
* ["user"] => string The owner of the directory<BR>
* ["group"] => string The group-owner of the directory<BR>
* ["files_inside"]=> string The number of files/dirs inside the directory
* excluding "." and ".."<BR>
* ["date"] => int The creation-date as Unix timestamp<BR>
* ["is_dir"] => bool true, cause this is a dir<BR>
* <BR><BR>
* Files:<BR>
* ["name"] => string The name of the file<BR>
* ["size"] => int Size in bytes<BR>
* ["rights"] => string The rights of the file (in style "rwxr-xr-x")<BR>
* ["user"] => string The owner of the file<BR>
* ["group"] => string The group-owner of the file<BR>
* ["date"] => int The creation-date as Unix timestamp<BR>
* ["is_dir"] => bool false, cause this is a file<BR>
*
* @access public
* @param string $dir (optional) The directory to list or null, when listing the current directory.
* @param int $mode (optional) The mode which types to list (files, directories or both).
* @return mixed The directory list as described above or PEAR::Error on failure.
* @see NET_FTP_DIRS_FILES, NET_FTP_DIRS_ONLY, NET_FTP_FILES_ONLY, NET_FTP_RAWLIST, NET_FTP_ERR_DETERMINEPATH_FAILED, NET_FTP_ERR_RAWDIRLIST_FAILED, NET_FTP_ERR_DIRLIST_UNSUPPORTED
*/
function ls($dir = null, $mode = NET_FTP_DIRS_FILES)
{
if (!isset($dir)) {
$dir = @ftp_pwd($this->_handle);
if (!$dir) {
return $this->raiseError("Could not retrieve current directory", NET_FTP_ERR_DETERMINEPATH_FAILED);
}
}
if (($mode != NET_FTP_FILES_ONLY) && ($mode != NET_FTP_DIRS_ONLY) && ($mode != NET_FTP_RAWLIST)) {
$mode = NET_FTP_DIRS_FILES;
}
 
switch ($mode) {
case NET_FTP_DIRS_FILES: $res = $this->_ls_both ( $dir );
break;
case NET_FTP_DIRS_ONLY: $res = $this->_ls_dirs ( $dir );
break;
case NET_FTP_FILES_ONLY: $res = $this->_ls_files ( $dir );
break;
case NET_FTP_RAWLIST: $res = @ftp_rawlist($this->_handle, $dir);
break;
}
 
return $res;
}
 
/**
* This method will delete the given file or directory ($path) from the server
* (maybe recursive).
*
* Whether the given string is a file or directory is only determined by the last
* sign inside the string ("/" or not).
*
* If you specify a directory, you can optionally specify $recursive as true,
* to let the directory be deleted recursive (with all sub-directories and files
* inherited).
*
* You can either give a absolute or relative path for the file / dir. If you choose to
* use the relative path, it will be automatically completed with the actual
* selected directory.
*
* @access public
* @param string $path The absolute or relative path to the file / directory.
* @param bool $recursive (optional)
* @return mixed True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_DELETEFILE_FAILED, NET_FTP_ERR_DELETEDIR_FAILED, NET_FTP_ERR_REMOTEPATHNODIR
*/
function rm($path, $recursive = false)
{
$path = $this->_construct_path($path);
 
if ($this->_check_dir($path)) {
if ($recursive) {
return $this->_rm_dir_recursive($path);
} else {
return $this->_rm_dir($path);
}
} else {
return $this->_rm_file($path);
}
}
 
/**
* This function will download a file from the ftp-server. You can either spcify a absolute
* path to the file (beginning with "/") or a relative one, which will be completed
* with the actual directory you selected on the server. You can specify
* the path to which the file will be downloaded on the local
* maschine, if the file should be overwritten if it exists (optionally, default is
* no overwriting) and in which mode (FTP_ASCII or FTP_BINARY) the file should be
* downloaded (if you do not specify this, the method tries to determine it automatically
* from the mode-directory or uses the default-mode, set by you). If you give a relative
* path to the local-file, the script-path is used as basepath.
*
* @access public
* @param string $remote_file The absolute or relative path to the file to download
* @param string $local_file The local file to put the downloaded in
* @param bool $overwrite (optional) Whether to overwrite existing file
* @param int $mode (optional) Either FTP_ASCII or FTP_BINARY
* @return mixed True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN, NET_FTP_ERR_OVERWRITELOCALFILE_FAILED, NET_FTP_ERR_OVERWRITELOCALFILE_FAILED
*/
function get($remote_file, $local_file, $overwrite = false, $mode = null)
{
if (!isset($mode)) {
$mode = $this->checkFileExtension($remote_file);
}
 
$remote_file = $this->_construct_path($remote_file);
 
if (@file_exists($local_file) && !$overwrite) {
return $this->raiseError("Local file '$local_file' exists and may not be overwriten.", NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN);
}
if (@file_exists($local_file) && !@is_writeable($local_file) && $overwrite) {
return $this->raiseError("Local file '$local_file' is not writeable. Can not overwrite.", NET_FTP_ERR_OVERWRITELOCALFILE_FAILED);
}
 
if (@function_exists('ftp_nb_get')){
$res = @ftp_nb_get($this->_handle, $local_file, $remote_file, $mode);
while ($res == FTP_MOREDATA) {
$this->_announce('nb_get');
$res = @ftp_nb_continue ($this->_handle);
}
} else {
$res = @ftp_get($this->_handle, $local_file, $remote_file, $mode);
}
if (!$res) {
return $this->raiseError("File '$remote_file' could not be downloaded to '$local_file'.", NET_FTP_ERR_OVERWRITELOCALFILE_FAILED);
} else {
return true;
}
}
 
/**
* This function will upload a file to the ftp-server. You can either specify a absolute
* path to the remote-file (beginning with "/") or a relative one, which will be completed
* with the actual directory you selected on the server. You can specify
* the path from which the file will be uploaded on the local
* maschine, if the file should be overwritten if it exists (optionally, default is
* no overwriting) and in which mode (FTP_ASCII or FTP_BINARY) the file should be
* downloaded (if you do not specify this, the method tries to determine it automatically
* from the mode-directory or uses the default-mode, set by you). If you give a relative
* path to the local-file, the script-path is used as basepath.
*
* @access public
* @param string $local_file The local file to upload
* @param string $remote_file The absolute or relative path to the file to upload to
* @param bool $overwrite (optional) Whether to overwrite existing file
* @param int $mode (optional) Either FTP_ASCII or FTP_BINARY
* @return mixed True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_LOCALFILENOTEXIST, NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN, NET_FTP_ERR_UPLOADFILE_FAILED
*/
function put($local_file, $remote_file, $overwrite = false, $mode = null)
{
if (!isset($mode)) {
$mode = $this->checkFileExtension($local_file);
}
$remote_file = $this->_construct_path($remote_file);
 
if (!@file_exists($local_file)) {
return $this->raiseError("Local file '$local_file' does not exist.", NET_FTP_ERR_LOCALFILENOTEXIST);
}
if ((@ftp_size($this->_handle, $remote_file) != -1) && !$overwrite) {
return $this->raiseError("Remote file '$remote_file' exists and may not be overwriten.", NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN);
}
 
if (function_exists('ftp_nb_put')){
$res = @ftp_nb_put($this->_handle, $remote_file, $local_file, $mode);
while ($res == FTP_MOREDATA) {
$this->_announce('nb_put');
$res = @ftp_nb_continue($this->_handle);
}
 
} else {
$res = @ftp_put($this->_handle, $remote_file, $local_file, $mode);
}
if (!$res) {
return $this->raiseError("File '$local_file' could not be uploaded to '$remote_file'.", NET_FTP_ERR_UPLOADFILE_FAILED);
} else {
return true;
}
}
 
/**
* This functionality allows you to transfer a whole directory-structure from the
* remote-ftp to your local host. You have to give a remote-directory (ending with
* '/') and the local directory (ending with '/') where to put the files you download.
* The remote path is automatically completed with the current-remote-dir, if you give
* a relative path to this function. You can give a relative path for the $local_path,
* too. Then the script-basedir will be used for comletion of the path.
* The parameter $overwrite will determine, whether to overwrite existing files or not.
* Standard for this is false. Fourth you can explicitly set a mode for all transfer-
* actions done. If you do not set this, the method tries to determine the transfer-
* mode by checking your mode-directory for the file-extension. If the extension is not
* inside the mode-directory, it will get your default-mode.
*
* @access public
* @param string $remote_path The path to download
* @param string $local_path The path to download to
* @param bool $overwrite (optional) Whether to overwrite existing files (true) or not (false, standard).
* @param int $mode (optional) The transfermode (either FTP_ASCII or FTP_BINARY).
* @return mixed True on succes, otherwise PEAR::Error
* @see NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN, NET_FTP_ERR_OVERWRITELOCALFILE_FAILED, NET_FTP_ERR_OVERWRITELOCALFILE_FAILED, NET_FTP_ERR_REMOTEPATHNODIR, NET_FTP_ERR_LOCALPATHNODIR,NET_FTP_ERR_CREATELOCALDIR_FAILED
*/
function getRecursive($remote_path, $local_path, $overwrite = false, $mode = null)
{
$remote_path = $this->_construct_path($remote_path);
if (!$this->_check_dir($remote_path)) {
return $this->raiseError("Given remote-path '$remote_path' seems not to be a directory.", NET_FTP_ERR_REMOTEPATHNODIR);
}
if (!$this->_check_dir($local_path)) {
return $this->raiseError("Given local-path '$local_path' seems not to be a directory.", NET_FTP_ERR_LOCALPATHNODIR);
}
 
if (!@is_dir($local_path)) {
$res = @mkdir($local_path);
if (!$res) {
return $this->raiseError("Could not create dir '$local_path'", NET_FTP_ERR_CREATELOCALDIR_FAILED);
}
}
$dir_list = array();
$dir_list = $this->ls($remote_path, NET_FTP_DIRS_ONLY);
foreach ($dir_list as $dir_entry) {
if ($dir_entry['name'] != '.' && $dir_entry['name'] != '..') {
$remote_path_new = $remote_path.$dir_entry["name"]."/";
$local_path_new = $local_path.$dir_entry["name"]."/";
$result = $this->getRecursive($remote_path_new, $local_path_new, $overwrite, $mode);
if ($this->isError($result)) {
return $result;
}
}
}
$file_list = array();
$file_list = $this->ls($remote_path, NET_FTP_FILES_ONLY);
foreach ($file_list as $file_entry) {
$remote_file = $remote_path.$file_entry["name"];
$local_file = $local_path.$file_entry["name"];
$result = $this->get($remote_file, $local_file, $overwrite, $mode);
if ($this->isError($result)) {
return $result;
}
}
return true;
}
 
/**
* This functionality allows you to transfer a whole directory-structure from your
* local host to the remote-ftp. You have to give a remote-directory (ending with
* '/') and the local directory (ending with '/') where to put the files you download.
* The remote path is automatically completed with the current-remote-dir, if you give
* a relative path to this function. You can give a relative path for the $local_path,
* too. Then the script-basedir will be used for comletion of the path.
* The parameter $overwrite will determine, whether to overwrite existing files or not.
* Standard for this is false. Fourth you can explicitly set a mode for all transfer-
* actions done. If you do not set this, the method tries to determine the transfer-
* mode by checking your mode-directory for the file-extension. If the extension is not
* inside the mode-directory, it will get your default-mode.
*
* @access public
* @param string $remote_path The path to download
* @param string $local_path The path to download to
* @param bool $overwrite (optional) Whether to overwrite existing files (true) or not (false, standard).
* @param int $mode (optional) The transfermode (either FTP_ASCII or FTP_BINARY).
* @return mixed True on succes, otherwise PEAR::Error
* @see NET_FTP_ERR_LOCALFILENOTEXIST, NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN, NET_FTP_ERR_UPLOADFILE_FAILED, NET_FTP_ERR_LOCALPATHNODIR, NET_FTP_ERR_REMOTEPATHNODIR
*/
function putRecursive($local_path, $remote_path, $overwrite = false, $mode = null)
{
$remote_path = $this->_construct_path($remote_path);
if (!$this->_check_dir($local_path) || !is_dir($local_path)) {
return $this->raiseError("Given local-path '$local_path' seems not to be a directory.", NET_FTP_ERR_LOCALPATHNODIR);
}
if (!$this->_check_dir($remote_path)) {
return $this->raiseError("Given remote-path '$remote_path' seems not to be a directory.", NET_FTP_ERR_REMOTEPATHNODIR);
}
$old_path = $this->pwd();
if ($this->isError($this->cd($remote_path))) {
$res = $this->mkdir($remote_path);
if ($this->isError($res)) {
return $res;
}
}
$this->cd($old_path);
$dir_list = $this->_ls_local($local_path);
foreach ($dir_list["dirs"] as $dir_entry) {
$remote_path_new = $remote_path.$dir_entry."/";
$local_path_new = $local_path.$dir_entry."/";
$result = $this->putRecursive($local_path_new, $remote_path_new, $overwrite, $mode);
if ($this->isError($result)) {
return $result;
}
}
 
foreach ($dir_list["files"] as $file_entry) {
$remote_file = $remote_path.$file_entry;
$local_file = $local_path.$file_entry;
$result = $this->put($local_file, $remote_file, $overwrite, $mode);
if ($this->isError($result)) {
return $result;
}
}
return true;
}
 
/**
* This checks, whether a file should be transfered in ascii- or binary-mode
* by it's file-extension. If the file-extension is not set or
* the extension is not inside one of the extension-dirs, the actual set
* transfer-mode is returned.
*
* @access public
* @param string $filename The filename to be checked
* @return int Either FTP_ASCII or FTP_BINARY
*/
function checkFileExtension($filename)
{
$pattern = "/\.(.*)$/";
$has_extension = preg_match($pattern, $filename, $eregs);
if (!$has_extension) {
return $this->_mode;
} else {
$ext = $eregs[1];
}
 
if (!empty($this->_file_extensions[$ext])) {
return $this->_file_extensions[$ext];
}
 
return $this->_mode;
}
 
/**
* Set the Hostname
*
* @access public
* @param string $host The Hostname to set
* @return bool True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_HOSTNAMENOSTRING
*/
function setHostname($host)
{
if (!is_string($host)) {
return PEAR::raiseError("Hostname must be a string.", NET_FTP_ERR_HOSTNAMENOSTRING);
}
$this->_hostname = $host;
return true;
}
 
/**
* Set the Port
*
* @access public
* @param int $port The Port to set
* @return bool True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_PORTLESSZERO
*/
function setPort($port)
{
if (!is_int($port) || ($port < 0)) {
PEAR::raiseError("Invalid port. Has to be integer >= 0", NET_FTP_ERR_PORTLESSZERO);
}
$this->_port = $port;
return true;
}
 
/**
* Set the Username
*
* @access public
* @param string $user The Username to set
* @return mixed True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_USERNAMENOSTRING
*/
function setUsername($user)
{
if (empty($user) || !is_string($user)) {
return PEAR::raiseError('Username $user invalid.', NET_FTP_ERR_USERNAMENOSTRING);
}
$this->_username = $user;
}
 
/**
* Set the Password
*
* @access private
* @param string $password The Password to set
* @return void
* @see NET_FTP_ERR_PASSWORDNOSTRING
*/
function setPassword($password)
{
if (empty($password) || !is_string($password)) {
return PEAR::raiseError('Password xxx invalid.', NET_FTP_ERR_PASSWORDNOSTRING);
}
$this->_password = $password;
}
 
/**
* Set the transfer-mode. You can use the predefined constants
* FTP_ASCII or FTP_BINARY. The mode will be stored for any further transfers.
*
* @access public
* @param int $mode The mode to set
* @return mixed True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_NOMODECONST
*/
function setMode($mode)
{
if (($mode == FTP_ASCII) || ($mode == FTP_BINARY)) {
$this->_mode = $mode;
return true;
} else {
return $this->raiseError('FTP-Mode has either to be FTP_ASCII or FTP_BINARY', NET_FTP_ERR_NOMODECONST);
}
}
 
/**
* Set the transfer-method to passive mode
*
* @access public
* @return void
*/
function setPassive()
{
$this->_passv = true;
@ftp_pasv($this->_handle, true);
}
 
/**
* Set the transfer-method to active mode
*
* @access public
* @return void
*/
function setActive()
{
$this->_passv = false;
@ftp_pasv($this->_handle, false);
}
 
/**
* Set the timeout for FTP operations
* Use this method to set a timeout for FTP operation. Timeout has to be an integer.
*
* @acess public
* @param int $timeout the timeout to use
* @return bool True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_TIMEOUTLESSZERO, NET_FTP_ERR_SETTIMEOUT_FAILED
*/
function setTimeout ( $timeout = 0 )
{
if (!is_int($timeout) || ($timeout < 0)) {
return PEAR::raiseError("Timeout $timeout is invalid, has to be an integer >= 0", NET_FTP_ERR_TIMEOUTLESSZERO);
}
$this->_timeout = $timeout;
if (isset($this->_handle) && is_resource($this->_handle)) {
$res = @ftp_set_option($this->_handle, FTP_TIMEOUT_SEC, $timeout);
} else {
$res = true;
}
if (!$res) {
return PEAR::raiseError("Set timeout failed.", NET_FTP_ERR_SETTIMEOUT_FAILED);
}
return true;
}
/**
* Adds an extension to a mode-directory
* The mode-directory saves file-extensions coresponding to filetypes
* (ascii e.g.: 'php', 'txt', 'htm',...; binary e.g.: 'jpg', 'gif', 'exe',...).
* The extensions have to be saved without the '.'. And
* can be predefined in an external file (see: getExtensionsFile()).
*
* The array is build like this: 'php' => FTP_ASCII, 'png' => FTP_BINARY
*
* To change the mode of an extension, just add it again with the new mode!
*
* @access public
* @param int $mode Either FTP_ASCII or FTP_BINARY
* @param string $ext Extension
* @return void
*/
function addExtension($mode, $ext)
{
$this->_file_extensions[$ext] = $mode;
}
 
/**
* This function removes an extension from the mode-directories
* (described above).
*
* @access public
* @param string $ext The extension to remove
* @return void
*/
function removeExtension($ext)
{
unset($this->_file_extensions[$ext]);
}
 
/**
* This get's both (ascii- and binary-mode-directories) from the given file.
* Beware, if you read a file into the mode-directory, all former set values
* will be unset!
*
* @access public
* @param string $filename The file to get from
* @return mixed True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_EXTFILENOTEXIST, NET_FTP_ERR_EXTFILEREAD_FAILED
*/
function getExtensionsFile($filename)
{
if (!file_exists($filename)) {
return $this->raiseError("Extensions-file '$filename' does not exist", NET_FTP_ERR_EXTFILENOTEXIST);
}
 
if (!is_readable($filename)) {
return $this->raiseError("Extensions-file '$filename' is not readable", NET_FTP_ERR_EXTFILEREAD_FAILED);
}
 
$this->_file_extension = @parse_ini_file($filename);
return true;
}
 
/**
* Returns the Hostname
*
* @access public
* @return string The Hostname
*/
function getHostname()
{
return $this->_hostname;
}
 
/**
* Returns the Port
*
* @access public
* @return int The Port
*/
function getPort()
{
return $this->_port;
}
 
/**
* Returns the Username
*
* @access public
* @return string The Username
*/
function getUsername()
{
return $this->_username;
}
 
/**
* Returns the Password
*
* @access public
* @return string The Password
*/
function getPassword()
{
return $this->_password;
}
 
/**
* Returns the Transfermode
*
* @access public
* @return int The transfermode, either FTP_ASCII or FTP_BINARY.
*/
function getMode()
{
return $this->_mode;
}
 
/**
* Returns, whether the connection is set to passive mode or not
*
* @access public
* @return bool True if passive-, false if active-mode
*/
function isPassive()
{
return $this->_passv;
}
 
/**
* Returns the mode set for a file-extension
*
* @access public
* @param string $ext The extension you wanna ask for
* @return int Either FTP_ASCII, FTP_BINARY or NULL (if not set a mode for it)
*/
function getExtensionMode($ext)
{
return @$this->_file_extensions[$ext];
}
 
/**
* Get the currently set timeout.
* Returns the actual timeout set.
*
* @access public
* @return int The actual timeout
*/
function getTimeout ( )
{
return ftp_get_option($this->_handle, FTP_TIMEOUT_SEC);
}
 
/**
* Adds a Net_FTP_Observer instance to the list of observers
* that are listening for messages emitted by this Net_FTP instance.
*
* @param object $observer The Net_FTP_Observer instance to attach
* as a listener.
* @return boolean True if the observer is successfully attached.
* @access public
* @since 1.3
*/
function attach(&$observer)
{
if (!is_a($observer, 'Net_FTP_Observer')) {
return false;
}
 
$this->_listeners[$observer->getId()] = &$observer;
return true;
}
 
/**
* Removes a Net_FTP_Observer instance from the list of observers.
*
* @param object $observer The Net_FTP_Observer instance to detach
* from the list of listeners.
* @return boolean True if the observer is successfully detached.
* @access public
* @since 1.3
*/
function detach($observer)
{
if (!is_a($observer, 'Net_FTP_Observer') ||
!isset($this->_listeners[$observer->getId()])) {
return false;
}
 
unset($this->_listeners[$observer->getId()]);
return true;
}
 
/**
* Informs each registered observer instance that a new message has been
* sent.
*
* @param mixed $event A hash describing the net event.
* @access private
* @since 1.3
*/
function _announce($event)
{
foreach ($this->_listeners as $id => $listener) {
$this->_listeners[$id]->notify($event);
}
}
/**
* Rebuild the path, if given relative
*
* @access private
* @param string $path The path to check and construct
* @return string The build path
*/
function _construct_path($path)
{
if ((substr($path, 0, 1) != "/") && (substr($path, 0, 2) != "./")) {
$actual_dir = @ftp_pwd($this->_handle);
if (substr($actual_dir, (strlen($actual_dir) - 2), 1) != "/") {
$actual_dir .= "/";
}
$path = $actual_dir.$path;
}
return $path;
}
 
/**
* Checks, whether a given string is a directory-path (ends with "/") or not.
*
* @access private
* @param string $path Path to check
* @return bool True if $path is a directory, otherwise false
*/
function _check_dir($path)
{
if (substr($path, (strlen($path) - 1), 1) == "/") {
return true;
} else {
return false;
}
}
 
/**
* This will remove a file
*
* @access private
* @param string $file The file to delete
* @return mixed True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_DELETEFILE_FAILED
*/
function _rm_file($file)
{
if (substr($file, 0, 1) != "/") {
$actual_dir = @ftp_pwd($this->_handle);
if (substr($actual_dir, (strlen($actual_dir) - 2), 1) != "/") {
$actual_dir .= "/";
}
$file = $actual_dir.$file;
}
$res = @ftp_delete($this->_handle, $file);
if (!$res) {
return $this->raiseError("Could not delete file '$file'.", NET_FTP_ERR_DELETEFILE_FAILED);
} else {
return true;
}
}
 
/**
* This will remove a dir
*
* @access private
* @param string $dir The dir to delete
* @return mixed True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_REMOTEPATHNODIR, NET_FTP_ERR_DELETEDIR_FAILED
*/
function _rm_dir($dir)
{
if (substr($dir, (strlen($dir) - 1), 1) != "/") {
return $this->raiseError("Directory name '$dir' is invalid, has to end with '/'", NET_FTP_ERR_REMOTEPATHNODIR);
}
$res = @ftp_rmdir($this->_handle, $dir);
if (!$res) {
return $this->raiseError("Could not delete directory '$dir'.", NET_FTP_ERR_DELETEDIR_FAILED);
} else {
return true;
}
}
 
/**
* This will remove a dir and all subdirs and -files
*
* @access private
* @param string $file The dir to delete recursively
* @return mixed True on success, otherwise PEAR::Error
* @see NET_FTP_ERR_REMOTEPATHNODIR, NET_FTP_ERR_DELETEDIR_FAILED
*/
function _rm_dir_recursive($dir)
{
if (substr($dir, (strlen($dir) - 1), 1) != "/") {
return $this->raiseError("Directory name '$dir' is invalid, has to end with '/'", NET_FTP_ERR_REMOTEPATHNODIR);
}
$file_list = $this->_ls_files($dir);
foreach ($file_list as $file) {
$file = $dir.$file["name"];
$res = $this->rm($file);
if ($this->isError($res)) {
return $res;
}
}
$dir_list = $this->_ls_dirs($dir);
foreach ($dir_list as $new_dir) {
$new_dir = $dir.$new_dir["name"]."/";
$res = $this->_rm_dir_recursive($new_dir);
if ($this->isError($res)) {
return $res;
}
}
$res = $this->_rm_dir($dir);
if (PEAR::isError($res)) {
return $res;
} else {
return true;
}
}
 
/**
* Lists up files and directories
*
* @access private
* @param string $dir The directory to list up
* @return array An array of dirs and files
*/
function _ls_both($dir)
{
$list_splitted = $this->_list_and_parse($dir);
if (!is_array($list_splitted["files"])) {
$list_splitted["files"] = array();
}
if (!is_array($list_splitted["dirs"])) {
$list_splitted["dirs"] = array();
}
$res = array();
@array_splice($res, 0, 0, $list_splitted["files"]);
@array_splice($res, 0, 0, $list_splitted["dirs"]);
return $res;
}
 
/**
* Lists up directories
*
* @access private
* @param string $dir The directory to list up
* @return array An array of dirs
*/
function _ls_dirs($dir)
{
$list = $this->_list_and_parse($dir);
if (PEAR::isError($list)) {
return $list;
}
return $list["dirs"];
}
 
/**
* Lists up files
*
* @access private
* @param string $dir The directory to list up
* @return array An array of files
*/
function _ls_files($dir)
{
$list = $this->_list_and_parse($dir);
if (PEAR::isError($list)) {
return $list;
}
return $list["files"];
}
 
/**
* This lists up the directory-content and parses the items into well-formated arrays
* The results of this array are sorted (dirs on top, sorted by name;
* files below, sorted by name).
*
* @access private
* @param string $dir The directory to parse
* @return array Lists of dirs and files
* @see NET_FTP_ERR_RAWDIRLIST_FAILED
*/
function _list_and_parse($dir)
{
$dirs_list = array();
$files_list = array();
$dir_list = @ftp_rawlist($this->_handle, $dir);
if ($dir_list === false) {
return PEAR::raiseError('Could not get raw directory listing.', NET_FTP_ERR_RAWDIRLIST_FAILED);
}
if (!isset($this->_matcher) || PEAR::isError($this->_matcher)) {
$this->_matcher = $this->_determine_os_match($dir_list);
if (PEAR::isError($this->_matcher)) {
return $this->_matcher;
}
}
foreach ($dir_list as $entry) {
if (!preg_match($this->_matcher['pattern'], $entry, $m)) {
continue;
}
$entry = array();
foreach ($this->_matcher['map'] as $key=>$val) {
$entry[$key] = $m[$val];
}
$entry['stamp'] = $this->_parse_Date($entry['date']);
 
if ($entry['is_dir']) {
$dirs_list[] = $entry;
} else {
$files_list[] = $entry;
}
}
@usort($dirs_list, array("Net_FTP", "_nat_sort"));
@usort($files_list, array("Net_FTP", "_nat_sort"));
$res["dirs"] = (is_array($dirs_list)) ? $dirs_list : array();
$res["files"] = (is_array($files_list)) ? $files_list : array();
return $res;
}
/**
* Determine server OS
* This determines the server OS and returns a valid regex to parse
* ls() output.
*
* @access private
* @param array $dir_list The raw dir list to parse
* @return mixed An array of 'pattern' and 'map' on success, otherwise PEAR::Error
* @see NET_FTP_ERR_DIRLIST_UNSUPPORTED
*/
function _determine_os_match(&$dir_list) {
foreach ($dir_list as $entry) {
foreach ($this->_ls_match as $os => $match) {
if (preg_match($match['pattern'], $entry)) {
return $match;
}
}
}
$error = 'The list style of your server seems not to be supported. Please email a "$ftp->ls(NET_FTP_RAWLIST);" output plus info on the server to the maintainer of this package to get it supported! Thanks for your help!';
return PEAR::raiseError($error, NET_FTP_ERR_DIRLIST_UNSUPPORTED);
}
/**
* Lists a local directory
*
* @access private
* @param string $dir_path The dir to list
* @return array The list of dirs and files
*/
function _ls_local($dir_path)
{
$dir = dir($dir_path);
$dir_list = array();
$file_list = array();
while (false !== ($entry = $dir->read())) {
if (($entry != '.') && ($entry != '..')) {
if (is_dir($dir_path.$entry)) {
$dir_list[] = $entry;
} else {
$file_list[] = $entry;
}
}
}
$dir->close();
$res['dirs'] = $dir_list;
$res['files'] = $file_list;
return $res;
}
 
/**
* Function for use with usort().
* Compares the list-array-elements by name.
*
* @access private
*/
function _nat_sort($item_1, $item_2)
{
return strnatcmp($item_1['name'], $item_2['name']);
}
 
/**
* Parse dates to timestamps
*
* @access private
* @param string $date Date
* @return int Timestamp
* @see NET_FTP_ERR_DATEFORMAT_FAILED
*/
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";
$tmpDate = strtotime($date);
if ($tmpDate > time()) {
$year--;
$date = "$month $day, $year $hour:$minute";
}
}
// 09-10-04 => 09/10/04
elseif (preg_match('/^\d\d-\d\d-\d\d/',$date)) {
$date = str_replace('-','/',$date);
}
$res = strtotime($date);
if (!$res) {
return $this->raiseError('Dateconversion failed.', NET_FTP_ERR_DATEFORMAT_FAILED);
}
return $res;
}
}
?>
/trunk/api/pear/Net/URL.php
New file
0,0 → 1,410
<?php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2002-2004, Richard Heyes |
// | All rights reserved. |
// | |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | are met: |
// | |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | permission. |
// | |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// | |
// +-----------------------------------------------------------------------+
// | Author: Richard Heyes <richard at php net> |
// +-----------------------------------------------------------------------+
//
// $Id: URL.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
//
// Net_URL Class
 
class Net_URL
{
/**
* Full url
* @var string
*/
var $url;
 
/**
* Protocol
* @var string
*/
var $protocol;
 
/**
* Username
* @var string
*/
var $username;
 
/**
* Password
* @var string
*/
var $password;
 
/**
* Host
* @var string
*/
var $host;
 
/**
* Port
* @var integer
*/
var $port;
 
/**
* Path
* @var string
*/
var $path;
 
/**
* Query string
* @var array
*/
var $querystring;
 
/**
* Anchor
* @var string
*/
var $anchor;
 
/**
* Whether to use []
* @var bool
*/
var $useBrackets;
 
/**
* PHP4 Constructor
*
* @see __construct()
*/
function Net_URL($url = null, $useBrackets = true)
{
$this->__construct($url, $useBrackets);
}
 
/**
* PHP5 Constructor
*
* Parses the given url and stores the various parts
* Defaults are used in certain cases
*
* @param string $url Optional URL
* @param bool $useBrackets Whether to use square brackets when
* multiple querystrings with the same name
* exist
*/
function __construct($url = null, $useBrackets = true)
{
$HTTP_SERVER_VARS = !empty($_SERVER) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
 
$this->useBrackets = $useBrackets;
$this->url = $url;
$this->user = '';
$this->pass = '';
$this->host = '';
$this->port = 80;
$this->path = '';
$this->querystring = array();
$this->anchor = '';
 
// Only use defaults if not an absolute URL given
if (!preg_match('/^[a-z0-9]+:\/\//i', $url)) {
 
$this->protocol = (@$HTTP_SERVER_VARS['HTTPS'] == 'on' ? 'https' : 'http');
 
/**
* Figure out host/port
*/
if (!empty($HTTP_SERVER_VARS['HTTP_HOST']) AND preg_match('/^(.*)(:([0-9]+))?$/U', $HTTP_SERVER_VARS['HTTP_HOST'], $matches)) {
$host = $matches[1];
if (!empty($matches[3])) {
$port = $matches[3];
} else {
$port = $this->getStandardPort($this->protocol);
}
}
 
$this->user = '';
$this->pass = '';
$this->host = !empty($host) ? $host : (isset($HTTP_SERVER_VARS['SERVER_NAME']) ? $HTTP_SERVER_VARS['SERVER_NAME'] : 'localhost');
$this->port = !empty($port) ? $port : (isset($HTTP_SERVER_VARS['SERVER_PORT']) ? $HTTP_SERVER_VARS['SERVER_PORT'] : $this->getStandardPort($this->protocol));
$this->path = !empty($HTTP_SERVER_VARS['PHP_SELF']) ? $HTTP_SERVER_VARS['PHP_SELF'] : '/';
$this->querystring = isset($HTTP_SERVER_VARS['QUERY_STRING']) ? $this->_parseRawQuerystring($HTTP_SERVER_VARS['QUERY_STRING']) : null;
$this->anchor = '';
}
 
// Parse the url and store the various parts
if (!empty($url)) {
$urlinfo = parse_url($url);
 
// Default querystring
$this->querystring = array();
 
foreach ($urlinfo as $key => $value) {
switch ($key) {
case 'scheme':
$this->protocol = $value;
$this->port = $this->getStandardPort($value);
break;
 
case 'user':
case 'pass':
case 'host':
case 'port':
$this->$key = $value;
break;
 
case 'path':
if ($value{0} == '/') {
$this->path = $value;
} else {
$path = dirname($this->path) == DIRECTORY_SEPARATOR ? '' : dirname($this->path);
$this->path = sprintf('%s/%s', $path, $value);
}
break;
 
case 'query':
$this->querystring = $this->_parseRawQueryString($value);
break;
 
case 'fragment':
$this->anchor = $value;
break;
}
}
}
}
 
/**
* Returns full url
*
* @return string Full url
* @access public
*/
function getURL()
{
$querystring = $this->getQueryString();
 
$this->url = $this->protocol . '://'
. $this->user . (!empty($this->pass) ? ':' : '')
. $this->pass . (!empty($this->user) ? '@' : '')
. $this->host . ($this->port == $this->getStandardPort($this->protocol) ? '' : ':' . $this->port)
. $this->path
. (!empty($querystring) ? '?' . $querystring : '')
. (!empty($this->anchor) ? '#' . $this->anchor : '');
 
return $this->url;
}
 
/**
* Adds a querystring item
*
* @param string $name Name of item
* @param string $value Value of item
* @param bool $preencoded Whether value is urlencoded or not, default = not
* @access public
*/
function addQueryString($name, $value, $preencoded = false)
{
if ($preencoded) {
$this->querystring[$name] = $value;
} else {
$this->querystring[$name] = is_array($value) ? array_map('rawurlencode', $value): rawurlencode($value);
}
}
 
/**
* Removes a querystring item
*
* @param string $name Name of item
* @access public
*/
function removeQueryString($name)
{
if (isset($this->querystring[$name])) {
unset($this->querystring[$name]);
}
}
 
/**
* Sets the querystring to literally what you supply
*
* @param string $querystring The querystring data. Should be of the format foo=bar&x=y etc
* @access public
*/
function addRawQueryString($querystring)
{
$this->querystring = $this->_parseRawQueryString($querystring);
}
 
/**
* Returns flat querystring
*
* @return string Querystring
* @access public
*/
function getQueryString()
{
if (!empty($this->querystring)) {
foreach ($this->querystring as $name => $value) {
if (is_array($value)) {
foreach ($value as $k => $v) {
$querystring[] = $this->useBrackets ? sprintf('%s[%s]=%s', $name, $k, $v) : ($name . '=' . $v);
}
} elseif (!is_null($value)) {
$querystring[] = $name . '=' . $value;
} else {
$querystring[] = $name;
}
}
$querystring = implode(ini_get('arg_separator.output'), $querystring);
} else {
$querystring = '';
}
 
return $querystring;
}
 
/**
* Parses raw querystring and returns an array of it
*
* @param string $querystring The querystring to parse
* @return array An array of the querystring data
* @access private
*/
function _parseRawQuerystring($querystring)
{
$parts = preg_split('/[' . preg_quote(ini_get('arg_separator.input'), '/') . ']/', $querystring, -1, PREG_SPLIT_NO_EMPTY);
$return = array();
 
foreach ($parts as $part) {
if (strpos($part, '=') !== false) {
$value = substr($part, strpos($part, '=') + 1);
$key = substr($part, 0, strpos($part, '='));
} else {
$value = null;
$key = $part;
}
if (substr($key, -2) == '[]') {
$key = substr($key, 0, -2);
if (@!is_array($return[$key])) {
$return[$key] = array();
$return[$key][] = $value;
} else {
$return[$key][] = $value;
}
} elseif (!$this->useBrackets AND !empty($return[$key])) {
$return[$key] = (array)$return[$key];
$return[$key][] = $value;
} else {
$return[$key] = $value;
}
}
 
return $return;
}
 
/**
* Resolves //, ../ and ./ from a path and returns
* the result. Eg:
*
* /foo/bar/../boo.php => /foo/boo.php
* /foo/bar/../../boo.php => /boo.php
* /foo/bar/.././/boo.php => /foo/boo.php
*
* This method can also be called statically.
*
* @param string $url URL path to resolve
* @return string The result
*/
function resolvePath($path)
{
$path = explode('/', str_replace('//', '/', $path));
 
for ($i=0; $i<count($path); $i++) {
if ($path[$i] == '.') {
unset($path[$i]);
$path = array_values($path);
$i--;
 
} elseif ($path[$i] == '..' AND ($i > 1 OR ($i == 1 AND $path[0] != '') ) ) {
unset($path[$i]);
unset($path[$i-1]);
$path = array_values($path);
$i -= 2;
 
} elseif ($path[$i] == '..' AND $i == 1 AND $path[0] == '') {
unset($path[$i]);
$path = array_values($path);
$i--;
 
} else {
continue;
}
}
 
return implode('/', $path);
}
 
/**
* Returns the standard port number for a protocol
*
* @param string $scheme The protocol to lookup
* @return integer Port number or NULL if no scheme matches
*
* @author Philippe Jausions <Philippe.Jausions@11abacus.com>
*/
function getStandardPort($scheme)
{
switch (strtolower($scheme)) {
case 'http': return 80;
case 'https': return 443;
case 'ftp': return 21;
case 'imap': return 143;
case 'imaps': return 993;
case 'pop3': return 110;
case 'pop3s': return 995;
default: return null;
}
}
 
/**
* Forces the URL to a particular protocol
*
* @param string $protocol Protocol to force the URL to
* @param integer $port Optional port (standard port is used by default)
*/
function setProtocol($protocol, $port = null)
{
$this->protocol = $protocol;
$this->port = is_null($port) ? $this->getStandardPort() : $port;
}
 
}
?>
/trunk/api/pear/Net/FTP/Observer.php
New file
0,0 → 1,101
<?php
 
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
 
/**
* Net_FTP observer.
*
* This class implements the Observer part of a Subject-Observer
* design pattern. It listens to the events sent by a Net_FTP instance.
* This module had many influences from the Log_observer code.
*
* PHP versions 4 and 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @category Networking
* @package FTP
* @author Tobias Schlitt <toby@php.net>
* @author Laurent Laville <pear@laurent-laville.org>
* @author Chuck Hagenbuch <chuck@horde.org>
* @copyright 1997-2005 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: Observer.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
* @link http://pear.php.net/package/Net_FTP
* @since File available since Release 0.0.1
*/
 
/**
* This class implements the Observer part of a Subject-Observer
* design pattern. It listens to the events sent by a Net_FTP instance.
* This module had many influences from the Log_observer code.
*
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @category Networking
* @package FTP
* @author Laurent Laville <pear@laurent-laville.org>
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Tobias Schlitt <toby@php.net>
* @copyright 1997-2005 The PHP Group
* @version Release: 1.3.0
* @link http://pear.php.net/package/Net_FTP
* @since 1.3.0.0
* @access public
*
* @example observer_upload.php An example of Net_FTP_Observer implementation.
*/
class Net_FTP_Observer
{
/**
* Instance-specific unique identification number.
*
* @var integer
* @since 1.3.0
* @access private
*/
var $_id;
 
/**
* Creates a new basic Net_FTP_Observer instance.
*
* @since 1.3.0
* @access public
*/
function Net_FTP_Observer()
{
$this->_id = md5(microtime());
}
 
/**
* Returns the listener's identifier
*
* @return string
* @since 1.3.0
* @access public
*/
function getId()
{
return $this->_id;
}
 
/**
* This is a stub method to make sure that Net_FTP_Observer classes do
* something when they are notified of a message. The default behavior
* is to just do nothing.
* You should override this method.
*
* @param mixed $event A hash describing the net event.
*
* @since 1.3.0
* @access public
*/
function notify($event)
{
return;
}
}
?>
/trunk/api/pear/Net/SMTP.php
New file
0,0 → 1,970
<?php
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Chuck Hagenbuch <chuck@horde.org> |
// | Jon Parise <jon@php.net> |
// | Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar> |
// +----------------------------------------------------------------------+
 
require_once 'PEAR.php';
require_once 'Net/Socket.php';
 
/**
* Provides an implementation of the SMTP protocol using PEAR's
* Net_Socket:: class.
*
* @package Net_SMTP
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Jon Parise <jon@php.net>
* @author Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar>
*
* @example basic.php A basic implementation of the Net_SMTP package.
*/
class Net_SMTP
{
/**
* The server to connect to.
* @var string
* @access public
*/
var $host = 'localhost';
 
/**
* The port to connect to.
* @var int
* @access public
*/
var $port = 25;
 
/**
* The value to give when sending EHLO or HELO.
* @var string
* @access public
*/
var $localhost = 'localhost';
 
/**
* List of supported authentication methods, in preferential order.
* @var array
* @access public
*/
var $auth_methods = array('DIGEST-MD5', 'CRAM-MD5', 'LOGIN', 'PLAIN');
 
/**
* Should debugging output be enabled?
* @var boolean
* @access private
*/
var $_debug = false;
 
/**
* The socket resource being used to connect to the SMTP server.
* @var resource
* @access private
*/
var $_socket = null;
 
/**
* The most recent server response code.
* @var int
* @access private
*/
var $_code = -1;
 
/**
* The most recent server response arguments.
* @var array
* @access private
*/
var $_arguments = array();
 
/**
* Stores detected features of the SMTP server.
* @var array
* @access private
*/
var $_esmtp = array();
 
/**
* Instantiates a new Net_SMTP object, overriding any defaults
* with parameters that are passed in.
*
* @param string The server to connect to.
* @param int The port to connect to.
* @param string The value to give when sending EHLO or HELO.
*
* @access public
* @since 1.0
*/
function Net_SMTP($host = null, $port = null, $localhost = null)
{
if (isset($host)) $this->host = $host;
if (isset($port)) $this->port = $port;
if (isset($localhost)) $this->localhost = $localhost;
 
$this->_socket = new Net_Socket();
 
/*
* Include the Auth_SASL package. If the package is not available,
* we disable the authentication methods that depend upon it.
*/
if ((@include_once 'Auth/SASL.php') === false) {
$pos = array_search('DIGEST-MD5', $this->auth_methods);
unset($this->auth_methods[$pos]);
$pos = array_search('CRAM-MD5', $this->auth_methods);
unset($this->auth_methods[$pos]);
}
}
 
/**
* Set the value of the debugging flag.
*
* @param boolean $debug New value for the debugging flag.
*
* @access public
* @since 1.1.0
*/
function setDebug($debug)
{
$this->_debug = $debug;
}
 
/**
* Send the given string of data to the server.
*
* @param string $data The string of data to send.
*
* @return mixed True on success or a PEAR_Error object on failure.
*
* @access private
* @since 1.1.0
*/
function _send($data)
{
if ($this->_debug) {
echo "DEBUG: Send: $data\n";
}
 
if (PEAR::isError($error = $this->_socket->write($data))) {
return new PEAR_Error('Failed to write to socket: ' .
$error->getMessage());
}
 
return true;
}
 
/**
* Send a command to the server with an optional string of arguments.
* A carriage return / linefeed (CRLF) sequence will be appended to each
* command string before it is sent to the SMTP server.
*
* @param string $command The SMTP command to send to the server.
* @param string $args A string of optional arguments to append
* to the command.
*
* @return mixed The result of the _send() call.
*
* @access private
* @since 1.1.0
*/
function _put($command, $args = '')
{
if (!empty($args)) {
return $this->_send($command . ' ' . $args . "\r\n");
}
 
return $this->_send($command . "\r\n");
}
 
/**
* Read a reply from the SMTP server. The reply consists of a response
* code and a response message.
*
* @param mixed $valid The set of valid response codes. These
* may be specified as an array of integer
* values or as a single integer value.
*
* @return mixed True if the server returned a valid response code or
* a PEAR_Error object is an error condition is reached.
*
* @access private
* @since 1.1.0
*
* @see getResponse
*/
function _parseResponse($valid)
{
$this->_code = -1;
$this->_arguments = array();
 
while ($line = $this->_socket->readLine()) {
if ($this->_debug) {
echo "DEBUG: Recv: $line\n";
}
 
/* If we receive an empty line, the connection has been closed. */
if (empty($line)) {
$this->disconnect();
return new PEAR_Error("Connection was unexpectedly closed");
}
 
/* Read the code and store the rest in the arguments array. */
$code = substr($line, 0, 3);
$this->_arguments[] = trim(substr($line, 4));
 
/* Check the syntax of the response code. */
if (is_numeric($code)) {
$this->_code = (int)$code;
} else {
$this->_code = -1;
break;
}
 
/* If this is not a multiline response, we're done. */
if (substr($line, 3, 1) != '-') {
break;
}
}
 
/* Compare the server's response code with the valid code. */
if (is_int($valid) && ($this->_code === $valid)) {
return true;
}
 
/* If we were given an array of valid response codes, check each one. */
if (is_array($valid)) {
foreach ($valid as $valid_code) {
if ($this->_code === $valid_code) {
return true;
}
}
}
 
return new PEAR_Error("Invalid response code received from server");
}
 
/**
* Return a 2-tuple containing the last response from the SMTP server.
*
* @return array A two-element array: the first element contains the
* response code as an integer and the second element
* contains the response's arguments as a string.
*
* @access public
* @since 1.1.0
*/
function getResponse()
{
return array($this->_code, join("\n", $this->_arguments));
}
 
/**
* Attempt to connect to the SMTP server.
*
* @param int $timeout The timeout value (in seconds) for the
* socket connection.
* @param bool $persistent Should a persistent socket connection
* be used?
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access public
* @since 1.0
*/
function connect($timeout = null, $persistent = false)
{
$result = $this->_socket->connect($this->host, $this->port,
$persistent, $timeout);
if (PEAR::isError($result)) {
return new PEAR_Error('Failed to connect socket: ' .
$result->getMessage());
}
 
if (PEAR::isError($error = $this->_parseResponse(220))) {
return $error;
}
if (PEAR::isError($error = $this->_negotiate())) {
return $error;
}
 
return true;
}
 
/**
* Attempt to disconnect from the SMTP server.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access public
* @since 1.0
*/
function disconnect()
{
if (PEAR::isError($error = $this->_put('QUIT'))) {
return $error;
}
if (PEAR::isError($error = $this->_parseResponse(221))) {
return $error;
}
if (PEAR::isError($error = $this->_socket->disconnect())) {
return new PEAR_Error('Failed to disconnect socket: ' .
$error->getMessage());
}
 
return true;
}
 
/**
* Attempt to send the EHLO command and obtain a list of ESMTP
* extensions available, and failing that just send HELO.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
*
* @access private
* @since 1.1.0
*/
function _negotiate()
{
if (PEAR::isError($error = $this->_put('EHLO', $this->localhost))) {
return $error;
}
 
if (PEAR::isError($this->_parseResponse(250))) {
/* If we receive a 503 response, we're already authenticated. */
if ($this->_code === 503) {
return true;
}
 
/* If the EHLO failed, try the simpler HELO command. */
if (PEAR::isError($error = $this->_put('HELO', $this->localhost))) {
return $error;
}
if (PEAR::isError($this->_parseResponse(250))) {
return new PEAR_Error('HELO was not accepted: ', $this->_code);
}
 
return true;
}
 
foreach ($this->_arguments as $argument) {
$verb = strtok($argument, ' ');
$arguments = substr($argument, strlen($verb) + 1,
strlen($argument) - strlen($verb) - 1);
$this->_esmtp[$verb] = $arguments;
}
 
return true;
}
 
/**
* Returns the name of the best authentication method that the server
* has advertised.
*
* @return mixed Returns a string containing the name of the best
* supported authentication method or a PEAR_Error object
* if a failure condition is encountered.
* @access private
* @since 1.1.0
*/
function _getBestAuthMethod()
{
$available_methods = explode(' ', $this->_esmtp['AUTH']);
 
foreach ($this->auth_methods as $method) {
if (in_array($method, $available_methods)) {
return $method;
}
}
 
return new PEAR_Error('No supported authentication methods');
}
 
/**
* Attempt to do SMTP authentication.
*
* @param string The userid to authenticate as.
* @param string The password to authenticate with.
* @param string The requested authentication method. If none is
* specified, the best supported method will be used.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access public
* @since 1.0
*/
function auth($uid, $pwd , $method = '')
{
if (empty($this->_esmtp['AUTH'])) {
return new PEAR_Error('SMTP server does no support authentication');
}
 
/*
* If no method has been specified, get the name of the best supported
* method advertised by the SMTP server.
*/
if (empty($method)) {
if (PEAR::isError($method = $this->_getBestAuthMethod())) {
/* Return the PEAR_Error object from _getBestAuthMethod(). */
return $method;
}
} else {
$method = strtoupper($method);
if (!in_array($method, $this->auth_methods)) {
return new PEAR_Error("$method is not a supported authentication method");
}
}
 
switch ($method) {
case 'DIGEST-MD5':
$result = $this->_authDigest_MD5($uid, $pwd);
break;
case 'CRAM-MD5':
$result = $this->_authCRAM_MD5($uid, $pwd);
break;
case 'LOGIN':
$result = $this->_authLogin($uid, $pwd);
break;
case 'PLAIN':
$result = $this->_authPlain($uid, $pwd);
break;
default:
$result = new PEAR_Error("$method is not a supported authentication method");
break;
}
 
/* If an error was encountered, return the PEAR_Error object. */
if (PEAR::isError($result)) {
return $result;
}
 
/* RFC-2554 requires us to re-negotiate ESMTP after an AUTH. */
if (PEAR::isError($error = $this->_negotiate())) {
return $error;
}
 
return true;
}
 
/**
* Authenticates the user using the DIGEST-MD5 method.
*
* @param string The userid to authenticate as.
* @param string The password to authenticate with.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access private
* @since 1.1.0
*/
function _authDigest_MD5($uid, $pwd)
{
if (PEAR::isError($error = $this->_put('AUTH', 'DIGEST-MD5'))) {
return $error;
}
/* 334: Continue authentication request */
if (PEAR::isError($error = $this->_parseResponse(334))) {
/* 503: Error: already authenticated */
if ($this->_code === 503) {
return true;
}
return $error;
}
 
$challenge = base64_decode($this->_arguments[0]);
$digest = &Auth_SASL::factory('digestmd5');
$auth_str = base64_encode($digest->getResponse($uid, $pwd, $challenge,
$this->host, "smtp"));
 
if (PEAR::isError($error = $this->_put($auth_str))) {
return $error;
}
/* 334: Continue authentication request */
if (PEAR::isError($error = $this->_parseResponse(334))) {
return $error;
}
 
/*
* We don't use the protocol's third step because SMTP doesn't allow
* subsequent authentication, so we just silently ignore it.
*/
if (PEAR::isError($error = $this->_put(' '))) {
return $error;
}
/* 235: Authentication successful */
if (PEAR::isError($error = $this->_parseResponse(235))) {
return $error;
}
}
 
/**
* Authenticates the user using the CRAM-MD5 method.
*
* @param string The userid to authenticate as.
* @param string The password to authenticate with.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access private
* @since 1.1.0
*/
function _authCRAM_MD5($uid, $pwd)
{
if (PEAR::isError($error = $this->_put('AUTH', 'CRAM-MD5'))) {
return $error;
}
/* 334: Continue authentication request */
if (PEAR::isError($error = $this->_parseResponse(334))) {
/* 503: Error: already authenticated */
if ($this->_code === 503) {
return true;
}
return $error;
}
 
$challenge = base64_decode($this->_arguments[0]);
$cram = &Auth_SASL::factory('crammd5');
$auth_str = base64_encode($cram->getResponse($uid, $pwd, $challenge));
 
if (PEAR::isError($error = $this->_put($auth_str))) {
return $error;
}
 
/* 235: Authentication successful */
if (PEAR::isError($error = $this->_parseResponse(235))) {
return $error;
}
}
 
/**
* Authenticates the user using the LOGIN method.
*
* @param string The userid to authenticate as.
* @param string The password to authenticate with.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access private
* @since 1.1.0
*/
function _authLogin($uid, $pwd)
{
if (PEAR::isError($error = $this->_put('AUTH', 'LOGIN'))) {
return $error;
}
/* 334: Continue authentication request */
if (PEAR::isError($error = $this->_parseResponse(334))) {
/* 503: Error: already authenticated */
if ($this->_code === 503) {
return true;
}
return $error;
}
 
if (PEAR::isError($error = $this->_put(base64_encode($uid)))) {
return $error;
}
/* 334: Continue authentication request */
if (PEAR::isError($error = $this->_parseResponse(334))) {
return $error;
}
 
if (PEAR::isError($error = $this->_put(base64_encode($pwd)))) {
return $error;
}
 
/* 235: Authentication successful */
if (PEAR::isError($error = $this->_parseResponse(235))) {
return $error;
}
 
return true;
}
 
/**
* Authenticates the user using the PLAIN method.
*
* @param string The userid to authenticate as.
* @param string The password to authenticate with.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access private
* @since 1.1.0
*/
function _authPlain($uid, $pwd)
{
if (PEAR::isError($error = $this->_put('AUTH', 'PLAIN'))) {
return $error;
}
/* 334: Continue authentication request */
if (PEAR::isError($error = $this->_parseResponse(334))) {
/* 503: Error: already authenticated */
if ($this->_code === 503) {
return true;
}
return $error;
}
 
$auth_str = base64_encode(chr(0) . $uid . chr(0) . $pwd);
 
if (PEAR::isError($error = $this->_put($auth_str))) {
return $error;
}
 
/* 235: Authentication successful */
if (PEAR::isError($error = $this->_parseResponse(235))) {
return $error;
}
 
return true;
}
 
/**
* Send the HELO command.
*
* @param string The domain name to say we are.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access public
* @since 1.0
*/
function helo($domain)
{
if (PEAR::isError($error = $this->_put('HELO', $domain))) {
return $error;
}
if (PEAR::isError($error = $this->_parseResponse(250))) {
return $error;
}
 
return true;
}
 
/**
* Send the MAIL FROM: command.
*
* @param string The sender (reverse path) to set.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access public
* @since 1.0
*/
function mailFrom($sender)
{
if (PEAR::isError($error = $this->_put('MAIL', "FROM:<$sender>"))) {
return $error;
}
if (PEAR::isError($error = $this->_parseResponse(250))) {
return $error;
}
 
return true;
}
 
/**
* Send the RCPT TO: command.
*
* @param string The recipient (forward path) to add.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access public
* @since 1.0
*/
function rcptTo($recipient)
{
if (PEAR::isError($error = $this->_put('RCPT', "TO:<$recipient>"))) {
return $error;
}
if (PEAR::isError($error = $this->_parseResponse(array(250, 251)))) {
return $error;
}
 
return true;
}
 
/**
* Quote the data so that it meets SMTP standards.
*
* This is provided as a separate public function to facilitate easier
* overloading for the cases where it is desirable to customize the
* quoting behavior.
*
* @param string The message text to quote. The string must be passed
* by reference, and the text will be modified in place.
*
* @access public
* @since 1.2
*/
function quotedata(&$data)
{
/*
* Change Unix (\n) and Mac (\r) linefeeds into Internet-standard CRLF
* (\r\n) linefeeds.
*/
$data = preg_replace("/([^\r]{1})\n/", "\\1\r\n", $data);
$data = preg_replace("/\n\n/", "\n\r\n", $data);
 
/*
* Because a single leading period (.) signifies an end to the data,
* legitimate leading periods need to be "doubled" (e.g. '..').
*/
$data = preg_replace("/\n\./", "\n..", $data);
}
 
/**
* Send the DATA command.
*
* @param string The message body to send.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access public
* @since 1.0
*/
function data($data)
{
/*
* RFC 1870, section 3, subsection 3 states "a value of zero indicates
* that no fixed maximum message size is in force". Furthermore, it
* says that if "the parameter is omitted no information is conveyed
* about the server's fixed maximum message size".
*/
if (isset($this->_esmtp['SIZE']) && ($this->_esmtp['SIZE'] > 0)) {
if (strlen($data) >= $this->_esmtp['SIZE']) {
$this->disconnect();
return new PEAR_Error('Message size excedes the server limit');
}
}
 
/* Quote the data based on the SMTP standards. */
$this->quotedata($data);
 
if (PEAR::isError($error = $this->_put('DATA'))) {
return $error;
}
if (PEAR::isError($error = $this->_parseResponse(354))) {
return $error;
}
 
if (PEAR::isError($this->_send($data . "\r\n.\r\n"))) {
return new PEAR_Error('write to socket failed');
}
if (PEAR::isError($error = $this->_parseResponse(250))) {
return $error;
}
 
return true;
}
 
/**
* Send the SEND FROM: command.
*
* @param string The reverse path to send.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access public
* @since 1.2.6
*/
function sendFrom($path)
{
if (PEAR::isError($error = $this->_put('SEND', "FROM:<$path>"))) {
return $error;
}
if (PEAR::isError($error = $this->_parseResponse(250))) {
return $error;
}
 
return true;
}
 
/**
* Backwards-compatibility wrapper for sendFrom().
*
* @param string The reverse path to send.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
*
* @access public
* @since 1.0
* @deprecated 1.2.6
*/
function send_from($path)
{
return sendFrom($path);
}
 
/**
* Send the SOML FROM: command.
*
* @param string The reverse path to send.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access public
* @since 1.2.6
*/
function somlFrom($path)
{
if (PEAR::isError($error = $this->_put('SOML', "FROM:<$path>"))) {
return $error;
}
if (PEAR::isError($error = $this->_parseResponse(250))) {
return $error;
}
 
return true;
}
 
/**
* Backwards-compatibility wrapper for somlFrom().
*
* @param string The reverse path to send.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
*
* @access public
* @since 1.0
* @deprecated 1.2.6
*/
function soml_from($path)
{
return somlFrom($path);
}
 
/**
* Send the SAML FROM: command.
*
* @param string The reverse path to send.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access public
* @since 1.2.6
*/
function samlFrom($path)
{
if (PEAR::isError($error = $this->_put('SAML', "FROM:<$path>"))) {
return $error;
}
if (PEAR::isError($error = $this->_parseResponse(250))) {
return $error;
}
 
return true;
}
 
/**
* Backwards-compatibility wrapper for samlFrom().
*
* @param string The reverse path to send.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
*
* @access public
* @since 1.0
* @deprecated 1.2.6
*/
function saml_from($path)
{
return samlFrom($path);
}
 
/**
* Send the RSET command.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access public
* @since 1.0
*/
function rset()
{
if (PEAR::isError($error = $this->_put('RSET'))) {
return $error;
}
if (PEAR::isError($error = $this->_parseResponse(250))) {
return $error;
}
 
return true;
}
 
/**
* Send the VRFY command.
*
* @param string The string to verify
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access public
* @since 1.0
*/
function vrfy($string)
{
/* Note: 251 is also a valid response code */
if (PEAR::isError($error = $this->_put('VRFY', $string))) {
return $error;
}
if (PEAR::isError($error = $this->_parseResponse(250))) {
return $error;
}
 
return true;
}
 
/**
* Send the NOOP command.
*
* @return mixed Returns a PEAR_Error with an error message on any
* kind of failure, or true on success.
* @access public
* @since 1.0
*/
function noop()
{
if (PEAR::isError($error = $this->_put('NOOP'))) {
return $error;
}
if (PEAR::isError($error = $this->_parseResponse(250))) {
return $error;
}
 
return true;
}
 
/**
* Backwards-compatibility method. identifySender()'s functionality is
* now handled internally.
*
* @return boolean This method always return true.
*
* @access public
* @since 1.0
*/
function identifySender()
{
return true;
}
}
 
?>