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.

191 lines
6.0 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: ./includes/install.inc.php
  11. // Repository: $HeadURL: file:///svn/p/refbase/code/branches/bleeding-edge/includes/install.inc.php $
  12. // Author(s): Richard Karnesky <mailto:karnesky@gmail.com> and
  13. // Matthias Steffens <mailto:refbase@extracts.de>
  14. //
  15. // Created: 16-Aug-06, 18:00
  16. // Modified: $Date: 2017-04-13 02:00:18 +0000 (Thu, 13 Apr 2017) $
  17. // $Author: karnesky $
  18. // $Revision: 1416 $
  19. // This file contains functions
  20. // that are used when installing
  21. // or updating a refbase database.
  22. // --------------------------------------------------------------------
  23. // This function attempts to find a file (or program) on disk. It searches directories
  24. // given in '$fileLocations' for existing file/program names given in '$fileNames'.
  25. // Note that, currently, this function won't look into subdirectories.
  26. //
  27. // Authors: Richard Karnesky <mailto:karnesky@gmail.com> and
  28. // Matthias Steffens <mailto:refbase@extracts.de>
  29. function locateFile($fileLocations, $fileNames, $returnParentDirOnly)
  30. {
  31. $filePath = "";
  32. foreach ($fileLocations as $location)
  33. {
  34. foreach ($fileNames as $name)
  35. {
  36. if (file_exists("$location/$name"))
  37. {
  38. if ($returnParentDirOnly)
  39. $filePath = realpath($location) . "/";
  40. else
  41. $filePath = realpath("$location/$name");
  42. break 2;
  43. }
  44. }
  45. }
  46. return $filePath;
  47. }
  48. // --------------------------------------------------------------------
  49. // Connect to the MySQL database with admin permissions:
  50. // TODO: I18n
  51. function connectToMySQLDatabaseAsAdmin($adminUserName, $adminPassword)
  52. {
  53. global $hostName; // these variables are specified in 'db.inc.php'
  54. global $databaseName;
  55. global $connection;
  56. // Establish a *new* connection that has admin permissions
  57. // (1) OPEN the database connection:
  58. if (!($connection = @ mysqli_connect($hostName, $adminUserName, $adminPassword, $databaseName)))
  59. if (mysqli_errno($connection) != 0) // this works around a stupid(?) behaviour of the Roxen webserver that returns 'errno: 0' on success! ?:-(
  60. showErrorMsg("The following error occurred while trying to connect to the host:", "");
  61. }
  62. // --------------------------------------------------------------------
  63. // Check for the presence of a value in a table,
  64. // and if it doesn't exist, add the given row to that same table:
  65. //
  66. // Authors: Richard Karnesky <mailto:karnesky@gmail.com> and
  67. // Matthias Steffens <mailto:refbase@extracts.de>
  68. function insertIfNotExists($keysArray, $table, $values, $userID = "")
  69. {
  70. global $connection;
  71. $selectClauseArray = array();
  72. $whereClauseArray = array();
  73. foreach ($keysArray as $keyColumn => $keyValue)
  74. {
  75. $selectClauseArray[] = $keyColumn;
  76. $whereClauseArray[] = $keyColumn . " = " . quote_smart($keyValue);
  77. }
  78. $query = "SELECT " . implode(", ", $selectClauseArray)
  79. . " FROM " . $table
  80. . " WHERE " . implode(" AND ", $whereClauseArray);
  81. if ($userID != "") // note that 'if (!empty($userID))' doesn't work here since '$userID = 0' would incorrectly be treated as 'empty'
  82. $query .= " AND user_id = " . $userID;
  83. $result = queryMySQLDatabase($query); // function 'queryMySQLDatabase()' is defined in 'include.inc.php'
  84. $rowsFound = @ mysqli_num_rows($result);
  85. if ($rowsFound == 0)
  86. {
  87. $query = "INSERT INTO " . $table . " VALUES " . $values;
  88. $result = queryMySQLDatabase($query);
  89. return "true";
  90. }
  91. else
  92. {
  93. return "false";
  94. }
  95. }
  96. // --------------------------------------------------------------------
  97. // Check for the presence of a column in a table,
  98. // and if it doesn't exist, add the given column to that same table:
  99. //
  100. // Author: Richard Karnesky <mailto:karnesky@gmail.com>
  101. function addColumnIfNotExists($column, $table, $properties)
  102. {
  103. global $connection;
  104. $present = false;
  105. $queryFields = "SHOW FIELDS FROM " . $table;
  106. $result = queryMySQLDatabase($queryFields); // function 'queryMySQLDatabase()' is defined in 'include.inc.php'
  107. while ($row = @ mysqli_fetch_array($result)) // for all fields found, check if any of their names matches the field name that we want to add
  108. if ($row["Field"] == $column)
  109. $present = true;
  110. if (!$present)
  111. {
  112. $query = "ALTER TABLE " . $table . " ADD COLUMN " . $column . " " . $properties;
  113. $result = queryMySQLDatabase($query);
  114. return "true";
  115. }
  116. else
  117. {
  118. return "false";
  119. }
  120. }
  121. // --------------------------------------------------------------------
  122. // Check for the presence of a table in the currently selected database,
  123. // and if it doesn't exist, add the given table to that same database:
  124. // This is similar to "CREATE TABLE IF NOT EXISTS ..." but allows us
  125. // to return appropriate feedback
  126. function addTableIfNotExists($table, $properties)
  127. {
  128. global $connection;
  129. $present = false;
  130. $queryFields = "SHOW TABLES";
  131. $result = queryMySQLDatabase($queryFields); // function 'queryMySQLDatabase()' is defined in 'include.inc.php'
  132. while ($row = @ mysqli_fetch_array($result)) // for all tables found, check if any of their names matches the table name that we want to add
  133. if ($row[0] == $table)
  134. $present = true;
  135. if (!$present)
  136. {
  137. $query = "CREATE TABLE " . $table . " " . $properties;
  138. $result = queryMySQLDatabase($query);
  139. return "true";
  140. }
  141. else
  142. {
  143. return "false";
  144. }
  145. }
  146. // --------------------------------------------------------------------
  147. // Show error in red:
  148. function fieldError($fieldName, $errors)
  149. {
  150. if (isset($errors[$fieldName]))
  151. echo returnMsg($errors[$fieldName], "warning", "strong", "", "\n\t\t\t", "\n\t\t\t<br>"); // function 'returnMsg()' is defined in 'include.inc.php'
  152. }
  153. // --------------------------------------------------------------------
  154. ?>