fixes for windows

This commit is contained in:
jygaulier
2012-04-25 13:53:41 +02:00
parent f0b1dbcee3
commit 6f5ac2d417

204
lib/classes/task/period/ftpPull.class.php Normal file → Executable file
View File

@@ -2,7 +2,7 @@
/* /*
* This file is part of Phraseanet * This file is part of Phraseanet
* *
* (c) 2005-2012 Alchemy * (c) 2005-2010 Alchemy
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
@@ -16,6 +16,7 @@
*/ */
class task_period_ftpPull extends task_appboxAbstract class task_period_ftpPull extends task_appboxAbstract
{ {
protected $debug = false; protected $debug = false;
protected $proxy; protected $proxy;
protected $proxyport; protected $proxyport;
@@ -47,24 +48,30 @@ class task_period_ftpPull extends task_appboxAbstract
, "password", "ssl", "ftppath", "localpath" , "password", "ssl", "ftppath", "localpath"
, "passive", "period" , "passive", "period"
); );
if ($dom = @DOMDocument::loadXML($oldxml)) { if ($dom = @DOMDocument::loadXML($oldxml))
{
$xmlchanged = false; $xmlchanged = false;
foreach (array("str:proxy", "str:proxyport", "str:period", "boo:passive", "boo:ssl", "str:password", "str:user", "str:ftppath", "str:localpath", "str:port", "str:host") as $pname) { foreach (array("str:proxy", "str:proxyport", "str:period", "boo:passive", "boo:ssl", "str:password", "str:user", "str:ftppath", "str:localpath", "str:port", "str:host") 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];
if ($ns = $dom->getElementsByTagName($pname)->item(0)) { if ($ns = $dom->getElementsByTagName($pname)->item(0))
{
// le champ existait dans le xml, on supprime son ancienne valeur (tout le contenu) // le champ existait dans le xml, on supprime son ancienne valeur (tout le contenu)
while (($n = $ns->firstChild)) while (($n = $ns->firstChild))
$ns->removeChild($n); $ns->removeChild($n);
} else { }
else
{
// le champ n'existait pas dans le xml, on le cree // le champ n'existait pas dans le xml, on le cree
$dom->documentElement->appendChild($dom->createTextNode("\t")); $dom->documentElement->appendChild($dom->createTextNode("\t"));
$ns = $dom->documentElement->appendChild($dom->createElement($pname)); $ns = $dom->documentElement->appendChild($dom->createElement($pname));
$dom->documentElement->appendChild($dom->createTextNode("\n")); $dom->documentElement->appendChild($dom->createTextNode("\n"));
} }
// on fixe sa valeur // on fixe sa valeur
switch ($ptype) { switch ($ptype)
{
case "str": case "str":
$ns->appendChild($dom->createTextNode($pvalue)); $ns->appendChild($dom->createTextNode($pvalue));
break; break;
@@ -81,27 +88,30 @@ class task_period_ftpPull extends task_appboxAbstract
public function xml2graphic($xml, $form) public function xml2graphic($xml, $form)
{ {
if (($sxml = simplexml_load_string($xml))) { // in fact XML IS always valid here... if (($sxml = simplexml_load_string($xml))) // in fact XML IS always valid here...
{
// ... but we could check for safe values (ex. 0 < period < 3600) // ... but we could check for safe values (ex. 0 < period < 3600)
?> ?>
<script type="text/javascript"> <script type="text/javascript">
<?php echo $form ?>.proxy.value = "<?php echo p4string::MakeString($sxml->proxy, "js", '"') ?>"; <?php echo $form ?>.proxy.value = "<?php echo p4string::MakeString($sxml->proxy, "js", '"') ?>";
<?php echo $form ?>.proxyport.value = "<?php echo p4string::MakeString($sxml->proxyport, "js", '"') ?>"; <?php echo $form ?>.proxyport.value = "<?php echo p4string::MakeString($sxml->proxyport, "js", '"') ?>";
<?php echo $form ?>.period.value = "<?php echo p4string::MakeString($sxml->period, "js", '"') ?>"; <?php echo $form ?>.period.value = "<?php echo p4string::MakeString($sxml->period, "js", '"') ?>";
<?php echo $form ?>.localpath.value = "<?php echo p4string::MakeString($sxml->localpath, "js", '"') ?>"; <?php echo $form ?>.localpath.value = "<?php echo p4string::MakeString($sxml->localpath, "js", '"') ?>";
<?php echo $form ?>.ftppath.value = "<?php echo p4string::MakeString($sxml->ftppath, "js", '"') ?>"; <?php echo $form ?>.ftppath.value = "<?php echo p4string::MakeString($sxml->ftppath, "js", '"') ?>";
<?php echo $form ?>.host.value = "<?php echo p4string::MakeString($sxml->host, "js", '"') ?>"; <?php echo $form ?>.host.value = "<?php echo p4string::MakeString($sxml->host, "js", '"') ?>";
<?php echo $form ?>.port.value = "<?php echo p4string::MakeString($sxml->port, "js", '"') ?>"; <?php echo $form ?>.port.value = "<?php echo p4string::MakeString($sxml->port, "js", '"') ?>";
<?php echo $form ?>.user.value = "<?php echo p4string::MakeString($sxml->user, "js", '"') ?>"; <?php echo $form ?>.user.value = "<?php echo p4string::MakeString($sxml->user, "js", '"') ?>";
<?php echo $form ?>.password.value = "<?php echo p4string::MakeString($sxml->password, "js", '"') ?>"; <?php echo $form ?>.password.value = "<?php echo p4string::MakeString($sxml->password, "js", '"') ?>";
<?php echo $form ?>.ssl.checked = <?php echo p4field::isyes($sxml->ssl) ? "true" : 'false' ?>; <?php echo $form ?>.ssl.checked = <?php echo p4field::isyes($sxml->ssl) ? "true" : 'false' ?>;
<?php echo $form ?>.passive.checked = <?php echo p4field::isyes($sxml->passive) ? "true" : 'false' ?>; <?php echo $form ?>.passive.checked = <?php echo p4field::isyes($sxml->passive) ? "true" : 'false' ?>;
</script> </script>
<?php <?php
return(""); return("");
} else { // ... so we NEVER come here }
else // ... so we NEVER come here
{
// bad xml // bad xml
return("BAD XML"); return("BAD XML");
} }
@@ -110,7 +120,7 @@ class task_period_ftpPull extends task_appboxAbstract
public function printInterfaceJS() public function printInterfaceJS()
{ {
global $parm; global $parm;
?> ?>
<script type="text/javascript"> <script type="text/javascript">
function chgxmltxt(textinput, fieldname) function chgxmltxt(textinput, fieldname)
{ {
@@ -125,7 +135,7 @@ class task_period_ftpPull extends task_appboxAbstract
setDirty(); setDirty();
} }
</script> </script>
<?php <?php
} }
function getGraphicForm() function getGraphicForm()
@@ -137,46 +147,46 @@ class task_period_ftpPull extends task_appboxAbstract
{ {
global $parm; global $parm;
ob_start(); ob_start();
?> ?>
<form name="graphicForm" onsubmit="return(false);" method="post"> <form name="graphicForm" onsubmit="return(false);" method="post">
<br/> <br/>
<?php echo('task::ftp:proxy') ?> <?php echo('task::ftp:proxy') ?>
<input type="text" name="proxy" style="width:400px;" onchange="chgxmltxt(this, 'proxy');"><br/> <input type="text" name="proxy" style="width:400px;" onchange="chgxmltxt(this, 'proxy');"><br/>
<br/> <br/>
<?php echo('task::ftp:proxy port') ?> <?php echo('task::ftp:proxy port') ?>
<input type="text" name="proxyport" style="width:400px;" onchange="chgxmltxt(this, 'proxyport');"><br/> <input type="text" name="proxyport" style="width:400px;" onchange="chgxmltxt(this, 'proxyport');"><br/>
<br/> <br/>
<?php echo('task::ftp:host') ?> <?php echo('task::ftp:host') ?>
<input type="text" name="host" style="width:400px;" onchange="chgxmltxt(this, 'host');"><br/> <input type="text" name="host" style="width:400px;" onchange="chgxmltxt(this, 'host');"><br/>
<br/> <br/>
<?php echo('task::ftp:port') ?> <?php echo('task::ftp:port') ?>
<input type="text" name="port" style="width:400px;" onchange="chgxmltxt(this, 'port');"><br/> <input type="text" name="port" style="width:400px;" onchange="chgxmltxt(this, 'port');"><br/>
<br/> <br/>
<?php echo('task::ftp:user') ?> <?php echo('task::ftp:user') ?>
<input type="text" name="user" style="width:400px;" onchange="chgxmltxt(this, 'user');"><br/> <input type="text" name="user" style="width:400px;" onchange="chgxmltxt(this, 'user');"><br/>
<br/> <br/>
<?php echo('task::ftp:password') ?> <?php echo('task::ftp:password') ?>
<input type="password" name="password" style="width:400px;" onchange="chgxmltxt(this, 'password');"><br/> <input type="password" name="password" style="width:400px;" onchange="chgxmltxt(this, 'password');"><br/>
<br/> <br/>
<?php echo('task::ftp:chemin distant') ?> <?php echo('task::ftp:chemin distant') ?>
<input type="text" name="ftppath" style="width:400px;" onchange="chgxmltxt(this, 'ftppath');"><br/> <input type="text" name="ftppath" style="width:400px;" onchange="chgxmltxt(this, 'ftppath');"><br/>
<br/> <br/>
<?php echo('task::ftp:localpath') ?> <?php echo('task::ftp:localpath') ?>
<input type="text" name="localpath" style="width:400px;" onchange="chgxmltxt(this, 'localpath');"><br/> <input type="text" name="localpath" style="width:400px;" onchange="chgxmltxt(this, 'localpath');"><br/>
<br/> <br/>
<input type="checkbox" name="passive" onchange="chgxmlck(this)"> <input type="checkbox" name="passive" onchange="chgxmlck(this)">
<?php echo _('task::ftp:mode passif') ?> <?php echo _('task::ftp:mode passif') ?>
<br/> <br/>
<input type="checkbox" name="ssl" onchange="chgxmlck(this)"> <input type="checkbox" name="ssl" onchange="chgxmlck(this)">
<?php echo _('task::ftp:utiliser SSL') ?> <?php echo _('task::ftp:utiliser SSL') ?>
<br/> <br/>
<?php echo('task::_common_:periodicite de la tache') ?> <?php echo('task::_common_:periodicite de la tache') ?>
<input type="text" name="period" style="width:40px;" onchange="chgxmltxt(this, 'period');"> <input type="text" name="period" style="width:40px;" onchange="chgxmltxt(this, 'period');">
&nbsp;<?php echo('task::_common_:minutes (unite temporelle)') ?><br/> &nbsp;<?php echo('task::_common_:minutes (unite temporelle)') ?><br/>
</form> </form>
<?php <?php
$out = ob_get_clean(); $out = ob_get_clean();
return $out; return $out;
@@ -192,17 +202,24 @@ class task_period_ftpPull extends task_appboxAbstract
, "password", "passive", "ssl", "debug" , "password", "passive", "ssl", "debug"
); );
if ($parm["xml"] === null) { if ($parm["xml"] === null)
{
// pas de xml 'raw' : on accepte les champs 'graphic view' // pas de xml 'raw' : on accepte les champs 'graphic view'
if ($domTaskSettings = DOMDocument::loadXML($taskrow["settings"])) { if ($domTaskSettings = DOMDocument::loadXML($taskrow["settings"]))
{
$xmlchanged = false; $xmlchanged = false;
foreach (array("proxy", "proxyport", "period", "host", "port", "user", "password", "ssl", "passive", "localpath", "ftppath") as $f) { foreach (array("proxy", "proxyport", "period", "host", "port", "user", "password", "ssl", "passive", "localpath", "ftppath") as $f)
if ($parm[$f] !== NULL) { {
if ($ns = $domTaskSettings->getElementsByTagName($f)->item(0)) { if ($parm[$f] !== NULL)
{
if ($ns = $domTaskSettings->getElementsByTagName($f)->item(0))
{
// le champ existait dans le xml, on supprime son ancienne valeur (tout le contenu) // le champ existait dans le xml, on supprime son ancienne valeur (tout le contenu)
while (($n = $ns->firstChild)) while (($n = $ns->firstChild))
$ns->removeChild($n); $ns->removeChild($n);
} else { }
else
{
// le champ n'existait pas dans le xml, on le cree // le champ n'existait pas dans le xml, on le cree
$domTaskSettings->documentElement->appendChild($domTaskSettings->createTextNode("\t")); $domTaskSettings->documentElement->appendChild($domTaskSettings->createTextNode("\t"));
$ns = $domTaskSettings->documentElement->appendChild($domTaskSettings->createElement($f)); $ns = $domTaskSettings->documentElement->appendChild($domTaskSettings->createElement($f));
@@ -219,37 +236,46 @@ class task_period_ftpPull extends task_appboxAbstract
} }
// si on doit changer le xml, on verifie qu'il est valide // si on doit changer le xml, on verifie qu'il est valide
if ($parm["xml"] && ! DOMDocument::loadXML($parm["xml"])) { if ($parm["xml"] && !DOMDocument::loadXML($parm["xml"]))
return(false); return(false);
}
$sql = ""; $sql = "";
$params = array(':task_id' => $taskid); $params = array(':task_id' => $taskid);
if ($parm["xml"] !== NULL) { if ($parm["xml"] !== NULL)
{
$sql .= ( $sql ? " ," : "") . "settings = :settings"; $sql .= ( $sql ? " ," : "") . "settings = :settings";
$params[':settings'] = $parm['xml']; $params[':settings'] = $parm['xml'];
} }
if ($parm["name"] !== NULL) { if ($parm["name"] !== NULL)
{
$sql .= ( $sql ? " ," : "") . "name = :name"; $sql .= ( $sql ? " ," : "") . "name = :name";
$params[':name'] = $parm['name']; $params[':name'] = $parm['name'];
} }
if ($parm["active"] !== NULL) { if ($parm["active"] !== NULL)
{
$sql .= ( $sql ? " ," : "") . "active = :active"; $sql .= ( $sql ? " ," : "") . "active = :active";
$params[':active'] = $parm['active']; $params[':active'] = $parm['active'];
} }
if ($sql) { if ($sql)
try { {
try
{
$sql = "UPDATE task2 SET $sql WHERE task_id = :task_id"; $sql = "UPDATE task2 SET $sql WHERE task_id = :task_id";
$stmt = $conn->prepare($sql); $stmt = $conn->prepare($sql);
$stmt->execute($params); $stmt->execute($params);
$stmt->closeCursor(); $stmt->closeCursor();
return true; return true;
} catch (Exception $e) { }
catch (Exception $e)
{
return false; return false;
} }
} else { }
else
{
return true; return true;
} }
} }
@@ -262,8 +288,8 @@ class task_period_ftpPull extends task_appboxAbstract
$this->port = (string) ($sx_task_settings->port); $this->port = (string) ($sx_task_settings->port);
$this->user = (string) ($sx_task_settings->user); $this->user = (string) ($sx_task_settings->user);
$this->password = (string) ($sx_task_settings->password); $this->password = (string) ($sx_task_settings->password);
$this->ssl = ! ! ((string) ($sx_task_settings->ssl)); $this->ssl = !!((string) ($sx_task_settings->ssl));
$this->passive = ! ! ((string) ($sx_task_settings->passive)); $this->passive = !!((string) ($sx_task_settings->passive));
$this->ftppath = (string) ($sx_task_settings->ftppath); $this->ftppath = (string) ($sx_task_settings->ftppath);
$this->localpath = (string) ($sx_task_settings->localpath); $this->localpath = (string) ($sx_task_settings->localpath);
@@ -274,13 +300,32 @@ class task_period_ftpPull extends task_appboxAbstract
protected function retrieve_content(appbox $appbox) protected function retrieve_content(appbox $appbox)
{ {
try { foreach(array('localpath', 'host', 'port', 'user', 'password', 'ftppath') as $f)
if ( ! is_dir($this->localpath) || ! system_file::mkdir($this->localpath)) {
throw new Exception("$this->localpath is not writeable\n"); if(trim((string)($this->{$f})) === '')
{
if ( ! is_writeable($this->localpath)) $this->log('setting \''.$f.'\' must be set');
throw new Exception("$this->localpath is not writeable\n"); $this->running = FALSE;
}
}
if (!is_dir($this->localpath) || !system_file::mkdir($this->localpath))
{
$this->log('\''.$this->localpath.'\' does not exists');
$this->running = FALSE;
}
if (!is_writeable($this->localpath))
{
$this->log('\''.$this->localpath.'\' is not writeable');
$this->running = FALSE;
}
if(!$this->running )
{
$this->set_status(self::STATUS_STOPPED);
return array();
}
try
{
$ftp = new ftpclient($this->host, $port, 90, $this->ssl, $this->proxy, $this->proxyport); $ftp = new ftpclient($this->host, $port, 90, $this->ssl, $this->proxy, $this->proxyport);
$ftp->login($this->user, $this->password); $ftp->login($this->user, $this->password);
$ftp->chdir($this->ftppath); $ftp->chdir($this->ftppath);
@@ -291,20 +336,38 @@ class task_period_ftpPull extends task_appboxAbstract
if ($this->debug) if ($this->debug)
echo "attente de 25sec pour avoir les fichiers froids...\n"; echo "attente de 25sec pour avoir les fichiers froids...\n";
sleep(25); for($t = 25; $this->running && $t > 0; $t--) // DON'T do sleep($this->period - $when_started) because it prevents ticks !
{
$s = $this->get_status();
if($s == self::STATUS_TOSTOP)
{
if (isset($ftp) && $ftp instanceof ftpclient)
$ftp->close();
$this->set_status(self::STATUS_STOPPED);
$this->running = FALSE;
return array();
}
else
{
sleep(1);
}
}
$list_2 = $ftp->list_directory(true); $list_2 = $ftp->list_directory(true);
foreach ($list_1 as $filepath => $timestamp) { foreach ($list_1 as $filepath => $timestamp)
$done ++; {
$done++;
$this->setProgress($done, $todo); $this->setProgress($done, $todo);
if ( ! isset($list_2[$filepath])) { if (!isset($list_2[$filepath]))
{
if ($this->debug) if ($this->debug)
echo "le fichier $filepath a disparu...\n"; echo "le fichier $filepath a disparu...\n";
continue; continue;
} }
if ($list_2[$filepath] !== $timestamp) { if ($list_2[$filepath] !== $timestamp)
{
if ($this->debug) if ($this->debug)
echo "le fichier $filepath a ete modifie depuis le dernier passage...\n"; echo "le fichier $filepath a ete modifie depuis le dernier passage...\n";
continue; continue;
@@ -313,7 +376,8 @@ class task_period_ftpPull extends task_appboxAbstract
$finalpath = p4string::addEndSlash($this->localpath) . ($filepath[0] == '/' ? mb_substr($filepath, 1) : $filepath); $finalpath = p4string::addEndSlash($this->localpath) . ($filepath[0] == '/' ? mb_substr($filepath, 1) : $filepath);
echo "Ok pour rappatriement de $filepath vers $finalpath\n"; echo "Ok pour rappatriement de $filepath vers $finalpath\n";
try { try
{
if (file_exists($finalpath)) if (file_exists($finalpath))
throw new Exception("Un fichier du meme nom ($finalpath) existe deja..."); throw new Exception("Un fichier du meme nom ($finalpath) existe deja...");
@@ -321,7 +385,9 @@ class task_period_ftpPull extends task_appboxAbstract
$ftp->get($finalpath, $filepath); $ftp->get($finalpath, $filepath);
$ftp->delete($filepath); $ftp->delete($filepath);
} catch (Exception $e) { }
catch (Exception $e)
{
if ($this->debug) if ($this->debug)
echo "Erreur lors du rappatriement de $filepath : " . $e->getMessage() . "\n"; echo "Erreur lors du rappatriement de $filepath : " . $e->getMessage() . "\n";
} }
@@ -330,10 +396,13 @@ class task_period_ftpPull extends task_appboxAbstract
$ftp->close(); $ftp->close();
$this->setProgress(0, 0); $this->setProgress(0, 0);
} catch (Exception $e) { }
catch (Exception $e)
{
if (isset($ftp) && $ftp instanceof ftpclient) if (isset($ftp) && $ftp instanceof ftpclient)
$ftp->close(); $ftp->close();
echo $e->getMessage() . "\n"; echo $e->getMessage() . "\n";
return array();
} }
} }
@@ -346,4 +415,5 @@ class task_period_ftpPull extends task_appboxAbstract
{ {
} }
} }