You are here

function PclZip::privReadFileHeader in Quiz 6.5

Same name and namespace in other branches
  1. 6.6 includes/moodle/lib/pclzip/pclzip.lib.php \PclZip::privReadFileHeader()
4 calls to PclZip::privReadFileHeader()
PclZip::privDeleteByRule in includes/moodle/lib/pclzip/pclzip.lib.php
PclZip::privExtractFile in includes/moodle/lib/pclzip/pclzip.lib.php
PclZip::privExtractFileAsString in includes/moodle/lib/pclzip/pclzip.lib.php
PclZip::privExtractFileInOutput in includes/moodle/lib/pclzip/pclzip.lib.php

File

includes/moodle/lib/pclzip/pclzip.lib.php, line 4080

Class

PclZip

Code

function privReadFileHeader(&$p_header) {

  //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadFileHeader", "");
  $v_result = 1;

  // ----- Read the 4 bytes signature
  $v_binary_data = @fread($this->zip_fd, 4);

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
  $v_data = unpack('Vid', $v_binary_data);

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");

  // ----- Check signature
  if ($v_data['id'] != 0x4034b50) {

    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Invalid File header");

    // ----- Error log
    PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');

    // ----- Return

    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
    return PclZip::errorCode();
  }

  // ----- Read the first 42 bytes of the header
  $v_binary_data = fread($this->zip_fd, 26);

  // ----- Look for invalid block size
  if (strlen($v_binary_data) != 26) {
    $p_header['filename'] = "";
    $p_header['status'] = "invalid_header";

    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid block size : ".strlen($v_binary_data));

    // ----- Error log
    PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : " . strlen($v_binary_data));

    // ----- Return

    //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
    return PclZip::errorCode();
  }

  // ----- Extract the values

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Header : '".$v_binary_data."'");

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Header (Hex) : '".bin2hex($v_binary_data)."'");
  $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);

  // ----- Get filename

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "File name length : ".$v_data['filename_len']);
  $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Filename : \''.$p_header['filename'].'\'');

  // ----- Get extra_fields

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extra field length : ".$v_data['extra_len']);
  if ($v_data['extra_len'] != 0) {
    $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
  }
  else {
    $p_header['extra'] = '';
  }

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Extra field : \''.bin2hex($p_header['extra']).'\'');

  // ----- Extract properties
  $p_header['version_extracted'] = $v_data['version'];

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Version need to extract : ('.$p_header['version_extracted'].') \''.($p_header['version_extracted']/10).'.'.($p_header['version_extracted']%10).'\'');
  $p_header['compression'] = $v_data['compression'];

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compression method : \''.$p_header['compression'].'\'');
  $p_header['size'] = $v_data['size'];

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size : \''.$p_header['size'].'\'');
  $p_header['compressed_size'] = $v_data['compressed_size'];

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Compressed Size : \''.$p_header['compressed_size'].'\'');
  $p_header['crc'] = $v_data['crc'];

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'CRC : \''.sprintf("0x%X", $p_header['crc']).'\'');
  $p_header['flag'] = $v_data['flag'];

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Flag : \''.$p_header['flag'].'\'');
  $p_header['filename_len'] = $v_data['filename_len'];

  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Filename_len : \''.$p_header['filename_len'].'\'');

  // ----- Recuperate date in UNIX format
  $p_header['mdate'] = $v_data['mdate'];
  $p_header['mtime'] = $v_data['mtime'];
  if ($p_header['mdate'] && $p_header['mtime']) {

    // ----- Extract time
    $v_hour = ($p_header['mtime'] & 0xf800) >> 11;
    $v_minute = ($p_header['mtime'] & 0x7e0) >> 5;
    $v_seconde = ($p_header['mtime'] & 0x1f) * 2;

    // ----- Extract date
    $v_year = (($p_header['mdate'] & 0xfe00) >> 9) + 1980;
    $v_month = ($p_header['mdate'] & 0x1e0) >> 5;
    $v_day = $p_header['mdate'] & 0x1f;

    // ----- Get UNIX date format
    $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);

    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
  }
  else {
    $p_header['mtime'] = time();

    //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date is actual : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
  }

  // TBC

  //for(reset($v_data); $key = key($v_data); next($v_data)) {

  //  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Attribut[$key] = ".$v_data[$key]);

  //}

  // ----- Set the stored filename
  $p_header['stored_filename'] = $p_header['filename'];

  // ----- Set the status field
  $p_header['status'] = "ok";

  // ----- Return

  //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
  return $v_result;
}