[ PHPXref.com ] [ Generated: Sun Jul 20 19:31:32 2008 ] [ phpAdsNew 2.0.8 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/admin/ -> lib-ftp.inc.php (source)

   1  <?php // $Revision: 2.0.2.1 $
   2  
   3  /************************************************************************/
   4  /* phpAdsNew 2                                                          */
   5  /* ===========                                                          */
   6  /*                                                                      */
   7  /* Copyright (c) 2001 by TOMO <groove@spencernetwork.org>               */
   8  /* For more information visit: http://www.phpadsnew.com                 */
   9  /************************************************************************/
  10  
  11  
  12  
  13  // Avoid redeclaring function if the extension is loaded
  14  if (extension_loaded('ftp'))
  15      return true;
  16  
  17  
  18  
  19  $ftp_debug = FALSE;
  20  $ftp_umask = 0022;
  21  $ftp_timeout = 30;
  22  
  23  
  24  if (!defined("FTP_BINARY")) {
  25      define("FTP_BINARY", 1);
  26  }
  27  if (!defined("FTP_ASCII")) {
  28      define("FTP_ASCII", 0);
  29  }
  30  
  31  
  32  function ftp_connect($server, $port = 21)
  33  {
  34      global $ftp_timeout;
  35      
  36      ftp_debug("Trying to ".$server.":".$port." ...\n");
  37      $sock = @fsockopen($server, $port, $errno, $errstr, $ftp_timeout);
  38      if ($sock && ftp_ok($sock)) {
  39          ftp_debug("Connected to remote host \"".$server.":".$port."\"\n");
  40          return $sock;
  41      } else {
  42          ftp_debug("Cannot connect to remote host \"".$server.":".$port."\"\n");
  43          ftp_debug("Error : ".$errstr." (".$errno.")\n");
  44          return FALSE;
  45      }
  46  }
  47  
  48  function ftp_login($sock, $user, $pass)
  49  {
  50      ftp_putcmd($sock, "USER", $user);
  51      if (!ftp_ok($sock)) {
  52          return FALSE;
  53      }
  54      ftp_putcmd($sock, "PASS", $pass);
  55      if (ftp_ok($sock)) {
  56          ftp_debug("Authentication succeeded\n");
  57          return TRUE;
  58      } else {
  59          ftp_debug("Error : Authentication failed\n");
  60          return FALSE;
  61      }
  62  }
  63  
  64  function ftp_pwd($sock)
  65  {
  66      ftp_putcmd($sock, "PWD");
  67      $response = ftp_getresp($sock);
  68      if (!$response) {
  69          return FALSE;
  70      }
  71      if (ereg("^[123]", $response)) {
  72          return ereg_replace("^[0-9]{3} \"(.+)\" .+\r\n", "\\1", $response);
  73      } else {
  74          return FALSE;
  75      }
  76  }
  77  
  78  function ftp_size($sock, $pathname)
  79  {
  80      ftp_putcmd($sock, "SIZE", $pathname);
  81      $response = ftp_getresp($sock);
  82      if (!$response) {
  83          return FALSE;
  84      }
  85      if (ereg("^[123]", $response)) {
  86          return ereg_replace("^[0-9]{3} ([0-9]+)\r\n", "\\1", $response);
  87      } else {
  88          return -1;
  89      }
  90  }
  91  
  92  function ftp_mdtm($sock, $pathname)
  93  {
  94      ftp_putcmd($sock, "MDTM", $pathname);
  95      $response = ftp_getresp($sock);
  96      if (!$response) {
  97          return FALSE;
  98      }
  99      if (ereg("^[123]", $response)) {
 100          $mdtm = ereg_replace("^[0-9]{3} ([0-9]+)\r\n", "\\1", $response);
 101          list ($year, $mon, $day, $hour, $min, $sec) = sscanf($mdtm, "%4d%2d%2d%2d%2d%2d");
 102          $timestamp = mktime($hour, $min, $sec, $mon, $day, $year);
 103          return $timestamp;
 104      } else {
 105          return -1;
 106      }
 107  }
 108  
 109  function ftp_systype($sock)
 110  {
 111      ftp_putcmd($sock, "SYST");
 112      $data = ftp_getresp($sock);
 113      if ($data) {
 114          $DATA = explode(" ", $data);
 115          return $DATA[1];
 116      } else {
 117          return FALSE;
 118      }
 119  }
 120  
 121  function ftp_cdup($sock)
 122  {
 123      ftp_putcmd($sock, "CDUP");
 124      return ftp_ok($sock);
 125  }
 126  
 127  function ftp_chdir($sock, $dir)
 128  {
 129      ftp_putcmd($sock, "CWD", $dir);
 130      return ftp_ok($sock);
 131  }
 132  
 133  function ftp_delete($sock, $pathname)
 134  {
 135      ftp_putcmd($sock, "DELE", $pathname);
 136      return ftp_ok($sock);
 137  }
 138  
 139  function ftp_rmdir($sock, $pathname)
 140  {
 141      ftp_putcmd($sock, "RMD", $pathname);
 142      return ftp_ok($sock);
 143  }
 144  
 145  function ftp_mkdir($sock, $pathname)
 146  {
 147      ftp_putcmd($sock, "MKD", $pathname);
 148      return ftp_ok($sock);
 149  }
 150  
 151  function ftp_rename($sock, $from, $to)
 152  {
 153      if (!ftp_file_exists($sock, $from)) {
 154          ftp_debug("Error : No such file or directory \"".$from."\"\n");
 155          return FALSE;
 156      }
 157      
 158      ftp_putcmd($sock, "RNFR", $from);
 159      
 160      if (!ftp_ok($sock)) {
 161          return FALSE;
 162      }
 163      
 164      ftp_putcmd($sock, "RNTO", $to);
 165      
 166      return ftp_ok($sock);
 167  }
 168  
 169  function ftp_nlist($sock, $arg = "", $pathname = "")
 170  {
 171      ftp_putcmd($sock, "PASV");
 172      $string = ftp_getresp($sock);
 173      
 174      if ($arg == "") {
 175          $nlst = "NLST";
 176      } else {
 177          $nlst = "NLST ".$arg;
 178      }
 179      ftp_putcmd($sock, $nlst, $pathname);
 180      
 181      $sock_data = ftp_open_data_connection($string);
 182      if (!$sock_data) {
 183          return FALSE;
 184      }
 185      if (ftp_ok($sock)) {
 186          ftp_debug("Connected to remote host\n");
 187      } else {
 188          ftp_debug("Cannot connect to remote host\n");
 189          return FALSE;
 190      }
 191      
 192      while (!feof($sock_data)) {
 193          $list[] = ereg_replace("[\r\n]", "", fgets($sock_data, 512));
 194      }
 195      ftp_close_data_connection($sock_data);
 196      ftp_debug(implode("\n", $list));
 197      
 198      if (ftp_ok($sock)) {
 199          return $list;
 200      } else {
 201          return FALSE;
 202      }
 203  }
 204  
 205  function ftp_rawlist($sock, $pathname = "")
 206  {
 207      ftp_putcmd($sock, "PASV");
 208      $response = ftp_getresp($sock);
 209      
 210      ftp_putcmd($sock, "LIST", $pathname);
 211      
 212      $sock_data = ftp_open_data_connection($response);
 213      if (!$sock_data) {
 214          return FALSE;
 215      }
 216      if (ftp_ok($sock)) {
 217          ftp_debug("Connected to remote host\n");
 218      } else {
 219          ftp_debug("Cannot connect to remote host\n");
 220          return FALSE;
 221      }
 222      
 223      while (!feof($sock_data)) {
 224          $list[] = ereg_replace("[\r\n]", "", fgets($sock_data, 512));
 225      }
 226      ftp_debug(implode("\n", $list));
 227      ftp_close_data_connection($sock_data);
 228      
 229      if (ftp_ok($sock)) {
 230          return $list;
 231      } else {
 232          return FALSE;
 233      }
 234  }
 235  
 236  function ftp_get($sock, $localfile, $remotefile, $mode = 1)
 237  {
 238      global $ftp_umask;
 239      
 240      if ($mode) {
 241          $type = "I";
 242      } else {
 243          $type = "A";
 244      }
 245      
 246      if (!ftp_file_exists($sock, $remotefile)) {
 247          ftp_debug("Error : No such file or directory \"".$remotefile."\"\n");
 248          ftp_debug("Error : GET failed\n");
 249          return FALSE;
 250      }
 251      
 252      if (@file_exists($localfile)) {
 253          ftp_debug("Warning : local file will be overwritten\n");
 254      } else {
 255          umask($ftp_umask);
 256      }
 257      
 258      $fp = @fopen($localfile, "w");
 259      if (!$fp) {
 260          ftp_debug("Error : Cannot create \"".$localfile."\"");
 261          ftp_debug("Error : GET failed\n");
 262          return FALSE;
 263      }
 264      
 265      ftp_putcmd($sock, "PASV");
 266      $string = ftp_getresp($sock);
 267      
 268      ftp_putcmd($sock, "TYPE", $type);
 269      ftp_getresp($sock);
 270      
 271      ftp_putcmd($sock, "RETR", $remotefile);
 272      
 273      $sock_data = ftp_open_data_connection($string);
 274      if (!$sock_data) {
 275          return FALSE;
 276      }
 277      if (ftp_ok($sock)) {
 278          ftp_debug("Connected to remote host\n");
 279      } else {
 280          ftp_debug("Cannot connect to remote host\n");
 281          ftp_debug("Error : GET failed\n");
 282          return FALSE;
 283      }
 284      
 285      ftp_debug("Retrieving remote file \"".$remotefile."\" to local file \"".$localfile."\"\n");
 286      while (!feof($sock_data)) {
 287          fputs($fp, fread($sock_data, 4096));
 288      }
 289      fclose($fp);
 290      
 291      ftp_close_data_connection($sock_data);
 292      
 293      return ftp_ok($sock);
 294  }
 295  
 296  function ftp_fget($sock, $fp, $remotefile, $mode = 1)
 297  {
 298      global $ftp_umask;
 299      
 300      if ($mode) {
 301          $type = "I";
 302      } else {
 303          $type = "A";
 304      }
 305      
 306      if (!ftp_file_exists($sock, $remotefile)) {
 307          ftp_debug("Error : No such file or directory \"".$remotefile."\"\n");
 308          ftp_debug("Error : GET failed\n");
 309          return FALSE;
 310      }
 311      
 312      ftp_putcmd($sock, "PASV");
 313      $string = ftp_getresp($sock);
 314      
 315      ftp_putcmd($sock, "TYPE", $type);
 316      ftp_getresp($sock);
 317      
 318      ftp_putcmd($sock, "RETR", $remotefile);
 319      
 320      $sock_data = ftp_open_data_connection($string);
 321      if (!$sock_data) {
 322          return FALSE;
 323      }
 324      if (ftp_ok($sock)) {
 325          ftp_debug("Connected to remote host\n");
 326      } else {
 327          ftp_debug("Cannot connect to remote host\n");
 328          ftp_debug("Error : GET failed\n");
 329          return FALSE;
 330      }
 331      
 332      ftp_debug("Retrieving remote file \"".$remotefile."\" to local file \"".$localfile."\"\n");
 333      while (!feof($sock_data)) {
 334          fputs($fp, fread($sock_data, 4096));
 335      }
 336      
 337      ftp_close_data_connection($sock_data);
 338      
 339      return ftp_ok($sock);
 340  }
 341  
 342  function ftp_put($sock, $remotefile, $localfile, $mode = 1)
 343  {
 344      if ($mode) {
 345          $type = "I";
 346      } else {
 347          $type = "A";
 348      }
 349      
 350      if (!file_exists($localfile)) {
 351          ftp_debug("Error : No such file or directory \"".$localfile."\"\n");
 352          ftp_debug("Error : PUT failed\n");
 353          return FALSE;
 354      }
 355      
 356      $fp = @fopen($localfile, "r");
 357      if (!$fp) {
 358          ftp_debug("Cannot read file \"".$localfile."\"\n");
 359          ftp_debug("Error : PUT failed\n");
 360          return FALSE;
 361      }
 362      
 363      ftp_putcmd($sock, "PASV");
 364      $string = ftp_getresp($sock);
 365      
 366      ftp_putcmd($sock, "TYPE", $type);
 367      ftp_getresp($sock);
 368      
 369      if (ftp_file_exists($sock, $remotefile)) {
 370          ftp_debug("Warning : Remote file will be overwritten\n");
 371      }
 372      
 373      ftp_putcmd($sock, "STOR", $remotefile);
 374      
 375      $sock_data = ftp_open_data_connection($string);
 376      if (!$sock_data) {
 377          return FALSE;
 378      }
 379      if (ftp_ok($sock)) {
 380          ftp_debug("Connected to remote host\n");
 381      } else {
 382          ftp_debug("Cannot connect to remote host\n");
 383          ftp_debug("Error : PUT failed\n");
 384          return FALSE;
 385      }
 386      
 387      ftp_debug("Storing local file \"".$localfile."\" to remote file \"".$remotefile."\"\n");
 388      while (!feof($fp)) {
 389          fputs($sock_data, fread($fp, 4096));
 390      }
 391      fclose($fp);
 392      
 393      ftp_close_data_connection($sock_data);
 394      
 395      return ftp_ok($sock);
 396  }
 397  
 398  function ftp_fput($sock, $remotefile, $fp, $mode = 1)
 399  {
 400      if ($mode) {
 401          $type = "I";
 402      } else {
 403          $type = "A";
 404      }
 405      
 406      ftp_putcmd($sock, "PASV");
 407      $string = ftp_getresp($sock);
 408      
 409      ftp_putcmd($sock, "TYPE", $type);
 410      ftp_getresp($sock);
 411      
 412      if (ftp_file_exists($sock, $remotefile)) {
 413          ftp_debug("Warning : Remote file will be overwritten\n");
 414      }
 415      
 416      ftp_putcmd($sock, "STOR", $remotefile);
 417      
 418      $sock_data = ftp_open_data_connection($string);
 419      if (!$sock_data) {
 420          return FALSE;
 421      }
 422      if (ftp_ok($sock)) {
 423          ftp_debug("Connected to remote host\n");
 424      } else {
 425          ftp_debug("Cannot connect to remote host\n");
 426          ftp_debug("Error : PUT failed\n");
 427          return FALSE;
 428      }
 429      
 430      ftp_debug("Storing local file \"".$localfile."\" to remote file \"".$remotefile."\"\n");
 431      while (!feof($fp)) {
 432          fputs($sock_data, fread($fp, 4096));
 433      }
 434      
 435      ftp_close_data_connection($sock_data);
 436      
 437      return ftp_ok($sock);
 438  }
 439  
 440  function ftp_site($sock, $command)
 441  {
 442      ftp_putcmd($sock, "SITE", $command);
 443      return ftp_ok($sock);
 444  }
 445  
 446  function ftp_quit($sock)
 447  {
 448      ftp_putcmd($sock, "QUIT");
 449      if (ftp_ok($sock) && fclose($sock)) {
 450          ftp_debug("Disconnected from remote host\n");
 451          return TRUE;
 452      } else {
 453          return FALSE;
 454      }
 455  }
 456  
 457  /* Private Functions */
 458  
 459  function ftp_putcmd($sock, $cmd, $arg = "")
 460  {
 461      if (!$sock) {
 462          return FALSE;
 463      }
 464      
 465      if ($arg != "") {
 466          $cmd = $cmd." ".$arg;
 467      }
 468      
 469      fputs($sock, $cmd."\r\n");
 470      ftp_debug("> ".$cmd."\n");
 471      
 472      return TRUE;
 473  }
 474  
 475  function ftp_getresp($sock)
 476  {
 477      if (!$sock) {
 478          return FALSE;
 479      }
 480      
 481      $response = "";
 482      do {
 483          $res = fgets($sock, 512);
 484          $response .= $res;
 485      } while (substr($res, 3, 1) != " ");
 486      
 487      ftp_debug(str_replace("\r\n", "\n", $response));
 488      
 489      return $response;
 490  }
 491  
 492  function ftp_ok($sock)
 493  {
 494      if (!$sock) {
 495          return FALSE;
 496      }
 497      
 498      $response = ftp_getresp($sock);
 499      if (ereg("^[123]", $response)) {
 500          return TRUE;
 501      } else {
 502          return FALSE;
 503      }
 504  }
 505  
 506  function ftp_file_exists($sock, $pathname)
 507  {
 508      if (!$sock) {
 509          return FALSE;
 510      }
 511      
 512      ftp_putcmd($sock, "MDTM", $pathname);
 513      if (ftp_ok($sock)) {
 514          ftp_debug("Remote file ".$pathname." exists\n");
 515          return TRUE;
 516      } else {
 517          ftp_debug("Remote file ".$pathname." does not exist\n");
 518          return FALSE;
 519      }
 520  }
 521  
 522  function ftp_close_data_connection($sock)
 523  {
 524      ftp_debug("Disconnected from remote host\n");
 525      return fclose($sock);
 526  }
 527  
 528  function ftp_open_data_connection($string)
 529  {
 530      $data = ereg_replace("^.*\\(([0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+)\\).*$", "\\1", $string);
 531      $DATA = explode(",", $data);
 532      $ipaddr = $DATA[0].".".$DATA[1].".".$DATA[2].".".$DATA[3];
 533      $port = $DATA[4]*256 + $DATA[5];
 534      $data_connection = @fsockopen($ipaddr, $port);
 535      if ($data_connection) {
 536          ftp_debug("Trying to ".$ipaddr.":".$port." ...\n");
 537          return $data_connection;
 538      } else {
 539          ftp_debug("Error : Cannot open data connection to ".$this->server.":".$port."\n");
 540          ftp_debug("Error : ".$errstr." (".$errno.")\n");
 541          return FALSE;
 542      }
 543  }
 544  
 545  function ftp_debug($message = "")
 546  {
 547      global $ftp_debug;
 548      
 549      if ($ftp_debug) {
 550          echo $message;
 551      }
 552      
 553      return TRUE;
 554  }
 555  
 556  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]