Skip to content

Commit 3d213cc

Browse files
committed
Allow show files contents
1 parent f8f930b commit 3d213cc

File tree

5 files changed

+70
-10
lines changed

5 files changed

+70
-10
lines changed

src/functions.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,15 @@
153153
$function_color = function ($text) {
154154
return "\033[0;32m$text\033[0m";
155155
};
156+
157+
$function_parent_dir = function($relative_path) {
158+
if($relative_path):
159+
$query = isset($_GET['php-server']) ? '?php-server=' . $_GET['php-server'] : '';
160+
$relative_path = explode('/', $relative_path);
161+
array_pop($relative_path);
162+
$relative_path = implode('/', $relative_path);
163+
$relative_path .= $relative_path ? $query : '/' . $query;
164+
?>
165+
<p class="half"><a href="<?= $relative_path ?>">Parent dir</a></p>
166+
<?php endif;
167+
};

src/pages/_template.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,23 @@
9999
min-width: 30px;
100100
display: inline-block;
101101
text-indent: 0;
102+
103+
}
104+
.file-code {
105+
border: 1px gray solid;
106+
padding: 1%;
107+
background: #fff;
108+
overflow-x: auto;
109+
width: 98%;
110+
margin: 0 auto;
111+
white-space: pre;
112+
word-wrap: break-word;
113+
-moz-tab-size: 4;
114+
-o-tab-size: 4;
115+
tab-size: 4;
116+
}
117+
.file-code code {
118+
white-space: normal;
102119
}
103120
.date {
104121
float: right;
@@ -130,11 +147,14 @@
130147
</head>
131148
<body>
132149
<?php
133-
if (strpos($title, 'Index of ') === 0)
150+
if (strpos($title, 'Index of ') === 0 || strpos($title, 'File ') === 0)
134151
{
135152
$dirs = explode('/', $title);
153+
$pre = $dirs[0];
136154
unset($dirs[0]);
137155

156+
$query = isset($_GET['php-server']) ? '?php-server=' . $_GET['php-server'] : '';
157+
138158
$dirs = array_values($dirs);
139159
$breadcrumbs = '';
140160
for ($i = 0; $i < count($dirs); $i++)
@@ -144,12 +164,13 @@
144164
{
145165
$dir .= '/' . $dirs[$s];
146166
}
167+
$dir .= $query;
147168
$breadcrumbs .= "<a href=\"{$dir}\" class=\"uri\" data-id=\"{$i}\">"
148169
. ($i === 0 ? '' : '/')
149170
. "{$dirs[$i]}</a>";
150171
}
151172

152-
$title = 'Index of <a href="/" class="uri" data-id="-1">/</a>' . $breadcrumbs;
173+
$title = $pre . ' <a href="/" class="uri" data-id="-1">/</a>' . $breadcrumbs;
153174
}
154175
?>
155176
<h1><?= $title ?></h1>

src/pages/default.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
1-
<?php if(! empty($relative_path)): ?>
2-
<?php
3-
$rdirs = explode('/', $relative_path);
4-
array_pop($rdirs);
5-
$rdirs = implode('/', $rdirs);
6-
?>
7-
<p class="half"><a href="<?= ! empty($rdirs) ? $rdirs : '/' ?>">Parent dir</a></p>
8-
<?php endif ?>
1+
<?php $function_parent_dir($relative_path) ?>
92
<?php if(! empty($paths)): ?>
103
<p class="half" style="float: right"><input type="text" id="search" placeholder="Search..."></p>
114
<?php endif ?>
125
<table>
136
<thead>
147
<tr>
8+
<?= $function_order_link('type', 'Type') ?>
159
<?= $function_order_link('filename', 'Name') ?>
1610
<?= $function_order_link('size', 'Size') ?>
1711
<?= $function_order_link('owner', 'Owner') ?>
1812
<?= $function_order_link('group', 'Group') ?>
1913
<?= $function_order_link('perms', 'Permissions') ?>
2014
<?= $function_order_link('mTime', 'Modified') ?>
15+
<th>Options</th>
2116
</tr>
2217
</thead>
2318
<tbody>
@@ -43,13 +38,18 @@
4338
<tr>
4439
<td>
4540
<span><?= $path['type'] ?></span>
41+
</td>
42+
<td>
4643
<a href="<?= $path['href'] ?>"><?= $path['filename'] ?></a>
4744
</td>
4845
<td><?= $path['size'] ?></td>
4946
<td title="<?= $user ?>"><?= $path['owner'] ?></td>
5047
<td title="<?= $group ?>"><?= $path['group'] ?></td>
5148
<td><?= $path['perms'] ?></td>
5249
<td><?= $path['mTime'] ?></td>
50+
<td>
51+
<a href="<?= $path['href'] ?>?php-server=show">Show</a>
52+
</td>
5353
</tr>
5454
<?php endforeach ?>
5555
</tbody>

src/pages/show-file.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
$contents = file_get_contents($absolute_path);
3+
$contents = substr($absolute_path, -3) === 'php'
4+
? highlight_string($contents, true)
5+
: htmlentities($contents);
6+
$function_parent_dir($relative_path);
7+
?>
8+
<div class="file-code"><?= $contents ?></div>

src/server_router.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@
3737
$relative_path = preg_replace('/\/$/', '', urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
3838
$absolute_path = $config['root'] . $relative_path;
3939

40+
// Show file contents or goto listAll
41+
if (isset($_GET['php-server']) && $_GET['php-server'] === 'show')
42+
{
43+
if (is_dir($absolute_path))
44+
{
45+
goto listAll;
46+
}
47+
48+
$title = 'File ' . $relative_path;
49+
$page = 'show-file';
50+
51+
require __DIR__ . '/pages/_template.php';
52+
53+
$function_clean_vars();
54+
return true;
55+
}
56+
4057
// If is not a dir get the file content
4158
if (! is_dir($absolute_path) && is_file($absolute_path))
4259
{
@@ -76,6 +93,8 @@
7693
}
7794
}
7895

96+
listAll:
97+
7998
// If has not any index and the called file or dir does not exist
8099
// means that we need a Error 404
81100
if (! file_exists($absolute_path))

0 commit comments

Comments
 (0)