1<?php 2# Copyright (C) 2010 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16############################################################################### 17 18# Lists the content of the LayoutTests directory 19# 20# Usage: 21# get_layout_tests_dir_contents.php?path=PATH&recurse=RECURSE&separator=SEPARATOR&mode=MODE 22# where 23# PATH - relative path in the LayoutTests dir 24# RECURSE = [true|false] (defaults to true) 25# SEPARATOR = a string separating paths in result (defaults to \n) 26# MODE = [folders|files] (defaults to files) - if 'folders' then lists only folders, 27# if 'files' then only files 28 29 # The server document root is LayoutTests/http/tests. See run_apache2.py. 30 $rootDir = realpath($_SERVER['DOCUMENT_ROOT'] . '..' . DIRECTORY_SEPARATOR . '..'); 31 32 function getAbsolutePath($relPath) { 33 global $rootDir; 34 return $rootDir . DIRECTORY_SEPARATOR . $relPath; 35 } 36 37 function getFilesAsArray($relPath) { 38 return array_slice(scandir(getAbsolutePath($relPath)), 2); 39 } 40 41 function isIgnored($basename) { 42 return substr($basename, 0, 1) == '.'; 43 } 44 45 function getAllFilesUnderAsArray($relPath, $recurse, $mode) { 46 $files = getFilesAsArray($relPath); 47 $result = array(); 48 49 foreach($files as $i => $value) { 50 if (isIgnored($value)) { 51 continue; 52 } 53 if ($relPath == '') { 54 $filePath = $value; 55 } else { 56 $filePath = $relPath . DIRECTORY_SEPARATOR . $value; 57 } 58 59 if (is_dir(getAbsolutePath($filePath))) { 60 if ($mode == 'folders') { 61 $result = array_merge($result, (array)$filePath); 62 } 63 if ($recurse) { 64 $result = array_merge($result, getAllFilesUnderAsArray($filePath, $recurse, $mode)); 65 } 66 } else if ($mode == 'files') { 67 $result = array_merge($result, (array)$filePath); 68 } 69 } 70 71 return $result; 72 } 73 74 function main() { 75 global $rootDir; 76 77 if (!isset($_GET['separator'])) { 78 $separator = "\n"; 79 } else { 80 $separator = $_GET['separator']; 81 } 82 83 $recurse = (strtolower($_GET['recurse']) != 'false'); 84 85 if (strtolower($_GET['mode']) == 'folders') { 86 $mode = 'folders'; 87 } else { 88 $mode = 'files'; 89 } 90 91 # Very primitive check if path tries to go above DOCUMENT_ROOT or is absolute 92 $path = $_GET['path']; 93 if (strpos($path, "..") !== False || 94 substr($path, 0, 1) == DIRECTORY_SEPARATOR) { 95 return; 96 } 97 98 # If we don't want realpath to append any prefixes we need to pass it an absolute path 99 $relPath = substr(realpath(getAbsolutePath($path)), strlen($rootDir) + 1); 100 101 # If the path is not found, return nothing. 102 if ($path !== "" && $relPath == "") 103 return; 104 105 # If there is an error of some sort it will be output as a part of the answer! 106 foreach (getAllFilesUnderAsArray($relPath, $recurse, $mode) as $i => $value) { 107 echo "$value$separator"; 108 } 109 } 110 111 main(); 112?> 113