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.

198 lines
9.3 KiB

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