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.

149 lines
8.6 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: ./rss.php
  13. // Repository: $HeadURL: file:///svn/p/refbase/code/branches/bleeding-edge/rss.php $
  14. // Author(s): Matthias Steffens <mailto:refbase@extracts.de>
  15. //
  16. // Created: 25-Sep-04, 12:10
  17. // Modified: $Date: 2017-04-13 02:00:18 +0000 (Thu, 13 Apr 2017) $
  18. // $Author: karnesky $
  19. // $Revision: 1416 $
  20. // This script will generate a dynamic RSS feed for the current query.
  21. // Usage: Perform your query until you've got the desired results. Then, copy the "RSS" link in the header
  22. // message of any search results page and use this URL as feed URL when subscribing within your Newsreader.
  23. // TODO: - support 'startRecord' parameter
  24. // - support 'citeOrder' parameter to allow for sort options other than the current default
  25. // (which equals '$citeOrder="creation-date"' in 'show.php')
  26. // NOTE: As an alternative to fixing/improving 'rss.php', it may be desirable to add "RSS XML" as a proper
  27. // export format, and let 'show.php' generate the RSS output (this would effectively deprecate 'rss.php');
  28. // However, the drawback would be longer/uglier RSS URLs...
  29. // Incorporate some include files:
  30. include 'initialize/db.inc.php'; // 'db.inc.php' is included to hide username and password
  31. include 'includes/include.inc.php'; // include common functions
  32. include 'includes/cite.inc.php'; // include citation functions
  33. include 'initialize/ini.inc.php'; // include common variables
  34. // --------------------------------------------------------------------
  35. // START A SESSION:
  36. // call the 'start_session()' function (from 'include.inc.php') which will also read out available session variables:
  37. start_session(true);
  38. // --------------------------------------------------------------------
  39. // Initialize preferred display language:
  40. // (note that 'locales.inc.php' has to be included *after* the call to the 'start_session()' function)
  41. include 'includes/locales.inc.php'; // include the locales
  42. // --------------------------------------------------------------------
  43. // Extract any parameters passed to the script:
  44. if (isset($_REQUEST['where']))
  45. $queryWhereClause = $_REQUEST['where']; // get the WHERE clause that was passed within the link
  46. else
  47. $queryWhereClause = "";
  48. if (isset($_REQUEST['showRows']) AND preg_match("/^[1-9]+[0-9]*$/", $_REQUEST['showRows'])) // contains the desired number of search results (OpenSearch equivalent: '{count}')
  49. $showRows = $_REQUEST['showRows'];
  50. else
  51. $showRows = $_SESSION['userRecordsPerPage']; // get the default number of records per page preferred by the current user
  52. // NOTE: while we read out the 'startRecord' parameter, it isn't yet supported by 'rss.php'!
  53. if (isset($_REQUEST['startRecord'])) // contains the offset of the first search result, starting with one (OpenSearch equivalent: '{startIndex}')
  54. $rowOffset = ($_REQUEST['startRecord']) - 1; // first row number in a MySQL result set is 0 (not 1)
  55. else
  56. $rowOffset = ""; // if no value to the 'startRecord' parameter is given, we'll output records starting with the first record in the result set
  57. if (isset($_REQUEST['recordSchema'])) // contains the desired response format; currently, 'rss.php' will only recognize 'rss' (outputs RSS 2.0), future versions may also allow for 'atom'
  58. $recordSchema = $_REQUEST['recordSchema'];
  59. else
  60. $recordSchema = "rss"; // if no particular response format was requested we'll output found results as RSS 2.0
  61. // Check the correct parameters have been passed:
  62. if (empty($queryWhereClause)) // if 'rss.php' was called without the 'where' parameter:
  63. {
  64. // return an appropriate error message:
  65. $HeaderString = returnMsg($loc["Warning_IncorrectOrMissingParams"] . " '" . scriptURL() . "'!", "warning", "strong", "HeaderString"); // functions 'returnMsg()' and 'scriptURL()' are defined in 'include.inc.php'
  66. // Redirect the browser back to the calling page:
  67. header("Location: " . $referer); // variable '$referer' is globally defined in function 'start_session()' in 'include.inc.php'
  68. exit; // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> !EXIT! <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  69. }
  70. else
  71. {
  72. $sanitizedWhereClause = extractWHEREclause(" WHERE " . $queryWhereClause); // attempt to sanitize custom WHERE clause from SQL injection attacks (function 'extractWHEREclause()' is defined in 'include.inc.php')
  73. }
  74. // --------------------------------------------------------------------
  75. // If we made it here, then the script was called with all required parameters (which, currently, is just the 'where' parameter :)
  76. // CONSTRUCT SQL QUERY:
  77. // Note: the 'verifySQLQuery()' function that gets called below will add the user specific fields to the 'SELECT' clause and the
  78. // 'LEFT JOIN...' part to the 'FROM' clause of the SQL query if a user is logged in. It will also add 'orig_record', 'serial', 'file', 'url', 'doi', 'isbn' & 'type' columns
  79. // as required. Therefore it's sufficient to provide just the plain SQL query here:
  80. $sqlQuery = buildSELECTclause("RSS", "1", "", false, false); // function 'buildSELECTclause()' is defined in 'include.inc.php'
  81. $sqlQuery .= " FROM $tableRefs WHERE " . $sanitizedWhereClause; // add FROM clause and the specified WHERE clause
  82. $sqlQuery .= " ORDER BY created_date DESC, created_time DESC, modified_date DESC, modified_time DESC, serial DESC"; // sort records such that newly added/edited records get listed top of the list
  83. // since a malicious user could change the 'where' parameter manually to gain access to user-specific data of other users, we'll run the SQL query thru the 'verifySQLQuery()' function:
  84. // (this function does also add/remove user-specific query code as required and will fix problems with escape sequences within the SQL query)
  85. $query = verifySQLQuery($sqlQuery, "", "RSS", "1"); // function 'verifySQLQuery()' is defined in 'include.inc.php'
  86. // the 'verifySQLQuery()' function will save an error message to the 'HeaderString' session variable if something went wrong (e.g., if a user who's NOT logged in tries to query user specific fields)
  87. if (isset($_SESSION['HeaderString'])) // if there's a 'HeaderString' session variable
  88. {
  89. header("Location: index.php"); // redirect to main page ('index.php') which will display the error message stored within the 'HeaderString' session variable
  90. exit; // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> !EXIT! <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  91. }
  92. // --------------------------------------------------------------------
  93. // (1) OPEN CONNECTION, (2) SELECT DATABASE
  94. connectToMySQLDatabase(); // function 'connectToMySQLDatabase()' is defined in 'include.inc.php'
  95. // --------------------------------------------------------------------
  96. // (3) RUN the query on the database through the connection:
  97. $result = queryMySQLDatabase($query); // function 'queryMySQLDatabase()' is defined in 'include.inc.php'
  98. // find out how many rows are available:
  99. $rowsFound = @ mysqli_num_rows($result);
  100. // construct a meaningful channel description based on the specified 'WHERE' clause:
  101. $rssChannelDescription = "Displays all newly added records where " . explainSQLQuery($sanitizedWhereClause) . "."; // function 'explainSQLQuery()' is defined in 'include.inc.php'
  102. // Generate RSS XML data from the result set (upto the limit given in '$showRows'):
  103. $rssFeed = generateRSS($result, $showRows, $rssChannelDescription); // function 'generateRSS()' is defined in 'include.inc.php'
  104. // --------------------------------------------------------------------
  105. // (4) DISPLAY search results as RSS feed:
  106. // set mimetype to 'application/rss+xml' and character encoding to the one given in '$contentTypeCharset' (which is defined in 'ini.inc.php'):
  107. setHeaderContentType("application/rss+xml", $contentTypeCharset); // function 'setHeaderContentType()' is defined in 'include.inc.php'
  108. echo $rssFeed;
  109. // --------------------------------------------------------------------
  110. // (5) CLOSE the database connection:
  111. disconnectFromMySQLDatabase(); // function 'disconnectFromMySQLDatabase()' is defined in 'include.inc.php'
  112. // --------------------------------------------------------------------
  113. ?>