Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezuri.php
Go to the documentation of this file.
1 <?php
27 class eZURI
28 {
34 public $URI;
35
36 public $OriginalURI;
37
43 public $URIArray;
44
50 public $Index;
51
57 public $UserArray;
58
68 private static $transformURIMode = "relative";
69
76 public function __construct( $uri )
77 {
78 $this->setURIString( $uri );
79 }
80
89 public static function decodeIRI( $str )
90 {
91 $str = urldecode( $str ); // Decode %xx entries, we now have a utf-8 string
92 $codec = eZTextCodec::instance( 'utf-8' ); // Make sure string is converted from utf-8 to internal encoding
93 return $codec->convertString( $str );
94 }
95
104 public static function encodeIRI( $str )
105 {
106 $codec = eZTextCodec::instance( false, 'utf-8' );
107 $str = $codec->convertString( $str ); // Make sure the string is in utf-8
108 $out = explode( "/", $str ); // Don't encode the slashes
109 foreach ( $out as $i => $o )
110 {
111 if ( preg_match( "#^[\‍(]([a-zA-Z0-9_]+)[\‍)]#", $o, $m ) )
112 {
113 // Don't encode '(' and ')' in user parameters
114 $out[$i] = '(' . urlencode( $m[1] ) . ')';
115 }
116 else
117 {
118 $out[$i] = urlencode( $o ); // Let PHP do the rest
119 }
120 }
121 $tmp = join( "/", $out );
122 // Don't encode '~' in URLs
123 $tmp = str_replace( '%7E', '~', $tmp );
124 return $tmp;
125 }
126
134 public static function codecURL( $url, $encode )
135 {
136 $originalLocale = setlocale( LC_CTYPE, "0" );
137 setlocale( LC_CTYPE, 'C' );
138 // Parse URL and encode the path.
139 $data = parse_url( $url );
140 setlocale( LC_CTYPE, $originalLocale );
141
142 if ( isset( $data['path'] ) )
143 {
144 if ( $encode )
145 $data['path'] = eZURI::encodeIRI( $data['path'] ); // Make sure it is encoded to IRI format
146 else
147 $data['path'] = eZURI::decodeIRI( $data['path'] ); // Make sure it is dencoded to internal encoding
148 }
149
150 // Reconstruct the URL
151 $host = '';
152 $preHost = '';
153 if ( isset( $data['user'] ) )
154 {
155 if ( isset( $data['pass'] ) )
156 $preHost .= $data['user'] . ':' . $data['pass'] . '@';
157 else
158 $preHost .= $data['user'] . '@';
159 }
160 if ( isset( $data['host'] ) )
161 {
162 if ( isset( $data['port'] ) )
163 $host = $preHost . $data['host'] . ':' . $data['port'];
164 else
165 $host = $preHost . $data['host'];
166 }
167 $url = '';
168 if ( isset( $data['scheme'] ) )
169 $url = $data['scheme'] . '://' . $host;
170 else if ( strlen( $host ) > 0 )
171 $url = '//' . $host;
172 if ( isset( $data['path'] ) )
173 {
174 $url .= $data['path'];
175 }
176 if ( isset( $data['query'] ) )
177 {
178 $url .= '?' . $data['query'];
179 }
180 if ( isset( $data['fragment'] ) )
181 {
182 $url .= '#' . $data['fragment'];
183 }
184
185 return $url;
186 }
187
194 public static function encodeURL( $url )
195 {
196 return eZURI::codecURL( $url, true );
197 }
198
205 public static function decodeURL( $url )
206 {
207 return eZURI::codecURL( $url, false );
208 }
209
220 public function setURIString( $uri, $fullInitialize = true )
221 {
222 if ( strlen( $uri ) > 0 and
223 $uri[0] == '/' )
224 $uri = substr( $uri, 1 );
225
226 $uri = eZURI::decodeIRI( $uri );
227
228 $this->URI = $uri;
229 $this->URIArray = explode( '/', $uri );
230 $this->Index = 0;
231
232 if ( !$fullInitialize )
233 return;
234
235 $this->OriginalURI = $uri;
236 $this->UserArray = array();
237
238 unset( $paramName, $paramValue );
239 foreach ( array_keys( $this->URIArray ) as $key )
240 {
241 if ( !isset( $this->URIArray[$key] ) )
242 continue;
243
244 if ( preg_match( "/^[\‍(][a-zA-Z0-9_]+[\‍)]/", $this->URIArray[$key] ) )
245 {
246 if ( isset( $paramName, $paramValue ) )
247 {
248 $this->UserArray[ $paramName ] = $paramValue;
249 unset( $paramName, $paramValue );
250 }
251 $paramName = substr( $this->URIArray[$key], 1, strlen( $this->URIArray[$key] ) - 2 );
252 if ( isset( $this->URIArray[$key+1] ) )
253 {
254 $this->UserArray[ $paramName ] = $this->URIArray[$key+1];
255 unset( $this->URIArray[$key+1] );
256 }
257 else
258 $this->UserArray[ $paramName ] = "";
259 unset( $this->URIArray[$key] );
260 }
261 else if ( isset( $paramName ) )
262 {
263 $this->UserArray[ $paramName ] .= '/' . $this->URIArray[$key];
264 unset( $this->URIArray[$key] );
265 }
266 }
267
268 // Remake the URI without any user parameters
269 $this->URI = implode( '/', $this->URIArray );
270
271 if ( eZINI::instance( 'template.ini' )->variable( 'ControlSettings', 'AllowUserVariables' ) == 'false' )
272 {
273 $this->UserArray = array();
274 }
275 // Convert filter string to current locale
276 $this->convertFilterString();
277 }
278
287 public function uriString( $withLeadingSlash = false )
288 {
289 $uri = $this->URI;
290 if ( $withLeadingSlash )
291 $uri = "/$uri";
292 return $uri;
293 }
294
303 public function originalURIString( $withLeadingSlash = false )
304 {
305 $uri = $this->OriginalURI;
306 if ( $withLeadingSlash )
307 $uri = "/$uri";
308 return $uri;
309 }
310
316 public function isEmpty()
317 {
318 return $this->URI == '' or $this->URI == '/';
319 }
320
328 public function element( $index = 0, $relative = true )
329 {
330 $pos = $index;
331 if ( $relative )
332 $pos += $this->Index;
333 if ( isset( $this->URIArray[$pos] ) )
334 return $this->URIArray[$pos];
335 $ret = null;
336 return $ret;
337 }
338
345 public function elements( $as_text = true )
346 {
347 $elements = array_slice( $this->URIArray, $this->Index );
348 if ( $as_text )
349 {
350 $retValue = implode( '/', $elements );
351 return $retValue;
352 }
353 else
354 return $elements;
355 }
356
364 public function convertFilterString()
365 {
366 foreach ( array_keys( $this->UserArray ) as $paramKey )
367 {
368 if ( $paramKey == 'namefilter' )
369 {
370 $char = $this->UserArray[$paramKey];
371 $char = urldecode( $char );
372
373 $codec = eZTextCodec::instance( 'utf-8', false );
374 if ( $codec )
375 $char = $codec->convertString( $char );
376 }
377 }
378 }
379
385 public function userParameters()
386 {
387 return $this->UserArray;
388 }
389
395 public function toBeginning()
396 {
397 $this->Index = 0;
398 }
399
405 public function toEnd()
406 {
407 $this->Index = count( $this->URIArray );
408 }
409
416 public function increase( $num = 1 )
417 {
418 $this->Index += $num;
419 if ( $this->Index < 0 )
420 $this->Index = 0;
421 }
422
429 public function dropBase()
430 {
431 $elements = array_slice( $this->URIArray, $this->Index );
432 $this->URIArray = $elements;
433 $this->URI = implode( '/', $this->URIArray );
434 $uri = $this->URI;
435 foreach ( $this->UserArray as $name => $value )
436 {
437 $uri .= '/(' . $name . ')/' . $value;
438 }
439 $this->OriginalURI = $uri;
440 $this->Index = 0;
441 }
442
448 public function index()
449 {
450 return $this->Index;
451 }
452
459 public function base( $as_text = true )
460 {
461 $elements = array_slice( $this->URIArray, 0, $this->Index );
462 if ( $as_text )
463 {
464 $baseAsText = '/' . implode( '/', $elements );
465 return $baseAsText;
466 }
467 else
468 return $elements;
469 }
470
480 public function matchBase( eZURI $uri )
481 {
482 if ( !( $uri instanceof eZURI ) )
483 {
484 return false;
485 }
486 if ( count( $this->URIArray ) == 0 or
487 count( $uri->URIArray ) == 0 )
488 {
489 return false;
490 }
491 for ( $i = 0; $i < count( $this->URIArray ); ++$i )
492 {
493 if ( $this->URIArray[$i] != $uri->URIArray[$i] )
494 {
495 return false;
496 }
497 }
498 return true;
499 }
500
506 public function attributes()
507 {
508 return array( 'element',
509 'base',
510 'tail',
511 'index',
512 'uri',
513 'original_uri',
514 'query_string' );
515 }
516
523 public function hasAttribute( $attr )
524 {
525 return in_array( $attr, $this->attributes() );
526 }
527
534 public function attribute( $attr )
535 {
536 switch ( $attr )
537 {
538 case 'element':
539 return $this->element();
540 break;
541 case 'tail':
542 return $this->elements();
543 break;
544 case 'base':
545 return $this->base();
546 break;
547 case 'index':
548 return $this->index();
549 break;
550 case 'uri':
551 return $this->uriString();
552 break;
553 case 'original_uri':
554 return $this->originalURIString();
555 break;
556 case 'query_string':
557 return eZSys::queryString();
558 break;
559 default:
560 {
561 eZDebug::writeError( "Attribute '$attr' does not exist", __METHOD__ );
562 return null;
563 } break;
564 }
565 }
566
574 public static function instance( $uri = false )
575 {
576 // If $uri is false we assume the caller wants eZSys::requestURI()
577 if ( $uri === false or $uri == eZSys::requestURI() )
578 {
579 if ( !isset( $GLOBALS['eZURIRequestInstance'] ) )
580 {
581 $GLOBALS['eZURIRequestInstance'] = new eZURI( eZSys::requestURI() );
582 }
583 return $GLOBALS['eZURIRequestInstance'];
584 }
585
586 return new eZURI( $uri );
587 }
588
599 public static function transformURI( &$href, $ignoreIndexDir = false, $serverURL = null, $htmlEscape = true )
600 {
601 // When using ezroot, immediately stop if the linked element is external
602 if ( $ignoreIndexDir )
603 {
604 // The initial / needs to be removed as it is not expected by the cluster handler,
605 // but we must use a temp variable since $href is required later
606 $trimmedHref = ltrim( $href, '/' );
607 $modifiedHref = eZClusterFileHandler::instance()->applyServerUri( $trimmedHref );
608 if ( $modifiedHref != $trimmedHref )
609 {
610 $href = $htmlEscape ? self::escapeHtmlTransformUri( $modifiedHref ) : $modifiedHref;
611 return true;
612 }
613 unset( $modifiedHref );
614 }
615
616 if ( $serverURL === null )
617 {
618 $serverURL = self::$transformURIMode;
619 }
620
621 if ( $href === null )
622 {
623 $href = '';
624 }
625
626 if ( preg_match( "#^[a-zA-Z0-9]+:#", $href ) || substr( $href, 0, 2 ) == '//' )
627 return false;
628
629 if ( strlen( $href ) == 0 )
630 $href = '/';
631 else if ( $href[0] == '#' )
632 {
633 $href = $htmlEscape ? htmlspecialchars( $href ) : $href;
634 return true;
635 }
636 else if ( $href[0] != '/' )
637 {
638 $href = '/' . $href;
639 }
640
641 $sys = eZSys::instance();
642 $dir = !$ignoreIndexDir ? $sys->indexDir() : $sys->wwwDir();
643 $serverURL = $serverURL === 'full' ? $sys->serverURL() : '' ;
644 $href = $serverURL . $dir . $href;
645 if ( !$ignoreIndexDir )
646 {
647 $href = preg_replace( "#^(//)#", "/", $href );
648 $href = preg_replace( "#(^.*)(/+)$#", "\$1", $href );
649 }
650 $href = $htmlEscape ? self::escapeHtmlTransformUri( $href ) : $href;
651
652 if ( $href == "" )
653 $href = "/";
654
655 return true;
656 }
657
666 private static function escapeHtmlTransformUri( $href )
667 {
668 return str_replace( '&amp;amp;', '&amp;', htmlspecialchars( $href ) );
669 }
670
679 public static function getTransformURIMode()
680 {
681 return self::$transformURIMode;
682 }
683
692 public static function setTransformURIMode( $mode )
693 {
694 self::$transformURIMode = $mode;
695 }
696
697 }
698
699 ?>
eZClusterFileHandler\instance
static instance( $filename=false)
Definition: ezclusterfilehandler.php:21
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZSys\requestURI
static requestURI()
Definition: ezsys.php:1262
eZSys\instance
static instance()
Definition: ezsys.php:1272
eZSys\queryString
static queryString()
Definition: ezsys.php:607
eZTextCodec\instance
static instance( $inputCharsetCode, $outputCharsetCode=false, $alwaysReturn=true)
Definition: eztextcodec.php:446
eZURI
Definition: ezuri.php:28
eZURI\escapeHtmlTransformUri
static escapeHtmlTransformUri( $href)
Definition: ezuri.php:666
eZURI\decodeURL
static decodeURL( $url)
Definition: ezuri.php:205
eZURI\$URIArray
$URIArray
Definition: ezuri.php:43
eZURI\isEmpty
isEmpty()
Definition: ezuri.php:316
eZURI\$Index
$Index
Definition: ezuri.php:50
eZURI\toBeginning
toBeginning()
Definition: ezuri.php:395
eZURI\__construct
__construct( $uri)
Definition: ezuri.php:76
eZURI\dropBase
dropBase()
Definition: ezuri.php:429
eZURI\instance
static instance( $uri=false)
Definition: ezuri.php:574
eZURI\attribute
attribute( $attr)
Definition: ezuri.php:534
eZURI\$OriginalURI
$OriginalURI
Definition: ezuri.php:36
eZURI\index
index()
Definition: ezuri.php:448
eZURI\elements
elements( $as_text=true)
Definition: ezuri.php:345
eZURI\originalURIString
originalURIString( $withLeadingSlash=false)
Definition: ezuri.php:303
eZURI\getTransformURIMode
static getTransformURIMode()
Definition: ezuri.php:679
eZURI\codecURL
static codecURL( $url, $encode)
Definition: ezuri.php:134
eZURI\userParameters
userParameters()
Definition: ezuri.php:385
eZURI\decodeIRI
static decodeIRI( $str)
Definition: ezuri.php:89
eZURI\attributes
attributes()
Definition: ezuri.php:506
eZURI\$UserArray
$UserArray
Definition: ezuri.php:57
eZURI\transformURI
static transformURI(&$href, $ignoreIndexDir=false, $serverURL=null, $htmlEscape=true)
Definition: ezuri.php:599
eZURI\setTransformURIMode
static setTransformURIMode( $mode)
Definition: ezuri.php:692
eZURI\uriString
uriString( $withLeadingSlash=false)
Definition: ezuri.php:287
eZURI\base
base( $as_text=true)
Definition: ezuri.php:459
eZURI\$URI
$URI
Definition: ezuri.php:34
eZURI\$transformURIMode
static $transformURIMode
Definition: ezuri.php:68
eZURI\increase
increase( $num=1)
Definition: ezuri.php:416
eZURI\matchBase
matchBase(eZURI $uri)
Definition: ezuri.php:480
eZURI\encodeIRI
static encodeIRI( $str)
Definition: ezuri.php:104
eZURI\element
element( $index=0, $relative=true)
Definition: ezuri.php:328
eZURI\setURIString
setURIString( $uri, $fullInitialize=true)
Definition: ezuri.php:220
eZURI\encodeURL
static encodeURL( $url)
Definition: ezuri.php:194
eZURI\convertFilterString
convertFilterString()
Definition: ezuri.php:364
eZURI\hasAttribute
hasAttribute( $attr)
Definition: ezuri.php:523
eZURI\toEnd
toEnd()
Definition: ezuri.php:405
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.