1<?php
2/**
3 *
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Myron Turner <turnermm02@shaw.ca>
7 */
8
9
10/**
11 * All DokuWiki plugins to extend the admin function
12 * need to inherit from this class
13 */
14class admin_plugin_preregister extends DokuWiki_Admin_Plugin {
15
16    var $output = '';
17    private $metaFn;
18
19    function __construct() {
20       $metafile= 'preregister:db';
21       $this->metaFn = metaFN($metafile,'.ser');
22    }
23    /**
24     * handle user request
25     */
26    function handle() {
27
28      if (!isset($_REQUEST['cmd'])) return;   // first time - nothing to do
29
30      $this->output = 'invalid';
31
32      if (!checkSecurityToken()) return;
33      if (!is_array($_REQUEST['cmd'])) return;
34
35      // verify valid values
36      switch (key($_REQUEST['cmd'])) {
37        case 'confirm' :
38        $this->prune_datafile($_REQUEST['del']) ;
39         break;
40        case 'secure' :
41        $this->secure_datafile() ;
42        break;
43      }
44   //  msg('<pre>' . print_r($_REQUEST['del'],true) . '</pre>');
45
46    }
47
48    /**
49     * output appropriate html
50     */
51    function html() {
52    ptln('<a href="javascript:prereg_Inf.toggle();void 0"><span style="line-height: 175%">Toggle info</span></a><br />');
53     ptln('<div id="prereg_info" style="border: 1px solid silver; padding:4px;">'.     $this->locale_xhtml('info') . '</div><br>' );
54
55
56      ptln('<form action="'.wl($ID).'" method="post">');
57
58      // output hidden values to ensure dokuwiki will return back to this plugin
59      ptln('  <input type="hidden" name="do"   value="admin" />');
60      ptln('  <input type="hidden" name="page" value="'.$this->getPluginName().'" />');
61      formSecurityToken();
62      ptln('  <input type="submit" name="cmd[confirm]"  value="'.$this->getLang('btn_confirm').'" />&nbsp;&nbsp;');
63      ptln('  <input type="submit" name="cmd[secure]"  value="'.$this->getLang('btn_secure').'" />');
64       ptln('<br /><br /><div>');
65      echo $this->getConfirmList();
66      ptln('</div>');
67      ptln('</form>');
68      $this->js();
69    }
70
71    function getConfirmList() {
72        global $conf;
73
74        $delete_time = $this->getConf('list_age');
75        if(strpos($delete_time,'*') !== false) {
76           $elems = explode('*',$delete_time);
77           $delete_time = 1;
78           foreach($elems as $n) {
79               $delete_time *= $n;
80           }
81        }
82
83        $data = unserialize(io_readFile($this->metaFn,false));
84        if (!is_array($data)) $data = array();
85        $hidden = array();
86        $result = "<table cellspacing='2'><th>login</th><th>email</th><th>name</th><th>save time</th><th>age</th>";
87        $current_time = time();
88        foreach($data as $index=>$entry) {
89            $age = $current_time - $entry['savetime'];
90            if($age >= $delete_time) {
91                $hidden[] = $index;
92                $hours = round(($age/3600),2);
93                if($hours >= 24) {
94                    $hours = round($hours/24,2);
95                    $hours .= ' day(s)';
96                }
97                else $hours .= ' hours';
98                $result .= '<tr><td>'. $entry['login'] . '</td><td>' . $entry['email'] . '</td><td>'  . $entry['fullname']
99                . '</td><td>' . strftime($conf['dformat'],$entry['savetime']) . '</td><td>' . $hours .  "</td></tr>\n";
100              }
101        }
102        $result .= '</table>';
103        foreach($hidden as $del) {
104            $result .= "\n<input type='hidden' name='del[]'  value = '$del' />";
105        }
106        return $result ."\n";
107    }
108
109    function secure_datafile() {
110         $perm = substr(sprintf('%o', fileperms($this->metaFn )), -4);
111         if(preg_match('/\d\d(\d)/',$perm,$matches)) {
112            if($matches[1] > 0) {
113                 msg("Data file is currently accessible to all: $perm");
114                 if(chmod($this->metaFn ,0600)) {
115                    msg("Succesfully change permissions to: 0600");
116                 }
117                 else  msg("Unable to change permissions to: 0600");
118             }
119         }
120    }
121
122    function prune_datafile($which) {
123        $data = unserialize(io_readFile($this->metaFn,false));
124        if (!is_array($data)) $data = array();
125        foreach($data as $index=>$entry) {
126            if(in_array($index,$which)) {
127                 unset($data[$index]);
128            }
129        }
130
131        io_saveFile($this->metaFn,serialize($data));
132    }
133
134    function js() {
135echo <<<SCRIPT
136<script type="text/javascript">
137    //<![CDATA[
138var prereg_Inf = {
139dom_style: document.getElementById('prereg_info').style,
140open: function() { this.dom_style.display = 'block'; },
141close: function() { this.dom_style.display = "none"; },
142toggle: function() {
143if(this.is_open) { this.close(); this.is_open=false; return; }
144this.open(); this.is_open=true;
145},
146is_open: true,
147};
148    //]]>
149 </script>
150SCRIPT;
151
152    }
153}