$decimal * $tolerance); return [ $numerator, $denominator ]; } public static function ensureHasTrailingSlash($string) { if (strlen($string) > 0 && substr($string, strlen($string) - 1, 1) != '/') { $string .= '/'; } return $string; } public static function getEnvironmentFilePath() { return sprintf('%s/.env', dirname(dirname(__DIR__))); } public static function getEnvironmentSetting($settingName, $envFile = null) { $envFile = $envFile ?? MiscHelper::getEnvironmentFilePath(); if (!file_exists($envFile)) { return null; } $matches = null; if (preg_match(sprintf('/^\s*%s\s*=\s*(.+)$/im', preg_quote($settingName)), file_get_contents($envFile), $matches)) { return trim($matches[1]); } return null; } public static function isAppInstalled() { return MiscHelper::getEnvironmentSetting('APP_INSTALLED'); } public static function isExecEnabled() { $disabled = explode(',', ini_get('disable_functions')); return !in_array('exec', $disabled); } /** * Tests whether the provided URL belongs to the current application (i.e. both scheme and hostname match.) * @param $url * @return bool */ public static function isSafeUrl($url) { $parts = parse_url($url); $validParts = parse_url(url('/')); return ($parts['scheme'] == $validParts['scheme'] && $parts['host'] == $validParts['host']); } public static function randomString($length = 10) { $seed = 'abcdefghijklmnopqrstuvwxyz01234567890'; $string = ''; while (strlen($string) < $length) { $string .= substr($seed, rand(0, strlen($seed) - 1), 1); } return $string; } public static function setEnvironmentSetting($settingName, $value) { if (is_null(MiscHelper::getEnvironmentSetting($settingName))) { return file_put_contents(MiscHelper::getEnvironmentFilePath(), sprintf('%s=%s', $settingName, $value) . PHP_EOL, FILE_APPEND); } return false; } }