17d18b5caSAndreas Gohr<?php 27d18b5caSAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\types; 47d18b5caSAndreas Gohr 5ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\ValidationException; 67d18b5caSAndreas Gohr 7d6d97f60SAnna Dabrowskaclass Url extends Text 8d6d97f60SAnna Dabrowska{ 9*7fe2cdf2SAndreas Gohr protected $config = [ 10*7fe2cdf2SAndreas Gohr 'autoscheme' => 'https', 11*7fe2cdf2SAndreas Gohr 'prefix' => '', 12*7fe2cdf2SAndreas Gohr 'postfix' => '', 13*7fe2cdf2SAndreas Gohr 'fixedtitle' => '', 14*7fe2cdf2SAndreas Gohr 'autoshorten' => true 15*7fe2cdf2SAndreas Gohr ]; 16e76a6885SAndreas Gohr 177d18b5caSAndreas Gohr /** 187d18b5caSAndreas Gohr * The final string should be an URL 197d18b5caSAndreas Gohr * 2023169abeSAndreas Gohr * @param string $rawvalue 21806eec82SAndreas Gohr * @return int|string|void 227d18b5caSAndreas Gohr */ 23d6d97f60SAnna Dabrowska public function validate($rawvalue) 24d6d97f60SAnna Dabrowska { 2523169abeSAndreas Gohr $rawvalue = parent::validate($rawvalue); 26806eec82SAndreas Gohr 2723169abeSAndreas Gohr $url = $this->buildURL($rawvalue); 287d18b5caSAndreas Gohr 297d18b5caSAndreas Gohr $schemes = getSchemes(); 307234bfb1Ssplitbrain $regex = '^(' . implode('|', $schemes) . '):\/\/.+'; 317d18b5caSAndreas Gohr if (!preg_match("/$regex/i", $url)) { 327d18b5caSAndreas Gohr throw new ValidationException('Url invalid', $url); 337d18b5caSAndreas Gohr } 34806eec82SAndreas Gohr 3523169abeSAndreas Gohr return $rawvalue; 367d18b5caSAndreas Gohr } 377d18b5caSAndreas Gohr 387d18b5caSAndreas Gohr /** 397d18b5caSAndreas Gohr * @param string $value 407d18b5caSAndreas Gohr * @param \Doku_Renderer $R 417d18b5caSAndreas Gohr * @param string $mode 427d18b5caSAndreas Gohr * @return bool 437d18b5caSAndreas Gohr */ 44d6d97f60SAnna Dabrowska public function renderValue($value, \Doku_Renderer $R, $mode) 45d6d97f60SAnna Dabrowska { 46e76a6885SAndreas Gohr $url = $this->buildURL($value); 47c7273e8eSAndreas Gohr $title = $this->generateTitle($url); 48c7273e8eSAndreas Gohr $R->externallink($url, $title); 497d18b5caSAndreas Gohr return true; 507d18b5caSAndreas Gohr } 517d18b5caSAndreas Gohr 52e76a6885SAndreas Gohr /** 53c7273e8eSAndreas Gohr * Make a label for the link 54c7273e8eSAndreas Gohr * 55c7273e8eSAndreas Gohr * @param $url 56c7273e8eSAndreas Gohr * @return string 57c7273e8eSAndreas Gohr */ 5895507c08SAnna Dabrowska protected function generateTitle($url) 5995507c08SAnna Dabrowska { 60c7273e8eSAndreas Gohr if ($this->config['fixedtitle']) return $this->config['fixedtitle']; 61c7273e8eSAndreas Gohr if (!$this->config['autoshorten']) return $url; 62c7273e8eSAndreas Gohr 63c7273e8eSAndreas Gohr $parsed = parse_url($url); 64c7273e8eSAndreas Gohr 65c7273e8eSAndreas Gohr $title = $parsed['host']; 66c7273e8eSAndreas Gohr $title = preg_replace('/^www\./i', '', $title); 67c7273e8eSAndreas Gohr if (isset($parsed['path']) && $parsed['path'] === '/') { 68c7273e8eSAndreas Gohr unset($parsed['path']); 69c7273e8eSAndreas Gohr } 70c7273e8eSAndreas Gohr if ( 71c7273e8eSAndreas Gohr isset($parsed['path']) || 72c7273e8eSAndreas Gohr isset($parsed['query']) || 73c7273e8eSAndreas Gohr isset($parsed['fragment']) 74c7273e8eSAndreas Gohr ) { 75c7273e8eSAndreas Gohr $title .= '/…'; 76c7273e8eSAndreas Gohr } 77c7273e8eSAndreas Gohr return $title; 78c7273e8eSAndreas Gohr } 79c7273e8eSAndreas Gohr 80c7273e8eSAndreas Gohr /** 81e76a6885SAndreas Gohr * Creates the full URL and applies the autoscheme if needed 82e76a6885SAndreas Gohr * 83e76a6885SAndreas Gohr * @param string $value 84e76a6885SAndreas Gohr * @return string 85e76a6885SAndreas Gohr */ 86d6d97f60SAnna Dabrowska protected function buildURL($value) 87d6d97f60SAnna Dabrowska { 88e76a6885SAndreas Gohr $url = $this->config['prefix'] . trim($value) . $this->config['postfix']; 89e76a6885SAndreas Gohr 90e76a6885SAndreas Gohr if (!preg_match('/\w+:\/\//', $url)) { 91e76a6885SAndreas Gohr $url = $this->config['autoscheme'] . '://' . $url; 92e76a6885SAndreas Gohr } 93e76a6885SAndreas Gohr 94e76a6885SAndreas Gohr return $url; 95e76a6885SAndreas Gohr } 967d18b5caSAndreas Gohr} 97