sqlvalidator.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. {
  47. var $url;
  48. var $service_name;
  49. var $wsdl;
  50. var $output_type;
  51. var $username;
  52. var $password;
  53. var $calling_program;
  54. var $calling_program_version;
  55. var $target_dbms;
  56. var $target_dbms_version;
  57. var $connectionTechnology;
  58. var $connection_technology_version;
  59. var $interactive;
  60. var $service_link = null;
  61. var $session_data = null;
  62. /**
  63. * Private functions - You don't need to mess with these
  64. */
  65. /**
  66. * Service opening
  67. *
  68. * @param string $url URL of Mimer SQL Validator WSDL file
  69. *
  70. * @return object Object to use
  71. *
  72. * @access private
  73. */
  74. function _openService($url)
  75. {
  76. if ($GLOBALS['sqlvalidator_soap'] == 'PHP') {
  77. $obj = new SOAPClient($url);
  78. } else {
  79. $obj = new SOAP_Client($url, true);
  80. }
  81. return $obj;
  82. } // end of the "openService()" function
  83. /**
  84. * Service initializer to connect to server
  85. *
  86. * @param object $obj Service object
  87. * @param string $username Username
  88. * @param string $password Password
  89. * @param string $calling_program Name of calling program
  90. * @param string $calling_program_version Version of calling program
  91. * @param string $target_dbms Target DBMS
  92. * @param string $target_dbms_version Version of target DBMS
  93. * @param string $connection_technology Connection Technology
  94. * @param string $connection_technology_version Con. Technology version
  95. * @param integer $interactive boolean 1/0 to specify if
  96. * we are an interactive system
  97. *
  98. * @return object stdClass return object with data
  99. *
  100. * @access private
  101. */
  102. function _openSession($obj, $username, $password, $calling_program,
  103. $calling_program_version, $target_dbms, $target_dbms_version,
  104. $connection_technology, $connection_technology_version, $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 $obj Service object
  128. * @param object $session Session object
  129. * @param string $sql SQL Query to validate
  130. * @param string $method 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 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(
  165. $this->service_link, $this->session_data, $sql, $this->output_type
  166. );
  167. return $ret;
  168. } // end of the "validate()" function
  169. /**
  170. * Public functions
  171. */
  172. /**
  173. * Constructor
  174. *
  175. * @access public
  176. */
  177. function __construct()
  178. {
  179. $this->url = 'http://sqlvalidator.mimer.com/v1/services';
  180. $this->service_name = 'SQL99Validator';
  181. $this->wsdl = '?wsdl';
  182. $this->output_type = 'html';
  183. $this->username = 'anonymous';
  184. $this->password = '';
  185. $this->calling_program = 'PHP_SQLValidator';
  186. $this->calling_program_version = PMA_VERSION;
  187. $this->target_dbms = 'N/A';
  188. $this->target_dbms_version = 'N/A';
  189. $this->connection_technology = 'PHP';
  190. $this->connection_technology_version = phpversion();
  191. $this->interactive = 1;
  192. $this->service_link = null;
  193. $this->session_data = null;
  194. } // end of the "PMA_SQLValidator()" function
  195. /**
  196. * Sets credentials
  197. *
  198. * @param string $username the username
  199. * @param string $password the password
  200. *
  201. * @return void
  202. * @access public
  203. */
  204. function setCredentials($username, $password)
  205. {
  206. $this->username = $username;
  207. $this->password = $password;
  208. } // end of the "setCredentials()" function
  209. /**
  210. * Sets the calling program
  211. *
  212. * @param string $calling_program the calling program name
  213. * @param string $calling_program_version the calling program revision
  214. *
  215. * @return void
  216. * @access public
  217. */
  218. function setCallingProgram($calling_program, $calling_program_version)
  219. {
  220. $this->calling_program = $calling_program;
  221. $this->calling_program_version = $calling_program_version;
  222. } // end of the "setCallingProgram()" function
  223. /**
  224. * Appends the calling program
  225. *
  226. * @param string $calling_program the calling program name
  227. * @param string $calling_program_version the calling program revision
  228. *
  229. * @return void
  230. * @access public
  231. */
  232. function appendCallingProgram($calling_program, $calling_program_version)
  233. {
  234. $this->calling_program .= ' - ' . $calling_program;
  235. $this->calling_program_version .= ' - ' . $calling_program_version;
  236. } // end of the "appendCallingProgram()" function
  237. /**
  238. * Sets the target DBMS
  239. *
  240. * @param string $target_dbms the target DBMS name
  241. * @param string $target_dbms_version the target DBMS revision
  242. *
  243. * @return void
  244. * @access public
  245. */
  246. function setTargetDbms($target_dbms, $target_dbms_version)
  247. {
  248. $this->target_dbms = $target_dbms;
  249. $this->target_dbms_version = $target_dbms_version;
  250. } // end of the "setTargetDbms()" function
  251. /**
  252. * Appends the target DBMS
  253. *
  254. * @param string $target_dbms the target DBMS name
  255. * @param string $target_dbms_version the target DBMS revision
  256. *
  257. * @return void
  258. * @access public
  259. */
  260. function appendTargetDbms($target_dbms, $target_dbms_version)
  261. {
  262. $this->target_dbms .= ' - ' . $target_dbms;
  263. $this->target_dbms_version .= ' - ' . $target_dbms_version;
  264. } // end of the "appendTargetDbms()" function
  265. /**
  266. * Sets the connection technology used
  267. *
  268. * @param string $connection_technology the con. technology name
  269. * @param string $connection_technology_version the con. technology revision
  270. *
  271. * @return void
  272. * @access public
  273. */
  274. function setConnectionTechnology(
  275. $connection_technology, $connection_technology_version
  276. ) {
  277. $this->connection_technology = $connection_technology;
  278. $this->connection_technology_version = $connection_technology_version;
  279. } // end of the "setConnectionTechnology()" function
  280. /**
  281. * Appends the connection technology used
  282. *
  283. * @param string $connection_technology the con. technology name
  284. * @param string $connection_technology_version the con. technology revision
  285. *
  286. * @return void
  287. * @access public
  288. */
  289. function appendConnectionTechnology(
  290. $connection_technology, $connection_technology_version
  291. ) {
  292. $this->connection_technology .= ' - ' . $connection_technology;
  293. $this->connection_technology_version .= ' - ' . $connection_technology_version;
  294. } // end of the "appendConnectionTechnology()" function
  295. /**
  296. * Sets whether interactive mode should be used or not
  297. *
  298. * @param integer $interactive whether interactive mode should be used or not
  299. *
  300. * @return void
  301. * @access public
  302. */
  303. function setInteractive($interactive)
  304. {
  305. $this->interactive = $interactive;
  306. } // end of the "setInteractive()" function
  307. /**
  308. * Sets the output type to use
  309. *
  310. * @param string $output_type the output type to use
  311. *
  312. * @return void
  313. * @access public
  314. */
  315. function setOutputType($output_type)
  316. {
  317. $this->output_type = $output_type;
  318. } // end of the "setOutputType()" function
  319. /**
  320. * Starts service
  321. *
  322. * @return void
  323. * @access public
  324. */
  325. function startService()
  326. {
  327. $this->service_link = $this->_openService(
  328. $this->url . '/' . $this->service_name . $this->wsdl
  329. );
  330. } // end of the "startService()" function
  331. /**
  332. * Starts session
  333. *
  334. * @return void
  335. * @access public
  336. */
  337. function startSession()
  338. {
  339. $this->session_data = $this->_openSession(
  340. $this->service_link, $this->username, $this->password,
  341. $this->calling_program, $this->calling_program_version,
  342. $this->target_dbms, $this->target_dbms_version,
  343. $this->connection_technology, $this->connection_technology_version,
  344. true // FIXME: Are we to tell them that we are interactive?
  345. );
  346. if (isset($this->session_data)
  347. && ($this->session_data != null)
  348. && ($this->session_data->target != $this->url)
  349. ) {
  350. // Reopens the service on the new URL that was provided
  351. $url = $this->session_data->target;
  352. $this->startService();
  353. }
  354. } // end of the "startSession()" function
  355. /**
  356. * Do start service and session
  357. *
  358. * @return void
  359. * @access public
  360. */
  361. function start()
  362. {
  363. $this->startService();
  364. $this->startSession();
  365. } // end of the "start()" function
  366. /**
  367. * Call to determine just if a query is valid or not.
  368. *
  369. * @param string $sql SQL statement to validate
  370. *
  371. * @return string Validator string from Mimer
  372. *
  373. * @see _validate
  374. */
  375. function isValid($sql)
  376. {
  377. $res = $this->_validate($sql);
  378. return $res->standard;
  379. } // end of the "isValid()" function
  380. /**
  381. * Call for complete validator response
  382. *
  383. * @param string $sql SQL statement to validate
  384. *
  385. * @return string Validator string from Mimer
  386. *
  387. * @see _validate
  388. */
  389. function validationString($sql)
  390. {
  391. $res = $this->_validate($sql);
  392. return $res->data;
  393. } // end of the "validationString()" function
  394. } // end class PMA_SQLValidator
  395. //add an extra check to ensure that the class was defined without errors
  396. if (!class_exists('PMA_SQLValidator')) {
  397. $GLOBALS['sqlvalidator_error'] = true;
  398. }
  399. } // end else
  400. ?>