PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/db/tbl_replace.php

https://gitlab.com/a.loskutnikov/sitimobile
PHP | 270 lines | 211 code | 23 blank | 36 comment | 79 complexity | 2d765f73d801285fcf48b26e1832c5cb MD5 | raw file
  1. <?php
  2. /* $Id: tbl_replace.php,v 1.51.2.2 2002/06/08 18:23:54 rabus Exp $ */
  3. /**
  4. * Gets some core libraries
  5. */
  6. require('./libraries/grab_globals.lib.php');
  7. require('./libraries/common.lib.php');
  8. /**
  9. * Initializes some variables
  10. */
  11. // Defines the url to return in case of success of the query
  12. if (isset($sql_query)) {
  13. $sql_query = urldecode($sql_query);
  14. }
  15. $is_gotofile = FALSE;
  16. if (isset($after_insert) && $after_insert == 'new_insert') {
  17. $goto = 'tbl_change.php'
  18. . '?lang=' . $lang
  19. . '&server=' . $server
  20. . '&db=' . urlencode($db)
  21. . '&table=' . urlencode($table)
  22. . '&goto=' . urlencode($goto)
  23. . '&pos=' . $pos
  24. . '&session_max_rows=' . $session_max_rows
  25. . '&disp_direction=' . $disp_direction
  26. . '&repeat_cells=' . $repeat_cells
  27. . (empty($sql_query) ? '' : '&sql_query=' . urlencode($sql_query));
  28. } else if ($goto == 'sql.php') {
  29. $goto = 'sql.php?'
  30. . 'lang=' . $lang
  31. . '&server=' . $server
  32. . '&db=' . urlencode($db)
  33. . '&table=' . urlencode($table)
  34. . '&pos=' . $pos
  35. . '&session_max_rows=' . $session_max_rows
  36. . '&disp_direction=' . $disp_direction
  37. . '&repeat_cells=' . $repeat_cells
  38. . '&sql_query=' . urlencode($sql_query);
  39. } else if (!empty($goto)) {
  40. // Security checkings
  41. $is_gotofile = ereg_replace('^([^?]+).*$', '\\1', $goto);
  42. if (!@file_exists('./' . $is_gotofile)) {
  43. $goto = (empty($table)) ? 'db_details.php' : 'tbl_properties.php';
  44. $is_gotofile = TRUE;
  45. } else {
  46. $is_gotofile = ($is_gotofile == $goto);
  47. }
  48. }
  49. // Defines the url to return in case of failure of the query
  50. if (isset($err_url)) {
  51. $err_url = urldecode($err_url);
  52. } else {
  53. $err_url = str_replace('&', '&amp;', $goto)
  54. . (empty($primary_key) ? '' : '&amp;primary_key=' . $primary_key);
  55. }
  56. // Resets tables defined in the configuration file
  57. reset($fields);
  58. if (isset($funcs)) {
  59. reset($funcs);
  60. }
  61. // Misc
  62. if (get_magic_quotes_gpc()) {
  63. $submit_type = stripslashes($submit_type);
  64. }
  65. /**
  66. * Prepares the update of a row
  67. */
  68. if (isset($primary_key) && ($submit_type != $strInsertAsNewRow)) {
  69. // Restore the "primary key" to a convenient format
  70. $primary_key = urldecode($primary_key);
  71. // Defines the SET part of the sql query
  72. $valuelist = '';
  73. while (list($key, $val) = each($fields)) {
  74. $encoded_key = $key;
  75. $key = urldecode($key);
  76. switch (strtolower($val)) {
  77. case 'null':
  78. break;
  79. case '$enum$':
  80. // if we have an enum, then construct the value
  81. $f = 'field_' . md5($key);
  82. if (!empty($$f)) {
  83. $val = implode(',', $$f);
  84. if ($val == 'null') {
  85. // void
  86. } else {
  87. $val = "'" . PMA_sqlAddslashes(urldecode($val)) . "'";
  88. }
  89. } else {
  90. $val = "''";
  91. }
  92. break;
  93. case '$set$':
  94. // if we have a set, then construct the value
  95. $f = 'field_' . md5($key);
  96. if (!empty($$f)) {
  97. $val = implode(',', $$f);
  98. $val = "'" . PMA_sqlAddslashes(urldecode($val)) . "'";
  99. } else {
  100. $val = "''";
  101. }
  102. break;
  103. default:
  104. if (get_magic_quotes_gpc()) {
  105. $val = "'" . str_replace('\\"', '"', $val) . "'";
  106. } else {
  107. $val = "'" . PMA_sqlAddslashes($val) . "'";
  108. }
  109. break;
  110. } // end switch
  111. // Was the Null checkbox checked for this field?
  112. if (isset($fields_null) && isset($fields_null[$encoded_key])) {
  113. $val = 'NULL';
  114. }
  115. // No change for this column and no MySQL function is used -> next column
  116. if (empty($funcs[$encoded_key])
  117. && isset($fields_prev) && isset($fields_prev[$encoded_key])
  118. && ("'" . PMA_sqlAddslashes(urldecode($fields_prev[$encoded_key])) . "'" == $val)) {
  119. continue;
  120. }
  121. else if (!empty($val)) {
  122. if (empty($funcs[$encoded_key])) {
  123. $valuelist .= PMA_backquote($key) . ' = ' . $val . ', ';
  124. } else if ($val == '\'\''
  125. && (ereg('^(NOW|CURDATE|CURTIME|UNIX_TIMESTAMP|RAND|USER|LAST_INSERT_ID)$', $funcs[$encoded_key]))) {
  126. $valuelist .= PMA_backquote($key) . ' = ' . $funcs[$encoded_key] . '(), ';
  127. } else {
  128. $valuelist .= PMA_backquote($key) . ' = ' . $funcs[$encoded_key] . "($val), ";
  129. }
  130. }
  131. } // end while
  132. // Builds the sql upate query
  133. $valuelist = ereg_replace(', $', '', $valuelist);
  134. if (!empty($valuelist)) {
  135. $query = 'UPDATE ' . PMA_backquote($table) . ' SET ' . $valuelist . ' WHERE' . $primary_key
  136. . ((PMA_MYSQL_INT_VERSION >= 32300) ? ' LIMIT 1' : '');
  137. $message = $strAffectedRows . '&nbsp;';
  138. }
  139. // No change -> move back to the calling script
  140. else {
  141. $message = $strNoModification;
  142. if ($is_gotofile) {
  143. $js_to_run = 'functions.js';
  144. include('./header.inc.php');
  145. include('./' . ereg_replace('\.\.*', '.', $goto));
  146. } else {
  147. header('Location: ' . $cfgPmaAbsoluteUri . $goto . '&message=' . urlencode($message));
  148. }
  149. exit();
  150. }
  151. } // end row update
  152. /**
  153. * Prepares the insert of a row
  154. */
  155. else {
  156. $fieldlist = '';
  157. $valuelist = '';
  158. while (list($key, $val) = each($fields)) {
  159. $encoded_key = $key;
  160. $key = urldecode($key);
  161. $fieldlist .= PMA_backquote($key) . ', ';
  162. switch (strtolower($val)) {
  163. case 'null':
  164. break;
  165. case '$enum$':
  166. // if we have a set, then construct the value
  167. $f = 'field_' . md5($key);
  168. if (!empty($$f)) {
  169. $val = implode(',', $$f);
  170. if ($val == 'null') {
  171. // void
  172. } else {
  173. $val = "'" . PMA_sqlAddslashes(urldecode($val)) . "'";
  174. }
  175. } else {
  176. $val = "''";
  177. }
  178. break;
  179. case '$set$':
  180. // if we have a set, then construct the value
  181. $f = 'field_' . md5($key);
  182. if (!empty($$f)) {
  183. $val = implode(',', $$f);
  184. $val = "'" . PMA_sqlAddslashes(urldecode($val)) . "'";
  185. } else {
  186. $val = "''";
  187. }
  188. break;
  189. default:
  190. if (get_magic_quotes_gpc()) {
  191. $val = "'" . str_replace('\\"', '"', $val) . "'";
  192. } else {
  193. $val = "'" . PMA_sqlAddslashes($val) . "'";
  194. }
  195. break;
  196. } // end switch
  197. // Was the Null checkbox checked for this field?
  198. if (isset($fields_null) && isset($fields_null[$encoded_key])) {
  199. $val = 'NULL';
  200. }
  201. if (empty($funcs[$encoded_key])) {
  202. $valuelist .= $val . ', ';
  203. } else if (($val == '\'\''
  204. && ereg('^(UNIX_TIMESTAMP|RAND|LAST_INSERT_ID)$', $funcs[$encoded_key]))
  205. || ereg('^(NOW|CURDATE|CURTIME|USER)$', $funcs[$encoded_key])) {
  206. $valuelist .= $funcs[$encoded_key] . '(), ';
  207. } else {
  208. $valuelist .= $funcs[$encoded_key] . '(' . $val . '), ';
  209. }
  210. } // end while
  211. // Builds the sql insert query
  212. $fieldlist = ereg_replace(', $', '', $fieldlist);
  213. $valuelist = ereg_replace(', $', '', $valuelist);
  214. $query = 'INSERT INTO ' . PMA_backquote($table) . ' (' . $fieldlist . ') VALUES (' . $valuelist . ')';
  215. $message = $strInsertedRows . '&nbsp;';
  216. } // end row insertion
  217. /**
  218. * Executes the sql query and get the result, then move back to the calling
  219. * page
  220. */
  221. mysql_select_db($db);
  222. $sql_query = $query . ';';
  223. $result = mysql_query($query);
  224. if (!$result) {
  225. $error = mysql_error();
  226. include('./header.inc.php');
  227. PMA_mysqlDie($error, '', '', $err_url);
  228. } else {
  229. if (@mysql_affected_rows()) {
  230. $message .= @mysql_affected_rows();
  231. } else {
  232. $message = $strModifications;
  233. }
  234. if ($is_gotofile) {
  235. if ($goto == 'db_details.php' && !empty($table)) {
  236. unset($table);
  237. }
  238. $js_to_run = 'functions.js';
  239. include('./header.inc.php');
  240. include('./' . ereg_replace('\.\.*', '.', $goto));
  241. } else {
  242. $add_query = (strpos(' ' . $goto, 'tbl_change') ? '&disp_query=' . urlencode($sql_query) : '');
  243. header('Location: ' . $cfgPmaAbsoluteUri . $goto . '&message=' . urlencode($message) . $add_query);
  244. }
  245. exit();
  246. } // end if
  247. ?>