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.

226 lines
9.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: ./sql_search.php
  11. // Repository: $HeadURL: file:///svn/p/refbase/code/branches/bleeding-edge/sql_search.php $
  12. // Author(s): Matthias Steffens <mailto:refbase@extracts.de>
  13. //
  14. // Created: 29-Jul-02, 16:39
  15. // Modified: $Date: 2012-03-05 10:19:03 +0000 (Mon, 05 Mar 2012) $
  16. // $Author: msteffens $
  17. // $Revision: 1363 $
  18. // Search form that offers to specify a custom sql query.
  19. // It offers some output options (like how many records to display per page)
  20. // and provides some examples and links for further information on sql queries.
  21. // Incorporate some include files:
  22. include 'initialize/db.inc.php'; // 'db.inc.php' is included to hide username and password
  23. include 'includes/header.inc.php'; // include header
  24. include 'includes/footer.inc.php'; // include footer
  25. include 'includes/include.inc.php'; // include common functions
  26. include 'initialize/ini.inc.php'; // include common variables
  27. // --------------------------------------------------------------------
  28. // START A SESSION:
  29. // call the 'start_session()' function (from 'include.inc.php') which will also read out available session variables:
  30. start_session(true);
  31. // --------------------------------------------------------------------
  32. // Initialize preferred display language:
  33. // (note that 'locales.inc.php' has to be included *after* the call to the 'start_session()' function)
  34. include 'includes/locales.inc.php'; // include the locales
  35. // --------------------------------------------------------------------
  36. // If there's no stored message available:
  37. if (!isset($_SESSION['HeaderString']))
  38. $HeaderString = $loc["SearchSQL"].":"; // Provide the default message
  39. else
  40. {
  41. $HeaderString = $_SESSION['HeaderString']; // extract 'HeaderString' session variable (only necessary if register globals is OFF!)
  42. // Note: though we clear the session variable, the current message is still available to this script via '$HeaderString':
  43. deleteSessionVariable("HeaderString"); // function 'deleteSessionVariable()' is defined in 'include.inc.php'
  44. }
  45. // Extract the view type requested by the user (either 'Mobile', 'Print', 'Web' or ''):
  46. // ('' will produce the default 'Web' output style)
  47. if (isset($_REQUEST['viewType']))
  48. $viewType = $_REQUEST['viewType'];
  49. else
  50. $viewType = "";
  51. // Check if the script was called with parameters (like: 'sql_search.php?customQuery=1&sqlQuery=...&showQuery=...&showLinks=...')
  52. // If so, the parameter 'customQuery=1' will be set:
  53. if (isset($_REQUEST['customQuery']))
  54. $customQuery = $_REQUEST['customQuery']; // accept any previous SQL queries
  55. else
  56. $customQuery = "0";
  57. if ($customQuery == "1") // the script was called with parameters
  58. {
  59. $sqlQuery = $_REQUEST['sqlQuery']; // accept any previous SQL queries
  60. $sqlQuery = stripSlashesIfMagicQuotes($sqlQuery); // function 'stripSlashesIfMagicQuotes()' is defined in 'include.inc.php'
  61. $showQuery = $_REQUEST['showQuery']; // extract the $showQuery parameter
  62. if ("$showQuery" == "1")
  63. $checkQuery = " checked";
  64. else
  65. $checkQuery = "";
  66. $showLinks = $_REQUEST['showLinks']; // extract the $showLinks parameter
  67. if ("$showLinks" == "1")
  68. $checkLinks = " checked";
  69. else
  70. $checkLinks = "";
  71. $showRows = $_REQUEST['showRows']; // extract the $showRows parameter
  72. $displayType = $_REQUEST['submit']; // extract the type of display requested by the user (either 'Display', 'Cite', 'List' or '')
  73. $citeStyle = $_REQUEST['citeStyle']; // get the cite style chosen by the user (only occurs in 'extract.php' form and in query result lists)
  74. $citeOrder = $_REQUEST['citeOrder']; // get the citation sort order chosen by the user (only occurs in 'extract.php' form and in query result lists)
  75. }
  76. else // if there was no previous SQL query provide the default one:
  77. {
  78. // default SQL query:
  79. // TODO: build the complete SQL query using functions 'buildFROMclause()' and 'buildORDERclause()'
  80. $sqlQuery = buildSELECTclause("", "", "", false, false); // function 'buildSELECTclause()' is defined in 'include.inc.php'
  81. if (isset($_SESSION['loginEmail']))
  82. $sqlQuery .= " FROM $tableRefs WHERE location RLIKE \"" . $loginEmail . "\" ORDER BY year DESC, author"; // '$loginEmail' is defined in function 'start_session()' (in 'include.inc.php')
  83. else
  84. $sqlQuery .= " FROM $tableRefs WHERE year &gt; 2001 ORDER BY year DESC, author";
  85. $checkQuery = "";
  86. $checkLinks = " checked";
  87. // Get the default number of records per page preferred by the current user:
  88. $showRows = $_SESSION['userRecordsPerPage'];
  89. $displayType = ""; // ('' will produce the default view)
  90. $citeStyle = "";
  91. $citeOrder = "";
  92. }
  93. // Show the login status:
  94. showLogin(); // (function 'showLogin()' is defined in 'include.inc.php')
  95. // (2a) Display header:
  96. // call the 'displayHTMLhead()' and 'showPageHeader()' functions (which are defined in 'header.inc.php'):
  97. displayHTMLhead(encodeHTML($officialDatabaseName) . " -- " . $loc["SQLSearch"], "index,follow", "Search the " . encodeHTML($officialDatabaseName), "", false, "", $viewType, array());
  98. showPageHeader($HeaderString);
  99. // (2b) Start <form> and <table> holding the form elements:
  100. ?>
  101. <form action="search.php" method="GET">
  102. <input type="hidden" name="formType" value="sqlSearch">
  103. <input type="hidden" name="submit" value="<?php echo $displayType; ?>">
  104. <input type="hidden" name="citeStyle" value="<?php echo rawurlencode($citeStyle); ?>">
  105. <input type="hidden" name="citeOrder" value="<?php echo $citeOrder; ?>">
  106. <table align="center" border="0" cellpadding="0" cellspacing="10" width="95%" summary="This table holds the search form">
  107. <tr>
  108. <td width="58" valign="top"><b><?php echo $loc["SQLQuery"]; ?>:</b></td>
  109. <td width="10">&nbsp;</td>
  110. <td colspan="2">
  111. <textarea name="sqlQuery" rows="6" cols="60"><?php echo $sqlQuery; ?></textarea>
  112. </td>
  113. </tr>
  114. <tr>
  115. <td valign="top"><b><?php echo $loc["DisplayOptions"]; ?>:</b></td>
  116. <td>&nbsp;</td>
  117. <td width="205" valign="top">
  118. <input type="checkbox" name="showLinks" value="1"<?php echo $checkLinks; ?>>&nbsp;&nbsp;&nbsp;<?php echo $loc["ShowLinks"]; ?>
  119. </td>
  120. <td valign="top">
  121. <?php echo $loc["ShowRecordsPerPage_Prefix"]; ?>&nbsp;&nbsp;&nbsp;<input type="text" name="showRows" value="<?php echo $showRows; ?>" size="4" title="<?php echo $loc["DescriptionShowRecordsPerPage"]; ?>">&nbsp;&nbsp;&nbsp;<?php echo $loc["ShowRecordsPerPage_Suffix"]; ?>
  122. </td>
  123. </tr>
  124. <tr>
  125. <td>&nbsp;</td>
  126. <td>&nbsp;</td>
  127. <td valign="top">
  128. <input type="checkbox" name="showQuery" value="1"<?php echo $checkQuery; ?>>&nbsp;&nbsp;&nbsp;<?php echo $loc["DisplaySQLquery"]; ?>
  129. </td>
  130. <td valign="top">
  131. <?php echo $loc["ViewType"]; ?>:&nbsp;&nbsp;
  132. <select name="viewType">
  133. <option value="Web"><?php echo $loc["web"]; ?></option>
  134. <option value="Print"><?php echo $loc["print"]; ?></option>
  135. <option value="Mobile"><?php echo $loc["mobile"]; ?></option>
  136. </select>
  137. </td>
  138. </tr>
  139. <tr>
  140. <td>&nbsp;</td>
  141. <td>&nbsp;</td><?php
  142. if (isset($_SESSION['user_permissions']) AND preg_match("/allow_sql_search/", $_SESSION['user_permissions'])) // if the 'user_permissions' session variable contains 'allow_sql_search'...
  143. // adjust the title string for the search button
  144. {
  145. $sqlSearchButtonLock = "";
  146. $sqlSearchTitle = $loc["SearchVerbatim"];
  147. }
  148. else // Note, that disabling the submit button is just a cosmetic thing -- the user can still submit the form by pressing enter or by building the correct URL from scratch!
  149. {
  150. $sqlSearchButtonLock = " disabled";
  151. $sqlSearchTitle = $loc["NoPermission"] . $loc["NoPermission_ForSQL"];
  152. }
  153. ?>
  154. <td colspan="2">
  155. <br>
  156. <input type="submit" value="<?php echo $loc["Search"]; ?>" title="<?php echo $sqlSearchTitle; ?>"<?php echo $sqlSearchButtonLock; ?>>
  157. </td>
  158. </tr>
  159. <tr>
  160. <td align="center" colspan="4">&nbsp;</td>
  161. </tr>
  162. <tr>
  163. <td valign="top"><b><?php echo $loc["Examples"]; ?>:</b></td>
  164. <td>&nbsp;</td>
  165. <td colspan="2">
  166. <code>SELECT author, title, year, publication FROM <?php echo $tableRefs; ?> WHERE publication = "Polar Biology" AND author RLIKE "Legendre|Ambrose" ORDER BY year DESC, author</code>
  167. </td>
  168. </tr>
  169. <tr>
  170. <td valign="top">&nbsp;</td>
  171. <td>&nbsp;</td>
  172. <td colspan="2">
  173. <code>SELECT serial, author, title, year, publication, volume FROM <?php echo $tableRefs; ?> ORDER BY serial DESC LIMIT 10</code>
  174. </td>
  175. </tr>
  176. <tr>
  177. <td valign="top"><b><?php echo $loc["Help"]; ?>:</b></td>
  178. <td>&nbsp;</td>
  179. <td colspan="2">
  180. <?php echo $loc["MySQL-Info"]; ?>
  181. </td>
  182. </tr>
  183. </table>
  184. </form><?php
  185. // --------------------------------------------------------------------
  186. // DISPLAY THE HTML FOOTER:
  187. // call the 'showPageFooter()' and 'displayHTMLfoot()' functions (which are defined in 'footer.inc.php')
  188. showPageFooter($HeaderString);
  189. displayHTMLfoot();
  190. // --------------------------------------------------------------------
  191. ?>