"Fossies" - the Fresh Open Source Software Archive 
Member "fogproject-1.5.9/packages/web/lib/fog/fogftp.class.php" (13 Sep 2020, 23565 Bytes) of package /linux/misc/fogproject-1.5.9.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the latest
Fossies "Diffs" side-by-side code changes report for "fogftp.class.php":
1.5.8_vs_1.5.9.
1 <?php
2 /**
3 * Handles FTP connections and operations for FOG
4 *
5 * PHP version 5
6 *
7 * @category FOGFTP
8 * @package FOGProject
9 * @author Tom Elliott <tommygunsster@gmail.com>
10 * @license http://opensource.org/licenses/gpl-3.0 GPLv3
11 * @link https://fogproject.org
12 */
13 /**
14 * Handles FTP connections and operations for FOG
15 *
16 * @category FOGFTP
17 * @package FOGProject
18 * @author Tom Elliott <tommygunsster@gmail.com>
19 * @license http://opensource.org/licenses/gpl-3.0 GPLv3
20 * @link https://fogproject.org
21 */
22 class FOGFTP extends FOGGetSet
23 {
24 /**
25 * The default data layout
26 *
27 * @var array
28 */
29 protected $data = array(
30 'host' => '',
31 'username' => '',
32 'password' => '',
33 'port' => 21,
34 'timeout' => 90,
35 'passive' => true,
36 'mode' => FTP_BINARY,
37 );
38 /**
39 * The link to the FTP server
40 *
41 * @var resource
42 */
43 private $_link;
44 /**
45 * The connection hash
46 *
47 * @var string
48 */
49 private $_lastConnectionHash;
50 /**
51 * The last login hash
52 *
53 * @var string
54 */
55 private $_lastLoginHash;
56 /**
57 * The current connection hash
58 *
59 * @var string
60 */
61 private $_currentConnectionHash;
62 /**
63 * The current login hash
64 *
65 * @var string
66 */
67 private $_currentLoginHash;
68 /**
69 * Destroy the ftp object
70 *
71 * @return void
72 */
73 public function __destruct()
74 {
75 $this->close();
76 }
77 /**
78 * FTP Alloc as a method
79 *
80 * @param double|int $filesize the filesize to allocate
81 * @param mixed $result the result to allocate for
82 *
83 * @return ftp_alloc
84 */
85 public function alloc(
86 $filesize,
87 &$result
88 ) {
89 return ftp_alloc(
90 $this->_link,
91 $filesize,
92 $result
93 );
94 }
95 /**
96 * Change to parent directory
97 *
98 * @return ftp_cdup
99 */
100 public function cdup()
101 {
102 return ftp_cdup($this->_link);
103 }
104 /**
105 * Change directory as requested
106 *
107 * @param string $directory the directory to change to
108 *
109 * @return ftp_chdir
110 */
111 public function chdir($directory)
112 {
113 return ftp_chdir(
114 $this->_link,
115 $directory
116 );
117 }
118 /**
119 * Change permissions on directory
120 *
121 * @param string $mode the permissions/mode to set on file
122 * @param string $filename the file to change permissions on
123 *
124 * @return object
125 */
126 public function chmod(
127 $mode,
128 $filename
129 ) {
130 if (!$mode) {
131 $mode = $this->get('mode');
132 }
133 @ftp_chmod(
134 $this->_link,
135 $mode,
136 $filename
137 );
138 return $this;
139 }
140 /**
141 * Close the current ftp session
142 *
143 * @return object
144 */
145 public function close()
146 {
147 if ($this->_link) {
148 ftp_close($this->_link);
149 }
150 $this->_link = null;
151 return $this;
152 }
153 /**
154 * Connect to the ftp server
155 *
156 * @param string $host the host to connect to
157 * @param int $port the port to use
158 * @param int $timeout the timeout setting of the connection
159 * @param bool $autologin should we auto login
160 * @param string $connectmethod how to connect to the ftp server
161 *
162 * @return object
163 */
164 public function connect(
165 $host = '',
166 $port = 0,
167 $timeout = 90,
168 $autologin = true,
169 $connectmethod = 'ftp_connect'
170 ) {
171 try {
172 $this->_currentConnectionHash = password_hash(
173 serialize($this->data),
174 PASSWORD_BCRYPT,
175 ['cost'=>11]
176 );
177 if ($this->_link
178 && $this->_currentConnectionHash == $this->_lastConnectionHash
179 ) {
180 return $this;
181 }
182 if (!$host) {
183 $host = $this->get('host');
184 }
185 list(
186 $portOverride,
187 $timeoutOverride
188 ) = self::getSubObjectIDs(
189 'Service',
190 array(
191 'name' => array(
192 'FOG_FTP_PORT',
193 'FOG_FTP_TIMEOUT'
194 )
195 ),
196 'value',
197 false,
198 'AND',
199 'name',
200 false,
201 ''
202 );
203 if (!$port) {
204 if ($portOverride) {
205 $port = $portOverride;
206 } else {
207 $port = $this->get('port');
208 }
209 }
210 if (!$timeout) {
211 if ($timeoutOverride) {
212 $timeout = $timeoutOverride;
213 } else {
214 $timeout = $this->get('timeout');
215 }
216 }
217 $this->_link = $connectmethod($host, $port, $timeout);
218 if ($this->_link === false) {
219 trigger_error(_('FTP connection failed'), E_USER_NOTICE);
220 $this->ftperror($this->data);
221 }
222 if ($autologin) {
223 $this->login();
224 $this->pasv($this->get('passive'));
225 }
226 } catch (Exception $e) {
227 throw new Exception($e->getMessage());
228 }
229 $this->_lastConnectionHash = $this->_currentConnectionHash;
230 return $this;
231 }
232 /**
233 * Deletes the item passed
234 *
235 * @param string $path the item to delete
236 *
237 * @return object
238 */
239 public function delete($path)
240 {
241 if (!$this->exists($path)) {
242 return $this;
243 }
244 if (!$this->rmdir($path)
245 && !@ftp_delete($this->_link, $path)
246 ) {
247 $filelist = $this->nlist($path);
248 foreach ((array)$filelist as &$file) {
249 $this->delete($file);
250 unset($file);
251 }
252 $this->rmdir($path);
253 $rawfilelist = $this->rawlist("-a $path");
254 $path = trim($path, '/');
255 $path = trim($path);
256 foreach ((array)$rawfilelist as &$file) {
257 $chunk = preg_split("/\s+/", $file);
258 if (in_array($chunk[8], array('.', '..'))) {
259 continue;
260 }
261 $tmpfile = sprintf(
262 '/%s/%s',
263 $path,
264 $chunk[8]
265 );
266 $this->delete($tmpfile);
267 unset($file);
268 }
269 $this->delete($path);
270 }
271 return $this;
272 }
273 /**
274 * Execute command via ftp
275 *
276 * @param string $command the command to execute
277 *
278 * @return bool
279 */
280 public function exec($command)
281 {
282 return ftp_exec(
283 $this->_link,
284 escapeshellcmd($command)
285 );
286 }
287 /**
288 * Get specified portions of a file
289 *
290 * @param resource $handle the ftp resource
291 * @param string $remote_file the remote file
292 * @param int $mode mode of the connection
293 * @param int $resumepos the position to resume from
294 *
295 * @return ftp_fget
296 */
297 public function fget(
298 $handle,
299 $remote_file,
300 $mode = 0,
301 $resumepos = 0
302 ) {
303 if (!$mode) {
304 $mode = $this->get('mode');
305 }
306 if ($resumepos) {
307 return ftp_fget(
308 $this->_link,
309 $handle,
310 $remote_file,
311 $mode,
312 $resumepos
313 );
314 }
315 return ftp_fget(
316 $this->_link,
317 $handle,
318 $remote_file,
319 $mode
320 );
321 }
322 /**
323 * Get specified portions of a file
324 *
325 * @param string $remote_file the remote file
326 * @param resource $handle the ftp resource
327 * @param int $mode mode of the connection
328 * @param int $startpos the position to start at
329 *
330 * @return ftp_fget
331 */
332 public function fput(
333 $remote_file,
334 $handle,
335 $mode = 0,
336 $startpos = 0
337 ) {
338 if (!$mode) {
339 $mode = $this->get('mode');
340 }
341 if ($startpos) {
342 return ftp_fput(
343 $this->_link,
344 $remote_file,
345 $handle,
346 $mode,
347 $startpos
348 );
349 }
350 return ftp_fput(
351 $this->_link,
352 $remote_file,
353 $handle,
354 $mode
355 );
356 }
357 /**
358 * Returns the ftp error
359 *
360 * @param mixed $data the data info
361 *
362 * @throws Exception
363 * @return void
364 */
365 public function ftperror($data)
366 {
367 $error = error_get_last();
368 throw new Exception(
369 sprintf(
370 '%s: %s, %s: %s, %s: %s, %s: %s, %s: %s, %s: %s',
371 _('Type'),
372 $error['type'],
373 _('File'),
374 $error['file'],
375 _('Line'),
376 $error['line'],
377 _('Message'),
378 $error['message'],
379 _('Host'),
380 $data['host'],
381 _('Username'),
382 $data['username']
383 )
384 );
385 }
386 /**
387 * Get ftp options
388 *
389 * @param mixed $option the option to get
390 *
391 * @return ftp_get_option
392 */
393 public function getOption($option)
394 {
395 return ftp_get_option(
396 $this->_link,
397 $option
398 );
399 }
400 /**
401 * Pulls files, quite literally ftp_get but get is
402 * a common method that doesn't tie with with this get
403 *
404 * @param string $local_file the local file
405 * @param string $remote_file the remote file
406 * @param mixed $mode the mode to get file
407 * @param mixed $resumepos the position to continue from
408 *
409 * @return ftp_get
410 */
411 public function pull(
412 $local_file,
413 $remote_file,
414 $mode = 0,
415 $resumepos = 0
416 ) {
417 if (!$mode) {
418 $mode = $this->get('mode');
419 }
420 if ($resumepos) {
421 return ftp_get(
422 $this->_link,
423 $local_file,
424 $remote_file,
425 $mode,
426 $resumepos
427 );
428 }
429 return ftp_get(
430 $this->_link,
431 $local_file,
432 $remote_file,
433 $mode
434 );
435 }
436 /**
437 * Perform the login
438 *
439 * @param string $username the username to login with
440 * @param string $password the password to login with
441 *
442 * @throws Exception
443 * @return object
444 */
445 public function login(
446 $username = null,
447 $password = null
448 ) {
449 try {
450 $this->_currentLoginHash = password_hash(
451 serialize($this->_link),
452 PASSWORD_BCRYPT,
453 ['cost'=>11]
454 );
455 if ($this->_currentLoginHash == $this->_lastLoginHash) {
456 return $this;
457 }
458 if (!$username) {
459 $username = $this->get('username');
460 }
461 if (!$password) {
462 $password = $this->get('password');
463 }
464 $password = htmlspecialchars_decode($password, ENT_QUOTES | ENT_HTML401);
465 if (ftp_login($this->_link, $username, $password) === false) {
466 $this->ftperror($this->data);
467 }
468 } catch (Exception $e) {
469 throw new Exception($e->getMessage());
470 }
471 $this->_lastLoginHash = $this->_currentLoginHash;
472 return $this;
473 }
474 /**
475 * MDTM as a method
476 *
477 * @param string $remote_file the remote file
478 *
479 * @return ftp_mdtm
480 */
481 public function mdtm($remote_file)
482 {
483 return ftp_mdtm($this->_link, $remote_file);
484 }
485 /**
486 * Creates directory on ftp site
487 *
488 * @param string $directory the directory to make
489 *
490 * @return ftp_mkdir
491 */
492 public function mkdir($directory)
493 {
494 return ftp_mkdir($this->_link, $directory);
495 }
496 /**
497 * Continue non-blocking
498 *
499 * @return ftp_nb_continue
500 */
501 public function nb_continue()
502 {
503 return ftp_nb_continue($this->_link);
504 }
505 /**
506 * Fget non-blocking
507 *
508 * @param resource $handle the file handle local
509 * @param string $remote_file the file to get
510 * @param mixed $mode the mode to fget the file
511 * @param mixed $resumepos the position to continue from
512 *
513 * @return ftp_nb_fget
514 */
515 public function nb_fget(
516 $handle,
517 $remote_file,
518 $mode = 0,
519 $resumepos = 0
520 ) {
521 if (!$mode) {
522 $mode = $this->get('mode');
523 }
524 if ($resumepos) {
525 return ftp_nb_fget(
526 $this->_link,
527 $handle,
528 $remote_file,
529 $mode,
530 $resumepos
531 );
532 }
533 return ftp_nb_fget(
534 $this->_link,
535 $handle,
536 $remote_file,
537 $mode
538 );
539 }
540 /**
541 * Fput non-blocking
542 *
543 * @param string $remote_file the file to get
544 * @param resource $handle the file handle local
545 * @param mixed $mode the mode to fget the file
546 * @param mixed $startpos the position to continue from
547 *
548 * @return ftp_nb_fput
549 */
550 public function nb_fput(
551 $remote_file,
552 $handle,
553 $mode = 0,
554 $startpos = 0
555 ) {
556 if (!$mode) {
557 $mode = $this->get('mode');
558 }
559 if ($startpos) {
560 return ftp_nb_fput(
561 $this->_link,
562 $remote_file,
563 $handle,
564 $mode,
565 $startpos
566 );
567 }
568 return ftp_nb_fput(
569 $this->_link,
570 $remote_file,
571 $handle,
572 $mode
573 );
574 }
575 /**
576 * Get non-blocking
577 *
578 * @param string $local_file the file handle local
579 * @param string $remote_file the file to get
580 * @param mixed $mode the mode to fget the file
581 * @param mixed $resumepos the position to continue from
582 *
583 * @return ftp_nb_get
584 */
585 public function nb_get(
586 $local_file,
587 $remote_file,
588 $mode = 0,
589 $resumepos = 0
590 ) {
591 if (!$mode) {
592 $mode = $this->get('mode');
593 }
594 if ($resumepos) {
595 return ftp_nb_get(
596 $this->_link,
597 $local_file,
598 $remote_file,
599 $mode,
600 $resumepos
601 );
602 }
603 return ftp_nb_get(
604 $this->_link,
605 $local_file,
606 $remote_file,
607 $mode
608 );
609 }
610 /**
611 * Put non-blocking
612 *
613 * @param string $remote_file the file to get
614 * @param string $local_file the file handle local
615 * @param mixed $mode the mode to fget the file
616 * @param mixed $startpos the position to continue from
617 *
618 * @return ftp_nb_put
619 */
620 public function nb_put(
621 $remote_file,
622 $local_file,
623 $mode = 0,
624 $startpos = 0
625 ) {
626 if (!$mode) {
627 $mode = $this->get('mode');
628 }
629 if ($startpos) {
630 return ftp_nb_put(
631 $this->_link,
632 $remote_file,
633 $local_file,
634 $mode,
635 $startpos
636 );
637 }
638 return ftp_nb_put(
639 $this->_link,
640 $remote_file,
641 $local_file,
642 $mode
643 );
644 }
645 /**
646 * FTP nlist
647 *
648 * @param string $directory the directory to list
649 *
650 * @return ftp_nlist
651 */
652 public function nlist($directory)
653 {
654 return ftp_nlist(
655 $this->_link,
656 $directory
657 );
658 }
659 /**
660 * FTP Pasv or not
661 *
662 * @param mixed $pasv the pasv mode
663 *
664 * @return ftp_pasv
665 */
666 public function pasv($pasv = false)
667 {
668 if (!$pasv) {
669 $pasv = $this->get('passive');
670 }
671 return ftp_pasv(
672 $this->_link,
673 $pasv
674 );
675 }
676 /**
677 * Put
678 *
679 * @param string $remote_file the file to put
680 * @param string $local_file the file handle local
681 * @param mixed $mode the mode to fget the file
682 * @param mixed $startpos the position to continue from
683 *
684 * @return ftp_put
685 */
686 public function put(
687 $remote_file,
688 $local_file,
689 $mode = 0,
690 $startpos = 0
691 ) {
692 if (!$mode) {
693 $mode = $this->get('mode');
694 }
695 if ($startpos) {
696 return ftp_put(
697 $this->_link,
698 $remote_file,
699 $local_file,
700 $mode,
701 $resumepos
702 );
703 }
704 return ftp_put(
705 $this->_link,
706 $remote_file,
707 $local_file,
708 $mode,
709 $resumepos
710 );
711 }
712 /**
713 * Print working directory
714 *
715 * @return ftp_pwd
716 */
717 public function pwd()
718 {
719 return ftp_pwd($this->_link);
720 }
721 /**
722 * Alias to close the ftp connection
723 *
724 * @return close
725 */
726 public function quit()
727 {
728 return $this->close();
729 }
730 /**
731 * Perform raw ftp command
732 *
733 * @param string $command the command to run
734 *
735 * @return ftp_raw
736 */
737 public function raw($command)
738 {
739 return ftp_raw($this->_link, $command);
740 }
741 /**
742 * Rawlist essentially ls -la from ftp perspective
743 *
744 * @param string $directory the directory to list
745 * @param mixed $recursive to delve deeper
746 *
747 * @return ftp_rawlist
748 */
749 public function rawlist(
750 $directory,
751 $recursive = false
752 ) {
753 return ftp_rawlist(
754 $this->_link,
755 $directory,
756 $recursive
757 );
758 }
759 /**
760 * List files recursive
761 *
762 * @param string $path The path to list recursive.
763 *
764 * @return array;
765 */
766 public function listrecursive($path)
767 {
768 $lines = $this->rawlist($path);
769 $rawlist = join("\n", $lines);
770 preg_match_all(
771 '/^([drwx+-]{10})\s+(\d+)\s+(\w+)\s+(\w+)\s+(\d+)\s+(.{12}) (.*)$/m',
772 $rawlist,
773 $matches,
774 PREG_SET_ORDER
775 );
776 $result = array();
777 foreach ((array)$matches as $index => &$line) {
778 array_shift($line);
779 $name = $line[count($line) - 1];
780 $type = $line[0][0];
781 $filepath = $path.'/'.$name;
782 if ($type == 'd') {
783 if (in_array($name, array('.', '..'))) {
784 continue;
785 }
786 $result = array_merge(
787 $result,
788 $this->listrecursive($filepath)
789 );
790 } else {
791 $result[] = $filepath;
792 }
793 }
794 return $result;
795 }
796
797 /**
798 * Rename function
799 *
800 * @param string $oldname the old name
801 * @param string $newname the name to change to
802 *
803 * @return ftp_rename
804 */
805 public function rename(
806 $oldname,
807 $newname
808 ) {
809 if (!(ftp_rename($this->_link, $oldname, $newname)
810 || $this->put($newname, $oldname))
811 ) {
812 $this->ftperror($this->data);
813 }
814 return $this;
815 }
816 /**
817 * Remove directory
818 *
819 * @param string $directory the directory to remove
820 *
821 * @return ftp_rmdir
822 */
823 public function rmdir($directory)
824 {
825 return @ftp_rmdir($this->_link, $directory);
826 }
827 /**
828 * Set the options for the ftp session
829 *
830 * @param string $option the option to set
831 * @param mixed $value the value to set to
832 *
833 * @return ftp_set_option
834 */
835 public function set_option(
836 $option,
837 $value
838 ) {
839 return ftp_set_option($this->_link, $option, $value);
840 }
841 /**
842 * Site to run command on
843 *
844 * @param string $command the command to run
845 *
846 * @return ftp_site
847 */
848 public function site($command)
849 {
850 return ftp_site(
851 $this->_link,
852 $command
853 );
854 }
855 /**
856 * Size of file
857 *
858 * @param string $remote_file the remote file to get size
859 * @param mixed $rawsize to use rawlist method
860 *
861 * @return ftp_size
862 */
863 public function size(
864 $remote_file,
865 $rawsize = true
866 ) {
867 if ($rawsize) {
868 return $this->rawsize($remote_file);
869 }
870 return ftp_size($this->_link, $remote_file);
871 }
872 /**
873 * Size of file raw (string)
874 *
875 * @param string $remote_file the file to get rawsize
876 *
877 * @return float
878 */
879 public function rawsize($remote_file)
880 {
881 if (!$this->exists($remote_file)) {
882 return 0;
883 }
884 $size = 0;
885 $filelist = $this->rawlist($remote_file);
886 if (!$filelist) {
887 $filelist = $this->rawlist(dirname($remote_file));
888 $filename = basename($remote_file);
889 $filelist = preg_grep("#$filename#", $filelist);
890 }
891 foreach ((array)$filelist as &$file) {
892 $fileinfo = preg_split(
893 '#\s+#',
894 $file,
895 null,
896 PREG_SPLIT_NO_EMPTY
897 );
898 $size += (float)$fileinfo[4];
899 }
900 return $size;
901 }
902 /**
903 * Connect to the ftp server
904 *
905 * @param string $host the host to connect to
906 * @param int $port the port to use
907 * @param int $timeout the timeout setting of the connection
908 * @param bool $autologin should we auto login
909 *
910 * @return object
911 */
912 public function ssl_connect(
913 $host = '',
914 $port = 0,
915 $timeout = 90,
916 $autologin = true
917 ) {
918 try {
919 $this->connect(
920 $host,
921 $port,
922 $timeout,
923 $autologin,
924 'ftp_ssl_connect'
925 );
926 } catch (Exception $e) {
927 throw new Exception($e->getMessage());
928 }
929 return $this;
930 }
931 /**
932 * The system type
933 *
934 * @return ftp_systype
935 */
936 public function systype()
937 {
938 return ftp_systype($this->_link);
939 }
940 /**
941 * Tests if item exits
942 *
943 * @param string $path the path to test
944 *
945 * @return bool
946 */
947 public function exists($path)
948 {
949 $tmppath = dirname($path);
950 $rawlisting = $this->rawlist("-a $tmppath");
951 $dirlisting = array();
952 foreach ((array)$rawlisting as &$file) {
953 $chunk = preg_split('/\s+/', $file);
954 if (in_array($chunk[8], array('.', '..'))) {
955 continue;
956 }
957 $dirlisting[] = sprintf(
958 '/%s/%s',
959 trim(trim($tmppath, '/'), '\\'),
960 $chunk[8]
961 );
962 }
963 return in_array($path, $dirlisting);
964 }
965 }