misc.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php namespace HashOver;
  2. // Copyright (C) 2010-2017 Jacob Barkdull
  3. // This file is part of HashOver.
  4. //
  5. // HashOver is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as
  7. // published by the Free Software Foundation, either version 3 of the
  8. // License, or (at your option) any later version.
  9. //
  10. // HashOver is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with HashOver. If not, see <http://www.gnu.org/licenses/>.
  17. // Display source code
  18. if (basename ($_SERVER['PHP_SELF']) === basename (__FILE__)) {
  19. if (isset ($_GET['source'])) {
  20. header ('Content-type: text/plain; charset=UTF-8');
  21. exit (file_get_contents (basename (__FILE__)));
  22. } else {
  23. exit ('<b>HashOver</b>: This is a class file.');
  24. }
  25. }
  26. class Misc
  27. {
  28. public $mode;
  29. // XSS-unsafe characters to search for
  30. protected $searchXSS = array (
  31. '&',
  32. '<',
  33. '>',
  34. '"',
  35. "'",
  36. '/',
  37. '\\'
  38. );
  39. // XSS-safe replacement character entities
  40. protected $replaceXSS = array (
  41. '&amp;',
  42. '&lt;',
  43. '&gt;',
  44. '&quot;',
  45. '&#x27;',
  46. '&#x2F;',
  47. '&#92;'
  48. );
  49. public function __construct ($mode)
  50. {
  51. $this->mode = $mode;
  52. }
  53. // Make a string XSS-safe
  54. public function makeXSSsafe ($string)
  55. {
  56. // Return cookie value without harmful characters
  57. return str_replace ($this->searchXSS, $this->replaceXSS, $string);
  58. }
  59. // JavaScript-specific escaping
  60. public function jsEscape ($string, $characters = "\\'")
  61. {
  62. return addcslashes ($string, $characters);
  63. }
  64. // Returns error in HTML paragraph
  65. public function displayError ($error = 'Something went wrong!')
  66. {
  67. $xss_safe = $this->makeXSSsafe ($error);
  68. $data = array ();
  69. switch ($this->mode) {
  70. // Minimal JavaScript to display error message on page
  71. case 'javascript': {
  72. $data[] = 'var hashover = document.getElementById (\'hashover\') || document.body;';
  73. $data[] = 'var error = \'<p><b>HashOver</b>: ' . $xss_safe . '</p>\';' . PHP_EOL;
  74. $data[] = 'hashover.innerHTML += error;';
  75. break;
  76. }
  77. // RSS XML to indicate error
  78. case 'rss': {
  79. $data[] = '<?xml version="1.0" encoding="UTF-8"?>';
  80. $data[] = '<error>HashOver: ' . $xss_safe . '</error>';
  81. break;
  82. }
  83. // JSON to indicate error
  84. case 'json': {
  85. $data[] = json_encode (array (
  86. 'message' => $error,
  87. 'type' => 'error'
  88. ));
  89. break;
  90. }
  91. // Default just return the error message
  92. default: {
  93. $data[] = 'HashOver: ' . $error;
  94. break;
  95. }
  96. }
  97. echo implode (PHP_EOL, $data);
  98. }
  99. }