apex /administrator/components/com_akeeba/akeeba/engines/archiver/zip.php

Language PHP Lines 1239
MD5 Hash 26a9c63660a976c45a48b4c5cca37216 Estimated Cost $23,314 (why?)
Repository https://bitbucket.org/kraymitchell/apex.git View Raw File
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
<?php
/**
 * Akeeba Engine
 * The modular PHP5 site backup engine
 * @copyright Copyright (c)2009-2012 Nicholas K. Dionysopoulos
 * @license GNU GPL version 3 or, at your option, any later version
 * @package akeebaengine
 *
 */

// Protection against direct access
defined('AKEEBAENGINE') or die('Restricted access');

class AEArchiverZip extends AEAbstractArchiver {

	/** @var string Beginning of central directory record. */
	private $_ctrlDirHeader = "\x50\x4b\x01\x02";

	/** @var string End of central directory record. */
	private $_ctrlDirEnd = "\x50\x4b\x05\x06";

	/** @var string Beginning of file contents. */
	private $_fileHeader = "\x50\x4b\x03\x04";

	/** @var string The name of the temporary file holding the ZIP's Central Directory */
	private $_ctrlDirFileName;

	/** @var string The name of the file holding the ZIP's data, which becomes the final archive */
	private $_dataFileName;

	/** @var integer The total number of files and directories stored in the ZIP archive */
	private $_totalFileEntries;
	
	/** @var integer The total size of data in the archive. Note: On 32-bit versions of PHP, this will overflow for archives over 2Gb! */
	private $_totalDataSize = 0;

	/** @var integer The chunk size for CRC32 calculations */
	private $AkeebaPackerZIP_CHUNK_SIZE;

	/** @var bool Should I use Split ZIP? */
	private $_useSplitZIP = false;

	/** @var int Maximum fragment size, in bytes */
	private $_fragmentSize = 0;

	/** @var int Current fragment number */
	private $_currentFragment = 1;

	/** @var int Total number of fragments */
	private $_totalFragments = 1;

	/** @var string Archive full path without extension */
	private $_dataFileNameBase = '';

	/** @var bool Should I store symlinks as such (no dereferencing?) */
	private $_symlink_store_target = false;

	/**
	 * Extend the bootstrap code to add some define's used by the ZIP format engine
	 * @see backend/akeeba/abstract/AEAbstractArchiver#__bootstrap_code()
	 */
	protected function __bootstrap_code()
	{
		if(!defined('_AKEEBA_COMPRESSION_THRESHOLD'))
		{
			$config = AEFactory::getConfiguration();
			define("_AKEEBA_COMPRESSION_THRESHOLD", $config->get('engine.archiver.common.big_file_threshold')); // Don't compress files over this size
			define("_AKEEBA_DIRECTORY_READ_CHUNK", $config->get('engine.archiver.zip.cd_glue_chunk_size')); // How much data to read at once when finalizing ZIP archives
		}
		parent::__bootstrap_code();
	}

	/**
	 * Class constructor - initializes internal operating parameters
	 */
	public function __construct()
	{
		AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "AEArchiverZip :: New instance");

		// Get chunk override
		$registry = AEFactory::getConfiguration();
		if( $registry->get('engine.archiver.common.chunk_size', 0) > 0 )
		{
			$this->AkeebaPackerZIP_CHUNK_SIZE = AKEEBA_CHUNK;
		}
		else
		{
			// Try to use as much memory as it's possible for CRC32 calculation
			$memLimit = ini_get("memory_limit");
			
			if(strstr($memLimit, 'M')) {
				$memLimit = (int)$memLimit * 1048576;
			} elseif(strstr($totalRAM, 'K')) {
				$memLimit = (int)$memLimit * 1024;
			} elseif(strstr($memLimit, 'G')) {
				$memLimit = (int)$memLimit * 1073741824;
			} else {
				$memLimit = (int)$memLimit;
			}
			
			if( is_numeric($memLimit) && ($memLimit < 0) ) $memLimit = ""; // 1.2a3 -- Rare case with memory_limit < 0, e.g. -1Mb!
			if ( ($memLimit == "") ) {
				// No memory limit, use 2Mb chunks (fairly large, right?)
				$this->AkeebaPackerZIP_CHUNK_SIZE = 2097152;
			} elseif ( function_exists("memory_get_usage") ) {
				// PHP can report memory usage, see if there's enough available memory; Joomla! alone eats about 5-6Mb! This code is called on files <= 1Mb
				$memLimit = $this->_return_bytes( $memLimit );
				$availableRAM = $memLimit - memory_get_usage();

				if ($availableRAM <= 0) {
					// Some PHP implemenations also return the size of the httpd footprint!
					if ( ($memLimit - 6291456) > 0 ) {
						$this->AkeebaPackerZIP_CHUNK_SIZE = $memLimit - 6291456;
					} else {
						$this->AkeebaPackerZIP_CHUNK_SIZE = 2097152;
					}
				} else {
					$this->AkeebaPackerZIP_CHUNK_SIZE = $availableRAM * 0.5;
				}
			} else {
				// PHP can't report memory usage, use a conservative 512Kb
				$this->AkeebaPackerZIP_CHUNK_SIZE = 524288;
			}
		}

		// NEW 2.3: Should we enable Split ZIP feature?
		$fragmentsize = $registry->get('engine.archiver.common.part_size', 0);
		if($fragmentsize >= 65536)
		{
			// If the fragment size is AT LEAST 64Kb, enable Split ZIP
			$this->_useSplitZIP = true;
			$this->_fragmentSize = $fragmentsize;
			// Indicate that we have at least 1 part
			$statistics = AEFactory::getStatistics();
			$statistics->updateMultipart(1);
		}

		// NEW 2.3: Should I use Symlink Target Storage?
		$dereferencesymlinks = $registry->get('engine.archiver.common.dereference_symlinks', true);
		if(!$dereferencesymlinks)
		{
			// We are told not to dereference symlinks. Are we on Windows?
			if (function_exists('php_uname'))
			{
				$isWindows = stristr(php_uname(), 'windows');
			}
			else
			{
				$isWindows = (DIRECTORY_SEPARATOR == '\\');
			}
			// If we are not on Windows, enable symlink target storage
			$this->_symlink_store_target = !$isWindows;
		}


		AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "Chunk size for CRC is now " . $this->AkeebaPackerZIP_CHUNK_SIZE . " bytes");

		parent::__construct();
	}

	/**
	 * Initialises the archiver class, creating the archive from an existent
	 * installer's JPA archive.
	 *
	 * @param string $sourceJPAPath Absolute path to an installer's JPA archive
	 * @param string $targetArchivePath Absolute path to the generated archive
	 * @param array $options A named key array of options (optional). This is currently not supported
	 */
	public function initialize( $targetArchivePath, $options = array() )
	{
		AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "AEArchiverZip :: initialize - archive $targetArchivePath");

		// Get names of temporary files
		$configuration = AEFactory::getConfiguration();
		$this->_ctrlDirFileName = tempnam( $configuration->get('akeeba.basic.output_directory'), 'akzcd' );
		$this->_dataFileName = $targetArchivePath;

		// If we use splitting, initialize
		if($this->_useSplitZIP)
		{
			AEUtilLogger::WriteLog(_AE_LOG_INFO, "AEArchiverZip :: Split ZIP creation enabled");
			$this->_dataFileNameBase = dirname($targetArchivePath).'/'.basename($targetArchivePath,'.zip');
			$this->_dataFileName = $this->_dataFileNameBase.'.z01';
		}

		$this->_ctrlDirFileName = basename($this->_ctrlDirFileName);
		$pos = strrpos($this->_ctrlDirFileName, '/');
		if($pos !== false) {
			$this->_ctrlDirFileName = substr($this->_ctrlDirFileName, $pos+1);
		}
		$pos = strrpos($this->_ctrlDirFileName, '\\');
		if($pos !== false) {
			$this->_ctrlDirFileName = substr($this->_ctrlDirFileName, $pos+1);
		}
		$this->_ctrlDirFileName = AEUtilTempfiles::registerTempFile($this->_ctrlDirFileName);

		AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "AEArchiverZip :: CntDir Tempfile = " . $this->_ctrlDirFileName);

		// Create temporary file
		if(!@touch( $this->_ctrlDirFileName ))
		{
			$this->setError("Could not open temporary file for ZIP archiver. Please check your temporary directory's permissions!");
			return false;
		}
		if(function_exists('chmod')) {
			chmod($this->_ctrlDirFileName, 0666);
		}

		// Try to kill the archive if it exists
		AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "AEArchiverZip :: Killing old archive");
		$fp = fopen( $this->_dataFileName, "wb" );
		if (!($fp === false)) {
			ftruncate( $fp,0 );
			fclose( $fp );
		} else {
			@unlink( $this->_dataFileName );
		}
		if(!@touch( $this->_dataFileName ))
		{
			$this->setError("Could not open archive file for ZIP archiver. Please check your output directory's permissions!");
			return false;
		}
		if(function_exists('chmod')) {
			chmod($this->_dataFileName, 0666);
		}		

		// On split archives, include the "Split ZIP" header, for PKZIP 2.50+ compatibility
		if($this->_useSplitZIP)
		{
			file_put_contents($this->_dataFileName, "\x50\x4b\x07\x08");
			// Also update the statistics table that we are a multipart archive...
			$statistics = AEFactory::getStatistics();
			$statistics->updateMultipart(1);
		}
	}

	/**
	 * Creates the ZIP file out of its pieces.
	 * Official ZIP file format: http://www.pkware.com/appnote.txt
	 *
	 * @return boolean TRUE on success, FALSE on failure
	 */
	public function finalize()
	{
		// 1. Get size of central directory
		clearstatcache();
		$this->_totalDataSize += @filesize( $this->_dataFileName );
		$cdSize = @filesize( $this->_ctrlDirFileName );

		// 2. Append Central Directory to data file and remove the CD temp file afterwards
		$dataFP = fopen( $this->_dataFileName, "ab" );
		$cdFP = fopen( $this->_ctrlDirFileName, "rb" );

		if( $dataFP === false )
		{
			$this->setError('Could not open ZIP data file '.$this->_dataFileName.' for reading');
			return false;
		}

		if ( $cdFP === false ) {
			// Already glued, return
			fclose( $dataFP );
			return false;
		}

		if(!$this->_useSplitZIP)
		{
			while( !feof($cdFP) )
			{
				$chunk = fread( $cdFP, _AKEEBA_DIRECTORY_READ_CHUNK );
				$this->_fwrite( $dataFP, $chunk );
				if($this->getError()) return;
			}
			unset( $chunk );
			fclose( $cdFP );
		}
		else
		// Special considerations for Split ZIP
		{
			// Calcuate size of Central Directory + EOCD records
			$comment_length = function_exists('mb_strlen') ? mb_strlen($this->_comment, '8bit') : strlen($this->_comment);
			$total_cd_eocd_size = $cdSize + 22 + $comment_length;
			// Free space on the part
			clearstatcache();
			$current_part_size = @filesize($this->_dataFileName);
			$free_space = $this->_fragmentSize - ($current_part_size === false ? 0 : $current_part_size);
			if( ($free_space < $total_cd_eocd_size) && ($total_cd_eocd_size > 65536) )
			{
				// Not enough space on archive for CD + EOCD, will go on separate part
				// Create new final part
				if(!$this->_createNewPart(true))
				{
					// Die if we couldn't create the new part
					$this->setError('Could not create new ZIP part file '.basename($this->_dataFileName));
					return false;
				}
				else
				{
					// Close the old data file
					fclose($dataFP);
					// Open data file for output
					$dataFP = @fopen( $this->_dataFileName, "ab");
					if ($dataFP === false)
					{
						$this->setError("Could not open archive file {$this->_dataFileName} for append!");
						return false;
					}
					// Write the CD record
					while( !feof($cdFP) )
					{
						$chunk = fread( $cdFP, _AKEEBA_DIRECTORY_READ_CHUNK );
						$this->_fwrite( $dataFP, $chunk );
						if($this->getError()) return;
					}
					unset( $chunk );
					fclose( $cdFP );
				}
			}
			else
			{
				// Glue the CD + EOCD on the same part if they fit, or anyway if they are less than 64Kb.
				// NOTE: WE *MUST NOT* CREATE FRAGMENTS SMALLER THAN 64Kb!!!!
				while( !feof($cdFP) )
				{
					$chunk = fread( $cdFP, _AKEEBA_DIRECTORY_READ_CHUNK );
					$this->_fwrite( $dataFP, $chunk );
					if($this->getError()) return;
				}
				unset( $chunk );
				fclose( $cdFP );
			}
		}

		AEUtilTempfiles::unregisterAndDeleteTempFile($this->_ctrlDirFileName);

		// 3. Write the rest of headers to the end of the ZIP file
		fclose( $dataFP );
		clearstatcache();		
		$dataFP = fopen( $this->_dataFileName, "ab" );
		if($dataFP === false)
		{
			$this->setError('Could not open '.$this->_dataFileName.' for append');
			return false;
		}
		$this->_fwrite( $dataFP, $this->_ctrlDirEnd );
		if($this->getError()) return;
		if($this->_useSplitZIP)
		{
			// Split ZIP files, enter relevant disk number information
			$this->_fwrite( $dataFP, pack('v', $this->_totalFragments - 1) ); /* Number of this disk. */
			$this->_fwrite( $dataFP, pack('v', $this->_totalFragments - 1) ); /* Disk with central directory start. */
		}
		else
		{
			// Non-split ZIP files, the disk numbers MUST be 0
			$this->_fwrite( $dataFP, pack('V', 0) );
		}
		$this->_fwrite( $dataFP, pack('v', $this->_totalFileEntries) ); /* Total # of entries "on this disk". */
		$this->_fwrite( $dataFP, pack('v', $this->_totalFileEntries) ); /* Total # of entries overall. */
		$this->_fwrite( $dataFP, pack('V', $cdSize) ); /* Size of central directory. */
		$this->_fwrite( $dataFP, pack('V', $this->_totalDataSize) ); /* Offset to start of central dir. */
		$sizeOfComment = $comment_length = function_exists('mb_strlen') ? mb_strlen($this->_comment, '8bit') : strlen($this->_comment);
		// 2.0.b2 -- Write a ZIP file comment
		$this->_fwrite( $dataFP, pack('v', $sizeOfComment) ); /* ZIP file comment length. */
		$this->_fwrite( $dataFP, $this->_comment );
		fclose( $dataFP );
		//sleep(2);

		// If Split ZIP and there is no .zip file, rename the last fragment to .ZIP
		if($this->_useSplitZIP)
		{
			$extension = substr($this->_dataFileName, -3);
			if($extension != '.zip')
			{
				AEUtilLogger::WriteLog(_AE_LOG_DEBUG, 'Renaming last ZIP part to .ZIP extension');
				$newName = $this->_dataFileNameBase.'.zip';
				if(!@rename($this->_dataFileName, $newName))
				{
					$this->setError('Could not rename last ZIP part to .ZIP extension.');
					return false;
				}
				$this->_dataFileName = $newName;
			}
		}
		// If Split ZIP and only one fragment, change the signature
		if($this->_useSplitZIP && ($this->_totalFragments == 1) )
		{
			$fp = fopen($this->_dataFileName, 'r+b');
			$this->_fwrite($fp, "\x50\x4b\x30\x30" );
		}

		if( function_exists('chmod') )
		{
			@chmod($this->_dataFileName, 0755);
		}
		return true;
	}


	/**
	 * Returns a string with the extension (including the dot) of the files produced
	 * by this class.
	 * @return string
	 */
	public function getExtension()
	{
		return '.zip';
	}


	/**
	 * The most basic file transaction: add a single entry (file or directory) to
	 * the archive.
	 *
	 * @param bool $isVirtual If true, the next parameter contains file data instead of a file name
	 * @param string $sourceNameOrData Absolute file name to read data from or the file data itself is $isVirtual is true
	 * @param string $targetName The (relative) file name under which to store the file in the archive
	 * @return True on success, false otherwise
	 */
	protected function _addFile( $isVirtual, &$sourceNameOrData, $targetName )
	{
		static $configuration;

		// Note down the starting disk number for Split ZIP archives
		if($this->_useSplitZIP) {
			$starting_disk_number_for_this_file = $this->_currentFragment - 1;
		} else {
			$starting_disk_number_for_this_file = 0;
		}

		if(!$configuration) {
			$configuration = AEFactory::getConfiguration();
		}

		if(!$configuration->get('volatile.engine.archiver.processingfile',false)) {
			// See if it's a directory
			$isDir = $isVirtual ? false : is_dir($sourceNameOrData);
			// See if it's a symlink (w/out dereference)
			$isSymlink = false;
			if($this->_symlink_store_target && !$isVirtual) {
				$isSymlink = is_link($sourceNameOrData);
			}

			// Get real size before compression
			if($isVirtual) {
				$fileSize = function_exists('mb_strlen') ? mb_strlen($sourceNameOrData, '8bit') : strlen($sourceNameOrData);
			} else {
				if($isSymlink) {
					$fileSize = function_exists('mb_strlen') ? mb_strlen(@readlink($sourceNameOrData),'8bit') : strlen(@readlink($sourceNameOrData));
				} else {
					$fileSize = $isDir ? 0 : @filesize($sourceNameOrData);
				}
			}

			// Get last modification time to store in archive
			$ftime = $isVirtual ? time() : @filemtime( $sourceNameOrData );

			// Decide if we will compress
			if ($isDir || $isSymlink) {
				$compressionMethod = 0; // don't compress directories...
			} else {
				// Do we have plenty of memory left?
				$memLimit = ini_get("memory_limit");
				if(strstr($memLimit, 'M')) {
					$memLimit = (int)$memLimit * 1048576;
				} elseif(strstr($memLimit, 'K')) {
					$memLimit = (int)$memLimit * 1024;
				} elseif(strstr($memLimit, 'G')) {
					$memLimit = (int)$memLimit * 1073741824;
				} else {
					$memLimit = (int)$memLimit;
				}				
				if (($memLimit == "") || ($fileSize >= _AKEEBA_COMPRESSION_THRESHOLD)) {
					// No memory limit, or over 1Mb files => always compress up to 1Mb files (otherwise it times out)
					$compressionMethod = ($fileSize <= _AKEEBA_COMPRESSION_THRESHOLD) ? 8 : 0;
				} elseif ( function_exists("memory_get_usage") ) {
					// PHP can report memory usage, see if there's enough available memory; Joomla! alone eats about 5-6Mb! This code is called on files <= 1Mb
					$memLimit = $this->_return_bytes( $memLimit );
					$availableRAM = $memLimit - memory_get_usage();
					$compressionMethod = (($availableRAM / 2.5) >= $fileSize) ? 8 : 0;
				} else {
					// PHP can't report memory usage, compress only files up to 512Kb (conservative approach) and hope it doesn't break
					$compressionMethod = ($fileSize <= 524288) ? 8 : 0;;
				}
			}

			$compressionMethod = function_exists("gzcompress") ? $compressionMethod : 0;

			$storedName = $targetName;

			if($isVirtual) AEUtilLogger::WriteLog(_AE_LOG_DEBUG, '  Virtual add:'.$storedName.' ('.$fileSize.') - '.$compressionMethod);

			/* "Local file header" segment. */
			$unc_len = $fileSize; // File size

			if (!$isDir) {
				// Get CRC for regular files, not dirs
				if($isVirtual)
				{
					$crc = crc32($sourceNameOrData);
				}
				else
				{
					$crcCalculator = new AECRC32CalcClass;
					$crc     = $crcCalculator->crc32_file( $sourceNameOrData, $this->AkeebaPackerZIP_CHUNK_SIZE ); // This is supposed to be the fast way to calculate CRC32 of a (large) file.
					unset( $crcCalculator );

					// If the file was unreadable, $crc will be false, so we skip the file
					if ($crc === false) {
						$this->setWarning( 'Could not calculate CRC32 for '.$sourceNameOrData);
						return false;
					}
				}
			} else if($isSymlink) {
				$crc = crc32( @readlink($sourceNameOrData) );
			} else {
				// Dummy CRC for dirs
				$crc = 0;
				$storedName .= "/";
				$unc_len = 0;
			}
			
			// If we have to compress, read the data in memory and compress it
			if ($compressionMethod == 8) {
				// Get uncompressed data
				if( $isVirtual )
				{
					$udata =& $sourceNameOrData;
				}
				else
				{
					$udata = @file_get_contents( $sourceNameOrData ); // PHP > 4.3.0 saves us the trouble
				}

				if ($udata === FALSE) {
					// Unreadable file, skip it. Normally, we should have exited on CRC code above
					$this->setWarning( 'Unreadable file '.$sourceNameOrData.'. Check permissions' );
					return false;
				} else {
					// Proceed with compression
					$zdata   = @gzcompress($udata);
					if ($zdata === false) {
						// If compression fails, let it behave like no compression was available
						$c_len = $unc_len;
						$compressionMethod = 0;
					} else {
						unset( $udata );

						$zdata   = substr(substr($zdata, 0, -4), 2);
						$c_len   = (function_exists('mb_strlen') ? mb_strlen($zdata,'8bit') : strlen($zdata));
					}
				}
			} else {
				$c_len = $unc_len;
			}

			/* Get the hex time. */
			$dtime    = dechex($this->_unix2DosTime($ftime));

			if( (function_exists('mb_strlen') ? mb_strlen($dtime,'8bit') : strlen($dtime)) < 8 ) $dtime = "00000000";
			$hexdtime = chr(hexdec($dtime[6] . $dtime[7])) .
			chr(hexdec($dtime[4] . $dtime[5])) .
			chr(hexdec($dtime[2] . $dtime[3])) .
			chr(hexdec($dtime[0] . $dtime[1]));

			// Get current data file size
			//clearstatcache();
			//$old_offset = @filesize( $this->_dataFileName );

			// If it's a split ZIP file, we've got to make sure that the header can fit in the part
			if($this->_useSplitZIP) {
				// Get header size, taking into account any extra header necessary
				$header_size = 30 + (function_exists('mb_strlen') ? mb_strlen($storedName,'8bit') : strlen($storedName));
				// Compare to free part space
				clearstatcache();
				$current_part_size = @filesize($this->_dataFileName);
				$free_space = $this->_fragmentSize - ($current_part_size === false ? 0 : $current_part_size);
				if($free_space <= $header_size)
				{
					// Not enough space on current part, create new part
					if(!$this->_createNewPart())
					{
						$this->setError('Could not create new ZIP part file '.basename($this->_dataFileName));
						return false;
					}
				}
			}
			// Open data file for output
			$fp = @fopen( $this->_dataFileName, "ab");
			if ($fp === false)
			{
				$this->setError("Could not open archive file {$this->_dataFileName} for append!");
				return false;
			}

			$seek_result = @fseek($fp, 0, SEEK_END);
			$old_offset = ($seek_result == -1) ? false : @ftell($fp);
			if($old_offset === false) {
				@clearstatcache();
				$old_offset = @filesize($this->_dataFileName);
			}

			// Get the file name length in bytes
			if(function_exists('mb_strlen')) {
				$fn_length = mb_strlen($storedName, '8bit');
			} else {
				$fn_length = strlen($storedName);
			}

			$this->_fwrite( $fp, $this->_fileHeader );				/* Begin creating the ZIP data. */
			if(!$isSymlink) {
				$this->_fwrite( $fp, "\x14\x00" );					/* Version needed to extract. */
			}
			else {
				$this->_fwrite( $fp, "\x0a\x03" );					/* Version needed to extract. */
			}
			$this->_fwrite( $fp, pack('v',2048) ); 					/* General purpose bit flag. Bit 11 set = use UTF-8 encoding for filenames & comments */
			$this->_fwrite( $fp, ($compressionMethod == 8) ? "\x08\x00" : "\x00\x00" );	/* Compression method. */
			$this->_fwrite( $fp, $hexdtime );						/* Last modification time/date. */
			$this->_fwrite( $fp, pack('V', $crc) );					/* CRC 32 information. */
			if(!isset($c_len)) $c_len = $unc_len;
			$this->_fwrite( $fp, pack('V', $c_len) );				/* Compressed filesize. */
			$this->_fwrite( $fp, pack('V', $unc_len) );				/* Uncompressed filesize. */
			$this->_fwrite( $fp, pack('v', $fn_length) );			/* Length of filename. */
			$this->_fwrite( $fp, pack('v', 0) );					/* Extra field length. */
			$this->_fwrite( $fp, $storedName );						/* File name. */
			
			// Cache useful information about the file
			if(!$isDir && !$isSymlink && !$isVirtual)
			{
				$configuration->set('volatile.engine.archiver.unc_len', $unc_len);
				$configuration->set('volatile.engine.archiver.hexdtime', $hexdtime);
				$configuration->set('volatile.engine.archiver.crc', $crc);
				$configuration->set('volatile.engine.archiver.c_len', $c_len);
				$configuration->set('volatile.engine.archiver.fn_length', $fn_length);
				$configuration->set('volatile.engine.archiver.old_offset', $old_offset);
				$configuration->set('volatile.engine.archiver.storedName', $storedName);
				$configuration->set('volatile.engine.archiver.sourceNameOrData', $sourceNameOrData);
			}
		}
		else
		{
			// Since we are continuing archiving, it's an uncompressed regular file. Set up the variables.
			$compressionMethod	= 1;
			$isDir				= false;
			$isSymlink			= false;
			$unc_len			= $configuration->get('volatile.engine.archiver.unc_len');
			$hexdtime			= $configuration->get('volatile.engine.archiver.hexdtime');
			$crc				= $configuration->get('volatile.engine.archiver.crc');
			$c_len				= $configuration->get('volatile.engine.archiver.c_len');
			$fn_length			= $configuration->get('volatile.engine.archiver.fn_length');
			$old_offset			= $configuration->get('volatile.engine.archiver.old_offset');
			$storedName			= $configuration->get('volatile.engine.archiver.storedName');
			
			
			// Open data file for output
			$fp = @fopen( $this->_dataFileName, "ab");
			if ($fp === false)
			{
				$this->setError("Could not open archive file {$this->_dataFileName} for append!");
				return false;
			}
		}


		/* "File data" segment. */
		if ($compressionMethod == 8) {
			// Just dump the compressed data
			if(!$this->_useSplitZIP)
			{
				$this->_fwrite( $fp, $zdata );
				if($this->getError()) return;
			}
			else
			{
				// Split ZIP. Check if we need to split the part in the middle of the data.
				clearstatcache();
				$current_part_size = @filesize($this->_dataFileName);
				$free_space = $this->_fragmentSize - ($current_part_size === false ? 0 : $current_part_size);
				if($free_space >= (function_exists('mb_strlen') ? mb_strlen($zdata,'8bit') : strlen($zdata)) )
				{
					// Write in one part
					$this->_fwrite( $fp, $zdata );
					if($this->getError()) return;
				}
				else
				{
					$bytes_left = (function_exists('mb_strlen') ? mb_strlen($zdata,'8bit') : strlen($zdata));

					while($bytes_left > 0)
					{
						clearstatcache();
						$current_part_size = @filesize($this->_dataFileName);
						$free_space = $this->_fragmentSize - ($current_part_size === false ? 0 : $current_part_size);

						// Split between parts - Write a part
						$this->_fwrite( $fp, $zdata, min((function_exists('mb_strlen') ? mb_strlen($zdata,'8bit') : strlen($zdata)),$free_space) );
						if($this->getError()) return;

						// Get the rest of the data
						$bytes_left = (function_exists('mb_strlen') ? mb_strlen($zdata,'8bit') : strlen($zdata)) - $free_space;

						if($bytes_left > 0)
						{
							// Create new part
							if(!$this->_createNewPart())
							{
								// Die if we couldn't create the new part
								$this->setError('Could not create new ZIP part file '.basename($this->_dataFileName));
								return false;
							}
							else
							{
								// Close the old data file
								fclose($fp);
								// Open data file for output
								$fp = @fopen( $this->_dataFileName, "ab");
								if ($fp === false)
								{
									$this->setError("Could not open archive file {$this->_dataFileName} for append!");
									return false;
								}
							}
							$zdata = substr($zdata, -$bytes_left);
						}
					}
				}
			}
			unset( $zdata );
		} elseif ( !($isDir || $isSymlink) ) {
			// Virtual file, just write the data!
			if( $isVirtual )
			{
				// Just dump the data
				if(!$this->_useSplitZIP)
				{
					$this->_fwrite( $fp, $sourceNameOrData );
					if($this->getError()) return;
				}
				else
				{
					// Split ZIP. Check if we need to split the part in the middle of the data.
					clearstatcache();
					$current_part_size = @filesize($this->_dataFileName);
					$free_space = $this->_fragmentSize - ($current_part_size === false ? 0 : $current_part_size);
					if($free_space >= (function_exists('mb_strlen') ? mb_strlen($sourceNameOrData,'8bit') : strlen($sourceNameOrData)) )
					{
						// Write in one part
						$this->_fwrite( $fp, $sourceNameOrData );
						if($this->getError()) return;
					}
					else
					{
						$bytes_left = (function_exists('mb_strlen') ? mb_strlen($sourceNameOrData,'8bit') : strlen($sourceNameOrData));

						while($bytes_left > 0)
						{
							clearstatcache();
							$current_part_size = @filesize($this->_dataFileName);
							$free_space = $this->_fragmentSize - ($current_part_size === false ? 0 : $current_part_size);
							// Split between parts - Write first part
							$this->_fwrite( $fp, $sourceNameOrData, min( (function_exists('mb_strlen') ? mb_strlen($zdata,'8bit') : strlen($zdata)) ,$free_space) );
							if($this->getError()) return;
							// Get the rest of the data
							$rest_size = (function_exists('mb_strlen') ? mb_strlen($sourceNameOrData,'8bit') : strlen($sourceNameOrData)) - $free_space;
							if($rest_size > 0)
							{
								// Create new part if required
								if(!$this->_createNewPart())
								{
									// Die if we couldn't create the new part
									$this->setError('Could not create new ZIP part file '.basename($this->_dataFileName));
									return false;
								}
								else
								{
									// Close the old data file
									fclose($fp);
									// Open data file for output
									$fp = @fopen( $this->_dataFileName, "ab");
									if ($fp === false)
									{
										$this->setError("Could not open archive file {$this->_dataFileName} for append!");
										return false;
									}
								}
								// Get the rest of the compressed data
								$zdata = substr($sourceNameOrData, -$rest_size);
							}
							$bytes_left = $rest_size;
						}
					}
				}
			}
			else
			{
				// IMPORTANT! Only this case can be spanned across steps: uncompressed, non-virtual data
				if($configuration->get('volatile.engine.archiver.processingfile',false))
				{
					$sourceNameOrData = $configuration->get('volatile.engine.archiver.sourceNameOrData', '');
					$unc_len = $configuration->get('volatile.engine.archiver.unc_len', 0);
					$resume = $configuration->get('volatile.engine.archiver.resume', 0);
				}

				// Copy the file contents, ignore directories
				$zdatafp = @fopen( $sourceNameOrData, "rb" );
				if( $zdatafp === FALSE )
				{
					$this->setWarning( 'Unreadable file '.$sourceNameOrData.'. Check permissions' );
					return false;
				}
				else
				{
					$timer = AEFactory::getTimer();
					// Seek to the resume point if required
					if($configuration->get('volatile.engine.archiver.processingfile',false))
					{
						// Seek to new offset
						$seek_result = @fseek($zdatafp, $resume);
						if( $seek_result === -1 )
						{
							// What?! We can't resume!
							$this->setError(sprintf('Could not resume packing of file %s. Your archive is damaged!', $sourceNameOrData));
							return false;
						}

						// Doctor the uncompressed size to match the remainder of the data
						$unc_len = $unc_len - $resume;
					}

					if(!$this->_useSplitZIP)
					{
						// For non Split ZIP, just dump the file very fast
						while( !feof($zdatafp) && ($timer->getTimeLeft() > 0) && ($unc_len > 0) ) {
							$zdata = fread($zdatafp, AKEEBA_CHUNK);
							$this->_fwrite( $fp, $zdata, min((function_exists('mb_strlen') ? mb_strlen($zdata,'8bit') : strlen($zdata)), AKEEBA_CHUNK) );
							$unc_len -= AKEEBA_CHUNK;
							if($this->getError()) return;
						}
						if(!feof($zdatafp) && ($unc_len != 0) )
						{
							// We have to break, or we'll time out!
							$resume = @ftell($zdatafp);
							$configuration->set('volatile.engine.archiver.resume', $resume);
							$configuration->set('volatile.engine.archiver.processingfile',true);
							return true;
						}
					}
					else
					{
						// Split ZIP - Do we have enough space to host the whole file?
						clearstatcache();
						$current_part_size = @filesize($this->_dataFileName);
						$free_space = $this->_fragmentSize - ($current_part_size === false ? 0 : $current_part_size);
						if($free_space >= $unc_len)
						{
							// Yes, it will fit inside this part, do quick copy
							while( !feof($zdatafp) && ($timer->getTimeLeft() > 0) && ($unc_len > 0) ) {
								$zdata = fread($zdatafp, AKEEBA_CHUNK);
								$this->_fwrite( $fp, $zdata, min((function_exists('mb_strlen') ? mb_strlen($zdata,'8bit') : strlen($zdata)), AKEEBA_CHUNK) );
								$unc_len -= AKEEBA_CHUNK;
								if($this->getError()) return;

							}
							if(!feof($zdatafp) && ($unc_len != 0))
							{
								// We have to break, or we'll time out!
								$resume = @ftell($zdatafp);
								$configuration->set('volatile.engine.archiver.resume', $resume);
								$configuration->set('volatile.engine.archiver.processingfile',true);
								return true;
							}
						}
						else
						{
							// No, we'll have to split between parts. We'll loop until we run
							// out of space.
							while( !feof($zdatafp) && ($timer->getTimeLeft() > 0) )
							{
								// No, we'll have to split between parts. Write the first part
								// Find optimal chunk size
								clearstatcache();
								$current_part_size = @filesize($this->_dataFileName);
								$free_space = $this->_fragmentSize - ($current_part_size === false ? 0 : $current_part_size);

								$chunk_size_primary = min(AKEEBA_CHUNK, $free_space);
								if($chunk_size_primary <= 0) $chunk_size_primary = max(AKEEBA_CHUNK, $free_space);
								// Calculate if we have to read some more data (smaller chunk size)
								// and how many times we must read w/ the primary chunk size
								$chunk_size_secondary = $free_space % $chunk_size_primary;
								$loop_times = ($free_space - $chunk_size_secondary) / $chunk_size_primary;
								// Read and write with the primary chunk size
								for( $i = 1; $i <= $loop_times; $i++ )
								{
									$zdata = fread($zdatafp, $chunk_size_primary);
									$this->_fwrite( $fp, $zdata, min((function_exists('mb_strlen') ? mb_strlen($zdata,'8bit') : strlen($zdata)), $chunk_size_primary) );
									$unc_len -= $chunk_size_primary;
									if($this->getError()) return;

									// Do we have enough time to proceed?
									if( (!feof($zdatafp)) && ($unc_len != 0) && ($timer->getTimeLeft() <= 0) ) {
										// No, we have to break, or we'll time out!
										$resume = @ftell($zdatafp);
										$configuration->set('volatile.engine.archiver.resume', $resume);
										$configuration->set('volatile.engine.archiver.processingfile',true);
										return true;
									}
								}
								// Read and write w/ secondary chunk size, if non-zero
								if($chunk_size_secondary > 0)
								{
									$zdata = fread($zdatafp, $chunk_size_secondary);
									$this->_fwrite( $fp, $zdata, min((function_exists('mb_strlen') ? mb_strlen($zdata,'8bit') : strlen($zdata)), $chunk_size_secondary) );
									$unc_len -= $chunk_size_secondary;
									if($this->getError()) return;
								}

								// Do we have enough time to proceed?
								if( (!feof($zdatafp)) && ($unc_len != 0) && ($timer->getTimeLeft() <= 0) ) {
									// No, we have to break, or we'll time out!
									$resume = @ftell($zdatafp);
									$configuration->set('volatile.engine.archiver.resume', $resume);
									$configuration->set('volatile.engine.archiver.processingfile',true);

									// ...and create a new part as well
									if(!$this->_createNewPart())
									{
										// Die if we couldn't create the new part
										$this->setError('Could not create new ZIP part file '.basename($this->_dataFileName));
										return false;
									}

									// ...then, return
									return true;
								}

								// Create new ZIP part, but only if we'll have more data to write
								if(!feof($zdatafp) && ($unc_len > 0) )
								{
									// Create new ZIP part
									if(!$this->_createNewPart())
									{
										// Die if we couldn't create the new part
										$this->setError('Could not create new ZIP part file '.basename($this->_dataFileName));
										return false;
									}
									else
									{
										// Close the old data file
										fclose($fp);

										// We have created the part. If the user asked for immediate post-proc, break step now.
										if($configuration->get('engine.postproc.common.after_part',0))
										{
											$resume = @ftell($zdatafp);
											$configuration->set('volatile.engine.archiver.resume', $resume);
											$configuration->set('volatile.engine.archiver.processingfile',true);
											
											$configuration->set('volatile.breakflag',true);
											@fclose($zdatafp);
											@fclose($fp);
											return true;
										}

										// Open data file for output
										$fp = @fopen( $this->_dataFileName, "ab");
										if ($fp === false)
										{
											$this->setError("Could not open archive file {$this->_dataFileName} for append!");
											return false;
										}
									}
								}

							} // end while

						}
					}
					fclose( $zdatafp );
				}
			}
		}
		elseif($isSymlink)
		{
			$this->_fwrite($fp, @readlink($sourceNameOrData) );
		}

		// Done with data file.
		fclose( $fp );

		// Open the central directory file for append
		$fp = @fopen( $this->_ctrlDirFileName, "ab");
		if ($fp === false)
		{
			$this->setError("Could not open Central Directory temporary file for append!");
			return false;
		}
		$this->_fwrite( $fp, $this->_ctrlDirHeader );
		if(!$isSymlink)
		{
			$this->_fwrite( $fp, "\x14\x00" );                /* Version made by (always set to 2.0). */
			$this->_fwrite( $fp, "\x14\x00" );					/* Version needed to extract */
			$this->_fwrite( $fp, pack('v',2048) );                /* General purpose bit flag */
			$this->_fwrite( $fp, ($compressionMethod == 8) ? "\x08\x00" : "\x00\x00" );	/* Compression method. */
		}
		else
		{
			// Symlinks get special treatment
			$this->_fwrite( $fp, "\x14\x03" );                /* Version made by (version 2.0 with UNIX extensions). */
			$this->_fwrite( $fp, "\x0a\x03" );					/* Version needed to extract */
			$this->_fwrite( $fp, pack('v',2048) );                /* General purpose bit flag */
			$this->_fwrite( $fp, "\x00\x00" );	/* Compression method. */
		}

		$this->_fwrite( $fp, $hexdtime );                 /* Last mod time/date. */
		$this->_fwrite( $fp, pack('V', $crc) );           /* CRC 32 information. */
		$this->_fwrite( $fp, pack('V', $c_len) );         /* Compressed filesize. */
		if($compressionMethod == 0) {
			// When we are not compressing, $unc_len is being reduced to 0 while backing up.
			// With this trick, we always store the correct length, as in this case the compressed
			// and uncompressed length is always the same.
			$this->_fwrite( $fp, pack('V', $c_len) );       /* Uncompressed filesize. */
		} else {
			// When compressing, the uncompressed length differs from compressed length
			// and this line writes the correct value.
			$this->_fwrite( $fp, pack('V', $unc_len) );       /* Uncompressed filesize. */
		}
		$this->_fwrite( $fp, pack('v', $fn_length) );  /* Length of filename. */
		$this->_fwrite( $fp, pack('v', 0 ) ); /* Extra field length. */
		$this->_fwrite( $fp, pack('v', 0 ) );             /* File comment length. */
		$this->_fwrite( $fp, pack('v', $starting_disk_number_for_this_file ) ); /* Disk number start. */
		$this->_fwrite( $fp, pack('v', 0 ) );             /* Internal file attributes. */
		if(!$isSymlink)
		{
			$this->_fwrite( $fp, pack('V', $isDir ? 0x41FF0010 : 0xFE49FFE0) ); /* External file attributes -   'archive' bit set. */
		}
		else
		{
			// For SymLinks we store UNIX file attributes
			$this->_fwrite( $fp, "\x20\x80\xFF\xA1" ); /* External file attributes for Symlink. */
		}
		$this->_fwrite( $fp, pack('V', $old_offset) );    /* Relative offset of local header. */
		$this->_fwrite( $fp, $storedName );                     /* File name. */
		/* Optional extra field, file comment goes here. */

		// Finished with Central Directory
		fclose( $fp );

		// Finaly, increase the file counter by one
		$this->_totalFileEntries++;

		// Uncache data
		$configuration->set('volatile.engine.archiver.sourceNameOrData', null);
		$configuration->set('volatile.engine.archiver.unc_len', null);
		$configuration->set('volatile.engine.archiver.resume', null);
		$configuration->set('volatile.engine.archiver.hexdtime', null);
		$configuration->set('volatile.engine.archiver.crc', null);
		$configuration->set('volatile.engine.archiver.c_len', null);
		$configuration->set('volatile.engine.archiver.fn_length', null);
		$configuration->set('volatile.engine.archiver.old_offset', null);
		$configuration->set('volatile.engine.archiver.storedName', null);
		$configuration->set('volatile.engine.archiver.sourceNameOrData', null);
		
		$configuration->set('volatile.engine.archiver.processingfile',false);

		// ... and return TRUE = success
		return TRUE;
	}

	// ------------------------------------------------------------------------
	// Archiver-specific utility functions
	// ------------------------------------------------------------------------

	/**
	 * Converts a UNIX timestamp to a 4-byte DOS date and time format
	 * (date in high 2-bytes, time in low 2-bytes allowing magnitude
	 * comparison).
	 *
	 * @param integer $unixtime  The current UNIX timestamp.
	 *
	 * @return integer  The current date in a 4-byte DOS format.
	 */
	private function _unix2DOSTime($unixtime = null)
	{
		$timearray = (is_null($unixtime)) ? getdate() : getdate($unixtime);

		if ($timearray['year'] < 1980) {
			$timearray['year']    = 1980;
			$timearray['mon']     = 1;
			$timearray['mday']    = 1;
			$timearray['hours']   = 0;
			$timearray['minutes'] = 0;
			$timearray['seconds'] = 0;
		}

		return (($timearray['year'] - 1980) << 25) |
		($timearray['mon'] << 21) |
		($timearray['mday'] << 16) |
		($timearray['hours'] << 11) |
		($timearray['minutes'] << 5) |
		($timearray['seconds'] >> 1);
	}

	private function _createNewPart($finalPart = false)
	{
		// Push the previous part if we have to post-process it immediately
		$configuration = AEFactory::getConfiguration();
		if($configuration->get('engine.postproc.common.after_part',0))
		{
			$this->finishedPart[] = $this->_dataFileName;
		}
		
		// Add the part's size to our rolling sum
		clearstatcache();
		$this->_totalDataSize += filesize($this->_dataFileName);

		$this->_totalFragments++;
		$this->_currentFragment = $this->_totalFragments;
		if($finalPart)
		{
			$this->_dataFileName = $this->_dataFileNameBase.'.zip';
		}
		else
		{
			$this->_dataFileName = $this->_dataFileNameBase.'.z'.sprintf('%02d', $this->_currentFragment);
		}
		AEUtilLogger::WriteLog(_AE_LOG_INFO, 'Creating new ZIP part #'.$this->_currentFragment.', file '.$this->_dataFileName);
		// Inform CUBE that we have changed the multipart number
		$statistics = AEFactory::getStatistics();
		$statistics->updateMultipart($this->_totalFragments);
		// Try to remove any existing file
		@unlink($this->_dataFileName);
		// Touch the new file
		$result = @touch($this->_dataFileName);
		if(function_exists('chmod')) {
			chmod($this->_dataFileName, 0666);
		}		
		return $result;
	}
}

// ===================================================================================================

/**
 * A handy class to abstract the calculation of CRC32 of files under various
 * server conditions and versions of PHP.
 */
class AECRC32CalcClass
{
	/**
	 * Returns the CRC32 of a file, selecting the more appropriate algorithm.
	 *
	 * @param string $filename Absolute path to the file being processed
	 * @param integer $AkeebaPackerZIP_CHUNK_SIZE Obsoleted
	 * @return integer The CRC32 in numerical form
	 */
	public function crc32_file( $filename, $AkeebaPackerZIP_CHUNK_SIZE )
	{
		static $configuration;

		if(!$configuration)
		{
			$configuration = AEFactory::getConfiguration();
		}

		if( function_exists("hash_file") )
		{
			$res = $this->crc32_file_php512( $filename );
			AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "File $filename - CRC32 = " . dechex($res) . " [PHP512]" );
		}
		else if ( function_exists("file_get_contents") && ( @filesize($filename) <= $AkeebaPackerZIP_CHUNK_SIZE ) ) {
			$res = $this->crc32_file_getcontents( $filename );
			AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "File $filename - CRC32 = " . dechex($res) . " [GETCONTENTS]" );
		} else {
			$res = $this->crc32_file_php4($filename, $AkeebaPackerZIP_CHUNK_SIZE);
			AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "File $filename - CRC32 = " . dechex($res) . " [PHP4]" );
		}

		if ($res === FALSE) {
			$this->setWarning( "File $filename - NOT READABLE: CRC32 IS WRONG!" );
		}
		return $res;
	}

	/**
	 * Very efficient CRC32 calculation for PHP 5.1.2 and greater, requiring
	 * the 'hash' PECL extension
	 *
	 * @param string $filename Absolute filepath
	 * @return integer The CRC32
	 */
	private function crc32_file_php512($filename)
	{
		// Detection of buggy PHP hosts
		static $mustInvert = null;
		if(is_null($mustInvert))
		{
			$test_crc = @hash('crc32b', 'test', false);
			$mustInvert = ( strtolower($test_crc) == '0c7e7fd8'); // Normally, it's D87F7E0C :)
			if($mustInvert) AEUtilLogger::WriteLog(_AE_LOG_WARNING,'Your server has a buggy PHP version which produces inverted CRC32 values. Attempting a workaround. ZIP files may appear as corrupt.');
		}

		$res = @hash_file('crc32b', $filename, false );
		if($mustInvert)
		{
			// Workaround for buggy PHP versions (I think before 5.1.8) which produce inverted CRC32 sums
			$res2 = substr($res,6,2) . substr($res,4,2) . substr($res,2,2) . substr($res,0,2);
			$res = $res2;
		}
		$res = hexdec( $res );
		return $res;
	}

	/**
	 * A compatible CRC32 calculation using file_get_contents, utilizing immense
	 * ammounts of RAM
	 *
	 * @param string $filename
	 * @return integer
	 */
	private function crc32_file_getcontents($filename)
	{
		return crc32( @file_get_contents($filename) );
	}

	/**
	 * There used to be a workaround for large files under PHP4. However, it never
	 * really worked, so it is removed and a warning is posted instead.
	 *
	 * @param string $filename
	 * @param integer $AkeebaPackerZIP_CHUNK_SIZE
	 * @return integer
	 */
	private function crc32_file_php4($filename, $AkeebaPackerZIP_CHUNK_SIZE)
	{
		$this->setWarning( "Function hash_file not detected processing the 'large'' file $filename; it will appear as seemingly corrupt in the archive. Only the CRC32 is invalid, though. Please read our forum announcement for explanation of this message." );
		return 0;
	}
}
Back to Top