function get_tag_contents($html, $tag, $start_str, $tag_pos = 0, $length = 0) { if (!empty($start_str)) { $html = end(explode($start_str, $html)); } $tag_pos = stripos($html, '<' . $tag, $tag_pos); $length = stripos($html, '</' . $tag, $length); //echo "$tag_pos|$length<br />"; if (false !== $tag_pos && $tag_pos < $length) { return get_tag_contents($html, $tag, '', $tag_pos + 1, $length + 1); } else { return substr($html, 0, $length); } }
function thc_filelist($location) { $newline = "\n"; // deny them higher directories or even the current directory if (substr($location, 0, 1) == "." || strlen($location) == 0) { return; } // they tried to get clever and go to a higher directory $remote = substr($location, 0, 4) == "http"; if (!$remote) { $lowest = getcwd(); // make sure they can't go lower than the script $dir = $lowest . $location; if (!is_dir($dir)) { // echo "<h1>" . $dir . " - Not a Valid Directory </h1>"; return; } else { if ($handle = opendir($dir)) { $filestring = ""; while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $filestring .= $location . "/" . $file . $newline; } } closedir($handle); $filearray = explode($newline, $filestring); return $filearray; } } } else { $result = ""; $handle = fopen($location, "r"); while (!feof($handle)) { $result .= fgets($handle, 4096); } fclose($handle); if (preg_match("/ListBucketResult/", $result)) { $filearray = get_tag_contents($result, "Key"); $filestring = ""; foreach ($filearray as $value) { $filestring .= $location . "/" . $value . $newline; } $filearray = explode($newline, trim($filestring, $newline)); return $filearray; } else { // they are just doing a directory to a remote server (without an index.html file, of course) $lines = explode("\n", $result); $filestring = ""; foreach ($lines as $value) { // $pattern = '/[^\w][a-z0-9- ]+(\.[a-z0-9]{3})[^\w]/'; $pattern = '/[a-z0-9_ -]+\\.([a-z0-9]{3})/'; if (preg_match($pattern, $value, $matches)) { $filestring .= $location . "/" . $matches[0] . $newline; } } $filearray = explode($newline, trim($filestring, $newline)); return $filearray; } } }