fogproject  1.5.9
About: FOG is a Linux-based network computer cloning solution for Windows, Mac OSX and various Linux distributions that ties together a few open-source tools with a PHP-based web interface. FOG doesn’t use any boot disks, or CDs; everything is done via TFTP and PXE.
  Fossies Dox: fogproject-1.5.9.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

fogftp.class.php
Go to the documentation of this file.
1 <?php
22 class FOGFTP extends FOGGetSet
23 {
29  protected $data = array(
30  'host' => '',
31  'username' => '',
32  'password' => '',
33  'port' => 21,
34  'timeout' => 90,
35  'passive' => true,
36  'mode' => FTP_BINARY,
37  );
43  private $_link;
55  private $_lastLoginHash;
73  public function __destruct()
74  {
75  $this->close();
76  }
85  public function alloc(
86  $filesize,
87  &$result
88  ) {
89  return ftp_alloc(
90  $this->_link,
91  $filesize,
92  $result
93  );
94  }
100  public function cdup()
101  {
102  return ftp_cdup($this->_link);
103  }
111  public function chdir($directory)
112  {
113  return ftp_chdir(
114  $this->_link,
115  $directory
116  );
117  }
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  }
145  public function close()
146  {
147  if ($this->_link) {
148  ftp_close($this->_link);
149  }
150  $this->_link = null;
151  return $this;
152  }
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
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  }
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  }
280  public function exec($command)
281  {
282  return ftp_exec(
283  $this->_link,
284  escapeshellcmd($command)
285  );
286  }
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  }
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  }
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  }
393  public function getOption($option)
394  {
395  return ftp_get_option(
396  $this->_link,
397  $option
398  );
399  }
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  }
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  }
481  public function mdtm($remote_file)
482  {
483  return ftp_mdtm($this->_link, $remote_file);
484  }
492  public function mkdir($directory)
493  {
494  return ftp_mkdir($this->_link, $directory);
495  }
501  public function nb_continue()
502  {
503  return ftp_nb_continue($this->_link);
504  }
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  }
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  }
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  }
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  }
652  public function nlist($directory)
653  {
654  return ftp_nlist(
655  $this->_link,
656  $directory
657  );
658  }
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  }
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  }
717  public function pwd()
718  {
719  return ftp_pwd($this->_link);
720  }
726  public function quit()
727  {
728  return $this->close();
729  }
737  public function raw($command)
738  {
739  return ftp_raw($this->_link, $command);
740  }
749  public function rawlist(
750  $directory,
751  $recursive = false
752  ) {
753  return ftp_rawlist(
754  $this->_link,
755  $directory,
756  $recursive
757  );
758  }
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 
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  }
823  public function rmdir($directory)
824  {
825  return @ftp_rmdir($this->_link, $directory);
826  }
835  public function set_option(
836  $option,
837  $value
838  ) {
839  return ftp_set_option($this->_link, $option, $value);
840  }
848  public function site($command)
849  {
850  return ftp_site(
851  $this->_link,
852  $command
853  );
854  }
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  }
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  }
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  }
936  public function systype()
937  {
938  return ftp_systype($this->_link);
939  }
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 }
FOGFTP\login
login( $username=null, $password=null)
Definition: fogftp.class.php:445
FOGFTP\$data
$data
Definition: fogftp.class.php:29
FOGFTP\rawlist
rawlist( $directory, $recursive=false)
Definition: fogftp.class.php:749
FOGFTP\quit
quit()
Definition: fogftp.class.php:726
FOGFTP\close
close()
Definition: fogftp.class.php:145
FOGFTP\site
site($command)
Definition: fogftp.class.php:848
FOGFTP\nb_get
nb_get( $local_file, $remote_file, $mode=0, $resumepos=0)
Definition: fogftp.class.php:585
$file
if(isset($_REQUEST['legclient'])) if(isset($_REQUEST['newclient'])) if(isset($_REQUEST['fogprep'])) if(isset($_REQUEST['fogcrypt'])) if(isset($_REQUEST['smartinstaller'])) if(!file_exists($filename)) $file
Definition: download.php:61
FOGFTP
Definition: fogftp.class.php:23
FOGFTP\fget
fget( $handle, $remote_file, $mode=0, $resumepos=0)
Definition: fogftp.class.php:297
FOGFTP\size
size( $remote_file, $rawsize=true)
Definition: fogftp.class.php:863
FOGFTP\exec
exec($command)
Definition: fogftp.class.php:280
FOGFTP\nb_fget
nb_fget( $handle, $remote_file, $mode=0, $resumepos=0)
Definition: fogftp.class.php:515
FOGGetSet
Definition: foggetset.class.php:23
FOGFTP\chmod
chmod( $mode, $filename)
Definition: fogftp.class.php:126
FOGFTP\ftperror
ftperror($data)
Definition: fogftp.class.php:365
$password
$password
Definition: checkcredentials.php:27
FOGFTP\cdup
cdup()
Definition: fogftp.class.php:100
FOGFTP\connect
connect( $host='', $port=0, $timeout=90, $autologin=true, $connectmethod='ftp_connect')
Definition: fogftp.class.php:164
FOGFTP\pasv
pasv($pasv=false)
Definition: fogftp.class.php:666
FOGFTP\getOption
getOption($option)
Definition: fogftp.class.php:393
FOGFTP\nlist
nlist($directory)
Definition: fogftp.class.php:652
FOGFTP\put
put( $remote_file, $local_file, $mode=0, $startpos=0)
Definition: fogftp.class.php:686
FOGFTP\rename
rename( $oldname, $newname)
Definition: fogftp.class.php:805
FOGFTP\alloc
alloc( $filesize, &$result)
Definition: fogftp.class.php:85
FOGFTP\chdir
chdir($directory)
Definition: fogftp.class.php:111
FOGFTP\mdtm
mdtm($remote_file)
Definition: fogftp.class.php:481
FOGFTP\rawsize
rawsize($remote_file)
Definition: fogftp.class.php:879
FOGFTP\rmdir
rmdir($directory)
Definition: fogftp.class.php:823
$path
if(!is_string($_GET['path'])) $path
Definition: getfiles.php:31
FOGFTP\pull
pull( $local_file, $remote_file, $mode=0, $resumepos=0)
Definition: fogftp.class.php:411
FOGFTP\$_lastLoginHash
$_lastLoginHash
Definition: fogftp.class.php:55
FOGFTP\$_lastConnectionHash
$_lastConnectionHash
Definition: fogftp.class.php:49
FOGFTP\ssl_connect
ssl_connect( $host='', $port=0, $timeout=90, $autologin=true)
Definition: fogftp.class.php:912
FOGFTP\$_currentLoginHash
$_currentLoginHash
Definition: fogftp.class.php:67
FOGFTP\mkdir
mkdir($directory)
Definition: fogftp.class.php:492
FOGFTP\set_option
set_option( $option, $value)
Definition: fogftp.class.php:835
FOGFTP\__destruct
__destruct()
Definition: fogftp.class.php:73
FOGFTP\nb_continue
nb_continue()
Definition: fogftp.class.php:501
FOGFTP\fput
fput( $remote_file, $handle, $mode=0, $startpos=0)
Definition: fogftp.class.php:332
FOGFTP\raw
raw($command)
Definition: fogftp.class.php:737
FOGBase\getSubObjectIDs
static getSubObjectIDs( $object='Host', $findWhere=array(), $getField='id', $not=false, $operator='AND', $orderBy='name', $groupBy=false, $filter='array_unique')
Definition: fogbase.class.php:2006
$username
$username
Definition: checkcredentials.php:25
FOGFTP\$_currentConnectionHash
$_currentConnectionHash
Definition: fogftp.class.php:61
FOGFTP\listrecursive
listrecursive($path)
Definition: fogftp.class.php:766
FOGFTP\$_link
$_link
Definition: fogftp.class.php:43
FOGFTP\pwd
pwd()
Definition: fogftp.class.php:717
FOGFTP\nb_put
nb_put( $remote_file, $local_file, $mode=0, $startpos=0)
Definition: fogftp.class.php:620
FOGFTP\nb_fput
nb_fput( $remote_file, $handle, $mode=0, $startpos=0)
Definition: fogftp.class.php:550
FOGFTP\systype
systype()
Definition: fogftp.class.php:936
$host
$host
Definition: hostnameloop.php:27
$lines
$lines
Definition: logtoview.php:41
FOGFTP\exists
exists($path)
Definition: fogftp.class.php:947