phpcp/kernel.php

197 lines
5.3 KiB
PHP
Raw Normal View History

2020-09-16 12:24:54 +08:00
<?php
class filesystem
{
private $path = null;
public function __construct()
2020-09-16 12:24:54 +08:00
{
if (func_num_args() > 0) {
$this->path = func_get_arg(0);
}
2020-09-16 12:24:54 +08:00
}
public function chmod()
2020-09-16 12:24:54 +08:00
{
if (!function_exists("chmod")) {
return false;
}
2020-09-16 12:24:54 +08:00
if (func_num_args() == 3) {
$path = $this->path;
$dirperms = func_get_arg(0);
$fileperms = func_get_arg(1);
$recursive = func_get_arg(2);
} elseif (func_num_args() == 4) {
$path = func_get_arg(0);
$dirperms = func_get_arg(1);
$fileperms = func_get_arg(2);
$recursive = func_get_arg(3);
} else {
return false;
}
if (!file_exists($path)) {
return false;
}
if (!is_dir($path)) {
return chmod($path, octdec($fileperms));
}
if (!$recursive || !($dh = opendir($path))) {
return chmod($path, octdec($dirperms));
}
2020-09-16 12:24:54 +08:00
while (($entry = readdir($dh)) !== false) {
if ($entry != "." && $entry != "..") {
$this->chmod($path . "/" . $entry, $dirperms, $fileperms, $recursive);
}
}
closedir($dh);
return chmod($path, octdec($dirperms));
2020-09-16 12:24:54 +08:00
}
public function chpath()
2020-09-16 12:24:54 +08:00
{
if (func_num_args() < 1) {
return false;
} else {
$this->path = func_get_arg(0);
}
}
public function cppath()
2020-09-16 12:24:54 +08:00
{
if (func_num_args() == 1) {
$path = $this->path;
$topath = func_get_arg(0);
} elseif (func_num_args() == 2) {
$path = func_get_arg(1);
$topath = func_get_arg(0);
;
2020-09-16 12:24:54 +08:00
} else {
return false;
}
if (!file_exists($path)) {
return false;
} elseif (!is_dir($path)) {
return copy($path, $topath);
} elseif (!mkdir($topath, 0755, true)) {
return false;
}
if (!($dh = opendir($path))) {
return false;
}
2020-09-16 12:24:54 +08:00
while (($entry = readdir($dh)) !== false) {
if ($entry != "." && $entry != "..") {
$this->cppath($topath . "/" . $entry, $path . "/" . $entry);
}
}
closedir($dh);
return true;
}
public function rmpath()
2020-09-16 12:24:54 +08:00
{
if (func_num_args() > 0) {
$path = func_get_arg(0);
} else {
$path = $this->path;
}
if (!file_exists($path) && !is_link($path)) {
return true;
} elseif (is_link($path)) {
return unlink($path);
} elseif (!is_dir($path)) {
return unlink($path);
}
if (!($dh = opendir($path))) {
return false;
}
2020-09-16 12:24:54 +08:00
while (($entry = readdir($dh)) !== false) {
if ($entry != "." && $entry != "..") {
$this->rmpath($path . "/" . $entry);
}
}
return rmdir($path);
}
public function getpath()
2020-09-16 12:24:54 +08:00
{
if (func_num_args() > 0) {
$path = func_get_arg(0);
} else {
$path = $this->path;
}
if (is_dir($path)) {
$fs = array(array(), array(), array());
if (!($dh = opendir($path))) {
return false;
}
2020-09-16 12:24:54 +08:00
while (($entry = readdir($dh)) !== false) {
if ($entry != "." && $entry != "..") {
if (is_dir($entry = ___realpath($path . "/" . $entry))) {
$fs[0][] = $entry;
} elseif (is_file($entry)) {
$fs[1][] = $entry;
} else {
if ($entry != "") {
$fs[2][] = $entry;
}
}
}
}
closedir($dh);
if ((count($fs, 1) - 3) < 1) {
return null;
}
if (count($fs[0]) > 0) {
usort($fs[0], "___sortcmp");
}
if (count($fs[1]) > 0) {
usort($fs[1], "___sortcmp");
}
if (count($fs[2]) > 0) {
usort($fs[2], "___sortcmp");
}
2020-09-16 12:24:54 +08:00
return $fs;
} elseif (file_exists($path)) {
if (!($fs = stat($path))) {
return false;
} else {
return $fs;
}
} else {
return false;
}
}
public function getfinfo()
2020-09-16 12:24:54 +08:00
{
if (!function_exists("finfo_open")) {
return false;
}
2020-09-16 12:24:54 +08:00
$finfo = finfo_open();
if (func_num_args() > 0) {
return finfo_file($finfo, func_get_arg(0));
}
return finfo_file($finfo, $this->path);
}
public function getperms()
2020-09-16 12:24:54 +08:00
{
if (!function_exists("fileperms")) {
return false;
}
2020-09-16 12:24:54 +08:00
if (func_num_args() > 0) {
$path = func_get_arg(0);
if (($perms = fileperms($path)) === false) {
return false;
} else {
return substr(sprintf("%o", $perms), -4);
}
}
if (($perms = fileperms($this->path)) === false) {
return false;
} else {
return substr(sprintf("%o", $perms), -4);
}
}
}
2020-10-21 20:18:22 +08:00
?>