php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #21053 ftp_exec() return value modification
Submitted: 2002-12-16 15:24 UTC Modified: 2003-04-27 15:08 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: pdmckone at engmail dot uwaterloo dot ca Assigned:
Status: Closed Package: FTP related
PHP Version: 4.2.3 OS: FreeBSD 4.6.2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: pdmckone at engmail dot uwaterloo dot ca
New email:
PHP Version: OS:

 

 [2002-12-16 15:24 UTC] pdmckone at engmail dot uwaterloo dot ca
The ftp_exec routine in PHP 4.2.3 doesn't work as advertised in the manual, returning true or false status rather than command output.

I've written a replacement version of the routine, which has worked well for me under FreeBSD 4.6.2.  My changes were:

in ftp.h:

/* exec a command [special features], return response on success, false on error */
char**     ftp_exec(ftpbuf_t *ftp, const char *cmd);

(change return type from int to char**)

in php_ftp.c:

/* {{{ proto array ftp_exec(resource stream, string cmd)
   Returns the results of a system command as an array of output lines */
PHP_FUNCTION(ftp_exec)
{
  pval            *z_ftp;
  ftpbuf_t        *ftp;
  char            **llist, **ptr, *cmd;
  int             cmd_len;

  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_ftp, &cmd, &cmd_len) == FAILURE) {
    return;
  }

  ZEND_FETCH_RESOURCE(ftp, ftpbuf_t*, &z_ftp, -1, le_ftpbuf_name, le_ftpbuf);

  /* get raw command output */
  if (NULL == (llist = ftp_exec(ftp, cmd))) {
    RETURN_FALSE;
  }

  array_init(return_value);
  for (ptr = llist; *ptr; ptr++)
    add_next_index_string(return_value, *ptr, 1);
  free(llist);
}
/* }}} */

Finally, in ftp.c:

/* {{{ ftp_exec
 */
char**
ftp_exec(ftpbuf_t *ftp, const char *cmd)
{
  char            **ret = NULL;
  char            **entry;
  char            *text;

  if (!ftp_type(ftp, FTPTYPE_ASCII))
    return NULL;

  if (!ftp_putcmd(ftp, "SITE EXEC", cmd))
    return NULL;

  ret = malloc(FTP_BUFSIZE * sizeof(char*));
  if (ret == NULL) {
    perror("malloc");
    return NULL;
  }

  entry = ret;
  text = (char*) (ret + 100);
  *entry = text;

  while(1) {
    if (!ftp_readline(ftp)) {
      free(ret);
      return NULL;
    }

    strcpy(text, ftp->inbuf);
    text += strlen(ftp->inbuf) + 1;
    *++entry = text;

    /* Break out when the end-tag is found */
    if (isdigit(ftp->inbuf[0]) &&
        isdigit(ftp->inbuf[1]) &&
        isdigit(ftp->inbuf[2]) &&
                ftp->inbuf[3] == ' ') {
      break;
    }

  }

  *entry = NULL;

  return ret;
}
/* }}} */

I had to make, make install, etc, then restart the httpd to pick up the new version.

The code works for me, returning an array of the raw output of a command, one line per array entry.   We needed the output from "site exec quota -v", which from our mailserver comes prefixed with intermediate status levels:

200-quota -v
200-Disk quotas for jqpublic (uid 12345):
200-Filesystem     usage  quota  limit    timeleft  ...
...
200 (end of 'quota -v')

It requires some extra parsing at the application end, but I figured it's safer to have to parse, than to have to figure out all the possible styles of output ahead of time.

PDM

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-16 15:37 UTC] goba@php.net
What you sent in your report is not a documentation problem, but an FTP related feature request. Because you includeded C source code too, I recategorize this as FTP related. Also modified summary
 [2003-01-30 22:58 UTC] pollita@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.

Rather than modify the behavior of ftp_exec(), I've added a new function ftp_raw() which has the added feature of being able to send arbitrary commands rather than just "SITE EXEC" commands.  This new function will return NULL on failure or an array of response strings on success.  I'll also update the ftp_exec() documentation to reflect the boolean return value.
 [2003-04-27 12:44 UTC] darren at dazza dot org
I'm very interested in the ftp_raw() function, but I can't seem to find it in either the 4.3.x-dev or 4.5.x-dev CVS snapshots, only in the 5.0.x-dev one.

Any plans to back-port ftp_raw()?

-Darren
 [2003-04-27 14:23 UTC] pollita@php.net
4.5-dev is a quasi-unofficial branch so I'm going to quasi-ignore it in this comment (don't ask).

Once a branch has gone "stable" as 4.3 has no new functionality is to be added to it.  Only bug fixes are introduced.  (This keeps the stable stuff stable)

The Development (or HEAD) branch, is where all the new and exciting toys go.  If you'd like to get PHP5 level ftp extension functionality your best bet is, obviously, to upgrade to PHP5.  Since that's development stage however, I probably wouldn't recommend it for a production server (nevermind that PHP5 has a few memleaks still) :)

The middle ground will take a minor amount of work.  I havn't tested it, but I'm 99% certain you can grab the ext/ftp directory from PHP5's tarball on snaps.php.net and drop it over the top of PHP4.3's version.  Then ./configure --enable-ftp && make

Good luck!
 [2003-04-27 15:08 UTC] derick@php.net
Don't forget to run "rm configure && ./buildconf" before your call to ./configure though.

Derick
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 00:01:30 2024 UTC