1
0
mirror of https://github.com/misterunknown/ifm.git synced 2025-08-10 18:14:00 +02:00

fix code style warnings by latest psr

This commit is contained in:
Dmitriy Novash
2022-12-22 18:37:26 +02:00
parent 096a391809
commit 6ab7e569ef
5 changed files with 167 additions and 134 deletions

View File

@@ -26,16 +26,19 @@ $IFM_SRC_PHP = [
$options = getopt(null, ["language::", "languages::", "lang::", "cdn"]); $options = getopt(null, ["language::", "languages::", "lang::", "cdn"]);
// build CDN version? // build CDN version?
if (isset($options['cdn'])) if (isset($options['cdn'])) {
define("IFM_CDN", true); define("IFM_CDN", true);
else } else {
define("IFM_CDN", false); define("IFM_CDN", false);
}
// process languages // process languages
$langs = []; $langs = [];
foreach ($options as $key => $value) foreach ($options as $key => $value) {
if (substr($key, 0, 4) == "lang") if (substr($key, 0, 4) == "lang") {
$langs = array_merge($langs, explode(",", $value)); $langs = array_merge($langs, explode(",", $value));
}
}
$langs = array_unique($langs); $langs = array_unique($langs);
if (!empty($langs)) { if (!empty($langs)) {
@@ -45,25 +48,28 @@ if (!empty($langs)) {
} }
// ensure english is available, as it gets merged with the other languages // ensure english is available, as it gets merged with the other languages
// in case of missing keys in other languages. // in case of missing keys in other languages.
if (!in_array("all", $langs) || !in_array("en", $langs)) if (!in_array("all", $langs) || !in_array("en", $langs)) {
array_push($langs, "en"); array_push($langs, "en");
}
if (in_array("all", $langs)) if (in_array("all", $langs))
$langs = array_map( $langs = array_map(
function($lang_file) {return pathinfo($lang_file)['filename']; }, function ($lang_file) { return pathinfo($lang_file)['filename']; },
glob("src/i18n/*.json") glob("src/i18n/*.json")
); );
$vars['languageincludes'] = ""; $vars['languageincludes'] = "";
foreach ($langs as $l) foreach ($langs as $l) {
if (file_exists("src/i18n/".$l.".json")) if (file_exists("src/i18n/".$l.".json")) {
$vars['languageincludes'] .= $vars['languageincludes'] .=
'$i18n["'.$l.'"] = <<<\'f00bar\'' . "\n" '$i18n["'.$l.'"] = <<<\'f00bar\'' . "\n"
. file_get_contents( "src/i18n/".$l.".json" ) . "\n" . file_get_contents( "src/i18n/".$l.".json" ) . "\n"
. 'f00bar;' . "\n" . 'f00bar;' . "\n"
. '$i18n["'.$l.'"] = json_decode( $i18n["'.$l.'"], true );' . "\n" ; . '$i18n["'.$l.'"] = json_decode( $i18n["'.$l.'"], true );' . "\n" ;
else } else {
print "WARNING: Language file src/i18n/".$l.".json not found.\n"; print "WARNING: Language file src/i18n/".$l.".json not found.\n";
}
}
// Concat PHP Files // Concat PHP Files
$compiled = ["<?php"]; $compiled = ["<?php"];
@@ -79,13 +85,14 @@ $compiled = str_replace("@@@ASSETS_CSS@@@", file_get_contents("src/assets".(IFM_
$compiled = str_replace("@@@ASSETS_JS@@@", file_get_contents("src/assets".(IFM_CDN?".cdn":"").".js"), $compiled); $compiled = str_replace("@@@ASSETS_JS@@@", file_get_contents("src/assets".(IFM_CDN?".cdn":"").".js"), $compiled);
// Process file includes // Process file includes
$includes = NULL; $includes = null;
preg_match_all("/\@\@\@file:([^\@]+)\@\@\@/", $compiled, $includes, PREG_SET_ORDER); preg_match_all("/\@\@\@file:([^\@]+)\@\@\@/", $compiled, $includes, PREG_SET_ORDER);
foreach ($includes as $file) foreach ($includes as $file) {
$compiled = str_replace($file[0], file_get_contents($file[1]), $compiled); $compiled = str_replace($file[0], file_get_contents($file[1]), $compiled);
}
// Process ace includes // Process ace includes
$includes = NULL; $includes = null;
$vars['ace_includes'] = ""; $vars['ace_includes'] = "";
preg_match_all("/\@\@\@acedir:([^\@]+)\@\@\@/", $compiled, $includes, PREG_SET_ORDER); preg_match_all("/\@\@\@acedir:([^\@]+)\@\@\@/", $compiled, $includes, PREG_SET_ORDER);
foreach ($includes as $dir) { foreach ($includes as $dir) {
@@ -100,15 +107,17 @@ foreach ($includes as $dir) {
} }
// Process variable includes // Process variable includes
$includes = NULL; $includes = null;
preg_match_all("/\@\@\@vars:([^\@]+)\@\@\@/", $compiled, $includes, PREG_SET_ORDER); preg_match_all("/\@\@\@vars:([^\@]+)\@\@\@/", $compiled, $includes, PREG_SET_ORDER);
foreach( $includes as $var ) foreach ($includes as $var) {
$compiled = str_replace($var[0], $vars[$var[1]], $compiled); $compiled = str_replace($var[0], $vars[$var[1]], $compiled);
}
$compiled = str_replace('IFM_VERSION', IFM_VERSION, $compiled); $compiled = str_replace('IFM_VERSION', IFM_VERSION, $compiled);
if (!is_dir(IFM_RELEASE_DIR)) if (!is_dir(IFM_RELEASE_DIR)) {
mkdir(IFM_RELEASE_DIR); mkdir(IFM_RELEASE_DIR);
}
// build standalone ifm // build standalone ifm
file_put_contents(IFM_RELEASE_DIR . (IFM_CDN ? 'cdn.' : '') . IFM_STANDALONE, $compiled); file_put_contents(IFM_RELEASE_DIR . (IFM_CDN ? 'cdn.' : '') . IFM_STANDALONE, $compiled);
@@ -123,7 +132,7 @@ $ifm->run();
// build compressed ifm // build compressed ifm
file_put_contents( file_put_contents(
IFM_RELEASE_DIR . (IFM_CDN ? 'cdn.' : '') . IFM_STANDALONE_GZ, IFM_RELEASE_DIR . (IFM_CDN ? 'cdn.' : '') . IFM_STANDALONE_GZ,
'<?php eval( gzdecode( file_get_contents( __FILE__, false, null, 85 ) ) ); exit(0); ?>' '<?php eval(gzdecode(file_get_contents(__FILE__, false, null, 85))); exit(0); ?>'
. gzencode(file_get_contents(IFM_RELEASE_DIR . (IFM_CDN ? 'cdn.' : '') . IFM_STANDALONE, false, null, 5)) . gzencode(file_get_contents(IFM_RELEASE_DIR . (IFM_CDN ? 'cdn.' : '') . IFM_STANDALONE, false, null, 5))
); );
// build lib // build lib

View File

@@ -6,53 +6,55 @@
class Htpasswd { class Htpasswd {
public $users = []; public $users = [];
public function __construct( $filename="" ) { public function __construct($filename="") {
if( $filename ) if ($filename) {
$this->load( $filename ); $this->load($filename);
}
} }
/** /**
* Load a new htpasswd file * Load a new htpasswd file
*/ */
public function load( $filename ) { public function load($filename) {
unset( $this->users ); unset($this->users);
if( file_exists( $filename ) && is_readable( $filename ) ) { if (file_exists($filename) && is_readable($filename)) {
$lines = file( $filename ); $lines = file( $filename );
foreach( $lines as $line ) { foreach ($lines as $line) {
list( $user, $pass ) = explode( ":", $line ); list($user, $pass) = explode(":", $line);
$this->users[$user] = trim( $pass ); $this->users[$user] = trim($pass);
} }
return true; return true;
} else
return false;
}
public function getUsers() {
return array_keys( $this->users );
}
public function userExist( $user ) {
return isset( $this->users[ $user ] );
}
public function verify( $user, $pass ) {
if( isset( $this->users[$user] ) ) {
return $this->verifyPassword( $pass, $this->users[$user] );
} else { } else {
return false; return false;
} }
} }
public function verifyPassword( $pass, $hash ) { public function getUsers() {
if( substr( $hash, 0, 4 ) == '$2y$' ) { return array_keys($this->users);
return password_verify( $pass, $hash ); }
} elseif( substr( $hash, 0, 6 ) == '$apr1$' ) {
public function userExist($user) {
return isset($this->users[$user]);
}
public function verify($user, $pass) {
if (isset($this->users[$user])) {
return $this->verifyPassword($pass, $this->users[$user]);
} else {
return false;
}
}
public function verifyPassword($pass, $hash) {
if (substr($hash, 0, 4) == '$2y$') {
return password_verify($pass, $hash);
} elseif (substr($hash, 0, 6) == '$apr1$') {
$apr1 = new APR1_MD5(); $apr1 = new APR1_MD5();
return $apr1->check( $pass, $hash ); return $apr1->check($pass, $hash);
} elseif( substr( $hash, 0, 5 ) == '{SHA}' ) { } elseif (substr($hash, 0, 5) == '{SHA}') {
return base64_encode( sha1( $pass, TRUE ) ) == substr( $hash, 5 ); return base64_encode(sha1($pass, true)) == substr($hash, 5);
} else { // assume CRYPT } else { // assume CRYPT
return crypt( $pass, $hash ) == $hash; return crypt($pass, $hash) == $hash;
} }
} }
} }
@@ -64,64 +66,73 @@ class Htpasswd {
*/ */
class APR1_MD5 { class APR1_MD5 {
const BASE64_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; const BASE64_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
const APRMD5_ALPHABET = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; const APRMD5_ALPHABET = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
// Source/References for core algorithm: // Source/References for core algorithm:
// http://www.cryptologie.net/article/126/bruteforce-apr1-hashes/ // http://www.cryptologie.net/article/126/bruteforce-apr1-hashes/
// http://svn.apache.org/viewvc/apr/apr-util/branches/1.3.x/crypto/apr_md5.c?view=co // http://svn.apache.org/viewvc/apr/apr-util/branches/1.3.x/crypto/apr_md5.c?view=co
// http://www.php.net/manual/en/function.crypt.php#73619 // http://www.php.net/manual/en/function.crypt.php#73619
// http://httpd.apache.org/docs/2.2/misc/password_encryptions.html // http://httpd.apache.org/docs/2.2/misc/password_encryptions.html
// Wikipedia // Wikipedia
public static function hash($mdp, $salt = null) { public static function hash($mdp, $salt = null) {
if (is_null($salt)) if (is_null($salt)) {
$salt = self::salt(); $salt = self::salt();
$salt = substr($salt, 0, 8); }
$max = strlen($mdp); $salt = substr($salt, 0, 8);
$context = $mdp.'$apr1$'.$salt; $max = strlen($mdp);
$binary = pack('H32', md5($mdp.$salt.$mdp)); $context = $mdp.'$apr1$'.$salt;
for($i=$max; $i>0; $i-=16) $binary = pack('H32', md5($mdp.$salt.$mdp));
$context .= substr($binary, 0, min(16, $i)); for ($i=$max; $i>0; $i-=16) {
for($i=$max; $i>0; $i>>=1) $context .= substr($binary, 0, min(16, $i));
$context .= ($i & 1) ? chr(0) : $mdp[0]; }
$binary = pack('H32', md5($context)); for ($i=$max; $i>0; $i>>=1) {
for($i=0; $i<1000; $i++) { $context .= ($i & 1) ? chr(0) : $mdp[0];
$new = ($i & 1) ? $mdp : $binary; }
if($i % 3) $new .= $salt; $binary = pack('H32', md5($context));
if($i % 7) $new .= $mdp; for ($i=0; $i<1000; $i++) {
$new .= ($i & 1) ? $binary : $mdp; $new = ($i & 1) ? $mdp : $binary;
$binary = pack('H32', md5($new)); if ($i % 3) {
} $new .= $salt;
$hash = ''; }
for ($i = 0; $i < 5; $i++) { if ($i % 7) {
$k = $i+6; $new .= $mdp;
$j = $i+12; }
if($j == 16) $j = 5; $new .= ($i & 1) ? $binary : $mdp;
$hash = $binary[$i].$binary[$k].$binary[$j].$hash; $binary = pack('H32', md5($new));
} }
$hash = chr(0).chr(0).$binary[11].$hash; $hash = '';
$hash = strtr( for ($i = 0; $i < 5; $i++) {
strrev(substr(base64_encode($hash), 2)), $k = $i+6;
self::BASE64_ALPHABET, $j = $i+12;
self::APRMD5_ALPHABET if ($j == 16) {
); $j = 5;
return '$apr1$'.$salt.'$'.$hash; }
} $hash = $binary[$i].$binary[$k].$binary[$j].$hash;
}
$hash = chr(0).chr(0).$binary[11].$hash;
$hash = strtr(
strrev(substr(base64_encode($hash), 2)),
self::BASE64_ALPHABET,
self::APRMD5_ALPHABET
);
return '$apr1$'.$salt.'$'.$hash;
}
// 8 character salts are the best. Don't encourage anything but the best. // 8 character salts are the best. Don't encourage anything but the best.
public static function salt() { public static function salt() {
$alphabet = self::APRMD5_ALPHABET; $alphabet = self::APRMD5_ALPHABET;
$salt = ''; $salt = '';
for($i=0; $i<8; $i++) { for ($i=0; $i<8; $i++) {
$offset = hexdec(bin2hex(openssl_random_pseudo_bytes(1))) % 64; $offset = hexdec(bin2hex(openssl_random_pseudo_bytes(1))) % 64;
$salt .= $alphabet[$offset]; $salt .= $alphabet[$offset];
} }
return $salt; return $salt;
} }
public static function check($plain, $hash) { public static function check($plain, $hash) {
$parts = explode('$', $hash); $parts = explode('$', $hash);
return self::hash($plain, $parts[2]) === $hash; return self::hash($plain, $parts[2]) === $hash;
} }
} }

View File

@@ -1399,7 +1399,7 @@ function IFM(params) {
* @returns {boolean} * @returns {boolean}
*/ */
this.inArray = function(needle, haystack) { this.inArray = function(needle, haystack) {
for( let i = 0; i < haystack.length; i++ ) for ( let i = 0; i < haystack.length; i++ )
if( haystack[i] == needle ) if( haystack[i] == needle )
return true; return true;
return false; return false;
@@ -1575,7 +1575,7 @@ function IFM(params) {
this.generateGuid = function() { this.generateGuid = function() {
let result, i, j; let result, i, j;
result = ''; result = '';
for( j = 0; j < 20; j++ ) { for ( j = 0; j < 20; j++ ) {
i = Math.floor( Math.random() * 16 ).toString( 16 ).toUpperCase(); i = Math.floor( Math.random() * 16 ).toString( 16 ).toUpperCase();
result = result + i; result = result + i;
} }

View File

@@ -24,8 +24,9 @@ class IFMArchive {
* Add a folder to an archive * Add a folder to an archive
*/ */
private static function addFolder(&$archive, $folder, $offset=0, $exclude_callback=null) { private static function addFolder(&$archive, $folder, $offset=0, $exclude_callback=null) {
if ($offset == 0) if ($offset == 0) {
$offset = strlen(dirname($folder)) + 1; $offset = strlen(dirname($folder)) + 1;
}
$archive->addEmptyDir(substr($folder, $offset)); $archive->addEmptyDir(substr($folder, $offset));
$handle = opendir($folder); $handle = opendir($folder);
while (false !== $f = readdir($handle)) { while (false !== $f = readdir($handle)) {
@@ -33,13 +34,15 @@ class IFMArchive {
$filePath = $folder . '/' . $f; $filePath = $folder . '/' . $f;
if (file_exists($filePath) && is_readable($filePath)) { if (file_exists($filePath) && is_readable($filePath)) {
if (is_file($filePath)) { if (is_file($filePath)) {
if (!is_callable($exclude_callback) || $exclude_callback($f)) if (!is_callable($exclude_callback) || $exclude_callback($f)) {
$archive->addFile( $filePath, substr( $filePath, $offset ) ); $archive->addFile($filePath, substr($filePath, $offset));
}
} elseif (is_dir($filePath)) { } elseif (is_dir($filePath)) {
if (is_callable($exclude_callback)) if (is_callable($exclude_callback)) {
self::addFolder($archive, $filePath, $offset, $exclude_callback); self::addFolder($archive, $filePath, $offset, $exclude_callback);
else } else {
self::addFolder($archive, $filePath, $offset); self::addFolder($archive, $filePath, $offset);
}
} }
} }
} }
@@ -54,18 +57,22 @@ class IFMArchive {
$a = new ZipArchive(); $a = new ZipArchive();
$a->open($archivename, ZIPARCHIVE::CREATE); $a->open($archivename, ZIPARCHIVE::CREATE);
if (!is_array($filenames)) if (!is_array($filenames)) {
$filenames = array($filenames); $filenames = array($filenames);
}
foreach ($filenames as $f) foreach ($filenames as $f)
if (is_dir($f)) if (is_dir($f)) {
if (is_callable($exclude_callback)) if (is_callable($exclude_callback)) {
self::addFolder( $a, $f, null, $exclude_callback ); self::addFolder($a, $f, null, $exclude_callback);
else } else {
self::addFolder( $a, $f ); self::addFolder($a, $f);
elseif (is_file($f)) }
if (!is_callable($exclude_callback) || $exclude_callback($f)) } elseif (is_file($f)) {
if (!is_callable($exclude_callback) || $exclude_callback($f)) {
$a->addFile($f, pathinfo($f, PATHINFO_BASENAME)); $a->addFile($f, pathinfo($f, PATHINFO_BASENAME));
}
}
try { try {
return $a->close(); return $a->close();
@@ -78,16 +85,18 @@ class IFMArchive {
* Unzip a zip file * Unzip a zip file
*/ */
public static function extractZip($file, $destination="./") { public static function extractZip($file, $destination="./") {
if (!file_exists($file)) if (!file_exists($file)) {
return false; return false;
}
$zip = new ZipArchive; $zip = new ZipArchive;
$res = $zip->open($file); $res = $zip->open($file);
if ($res === true) { if ($res === true) {
$zip->extractTo($destination); $zip->extractTo($destination);
$zip->close(); $zip->close();
return true; return true;
} else } else {
return false; return false;
}
} }
/** /**
@@ -98,14 +107,17 @@ class IFMArchive {
$a = new PharData($tmpf); $a = new PharData($tmpf);
try { try {
if (!is_array($filenames)) if (!is_array($filenames)) {
$filenames = array($filenames); $filenames = array($filenames);
}
foreach ($filenames as $f) foreach ($filenames as $f) {
if (is_dir($f)) if (is_dir($f)) {
self::addFolder($a, $f); self::addFolder($a, $f);
elseif (is_file($f)) } elseif (is_file($f)) {
$a->addFile($f, pathinfo($f, PATHINFO_BASENAME)); $a->addFile($f, pathinfo($f, PATHINFO_BASENAME));
}
}
switch ($format) { switch ($format) {
case "tar.gz": case "tar.gz":
$a->compress(Phar::GZ); $a->compress(Phar::GZ);
@@ -127,8 +139,9 @@ class IFMArchive {
* Extracts a tar archive * Extracts a tar archive
*/ */
public static function extractTar($file, $destination="./") { public static function extractTar($file, $destination="./") {
if (!file_exists($file)) if (!file_exists($file)) {
return false; return false;
}
$tar = new PharData($file); $tar = new PharData($file);
try { try {
$tar->extractTo($destination, null, true); $tar->extractTo($destination, null, true);

View File

@@ -581,7 +581,7 @@ f00bar;
if (isset($d['filename']) && $this->isFilenameValid($d['filename'])) { if (isset($d['filename']) && $this->isFilenameValid($d['filename'])) {
if (isset($d['content'])) { if (isset($d['content'])) {
// work around magic quotes // work around magic quotes
if((function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc()) if ((function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc())
|| (ini_get('magic_quotes_sybase') && (strtolower(ini_get('magic_quotes_sybase'))!="off")) ) { || (ini_get('magic_quotes_sybase') && (strtolower(ini_get('magic_quotes_sybase'))!="off")) ) {
$content = stripslashes($d['content']); $content = stripslashes($d['content']);
} else { } else {
@@ -1024,7 +1024,7 @@ f00bar;
if ($login_failed === true) if ($login_failed === true)
throw new IFMException("Authentication failed: Wrong credentials", true); throw new IFMException("Authentication failed: Wrong credentials", true);
else else
throw new IFMException("Not authenticated" , true); throw new IFMException("Not authenticated", true);
} }
} }
@@ -1057,7 +1057,7 @@ f00bar;
} }
$u = $uuid . "=" . $user . "," . $basedn; $u = $uuid . "=" . $user . "," . $basedn;
if (!$ds = ldap_connect($ldap_server)) { if (!$ds = ldap_connect($ldap_server)) {
throw new IFMException("Could not reach the ldap server." , true); throw new IFMException("Could not reach the ldap server.", true);
//trigger_error("Could not reach the ldap server.", E_USER_ERROR); //trigger_error("Could not reach the ldap server.", E_USER_ERROR);
return false; return false;
} }
@@ -1069,14 +1069,14 @@ f00bar;
if (ldap_count_entries($ds, ldap_search($ds, $u, $ufilter)) == 1) { if (ldap_count_entries($ds, ldap_search($ds, $u, $ufilter)) == 1) {
$authenticated = true; $authenticated = true;
} else { } else {
throw new IFMException("User not allowed." , true); throw new IFMException("User not allowed.", true);
//trigger_error("User not allowed.", E_USER_ERROR); //trigger_error("User not allowed.", E_USER_ERROR);
$authenticated = false; $authenticated = false;
} }
} else } else
$authenticated = true; $authenticated = true;
} else { } else {
throw new IFMException(ldap_error($ds) , true); throw new IFMException(ldap_error($ds), true);
//trigger_error(ldap_error($ds), E_USER_ERROR); //trigger_error(ldap_error($ds), E_USER_ERROR);
$authenticated = false; $authenticated = false;
} }
@@ -1104,7 +1104,7 @@ f00bar;
private function isAbsolutePath($path) { private function isAbsolutePath($path) {
if ($path === null || $path === '') if ($path === null || $path === '')
return false; return false;
return $path[0] === DIRECTORY_SEPARATOR || preg_match('~^[A-Z]:(?![^/\\\\])~i',$path) > 0; return $path[0] === DIRECTORY_SEPARATOR || preg_match('~^[A-Z]:(?![^/\\\\])~i', $path) > 0;
} }
private function getRootDir() { private function getRootDir() {
@@ -1248,7 +1248,7 @@ f00bar;
$ret = ""; $ret = "";
foreach ($parts as $part) foreach ($parts as $part)
if (trim($part) != "") if (trim($part) != "")
$ret .= (empty($ret) ? rtrim($part,"/") : trim($part, '/'))."/"; $ret .= (empty($ret) ? rtrim($part, "/") : trim($part, '/'))."/";
return rtrim($ret, "/"); return rtrim($ret, "/");
} }