ultimix

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

rewrites.php
Go to the documentation of this file.
1 <?php
2
3 /*
4 * This source code is a part of the Ultimix Project.
5 * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6 * it's own license wich could be found from the corresponding files or sources.
7 * This source code is provided "as is" without any warranties or garanties.
8 *
9 * Have a nice day!
10 *
11 * @url http://ultimix.sorceforge.net
12 *
13 * @author Alexey "gdever" Dodonov
14 */
15
16 global $RewritesCacheChanged;
17 global $RewritesCache;
18 $RewritesCache = array();
19
30 global $RewriteMap;
31 $RewriteMap = array();
32
55 function _set_rewrite_map( $PackageScriptPath = false , $FileName = 'local_rewrite_map' )
56 {
57 try
58 {
59 global $RewriteMap;
60 if( $PackageScriptPath !== false )
61 {
62 $RewriteMap = _file_get_contents( dirname( $PackageScriptPath )."/conf/$FileName" );
63 if( $RewriteMap === false )
64 {
65 return;
66 }
67 $RewriteMap = str_replace( "\r" , "\n" , $RewriteMap );
68 $RewriteMap = str_replace( "\n\n" , "\n" , $RewriteMap );
69 if( $RewriteMap != '' )
70 {
71 $RewriteMap = explode( "\n" , $RewriteMap );
72 for( $i = 0 ; $i < count( $RewriteMap ) ; $i++ )
73 {
74 $RewriteMap[ $i ] = explode( '#' , $RewriteMap[ $i ] );
75 }
76 return;
77 }
78 $RewriteMap = false;
79 }
80 }
81 catch( Exception $e )
82 {
83 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
84 }
85 }
86
101 function _reset_rewrite_map()
102 {
103 try
104 {
105 global $RewriteMap;
106 $RewriteMap = false;
107 }
108 catch( Exception $e )
109 {
110 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
111 }
112 }
113
114 $MakeFullVersionCacheChanged = false;
115 $MakeFullVersionCache = array();
116
139 function _complete_package_version( $PackageName )
140 {
141 $FullPackageVersion = '';
142
143 $Packages = explode( '::' , $PackageName );
144 if( isset( $Packages[ 1 ] ) )
145 {
146 $l = count( $Packages ) - 1;
147 for( $i = 0 ; $i < $l ; $i++ )
148 {
149 $FullPackageVersion .= 'last::';
150 }
151 }
152
153 return( $FullPackageVersion );
154 }
155
182 function _make_full_version( $PackageName , $PackageVersion )
183 {
184 try
185 {
186 global $MakeFullVersionCache;
187 if( isset( $MakeFullVersionCache[ "$PackageName.$PackageVersion" ] ) )
188 {
189 return( $MakeFullVersionCache[ "$PackageName.$PackageVersion" ] );
190 }
191
192 $FullPackageVersion = $PackageVersion;
193 if( $PackageVersion == 'last' || strpos( $PackageVersion , '::' ) === false )
194 {
195 $FullPackageVersion = _complete_package_version( $PackageName ).$PackageVersion;
196 }
197
198 global $MakeFullVersionCacheChanged;
199 $MakeFullVersionCacheChanged = true;
200
201 return( $MakeFullVersionCache[ "$PackageName.$PackageVersion" ] = $FullPackageVersion );
202 }
203 catch( Exception $e )
204 {
205 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
206 }
207 }
208
239 function _apply_to_the_whole_name( $PackageName , $PackageVersion , $Rules )
240 {
241 try
242 {
243 global $RewriteMap;
244
245 foreach( $RewriteMap as $i => $r )
246 {
247 if( preg_match( $r[ 0 ] , $PackageName.'.'.$PackageVersion ) > 0 )
248 {
249 $PackageName = $r[ 1 ];
250 $PackageVersion = $r[ 2 ];
251 break;
252 }
253 }
254
255 return( array( $PackageName , $PackageVersion ) );
256 }
257 catch( Exception $e )
258 {
259 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
260 }
261 }
262
289 function _set_rewrited( &$PackageNameSegments , $r )
290 {
291 try
292 {
293 if( preg_match( $r[ 0 ] , $PackageNameSegments[ $j ].'.'.$PackageVersionSegments[ $j ] ) > 0 )
294 {
295 $PackageNameSegments[ $j ] = $r[ 1 ];
296 $PackageInfoParts = explode( '.' , $r[ 2 ] );
297
298 if( count( $PackageInfoParts ) == 2 )
299 {
300 $PackageVersionSegments[ $j ] = $PackageInfoParts[ 0 ];
301 }
302 else
303 {
304 $PackageVersionSegments[ $j ] = $PackageInfoParts[ 0 ].'.';
305 $PackageVersionSegments[ $j ] .= $PackageInfoParts[ 1 ].'.';
306 $PackageVersionSegments[ $j ] .= $PackageInfoParts[ 2 ];
307 }
308
309 return( true );
310 }
311
312 return( false );
313 }
314 catch( Exception $e )
315 {
316 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
317 }
318 }
319
350 function _apply_to_the_parts( $PackageName , $PackageVersion , $Rules )
351 {
352 try
353 {
354 global $RewriteMap;
355
356 $PackageNameSegments = explode( '::' , $PackageName );
357 $PackageVersionSegments = explode( '::' , $PackageVersion );
358
359 foreach( $RewriteMap as $r )
360 {
361 for( $j = 0 ; $j < count( $PackageNameSegments ) ; $j++ )
362 {
363 if( _set_rewrited( $PackageNameSegments[ $j ] , $r ) )
364 {
365 break;
366 }
367 }
368 }
369
370 $PackageName = implode( '::' , $PackageNameSegments );
371 $PackageVersion = implode( '::' , $PackageVersionSegments );
372
373 return( array( $PackageName , $PackageVersion ) );
374 }
375 catch( Exception $e )
376 {
377 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
378 }
379 }
380
407 function _apply_both_transforms( $PackageName , $PackageVersion )
408 {
409 try
410 {
411 global $RewriteMap;
412 list( $PackageName , $PackageVersion ) = _apply_to_the_whole_name(
413 $PackageName ,
414 $PackageVersion ,
415 $RewriteMap
416 );
417 $PackageVersion = _make_full_version( $PackageName , $PackageVersion );
418
419 $NameParts = explode( '::' , $PackageName );
420 $VersionParts = explode( '::' , $PackageVersion );
421 list( $PackageName , $PackageVersion ) = _apply_to_the_parts(
422 $PackageName ,
423 $PackageVersion ,
424 $RewriteMap
425 );
426 return( array( $PackageName , $PackageVersion ) );
427 }
428 catch( Exception $e )
429 {
430 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
431 }
432 }
433
464 function _apply_local_rewrite_map( $PackageName , $PackageVersion , $PackageScriptPath )
465 {
466 try
467 {
468 _set_rewrite_map( $PackageScriptPath );
469
470 global $RewriteMap;
471
472 if( $RewriteMap !== false )
473 {
474 # \~russian пытаемся применить карты рерайтов ко всему имени пакета
475 # \~english trying to apply rewrite map to the hole package name and version
476
477 list( $PackageName , $PackageVersion ) = _apply_both_transforms( $PackageName , $PackageVersion );
478 }
479
480 return( array( $PackageName , $PackageVersion ) );
481 }
482 catch( Exception $e )
483 {
484 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
485 }
486 }
487
488 global $RewritedPackageCacheChanged;
489 $RewritedPackageCacheChanged = false;
490
491 global $RewritedPackageCache;
492 $RewritedPackageCache = array();
493
524 function _apply_global_rewrite_map( $Name , $Version , $ROOT_DIR )
525 {
526 try
527 {
528 global $RewritedPackageCache;
529 $Key = "$Name $Version $ROOT_DIR";
530 if( isset( $RewritedPackageCache[ $Key ] ) === false )
531 {
532 _set_rewrite_map( "$ROOT_DIR/packages/_core_data/unexisting_file" , "cf_global_rewrite_rules" );
533 global $RewriteMap;
534 if( $RewriteMap !== false )
535 {
536 list( $Name , $Version ) = _apply_both_transforms( $Name , $Version );
537 }
538 _reset_rewrite_map();
539 global $RewritedPackageCacheChanged;
540
541 $RewritedPackageCacheChanged = true;
542 $RewritedPackageCache[ $Key ] = array( $Name , $Version );
543 }
544 return( $RewritedPackageCache[ $Key ] );
545 }
546 catch( Exception $e )
547 {
548 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
549 }
550 }
551
582 function _apply_rewrites( $PackageName , $PackageVersion , $PackageScriptPath )
583 {
584 try
585 {
586 list( $PackageName , $PackageVersion ) = _apply_local_rewrite_map(
587 $PackageName ,
588 $PackageVersion ,
589 $PackageScriptPath
590 );
591
592 # глобальная переадресация
593 # global rewriting
594 list( $PackageName , $PackageVersion ) = _apply_global_rewrite_map(
595 $PackageName ,
596 $PackageVersion ,
597 INSTALL_DIR
598 );
599
600 return( array( $PackageName , $PackageVersion ) );
601 }
602 catch( Exception $e )
603 {
604 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
605 }
606 }
607
608 $PackageInfoAfterRewritesCacheChanged = false;
609 $PackageInfoAfterRewritesCache = array();
610
642 function _fill_rewrites_cache( $PackageName , $PackageVersion , $PackageScriptPath )
643 {
644 try
645 {
646 global $ENABLE_REWRITES;
647 if( $ENABLE_REWRITES )
648 {
649 list( $PackageName , $PackageVersion ) = _apply_rewrites(
650 $PackageName ,
651 $PackageVersion ,
652 $PackageScriptPath
653 );
654
655 global $RewritesCacheChanged;
656 $RewritesCacheChanged = true;
657 }
658 }
659 catch( Exception $e )
660 {
661 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
662 }
663 }
664
696 function _get_package_info_after_rewrites( $PackageName , $PackageVersion , $PackageScriptPath = false )
697 {
698 try
699 {
700 global $RewritesCache;
701 $Key = "$PackageName $PackageVersion $PackageScriptPath";
702
703 if( isset( $RewritesCache[ $Key ] ) )
704 {
705 return( $RewritesCache[ $Key ] );
706 }
707 $PackageVersion = _make_full_version( $PackageName , $PackageVersion );
708
709 _fill_rewrites_cache( $PackageName , $PackageVersion , $PackageScriptPath );
710
711 return( $RewritesCache[ $Key ] = array( 0 => $PackageName , 1 => $PackageVersion ) );
712 }
713 catch( Exception $e )
714 {
715 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
716 }
717 }
718
719 ?>