1<?php
2	/**
3	 *
4	 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
5	 * @author     Andreas Gohr <andi@splitbrain.org>
6	 */
7	// must be run within Dokuwiki
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_crypto extends DokuWiki_Action_Plugin {
14
15	/**
16	 * Constructor. Load helper plugin
17	 */
18	function action_plugin_crypto(){
19	}
20
21	/**
22	 * Registers a callback function for a given event
23	 */
24	function register($controller) {
25		$controller->register_hook('DOKUWIKI_STARTED', 'AFTER',  $this, 'add_crypto_config');
26		$controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button');
27		$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'insert_javascript');
28	}
29
30	function add_crypto_config(&$event, $param) {
31		global $JSINFO;
32		$JSINFO['crypto_cache_password'] = $this->getConf("cache_password");
33	}
34
35	function insert_button(&$event, $param) {
36		$event->data []= array(
37			'type'  => 'encrypt',
38			'title' => 'Encrypt selected Text',
39			'icon'  => DOKU_BASE.'lib/plugins/crypto/lock.png');
40		$event->data []= array(
41			'type'  => 'decrypt',
42			'title' => 'Decrypt selected Text',
43			'icon'  => DOKU_BASE.'lib/plugins/crypto/lock_break.png');
44	}
45
46	function insert_javascript(&$event, $param) {
47		$event->data["script"][] = array (
48			"type" => "text/javascript",
49			"src" => DOKU_BASE."lib/plugins/crypto/js/encrypt-dialog.js",
50			"_data" => "",
51		);
52
53		$event->data["script"][] = array (
54			"type" => "text/javascript",
55			"src" => DOKU_BASE."lib/plugins/crypto/js/decrypt-dialog.js",
56			"_data" => "",
57		);
58
59		$event->data["script"][] = array (
60			"type" => "text/javascript",
61			"src" => DOKU_BASE."lib/plugins/crypto/js/toggle-encryption-dialog.js",
62			"_data" => "",
63		);
64	}
65}
66
67function crypto_error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
68	throw new Exception("Crypt Error");
69}
70
71function crypto_encrypt($data, $secret) {
72	require_once(DOKU_INC.'inc/blowfish.php');
73	$data = PMA_blowfish_encrypt($data, $secret);
74	$data = wordwrap($data, 8, ' ', true);
75	return $data;
76}
77
78function crypto_decrypt($data, $secret) {
79	$data = strtr($data, ' ', '');
80	require_once(DOKU_INC.'inc/blowfish.php');
81	$data = PMA_blowfish_decrypt($data, $secret);
82	return $data;
83}
84
85function ajax_crypto_encrypt() {
86	set_error_handler("crypto_error_handler");
87	$result = "";
88	try {
89		$data   = $_POST['data'];
90		$secret = $_POST['secret'];
91		if (empty($secret)) {
92			print "";
93		} else {
94			$result = crypto_encrypt($secret.$data, $secret);
95		}
96	} catch (Exception $e) {
97		$result = $e->getMessage();
98	}
99
100	print $result;
101	restore_error_handler();
102}
103
104function ajax_crypto_decrypt() {
105	set_error_handler("crypto_error_handler");
106	$result = "";
107	try {
108		$data   = $_POST['data'];
109		$secret = $_POST['secret'];
110		if (empty($secret)) {
111			print "";
112		} else {
113			$result = crypto_decrypt($data, $secret);
114			if (strpos($result, $secret) === 0) {
115				$result = substr($result, strlen($secret), strlen($data) - strlen($secret));
116			} else {
117				$result = "";
118			}
119		}
120	} catch (Exception $e) {
121		$result = $e->getMessage();
122	}
123
124	print $result;
125	restore_error_handler();
126}
127