From 4f3478fcd31bb42e618e66a479ff15d6efca4124 Mon Sep 17 00:00:00 2001
From: "cedric@yterium.com" <>
Date: Fri, 11 Feb 2011 21:08:35 +0000
Subject: [PATCH] extraire les methodes de determination des metas data
 (largeur, hauteur, duree, ...) par extension, afin de permettre d'ajouter
 facilement de nouvelles methodes. Integrer la methode de lecture de la taille
 et duree des flv (lapin_malin)

---
 .gitattributes              |   4 ++
 inc/renseigner_document.php | 124 ++++++------------------------------
 metadata/flv.php            |  36 +++++++++++
 metadata/image.php          |  56 ++++++++++++++++
 metadata/svg.php            |  63 ++++++++++++++++++
 metadata/swf.php            |  18 ++++++
 6 files changed, 197 insertions(+), 104 deletions(-)
 create mode 100644 metadata/flv.php
 create mode 100644 metadata/image.php
 create mode 100644 metadata/svg.php
 create mode 100644 metadata/swf.php

diff --git a/.gitattributes b/.gitattributes
index 96d2cd57..a43e58d1 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -71,6 +71,10 @@ lang/medias_fr.php -text
 /medias_autoriser.php -text
 /medias_fonctions.php -text
 /medias_pipelines.php -text
+metadata/flv.php -text
+metadata/image.php -text
+metadata/svg.php -text
+metadata/swf.php -text
 modalbox/jquery.simplemodal-1.3.3.js -text
 modalbox/modal_closebox.png -text
 modalbox/modalbox.js -text
diff --git a/inc/renseigner_document.php b/inc/renseigner_document.php
index d2fd721b..f1d72c5e 100644
--- a/inc/renseigner_document.php
+++ b/inc/renseigner_document.php
@@ -52,10 +52,14 @@ function renseigner_source_distante($source){
  * @return <type>
  */
 function renseigner_taille_dimension_image($fichier,$ext){
-	$infos = array();
-	
-	$infos['type_image'] = false;
 
+	$infos = array(
+		'largeur'=>0,
+		'hauteur'=>0,
+		'type_image'=>'',
+		'taille'=>0
+	);
+	
 	// Quelques infos sur le fichier
 	if (
 	    !$fichier
@@ -65,110 +69,22 @@ function renseigner_taille_dimension_image($fichier,$ext){
 		return _T('medias:erreur_copie_fichier',array('nom'=> $fichier));
 	}
 
-	// VIDEOS : Prevoir traitement specifique ?
-	// (http://www.getid3.org/ peut-etre)
-	if ($ext == "mov") {
-		$infos['largeur'] = 0;
-		$infos['hauteur'] = 0;
-	}
-	
-	// SVG : recuperer les dimensions et supprimer les scripts
-	elseif ($ext == "svg") {
-		list($infos['largeur'],$infos['hauteur'])= traite_svg($fichier);
-	}
-	
-	// image ?
-	else {
-		
-		// Si c'est une image, recuperer sa taille et son type (detecte aussi swf)
-		$size_image = @getimagesize($fichier);
-		$infos['largeur'] = intval($size_image[0]);
-		$infos['hauteur'] = intval($size_image[1]);
-		$infos['type_image'] = decoder_type_image($size_image[2]);
+	// chercher une fonction de description
+	$meta = array();
+	if ($metadata = charger_fonction($ext,"metadata",true)){
+		$meta = $metadata($fichier);
 	}
+  else {
+	  $media = sql_getfetsel('media','spip_types_documents','extension='.sql_quote($ext));
+	  if ($metadata = charger_fonction($media,"metadata",true)){
+		  $meta = $metadata($fichier);
+	  }
+  }
+  foreach($meta as $m=>$v)
+	  if (in_array($m,array('largeur','hauteur','type_image'/*,'duree'*/)))
+		  $infos[$m] = $v;
 
 	return $infos;
 }
 
-if (!function_exists('traite_svg')){
-/**
- * Determiner les dimensions d'un svg, et enlever ses scripts si necessaire
- *
- * @param string $file
- * @return array
- */
-// http://doc.spip.org/@traite_svg
-function traite_svg($file)
-{
-	$texte = spip_file_get_contents($file);
-
-	// Securite si pas admin : virer les scripts et les references externes
-	// sauf si on est en mode javascript 'ok' (1), cf. inc_version
-	if ($GLOBALS['filtrer_javascript'] < 1
-	AND $GLOBALS['visiteur_session']['statut'] != '0minirezo') {
-		include_spip('inc/texte');
-		$new = trim(safehtml($texte));
-		// petit bug safehtml
-		if (substr($new,0,2) == ']>') $new = ltrim(substr($new,2));
-		if ($new != $texte) ecrire_fichier($file, $texte = $new);
-	}
-
-	$width = $height = 150;
-	if (preg_match(',<svg[^>]+>,', $texte, $s)) {
-		$s = $s[0];
-		if (preg_match(',\WviewBox\s*=\s*.\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+),i', $s, $r)){
-			$width = $r[3];
-                	$height = $r[4];
-		} else {
-	// si la taille est en centimetre, estimer le pixel a 1/64 de cm
-		if (preg_match(',\Wwidth\s*=\s*.(\d+)([^"\']*),i', $s, $r)){
-			if ($r[2] != '%') {
-				$width = $r[1];
-				if ($r[2] == 'cm') $width <<=6;
-			}	
-		}
-		if (preg_match(',\Wheight\s*=\s*.(\d+)([^"\']*),i', $s, $r)){
-			if ($r[2] != '%') {
-	                	$height = $r[1];
-				if ($r[2] == 'cm') $height <<=6;
-			}
-		}
-	   }
-	}
-	return array($width, $height);
-}
-}
-
-if (!function_exists('decoder_type_image')){
-/**
- * Convertit le type numerique retourne par getimagesize() en extension fichier
- *
- * @param int $type
- * @param bool $strict
- * @return string
- */
-// http://doc.spip.org/@decoder_type_image
-function decoder_type_image($type, $strict = false) {
-	switch ($type) {
-		case 1:
-			return "gif";
-		case 2:
-			return "jpg";
-		case 3:
-			return "png";
-		case 4:
-			return $strict ? "" : "swf";
-		case 5:
-			return "psd";
-		case 6:
-			return "bmp";
-		case 7:
-		case 8:
-			return "tif";
-		default:
-			return "";
-	}
-}
-}
-
 ?>
\ No newline at end of file
diff --git a/metadata/flv.php b/metadata/flv.php
new file mode 100644
index 00000000..a3eb2403
--- /dev/null
+++ b/metadata/flv.php
@@ -0,0 +1,36 @@
+<?php
+
+/***************************************************************************\
+ *  SPIP, Systeme de publication pour l'internet                           *
+ *                                                                         *
+ *  Copyright (c) 2001-2011                                                *
+ *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
+ *                                                                         *
+ *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
+ *  Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne.   *
+\***************************************************************************/
+
+if (!defined('_ECRIRE_INC_VERSION')) return;
+
+function metadata_flv_dist($file, $bigindian = true){
+	$meta = array();
+
+	if ($fd = fopen($file, 'r')
+	  AND $raw = fread($fd, 512)){
+
+		$keys = array('largeur'=>'width', 'hauteur'=>'height','duree'=>'duration','framerate'=>'framerate');
+		foreach ($keys as $m=>$k) {
+			if (($i = strpos($raw, $k))>-1){
+				$bytes = substr($raw, $i+strlen($k)+1, 8);
+				if ($bigindian)
+					$bytes = strrev($bytes);
+				$zz = unpack("dflt", $bytes); // unpack the bytes
+				$meta[$m] = $zz['flt']; // return the number from the associative array
+			}
+		}
+	}
+
+	return $meta;
+}
+
+?>
\ No newline at end of file
diff --git a/metadata/image.php b/metadata/image.php
new file mode 100644
index 00000000..544514cd
--- /dev/null
+++ b/metadata/image.php
@@ -0,0 +1,56 @@
+<?php
+
+/***************************************************************************\
+ *  SPIP, Systeme de publication pour l'internet                           *
+ *                                                                         *
+ *  Copyright (c) 2001-2011                                                *
+ *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
+ *                                                                         *
+ *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
+ *  Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne.   *
+\***************************************************************************/
+
+if (!defined('_ECRIRE_INC_VERSION')) return;
+
+function metadata_image_dist($fichier){
+	$meta = array();
+
+	if ($size_image = @getimagesize($fichier)) {
+		$meta['largeur'] = intval($size_image[0]);
+		$meta['hauteur'] = intval($size_image[1]);
+		$meta['type_image'] = decoder_type_image($size_image[2]);
+	}
+
+	return $meta;
+}
+
+/**
+ * Convertit le type numerique retourne par getimagesize() en extension fichier
+ *
+ * @param int $type
+ * @param bool $strict
+ * @return string
+ */
+// http://doc.spip.org/@decoder_type_image
+function decoder_type_image($type, $strict = false) {
+	switch ($type) {
+		case 1:
+			return "gif";
+		case 2:
+			return "jpg";
+		case 3:
+			return "png";
+		case 4:
+			return $strict ? "" : "swf";
+		case 5:
+			return "psd";
+		case 6:
+			return "bmp";
+		case 7:
+		case 8:
+			return "tif";
+		default:
+			return "";
+	}
+}
+?>
\ No newline at end of file
diff --git a/metadata/svg.php b/metadata/svg.php
new file mode 100644
index 00000000..022233d1
--- /dev/null
+++ b/metadata/svg.php
@@ -0,0 +1,63 @@
+<?php
+
+/***************************************************************************\
+ *  SPIP, Systeme de publication pour l'internet                           *
+ *                                                                         *
+ *  Copyright (c) 2001-2011                                                *
+ *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
+ *                                                                         *
+ *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
+ *  Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne.   *
+\***************************************************************************/
+
+if (!defined('_ECRIRE_INC_VERSION')) return;
+
+/**
+ * Determiner les dimensions d'un svg, et enlever ses scripts si necessaire
+ *
+ * @param string $file
+ * @return array
+ */
+// http://doc.spip.org/@traite_svg
+function medata_svg_dist($file){
+	$meta = array();
+
+	$texte = spip_file_get_contents($file);
+
+	// Securite si pas admin : virer les scripts et les references externes
+	// sauf si on est en mode javascript 'ok' (1), cf. inc_version
+	if ($GLOBALS['filtrer_javascript'] < 1
+	AND $GLOBALS['visiteur_session']['statut'] != '0minirezo') {
+		include_spip('inc/texte');
+		$new = trim(safehtml($texte));
+		// petit bug safehtml
+		if (substr($new,0,2) == ']>') $new = ltrim(substr($new,2));
+		if ($new != $texte) ecrire_fichier($file, $texte = $new);
+	}
+
+	$width = $height = 150;
+	if (preg_match(',<svg[^>]+>,', $texte, $s)) {
+		$s = $s[0];
+		if (preg_match(',\WviewBox\s*=\s*.\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+),i', $s, $r)){
+			$width = $r[3];
+                	$height = $r[4];
+		} else {
+	// si la taille est en centimetre, estimer le pixel a 1/64 de cm
+		if (preg_match(',\Wwidth\s*=\s*.(\d+)([^"\']*),i', $s, $r)){
+			if ($r[2] != '%') {
+				$width = $r[1];
+				if ($r[2] == 'cm') $width <<=6;
+			}
+		}
+		if (preg_match(',\Wheight\s*=\s*.(\d+)([^"\']*),i', $s, $r)){
+			if ($r[2] != '%') {
+	                	$height = $r[1];
+				if ($r[2] == 'cm') $height <<=6;
+			}
+		}
+	   }
+	}
+	$meta['largeur'] = $width;
+	$meta['hauteur'] = $height;
+	return $meta;
+}
\ No newline at end of file
diff --git a/metadata/swf.php b/metadata/swf.php
new file mode 100644
index 00000000..ab1ceb5b
--- /dev/null
+++ b/metadata/swf.php
@@ -0,0 +1,18 @@
+<?php
+
+/***************************************************************************\
+ *  SPIP, Systeme de publication pour l'internet                           *
+ *                                                                         *
+ *  Copyright (c) 2001-2011                                                *
+ *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
+ *                                                                         *
+ *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
+ *  Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne.   *
+\***************************************************************************/
+
+if (!defined('_ECRIRE_INC_VERSION')) return;
+
+function metadata_swf_dist($file){
+	$metadata = charger_fonction('image','metadata');
+	return $metadata($file);
+}
\ No newline at end of file
-- 
GitLab