1<?php
2if (!defined('DOKU_INC')) die();
3if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4
5require_once(DOKU_PLUGIN.'admin.php');
6
7class admin_plugin_subconfhelper extends DokuWiki_Admin_Plugin {
8
9
10    function getMenuSort() { return 200; }
11    function forAdminOnly() { return false; }
12
13    function handle() {/*{{{*/
14        global $lang;
15
16	$this->meta = DOKU_PLUGIN.'subconfhelper/conf/subconfhelper_metadata.php';
17	$this->default = DOKU_PLUGIN.'subconfhelper/conf/subconfhelper_default.php';
18	$this->config_path = DOKU_CONF;
19	$this->config_prefix = 'subconfhelper_';
20
21        if (!isset($_REQUEST['vhost']['admin'])) { $admin = null; }
22	else {
23	    $admin = (is_array($_REQUEST['vhost']['admin'])) ? key( $_REQUEST['vhost']['admin'] )
24			    : $_REQUEST['vhost']['admin'];
25	}
26
27	if( isset( $_REQUEST['vhost']['vhost']) && preg_match( '/[a-z0-9_\-]/', $_REQUEST['vhost']['vhost'] )) {
28	    $vhost = $_REQUEST['vhost']['vhost'];
29	    if( $vhost == '__new__' ) {
30		if( isset( $_REQUEST['ns']['ns']) && preg_match( '/[a-z0-9_\-]/', $_REQUEST['ns']['ns'] )) {
31		    $vhost = $_REQUEST['ns']['ns'];
32		}
33	    }
34            $file = $this->config_path.$this->config_prefix.$vhost.'.php';
35	    $_input = $_REQUEST['config'];
36	}
37
38        // handle actions
39        switch($admin) {
40
41            case 'vhost_new':
42                if( is_file( $file ) || is_dir( $file ) || !touch( $file )) {
43                    msg($this->getLang('msg_vhost_error'), -1);
44                } else {
45                    msg($this->getLang('msg_vhost_delete'), 1);
46                }
47              break;
48
49            case 'vhost_delete':
50                if( !is_file( $file ) || is_dir( $file ) || !unlink( $file )) {
51                    msg($this->getLang('msg_vhost_error'), -1);
52                } else {
53                    msg($this->getLang('msg_vhost_save'), 1);
54                }
55              break;
56
57            case 'vhost_save':
58		if( !$vhost || !is_file( $file )) break;
59
60		$_changed   = false;
61		$_error	    = false;
62
63		$_config = &plugin_load( 'helper', 'subconfhelper_config' );
64		$_config->configuration( $this->meta,  array( $file ));
65
66		while (list($key) = each($_config->setting)) {
67		  $input = isset($_input[$key]) ? $_input[$key] : NULL;
68		  if ($_config->setting[$key]->update($input)) {
69		    $_changed = true;
70		  }
71		  if ($_config->setting[$key]->error()) $_error = true;
72		}
73		if( $_changed  && !$_error && $_config->save_settings( $vhost )) {
74		    msg($this->getLang('msg_vhost_save'), 1);
75		} else {
76		    msg($this->getLang('msg_vhost_error'), -1);
77		}
78              break;
79
80            default:
81                // do nothing - show dashboard
82                break;
83        }
84    }/*}}}*/
85
86    function html() {/*{{{*/
87        ptln('<div  class="act-admin"><div id="subconfhelper">');
88        $this->xhtml_vhost_list();
89        ptln('</div></div>');
90    }/*}}}*/
91
92    function xhtml_vhost_list( ) {/*{{{*/
93        global $lang;
94        global $ID;
95
96        ptln( '<div class="level2">' );
97        ptln( '<p>' . $this->getLang( 'vhost_text' ).'</p>' );
98        ptln( '<ul>' );
99
100        $vhost = '__new__';
101        ptln( '<li class="vhostitem"><h2>'.$this->getLang( 'vhost' ).$vhost.'</h2></li>' );
102        ptln( '<form action="" method="post">' );
103	ptln( "<input type='hidden' name='vhost[admin]' value='vhost_new' />" );
104        ptln( '<ul><li>' );
105        ptln( '<span class="outkey"><label for="vhost-new">'.$this->getLang( 'vhost-new' ).'</label></span>' );
106        ptln( '<input id="vhost-new" type="text" class="edit" name="vhost[vhost]" />' );
107        ptln( '<li class="submit"><input type="submit" value="Save"></li>' );
108	ptln( "</form>" );
109        ptln( '</li></ul>' );
110
111        if( $handle = opendir( $this->config_path )) {
112            while( false !== ( $file = readdir( $handle ))) {
113                if( strpos( $file, $this->config_prefix ) === 0  && !strpos( $file, '.bak' )) {
114                    $vhost = str_replace( '.php', '', str_replace( $this->config_prefix, '', $file ));
115                    ptln( '<li class="vhostitem">' );
116                    ptln( '<h2>'.$this->getLang( 'vhost' ).$vhost.'</h2>' );
117                    $this->xhtml_vhost_item( $vhost, $this->config_path.$file );
118                    ptln( '</li>' );
119
120                }
121            }
122            closedir($handle);
123
124
125        }
126
127        ptln('</ul>');
128        ptln('</div>');
129    }/*}}}*/
130
131    function xhtml_vhost_item( $vhost, $file ) {/*{{{*/
132        global $lang;
133        global $ID;
134
135        $meta = $this->meta;
136        $default = $this->default;
137        $_config = &plugin_load( 'helper', 'subconfhelper_config');
138        #$_config->configuration( $meta, array( $file ), array( $default )); // TODO defaults
139        $_config->configuration( $meta, array( $file ));
140
141        if ($_config->locked)
142            ptln('<div class="info">'.$this->getLang('locked').'</div>');
143        elseif ($this->_error)
144            ptln('<div class="error">'.$this->getLang('error').'</div>');
145        elseif ($this->_changed)
146            ptln('<div class="success">'.$this->getLang('updated').'</div>');
147
148	ptln( "<form action='' method='post'>" );
149	ptln( "<input type='hidden' name='vhost[admin]' value='vhost_save' />" );
150	ptln( "<input type='hidden' name='vhost[vhost]' value='$vhost' />" );
151        ptln( '<ul>');
152        foreach( $_config->setting as $setting ) {
153
154          list($label,$input) = $setting->html($this, $this->_error);
155
156          $class = $setting->is_default() ? ' class="default"' : ($setting->is_protected() ? ' class="protected"' : '');
157          $error = $setting->error() ? ' class="value error"' : ' class="value"';
158          $icon = $setting->caution() ? '<img src="'.DOKU_PLUGIN_IMAGES.$setting->caution().'.png" '
159                                .'alt="'.$setting->caution().'" title="'.$this->getLang($setting->caution()).'" />' : '';
160
161          ptln( '<li '.$class.'>');
162          ptln( '<span class="outkey">'.$icon.$label.'</span>');
163          ptln( $input );
164          ptln( '</li>');
165        }
166        ptln( "<li class='submit'>" );
167        ptln( "<input type='submit' value='".$lang['btn_save']."'>" );
168        ptln( "<input type='submit' name=vhost[admin][vhost_delete] value='".$this->getLang( 'btn_vhost_del' )."'>" );
169        ptln( '</li>');
170        ptln( '</ul>');
171	ptln( "</form>" );
172
173    }/*}}}*/
174
175}
176