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
|
<?php
namespace SMW\DataValues;
use SMWURIValue as UriValue;
/**
* https://www.ietf.org/rfc/rfc3986.txt describes:
*
* " ... Uniform Resource Identifier (URI) is a compact sequence of characters
* that identifies an abstract or physical resource." with "... Uniform Resource
* Locator" (URL) refers to the subset of URIs that provide a means of locating
* the resource by describing its primary access mechanism ..."
*
* @license GNU GPL v2+
* @since 2.5
*
* @author mwjames
*/
class ExternalFormatterUriValue extends UriValue {
/**
* @param string $typeid
*/
public function __construct( $typeid = '' ) {
parent::__construct( '__peru' );
}
/**
* @see DataValue::parseUserValue
*
* @param string $value
*/
protected function parseUserValue( $value ) {
if ( $value === '' ) {
$this->addErrorMsg( 'smw_emptystring' );
return;
}
if ( filter_var( $value, FILTER_VALIDATE_URL ) === false && preg_match( '/((mailto\:|(news|urn|tel|(ht|f)tp(s?))\:\/\/){1}\S+)/u', $value ) === false ) {
$this->addErrorMsg( [ 'smw-datavalue-external-formatter-invalid-uri', $value ] );
return;
}
if ( strpos( $value, '$1' ) === false ) {
$this->addErrorMsg( 'smw-datavalue-external-formatter-uri-missing-placeholder' );
return;
}
parent::parseUserValue( $value );
}
/**
* @since 2.5
*
* @param string $value
*
* @return string
*/
public function getUriWithPlaceholderSubstitution( $value ) {
if ( !$this->isValid() ) {
return '';
}
// Avoid already encoded values like `W%D6LLEKLA01` to be
// encoded twice
$value = $this->encode( rawurldecode( $value ) );
// %241 == encoded $1
return str_replace( [ '%241', '$1' ], [ '$1', $value ], $this->getDataItem()->getUri() );
}
// http://php.net/manual/en/function.urlencode.php#97969
private function encode( $string ) {
return str_replace(
[ '%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D' ],
[ '!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]" ],
urlencode( $string )
);
}
}
|