/gui/tools/pma/libraries/sqlvalidator.class.php

https://github.com/BenBE/ispCP · PHP · 439 lines · 188 code · 60 blank · 191 comment · 21 complexity · 5b47be4d5d57ed0d9c3e9c698db469f0 MD5 · raw file

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * PHP interface to MimerSQL Validator
  5. *
  6. * Copyright 2002, 2003 Robin Johnson <robbat2@users.sourceforge.net>
  7. * http://www.orbis-terrarum.net/?l=people.robbat2
  8. *
  9. * All data is transported over HTTP-SOAP
  10. * And uses either the PEAR SOAP Module or PHP SOAP extension
  11. *
  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"
  15. *
  16. * @access public
  17. *
  18. * @package phpMyAdmin
  19. */
  20. if (! defined('PHPMYADMIN')) {
  21. exit;
  22. }
  23. /**
  24. * Load SOAP client.
  25. */
  26. if (class_exists('SOAPClient')) {
  27. $GLOBALS['sqlvalidator_error'] = false;
  28. $GLOBALS['sqlvalidator_soap'] = 'PHP';
  29. } else {
  30. @include_once 'SOAP/Client.php';
  31. if (class_exists('SOAP_Client')) {
  32. $GLOBALS['sqlvalidator_soap'] = 'PEAR';
  33. $GLOBALS['sqlvalidator_error'] = false;
  34. } else {
  35. $GLOBALS['sqlvalidator_soap'] = 'NONE';
  36. $GLOBALS['sqlvalidator_error'] = TRUE;
  37. PMA_warnMissingExtension('soap');
  38. }
  39. }
  40. if (!$GLOBALS['sqlvalidator_error']) {
  41. // Ok, we have SOAP Support, so let's use it!
  42. /**
  43. * @package phpMyAdmin
  44. */
  45. class PMA_SQLValidator {
  46. var $url;
  47. var $service_name;
  48. var $wsdl;
  49. var $output_type;
  50. var $username;
  51. var $password;
  52. var $calling_program;
  53. var $calling_program_version;
  54. var $target_dbms;
  55. var $target_dbms_version;
  56. var $connectionTechnology;
  57. var $connection_technology_version;
  58. var $interactive;
  59. var $service_link = null;
  60. var $session_data = null;
  61. /**
  62. * Private functions - You don't need to mess with these
  63. */
  64. /**
  65. * Service opening
  66. *
  67. * @param string URL of Mimer SQL Validator WSDL file
  68. *
  69. * @return object Object to use
  70. *
  71. * @access private
  72. */
  73. function _openService($url)
  74. {
  75. if ($GLOBALS['sqlvalidator_soap'] == 'PHP') {
  76. $obj = new SOAPClient($url);
  77. } else {
  78. $obj = new SOAP_Client($url, TRUE);
  79. }
  80. return $obj;
  81. } // end of the "openService()" function
  82. /**
  83. * Service initializer to connect to server
  84. *
  85. * @param object Service object
  86. * @param string Username
  87. * @param string Password
  88. * @param string Name of calling program
  89. * @param string Version of calling program
  90. * @param string Target DBMS
  91. * @param string Version of target DBMS
  92. * @param string Connection Technology
  93. * @param string version of Connection Technology
  94. * @param integer boolean of 1/0 to specify if we are an interactive system
  95. *
  96. * @return object stdClass return object with data
  97. *
  98. * @access private
  99. */
  100. function _openSession($obj, $username, $password,
  101. $calling_program, $calling_program_version,
  102. $target_dbms, $target_dbms_version,
  103. $connection_technology, $connection_technology_version,
  104. $interactive)
  105. {
  106. $use_array = array(
  107. "a_userName" => $username,
  108. "a_password" => $password,
  109. "a_callingProgram" => $calling_program,
  110. "a_callingProgramVersion" => $calling_program_version,
  111. "a_targetDbms" => $target_dbms,
  112. "a_targetDbmsVersion" => $target_dbms_version,
  113. "a_connectionTechnology" => $connection_technology,
  114. "a_connectionTechnologyVersion" => $connection_technology_version,
  115. "a_interactive" => $interactive,
  116. );
  117. if ($GLOBALS['sqlvalidator_soap'] == 'PHP') {
  118. $ret = $obj->__soapCall("openSession", $use_array);
  119. } else {
  120. $ret = $obj->call("openSession", $use_array);
  121. }
  122. return $ret;
  123. } // end of the "_openSession()" function
  124. /**
  125. * Validator sytem call
  126. *
  127. * @param object Service object
  128. * @param object Session object
  129. * @param string SQL Query to validate
  130. * @param string Data return type
  131. *
  132. * @return object stClass return with data
  133. *
  134. * @access private
  135. */
  136. function _validateSQL($obj, $session, $sql, $method)
  137. {
  138. $use_array = array(
  139. "a_sessionId" => $session->sessionId,
  140. "a_sessionKey" => $session->sessionKey,
  141. "a_SQL" => $sql,
  142. "a_resultType" => $this->output_type,
  143. );
  144. if ($GLOBALS['sqlvalidator_soap'] == 'PHP') {
  145. $res = $obj->__soapCall("validateSQL", $use_array);
  146. } else {
  147. $res = $obj->call("validateSQL", $use_array);
  148. }
  149. return $res;
  150. } // end of the "validateSQL()" function
  151. /**
  152. * Validator sytem call
  153. *
  154. * @param string SQL Query to validate
  155. *
  156. * @return object stdClass return with data
  157. *
  158. * @access private
  159. *
  160. * @see validateSQL()
  161. */
  162. function _validate($sql)
  163. {
  164. $ret = $this->_validateSQL($this->service_link, $this->session_data,
  165. $sql, $this->output_type);
  166. return $ret;
  167. } // end of the "validate()" function
  168. /**
  169. * Public functions
  170. */
  171. /**
  172. * Constructor
  173. *
  174. * @access public
  175. */
  176. function __construct()
  177. {
  178. $this->url = 'http://sqlvalidator.mimer.com/v1/services';
  179. $this->service_name = 'SQL99Validator';
  180. $this->wsdl = '?wsdl';
  181. $this->output_type = 'html';
  182. $this->username = 'anonymous';
  183. $this->password = '';
  184. $this->calling_program = 'PHP_SQLValidator';
  185. $this->calling_program_version = PMA_VERSION;
  186. $this->target_dbms = 'N/A';
  187. $this->target_dbms_version = 'N/A';
  188. $this->connection_technology = 'PHP';
  189. $this->connection_technology_version = phpversion();
  190. $this->interactive = 1;
  191. $this->service_link = null;
  192. $this->session_data = null;
  193. } // end of the "PMA_SQLValidator()" function
  194. /**
  195. * Sets credentials
  196. *
  197. * @param string the username
  198. * @param string the password
  199. *
  200. * @access public
  201. */
  202. function setCredentials($username, $password)
  203. {
  204. $this->username = $username;
  205. $this->password = $password;
  206. } // end of the "setCredentials()" function
  207. /**
  208. * Sets the calling program
  209. *
  210. * @param string the calling program name
  211. * @param string the calling program revision
  212. *
  213. * @access public
  214. */
  215. function setCallingProgram($calling_program, $calling_program_version)
  216. {
  217. $this->calling_program = $calling_program;
  218. $this->calling_program_version = $calling_program_version;
  219. } // end of the "setCallingProgram()" function
  220. /**
  221. * Appends the calling program
  222. *
  223. * @param string the calling program name
  224. * @param string the calling program revision
  225. *
  226. * @access public
  227. */
  228. function appendCallingProgram($calling_program, $calling_program_version)
  229. {
  230. $this->calling_program .= ' - ' . $calling_program;
  231. $this->calling_program_version .= ' - ' . $calling_program_version;
  232. } // end of the "appendCallingProgram()" function
  233. /**
  234. * Sets the target DBMS
  235. *
  236. * @param string the target DBMS name
  237. * @param string the target DBMS revision
  238. *
  239. * @access public
  240. */
  241. function setTargetDbms($target_dbms, $target_dbms_version)
  242. {
  243. $this->target_dbms = $target_dbms;
  244. $this->target_dbms_version = $target_dbms_version;
  245. } // end of the "setTargetDbms()" function
  246. /**
  247. * Appends the target DBMS
  248. *
  249. * @param string the target DBMS name
  250. * @param string the target DBMS revision
  251. *
  252. * @access public
  253. */
  254. function appendTargetDbms($target_dbms, $target_dbms_version)
  255. {
  256. $this->target_dbms .= ' - ' . $target_dbms;
  257. $this->target_dbms_version .= ' - ' . $target_dbms_version;
  258. } // end of the "appendTargetDbms()" function
  259. /**
  260. * Sets the connection technology used
  261. *
  262. * @param string the connection technology name
  263. * @param string the connection technology revision
  264. *
  265. * @access public
  266. */
  267. function setConnectionTechnology($connection_technology, $connection_technology_version)
  268. {
  269. $this->connection_technology = $connection_technology;
  270. $this->connection_technology_version = $connection_technology_version;
  271. } // end of the "setConnectionTechnology()" function
  272. /**
  273. * Appends the connection technology used
  274. *
  275. * @param string the connection technology name
  276. * @param string the connection technology revision
  277. *
  278. * @access public
  279. */
  280. function appendConnectionTechnology($connection_technology, $connection_technology_version)
  281. {
  282. $this->connection_technology .= ' - ' . $connection_technology;
  283. $this->connection_technology_version .= ' - ' . $connection_technology_version;
  284. } // end of the "appendConnectionTechnology()" function
  285. /**
  286. * Sets whether interactive mode should be used or not
  287. *
  288. * @param integer whether interactive mode should be used or not
  289. *
  290. * @access public
  291. */
  292. function setInteractive($interactive)
  293. {
  294. $this->interactive = $interactive;
  295. } // end of the "setInteractive()" function
  296. /**
  297. * Sets the output type to use
  298. *
  299. * @param string the output type to use
  300. *
  301. * @access public
  302. */
  303. function setOutputType($output_type)
  304. {
  305. $this->output_type = $output_type;
  306. } // end of the "setOutputType()" function
  307. /**
  308. * Starts service
  309. *
  310. * @access public
  311. */
  312. function startService()
  313. {
  314. $this->service_link = $this->_openService($this->url . '/' . $this->service_name . $this->wsdl);
  315. } // end of the "startService()" function
  316. /**
  317. * Starts session
  318. *
  319. * @access public
  320. */
  321. function startSession()
  322. {
  323. $this->session_data = $this->_openSession($this->service_link, $this->username, $this->password,
  324. $this->calling_program, $this->calling_program_version,
  325. $this->target_dbms, $this->target_dbms_version,
  326. $this->connection_technology, $this->connection_technology_version,
  327. $this->interactive);
  328. if (isset($this->session_data) && ($this->session_data != null)
  329. && ($this->session_data->target != $this->url)) {
  330. // Reopens the service on the new URL that was provided
  331. $url = $this->session_data->target;
  332. $this->startService();
  333. }
  334. } // end of the "startSession()" function
  335. /**
  336. * Do start service and session
  337. *
  338. * @access public
  339. */
  340. function start()
  341. {
  342. $this->startService();
  343. $this->startSession();
  344. } // end of the "start()" function
  345. /**
  346. * Call to determine just if a query is valid or not.
  347. *
  348. * @param string SQL statement to validate
  349. *
  350. * @return string Validator string from Mimer
  351. *
  352. * @see _validate
  353. */
  354. function isValid($sql)
  355. {
  356. $res = $this->_validate($sql);
  357. return $res->standard;
  358. } // end of the "isValid()" function
  359. /**
  360. * Call for complete validator response
  361. *
  362. * @param string SQL statement to validate
  363. *
  364. * @return string Validator string from Mimer
  365. *
  366. * @see _validate
  367. */
  368. function validationString($sql)
  369. {
  370. $res = $this->_validate($sql);
  371. return $res->data;
  372. } // end of the "validationString()" function
  373. } // end class PMA_SQLValidator
  374. //add an extra check to ensure that the class was defined without errors
  375. if (!class_exists('PMA_SQLValidator')) {
  376. $GLOBALS['sqlvalidator_error'] = TRUE;
  377. }
  378. } // end else
  379. ?>