PageRenderTime 1477ms CodeModel.GetById 26ms RepoModel.GetById 3ms app.codeStats 0ms

/inc/scripts/emsproxy.php

https://github.com/EmranAhmed/wp-easycart
PHP | 479 lines | 225 code | 254 blank | 0 comment | 91 complexity | d83ed993020fc38d3dab0284493ab5fd MD5 | raw file
  1. <?
  2. header('Content-Type: application/octet-stream');
  3. error_reporting(125);
  4. $data = 'OK:';
  5. function err( $error )
  6. {
  7. die("err: $error");
  8. }
  9. function myerr()
  10. {
  11. $e = mysql_error();
  12. mysql_free_result( mysql_query( 'ROLLBACK' ) );
  13. err( $e );
  14. }
  15. function pgerr( $err )
  16. {
  17. $e = pg_last_error();
  18. if( $e == '' ) $e = $err;
  19. if( $e == '' ) $e = 'unknown pgsql error';
  20. pg_free_result( pg_query( 'ROLLBACK' ) );
  21. err( $e );
  22. }
  23. function dump( $val )
  24. {
  25. global $data;
  26. $len = strlen($val);
  27. if( $len < 255 )
  28. $data .= chr($len);
  29. else
  30. $data .= "\xFF".pack('V',$len);
  31. $data .= $val;
  32. if( strlen($data) > 100000 ) {
  33. echo $data;
  34. $data = '';
  35. }
  36. }
  37. array_key_exists('server',$_POST) and array_key_exists('host',$_POST) and array_key_exists('port',$_POST) and array_key_exists('user',$_POST) and array_key_exists('password',$_POST) and array_key_exists('dbname',$_POST) or err("mailformed request");
  38. if( get_magic_quotes_gpc() )
  39. foreach( $_POST as $key => $value )
  40. $_POST[$key] = stripslashes($value);
  41. $commit = array_key_exists('commit',$_POST);
  42. if( $_POST['server'] == 'mysql' ) {
  43. $host = $_POST['host'];
  44. if( $_POST['port'] )
  45. $host .= ':'.$_POST['port'];
  46. $conn = mysql_connect($host,$_POST['user'],$_POST['password']) or myerr();
  47. if( $_POST['dbname'] != '' )
  48. mysql_select_db( $_POST['dbname'] ) or myerr();
  49. if( array_key_exists('charset',$_POST) && $_POST['charset'] != '' )
  50. mysql_query( 'SET NAMES \'' . $_POST['charset'] . '\'' ) or myerr();
  51. $result = FALSE;
  52. mysql_free_result( mysql_query( 'BEGIN' ) );
  53. for( $rn = 1; $rn < 1000; ++$rn ) {
  54. if( !array_key_exists( 'r'.$rn, $_POST ) )
  55. break;
  56. $data = 'OK:';
  57. $req = $_POST['r'.$rn];
  58. if( $req == 'connect' ) {
  59. dump( mysql_get_server_info() );
  60. dump( mysql_get_client_info() );
  61. dump( mysql_get_proto_info() );
  62. dump( mysql_get_host_info() );
  63. } else {
  64. $result = mysql_query($req) or myerr();
  65. if( $result === TRUE ) {
  66. dump( 0 );
  67. dump( mysql_affected_rows() );
  68. } else {
  69. $width = mysql_num_fields($result);
  70. $height = mysql_num_rows($result);
  71. dump($width);
  72. dump($height);
  73. for( $i = 0; $i < $width; ++$i ) {
  74. dump( mysql_field_name( $result, $i ) );
  75. $type = mysql_field_type( $result, $i );
  76. $len = mysql_field_len( $result, $i );
  77. $meta = mysql_fetch_field( $result, $i );
  78. $sflags = explode( ' ', mysql_field_flags ( $result, $i ) );
  79. $fl = 0;
  80. if( $meta->not_null ) $fl += 1;
  81. if( $meta->primary_key ) $fl += 2;
  82. if( $meta->unique_key ) $fl += 4;
  83. if( $meta->multiple_key ) $fl += 8;
  84. if( $meta->blob ) $fl += 16;
  85. if( $meta->unsigned ) $fl += 32;
  86. if( $meta->zerofill ) $fl += 64;
  87. if( in_array( 'binary', $sflags ) ) $fl += 128;
  88. if( in_array( 'enum', $sflags ) ) $fl += 256;
  89. if( in_array( 'auto_increment', $sflags ) ) $fl += 512;
  90. if( in_array( 'timestamp', $sflags ) ) $fl += 1024;
  91. if( in_array( 'set', $sflags ) ) $fl += 2048;
  92. if( $type == 'int' ) {
  93. if( $len > 11 ) $type = 8; # LONGLONG
  94. elseif( $len > 9 ) $type = 3; # LONG
  95. elseif( $len > 6 ) $type = 9; # INT24
  96. elseif( $len > 4 ) $type = 2; # SHORT
  97. else $type = 1; # TINY
  98. } elseif( $type == 'real' ) {
  99. if( $len == 12 ) $type = 4; # FLOAT
  100. elseif( $len == 22 ) $type = 5; # DOUBLE
  101. else $type = 0; # DECIMAL
  102. } elseif( $type == 'null' ) $type = 6; # NULL
  103. elseif( $type == 'timestamp' ) $type = 7; # TIMESTAMP
  104. elseif( $type == 'date' ) $type = 10; # DATE
  105. elseif( $type == 'time' ) $type = 11; # TIME
  106. elseif( $type == 'datetime' ) $type = 12; # DATETIME
  107. elseif( $type == 'year' ) $type = 13; # YEAR
  108. elseif( $type == 'blob' ) {
  109. if( $len > 65536 ) $type = 251; # LONG BLOB
  110. elseif( $len > 255 ) $type = 252; # BLOB
  111. else $type = 249; # TINY BLOB
  112. } elseif( $type == 'string' ) $type = 253; # VARCHAR
  113. else
  114. $type = 252;
  115. dump( $type );
  116. dump( $fl );
  117. dump( $len );
  118. }
  119. for( $i = 0; $i < $height; ++$i ) {
  120. $row = mysql_fetch_row( $result );
  121. for( $j = 0; $j < $width; ++$j )
  122. if( is_null($row[$j]) )
  123. dump( '' );
  124. else
  125. dump( ' '.$row[$j] );
  126. }
  127. mysql_free_result( $result );
  128. }
  129. }
  130. }
  131. mysql_free_result( mysql_query( $commit ? 'COMMIT' : 'ROLLBACK' ) );
  132. } elseif( $_POST['server'] == 'pgsql' ) {
  133. $conn = '';
  134. if( $_POST['host'] != '' ) $conn .= "host=$_POST[host]";
  135. if( $_POST['port'] != '' ) $conn .= " port=$_POST[port]";
  136. if( $_POST['dbname'] != '' ) $conn .= " dbname=$_POST[dbname]";
  137. if( $_POST['user'] != '' ) $conn .= " user=$_POST[user]";
  138. if( $_POST['password'] != '' ) $conn .= " password=$_POST[password]";
  139. $conn = pg_connect( $conn ) || pgerr('some connection error');
  140. if( !$commit || array_key_exists( 'r2', $_POST ) )
  141. pg_free_result( pg_query( 'BEGIN' ) );
  142. $result = FALSE;
  143. for( $rn = 1; $rn < 1000; ++$rn ) {
  144. if( !array_key_exists( 'r'.$rn, $_POST ) )
  145. break;
  146. $data = 'OK:';
  147. $req = $_POST['r'.$rn];
  148. if( $req == 'connect' ) {
  149. dump( 0 );
  150. dump( 0 );
  151. dump( 0 );
  152. } elseif( substr($req,0,11) == 'blob_create' ) {
  153. list($oid) = sscanf( $req, 'blob_create %u' );
  154. pg_free_result( pg_query( $commit ? 'COMMIT' : 'ROLLBACK' ) );
  155. pg_free_result( pg_query( 'BEGIN' ) );
  156. $oid = pg_lo_create() or pgerr('lo_create failed');
  157. pg_free_result( pg_query( 'COMMIT' ) );
  158. pg_free_result( pg_query( 'BEGIN' ) );
  159. dump($oid);
  160. } elseif( substr($req,0,11) == 'blob_delete' ) {
  161. list($oid) = sscanf( $req, 'blob_delete %u' );
  162. $oid = pg_lo_unlink($oid) or pgerr('lo_unlink failed');
  163. } elseif( substr($req,0,10) == 'blob_write' ) {
  164. list($oid) = sscanf( $req, 'blob_write %s ' );
  165. $bin = substr($req,12+strlen($oid));
  166. $obj = pg_lo_open($oid,'w') or pgerr( 'lo_open failed' );
  167. $res = pg_lo_write($obj,$bin) or pgerr( 'lo_write failed' );
  168. pg_lo_close($obj);
  169. dump($res);
  170. } elseif( substr($req,0,9) == 'blob_read' ) {
  171. list($oid) = sscanf( $req, 'blob_read %u' );
  172. $obj = pg_lo_open($oid,'r') or pgerr( 'lo_open failed' );
  173. pg_lo_seek($obj,0,PGSQL_SEEK_END);
  174. $len = pg_lo_tell($obj);
  175. pg_lo_seek($obj,0,PGSQL_SEEK_SET);
  176. $res = pg_lo_read($obj,$len) or pgerr( 'lo_read failed' );
  177. pg_lo_close($obj);
  178. dump($res);
  179. } else {
  180. $result = pg_query($req) or pgerr("error at request: $req");
  181. if( pg_result_status($result) == PGSQL_COMMAND_OK ) {
  182. dump( 0 );
  183. dump( pg_affected_rows($result) );
  184. dump( pg_last_oid($result) );
  185. pg_free_result($result);
  186. } elseif( pg_result_status($result) == PGSQL_EMPTY_QUERY ) {
  187. dump( 0 );
  188. dump( 0 );
  189. pg_free_result($result);
  190. } elseif( pg_result_status($result) == PGSQL_TUPLES_OK ) {
  191. $width = pg_num_fields($result);
  192. $height = pg_num_rows($result);
  193. dump($width);
  194. dump($height);
  195. for( $i = 0; $i < $width; ++$i ) {
  196. $type = pg_field_type( $result, $i );
  197. dump( pg_field_name( $result, $i ) );
  198. dump( $type );
  199. dump( pg_field_size( $result, $i ) );
  200. }
  201. for( $i = 0; $i < $height; ++$i ) {
  202. $row = pg_fetch_row( $result );
  203. for( $j = 0; $j < $width; ++$j )
  204. if( is_null($row[$j]) )
  205. dump( '' );
  206. else
  207. dump( ' '.$row[$j] );
  208. }
  209. pg_free_result( $result );
  210. } else {
  211. $e = pg_result_error($result);
  212. pg_free_result($result);
  213. err( $e );
  214. }
  215. }
  216. }
  217. pg_free_result( pg_query( $commit ? 'COMMIT' : 'ROLLBACK' ) );
  218. } else {
  219. err("server type '$_POST[server] is not supported");
  220. }
  221. if( $data != '' ) echo $data;
  222. ?>