public function Get_Tree($dir, $recursive = false, $filter = "") { $tree = array(); $mydir = dir($dir); if (!$mydir) { return array(); } while (($file = $mydir->read()) !== false) { if ($file == "." || $file == "..") { continue; } if (!empty($filter) && strpos($file, $filter) === false) { continue; } $theFile = $dir . "/" . $file; if (is_dir($theFile)) { if ($recursive) { $tree[$file]["sub"] = $this->Get_Tree($dir . "/" . $file, true); } $tree[$file]["size"] = "---"; } else { $tree[$file]["size"] = $this->Get_Size(filesize($theFile)); } $tree[$file]["attr"] = $this->Get_Attrib(substr(DecOct(fileperms($theFile)), -3)); $tree[$file]["time"] = date("Y/d/y H:i:s", filemtime($theFile)); } $mydir->close(); return $tree; }
function _writeLongHeader($p_filename) { $v_size = sprintf("%11s ", DecOct(strlen($p_filename))); $v_typeflag = 'L'; $v_linkname = ''; $v_magic = ''; $v_version = ''; $v_uname = ''; $v_gname = ''; $v_devmajor = ''; $v_devminor = ''; $v_prefix = ''; $v_binary_data_first = pack("a100a8a8a8a12A12", '././@LongLink', 0, 0, 0, $v_size, 0); $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12", $v_typeflag, $v_linkname, $v_magic, $v_version, $v_uname, $v_gname, $v_devmajor, $v_devminor, $v_prefix, ''); // ----- Calculate the checksum $v_checksum = 0; // ..... First part of the header for ($i = 0; $i < 148; $i++) { $v_checksum += ord(substr($v_binary_data_first, $i, 1)); } // ..... Ignore the checksum value and replace it by ' ' (space) for ($i = 148; $i < 156; $i++) { $v_checksum += ord(' '); } // ..... Last part of the header for ($i = 156, $j = 0; $i < 512; $i++, $j++) { $v_checksum += ord(substr($v_binary_data_last, $j, 1)); } // ----- Write the first 148 bytes of the header in the archive $this->_writeBlock($v_binary_data_first, 148); // ----- Write the calculated checksum $v_checksum = sprintf("%6s ", DecOct($v_checksum)); $v_binary_data = pack("a8", $v_checksum); $this->_writeBlock($v_binary_data, 8); // ----- Write the last 356 bytes of the header in the archive $this->_writeBlock($v_binary_data_last, 356); // ----- Write the filename as content of the block $i = 0; while (($v_buffer = substr($p_filename, $i++ * 512, 512)) != '') { $v_binary_data = pack("a512", "{$v_buffer}"); $this->_writeBlock($v_binary_data); } return true; }
function _writeFileHeader($p_file, $p_sname, $p_data = false) { if (!$p_data) { if (!$p_sname) { $p_sname = $p_file; } $p_sname = $this->_pathTrans($p_sname); $h_info = stat($p_file); $h[0] = sprintf("%6s ", DecOct($h_info[4])); $h[] = sprintf("%6s ", DecOct($h_info[5])); $h[] = sprintf("%6s ", DecOct(fileperms($p_file))); clearstatcache(); $h[] = sprintf("%11s ", DecOct(filesize($p_file))); $h[] = sprintf("%11s", DecOct(filemtime($p_file))); $dir = @is_dir($p_file) ? '5' : ''; } else { $dir = ''; $p_data = sprintf("%11s ", DecOct($p_data)); $time = sprintf("%11s ", DecOct(time())); $h = array(" 0 ", " 0 ", " 40777 ", $p_data, $time); } $data_first = pack("a100a8a8a8a12A12", $p_sname, $h[2], $h[0], $h[1], $h[3], $h[4]); $data_last = pack("a1a100a6a2a32a32a8a8a155a12", $dir, '', '', '', '', '', '', '', '', ""); for ($i = 0, $chks = 0; $i < 148; $i++) { $chks += ord($data_first[$i]); } for ($i = 156, $chks += 256, $j = 0; $i < 512; $i++, $j++) { $chks += ord($data_last[$j]); } $this->_write($data_first); $chks = pack("a8", sprintf("%6s ", DecOct($chks))); $this->_write($chks . $data_last); return true; }
function PclTarHandleReadHeader($v_binary_data, &$v_header) { TrFctStart(__FILE__, __LINE__, "PclTarHandleReadHeader", ""); $v_result = 1; // ----- Read the 512 bytes header /* if ($p_tar_mode == "tar") $v_binary_data = fread($p_tar, 512); else $v_binary_data = gzread($p_tar, 512); */ // ----- Look for no more block if (strlen($v_binary_data) == 0) { $v_header[filename] = ""; $v_header[status] = "empty"; // ----- Return TrFctEnd(__FILE__, __LINE__, $v_result, "End of archive found"); return $v_result; } // ----- Look for invalid block size if (strlen($v_binary_data) != 512) { $v_header[filename] = ""; $v_header[status] = "invalid_header"; TrFctMessage(__FILE__, __LINE__, 2, "Invalid block size : " . strlen($v_binary_data)); // ----- Error log PclErrorLog(-10, "Invalid block size : " . strlen($v_binary_data)); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Calculate the checksum $v_checksum = 0; // ..... First part of the header for ($i = 0; $i < 148; $i++) { $v_checksum += ord(substr($v_binary_data, $i, 1)); } // ..... Ignore the checksum value and replace it by ' ' (space) for ($i = 148; $i < 156; $i++) { $v_checksum += ord(' '); } // ..... Last part of the header for ($i = 156; $i < 512; $i++) { $v_checksum += ord(substr($v_binary_data, $i, 1)); } TrFctMessage(__FILE__, __LINE__, 3, "Calculated checksum : {$v_checksum}"); // ----- Extract the values TrFctMessage(__FILE__, __LINE__, 2, "Header : '{$v_binary_data}'"); $v_data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $v_binary_data); // ----- Extract the checksum for check $v_header[checksum] = OctDec(trim($v_data[checksum])); TrFctMessage(__FILE__, __LINE__, 3, "File checksum : {$v_header['checksum']}"); if ($v_header[checksum] != $v_checksum) { TrFctMessage(__FILE__, __LINE__, 2, "File checksum is invalid : {$v_checksum} calculated, {$v_header['checksum']} expected"); $v_header[filename] = ""; $v_header[status] = "invalid_header"; // ----- Look for last block (empty block) if ($v_checksum == 256 && $v_header[checksum] == 0) { $v_header[status] = "empty"; // ----- Return TrFctEnd(__FILE__, __LINE__, $v_result, "End of archive found"); return $v_result; } // ----- Error log PclErrorLog(-13, "Invalid checksum : {$v_checksum} calculated, {$v_header['checksum']} expected"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } TrFctMessage(__FILE__, __LINE__, 2, "File checksum is valid ({$v_checksum})"); // ----- Extract the properties $v_header[filename] = trim($v_data[filename]); TrFctMessage(__FILE__, __LINE__, 2, "Name : '{$v_header['filename']}'"); $v_header[mode] = OctDec(trim($v_data[mode])); TrFctMessage(__FILE__, __LINE__, 2, "Mode : '" . DecOct($v_header[mode]) . "'"); $v_header[uid] = OctDec(trim($v_data[uid])); TrFctMessage(__FILE__, __LINE__, 2, "Uid : '{$v_header['uid']}'"); $v_header[gid] = OctDec(trim($v_data[gid])); TrFctMessage(__FILE__, __LINE__, 2, "Gid : '{$v_header['gid']}'"); $v_header[size] = OctDec(trim($v_data[size])); TrFctMessage(__FILE__, __LINE__, 2, "Size : '{$v_header['size']}'"); $v_header[mtime] = OctDec(trim($v_data[mtime])); TrFctMessage(__FILE__, __LINE__, 2, "Date : " . date("l dS of F Y h:i:s A", $v_header[mtime])); if (($v_header[typeflag] = $v_data[typeflag]) == "5") { $v_header[size] = 0; TrFctMessage(__FILE__, __LINE__, 2, "Size (folder) : '{$v_header['size']}'"); } TrFctMessage(__FILE__, __LINE__, 2, "File typeflag : {$v_header['typeflag']}"); /* ----- All these fields are removed form the header because they do not carry interesting info $v_header[link] = trim($v_data[link]); TrFctMessage(__FILE__, __LINE__, 2, "Linkname : $v_header[linkname]"); $v_header[magic] = trim($v_data[magic]); TrFctMessage(__FILE__, __LINE__, 2, "Magic : $v_header[magic]"); $v_header[version] = trim($v_data[version]); TrFctMessage(__FILE__, __LINE__, 2, "Version : $v_header[version]"); $v_header[uname] = trim($v_data[uname]); TrFctMessage(__FILE__, __LINE__, 2, "Uname : $v_header[uname]"); $v_header[gname] = trim($v_data[gname]); TrFctMessage(__FILE__, __LINE__, 2, "Gname : $v_header[gname]"); $v_header[devmajor] = trim($v_data[devmajor]); TrFctMessage(__FILE__, __LINE__, 2, "Devmajor : $v_header[devmajor]"); $v_header[devminor] = trim($v_data[devminor]); TrFctMessage(__FILE__, __LINE__, 2, "Devminor : $v_header[devminor]"); */ // ----- Set the status field $v_header[status] = "ok"; // ----- Return TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; }
private function _writeHeaderBlock($strFilename, $iSize, $p_mtime = 0, $p_perms = 0, $p_type = '', $p_uid = 0, $p_gid = 0) { $strFilename = $this->_normalizePath($strFilename); if (strlen($strFilename) > 99) { if (!$this->_writeLongHeader($strFilename)) { return false; } } if ($p_type == "5") { $v_size = sprintf("%11s ", DecOct(0)); } else { $v_size = sprintf("%11s ", DecOct($iSize)); } $v_uid = sprintf("%6s ", DecOct($p_uid)); $v_gid = sprintf("%6s ", DecOct($p_gid)); $v_perms = sprintf("%6s ", DecOct($p_perms)); $v_mtime = sprintf("%11s", DecOct($p_mtime)); $v_linkname = ''; $v_magic = ''; $v_version = ''; $v_uname = ''; $v_gname = ''; $v_devmajor = ''; $v_devminor = ''; $v_prefix = ''; $v_binary_data_first = pack("a100a8a8a8a12A12", $strFilename, $v_perms, $v_uid, $v_gid, $v_size, $v_mtime); $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12", $p_type, $v_linkname, $v_magic, $v_version, $v_uname, $v_gname, $v_devmajor, $v_devminor, $v_prefix, ''); $v_checksum = 0; for ($i = 0; $i < 148; $i++) { $v_checksum += ord(self::$bMbstring ? mb_substr($v_binary_data_first, $i, 1, "latin1") : substr($v_binary_data_first, $i, 1)); } for ($i = 148; $i < 156; $i++) { $v_checksum += ord(' '); } for ($i = 156, $j = 0; $i < 512; $i++, $j++) { $v_checksum += ord(self::$bMbstring ? mb_substr($v_binary_data_last, $j, 1, "latin1") : substr($v_binary_data_last, $j, 1)); } $this->_writeBlock($v_binary_data_first, 148); $v_checksum = sprintf("%6s ", DecOct($v_checksum)); $v_binary_data = pack("a8", $v_checksum); $this->_writeBlock($v_binary_data, 8); $this->_writeBlock($v_binary_data_last, 356); return true; }
/** * Private static function to compute the header to be sent before a file in a tar archive. * * @access private * @author Laurent Perrin * @version 02/09/04 * @since 02/09/04 * @param string $archive_filename Filename as it will appear in the archive. Will be troncated at 99 chars. * @param string $real_filename Real filename, to be passed to filesize(), stat(), and sileperms(). * @return string 512 byte long tar header for this file. **/ function _tar_header($archive_filename, $real_filename) { $archive_filename = substr($archive_filename, 0, 99); $header = ''; // ----- Get file info $v_info = stat($real_filename); $v_uid = sprintf("%6s ", DecOct($v_info[4])); $v_gid = sprintf("%6s ", DecOct($v_info[5])); $v_perms = sprintf("%6s ", DecOct(fileperms($real_filename))); // ----- File mtime $v_mtime_data = filemtime($real_filename); $v_mtime = sprintf("%11s", DecOct($v_mtime_data)); // ----- File typeflag // '0' or '\0' is the code for regular file // '5' is directory if (is_dir($real_filename)) { $v_typeflag = '5'; $v_size = 0; } else { $v_typeflag = ''; // ----- Get the file size $v_size = filesize($real_filename); } $v_size = sprintf("%11s ", DecOct($v_size)); // ----- Compose the binary string of the header in two parts arround the checksum position $v_binary_data_first = pack("a100a8a8a8a12A12", $archive_filename, $v_perms, $v_uid, $v_gid, $v_size, $v_mtime); $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12", $v_typeflag, '', '', '', '', '', '', '', '', ''); // ----- Calculate the checksum $v_checksum = 0; // ..... First part of the header for ($i = 0; $i < 148; $i++) { $v_checksum += ord(substr($v_binary_data_first, $i, 1)); } // ..... Ignore the checksum value and replace it by ' ' (space) for ($i = 148; $i < 156; $i++) { $v_checksum += ord(' '); } // ..... Last part of the header for ($i = 156, $j = 0; $i < 512; $i++, $j++) { $v_checksum += ord(substr($v_binary_data_last, $j, 1)); } // ----- Write the first 148 bytes of the header in the archive $header .= $v_binary_data_first; // ----- Write the calculated checksum $v_checksum = sprintf("%6s ", DecOct($v_checksum)); $v_binary_data = pack("a8", $v_checksum); $header .= $v_binary_data; // ----- Write the last 356 bytes of the header in the archive $header .= $v_binary_data_last; return $header; }
function build_page($method = "") { global $mystep, $req, $tpl, $tpl_info, $setting, $idx, $tpl_path, $method; $fso = $mystep->getInstance("MyFSO"); $tpl_info['idx'] = "web_template"; if ($method != "show") { $tpl_info['idx'] .= $method == "list" ? "_list" : "_input"; } $tpl_tmp = $mystep->getInstance("MyTpl", $tpl_info); if ($method == "show") { $tpl_tmp->Set_Variable('title', $setting['language']['admin_web_template_title']); $tpl_tmp->Set_Variable('tpl_idx', $idx); $tpl_list = $fso->Get_List($tpl_path); $max_count = count($tpl_list['dir']); $the_list = array(); for ($i = 0; $i < $max_count; $i++) { $tpl_list['dir'][$i] = basename($tpl_list['dir'][$i]); if ($tpl_list['dir'][$i] == "cache" || strpos($tpl_list['dir'][$i], "admin") !== false) { continue; } $tpl_tmp->Set_Loop("tpl_list", array("idx" => $tpl_list['dir'][$i], "img" => is_file($tpl_path . $tpl_list['dir'][$i] . "/sample.jpg") ? "/" . $setting['path']['template'] . "/" . $tpl_list['dir'][$i] . "/sample.jpg" : "/images/noimage.gif")); $the_list[] = $tpl_list['dir'][$i]; } $tpl_tmp->Set_Variable('tpl_list', toJson($the_list, $setting['gen']['charset'])); $max_count = count($GLOBALS['website']); for ($i = 0; $i < $max_count; $i++) { $setting_sub = getSubSetting($GLOBALS['website'][$i]['web_id']); $GLOBALS['website'][$i]['tpl'] = $setting_sub['gen']['template']; $tpl_tmp->Set_Loop("website", $GLOBALS['website'][$i]); } } elseif ($method == "list") { $tpl_tmp->Set_Variable('title', $setting['language']['admin_web_template_title']); $tpl_tmp->Set_Variable('tpl_idx', $idx); $tpl_list = $fso->Get_List($tpl_path); $max_count = count($tpl_list['dir']); for ($i = 0; $i < $max_count; $i++) { $tpl_list['dir'][$i] = basename($tpl_list['dir'][$i]); if ($tpl_list['dir'][$i] == "cache") { continue; } $tpl_tmp->Set_Loop("tpl_list", array("idx" => $tpl_list['dir'][$i], "selected" => $tpl_list['dir'][$i] == $idx ? "selected" : "")); } $css_file = ROOT_PATH . "/images/" . $idx . "/style.css"; if (is_file($css_file)) { $tpl_tmp->Set_Loop("file", array("name" => "style.css", "size" => GetFileSize(filesize($css_file)), "attr" => $fso->Get_Attrib(substr(DecOct(fileperms($css_file)), -3)), "time" => date("Y/m/d H:i:s", filemtime($css_file)))); } $file_list = $fso->Get_Tree($tpl_path . $idx, false, ".tpl"); foreach ($file_list as $key => $value) { $curFile = $value; $curFile['name'] = $key; $tpl_tmp->Set_Loop("file", $curFile); } } else { $file = array(); $file['idx'] = $idx; $file['content'] = ""; if ($method == "edit") { $file['name'] = $req->getGet("file"); if ($file['name'] == "style.css") { $the_file = ROOT_PATH . "/images/" . $idx . "/style.css"; $file['type'] = "css"; } else { $the_file = $tpl_path . $idx . "/" . $file['name']; $file['type'] = "htmlmixed"; } if (is_file($the_file)) { $file['content'] = file_get_contents($the_file); $file['content'] = htmlspecialchars($file['content']); $file['content'] = str_replace("\t", " ", $file['content']); } $tpl_tmp->Set_Variable('title', $setting['language']['admin_web_template_edit']); } else { $file['name'] = ""; $tpl_tmp->Set_Variable('title', $setting['language']['admin_web_template_add']); } $tpl_tmp->Set_Variable('readonly', $method == "edit" ? "readonly" : ""); $tpl_tmp->Set_Variables($file, "file"); } $tpl_tmp->Set_Variable('back_url', $req->getServer("HTTP_REFERER")); $tpl_tmp->Set_Variable('method', $method); $tpl->Set_Variable('main', $tpl_tmp->Get_Content('$db, $setting')); unset($tpl_tmp); $mystep->show($tpl); return; }
function _writeHeader($p_filename, $p_stored_filename) { if ($p_stored_filename == '') { $p_stored_filename = $p_filename; } $v_reduce_filename = $this->_pathReduction($p_stored_filename); $v_info = stat($p_filename); $v_uid = sprintf("%6s ", DecOct($v_info[4])); $v_gid = sprintf("%6s ", DecOct($v_info[5])); $v_perms = sprintf("%6s ", DecOct(fileperms($p_filename))); $v_mtime = sprintf("%11s", DecOct(filemtime($p_filename))); if (@is_dir($p_filename)) { $v_typeflag = "5"; $v_size = sprintf("%11s ", DecOct(0)); } else { $v_typeflag = ''; clearstatcache(); $v_size = sprintf("%11s ", DecOct(filesize($p_filename))); } $v_linkname = ''; $v_magic = ''; $v_version = ''; $v_uname = ''; $v_gname = ''; $v_devmajor = ''; $v_devminor = ''; $v_prefix = ''; $v_binary_data_first = pack("a100a8a8a8a12A12", $v_reduce_filename, $v_perms, $v_uid, $v_gid, $v_size, $v_mtime); $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12", $v_typeflag, $v_linkname, $v_magic, $v_version, $v_uname, $v_gname, $v_devmajor, $v_devminor, $v_prefix, ''); // ----- Calculate the checksum $v_checksum = 0; // ..... First part of the header for ($i = 0; $i < 148; $i++) { $v_checksum += ord(substr($v_binary_data_first, $i, 1)); } // ..... Ignore the checksum value and replace it by ' ' (space) for ($i = 148; $i < 156; $i++) { $v_checksum += ord(' '); } // ..... Last part of the header for ($i = 156, $j = 0; $i < 512; $i++, $j++) { $v_checksum += ord(substr($v_binary_data_last, $j, 1)); } // ----- Write the first 148 bytes of the header in the archive if ($this->_compress) { @gzputs($this->_file, $v_binary_data_first, 148); } else { @fputs($this->_file, $v_binary_data_first, 148); } // ----- Write the calculated checksum $v_checksum = sprintf("%6s ", DecOct($v_checksum)); $v_binary_data = pack("a8", $v_checksum); if ($this->_compress) { @gzputs($this->_file, $v_binary_data, 8); } else { @fputs($this->_file, $v_binary_data, 8); } // ----- Write the last 356 bytes of the header in the archive if ($this->_compress) { @gzputs($this->_file, $v_binary_data_last, 356); } else { @fputs($this->_file, $v_binary_data_last, 356); } return true; }
function writeHeader($filename, $keep_filename) { $packF = 'a100a8a8a8a12A12'; $packL = 'a1a100a6a2a32a32a8a8a155a12'; if (strlen($keep_filename) <= 0) { $keep_filename = $filename; } $filename_ready = $this->makeGoodPath($keep_filename); if (strlen($filename_ready) > 99) { //write long header $dataFirst = pack($packF, '././LongLink', 0, 0, 0, sprintf('%11s ', DecOct(strlen($filename_ready))), 0); $dataLast = pack($packL, 'L', '', '', '', '', '', '', '', '', ''); // Calculate the checksum $checksum = 0; // First part of the header for ($i = 0; $i < 148; $i++) { $checksum += ord(substr($dataFirst, $i, 1)); } // Ignore the checksum value and replace it by ' ' (space) for ($i = 148; $i < 156; $i++) { $checksum += ord(' '); } // Last part of the header for ($i = 156, $j = 0; $i < 512; $i++, $j++) { $checksum += ord(substr($dataLast, $j, 1)); } // Write the first 148 bytes of the header in the archive $this->writeBlock($dataFirst, 148); // Write the calculated checksum $checksum = sprintf('%6s ', DecOct($checksum)); $binaryData = pack('a8', $checksum); $this->writeBlock($binaryData, 8); // Write the last 356 bytes of the header in the archive $this->writeBlock($dataLast, 356); $tmp_filename = $this->makeGoodPath($filename_ready); $i = 0; while (($buffer = substr($tmp_filename, $i++ * 512, 512)) != '') { $binaryData = pack('a512', $buffer); $this->writeBlock($binaryData); } return true; } $file_info = stat($filename); if (is_dir($filename)) { $typeflag = '5'; $size = sprintf('%11s ', DecOct(0)); } else { $typeflag = ''; clearstatcache(); $size = sprintf('%11s ', DecOct(filesize($filename))); } $dataFirst = pack($packF, $filename_ready, sprintf('%6s ', DecOct(fileperms($filename))), sprintf('%6s ', DecOct($file_info[4])), sprintf('%6s ', DecOct($file_info[5])), $size, sprintf('%11s', DecOct(filemtime($filename)))); $dataLast = pack($packL, $typeflag, '', '', '', '', '', '', '', '', ''); $checksum = 0; for ($i = 0; $i < 148; $i++) { $checksum += ord(substr($dataFirst, $i, 1)); } for ($i = 148; $i < 156; $i++) { $checksum += ord(' '); } for ($i = 156, $j = 0; $i < 512; $i++, $j++) { $checksum += ord(substr($dataLast, $j, 1)); } $this->writeBlock($dataFirst, 148); $checksum = sprintf('%6s ', DecOct($checksum)); $binaryData = pack('a8', $checksum); $this->writeBlock($binaryData, 8); $this->writeBlock($dataLast, 356); return true; }
private static function tar_head($type, $filename, $filesize = 0, $fileperms = 0744, $uid = 0, $gid = 0, $update_date = null) { if ($update_date === null) { $update_date = time(); } $checksum = 256; $first = pack("a100a8a8a8a12A12", $filename, sprintf("%8s", DecOct($fileperms)), sprintf("%8s", DecOct($uid)), sprintf("%8s", DecOct($gid)), sprintf("%12s", $type === 0 ? DecOct($filesize) : 0), sprintf("%12s", DecOct($update_date))); $last = pack("a1a100a6a2a32a32a8a8a155a12", $type, null, null, null, null, null, null, null, null, null); for ($i = 0; $i < strlen($first); $i++) { $checksum += ord($first[$i]); } for ($i = 0; $i < strlen($last); $i++) { $checksum += ord($last[$i]); } return $first . pack("a8", sprintf("%8s", DecOct($checksum))) . $last; }
function PclTarHandleHeader($p_tar, $p_filename, $p_mode, &$p_header, $p_stored_filename) { TrFctStart(__FILE__, __LINE__, "PclTarHandleHeader", "tar={$p_tar}, file='{$p_filename}', mode='{$p_mode}', stored_filename='{$p_stored_filename}'"); $v_result = 1; // ----- Check the parameters if ($p_tar == 0 || $p_filename == "") { // ----- Error log PclErrorLog(-3, "Invalid file descriptor in file " . __FILE__ . ", line " . __LINE__); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Filename (reduce the path of stored name) if ($p_stored_filename == "") { $p_stored_filename = $p_filename; } $v_reduce_filename = PclTarHandlePathReduction($p_stored_filename); //TrFctMessage(__FILE__, __LINE__, 2, "Filename (reduced) '$v_reduce_filename', strlen ".strlen($v_reduce_filename)); // ----- Get file info $v_info = stat($p_filename); $v_uid = sprintf("%6s ", DecOct($v_info[4])); $v_gid = sprintf("%6s ", DecOct($v_info[5])); //TrFctMessage(__FILE__, __LINE__, 3, "uid=$v_uid, gid=$v_gid"); $v_perms = sprintf("%6s ", DecOct(fileperms($p_filename))); //TrFctMessage(__FILE__, __LINE__, 3, "file permissions $v_perms"); // ----- File mtime $v_mtime_data = filemtime($p_filename); //TrFctMessage(__FILE__, __LINE__, 2, "File mtime : $v_mtime_data"); $v_mtime = sprintf("%11s", DecOct($v_mtime_data)); // ----- File typeflag // '0' or '\0' is the code for regular file // '5' is directory if (is_dir($p_filename)) { $v_typeflag = "5"; $v_size = 0; } else { $v_typeflag = ""; // ----- Get the file size clearstatcache(); $v_size = filesize($p_filename); } //TrFctMessage(__FILE__, __LINE__, 2, "File size : $v_size"); $v_size = sprintf("%11s ", DecOct($v_size)); //TrFctMessage(__FILE__, __LINE__, 2, "File typeflag : $v_typeflag"); // ----- Linkname $v_linkname = ""; // ----- Magic $v_magic = ""; // ----- Version $v_version = ""; // ----- uname $v_uname = ""; // ----- gname $v_gname = ""; // ----- devmajor $v_devmajor = ""; // ----- devminor $v_devminor = ""; // ----- prefix $v_prefix = ""; // ----- Compose the binary string of the header in two parts arround the checksum position $v_binary_data_first = pack("a100a8a8a8a12A12", $v_reduce_filename, $v_perms, $v_uid, $v_gid, $v_size, $v_mtime); $v_binary_data_last = pack("a1a100a6a2a32a32a8a8a155a12", $v_typeflag, $v_linkname, $v_magic, $v_version, $v_uname, $v_gname, $v_devmajor, $v_devminor, $v_prefix, ""); // ----- Calculate the checksum $v_checksum = 0; // ..... First part of the header for ($i = 0; $i < 148; $i++) { $v_checksum += ord(substr($v_binary_data_first, $i, 1)); } // ..... Ignore the checksum value and replace it by ' ' (space) for ($i = 148; $i < 156; $i++) { $v_checksum += ord(' '); } // ..... Last part of the header for ($i = 156, $j = 0; $i < 512; $i++, $j++) { $v_checksum += ord(substr($v_binary_data_last, $j, 1)); } //TrFctMessage(__FILE__, __LINE__, 3, "Calculated checksum : $v_checksum"); // ----- Write the first 148 bytes of the header in the archive if ($p_mode == "tar") { fputs($p_tar, $v_binary_data_first, 148); } else { gzputs($p_tar, $v_binary_data_first, 148); } // ----- Write the calculated checksum $v_checksum = sprintf("%6s ", DecOct($v_checksum)); $v_binary_data = pack("a8", $v_checksum); if ($p_mode == "tar") { fputs($p_tar, $v_binary_data, 8); } else { gzputs($p_tar, $v_binary_data, 8); } // ----- Write the last 356 bytes of the header in the archive if ($p_mode == "tar") { fputs($p_tar, $v_binary_data_last, 356); } else { gzputs($p_tar, $v_binary_data_last, 356); } // ----- Set the properties in the header "structure" $p_header[filename] = $v_reduce_filename; $p_header[mode] = $v_perms; $p_header[uid] = $v_uid; $p_header[gid] = $v_gid; $p_header[size] = $v_size; $p_header[mtime] = $v_mtime; $p_header[typeflag] = $v_typeflag; $p_header[status] = "added"; // ----- Return TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; }
function PclTarHandleReadHeader($v_binary_data, &$v_header) { TrFctStart(__FILE__, __LINE__, "PclTarHandleReadHeader", ""); $v_result = 1; if (strlen($v_binary_data) == 0) { $v_header[filename] = ""; $v_header[status] = "empty"; TrFctEnd(__FILE__, __LINE__, $v_result, "End of archive found"); return $v_result; } if (strlen($v_binary_data) != 512) { $v_header[filename] = ""; $v_header[status] = "invalid_header"; TrFctMessage(__FILE__, __LINE__, 2, "Invalid block size : " . strlen($v_binary_data)); PclErrorLog(-10, "Invalid block size : " . strlen($v_binary_data)); TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } $v_checksum = 0; for ($i = 0; $i < 148; $i++) { $v_checksum += ord(substr($v_binary_data, $i, 1)); } for ($i = 148; $i < 156; $i++) { $v_checksum += ord(' '); } for ($i = 156; $i < 512; $i++) { $v_checksum += ord(substr($v_binary_data, $i, 1)); } TrFctMessage(__FILE__, __LINE__, 3, "Calculated checksum : {$v_checksum}"); TrFctMessage(__FILE__, __LINE__, 2, "Header : '{$v_binary_data}'"); $v_data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $v_binary_data); $v_header[checksum] = OctDec(trim($v_data[checksum])); TrFctMessage(__FILE__, __LINE__, 3, "File checksum : {$v_header['checksum']}"); if ($v_header[checksum] != $v_checksum) { TrFctMessage(__FILE__, __LINE__, 2, "File checksum is invalid : {$v_checksum} calculated, {$v_header['checksum']} expected"); $v_header[filename] = ""; $v_header[status] = "invalid_header"; if ($v_checksum == 256 && $v_header[checksum] == 0) { $v_header[status] = "empty"; TrFctEnd(__FILE__, __LINE__, $v_result, "End of archive found"); return $v_result; } PclErrorLog(-13, "Invalid checksum : {$v_checksum} calculated, {$v_header['checksum']} expected"); TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } TrFctMessage(__FILE__, __LINE__, 2, "File checksum is valid ({$v_checksum})"); $v_header[filename] = trim($v_data[filename]); TrFctMessage(__FILE__, __LINE__, 2, "Name : '{$v_header['filename']}'"); $v_header[mode] = OctDec(trim($v_data[mode])); TrFctMessage(__FILE__, __LINE__, 2, "Mode : '" . DecOct($v_header[mode]) . "'"); $v_header[uid] = OctDec(trim($v_data[uid])); TrFctMessage(__FILE__, __LINE__, 2, "Uid : '{$v_header['uid']}'"); $v_header[gid] = OctDec(trim($v_data[gid])); TrFctMessage(__FILE__, __LINE__, 2, "Gid : '{$v_header['gid']}'"); $v_header[size] = OctDec(trim($v_data[size])); TrFctMessage(__FILE__, __LINE__, 2, "Size : '{$v_header['size']}'"); $v_header[mtime] = OctDec(trim($v_data[mtime])); TrFctMessage(__FILE__, __LINE__, 2, "Date : " . date("l dS of F Y h:i:s A", $v_header[mtime])); if (($v_header[typeflag] = $v_data[typeflag]) == "5") { $v_header[size] = 0; TrFctMessage(__FILE__, __LINE__, 2, "Size (folder) : '{$v_header['size']}'"); } TrFctMessage(__FILE__, __LINE__, 2, "File typeflag : {$v_header['typeflag']}"); $v_header[status] = "ok"; TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; }
function mkdir_ftp($dir, $mode) { global $phpFtp, $FTP_CONNECT; if (defined("FTP_ROOT")) { $script_path = substr($_SERVER["PHP_SELF"], 0, strrpos($_SERVER["PHP_SELF"], '/') + 1); dealFTPDir($script_path); $dir = FTP_ROOT . '/' . $script_path . $dir; $dir = remove_dots($dir); if (!$FTP_CONNECT) { ftpConnect(); } $phpFtp->chdir(substr($dir, 0, strrpos($dir, '/'))); $ret = $phpFtp->mkdir($dir); $mode = DecOct($mode); $phpFtp->chmod($mode, $dir); return $ret; } else { return mkdir($dir, $mode); } }