1<?php 2 3namespace dokuwiki\plugin\struct\types; 4 5use dokuwiki\plugin\struct\meta\ValidationException; 6 7class Url extends Text 8{ 9 10 protected $config = array( 11 'autoscheme' => 'https', 12 'prefix' => '', 13 'postfix' => '', 14 ); 15 16 /** 17 * The final string should be an URL 18 * 19 * @param string $rawvalue 20 * @return int|string|void 21 */ 22 public function validate($rawvalue) 23 { 24 $rawvalue = parent::validate($rawvalue); 25 26 $url = $this->buildURL($rawvalue); 27 28 $schemes = getSchemes(); 29 $regex = '^(' . join('|', $schemes) . '):\/\/.+'; 30 if (!preg_match("/$regex/i", $url)) { 31 throw new ValidationException('Url invalid', $url); 32 } 33 34 return $rawvalue; 35 } 36 37 /** 38 * @param string $value 39 * @param \Doku_Renderer $R 40 * @param string $mode 41 * @return bool 42 */ 43 public function renderValue($value, \Doku_Renderer $R, $mode) 44 { 45 $url = $this->buildURL($value); 46 $R->externallink($url); 47 return true; 48 } 49 50 /** 51 * Creates the full URL and applies the autoscheme if needed 52 * 53 * @param string $value 54 * @return string 55 */ 56 protected function buildURL($value) 57 { 58 $url = $this->config['prefix'] . trim($value) . $this->config['postfix']; 59 60 if (!preg_match('/\w+:\/\//', $url)) { 61 $url = $this->config['autoscheme'] . '://' . $url; 62 } 63 64 return $url; 65 } 66} 67