Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 76
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
base_extractor
0.00% covered (danger)
0.00%
0 / 76
0.00% covered (danger)
0.00%
0 / 4
930
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 init_extractor
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
156
 write_end
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
42
 flush
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
132
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\db\extractor;
15
16use phpbb\db\extractor\exception\invalid_format_exception;
17use phpbb\db\extractor\exception\extractor_not_initialized_exception;
18
19/**
20 * Abstract base class for database extraction
21 */
22abstract class base_extractor implements extractor_interface
23{
24    /**
25     * @var    \phpbb\filesystem\temp
26     */
27    protected $temp;
28
29    /**
30     * @var    \phpbb\request\request_interface
31     */
32    protected $request;
33
34    /**
35     * @var    \phpbb\db\driver\driver_interface
36     */
37    protected $db;
38
39    /**
40     * @var    bool
41     */
42    protected $download;
43
44    /**
45     * @var    bool
46     */
47    protected $store;
48
49    /**
50     * @var    int
51     */
52    protected $time;
53
54    /**
55     * @var    string
56     */
57    protected $format;
58
59    /**
60     * @var    resource
61     */
62    protected $fp;
63
64    /**
65     * @var string
66     */
67    protected $write;
68
69    /**
70     * @var string
71     */
72    protected $close;
73
74    /**
75     * @var bool
76     */
77    protected $run_comp;
78
79    /**
80     * @var bool
81     */
82    protected $is_initialized;
83
84    /**
85     * Constructor
86     *
87     * @param \phpbb\filesystem\temp $temp
88     * @param \phpbb\request\request_interface $request
89     * @param \phpbb\db\driver\driver_interface $db
90     */
91    public function __construct(\phpbb\filesystem\temp $temp, \phpbb\request\request_interface $request, \phpbb\db\driver\driver_interface $db)
92    {
93        $this->temp             = $temp;
94        $this->request            = $request;
95        $this->db                = $db;
96        $this->fp                = null;
97
98        $this->is_initialized   = false;
99    }
100
101    /**
102    * {@inheritdoc}
103    */
104    public function init_extractor($format, $filename, $time, $download = false, $store = false)
105    {
106        $this->download            = $download;
107        $this->store            = $store;
108        $this->time                = $time;
109        $this->format            = $format;
110
111        switch ($format)
112        {
113            case 'text':
114                $ext = '.sql';
115                $open = 'fopen';
116                $this->write = 'fwrite';
117                $this->close = 'fclose';
118                $mimetype = 'text/x-sql';
119            break;
120            case 'bzip2':
121                $ext = '.sql.bz2';
122                $open = 'bzopen';
123                $this->write = 'bzwrite';
124                $this->close = 'bzclose';
125                $mimetype = 'application/x-bzip2';
126            break;
127            case 'gzip':
128                $ext = '.sql.gz';
129                $open = 'gzopen';
130                $this->write = 'gzwrite';
131                $this->close = 'gzclose';
132                $mimetype = 'application/x-gzip';
133            break;
134            default:
135                throw new invalid_format_exception();
136            break;
137        }
138
139        if ($download === true)
140        {
141            $name = $filename . $ext;
142            header('Cache-Control: private, no-cache');
143            header("Content-Type: $mimetype; name=\"$name\"");
144            header("Content-disposition: attachment; filename=$name");
145
146            switch ($format)
147            {
148                case 'bzip2':
149                    ob_start();
150                break;
151
152                case 'gzip':
153                    if (strpos($this->request->header('Accept-Encoding'), 'gzip') !== false && strpos(strtolower($this->request->header('User-Agent')), 'msie') === false)
154                    {
155                        ob_start('ob_gzhandler');
156                    }
157                    else
158                    {
159                        $this->run_comp = true;
160                    }
161                break;
162            }
163        }
164
165        if ($store === true)
166        {
167            $file = $this->temp->get_dir() . '/' . $filename . $ext;
168
169            $this->fp = $open($file, 'w');
170
171            if (!$this->fp)
172            {
173                throw new \phpbb\exception\runtime_exception('FILE_WRITE_FAIL');
174            }
175        }
176
177        $this->is_initialized = true;
178    }
179
180    /**
181    * {@inheritdoc}
182    */
183    public function write_end()
184    {
185        static $close;
186
187        if (!$this->is_initialized)
188        {
189            throw new extractor_not_initialized_exception();
190        }
191
192        if ($this->store)
193        {
194            if ($close === null)
195            {
196                $close = $this->close;
197            }
198            $close($this->fp);
199        }
200
201        // bzip2 must be written all the way at the end
202        if ($this->download && $this->format === 'bzip2')
203        {
204            $c = ob_get_clean();
205            echo bzcompress($c);
206        }
207    }
208
209    /**
210    * {@inheritdoc}
211    */
212    public function flush($data)
213    {
214        static $write;
215
216        if (!$this->is_initialized)
217        {
218            throw new extractor_not_initialized_exception();
219        }
220
221        if ($this->store === true)
222        {
223            if ($write === null)
224            {
225                $write = $this->write;
226            }
227            $write($this->fp, $data);
228        }
229
230        if ($this->download === true)
231        {
232            if ($this->format === 'bzip2' || $this->format === 'text' || ($this->format === 'gzip' && !$this->run_comp))
233            {
234                echo $data;
235            }
236
237            // we can write the gzip data as soon as we get it
238            if ($this->format === 'gzip')
239            {
240                if ($this->run_comp)
241                {
242                    echo gzencode($data);
243                }
244                else
245                {
246                    ob_flush();
247                    flush();
248                }
249            }
250        }
251    }
252}