<?php
 class vB_Utilities { use vB_Trait_NoSerialize; public static function logAdminAction(string $script, string $action, string $message = '', int $userid = -1) { if ($userid == -1) { $userid = vB::getCurrentSession()->fetch_userinfo()['userid']; } $request = vB::getRequest(); vB::getDbAssertor()->insert('vBForum:adminlog', [ 'userid' => $userid, 'dateline' => $request->getTimeNow(), 'script' => $script, 'action' => $action, 'extrainfo' => $message, 'ipaddress' => $request->getIpAddress(), ]); } public static function vbmkdir($path, $mode = 0777) { if (is_dir($path)) { if (!(is_writable($path))) { @chmod($path, $mode); } return true; } else { $oldmask = @umask(0); $partialpath = dirname($path); if (!self::vbmkdir($partialpath, $mode)) { return false; } else { return @mkdir($path, $mode); } } } public static function ini_size_to_bytes($value) { $value = trim($value); $retval = intval($value); switch(strtolower($value[strlen($value) - 1])) { case 'g': $retval *= 1024; case 'm': $retval *= 1024; case 'k': $retval *= 1024; break; } return $retval; } public static function extendMemoryLimit() { $value = '256M'; $new_memory_limit = self::ini_size_to_bytes($value); self::extendMemoryLimitBytes($new_memory_limit); } public static function extendMemoryLimitBytes($value) { $current_memory_limit = self::ini_size_to_bytes(ini_get('memory_limit')); if ($current_memory_limit > 0 AND $current_memory_limit < $value) { try { @ini_set('memory_limit', $value); } catch (Exception $e) { } } } public static function getTmpFileName($entropy = '', $prefix = 'vb_', $suffix = '') { $options = vB::getDatastore()->getValue('options'); if ($options['safeupload']) { if (empty($entropy)) { $entropy = vB::getCurrentSession()->get('userid'); } $filename = $options['tmppath'] . DIRECTORY_SEPARATOR . $prefix . md5(uniqid(microtime()) . $entropy) . $suffix; touch($filename); } else { $userContext = vB::getUserContext(); if ($userContext AND $userContext->hasPermission('adminpermissions', 'cancontrolpanel')) { $filename = tempnam(self::getTmpDir(), $prefix); } else { $filename = @tempnam(self::getTmpDir(), $prefix); } if ($filename AND $suffix) { unlink($filename); $filename = $filename . $suffix; touch($filename); } } return $filename; } public static function getTmpDir() { $options = vB::getDatastore()->getValue('options'); if ($options['safeupload']) { $path = $options['tmppath']; } else { $path = ini_get('upload_tmp_dir'); if (!$path OR !is_writable($path)) { $path = sys_get_temp_dir(); } } return $path; } public static function getStackTrace() { $trace = debug_backtrace(); $trace_item_blank = array( 'type' => '', 'file' => '', 'line' => '', 'class' => '', ); $dir = trim(DIR, '/\\'); $dir = substr($dir, -4) == 'core' ? substr($dir, 0, -4) : $dir; $dir = trim($dir, '/\\'); $traceString = ''; foreach ($trace AS $index => $trace_item) { $trace_item += $trace_item_blank; if (in_array($trace_item['function'], array('require', 'require_once', 'include', 'include_once'))) { $param = array(); foreach ($trace_item['args'] AS $arg) { $param[] = str_replace($dir, '[path]', $arg); } $param = implode(', ', $param); } else { $param = array(); if (isset($trace_item['args'])) { foreach ($trace_item['args'] AS $arg) { $argType = gettype($arg); switch ($argType) { case 'integer': case 'double': $argVal = $arg; break; case 'string': $len = strlen($arg); $argVal = "'" . ($len > 30 ? substr($arg, 0, 25) . '[len:' . $len . ']' : $arg) . "'"; break; case 'array': $argVal = 'array[len:' . count($arg) . ']'; break; case 'boolean': $argVal = $arg ? 'true' : 'false'; break; case 'object': $argVal = get_class($arg); break; case 'resource': $argVal = 'resource[type:' . get_resource_type($arg) . ']'; break; default: $argVal = $argType; break; } $param[] = $argVal; } } $param = implode(', ', $param); } $trace_item['file'] = str_replace($dir, '[path]', $trace_item['file']); $traceString .= "#$index: " . $trace_item['class'] . $trace_item['type'] . $trace_item['function'] . "($param)" . ($trace_item['file'] ? ' called in ' . $trace_item['file'] . ' on line ' . $trace_item['line'] : '') . "\n"; } $traceString = htmlspecialchars($traceString); return $traceString; } public static function getExceptionTrace($e) { return '## ' . $e->getFile() . '(' . $e->getLine() . ") Exception Thrown \n" . $e->getTraceAsString(); } } 