File modules/cms/generator/target/FtpTarget.class.php

Last commit: Thu Nov 19 14:53:31 2020 +0100	Jan Dankert	Using warn() instead of error()
1 <?php 2 // OpenRat Content Management System 3 // Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de 4 // 5 // This program is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU General Public License 7 // as published by the Free Software Foundation; either version 2 8 // of the License, or (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with this program; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 namespace cms\generator\target; 19 20 use cms\base\Configuration; 21 use logger\Logger; 22 use util\exception\PublisherException; 23 use util\exception\UIException; 24 25 26 /** 27 * FTP-BaseTarget. 28 * 29 * @author Jan Dankert 30 */ 31 class FtpTarget extends BaseTarget 32 { 33 private $connection; 34 35 // Aufbauen der Verbindung 36 public function open() 37 { 38 //$conf = \cms\base\Configuration::rawConfig(); 39 //$conf_ftp = $conf['publish']['ftp']; 40 $ftp = $this->url; 41 42 // Die projektspezifischen Werte gewinnen bei �berschneidungen mit den Default-Werten 43 //$ftp = array_merge($conf_ftp, $ftp); 44 45 $this->connection = $this->createConnection(); 46 47 if (!$this->connection) { 48 Logger::warn('Cannot connect to ' . $this->url->host . ':' . $this->url->port); 49 throw new PublisherException('Cannot connect to ' . $this->url->scheme . '-server: ' . $this->url->host . ':' . $this->url->port); 50 } 51 52 if (empty($this->url->user)) { 53 $ftp['user'] = 'anonymous'; 54 $ftp['pass'] = 'openrat@openrat.de'; 55 } 56 57 if (!ftp_login($this->connection, $this->url->user, $this->url->pass)) 58 throw new PublisherException('Unable to login as user ' . $this->url->user); 59 60 $pasv = $this->url->fragment == 'passive'; 61 62 if ( $pasv ) 63 if (!ftp_pasv($this->connection, true)) 64 throw new PublisherException('Cannot switch to FTP PASV mode'); 65 66 if ( $this->url->query ) { 67 parse_str($this->url->query, $ftp_var); 68 69 if (isset($ftp_var['site'])) { 70 $site_commands = explode(',', $ftp_var['site']); 71 foreach ($site_commands as $cmd) { 72 if (!@ftp_site($this->connection, $cmd)) 73 throw new PublisherException('unable to do SITE command: ' . $cmd); 74 } 75 } 76 } 77 78 $path = rtrim($this->url->path, '/'); 79 80 if (!@ftp_chdir($this->connection, $path)) 81 throw new PublisherException('unable CHDIR to directory: ' . $path); 82 } 83 84 85 /** 86 * Kopieren einer Datei vom lokalen System auf den FTP-Server. 87 * 88 * @param String Quelle 89 * @param String Ziel 90 * @param int FTP-Mode (BINARY oder ASCII) 91 */ 92 public function put($source, $dest, $lastChangeDate) 93 { 94 $dest = $this->url->path . '/' . $dest; 95 96 //$this->log .= "Copying file: $source -&gt; $dest ...\n"; 97 98 $mode = FTP_BINARY; 99 $p = strrpos(basename($dest), '.'); // Letzten Punkt suchen 100 101 if ($p !== false) // Wennn letzten Punkt gefunden, dann dort aufteilen 102 { 103 $extension = substr(basename($dest), $p + 1); 104 $type = Configuration::subset('mime-types')->get( $extension,'application/download'); 105 if (substr($type, 0, 5) == 'text/') 106 $mode = FTP_ASCII; 107 } 108 109 Logger::debug("FTP PUT target:$dest mode:" . (($mode == FTP_ASCII) ? 'ascii' : 'binary')); 110 111 if (!@ftp_put($this->connection, $dest, $source, $mode)) { 112 if (!$this->mkdirs(dirname($dest))) 113 return; // Fehler. 114 115 ftp_chdir($this->connection, $this->url->path); 116 117 if (!@ftp_put($this->connection, $dest, $source, $mode)) 118 throw new PublisherException("FTP PUT failed.\n" . 119 "source : $source\n" . 120 "destination: $dest"); 121 122 } 123 } 124 125 126 /** 127 * Private Methode zum rekursiven Anlegen von Verzeichnissen. 128 * 129 * @param String Pfad 130 * @return boolean true, wenn ok 131 */ 132 private function mkdirs($strPath) 133 { 134 if (@ftp_chdir($this->connection, $strPath)) 135 return true; // Verzeichnis existiert schon :) 136 137 $pStrPath = dirname($strPath); 138 139 if (!$this->mkdirs($pStrPath)) 140 return false; 141 142 if (!@ftp_mkdir($this->connection, $strPath)) 143 throw new PublisherException("failed to create remote directory: $strPath"); 144 145 return true; 146 } 147 148 149 /** 150 * Schliessen der FTP-Verbindung.<br> 151 * Sollte unbedingt aufgerufen werden, damit keine unn�tigen Sockets aufbleiben. 152 */ 153 public function close() 154 { 155 if (!@ftp_quit($this->connection)) { 156 // Closing not possible. 157 // Only logging. Maybe we could throw an Exception here? 158 Logger::warn('Failed to close FTP connection. Continueing...'); 159 return; 160 } 161 } 162 163 protected function createConnection() 164 { 165 return ftp_connect($this->url->host, $this->url->port); 166 } 167 168 public static function isAvailable() 169 { 170 return function_exists('ftp_connect'); 171 } 172 } 173 174 175 ?>
Download modules/cms/generator/target/FtpTarget.class.php
History Thu, 19 Nov 2020 14:53:31 +0100 Jan Dankert Using warn() instead of error() Wed, 18 Nov 2020 21:01:59 +0100 Jan Dankert Renaming of target classes for naming convention reasons.