Passed
Push — master ( 641a53...544fde )
by Giancarlos
02:24
created

ZipFile::file()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0023

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 11
cts 12
cp 0.9167
rs 9.4285
cc 2
eloc 13
nc 2
nop 0
crap 2.0023
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Giansalex
5
 * Date: 16/07/2017
6
 * Time: 13:00
7
 */
8
9
namespace Greenter\Helper;
10
11
/**
12
 * Class ZipFile
13
 * @package Greenter\Helper
14
 */
15
class ZipFile
16
{
17
    /**
18
     * Whether to echo zip as it's built or return as string from -> file
19
     *
20
     * @private  boolean  $doWrite
21
     */
22
    private $doWrite      = false;
23
    /**
24
     * Array to store compressed data
25
     *
26
     * @private  array    $datasec
27
     */
28
    private $datasec      = array();
29
    /**
30
     * Central directory
31
     *
32
     * @private  array    $ctrl_dir
33
     */
34
    private $ctrl_dir     = array();
35
    /**
36
     * End of central directory record
37
     *
38
     * @private  string   $eof_ctrl_dir
39
     */
40
    private $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
41
    /**
42
     * Last offset position
43
     *
44
     * @private  integer  $old_offset
45
     */
46
    private $old_offset   = 0;
47
    /**
48
     * Sets member privateiable this -> doWrite to true
49
     * - Should be called immediately after class instantiantion
50
     * - If set to true, then ZIP archive are echo'ed to STDOUT as each
51
     *   file is added via this -> addfile(), and central directories are
52
     *   echoed to STDOUT on final call to this -> file().  Also,
53
     *   this -> file() returns an empty string so it is safe to issue a
54
     *   "echo $zipfile;" command
55
     *
56
     * @access public
57
     *
58
     * @return void
59
     */
60
    public function setDoWrite()
61
    {
62
        $this -> doWrite = true;
63
    } // end of the 'setDoWrite()' method
64
    /**
65
     * Converts an Unix timestamp to a four byte DOS date and time format (date
66
     * in high two bytes, time in low two bytes allowing magnitude comparison).
67
     *
68
     * @param integer $unixtime the current Unix timestamp
69
     *
70
     * @return integer the current date in a four byte DOS format
71
     *
72
     * @access private
73
     */
74 4
    public function unix2DosTime($unixtime = 0)
75
    {
76 4
        $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
77 4
        if ($timearray['year'] < 1980) {
78
            $timearray['year']    = 1980;
79
            $timearray['mon']     = 1;
80
            $timearray['mday']    = 1;
81
            $timearray['hours']   = 0;
82
            $timearray['minutes'] = 0;
83
            $timearray['seconds'] = 0;
84
        } // end if
85 4
        return (($timearray['year'] - 1980) << 25)
86 4
            | ($timearray['mon'] << 21)
87 4
            | ($timearray['mday'] << 16)
88 4
            | ($timearray['hours'] << 11)
89 4
            | ($timearray['minutes'] << 5)
90 4
            | ($timearray['seconds'] >> 1);
91
    } // end of the 'unix2DosTime()' method
92
    /**
93
     * Adds "file" to archive
94
     *
95
     * @param string  $data file contents
96
     * @param string  $name name of the file in the archive (may contains the path)
97
     * @param integer $time the current timestamp
98
     *
99
     * @access public
100
     *
101
     * @return void
102
     */
103 4
    public function addFile($data, $name, $time = 0)
104
    {
105 4
        $name     = str_replace('\\', '/', $name);
106 4
        $hexdtime = pack('V', $this->unix2DosTime($time));
107 4
        $fr   = "\x50\x4b\x03\x04";
108 4
        $fr   .= "\x14\x00";            // ver needed to extract
109 4
        $fr   .= "\x00\x00";            // gen purpose bit flag
110 4
        $fr   .= "\x08\x00";            // compression method
111 4
        $fr   .= $hexdtime;             // last mod time and date
112
        // "local file header" segment
113 4
        $unc_len = strlen($data);
114 4
        $crc     = crc32($data);
115 4
        $zdata   = gzcompress($data);
116 4
        $zdata   = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
117 4
        $c_len   = strlen($zdata);
118 4
        $fr      .= pack('V', $crc);             // crc32
119 4
        $fr      .= pack('V', $c_len);           // compressed filesize
120 4
        $fr      .= pack('V', $unc_len);         // uncompressed filesize
121 4
        $fr      .= pack('v', strlen($name));    // length of filename
122 4
        $fr      .= pack('v', 0);                // extra field length
123 4
        $fr      .= $name;
124
        // "file data" segment
125 4
        $fr .= $zdata;
126
        // echo this entry on the fly, ...
127 4
        if ( !$this -> doWrite) {
128 4
            $this -> datasec[] = $fr;
129 4
        }
130
        // now add to central directory record
131 4
        $cdrec = "\x50\x4b\x01\x02";
132 4
        $cdrec .= "\x00\x00";                // version made by
133 4
        $cdrec .= "\x14\x00";                // version needed to extract
134 4
        $cdrec .= "\x00\x00";                // gen purpose bit flag
135 4
        $cdrec .= "\x08\x00";                // compression method
136 4
        $cdrec .= $hexdtime;                 // last mod time & date
137 4
        $cdrec .= pack('V', $crc);           // crc32
138 4
        $cdrec .= pack('V', $c_len);         // compressed filesize
139 4
        $cdrec .= pack('V', $unc_len);       // uncompressed filesize
140 4
        $cdrec .= pack('v', strlen($name)); // length of filename
141 4
        $cdrec .= pack('v', 0);             // extra field length
142 4
        $cdrec .= pack('v', 0);             // file comment length
143 4
        $cdrec .= pack('v', 0);             // disk number start
144 4
        $cdrec .= pack('v', 0);             // internal file attributes
145 4
        $cdrec .= pack('V', 32);            // external file attributes
146
        // - 'archive' bit set
147 4
        $cdrec .= pack('V', $this -> old_offset); // relative offset of local header
148 4
        $this -> old_offset += strlen($fr);
149 4
        $cdrec .= $name;
150
        // optional extra field, file comment goes here
151
        // save to central directory
152 4
        $this -> ctrl_dir[] = $cdrec;
153 4
    } // end of the 'addFile()' method
154
    /**
155
     * Echo central dir if ->doWrite==true, else build string to return
156
     *
157
     * @return string  if ->doWrite {empty string} else the ZIP file contents
158
     *
159
     * @access public
160
     */
161 4
    public function file()
162
    {
163 4
        $ctrldir = implode('', $this -> ctrl_dir);
164
        $header = $ctrldir .
165 4
            $this -> eof_ctrl_dir .
166 4
            pack('v', sizeof($this -> ctrl_dir)) . //total #of entries "on this disk"
167 4
            pack('v', sizeof($this -> ctrl_dir)) . //total #of entries overall
168 4
            pack('V', strlen($ctrldir)) .          //size of central dir
169 4
            pack('V', $this -> old_offset) .       //offset to start of central dir
170 4
            "\x00\x00";                            //.zip file comment length
171 4
        if ( $this -> doWrite ) { // Send central directory & end ctrl dir to STDOUT
172
            return "";            // Return empty string
173
        }
174
175
        // Return entire ZIP archive as string
176 4
        $data = implode('', $this -> datasec);
177 4
        return $data . $header;
178
    } // end of the 'file()' method
179
}
180