1<?php
2/**
3 * Site Export Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13
14require_once(DOKU_PLUGIN.'action.php');
15
16class action_plugin_translator extends DokuWiki_Action_Plugin {
17
18	var $functions = null;		// Helper
19
20	function getInfo(){
21        return array_merge(confToHash(dirname(__FILE__).'/info.txt'), array(
22				'name' => 'Translator (Action Component)',
23		));
24    }
25
26	/**
27	* Register Plugin in DW
28	**/
29	function register(&$controller) {
30		$controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'export_action');
31		$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajax_google_translation');
32	}
33
34	/**
35	* Export via calling URL
36	**/
37	function export_action( &$event ) {
38		global $INFO;
39
40		if (	(empty($_REQUEST['export_language'])
41				|| empty($_REQUEST['Category'])
42				|| empty($_REQUEST['Language'])
43				|| empty($_REQUEST['Version'])
44				|| empty($INFO['userinfo'])) && empty($_REQUEST['fn']) ) return;
45
46		// check Database
47		if ( !$functions =& plugin_load('helper', 'translator') ) {
48			print $this->lang['helpermissing'];
49			return false;
50		}
51
52		$functions->init_database();
53
54		// Handle prior Requests
55		if ( !empty($_REQUEST['fn']) ) {
56			$functions->_handleRequest($_REQUEST['fn']);
57			return;
58		}
59
60		$Category = trim($_REQUEST['Category']);
61		$Language = trim($_REQUEST['Language']);
62		$Version = trim($_REQUEST['Version']);
63		$Encode = empty($_REQUEST['NoEncode']);
64		if ( ($CategoryArray = $functions->_getCategoryFromName($Category)) === false ) {
65			return;
66		}
67
68		if ( $Language != 'export all' && !in_array( $Language, $functions->_getAvailableLanguages($Category) ) ) {
69			msg($functions->_messageReplacer('CategoryNotSupportedInLanguage', array($Category, $Language)), -1);
70		}
71
72		list($CategoryID, $CategoryFileName) = $CategoryArray;
73
74		if ( $Language != 'export all' ) { $Language = array($Language); }
75		else { $Language = $functions->_getAvailableLanguages($CategoryID, $Version); }
76
77		if ( count($Language) > 1 ) {
78			require_once(DOKU_INC . "inc/ZipLib.class.php");
79			$zipFile = new ZipLib();
80		}
81
82		foreach ( $Language as $exportLang ) {
83
84			$fileName = str_replace("(.*?)", ( $exportLang == $this->getConf('default_language') ? "" : "_$exportLang" ), $CategoryFileName);
85
86			$data = $functions->_getTranslation($CategoryID, $Version, $exportLang);
87
88			foreach( array_keys($data) as $key ) {
89				$data[$key] = str_replace("\\\\", "\\", $data[$key]);
90				$data[$key] = str_replace("\\\"", "\"", $data[$key]);
91				$data[$key] = str_replace("\\'", "'", $data[$key]);
92				$data[$key] = stripslashes($key) .'=' . $data[$key]; //( $Encode ? $functions->_stringEncode($data[$key]) : $data[$key]);
93			}
94
95			if ( count($Language) > 1 ) {
96				$zipFile->add_File(implode("\r\n", array_values($data)), $fileName );
97			}
98		}
99
100		if ( count($Language) == 1 ) {
101			$functions->_sendLanguageFile(array_values($data), $fileName);
102		} else {
103			$fileName = str_replace("(.*?)", "_all", $CategoryFileName) . ".zip";
104			$functions->_sendLanguageFile(array(), $fileName);
105			print $zipFile->get_file();
106		}
107
108		exit();
109	}
110
111	function ajax_google_translation(&$event) {
112
113		if ( $event->data != '_google_translation' ) {
114			return;
115		}
116
117		// check Database
118		if ( !$functions =& plugin_load('helper', 'translator') ) {
119			return false;
120		}
121
122		$functions->init_database();
123
124		$url = 'http://ajax.googleapis.com/ajax/services/language/translate';
125		$data = array(
126			'v' => '1.0',
127			'langpair' => 'en|' . $_REQUEST['translation'],
128			'q' => stripslashes(str_replace('\n', '%%%%', $functions->_getMasterKeyIDFromName(intval($_REQUEST['key']), intval($_REQUEST['category']), true, false)))
129		);
130
131		$http = new DokuHTTPClient();
132		//$http->debug = true;
133
134		$response = $http->post($url, $data);
135		if ( empty($response) ) {
136			exit();
137		}
138
139		include_once(DOKU_INC . 'inc/JSON.php');
140		$json = new JSON();
141		$value = $json->decode($response);
142		print preg_replace("/\*?%{4}\s*/", "\n", urldecode($value->responseData->translatedText));
143
144		exit();
145	}
146}