@@ -25,6 +25,7 @@ final class Updater
2525 private $ log = [];
2626 private $ status ;
2727 private $ clear ;
28+ private $ archive_relative_paths ;
2829
2930 /**
3031 * Constructs a new instance of the class and starts the update process for the provided version.
@@ -197,6 +198,7 @@ private function Unlock()
197198 {
198199 if (!$ this ->CleanUp ()) {
199200 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup process failed. " ];
201+ $ this ->status = $ this ::ERROR ;
200202 }
201203 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Releasing update lock. " ];
202204 if (!file_exists ($ this ->dir . '/update.lock ' )) {
@@ -360,7 +362,7 @@ private function Extract()
360362 if ($ zip ->extractTo ($ extract_path )) {
361363 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Extraction completed. $ extract_path " ];
362364 $ zip ->close ();
363- rename (glob ($ extract_path . '/* ' )[0 ], $ extract_path . '/tmp_ ' . $ this ->repository );
365+ rename (glob ($ extract_path . '/* ' )[0 ], $ extract_path . '/ ' . $ this ->repository );
364366 return true ;
365367 } else {
366368 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Extraction failed. $ extract_path " ];
@@ -403,15 +405,17 @@ private function Upgrade()
403405 $ release_exclude ['filename ' ] = array_unique ($ release_exclude ['filename ' ]);
404406 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Upgrade exclude: \n" . json_encode ($ release_exclude , JSON_PRETTY_PRINT )];
405407
406- $ release_paths = $ this ->MapPath ($ this ->dir . "/update/extract/tmp_ {$ this ->repository }" , $ release_exclude );
408+ $ release_paths = $ this ->MapPath ($ this ->dir . "/update/extract/ {$ this ->repository }" , $ release_exclude );
407409 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Upgrade lists: \n" . json_encode ($ release_paths , JSON_PRETTY_PRINT )];
408410
409411 $ release_relative_paths = array_map (function ($ release_path ) {
410- return substr_replace ($ release_path , '' , 0 , strlen ($ this ->dir . "/update/extract/tmp_ {$ this ->repository }" ));
412+ return substr_replace ($ release_path , '' , 0 , strlen ($ this ->dir . "/update/extract/ {$ this ->repository }" ));
411413 }, $ release_paths );
412414
415+ $ this ->archive_relative_paths = $ release_relative_paths ;
416+
413417 foreach ($ release_relative_paths as $ release_relative_path ) {
414- $ release_path = $ this ->dir . "/update/extract/tmp_ {$ this ->repository }$ release_relative_path " ;
418+ $ release_path = $ this ->dir . "/update/extract/ {$ this ->repository }$ release_relative_path " ;
415419 $ source_path = $ this ->dir . "$ release_relative_path " ;
416420 if (is_dir ($ release_path )) {
417421 if (!is_dir ($ source_path )) {
@@ -474,18 +478,59 @@ private function Upgrade()
474478
475479 private function CleanUp ()
476480 {
481+ $ cleaned = true ;
482+ if (file_exists ($ this ->dir . "/update/ " . $ this ->repository . ".zip " ) && !$ this ->Delete ($ this ->dir . "/update/ " . $ this ->repository . ".zip " )) {
483+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to delete downloaded zip file. " . $ this ->dir . "/update/ " . $ this ->repository . ".zip " ];
484+ $ cleaned = false ;
485+ };
486+ if (!$ this ->clear && $ this ->status !== $ this ::ERROR ) {
487+ $ archived = true ;
488+ $ zip = new ZipArchive ;
489+ $ zip_file = $ this ->dir . "/update/ {$ this ->repository }.zip " ;
490+ if ($ zip ->open ($ zip_file , ZipArchive::CREATE ) !== true ) {
491+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to create archive file. $ zip_file " ];
492+ $ cleaned = false ;
493+ $ archived = false ;
494+ }
495+ if ($ zip ->addEmptyDir ($ this ->repository ) === false ) {
496+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to create directory in archive file. {$ this ->repository }" ];
497+ $ cleaned = false ;
498+ $ archived = false ;
499+ }
500+ foreach ($ this ->archive_relative_paths as $ archive_relative_path ) {
501+ $ release_path = $ this ->dir . "/update/extract/ {$ this ->repository }$ archive_relative_path " ;
502+ if (is_dir ($ release_path )) {
503+ if ($ zip ->addEmptyDir ($ this ->repository . $ archive_relative_path ) === false ) {
504+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to create directory in archive file. " . $ this ->repository . $ archive_relative_path ];
505+ $ cleaned = false ;
506+ $ archived = false ;
507+ }
508+ } elseif (is_file ($ release_path )) {
509+ if ($ zip ->addFile ($ release_path , $ this ->repository . $ archive_relative_path ) === false ) {
510+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to add file to archive file. " . $ this ->repository . $ archive_relative_path ];
511+ $ cleaned = false ;
512+ $ archived = false ;
513+ };
514+ }
515+ }
516+ $ zip ->close ();
517+ if (!$ archived ) {
518+ if (file_exists ($ zip_file ) && !$ this ->Delete ($ zip_file )) {
519+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to delete archive file. $ zip_file " ];
520+ $ cleaned = false ;
521+ };
522+ }
523+ }
477524 if (file_exists ($ this ->dir . "/update/extract " ) && !$ this ->Delete ($ this ->dir . "/update/extract " )) {
478- $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup failed . " . $ this ->dir . "/update/extract " ];
479- return false ;
525+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Failed to delete extracted folder . " . $ this ->dir . "/update/extract " ];
526+ $ cleaned = false ;
480527 };
481- if ($ this ->clear ) {
482- if (file_exists ($ this ->dir . "/update/ " . $ this ->repository . ".zip " ) && !$ this ->Delete ($ this ->dir . "/update/ " . $ this ->repository . ".zip " )) {
483- $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup failed. " . $ this ->dir . "/update/ " . $ this ->repository . ".zip " ];
484- return false ;
485- };
528+ if ($ cleaned ) {
529+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup completed. " ];
530+ } else {
531+ $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup completed with errors. " ];
486532 }
487- $ this ->log [] = [date ("Y-m-d H:i:s " ), "Cleanup completed. " ];
488- return true ;
533+ return $ cleaned ;
489534 }
490535
491536 private function Install ()
@@ -494,51 +539,60 @@ private function Install()
494539 if (!$ this ->Lock ()) {
495540 if (file_exists ($ this ->dir . "/update.lock " )) {
496541 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Update already running. Update terminated. " ];
497- return $ this ::BUSY ;
542+ $ this ->status = $ this ::BUSY ;
543+ return $ this ->status ;
498544 }
499545 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Update lock aquiring failed. Update terminated. " ];
546+ $ this ->status = $ this ::ERROR ;
500547 $ this ->Unlock ();
501- return $ this :: ERROR ;
548+ return $ this -> status ;
502549 }
503550 if (!$ this ->Folder ()) {
504551 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Folder creation process failed. Update terminated. " ];
552+ $ this ->status = $ this ::ERROR ;
505553 $ this ->Unlock ();
506- return $ this :: ERROR ;
554+ return $ this -> status ;
507555 }
508556 if (!$ this ->Version ()) {
509557 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Version check process failed. Update terminated. " ];
558+ $ this ->status = $ this ::ERROR ;
510559 $ this ->Unlock ();
511- return $ this :: ERROR ;
560+ return $ this -> status ;
512561 }
513562 if (!version_compare ($ this ->release , $ this ->version , '> ' )) {
514563 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Version already up to date (Release {$ this ->release }). Update terminated. " ];
564+ $ this ->status = $ this ::LATEST ;
515565 $ this ->Unlock ();
516- return $ this :: LATEST ;
566+ return $ this -> status ;
517567 }
518568 $ download_try = 0 ;
519569 while (!$ this ->Download ($ this ->zip_url )) {
520570 $ download_try ++;
521571 if ($ download_try > 3 ) {
522572 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Download process failed. Update terminated. " ];
573+ $ this ->status = $ this ::ERROR ;
523574 $ this ->Unlock ();
524- return $ this :: ERROR ;
575+ return $ this -> status ;
525576 }
526577 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Unable to retrieve download. Retry in 5 seconds. " ];
527578 sleep (5 );
528579 }
529580 if (!$ this ->Extract ()) {
530581 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Extraction process failed. Update terminated. " ];
582+ $ this ->status = $ this ::ERROR ;
531583 $ this ->Unlock ();
532- return $ this :: ERROR ;
584+ return $ this -> status ;
533585 }
534586 if (!$ this ->Upgrade ()) {
535587 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Upgrade process failed. Update terminated. " ];
588+ $ this ->status = $ this ::ERROR ;
536589 $ this ->Unlock ();
537- return $ this :: ERROR ;
590+ return $ this -> status ;
538591 }
539592 $ this ->log [] = [date ("Y-m-d H:i:s " ), "Update completed. " ];
593+ $ this ->status = $ this ::UPDATED ;
540594 $ this ->Unlock ();
541- return $ this :: UPDATED ;
595+ return $ this -> status ;
542596 }
543597
544598 private function MapPath ($ path , $ exclude = ['path ' => [], 'filename ' => []], &$ list = [])
0 commit comments