You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
9.4 KiB

  1. <?php
  2. // turn on warnings and notice during developement
  3. include('initialize/PhpErrorSettings.inc.php');
  4. // Project: Web Reference Database (refbase) <http://www.refbase.net>
  5. // Copyright: Matthias Steffens <mailto:refbase@extracts.de> and the file's
  6. // original author(s).
  7. //
  8. // This code is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY. Please see the GNU General Public
  10. // License for more details.
  11. //
  12. // File: ./duplicate_modify.php
  13. // Repository: $HeadURL: file:///svn/p/refbase/code/branches/bleeding-edge/duplicate_modify.php $
  14. // Author(s): Matthias Steffens <mailto:refbase@extracts.de>
  15. //
  16. // Created: 27-Jan-07, 23:22
  17. // Modified: $Date: 2017-04-13 02:00:18 +0000 (Thu, 13 Apr 2017) $
  18. // $Author: karnesky $
  19. // $Revision: 1416 $
  20. // This php script will flag records as original and duplicate records.
  21. // It then displays the affected records using 'search.php' so that the user
  22. // can verify the changes.
  23. // TODO: I18n
  24. // Incorporate some include files:
  25. include 'initialize/db.inc.php'; // 'db.inc.php' is included to hide username and password
  26. include 'includes/include.inc.php'; // include common functions
  27. include 'initialize/ini.inc.php'; // include common variables
  28. // --------------------------------------------------------------------
  29. // START A SESSION:
  30. // call the 'start_session()' function (from 'include.inc.php') which will also read out available session variables:
  31. start_session(true);
  32. // --------------------------------------------------------------------
  33. // Initialize preferred display language:
  34. // (note that 'locales.inc.php' has to be included *after* the call to the 'start_session()' function)
  35. include 'includes/locales.inc.php'; // include the locales
  36. // --------------------------------------------------------------------
  37. // Clear any errors that might have been found previously:
  38. $errors = array();
  39. // Write the form variables into an array:
  40. foreach($_REQUEST as $varname => $value)
  41. $formVars[$varname] = trim($value); // remove any leading or trailing whitespace from the field's contents & copy the trimmed string to the '$formVars' array
  42. // $formVars[$varname] = trim(clean($value, 50)); // the use of the clean function would be more secure!
  43. // --------------------------------------------------------------------
  44. // Extract form variables:
  45. // Note: Although we could use the '$formVars' array directly below (e.g.: $formVars['origRecord'] etc., like in 'user_validation.php'), we'll read out
  46. // all variables individually again. This is done to enhance readability. (A smarter way of doing so seems be the use of the 'extract()' function, but that
  47. // may expose yet another security hole...)
  48. // First of all, check if this script was called by something else than 'duplicate_manager.php':
  49. if (!preg_match("#/duplicate_manager\.php#i", $referer)) // variable '$referer' is globally defined in function 'start_session()' in 'include.inc.php'
  50. {
  51. // return an appropriate error message:
  52. $HeaderString = returnMsg($loc["Warning_InvalidCallToScript"] . " '" . scriptURL() . "'!", "warning", "strong", "HeaderString"); // functions 'returnMsg()' and 'scriptURL()' are defined in 'include.inc.php'
  53. header("Location: " . $referer); // redirect to calling page
  54. exit; // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> !EXIT! <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  55. }
  56. // Extract the form used by the user:
  57. $formType = $formVars['formType'];
  58. // Extract the view type requested by the user (either 'Mobile', 'Print', 'Web' or ''):
  59. // ('' will produce the default 'Web' output style)
  60. if (isset($formVars['viewType']))
  61. $viewType = $formVars['viewType'];
  62. else
  63. $viewType = "";
  64. // Extract other form values provided by 'duplicate_manager.php':
  65. if (isset($formVars['origRecord']))
  66. $origRecord = $formVars['origRecord'];
  67. else
  68. $origRecord = "";
  69. if (isset($formVars['dupRecords']))
  70. $dupRecords = $formVars['dupRecords'];
  71. else
  72. $dupRecords = "";
  73. // Extract serial numbers (i.e. discard any non-digit characters from the original user input):
  74. $origRecordSerial = preg_replace("/\D*(\d+).*/", "\\1", $origRecord); // extract the first number given
  75. $dupRecordSerialsArray = preg_split("/\D+/", $dupRecords, -1, PREG_SPLIT_NO_EMPTY); // extract all given serial numbers (the 'PREG_SPLIT_NO_EMPTY' flag causes only non-empty pieces to be returned)
  76. // --------------------------------------------------------------------
  77. // (1) OPEN CONNECTION, (2) SELECT DATABASE
  78. connectToMySQLDatabase(); // function 'connectToMySQLDatabase()' is defined in 'include.inc.php'
  79. // --------------------------------------------------------------------
  80. // VALIDATE data fields:
  81. // NOTE: for all fields that are validated here must exist error parsing code (of the form: " . fieldError("origRecord", $errors) . ")
  82. // in front of the respective <input> form field in 'duplicate_manager.php'! Otherwise the generated error won't be displayed!
  83. // Validate the 'Original' field:
  84. if (empty($origRecord) OR !preg_match("/\d/", $origRecord))
  85. $errors["origRecord"] = "You must specify a serial number for the original record:"; // 'origRecord' must not be empty and must contain a number
  86. elseif (preg_match("/\d\D+\d/", $origRecord))
  87. $errors["origRecord"] = "You can only specify a single record as original entry:"; // only one serial number must be given
  88. elseif (in_array($origRecordSerial, $dupRecordSerialsArray))
  89. $errors["origRecord"] = "The original record cannot be one of the duplicate records:"; // the serial number of the original record must not be given within the list of duplicate serial numbers
  90. // Validate the 'Duplicates' field:
  91. if (empty($dupRecords) OR !preg_match("/\d/", $dupRecords))
  92. $errors["dupRecords"] = "You must specify at least one serial number that identifies a duplicate record:"; // 'dupRecords' must not be empty and at least one serial number must be given
  93. // --------------------------------------------------------------------
  94. // Now the script has finished the validation, check if there were any errors:
  95. if (count($errors) > 0)
  96. {
  97. // Write back session variables:
  98. saveSessionVariable("errors", $errors); // function 'saveSessionVariable()' is defined in 'include.inc.php'
  99. saveSessionVariable("formVars", $formVars);
  100. // There are errors. Relocate back to the 'Flag Duplicates' form (script 'duplicate_manager.php'):
  101. header("Location: " . $referer);
  102. exit; // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> !EXIT! <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  103. }
  104. // --------------------------------------------------------------------
  105. // If we made it here, then the data is considered valid!
  106. // CONSTRUCT SQL QUERY:
  107. // UPDATE field 'orig_record' in table 'refs':
  108. // original record:
  109. $queryArray[] = "UPDATE $tableRefs SET "
  110. . "orig_record = -" . $origRecordSerial
  111. . " WHERE serial = " . $origRecordSerial;
  112. // duplicate record(s):
  113. $queryArray[] = "UPDATE $tableRefs SET "
  114. . "orig_record = " . $origRecordSerial
  115. . " WHERE serial RLIKE \"^(" . implode("|", $dupRecordSerialsArray) . ")$\"";
  116. // --------------------------------------------------------------------
  117. // (3) RUN QUERY, (4) DISPLAY HEADER & RESULTS
  118. // (3) RUN the queries on the database through the connection:
  119. foreach($queryArray as $query)
  120. $result = queryMySQLDatabase($query); // function 'queryMySQLDatabase()' is defined in 'include.inc.php'
  121. $affectedRows = ($result ? mysqli_affected_rows ($connection) : 0); // get the number of rows that were modified (or return 0 if an error occurred)
  122. if ($affectedRows == 0) // no rows were affected by the update
  123. {
  124. // we'll file this additional error element here so that the 'errors' session variable isn't empty causing 'duplicate_manager.php' to re-load the form data that were submitted by the user
  125. $errors["ignoredRecords"] = "all";
  126. // return an appropriate error message:
  127. $HeaderString = returnMsg("Nothing was changed by your query!", "warning", "strong", "HeaderString"); // function 'returnMsg()' is defined in 'include.inc.php'
  128. // Write back session variables:
  129. saveSessionVariable("errors", $errors); // function 'saveSessionVariable()' is defined in 'include.inc.php'
  130. saveSessionVariable("formVars", $formVars);
  131. // Relocate back to the 'Flag Duplicates' form (script 'duplicate_manager.php'):
  132. header("Location: " . $referer);
  133. exit; // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> !EXIT! <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  134. }
  135. // Build correct header message:
  136. $HeaderString = returnMsg("The records below have been successfully flagged as original/duplicate records:", "", "", "HeaderString"); // function 'returnMsg()' is defined in 'include.inc.php'
  137. // Merge all given record serial numbers:
  138. $allRecordSerialsString = $origRecordSerial . "," . implode(",", $dupRecordSerialsArray);
  139. // (4) Call 'show.php' which will display all affected records along with the header message
  140. // (routing feedback output to a different script page will avoid any reload problems effectively!)
  141. header("Location: show.php?records=" . $allRecordSerialsString);
  142. // --------------------------------------------------------------------
  143. // (5) CLOSE CONNECTION
  144. disconnectFromMySQLDatabase(); // function 'disconnectFromMySQLDatabase()' is defined in 'include.inc.php'
  145. // --------------------------------------------------------------------
  146. ?>