/**
  * {@inheritDoc}
  */
 public function getLogin()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'getLogin', array());
     return parent::getLogin();
 }
Пример #2
0
 protected function doExport(Application $app, Task $task, FtpExport $export)
 {
     $settings = simplexml_load_string($task->getSettings());
     $proxy = (string) $settings->proxy;
     $proxyport = (string) $settings->proxyport;
     $state = "";
     $ftp_server = $export->getAddr();
     $ftp_user_name = $export->getLogin();
     $ftp_user_pass = $export->getPwd();
     $ftpLog = $ftp_user_name . "@" . \p4string::addEndSlash($ftp_server) . $export->getDestfolder();
     if ($export->getCrash() == 0) {
         $line = $this->translator->trans('task::ftp:Etat d\'envoi FTP vers le serveur "%server%" avec le compte "%username%" et pour destination le dossier : "%directory%"', ['%server%' => $ftp_server, '%username%' => $ftp_user_name, '%directory%' => $export->getDestfolder()]) . PHP_EOL;
         $state .= $line;
         $this->log('debug', $line);
     }
     $state .= $line = $this->translator->trans("task::ftp:TENTATIVE no %number%, %date%", ['%number%' => $export->getCrash() + 1, '%date%' => "  (" . date('r') . ")"]) . PHP_EOL;
     $this->log('debug', $line);
     try {
         $ssl = $export->isSsl();
         $ftp_client = $app['phraseanet.ftp.client']($ftp_server, 21, 300, $ssl, $proxy, $proxyport);
         $ftp_client->login($ftp_user_name, $ftp_user_pass);
         if ($export->isPassif()) {
             try {
                 $ftp_client->passive(true);
             } catch (\Exception $e) {
                 $this->log('debug', $e->getMessage());
             }
         }
         if (trim($export->getDestfolder()) != '') {
             try {
                 $ftp_client->chdir($export->getDestFolder());
                 $export->setDestfolder('/' . $export->getDestfolder());
             } catch (\Exception $e) {
                 $this->log('debug', $e->getMessage());
             }
         } else {
             $export->setDestfolder('/');
         }
         if (trim($export->getFoldertocreate()) != '') {
             try {
                 $ftp_client->mkdir($export->getFoldertocreate());
             } catch (\Exception $e) {
                 $this->log('debug', $e->getMessage());
             }
             try {
                 $new_dir = $ftp_client->add_end_slash($export->getDestfolder()) . $export->getFoldertocreate();
                 $ftp_client->chdir($new_dir);
             } catch (\Exception $e) {
                 $this->log('debug', $e->getMessage());
             }
         }
         $obj = [];
         $basefolder = '';
         if (!in_array(trim($export->getDestfolder()), ['.', './', ''])) {
             $basefolder = \p4string::addEndSlash($export->getDestfolder());
         }
         $basefolder .= $export->getFoldertocreate();
         if (in_array(trim($basefolder), ['.', './', ''])) {
             $basefolder = '/';
         }
         foreach ($export->getElements() as $exportElement) {
             if ($exportElement->isDone()) {
                 continue;
             }
             $base_id = $exportElement->getBaseId();
             $record_id = $exportElement->getRecordId();
             $subdef = $exportElement->getSubdef();
             $localfile = null;
             try {
                 $sbas_id = \phrasea::sbasFromBas($app, $base_id);
                 $record = new \record_adapter($app, $sbas_id, $record_id);
                 $sdcaption = $app['serializer.caption']->serialize($record->get_caption(), CaptionSerializer::SERIALIZE_XML, $exportElement->isBusinessfields());
                 $remotefile = $exportElement->getFilename();
                 if ($subdef == 'caption') {
                     $desc = $app['serializer.caption']->serialize($record->get_caption(), CaptionSerializer::SERIALIZE_XML, $exportElement->isBusinessfields());
                     $localfile = sys_get_temp_dir() . '/' . md5($desc . time() . mt_rand());
                     if (file_put_contents($localfile, $desc) === false) {
                         throw new \Exception('Impossible de creer un fichier temporaire');
                     }
                 } elseif ($subdef == 'caption-yaml') {
                     $desc = $app['serializer.caption']->serialize($record->get_caption(), CaptionSerializer::SERIALIZE_YAML, $exportElement->isBusinessfields());
                     $localfile = sys_get_temp_dir() . '/' . md5($desc . time() . mt_rand());
                     if (file_put_contents($localfile, $desc) === false) {
                         throw new \Exception('Impossible de creer un fichier temporaire');
                     }
                 } else {
                     $sd = $record->get_subdefs();
                     if (!$sd || !isset($sd[$subdef])) {
                         continue;
                     }
                     $localfile = $sd[$subdef]->get_pathfile();
                     if (!file_exists($localfile)) {
                         throw new \Exception('Le fichier local n\'existe pas');
                     }
                 }
                 $current_folder = rtrim(str_replace('//', '/', $basefolder . $exportElement->getFolder()), '/');
                 if ($ftp_client->pwd() != $current_folder) {
                     try {
                         $ftp_client->chdir($current_folder);
                     } catch (\Exception $e) {
                         $this->log('debug', $e->getMessage());
                     }
                 }
                 $ftp_client->put($remotefile, $localfile);
                 $obj[] = ["name" => $subdef, "size" => filesize($localfile), "shortXml" => $sdcaption ? $sdcaption : ''];
                 if ($subdef == 'caption') {
                     unlink($localfile);
                 }
                 $exportElement->setDone(true)->setError(false);
                 $app['EM']->persist($exportElement);
                 $app['EM']->flush();
                 $this->logexport($app, $record, $obj, $ftpLog);
             } catch (\Exception $e) {
                 $state .= $line = $this->translator->trans('task::ftp:File "%file%" (record %record_id%) de la base "%basename%" (Export du Document) : Transfert cancelled (le document n\'existe plus)', ['%file%' => basename($localfile), '%record_id%' => $record_id, '%basename%' => \phrasea::sbas_labels(\phrasea::sbasFromBas($app, $base_id), $app)]) . "\n<br/>";
                 $this->log('debug', $line);
                 // One failure max
                 $exportElement->setDone($exportElement->isError())->setError(true);
                 $app['EM']->persist($exportElement);
                 $app['EM']->flush();
             }
         }
         if ($export->isLogfile()) {
             $this->log('debug', "logfile ");
             $date = new DateTime();
             $buffer = '#transfert finished ' . $date->format(DATE_ATOM) . "\n\n";
             foreach ($export->getElements() as $exportElement) {
                 if (!$exportElement->isDone() || $exportElement->isError()) {
                     continue;
                 }
                 $filename = $exportElement->getFilename();
                 $folder = $exportElement->getFilename();
                 $root = $export->getFoldertocreate();
                 $buffer .= $root . '/' . $folder . $filename . "\n";
             }
             $tmpfile = sys_get_temp_dir() . '/tmpftpbuffer' . $date->format('U') . '.txt';
             file_put_contents($tmpfile, $buffer);
             $remotefile = $date->format('U') . '-transfert.log';
             $ftp_client->chdir($export->getDestFolder());
             $ftp_client->put($remotefile, $tmpfile);
             unlink($tmpfile);
         }
         $ftp_client->close();
     } catch (\Exception $e) {
         $state .= $line = $e . "\n";
         $this->log('debug', $line);
         $export->incrementCrash();
         $app['EM']->persist($export);
         $app['EM']->flush();
     }
     $this->finalize($app, $export);
 }