'; $realPathResult = realpath($dir); if ($realPathResult !== false) { return $realPathResult; } if (($dir == '/') || (strlen($dir) < 4)) { return $dir; } $levelsUp++; } return '/'; } public static function pathBeginsWithSymLinksExpanded($input, $beginsWith, $errorMsg = 'Path is outside allowed path') { $closestExistingFolder = self::findClosestExistingFolderSymLinksExpanded($input); //throw new SanityException('hm.' . $input . ' :
' . $closestExistingFolder); self::pathBeginsWith($closestExistingFolder, $beginsWith, $errorMsg); } public static function absPathExists($input, $errorMsg = 'Path does not exist') { self::absPath($input); if (@!file_exists($input)) { throw new SanityException($errorMsg); } return $input; } public static function absPathExistsAndIsDir( $input, $errorMsg = 'Path points to a file (it should point to a directory)' ) { self::absPathExists($input); if (!is_dir($input)) { throw new SanityException($errorMsg); } return $input; } public static function absPathExistsAndIsFile( $input, $errorMsg = 'Path points to a directory (it should not do that)' ) { self::absPathExists($input, 'File does not exist'); if (@is_dir($input)) { throw new SanityException($errorMsg); } return $input; } public static function absPathExistsAndIsNotDir( $input, $errorMsg = 'Path points to a directory (it should point to a file)' ) { self::absPathExistsAndIsFile($input, $errorMsg); return $input; } public static function pregMatch($pattern, $input, $errorMsg = 'Does not match expected pattern') { self::noNUL($input); self::mustBeString($input); if (!preg_match($pattern, $input)) { throw new SanityException($errorMsg); } return $input; } public static function isJSONArray($input, $errorMsg = 'Not a JSON array') { self::noNUL($input); self::mustBeString($input); self::notEmpty($input); if ((strpos($input, '[') !== 0) || (!is_array(json_decode($input)))) { throw new SanityException($errorMsg); } return $input; } public static function isJSONObject($input, $errorMsg = 'Not a JSON object') { self::noNUL($input); self::mustBeString($input); self::notEmpty($input); if ((strpos($input, '{') !== 0) || (!is_object(json_decode($input)))) { throw new SanityException($errorMsg); } return $input; } }