[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins / odt2spip_32 / inc / convertir_avec_libreoffice.php
diff --git a/www/plugins/odt2spip_32/inc/convertir_avec_libreoffice.php b/www/plugins/odt2spip_32/inc/convertir_avec_libreoffice.php
new file mode 100644 (file)
index 0000000..a43a27b
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+/**
+ * Convertit un fichier en utilisant l’application libreoffice.
+ *
+ * @note : nécessite que libreoffice soit installée sur le serveur.
+ */
+
+if (!defined('_ECRIRE_INC_VERSION')) {
+       return;
+}
+
+
+/**
+ * Convertit un document transmis dans le format spécifié.
+ *
+ * @param string $fichier (chemin)
+ * @param string $format_destination
+ * @return bool|string
+ */
+function convertir_avec_libreoffice($fichier, $format_destination = 'odt') {
+       if (!file_exists($fichier) or !is_readable($fichier)) {
+               return false;
+       }
+
+       include_spip('inc/libreoffice');
+       $destination = odt2spip_get_repertoire_temporaire();
+
+       try {
+               $libreoffice = (new LibreOffice($fichier))
+                       ->setConvertTo($format_destination)
+                       ->setOutputDir($destination)
+                       ->execute();
+       } catch (\Exception $e) {
+               spip_log($e->getMessage(), 'odtspip.' . _LOG_ERREUR);
+               return false;
+       }
+
+       if ($libreoffice->getErrors()) {
+               return false;
+       }
+
+       return $libreoffice->getConvertedFile();
+}
\ No newline at end of file