From 65677c889dd7a2e7fba10347d97c98aa2d2453a0 Mon Sep 17 00:00:00 2001 From: Phil Burton <phil@d3r.com> Date: Tue, 2 Apr 2019 11:59:16 +0100 Subject: [PATCH 1/4] Format code to be PSR2 compliant --- index.php | 585 +++++++++++++++++++++++++++--------------------------- 1 file changed, 296 insertions(+), 289 deletions(-) diff --git a/index.php b/index.php index 8de2218..635a195 100644 --- a/index.php +++ b/index.php @@ -1,46 +1,47 @@ <?php + include("config.php"); $signout = '<a href="' . basename($_SERVER['PHP_SELF']) . '?signout">Sign out</a>'; // Respect HTTPS or not if (isset($_SERVER['HTTPS'])) { - $url = preg_replace("/^http:\/\//", "https://", $url); + $url = preg_replace("/^http:\/\//", "https://", $url); } else { - $url = preg_replace("/^https:\/\//", "http://", $url); + $url = preg_replace("/^https:\/\//", "http://", $url); } if (isset($_GET) && count($_GET) > 0) { - $getstring = "?" . http_build_query($_GET); + $getstring = "?" . http_build_query($_GET); } else { - $getstring = ""; + $getstring = ""; } if (isset($_GET['signout'])) { - if (isset($_COOKIE['password'])) { - unset($_COOKIE['password']); - setcookie("password", null, -1, "/", $_SERVER['SERVER_NAME']); - header("Location: " . $url); - } + if (isset($_COOKIE['password'])) { + unset($_COOKIE['password']); + setcookie("password", null, -1, "/", $_SERVER['SERVER_NAME']); + header("Location: " . $url); + } } if ($_POST && $pass) { - if ((hash("sha256", $_POST['password']) == $pass)) { - if (isset($_POST['rememberme'])) { - /* Set cookie to last 1 year */ - setcookie("password", hash("sha256", $_POST['password']), time()+60*60*24*365, "/", $_SERVER['SERVER_NAME'] ); + if ((hash("sha256", $_POST['password']) == $pass)) { + if (isset($_POST['rememberme'])) { + // Set cookie to last 1 year + setcookie("password", hash("sha256", $_POST['password']), time()+60*60*24*365, "/", $_SERVER['SERVER_NAME']); + } else { + // Cookie expires when browser closes + setcookie("password", hash("sha256", $_POST['password']), false, "/", $_SERVER['SERVER_NAME']); + } + header("Location: " . $url . $getstring); } else { - /* Cookie expires when browser closes */ - setcookie("password", hash("sha256", $_POST['password']), false, "/", $_SERVER['SERVER_NAME'] ); + header("Location: " . $url); + exit(); } - header("Location: " . $url . $getstring); - } else { - header("Location: " . $url); - exit(); - } -} else if ($pass) { - if (!isset($_COOKIE["password"]) || (isset($_COOKIE["password"]) && $_COOKIE["password"] != $pass)) { -?> +} elseif ($pass) { + if (!isset($_COOKIE["password"]) || (isset($_COOKIE["password"]) && $_COOKIE["password"] != $pass)) { + ?> <html> <head> <title><?php echo $logontitle ?></title> @@ -60,35 +61,37 @@ if ($_POST && $pass) { </form> </body> </html> -<?php - exit(); - } + + <?php + exit(); + } } else { - $signout = ""; + $signout = ""; } -function size_readable($size, $max = null, $system = "si", $retstring = "%01.2f %s") { - // Pick units - $systems['si']['prefix'] = array('B', 'K', 'MB', 'GB', 'TB', 'PB'); - $systems['si']['size'] = 1000; - $systems['bi']['prefix'] = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'); - $systems['bi']['size'] = 1024; - $sys = isset($systems[$system]) ? $systems[$system] : $systems['si']; - - // Max unit to display - $depth = count($sys['prefix']) - 1; - if ($max && false !== $d = array_search($max, $sys['prefix'])) { - $depth = $d; - } - - // Loop - $i = 0; - while ($size >= $sys['size'] && $i < $depth) { - $size /= $sys['size']; - $i++; - } - - return sprintf($retstring, $size, $sys['prefix'][$i]); +function size_readable($size, $max = null, $system = "si", $retstring = "%01.2f %s") +{ + // Pick units + $systems['si']['prefix'] = array('B', 'K', 'MB', 'GB', 'TB', 'PB'); + $systems['si']['size'] = 1000; + $systems['bi']['prefix'] = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'); + $systems['bi']['size'] = 1024; + $sys = isset($systems[$system]) ? $systems[$system] : $systems['si']; + + // Max unit to display + $depth = count($sys['prefix']) - 1; + if ($max && false !== $d = array_search($max, $sys['prefix'])) { + $depth = $d; + } + + // Loop + $i = 0; + while ($size >= $sys['size'] && $i < $depth) { + $size /= $sys['size']; + $i++; + } + + return sprintf($retstring, $size, $sys['prefix'][$i]); } $nameurl = "?C=N;O=A"; @@ -99,20 +102,19 @@ $sizeurl = "?C=S;O=A"; $sort = ['M', 'D']; if (isset($_GET['C'])) { - $sort = preg_split("/;O=/", $_GET['C']); - if ($_GET['C'] == "N;O=A") { - $nameurl = "?C=N;O=D"; - } else if ($_GET['C'] == "M;O=A") { - $modifiedurl = "?C=M;O=D"; - } else if ($_GET['C'] == "S;O=A") { - $sizeurl = "?C=S;O=D"; - } + $sort = preg_split("/;O=/", $_GET['C']); + if ($_GET['C'] == "N;O=A") { + $nameurl = "?C=N;O=D"; + } elseif ($_GET['C'] == "M;O=A") { + $modifiedurl = "?C=M;O=D"; + } elseif ($_GET['C'] == "S;O=A") { + $sizeurl = "?C=S;O=D"; + } } $listing = array(); date_default_timezone_set("GB"); -#print_r(listdir_by_date("./")); $files_directory = "./"; @@ -121,224 +123,224 @@ $files = array(); $fdirectory = opendir("$files_directory"); while ($file = readdir($fdirectory)) { - if (($file == ".") || ($file == "..")) { - } else { - array_push($files, $file); - } + if (($file == ".") || ($file == "..")) { + } else { + array_push($files, $file); + } } foreach ($files as $key => $file) { - $c_date = filemtime("$files_directory/$file"); - $fileCollection[] = [ - 'date' => $c_date, - 'file' => $file, - 'size' => filesize("$files_directory/$file") - ]; + $c_date = filemtime("$files_directory/$file"); + $fileCollection[] = [ + 'date' => $c_date, + 'file' => $file, + 'size' => filesize("$files_directory/$file") + ]; } if (isset($sort)) { // Sort by modified date - if ($sort[0] == "M") { - if ($sort[1] == "A") { - usort( - $fileCollection, - function($a, $b) { - return $a['date'] > $b['date']; + if ($sort[0] == "M") { + if ($sort[1] == "A") { + usort( + $fileCollection, + function ($a, $b) { + return $a['date'] > $b['date']; + } + ); + } else { + usort( + $fileCollection, + function ($a, $b) { + return $a['date'] < $b['date']; + } + ); + } + // Sort by name + } elseif ($sort[0] == "N") { + if ($sort[1] == "A") { + usort( + $fileCollection, + function ($a, $b) { + return $a['file'] > $b['file']; + } + ); + } else { + usort( + $fileCollection, + function ($a, $b) { + return $a['file'] < $b['file']; + } + ); + } + // Sort by size + } elseif ($sort[0] == "S") { + if ($sort[1] == "A") { + usort( + $fileCollection, + function ($a, $b) { + return $a['size'] > $b['size']; + } + ); + } else { + usort( + $fileCollection, + function ($a, $b) { + return $a['size'] < $b['size']; + } + ); } - ); - } else { - usort( - $fileCollection, - function($a, $b) { - return $a['date'] < $b['date']; - } - ); - } - // Sort by name - } else if ($sort[0] == "N") { - if ($sort[1] == "A") { - usort( - $fileCollection, - function($a, $b) { - return $a['file'] > $b['file']; - } - ); - } else { - usort( - $fileCollection, - function($a, $b) { - return $a['file'] < $b['file']; - } - ); - } - // Sort by size - } else if ($sort[0] == "S") { - if ($sort[1] == "A") { - usort( - $fileCollection, - function($a, $b) { - return $a['size'] > $b['size']; - } - ); - } else { - usort( - $fileCollection, - function($a, $b) { - return $a['size'] < $b['size']; - } - ); } - } } else { - krsort($fileCollection); + krsort($fileCollection); } $listsize = 0; foreach ($fileCollection as $item) { // Only show last X entries if requested - if (isset($_GET['last']) && ($listsize > $_GET['last'] - 1)) { - break; - } - - // Drop if on index ignore list - if (in_array($item['file'], $indexignore)) { - continue; - } - - // Drop if outside (optional) date range - if (isset($_GET['from'])) { - $from = $_GET['from']; - // If $from has a -, assume not Unix time - if (strpos($from, "-") !== false) { - $from = strtotime($from); + if (isset($_GET['last']) && ($listsize > $_GET['last'] - 1)) { + break; } - if (isset($_GET['to'])) { - $to = $_GET['to']; - // If $to has a -, assume not Unix time - if (strpos($to, "-") !== false) { - $to = strtotime($to); - } - } else { - $to = time(); + // Drop if on index ignore list + if (in_array($item['file'], $indexignore)) { + continue; + } + + // Drop if outside (optional) date range + if (isset($_GET['from'])) { + $from = $_GET['from']; + // If $from has a -, assume not Unix time + if (strpos($from, "-") !== false) { + $from = strtotime($from); + } + + if (isset($_GET['to'])) { + $to = $_GET['to']; + // If $to has a -, assume not Unix time + if (strpos($to, "-") !== false) { + $to = strtotime($to); + } + } else { + $to = time(); + } + + if ($item['date'] < $from || $item['date'] > $to) { + continue; + } } - if ($item['date'] < $from || $item['date'] > $to) { - continue; + $fileparts = explode(".", strtolower($item['file'])); + $icon = "unknown"; + + switch ($fileparts[count($fileparts)-1]) { + case "png": + case "jpg": + case "gif": + case "bmp": + case "jpeg": + case "svg": + $icon = "image2"; + break; + case "exe": + case "jar": + case "class": + case "apk": + $icon = "binary"; + break; + case "c": + case "cpp": + case "h": + $icon = "c"; + break; + case "doc": + case "docx": + case "dot": + case "dotx": + case "odt": + case "ott": + case "oth": + case "odm": + case "xls": + case "xlsx": + case "ppt": + case "pptx": + $icon = "layout"; + break; + case "avi": + case "mkv": + case "wmv": + case "ogv": + case "3gp": + case "divx": + case "mov": + $icon = "movie"; + break; + case "pdf": + $icon = "pdf"; + break; + case "ps": + $icon = "ps"; + break; + case "java": + case "php": + case "html": + case "htm": + case "xml": + case "cs": + case "sh": + case "bat": + case "js": + case "jsp": + case "py": + case "cgi": + case "pl": + $icon = "script"; + break; + case "wav": + $icon = "sound1"; + break; + case "mp3": + case "ogg": + case "wma": + case "aac": + $icon = "sound2"; + break; + case "tex": // Flow through + $icon = "tex"; + case "txt": + case "rtf": + case "log": + $icon = "text"; + break; + case "zip": + case "rar": + case "gz": + case "bz2": + case "tar": + case "tgz": + case "7z": + case "iso": + $icon = "compressed"; + break; + default: + $icon = "unknown"; + break; } - } - - $fileparts = explode(".", strtolower($item['file'])); - $icon = "unknown"; - - switch ($fileparts[count($fileparts)-1]) { - case "png": - case "jpg": - case "gif": - case "bmp": - case "jpeg": - case "svg": - $icon="image2"; - break; - case "exe": - case "jar": - case "class": - case "apk": - $icon="binary"; - break; - case "c": - case "cpp": - case "h": - $icon="c"; - break; - case "doc": - case "docx": - case "dot": - case "dotx": - case "odt": - case "ott": - case "oth": - case "odm": - case "xls": - case "xlsx": - case "ppt": - case "pptx": - $icon="layout"; - break; - case "avi": - case "mkv": - case "wmv": - case "ogv": - case "3gp": - case "divx": - case "mov": - $icon="movie"; - break; - case "pdf": - $icon="pdf"; - break; - case "ps": - $icon="ps"; - break; - case "java": - case "php": - case "html": - case "htm": - case "xml": - case "cs": - case "sh": - case "bat": - case "js": - case "jsp": - case "py": - case "cgi": - case "pl": - $icon="script"; - break; - case "wav": - $icon="sound1"; - break; - case "mp3": - case "ogg": - case "wma": - case "aac": - $icon="sound2"; - break; - case "tex": - $icon="tex"; - case "txt": - case "rtf": - case "log": - $icon="text"; - break; - case "zip": - case "rar": - case "gz": - case "bz2": - case "tar": - case "tgz": - case "7z": - case "iso": - $icon="compressed"; - break; - default: - $icon="unknown"; - break; - } - - $listing[$listsize]['filename'] = utf8_encode($item['file']); - $listing[$listsize]['type'] = $icon; - $listing[$listsize]['modified'] = $item['date']; - $listing[$listsize]['size'] = $item['size']; - - $listsize++; + + $listing[$listsize]['filename'] = utf8_encode($item['file']); + $listing[$listsize]['type'] = $icon; + $listing[$listsize]['modified'] = $item['date']; + $listing[$listsize]['size'] = $item['size']; + + $listsize++; } clearstatcache(); if (!isset($_GET['format']) || $_GET['format'] == "html") { -?> + ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <html> <head> @@ -347,48 +349,53 @@ if (!isset($_GET['format']) || $_GET['format'] == "html") { </head> <body> -<?php -if ($headerfile) { - echo file_get_contents($headerfile); -} -?> + <?php + if ($headerfile) { + echo file_get_contents($headerfile); + } + ?> <pre><div class="icon iconblank"></div><a href="<?php echo $nameurl; ?>">Name</a> <a href="<?php echo $modifiedurl; ?>">Last modified</a> <a href="<?php echo $sizeurl; ?>">Size</a> <?php echo $signout; ?> -<hr><?php -for ($i = 0; $i < count($listing); $i++) { - if (strlen($listing[$i]['filename']) > 23) { - $preview = substr($listing[$i]['filename'], 0, 20) . "..>"; - } else { - $preview = $listing[$i]['filename']; - } - - $spaces = ""; - - for ($j = 0; $j < (24 - strlen($preview)); $j++) { - $spaces = $spaces . " "; - } - echo '<div class="icon icon' . $listing[$i]['type'] . '"></div><a href="' . rawurlencode($listing[$i]['filename']) . '">' . $preview . "</a>" . $spaces . date("d-M-Y H:i", $listing[$i]['modified']) . " " . size_readable($listing[$i]['size'], null, "bi") . " " . "\n"; -} -?> +<hr> + <?php + for ($i = 0; $i < count($listing); $i++) { + if (strlen($listing[$i]['filename']) > 23) { + $preview = substr($listing[$i]['filename'], 0, 20) . "..>"; + } else { + $preview = $listing[$i]['filename']; + } + + $spaces = ""; + + for ($j = 0; $j < (24 - strlen($preview)); $j++) { + $spaces = $spaces . " "; + } + echo '<div class="icon icon' . $listing[$i]['type'] . '"></div><a href="' . rawurlencode($listing[$i]['filename']) . '">' . $preview . "</a>" . $spaces . date("d-M-Y H:i", $listing[$i]['modified']) . " " . size_readable($listing[$i]['size'], null, "bi") . " " . "\n"; + } + ?> <hr></pre> -<?php echo "<address>Apache Server at " . $_SERVER['SERVER_NAME'] . " Port " . $_SERVER['SERVER_PORT'] . "</address>"; ?> + <?php echo "<address>Apache Server at " + . $_SERVER['SERVER_NAME'] + . " Port " + . $_SERVER['SERVER_PORT'] + . "</address>" + ?> </body></html> -<?php -} else if ($_GET['format'] == "json") { - header("Content-type: application/json"); - - echo json_encode($listing); -} else if ($_GET['format'] == "raw") { - print_r($listing); -} else if ($_GET['format'] == "plain") { - header("Content-type: text/plain"); - - for ($i = 0; $i < count($listing); $i++) { - echo $listing[$i]['filename'] . "\n"; - } -} else if ($_GET['format'] == "light") { - for ($i = 0; $i < count($listing); $i++) { - echo '<a href="' . rawurlencode($listing[$i]['filename']) . '">' . $listing[$i]['filename'] . '</a><br>'; - } + <?php +} elseif ($_GET['format'] == "json") { + header("Content-type: application/json"); + echo json_encode($listing); +} elseif ($_GET['format'] == "raw") { + print_r($listing); +} elseif ($_GET['format'] == "plain") { + header("Content-type: text/plain"); + + for ($i = 0; $i < count($listing); $i++) { + echo $listing[$i]['filename'] . "\n"; + } +} elseif ($_GET['format'] == "light") { + for ($i = 0; $i < count($listing); $i++) { + echo '<a href="' . rawurlencode($listing[$i]['filename']) . '">' . $listing[$i]['filename'] . '</a><br>'; + } } ?> -- GitLab From 728ab1c8fb6af2dc63310415b3eec6e8e70bfb99 Mon Sep 17 00:00:00 2001 From: Phil Burton <phil@d3r.com> Date: Tue, 2 Apr 2019 12:06:27 +0100 Subject: [PATCH 2/4] Wrap at 120 chars and make sure we have spaces between operators and data, e.g. = 'bar' instead of ='bar' --- index.php | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/index.php b/index.php index 635a195..7d0788a 100644 --- a/index.php +++ b/index.php @@ -29,7 +29,13 @@ if ($_POST && $pass) { if ((hash("sha256", $_POST['password']) == $pass)) { if (isset($_POST['rememberme'])) { // Set cookie to last 1 year - setcookie("password", hash("sha256", $_POST['password']), time()+60*60*24*365, "/", $_SERVER['SERVER_NAME']); + setcookie( + "password", + hash("sha256", $_POST['password']), + time() + 60 * 60 * 24 * 365, + "/", + $_SERVER['SERVER_NAME'] + ); } else { // Cookie expires when browser closes setcookie("password", hash("sha256", $_POST['password']), false, "/", $_SERVER['SERVER_NAME']); @@ -234,7 +240,7 @@ foreach ($fileCollection as $item) { $fileparts = explode(".", strtolower($item['file'])); $icon = "unknown"; - switch ($fileparts[count($fileparts)-1]) { + switch ($fileparts[count($fileparts) - 1]) { case "png": case "jpg": case "gif": @@ -354,7 +360,12 @@ if (!isset($_GET['format']) || $_GET['format'] == "html") { echo file_get_contents($headerfile); } ?> -<pre><div class="icon iconblank"></div><a href="<?php echo $nameurl; ?>">Name</a> <a href="<?php echo $modifiedurl; ?>">Last modified</a> <a href="<?php echo $sizeurl; ?>">Size</a> <?php echo $signout; ?> +<pre> + <div class="icon iconblank"></div> + <a href="<?php echo $nameurl; ?>">Name</a> + <a href="<?php echo $modifiedurl; ?>">Last modified</a> + <a href="<?php echo $sizeurl; ?>">Size</a> + <?php echo $signout ?> <hr> <?php for ($i = 0; $i < count($listing); $i++) { @@ -369,7 +380,19 @@ if (!isset($_GET['format']) || $_GET['format'] == "html") { for ($j = 0; $j < (24 - strlen($preview)); $j++) { $spaces = $spaces . " "; } - echo '<div class="icon icon' . $listing[$i]['type'] . '"></div><a href="' . rawurlencode($listing[$i]['filename']) . '">' . $preview . "</a>" . $spaces . date("d-M-Y H:i", $listing[$i]['modified']) . " " . size_readable($listing[$i]['size'], null, "bi") . " " . "\n"; + echo '<div class="icon icon' + . $listing[$i]['type'] + . '"></div><a href="' + . rawurlencode($listing[$i]['filename']) + . '">' + . $preview + . "</a>" + . $spaces + . date("d-M-Y H:i", $listing[$i]['modified']) + . " " + . size_readable($listing[$i]['size'], null, "bi") + . " " + . "\n"; } ?> <hr></pre> -- GitLab From 98b6a49da115ef5c0efbc3fa90d20030c06a6beb Mon Sep 17 00:00:00 2001 From: Phil Burton <phil@d3r.com> Date: Tue, 2 Apr 2019 12:07:01 +0100 Subject: [PATCH 3/4] Remove the needless semi colons --- index.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.php b/index.php index 7d0788a..67c2d16 100644 --- a/index.php +++ b/index.php @@ -362,9 +362,9 @@ if (!isset($_GET['format']) || $_GET['format'] == "html") { ?> <pre> <div class="icon iconblank"></div> - <a href="<?php echo $nameurl; ?>">Name</a> - <a href="<?php echo $modifiedurl; ?>">Last modified</a> - <a href="<?php echo $sizeurl; ?>">Size</a> + <a href="<?php echo $nameurl ?>">Name</a> + <a href="<?php echo $modifiedurl ?>">Last modified</a> + <a href="<?php echo $sizeurl ?>">Size</a> <?php echo $signout ?> <hr> <?php -- GitLab From 33581d71bcdb62ffb660e7ac3c5c71c8d17fed32 Mon Sep 17 00:00:00 2001 From: Phil Burton <phil@d3r.com> Date: Tue, 2 Apr 2019 12:23:30 +0100 Subject: [PATCH 4/4] Switch to newer array syntax --- index.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.php b/index.php index 67c2d16..8182011 100644 --- a/index.php +++ b/index.php @@ -78,9 +78,9 @@ if ($_POST && $pass) { function size_readable($size, $max = null, $system = "si", $retstring = "%01.2f %s") { // Pick units - $systems['si']['prefix'] = array('B', 'K', 'MB', 'GB', 'TB', 'PB'); + $systems['si']['prefix'] = ['B', 'K', 'MB', 'GB', 'TB', 'PB']; $systems['si']['size'] = 1000; - $systems['bi']['prefix'] = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'); + $systems['bi']['prefix'] = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB']; $systems['bi']['size'] = 1024; $sys = isset($systems[$system]) ? $systems[$system] : $systems['si']; @@ -124,8 +124,8 @@ date_default_timezone_set("GB"); $files_directory = "./"; -$fileCollection = array(); -$files = array(); +$fileCollection = []; +$files = []; $fdirectory = opendir("$files_directory"); while ($file = readdir($fdirectory)) { -- GitLab