1<?php
2/**
3 * Plugin DokuCrypt: Enables client side encryption
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Scott Moser <smoser@brickies.net>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'syntax.php');
14
15/**
16 * All DokuWiki plugins to extend the parser/rendering mechanism
17 * need to inherit from this class
18 */
19class syntax_plugin_crypt extends DokuWiki_Syntax_Plugin {
20
21    var $curNum=0;
22    var $curLock=0;
23    /**
24     * return some info
25     */
26    function getInfo(){
27        return array(
28            'author' => 'Scott Moser',
29            'email'  => 'smoser@brickies.net',
30            'date'   => '2009-02-17',
31            'name'   => 'Client Side Encryption Plugin',
32            'desc'   => 'Allows Javascript Encryption of wiki text',
33            'url'    => 'http://brickies.net/wiki/dokucrypt:start',
34        );
35    }
36
37    function getType(){ return 'protected'; }
38    function getAllowedTypes() { return array(); }
39    function getSort(){ return 999; }
40    function connectTo($mode) {
41        $this->Lexer->addEntryPattern('<ENCRYPTED.*?>(?=.*?</ENCRYPTED>)',
42            $mode,'plugin_crypt');
43    }
44    function postConnect() {
45        $this->Lexer->addExitPattern('</ENCRYPTED>','plugin_crypt');
46    }
47
48    /**
49     * Handle the match
50     */
51    function handle($match, $state, $pos, &$handler){
52        switch ($state) {
53          case DOKU_LEXER_ENTER :
54                // parse something like <ENCRYPTED> or <ENCRYPTED LOCK=foo>
55                $attr=array( "lock" => "default", "collapsed" => "0" );
56                if(($x=strpos($match,"LOCK="))!==false) {
57                    $x+=strlen("LOCK=");
58                    if(($end=strpos($match," ",$x))!==false) {
59                       $len=$end-$x;
60                    } else { $len=-1; }
61                    $attr["lock"]=substr($match,$x,$len);
62                }
63                if(($x=strpos($match,"COLLAPSED="))!==false) {
64                    $x+=strlen("COLLAPSED=");
65                    if(($end=strpos($match," ",$x))!==false) {
66                       $len=$end-$x;
67                    } else { $len=-1; }
68                    $attr["collapsed"]=substr($match,$x,$len);
69                }
70                return(array($state,$attr));
71          case DOKU_LEXER_UNMATCHED :  return array($state, $match);
72          case DOKU_LEXER_EXIT :       return array($state, '');
73        }
74        return array();
75    }
76
77    /**
78     * Create output
79     */
80    function render($mode, &$renderer, $data) {
81        if($mode == 'xhtml'){
82            list($state, $match) = $data;
83            switch ($state) {
84              case DOKU_LEXER_ENTER :
85                $this->curLock=$match;
86                break;
87              case DOKU_LEXER_UNMATCHED :
88                $curid="crypto_decrypted_" . $this->curNum;
89                // $renderer->doc.="<a href=\"javascript:decryptToId(" .
90                //    "'$curid','" . $this->curLock . "','$match');\">" .
91                //    "Decrypt text</a>\n" .
92                //    "<div id='$curid'></div>\n";
93                $renderer->doc.="<a id='$curid" . "_atag' " .
94                  "class='wikilink1 JSnocheck' " .
95                  "href=\"javascript:toggleCryptDiv(" .
96                  "'$curid','" . $this->curLock["lock"] . "','" .
97                  hsc(str_replace("\n","\\n",$match)) . "');\">" .
98                  "Decrypt Encrypted Text</a>" .
99                  "[<a class='wikilink1 JSnocheck' " .
100                  "href=\"javascript:toggleElemVisibility('$curid');\">" .
101                  "Toggle Visible</a>]\n" .
102                  "<PRE id='$curid' style=\"" .
103                     (($this->curLock["collapsed"] == 1) ?
104                        "visibility:hidden;position:absolute" :
105                        "visibility:visible;position:relative" ) .
106                  "\">".hsc($match)."</PRE>";
107                $this->curNum++;
108                break;
109              case DOKU_LEXER_EXIT :
110                break;
111            }
112            return true;
113        }
114        return false;
115    }
116
117}
118
119
120