Utils.php in N1ED - Visual editor as CKEditor plugin with Bootstrap support 7
File
vendor/edsdk/file-uploader-server-php/src/lib/file/Utils.php
View source
<?php
namespace EdSDK\FileUploaderServer\lib\file;
class Utils {
public static function getNameWithoutExt($filename) {
$ext = Utils::getExt($filename);
if ($ext == null) {
return $filename;
}
return substr($filename, 0, strlen($filename) - strlen($ext) - 1);
}
public static function getExt($name) {
$i = strrpos($name, '.');
if ($i !== false) {
return substr($name, $i + 1);
}
return null;
}
public static function getFreeFileName($dir, $defaultName, $alwaysWithIndex) {
$i = $alwaysWithIndex ? 0 : -1;
do {
$i++;
if ($i == 0) {
$name = $defaultName;
}
else {
$name = Utils::getNameWithoutExt($defaultName) . "_" . $i . (Utils::getExt($defaultName) != null ? "." . Utils::getExt($defaultName) : "");
}
$filePath = $dir . $name;
$ok = !file_exists($filePath);
} while (!$ok);
return $name;
}
const PROHIBITED_SYMBOLS = "/\\?%*:|\"<>";
public static function fixFileName($name) {
$newName = "";
for ($i = 0; $i < strlen($name); $i++) {
$ch = substr($name, $i, 1);
if (strpos(Utils::PROHIBITED_SYMBOLS, $ch) !== FALSE) {
$ch = "_";
}
$newName = $newName . $ch;
}
return $newName;
}
public static function isFileNameSyntaxOk($name) {
if (strlen($name) == 0 || $name == "." || strpos($name, "..") > -1) {
return false;
}
for ($i = 0; $i < strlen(Utils::PROHIBITED_SYMBOLS); $i++) {
if (strpos($name, substr(Utils::PROHIBITED_SYMBOLS, $i, 1)) !== false) {
return false;
}
}
if (strlen($name) > 260) {
return false;
}
return true;
}
public static function isImage($name) {
$exts = [
"gif",
"jpg",
"jpeg",
"png",
];
$ext = Utils::getExt($name);
for ($i = 0; $i < count($exts); $i++) {
if ($exts[$i] === strtolower($ext)) {
return true;
}
}
return false;
}
}