Merge pull request #1526 from jygaulier/PHRAS-750_ftp-proxy-auth

#PHRAS-750 #time 3h
This commit is contained in:
jygaulier
2015-10-07 15:53:09 +02:00
3 changed files with 80 additions and 33 deletions

View File

@@ -21,9 +21,11 @@ class FtpServiceProvider implements ServiceProviderInterface
*/ */
public function register(Application $app) public function register(Application $app)
{ {
$app['phraseanet.ftp.client'] = $app->protect(function ($host, $port = 21, $timeout = 90, $ssl = false, $proxy = false, $proxyport = false) { $app['phraseanet.ftp.client'] = $app->protect(
return new \ftpclient($host, $port, $timeout, $ssl, $proxy, $proxyport); function ($host, $port=21, $timeout=90, $ssl=false, $proxyhost=false, $proxyport=false, $proxyuser=false, $proxypwd=false) {
}); return new \ftpclient($host, $port, $timeout, $ssl, $proxyhost, $proxyport, $proxyuser, $proxypwd);
}
);
} }
/** /**

View File

@@ -3,36 +3,54 @@
class ftpclient class ftpclient
{ {
protected $connexion; protected $connexion;
protected $proxy;
protected $host; protected $host;
protected $port;
protected $cached_dirs = array(); protected $cached_dirs = array();
protected $debug = false; protected $debug = false;
private $proxyhost = false;
private $proxyport = false;
private $proxyuser = false;
private $proxypwd = false;
public function __construct($host, $port = 21, $timeout = 90, $ssl = false, $proxy = false, $proxyport = false) private function cleanAddr($addr, &$usedSSL)
{ {
$host = mb_substr($host, -1, 1) == '/' ? mb_substr($host, 0, (mb_strlen($host) - 1)) : $host; $addr = trim($addr);
if(substr($addr, 0, 7) === "ftps://") {
$addr = substr($addr, 7);
$usedSSL = true;
}
elseif(substr($addr, 0, 6) === "ftp://") {
$addr = substr($addr, 6);
}
if(substr($addr, -1, 1) == '/') {
$addr = substr($addr, 0, (strlen($addr) - 1));
}
return $addr;
}
if (($p = mb_strpos($host, 'ftp://')) !== false) public function __construct($host, $port = 21, $timeout = 90, $ssl = false,
$host = mb_substr($host, 6); $proxyhost = false,
$proxyport = false,
$proxyuser = false,
$proxypwd = false
)
{
$this->host = $host;
$this->port = $port;
$this->proxyhost = $proxyhost;
$this->proxyport = $proxyport;
$this->proxyuser = $proxyuser;
$this->proxypwd = $proxypwd;
$host = $proxy ? $proxy : $host; // if there is a proxy, connect to it, not on host/port provided
$host = $proxyhost ? $proxyhost : $host;
$port = $proxyport ? $proxyport : $port; $port = $proxyport ? $proxyport : $port;
if ($this->debug && $proxy) // clean the addr, force ssl if needed
echo "Utilisation du proxy $proxy\n<br>"; $host = $this->cleanAddr($host, $ssl);
if ($this->debug && $proxyport)
echo "Utilisation du port proxy $proxyport\n<br>";
$this->proxy = $proxy;
$this->host = $host;
if ($this->debug) if ($this->debug)
echo "Ouverture de connection vers $host:$port timeout $timeout et proxy $proxy:$proxyport\n<br>"; echo "Ouverture de connexion vers $host:$port timeout $timeout\n<br>";
if (trim($host) == '') {
throw new Exception('Nom d\'hote incorrect ' . $host);
}
try { try {
if ($ssl === true) { if ($ssl === true) {
@@ -63,22 +81,29 @@ class ftpclient
public function login($username, $password) public function login($username, $password)
{ {
$username = $this->proxy ? $username . "@" . $this->host : $username; if($this->proxyhost) {
$username .= ("@" . $this->host);
if ($this->proxyuser) {
$username .= (' ' . $this->proxyuser);
}
}
$retry = 3; $retry = 3;
$done = false; $done = false;
if ($this->debug) while (!$done && $retry-- > 0) {
echo "tentative de login avec $username, $password\n<br>"; $ret = ftp_raw($this->connexion, "USER ".$username);
if(is_array($ret) && $password) {
while ($retry > 0) { $ret = ftp_raw($this->connexion, "PASS ".$password);
if ((ftp_login($this->connexion, $username, $password)) === false) { }
$retry --; if(is_array($ret) && $this->proxypwd) {
} else { $ret = ftp_raw($this->connexion, "ACCT ".$this->proxypwd);
$retry = 0; }
if(is_array($ret)) {
$done = true; $done = true;
} }
} }
if (! $done) { if (! $done) {
throw new Exception('Impossible de s\'authentifier sur le serveur FTP'); throw new Exception('Impossible de s\'authentifier sur le serveur FTP');
} }

View File

@@ -46,13 +46,13 @@ class task_period_ftp extends task_appboxAbstract
{ {
$request = http_request::getInstance(); $request = http_request::getInstance();
$parm2 = $request->get_parms('proxy', 'proxyport', 'period', 'syslog'); $parm2 = $request->get_parms('proxy', 'proxyport', 'proxyuser', 'proxypwd', 'period', 'syslog');
$dom = new DOMDocument(); $dom = new DOMDocument();
$dom->preserveWhiteSpace = false; $dom->preserveWhiteSpace = false;
$dom->formatOutput = true; $dom->formatOutput = true;
if ((@$dom->loadXML($oldxml)) != FALSE) { if ((@$dom->loadXML($oldxml)) != FALSE) {
$xmlchanged = false; $xmlchanged = false;
foreach (array('str:proxy', 'str:proxyport', 'str:period', 'pop:syslog') as $pname) { foreach (array('str:proxy', 'str:proxyport', 'str:proxyuser', 'str:proxypwd', 'str:period', 'pop:syslog') as $pname) {
$ptype = substr($pname, 0, 3); $ptype = substr($pname, 0, 3);
$pname = substr($pname, 4); $pname = substr($pname, 4);
$pvalue = $parm2[$pname]; $pvalue = $parm2[$pname];
@@ -101,6 +101,8 @@ class task_period_ftp extends task_appboxAbstract
{ {
proxy.value = xml.find("proxy").text(); proxy.value = xml.find("proxy").text();
proxyport.value = xml.find("proxyport").text(); proxyport.value = xml.find("proxyport").text();
proxyuser.value = xml.find("proxyuser").text();
proxypwd.value = xml.find("proxypwd").text();
period.value = xml.find("period").text(); period.value = xml.find("period").text();
} }
} }
@@ -154,6 +156,18 @@ class task_period_ftp extends task_appboxAbstract
<input class="formElem" type="text" name="proxyport" /> <input class="formElem" type="text" name="proxyport" />
</div> </div>
</div> </div>
<div class="control-group">
<label class="control-label"><?php echo _('task::ftp:proxy user') ?></label>
<div class="controls">
<input class="formElem" type="text" name="proxyuser" />
</div>
</div>
<div class="control-group">
<label class="control-label"><?php echo _('task::ftp:proxy password') ?></label>
<div class="controls">
<input class="formElem" type="password" name="proxypwd" />
</div>
</div>
<div class="control-group"> <div class="control-group">
<label class="control-label"><?php echo _('task::_common_:periodicite de la tache') ?></label> <label class="control-label"><?php echo _('task::_common_:periodicite de la tache') ?></label>
<div class="controls"> <div class="controls">
@@ -177,6 +191,8 @@ class task_period_ftp extends task_appboxAbstract
, 'active' , 'active'
, 'proxy' , 'proxy'
, 'proxyport' , 'proxyport'
, 'proxyuser'
, 'proxypwd'
, 'period' , 'period'
, 'debug' , 'debug'
); );
@@ -189,6 +205,8 @@ class task_period_ftp extends task_appboxAbstract
foreach (array( foreach (array(
'proxy' 'proxy'
, 'proxyport' , 'proxyport'
, 'proxyuser'
, 'proxypwd'
, 'period' , 'period'
) as $f) { ) as $f) {
if ($parm[$f] !== NULL) { if ($parm[$f] !== NULL) {
@@ -718,6 +736,8 @@ class task_period_ftp extends task_appboxAbstract
<tasksettings> <tasksettings>
<proxy></proxy> <proxy></proxy>
<proxyport></proxyport> <proxyport></proxyport>
<proxyuser></proxyuser>
<proxypwd></proxypwd>
<period>%s</period> <period>%s</period>
<syslog></syslog> <syslog></syslog>
</tasksettings>", min(max($period, self::MINPERIOD), self::MAXPERIOD)); </tasksettings>", min(max($period, self::MINPERIOD), self::MAXPERIOD));