1<?php
2/**
3 * @license    GPL3 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Samuel Fischer <sf@notomorrow.de>
5 *
6 */
7
8if(!defined('DOKU_INC')) die();
9if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
10
11require_once(DOKU_PLUGIN.'action.php');
12
13class action_plugin_forcessllogin extends DokuWiki_Action_Plugin {
14  function getInfo( ) {
15    return array(
16        'author' => 'Samuel Fischer',
17        'email'  => 'sf@notomorrow.de',
18        'date'   => '2012-01-23',
19        'name'   => 'forcessllogin',
20        'desc'   => 'redirects login requests to ssl',
21        'url'    => 'http://www.dokuwiki.org/plugin:forcessllogin',
22    );
23  }
24  function register(Doku_Event_Handler $controller) {
25    $controller->register_hook('TPL_ACT_RENDER', 'BEFORE',  $this, 'forcessllogin');
26    $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE',  $this, 'forcessllogin');
27  }
28  function forcessllogin(&$event, $param) {
29    global $ACT;
30    if( is_ssl( )) return;
31
32    $acts = explode(',',$this->getConf('actions'));
33    $intercept = false;
34
35    if( !is_array( $acts )) $acts = array( );
36    if( $ACT == 'denied' && in_array( 'login', $acts )){ $intercept = true; }
37    elseif( !in_array( $ACT, $acts )) return;
38
39    if( $event->name == 'ACTION_ACT_PREPROCESS' && !$this->getConf('splashpage')) {
40      send_redirect( $this->_path( ));
41      exit;
42    }
43    if( $event->name == 'TPL_ACT_RENDER' ) {
44      $event->preventDefault();
45      if( $intercept ){
46        if( $ACT == 'denied' ) echo p_locale_xhtml('denied');
47        if( !in_array( $ACT, $acts )) return;
48        $ACT = 'login';
49      }
50      echo $this->locale_xhtml('splashpage');
51      $this->_render( $ACT );
52    }
53  }
54  function _render( $act ) {
55    global $ID;
56    $form = new Doku_Form(array('id'=>'forcessllogin1',
57        'action' => $this->_path( ),
58        'method' => 'get'));
59     $form->addHidden( 'id', $ID );
60     $form->addHidden( 'do', $act );
61    if( $this->getConf('cert')) {
62      if( strpos( $this->getLang('certinfo'), '{{name}}' ) !== false ) {
63        $form->addElement('<p>'
64            .str_replace( '{{name}}', $this->getConf('cert'), $this->getLang('certinfo') )
65            .'</p>'.NL ); }
66      else {
67        $form->addElement('<p>'.$this->getLang('certinfo')." ".$this->getConf('cert').'</p>'.NL ); }}
68
69    if( $this->getConf('ca')) {
70      if( strpos( $this->getLang('ca'), '{{name}}' ) !== false ) {
71        $form->addElement('<p>'
72            .str_replace( '{{name}}', $this->getConf('ca'), $this->getLang('cainfo') )
73            .'</p>'.NL ); }
74      else {
75        $form->addElement('<p>'.$this->getLang('cainfo')
76            ." <a href='".$this->getConf('ca')."'>".$this->getConf('ca')."</a></p>".NL ); }}
77
78    $form->addElement(form_makeButton('submit','',$this->getLang('submit'),
79      array('accesskey'=>'h','title'=>$this->getLang('submittitle'), 'id'=>'focus__this' )));
80    $form->printForm();
81
82    $form = new Doku_Form(array('id'=>'forcessllogin2', 'method' => 'get'));
83    $form->addElement(form_makeButton('submit','',$this->getLang('cancel'),
84      array('accesskey'=>'c','title'=>$this->getLang('canceltitle'))));
85    $form->printForm();
86  }
87  function _path( ) {
88    $base = str_replace( 'http://', 'https://', getBaseUrl( 'absolute' ));
89    $base = rtrim($base, getBaseUrl());  # right trim the dokuwiki directory from the base URL
90    $path = $_SERVER['REQUEST_URI'];
91    return $base.$path;
92  }
93}
94