1 <?php
2 /**
3  * Dokuwiki Action Plugin Subconfhelper
4  *
5  * @license   GPLv3(http://www.gnu.org/licenses/gpl.html)
6  * @author    sf@notomorrow.de
7  */
8 
9 if( !defined( 'DOKU_INC' )) die( );
10 if( !defined( 'DOKU_PLUGIN' )) define( 'DOKU_PLUGIN',DOKU_INC.'lib/plugins/' );
11 require_once( DOKU_PLUGIN.'action.php' );
12 
13 class action_plugin_subconfhelper extends DokuWiki_Action_Plugin {
14 
15   var $sconf = array( );
16 
17   function getInfo( ){
18     return array(
19         'author' => 'ai',
20         'email'  => 'ai',
21         'date'   => '2013-09-30',
22         'name'   => 'subconfhelper',
23         'desc'   => 'override various configuration settings per subdomain',
24         'url'    => 'https://www.dokuwiki.org/plugin:subconfhelper' );
25   }
26   function register( &$controller) {
27     $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE',  $this, 'check_vhost' );
28     $controller->register_hook('TPL_ACT_RENDER', 'BEFORE',  $this, 'check_act' );
29   }
30   function read_config( $domain ) {
31     if( !$domain ) { return false; }
32     if( isset( $this->sconf[$domain] )) {
33       return $this->sconf[$domain]; }
34     else {
35       $this->sconf[$domain] = array( );
36       $conf = &$this->sconf[$domain]; }
37     $this->config_path = DOKU_CONF;
38     $this->config_prefix = 'subconfhelper_';
39     while( $domain && !$subdomain ) {
40         $conf_file = $this->config_path.$this->config_prefix.$domain.'.php';
41         if( !file_exists( $conf_file )) {
42             if( strpos( $domain, '.' )) {
43                 $domain = substr( $domain, 0, strrpos( $domain, '.' )); }
44             else {
45                 $subdomain = $domain; }}
46         else {
47             $subdomain = $domain; }}
48     if( file_exists( $conf_file )) {
49         require_once( $conf_file ); }
50     return $conf;
51   }
52   function check_vhost( &$event, $param ) {
53     global $ACT, $INFO;
54     $domain = $_SERVER['HTTP_HOST'];
55     $sconf  = $this->read_config( $domain );
56     if( is_array( $sconf )) {
57         if( $ACT == 'register' && $_POST['save'] ) {
58             if( $this->override_register( $sconf )) {
59                 $ACT = 'login'; }
60             else {
61       	      $_POST['save'] = false; }}
62         $this->override_defaultpage( $sconf );
63         $this->override_template( $sconf );
64         $this->override_namespace( $sconf );
65         $this->override_conf( $sconf );
66     }
67   }
68   function check_act( &$event, $param ) {
69     global $conf, $ACT, $INFO;
70     $domain = $_SERVER['HTTP_HOST'];
71     $sconf = $this->read_config( $domain );
72     if( $ACT == 'index' && $sconf['ns'] ) {
73       $dir = $conf['datadir'];
74       $ns  = cleanID($ns);
75       $ns  = utf8_encodeFN( str_replace( ':', '/', $ns));
76       echo p_locale_xhtml('index');
77       echo '<div id="index__tree">';
78       $data = array();
79       search($data,$conf['datadir'],'search_index',array( 'ns' => $ns ), $sconf['ns'] );
80       echo html_buildlist( $data,'idx','html_list_index','html_li_index' );
81       echo '</div>';
82       $event->preventDefault();
83     }
84   }
85   function override_conf( $conf_override ) {
86       global $conf;
87       foreach( array( 'disableactions' ) as $key ) {
88           if( isset( $conf_override[$key] )) {
89       	    $conf[$key] = $conf_override[$key]; }}
90   }
91   function override_register( $conf_override ) {
92     global $lang;
93     global $conf;
94     global $auth;
95     global $INPUT;
96 
97     if(!$INPUT->post->bool('save')) return false;
98     if(!actionOK('register')) return false;
99 
100     $default_groups = isset( $conf_override['default_groups'] ) ? $conf_override['default_groups'] : $conf['default_groups'];
101     if(!$auth) return false;
102 
103     // gather input
104     $login    = trim($auth->cleanUser($INPUT->post->str('login')));
105     $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname')));
106     $email    = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email')));
107     $pass     = $INPUT->post->str('pass');
108     $passchk  = $INPUT->post->str('passchk');
109 
110     if(empty($login) || empty($fullname) || empty($email)) {
111         msg($lang['regmissing'], -1);
112         return false;
113     }
114 
115     if($conf['autopasswd']) {
116         $pass = auth_pwgen($login); // automatically generate password
117     } elseif(empty($pass) || empty($passchk)) {
118         msg($lang['regmissing'], -1); // complain about missing passwords
119         return false;
120     } elseif($pass != $passchk) {
121         msg($lang['regbadpass'], -1); // complain about misspelled passwords
122         return false;
123     }
124 
125     //check mail
126     if(!mail_isvalid($email)) {
127         msg($lang['regbadmail'], -1);
128         return false;
129     }
130 
131     //okay try to create the user
132     if(!$auth->triggerUserMod('create', array($login, $pass, $fullname, $email, $default_group ))) {
133         msg($lang['reguexists'], -1);
134         return false;
135     }
136 
137     // send notification about the new user
138     $subscription = new Subscription();
139     $subscription->send_register($login, $fullname, $email);
140 
141     // are we done?
142     if(!$conf['autopasswd']) {
143         msg($lang['regsuccess2'], 1);
144         return true;
145     }
146 
147     // autogenerated password? then send password to user
148     if(auth_sendPassword($login, $pass)) {
149         msg($lang['regsuccess'], 1);
150         return true;
151     } else {
152         msg($lang['regmailfail'], -1);
153         return false;
154     }
155   }
156   function override_template( $conf_override ) {
157     $t = plugin_load( 'action', 'templateconfhelper_templateaction' );
158     if( $t ) {
159       $t->tpl_switch( $conf_override['template'] ); }
160   }
161   function override_defaultpage( $conf_override ) {
162     global $ID,$INFO,$conf;
163     if( $defaultpage = $conf_override['default_startpage'] ) {
164 	if( $ID == $conf['start'] ) {
165 	    $ID	  = $defaultpage;
166 	    $NS   = getNS( $ID );
167 	    $INFO = pageinfo( );
168 	    $JSINFO['id'] = $ID;
169 	    $INFO['namespace'] = (string) $INFO['namespace'];
170 	    if( $conf['breadcrumbs'] ) breadcrumbs( ); }}
171   }
172   function override_namespace( $conf_override ) {
173     global $ID,$INFO,$conf;
174     if( !$conf_override['ns'] ) { return ''; }
175     $path = explode( ':', $ID );
176     if( strpos( $ID, $conf_override['ns'] ) === 0 ) { return ''; }
177     if( strpos( ':'.$ID, str_replace( '/', ':',
178                     $conf_override['ns'] )) === 0 ) { return ''; }
179     if( $conf_override['ns_inherit'] ) {
180       $newfile = wikiFN( $conf_override['ns'].':'.$ID );
181       if( !file_exists( wikiFN( $conf_override['ns'].':'.$ID ))
182                      && file_exists( wikiFN($ID))) { return ''; }}
183     $ID = $conf_override['ns'].':'.$ID;
184     if( strpos( $ID, ':' ) === 0 ) {
185 	$ID = substr( $ID, 1 ); }
186     $NS = getNS( $ID );
187     $INFO = pageinfo( );
188     $JSINFO['id'] = $ID;
189     $INFO['namespace'] = (string) $INFO['namespace'];
190     if( $conf['breadcrumbs'] ) breadcrumbs();
191   }
192 }
193