PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/_mmServerScripts/mysql.php

https://bitbucket.org/jojoluzifer/honey-project
PHP | 438 lines | 330 code | 66 blank | 42 comment | 58 complexity | cacc9e267f1942d1c5283a6cca9f07c6 MD5 | raw file
  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 could not be opened when trying to retrieve the tables.'));
  8. define('CONN_NOT_OPEN_GET_DB_LIST', create_error('The Connection could not be opened when trying to retrieve the database list.'));
  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( preg_match("/host=([^;]+);/", $ConnectionString, $ret) ) {
  68. $this->hostname = $ret[1];
  69. }
  70. if( $DB ) {
  71. $this->database = $DB;
  72. }
  73. elseif( preg_match("/db=([^;]+);/", $ConnectionString, $ret) ) {
  74. $this->database = $ret[1];
  75. }
  76. if( $UID ) {
  77. $this->username = $UID;
  78. }
  79. elseif( preg_match("/uid=([^;]+);/", $ConnectionString, $ret) ) {
  80. $this->username = $ret[1];
  81. }
  82. if( $Pwd ) {
  83. $this->password = $Pwd;
  84. }
  85. elseif( preg_match("/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. //added backtick for handling reserved words and special characters
  126. //http://dev.mysql.com/doc/refman/5.0/en/legal-names.html
  127. $sql = ' SHOW TABLES FROM ' . $this->ensureTicks($table_name) ;
  128. $results = mysql_query($sql, $this->connectionId) or $this->HandleException();
  129. $xmlOutput = "<RESULTSET><FIELDS>";
  130. // Columns are referenced by index, so Schema and
  131. // Catalog must be specified even though they are not supported
  132. $xmlOutput .= '<FIELD><NAME>TABLE_CATALOG</NAME></FIELD>'; // column 0 (zero-based)
  133. $xmlOutput .= '<FIELD><NAME>TABLE_SCHEMA</NAME></FIELD>'; // column 1
  134. $xmlOutput .= '<FIELD><NAME>TABLE_NAME</NAME></FIELD>'; // column 2
  135. $xmlOutput .= "</FIELDS><ROWS>";
  136. if (is_resource($results) && mysql_num_rows($results) > 0){
  137. while ($row = mysql_fetch_array($results)){
  138. $xmlOutput .= '<ROW><VALUE/><VALUE/><VALUE>' . $row[0]. '</VALUE></ROW>';
  139. }
  140. }
  141. $xmlOutput .= "</ROWS></RESULTSET>";
  142. }
  143. return $xmlOutput;
  144. }
  145. function GetViews()
  146. {
  147. // not supported
  148. return "<RESULTSET><FIELDS></FIELDS><ROWS></ROWS></RESULTSET>";
  149. }
  150. function GetProcedures()
  151. {
  152. // not supported
  153. return "<RESULTSET><FIELDS></FIELDS><ROWS></ROWS></RESULTSET>";
  154. }
  155. function GetColumnsOfTable($TableName)
  156. {
  157. $xmlOutput = "";
  158. //added backtick for handling reserved words and special characters
  159. //http://dev.mysql.com/doc/refman/5.0/en/legal-names.html
  160. $query = "DESCRIBE ".$this->ensureTicks($TableName);
  161. $result = mysql_query($query) or $this->HandleException();
  162. if ($result)
  163. {
  164. $xmlOutput = "<RESULTSET><FIELDS>";
  165. // Columns are referenced by index, so Schema and
  166. // Catalog must be specified even though they are not supported
  167. $xmlOutput .= "<FIELD><NAME>TABLE_CATALOG</NAME></FIELD>"; // column 0 (zero-based)
  168. $xmlOutput .= "<FIELD><NAME>TABLE_SCHEMA</NAME></FIELD>"; // column 1
  169. $xmlOutput .= "<FIELD><NAME>TABLE_NAME</NAME></FIELD>"; // column 2
  170. $xmlOutput .= "<FIELD><NAME>COLUMN_NAME</NAME></FIELD>";
  171. $xmlOutput .= "<FIELD><NAME>DATA_TYPE</NAME></FIELD>";
  172. $xmlOutput .= "<FIELD><NAME>IS_NULLABLE</NAME></FIELD>";
  173. $xmlOutput .= "<FIELD><NAME>COLUMN_SIZE</NAME></FIELD>";
  174. $xmlOutput .= "</FIELDS><ROWS>";
  175. // The fields returned from DESCRIBE are: Field, Type, Null, Key, Default, Extra
  176. while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
  177. {
  178. $xmlOutput .= "<ROW><VALUE/><VALUE/><VALUE/>";
  179. // Separate type from size. Format is: type(size)
  180. if (preg_match("/(.*)\((.*)\)/", $row["Type"], $ret))
  181. {
  182. $type = $ret[1];
  183. $size = $ret[2];
  184. }
  185. else
  186. {
  187. $type = $row["Type"];
  188. $size = "";
  189. }
  190. // MySQL sets nullable to "YES" or "", so we need to set "NO"
  191. $null = $row["Null"];
  192. if ($null == "")
  193. $null = "NO";
  194. $xmlOutput .= "<VALUE>" . $row["Field"] . "</VALUE>";
  195. $xmlOutput .= "<VALUE>" . $type . "</VALUE>";
  196. $xmlOutput .= "<VALUE>" . $null . "</VALUE>";
  197. $xmlOutput .= "<VALUE>" . $size . "</VALUE></ROW>";
  198. }
  199. mysql_free_result($result);
  200. $xmlOutput .= "</ROWS></RESULTSET>";
  201. }
  202. return $xmlOutput;
  203. }
  204. function GetParametersOfProcedure($ProcedureName, $SchemaName, $CatalogName)
  205. {
  206. // not supported on MySQL
  207. return '<RESULTSET><FIELDS></FIELDS><ROWS></ROWS></RESULTSET>';
  208. }
  209. function ExecuteSQL($aStatement, $MaxRows)
  210. {
  211. if ( get_magic_quotes_gpc() )
  212. {
  213. $aStatement = stripslashes( $aStatement ) ;
  214. }
  215. $xmlOutput = "";
  216. $result = mysql_query($aStatement) or $this->HandleException();
  217. if (isset($result) && is_resource($result))
  218. {
  219. $xmlOutput = "<RESULTSET><FIELDS>";
  220. $fieldCount = mysql_num_fields($result);
  221. for ($i=0; $i < $fieldCount; $i++)
  222. {
  223. $meta = mysql_fetch_field($result);
  224. if ($meta)
  225. {
  226. $xmlOutput .= '<FIELD';
  227. $xmlOutput .= ' type="' . $meta->type;
  228. $xmlOutput .= '" max_length="' . $meta->max_length;
  229. $xmlOutput .= '" table="' . $meta->table;
  230. $xmlOutput .= '" not_null="' . $meta->not_null;
  231. $xmlOutput .= '" numeric="' . $meta->numeric;
  232. $xmlOutput .= '" unsigned="' . $meta->unsigned;
  233. $xmlOutput .= '" zerofill="' . $meta->zerofill;
  234. $xmlOutput .= '" primary_key="' . $meta->primary_key;
  235. $xmlOutput .= '" multiple_key="'. $meta->multiple_key;
  236. $xmlOutput .= '" unique_key="' . $meta->unique_key;
  237. $xmlOutput .= '"><NAME>' . $meta->name;
  238. $xmlOutput .= '</NAME></FIELD>';
  239. }
  240. }
  241. $xmlOutput .= "</FIELDS><ROWS>";
  242. $row = mysql_fetch_assoc($result);
  243. for ($i=0; $row && ($i < $MaxRows); $i++)
  244. {
  245. $xmlOutput .= "<ROW>";
  246. foreach ($row as $key => $value)
  247. {
  248. $xmlOutput .= "<VALUE>";
  249. $xmlOutput .= htmlspecialchars($value);
  250. $xmlOutput .= "</VALUE>";
  251. }
  252. $xmlOutput .= "</ROW>";
  253. $row = mysql_fetch_assoc($result);
  254. }
  255. mysql_free_result($result);
  256. $xmlOutput .= "</ROWS></RESULTSET>";
  257. }
  258. return $xmlOutput;
  259. }
  260. function GetProviderTypes()
  261. {
  262. return '<RESULTSET><FIELDS></FIELDS><ROWS></ROWS></RESULTSET>';
  263. }
  264. function ExecuteSP($aProcStatement, $TimeOut, $Parameters)
  265. {
  266. return '<RESULTSET><FIELDS></FIELDS><ROWS></ROWS></RESULTSET>';
  267. }
  268. function ReturnsResultSet($ProcedureName)
  269. {
  270. return '<RETURNSRESULTSET status=false></RETURNSRESULTSET>';
  271. }
  272. function SupportsProcedure()
  273. {
  274. return '<SUPPORTSPROCEDURE status=false></SUPPORTSPROCEDURE>';
  275. }
  276. /*
  277. * HandleException added by InterAKT for ease in database translation answer
  278. */
  279. function HandleException()
  280. {
  281. global $debug_to_file, $f;
  282. $this->error = create_error(' MySQL Error#: '. ((int)mysql_errno()) . "\n\n".mysql_error());
  283. log_messages($this->error);
  284. die($this->error.'</HTML>');
  285. }
  286. function ensureTicks($inputSQL)
  287. {
  288. $outSQL = $inputSQL;
  289. //added backtick for handling reserved words and special characters
  290. //http://dev.mysql.com/doc/refman/5.0/en/legal-names.html
  291. //only add ticks if not already there
  292. $oLength = strlen($outSQL);
  293. $bHasTick = false;
  294. if (($oLength > 0) && (($outSQL[0] == "`") && ($outSQL[$oLength-1] == "`")))
  295. {
  296. $bHasTick = true;
  297. }
  298. if ($bHasTick == false)
  299. {
  300. $outSQL = "`".$outSQL."`";
  301. }
  302. return $outSQL;
  303. }
  304. function GetDatabaseList()
  305. {
  306. $xmlOutput = '<RESULTSET><FIELDS><FIELD><NAME>NAME</NAME></FIELD></FIELDS><ROWS>';
  307. if (isset($this->connectionId) && is_resource($this->connectionId)){
  308. $dbList = mysql_list_dbs($this->connectionId);
  309. while ($row = mysql_fetch_object($dbList))
  310. {
  311. $xmlOutput .= '<ROW><VALUE>' . $row->Database . '</VALUE></ROW>';
  312. }
  313. }else{
  314. $this->error = CONN_NOT_OPEN_GET_DB_LIST;
  315. return $this->error;
  316. }
  317. $xmlOutput .= '</ROWS></RESULTSET>';
  318. return $xmlOutput;
  319. }
  320. function GetPrimaryKeysOfTable($TableName)
  321. {
  322. $xmlOutput = '';
  323. //added backtick for handling reserved words and special characters
  324. //http://dev.mysql.com/doc/refman/5.0/en/legal-names.html
  325. $query = "DESCRIBE ".$this->ensureTicks($TableName);
  326. $result = mysql_query($query) or $this->HandleException();
  327. if ($result)
  328. {
  329. $xmlOutput = '<RESULTSET><FIELDS>';
  330. // Columns are referenced by index, so Schema and
  331. // Catalog must be specified even though they are not supported
  332. $xmlOutput .= '<FIELD><NAME>TABLE_CATALOG</NAME></FIELD>'; // column 0 (zero-based)
  333. $xmlOutput .= '<FIELD><NAME>TABLE_SCHEMA</NAME></FIELD>'; // column 1
  334. $xmlOutput .= '<FIELD><NAME>TABLE_NAME</NAME></FIELD>'; // column 2
  335. $xmlOutput .= '<FIELD><NAME>COLUMN_NAME</NAME></FIELD>';
  336. $xmlOutput .= '<FIELD><NAME>DATA_TYPE</NAME></FIELD>';
  337. $xmlOutput .= '<FIELD><NAME>IS_NULLABLE</NAME></FIELD>';
  338. $xmlOutput .= '<FIELD><NAME>COLUMN_SIZE</NAME></FIELD>';
  339. $xmlOutput .= '</FIELDS><ROWS>';
  340. // The fields returned from DESCRIBE are: Field, Type, Null, Key, Default, Extra
  341. while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
  342. {
  343. if (strtoupper($row['Key']) == 'PRI'){
  344. $xmlOutput .= '<ROW><VALUE/><VALUE/><VALUE/>';
  345. // Separate type from size. Format is: type(size)
  346. if (preg_match("/(.*)\((.*)\)/", $row['Type'], $ret))
  347. {
  348. $type = $ret[1];
  349. $size = $ret[2];
  350. }
  351. else
  352. {
  353. $type = $row['Type'];
  354. $size = '';
  355. }
  356. // MySQL sets nullable to "YES" or "", so we need to set "NO"
  357. $null = $row['Null'];
  358. if ($null == '')
  359. $null = 'NO';
  360. $xmlOutput .= '<VALUE>' . $row['Field'] . '</VALUE>';
  361. $xmlOutput .= '<VALUE>' . $type . '</VALUE>';
  362. $xmlOutput .= '<VALUE>' . $null . '</VALUE>';
  363. $xmlOutput .= '<VALUE>' . $size . '</VALUE></ROW>';
  364. }
  365. }
  366. mysql_free_result($result);
  367. $xmlOutput .= '</ROWS></RESULTSET>';
  368. }
  369. return $xmlOutput;
  370. }
  371. } // class MySqlConnection
  372. ?>