<?php
	/**
	 *
	 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
	 * @author     Andreas Gohr <andi@splitbrain.org>
	 */
	// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN'))   define('DOKU_PLUGIN',  DOKU_INC.'lib/plugins/');

require_once(DOKU_PLUGIN.'action.php');

class action_plugin_crypto extends DokuWiki_Action_Plugin {

	/**
	 * Constructor. Load helper plugin
	 */
	function action_plugin_crypto(){
	}

	/**
	 * Registers a callback function for a given event
	 */
	function register($controller) {
		$controller->register_hook('DOKUWIKI_STARTED', 'AFTER',  $this, 'add_crypto_config');
		$controller->register_hook('TOOLBAR_DEFINE', 'AFTER', $this, 'insert_button');
		$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'insert_javascript');
	}

	function add_crypto_config(&$event, $param) {
		global $JSINFO;
		$JSINFO['crypto_cache_password'] = $this->getConf("cache_password");
	}

	function insert_button(&$event, $param) {
		$event->data []= array(
			'type'  => 'encrypt',
			'title' => 'Encrypt selected Text',
			'icon'  => DOKU_BASE.'lib/plugins/crypto/lock.png');
		$event->data []= array(
			'type'  => 'decrypt',
			'title' => 'Decrypt selected Text',
			'icon'  => DOKU_BASE.'lib/plugins/crypto/lock_break.png');
	}

	function insert_javascript(&$event, $param) {
		$event->data["script"][] = array (
			"type" => "text/javascript",
			"src" => DOKU_BASE."lib/plugins/crypto/js/encrypt-dialog.js",
			"_data" => "",
		);

		$event->data["script"][] = array (
			"type" => "text/javascript",
			"src" => DOKU_BASE."lib/plugins/crypto/js/decrypt-dialog.js",
			"_data" => "",
		);

		$event->data["script"][] = array (
			"type" => "text/javascript",
			"src" => DOKU_BASE."lib/plugins/crypto/js/toggle-encryption-dialog.js",
			"_data" => "",
		);
	}
}

function crypto_error_handler($errno, $errstr, $errfile, $errline, $errcontext) {
	throw new Exception("Crypt Error");
}

function crypto_encrypt($data, $secret) {
	require_once(DOKU_INC.'inc/blowfish.php');
	$data = PMA_blowfish_encrypt($data, $secret);
	$data = wordwrap($data, 8, ' ', true);
	return $data;
}

function crypto_decrypt($data, $secret) {
	$data = strtr($data, ' ', '');
	require_once(DOKU_INC.'inc/blowfish.php');
	$data = PMA_blowfish_decrypt($data, $secret);
	return $data;
}

function ajax_crypto_encrypt() {
	set_error_handler("crypto_error_handler");
	$result = "";
	try {
		$data   = $_POST['data'];
		$secret = $_POST['secret'];
		if (empty($secret)) {
			print "";
		} else {
			$result = crypto_encrypt($secret.$data, $secret);
		}
	} catch (Exception $e) {
		$result = $e->getMessage();
	}

	print $result;
	restore_error_handler();
}

function ajax_crypto_decrypt() {
	set_error_handler("crypto_error_handler");
	$result = "";
	try {
		$data   = $_POST['data'];
		$secret = $_POST['secret'];
		if (empty($secret)) {
			print "";
		} else {
			$result = crypto_decrypt($data, $secret);
			if (strpos($result, $secret) === 0) {
				$result = substr($result, strlen($secret), strlen($data) - strlen($secret));
			} else {
				$result = "";
			}
		}
	} catch (Exception $e) {
		$result = $e->getMessage();
	}

	print $result;
	restore_error_handler();
}
