PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/xampp/phpMyAdmin/tbl_replace.php

https://github.com/edmondscommerce/XAMPP-Magento-Demo-Site
PHP | 414 lines | 268 code | 45 blank | 101 comment | 69 complexity | 4addd888f80c8e2b76f5c5f0feae427f MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * manipulation of table data like inserting, replacing and updating
  5. *
  6. * usally called as form action from tbl_change.php to insert or update table rows
  7. *
  8. * @version $Id: tbl_replace.php 12245 2009-02-23 08:36:34Z lem9 $
  9. *
  10. * @todo 'edit_next' tends to not work as expected if used ... at least there is no order by
  11. * it needs the original query and the row number and than replace the LIMIT clause
  12. * @uses PMA_checkParameters()
  13. * @uses PMA_DBI_select_db()
  14. * @uses PMA_DBI_query()
  15. * @uses PMA_DBI_fetch_row()
  16. * @uses PMA_DBI_get_fields_meta()
  17. * @uses PMA_DBI_free_result()
  18. * @uses PMA_DBI_try_query()
  19. * @uses PMA_DBI_getError()
  20. * @uses PMA_DBI_affected_rows()
  21. * @uses PMA_DBI_insert_id()
  22. * @uses PMA_backquote()
  23. * @uses PMA_getUniqueCondition()
  24. * @uses PMA_sqlAddslashes()
  25. * @uses PMA_securePath()
  26. * @uses PMA_sendHeaderLocation()
  27. * @uses str_replace()
  28. * @uses count()
  29. * @uses file_exists()
  30. * @uses strlen()
  31. * @uses str_replace()
  32. * @uses preg_replace()
  33. * @uses is_array()
  34. * @uses $GLOBALS['db']
  35. * @uses $GLOBALS['table']
  36. * @uses $GLOBALS['goto']
  37. * @uses $GLOBALS['sql_query']
  38. * @uses PMA_File::getRecentBLOBReference()
  39. */
  40. /**
  41. * do not import request variable into global scope
  42. *
  43. * cannot be used as long as it could happen that the $goto file that is included
  44. * at the end of this script is not updated to work without imported request variables
  45. *
  46. * @todo uncomment this if all possible included files to rely on import request variables
  47. if (! defined('PMA_NO_VARIABLES_IMPORT')) {
  48. define('PMA_NO_VARIABLES_IMPORT', true);
  49. }
  50. */
  51. /**
  52. * Gets some core libraries
  53. */
  54. require_once './libraries/common.inc.php';
  55. // Check parameters
  56. PMA_checkParameters(array('db', 'table', 'goto'));
  57. PMA_DBI_select_db($GLOBALS['db']);
  58. /**
  59. * Initializes some variables
  60. */
  61. $goto_include = false;
  62. if (isset($_REQUEST['insert_rows']) && is_numeric($_REQUEST['insert_rows']) && $_REQUEST['insert_rows'] != $cfg['InsertRows']) {
  63. $cfg['InsertRows'] = $_REQUEST['insert_rows'];
  64. $GLOBALS['js_include'][] = 'tbl_change.js';
  65. require_once './libraries/header.inc.php';
  66. require './tbl_change.php';
  67. exit;
  68. }
  69. if (isset($_REQUEST['after_insert'])
  70. && in_array($_REQUEST['after_insert'], array('new_insert', 'same_insert', 'edit_next'))) {
  71. $url_params['after_insert'] = $_REQUEST['after_insert'];
  72. //$GLOBALS['goto'] = 'tbl_change.php';
  73. $goto_include = 'tbl_change.php';
  74. if (isset($_REQUEST['primary_key'])) {
  75. if ($_REQUEST['after_insert'] == 'same_insert') {
  76. foreach ($_REQUEST['primary_key'] as $pk) {
  77. $url_params['primary_key'][] = $pk;
  78. }
  79. } elseif ($_REQUEST['after_insert'] == 'edit_next') {
  80. foreach ($_REQUEST['primary_key'] as $pk) {
  81. $local_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
  82. . ' WHERE ' . str_replace('` =', '` >', $pk)
  83. . ' LIMIT 1;';
  84. $res = PMA_DBI_query($local_query);
  85. $row = PMA_DBI_fetch_row($res);
  86. $meta = PMA_DBI_get_fields_meta($res);
  87. // must find a unique condition based on unique key,
  88. // not a combination of all fields
  89. if ($tmp = PMA_getUniqueCondition($res, count($meta), $meta, $row, true)) {
  90. $_SESSION['edit_next'] = $tmp;
  91. }
  92. unset($tmp);
  93. }
  94. }
  95. }
  96. } elseif (! empty($GLOBALS['goto'])) {
  97. if (! preg_match('@^[a-z_]+\.php$@', $GLOBALS['goto'])) {
  98. // this should NOT happen
  99. //$GLOBALS['goto'] = false;
  100. $goto_include = false;
  101. } else {
  102. $goto_include = $GLOBALS['goto'];
  103. }
  104. if ($GLOBALS['goto'] == 'db_sql.php' && strlen($GLOBALS['table'])) {
  105. $GLOBALS['table'] = '';
  106. }
  107. }
  108. if (! $goto_include) {
  109. if (! strlen($GLOBALS['table'])) {
  110. $goto_include = 'db_sql.php';
  111. } else {
  112. $goto_include = 'tbl_sql.php';
  113. }
  114. }
  115. // Defines the url to return in case of failure of the query
  116. if (isset($_REQUEST['err_url'])) {
  117. $err_url = $_REQUEST['err_url'];
  118. } else {
  119. $err_url = 'tbl_change.php' . PMA_generate_common_url($url_params);
  120. }
  121. /**
  122. * Prepares the update/insert of a row
  123. */
  124. if (isset($_REQUEST['primary_key'])) {
  125. // we were editing something => use primary key
  126. $loop_array = (is_array($_REQUEST['primary_key']) ? $_REQUEST['primary_key'] : array($_REQUEST['primary_key']));
  127. $using_key = true;
  128. $is_insert = ($_REQUEST['submit_type'] == $GLOBALS['strInsertAsNewRow']);
  129. } else {
  130. // new row => use indexes
  131. $loop_array = array();
  132. foreach ($_REQUEST['fields']['multi_edit'] as $key => $dummy) {
  133. $loop_array[] = $key;
  134. }
  135. $using_key = false;
  136. $is_insert = true;
  137. }
  138. $query = array();
  139. $value_sets = array();
  140. $func_no_param = array(
  141. 'NOW',
  142. 'CURDATE',
  143. 'CURTIME',
  144. 'UTC_DATE',
  145. 'UTC_TIME',
  146. 'UTC_TIMESTAMP',
  147. 'UNIX_TIMESTAMP',
  148. 'RAND',
  149. 'USER',
  150. 'LAST_INSERT_ID',
  151. );
  152. foreach ($loop_array as $rowcount => $primary_key) {
  153. // skip fields to be ignored
  154. if (! $using_key && isset($_REQUEST['insert_ignore_' . $primary_key])) {
  155. continue;
  156. }
  157. // Defines the SET part of the sql query
  158. $query_values = array();
  159. // Map multi-edit keys to single-level arrays, dependent on how we got the fields
  160. $me_fields =
  161. isset($_REQUEST['fields']['multi_edit'][$rowcount])
  162. ? $_REQUEST['fields']['multi_edit'][$rowcount]
  163. : array();
  164. $me_fields_prev =
  165. isset($_REQUEST['fields_prev']['multi_edit'][$rowcount])
  166. ? $_REQUEST['fields_prev']['multi_edit'][$rowcount]
  167. : null;
  168. $me_funcs =
  169. isset($_REQUEST['funcs']['multi_edit'][$rowcount])
  170. ? $_REQUEST['funcs']['multi_edit'][$rowcount]
  171. : null;
  172. $me_fields_type =
  173. isset($_REQUEST['fields_type']['multi_edit'][$rowcount])
  174. ? $_REQUEST['fields_type']['multi_edit'][$rowcount]
  175. : null;
  176. $me_fields_null =
  177. isset($_REQUEST['fields_null']['multi_edit'][$rowcount])
  178. ? $_REQUEST['fields_null']['multi_edit'][$rowcount]
  179. : null;
  180. $me_fields_null_prev =
  181. isset($_REQUEST['fields_null_prev']['multi_edit'][$rowcount])
  182. ? $_REQUEST['fields_null_prev']['multi_edit'][$rowcount]
  183. : null;
  184. $me_auto_increment =
  185. isset($_REQUEST['auto_increment']['multi_edit'][$rowcount])
  186. ? $_REQUEST['auto_increment']['multi_edit'][$rowcount]
  187. : null;
  188. $primary_field = PMA_BS_GetPrimaryField($GLOBALS['db'], $GLOBALS['table']);
  189. // Fetch the current values of a row to use in case we have a protected field
  190. // @todo possibly move to ./libraries/tbl_replace_fields.inc.php
  191. if ($is_insert && $using_key && isset($me_fields_type) &&
  192. is_array($me_fields_type) && isset($primary_key)) {
  193. $prot_result = PMA_DBI_query('SELECT * FROM ' .
  194. PMA_backquote($table) . ' WHERE ' . $primary_key . ';');
  195. $prot_row = PMA_DBI_fetch_assoc($prot_result);
  196. PMA_DBI_free_result($prot_result);
  197. unset($prot_result);
  198. }
  199. foreach ($me_fields as $key => $val) {
  200. require './libraries/tbl_replace_fields.inc.php';
  201. // rajk - for blobstreaming
  202. if (NULL != $primary_field || strlen($primary_field) > 0)
  203. {
  204. $remove_blob_repo = isset($_REQUEST['remove_blob_repo_' . $key]) ? $_REQUEST['remove_blob_repo_' . $key] : NULL;
  205. $upload_blob_repo = isset($_REQUEST['upload_blob_repo_' . $key]) ? $_REQUEST['upload_blob_repo_' . $key] : NULL;
  206. // checks if an existing blob repository reference should be removed
  207. if (isset($remove_blob_repo) && !isset($upload_blob_repo))
  208. {
  209. $remove_blob_reference = $_REQUEST['remove_blob_ref_' . $key];
  210. if (isset($remove_blob_reference))
  211. $val = "''";
  212. }
  213. // checks if this field requires a bs reference attached to it
  214. $requires_bs_reference = isset($upload_blob_repo);
  215. if ($requires_bs_reference)
  216. {
  217. // get the most recent BLOB reference
  218. $bs_reference = PMA_File::getRecentBLOBReference();
  219. // if the most recent BLOB reference exists, set it as a field value
  220. if (!is_null($bs_reference))
  221. $val = "'" . PMA_sqlAddslashes($bs_reference) . "'";
  222. }
  223. }
  224. if (empty($me_funcs[$key])) {
  225. $cur_value = $val;
  226. } elseif ('UNIX_TIMESTAMP' === $me_funcs[$key] && $val != "''") {
  227. $cur_value = $me_funcs[$key] . '(' . $val . ')';
  228. } elseif (in_array($me_funcs[$key], $func_no_param)) {
  229. $cur_value = $me_funcs[$key] . '()';
  230. } else {
  231. $cur_value = $me_funcs[$key] . '(' . $val . ')';
  232. }
  233. // i n s e r t
  234. if ($is_insert) {
  235. // no need to add column into the valuelist
  236. if (strlen($cur_value)) {
  237. $query_values[] = $cur_value;
  238. // first inserted row so prepare the list of fields
  239. if (empty($value_sets)) {
  240. $query_fields[] = PMA_backquote($key);
  241. }
  242. }
  243. // u p d a t e
  244. } elseif (!empty($me_fields_null_prev[$key])
  245. && !isset($me_fields_null[$key])) {
  246. // field had the null checkbox before the update
  247. // field no longer has the null checkbox
  248. $query_values[] = PMA_backquote($key) . ' = ' . $cur_value;
  249. } elseif (empty($me_funcs[$key])
  250. && isset($me_fields_prev[$key])
  251. && ("'" . PMA_sqlAddslashes($me_fields_prev[$key]) . "'" == $val)) {
  252. // No change for this column and no MySQL function is used -> next column
  253. continue;
  254. } elseif (! empty($val)) {
  255. // avoid setting a field to NULL when it's already NULL
  256. // (field had the null checkbox before the update
  257. // field still has the null checkbox)
  258. if (!(! empty($me_fields_null_prev[$key])
  259. && isset($me_fields_null[$key]))) {
  260. $query_values[] = PMA_backquote($key) . ' = ' . $cur_value;
  261. }
  262. }
  263. } // end foreach ($me_fields as $key => $val)
  264. if (count($query_values) > 0) {
  265. if ($is_insert) {
  266. $value_sets[] = implode(', ', $query_values);
  267. } else {
  268. // build update query
  269. $query[] = 'UPDATE ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
  270. . ' SET ' . implode(', ', $query_values) . ' WHERE ' . str_replace('&#93;', ']', $primary_key) . ' LIMIT 1';
  271. }
  272. }
  273. } // end foreach ($loop_array as $primary_key)
  274. unset($me_fields_prev, $me_funcs, $me_fields_type, $me_fields_null, $me_fields_null_prev,
  275. $me_auto_increment, $cur_value, $key, $val, $loop_array, $primary_key, $using_key,
  276. $func_no_param);
  277. // Builds the sql query
  278. if ($is_insert && count($value_sets) > 0) {
  279. $query[] = 'INSERT INTO ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
  280. . ' (' . implode(', ', $query_fields) . ') VALUES (' . implode('), (', $value_sets) . ')';
  281. unset($query_fields, $value_sets);
  282. $message = PMA_Message::success('strRowsInserted');
  283. } elseif (! empty($query)) {
  284. $message = PMA_Message::success('strRowsAffected');
  285. } else {
  286. // No change -> move back to the calling script
  287. $message = PMA_Message::success('strNoModification');
  288. $GLOBALS['js_include'][] = 'functions.js';
  289. $active_page = $goto_include;
  290. require_once './libraries/header.inc.php';
  291. require './' . PMA_securePath($goto_include);
  292. exit;
  293. }
  294. unset($me_fields, $is_insert);
  295. /**
  296. * Executes the sql query and get the result, then move back to the calling
  297. * page
  298. */
  299. if (! empty($GLOBALS['sql_query'])) {
  300. $url_params['sql_query'] = $GLOBALS['sql_query'];
  301. $return_to_sql_query = $GLOBALS['sql_query'];
  302. }
  303. $GLOBALS['sql_query'] = implode('; ', $query) . ';';
  304. $total_affected_rows = 0;
  305. $last_messages = array();
  306. $warning_messages = array();
  307. $error_messages = array();
  308. foreach ($query as $single_query) {
  309. if ($GLOBALS['cfg']['IgnoreMultiSubmitErrors']) {
  310. $result = PMA_DBI_try_query($single_query);
  311. } else {
  312. $result = PMA_DBI_query($single_query);
  313. }
  314. if (! $result) {
  315. $error_messages[] = PMA_DBI_getError();
  316. } else {
  317. if (@PMA_DBI_affected_rows()) {
  318. $total_affected_rows += @PMA_DBI_affected_rows();
  319. }
  320. $insert_id = PMA_DBI_insert_id();
  321. if ($insert_id != 0) {
  322. // insert_id is id of FIRST record inserted in one insert, so if we
  323. // inserted multiple rows, we had to increment this
  324. if ($total_affected_rows > 0) {
  325. $insert_id = $insert_id + $total_affected_rows - 1;
  326. }
  327. $last_message = PMA_Message::notice('strInsertedRowId');
  328. $last_message->addParam($insert_id);
  329. $last_messages[] = $last_message;
  330. }
  331. PMA_DBI_free_result($result);
  332. } // end if
  333. foreach (PMA_DBI_get_warnings() as $warning) {
  334. $warning_messages[] = $warning['Level'] . ': #' . $warning['Code']
  335. . ' ' . $warning['Message'];
  336. }
  337. unset($result);
  338. }
  339. unset($single_query, $query);
  340. $message->addParam($total_affected_rows);
  341. $message->addMessages($last_messages, '<br />');
  342. if (! empty($warning_messages)) {
  343. /**
  344. * @todo use a <div class="warning"> in PMA_showMessage() for this part of
  345. * the message
  346. */
  347. $message->addMessages($warning_messages, '<br />');
  348. $message->isWarning(true);
  349. }
  350. if (! empty($error_messages)) {
  351. $message->addMessages($error_messages);
  352. $message->isError(true);
  353. }
  354. unset($error_messages, $warning_messages, $total_affected_rows, $last_messages, $last_message);
  355. if (isset($return_to_sql_query)) {
  356. $disp_query = $GLOBALS['sql_query'];
  357. $disp_message = $message;
  358. unset($message);
  359. $GLOBALS['sql_query'] = $return_to_sql_query;
  360. }
  361. $GLOBALS['js_include'][] = 'tbl_change.js';
  362. $GLOBALS['js_include'][] = 'functions.js';
  363. // in case we call sql.php which needs those:
  364. $GLOBALS['js_include'][] = 'mootools.js';
  365. $active_page = $goto_include;
  366. require_once './libraries/header.inc.php';
  367. require './' . PMA_securePath($goto_include);
  368. exit;
  369. ?>