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.

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