public static function parse($Name)
{
$Result = false;
$Name = str_replace('\\', '/', $Name);
$PathUploads = str_replace('\\', '/', PATH_UPLOADS);
if (preg_match('`^https?://`', $Name)) {
$Result = array('Name' => $Name, 'Type' => 'external', 'SaveName' => $Name, 'SaveFormat' => '%s', 'Url' => $Name);
return $Result;
} elseif (stringBeginsWith($Name, $PathUploads)) {
$Name = ltrim(substr($Name, strlen($PathUploads)), '/');
// This is an upload.
$Result = array('Name' => $Name, 'Type' => '', 'SaveName' => $Name, 'SaveFormat' => '%s');
} elseif (preg_match('`^~([^/]*)/(.*)$`', $Name, $Matches)) {
// The first part of the name tells us the type.
$Type = $Matches[1];
$Name = $Matches[2];
$Result = array('Name' => $Name, 'Type' => $Type, 'SaveName' => "~{$Type}/{$Name}", 'SaveFormat' => "~{$Type}/%s");
} else {
$Parts = parse_url($Name);
if (empty($Parts['scheme'])) {
$Name = ltrim($Name, '/');
// This is an upload in the uploads folder.
$Result = array('Name' => $Name, 'Type' => '', 'SaveName' => $Name, 'SaveFormat' => '%s');
} else {
// This is a url in the format type:://domain/path.
$Result = array('Name' => ltrim(val('path', $Parts), '/'), 'Type' => $Parts['scheme'], 'Domain' => val('host', $Parts));
$SaveFormat = "{$Result['Type']}://{$Result['Domain']}/%s";
$Result['SaveName'] = sprintf($SaveFormat, $Result['Name']);
$Result['SaveFormat'] = $SaveFormat;
}
}
if (!empty($Result['Domain'])) {
$UrlPrefix = self::urls("{$Result['Type']}://{$Result['Domain']}");
} else {
$UrlPrefix = self::urls($Result['Type']);
}
if ($UrlPrefix === false) {
$Result['Url'] = false;
} else {
$Result['Url'] = $UrlPrefix . '/' . $Result['Name'];
}
return $Result;
}