Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

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