Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
320 jpm 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
 
4
/**
5
 * Net_FTP main file.
6
 *
7
 * This file must be included to use the Net_FTP package.
8
 *
9
 * PHP versions 4 and 5
10
 *
11
 * LICENSE: This source file is subject to version 3.0 of the PHP license
12
 * that is available through the world-wide-web at the following URI:
13
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
14
 * the PHP License and are unable to obtain it through the web, please
15
 * send a note to license@php.net so we can mail you a copy immediately.
16
 *
17
 * @category   Networking
18
 * @package    FTP
19
 * @author     Tobias Schlitt <toby@php.net>
20
 * @copyright  1997-2005 The PHP Group
21
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
22
 * @version    CVS: $Id: FTP.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
23
 * @link       http://pear.php.net/package/Net_FTP
24
 * @since      File available since Release 0.0.1
25
 */
26
 
27
require_once 'PEAR.php';
28
 
29
/**
30
 * Option to let the ls() method return only files.
31
 *
32
 * @since 1.3
33
 * @name NET_FTP_FILES_ONLY
34
 * @see Net_FTP::ls()
35
 */
36
define('NET_FTP_FILES_ONLY', 0, true);
37
 
38
/**
39
 * Option to let the ls() method return only directories.
40
 *
41
 * @since 1.3
42
 * @name NET_FTP_DIRS_ONLY
43
 * @see Net_FTP::ls()
44
 */
45
define('NET_FTP_DIRS_ONLY', 1, true);
46
 
47
/**
48
 * Option to let the ls() method return directories and files (default).
49
 *
50
 * @since 1.3
51
 * @name NET_FTP_DIRS_FILES
52
 * @see Net_FTP::ls()
53
 */
54
define('NET_FTP_DIRS_FILES', 2, true);
55
 
56
/**
57
 * Option to let the ls() method return the raw directory listing from ftp_rawlist().
58
 *
59
 * @since 1.3
60
 * @name NET_FTP_RAWLIST
61
 * @see Net_FTP::ls()
62
 */
63
define('NET_FTP_RAWLIST', 3, true);
64
 
65
 
66
/**
67
 * Error code to indicate a failed connection
68
 * This error code indicates, that the connection you tryed to set up
69
 * could not be established. Check your connection settings (host & port)!
70
 *
71
 * @since 1.3
72
 * @name NET_FTP_ERR_CONNECT_FAILED
73
 * @see Net_FTP::connect()
74
 */
75
define('NET_FTP_ERR_CONNECT_FAILED', -1);
76
 
77
/**
78
 * Error code to indicate a failed login
79
 * This error code indicates, that the login to the FTP server failed. Check
80
 * your user data (username & password).
81
 *
82
 * @since 1.3
83
 * @name NET_FTP_ERR_LOGIN_FAILED
84
 * @see Net_FTP::login()
85
 */
86
define('NET_FTP_ERR_LOGIN_FAILED', -2);
87
 
88
/**
89
 * Error code to indicate a failed directory change
90
 * The cd() method failed. Ensure that the directory you wanted to access exists.
91
 *
92
 * @since 1.3
93
 * @name NET_FTP_ERR_DIRCHANGE_FAILED
94
 * @see Net_FTP::cd()
95
 */
96
define('NET_FTP_ERR_DIRCHANGE_FAILED', 2); // Compatibillity reasons!
97
 
98
/**
99
 * Error code to indicate that Net_FTP could not determine the current path
100
 * The cwd() method failed and could not determine the path you currently reside
101
 * in on the FTP server.
102
 *
103
 * @since 1.3
104
 * @name NET_FTP_ERR_DETERMINEPATH_FAILED
105
 * @see Net_FTP::pwd()
106
 */
107
define('NET_FTP_ERR_DETERMINEPATH_FAILED', 4); // Compatibillity reasons!
108
 
109
/**
110
 * Error code to indicate that the creation of a directory failed
111
 * The directory you tryed to create could not be created. Check the
112
 * access rights on the parent directory!
113
 *
114
 * @since 1.3
115
 * @name NET_FTP_ERR_CREATEDIR_FAILED
116
 * @see Net_FTP::mkdir()
117
 */
118
define('NET_FTP_ERR_CREATEDIR_FAILED', -4);
119
 
120
/**
121
 * Error code to indicate that the EXEC execution failed.
122
 * The execution of a command using EXEC failed. Ensure, that your
123
 * FTP server supports the EXEC command.
124
 *
125
 * @since 1.3
126
 * @name NET_FTP_ERR_EXEC_FAILED
127
 * @see Net_FTP::execute()
128
 */
129
define('NET_FTP_ERR_EXEC_FAILED', -5);
130
 
131
/**
132
 * Error code to indicate that the SITE command failed.
133
 * The execution of a command using SITE failed. Ensure, that your
134
 * FTP server supports the SITE command.
135
 *
136
 * @since 1.3
137
 * @name NET_FTP_ERR_SITE_FAILED
138
 * @see Net_FTP::site()
139
 */
140
define('NET_FTP_ERR_SITE_FAILED', -6);
141
 
142
/**
143
 * Error code to indicate that the CHMOD command failed.
144
 * The execution of CHMOD failed. Ensure, that your
145
 * FTP server supports the CHMOD command and that you have the appropriate
146
 * access rights to use CHMOD.
147
 *
148
 * @since 1.3
149
 * @name NET_FTP_ERR_CHMOD_FAILED
150
 * @see Net_FTP::chmod()
151
 */
152
define('NET_FTP_ERR_CHMOD_FAILED', -7);
153
 
154
/**
155
 * Error code to indicate that a file rename failed
156
 * The renaming of a file on the server failed. Ensure that you have the
157
 * appropriate access rights to rename the file.
158
 *
159
 * @since 1.3
160
 * @name NET_FTP_ERR_RENAME_FAILED
161
 * @see Net_FTP::rename()
162
 */
163
define('NET_FTP_ERR_RENAME_FAILED', -8);
164
 
165
/**
166
 * Error code to indicate that the MDTM command failed
167
 * The MDTM command is not supported for directories. Ensure that you gave
168
 * a file path to the mdtm() method, not a directory path.
169
 *
170
 * @since 1.3
171
 * @name NET_FTP_ERR_MDTMDIR_UNSUPPORTED
172
 * @see Net_FTP::mdtm()
173
 */
174
define('NET_FTP_ERR_MDTMDIR_UNSUPPORTED', -9);
175
 
176
/**
177
 * Error code to indicate that the MDTM command failed
178
 * The MDTM command failed. Ensure that your server supports the MDTM command.
179
 *
180
 * @since 1.3
181
 * @name NET_FTP_ERR_MDTM_FAILED
182
 * @see Net_FTP::mdtm()
183
 */
184
define('NET_FTP_ERR_MDTM_FAILED', -10);
185
 
186
/**
187
 * Error code to indicate that a date returned by the server was misformated
188
 * A date string returned by your server seems to be missformated and could not be
189
 * parsed. Check that the server is configured correctly. If you're sure, please
190
 * send an email to the auhtor with a dumped output of $ftp->ls('./', NET_FTP_RAWLIST);
191
 * to get the date format supported.
192
 *
193
 * @since 1.3
194
 * @name NET_FTP_ERR_DATEFORMAT_FAILED
195
 * @see Net_FTP::mdtm(), Net_FTP::ls()
196
 */
197
define('NET_FTP_ERR_DATEFORMAT_FAILED', -11);
198
 
199
/**
200
 * Error code to indicate that the SIZE command failed
201
 * The determination of the filesize of a file failed. Ensure that your server supports the
202
 * SIZE command.
203
 *
204
 * @since 1.3
205
 * @name NET_FTP_ERR_SIZE_FAILED
206
 * @see Net_FTP::size()
207
 */
208
define('NET_FTP_ERR_SIZE_FAILED', -12);
209
 
210
/**
211
 * Error code to indicate that a local file could not be overwritten
212
 * You specified not to overwrite files. Therefore the local file has not been
213
 * overwriten. If you want to get the file overwriten, please set the option to
214
 * do so.
215
 *
216
 * @since 1.3
217
 * @name NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN
218
 * @see Net_FTP::get(), Net_FTP::getRecursive()
219
 */
220
define('NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN', -13);
221
 
222
/**
223
 * Error code to indicate that a local file could not be overwritten
224
 * Also you specified to overwrite the local file you want to download to,
225
 * it has not been possible to do so. Check that you have the appropriate access
226
 * rights on the local file to overwrite it.
227
 *
228
 * @since 1.3
229
 * @name NET_FTP_ERR_OVERWRITELOCALFILE_FAILED
230
 * @see Net_FTP::get(), Net_FTP::getRecursive()
231
 */
232
define('NET_FTP_ERR_OVERWRITELOCALFILE_FAILED', -14);
233
 
234
/**
235
 * Error code to indicate that the file you wanted to upload does not exist
236
 * The file you tried to upload does not exist. Ensure that it exists.
237
 *
238
 * @since 1.3
239
 * @name NET_FTP_ERR_LOCALFILENOTEXIST
240
 * @see Net_FTP::put(), Net_FTP::putRecursive()
241
 */
242
define('NET_FTP_ERR_LOCALFILENOTEXIST', -15);
243
 
244
/**
245
 * Error code to indicate that a remote file could not be overwritten
246
 * You specified not to overwrite files. Therefore the remote file has not been
247
 * overwriten. If you want to get the file overwriten, please set the option to
248
 * do so.
249
 *
250
 * @since 1.3
251
 * @name NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN
252
 * @see Net_FTP::put(), Net_FTP::putRecursive()
253
 */
254
define('NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN', -16);
255
 
256
/**
257
 * Error code to indicate that the upload of a file failed
258
 * The upload you tried failed. Ensure that you have appropriate access rights
259
 * to upload the desired file.
260
 *
261
 * @since 1.3
262
 * @name NET_FTP_ERR_UPLOADFILE_FAILED
263
 * @see Net_FTP::put(), Net_FTP::putRecursive()
264
 */
265
define('NET_FTP_ERR_UPLOADFILE_FAILED', -17);
266
 
267
/**
268
 * Error code to indicate that you specified an incorrect directory path
269
 * The remote path you specified seems not to be a directory. Ensure that
270
 * the path you specify is a directory and that the path string ends with
271
 * a /.
272
 *
273
 * @since 1.3
274
 * @name NET_FTP_ERR_REMOTEPATHNODIR
275
 * @see Net_FTP::putRecursive(), Net_FTP::getRecursive()
276
 */
277
define('NET_FTP_ERR_REMOTEPATHNODIR', -18);
278
 
279
/**
280
 * Error code to indicate that you specified an incorrect directory path
281
 * The local path you specified seems not to be a directory. Ensure that
282
 * the path you specify is a directory and that the path string ends with
283
 * a /.
284
 *
285
 * @since 1.3
286
 * @name NET_FTP_ERR_LOCALPATHNODIR
287
 * @see Net_FTP::putRecursive(), Net_FTP::getRecursive()
288
 */
289
define('NET_FTP_ERR_LOCALPATHNODIR', -19);
290
 
291
/**
292
 * Error code to indicate that a local directory failed to be created
293
 * You tried to create a local directory through getRecursive() method,
294
 * which has failed. Ensure that you have the appropriate access rights
295
 * to create it.
296
 *
297
 * @since 1.3
298
 * @name NET_FTP_ERR_CREATELOCALDIR_FAILED
299
 * @see Net_FTP::getRecursive()
300
 */
301
define('NET_FTP_ERR_CREATELOCALDIR_FAILED', -20);
302
 
303
/**
304
 * Error code to indicate that the provided hostname was incorrect
305
 * The hostname you provided was invalid. Ensure to provide either a
306
 * full qualified domain name or an IP address.
307
 *
308
 * @since 1.3
309
 * @name NET_FTP_ERR_HOSTNAMENOSTRING
310
 * @see Net_FTP::setHostname()
311
 */
312
define('NET_FTP_ERR_HOSTNAMENOSTRING', -21);
313
 
314
/**
315
 * Error code to indicate that the provided port was incorrect
316
 * The port number you provided was invalid. Ensure to provide either a
317
 * a numeric port number greater zero.
318
 *
319
 * @since 1.3
320
 * @name NET_FTP_ERR_PORTLESSZERO
321
 * @see Net_FTP::setPort()
322
 */
323
define('NET_FTP_ERR_PORTLESSZERO', -22);
324
 
325
/**
326
 * Error code to indicate that you provided an invalid mode constant
327
 * The mode constant you provided was invalid. You may only provide
328
 * FTP_ASCII or FTP_BINARY.
329
 *
330
 * @since 1.3
331
 * @name NET_FTP_ERR_NOMODECONST
332
 * @see Net_FTP::setMode()
333
 */
334
define('NET_FTP_ERR_NOMODECONST', -23);
335
 
336
/**
337
 * Error code to indicate that you provided an invalid timeout
338
 * The timeout you provided was invalid. You have to provide a timeout greater
339
 * or equal to zero.
340
 *
341
 * @since 1.3
342
 * @name NET_FTP_ERR_TIMEOUTLESSZERO
343
 * @see Net_FTP::Net_FTP(), Net_FTP::setTimeout()
344
 */
345
define('NET_FTP_ERR_TIMEOUTLESSZERO', -24);
346
 
347
/**
348
 * Error code to indicate that you provided an invalid timeout
349
 * An error occured while setting the timeout. Ensure that you provide a
350
 * valid integer for the timeount and that your PHP installation works
351
 * correctly.
352
 *
353
 * @since 1.3
354
 * @name NET_FTP_ERR_SETTIMEOUT_FAILED
355
 * @see Net_FTP::Net_FTP(), Net_FTP::setTimeout()
356
 */
357
define('NET_FTP_ERR_SETTIMEOUT_FAILED', -25);
358
 
359
/**
360
 * Error code to indicate that the provided extension file doesn't exist
361
 * The provided extension file does not exist. Ensure to provided an
362
 * existant extension file.
363
 *
364
 * @since 1.3
365
 * @name NET_FTP_ERR_EXTFILENOTEXIST
366
 * @see Net_FTP::getExtensionFile()
367
 */
368
define('NET_FTP_ERR_EXTFILENOTEXIST', -26);
369
 
370
/**
371
 * Error code to indicate that the provided extension file is not readable
372
 * The provided extension file is not readable. Ensure to have sufficient
373
 * access rights for it.
374
 *
375
 * @since 1.3
376
 * @name NET_FTP_ERR_EXTFILEREAD_FAILED
377
 * @see Net_FTP::getExtensionFile()
378
 */
379
define('NET_FTP_ERR_EXTFILEREAD_FAILED', -27);
380
 
381
/**
382
 * Error code to indicate that the deletion of a file failed
383
 * The specified file could not be deleted. Ensure to have sufficient
384
 * access rights to delete the file.
385
 *
386
 * @since 1.3
387
 * @name NET_FTP_ERR_EXTFILEREAD_FAILED
388
 * @see Net_FTP::rm()
389
 */
390
define('NET_FTP_ERR_DELETEFILE_FAILED', -28);
391
 
392
/**
393
 * Error code to indicate that the deletion of a directory faild
394
 * The specified file could not be deleted. Ensure to have sufficient
395
 * access rights to delete the file.
396
 *
397
 * @since 1.3
398
 * @name NET_FTP_ERR_EXTFILEREAD_FAILED
399
 * @see Net_FTP::rm()
400
 */
401
define('NET_FTP_ERR_DELETEDIR_FAILED', -29);
402
 
403
/**
404
 * Error code to indicate that the directory listing failed
405
 * PHP could not list the directory contents on the server. Ensure
406
 * that your server is configured appropriate.
407
 *
408
 * @since 1.3
409
 * @name NET_FTP_ERR_RAWDIRLIST_FAILED
410
 * @see Net_FTP::ls()
411
 */
412
define('NET_FTP_ERR_RAWDIRLIST_FAILED', -30);
413
 
414
/**
415
 * Error code to indicate that the directory listing failed
416
 * The directory listing format your server uses seems not to
417
 * be supported by Net_FTP. Please send the output of the
418
 * call ls('./', NET_FTP_RAWLIST); to the author of this
419
 * class to get it supported.
420
 *
421
 * @since 1.3
422
 * @name NET_FTP_ERR_DIRLIST_UNSUPPORTED
423
 * @see Net_FTP::ls()
424
 */
425
define('NET_FTP_ERR_DIRLIST_UNSUPPORTED', -31);
426
 
427
/**
428
 * Error code to indicate failed disconnecting
429
 * This error code indicates, that disconnection was not possible.
430
 *
431
 * @since 1.3
432
 * @name NET_FTP_ERR_DISCONNECT_FAILED
433
 * @see Net_FTP::disconnect()
434
 */
435
define('NET_FTP_ERR_DISCONNECT_FAILED', -32);
436
 
437
/**
438
 * Error code to indicate that the username you provided was invalid.
439
 * Check that you provided a non-empty string as the username.
440
 *
441
 * @since 1.3
442
 * @name NET_FTP_ERR_USERNAMENOSTRING
443
 * @see Net_FTP::setUsername()
444
 */
445
define('NET_FTP_ERR_USERNAMENOSTRING', -33);
446
 
447
/**
448
 * Error code to indicate that the username you provided was invalid.
449
 * Check that you provided a non-empty string as the username.
450
 *
451
 * @since 1.3
452
 * @name NET_FTP_ERR_PASSWORDNOSTRING
453
 * @see Net_FTP::setPassword()
454
 */
455
define('NET_FTP_ERR_PASSWORDNOSTRING', -33);
456
 
457
/**
458
 * Class for comfortable FTP-communication
459
 *
460
 * This class provides comfortable communication with FTP-servers. You may do everything
461
 * enabled by the PHP-FTP-extension and further functionalities, like recursive-deletion,
462
 * -up- and -download. Another feature is to create directories recursively.
463
 *
464
 * @license   http://www.php.net/license/3_0.txt  PHP License 3.0
465
 * @category  Networking
466
 * @package   FTP
467
 * @author    Tobias Schlitt <toby@php.net>
468
 * @copyright 1997-2005 The PHP Group
469
 * @version   Release: 1.3.0
470
 * @link      http://pear.php.net/package/Net_FTP
471
 * @since     0.0.1
472
 * @access    public
473
 */
474
class Net_FTP extends PEAR
475
{
476
    /**
477
     * The host to connect to
478
     *
479
     * @access  private
480
     * @var     string
481
     */
482
 
483
    var $_hostname;
484
 
485
    /**
486
     * The port for ftp-connection (standard is 21)
487
     *
488
     * @access  private
489
     * @var     int
490
     */
491
 
492
    var $_port = 21;
493
 
494
    /**
495
     * The username for login
496
     *
497
     * @access  private
498
     * @var     string
499
     */
500
 
501
    var $_username;
502
 
503
    /**
504
     * The password for login
505
     *
506
     * @access  private
507
     * @var     string
508
     */
509
 
510
    var $_password;
511
 
512
    /**
513
     * Determine whether to use passive-mode (true) or active-mode (false)
514
     *
515
     * @access  private
516
     * @var     bool
517
     */
518
 
519
    var $_passv;
520
 
521
    /**
522
     * The standard mode for ftp-transfer
523
     *
524
     * @access  private
525
     * @var     int
526
     */
527
 
528
    var $_mode = FTP_BINARY;
529
 
530
    /**
531
     * This holds the handle for the ftp-connection
532
     *
533
     * @access  private
534
     * @var     resource
535
     */
536
 
537
    var $_handle;
538
 
539
    /**
540
     * Contains the timeout for FTP operations
541
     *
542
     * @access  private
543
     * @var     int
544
     * @since   1.3
545
     */
546
 
547
    var $_timeout = 90;
548
 
549
    /**
550
     * Saves file-extensions for ascii- and binary-mode
551
     *
552
     * The array contains 2 sub-arrays ("ascii" and "binary"), which both contain
553
     * file-extensions without the "." (".php" = "php").
554
     *
555
     * @access  private
556
     * @var     array
557
     */
558
 
559
    var $_file_extensions;
560
 
561
    /**
562
     * ls match
563
     * Matches the ls entries against a regex and maps the resulting array to speaking names
564
     *
565
     * @access  private
566
     * @var     array
567
     * @since   1.3
568
     */
569
 
570
    var $_ls_match = array(
571
        'unix'    => array(
572
            'pattern' => '/(?:(d)|.)([rwxt-]+)\s+(\w+)\s+(\w+)\s+(\w+)\s+(\w+)\s+(\S+\s+\S+\s+\S+)\s+(.+)/',
573
            'map'     => array('name'=>8,'size'=>6,'rights'=>2,'user'=>4,'group'=>5,
574
                              'files_inside'=>3,'date'=>7,'is_dir'=>1)
575
        ),
576
        'windows' => array(
577
            'pattern' => '/(.+)\s+(.+)\s+((<DIR>)|[0-9]+)\s+(.+)/',
578
            'map'     => array('name'=>5,'date'=>1,'is_dir'=>3)
579
        )
580
    );
581
 
582
    /**
583
     * matcher
584
     * Stores the matcher for the current connection
585
     *
586
     * @access  private
587
     * @var     array
588
     * @since   1.3
589
     */
590
    var $_matcher = null;
591
 
592
    /**
593
     * Holds all Net_FTP_Observer objects
594
     * that wish to be notified of new messages.
595
     *
596
     * @var     array
597
     * @access  private
598
     * @since   1.3
599
     */
600
 
601
    var $_listeners = array();
602
 
603
    /**
604
     * This generates a new FTP-Object. The FTP-connection will not be established, yet.
605
     * You can leave $host and $port blank, if you want. The $host will not be set
606
     * and the $port will be left at 21. You have to set the $host manualy before
607
     * trying to connect or with the connect() method.
608
     *
609
     * @access  public
610
     * @param   string $host    (optional) The hostname
611
     * @param   int    $port    (optional) The port
612
     * @param   int    $timeout (optional) Sets the standard timeout
613
     * @return  void
614
     * @see     Net_FTP::setHostname(), Net_FTP::setPort(), Net_FTP::connect()
615
     */
616
 
617
    function Net_FTP($host = null, $port = null, $timeout = 90)
618
    {
619
        $this->PEAR();
620
        if (isset($host)) {
621
            $this->setHostname($host);
622
        }
623
        if (isset($port)) {
624
            $this->setPort($port);
625
        }
626
        $this->_timeout = $timeout;
627
        $this->_file_extensions[FTP_ASCII] = array();
628
        $this->_file_extensions[FTP_BINARY] = array();
629
    }
630
 
631
    /**
632
     * This function generates the FTP-connection. You can optionally define a
633
     * hostname and/or a port. If you do so, this data is stored inside the object.
634
     *
635
     * @access  public
636
     * @param   string $host    (optional) The Hostname
637
     * @param   int    $port    (optional) The Port
638
     * @return  mixed           True on success, otherwise PEAR::Error
639
     * @see     NET_FTP_ERR_CONNECT_FAILED
640
     */
641
 
642
    function connect($host = null, $port = null)
643
    {
644
        $this->_matcher = null;
645
        if (isset($host)) {
646
            $this->setHostname($host);
647
        }
648
        if (isset($port)) {
649
            $this->setPort($port);
650
        }
651
        $handle = @ftp_connect($this->getHostname(), $this->getPort(), $this->_timeout);
652
        if (!$handle) {
653
            return $this->raiseError("Connection to host failed", NET_FTP_ERR_CONNECT_FAILED);
654
        } else {
655
            $this->_handle =& $handle;
656
            return true;
657
        }
658
    }
659
 
660
    /**
661
     * This function close the FTP-connection
662
     *
663
     * @access  public
664
     * @return  bool|PEAR_Error Returns true on success, PEAR_Error on failure
665
     */
666
 
667
    function disconnect()
668
    {
669
        $res = @ftp_close($this->_handle);
670
        if (!$res) {
671
            return PEAR::raiseError('Disconnect failed.', NET_FTP_ERR_DISCONNECT_FAILED);
672
        }
673
        return true;
674
    }
675
 
676
    /**
677
     * This logges you into the ftp-server. You are free to specify username and password
678
     * in this method. If you specify it, the values will be taken into the corresponding
679
     * attributes, if do not specify, the attributes are taken.
680
     *
681
     * @access  public
682
     * @param   string $username  (optional) The username to use
683
     * @param   string $password  (optional) The password to use
684
     * @return  mixed             True on success, otherwise PEAR::Error
685
     * @see     NET_FTP_ERR_LOGIN_FAILED
686
     */
687
 
688
    function login($username = null, $password = null)
689
    {
690
        if (!isset($username)) {
691
            $username = $this->getUsername();
692
        } else {
693
            $this->setUsername($username);
694
        }
695
 
696
        if (!isset($password)) {
697
            $password = $this->getPassword();
698
        } else {
699
            $this->setPassword($password);
700
        }
701
 
702
        $res = @ftp_login($this->_handle, $username, $password);
703
 
704
        if (!$res) {
705
            return $this->raiseError("Unable to login", NET_FTP_ERR_LOGIN_FAILED);
706
        } else {
707
            return true;
708
        }
709
    }
710
 
711
    /**
712
     * This changes the currently used directory. You can use either an absolute
713
     * directory-path (e.g. "/home/blah") or a relative one (e.g. "../test").
714
     *
715
     * @access  public
716
     * @param   string $dir  The directory to go to.
717
     * @return  mixed        True on success, otherwise PEAR::Error
718
     * @see     NET_FTP_ERR_DIRCHANGE_FAILED
719
     */
720
 
721
    function cd($dir)
722
    {
723
        $erg = @ftp_chdir($this->_handle, $dir);
724
        if (!$erg) {
725
            return $this->raiseError("Directory change failed", NET_FTP_ERR_DIRCHANGE_FAILED);
726
        } else {
727
            return true;
728
        }
729
    }
730
 
731
    /**
732
     * Show's you the actual path on the server
733
     * This function questions the ftp-handle for the actual selected path and returns it.
734
     *
735
     * @access  public
736
     * @return  mixed        The actual path or PEAR::Error
737
     * @see     NET_FTP_ERR_DETERMINEPATH_FAILED
738
     */
739
 
740
    function pwd()
741
    {
742
        $res = @ftp_pwd($this->_handle);
743
        if (!$res) {
744
            return $this->raiseError("Could not determine the actual path.", NET_FTP_ERR_DETERMINEPATH_FAILED);
745
        } else {
746
            return $res;
747
        }
748
    }
749
 
750
    /**
751
     * This works similar to the mkdir-command on your local machine. You can either give
752
     * it an absolute or relative path. The relative path will be completed with the actual
753
     * selected server-path. (see: pwd())
754
     *
755
     * @access  public
756
     * @param   string $dir       Absolute or relative dir-path
757
     * @param   bool   $recursive (optional) Create all needed directories
758
     * @return  mixed             True on success, otherwise PEAR::Error
759
     * @see     NET_FTP_ERR_CREATEDIR_FAILED
760
     */
761
 
762
    function mkdir($dir, $recursive = false)
763
    {
764
        $dir = $this->_construct_path($dir);
765
        $savedir = $this->pwd();
766
        $this->pushErrorHandling(PEAR_ERROR_RETURN);
767
        $e = $this->cd($dir);
768
        $this->popErrorHandling();
769
        if ($e === true) {
770
            $this->cd($savedir);
771
            return true;
772
        }
773
        $this->cd($savedir);
774
        if ($recursive === false){
775
            $res = @ftp_mkdir($this->_handle, $dir);
776
            if (!$res) {
777
                return $this->raiseError("Creation of '$dir' failed", NET_FTP_ERR_CREATEDIR_FAILED);
778
            } else {
779
                return true;
780
            }
781
        } else {
782
            if(strpos($dir, '/') === false) {
783
                return $this->mkdir($dir,false);
784
            }
785
            $pos = 0;
786
            $res = $this->mkdir(dirname($dir), true);
787
            $res = $this->mkdir($dir, false);
788
            if ($res !== true) {
789
                return $res;
790
            }
791
            return true;
792
        }
793
    }
794
 
795
    /**
796
     * This method tries executing a command on the ftp, using SITE EXEC.
797
     *
798
     * @access  public
799
     * @param   string $command The command to execute
800
     * @return  mixed           The result of the command (if successfull), otherwise PEAR::Error
801
     * @see     NET_FTP_ERR_EXEC_FAILED
802
     */
803
 
804
    function execute($command)
805
    {
806
        $res = @ftp_exec($this->_handle, $command);
807
        if (!$res) {
808
            return $this->raiseError("Execution of command '$command' failed.", NET_FTP_ERR_EXEC_FAILED);
809
        } else {
810
            return $res;
811
        }
812
    }
813
 
814
    /**
815
     * Execute a SITE command on the server
816
     * This method tries to execute a SITE command on the ftp server.
817
     *
818
     * @access  public
819
     * @param   string $command   The command with parameters to execute
820
     * @return  mixed             True if successful, otherwise PEAR::Error
821
     * @see     NET_FTP_ERR_SITE_FAILED
822
     */
823
 
824
    function site($command)
825
    {
826
        $res = @ftp_site($this->_handle, $command);
827
        if (!$res) {
828
            return $this->raiseError("Execution of SITE command '$command' failed.", NET_FTP_ERR_SITE_FAILED);
829
        } else {
830
            return $res;
831
        }
832
    }
833
 
834
    /**
835
     * This method will try to chmod the file specified on the server
836
     * Currently, you must give a number as the the permission argument (777 or
837
     * similar). The file can be either a relative or absolute path.
838
     * NOTE: Some servers do not support this feature. In that case, you will
839
     * get a PEAR error object returned. If successful, the method returns true
840
     *
841
     * @access  public
842
     * @param   mixed   $target        The file or array of files to set permissions for
843
     * @param   integer $permissions   The mode to set the file permissions to
844
     * @return  mixed                  True if successful, otherwise PEAR::Error
845
     * @see     NET_FTP_ERR_CHMOD_FAILED
846
     */
847
 
848
    function chmod($target, $permissions)
849
    {
850
        // If $target is an array: Loop through it.
851
        if (is_array($target)) {
852
 
853
            for ($i = 0; $i < count($target); $i++) {
854
                $res = $this->chmod($target[$i], $permissions);
855
                if (PEAR::isError($res)) {
856
                    return $res;
857
                } // end if isError
858
            } // end for i < count($target)
859
 
860
        } else {
861
 
862
            $res = $this->site("CHMOD " . $permissions . " " . $target);
863
            if (!$res) {
864
                return PEAR::raiseError("CHMOD " . $permissions . " " . $target . " failed", NET_FTP_ERR_CHMOD_FAILED);
865
            } else {
866
                return $res;
867
            }
868
 
869
        } // end if is_array
870
 
871
    } // end method chmod
872
 
873
    /**
874
     * This method will try to chmod a folder and all of its contents
875
     * on the server. The target argument must be a folder or an array of folders
876
     * and the permissions argument have to be an integer (i.e. 777).
877
     * The file can be either a relative or absolute path.
878
     * NOTE: Some servers do not support this feature. In that case, you
879
     * will get a PEAR error object returned. If successful, the method
880
     * returns true
881
     *
882
     * @access  public
883
     * @param   mixed   $target        The folder or array of folders to
884
     *                                 set permissions for
885
     * @param   integer $permissions   The mode to set the folder
886
     *                                 and file permissions to
887
     * @return  mixed                  True if successful, otherwise PEAR::Error
888
     * @see     NET_FTP_ERR_CHMOD_FAILED, NET_FTP_ERR_DETERMINEPATH_FAILED, NET_FTP_ERR_RAWDIRLIST_FAILED, NET_FTP_ERR_DIRLIST_UNSUPPORTED
889
     */
890
 
891
    function chmodRecursive($target, $permissions)
892
    {
893
        static $dir_permissions;
894
 
895
        if(!isset($dir_permissions)){ // Making directory specific permissions
896
            $dir_permissions = $this->_makeDirPermissions($permissions);
897
        }
898
 
899
        // If $target is an array: Loop through it
900
        if (is_array($target)) {
901
 
902
            for ($i = 0; $i < count($target); $i++) {
903
                $res = $this->chmodRecursive($target[$i], $permissions);
904
                if (PEAR::isError($res)) {
905
                    return $res;
906
                } // end if isError
907
            } // end for i < count($target)
908
 
909
        } else {
910
 
911
            $remote_path = $this->_construct_path($target);
912
 
913
            // Chmod the directory itself
914
            $result = $this->chmod($remote_path, $dir_permissions);
915
 
916
            if (PEAR::isError($result)) {
917
                return $result;
918
            }
919
 
920
            // If $remote_path last character is not a slash, add one
921
            if (substr($remote_path, strlen($remote_path)-1) != "/") {
922
 
923
                $remote_path .= "/";
924
            }
925
 
926
            $dir_list = array();
927
            $mode = NET_FTP_DIRS_ONLY;
928
            $dir_list = $this->ls($remote_path, $mode);
929
            foreach ($dir_list as $dir_entry) {
930
 
931
                $remote_path_new = $remote_path.$dir_entry["name"]."/";
932
 
933
                // Chmod the directory we're about to enter
934
                $result = $this->chmod($remote_path_new, $dir_permissions);
935
 
936
                if (PEAR::isError($result)) {
937
                    return $result;
938
                }
939
 
940
                $result = $this->chmodRecursive($remote_path_new, $permissions);
941
 
942
                if (PEAR::isError($result)) {
943
                    return $result;
944
                }
945
 
946
            } // end foreach dir_list as dir_entry
947
 
948
            $file_list = array();
949
            $mode = NET_FTP_FILES_ONLY;
950
            $file_list = $this->ls($remote_path, $mode);
951
 
952
            foreach ($file_list as $file_entry) {
953
 
954
                $remote_file = $remote_path.$file_entry["name"];
955
 
956
                $result = $this->chmod($remote_file, $permissions);
957
 
958
                if (PEAR::isError($result)) {
959
                    return $result;
960
                }
961
 
962
            } // end foreach $file_list
963
 
964
        } // end if is_array
965
 
966
        return true; // No errors
967
 
968
    } // end method chmodRecursive
969
 
970
    /**
971
     * Rename or move a file or a directory from the ftp-server
972
     *
973
     * @access  public
974
     * @param   string $remote_from The remote file or directory original to rename or move
975
     * @param   string $remote_to The remote file or directory final to rename or move
976
     * @return  bool $res True on success, otherwise PEAR::Error
977
     * @see     NET_FTP_ERR_RENAME_FAILED
978
     */
979
 
980
    function rename ($remote_from, $remote_to)
981
    {
982
        $res = @ftp_rename($this->_handle, $remote_from, $remote_to);
983
        if(!$res) {
984
            return $this->raiseError("Could not rename ".$remote_from." to ".$remote_to." !", NET_FTP_ERR_RENAME_FAILED);
985
        }
986
        return true;
987
    }
988
 
989
    /**
990
     * This will return logical permissions mask for directory.
991
     * if directory have to be writeable it have also be executable
992
     *
993
     * @access  private
994
     * @param   string $permissions    File permissions in digits for file (i.e. 666)
995
     * @return  string                 File permissions in digits for directory (i.e. 777)
996
     */
997
 
998
    function _makeDirPermissions($permissions){
999
        $permissions = (string)$permissions;
1000
 
1001
        for($i = 0; $i < strlen($permissions); $i++){ // going through (user, group, world)
1002
            if((int)$permissions{$i} & 4 and !((int)$permissions{$i} & 1)){ // Read permission is set
1003
                                                                            // but execute not yet
1004
                (int)$permissions{$i} = (int)$permissions{$i} + 1; // Adding execute flag
1005
            }
1006
        }
1007
 
1008
        return (string)$permissions;
1009
    }
1010
 
1011
    /**
1012
     * This will return the last modification-time of a file. You can either give this
1013
     * function a relative or an absolute path to the file to check.
1014
     * NOTE: Some servers will not support this feature and the function works
1015
     * only on files, not directories! When successful,
1016
     * it will return the last modification-time as a unix-timestamp or, when $format is
1017
     * specified, a preformated timestring.
1018
     *
1019
     * @access  public
1020
     * @param   string $file    The file to check
1021
     * @param   string $format  (optional) The format to give the date back
1022
     *                          if not set, it will return a Unix timestamp
1023
     * @return  mixed           Unix timestamp, a preformated date-string or PEAR::Error
1024
     * @see     NET_FTP_ERR_MDTMDIR_UNSUPPORTED, NET_FTP_ERR_MDTM_FAILED, NET_FTP_ERR_DATEFORMAT_FAILED
1025
     */
1026
 
1027
    function mdtm($file, $format = null)
1028
    {
1029
        $file = $this->_construct_path($file);
1030
        if ($this->_check_dir($file)) {
1031
            return $this->raiseError("Filename '$file' seems to be a directory.", NET_FTP_ERR_MDTMDIR_UNSUPPORTED);
1032
        }
1033
        $res = @ftp_mdtm($this->_handle, $file);
1034
        if ($res == -1) {
1035
            return $this->raiseError("Could not get last-modification-date of '$file'.", NET_FTP_ERR_MDTM_FAILED);
1036
        }
1037
        if (isset($format)) {
1038
            $res = date($format, $res);
1039
            if (!$res) {
1040
                return $this->raiseError("Date-format failed on timestamp '$res'.", NET_FTP_ERR_DATEFORMAT_FAILED);
1041
            }
1042
        }
1043
        return $res;
1044
    }
1045
 
1046
    /**
1047
     * This will return the size of a given file in bytes. You can either give this function
1048
     * a relative or an absolute file-path. NOTE: Some servers do not support this feature!
1049
     *
1050
     * @access  public
1051
     * @param   string $file   The file to check
1052
     * @return  mixed          Size in bytes or PEAR::Error
1053
     * @see     NET_FTP_ERR_SIZE_FAILED
1054
     */
1055
 
1056
    function size($file)
1057
    {
1058
        $file = $this->_construct_path($file);
1059
        $res = @ftp_size($this->_handle, $file);
1060
        if ($res == -1) {
1061
            return $this->raiseError("Could not determine filesize of '$file'.", NET_FTP_ERR_SIZE_FAILED);
1062
        } else {
1063
            return $res;
1064
        }
1065
    }
1066
 
1067
    /**
1068
     * This method returns a directory-list of the current directory or given one.
1069
     * To display the current selected directory, simply set the first parameter to null
1070
     * or leave it blank, if you do not want to use any other parameters.
1071
     * <BR><BR>
1072
     * There are 4 different modes of listing directories. Either to list only
1073
     * the files (using NET_FTP_FILES_ONLY), to list only directories (using
1074
     * NET_FTP_DIRS_ONLY) or to show both (using NET_FTP_DIRS_FILES, which is default).
1075
     * <BR><BR>
1076
     * The 4th one is the NET_FTP_RAWLIST, which returns just the array created by the
1077
     * ftp_rawlist()-function build into PHP.
1078
     * <BR><BR>
1079
     * The other function-modes will return an array containing the requested data.
1080
     * The files and dirs are listed in human-sorted order, but if you select
1081
     * NET_FTP_DIRS_FILES the directories will be added above the files,
1082
     * but although both sorted.
1083
     * <BR><BR>
1084
     * All elements in the arrays are associative arrays themselves. The have the following
1085
     * structure:
1086
     * <BR><BR>
1087
     * Dirs:<BR>
1088
     *           ["name"]        =>  string The name of the directory<BR>
1089
     *           ["rights"]      =>  string The rights of the directory (in style "rwxr-xr-x")<BR>
1090
     *           ["user"]        =>  string The owner of the directory<BR>
1091
     *           ["group"]       =>  string The group-owner of the directory<BR>
1092
     *           ["files_inside"]=>  string The number of files/dirs inside the directory
1093
     *                                      excluding "." and ".."<BR>
1094
     *           ["date"]        =>  int The creation-date as Unix timestamp<BR>
1095
     *           ["is_dir"]      =>  bool true, cause this is a dir<BR>
1096
     * <BR><BR>
1097
     * Files:<BR>
1098
     *           ["name"]        =>  string The name of the file<BR>
1099
     *           ["size"]        =>  int Size in bytes<BR>
1100
     *           ["rights"]      =>  string The rights of the file (in style "rwxr-xr-x")<BR>
1101
     *           ["user"]        =>  string The owner of the file<BR>
1102
     *           ["group"]       =>  string The group-owner of the file<BR>
1103
     *           ["date"]        =>  int The creation-date as Unix timestamp<BR>
1104
     *           ["is_dir"]      =>  bool false, cause this is a file<BR>
1105
     *
1106
     * @access  public
1107
     * @param   string $dir   (optional) The directory to list or null, when listing the current directory.
1108
     * @param   int    $mode  (optional) The mode which types to list (files, directories or both).
1109
     * @return  mixed         The directory list as described above or PEAR::Error on failure.
1110
     * @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
1111
     */
1112
 
1113
    function ls($dir = null, $mode = NET_FTP_DIRS_FILES)
1114
    {
1115
        if (!isset($dir)) {
1116
            $dir = @ftp_pwd($this->_handle);
1117
            if (!$dir) {
1118
                return $this->raiseError("Could not retrieve current directory", NET_FTP_ERR_DETERMINEPATH_FAILED);
1119
            }
1120
        }
1121
        if (($mode != NET_FTP_FILES_ONLY) && ($mode != NET_FTP_DIRS_ONLY) && ($mode != NET_FTP_RAWLIST)) {
1122
            $mode = NET_FTP_DIRS_FILES;
1123
        }
1124
 
1125
        switch ($mode) {
1126
            case NET_FTP_DIRS_FILES:    $res = $this->_ls_both ( $dir );
1127
                                        break;
1128
            case NET_FTP_DIRS_ONLY:     $res = $this->_ls_dirs ( $dir );
1129
                                        break;
1130
            case NET_FTP_FILES_ONLY:    $res = $this->_ls_files ( $dir );
1131
                                        break;
1132
            case NET_FTP_RAWLIST:       $res = @ftp_rawlist($this->_handle, $dir);
1133
                                        break;
1134
        }
1135
 
1136
        return $res;
1137
    }
1138
 
1139
    /**
1140
     * This method will delete the given file or directory ($path) from the server
1141
     * (maybe recursive).
1142
     *
1143
     * Whether the given string is a file or directory is only determined by the last
1144
     * sign inside the string ("/" or not).
1145
     *
1146
     * If you specify a directory, you can optionally specify $recursive as true,
1147
     * to let the directory be deleted recursive (with all sub-directories and files
1148
     * inherited).
1149
     *
1150
     * You can either give a absolute or relative path for the file / dir. If you choose to
1151
     * use the relative path, it will be automatically completed with the actual
1152
     * selected directory.
1153
     *
1154
     * @access  public
1155
     * @param   string $path      The absolute or relative path to the file / directory.
1156
     * @param   bool   $recursive (optional)
1157
     * @return  mixed             True on success, otherwise PEAR::Error
1158
     * @see     NET_FTP_ERR_DELETEFILE_FAILED, NET_FTP_ERR_DELETEDIR_FAILED, NET_FTP_ERR_REMOTEPATHNODIR
1159
     */
1160
 
1161
    function rm($path, $recursive = false)
1162
    {
1163
        $path = $this->_construct_path($path);
1164
 
1165
        if ($this->_check_dir($path)) {
1166
            if ($recursive) {
1167
                return $this->_rm_dir_recursive($path);
1168
            } else {
1169
                return $this->_rm_dir($path);
1170
            }
1171
        } else {
1172
            return $this->_rm_file($path);
1173
        }
1174
    }
1175
 
1176
    /**
1177
     * This function will download a file from the ftp-server. You can either spcify a absolute
1178
     * path to the file (beginning with "/") or a relative one, which will be completed
1179
     * with the actual directory you selected on the server. You can specify
1180
     * the path to which the file will be downloaded on the local
1181
     * maschine, if the file should be overwritten if it exists (optionally, default is
1182
     * no overwriting) and in which mode (FTP_ASCII or FTP_BINARY) the file should be
1183
     * downloaded (if you do not specify this, the method tries to determine it automatically
1184
     * from the mode-directory or uses the default-mode, set by you). If you give a relative
1185
     * path to the local-file, the script-path is used as basepath.
1186
     *
1187
     * @access  public
1188
     * @param   string $remote_file The absolute or relative path to the file to download
1189
     * @param   string $local_file  The local file to put the downloaded in
1190
     * @param   bool   $overwrite   (optional) Whether to overwrite existing file
1191
     * @param   int    $mode        (optional) Either FTP_ASCII or FTP_BINARY
1192
     * @return  mixed               True on success, otherwise PEAR::Error
1193
     * @see     NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN, NET_FTP_ERR_OVERWRITELOCALFILE_FAILED, NET_FTP_ERR_OVERWRITELOCALFILE_FAILED
1194
     */
1195
 
1196
    function get($remote_file, $local_file, $overwrite = false, $mode = null)
1197
    {
1198
        if (!isset($mode)) {
1199
            $mode = $this->checkFileExtension($remote_file);
1200
        }
1201
 
1202
        $remote_file = $this->_construct_path($remote_file);
1203
 
1204
        if (@file_exists($local_file) && !$overwrite) {
1205
            return $this->raiseError("Local file '$local_file' exists and may not be overwriten.", NET_FTP_ERR_OVERWRITELOCALFILE_FORBIDDEN);
1206
        }
1207
        if (@file_exists($local_file) && !@is_writeable($local_file) && $overwrite) {
1208
            return $this->raiseError("Local file '$local_file' is not writeable. Can not overwrite.", NET_FTP_ERR_OVERWRITELOCALFILE_FAILED);
1209
        }
1210
 
1211
        if (@function_exists('ftp_nb_get')){
1212
            $res = @ftp_nb_get($this->_handle, $local_file, $remote_file, $mode);
1213
            while ($res == FTP_MOREDATA) {
1214
                $this->_announce('nb_get');
1215
                $res = @ftp_nb_continue ($this->_handle);
1216
            }
1217
        } else {
1218
            $res = @ftp_get($this->_handle, $local_file, $remote_file, $mode);
1219
        }
1220
        if (!$res) {
1221
            return $this->raiseError("File '$remote_file' could not be downloaded to '$local_file'.", NET_FTP_ERR_OVERWRITELOCALFILE_FAILED);
1222
        } else {
1223
            return true;
1224
        }
1225
    }
1226
 
1227
    /**
1228
     * This function will upload a file to the ftp-server. You can either specify a absolute
1229
     * path to the remote-file (beginning with "/") or a relative one, which will be completed
1230
     * with the actual directory you selected on the server. You can specify
1231
     * the path from which the file will be uploaded on the local
1232
     * maschine, if the file should be overwritten if it exists (optionally, default is
1233
     * no overwriting) and in which mode (FTP_ASCII or FTP_BINARY) the file should be
1234
     * downloaded (if you do not specify this, the method tries to determine it automatically
1235
     * from the mode-directory or uses the default-mode, set by you). If you give a relative
1236
     * path to the local-file, the script-path is used as basepath.
1237
     *
1238
     * @access  public
1239
     * @param   string $local_file  The local file to upload
1240
     * @param   string $remote_file The absolute or relative path to the file to upload to
1241
     * @param   bool   $overwrite   (optional) Whether to overwrite existing file
1242
     * @param   int    $mode        (optional) Either FTP_ASCII or FTP_BINARY
1243
     * @return  mixed               True on success, otherwise PEAR::Error
1244
     * @see     NET_FTP_ERR_LOCALFILENOTEXIST, NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN, NET_FTP_ERR_UPLOADFILE_FAILED
1245
     */
1246
 
1247
    function put($local_file, $remote_file, $overwrite = false, $mode = null)
1248
    {
1249
        if (!isset($mode)) {
1250
            $mode = $this->checkFileExtension($local_file);
1251
        }
1252
        $remote_file = $this->_construct_path($remote_file);
1253
 
1254
        if (!@file_exists($local_file)) {
1255
            return $this->raiseError("Local file '$local_file' does not exist.", NET_FTP_ERR_LOCALFILENOTEXIST);
1256
        }
1257
        if ((@ftp_size($this->_handle, $remote_file) != -1) && !$overwrite) {
1258
            return $this->raiseError("Remote file '$remote_file' exists and may not be overwriten.", NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN);
1259
        }
1260
 
1261
        if (function_exists('ftp_nb_put')){
1262
            $res = @ftp_nb_put($this->_handle, $remote_file, $local_file, $mode);
1263
            while ($res == FTP_MOREDATA) {
1264
                $this->_announce('nb_put');
1265
                $res = @ftp_nb_continue($this->_handle);
1266
            }
1267
 
1268
        } else {
1269
            $res = @ftp_put($this->_handle, $remote_file, $local_file, $mode);
1270
        }
1271
        if (!$res) {
1272
            return $this->raiseError("File '$local_file' could not be uploaded to '$remote_file'.", NET_FTP_ERR_UPLOADFILE_FAILED);
1273
        } else {
1274
            return true;
1275
        }
1276
    }
1277
 
1278
    /**
1279
     * This functionality allows you to transfer a whole directory-structure from the
1280
     * remote-ftp to your local host. You have to give a remote-directory (ending with
1281
     * '/') and the local directory (ending with '/') where to put the files you download.
1282
     * The remote path is automatically completed with the current-remote-dir, if you give
1283
     * a relative path to this function. You can give a relative path for the $local_path,
1284
     * too. Then the script-basedir will be used for comletion of the path.
1285
     * The parameter $overwrite will determine, whether to overwrite existing files or not.
1286
     * Standard for this is false. Fourth you can explicitly set a mode for all transfer-
1287
     * actions done. If you do not set this, the method tries to determine the transfer-
1288
     * mode by checking your mode-directory for the file-extension. If the extension is not
1289
     * inside the mode-directory, it will get your default-mode.
1290
     *
1291
     * @access  public
1292
     * @param   string $remote_path The path to download
1293
     * @param   string $local_path  The path to download to
1294
     * @param   bool   $overwrite   (optional) Whether to overwrite existing files (true) or not (false, standard).
1295
     * @param   int    $mode        (optional) The transfermode (either FTP_ASCII or FTP_BINARY).
1296
     * @return  mixed               True on succes, otherwise PEAR::Error
1297
     * @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
1298
     */
1299
 
1300
    function getRecursive($remote_path, $local_path, $overwrite = false, $mode = null)
1301
    {
1302
        $remote_path = $this->_construct_path($remote_path);
1303
        if (!$this->_check_dir($remote_path)) {
1304
            return $this->raiseError("Given remote-path '$remote_path' seems not to be a directory.", NET_FTP_ERR_REMOTEPATHNODIR);
1305
        }
1306
        if (!$this->_check_dir($local_path)) {
1307
            return $this->raiseError("Given local-path '$local_path' seems not to be a directory.", NET_FTP_ERR_LOCALPATHNODIR);
1308
        }
1309
 
1310
        if (!@is_dir($local_path)) {
1311
            $res = @mkdir($local_path);
1312
            if (!$res) {
1313
                return $this->raiseError("Could not create dir '$local_path'", NET_FTP_ERR_CREATELOCALDIR_FAILED);
1314
            }
1315
        }
1316
        $dir_list = array();
1317
        $dir_list = $this->ls($remote_path, NET_FTP_DIRS_ONLY);
1318
        foreach ($dir_list as $dir_entry) {
1319
            if ($dir_entry['name'] != '.' && $dir_entry['name'] != '..') {
1320
                $remote_path_new = $remote_path.$dir_entry["name"]."/";
1321
                $local_path_new = $local_path.$dir_entry["name"]."/";
1322
                $result = $this->getRecursive($remote_path_new, $local_path_new, $overwrite, $mode);
1323
                if ($this->isError($result)) {
1324
                    return $result;
1325
                }
1326
            }
1327
        }
1328
        $file_list = array();
1329
        $file_list = $this->ls($remote_path, NET_FTP_FILES_ONLY);
1330
        foreach ($file_list as $file_entry) {
1331
            $remote_file = $remote_path.$file_entry["name"];
1332
            $local_file = $local_path.$file_entry["name"];
1333
            $result = $this->get($remote_file, $local_file, $overwrite, $mode);
1334
            if ($this->isError($result)) {
1335
                return $result;
1336
            }
1337
        }
1338
        return true;
1339
    }
1340
 
1341
    /**
1342
     * This functionality allows you to transfer a whole directory-structure from your
1343
     * local host to the remote-ftp. You have to give a remote-directory (ending with
1344
     * '/') and the local directory (ending with '/') where to put the files you download.
1345
     * The remote path is automatically completed with the current-remote-dir, if you give
1346
     * a relative path to this function. You can give a relative path for the $local_path,
1347
     * too. Then the script-basedir will be used for comletion of the path.
1348
     * The parameter $overwrite will determine, whether to overwrite existing files or not.
1349
     * Standard for this is false. Fourth you can explicitly set a mode for all transfer-
1350
     * actions done. If you do not set this, the method tries to determine the transfer-
1351
     * mode by checking your mode-directory for the file-extension. If the extension is not
1352
     * inside the mode-directory, it will get your default-mode.
1353
     *
1354
     * @access  public
1355
     * @param   string $remote_path The path to download
1356
     * @param   string $local_path  The path to download to
1357
     * @param   bool   $overwrite   (optional) Whether to overwrite existing files (true) or not (false, standard).
1358
     * @param   int    $mode        (optional) The transfermode (either FTP_ASCII or FTP_BINARY).
1359
     * @return  mixed               True on succes, otherwise PEAR::Error
1360
     * @see     NET_FTP_ERR_LOCALFILENOTEXIST, NET_FTP_ERR_OVERWRITEREMOTEFILE_FORBIDDEN, NET_FTP_ERR_UPLOADFILE_FAILED, NET_FTP_ERR_LOCALPATHNODIR, NET_FTP_ERR_REMOTEPATHNODIR
1361
     */
1362
 
1363
    function putRecursive($local_path, $remote_path, $overwrite = false, $mode = null)
1364
    {
1365
        $remote_path = $this->_construct_path($remote_path);
1366
        if (!$this->_check_dir($local_path) || !is_dir($local_path)) {
1367
            return $this->raiseError("Given local-path '$local_path' seems not to be a directory.", NET_FTP_ERR_LOCALPATHNODIR);
1368
        }
1369
        if (!$this->_check_dir($remote_path)) {
1370
            return $this->raiseError("Given remote-path '$remote_path' seems not to be a directory.", NET_FTP_ERR_REMOTEPATHNODIR);
1371
        }
1372
        $old_path = $this->pwd();
1373
        if ($this->isError($this->cd($remote_path))) {
1374
            $res = $this->mkdir($remote_path);
1375
            if ($this->isError($res)) {
1376
                return $res;
1377
            }
1378
        }
1379
        $this->cd($old_path);
1380
        $dir_list = $this->_ls_local($local_path);
1381
        foreach ($dir_list["dirs"] as $dir_entry) {
1382
            $remote_path_new = $remote_path.$dir_entry."/";
1383
            $local_path_new = $local_path.$dir_entry."/";
1384
            $result = $this->putRecursive($local_path_new, $remote_path_new, $overwrite, $mode);
1385
            if ($this->isError($result)) {
1386
                return $result;
1387
            }
1388
        }
1389
 
1390
        foreach ($dir_list["files"] as $file_entry) {
1391
            $remote_file = $remote_path.$file_entry;
1392
            $local_file = $local_path.$file_entry;
1393
            $result = $this->put($local_file, $remote_file, $overwrite, $mode);
1394
            if ($this->isError($result)) {
1395
                return $result;
1396
            }
1397
        }
1398
        return true;
1399
    }
1400
 
1401
    /**
1402
     * This checks, whether a file should be transfered in ascii- or binary-mode
1403
     * by it's file-extension. If the file-extension is not set or
1404
     * the extension is not inside one of the extension-dirs, the actual set
1405
     * transfer-mode is returned.
1406
     *
1407
     * @access  public
1408
     * @param   string $filename  The filename to be checked
1409
     * @return  int               Either FTP_ASCII or FTP_BINARY
1410
     */
1411
 
1412
    function checkFileExtension($filename)
1413
    {
1414
        $pattern = "/\.(.*)$/";
1415
        $has_extension = preg_match($pattern, $filename, $eregs);
1416
        if (!$has_extension) {
1417
            return $this->_mode;
1418
        } else {
1419
            $ext = $eregs[1];
1420
        }
1421
 
1422
        if (!empty($this->_file_extensions[$ext])) {
1423
            return $this->_file_extensions[$ext];
1424
        }
1425
 
1426
        return $this->_mode;
1427
    }
1428
 
1429
    /**
1430
     * Set the Hostname
1431
     *
1432
     * @access  public
1433
     * @param   string $host The Hostname to set
1434
     * @return  bool True on success, otherwise PEAR::Error
1435
     * @see     NET_FTP_ERR_HOSTNAMENOSTRING
1436
     */
1437
 
1438
    function setHostname($host)
1439
    {
1440
        if (!is_string($host)) {
1441
            return PEAR::raiseError("Hostname must be a string.", NET_FTP_ERR_HOSTNAMENOSTRING);
1442
        }
1443
        $this->_hostname = $host;
1444
        return true;
1445
    }
1446
 
1447
    /**
1448
     * Set the Port
1449
     *
1450
     * @access  public
1451
     * @param   int $port    The Port to set
1452
     * @return  bool True on success, otherwise PEAR::Error
1453
     * @see     NET_FTP_ERR_PORTLESSZERO
1454
     */
1455
 
1456
    function setPort($port)
1457
    {
1458
        if (!is_int($port) || ($port < 0)) {
1459
            PEAR::raiseError("Invalid port. Has to be integer >= 0", NET_FTP_ERR_PORTLESSZERO);
1460
        }
1461
        $this->_port = $port;
1462
        return true;
1463
    }
1464
 
1465
    /**
1466
     * Set the Username
1467
     *
1468
     * @access  public
1469
     * @param   string $user The Username to set
1470
     * @return  mixed True on success, otherwise PEAR::Error
1471
     * @see     NET_FTP_ERR_USERNAMENOSTRING
1472
     */
1473
 
1474
    function setUsername($user)
1475
    {
1476
        if (empty($user) || !is_string($user)) {
1477
            return PEAR::raiseError('Username $user invalid.', NET_FTP_ERR_USERNAMENOSTRING);
1478
        }
1479
        $this->_username = $user;
1480
    }
1481
 
1482
    /**
1483
     * Set the Password
1484
     *
1485
     * @access  private
1486
     * @param   string $password  The Password to set
1487
     * @return  void
1488
     * @see     NET_FTP_ERR_PASSWORDNOSTRING
1489
     */
1490
 
1491
    function setPassword($password)
1492
    {
1493
        if (empty($password) || !is_string($password)) {
1494
            return PEAR::raiseError('Password xxx invalid.', NET_FTP_ERR_PASSWORDNOSTRING);
1495
        }
1496
        $this->_password = $password;
1497
    }
1498
 
1499
    /**
1500
     * Set the transfer-mode. You can use the predefined constants
1501
     * FTP_ASCII or FTP_BINARY. The mode will be stored for any further transfers.
1502
     *
1503
     * @access  public
1504
     * @param   int    $mode  The mode to set
1505
     * @return  mixed         True on success, otherwise PEAR::Error
1506
     * @see     NET_FTP_ERR_NOMODECONST
1507
     */
1508
 
1509
    function setMode($mode)
1510
    {
1511
        if (($mode == FTP_ASCII) || ($mode == FTP_BINARY)) {
1512
            $this->_mode = $mode;
1513
            return true;
1514
        } else {
1515
            return $this->raiseError('FTP-Mode has either to be FTP_ASCII or FTP_BINARY', NET_FTP_ERR_NOMODECONST);
1516
        }
1517
    }
1518
 
1519
    /**
1520
     * Set the transfer-method to passive mode
1521
     *
1522
     * @access  public
1523
     * @return  void
1524
     */
1525
 
1526
    function setPassive()
1527
    {
1528
        $this->_passv = true;
1529
        @ftp_pasv($this->_handle, true);
1530
    }
1531
 
1532
    /**
1533
     * Set the transfer-method to active mode
1534
     *
1535
     * @access  public
1536
     * @return  void
1537
     */
1538
 
1539
    function setActive()
1540
    {
1541
        $this->_passv = false;
1542
        @ftp_pasv($this->_handle, false);
1543
    }
1544
 
1545
    /**
1546
     * Set the timeout for FTP operations
1547
     * Use this method to set a timeout for FTP operation. Timeout has to be an integer.
1548
     *
1549
     * @acess   public
1550
     * @param   int $timeout the timeout to use
1551
     * @return  bool True on success, otherwise PEAR::Error
1552
     * @see     NET_FTP_ERR_TIMEOUTLESSZERO, NET_FTP_ERR_SETTIMEOUT_FAILED
1553
     */
1554
 
1555
    function setTimeout ( $timeout = 0 )
1556
    {
1557
        if (!is_int($timeout) || ($timeout < 0)) {
1558
            return PEAR::raiseError("Timeout $timeout is invalid, has to be an integer >= 0", NET_FTP_ERR_TIMEOUTLESSZERO);
1559
        }
1560
        $this->_timeout = $timeout;
1561
        if (isset($this->_handle) && is_resource($this->_handle)) {
1562
            $res = @ftp_set_option($this->_handle, FTP_TIMEOUT_SEC, $timeout);
1563
        } else {
1564
            $res = true;
1565
        }
1566
        if (!$res) {
1567
            return PEAR::raiseError("Set timeout failed.", NET_FTP_ERR_SETTIMEOUT_FAILED);
1568
        }
1569
        return true;
1570
    }
1571
 
1572
    /**
1573
     * Adds an extension to a mode-directory
1574
     * The mode-directory saves file-extensions coresponding to filetypes
1575
     * (ascii e.g.: 'php', 'txt', 'htm',...; binary e.g.: 'jpg', 'gif', 'exe',...).
1576
     * The extensions have to be saved without the '.'. And
1577
     * can be predefined in an external file (see: getExtensionsFile()).
1578
     *
1579
     * The array is build like this: 'php' => FTP_ASCII, 'png' => FTP_BINARY
1580
     *
1581
     * To change the mode of an extension, just add it again with the new mode!
1582
     *
1583
     * @access  public
1584
     * @param   int    $mode  Either FTP_ASCII or FTP_BINARY
1585
     * @param   string $ext   Extension
1586
     * @return  void
1587
     */
1588
 
1589
    function addExtension($mode, $ext)
1590
    {
1591
        $this->_file_extensions[$ext] = $mode;
1592
    }
1593
 
1594
    /**
1595
     * This function removes an extension from the mode-directories
1596
     * (described above).
1597
     *
1598
     * @access  public
1599
     * @param   string $ext  The extension to remove
1600
     * @return  void
1601
     */
1602
 
1603
    function removeExtension($ext)
1604
    {
1605
        unset($this->_file_extensions[$ext]);
1606
    }
1607
 
1608
    /**
1609
     * This get's both (ascii- and binary-mode-directories) from the given file.
1610
     * Beware, if you read a file into the mode-directory, all former set values
1611
     * will be unset!
1612
     *
1613
     * @access  public
1614
     * @param   string $filename  The file to get from
1615
     * @return  mixed             True on success, otherwise PEAR::Error
1616
     * @see     NET_FTP_ERR_EXTFILENOTEXIST, NET_FTP_ERR_EXTFILEREAD_FAILED
1617
     */
1618
 
1619
    function getExtensionsFile($filename)
1620
    {
1621
        if (!file_exists($filename)) {
1622
            return $this->raiseError("Extensions-file '$filename' does not exist", NET_FTP_ERR_EXTFILENOTEXIST);
1623
        }
1624
 
1625
        if (!is_readable($filename)) {
1626
            return $this->raiseError("Extensions-file '$filename' is not readable", NET_FTP_ERR_EXTFILEREAD_FAILED);
1627
        }
1628
 
1629
        $this->_file_extension = @parse_ini_file($filename);
1630
        return true;
1631
    }
1632
 
1633
    /**
1634
     * Returns the Hostname
1635
     *
1636
     * @access  public
1637
     * @return  string  The Hostname
1638
     */
1639
 
1640
    function getHostname()
1641
    {
1642
        return $this->_hostname;
1643
    }
1644
 
1645
    /**
1646
     * Returns the Port
1647
     *
1648
     * @access  public
1649
     * @return  int     The Port
1650
     */
1651
 
1652
    function getPort()
1653
    {
1654
        return $this->_port;
1655
    }
1656
 
1657
    /**
1658
     * Returns the Username
1659
     *
1660
     * @access  public
1661
     * @return  string  The Username
1662
     */
1663
 
1664
    function getUsername()
1665
    {
1666
        return $this->_username;
1667
    }
1668
 
1669
    /**
1670
     * Returns the Password
1671
     *
1672
     * @access  public
1673
     * @return  string  The Password
1674
     */
1675
 
1676
    function getPassword()
1677
    {
1678
        return $this->_password;
1679
    }
1680
 
1681
    /**
1682
     * Returns the Transfermode
1683
     *
1684
     * @access  public
1685
     * @return  int     The transfermode, either FTP_ASCII or FTP_BINARY.
1686
     */
1687
 
1688
    function getMode()
1689
    {
1690
        return $this->_mode;
1691
    }
1692
 
1693
    /**
1694
     * Returns, whether the connection is set to passive mode or not
1695
     *
1696
     * @access  public
1697
     * @return  bool    True if passive-, false if active-mode
1698
     */
1699
 
1700
    function isPassive()
1701
    {
1702
        return $this->_passv;
1703
    }
1704
 
1705
    /**
1706
     * Returns the mode set for a file-extension
1707
     *
1708
     * @access  public
1709
     * @param   string   $ext    The extension you wanna ask for
1710
     * @return  int              Either FTP_ASCII, FTP_BINARY or NULL (if not set a mode for it)
1711
     */
1712
 
1713
    function getExtensionMode($ext)
1714
    {
1715
        return @$this->_file_extensions[$ext];
1716
    }
1717
 
1718
    /**
1719
     * Get the currently set timeout.
1720
     * Returns the actual timeout set.
1721
     *
1722
     * @access public
1723
     * @return int The actual timeout
1724
     */
1725
 
1726
    function getTimeout ( )
1727
    {
1728
        return ftp_get_option($this->_handle, FTP_TIMEOUT_SEC);
1729
    }
1730
 
1731
    /**
1732
     * Adds a Net_FTP_Observer instance to the list of observers
1733
     * that are listening for messages emitted by this Net_FTP instance.
1734
     *
1735
     * @param   object   $observer     The Net_FTP_Observer instance to attach
1736
     *                                 as a listener.
1737
     * @return  boolean                True if the observer is successfully attached.
1738
     * @access  public
1739
     * @since   1.3
1740
     */
1741
 
1742
    function attach(&$observer)
1743
    {
1744
        if (!is_a($observer, 'Net_FTP_Observer')) {
1745
            return false;
1746
        }
1747
 
1748
        $this->_listeners[$observer->getId()] = &$observer;
1749
        return true;
1750
    }
1751
 
1752
    /**
1753
     * Removes a Net_FTP_Observer instance from the list of observers.
1754
     *
1755
     * @param   object   $observer     The Net_FTP_Observer instance to detach
1756
     *                                 from the list of listeners.
1757
     * @return  boolean                True if the observer is successfully detached.
1758
     * @access  public
1759
     * @since   1.3
1760
     */
1761
 
1762
    function detach($observer)
1763
    {
1764
        if (!is_a($observer, 'Net_FTP_Observer') ||
1765
            !isset($this->_listeners[$observer->getId()])) {
1766
            return false;
1767
        }
1768
 
1769
        unset($this->_listeners[$observer->getId()]);
1770
        return true;
1771
    }
1772
 
1773
    /**
1774
     * Informs each registered observer instance that a new message has been
1775
     * sent.
1776
     *
1777
     * @param   mixed     $event       A hash describing the net event.
1778
     * @access  private
1779
     * @since   1.3
1780
     */
1781
 
1782
    function _announce($event)
1783
    {
1784
        foreach ($this->_listeners as $id => $listener) {
1785
            $this->_listeners[$id]->notify($event);
1786
        }
1787
    }
1788
 
1789
        /**
1790
     * Rebuild the path, if given relative
1791
     *
1792
     * @access  private
1793
     * @param   string $path   The path to check and construct
1794
     * @return  string         The build path
1795
     */
1796
 
1797
    function _construct_path($path)
1798
    {
1799
        if ((substr($path, 0, 1) != "/") && (substr($path, 0, 2) != "./")) {
1800
            $actual_dir = @ftp_pwd($this->_handle);
1801
            if (substr($actual_dir, (strlen($actual_dir) - 2), 1) != "/") {
1802
                $actual_dir .= "/";
1803
            }
1804
            $path = $actual_dir.$path;
1805
        }
1806
        return $path;
1807
    }
1808
 
1809
    /**
1810
     * Checks, whether a given string is a directory-path (ends with "/") or not.
1811
     *
1812
     * @access  private
1813
     * @param   string $path  Path to check
1814
     * @return  bool          True if $path is a directory, otherwise false
1815
     */
1816
 
1817
    function _check_dir($path)
1818
    {
1819
        if (substr($path, (strlen($path) - 1), 1) == "/") {
1820
            return true;
1821
        } else {
1822
            return false;
1823
        }
1824
    }
1825
 
1826
    /**
1827
     * This will remove a file
1828
     *
1829
     * @access  private
1830
     * @param   string $file   The file to delete
1831
     * @return  mixed          True on success, otherwise PEAR::Error
1832
     * @see     NET_FTP_ERR_DELETEFILE_FAILED
1833
     */
1834
 
1835
    function _rm_file($file)
1836
    {
1837
        if (substr($file, 0, 1) != "/") {
1838
            $actual_dir = @ftp_pwd($this->_handle);
1839
            if (substr($actual_dir, (strlen($actual_dir) - 2), 1) != "/") {
1840
                $actual_dir .= "/";
1841
            }
1842
            $file = $actual_dir.$file;
1843
        }
1844
        $res = @ftp_delete($this->_handle, $file);
1845
 
1846
        if (!$res) {
1847
            return $this->raiseError("Could not delete file '$file'.", NET_FTP_ERR_DELETEFILE_FAILED);
1848
        } else {
1849
            return true;
1850
        }
1851
    }
1852
 
1853
    /**
1854
     * This will remove a dir
1855
     *
1856
     * @access  private
1857
     * @param   string $dir  The dir to delete
1858
     * @return  mixed        True on success, otherwise PEAR::Error
1859
     * @see     NET_FTP_ERR_REMOTEPATHNODIR, NET_FTP_ERR_DELETEDIR_FAILED
1860
     */
1861
 
1862
    function _rm_dir($dir)
1863
    {
1864
        if (substr($dir, (strlen($dir) - 1), 1) != "/") {
1865
            return $this->raiseError("Directory name '$dir' is invalid, has to end with '/'", NET_FTP_ERR_REMOTEPATHNODIR);
1866
        }
1867
        $res = @ftp_rmdir($this->_handle, $dir);
1868
        if (!$res) {
1869
            return $this->raiseError("Could not delete directory '$dir'.", NET_FTP_ERR_DELETEDIR_FAILED);
1870
        } else {
1871
            return true;
1872
        }
1873
    }
1874
 
1875
    /**
1876
     * This will remove a dir and all subdirs and -files
1877
     *
1878
     * @access  private
1879
     * @param   string $file  The dir to delete recursively
1880
     * @return  mixed         True on success, otherwise PEAR::Error
1881
     * @see     NET_FTP_ERR_REMOTEPATHNODIR, NET_FTP_ERR_DELETEDIR_FAILED
1882
     */
1883
 
1884
    function _rm_dir_recursive($dir)
1885
    {
1886
        if (substr($dir, (strlen($dir) - 1), 1) != "/") {
1887
            return $this->raiseError("Directory name '$dir' is invalid, has to end with '/'", NET_FTP_ERR_REMOTEPATHNODIR);
1888
        }
1889
        $file_list = $this->_ls_files($dir);
1890
        foreach ($file_list as $file) {
1891
            $file = $dir.$file["name"];
1892
            $res = $this->rm($file);
1893
            if ($this->isError($res)) {
1894
                return $res;
1895
            }
1896
        }
1897
        $dir_list = $this->_ls_dirs($dir);
1898
        foreach ($dir_list as $new_dir) {
1899
            $new_dir = $dir.$new_dir["name"]."/";
1900
            $res = $this->_rm_dir_recursive($new_dir);
1901
            if ($this->isError($res)) {
1902
                return $res;
1903
            }
1904
        }
1905
        $res = $this->_rm_dir($dir);
1906
        if (PEAR::isError($res)) {
1907
            return $res;
1908
        } else {
1909
            return true;
1910
        }
1911
    }
1912
 
1913
    /**
1914
     * Lists up files and directories
1915
     *
1916
     * @access  private
1917
     * @param   string $dir  The directory to list up
1918
     * @return  array        An array of dirs and files
1919
     */
1920
 
1921
    function _ls_both($dir)
1922
    {
1923
        $list_splitted = $this->_list_and_parse($dir);
1924
        if (!is_array($list_splitted["files"])) {
1925
            $list_splitted["files"] = array();
1926
        }
1927
        if (!is_array($list_splitted["dirs"])) {
1928
            $list_splitted["dirs"] = array();
1929
        }
1930
        $res = array();
1931
        @array_splice($res, 0, 0, $list_splitted["files"]);
1932
        @array_splice($res, 0, 0, $list_splitted["dirs"]);
1933
        return $res;
1934
    }
1935
 
1936
    /**
1937
     * Lists up directories
1938
     *
1939
     * @access  private
1940
     * @param   string $dir  The directory to list up
1941
     * @return  array        An array of dirs
1942
     */
1943
 
1944
    function _ls_dirs($dir)
1945
    {
1946
        $list = $this->_list_and_parse($dir);
1947
        if (PEAR::isError($list)) {
1948
            return $list;
1949
        }
1950
        return $list["dirs"];
1951
    }
1952
 
1953
    /**
1954
     * Lists up files
1955
     *
1956
     * @access  private
1957
     * @param   string $dir  The directory to list up
1958
     * @return  array        An array of files
1959
     */
1960
 
1961
    function _ls_files($dir)
1962
    {
1963
        $list = $this->_list_and_parse($dir);
1964
        if (PEAR::isError($list)) {
1965
            return $list;
1966
        }
1967
        return $list["files"];
1968
    }
1969
 
1970
    /**
1971
     * This lists up the directory-content and parses the items into well-formated arrays
1972
     * The results of this array are sorted (dirs on top, sorted by name;
1973
     * files below, sorted by name).
1974
     *
1975
     * @access  private
1976
     * @param   string $dir  The directory to parse
1977
     * @return  array        Lists of dirs and files
1978
     * @see     NET_FTP_ERR_RAWDIRLIST_FAILED
1979
     */
1980
 
1981
    function _list_and_parse($dir)
1982
    {
1983
        $dirs_list = array();
1984
        $files_list = array();
1985
        $dir_list = @ftp_rawlist($this->_handle, $dir);
1986
        if ($dir_list === false) {
1987
            return PEAR::raiseError('Could not get raw directory listing.', NET_FTP_ERR_RAWDIRLIST_FAILED);
1988
        }
1989
        if (!isset($this->_matcher) || PEAR::isError($this->_matcher)) {
1990
            $this->_matcher = $this->_determine_os_match($dir_list);
1991
            if (PEAR::isError($this->_matcher)) {
1992
                return $this->_matcher;
1993
            }
1994
        }
1995
        foreach ($dir_list as $entry) {
1996
            if (!preg_match($this->_matcher['pattern'], $entry, $m)) {
1997
                continue;
1998
            }
1999
            $entry = array();
2000
            foreach ($this->_matcher['map'] as $key=>$val) {
2001
                $entry[$key] = $m[$val];
2002
            }
2003
            $entry['stamp'] = $this->_parse_Date($entry['date']);
2004
 
2005
            if ($entry['is_dir']) {
2006
                $dirs_list[] = $entry;
2007
            } else {
2008
                $files_list[] = $entry;
2009
            }
2010
        }
2011
        @usort($dirs_list, array("Net_FTP", "_nat_sort"));
2012
        @usort($files_list, array("Net_FTP", "_nat_sort"));
2013
        $res["dirs"] = (is_array($dirs_list)) ? $dirs_list : array();
2014
        $res["files"] = (is_array($files_list)) ? $files_list : array();
2015
        return $res;
2016
    }
2017
 
2018
    /**
2019
     * Determine server OS
2020
     * This determines the server OS and returns a valid regex to parse
2021
     * ls() output.
2022
     *
2023
     * @access  private
2024
     * @param   array $dir_list The raw dir list to parse
2025
     * @return  mixed An array of 'pattern' and 'map' on success, otherwise PEAR::Error
2026
     * @see     NET_FTP_ERR_DIRLIST_UNSUPPORTED
2027
     */
2028
 
2029
    function _determine_os_match(&$dir_list) {
2030
    foreach ($dir_list as $entry) {
2031
        foreach ($this->_ls_match as $os => $match) {
2032
            if (preg_match($match['pattern'], $entry)) {
2033
                    return $match;
2034
                }
2035
            }
2036
    }
2037
        $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!';
2038
        return PEAR::raiseError($error, NET_FTP_ERR_DIRLIST_UNSUPPORTED);
2039
    }
2040
    /**
2041
     * Lists a local directory
2042
     *
2043
     * @access  private
2044
     * @param   string $dir_path  The dir to list
2045
     * @return  array             The list of dirs and files
2046
     */
2047
 
2048
    function _ls_local($dir_path)
2049
    {
2050
        $dir = dir($dir_path);
2051
        $dir_list = array();
2052
        $file_list = array();
2053
        while (false !== ($entry = $dir->read())) {
2054
            if (($entry != '.') && ($entry != '..')) {
2055
                if (is_dir($dir_path.$entry)) {
2056
                    $dir_list[] = $entry;
2057
                } else {
2058
                    $file_list[] = $entry;
2059
                }
2060
            }
2061
        }
2062
        $dir->close();
2063
        $res['dirs'] = $dir_list;
2064
        $res['files'] = $file_list;
2065
        return $res;
2066
    }
2067
 
2068
    /**
2069
     * Function for use with usort().
2070
     * Compares the list-array-elements by name.
2071
     *
2072
     * @access  private
2073
     */
2074
 
2075
    function _nat_sort($item_1, $item_2)
2076
    {
2077
        return strnatcmp($item_1['name'], $item_2['name']);
2078
    }
2079
 
2080
    /**
2081
     * Parse dates to timestamps
2082
     *
2083
     * @access  private
2084
     * @param   string $date  Date
2085
     * @return  int           Timestamp
2086
     * @see     NET_FTP_ERR_DATEFORMAT_FAILED
2087
     */
2088
 
2089
    function _parse_Date($date)
2090
    {
2091
        // Sep 10 22:06 => Sep 10, <year> 22:06
2092
        if (preg_match('/([A-Za-z]+)[ ]+([0-9]+)[ ]+([0-9]+):([0-9]+)/', $date, $res)) {
2093
            $year = date('Y');
2094
            $month = $res[1];
2095
            $day = $res[2];
2096
            $hour = $res[3];
2097
            $minute = $res[4];
2098
            $date = "$month $day, $year $hour:$minute";
2099
            $tmpDate = strtotime($date);
2100
            if ($tmpDate > time()) {
2101
                $year--;
2102
                $date = "$month $day, $year $hour:$minute";
2103
            }
2104
        }
2105
        // 09-10-04 => 09/10/04
2106
        elseif (preg_match('/^\d\d-\d\d-\d\d/',$date)) {
2107
            $date = str_replace('-','/',$date);
2108
        }
2109
        $res = strtotime($date);
2110
        if (!$res) {
2111
            return $this->raiseError('Dateconversion failed.', NET_FTP_ERR_DATEFORMAT_FAILED);
2112
        }
2113
        return $res;
2114
    }
2115
}
2116
?>