public function testAllowingUpperCaseSchemes() { $urlLinker = new UrlLinker(false, true); $text = '<div>HTTP://example.com</div>'; $expectedText = '<div><a href="HTTP://example.com">example.com</a></div>'; $this->assertSame($expectedText, $urlLinker->linkUrlsAndEscapeHtml($text)); $html = '<div>HTTP://example.com</div>'; $expectedHtml = '<div><a href="HTTP://example.com">example.com</a></div>'; $this->assertSame($expectedHtml, $urlLinker->linkUrlsInTrustedHtml($html)); }
/** * @param string $data * @return string */ public function convertLinks($data) { $linker = new UrlLinker(true, false); $data = $linker->linkUrlsInTrustedHtml($data); $config = HTMLPurifier_Config::createDefault(); // Append target="_blank" to all link (a) elements $config->set('HTML.TargetBlank', true); // allow cid, http and ftp $config->set('URI.AllowedSchemes', ['http' => true, 'https' => true, 'ftp' => true, 'mailto' => true]); // Disable the cache since ownCloud has no really appcache // TODO: Fix this - requires https://github.com/owncloud/core/issues/10767 to be fixed $config->set('Cache.DefinitionImpl', null); /** @var HTMLPurifier_HTMLDefinition $uri */ $uri = $config->getDefinition('HTML'); $uri->info_attr_transform_post['noreferrer'] = new TransformNoReferrer(); $purifier = new HTMLPurifier($config); return $purifier->purify($data); }
/** * process a message: * - replace urls with links * - replace object references with links * @param varchar $message */ public static function processAndFormatMessage($message, $replacements = 'user,object,url') { if (empty($message)) { return $message; } $replacements = Util\toTrimmedArray($replacements); // replace urls with links if (in_array('url', $replacements)) { $message = \Kwi\UrlLinker::getInstance()->linkUrlsAndEscapeHtml($message); } //replace object references with links if (in_array('object', $replacements) && preg_match_all('/(.?)#(\\d+)(.?)/', $message, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { // check if not a html code if ($match[1] == '&' && $match[3] == ';') { continue; } $templateId = Objects::getTemplateId($match[2]); $name = Objects::getName($match[2]); $name = strlen($name) > 30 ? mb_substr($name, 0, 30) . '…' : $name; $message = str_replace($match[0], $match[1] . '<a class="click obj-ref" itemid="' . $match[2] . '" templateid= "' . $templateId . '" title="' . $name . '"' . '>#' . $match[2] . '</a>' . $match[3], $message); } } //replace users with their names if (in_array('user', $replacements) && preg_match_all('/@([\\w\\.\\-]+[\\w])/', $message, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { $userId = DM\User::getIdByName($match[1]); if (is_numeric($userId)) { $userName = $match[1]; $message = str_replace($match[0], '<span class="cDB user-ref" title="' . User::getDisplayName($userId) . '">@' . $userName . '</span>', $message); } } } return $message; }
/** * @param string $text */ protected function linkify($text) { return $this->urlLinker->linkUrlsAndEscapeHtml($text); }
/** * @param string $text */ protected function linkify($text) { return $this->urlLinker->linkUrlsInTrustedHtml($text); }