1<?php
2/**
3 * DokuWiki Plugin crossdbsqlclient (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Heiko Heinz <heiko.heinz@soft2c.de>
7 */
8
9// must be run within Dokuwiki
10if (! defined ( 'DOKU_INC' ))
11	die ();
12
13class syntax_plugin_crossdbsqlclient_crossdbsqlclient extends DokuWiki_Syntax_Plugin {
14	/**
15	 *
16	 * @return string Syntax mode type
17	 */
18	public function getType() {
19		return 'container';
20	}
21	/**
22	 *
23	 * @return string Paragraph type
24	 */
25	public function getPType() {
26		return 'block';
27	}
28	/**
29	 *
30	 * @return int Sort order - Low numbers go before high numbers
31	 */
32	public function getSort() {
33		return 260;
34	}
35
36	/**
37	 * Connect lookup pattern to lexer.
38	 *
39	 * @param string $mode
40	 *        	Parser mode
41	 */
42	public function connectTo($mode) {
43		// case 1: crossdbsqlclient
44		// case 2: crossdbsqlclient>type|server|database|user|password
45 		$this->Lexer->addSpecialPattern ( '\{\{crossdbsqlclient\}\}', $mode, 'plugin_crossdbsqlclient_crossdbsqlclient' );
46		$this->Lexer->addSpecialPattern ( '\{\{crossdbsqlclient>.+\|.+\|.+\|.+\|.+\}\}', $mode, 'plugin_crossdbsqlclient_crossdbsqlclient' );
47	}
48
49	// public function postConnect() {
50	// $this->Lexer->addExitPattern('</FIXME>','plugin_crossdbsqlclient_crossdbsqlclient');
51	// }
52
53	/**
54	 * Handle matches of the crossdbsqlclient syntax
55	 *
56	 * @param string $match
57	 *        	The match of the syntax
58	 * @param int $state
59	 *        	The state of the handler
60	 * @param int $pos
61	 *        	The position in the document
62	 * @param Doku_Handler $handler
63	 *        	The handler
64	 * @return array Data for the renderer
65	 */
66	public function handle($match, $state, $pos, Doku_Handler &$handler) {
67
68		$pos = strpos($match, "{{crossdbsqlclient>");
69		if($pos !== FALSE){
70			$data = explode("|",preg_replace("/{{crossdbsqlclient>(.*?)}}/","\\1",$match));
71			return $data;
72		}
73
74		$data = array ();
75		return $data;
76	}
77
78	/**
79	 * Render xhtml output or metadata
80	 *
81	 * @param string $mode
82	 *        	Renderer mode (supported modes: xhtml)
83	 * @param Doku_Renderer $renderer
84	 *        	The renderer
85	 * @param array $data
86	 *        	The data from the handler() function
87	 * @return bool If rendering was successful.
88	 */
89	public function render($mode, Doku_Renderer &$renderer, $data) {
90
91		if ($mode != 'xhtml')
92			return false;
93
94		$renderer->info ['cache'] = false;
95
96		if (! $this->isAuthorized ()) {
97			echo '<div class="error">' . $this->getLang ( 'missingpermission' ) . '</div>';
98			return true;
99		}
100
101		$button = $this->getLang ( 'submit_button' );
102
103		$query='';
104		if(isset($_GET ['q']))
105		$query = trim ( $_GET ['q'] );
106
107		$queryEsc = strtr ( $query, array (
108				'<' => '&lt;'
109		) );
110
111		$templates = $this->getTemplates ();
112
113		$emptyresult = $this->getLang ( 'emptyresult' );
114
115		echo <<<EOT
116<script type="text/javascript"><!--
117function crossdbsqlclient_load(query)
118{
119	with ( document.crossdbsqlclient.q )
120	{
121		value = query;
122		focus();
123	}
124
125	return false;
126}
127//--></script>
128<form action="$_SERVER[PHP_SELF]" method="GET" name="crossdbsqlclient" id="crossdbsqlclient">
129 <input type="hidden" name="do" value="$_REQUEST[do]" />
130 <input type="hidden" name="page" value="$_REQUEST[page]" />
131 <input type="hidden" name="id" value="$_REQUEST[id]" />
132 <div>
133  $templates
134 </div>
135 $history
136 <textarea name="q" rows="5" cols="100" style="width: 100%;">$queryEsc</textarea>
137 <div>
138  <input style="float:right;padding:4px;margin:10px" type="submit" value="$button" />
139 </div>
140</form>
141<script type="text/javascript"><!--
142document.crossdbsqlclient.q.focus();
143document.crossdbsqlclient.q.select();
144//--></script>
145EOT;
146		$db = $this->connectToDb ($data);
147
148		$db->executeQuery($query);
149
150		return true;
151	}
152
153
154
155
156	private function getTemplates() {
157		$ret = '<u><b>' . $this->getLang ( 'templatesLabel' ) . '</b></u><br>';
158
159		$text = $this->getLang ( 'template.1.text' );
160		if ($text == '') {
161			return '';
162		}
163
164		for($i = 1; $i <= true; $i ++) {
165			$text = $this->getLang ( 'template.' . $i . '.text' );
166			if ($text == '') {
167				return $ret;
168			}
169
170			$sql = $this->getLang ( 'template.' . $i . '.sql' );
171			$ret = $ret . '<button style="margin:4px" onclick="return crossdbsqlclient_load(\'' . $sql . '\')">' . $text . '</button>';
172		}
173
174		return $ret;
175	}
176
177	private function connectToDb($data) { // DB-Verbindung herstellen - eRent
178		if(isset($data) && empty($data)==FALSE){
179			//type@server@database@user@password
180			$dbtype = $data[0];
181			$serverName = $data[1];
182			$dbName = $data[2];
183			$userName = $data[3];
184			$passWord = $data[4];
185			return $this->connectToDbWrapper($dbtype, $serverName, $userName, $passWord, $dbName);
186		}
187
188		return $this->connectToDefaultDb();
189	}
190
191
192	private function connectToDefaultDb() { // DB-Verbindung herstellen - eRent
193		$dbtype = $this->getConf ('dbtype');
194		$serverName = $this->getConf ( 'serverName' );
195		$userName = $this->getConf ( 'userName' );
196		$passWord = $this->getConf ( 'passWord' );
197		$dbName = $this->getConf ( 'dbName' );
198
199		return $this->connectToDbWrapper($dbtype, $serverName, $userName, $passWord, $dbName);
200	}
201
202	private function connectToDbWrapper($dbtype, $serverName, $userName, $passWord, $dbName) {
203
204		self::includeLib();
205
206		$dbWrapper = new DbWrapper($this, $dbtype, $serverName, $userName, $passWord, $dbName );
207
208		return $dbWrapper;
209	}
210
211	protected function isAuthorized() {
212		global $INPUT;
213		$remoteUser = $INPUT->server->str ( 'REMOTE_USER' );
214
215		if (! $remoteUser) {
216			return false;
217		}
218
219		global $USERINFO;
220		$groups = $USERINFO ['grps'];
221		$allowedUserGroups = $this->getConf ( 'allowedUserGroups' );
222
223		$allowedUserGroups = utf8_strtolower ( $allowedUserGroups );
224		$members = explode ( ',', $allowedUserGroups );
225		$members = array_map ( 'trim', $members );
226		$members = array_unique ( $members );
227		$members = array_filter ( $members );
228
229		// compare cleaned values
230		foreach ( $members as $member ) {
231			if ($member == 'ALL')
232				return true;
233			if (in_array ( $member, $groups ))
234				return true;
235			else {
236				if ($member == $remoteUser)
237					return true;
238			}
239		}
240		return false;
241	}
242
243	public static function includeLib()
244	{
245
246		if ( !class_exists( 'DbWrapper' ) )
247		{
248
249			$libFile = dirname( __FILE__ ) . '/dbwrapper.php';
250
251
252			{ include_once( $libFile ); }
253
254		}
255	}
256}
257
258// vim:ts=4:sw=4:et:
259