4 const APP_DEFAULT_PARAMS
= '--headless';
10 protected $command = array();
13 protected $errors = array();
16 protected $outputDir = '';
19 protected $convertTo = 'odt';
21 public function __construct($fichier) {
22 $this->fichier
= $fichier;
26 public function setConvertTo($valeur) {
27 return $this->addCommandParam('convert-to', $valeur);
30 public function setOutputDir($valeur) {
31 return $this->addCommandParam('outdir', $valeur);
34 public function addCommandParam($name, $valeur = null) {
37 $this->convertTo
= $valeur;
40 $valeur = rtrim($valeur, DIRECTORY_SEPARATOR
);
41 $this->outputDir
= $valeur;
44 if (is_null($valeur)) {
45 $this->command
[] = ' --' . $name;
47 $this->command
[] = ' --' . $name . ' ' . addslashes($valeur);
52 public function addCommandArgument($valeur) {
53 $this->command
[] = $valeur;
57 public function execute() {
58 $command = $this->createCommand();
60 spip_log("Commande exécutée : '$command'", 'odtspip.' . _LOG_DEBUG
);
61 exec($command, $output, $err);
64 // convert [...]tmp/odt2spip/1/simple.docx -> [...]tmp/odt2spip/1/simple.odt using filter : writer8
67 spip_log($err, 'odtspip.' . _LOG_DEBUG
);
68 $this->addError('Erreur dans l’exécution de la commande de conversion de document');
71 spip_log($output, 'odtspip.' . _LOG_DEBUG
);
77 * Crée la commande à exécuter sur le serveur
80 public function createCommand() {
81 if (defined('_LIBREOFFICE_PATH') and _LIBREOFFICE_PATH
) {
82 $command = _LIBREOFFICE_PATH
;
84 include_spip('inc/odt2spip');
85 $command = odt2spip_obtenir_commande_serveur('libreoffice');
87 $params = defined('_LIBREOFFICE_DEFAULT_PARAMS') ? _LIBREOFFICE_DEFAULT_PARAMS
: static::APP_DEFAULT_PARAMS
;
89 $command .= ' ' . $params;
91 $params = implode(' ', $this->command
);
93 $command .= ' ' . $params;
95 $command .= ' ' . str_replace(' ', '\ ', $this->fichier
);
97 // il doit pouvoir écrire quelque part
98 if (defined('_LIBREOFFICE_HOME') and _LIBREOFFICE_HOME
) {
99 $home = _LIBREOFFICE_HOME
;
101 $home = $this->outputDir ?
$this->outputDir
: dirname($this->fichier
);
104 $command = 'export HOME=' . realpath($home) . '; ' . $command;
113 public function addError($erreur) {
114 $this->errors
[] = $erreur;
118 * Retourne les erreurs
121 public function getErrors() {
122 return $this->errors
;
126 * Retourne le chemin du fichier créé.
127 * @return bool|string
129 public function getConvertedFile() {
132 . DIRECTORY_SEPARATOR
133 . pathinfo($this->fichier
, \PATHINFO_FILENAME
)
134 . '.' . strtolower($this->convertTo
);
136 if (file_exists($file)) {