• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

Suche erlaubt keine .php Files

_R_A_L_F_

Mitglied
Hallo,

über ein PHP Script möchte ich folgende Dateitypen erlauben durchsucht zu werden: html, htm und php

Problem, html und htm funktioniert, php Dateien werden aber nicht durchsucht.

Hier mal der vorhandene Code:
PHP:
$search_in = array('html', 'htm', 'php');  //Erlaubte Dateitypen

und die Funktion dazu
PHP:
function list_files($dir)
{
  global $recursive, $search_in;

  $result = array();
  if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
      while (($file = readdir($dh)) !== false) {
        if (!($file == '.' || $file == '..')) {
          $file = $dir . '/' . $file;
          if (is_dir($file) && $recursive == true && $file != './.' && $file != './..') {
            $result = array_merge($result, list_files($file));
          } else if (!is_dir($file)) {
            if (in_array(get_file_extension($file), $search_in)) {
              $result[] = $file;
            }           
          }
        }
      }
    }
  }
  return $result;
}

Muss statt "php" was anderes im Array stehen, damit dies auch funktioniert oder woran liegt das?

Danke schon mal
 
Danke schon mal für die Antwort.

Hier wäre der komplette Code der Search PHP Datei

PHP:
<?php
if (!isset($_GET['s'])) {
  die('GET nicht gesetzt!');
}

$highlight = true;//Highlight Resultat an/aus
$search_in = array('html', 'htm', 'php');//Erlaubte Dateitypen
$search_dir = '..';//Start Verzeichnis
$recursive = true;//Rekursiv suchen ja/nein
define('SIDE_CHARS', 15);
$file_count = 0;
$search_term = mb_strtolower($_GET['s'], 'UTF-8');

if ($search_term == "?s=") {
  $search_term = "";
}

$search_term = preg_replace('/^\/$/', '"/"', $search_term);
$search_term = preg_replace('/\+/', ' ', $search_term);
$search_term_length = strlen($search_term);
if (isset($_GET['liveCount'])){
  $search_live_count = $_GET['liveCount'];
}
$final_result = array();

$search_filter_init = $_GET['filter'];
$search_filter = preg_replace("/\*/", ".*", $search_filter_init);
$search_template = preg_replace('/\+/', ' ', $_GET['template']);
preg_match_all("/\#\{((?!title|href|token|count)[a-z]*)\}/", $search_template, $template_tokens);
$template_tokens = $template_tokens[1];

$files = list_files($search_dir);

foreach ($files as $file) {

  if (0 == filesize($file)) {
    continue;
  }

  if (!preg_match("/" . $search_filter . "/", $file)) {
    continue;
  }

  $contents = file_get_contents($file);
  preg_match("/\<title\>(.*)\<\/title\>/", $contents, $page_title); //Seiten Titel filtern
  if (preg_match("#\<body.*\>(.*)\<\/body\>#si", $contents, $body_content)) { //Inhalt nur zwischen <body></body> tags filtern
    $clean_content = strip_tags($body_content[0]); //entferne html tags
    $clean_content = preg_replace('/\s+/', ' ', $clean_content); //Entferne leerzeichen und co.

    $found = strpos_recursive(mb_strtolower($clean_content, 'UTF-8'), $search_term);

    $final_result[$file_count]['page_title'][] = $page_title[1];
    $final_result[$file_count]['file_name'][] = preg_replace("/^.{3}/", "\\1", $file);
  }

  for ($j = 0; $j < count($template_tokens); $j++) {
    if (preg_match("/\<meta\s+name=[\'|\"]" . $template_tokens[$j] . "[\'|\"]\s+content=[\'|\"](.*)[\'|\"]\>/", $contents, $res)) {
      $final_result[$file_count][$template_tokens[$j]] = $res[1];
    }
  }

  if ($found && !empty($found)) {
    for ($z = 0; $z < count($found[0]); $z++) {
      $pos = $found[0][$z][1];
      $side_chars = SIDE_CHARS;
      if ($pos < SIDE_CHARS) {
        $side_chars = $pos;
        if (isset($_GET['liveSearch']) and $_GET['liveSearch'] != "") {
          $pos_end = SIDE_CHARS + $search_term_length + 15;
        } else {
          $pos_end = SIDE_CHARS * 9 + $search_term_length;
        }
      } else {
        if (isset($_GET['liveSearch']) and $_GET['liveSearch'] != "") {
          $pos_end = SIDE_CHARS + $search_term_length + 15;
        } else {
          $pos_end = SIDE_CHARS * 9 + $search_term_length;
        }
      }

      $pos_start = $pos - $side_chars;
      $str = substr($clean_content, $pos_start, $pos_end);
      $result = preg_replace('#' . $search_term . '#ui', '<span class="search">\0</span>', $str);
      //$result = preg_replace('#'.$search_term.'#ui', '<span class="search">'.$search_term.'</span>', $str);
      $final_result[$file_count]['search_result'][] = $result;

    }
  } else {
    $final_result[$file_count]['search_result'][] = '';
  }
  $file_count++;
}

if ($file_count > 0) {

//Sortiere Ergebnis
  foreach ($final_result as $key => $row) {
    $search_result[$key] = $row['search_result'];
  }
  array_multisort($search_result, SORT_DESC, $final_result);
}

?>

  <div id="search-results">

    <?php if (count($final_result) > 0 and isset($_GET['liveSearch']) and $_GET['liveSearch'] != "") {
      echo "<div class='search-quick-result'>Suchergebnis</div>";
    } ?>

    <ol class="search_list">
      <?php
      $sum_of_results = 0;
      $match_count = 0;
      for ($i = 0; $i < count($final_result); $i++) {
        if (!empty($final_result[$i]['search_result'][0]) || $final_result[$i]['search_result'][0] !== '') {
          $match_count++;
          $sum_of_results += count($final_result[$i]['search_result']);
          if (isset($_GET['liveSearch']) and $_GET['liveSearch'] != "" and $i >= $search_live_count) {
          } else {
            ?>
            <li class="result-item">

              <?php
              $replacement = [$final_result[$i]['page_title'][0],
                  $final_result[$i]['file_name'][0],
                  $final_result[$i]['search_result'][0],
                  count($final_result[$i]['search_result'])
              ];
              $template = preg_replace(["/#{title}/","/#{href}/","/#{token}/","/#{count}/"],$replacement, $search_template);
              for ($k = 0; $k < count($template_tokens); $k++){
                if (isset($final_result[$i][$template_tokens[$k]])){
                  $template = preg_replace("/#{" . $template_tokens[$k] . "}/", $final_result[$i][$template_tokens[$k]], $template);
                }else{
                  $template = preg_replace("/#{" . $template_tokens[$k] . "}/", " ", $template);
                }
              }

              echo $template; ?>
            </li>
            <?php
          }
        }
      }

      if ($match_count == 0) {
        echo '<li><div class="search_error">Keine Ergebnisse für "<span class="search">' . $search_term . '</span>"<div/></li>';
      }
      ?>
      <?php
      if (isset($_GET['liveSearch']) and $_GET['liveSearch'] != "" and $match_count != 0) {
        ?>
        <li class="search_all">
          <a href='suchergebnisse.php?s=<?php echo $_GET['s']; ?>&amp;filter=<?php echo $search_filter_init; ?>'  class="search_submit">
            <?php
            echo "weitere ";
            echo $sum_of_results;
            echo $sum_of_results < 2 ? " Ergebnisse " : " anzeigen ";
            ?>
          </a>
        </li>
        <?php
      }
      ?>
    </ol>
  </div>

<?php
//Liste alle Dateien im Verzeichnis auf (und Unterverzeichnisse, falls on)
function list_files($dir)
{
  global $recursive, $search_in;

  $result = array();
  if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
      while (($file = readdir($dh)) !== false) {
        if (!($file == '.' || $file == '..')) {
          $file = $dir . '/' . $file;
          if (is_dir($file) && $recursive == true && $file != './.' && $file != './..') {
            $result = array_merge($result, list_files($file));
          } else if (!is_dir($file)) {
            if (in_array(get_file_extension($file), $search_in)) {
              $result[] = $file;
            }           
          }
        }
      }
    }
  }
  return $result;
}

//Dateiendung erhalten
function get_file_extension($filename)
{
  $result = '';
  $parts = explode('.', $filename);
  if (is_array($parts) && count($parts) > 1) {
    $result = end($parts);
  }
  return $result;
}

function strpos_recursive($haystack, $needle, $offset = 0, &$results = array())
{
  $offset = stripos($haystack, $needle, $offset);
  if ($offset === false) {
    return $results;
  } else {
    $pattern = '/' . $needle . '/ui';
    preg_match_all($pattern, $haystack, $results, PREG_OFFSET_CAPTURE);
    return $results;
  }
}

?>

Schon komisch.
 
Zurück
Oben