Fix for the Open in New Window in Patient/Client->Patients search gui, take 2.
[openemr.git] / phpmyadmin / libraries / zip.lib.php
blob2d377cc0818f49fd5fbf176dcdec1bcb80b81f88
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 */
8 /**
9 * Zip file creation class.
10 * Makes zip files.
12 * Based on :
14 * http://www.zend.com/codex.php?id=535&single=1
15 * By Eric Mueller <eric@themepark.com>
17 * http://www.zend.com/codex.php?id=470&single=1
18 * by Denis125 <webmaster@atlant.ru>
20 * a patch from Peter Listiak <mlady@users.sourceforge.net> for last modified
21 * date and time of the compressed file
23 * Official ZIP file format: http://www.pkware.com/appnote.txt
25 * @access public
27 class zipfile
29 /**
30 * Array to store compressed data
32 * @var array $datasec
34 var $datasec = array();
36 /**
37 * Central directory
39 * @var array $ctrl_dir
41 var $ctrl_dir = array();
43 /**
44 * End of central directory record
46 * @var string $eof_ctrl_dir
48 var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
50 /**
51 * Last offset position
53 * @var integer $old_offset
55 var $old_offset = 0;
58 /**
59 * Converts an Unix timestamp to a four byte DOS date and time format (date
60 * in high two bytes, time in low two bytes allowing magnitude comparison).
62 * @param integer the current Unix timestamp
64 * @return integer the current date in a four byte DOS format
66 * @access private
68 function unix2DosTime($unixtime = 0) {
69 $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
71 if ($timearray['year'] < 1980) {
72 $timearray['year'] = 1980;
73 $timearray['mon'] = 1;
74 $timearray['mday'] = 1;
75 $timearray['hours'] = 0;
76 $timearray['minutes'] = 0;
77 $timearray['seconds'] = 0;
78 } // end if
80 return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
81 ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
82 } // end of the 'unix2DosTime()' method
85 /**
86 * Adds "file" to archive
88 * @param string file contents
89 * @param string name of the file in the archive (may contains the path)
90 * @param integer the current timestamp
92 * @access public
94 function addFile($data, $name, $time = 0)
96 $name = str_replace('\\', '/', $name);
98 $dtime = dechex($this->unix2DosTime($time));
99 $hexdtime = '\x' . $dtime[6] . $dtime[7]
100 . '\x' . $dtime[4] . $dtime[5]
101 . '\x' . $dtime[2] . $dtime[3]
102 . '\x' . $dtime[0] . $dtime[1];
103 eval('$hexdtime = "' . $hexdtime . '";');
105 $fr = "\x50\x4b\x03\x04";
106 $fr .= "\x14\x00"; // ver needed to extract
107 $fr .= "\x00\x00"; // gen purpose bit flag
108 $fr .= "\x08\x00"; // compression method
109 $fr .= $hexdtime; // last mod time and date
111 // "local file header" segment
112 $unc_len = strlen($data);
113 $crc = crc32($data);
114 $zdata = gzcompress($data);
115 $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
116 $c_len = strlen($zdata);
117 $fr .= pack('V', $crc); // crc32
118 $fr .= pack('V', $c_len); // compressed filesize
119 $fr .= pack('V', $unc_len); // uncompressed filesize
120 $fr .= pack('v', strlen($name)); // length of filename
121 $fr .= pack('v', 0); // extra field length
122 $fr .= $name;
124 // "file data" segment
125 $fr .= $zdata;
127 // "data descriptor" segment (optional but necessary if archive is not
128 // served as file)
129 // nijel(2004-10-19): this seems not to be needed at all and causes
130 // problems in some cases (bug #1037737)
131 //$fr .= pack('V', $crc); // crc32
132 //$fr .= pack('V', $c_len); // compressed filesize
133 //$fr .= pack('V', $unc_len); // uncompressed filesize
135 // add this entry to array
136 $this -> datasec[] = $fr;
138 // now add to central directory record
139 $cdrec = "\x50\x4b\x01\x02";
140 $cdrec .= "\x00\x00"; // version made by
141 $cdrec .= "\x14\x00"; // version needed to extract
142 $cdrec .= "\x00\x00"; // gen purpose bit flag
143 $cdrec .= "\x08\x00"; // compression method
144 $cdrec .= $hexdtime; // last mod time & date
145 $cdrec .= pack('V', $crc); // crc32
146 $cdrec .= pack('V', $c_len); // compressed filesize
147 $cdrec .= pack('V', $unc_len); // uncompressed filesize
148 $cdrec .= pack('v', strlen($name)); // length of filename
149 $cdrec .= pack('v', 0); // extra field length
150 $cdrec .= pack('v', 0); // file comment length
151 $cdrec .= pack('v', 0); // disk number start
152 $cdrec .= pack('v', 0); // internal file attributes
153 $cdrec .= pack('V', 32); // external file attributes - 'archive' bit set
155 $cdrec .= pack('V', $this -> old_offset); // relative offset of local header
156 $this -> old_offset += strlen($fr);
158 $cdrec .= $name;
160 // optional extra field, file comment goes here
161 // save to central directory
162 $this -> ctrl_dir[] = $cdrec;
163 } // end of the 'addFile()' method
167 * Dumps out file
169 * @return string the zipped file
171 * @access public
173 function file()
175 $data = implode('', $this -> datasec);
176 $ctrldir = implode('', $this -> ctrl_dir);
178 return
179 $data .
180 $ctrldir .
181 $this -> eof_ctrl_dir .
182 pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"
183 pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall
184 pack('V', strlen($ctrldir)) . // size of central dir
185 pack('V', strlen($data)) . // offset to start of central dir
186 "\x00\x00"; // .zip file comment length
187 } // end of the 'file()' method
189 } // end of the 'zipfile' class