/home/mjc1/public_html/html/millennium/php-sftp-master/src/Hug/Sftp/Sftp.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
<?php

namespace Hug\Sftp;

use 
Exception;
use 
Hug\HString\HString as HString;
use 
phpseclib\Net\SFTP as SecFtp;

/**
 *
 */
class Sftp
{
    
/**
     * @const int port
     */
    
const port 22;

    
/**
     * @const int timeout
     */
    
const timeout 10;

    
/**
     * Login to SFTP server
     *
     * @param string $server 
     * @param string $user
     * @param string $password
     * @param int $port
     * @param int $timeout
     *
     * @return resource $sftp
     *
     */
    
private static function login($server$user$password$port self::port$timeout self::timeout)
    {
        
$sftp false;
        
        try
        {
            
$sftp = new SecFtp($server$port$timeout);
            if(!
$sftp->login($user$password))
            {
                
$sftp false;
            }
        }
        catch(
Exception $e)
        {
            
error_log("SFtp::login : " $e->getMessage());
        }

        return 
$sftp;
    }

    
/**
     * Test SFTP connection
     *
     * @param string $server 
     * @param string $user
     * @param string $password
     * @param int $port
     * @param int $timeout
     *
     * @return bool $test
     *
     */
    
public static function test($server$user$password$port self::port$timeout self::timeout)
    {
        
$test false;

        if(
false !== $sftp Sftp::login($server$user$password$port$timeout))
        {
            
$test true;
        }

        return 
$test;
    }

    
/**
     * Check if a file exists on SFTP Server
     * 
     * @param string $server 
     * @param string $user
     * @param string $password
     * @param string $remote_file
     * @param int $port
     * @param int $timeout
     *
     * @return bool $is_file
     */
    
public static function is_file($server$user$password$remote_file$port self::port$timeout self::timeout)
    {
        
$is_file false;
        try
        {
            if(
false !== $sftp Sftp::login($server$user$password$port$timeout))
            {
                if(
$sftp->is_file($remote_file))
                {
                    
$is_file true;
                }
            }
        }
        catch(
Exception $e)
        {
            
error_log("Sftp::is_file : " $e->getMessage());
        }

        return 
$is_file;
    }

    
/**
     * Delete a file on remote SFTP server
     *
     * @param string $server 
     * @param string $user
     * @param string $password
     * @param string $remote_file
     * @param int $port
     * @param int $timeout
     *
     * @return bool $deleted
     *
     */
    
public static function delete($server$user$password$remote_file$port self::port$timeout self::timeout)
    {
        
$deleted false;

        try
        {
            if(
false !== $sftp Sftp::login($server$user$password$port$timeout))
            {
                if(
$sftp->is_file($remote_file))
                {
                    if(
$sftp->delete($remote_file))
                    {
                        
$deleted true;
                    }
                }
            }
        }
        catch(
Exception $e)
        {
            
error_log("Sft::delete : " $e->getMessage());
        }

        return 
$deleted;
    }

    
/**
     * Recursively deletes files and folder in given directory
     *
     * If remote_path ends with a slash delete folder content
     * otherwise delete folder itself
     *
     * @param string $server 
     * @param string $user
     * @param string $password
     * @param string $remote_path
     * @param int $port
     * @param int $timeout
     *
     * @return bool $deleted
     *
     */
    
public static function rmdir($server$user$password$remote_path$port self::port$timeout self::timeout)
    {
        
$deleted false;

        try
        {
            if(
false !== $sftp Sftp::login($server$user$password$port$timeout))
            {
                
# Delete directory content
                
if(Sftp::clean_dir($remote_path$sftp))
                {
                    
# If remote_path do not ends with /
                    
if(!HString::ends_with($remote_path'/'))
                    {
                        
# Delete directory itself
                        
if($sftp->rmdir($remote_path))
                        {
                            
$deleted true;
                        }
                    }
                    else
                    {
                        
$deleted true;
                    }
                }
            }
        }
        catch(
Exception $e)
        {
            
error_log("Sftp::rmdir : " $e->getMessage());
        }

        return 
$deleted;
    }

    
/**
     * Recursively deletes files and folder
     *
     * @param string $remote_path
     * @param resource $sftp
     *
     * @return bool $clean
     */
    
private static function clean_dir($remote_path$sftp)
    {
        
$clean false;

        
$to_delete 0;
        
$deleted 0;

        
$list $sftp->nlist($remote_path);
        foreach (
$list as $element)
        {
            if(
$element!=='.' && $element!=='..')
            {
                
$to_delete++;
                
                if(
$sftp->is_dir($remote_path DIRECTORY_SEPARATOR $element))
                {
                    
# Empty directory
                    
Sftp::clean_dir($remote_path DIRECTORY_SEPARATOR $element$sftp);

                    
# Delete empty directory
                    
if($sftp->rmdir($remote_path DIRECTORY_SEPARATOR $element)) 
                    {
                        
$deleted++;
                    }
                }
                else
                {
                    
# Delete file
                    
if($sftp->delete($remote_path DIRECTORY_SEPARATOR $element))
                    {
                        
$deleted++;
                    }
                }
            }
        }

        if(
$deleted===$to_delete)
        {
            
$clean true;   
        }

        return 
$clean;
    }

    
/**
     * Recursively copy files and folders on remote SFTP server
     *
     * If local_path ends with a slash upload folder content
     * otherwise upload folder itself
     *
     * @param string $server 
     * @param string $user
     * @param string $password
     * @param string $local_path
     * @param string $remote_path
     * @param int $port
     * @param int $timeout
     *
     * @return bool $uploaded
     *
     */
    
public static function upload_dir($server$user$password$local_path$remote_path$port self::port$timeout self::timeout)
    {
        
$uploaded false;

        try
        {
            
# Remove trailing slash
             
$remote_path rtrim($remote_pathDIRECTORY_SEPARATOR);

            if(
false !== $sftp Sftp::login($server$user$password$port$timeout))
            {
                
# If local_path do not ends with /
                
if(!HString::ends_with($local_path'/'))
                {
                     
# Create fisrt level directory on remote filesystem
                    
$remote_path $remote_path DIRECTORY_SEPARATOR basename($local_path);
                    
$sftp->mkdir($remote_path);
                }

                if(
$sftp->is_dir($remote_path))
                {
                    
$uploaded Sftp::upload_all($sftp$local_path$remote_path);
                }
            }
        }
        catch(
Exception $e)
        {
            
error_log("Sftp::upload_dir : " $e->getMessage());
        }

        return 
$uploaded;
    }

    
/**
     * Recursively copy files and folders on remote SFTP server
     *
     * @param SFTP $sftp
     * @param string $local_dir
     * @param string $remote_dir
     *
     * @return bool $uploaded_all
     *
     */
    
private static function upload_all($sftp$local_dir$remote_dir)
    {
        
$uploaded_all false;
        try
        {
            
# Create remote directory
            
if(!$sftp->is_dir($remote_dir))
            {
                if(!
$sftp->mkdir($remote_dir))
                {
                    throw new 
Exception("Cannot create remote directory."1);
                }
            }

            
$to_upload 0;
            
$uploaded 0;

            
$d dir($local_dir);
            while(
$file $d->read())
            {
                if (
$file != "." && $file != "..")
                {
                    
$to_upload++;

                    if(
is_dir($local_dir DIRECTORY_SEPARATOR $file))
                    {
                        
# Upload directory
                        # Recursive part
                        
if(Sftp::upload_all(
                            
$sftp
                            
$local_dir DIRECTORY_SEPARATOR $file
                            
$remote_dir DIRECTORY_SEPARATOR $file))
                        {
                            
$uploaded++;
                        }
                    }
                    else
                    {
                        
# Upload file
                        
if($sftp->put(
                            
$remote_dir DIRECTORY_SEPARATOR $file,
                            
$local_dir DIRECTORY_SEPARATOR $file,
                            
SecFtp::SOURCE_LOCAL_FILE))
                        {
                            
$uploaded++;
                        }
                    } 
                }
            }
            
$d->close();

            if(
$to_upload===$uploaded)
            {
                
$uploaded_all true;
            }
        }
        catch(
Exception $e)
        {
            
error_log("Sftp::upload_all : " $e->getMessage());
        }

        return 
$uploaded_all;
    }

    
/**
     * Download a file from remote SFTP server
     *
     * @param string $server 
     * @param string $user
     * @param string $password
     * @param string $remote_file
     * @param string $local_file
     * @param int $port
     * @param int $timeout
     *
     * @return bool $downloaded
     *
     */
    
public static function download($server$user$password$remote_file$local_file$port self::port$timeout self::timeout)
    {
        
$downloaded false;

        try
        {
            if(
false !== $sftp Sftp::login($server$user$password$port$timeout))
            {
                
# Download File
                
if($sftp->get($remote_file$local_file))
                {
                    
$downloaded true;
                }
            }
        }
        catch(
Exception $e)
        {
            
error_log("Sftp::download : " $e->getMessage());
        }

        return 
$downloaded;
    }

    
/**
     * Download a directory from remote SFTP server
     *
     * If remote_dir ends with a slash download folder content
     * otherwise download folder itself
     *
     * @param string $server 
     * @param string $user
     * @param string $password
     * @param string $remote_dir
     * @param string $local_dir
     * @param int $port
     * @param int $timeout
     *
     * @return bool $downloaded
     *
     */
    
public static function download_dir($server$user$password$remote_dir$local_dir$port self::port$timeout self::timeout)
    {
        
$downloaded false;

        try
        {
            if(
is_dir($local_dir) && is_writable($local_dir))
            {
                if(
false !== $sftp Sftp::login($server$user$password$port$timeout))
                {
                    
# If remote_dir do not ends with /
                    
if(!HString::ends_with($remote_dir'/'))
                    {
                         
# Create fisrt level directory on local filesystem
                        
$local_dir rtrim($local_dirDIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR basename($remote_dir);
                        
mkdir($local_dir);
                    }
                    
                    
# Remove trailing slash
                     
$local_dir rtrim($local_dirDIRECTORY_SEPARATOR);

                    
# Recursive part
                    
$downloaded Sftp::download_all($sftp$remote_dir$local_dir);
                }
            }
            else
            {
                throw new 
Exception("Local directory does not exist or is not writable"1);
            }
        }
        catch(
Exception $e)
        {
            
error_log("Sftp::download_dir : " $e->getMessage());
        }

        return 
$downloaded;
    }

    
/**
     * Recursive function to download remote files
     *
     * @param ressource $sftp
     * @param string $remote_dir
     * @param string $local_dir
     *
     * @return bool $downloaded
     *
     */
    
private static function download_all($sftp$remote_dir$local_dir)
    {
        
$download_all false;
        
        try
        {
            if(
$sftp->is_dir($remote_dir))
            {
                
$files $sftp->nlist($remote_dir);
                if(
$files!==false)
                {
                    
$to_download 0;
                    
$downloaded 0;
                    
# do this for each file in the remote directory 
                    
foreach ($files as $file)
                    {
                        
// error_log('file : ' . $file);
                        # To prevent an infinite loop 
                        
if ($file != "." && $file != "..")
                        {
                            
$to_download++;
                            
# do the following if it is a directory 
                            
if ($sftp->is_dir($remote_dir DIRECTORY_SEPARATOR $file))
                            {                                
                                
# Create directory on local filesystem
                                
mkdir($local_dir DIRECTORY_SEPARATOR basename($file));
                                
                                
# Recursive part 
                                
if(Sftp::download_all($sftp$remote_dir DIRECTORY_SEPARATOR .$file$local_dir DIRECTORY_SEPARATOR basename($file)))
                                {
                                    
$downloaded++;
                                }
                            }
                            else
                            { 
                                
# Download files 
                                
if($sftp->get($remote_dir DIRECTORY_SEPARATOR $file$local_dir DIRECTORY_SEPARATOR basename($file)))
                                {
                                    
$downloaded++;
                                }
                            } 
                        }
                    }

                    
# Check all files and folders have been downloaded
                    
if($to_download===$downloaded)
                    {
                        
$download_all true;
                    }
                }
                else
                {
                    
# Nothing to download
                    
$download_all true;
                }
            }
        }
        catch(
Exception $e)
        {
            
error_log("Sftp::download_all : " $e->getMessage());
        }

        return 
$download_all;
    }

    
/**
     * Rename a file on remote SFTP server
     *
     * @param string $server 
     * @param string $user
     * @param string $password
     * @param string $current_filename
     * @param string $new_filename
     * @param int $port
     * @param int $timeout
     *
     * @return bool $renamed
     *
     */
    
public static function rename($server$user$password$current_filename$new_filename$port self::port$timeout self::timeout)
    {
        
$renamed false;

        try
        {
            if(
false !== $sftp Sftp::login($server$user$password$port$timeout))
            {
                if(
$sftp->rename($current_filename$new_filename))
                {
                    
$renamed true;
                }
            }
        }
        catch(
Exception $e)
        {
            
error_log("Sftp::rename : " $e->getMessage());
        }

        return 
$renamed;
    }

    
/**
     * Create a directory on remote SFTP server
     *
     * @param string $server 
     * @param string $user
     * @param string $password
     * @param string $directory
     * @param int $port
     * @param int $timeout
     *
     * @return bool $created
     *
     */
    
public static function mkdir($server$user$password$directory$port self::port$timeout self::timeout)
    {
        
$created false;

        try
        {
            if(
false !== $sftp Sftp::login($server$user$password$port$timeout))
            {
                if(
$sftp->mkdir($directorytrue))
                {
                    
$created true;
                }
            }
        }
        catch(
Exception $e)
        {
            
error_log("Sftp::mkdir : " $e->getMessage());
        }

        return 
$created;
    }

    
/**
     * Create and fill in a file on remote SFTP server
     *
     * @param string $server 
     * @param string $user
     * @param string $password
     * @param string $remote_file
     * @param int $port
     * @param int $timeout
     *
     * @return bool $content
     *
     */
    
public static function touch($server$user$password$remote_file$content ''$port self::port$timeout self::timeout)
    {
        
$created false;

        try
        {
            if(
false !== $sftp Sftp::login($server$user$password$port$timeout))
            {
                
# Create temp file
                
$local_file tmpfile();
                
fwrite($local_file$content);
                
fseek($local_file0);
                if(
$sftp->put($remote_file$local_file,  SecFtp::SOURCE_LOCAL_FILE))
                {
                    
$created true;
                }
                
fclose($local_file);
            } 
        }
        catch(
Exception $e)
        {
            
error_log("Sftp::touch : " $e->getMessage());
        }

        return 
$created;
    }

    
/**
     * Upload a file on SFTP server
     *
     * @param string $server 
     * @param string $user
     * @param string $password
     * @param string $local_file
     * @param string $remote_file
     * @param int $port
     * @param int $timeout
     *
     * @return bool $uploaded
     *
     */
    
public static function upload($server$user$password$local_file$remote_file$port self::port$timeout self::timeout)
    {
        
$uploaded false;
        
        try
        {
            if(
false !== $sftp Sftp::login($server$user$password$port$timeout))
            {
                if(
$sftp->put($remote_file$local_fileSecFtp::SOURCE_LOCAL_FILE))
                {
                    
$uploaded true;
                }
            }
        }
        catch(
Exception $e)
        {
            
error_log("Sftp::upload : " $e->getMessage());
        }

        return 
$uploaded;
    }

    
/**
     * List files in given directory on SFTP server
     *
     * @param string $server 
     * @param string $user
     * @param string $password
     * @param string $path
     * @param int $port
     * @param int $timeout
     *
     * @return array $files Files listed in directory or false
     *
     */
    
public static function scandir($server$user$password$path$port self::port$timeout self::timeout)
    {
        
$files false;

        if(
false !== $sftp Sftp::login($server$user$password$port$timeout))
        {
            
$files $sftp->nlist($path);
        }
        if(
is_array($files))
        {
            
# Removes . and ..
            
$files array_diff($files, ['.''..']);
        }

        return 
$files;
    }

    
/**
     * Get default login SFTP directory aka pwd
     *
     * @param string $server 
     * @param string $user
     * @param string $password
     * @param int $port
     * @param int $timeout
     *
     * @return string $dir Print Working Directory or false
     *
     */
    
public static function pwd($server$user$password$port self::port$timeout self::timeout)
    {
        
$dir false;

        if(
false !== $sftp Sftp::login($server$user$password$port$timeout))
        {
            
$dir $sftp->pwd();
        } 

        return 
$dir;
    }

}