1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Daniel-Constantin Mierla <miconda@gmail.com>
5 */
6// must be run within Dokuwiki
7if(!defined('DOKU_INC')) die();
8if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
9require_once(DOKU_INC.'inc/blowfish.php');
10
11class helper_plugin_requiz extends DokuWiki_Plugin {
12
13    /**
14     * Check if the REQUIZ should be used. Always check this before using the methods below.
15     *
16     * @return bool true when the REQUIZ should be used
17     */
18    function isEnabled(){
19        if(!$this->getConf('requizusr') && $_SERVER['REMOTE_USER']) return false;
20        return true;
21    }
22
23    /**
24     * Returns the HTML to display the REQUIZ with the chosen method
25     */
26    function getHTML(){
27        global $ID;
28
29		$rand = rand(17,1000000);
30		$qset = $this->getConf('requizset');
31		$qcnt = count($qset);
32	   	$qidx = $rand % $qcnt;
33        $secret = PMA_blowfish_encrypt($rand,auth_cookiesalt());
34
35        $out  = '';
36        $out .= '<div id="plugin__requiz_wrapper">';
37        $out .= '<input type="hidden" name="plugin__requiz_secret" value="'.hsc($secret).'" />';
38        $out .= '<br /><b>'.$qset[$qidx]['question'].'</b><br /> ';
39        $out .= '<label for="plugin__requiz">'.$this->getLang('fillrequiz').'</label> ';
40        $out .= '<select name="plugin__requiz" id="plugin__requiz" class="edit" style="width:200px"> ';
41		$out .= '  <option value="--">--</option>';
42		$acnt = count($qset[$qidx]['answers']);
43		$ilist = range(0, $acnt-1);
44		shuffle($ilist);
45		foreach ($ilist as $i) {
46			$out .= '  <option value="'.$qset[$qidx]['answers'][$i].'">'.$qset[$qidx]['answers'][$i].'</option>';
47		}
48        $out .= '</select>';
49        $out .= '</div><br />';
50        return $out;
51    }
52
53    /**
54     * Checks if the the REQUIZ was solved correctly
55     *
56     * @param  bool $msg when true, an error will be signalled through the msg() method
57     * @return bool true when the answer was correct, otherwise false
58     */
59    function check($msg=true){
60        // compare provided string with decrypted requiz
61        $rand = PMA_blowfish_decrypt($_REQUEST['plugin__requiz_secret'],auth_cookiesalt());
62		$qset = $this->getConf('requizset');
63		$qcnt = count($qset);
64	   	$qidx = $rand % $qcnt;
65
66        if(!$_REQUEST['plugin__requiz_secret'] ||
67           !$_REQUEST['plugin__requiz'] ||
68           $_REQUEST['plugin__requiz'] != $qset[$qidx]['valid']){
69            if($msg) msg($this->getLang('testfailed'),-1);
70            return false;
71        }
72        return true;
73    }
74
75    /**
76     * Build a semi-secret fixed string identifying the current page and user
77     *
78     * This string is always the same for the current user when editing the same
79     * page revision.
80     */
81    function _fixedIdent(){
82        global $ID;
83        $lm = @filemtime(wikiFN($ID));
84        return auth_browseruid().
85               auth_cookiesalt().
86               $ID.$lm;
87    }
88
89
90}
91