PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/_mmServerScripts/mysql.php

http://cp3046team16bj.codeplex.com
PHP | 412 lines | 315 code | 64 blank | 33 comment | 51 complexity | 3a6038ae113d8ef398cee9e10812d339 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // If this file is not included from the MMHTTPDB possible hacking problem.
  3. if (!function_exists('create_error')){
  4. die();
  5. }
  6. define('MYSQL_NOT_EXISTS', create_error("Your PHP server doesn't have the MySQL module loaded or you can't use the mysql_(p)connect functions."));
  7. define('CONN_NOT_OPEN_GET_TABLES', create_error('The Connection is not opened when trying to retrieve the tables. Please refer to www.interaktonline.com for more information.'));
  8. define('CONN_NOT_OPEN_GET_DB_LIST', create_error('The Connection is not opened when trying to retrieve the database list. Please refer to www.interaktonline.com for more information.'));
  9. if (!function_exists('mysql_connect') || !function_exists('mysql_pconnect') || !extension_loaded('mysql')){
  10. echo MYSQL_NOT_EXISTS;
  11. die();
  12. }
  13. // Now let's handle the crashes or any other PHP errors that we can catch
  14. function KT_ErrorHandler($errno, $errstr, $errfile, $errline) {
  15. global $f, $already_sent;
  16. $errortype = array (
  17. 1 => "Error",
  18. 2 => "Warning",
  19. 4 => "Parsing Error",
  20. 8 => "Notice",
  21. 16 => "Core Error",
  22. 32 => "Core Warning",
  23. 64 => "Compile Error",
  24. 128 => "Compile Warning",
  25. 256 => "User Error",
  26. 512 => "User Warning",
  27. 1024=> "User Notice",
  28. 2048=> "E_ALL",
  29. 2049=> "PHP5 E_STRICT"
  30. );
  31. $str = sprintf("[%s]\n%s:\t%s\nFile:\t\t'%s'\nLine:\t\t%s\n\n", date('d-m-Y H:i:s'),(isset($errortype[@$errno])?$errortype[@$errno]:('Unknown '.$errno)),@$errstr,@$errfile,@$errline);
  32. if (error_reporting() != 0) {
  33. @fwrite($f, $str);
  34. if (@$errno == 2 && isset($already_sent) && !$already_sent==true){
  35. $error = '<ERRORS>'."\n";
  36. $error .= '<ERROR><DESCRIPTION>An Warning Type error appeared. The error is logged into the log file.</DESCRIPTION></ERROR>'."\n";
  37. $error .= '</ERRORS>'."\n";
  38. $already_sent = true;
  39. echo $error;
  40. }
  41. }
  42. }
  43. if ($debug_to_file){
  44. $old_error_handler = set_error_handler("KT_ErrorHandler");
  45. }
  46. class MySqlConnection
  47. {
  48. /*
  49. // The 'var' keyword is deprecated in PHP5 ... we will define these variables at runtime.
  50. var $isOpen;
  51. var $hostname;
  52. var $database;
  53. var $username;
  54. var $password;
  55. var $timeout;
  56. var $connectionId;
  57. var $error;
  58. */
  59. function MySqlConnection($ConnectionString, $Timeout, $Host, $DB, $UID, $Pwd)
  60. {
  61. $this->isOpen = false;
  62. $this->timeout = $Timeout;
  63. $this->error = '';
  64. if( $Host ) {
  65. $this->hostname = $Host;
  66. }
  67. elseif( ereg("host=([^;]+);", $ConnectionString, $ret) ) {
  68. $this->hostname = $ret[1];
  69. }
  70. if( $DB ) {
  71. $this->database = $DB;
  72. }
  73. elseif( ereg("db=([^;]+);", $ConnectionString, $ret) ) {
  74. $this->database = $ret[1];
  75. }
  76. if( $UID ) {
  77. $this->username = $UID;
  78. }
  79. elseif( ereg("uid=([^;]+);", $ConnectionString, $ret) ) {
  80. $this->username = $ret[1];
  81. }
  82. if( $Pwd ) {
  83. $this->password = $Pwd;
  84. }
  85. elseif( ereg("pwd=([^;]+);", $ConnectionString, $ret) ) {
  86. $this->password = $ret[1];
  87. }
  88. }
  89. function Open()
  90. {
  91. $this->connectionId = mysql_connect($this->hostname, $this->username, $this->password);
  92. if (isset($this->connectionId) && $this->connectionId && is_resource($this->connectionId))
  93. {
  94. $this->isOpen = ($this->database == "") ? true : mysql_select_db($this->database, $this->connectionId);
  95. }
  96. else
  97. {
  98. $this->isOpen = false;
  99. }
  100. }
  101. function TestOpen()
  102. {
  103. return ($this->isOpen) ? '<TEST status=true></TEST>' : $this->HandleException();
  104. }
  105. function Close()
  106. {
  107. if (is_resource($this->connectionId) && $this->isOpen)
  108. {
  109. if (mysql_close($this->connectionId))
  110. {
  111. $this->isOpen = false;
  112. unset($this->connectionId);
  113. }
  114. }
  115. }
  116. function GetTables($table_name = '')
  117. {
  118. $xmlOutput = "";
  119. if ($this->isOpen && isset($this->connectionId) && is_resource($this->connectionId)){
  120. // 1. mysql_list_tables and mysql_tablename are deprecated in PHP5
  121. // 2. For backward compatibility GetTables don't have any parameters
  122. if ($table_name === ''){
  123. $table_name = @$_POST['Database'];
  124. }
  125. $sql = ' SHOW TABLES FROM ' . $table_name;
  126. $results = mysql_query($sql, $this->connectionId) or $this->HandleException();
  127. $xmlOutput = "<RESULTSET><FIELDS>";
  128. // Columns are referenced by index, so Schema and
  129. // Catalog must be specified even though they are not supported
  130. $xmlOutput .= '<FIELD><NAME>TABLE_CATALOG</NAME></FIELD>'; // column 0 (zero-based)
  131. $xmlOutput .= '<FIELD><NAME>TABLE_SCHEMA</NAME></FIELD>'; // column 1
  132. $xmlOutput .= '<FIELD><NAME>TABLE_NAME</NAME></FIELD>'; // column 2
  133. $xmlOutput .= "</FIELDS><ROWS>";
  134. if (is_resource($results) && mysql_num_rows($results) > 0){
  135. while ($row = mysql_fetch_array($results)){
  136. $xmlOutput .= '<ROW><VALUE/><VALUE/><VALUE>' . $row[0]. '</VALUE></ROW>';
  137. }
  138. }
  139. $xmlOutput .= "</ROWS></RESULTSET>";
  140. }
  141. return $xmlOutput;
  142. }
  143. function GetViews()
  144. {
  145. // not supported
  146. return "<RESULTSET><FIELDS></FIELDS><ROWS></ROWS></RESULTSET>";
  147. }
  148. function GetProcedures()
  149. {
  150. // not supported
  151. return "<RESULTSET><FIELDS></FIELDS><ROWS></ROWS></RESULTSET>";
  152. }
  153. function GetColumnsOfTable($TableName)
  154. {
  155. $xmlOutput = "";
  156. $query = "DESCRIBE $TableName";
  157. $result = mysql_query($query) or $this->HandleException();
  158. if ($result)
  159. {
  160. $xmlOutput = "<RESULTSET><FIELDS>";
  161. // Columns are referenced by index, so Schema and
  162. // Catalog must be specified even though they are not supported
  163. $xmlOutput .= "<FIELD><NAME>TABLE_CATALOG</NAME></FIELD>"; // column 0 (zero-based)
  164. $xmlOutput .= "<FIELD><NAME>TABLE_SCHEMA</NAME></FIELD>"; // column 1
  165. $xmlOutput .= "<FIELD><NAME>TABLE_NAME</NAME></FIELD>"; // column 2
  166. $xmlOutput .= "<FIELD><NAME>COLUMN_NAME</NAME></FIELD>";
  167. $xmlOutput .= "<FIELD><NAME>DATA_TYPE</NAME></FIELD>";
  168. $xmlOutput .= "<FIELD><NAME>IS_NULLABLE</NAME></FIELD>";
  169. $xmlOutput .= "<FIELD><NAME>COLUMN_SIZE</NAME></FIELD>";
  170. $xmlOutput .= "</FIELDS><ROWS>";
  171. // The fields returned from DESCRIBE are: Field, Type, Null, Key, Default, Extra
  172. while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
  173. {
  174. $xmlOutput .= "<ROW><VALUE/><VALUE/><VALUE/>";
  175. // Separate type from size. Format is: type(size)
  176. if (ereg("(.*)\\((.*)\\)", $row["Type"], $ret))
  177. {
  178. $type = $ret[1];
  179. $size = $ret[2];
  180. }
  181. else
  182. {
  183. $type = $row["Type"];
  184. $size = "";
  185. }
  186. // MySQL sets nullable to "YES" or "", so we need to set "NO"
  187. $null = $row["Null"];
  188. if ($null == "")
  189. $null = "NO";
  190. $xmlOutput .= "<VALUE>" . $row["Field"] . "</VALUE>";
  191. $xmlOutput .= "<VALUE>" . $type . "</VALUE>";
  192. $xmlOutput .= "<VALUE>" . $null . "</VALUE>";
  193. $xmlOutput .= "<VALUE>" . $size . "</VALUE></ROW>";
  194. }
  195. mysql_free_result($result);
  196. $xmlOutput .= "</ROWS></RESULTSET>";
  197. }
  198. return $xmlOutput;
  199. }
  200. function GetParametersOfProcedure($ProcedureName, $SchemaName, $CatalogName)
  201. {
  202. // not supported on MySQL
  203. return '<RESULTSET><FIELDS></FIELDS><ROWS></ROWS></RESULTSET>';
  204. }
  205. function ExecuteSQL($aStatement, $MaxRows)
  206. {
  207. if ( get_magic_quotes_gpc() )
  208. {
  209. $aStatement = stripslashes( $aStatement ) ;
  210. }
  211. $xmlOutput = "";
  212. $result = mysql_query($aStatement) or $this->HandleException();
  213. if (isset($result) && is_resource($result))
  214. {
  215. $xmlOutput = "<RESULTSET><FIELDS>";
  216. $fieldCount = mysql_num_fields($result);
  217. for ($i=0; $i < $fieldCount; $i++)
  218. {
  219. $meta = mysql_fetch_field($result);
  220. if ($meta)
  221. {
  222. $xmlOutput .= '<FIELD';
  223. $xmlOutput .= ' type="' . $meta->type;
  224. $xmlOutput .= '" max_length="' . $meta->max_length;
  225. $xmlOutput .= '" table="' . $meta->table;
  226. $xmlOutput .= '" not_null="' . $meta->not_null;
  227. $xmlOutput .= '" numeric="' . $meta->numeric;
  228. $xmlOutput .= '" unsigned="' . $meta->unsigned;
  229. $xmlOutput .= '" zerofill="' . $meta->zerofill;
  230. $xmlOutput .= '" primary_key="' . $meta->primary_key;
  231. $xmlOutput .= '" multiple_key="'. $meta->multiple_key;
  232. $xmlOutput .= '" unique_key="' . $meta->unique_key;
  233. $xmlOutput .= '"><NAME>' . $meta->name;
  234. $xmlOutput .= '</NAME></FIELD>';
  235. }
  236. }
  237. $xmlOutput .= "</FIELDS><ROWS>";
  238. $row = mysql_fetch_assoc($result);
  239. for ($i=0; $row && ($i < $MaxRows); $i++)
  240. {
  241. $xmlOutput .= "<ROW>";
  242. foreach ($row as $key => $value)
  243. {
  244. $xmlOutput .= "<VALUE>";
  245. $xmlOutput .= htmlspecialchars($value);
  246. $xmlOutput .= "</VALUE>";
  247. }
  248. $xmlOutput .= "</ROW>";
  249. $row = mysql_fetch_assoc($result);
  250. }
  251. mysql_free_result($result);
  252. $xmlOutput .= "</ROWS></RESULTSET>";
  253. }
  254. return $xmlOutput;
  255. }
  256. function GetProviderTypes()
  257. {
  258. return '<RESULTSET><FIELDS></FIELDS><ROWS></ROWS></RESULTSET>';
  259. }
  260. function ExecuteSP($aProcStatement, $TimeOut, $Parameters)
  261. {
  262. return '<RESULTSET><FIELDS></FIELDS><ROWS></ROWS></RESULTSET>';
  263. }
  264. function ReturnsResultSet($ProcedureName)
  265. {
  266. return '<RETURNSRESULTSET status=false></RETURNSRESULTSET>';
  267. }
  268. function SupportsProcedure()
  269. {
  270. return '<SUPPORTSPROCEDURE status=false></SUPPORTSPROCEDURE>';
  271. }
  272. /*
  273. * HandleException added by InterAKT for ease in database translation answer
  274. */
  275. function HandleException()
  276. {
  277. global $debug_to_file, $f;
  278. $this->error = create_error(' MySQL Error#: '. ((int)mysql_errno()) . "\n\n".mysql_error());
  279. log_messages($this->error);
  280. die($this->error.'</HTML>');
  281. }
  282. function GetDatabaseList()
  283. {
  284. $xmlOutput = '<RESULTSET><FIELDS><FIELD><NAME>NAME</NAME></FIELD></FIELDS><ROWS>';
  285. if (isset($this->connectionId) && is_resource($this->connectionId)){
  286. $dbList = mysql_list_dbs($this->connectionId);
  287. while ($row = mysql_fetch_object($dbList))
  288. {
  289. $xmlOutput .= '<ROW><VALUE>' . $row->Database . '</VALUE></ROW>';
  290. }
  291. }else{
  292. $this->error = CONN_NOT_OPEN_GET_DB_LIST;
  293. return $this->error;
  294. }
  295. $xmlOutput .= '</ROWS></RESULTSET>';
  296. return $xmlOutput;
  297. }
  298. function GetPrimaryKeysOfTable($TableName)
  299. {
  300. $xmlOutput = '';
  301. $query = "DESCRIBE $TableName";
  302. $result = mysql_query($query) or $this->HandleException();
  303. if ($result)
  304. {
  305. $xmlOutput = '<RESULTSET><FIELDS>';
  306. // Columns are referenced by index, so Schema and
  307. // Catalog must be specified even though they are not supported
  308. $xmlOutput .= '<FIELD><NAME>TABLE_CATALOG</NAME></FIELD>'; // column 0 (zero-based)
  309. $xmlOutput .= '<FIELD><NAME>TABLE_SCHEMA</NAME></FIELD>'; // column 1
  310. $xmlOutput .= '<FIELD><NAME>TABLE_NAME</NAME></FIELD>'; // column 2
  311. $xmlOutput .= '<FIELD><NAME>COLUMN_NAME</NAME></FIELD>';
  312. $xmlOutput .= '<FIELD><NAME>DATA_TYPE</NAME></FIELD>';
  313. $xmlOutput .= '<FIELD><NAME>IS_NULLABLE</NAME></FIELD>';
  314. $xmlOutput .= '<FIELD><NAME>COLUMN_SIZE</NAME></FIELD>';
  315. $xmlOutput .= '</FIELDS><ROWS>';
  316. // The fields returned from DESCRIBE are: Field, Type, Null, Key, Default, Extra
  317. while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
  318. {
  319. if (strtoupper($row['Key']) == 'PRI'){
  320. $xmlOutput .= '<ROW><VALUE/><VALUE/><VALUE/>';
  321. // Separate type from size. Format is: type(size)
  322. if (ereg("(.*)\\((.*)\\)", $row['Type'], $ret))
  323. {
  324. $type = $ret[1];
  325. $size = $ret[2];
  326. }
  327. else
  328. {
  329. $type = $row['Type'];
  330. $size = '';
  331. }
  332. // MySQL sets nullable to "YES" or "", so we need to set "NO"
  333. $null = $row['Null'];
  334. if ($null == '')
  335. $null = 'NO';
  336. $xmlOutput .= '<VALUE>' . $row['Field'] . '</VALUE>';
  337. $xmlOutput .= '<VALUE>' . $type . '</VALUE>';
  338. $xmlOutput .= '<VALUE>' . $null . '</VALUE>';
  339. $xmlOutput .= '<VALUE>' . $size . '</VALUE></ROW>';
  340. }
  341. }
  342. mysql_free_result($result);
  343. $xmlOutput .= '</ROWS></RESULTSET>';
  344. }
  345. return $xmlOutput;
  346. }
  347. } // class MySqlConnection
  348. ?>