1 |
<?php |
2 |
/** |
3 |
* Mageia build-system quick status report script. |
4 |
* List log files related to $_GET['k'] build path. |
5 |
* |
6 |
* @copyright Copyright (C) 2012 Mageia.Org |
7 |
* |
8 |
* @author Romain d'Alverny |
9 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2 |
10 |
* |
11 |
* This program is free software; you can redistribute it and/or modify it |
12 |
* under the terms of the GNU General Public License aspublished by the |
13 |
* Free Software Foundation; either version 2 of the License, or (at your |
14 |
* option) any later version. |
15 |
*/ |
16 |
|
17 |
$key = isset($_GET['k']) ? trim(strip_tags(html_entity_decode($_GET['k']))) : null; |
18 |
|
19 |
if (is_null($key)) { |
20 |
die('.'); |
21 |
} |
22 |
|
23 |
require 'conf.php'; |
24 |
|
25 |
// FIXME this expects to remove /uploads from $key. Could be different in the future. |
26 |
$key = substr($key, 8); |
27 |
$path = realpath($upload_dir . $key); |
28 |
$job = explode('/', $key); |
29 |
$job = end($job); |
30 |
|
31 |
if (false !== strpos($key, 'rejected')) { |
32 |
$valid = file_exists($path); |
33 |
$type = 'rejected'; |
34 |
$job = str_replace('.youri', '', $job); |
35 |
$path = str_replace('.youri', '', $path); |
36 |
} else { |
37 |
$valid = is_dir($path); |
38 |
$type = 'regular'; |
39 |
} |
40 |
|
41 |
if (!$valid) { |
42 |
header('Status: 404 Not Found'); |
43 |
header('HTTP/1.0 404 Not Found'); |
44 |
die('Sorry, not found'); |
45 |
} |
46 |
|
47 |
$list = array(); |
48 |
|
49 |
if ($type == 'rejected') { |
50 |
|
51 |
$list = glob($path . '*'); |
52 |
|
53 |
} else { |
54 |
|
55 |
$list = glob_recursive_tree($path . '/*'); |
56 |
|
57 |
$others = array( |
58 |
'.youri', |
59 |
'_i586.done', |
60 |
'_x86_64.done' |
61 |
); |
62 |
|
63 |
foreach ($others as $suffix) { |
64 |
$f = $path . $suffix; |
65 |
if (file_exists($f)) { |
66 |
$list[] = $f; |
67 |
} |
68 |
} |
69 |
} |
70 |
|
71 |
echo sprintf('<h4>%s</h4>', $job), |
72 |
print_list($list); |
73 |
|
74 |
// lib code below. |
75 |
|
76 |
/** |
77 |
* Format size in human-readable format. |
78 |
* |
79 |
* @param integer $a_bytes size in bytes |
80 |
* |
81 |
* @return string |
82 |
* |
83 |
* @author yatsynych |
84 |
* @link http://www.php.net/manual/fr/function.filesize.php#106935 |
85 |
*/ |
86 |
function _format_bytes($a_bytes) |
87 |
{ |
88 |
if ($a_bytes < 1024) { |
89 |
return $a_bytes .' B'; |
90 |
} elseif ($a_bytes < 1048576) { |
91 |
return round($a_bytes / 1024, 2) .' KiB'; |
92 |
} elseif ($a_bytes < 1073741824) { |
93 |
return round($a_bytes / 1048576, 2) . ' MiB'; |
94 |
} elseif ($a_bytes < 1099511627776) { |
95 |
return round($a_bytes / 1073741824, 2) . ' GiB'; |
96 |
} elseif ($a_bytes < 1125899906842624) { |
97 |
return round($a_bytes / 1099511627776, 2) .' TiB'; |
98 |
} elseif ($a_bytes < 1152921504606846976) { |
99 |
return round($a_bytes / 1125899906842624, 2) .' PiB'; |
100 |
} elseif ($a_bytes < 1180591620717411303424) { |
101 |
return round($a_bytes / 1152921504606846976, 2) .' EiB'; |
102 |
} elseif ($a_bytes < 1208925819614629174706176) { |
103 |
return round($a_bytes / 1180591620717411303424, 2) .' ZiB'; |
104 |
} else { |
105 |
return round($a_bytes / 1208925819614629174706176, 2) .' YiB'; |
106 |
} |
107 |
} |
108 |
|
109 |
/** |
110 |
* @param string $pattern |
111 |
* @param integer $flags |
112 |
* |
113 |
* @return array |
114 |
* |
115 |
* @author Mike |
116 |
* @link http://www.php.net/manual/fr/function.glob.php#106595 |
117 |
* |
118 |
* Does not support flag GLOB_BRACE |
119 |
* |
120 |
*/ |
121 |
function glob_recursive($pattern, $flags = 0) |
122 |
{ |
123 |
$files = glob($pattern, $flags); |
124 |
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) { |
125 |
$files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags)); |
126 |
} |
127 |
|
128 |
return $files; |
129 |
} |
130 |
|
131 |
function glob_recursive_tree($pattern, $flags = 0) |
132 |
{ |
133 |
$files = glob($pattern, $flags); |
134 |
foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) { |
135 |
$dirtop = explode('/', $dir); |
136 |
$files[end($dirtop)] = glob_recursive_tree($dir . '/' . basename($pattern), $flags); |
137 |
} |
138 |
|
139 |
return $files; |
140 |
} |
141 |
|
142 |
function print_list($list) |
143 |
{ |
144 |
global $upload_dir, $path; |
145 |
|
146 |
$l = array(); |
147 |
foreach ($list as $f) { |
148 |
if (!is_string($f)) { |
149 |
continue; |
150 |
} |
151 |
|
152 |
if (is_dir($f)) { |
153 |
$top = basename($f); |
154 |
$l[] = sprintf('<li><span class="dir">%s</span>%s</li>', $top, print_list($list[$top])); |
155 |
} elseif (file_exists($f)) { |
156 |
$l[] = sprintf('<li><a href="%s" rel="nofollow" class="view-inline">%s</a> <span class="filesize">(%s)</span></li>', |
157 |
'uploads' . str_replace($upload_dir, '', $f), |
158 |
basename($f), |
159 |
_format_bytes(filesize($f)) |
160 |
); |
161 |
} |
162 |
} |
163 |
return sprintf('<ul>%s</ul>', implode($l)); |
164 |
} |