[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins / odt2spip_32 / inc / libreoffice.php
1 <?php
2
3 class LibreOffice {
4 const APP_DEFAULT_PARAMS = '--headless';
5
6 /** @var string */
7 protected $fichier;
8
9 /** @var string[] */
10 protected $command = array();
11
12 /** @var string[] */
13 protected $errors = array();
14
15 /** @var string */
16 protected $outputDir = '';
17
18 /** @var string */
19 protected $convertTo = 'odt';
20
21 public function __construct($fichier) {
22 $this->fichier = $fichier;
23 return $this;
24 }
25
26 public function setConvertTo($valeur) {
27 return $this->addCommandParam('convert-to', $valeur);
28 }
29
30 public function setOutputDir($valeur) {
31 return $this->addCommandParam('outdir', $valeur);
32 }
33
34 public function addCommandParam($name, $valeur = null) {
35 switch ($name) {
36 case 'convert-to':
37 $this->convertTo = $valeur;
38 break;
39 case 'outdir':
40 $valeur = rtrim($valeur, DIRECTORY_SEPARATOR);
41 $this->outputDir = $valeur;
42 break;
43 }
44 if (is_null($valeur)) {
45 $this->command[] = ' --' . $name;
46 } else {
47 $this->command[] = ' --' . $name . ' ' . addslashes($valeur);
48 }
49 return $this;
50 }
51
52 public function addCommandArgument($valeur) {
53 $this->command[] = $valeur;
54 return $this;
55 }
56
57 public function execute() {
58 $command = $this->createCommand();
59
60 spip_log("Commande exécutée : '$command'", 'odtspip.' . _LOG_DEBUG);
61 exec($command, $output, $err);
62
63 // $output[0] :
64 // convert [...]tmp/odt2spip/1/simple.docx -> [...]tmp/odt2spip/1/simple.odt using filter : writer8
65
66 if ($err) {
67 spip_log($err, 'odtspip.' . _LOG_DEBUG);
68 $this->addError('Erreur dans l’exécution de la commande de conversion de document');
69 }
70
71 spip_log($output, 'odtspip.' . _LOG_DEBUG);
72
73 return $this;
74 }
75
76 /**
77 * Crée la commande à exécuter sur le serveur
78 * @return string
79 */
80 public function createCommand() {
81 if (defined('_LIBREOFFICE_PATH') and _LIBREOFFICE_PATH) {
82 $command = _LIBREOFFICE_PATH;
83 } else {
84 include_spip('inc/odt2spip');
85 $command = odt2spip_obtenir_commande_serveur('libreoffice');
86 }
87 $params = defined('_LIBREOFFICE_DEFAULT_PARAMS') ? _LIBREOFFICE_DEFAULT_PARAMS : static::APP_DEFAULT_PARAMS;
88 if ($params) {
89 $command .= ' ' . $params;
90 }
91 $params = implode(' ', $this->command);
92 if ($params) {
93 $command .= ' ' . $params;
94 }
95 $command .= ' ' . str_replace(' ', '\ ', $this->fichier);
96
97 // il doit pouvoir écrire quelque part
98 if (defined('_LIBREOFFICE_HOME') and _LIBREOFFICE_HOME) {
99 $home = _LIBREOFFICE_HOME;
100 } else {
101 $home = $this->outputDir ? $this->outputDir : dirname($this->fichier);
102 }
103
104 $command = 'export HOME=' . realpath($home) . '; ' . $command;
105
106 return $command;
107 }
108
109 /**
110 * Ajoute une erreur
111 * @return string[]
112 */
113 public function addError($erreur) {
114 $this->errors[] = $erreur;
115 }
116
117 /**
118 * Retourne les erreurs
119 * @return string[]
120 */
121 public function getErrors() {
122 return $this->errors;
123 }
124
125 /**
126 * Retourne le chemin du fichier créé.
127 * @return bool|string
128 */
129 public function getConvertedFile() {
130 $file =
131 $this->outputDir
132 . DIRECTORY_SEPARATOR
133 . pathinfo($this->fichier, \PATHINFO_FILENAME)
134 . '.' . strtolower($this->convertTo);
135
136 if (file_exists($file)) {
137 return $file;
138 }
139 return false;
140 }
141 }