[SPIP Zone] [Spip-zone-commit] r32396 - in /_plugins_/hash_documents: ./ exec/ exec/hash_documents.php hash.jpg hash_404.php hash_fonctions.php hash_tests.php plugin.xml

Super plugin ! Ca fonctionne avec gestion_documents ?
Merci

A+
Le 27 oct. 2009 à 13:43, fil@rezo.net a écrit :

Author: fil@rezo.net
Date: Tue Oct 27 13:43:04 2009
New Revision: 32396

Log:
plugin hash_documents, cf. Le plugin hash_documents - SPIP-Contrib

Added:
   _plugins_/hash_documents/
   _plugins_/hash_documents/exec/
   _plugins_/hash_documents/exec/hash_documents.php
   _plugins_/hash_documents/hash.jpg (with props)
   _plugins_/hash_documents/hash_404.php
   _plugins_/hash_documents/hash_fonctions.php
   _plugins_/hash_documents/hash_tests.php
   _plugins_/hash_documents/plugin.xml

Added: _plugins_/hash_documents/exec/hash_documents.php

======================================================================
--- _plugins_/hash_documents/exec/hash_documents.php (added)
+++ _plugins_/hash_documents/exec/hash_documents.php Tue Oct 27 13:43:04 2009
@@ -0,0 +1,84 @@
+<?php
+
+if (!defined("_ECRIRE_INC_VERSION")) return;
+
+function exec_hash_documents_dist($class = null)
+{
+ include_spip('hash_fonctions');
+
+
+ //
+ // affichages
+ //
+ include_spip("inc/presentation");
+
+ pipeline('exec_init',array('args'=>array('exec'=>'hash_documents'),'data'=>''));
+
+ $commencer_page = charger_fonction('commencer_page', 'inc');
+ echo $commencer_page("Hash documents", 'hash_documents');
+ echo "<br /><br /><br />\n";
+
+ echo gros_titre(_L('Hash documents'), '', false);
+
+ // colonne gauche
+ echo debut_gauche('', true);
+
+ echo pipeline('affiche_gauche',array('args'=>array('exec'=>'hash_documents'),'data'=>''));
+ echo creer_colonne_droite('', true);
+ echo pipeline('affiche_droite',array('args'=>array('exec'=>'hash_documents'),'data'=>''));
+
+ echo debut_droite("", true);
+
+ echo debut_cadre_trait_couleur('', true, '', 'Documents du site');
+
+ if (($hasher = intval(_request('hasher'))) > 0)
+ $modif = hasher_deplacer_n_documents($hasher);
+
+ if (($hasher = intval(_request('hasher'))) < 0)
+ $modif = hasher_deplacer_n_documents(-$hasher, true);
+
+ if ($modif) {
+ echo "<p>Documents modifiés : ".join(', ', $modif)."</p>";
+ }
+
+ // centre de la page
+ list($oui, $non) = hasher_compter_documents();
+ echo "Ce site comporte $oui documents hashés, et $non qui ne le sont pas encore (ou ne peuvent pas l'être).";
+
+ if (intval($non) > 0) {
+ $n = min(intval($non), 100);
+ echo "<p><a href='".parametre_url(self(), 'hasher', $n)."'>hasher $n documents</a></p>";
+ }
+
+ if (intval($oui) > 0) {
+ $n = min(intval($oui), 100);
+ echo "<p><a href='".parametre_url(self(), 'hasher', -$n)."'>déhasher $n documents</a></p>";
+ }
+
+ echo fin_cadre_trait_couleur(true);
+
+ echo "<br /><br />\n";
+
+ echo debut_cadre_trait_couleur('', true, '', 'Redirections');
+ $htaccess = _DIR_IMG.'.htaccess';
+ if (!lire_fichier($htaccess, $contenu)
+ OR !preg_match(',hash_404,', $contenu)) {
+ echo "<p>Veuillez installer dans $htaccess un fichier contenant les codes suivants :</p>";
+ } else {
+ echo "<p>Le fichier $htaccess semble correctement installé ; pour mémoire, il doit contenir les codes suivants :</p>";
+ }
+ echo propre('<cadre>
+RewriteEngine On
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteRule .* /plugins/hash_documents/hash_404.php [L]
+ </cadre>');
+
+ echo fin_cadre_trait_couleur(true);
+
+ // pied
+ echo fin_gauche() . fin_page();
+}
+
+?>

Added: _plugins_/hash_documents/hash.jpg

Binary file - no diff available.

Propchange: _plugins_/hash_documents/hash.jpg
------------------------------------------------------------------------------
   svn:mime-type = application/octet-stream

Added: _plugins_/hash_documents/hash_404.php

--- _plugins_/hash_documents/hash_404.php (added)
+++ _plugins_/hash_documents/hash_404.php Tue Oct 27 13:43:04 2009
@@ -0,0 +1,49 @@
+<?php
+
+## fichier appele par le .htaccess de IMG/ ; lequel doit contenir :
+
+/*
+
+RewriteEngine On
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteRule .* /plugins/hash_documents/hash_404.php [L]
+
+*/
+
+@require 'hash_fonctions.php';
+
+$doc = preg_replace(',^.*?IMG/,', '', $_SERVER['REQUEST_URI']);
+if (($dest = hasher_adresser_document($doc)
+AND file_exists('../../IMG/'.$dest))
+OR ($dest = hasher_adresser_document($doc, true)
+AND file_exists('../../IMG/'.$dest))
+) {
+ $url = 'http://'.$_SERVER['HTTP_HOST'].preg_replace(',^(.*?IMG/).*,', '\1', $_SERVER['REQUEST_URI']).$dest;
+ header('HTTP/1.1 301 Moved Permanently');
+ header('Location: '.$url);
+ echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
+<html><head>
+<title>301 Moved Permanently</title>
+</head><body>
+<h1>Moved Permanently</h1>
+<p>The requested URL '.htmlspecialchars($_SERVER['REQUEST_URI']).' has moved to <a href="'.$url.'">'.$url.'</a>.</p>
+<hr>
+'.$_SERVER['SERVER_SIGNATURE'].'
+</body></html>
+';
+}
+else {
+ echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
+<html><head>
+<title>404 Not Found</title>
+</head><body>
+<h1>Not Found</h1>
+<p>The requested URL '.htmlspecialchars($_SERVER['REQUEST_URI']).' was not found on this server.</p>
+<hr>
+'.$_SERVER['SERVER_SIGNATURE'].'
+</body></html>
+';
+}
+

Added: _plugins_/hash_documents/hash_fonctions.php

--- _plugins_/hash_documents/hash_fonctions.php (added)
+++ _plugins_/hash_documents/hash_fonctions.php Tue Oct 27 13:43:04 2009
@@ -0,0 +1,161 @@
+<?php
+
+## fonctions pour hasher les documents
+
+if (!defined("_ECRIRE_INC_VERSION")) return;
+
+/* pour un fichier d'origine situé dans IMG/{$ext}/xxxx.ext,
+ * prendre les 3 premiers caractères (a, b, c) du md5(xxxx.ext),
+ * et déplacer le fichier dans IMG/{$ext}/a/b/c/xxxx.ext
+ * attention on ignore le IMG/ eventuel dans $doc, et on retourne sans IMG/
+ * $rev sert a faire l'inverse
+ * @param string $doc
+ * @param bool $rev
+ * @return string
+ */
+function hasher_adresser_document($doc, $rev=false) {
+ switch ($rev) {
+ case false:
+ if (!preg_match(',^(?:IMG/)?([^/]+)/([^/]+\.\1)$,S', $doc, $r))
+ return false;
+ $m = md5($r[2]);
+ return $r[1].'/'.$m[0].'/'.$m[1].'/'.$m[2].'/'.$r[2];
+ case true:
+ if (!preg_match(',^(?:IMG/)?([^/]+)/./././([^/]+\.\1)$,S', $doc, $r))
+ return false;
+ return $r[1].'/'.$r[2];
+ }
+}
+
+/* Deplacer un fichier et sa reference dans la base de donnees
+ * avec tous les controles d'erreur
+ *
+ * @param int $id_document
+ * @param bool $rev
+ * @return bool
+ */
+function hasher_deplacer_document($id_document, $rev=false) {
+
+ // 1. recuperer les donnees du document
+ // et verifier qu'on peut le hasher
+ if (!$id_document = intval($id_document))
+ return false;
+ if (!$s = spip_query('SELECT fichier FROM spip_documents WHERE id_document='.$id_document)
+ OR !$t = spip_fetch_array($s)) {
+ spip_log("erreur select doc=$id_document ".var_export($s, true), 'hash');
+ return false;
+ }
+ $src = $t['fichier'];
+
+ // savoir si on a IMG/ devant (en SPIP 1.9.2) ou pas (SPIP 2)
+ $img = preg_match(',^IMG/,', $src)
+ ? 'IMG/' : '';
+ $dir_ref = preg_match(',^IMG/,', $src)
+ ? _DIR_RACINE : _DIR_IMG;
+
+ // si le src n'existe pas, ciao
+ if (!file_exists($dir_ref.$src)) {
+ spip_log("fichier $src n'existe pas", 'hash');
+ return false;
+ }
+
+ if (!$dest = hasher_adresser_document($src, $rev)) {
+ spip_log("erreur hasher_adresser_document($src)", 'hash');
+ return false;
+ }
+
+ // si le dest existe deja, renommer jusqu'a trouver un creneau libre
+ $i = 0;
+ while (file_exists(_DIR_IMG.$dest)) {
+ $i++;
+ $dest = preg_replace(',(-\d+)?(\.[^.]+)$,', '-'.$i.'\2', $dest);
+ }
+
+ // 2. creer au besoin les sous-repertoires
+ if (!is_dir(_DIR_IMG.$dir = dirname($dest))
+ AND !mkdir(_DIR_IMG.$dir, _SPIP_CHMOD, /* recursive, php5 */ true)) {
+ spip_log("erreur mkdir($dir)", 'hash');
+ return false;
+ }
+
+ // 3. Section critique : il faut modifier dans la base *et* deplacer
+ // on note les fichiers en cours de deplacement avec un - devant ; si
+ // ca casse on saura reparer
+ if (!spip_query('UPDATE spip_documents SET fichier=CONCAT("-", fichier) WHERE id_document='.$id_document)) {
+ spip_log("erreur update 1", 'hash');
+ return false;
+ }
+ // on deplace
+ if (!rename($dir_ref.$src, _DIR_IMG.$dest)) {
+ spip_log("erreur rename", 'hash');
+ spip_query('UPDATE spip_documents SET fichier="'.$src.'" WHERE id_document='.$id_document);
+ return false;
+ }
+ // on note la destination finale
+ if (!spip_query('UPDATE spip_documents SET fichier="'.$img.$dest.'" WHERE id_document='.$id_document)) {
+ spip_log("erreur update 2", 'hash');
+ return false;
+ }
+
+ // 4. Ouf c'est fini et sans erreur
+ return true;
+}
+
+/* Cette fonction prend les n documents non hashés les plus récents,
+ * et appelle hasher_deplacer_document() sur chacun d'eux. Elle renvoie
+ * un array() contenant les id_document des documents qu'elle a déplacés.
+ * @param int $n
+ * @param bool $rev
+ * @return array
+ * @return bool
+ */
+function hasher_deplacer_n_documents($n, $rev=false) {
+ if (!$n = intval($n)
+ OR !$s = spip_query($q = "SELECT id_document FROM spip_documents WHERE fichier REGEXP '^(IMG/)?[^/]+/"
+ . ($rev ? "./././" : "")
+ ."[^/]+$' AND distant='non' ORDER BY date DESC LIMIT $n")) {
+ spip_log("erreur requete $q", 'hash');
+ return false;
+ }
+
+ $docs = array();
+ while ($t = spip_fetch_array($s)) {
+ $id_document = $t['id_document'];
+ if (hasher_deplacer_document($id_document, $rev))
+ $docs = $id_document;
+ }
+
+ return $docs;
+}
+
+/* Compte les documents hashes et non hashes
+ * @return array
+ */
+function hasher_compter_documents() {
+ $s = spip_query($q = "SELECT COUNT(*) FROM spip_documents WHERE fichier REGEXP '^(IMG/)?[^/]+/"
+ ."[^/]+$' AND distant='non'");
+ $non = array_pop(spip_fetch_array($s));
+ $s = spip_query($q = "SELECT COUNT(*) FROM spip_documents WHERE fichier REGEXP '^(IMG/)?[^/]+/"
+ . "./././"
+ ."[^/]+$' AND distant='non'");
+ $oui = array_pop(spip_fetch_array($s));
+

[... 82 lines stripped ...]
_______________________________________________
Spip-zone-commit@rezo.net - http://listes.rezo.net/mailman/listinfo/spip-zone-commit

On Tue, Oct 27, 2009 at 2:02 PM, Yohann Prigent
<prigent.yohann@gmail.com> wrote:

Super plugin ! Ca fonctionne avec gestion_documents ?

Ca fonctionne avec tout

-- Fil