ultimix

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

package_manipulation.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
38 function _load_packages_list( $ROOT_DIR = INSTALL_DIR )
39 {
40 try
41 {
42 $File = _file_get_contents( $ROOT_DIR.'/packages/core/data/package_list' );
43 if( $File === false )
44 {
45 throw(
46 new Exception( 'An error occured while loading file '.$ROOT_DIR.'/packages/core/data/package_list' )
47 );
48 }
49
50 $File = str_replace( "\r" , "\n" , $File );
51 $File = str_replace( "\n\n" , "\n" , $File );
52 $PackagesInfo = explode( "\n" , $File );
53
54 $PackageList = array();
55
56 foreach( $PackagesInfo as $pi )
57 {
58 $PackageList [] = explode( '#' , $pi );
59 $tmp = explode( '#' , $pi );
60 }
61
62 return( $PackageList );
63 }
64 catch( Exception $e )
65 {
66 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
67 }
68 }
69
98 function _get_name_and_version( $RootPackage = '' , $RootVersion = '' )
99 {
100 try
101 {
102 if( $RootPackage == '' )
103 {
104 $PackageNamePrefix = '';
105 }
106 else
107 {
108 $PackageNamePrefix = $RootPackage.'::';
109 }
110
111 if( $RootVersion == '' )
112 {
113 $PackageVersionPrefix = '';
114 }
115 else
116 {
117 $PackageVersionPrefix = $RootVersion.'::';
118 }
119
120 return( array( $PackageNamePrefix , $PackageVersionPrefix ) );
121 }
122 catch( Exception $e )
123 {
124 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
125 }
126 }
127
166 function _fetch_subpackages_list( $RetArray , $ROOT_DIR , $p ,
167 $PackageNamePrefix , $PackageVersionPrefix )
168 {
169 try
170 {
171 $InfoParts = explode( '.' , $p[ 0 ] );
172 $RetArray [] = array(
173 'package_name' => $PackageNamePrefix.$InfoParts[ 0 ] ,
174 'package_version' => $PackageVersionPrefix.$InfoParts[ 1 ].'.'.$InfoParts[ 2 ].'.'.$InfoParts[ 3 ]
175 );
176
177 if( file_exists( $ROOT_DIR.'/packages/'.$p[ 1 ].'/packages/core/data/package_list' ) )
178 {
179 $Name = $PackageNamePrefix.$InfoParts[ 0 ];
180 $Version = $PackageVersionPrefix.$InfoParts[ 1 ].'.'.$InfoParts[ 2 ].'.'.$InfoParts[ 3 ];
181 $RetArray = array_merge(
182 $RetArray , _get_packages_list( $ROOT_DIR.'/packages/'.$p[ 1 ] , $Name , $Version )
183 );
184 }
185
186 return( $RetArray );
187 }
188 catch( Exception $e )
189 {
190 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
191 }
192 }
193
224 function _get_packages_list( $ROOT_DIR = INSTALL_DIR , $RootPackage = '' , $RootVersion = '' )
225 {
226 try
227 {
228 $PackageList = _load_packages_list( $ROOT_DIR );
229
230 list( $PackageNamePrefix , $PackageVersionPrefix ) = _get_name_and_version( $RootPackage , $RootVersion );
231
232 $RetArray = array();
233
234 foreach( $PackageList as $p )
235 {
236 $RetArray = _fetch_subpackages_list(
237 $RetArray , $ROOT_DIR , $p , $PackageNamePrefix , $PackageVersionPrefix
238 );
239 }
240
241 return( $RetArray );
242 }
243 catch( Exception $e )
244 {
245 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
246 }
247 }
248
283 function _fetch_subpackages( $ROOT_DIR , $p , $PackageNamePrefix , $PackageVersionPrefix )
284 {
285 try
286 {
287 $InfoParts = explode( '.' , $p[ 0 ] );
288
289 $SubPackages = array();
290
291 if( file_exists( $ROOT_DIR.'/packages/'.$p[ 1 ].'/packages/core/data/package_list' ) )
292 {
293 $Name = $PackageNamePrefix.$InfoParts[ 0 ];
294 $Version = $PackageVersionPrefix.$InfoParts[ 1 ].'.'.$InfoParts[ 2 ].'.'.$InfoParts[ 3 ];
295 $SubPackages = _get_packages_tree( $ROOT_DIR.'/packages/'.$p[ 1 ] , $Name , $Version );
296 }
297
298 return( $SubPackages );
299 }
300 catch( Exception $e )
301 {
302 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
303 }
304 }
305
344 function _fetch_subpackages_tree( $RetArray , $ROOT_DIR , $p , $PackageNamePrefix , $PackageVerPrefix )
345 {
346 try
347 {
348 $SubPackages = _fetch_subpackages( $ROOT_DIR , $p , $PackageNamePrefix , $PackageVerPrefix );
349
350 $InfoParts = explode( '.' , $p[ 0 ] );
351
352 $RetArray [] = array(
353 'package_signature' => $p[ 0 ] ,
354 'package_name' => $InfoParts[ 0 ] ,
355 'package_version' => $InfoParts[ 1 ].'.'.$InfoParts[ 2 ].'.'.$InfoParts[ 3 ] ,
356 'subpackages' => $SubPackages ,
357 'full_package_name' => $PackageNamePrefix.$InfoParts[ 0 ] ,
358 'full_package_version' => $PackageVerPrefix.$InfoParts[ 1 ].'.'.$InfoParts[ 2 ].'.'.
359 $InfoParts[ 3 ]
360 );
361 return( $RetArray );
362 }
363 catch( Exception $e )
364 {
365 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
366 }
367 }
368
399 function _get_packages_tree( $ROOT_DIR = INSTALL_DIR , $RootPackage = '' , $RootVersion = '' )
400 {
401 try
402 {
403 $PackageList = _load_packages_list( $ROOT_DIR );
404
405 list( $PackageNamePrefix , $PackageVersionPrefix ) = _get_name_and_version( $RootPackage , $RootVersion );
406
407 $RetArray = array();
408
409 foreach( $PackageList as $p )
410 {
411 $RetArray = _fetch_subpackages_tree(
412 $RetArray , $ROOT_DIR , $p , $PackageNamePrefix , $PackageVersionPrefix
413 );
414
415 rsort_by_field( $RetArray , 'package_signature' );
416 }
417
418 return( $RetArray );
419 }
420 catch( Exception $e )
421 {
422 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
423 }
424 }
425
452 function _unzip_package( $FileSource , $ROOT_DIR = INSTALL_DIR )
453 {
454 try
455 {
456 $Zip = new ZipArchive;
457 if( $Zip->open( $FileSource ) === false )
458 {
459 throw( new Exception( 'An error occured while opening package '.$FileSource ) );
460 }
461 if( $Zip->extractTo( $ROOT_DIR.'/packages/'.$SubDirPath.'/' ) === false )
462 {
463 throw( new Exception( 'An error occured while extracting package '.$FileSource ) );
464 }
465 @unlink( $FileSource );
466 $PackageList = _file_get_contents( $ROOT_DIR.'/packages/core/data/package_list' );
467 file_put_contents(
468 $ROOT_DIR.'/packages/core/data/package_list' ,
469 basename( $FileSource , '.zip' ).'#'.$SubDirPath."\r\n".$PackageList
470 );
471 $Zip->close();
472 }
473 catch( Exception $e )
474 {
475 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
476 }
477 }
478
505 function _install_package( $FileSource , $ROOT_DIR = INSTALL_DIR )
506 {
507 try
508 {
509 $FileSource = strtolower( $FileSource );
510 $SubDirPath = md5( date( 'YmdHis' ).$FileSource );
511 mkdir( $ROOT_DIR.'/packages/'.$SubDirPath );
512
513 _unzip_package( $FileSource , $ROOT_DIR );
514
515 return( $ROOT_DIR.'/packages/'.$SubDirPath );
516 }
517 catch( Exception $e )
518 {
519 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
520 }
521 }
522
553 function _get_packages_info( $PackageName , $PackageVersion , $ROOT_DIR = INSTALL_DIR )
554 {
555 try
556 {
557 $File = _file_get_contents( $ROOT_DIR.'/packages/core/data/package_list' );
558
559 if( $File === false )
560 {
561 throw(
562 new Exception( 'An error occured while loading file '.$ROOT_DIR.'/packages/core/data/package_list' )
563 );
564 }
565
566 $File = str_replace( "\r" , "\n" , $File );
567 $File = str_replace( "\n\n" , "\n" , $File );
568
569 return( explode( "\n" , $File ) );
570 }
571 catch( Exception $e )
572 {
573 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
574 }
575 }
576
595 function _delete_pf( $Path )
596 {
597 try
598 {
599 _rmdir_recurse( $Path );
600 @rmdir( $Path );
601 }
602 catch( Exception $e )
603 {
604 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
605 }
606 }
607
626 function _open_package_list( $Path )
627 {
628 try
629 {
630 $Handle = fopen( $Path , 'w' );
631
632 if( $Handle === false )
633 {
634 throw( new Exception( 'Unable to open package list' ) );
635 }
636
637 return( $Handle );
638 }
639 catch( Exception $e )
640 {
641 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
642 }
643 }
644
671 function _delete_package( $PackageName , $PackageVersion , $ROOT_DIR = INSTALL_DIR )
672 {
673 try
674 {
675 global $PackageCache;
676 $PackageCache = array();
677
678 $Handle = _open_package_list( $ROOT_DIR.'/packages/core/data/package_list' );
679
680 $PackagesInfo = _get_packages_info( $PackageName , $PackageVersion , $ROOT_DIR );
681
682 foreach( $PackagesInfo as $pi )
683 {
684 $PackageNameAndVersion = explode( '#' , $pi );
685 if( $PackageNameAndVersion[ 0 ] === $PackageName.'.'.$PackageVersion )
686 {
687 _delete_pf( $ROOT_DIR.'/packages/'.$PackageNameAndVersion[ 1 ] );
688 }
689 else
690 {
691 $pi .= "\r\n";
692 fwrite( $Handle , $pi , strlen( $pi ) );
693 }
694 }
695 fclose( $Handle );
696 }
697 catch( Exception $e )
698 {
699 $Args = func_get_args();throw( _get_exception_object( __FUNCTION__ , $Args , $e ) );
700 }
701 }
702
703 ?>