1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Etienne Mauvais <emauvaisfr@yahoo.fr>
5 */
6
7// must be run within Dokuwiki
8if(!defined('DOKU_INC')) die();
9
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'action.php');
12
13class action_plugin_listeabo extends DokuWiki_Action_Plugin {
14
15    /**
16     * return some info
17     */
18    function getInfo() {
19        return array(
20                'author' => 'Etienne M.',
21                'email'  => 'emauvaisfr@yahoo.fr',
22                'date'   => @file_get_contents(DOKU_PLUGIN.'listeabo/VERSION'),
23                'name'   => 'listeabo Plugin',
24                'desc'   => 'Affiche la liste des abonnements d\'un utilisateur / Displays the subscription list of a user',
25                'url'    => 'http://www.dokuwiki.org/fr:plugin:listeabo',
26                );
27    }
28
29    /**
30     * Constructor
31     */
32    function action_plugin_listeabo() {
33      $this->setupLocale();
34    }
35
36    /**
37     * register the eventhandlers
38     */
39    function register(&$contr) {
40        //$contr->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'ping', array());
41        $contr->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_handle_act', array());
42        $contr->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, '_handle_tpl_act', array());
43    }
44
45    /**
46     * catch listeabo action
47     *
48     * @author Etienne Mauvais <emauvaisfr@yahoo.fr>
49     */
50    function _handle_act(&$event, $param) {
51        if($event->data != 'listeabo') return;
52        $event->preventDefault();
53    }
54
55    function _handle_tpl_act(&$event, $param) {
56        global $lang;
57        global $INFO;
58        global $conf;
59        global $auth;
60
61        $nbAbo=0;
62        $nbPages=0;
63        $nbCat=0;
64
65        if($event->data != 'listeabo') return;
66        $event->preventDefault();
67
68        //Recherche des fichiers mlist dans le dossier meta
69        $pages = $this->list_mlist($conf['savedir'].'/meta',true, $pages);
70
71        //Si on est en mode admin
72        //if (in_array('admin',$INFO['userinfo']['grps'])) $admin = trim($_REQUEST['admin']);
73        //if (in_array('admin',$INFO['userinfo']['grps'])) $user = trim($_REQUEST['user']);
74        if (auth_ismanager($INFO['client'])) {
75          $admin = trim($_REQUEST['admin']);
76          $user = trim($_REQUEST['user']);
77        }
78        if (!$user) $user=$INFO['client'];
79        $userName=$auth->getUserData($user);
80        $userName=$userName['name'];
81        if (!$userName) $userName=$user;
82
83        //Recherche dans chaque fichier mlist trouve
84        foreach((is_array($pages)?$pages:array()) as $page) {
85          preg_match("/meta\/(.*).mlist$/",$page,$page2);
86          $page2=preg_replace("/\//",":",$page2[1]);
87
88          foreach(file($page) as $nom) {
89          $nom=chop($nom);
90            if ($nom==$user || $admin) {
91              //if (!in_array($page2, $mespages)) $mespages[]=$page2;
92              if (!is_array($mespages) || !in_array($page2, $mespages)) $mespages[]=$page2;
93              if ($admin) $abonnes[$page2][]=$nom;
94              else break;
95            }
96          }
97        }
98
99        if ($admin) print '<h1>'.$this->getLang('abo_aboactifs').'</h1>' . DOKU_LF;
100        else        print '<h1>'.$this->getLang('abo_abode').' '.$userName.'</h1>' . DOKU_LF;
101
102        if(!empty($mespages)) {
103          $pagelist = plugin_load('helper', 'pagelist');
104          print "<table>";
105
106          foreach($mespages as $page) {
107            if (!$pagelist) {
108              $titrePage=explode(":",$page);
109              $titrePage=$titrePage[sizeof($titrePage)-1];
110              $titrePage=str_replace('_',' ',$titrePage);
111            }
112            else {
113              $pagelist->page['id']=$page;
114              $pagelist->page['exists'] = 1;
115              $pagelist->_meta=NULL;
116              $titrePage = $pagelist->_getMeta('title');
117              if (!$titrePage) $titrePage = str_replace('_', ' ', noNS($page));
118              $titrePage = hsc($titrePage);
119            }
120            $lien = "<a href='doku.php?id=".$page."' class='wikilink1' style='font-weight: lighter;' title='".$page."'>$titrePage</a>";
121
122            print "<tr><td nowrap><ul><li><div class=\"li\">";
123            if ($titrePage) {
124              print "$lien";
125              $nbPages++;
126            }
127            else {
128              print $this->getLang('abo_cat')." \"".preg_replace("/:$/","",$page)."\"";
129              $nbCat++;
130            }
131            print "</li></div></ul></td>";
132
133            if ($admin) {
134              sort($abonnes[$page]);
135              $abonnesMEF="";
136              foreach ($abonnes[$page] as $abonne) {
137                $tmp=$auth->getUserData($abonne);
138                if ($tmp) $abonnesMEF[]='<a href="?do=listeabo&user='.$abonne.'" title="'.$this->getLang('abo_abode').' '.$tmp['name'].'">'.$tmp['name'].'</a>';
139                else $abonnesMEF[]=$abonne;
140                $nbAbo++;
141              }
142              print "<td nowrap style=\"padding-left:10px;\" valign=\"top\"><div>".join(", ", $abonnesMEF);
143            }
144            else {
145              print "<td nowrap style=\"padding-left:10px;\" align=\"right\"><div class=\"bar\" id=\"bar__bottom\" style=\"border:none;\">";
146              if ($titrePage) {
147                if ($user==$INFO['client'])
148                  tpl_link(wl($page,'do=unsubscribe'),
149                              $pre.(($inner)?$inner:$lang['btn_unsubscribe']).$suf,
150                              'class="action unsubscribe" rel="nofollow"');
151              }
152              else {
153                if ($user==$INFO['client'])
154                  tpl_link(wl($page,'do=unsubscribens'),
155                              $pre.(($inner)?$inner:$lang['btn_unsubscribens']).$suf,
156                              'class="action unsubscribens" rel="nofollow"');
157              }
158              $nbAbo++;
159            }
160            print "</div></td></tr>";
161          }
162          print "</table>";
163
164          print "$nbAbo ".$this->getLang('abo_cptabo')." ".$this->getLang('abo_cptdans')." $nbPages ".$this->getLang('abo_cptpages')." ".$this->getLang('abo_cptet')." $nbCat ".$this->getLang('abo_cptcat').".<br /><br />";
165        }
166        else {
167            print '<div class="level1"><p>'.$this->getLang('abo_noabo').'.</p></div>';
168            print '<div class="level1"><p>'.$this->getLang('abo_whoto').'</p></div>';
169        }
170        //if (in_array('admin',$INFO['userinfo']['grps'])) {
171        if (auth_ismanager($INFO['client'])) {
172          if (!$admin) print $this->getLang('abo_estadmin').'<a href="?do=listeabo&admin=true" class="wikilink">'.$this->getLang('abo_voirtousabo').'</a>.<br />';
173          if ($admin || $user != $INFO['client']) print '<a href="?do=listeabo" class="wikilink">'.$this->getLang('abo_voirvosabo').'</a>.';
174        }
175    }
176
177    function list_mlist($dir, $recursive=false, $files=0) {
178      static $files;
179
180      if(is_dir($dir)) {
181        if($dh = opendir($dir)) {
182          while(($file = readdir($dh)) !== false) {
183            if($file != "." && $file != "..") {
184              if (is_dir($dir."/".$file))
185                $this->list_mlist($dir."/".$file, $recursive, $files);
186              else if (preg_match("/\.mlist$/",$file))
187                $files[]=$dir."/".$file;
188            }
189          }
190          closedir($dh);
191        }
192      }
193      return $files;
194    }
195
196}
197
198// vim:ts=4:sw=4:et:enc=utf-8:
199