1
0

search.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * dolphin. Collection of useful PHP skeletons.
  4. * Copyright (C) 2016 Johannes 'Banana' Keßler
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  8. *
  9. * You should have received a copy of the
  10. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  11. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  12. */
  13. mb_http_output('UTF-8');
  14. mb_internal_encoding('UTF-8');
  15. ini_set('error_reporting',-1); // E_ALL & E_STRICT
  16. if(ini_get("magic_quotes_gpc") == 1) {
  17. die('Magic quotes is set to "on", and system is not able to change it. Please update Your php.ini file');
  18. }
  19. ## check request
  20. $_urlToParse = filter_var($_SERVER['QUERY_STRING'],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
  21. if(!empty($_urlToParse)) {
  22. # see http://de2.php.net/manual/en/regexp.reference.unicode.php
  23. if(preg_match('/[\p{C}\p{M}\p{Sc}\p{Sk}\p{So}\p{Zl}\p{Zp}]/u',$_urlToParse) === 1) {
  24. die('Malformed request. Make sure you know what you are doing.');
  25. }
  26. }
  27. # time settings
  28. date_default_timezone_set('Europe/Berlin');
  29. define('DEBUG',false);
  30. ## set the error reporting
  31. ini_set('log_errors',true);
  32. ini_set('error_log','error.log');
  33. if(DEBUG === true) {
  34. ini_set('display_errors',true);
  35. }
  36. else {
  37. ini_set('display_errors',false);
  38. }
  39. $requestString = false;
  40. $searchStr = false;
  41. $searchResult = false;
  42. $searchCount = 0;
  43. $tagLink = false;
  44. $catLink = false;
  45. if(isset($_POST['query'])) {
  46. $searchStr = trim($_POST['query']);
  47. $searchStr = strtolower($searchStr);
  48. $searchStr = preg_replace("/[^\p{L}\p{N}\p{Zs}]/u","",$searchStr);
  49. # kinda full txt search
  50. if(!empty($searchStr)) {
  51. $files = glob('20*/*/*/*.html');
  52. $limit = 0;
  53. if(!empty($files)) {
  54. foreach($files as $file) {
  55. if($limit > 30) break;
  56. $stream = new SplFileObject($file);
  57. $grepped = new RegexIterator($stream, '/'.$searchStr.'/i');
  58. foreach($grepped as $found) {
  59. $_text = $file;
  60. # this search for a headline tag and the result will be used as
  61. # the result text in the search results
  62. $headlines = new RegexIterator($stream,"/<h2>([^<]+)<\/h2>/");
  63. foreach($headlines as $headline) {
  64. $_text = trim($headline);
  65. $_text = strip_tags($_text);
  66. break;
  67. }
  68. $searchResult[] = array(
  69. 'href' => 'https://url.to/'.$file,
  70. 'text' => $_text
  71. );
  72. $limit++;
  73. #var_dump($found);
  74. #var_dump($file);
  75. #var_dump($searchResult);
  76. #exit();
  77. unset($headlines);
  78. unset($_text);
  79. break;
  80. }
  81. unset($stream);
  82. unset($grepped);
  83. }
  84. $searchCount = count($searchResult);
  85. }
  86. }
  87. # is it a tag?
  88. $tagDir = 'tags/'.$searchStr;
  89. if(file_exists($tagDir)) {
  90. $tagLink = $searchStr;
  91. }
  92. # is it a category?
  93. $topicsDir = 'topics/'.$searchStr;
  94. if(file_exists($topicsDir)) {
  95. $catLink = $searchStr;
  96. }
  97. }
  98. header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
  99. header("Cache-Control: post-check=0, pre-check=0", false);
  100. header("Pragma: no-cache");
  101. ?>
  102. <!DOCTYPE HTML>
  103. <html>
  104. <head>
  105. <title>Suche nach: <?php echo $searchStr; ?></title>
  106. <meta charset="utf-8" />
  107. <meta name="robots" content="noindex,nofollow" />
  108. <meta name="viewport" content="width=device-width, initial-scale=1" />
  109. </head>
  110. <body>
  111. <div >
  112. <article >
  113. <header>
  114. <h2>Suche</h2>
  115. <p>Gesucht: <?php echo $searchStr; ?> | Gefunden: <?php echo $searchCount; ?></p>
  116. </header>
  117. <?php if(!empty($tagLink)) { ?>
  118. <p>Tag: <a href="https://www.url.to/tags/<?php echo $tagLink; ?>"><?php echo $tagLink; ?></a></p>
  119. <?php } ?>
  120. <?php if(!empty($catLink)) { ?>
  121. <p>Kategorie: <a href="https://www.url.to/topics/<?php echo $catLink; ?>"><?php echo $catLink; ?></a></p>
  122. <?php } ?>
  123. <?php if(!empty($searchResult)) { ?>
  124. <ul>
  125. <?php
  126. foreach($searchResult as $result) {
  127. echo "<li><a href='".$result['href']."'>".$result['text']."</a></li>\n";
  128. }
  129. ?>
  130. </ul>
  131. <?php } else { ?>
  132. <div>
  133. <p>Leider nichts gefunden.</p>
  134. </div>
  135. <?php } ?>
  136. </article>
  137. </div>
  138. </body>
  139. </html>