refactor(修改权限): 优化权限修改功能

This commit is contained in:
xiaoqidun 2020-10-21 20:10:44 +08:00
parent ef741e85ba
commit e39d223768
2 changed files with 53 additions and 227 deletions

@ -4,14 +4,18 @@ class filesystem
{ {
private $path = null; private $path = null;
function __construct() public function __construct()
{ {
if (func_num_args() > 0) $this->path = func_get_arg(0); if (func_num_args() > 0) {
$this->path = func_get_arg(0);
}
} }
function chmod() public function chmod()
{ {
if (!function_exists("chmod") || !file_exists("perms.php")) return false; if (!function_exists("chmod")) {
return false;
}
if (func_num_args() == 3) { if (func_num_args() == 3) {
$path = $this->path; $path = $this->path;
$dirperms = func_get_arg(0); $dirperms = func_get_arg(0);
@ -25,22 +29,25 @@ class filesystem
} else { } else {
return false; return false;
} }
if (!file_exists($path)) return false; if (!file_exists($path)) {
require "perms.php"; return false;
if (!isset($perms["$dirperms"]) || !isset($perms["$fileperms"])) return false; }
if (!is_dir($path)) return chmod($path, $perms["$fileperms"]); if (!is_dir($path)) {
if (!$recursive || !($dh = opendir($path))) return chmod($path, $perms["$dirperms"]); return chmod($path, octdec($fileperms));
}
if (!$recursive || !($dh = opendir($path))) {
return chmod($path, octdec($dirperms));
}
while (($entry = readdir($dh)) !== false) { while (($entry = readdir($dh)) !== false) {
if ($entry != "." && $entry != "..") { if ($entry != "." && $entry != "..") {
$this->chmod($path . "/" . $entry, $dirperms, $fileperms, $recursive); $this->chmod($path . "/" . $entry, $dirperms, $fileperms, $recursive);
} }
} }
closedir($dh); closedir($dh);
return chmod($path, $perms["$dirperms"]); return chmod($path, octdec($dirperms));
} }
function chpath() public function chpath()
{ {
if (func_num_args() < 1) { if (func_num_args() < 1) {
return false; return false;
@ -49,14 +56,15 @@ class filesystem
} }
} }
function cppath() public function cppath()
{ {
if (func_num_args() == 1) { if (func_num_args() == 1) {
$path = $this->path; $path = $this->path;
$topath = func_get_arg(0); $topath = func_get_arg(0);
} elseif (func_num_args() == 2) { } elseif (func_num_args() == 2) {
$path = func_get_arg(1); $path = func_get_arg(1);
$topath = func_get_arg(0);; $topath = func_get_arg(0);
;
} else { } else {
return false; return false;
} }
@ -67,7 +75,9 @@ class filesystem
} elseif (!mkdir($topath, 0755, true)) { } elseif (!mkdir($topath, 0755, true)) {
return false; return false;
} }
if (!($dh = opendir($path))) return false; if (!($dh = opendir($path))) {
return false;
}
while (($entry = readdir($dh)) !== false) { while (($entry = readdir($dh)) !== false) {
if ($entry != "." && $entry != "..") { if ($entry != "." && $entry != "..") {
$this->cppath($topath . "/" . $entry, $path . "/" . $entry); $this->cppath($topath . "/" . $entry, $path . "/" . $entry);
@ -77,7 +87,7 @@ class filesystem
return true; return true;
} }
function rmpath() public function rmpath()
{ {
if (func_num_args() > 0) { if (func_num_args() > 0) {
$path = func_get_arg(0); $path = func_get_arg(0);
@ -91,7 +101,9 @@ class filesystem
} elseif (!is_dir($path)) { } elseif (!is_dir($path)) {
return unlink($path); return unlink($path);
} }
if (!($dh = opendir($path))) return false; if (!($dh = opendir($path))) {
return false;
}
while (($entry = readdir($dh)) !== false) { while (($entry = readdir($dh)) !== false) {
if ($entry != "." && $entry != "..") { if ($entry != "." && $entry != "..") {
$this->rmpath($path . "/" . $entry); $this->rmpath($path . "/" . $entry);
@ -100,7 +112,7 @@ class filesystem
return rmdir($path); return rmdir($path);
} }
function getpath() public function getpath()
{ {
if (func_num_args() > 0) { if (func_num_args() > 0) {
$path = func_get_arg(0); $path = func_get_arg(0);
@ -109,7 +121,9 @@ class filesystem
} }
if (is_dir($path)) { if (is_dir($path)) {
$fs = array(array(), array(), array()); $fs = array(array(), array(), array());
if (!($dh = opendir($path))) return false; if (!($dh = opendir($path))) {
return false;
}
while (($entry = readdir($dh)) !== false) { while (($entry = readdir($dh)) !== false) {
if ($entry != "." && $entry != "..") { if ($entry != "." && $entry != "..") {
if (is_dir($entry = ___realpath($path . "/" . $entry))) { if (is_dir($entry = ___realpath($path . "/" . $entry))) {
@ -124,10 +138,18 @@ class filesystem
} }
} }
closedir($dh); closedir($dh);
if ((count($fs, 1) - 3) < 1) return null; if ((count($fs, 1) - 3) < 1) {
if (count($fs[0]) > 0) usort($fs[0], "___sortcmp"); return null;
if (count($fs[1]) > 0) usort($fs[1], "___sortcmp"); }
if (count($fs[2]) > 0) usort($fs[2], "___sortcmp"); 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");
}
return $fs; return $fs;
} elseif (file_exists($path)) { } elseif (file_exists($path)) {
if (!($fs = stat($path))) { if (!($fs = stat($path))) {
@ -140,9 +162,11 @@ class filesystem
} }
} }
function getfinfo() public function getfinfo()
{ {
if (!function_exists("finfo_open")) return false; if (!function_exists("finfo_open")) {
return false;
}
$finfo = finfo_open(); $finfo = finfo_open();
if (func_num_args() > 0) { if (func_num_args() > 0) {
return finfo_file($finfo, func_get_arg(0)); return finfo_file($finfo, func_get_arg(0));
@ -150,9 +174,11 @@ class filesystem
return finfo_file($finfo, $this->path); return finfo_file($finfo, $this->path);
} }
function getperms() public function getperms()
{ {
if (!function_exists("fileperms")) return false; if (!function_exists("fileperms")) {
return false;
}
if (func_num_args() > 0) { if (func_num_args() > 0) {
$path = func_get_arg(0); $path = func_get_arg(0);
if (($perms = fileperms($path)) === false) { if (($perms = fileperms($path)) === false) {
@ -168,5 +194,3 @@ class filesystem
} }
} }
} }
?>

198
perms.php

@ -1,198 +0,0 @@
<?php
$perms = array(
"0000" => 0000, "0100" => 0100, "0010" => 0010,
"0001" => 0001, "0200" => 0200, "0020" => 0020,
"0002" => 0002, "0300" => 0300, "0030" => 0030,
"0003" => 0003, "0400" => 0400, "0040" => 0040,
"0004" => 0004, "0500" => 0500, "0050" => 0050,
"0005" => 0005, "0600" => 0600, "0060" => 0060,
"0006" => 0006, "0700" => 0700, "0070" => 0070,
"0007" => 0007, "0100" => 0100, "0010" => 0010,
"0110" => 0110, "0011" => 0011, "0120" => 0120,
"0012" => 0012, "0130" => 0130, "0013" => 0013,
"0140" => 0140, "0014" => 0014, "0150" => 0150,
"0015" => 0015, "0160" => 0160, "0016" => 0016,
"0170" => 0170, "0017" => 0017, "0200" => 0200,
"0020" => 0020, "0210" => 0210, "0021" => 0021,
"0220" => 0220, "0022" => 0022, "0230" => 0230,
"0023" => 0023, "0240" => 0240, "0024" => 0024,
"0250" => 0250, "0025" => 0025, "0260" => 0260,
"0026" => 0026, "0270" => 0270, "0027" => 0027,
"0300" => 0300, "0030" => 0030, "0310" => 0310,
"0031" => 0031, "0320" => 0320, "0032" => 0032,
"0330" => 0330, "0033" => 0033, "0340" => 0340,
"0034" => 0034, "0350" => 0350, "0035" => 0035,
"0360" => 0360, "0036" => 0036, "0370" => 0370,
"0037" => 0037, "0400" => 0400, "0040" => 0040,
"0410" => 0410, "0041" => 0041, "0420" => 0420,
"0042" => 0042, "0430" => 0430, "0043" => 0043,
"0440" => 0440, "0044" => 0044, "0450" => 0450,
"0045" => 0045, "0460" => 0460, "0046" => 0046,
"0470" => 0470, "0047" => 0047, "0500" => 0500,
"0050" => 0050, "0510" => 0510, "0051" => 0051,
"0520" => 0520, "0052" => 0052, "0530" => 0530,
"0053" => 0053, "0540" => 0540, "0054" => 0054,
"0550" => 0550, "0055" => 0055, "0560" => 0560,
"0056" => 0056, "0570" => 0570, "0057" => 0057,
"0600" => 0600, "0060" => 0060, "0610" => 0610,
"0061" => 0061, "0620" => 0620, "0062" => 0062,
"0630" => 0630, "0063" => 0063, "0640" => 0640,
"0064" => 0064, "0650" => 0650, "0065" => 0065,
"0660" => 0660, "0066" => 0066, "0670" => 0670,
"0067" => 0067, "0700" => 0700, "0070" => 0070,
"0710" => 0710, "0071" => 0071, "0720" => 0720,
"0072" => 0072, "0730" => 0730, "0073" => 0073,
"0740" => 0740, "0074" => 0074, "0750" => 0750,
"0075" => 0075, "0760" => 0760, "0076" => 0076,
"0770" => 0770, "0077" => 0077, "0100" => 0100,
"0101" => 0101, "0102" => 0102, "0103" => 0103,
"0104" => 0104, "0105" => 0105, "0106" => 0106,
"0107" => 0107, "0110" => 0110, "0111" => 0111,
"0112" => 0112, "0113" => 0113, "0114" => 0114,
"0115" => 0115, "0116" => 0116, "0117" => 0117,
"0120" => 0120, "0121" => 0121, "0122" => 0122,
"0123" => 0123, "0124" => 0124, "0125" => 0125,
"0126" => 0126, "0127" => 0127, "0130" => 0130,
"0131" => 0131, "0132" => 0132, "0133" => 0133,
"0134" => 0134, "0135" => 0135, "0136" => 0136,
"0137" => 0137, "0140" => 0140, "0141" => 0141,
"0142" => 0142, "0143" => 0143, "0144" => 0144,
"0145" => 0145, "0146" => 0146, "0147" => 0147,
"0150" => 0150, "0151" => 0151, "0152" => 0152,
"0153" => 0153, "0154" => 0154, "0155" => 0155,
"0156" => 0156, "0157" => 0157, "0160" => 0160,
"0161" => 0161, "0162" => 0162, "0163" => 0163,
"0164" => 0164, "0165" => 0165, "0166" => 0166,
"0167" => 0167, "0170" => 0170, "0171" => 0171,
"0172" => 0172, "0173" => 0173, "0174" => 0174,
"0175" => 0175, "0176" => 0176, "0177" => 0177,
"0200" => 0200, "0201" => 0201, "0202" => 0202,
"0203" => 0203, "0204" => 0204, "0205" => 0205,
"0206" => 0206, "0207" => 0207, "0210" => 0210,
"0211" => 0211, "0212" => 0212, "0213" => 0213,
"0214" => 0214, "0215" => 0215, "0216" => 0216,
"0217" => 0217, "0220" => 0220, "0221" => 0221,
"0222" => 0222, "0223" => 0223, "0224" => 0224,
"0225" => 0225, "0226" => 0226, "0227" => 0227,
"0230" => 0230, "0231" => 0231, "0232" => 0232,
"0233" => 0233, "0234" => 0234, "0235" => 0235,
"0236" => 0236, "0237" => 0237, "0240" => 0240,
"0241" => 0241, "0242" => 0242, "0243" => 0243,
"0244" => 0244, "0245" => 0245, "0246" => 0246,
"0247" => 0247, "0250" => 0250, "0251" => 0251,
"0252" => 0252, "0253" => 0253, "0254" => 0254,
"0255" => 0255, "0256" => 0256, "0257" => 0257,
"0260" => 0260, "0261" => 0261, "0262" => 0262,
"0263" => 0263, "0264" => 0264, "0265" => 0265,
"0266" => 0266, "0267" => 0267, "0270" => 0270,
"0271" => 0271, "0272" => 0272, "0273" => 0273,
"0274" => 0274, "0275" => 0275, "0276" => 0276,
"0277" => 0277, "0300" => 0300, "0301" => 0301,
"0302" => 0302, "0303" => 0303, "0304" => 0304,
"0305" => 0305, "0306" => 0306, "0307" => 0307,
"0310" => 0310, "0311" => 0311, "0312" => 0312,
"0313" => 0313, "0314" => 0314, "0315" => 0315,
"0316" => 0316, "0317" => 0317, "0320" => 0320,
"0321" => 0321, "0322" => 0322, "0323" => 0323,
"0324" => 0324, "0325" => 0325, "0326" => 0326,
"0327" => 0327, "0330" => 0330, "0331" => 0331,
"0332" => 0332, "0333" => 0333, "0334" => 0334,
"0335" => 0335, "0336" => 0336, "0337" => 0337,
"0340" => 0340, "0341" => 0341, "0342" => 0342,
"0343" => 0343, "0344" => 0344, "0345" => 0345,
"0346" => 0346, "0347" => 0347, "0350" => 0350,
"0351" => 0351, "0352" => 0352, "0353" => 0353,
"0354" => 0354, "0355" => 0355, "0356" => 0356,
"0357" => 0357, "0360" => 0360, "0361" => 0361,
"0362" => 0362, "0363" => 0363, "0364" => 0364,
"0365" => 0365, "0366" => 0366, "0367" => 0367,
"0370" => 0370, "0371" => 0371, "0372" => 0372,
"0373" => 0373, "0374" => 0374, "0375" => 0375,
"0376" => 0376, "0377" => 0377, "0400" => 0400,
"0401" => 0401, "0402" => 0402, "0403" => 0403,
"0404" => 0404, "0405" => 0405, "0406" => 0406,
"0407" => 0407, "0410" => 0410, "0411" => 0411,
"0412" => 0412, "0413" => 0413, "0414" => 0414,
"0415" => 0415, "0416" => 0416, "0417" => 0417,
"0420" => 0420, "0421" => 0421, "0422" => 0422,
"0423" => 0423, "0424" => 0424, "0425" => 0425,
"0426" => 0426, "0427" => 0427, "0430" => 0430,
"0431" => 0431, "0432" => 0432, "0433" => 0433,
"0434" => 0434, "0435" => 0435, "0436" => 0436,
"0437" => 0437, "0440" => 0440, "0441" => 0441,
"0442" => 0442, "0443" => 0443, "0444" => 0444,
"0445" => 0445, "0446" => 0446, "0447" => 0447,
"0450" => 0450, "0451" => 0451, "0452" => 0452,
"0453" => 0453, "0454" => 0454, "0455" => 0455,
"0456" => 0456, "0457" => 0457, "0460" => 0460,
"0461" => 0461, "0462" => 0462, "0463" => 0463,
"0464" => 0464, "0465" => 0465, "0466" => 0466,
"0467" => 0467, "0470" => 0470, "0471" => 0471,
"0472" => 0472, "0473" => 0473, "0474" => 0474,
"0475" => 0475, "0476" => 0476, "0477" => 0477,
"0500" => 0500, "0501" => 0501, "0502" => 0502,
"0503" => 0503, "0504" => 0504, "0505" => 0505,
"0506" => 0506, "0507" => 0507, "0510" => 0510,
"0511" => 0511, "0512" => 0512, "0513" => 0513,
"0514" => 0514, "0515" => 0515, "0516" => 0516,
"0517" => 0517, "0520" => 0520, "0521" => 0521,
"0522" => 0522, "0523" => 0523, "0524" => 0524,
"0525" => 0525, "0526" => 0526, "0527" => 0527,
"0530" => 0530, "0531" => 0531, "0532" => 0532,
"0533" => 0533, "0534" => 0534, "0535" => 0535,
"0536" => 0536, "0537" => 0537, "0540" => 0540,
"0541" => 0541, "0542" => 0542, "0543" => 0543,
"0544" => 0544, "0545" => 0545, "0546" => 0546,
"0547" => 0547, "0550" => 0550, "0551" => 0551,
"0552" => 0552, "0553" => 0553, "0554" => 0554,
"0555" => 0555, "0556" => 0556, "0557" => 0557,
"0560" => 0560, "0561" => 0561, "0562" => 0562,
"0563" => 0563, "0564" => 0564, "0565" => 0565,
"0566" => 0566, "0567" => 0567, "0570" => 0570,
"0571" => 0571, "0572" => 0572, "0573" => 0573,
"0574" => 0574, "0575" => 0575, "0576" => 0576,
"0577" => 0577, "0600" => 0600, "0601" => 0601,
"0602" => 0602, "0603" => 0603, "0604" => 0604,
"0605" => 0605, "0606" => 0606, "0607" => 0607,
"0610" => 0610, "0611" => 0611, "0612" => 0612,
"0613" => 0613, "0614" => 0614, "0615" => 0615,
"0616" => 0616, "0617" => 0617, "0620" => 0620,
"0621" => 0621, "0622" => 0622, "0623" => 0623,
"0624" => 0624, "0625" => 0625, "0626" => 0626,
"0627" => 0627, "0630" => 0630, "0631" => 0631,
"0632" => 0632, "0633" => 0633, "0634" => 0634,
"0635" => 0635, "0636" => 0636, "0637" => 0637,
"0640" => 0640, "0641" => 0641, "0642" => 0642,
"0643" => 0643, "0644" => 0644, "0645" => 0645,
"0646" => 0646, "0647" => 0647, "0650" => 0650,
"0651" => 0651, "0652" => 0652, "0653" => 0653,
"0654" => 0654, "0655" => 0655, "0656" => 0656,
"0657" => 0657, "0660" => 0660, "0661" => 0661,
"0662" => 0662, "0663" => 0663, "0664" => 0664,
"0665" => 0665, "0666" => 0666, "0667" => 0667,
"0670" => 0670, "0671" => 0671, "0672" => 0672,
"0673" => 0673, "0674" => 0674, "0675" => 0675,
"0676" => 0676, "0677" => 0677, "0700" => 0700,
"0701" => 0701, "0702" => 0702, "0703" => 0703,
"0704" => 0704, "0705" => 0705, "0706" => 0706,
"0707" => 0707, "0710" => 0710, "0711" => 0711,
"0712" => 0712, "0713" => 0713, "0714" => 0714,
"0715" => 0715, "0716" => 0716, "0717" => 0717,
"0720" => 0720, "0721" => 0721, "0722" => 0722,
"0723" => 0723, "0724" => 0724, "0725" => 0725,
"0726" => 0726, "0727" => 0727, "0730" => 0730,
"0731" => 0731, "0732" => 0732, "0733" => 0733,
"0734" => 0734, "0735" => 0735, "0736" => 0736,
"0737" => 0737, "0740" => 0740, "0741" => 0741,
"0742" => 0742, "0743" => 0743, "0744" => 0744,
"0745" => 0745, "0746" => 0746, "0747" => 0747,
"0750" => 0750, "0751" => 0751, "0752" => 0752,
"0753" => 0753, "0754" => 0754, "0755" => 0755,
"0756" => 0756, "0757" => 0757, "0760" => 0760,
"0761" => 0761, "0762" => 0762, "0763" => 0763,
"0764" => 0764, "0765" => 0765, "0766" => 0766,
"0767" => 0767, "0770" => 0770, "0771" => 0771,
"0772" => 0772, "0773" => 0773, "0774" => 0774,
"0775" => 0775, "0776" => 0776, "0777" => 0777
);
?>