diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -2688,6 +2688,7 @@ 'PhabricatorIconSetEditField' => 'applications/transactions/editfield/PhabricatorIconSetEditField.php', 'PhabricatorIconSetIcon' => 'applications/files/iconset/PhabricatorIconSetIcon.php', 'PhabricatorImageMacroRemarkupRule' => 'applications/macro/markup/PhabricatorImageMacroRemarkupRule.php', + 'PhabricatorImageRemarkupRule' => 'applications/files/markup/PhabricatorImageRemarkupRule.php', 'PhabricatorImageTransformer' => 'applications/files/PhabricatorImageTransformer.php', 'PhabricatorImagemagickSetupCheck' => 'applications/config/check/PhabricatorImagemagickSetupCheck.php', 'PhabricatorInFlightErrorView' => 'applications/config/view/PhabricatorInFlightErrorView.php', @@ -7523,6 +7524,7 @@ 'PhabricatorIconSetEditField' => 'PhabricatorEditField', 'PhabricatorIconSetIcon' => 'Phobject', 'PhabricatorImageMacroRemarkupRule' => 'PhutilRemarkupRule', + 'PhabricatorImageRemarkupRule' => 'PhutilRemarkupRule', 'PhabricatorImageTransformer' => 'Phobject', 'PhabricatorImagemagickSetupCheck' => 'PhabricatorSetupCheck', 'PhabricatorInFlightErrorView' => 'AphrontView', diff --git a/src/applications/files/application/PhabricatorFilesApplication.php b/src/applications/files/application/PhabricatorFilesApplication.php --- a/src/applications/files/application/PhabricatorFilesApplication.php +++ b/src/applications/files/application/PhabricatorFilesApplication.php @@ -37,6 +37,7 @@ public function getRemarkupRules() { return array( new PhabricatorEmbedFileRemarkupRule(), + new PhabricatorImageRemarkupRule(), ); } diff --git a/src/applications/files/markup/PhabricatorImageRemarkupRule.php b/src/applications/files/markup/PhabricatorImageRemarkupRule.php new file mode 100644 --- /dev/null +++ b/src/applications/files/markup/PhabricatorImageRemarkupRule.php @@ -0,0 +1,77 @@ + null, + 'alt' => null, + 'href' => null, + 'layout' => null, + 'width' => null, + 'height' => null, + ); + if ($this->isURI($matches[2])) { + $args['uri'] = new PhutilURI($matches[2]); + } else { + $parser = new PhutilSimpleOptions(); + $keys = $parser->parse($matches[2]); + + $uri_key = ''; + foreach (array('src', 'uri', 'url') as $key) { + if (array_key_exists($key, $keys)) { + $uri_key = $key; + } + } + if ($uri_key) { + $args['uri'] = new PhutilURI($keys[$uri_key]); + } + $args += $keys; + } + + $args += $defaults; + if ($args['uri']) { + $src_uri = id(new PhutilURI('/file/imageproxy/')) + ->setQueryParam('uri', (string)$args['uri']); + $img = $this->newTag( + 'img', + array( + 'src' => $src_uri, + 'alt' => $args['alt'], + 'href' => $args['href'], + 'layout' => $args['layout'], + 'width' => $args['width'], + 'height' => $args['height'], + )); + return $this->getEngine()->storeText($img); + } else { + return $matches[0]; + } + } + + // This can probably be simplified a ton. Even if kept as-is, this probably + // isn't the right place to put it + private function isURI($uri_string) { + $regex = '(https?\:\/\/)?'; // protocol + // username and password. Can maybe just remove this piece. + $regex .= '([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?'; + $regex .= '([a-z0-9-.]*)\.([a-z]{2,3})'; // Host or IP + $regex .= '(\:[0-9]{2,5})?'; // Port + $regex .= '(\/([a-z0-9+\$_-]\.?)+)*\/?'; // Path + $regex .= '(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?'; // Query string + $regex .= '(#[a-z_.-][a-z0-9+\$_.-]*)?'; // Anchor + + return preg_match("~^$regex$~i", $uri_string); + } +}