/usr/share/php/texy/src/Texy/modules/TexyLinkModule.php is in php-texy 2.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | <?php
/**
* This file is part of the Texy! (http://texy.info)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/
/**
* Links module.
*
* @author David Grudl
*/
final class TexyLinkModule extends TexyModule
{
/** @var string root of relative links */
public $root = '';
/** @var string image popup class */
public $imageClass;
/** @var string image popup event */
public $imageOnClick = 'return !popupImage(this.href)'; //
/** @var string class 'popup' event */
public $popupOnClick = 'return !popup(this.href)';
/** @var bool always use rel="nofollow" for absolute links? */
public $forceNoFollow = FALSE;
/** @var bool shorten URLs to more readable form? */
public $shorten = TRUE;
/** @var array link references */
private $references = array();
/** @var array */
private static $livelock;
private static $EMAIL;
public function __construct($texy)
{
$this->texy = $texy;
$texy->allowed['link/definition'] = TRUE;
$texy->addHandler('newReference', array($this, 'solveNewReference'));
$texy->addHandler('linkReference', array($this, 'solve'));
$texy->addHandler('linkEmail', array($this, 'solveUrlEmail'));
$texy->addHandler('linkURL', array($this, 'solveUrlEmail'));
$texy->addHandler('beforeParse', array($this, 'beforeParse'));
// [reference]
$texy->registerLinePattern(
array($this, 'patternReference'),
'#(\[[^\[\]\*\n'.TexyPatterns::MARK.']++\])#U',
'link/reference'
);
// direct url and email
$texy->registerLinePattern(
array($this, 'patternUrlEmail'),
'#(?<=^|[\s([<:\x17])(?:https?://|www\.|ftp://)[0-9.'.TexyPatterns::CHAR.'-][/\d'.TexyPatterns::CHAR.'+\.~%&?@=_:;\#!,\x{ad}-]{1,1000}[/\d'.TexyPatterns::CHAR.'+~%?@=_\#]#u',
'link/url',
'#(?:https?://|www\.|ftp://)#u'
);
self::$EMAIL = '['.TexyPatterns::CHAR.'][0-9.+_'.TexyPatterns::CHAR.'-]{0,63}@[0-9.+_'.TexyPatterns::CHAR.'\x{ad}-]{1,252}\.['.TexyPatterns::CHAR.'\x{ad}]{2,19}';
$texy->registerLinePattern(
array($this, 'patternUrlEmail'),
'#(?<=^|[\s([<\x17])'.self::$EMAIL.'#u',
'link/email',
'#'.self::$EMAIL.'#u'
);
}
/**
* Text pre-processing.
* @param Texy
* @param string
* @return void
*/
public function beforeParse($texy, & $text)
{
self::$livelock = array();
// [la trine]: http://www.latrine.cz/ text odkazu .(title)[class]{style}
if (!empty($texy->allowed['link/definition'])) {
$text = TexyRegexp::replace(
$text,
'#^\[([^\[\]\#\?\*\n]{1,100})\]: ++(\S{1,1000})(\ .{1,1000})?'.TexyPatterns::MODIFIER.'?\s*()$#mUu',
array($this, 'patternReferenceDef')
);
}
}
/**
* Callback for: [la trine]: http://www.latrine.cz/ text odkazu .(title)[class]{style}.
*
* @param array regexp matches
* @return string
* @internal
*/
public function patternReferenceDef($matches)
{
list(, $mRef, $mLink, $mLabel, $mMod) = $matches;
// [1] => [ (reference) ]
// [2] => link
// [3] => ...
// [4] => .(title)[class]{style}
$link = new TexyLink($mLink);
$link->label = trim($mLabel);
$link->modifier->setProperties($mMod);
$this->checkLink($link);
$this->addReference($mRef, $link);
return '';
}
/**
* Callback for: [ref].
*
* @param TexyLineParser
* @param array regexp matches
* @param string pattern name
* @return TexyHtml|string|FALSE
*/
public function patternReference($parser, $matches)
{
list(, $mRef) = $matches;
// [1] => [ref]
$tx = $this->texy;
$name = substr($mRef, 1, -1);
$link = $this->getReference($name);
if (!$link) {
return $tx->invokeAroundHandlers('newReference', $parser, array($name));
}
$link->type = TexyLink::BRACKET;
if ($link->label != '') { // NULL or ''
// prevent circular references
if (isset(self::$livelock[$link->name])) {
$content = $link->label;
} else {
self::$livelock[$link->name] = TRUE;
$el = TexyHtml::el();
$lineParser = new TexyLineParser($tx, $el);
$lineParser->parse($link->label);
$content = $el->toString($tx);
unset(self::$livelock[$link->name]);
}
} else {
$content = $this->textualUrl($link);
$content = $this->texy->protect($content, Texy::CONTENT_TEXTUAL);
}
return $tx->invokeAroundHandlers('linkReference', $parser, array($link, $content));
}
/**
* Callback for: http://davidgrudl.com david@grudl.com.
*
* @param TexyLineParser
* @param array regexp matches
* @param string pattern name
* @return TexyHtml|string|FALSE
*/
public function patternUrlEmail($parser, $matches, $name)
{
list($mURL) = $matches;
// [0] => URL
$link = new TexyLink($mURL);
$this->checkLink($link);
return $this->texy->invokeAroundHandlers(
$name === 'link/email' ? 'linkEmail' : 'linkURL',
$parser,
array($link)
);
}
/**
* Adds new named reference.
*
* @param string reference name
* @param TexyLink
* @return void
*/
public function addReference($name, TexyLink $link)
{
$link->name = TexyUtf::strtolower($name);
$this->references[$link->name] = $link;
}
/**
* Returns named reference.
*
* @param string reference name
* @return TexyLink reference descriptor (or FALSE)
*/
public function getReference($name)
{
$name = TexyUtf::strtolower($name);
if (isset($this->references[$name])) {
return clone $this->references[$name];
} else {
$pos = strpos($name, '?');
if ($pos === FALSE) {
$pos = strpos($name, '#');
}
if ($pos !== FALSE) { // try to extract ?... #... part
$name2 = substr($name, 0, $pos);
if (isset($this->references[$name2])) {
$link = clone $this->references[$name2];
$link->URL .= substr($name, $pos);
return $link;
}
}
}
return FALSE;
}
/**
* @param string
* @param string
* @param string
* @return TexyLink
*/
public function factoryLink($dest, $mMod, $label)
{
$tx = $this->texy;
$type = TexyLink::COMMON;
// [ref]
if (strlen($dest)>1 && $dest{0} === '[' && $dest{1} !== '*') {
$type = TexyLink::BRACKET;
$dest = substr($dest, 1, -1);
$link = $this->getReference($dest);
// [* image *]
} elseif (strlen($dest)>1 && $dest{0} === '[' && $dest{1} === '*') {
$type = TexyLink::IMAGE;
$dest = trim(substr($dest, 2, -2));
$image = $tx->imageModule->getReference($dest);
if ($image) {
$link = new TexyLink($image->linkedURL === NULL ? $image->URL : $image->linkedURL);
$link->modifier = $image->modifier;
}
}
if (empty($link)) {
$link = new TexyLink(trim($dest));
$this->checkLink($link);
}
if (strpos($link->URL, '%s') !== FALSE) {
$link->URL = str_replace('%s', urlencode($tx->stringToText($label)), $link->URL);
}
$link->modifier->setProperties($mMod);
$link->type = $type;
return $link;
}
/**
* Finish invocation.
*
* @param TexyHandlerInvocation handler invocation
* @param TexyLink
* @param TexyHtml|string
* @return TexyHtml|string
*/
public function solve($invocation, $link, $content = NULL)
{
if ($link->URL == NULL) {
return $content;
}
$tx = $this->texy;
$el = TexyHtml::el('a');
if (empty($link->modifier)) {
$nofollow = $popup = FALSE;
} else {
$nofollow = isset($link->modifier->classes['nofollow']);
$popup = isset($link->modifier->classes['popup']);
unset($link->modifier->classes['nofollow'], $link->modifier->classes['popup']);
$el->attrs['href'] = NULL; // trick - move to front
$link->modifier->decorate($tx, $el);
}
if ($link->type === TexyLink::IMAGE) {
// image
$el->attrs['href'] = Texy::prependRoot($link->URL, $tx->imageModule->linkedRoot);
if ($this->imageClass) {
$el->attrs['class'][] = $this->imageClass;
} else {
$el->attrs['onclick'] = $this->imageOnClick;
}
} else {
$el->attrs['href'] = Texy::prependRoot($link->URL, $this->root);
// rel="nofollow"
if ($nofollow || ($this->forceNoFollow && strpos($el->attrs['href'], '//') !== FALSE)) {
$el->attrs['rel'] = 'nofollow';
}
}
// popup on click
if ($popup) {
$el->attrs['onclick'] = $this->popupOnClick;
}
if ($content !== NULL) {
$el->add($content);
}
$tx->summary['links'][] = $el->attrs['href'];
return $el;
}
/**
* Finish invocation.
*
* @param TexyHandlerInvocation handler invocation
* @param TexyLink
* @return TexyHtml|string
*/
public function solveUrlEmail($invocation, $link)
{
$content = $this->textualUrl($link);
$content = $this->texy->protect($content, Texy::CONTENT_TEXTUAL);
return $this->solve(NULL, $link, $content);
}
/**
* Finish invocation.
*
* @param TexyHandlerInvocation handler invocation
* @param string
* @return FALSE
*/
public function solveNewReference($invocation, $name)
{
// no change
return FALSE;
}
/**
* Checks and corrects $URL.
* @param TexyLink
* @return void
*/
private function checkLink($link)
{
// remove soft hyphens; if not removed by Texy::process()
$link->URL = str_replace("\xC2\xAD", '', $link->URL);
if (strncasecmp($link->URL, 'www.', 4) === 0) {
// special supported case
$link->URL = 'http://' . $link->URL;
} elseif (preg_match('#'.self::$EMAIL.'$#Au', $link->URL)) {
// email
$link->URL = 'mailto:' . $link->URL;
} elseif (!$this->texy->checkURL($link->URL, Texy::FILTER_ANCHOR)) {
$link->URL = NULL;
} else {
$link->URL = str_replace('&', '&', $link->URL); // replace unwanted &
}
}
/**
* Returns textual representation of URL.
* @param TexyLink
* @return string
*/
private function textualUrl($link)
{
if ($this->texy->obfuscateEmail && preg_match('#^'.self::$EMAIL.'$#u', $link->raw)) { // email
return str_replace('@', "@<!-- -->", $link->raw);
}
if ($this->shorten && preg_match('#^(https?://|ftp://|www\.|/)#i', $link->raw)) {
$raw = strncasecmp($link->raw, 'www.', 4) === 0 ? 'none://' . $link->raw : $link->raw;
// parse_url() in PHP damages UTF-8 - use regular expression
if (!preg_match('~^(?:(?P<scheme>[a-z]+):)?(?://(?P<host>[^/?#]+))?(?P<path>(?:/|^)(?!/)[^?#]*)?(?:\?(?P<query>[^#]*))?(?:#(?P<fragment>.*))?()$~', $raw, $parts)) {
return $link->raw;
}
$res = '';
if ($parts['scheme'] !== '' && $parts['scheme'] !== 'none') {
$res .= $parts['scheme'] . '://';
}
if ($parts['host'] !== '') {
$res .= $parts['host'];
}
if ($parts['path'] !== '') {
$res .= (iconv_strlen($parts['path'], 'UTF-8') > 16 ? ("/\xe2\x80\xa6" . iconv_substr($parts['path'], -12, 12, 'UTF-8')) : $parts['path']);
}
if ($parts['query'] !== '') {
$res .= iconv_strlen($parts['query'], 'UTF-8') > 4 ? "?\xe2\x80\xa6" : ('?' . $parts['query']);
} elseif ($parts['fragment'] !== '') {
$res .= iconv_strlen($parts['fragment'], 'UTF-8') > 4 ? "#\xe2\x80\xa6" : ('#' . $parts['fragment']);
}
return $res;
}
return $link->raw;
}
}
final class TexyLink extends TexyObject
{
/** @see $type */
const
COMMON = 1,
BRACKET = 2,
IMAGE = 3;
/** @var string URL in resolved form */
public $URL;
/** @var string URL as written in text */
public $raw;
/** @var TexyModifier */
public $modifier;
/** @var int how was link created? */
public $type = TexyLink::COMMON;
/** @var string optional label, used by references */
public $label;
/** @var string reference name (if is stored as reference) */
public $name;
public function __construct($URL)
{
$this->URL = $URL;
$this->raw = $URL;
$this->modifier = new TexyModifier;
}
public function __clone()
{
if ($this->modifier) {
$this->modifier = clone $this->modifier;
}
}
}
|