$2$3', $string);
$string = preg_replace('#(^|[\n \-*/.])_(.+?)_($|[ \-*/.!?,:])#is', '$1$2$3', $string);
$string = preg_replace('#(^|[\n \-_*.])/(.+?)/($|[ \-_*.!?,:])#is', '$1$2$3', $string);
return nl2br($string);
}
/**
* Convert given number to human readable file size string.
* Will append Bytes, KiB, etc. depending on magnitude of number.
*
* @param int $bytes numeric value of the filesize to make readable
* @param int $decimals number of decimals to show, -1 for automatic
* @return string human-readable string representing the given filesize
*/
public static function readableFileSize(int $bytes, int $decimals = -1): string
{
static $sz = array('Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
$factor = floor((strlen($bytes) - 1) / 3);
if ($factor == 0) {
$decimals = 0;
} elseif ($decimals === -1) {
$decimals = 2 - floor((strlen($bytes) - 1) % 3);
}
return sprintf("%.{$decimals}f ", $bytes / pow(1024, $factor)) . $sz[$factor];
}
public static function sanitizeFilename(string $name): string
{
return preg_replace('/[^a-zA-Z0-9_\-]+/', '_', $name);
}
public static function safePath(string $path, string $prefix = ''): ?string
{
if (empty($path))
return null;
$path = trim($path);
if ($path[0] == '/' || preg_match('/[\x00-\x19?*]/', $path))
return null;
if (strpos($path, '..') !== false)
return null;
if (substr($path, 0, 2) !== './')
$path = "./$path";
if (empty($prefix))
return $path;
if (substr($prefix, 0, 2) !== './')
$prefix = "./$prefix";
if (substr($path, 0, strlen($prefix)) !== $prefix)
return null;
return $path;
}
/**
* Create human readable error description from a $_FILES[<..>]['error'] code
*
* @param int $code the code to turn into an error description
* @return string the error description
*/
public static function uploadErrorString($code)
{
switch ($code) {
case UPLOAD_ERR_INI_SIZE:
$message = "The uploaded file exceeds the upload_max_filesize directive in php.ini";
break;
case UPLOAD_ERR_FORM_SIZE:
$message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
break;
case UPLOAD_ERR_PARTIAL:
$message = "The uploaded file was only partially uploaded";
break;
case UPLOAD_ERR_NO_FILE:
$message = "No file was uploaded";
break;
case UPLOAD_ERR_NO_TMP_DIR:
$message = "Missing a temporary folder";
break;
case UPLOAD_ERR_CANT_WRITE:
$message = "Failed to write file to disk";
break;
case UPLOAD_ERR_EXTENSION:
$message = "File upload stopped by extension";
break;
default:
$message = "Unknown upload error";
break;
}
return $message;
}
/**
* Is given string a public ipv4 address?
*
* @param string $ip_addr input to check
* @return boolean true iff $ip_addr is a valid public ipv4 address
*/
public static function isPublicIpv4($ip_addr)
{
if (!preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/", $ip_addr))
return false;
$parts = explode(".", $ip_addr);
foreach ($parts as $part) {
if (!is_numeric($part) || $part > 255 || $part < 0)
return false;
}
if ($parts[0] == 0 || $parts[0] == 10 || $parts[0] == 127 || ($parts[0] > 223 && $parts[0] < 240))
return false;
if (($parts[0] == 192 && $parts[1] == 168) || ($parts[0] == 169 && $parts[1] == 254))
return false;
if ($parts[0] == 172 && $parts[1] > 15 && $parts[1] < 32)
return false;
return true;
}
/**
* Check whether $arrax contains all keys given in $keyList
* @param array $array An array
* @param array $keyList A list of strings which must all be valid keys in $array
* @return boolean
*/
public static function hasAllKeys($array, $keyList)
{
if (!is_array($array))
return false;
foreach ($keyList as $key) {
if (!isset($array[$key]))
return false;
}
return true;
}
/**
* Send a file to user for download.
*
* @param string $file path of local file
* @param string $name name of file to send to user agent
* @param bool $delete delete the file when done?
* @return bool false: file could not be opened.
* true: error while reading the file
* - on success, the function does not return
*/
public static function sendFile(string $file, string $name, bool $delete = false): bool
{
while ((@ob_get_level()) > 0)
@ob_end_clean();
$fh = @fopen($file, 'rb');
if ($fh === false) {
Message::addError('error-read', $file);
return false;
}
Header('Content-Type: application/octet-stream', true);
Header('Content-Disposition: attachment; filename=' . str_replace(array(' ', '=', ',', '/', '\\', ':', '?'), '_', iconv('UTF-8', 'ASCII//TRANSLIT', $name)));
Header('Content-Length: ' . @filesize($file));
while (!feof($fh)) {
$data = fread($fh, 16000);
if ($data === false) {
echo "\r\n\nDOWNLOAD INTERRUPTED!\n";
if ($delete)
@unlink($file);
return true;
}
echo $data;
@ob_flush();
@flush();
}
@fclose($fh);
if ($delete)
@unlink($file);
exit(0);
}
public static function getRole(?string $debugKey = null): string
{
if (strpos(";{$_SERVER['entitlement']};", CONFIG_ENTITLEMENT) !== false) {
$role = 'TUTOR';
} else if (strpos(";{$_SERVER[CONFIG_SCOPED_AFFILIATION]};", ';employee@') !== false
|| strpos(";{$_SERVER[CONFIG_SCOPED_AFFILIATION]};", ';staff@') !== false
|| strpos(";{$_SERVER[CONFIG_SCOPED_AFFILIATION]};", ';faculty@') !== false) {
$role = 'TUTOR';
} else {
if ($debugKey !== null) {
file_put_contents('/tmp/shib-student-' . $debugKey . '-'
. time() . '-' . $_SERVER['REMOTE_ADDR'] . '.txt', print_r($_SERVER, true));
}
$role = 'STUDENT';
}
return $role;
}
}