Write BIFF record Window2.

@access private
327344
function _storeWindow2()
    {
        $record         = 0x023E;     // Record identifier
        if ($this->_BIFF_version == 0x0500) {
            $length         = 0x000A;     // Number of bytes to follow
        } elseif ($this->_BIFF_version == 0x0600) {
            $length         = 0x0012;
        }

        $grbit          = 0x00B6;     // Option flags
        $rwTop          = 0x0000;     // Top row visible in window
        $colLeft        = 0x0000;     // Leftmost column visible in window


        // The options flags that comprise $grbit
        $fDspFmla       = 0;                     // 0 - bit
        $fDspGrid       = $this->_screen_gridlines; // 1
        $fDspRwCol      = 1;                     // 2
        $fFrozen        = $this->_frozen;        // 3
        $fDspZeros      = 1;                     // 4
        $fDefaultHdr    = 1;                     // 5
        $fArabic        = 0;                     // 6
        $fDspGuts       = $this->_outline_on;    // 7
        $fFrozenNoSplit = 0;                     // 0 - bit
        $fSelected      = $this->selected;       // 1
        $fPaged         = 1;                     // 2

        $grbit             = $fDspFmla;
        $grbit            |= $fDspGrid       << 1;
        $grbit            |= $fDspRwCol      << 2;
        $grbit            |= $fFrozen        << 3;
        $grbit            |= $fDspZeros      << 4;
        $grbit            |= $fDefaultHdr    << 5;
        $grbit            |= $fArabic        << 6;
        $grbit            |= $fDspGuts       << 7;
        $grbit            |= $fFrozenNoSplit << 8;
        $grbit            |= $fSelected      << 9;
        $grbit            |= $fPaged         << 10;

        $header  = pack("vv",   $record, $length);
        $data    = pack("vvv", $grbit, $rwTop, $colLeft);
        // FIXME !!!
        if ($this->_BIFF_version == 0x0500) {
            $rgbHdr         = 0x00000000; // Row/column heading and gridline color
            $data .= pack("V", $rgbHdr);
        } elseif ($this->_BIFF_version == 0x0600) {
            $rgbHdr       = 0x0040; // Row/column heading and gridline color index
            $zoom_factor_page_break = 0x0000;
            $zoom_factor_normal     = 0x0000;
            $data .= pack("vvvvV", $rgbHdr, 0x0000, $zoom_factor_page_break, $zoom_factor_normal, 0x00000000);
        }
        $this->_append($header.$data);
    }
1305186
function write_sty_bin(wb, opts) {
	var ba = buf_array();
	write_record(ba, "BrtBeginStyleSheet");
	write_FMTS_bin(ba, wb.SSF);
	write_FONTS_bin(ba, wb);
	write_FILLS_bin(ba, wb);
	write_BORDERS_bin(ba, wb);
	write_CELLSTYLEXFS_bin(ba, wb);
	write_CELLXFS_bin(ba, opts.cellXfs);
	write_STYLES_bin(ba, wb);
	write_DXFS_bin(ba, wb);
	write_TABLESTYLES_bin(ba, wb);
	write_COLORPALETTE_bin(ba, wb);
	/* FRTSTYLESHEET*/
	write_record(ba, "BrtEndStyleSheet");
	return ba.end();
}
87071
private function _writeScenProtect()
	{
		// Exit if sheet protection is not active
		if (!$this->_phpSheet->getProtection()->getSheet()) {
			return;
		}

		// Exit if scenarios are not protected
		if (!$this->_phpSheet->getProtection()->getScenarios()) {
			return;
		}

		$record = 0x00DD; // Record identifier
		$length = 0x0002; // Bytes to follow

		$header = pack('vv', $record, $length);
		$data = pack('v', 1);

		$this->_append($header . $data);
	}
743478
function Write($h, $txt, $currentx = 0, $link = '', $directionality = 'ltr', $align = '', $fill = 0)
	{
		if (empty($this->directWrite)) {
			$this->directWrite = new DirectWrite($this, $this->otl, $this->sizeConverter, $this->colorConverter);
		}

		$this->directWrite->Write($h, $txt, $currentx, $link, $directionality, $align, $fill);
	}
148985
function write_biff8_buf(wb, opts) {
	var o = opts || {};
	var bufs = [];

	if(wb && !wb.SSF) {
		wb.SSF = SSF.get_table();
	}
	if(wb && wb.SSF) {
		make_ssf(SSF); SSF.load_table(wb.SSF);
		// $FlowIgnore
		o.revssf = evert_num(wb.SSF); o.revssf[wb.SSF[65535]] = 0;
		o.ssf = wb.SSF;
	}
	o.cellXfs = [];
	o.Strings = []; o.Strings.Count = 0; o.Strings.Unique = 0;
	get_cell_style(o.cellXfs, {}, {revssf:{"General":0}});

	for(var i = 0; i < wb.SheetNames.length; ++i) bufs[bufs.length] = write_ws_biff8(i, o, wb);
	bufs.unshift(write_biff8_global(wb, bufs, o));
	return __toBuffer([bufs]);
}
1217326
private function _writeSheetProtection()
	{
		// record identifier
		$record = 0x0867;

		// prepare options
		$options  =   (int) !$this->_phpSheet->getProtection()->getObjects()
					| (int) !$this->_phpSheet->getProtection()->getScenarios()           << 1
					| (int) !$this->_phpSheet->getProtection()->getFormatCells()         << 2
					| (int) !$this->_phpSheet->getProtection()->getFormatColumns()       << 3
					| (int) !$this->_phpSheet->getProtection()->getFormatRows()          << 4
					| (int) !$this->_phpSheet->getProtection()->getInsertColumns()       << 5
					| (int) !$this->_phpSheet->getProtection()->getInsertRows()          << 6
					| (int) !$this->_phpSheet->getProtection()->getInsertHyperlinks()    << 7
					| (int) !$this->_phpSheet->getProtection()->getDeleteColumns()       << 8
					| (int) !$this->_phpSheet->getProtection()->getDeleteRows()          << 9
					| (int) !$this->_phpSheet->getProtection()->getSelectLockedCells()   << 10
					| (int) !$this->_phpSheet->getProtection()->getSort()                << 11
					| (int) !$this->_phpSheet->getProtection()->getAutoFilter()          << 12
					| (int) !$this->_phpSheet->getProtection()->getPivotTables()         << 13
					| (int) !$this->_phpSheet->getProtection()->getSelectUnlockedCells() << 14 ;

		// record data
		$recordData = pack(
			'vVVCVVvv'
			, 0x0867		// repeated record identifier
			, 0x0000		// not used
			, 0x0000		// not used
			, 0x00			// not used
			, 0x01000200	// unknown data
			, 0xFFFFFFFF	// unknown data
			, $options		// options
			, 0x0000		// not used
		);

		$length = strlen($recordData);
		$header = pack('vv', $record, $length);

		$this->_append($header . $recordData);
	}
1304701
function _write_header($fd)
	{
		$buf = pack('a3CiiIfa12', XDB_TAGNAME, $this->version,
			$this->hash_base, $this->hash_prime, 0, XDB_FLOAT_CHECK, '');

		fseek($fd, 0, SEEK_SET);
		fwrite($fd, $buf, 32);
	}
1299
private function _writePrintGridlines()
	{
		$record	  = 0x002b;					// Record identifier
		$length	  = 0x0002;					// Bytes to follow

		$fPrintGrid  = $this->_phpSheet->getPrintGridlines() ? 1 : 0;	// Boolean flag

		$header	  = pack("vv", $record, $length);
		$data	  = pack("v", $fPrintGrid);
		$this->_append($header . $data);
	}
696142
function write_biff8_global(wb, bufs, opts) {
	var A = buf_array();
	var _WB = ((wb||{}).Workbook||{});
	var _sheets = (_WB.Sheets||[]);
	var _wb = _WB.WBProps||{};
	var b8 = opts.biff == 8, b5 = opts.biff == 5;
	write_biff_rec(A, 0x0809, write_BOF(wb, 0x05, opts));
	if(opts.bookType == "xla") write_biff_rec(A, "Addin");
	write_biff_rec(A, "InterfaceHdr", b8 ? writeuint16(0x04b0) : null);
	write_biff_rec(A, "Mms", writezeroes(2));
	if(b5) write_biff_rec(A, "ToolbarHdr");
	if(b5) write_biff_rec(A, "ToolbarEnd");
	write_biff_rec(A, "InterfaceEnd");
	write_biff_rec(A, "WriteAccess", write_WriteAccess("SheetJS", opts));
	write_biff_rec(A, "CodePage", writeuint16(b8 ? 0x04b0 : 0x04E4));
	if(b8) write_biff_rec(A, "DSF", writeuint16(0));
	if(b8) write_biff_rec(A, "Excel9File");
	write_biff_rec(A, "RRTabId", write_RRTabId(wb.SheetNames.length));
	if(b8 && wb.vbaraw) {
		write_biff_rec(A, "ObProj");
		var cname = _wb.CodeName || "ThisWorkbook";
		write_biff_rec(A, "CodeName", write_XLUnicodeString(cname, opts));
	}
	write_biff_rec(A, "BuiltInFnGroupCount", writeuint16(0x11));
	write_biff_rec(A, "WinProtect", writebool(false));
	write_biff_rec(A, "Protect", writebool(false));
	write_biff_rec(A, "Password", writeuint16(0));
	if(b8) write_biff_rec(A, "Prot4Rev", writebool(false));
	if(b8) write_biff_rec(A, "Prot4RevPass", writeuint16(0));
	write_biff_rec(A, "Window1", write_Window1(opts));
	write_biff_rec(A, "Backup", writebool(false));
	write_biff_rec(A, "HideObj", writeuint16(0));
	write_biff_rec(A, "Date1904", writebool(safe1904(wb)=="true"));
	write_biff_rec(A, "CalcPrecision", writebool(true));
	if(b8) write_biff_rec(A, "RefreshAll", writebool(false));
	write_biff_rec(A, "BookBool", writeuint16(0));
	/* ... */
	write_FONTS_biff8(A, wb, opts);
	write_FMTS_biff8(A, wb.SSF, opts);
	write_CELLXFS_biff8(A, opts);
	/* ... */
	if(b8) write_biff_rec(A, "UsesELFs", writebool(false));
	var a = A.end();

	var C = buf_array();
	if(b8) write_biff_rec(C, "Country", write_Country());
	/* BIFF8: [SST *Continue] ExtSST */
	write_biff_rec(C, "EOF");
	var c = C.end();

	var B = buf_array();
	var blen = 0, j = 0;
	for(j = 0; j < wb.SheetNames.length; ++j) blen += (b8 ? 12 : 11) + (b8 ? 2 : 1) * wb.SheetNames[j].length;
	var start = a.length + blen + c.length;
	for(j = 0; j < wb.SheetNames.length; ++j) {
		var _sheet = _sheets[j] || ({});
		write_biff_rec(B, "BoundSheet8", write_BoundSheet8({pos:start, hs:_sheet.Hidden||0, dt:0, name:wb.SheetNames[j]}, opts));
		start += bufs[j].length;
	}
	/* 1*BoundSheet8 */
	var b = B.end();
	if(blen != b.length) throw new Error("BS8 " + blen + " != " + b.length);

	var out = [];
	if(a.length) out.push(a);
	if(b.length) out.push(b);
	if(c.length) out.push(c);
	return __toBuffer([out]);
}
307447
private function _saveRecords()
    {
        $offset = 50;
        if (is_array($this->records) && (count($this->records) > 0)) {
            foreach ($this->records as $index => $record) {
                //Save the record to the .shp file
                $record->saveToFile($this->SHPFile, $this->DBFFile, $index + 1);

                //Save the record to the .shx file
                fwrite($this->SHXFile, pack('N', $offset));
                fwrite($this->SHXFile, pack('N', $record->getContentLength()));
                $offset += (4 + $record->getContentLength());
            }
        }
    }
247254
private function _writeMarginTop()
	{
		$record  = 0x0028;				   // Record identifier
		$length  = 0x0008;				   // Bytes to follow

		$margin  = $this->_phpSheet->getPageMargins()->getTop();	 // Margin in inches

		$header	= pack("vv",  $record, $length);
		$data	  = pack("d",   $margin);
		if (self::getByteOrder()) { // if it's Big Endian
			$data = strrev($data);
		}

		$this->_append($header . $data);
	}
721080
public function onAfterWrite()
    {
        $changed = $this->owner->getChangedFields(true, 2);

        if (!$this->owner instanceof Folder && !$this->owner->CurrentVersionID) {
            $this->createVersion();
        }

        if (array_key_exists('Filename', $changed)) {
            if ($changed['Filename']['before'] == null) {
                return;
            }

            $oldDirname = '/' . trim(dirname($changed['Filename']['before']), '/');
            $newDirname = '/' . trim(dirname($changed['Filename']['after']), '/');

            if ($oldDirname == $newDirname) {
                return;
            }

            // First move the _versions directory across.
            $versionsDir = FileVersion::VERSION_FOLDER . '/' . $this->owner->ID;
            $oldVersions = BASE_PATH . $oldDirname . '/' . $versionsDir;
            $newVersions = BASE_PATH . $newDirname . '/' . $versionsDir;

            if (is_dir($oldVersions)) {
                if (!is_dir($newVersions)) {
                    mkdir($newVersions, Config::inst()->get('Filesystem', 'folder_create_mask'), true);
                }

                if (!is_dir($newVersions)) {
                    rename($oldVersions, $newVersions);
                }
            }

            // Then update individual version records to point to the new
            // location.
            foreach ($this->owner->Versions() as $version) {
                if (strpos($version->Filename, $oldDirname) === 0) {
                    $version->Filename = $newDirname . substr($version->Filename, strlen($oldDirname));
                    $version->write();
                }
            }
        }
    }
836988
private function _writeHeader()
	{
		$record  = 0x0014;			   // Record identifier

		/* removing for now
		// need to fix character count (multibyte!)
		if (strlen($this->_phpSheet->getHeaderFooter()->getOddHeader()) <= 255) {
			$str	  = $this->_phpSheet->getHeaderFooter()->getOddHeader();	   // header string
		} else {
			$str = '';
		}
		*/

		$recordData = PHPExcel_Shared_String::UTF8toBIFF8UnicodeLong($this->_phpSheet->getHeaderFooter()->getOddHeader());
		$length = strlen($recordData);

		$header   = pack("vv", $record, $length);

		$this->_append($header . $recordData);
	}
678208
function _storeOLEFile()
    {
        if($this->_BIFF_version == 0x0600) {
            $OLE = new OLE_PPS_File(OLE::Asc2Ucs('Workbook'));
        } else {
            $OLE = new OLE_PPS_File(OLE::Asc2Ucs('Book'));
        }
        if ($this->_tmp_dir != '') {
            $OLE->setTempDir($this->_tmp_dir);
        }
        $res = $OLE->init();
        if ($this->isError($res)) {
            return $this->raiseError("OLE Error: ".$res->getMessage());
        }
        $OLE->append($this->_data);

        $total_worksheets = count($this->_worksheets);
        for ($i = 0; $i < $total_worksheets; $i++) {
            while ($tmp = $this->_worksheets[$i]->getData()) {
                $OLE->append($tmp);
            }
        }

        $root = new OLE_PPS_Root(time(), time(), array($OLE));
        if ($this->_tmp_dir != '') {
            $root->setTempDir($this->_tmp_dir);
        }

        $res = $root->save($this->_filename);
        if ($this->isError($res)) {
            return $this->raiseError("OLE Error: ".$res->getMessage());
        }
        return true;
    }
104656
function _storeBof($type)
    {
        $record  = 0x0809;        // Record identifier

        // According to the SDK $build and $year should be set to zero.
        // However, this throws a warning in Excel 5. So, use magic numbers.
        if ($this->_BIFF_version == 0x0500) {
            $length  = 0x0008;
            $unknown = '';
            $build   = 0x096C;
            $year    = 0x07C9;
        } elseif ($this->_BIFF_version == 0x0600) {
            $length  = 0x0010;
            $unknown = pack("VV", 0x00000041, 0x00000006); //unknown last 8 bytes for BIFF8
            $build   = 0x0DBB;
            $year    = 0x07CC;
        }
        $version = $this->_BIFF_version;

        $header  = pack("vv",   $record, $length);
        $data    = pack("vvvv", $version, $type, $build, $year);
        $this->_prepend($header . $data . $unknown);
    }
36770
def write_record(self, event_str):
        """"""
        header = struct.pack('Q', len(event_str))
        header += struct.pack('I', masked_crc32c(header))
        footer = struct.pack('I', masked_crc32c(event_str))
        self._writer.write(header + event_str + footer)
11397
function _storeFooter()
    {
        $record  = 0x0015;               // Record identifier

        $str      = $this->_footer;       // Footer string
        $cch      = strlen($str);         // Length of footer string
        if ($this->_BIFF_version == 0x0600) {
            $encoding = 0x0;                  // TODO: Unicode support
            $length   = 3 + $cch;             // Bytes to follow
        } else {
            $length  = 1 + $cch;
        }

        $header    = pack("vv", $record, $length);
        if ($this->_BIFF_version == 0x0600) {
            $data      = pack("vC",  $cch, $encoding);
        } else {
            $data      = pack("C",  $cch);
        }

        $this->_prepend($header . $data . $str);
    }