1 | <?php |
---|
2 | /** |
---|
3 | * GetID3 |
---|
4 | * Gestion des métadonnées de fichiers sonores et vidéos directement dans SPIP |
---|
5 | * |
---|
6 | * Auteurs : |
---|
7 | * kent1 (http://www.kent1.info - kent1@arscenic.info), BoOz |
---|
8 | * 2008-2013 - Distribué sous licence GNU/GPL |
---|
9 | * |
---|
10 | */ |
---|
11 | |
---|
12 | if (!defined('_ECRIRE_INC_VERSION')) return; |
---|
13 | |
---|
14 | /** |
---|
15 | * Récupération des informations d'un document ou d'un fichier audio ou vidéo |
---|
16 | * |
---|
17 | * Si on a un id_document (en premier argument) on enregistre en base dans cette fonction |
---|
18 | * Si on a seulement un chemin de fichier (en second argument), on retourne un tableau des metas |
---|
19 | * |
---|
20 | * @param int/bool $id_document |
---|
21 | * Identifiant numérique duquel on doit récupérer les infos |
---|
22 | * @param string/bool $fichier |
---|
23 | * Chemin du fichier duquel on doit récupérer les infos |
---|
24 | * @param bool $logo |
---|
25 | * Doit on récupérer une vignette |
---|
26 | * @param bool $only_return |
---|
27 | * Ne fait t'on que retourner le résultat |
---|
28 | * @return array $infos |
---|
29 | * Les infos récupérées |
---|
30 | */ |
---|
31 | |
---|
32 | function inc_getid3_recuperer_infos($id_document=false,$fichier=null,$logo=false,$only_return=false){ |
---|
33 | |
---|
34 | if((!intval($id_document) && !isset($fichier))) |
---|
35 | return false; |
---|
36 | |
---|
37 | if(!function_exists('lire_config')) |
---|
38 | include_spip('inc/config'); |
---|
39 | |
---|
40 | if(!isset($fichier)){ |
---|
41 | include_spip('inc/documents'); |
---|
42 | $document = sql_fetsel("*", "spip_documents","id_document=".intval($id_document)); |
---|
43 | $fichier = get_spip_doc($document['fichier']); |
---|
44 | $extension = $document['extension']; |
---|
45 | }else |
---|
46 | $extension = strtolower(array_pop(explode('.',basename($fichier)))); |
---|
47 | |
---|
48 | /** |
---|
49 | * Récupération des metas du fichier |
---|
50 | */ |
---|
51 | $recuperer_id3 = charger_fonction('recuperer_id3','inc'); |
---|
52 | $infos = $recuperer_id3($fichier); |
---|
53 | |
---|
54 | /** |
---|
55 | * On enlève les infos vides |
---|
56 | */ |
---|
57 | if(strlen($document['titre']) > 0) |
---|
58 | unset($infos['titre']); |
---|
59 | |
---|
60 | if(strlen($document['descriptif']) > 0) |
---|
61 | unset($infos['descriptif']); |
---|
62 | |
---|
63 | foreach($infos as $key => $val){ |
---|
64 | if(!$val) |
---|
65 | unset($infos[$key]); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Si les champs sont vides, on ne les enregistre pas |
---|
70 | * Par contre s'ils sont présents dans le $_POST ou $_GET, |
---|
71 | * on les utilise (fin de conversion où on récupère le titre et autres infos du document original) |
---|
72 | */ |
---|
73 | if(!function_exists('filtrer_entites')) |
---|
74 | include_spip('inc/filtres'); |
---|
75 | foreach(array('titre','descriptif','credit') as $champ){ |
---|
76 | if(isset($infos[$champ])) |
---|
77 | $infos[$champ] = filtrer_entites($infos[$champ]); |
---|
78 | if(!isset($infos[$champ])) |
---|
79 | $infos[$champ] = ''; |
---|
80 | if(is_null($infos[$champ]) OR ($infos[$champ]=='')){ |
---|
81 | if(_request($champ)) |
---|
82 | $infos[$champ] = _request($champ); |
---|
83 | else |
---|
84 | unset($infos[$champ]); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Filesize tout seul est limité à 2Go |
---|
90 | * cf http://php.net/manual/fr/function.filesize.php#refsect1-function.filesize-returnvalues |
---|
91 | */ |
---|
92 | if(($infos['taille'] = @intval(filesize($fichier))) == '2147483647') |
---|
93 | $infos['taille'] = sprintf("%u", filesize($fichier)); |
---|
94 | |
---|
95 | /** |
---|
96 | * Si on a gis et que les fonctions de récupération de metadonnés nous ont renvoyé : |
---|
97 | * -* lat = la latitude; |
---|
98 | * -* lon = la longitude; |
---|
99 | * |
---|
100 | * Deux cas : |
---|
101 | * -* Si on a un id_document numérique |
---|
102 | * -** On recherche si on a déjà un point lié au document et on le modifie |
---|
103 | * -** Sinon on crée de nouvelles coordonnées |
---|
104 | * -* Si on n'a pas d'id_document (cas des metadonnées récupérées par les fonctions metadatas/....php) |
---|
105 | * -** On crée un point avec les coordonnées et on envoit dans le $_POST id_gis_meta |
---|
106 | * pour que le point soit lié dans le pipeline post_edition |
---|
107 | */ |
---|
108 | if(defined('_DIR_PLUGIN_GIS') && !$only_return && is_numeric($infos['latitude']) && is_numeric($infos['longitude'])){ |
---|
109 | $zoom = lire_config('gis/zoom',4); |
---|
110 | $config = @unserialize($GLOBALS['meta']['gis']); |
---|
111 | $c = array( |
---|
112 | 'titre' => $infos['titre'] ? $infos['titre'] : basename($fichier), |
---|
113 | 'lat'=> $infos['latitude'], |
---|
114 | 'lon' => $infos['longitude'], |
---|
115 | 'zoom' => $zoom |
---|
116 | ); |
---|
117 | |
---|
118 | if (defined('_DIR_PLUGIN_GISGEOM')) { |
---|
119 | $geojson = '{"type":"Point","coordinates":['.$infos['longitude'].','.$infos['latitude'].']}'; |
---|
120 | set_request('geojson',$geojson); |
---|
121 | } |
---|
122 | |
---|
123 | include_spip('action/editer_gis'); |
---|
124 | |
---|
125 | if(intval($id_document)){ |
---|
126 | if($id_gis = sql_getfetsel("G.id_gis","spip_gis AS G LEFT JOIN spip_gis_liens AS T ON T.id_gis=G.id_gis ","T.id_objet=" . intval($id_document) . " AND T.objet='document'")){ |
---|
127 | /** |
---|
128 | * Des coordonnées sont déjà définies pour ce document => on les update |
---|
129 | */ |
---|
130 | revisions_gis($id_gis,$c); |
---|
131 | }else{ |
---|
132 | /** |
---|
133 | * Aucune coordonnée n'est définie pour ce document => on les crée |
---|
134 | */ |
---|
135 | $id_gis = insert_gis(); |
---|
136 | revisions_gis($id_gis,$c); |
---|
137 | lier_gis($id_gis, 'document', $id_document); |
---|
138 | } |
---|
139 | }else{ |
---|
140 | /** |
---|
141 | * Aucune coordonnée n'est définie pour ce document => on les crée |
---|
142 | * On ajoute dans le $_POST id_gis_meta qui sera utilisable dans post_edition |
---|
143 | */ |
---|
144 | $id_gis = insert_gis(); |
---|
145 | revisions_gis($id_gis,$c); |
---|
146 | set_request('id_gis_meta',$id_gis); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * On remplit les champs de base de SPIP avec ce dont on dispose |
---|
152 | * |
---|
153 | * -* titre |
---|
154 | * -* descriptif |
---|
155 | */ |
---|
156 | if(isset($infos['title'])) |
---|
157 | $infos['titre'] = preg_replace('/_/',' ',utf8_encode($infos['title'])); |
---|
158 | |
---|
159 | else if(!isset($infos['title'])){ |
---|
160 | $titre = utf8_encode(strtolower(array_shift(explode('.',basename($fichier))))); |
---|
161 | $infos['titre'] = preg_replace('/_/',' ',$titre); |
---|
162 | } |
---|
163 | |
---|
164 | if(!isset($infos['descriptif'])){ |
---|
165 | /** |
---|
166 | * Ne pas prendre les comments foireux d'itunes |
---|
167 | */ |
---|
168 | if(isset($infos['comments']) && !preg_match('/0000[a-b|0-9]{4}/',$infos['comments'])) |
---|
169 | $infos['descriptif'] = utf8_encode($infos['comments']); |
---|
170 | else{ |
---|
171 | if(isset($infos['artist'])) |
---|
172 | $infos['descriptif'] .= utf8_encode($infos['artist'])."\n"; |
---|
173 | if(isset($infos['album'])) |
---|
174 | $infos['descriptif'] .= utf8_encode($infos['album'])."\n"; |
---|
175 | if(isset($infos['year'])) |
---|
176 | $infos['descriptif'] .= utf8_encode($infos['year'])."\n"; |
---|
177 | if(isset($infos['genre'])) |
---|
178 | $infos['descriptif'] .= utf8_encode($infos['genre'])."\n"; |
---|
179 | if(isset($infos['track_number'])) |
---|
180 | $infos['descriptif'] .= utf8_encode($infos['track_number'])."\n"; |
---|
181 | if(isset($infos['comment']) && !preg_match('/0000[a-b|0-9]{4}/',$infos['comment'])) |
---|
182 | $infos['descriptif'] .= "\n".utf8_encode($infos['comment'])."\n"; |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | /** |
---|
187 | * Les covers potentielles |
---|
188 | * On ne tente de récupération de vignettes que lorsque l'on n'a pas de vignettes |
---|
189 | * associée au document |
---|
190 | * On ne met en vignette de document que la première que l'on trouve |
---|
191 | */ |
---|
192 | $covers = array(); |
---|
193 | foreach($infos as $key=>$val){ |
---|
194 | if(preg_match('/cover/',$key)) |
---|
195 | $covers[] = $val; |
---|
196 | } |
---|
197 | |
---|
198 | $infos['credits'] = $infos['copyright_message']?$infos['copyright_message']:$infos['copyright']; |
---|
199 | |
---|
200 | if(!$infos['audiobitrate'] && !$infos['hasvideo']) |
---|
201 | $infos['audiobitrate'] = intval($infos['bitrate']); |
---|
202 | |
---|
203 | if(isset($infos['encoded_by'])) |
---|
204 | $infos['encodeur'] = $infos['encoded_by']; |
---|
205 | |
---|
206 | if((isset($infos['date']) OR isset($infos['original_release_time']) OR isset($infos['encoded_time']))){ |
---|
207 | if(preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/',$infos['date'])) |
---|
208 | $infos['date'] = $infos['date']; |
---|
209 | else if(preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/',$infos['original_release_time'])) |
---|
210 | $infos['date'] = $infos['original_release_time']; |
---|
211 | else if(preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/',$infos['encoded_time'])) |
---|
212 | $infos['date'] = $infos['encoded_time']; |
---|
213 | |
---|
214 | if(isset($valeurs['date']) && (strlen($valeurs['date'])=='10')) |
---|
215 | $infos['date'] = $infos['date'].' 00:00:00'; |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * Si on a du contenu dans les messages de copyright, |
---|
220 | * on essaie de trouver la licence, si on a le plugin Licence |
---|
221 | * |
---|
222 | * Pour l'instant uniquement valable sur les CC |
---|
223 | */ |
---|
224 | if(defined('_DIR_PLUGIN_LICENCE') && ((strlen($infos['copyright_message']) > 0) OR strlen($infos['copyright']) > 0)){ |
---|
225 | include_spip('licence_fonctions'); |
---|
226 | if(function_exists('licence_recuperer_texte')){ |
---|
227 | foreach(array($infos['copyright_message'],$infos['copyright']) as $contenu){ |
---|
228 | $infos['id_licence'] = licence_recuperer_texte($contenu); |
---|
229 | if(intval($infos['id_licence'])) |
---|
230 | break; |
---|
231 | } |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | if((count($covers) > 0) && !$only_return){ |
---|
236 | if(intval($id_document)) |
---|
237 | $id_vignette = sql_getfetsel('id_vignette','spip_documents','id_document='.intval($id_document)); |
---|
238 | else |
---|
239 | $id_vignette = 0; |
---|
240 | |
---|
241 | if(($id_vignette == 0)){ |
---|
242 | include_spip('inc/joindre_document'); |
---|
243 | $ajouter_documents = charger_fonction('ajouter_documents', 'action'); |
---|
244 | |
---|
245 | list($extension,$arg) = fixer_extension_document($covers[0]); |
---|
246 | $cover_ajout = array(array('tmp_name'=>$covers[0],'name'=> basename($covers[0]))); |
---|
247 | $ajoute = $ajouter_documents('new',$cover_ajout,'',0,'vignette'); |
---|
248 | |
---|
249 | if (is_numeric(reset($ajoute)) |
---|
250 | AND $id_vignette = reset($ajoute)) |
---|
251 | $infos['id_vignette'] = $id_vignette; |
---|
252 | } |
---|
253 | }else if(!$only_return && strlen($cover_defaut = lire_config('getid3/cover_defaut','')) > 1){ |
---|
254 | /** |
---|
255 | * Si on n'a pas de cover, |
---|
256 | * On ajoute la cover par défaut si elle existe comme vignette de document et |
---|
257 | * comme cover du fichier |
---|
258 | */ |
---|
259 | if(intval($id_document)) |
---|
260 | $id_vignette = sql_getfetsel('id_vignette','spip_documents','id_document='.intval($id_document)); |
---|
261 | else |
---|
262 | $id_vignette = 0; |
---|
263 | |
---|
264 | if(($id_vignette == 0)){ |
---|
265 | include_spip('inc/joindre_document'); |
---|
266 | include_spip('inc/distant'); |
---|
267 | $cover_defaut = find_in_path(copie_locale($cover_defaut)); |
---|
268 | $ajouter_documents = charger_fonction('ajouter_documents', 'action'); |
---|
269 | |
---|
270 | list($extension,$arg) = fixer_extension_document($cover_defaut); |
---|
271 | $cover_defaut = array(array('tmp_name'=>$cover_defaut,'name'=> basename($cover_defaut))); |
---|
272 | $ajoute = $ajouter_documents($id_vignette,$cover_defaut,'',0,'vignette'); |
---|
273 | |
---|
274 | if (is_numeric(reset($ajoute)) |
---|
275 | AND $id_vignette = reset($ajoute)){ |
---|
276 | $infos['id_vignette'] = $id_vignette; |
---|
277 | } |
---|
278 | } |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | * Si on a $only_return à true, on souhaite juste retourner les metas, sinon on les enregistre en base |
---|
283 | * Utile pour metadatas/video par exemple |
---|
284 | */ |
---|
285 | if(!$only_return && (intval($id_document) && (count($infos) > 0))){ |
---|
286 | foreach($infos as $champ=>$val){ |
---|
287 | if($document[$champ] == $val) |
---|
288 | unset($infos[$champ]); |
---|
289 | } |
---|
290 | if(count($infos) > 0){ |
---|
291 | include_spip('action/editer_document'); |
---|
292 | document_modifier($id_document, $infos); |
---|
293 | } |
---|
294 | return true; |
---|
295 | } |
---|
296 | |
---|
297 | return $infos; |
---|
298 | } |
---|
299 | |
---|
300 | ?> |
---|