[SPIP] +2.1.12
[velocampus/web/www.git] / www / ecrire / inc / documents.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2011 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13 if (!defined('_ECRIRE_INC_VERSION')) return;
14
15 include_spip('inc/actions'); // *action_auteur et determine_upload
16 include_spip('inc/date');
17
18 // donne le chemin du fichier relatif a _DIR_IMG
19 // pour stockage 'tel quel' dans la base de donnees
20 // http://doc.spip.org/@set_spip_doc
21 function set_spip_doc($fichier) {
22 if (strpos($fichier, _DIR_IMG) === 0)
23 return substr($fichier, strlen(_DIR_IMG));
24 else
25 return $fichier; // ex: fichier distant
26 }
27
28 // donne le chemin complet du fichier
29 // http://doc.spip.org/@get_spip_doc
30 function get_spip_doc($fichier) {
31 // fichier distant
32 if (preg_match(',^\w+://,', $fichier))
33 return $fichier;
34
35 // gestion d'erreurs, fichier=''
36 if (!strlen($fichier))
37 return false;
38
39 $fichier = (strpos($fichier, _DIR_IMG) === false)
40 ? _DIR_IMG . $fichier
41 : $fichier ;
42
43 // fichier normal
44 return $fichier;
45 }
46
47 // Constante indiquant le charset probable des documents non utf-8 joints
48
49 if (!defined('CHARSET_JOINT')) define('CHARSET_JOINT', 'iso-8859-1');
50
51 // Filtre pour #FICHIER permettant d'incruster le contenu d'un document
52 // Si 2e arg fourni, conversion dans le charset du site si possible
53
54 // http://doc.spip.org/@contenu_document
55 function contenu_document($arg, $charset='')
56 {
57 if (is_numeric($arg)) {
58 $r = sql_fetsel("fichier,distant", "spip_documents", "id_document=".sql_quote($arg));
59 if (!$r) return '';
60 $f = $r['fichier'];
61 $f = ($r['distant'] =='oui') ? _DIR_RACINE . copie_locale($f) : get_spip_doc($f);
62 }
63 else {
64 if (!@file_exists($f=$arg)){
65 if (!$f = copie_locale($f))
66 return '';
67 $f = _DIR_RACINE . $f;
68 }
69 }
70
71 $r = spip_file_get_contents($f);
72
73 if ($charset) {
74 include_spip('inc/charset');
75 if ($charset !== 'auto') {
76 $r = importer_charset($r, $charset);
77 } elseif ($GLOBALS['meta']['charset'] == 'utf-8' AND !is_utf8($r))
78 $r = importer_charset($r, CHARSET_JOINT);
79 }
80 return $r;
81 }
82
83 // http://doc.spip.org/@generer_url_document_dist
84 function generer_url_document_dist($id_document, $args='', $ancre='') {
85
86 include_spip('inc/autoriser');
87 if (!autoriser('voir', 'document', $id_document)) return '';
88
89 $r = sql_fetsel("fichier,distant", "spip_documents", "id_document=".sql_quote($id_document));
90
91 if (!$r) return '';
92
93 $f = $r['fichier'];
94
95 if ($r['distant'] == 'oui') return $f;
96
97 // Si droit de voir tous les docs, pas seulement celui-ci
98 // il est inutilement couteux de rajouter une protection
99
100 $r = autoriser('voir', 'document');
101
102 if ($r AND $r !== 'htaccess') return get_spip_doc($f);
103
104 include_spip('inc/securiser_action');
105
106 // cette action doit etre publique !
107 return generer_url_action('acceder_document',
108 $args . ($args ? "&" : '')
109 . 'arg='.$id_document
110 . ($ancre ? "&ancre=$ancre" : '')
111 . '&cle=' . calculer_cle_action($id_document.','.$f)
112 . '&file=' . rawurlencode($f)
113 ,false,true);
114 }
115
116 // http://doc.spip.org/@document_et_vignette
117 function document_et_vignette($document, $url, $portfolio=false) {
118 $image = $document['id_vignette'];
119
120 if ($image)
121 $image = sql_fetsel("*", "spip_documents", "id_document = ".$image);
122 if ($image) {
123 if (!$portfolio OR !($GLOBALS['meta']['creer_preview'] == 'oui')) {
124 $x = $image['largeur'];
125 $y = $image['hauteur'];
126 } else {
127 $x = 120;
128 $y = 110;
129 }
130 $image = get_spip_doc($image['fichier']);
131 } else {
132 if ($portfolio) {
133 $x = 110;
134 $y = 120;
135 } else $x = $y =-1;
136 }
137 if (!$url) $url = generer_url_document_dist($document['id_document'], 'document');
138 return vignette_automatique($image, $document, $url, $x, $y, '', "miniature_document");
139 }
140
141 //
142 // Affiche le document avec sa vignette par defaut
143 //
144 // Attention : en mode 'doc', si c'est un fichier graphique on prefere
145 // afficher une vue reduite, quand c'est possible (presque toujours, donc)
146 // En mode 'image', l'image conserve sa taille
147 //
148 // A noter : dans le portfolio prive on pousse le vice jusqu'a reduire la taille
149 // de la vignette -> c'est a ca que sert la variable $portfolio
150 // http://doc.spip.org/@vignette_automatique
151 function vignette_automatique($img, $doc, $lien, $x=0, $y=0, $align='', $class='spip_logos')
152 {
153 include_spip('inc/distant');
154 include_spip('inc/texte');
155 include_spip('inc/filtres_images_mini');
156 $e = $doc['extension'];
157 if (!$img) {
158 if ($img = image_du_document($doc)) {
159 if (!$x AND !$y) // eviter une double reduction
160 $img = image_reduire($img);
161 }
162 else{
163 $f = charger_fonction('vignette','inc');
164 $img = $f($e, false);
165 $size = @getimagesize($img);
166 $img = "<img src='$img' ".$size[3]." />";
167 }
168 }
169 else{
170 $size = @getimagesize($img);
171 $img = "<img src='$img' ".$size[3]." />";
172 }
173 // on appelle image_reduire independamment de la presence ou non
174 // des librairies graphiques
175 // la fonction sait se debrouiller et faire de son mieux dans tous les cas
176 if ($x OR $y) {
177 $img = image_reduire($img, $x, $y);
178 }
179 $img = inserer_attribut($img, 'alt', '');
180 $img = inserer_attribut($img, 'class', $class);
181 if ($align) $img = inserer_attribut($img, 'align', $align);
182
183 if (!$lien) return $img;
184
185 $titre = supprimer_tags(typo($doc['titre']));
186 $titre = " - " .taille_en_octets($doc['taille'])
187 . ($titre ? " - $titre" : "");
188
189 $type = sql_fetsel('titre, mime_type','spip_types_documents', "extension = " . sql_quote($e));
190
191 $mime = $type['mime_type'];
192 $titre = attribut_html(couper($type['titre'] . $titre, 80));
193
194 return "<a href='$lien' type='$mime' title='$titre'>$img</a>";
195 }
196
197 // Trouve une image caracteristique d'un document.
198 // Si celui-ci est une image et que les outils graphiques sont dispos,
199 // retourner le document (en exploitant sa copie locale s'il est distant).
200 // Autrement retourner la vignette fournie par SPIP pour ce type MIME
201 // Resultat: un fichier local existant
202
203 function image_du_document($document)
204 {
205 $e = $document['extension'];
206 if ((strpos($GLOBALS['meta']['formats_graphiques'], $e) !== false)
207 AND (!test_espace_prive() OR $GLOBALS['meta']['creer_preview']=='oui')
208 AND $document['fichier']) {
209 if ($document['distant'] == 'oui') {
210 $image = _DIR_RACINE.copie_locale($document['fichier']);
211 }
212 else
213 $image = get_spip_doc($document['fichier']);
214 if (@file_exists($image)) return $image;
215 }
216 return '';
217 }
218
219 //
220 // Afficher un document dans la colonne de gauche
221 //
222
223 // http://doc.spip.org/@afficher_documents_colonne
224 function afficher_documents_colonne($id, $type="article",$script=NULL) {
225 include_spip('inc/autoriser');
226
227 // il faut avoir les droits de modif sur l'article pour pouvoir uploader !
228 if (!autoriser('joindredocument',$type,$id))
229 return "";
230
231 include_spip('inc/presentation'); // pour l'aide quand on appelle afficher_documents_colonne depuis un squelette
232 // seuls cas connus : article, breve ou rubrique
233 if ($script==NULL){
234 $script = $type.'s_edit';
235 if (!test_espace_prive())
236 $script = parametre_url(self(),"show_docs",'');
237 }
238 $id_document_actif = _request('show_docs');
239
240 $joindre = charger_fonction('joindre', 'inc');
241
242 define('_INTERFACE_DOCUMENTS', true);
243 if (!_INTERFACE_DOCUMENTS
244 OR $GLOBALS['meta']["documents_$type"]=='non') {
245
246 // Ajouter nouvelle image
247 $ret = "<div id='images'>\n"
248 . $joindre(array(
249 'cadre' => 'relief',
250 'icone' => 'image-24.gif',
251 'fonction' => 'creer.gif',
252 'titre' => majuscules(_T('bouton_ajouter_image')).aide("ins_img"),
253 'script' => $script,
254 'args' => "id_$type=$id",
255 'id' => $id,
256 'intitule' => _T('info_telecharger'),
257 'mode' => 'image',
258 'type' => $type,
259 'ancre' => '',
260 'id_document' => 0,
261 'iframe_script' => generer_url_ecrire("documents_colonne","id=$id&type=$type",true)
262 ))
263 . '</div><br />';
264
265 if (!_INTERFACE_DOCUMENTS) {
266 //// Images sans documents
267 $res = sql_select("D.id_document", "spip_documents AS D LEFT JOIN spip_documents_liens AS T ON T.id_document=D.id_document", "T.id_objet=" . intval($id) . " AND T.objet=" . sql_quote($type) . " AND D.mode='image'", "", "D.id_document");
268
269 $ret .= "\n<div id='liste_images'>";
270
271 while ($doc = sql_fetch($res)) {
272 $id_document = $doc['id_document'];
273 $deplier = ($id_document_actif==$id_document);
274 $ret .= afficher_case_document($id_document, $id, $script, $type, $deplier);
275 }
276
277 $ret .= "</div><br /><br />\n";
278 }
279 }
280
281 /// Ajouter nouveau document
282 $bouton = !_INTERFACE_DOCUMENTS
283 ? majuscules(_T('bouton_ajouter_document')).aide("ins_doc")
284 : (_T('bouton_ajouter_image_document')).aide("ins_doc");
285
286 $ret .= "<div id='documents'></div>\n<div id='portfolio'></div>\n";
287 if ($GLOBALS['meta']["documents_$type"]!='non') {
288 $ret .= $joindre(array(
289 'cadre' => _INTERFACE_DOCUMENTS ? 'relief' : 'enfonce',
290 'icone' => 'doc-24.gif',
291 'fonction' => 'creer.gif',
292 'titre' => $bouton,
293 'script' => $script,
294 'args' => "id_$type=$id",
295 'id' => $id,
296 'intitule' => _T('info_telecharger'),
297 'mode' => _INTERFACE_DOCUMENTS ? 'choix' : 'document',
298 'type' => $type,
299 'ancre' => '',
300 'id_document' => 0,
301 'iframe_script' => generer_url_ecrire("documents_colonne","id=$id&type=$type",true)
302 ));
303 }
304
305 // Afficher les documents lies
306 $ret .= "<br /><div id='liste_documents'>\n";
307
308 //// Documents associes
309 $res = sql_select("D.id_document", "spip_documents AS D LEFT JOIN spip_documents_liens AS T ON T.id_document=D.id_document", "T.id_objet=" . intval($id) . " AND T.objet=" . sql_quote($type)
310 . ((!_INTERFACE_DOCUMENTS)
311 ? " AND D.mode='document'"
312 : " AND D.mode IN ('image','document')"
313 ), "", "D.mode, D.id_document");
314
315 while($row = sql_fetch($res))
316 $ret .= afficher_case_document($row['id_document'], $id, $script, $type, ($id_document_actif==$row['id_document']));
317
318 $ret .= "</div>";
319 if (test_espace_prive()){
320 $ret .= http_script('', "async_upload.js")
321 . http_script('$("form.form_upload").async_upload(async_upload_article_edit)');
322 }
323
324 return $ret;
325 }
326
327 //
328 // Affiche le raccourci <doc123|left>
329 // et l'insere quand on le clique
330 //
331 // http://doc.spip.org/@affiche_raccourci_doc
332 function affiche_raccourci_doc($doc, $id, $align) {
333 static $num = 0;
334
335 if ($align) {
336 $pipe = "|$align";
337
338 if ($GLOBALS['browser_barre'])
339 $onclick = "\nondblclick=\"barre_inserer('\\x3C$doc$id$pipe&gt;', $('textarea[name=texte]')[0]);\"\ntitle=\"". str_replace('&amp;', '&', entites_html(_T('double_clic_inserer_doc')))."\"";
340 } else {
341 $align='center';
342 }
343
344 return
345 ((++$num > 1) ? "" : http_script('', "spip_barre.js"))
346 . "\n<div style='text-align: $align'$onclick>&lt;$doc$id$pipe&gt;</div>\n";
347 }
348
349
350 // Est-ce que le document est inclus dans le texte ?
351 // http://doc.spip.org/@est_inclus
352 function est_inclus($id_document) {
353 return isset($GLOBALS['doublons_documents_inclus']) ?
354 in_array($id_document,$GLOBALS['doublons_documents_inclus']) : false;
355 }
356
357 //
358 // Afficher un document sous forme de bloc depliable
359 // en donnant un apercu
360 // et en indiquer le raccourci permettant l'incrustation
361 // Pour les distant, donner un bouton pour rappatriement (trombone)
362 // Pour les images, donnner les boutons de rotations
363
364
365 // http://doc.spip.org/@afficher_case_document
366 function afficher_case_document($id_document, $id, $script, $type, $deplier=false) {
367 global $spip_lang_right;
368
369 $document = sql_fetsel("D.id_document, D.id_vignette,D.extension,D.titre,D.descriptif,D.fichier,D.largeur,D.hauteur,D.taille,D.mode,D.distant, D.date, L.vu", "spip_documents AS D INNER JOIN spip_documents_liens AS L ON L.id_document=D.id_document", "L.id_objet=".intval($id)." AND objet=".sql_quote($type)." AND L.id_document=".intval($id_document));
370
371 if (!$document) return "";
372
373 $id_vignette = $document['id_vignette'];
374 $extension = $document['extension'];
375 $descriptif = $document['descriptif'];
376 $fichier = $document['fichier'];
377 $largeur = $document['largeur'];
378 $hauteur = $document['hauteur'];
379 $mode = $document['mode'];
380 $distant = $document['distant'];
381 $titre = $document['titre'];
382 $legender = charger_fonction('legender', 'inc');
383 $dist = '';
384
385 $r = sql_fetsel("titre,inclus", "spip_types_documents", "extension=".sql_quote($extension));
386 if ($r) {
387 $type_inclus = $r['inclus'];
388 $type_titre = $r['titre'];
389 }
390
391 if ($mode == 'document') {
392
393 if ($distant == 'oui') {
394 include_spip('inc/tourner');
395 $dist = "\n<div class='verdana1' style='float: $spip_lang_right; text-align: $spip_lang_right;'>"
396 . "\n<img src='" . chemin_image('attachment.gif') . "'\n\talt=\"$fichier\"\n\ttitle=\"$fichier\" />\n"
397 . bouton_copier_local($document, $type, $id, $id_document, $script)
398 . "</div>\n";
399 }
400
401 if (est_inclus($id_document))
402 $raccourci = affiche_raccourci_doc('doc', $id_document, '');
403 else {
404 $vign= (($type_inclus == "embed" OR $type_inclus == "image") AND $largeur > 0 AND $hauteur > 0);
405 $raccourci = $vign ? ("<b>"._T('info_inclusion_vignette')."</b><br />") : '';
406
407 $raccourci .= "<div style='color: 333333'>"
408 . affiche_raccourci_doc('doc', $id_document, 'left')
409 . affiche_raccourci_doc('doc', $id_document, 'center')
410 . affiche_raccourci_doc('doc', $id_document, 'right')
411 . "</div>\n";
412
413 if ($vign) {
414 $raccourci .= "<div style='padding:2px; ' class='arial1 spip_xx-small'>";
415 $raccourci .= "<b>"._T('info_inclusion_directe')."</b><br />";
416 $raccourci .= "<div style='color: 333333'>"
417 . affiche_raccourci_doc('emb', $id_document, 'left')
418 . affiche_raccourci_doc('emb', $id_document, 'center')
419 . affiche_raccourci_doc('emb', $id_document, 'right')
420 . "</div>\n";
421 $raccourci .= "</div>";
422 }
423 }
424 $ninclus = false;
425 $icone = 'doc-24.gif';
426 $style = 'e';
427
428 } else if ($mode == 'image') {
429
430 $icone = 'image-24.gif';
431 $style = 'r';
432 $ninclus = ($type_inclus !== 'image');
433 $doc = ($descriptif OR $titre) ? 'doc' : 'img';
434
435 if (est_inclus($id_document))
436 $raccourci = affiche_raccourci_doc($doc, $id_document, '');
437 else {
438 $raccourci =
439 affiche_raccourci_doc($doc, $id_document, 'left')
440 . affiche_raccourci_doc($doc, $id_document, 'center')
441 . affiche_raccourci_doc($doc, $id_document, 'right');
442 }
443
444 }
445 $cadre = lignes_longues(typo($titre ? $titre : basename($fichier)), 20);
446 // encapsuler chaque document dans un container pour permettre son remplacement en ajax
447 return '<div>'
448 . debut_cadre($style, $icone, '', $cadre, "document$id_document")
449 . ($ninclus ? '' :
450 ("\n<div style='text-align: center'>"
451 . $dist
452 . document_et_vignette($document, '', true)
453 . '</div>'
454 . "\n<div class='verdana1' style='text-align: center; color: black;'>\n"
455 . ($type_titre ? $type_titre :
456 ( _T('info_document').' '.majuscules($extension)))
457 . "</div>"))
458 . $apercu
459 . "\n<div style='padding:2px; ' class='arial1 spip_xx-small'>"
460 . $raccourci
461 . "</div>\n"
462 . $legender($id_document, $document, $script, $type, $id, "document$id_document", $deplier)
463 . fin_cadre($style)
464 . '</div>';
465 }
466
467 // Etablit la liste des documents orphelins, c'est-a-dire qui ne sont lies
468 // a rien ; renvoie un tableau (id_document)
469 // ici on ne join pas avec la table objet pour voir si l'objet existe vraiment
470 // on considere que c'est le role d'optimiser que de nettoyer les liens morts
471 // sinon eventuellement appeler avant une fonction nettoyer_liens_documents
472 // http://doc.spip.org/@lister_les_documents_orphelins
473 function lister_les_documents_orphelins() {
474 $s = sql_select("D.id_document, D.id_vignette", "spip_documents AS D LEFT JOIN spip_documents_liens AS L ON D.id_document=L.id_document", "(L.id_objet IS NULL)");
475
476 $orphelins = array();
477 while ($t = sql_fetch($s)) {
478 $orphelins[$t['id_document']] = true;
479 // la vignette d'un orphelin est orpheline
480 if ($t['id_vignette'])
481 $orphelins[$t['id_vignette']] = true;
482
483 }
484
485 // les vignettes qui n'appartiennent a aucun document sont aussi orphelines
486 $s = sql_select("V.id_document", "spip_documents AS V LEFT JOIN spip_documents AS D ON V.id_document=D.id_vignette", "V.mode='vignette' AND D.id_document IS NULL");
487 while ($t = sql_fetch($s))
488 $orphelins[$t['id_document']] = true;
489
490 return array_keys(array_filter($orphelins));
491 }
492
493 // Supprimer les documents de la table spip_documents,
494 // ainsi que les fichiers correspondants dans IMG/
495 // Fonction a n'appeler que sur des documents orphelins
496 // http://doc.spip.org/@supprimer_documents
497 function supprimer_documents($liste = array()) {
498 if (!count($liste))
499 return;
500
501 $in = sql_in('id_document', $liste);
502
503 // Supprimer les fichiers locaux et les copies locales
504 // des docs distants
505 $s = sql_select("fichier, distant", "spip_documents", $in);
506 while ($t = sql_fetch($s)) {
507 if ($t['distant'] == 'oui') {
508 include_spip('inc/distant');
509 if ($local = copie_locale($t['fichier'], 'test'))
510 spip_log("efface $local = ".$t['fichier']);
511 supprimer_fichier($local);
512 }
513 else {
514 if (@file_exists($f = get_spip_doc($t['fichier']))) {
515 spip_log("efface $f");
516 supprimer_fichier($f);
517 }
518 }
519 }
520
521 // Supprimer les entrees dans spip_documents et associees
522 sql_delete('spip_documents', $in);
523 // en principe il ne devrait rien y avoir ici si les documents sont bien orphelins
524 sql_delete('spip_documents_liens', $in);
525 }
526
527 ?>