X-Git-Url: http://git.cyclocoop.org/?p=velocampus%2Fweb%2Fwww.git;a=blobdiff_plain;f=www%2Fecrire%2Finc%2Fcharger_plugin.php;fp=www%2Fecrire%2Finc%2Fcharger_plugin.php;h=fab167e401c17741e8bd14ad478e8e27b6f5fd70;hp=0000000000000000000000000000000000000000;hb=80b4d3e85f78d402ed2e73f8f5d1bf4c19962eed;hpb=aaf970bf4cdaf76689ecc10609048e18d073820c diff --git a/www/ecrire/inc/charger_plugin.php b/www/ecrire/inc/charger_plugin.php new file mode 100644 index 0000000..fab167e --- /dev/null +++ b/www/ecrire/inc/charger_plugin.php @@ -0,0 +1,544 @@ +
  • '._T('plugin_info_automatique2',array('rep'=>joli_repertoire(_DIR_PLUGINS_AUTO))).'
  • ' + .'
  • '._T('plugin_info_automatique3').aide("install0")."
  • " + ."\n

    "._T('plugin_info_automatique_lib')."

    "; + } + + if (!$auto) + $auto = interface_plugins_auto($retour); + + } + + $message = _T('plugin_info_automatique_ftp',array('rep'=>joli_repertoire(_DIR_PLUGINS))); + if (!@is_dir(_DIR_PLUGINS)) + $message .= " — "._T('plugin_info_automatique_creer'); + + return debut_cadre_trait_couleur("spip-pack-24.png", true, "", _T('plugin_titre_automatique_ajouter')) + . "

    "._T('plugin_titre_automatique')."

    " + . "

    ".$message."

    \n" + . $auto + . fin_cadre_trait_couleur(true); + +} + + +// http://doc.spip.org/@interface_plugins_auto +function interface_plugins_auto($retour) { + + $res = "
    "; + + if ($retour) { + $res .= "
    $retour
    \n"; + } + + $liste = liste_plugins_distants(); + + $message .= '
    '._T('plugin_zip_adresse') + . '
    '._T('plugin_info_automatique_exemples').'
    "; + + $form = ""; + $form .= ""; + $form .= "
    \n" + . "
    \n"; + $form = redirige_action_post('charger_plugin', + '', // arg = 'plugins' / 'lib', a priori + '', + '', + $form); + + $res .= "
    "; + + $res .= $form; + $res .= "
    \n"; + + + $res .= "
    \n"; + + $res .= afficher_liste_listes_plugins(); + + if ($liste) { + $res .= afficher_liste_plugins_distants($liste); + + $menu = array(); + $compte = 0; + + $res .= + http_script(" + jQuery(function(){ + jQuery('.plugins li.item a[rel=info]').click(function(){ + var li = jQuery(this).parents('li').eq(0); + if (!jQuery('div.details',li).html()) { + jQuery('div.details',li).prepend(ajax_image_searching).load( + jQuery(this).attr('href').replace(/admin_plugin|plugins|charger_plugin/, 'info_plugin_distant'), {}, function(){ + li.addClass('on'); + } + ); + } + else { + if (jQuery('div.details',li).toggle().is(':visible')) + li.addClass('on'); + else + li.removeClass('on'); + } + return false; + }); + }); + "); + + } + return $res; +} + +function afficher_liste_plugins_distants($liste){ + $res = ""; + if (!$liste) return ""; + + $menu = array(); + $compte = 0; + + $afficher_plugin_distant = charger_fonction('afficher_plugin_distant','plugins'); + $url_page = self(); + foreach ($liste as $url => $info) { + $titre = $info[0]; + $titre = strtoupper(trim(typo(translitteration(unicode2charset(html2unicode($titre)))))); + $menu[$titre] = $afficher_plugin_distant($url_page, $url, $info, _request('plugin')==$url); + } + ksort($menu); + + $res .= + "

    "._T('plugins_compte',array('count' => count($menu)))."

    " + . '

    '._T('plugin_info_automatique_select',array('rep'=>joli_repertoire(_DIR_PLUGINS_AUTO))).'

    ' + . ""; + + return $res; +} + +// http://doc.spip.org/@chargeur_charger_zip +function chargeur_charger_zip($quoi = array()) +{ + if (!$quoi) { + return true; + } + if (is_scalar($quoi)) { + $quoi = array('zip' => $quoi); + } + if (isset($quoi['depot']) || isset($quoi['nom'])) { + $quoi['zip'] = $quoi['depot'] . $quoi['nom'] . '.zip'; + } + foreach (array( 'remove' => 'spip', + 'arg' => 'lib', + 'plugin' => null, + 'cache_cache' => null, + 'rename' => array(), + 'edit' => array(), + 'root_extract' => false, # extraire a la racine de dest ? + 'tmp' => sous_repertoire(_DIR_CACHE, 'chargeur') + ) + as $opt=>$def) { + isset($quoi[$opt]) || ($quoi[$opt] = $def); + } + + + # destination finale des fichiers + switch($quoi['arg']) { + case 'lib': + $quoi['dest'] = _DIR_RACINE.'lib/'; + break; + case 'plugins': + $quoi['dest'] = _DIR_PLUGINS_AUTO; + break; + default: + $quoi['dest'] = ''; + break; + } + + + if (!@file_exists($fichier = $quoi['fichier'])) + return 0; + + include_spip('inc/pclzip'); + $zip = new PclZip($fichier); + $list = $zip->listContent(); + + // on cherche la plus longue racine commune a tous les fichiers + $max_n = 999999; + foreach($list as $n) { + $p = array(); + foreach(explode('/', $n['filename']) as $n => $x) { + if ($n>$max_n) + continue; + $sofar = join('/',$p); + $paths[$n][$sofar]++; + $p[] = $x; + } + $max_n = min($n,$max_n); + } + + $total = $paths[0]['']; + $i = 0; + while (isset($paths[$i]) + AND count($paths[$i]) <= 1 + AND array_values($paths[$i]) == array($total)) + $i++; + + $racine = $i + ? array_pop(array_keys($paths[$i-1])).'/' + : ''; + + $quoi['remove'] = $racine; + + if (!strlen($nom = basename($racine))) + $nom = basename($fichier, '.zip'); + + $dir_export = $quoi['root_extract'] + ? $quoi['dest'] + : $quoi['dest'] . $nom.'/'; + + $tmpname = $quoi['tmp'].$nom.'/'; + + // On extrait, mais dans tmp/ si on ne veut pas vraiment le faire + $ok = $zip->extract( + PCLZIP_OPT_PATH, + $quoi['extract'] + ? $dir_export + : $tmpname + , + PCLZIP_OPT_SET_CHMOD, _SPIP_CHMOD, + PCLZIP_OPT_REPLACE_NEWER, + PCLZIP_OPT_REMOVE_PATH, $quoi['remove'] + ); + if ($zip->error_code < 0) { + spip_log('charger_decompresser erreur zip ' . $zip->error_code . + ' pour paquet: ' . $quoi['zip']); + return //$zip->error_code + $zip->errorName(true); + } + +/* + * desactive pour l'instant + * + * + if (!$quoi['cache_cache']) { + chargeur_montre_tout($quoi); + } + if ($quoi['rename']) { + chargeur_rename($quoi); + } + if ($quoi['edit']) { + chargeur_edit($dir_export, $quoi['edit']); + } + + if ($quoi['plugin']) { + chargeur_activer_plugin($quoi['plugin']); + } +*/ + + spip_log('charger_decompresser OK pour paquet: ' . $quoi['zip']); + + + + $size = $compressed_size = 0; + $removex = ',^'.preg_quote($quoi['remove'], ',').','; + foreach ($list as $a => $f) { + $size += $f['size']; + $compressed_size += $f['compressed_size']; + $list[$a] = preg_replace($removex,'',$f['filename']); + } + + // Indiquer par un fichier install.log + // a la racine que c'est chargeur qui a installe ce plugin + ecrire_fichier($tmpname.'/install.log', + "installation: charger_plugin\n" + ."date: ".gmdate('Y-m-d\TH:i:s\Z', time())."\n" + ."source: ".$quoi['zip']."\n" + ); + + + + return array( + 'files' => $list, + 'size' => $size, + 'compressed_size' => $compressed_size, + 'dirname' => $dir_export, + 'tmpname' => $tmpname + ); +} + +// pas de fichiers caches et preg_files() les ignore (*sigh*) +// http://doc.spip.org/@chargeur_montre_tout +function chargeur_montre_tout($quoi) +{ + # echo($quoi['dest']); + if (!($d = @opendir($quoi['dest']))) { + return; + } + while (($f = readdir($d)) !== false) { + if ($f == '.' || $f == '..' || $f[0] != '.') { + continue; + } + rename($quoi['dest'] . '/' . $f, $quoi['dest'] . '/'. substr($f, 1)); + } +} + +// renommer des morceaux +// http://doc.spip.org/@chargeur_edit +function chargeur_edit($dir, $edit) +{ + if (!($d = @opendir($dir))) { + return; + } + while (($f = readdir($d)) !== false) { + if ($f == '.' || $f == '..') { + continue; + } + if (is_dir($f = $dir . '/' . $f)) { + chargeur_edit($f, $edit); + } + $contenu = file_get_contents($f); + if (($change = preg_replace( + array_keys($edit), array_values($edit), $contenu)) == $contenu) { + continue; + } + $fw = fopen($f, 'w'); + fwrite($fw, $change); + fclose($fw); + } +} + +// renommer des morceaux +// http://doc.spip.org/@chargeur_rename +function chargeur_rename($quoi) +{ +/* + preg_files() est deficiante pour les fichiers caches, ca aurait pu etre bien pourtant ... +*/ + spip_log($quoi); + foreach ($quoi['rename'] as $old => $new) { + !is_writable($file = $quoi['dest'] . '/' . $old) || + rename($file, $quoi['dest'] . '/'. $new); + } +} + +// juste activer le plugin du repertoire $plugin +// http://doc.spip.org/@chargeur_activer_plugin +function chargeur_activer_plugin($plugin) +{ + spip_log('charger_decompresser activer plugin: ' . $plugin); + include_spip('inc/plugin'); + ecrire_plugin_actifs(array($plugin), false, 'ajoute'); +} + + +// http://doc.spip.org/@liste_fichiers_pclzip +function liste_fichiers_pclzip($status) { + $list = $status['files']; + + $ret = ''._T('plugin_zip_content',array('taille'=>taille_en_octets($status['size']), 'rep'=>$status['dirname'])).''; + + $l .= "\n"; + + include_spip('inc/filtres'); + if (preg_match(',([^<]+)<,', $svn, $t)) + $rev = '
    revision '.$t[1].'
    '; + if (preg_match(',([^<]+),', $svn, $t)) + $date = '
    ' . affdate($t[1]) .'
    '; + + return $ret . $rev . $date . $l; +} + +// Attention on ne sait pas ce que vaut cette URL +// http://doc.spip.org/@essaie_ajouter_liste_plugins +function essaie_ajouter_liste_plugins($url) { + if (!preg_match(',^https?://[^.]+\.[^.]+.*/.*[^/]$,', $url)) + return; + + include_spip('inc/distant'); + if (!$rss = recuperer_page($url) + OR !preg_match(', titre +// si $desc on ramene aussi le descriptif du paquet desc +// http://doc.spip.org/@chercher_enclosures_zip +function chercher_enclosures_zip($rss, $desc = '') { + $liste = array(); + include_spip('inc/syndic'); + foreach(analyser_backend($rss) as $item){ + if ($item['enclosures'] + AND $zips = extraire_balises($item['enclosures'], 'a')){ + if ($img = extraire_balise($item['descriptif'], 'img') + AND $src = extraire_attribut($img, 'src')) { + $item['icon'] = $src; + } + foreach ($zips as $zip) + if (extraire_attribut($zip, 'type') == 'application/zip') { + if ($url = extraire_attribut($zip, 'href')) { + $liste[$url] = array($item['titre'], $item['url']); + if ($desc===true OR $desc == $url) + $liste[$url][] = $item; + } + } + } + } + spip_log(count($liste).' enclosures au format zip'); + return $liste; +} + + +// Renvoie la liste des plugins distants (accessibles a travers +// l'une des listes de plugins) +// Si on passe desc = un url, ramener le descriptif de ce paquet +// http://doc.spip.org/@liste_plugins_distants +function liste_plugins_distants($desc = false) { + // TODO une liste multilingue a telecharger + $liste = array(); + + if (is_array($flux = @unserialize($GLOBALS['meta']['syndic_plug']))) { + + foreach ($flux as $url => $c) { + if (file_exists($cache=_DIR_TMP.'syndic_plug_'.md5($url).'.txt') + AND lire_fichier($cache, $rss)) + $liste = array_merge(unserialize($rss),$liste); + } + } + + return $liste; +} + +// http://doc.spip.org/@afficher_liste_listes_plugins +function afficher_liste_listes_plugins() { + if (!is_array($flux = @unserialize($GLOBALS['meta']['syndic_plug']))) + return ''; + + if (count($flux)){ + $ret = '

    '._T('plugin_info_automatique_liste').'

      '; + //$ret .= '
    • '._T('plugin_info_automatique_liste_officielle').'
    • '; + foreach ($flux as $url => $c) { + $a = ''; + $time = @filemtime(_DIR_TMP.'syndic_plug_'.md5($url).'.txt'); + $ret .= '
    • '.inserer_attribut(PtoBR(propre("[->$url]")),'title',$url).' ('._T('plugins_compte',array('count' => $c)).') ' + .($time?"
      " . _T('info_derniere_syndication').' '.affdate(date('Y-m-d H:i:s',$time)) ."
      ":'') + . $a .'
    • '; + } + $ret .= '
    '; + + $ret .= '
    '._T('plugin_info_automatique_liste_update').'
    '; + } + + return $ret; +} + +// Si le chargement auto est autorise, un bouton +// sinon on donne l'url du zip +// http://doc.spip.org/@bouton_telechargement_plugin +function bouton_telechargement_plugin($url, $rep) { + // essayer de creer le repertoire lib/ si on en a le droit + if (($rep == 'lib') AND !is_dir(_DIR_RACINE . 'lib')) + sous_repertoire(_DIR_RACINE . 'lib','',false,true); + + if (($rep == 'lib')? + is_dir(_DIR_RACINE . 'lib'): + (_DIR_PLUGINS_AUTO AND @is_dir(_DIR_PLUGINS_AUTO)) + ) + $bouton = redirige_action_post('charger_plugin', + $rep, // arg = 'lib' ou 'plugins' + '', + '', + "" + ."", + 'class="noajax"'); + else if ($rep == 'lib'){ + $bouton = "
    "._T('plugin_info_automatique1_lib')."\n" + .'
    1. '._T('plugin_info_automatique2',array('rep'=>joli_repertoire(_DIR_RACINE . 'lib/'))).'
    2. ' + .'
    3. '._T('plugin_info_automatique3').aide("install0")."
    "; + } + + return _T('plugin_info_telecharger',array('url'=>$url,'rep'=>$rep.'/')).$bouton; + +} + +?>