Merge pull request #431 from xmujay/0609_monitor
[phpmyadmin/aamir.git] / libraries / sqlvalidator.class.php
blob6c1fb5942c95d872e1afd741b491342c15ecfd93
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * PHP interface to MimerSQL Validator
6 * Copyright 2002, 2003 Robin Johnson <robbat2@users.sourceforge.net>
7 * http://www.orbis-terrarum.net/?l=people.robbat2
9 * All data is transported over HTTP-SOAP
10 * And uses either the PEAR SOAP Module or PHP SOAP extension
12 * Install instructions for PEAR SOAP:
13 * Make sure you have a really recent PHP with PEAR support
14 * run this: "pear install Mail_Mime Net_DIME SOAP"
16 * @access public
18 * @package PhpMyAdmin
20 if (! defined('PHPMYADMIN')) {
21 exit;
24 /**
25 * Load SOAP client.
27 if (class_exists('SOAPClient')) {
28 $GLOBALS['sqlvalidator_error'] = false;
29 $GLOBALS['sqlvalidator_soap'] = 'PHP';
30 } else {
31 @include_once 'SOAP/Client.php';
32 if (class_exists('SOAP_Client')) {
33 $GLOBALS['sqlvalidator_soap'] = 'PEAR';
34 $GLOBALS['sqlvalidator_error'] = false;
35 } else {
36 $GLOBALS['sqlvalidator_soap'] = 'NONE';
37 $GLOBALS['sqlvalidator_error'] = true;
38 PMA_warnMissingExtension('soap');
42 if (!$GLOBALS['sqlvalidator_error']) {
43 // Ok, we have SOAP Support, so let's use it!
45 /**
46 * @package PhpMyAdmin
48 class PMA_SQLValidator
50 var $url;
51 var $service_name;
52 var $wsdl;
53 var $output_type;
55 var $username;
56 var $password;
57 var $calling_program;
58 var $calling_program_version;
59 var $target_dbms;
60 var $target_dbms_version;
61 var $connectionTechnology;
62 var $connection_technology_version;
63 var $interactive;
65 var $service_link = null;
66 var $session_data = null;
69 /**
70 * Private functions - You don't need to mess with these
73 /**
74 * Service opening
76 * @param string $url URL of Mimer SQL Validator WSDL file
78 * @return object Object to use
80 * @access private
82 function _openService($url)
84 if ($GLOBALS['sqlvalidator_soap'] == 'PHP') {
85 $obj = new SOAPClient($url);
86 } else {
87 $obj = new SOAP_Client($url, true);
89 return $obj;
90 } // end of the "openService()" function
93 /**
94 * Service initializer to connect to server
96 * @param object $obj Service object
97 * @param string $username Username
98 * @param string $password Password
99 * @param string $calling_program Name of calling program
100 * @param string $calling_program_version Version of calling program
101 * @param string $target_dbms Target DBMS
102 * @param string $target_dbms_version Version of target DBMS
103 * @param string $connection_technology Connection Technology
104 * @param string $connection_technology_version Con. Technology version
105 * @param integer $interactive boolean 1/0 to specify if
106 * we are an interactive system
108 * @return object stdClass return object with data
110 * @access private
112 function _openSession($obj, $username, $password, $calling_program,
113 $calling_program_version, $target_dbms, $target_dbms_version,
114 $connection_technology, $connection_technology_version, $interactive
116 $use_array = array(
117 "a_userName" => $username,
118 "a_password" => $password,
119 "a_callingProgram" => $calling_program,
120 "a_callingProgramVersion" => $calling_program_version,
121 "a_targetDbms" => $target_dbms,
122 "a_targetDbmsVersion" => $target_dbms_version,
123 "a_connectionTechnology" => $connection_technology,
124 "a_connectionTechnologyVersion" => $connection_technology_version,
125 "a_interactive" => $interactive,
128 if ($GLOBALS['sqlvalidator_soap'] == 'PHP') {
129 $ret = $obj->__soapCall("openSession", $use_array);
130 } else {
131 $ret = $obj->call("openSession", $use_array);
134 return $ret;
135 } // end of the "_openSession()" function
139 * Validator sytem call
141 * @param object $obj Service object
142 * @param object $session Session object
143 * @param string $sql SQL Query to validate
144 * @param string $method Data return type
146 * @return object stClass return with data
148 * @access private
150 function _validateSQL($obj, $session, $sql, $method)
152 $use_array = array(
153 "a_sessionId" => $session->sessionId,
154 "a_sessionKey" => $session->sessionKey,
155 "a_SQL" => $sql,
156 "a_resultType" => $this->output_type,
159 if ($GLOBALS['sqlvalidator_soap'] == 'PHP') {
160 $res = $obj->__soapCall("validateSQL", $use_array);
161 } else {
162 $res = $obj->call("validateSQL", $use_array);
165 return $res;
166 } // end of the "validateSQL()" function
170 * Validator sytem call
172 * @param string $sql SQL Query to validate
174 * @return object stdClass return with data
176 * @access private
178 * @see validateSQL()
180 function _validate($sql)
182 $ret = $this->_validateSQL(
183 $this->service_link, $this->session_data, $sql, $this->output_type
185 return $ret;
186 } // end of the "validate()" function
190 * Public functions
194 * Constructor
196 * @access public
198 function __construct()
200 $this->url = 'http://sqlvalidator.mimer.com/v1/services';
201 $this->service_name = 'SQL99Validator';
202 $this->wsdl = '?wsdl';
204 $this->output_type = 'html';
206 $this->username = 'anonymous';
207 $this->password = '';
208 $this->calling_program = 'PHP_SQLValidator';
209 $this->calling_program_version = PMA_VERSION;
210 $this->target_dbms = 'N/A';
211 $this->target_dbms_version = 'N/A';
212 $this->connection_technology = 'PHP';
213 $this->connection_technology_version = phpversion();
214 $this->interactive = 1;
216 $this->service_link = null;
217 $this->session_data = null;
218 } // end of the "PMA_SQLValidator()" function
222 * Sets credentials
224 * @param string $username the username
225 * @param string $password the password
227 * @return void
228 * @access public
230 function setCredentials($username, $password)
232 $this->username = $username;
233 $this->password = $password;
234 } // end of the "setCredentials()" function
238 * Sets the calling program
240 * @param string $calling_program the calling program name
241 * @param string $calling_program_version the calling program revision
243 * @return void
244 * @access public
246 function setCallingProgram($calling_program, $calling_program_version)
248 $this->calling_program = $calling_program;
249 $this->calling_program_version = $calling_program_version;
250 } // end of the "setCallingProgram()" function
254 * Appends the calling program
256 * @param string $calling_program the calling program name
257 * @param string $calling_program_version the calling program revision
259 * @return void
260 * @access public
262 function appendCallingProgram($calling_program, $calling_program_version)
264 $this->calling_program .= ' - ' . $calling_program;
265 $this->calling_program_version .= ' - ' . $calling_program_version;
266 } // end of the "appendCallingProgram()" function
270 * Sets the target DBMS
272 * @param string $target_dbms the target DBMS name
273 * @param string $target_dbms_version the target DBMS revision
275 * @return void
276 * @access public
278 function setTargetDbms($target_dbms, $target_dbms_version)
280 $this->target_dbms = $target_dbms;
281 $this->target_dbms_version = $target_dbms_version;
282 } // end of the "setTargetDbms()" function
286 * Appends the target DBMS
288 * @param string $target_dbms the target DBMS name
289 * @param string $target_dbms_version the target DBMS revision
291 * @return void
292 * @access public
294 function appendTargetDbms($target_dbms, $target_dbms_version)
296 $this->target_dbms .= ' - ' . $target_dbms;
297 $this->target_dbms_version .= ' - ' . $target_dbms_version;
298 } // end of the "appendTargetDbms()" function
302 * Sets the connection technology used
304 * @param string $connection_technology the con. technology name
305 * @param string $connection_technology_version the con. technology revision
307 * @return void
308 * @access public
310 function setConnectionTechnology(
311 $connection_technology, $connection_technology_version
313 $this->connection_technology = $connection_technology;
314 $this->connection_technology_version = $connection_technology_version;
315 } // end of the "setConnectionTechnology()" function
319 * Appends the connection technology used
321 * @param string $connection_technology the con. technology name
322 * @param string $connection_technology_version the con. technology revision
324 * @return void
325 * @access public
327 function appendConnectionTechnology(
328 $connection_technology, $connection_technology_version
330 $this->connection_technology .= ' - ' . $connection_technology;
331 $this->connection_technology_version .= ' - ' . $connection_technology_version;
332 } // end of the "appendConnectionTechnology()" function
336 * Sets whether interactive mode should be used or not
338 * @param integer $interactive whether interactive mode should be used or not
340 * @return void
341 * @access public
343 function setInteractive($interactive)
345 $this->interactive = $interactive;
346 } // end of the "setInteractive()" function
350 * Sets the output type to use
352 * @param string $output_type the output type to use
354 * @return void
355 * @access public
357 function setOutputType($output_type)
359 $this->output_type = $output_type;
360 } // end of the "setOutputType()" function
364 * Starts service
366 * @return void
367 * @access public
369 function startService()
371 $this->service_link = $this->_openService(
372 $this->url . '/' . $this->service_name . $this->wsdl
374 } // end of the "startService()" function
378 * Starts session
380 * @return void
381 * @access public
383 function startSession()
385 $this->session_data = $this->_openSession(
386 $this->service_link, $this->username, $this->password,
387 $this->calling_program, $this->calling_program_version,
388 $this->target_dbms, $this->target_dbms_version,
389 $this->connection_technology, $this->connection_technology_version,
390 true // FIXME: Are we to tell them that we are interactive?
393 if (isset($this->session_data)
394 && ($this->session_data != null)
395 && ($this->session_data->target != $this->url)
397 // Reopens the service on the new URL that was provided
398 $url = $this->session_data->target;
399 $this->startService();
401 } // end of the "startSession()" function
405 * Do start service and session
407 * @return void
408 * @access public
410 function start()
412 $this->startService();
413 $this->startSession();
414 } // end of the "start()" function
418 * Call to determine just if a query is valid or not.
420 * @param string $sql SQL statement to validate
422 * @return string Validator string from Mimer
424 * @see _validate
426 function isValid($sql)
428 $res = $this->_validate($sql);
429 return $res->standard;
430 } // end of the "isValid()" function
434 * Call for complete validator response
436 * @param string $sql SQL statement to validate
438 * @return string Validator string from Mimer
440 * @see _validate
442 function validationString($sql)
444 $res = $this->_validate($sql);
445 return $res->data;
447 } // end of the "validationString()" function
448 } // end class PMA_SQLValidator
450 //add an extra check to ensure that the class was defined without errors
451 if (!class_exists('PMA_SQLValidator')) {
452 $GLOBALS['sqlvalidator_error'] = true;
455 } // end else