Version: .0

Type: Full Script

Category: Other

License: Other

Description: Dynamically create a WordPerfect merge data (.dat) file from MySql database. WordPerfect does NOT need to be installed on the machine with PHP/MYSQL. tested with WP 9 and PHP > 4.2.3



<?php
/*
build a word perfect merge data (.dat) file
*/

$fileName = 'merge_data_file.dat';

/* from php user notes for hexdec */
function hex2bin($hexdata) {  
  for ($i=0;$i<strlen($hexdata);$i+=2) {
     $bindata.=chr(hexdec(substr($hexdata,$i,2)));
  }
  return $bindata;
}

//WP BINARY SPACE
DEFINE('WP_SPACE',chr(128));
//fixed length defined in the wp documentation
DEFINE('HEADER_LENGTH',745);
//stuff like product version, minor version, file type...
DEFINE('BEGIN_HEADER',hex2bin('FF575043CE020000010A02010000000205000000'));
//THE FILE LENGTH IS 2 BYTES THAT WILL GO BETWEEN BEGIN_ AND END_ HEADER.
//mostly the header prefix: font style etc...
DEFINE('END_HEADER',hex2bin('0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000500000000000000000000000055010000004E0000004602000009250100000006000000940200000B3002000000280000009A020000085E010000000C000000C20200002800D61EC30f3908000011090000005A001B01008B143600540069006D006500730020004E0065007700200052006F006D0061006E00200052006500670075006C006100720000000000000000000100010058020100000004002800000000000000000000000000000000000000011202002400A1000000A100000050A54E250000000000000800DD0A10008301030003000200211000DDDD0B0B00030000040B00DD'));
/*WP FUNCTION CODES */
DEFINE('MERGE_FIELDNAMES_ON', hex2bin('DE4E0A000100000A00DE'));
DEFINE('MERGE_FIELDNAMES_OFF', hex2bin('DE4F0A000100000A00DE'));
DEFINE('MERGE_ENDFIELD', hex2bin('DE0A0A000000000A00DE'));
DEFINE('MERGE_ENDRECORD', hex2bin('DE0D0B00000000000B00DE'));
DEFINE('HARD_EOL', hex2bin('CC'));
DEFINE('HARD_EOP', hex2bin('D0091500000B00090001B0040000000001201500D0'));
/*
//THESE ARE EMBEDDED IN THE HEADER ABOVE
DEFINE('GLOBAL_ON', hex2bin('D00A10008301030003000200211000DD'));
DEFINE('GLOBAL_OFF', hex2bin('DD0B0B00030000040B000D'));
*/

/* END DEFINES; BEGIN MAIN */

$sql = 'SELECT * from table';
$r = mysql_query($sql);
$numFlds = mysql_num_fields($r);

//grab the fieldnames
for ($j = 0; $j < $numFlds; $j++) {
  //replace spaces with WP binary value
  $fldNms[] = str_replace(' ',WP_SPACE,trim(mysql_field_name($r, $j)));
}
$fieldNames = MERGE_FIELDNAMES_ON.implode(';',$fldNms).MERGE_FIELDNAMES_OFF.MERGE_ENDRECORD.HARD_EOP;

while($row = mysql_fetch_assoc($r)){
  $i=1;
  foreach($row as $value){

    //replace spaces with WP binary value
    $value = str_replace(' ',WP_SPACE,trim($value));
    
    if($i == $numFlds) {
      //last field in record, so we'll end the record and the page
      $delim = MERGE_ENDRECORD.HARD_EOP;
    }
    else {
      //just endfield and newline
      $delim = MERGE_ENDFIELD.HARD_EOL;
    }
    $records .= $value.$delim;
    $i++;
  }
}

$data = $fieldNames.$records;

//file length in bytes as a hex value
//to be inserted in the header
$fileLen = dechex(strlen($data)+HEADER_LENGTH);

//the $fileLen hex string is big endian?
$low = substr($fileLen,-2,2);
$high = str_replace($low,'',$fileLen);
if(strlen($high)==1) $high = '0'.$high;
$fileLen = $low.$high;

//insert the file length into the byte stream
$file = BEGIN_HEADER.hex2bin($fileLen).END_HEADER.$data;

header("Cache-Control: public");
header("Expires: 0");
header("Content-Disposition: attachment; filename=$fileName");
header("Content-Type: application/download-now");

echo $file; 


?>