Completed
Branch BUG/fatal-with-paypal-debug-li... (3a6198)
by
unknown
09:03 queued 40s
created

EEH_Sideloader::set_download_from()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * EEH_Sideloader
5
 *
6
 * This is a helper utility class that provides "sideloading" functionality.  Sideloading simply refers to retrieving files hosted elsehwere (usually github) that are downloaded into EE.
7
 *
8
 * @package     Event Espresso
9
 * @subpackage  /helpers/EEH_Sideloader.helper.php
10
 * @author      Darren Ethier
11
 *
12
 * ------------------------------------------------------------------------
13
 */
14
class EEH_Sideloader extends EEH_Base
15
{
16
17
    /**
18
     * @since   4.1.0
19
     * @var     string
20
     */
21
    private $_upload_to;
22
23
    /**
24
     * @since   4.10.5.p
25
     * @var     string
26
     */
27
    private $_download_from;
28
29
    /**
30
     * @since   4.1.0
31
     * @var     string
32
     */
33
    private $_permissions;
34
35
    /**
36
     * @since   4.1.0
37
     * @var     string
38
     */
39
    private $_new_file_name;
40
41
42
    /**
43
     * constructor allows the user to set the properties on the sideloader on construct.  However, there are also setters for doing so.
44
     *
45
     * @since 4.1.0
46
     * @param array $init array fo initializing the sideloader if keys match the properties.
47
     */
48
    public function __construct($init = array())
49
    {
50
        $this->_init($init);
51
    }
52
53
54
    /**
55
     * sets the properties for class either to defaults or using incoming initialization array
56
     *
57
     * @since 4.1.0
58
     * @param  array  $init array on init (keys match properties others ignored)
59
     * @return void
60
     */
61
    private function _init($init)
62
    {
63
        $defaults = array(
64
            '_upload_to' => $this->_get_wp_uploads_dir(),
65
            '_download_from' => '',
66
            '_permissions' => 0644,
67
            '_new_file_name' => 'EE_Sideloader_' . uniqid() . '.default'
68
            );
69
70
        $props = array_merge($defaults, $init);
71
72
        foreach ($props as $property => $val) {
73
            $setter = 'set' . $property;
74 View Code Duplication
            if (method_exists($this, $setter)) {
75
                $this->$setter($val);
76
            } else {
77
                 // No setter found.
78
                EE_Error::add_error(
79
                    sprintf(
80
                        esc_html__(
81
                            'EEH_Sideloader::%1$s not found. There is no setter for the %2$s property.',
82
                            'event_espresso'
83
                        ),
84
                        $setter,
85
                        $property
86
                    ),
87
                    __FILE__,
88
                    __FUNCTION__,
89
                    __LINE__
90
                );
91
            }
92
        }
93
94
        // make sure we include the required wp file for needed functions
95
        require_once(ABSPATH . 'wp-admin/includes/file.php');
96
    }
97
98
99
    // utilities
100
101
102
    /**
103
     * @since 4.1.0
104
     * @return void
105
     */
106
    private function _get_wp_uploads_dir()
107
    {
108
    }
109
110
    // setters
111
112
113
    /**
114
     * sets the _upload_to property to the directory to upload to.
115
     *
116
     * @since 4.1.0
117
     * @param $upload_to_folder
118
     * @return void
119
     */
120
    public function set_upload_to($upload_to_folder)
121
    {
122
        $this->_upload_to = $upload_to_folder;
123
    }
124
125
126
    /**
127
     * sets the _download_from property to the location we should download the file from.
128
     *
129
     * @since 4.10.5.p
130
     * @param string $download_from The full path to the file we should sideload.
131
     * @return void
132
     */
133
    public function set_download_from($download_from)
134
    {
135
        $this->_download_from = $download_from;
136
    }
137
138
139
    /**
140
     * sets the _permissions property used on the sideloaded file.
141
     *
142
     * @since 4.1.0
143
     * @param int $permissions
144
     * @return void
145
     */
146
    public function set_permissions($permissions)
147
    {
148
        $this->_permissions = $permissions;
149
    }
150
151
152
    /**
153
     * sets the _new_file_name property used on the sideloaded file.
154
     *
155
     * @since 4.1.0
156
     * @param string $new_file_name
157
     * @return void
158
     */
159
    public function set_new_file_name($new_file_name)
160
    {
161
        $this->_new_file_name = $new_file_name;
162
    }
163
164
    // getters
165
166
167
    /**
168
     * @since 4.1.0
169
     * @return string
170
     */
171
    public function get_upload_to()
172
    {
173
        return $this->_upload_to;
174
    }
175
176
177
    /**
178
     * @since 4.10.5.p
179
     * @return string
180
     */
181
    public function get_download_from()
182
    {
183
        return $this->_download_from;
184
    }
185
186
187
    /**
188
     * @since 4.1.0
189
     * @return int
190
     */
191
    public function get_permissions()
192
    {
193
        return $this->_permissions;
194
    }
195
196
197
    /**
198
     * @since 4.1.0
199
     * @return string
200
     */
201
    public function get_new_file_name()
202
    {
203
        return $this->_new_file_name;
204
    }
205
206
207
    // upload methods
208
209
210
    /**
211
     * Downloads the file using the WordPress HTTP API.
212
     *
213
     * @since 4.1.0
214
     * @return bool
215
     */
216
    public function sideload()
217
    {
218
        // setup temp dir
219
        $temp_file = wp_tempnam($this->_download_from);
220
221
        if (!$temp_file) {
222
            EE_Error::add_error(
223
                esc_html__('Something went wrong with the upload.  Unable to create a tmp file for the uploaded file on the server', 'event_espresso'),
224
                __FILE__,
225
                __FUNCTION__,
226
                __LINE__
227
            );
228
            return false;
229
        }
230
231
        do_action('AHEE__EEH_Sideloader__sideload__before', $this, $temp_file);
232
233
        $wp_remote_args = apply_filters('FHEE__EEH_Sideloader__sideload__wp_remote_args', array( 'timeout' => 500, 'stream' => true, 'filename' => $temp_file ), $this, $temp_file);
234
235
        $response = wp_safe_remote_get($this->_download_from, $wp_remote_args);
236
237
        if (is_wp_error($response) || 200 != wp_remote_retrieve_response_code($response)) {
238
            unlink($temp_file);
239
            if (defined('WP_DEBUG') && WP_DEBUG) {
240
                EE_Error::add_error(
241
                    sprintf(
242
                        esc_html__('Unable to upload the file. Either the path given to download from is incorrect, or something else happened. Here is the path given: %s', 'event_espresso'),
243
                        $this->_download_from
244
                    ),
245
                    __FILE__,
246
                    __FUNCTION__,
247
                    __LINE__
248
                );
249
            }
250
            return false;
251
        }
252
253
        // possible md5 check
254
        $content_md5 = wp_remote_retrieve_header($response, 'content-md5');
255
        if ($content_md5) {
256
            $md5_check = verify_file_md5($temp_file, $content_md5);
257
            if (is_wp_error($md5_check)) {
258
                unlink($temp_file);
259
                EE_Error::add_error(
260
                    $md5_check->get_error_message(),
261
                    __FILE__,
262
                    __FUNCTION__,
263
                    __LINE__
264
                );
265
                return false;
266
            }
267
        }
268
269
        $file = $temp_file;
270
271
        // now we have the file, let's get it in the right directory with the right name.
272
        $path = apply_filters('FHEE__EEH_Sideloader__sideload__new_path', $this->_upload_to . $this->_new_file_name, $this);
273
274
        // move file in
275
        if (false === @ rename($file, $path)) {
276
            unlink($temp_file);
277
            EE_Error::add_error(
278
                sprintf(
279
                    esc_html__('Unable to move the file to new location (possible permissions errors). This is the path the class attempted to move the file to: %s', 'event_espresso'),
280
                    $path
281
                ),
282
                __FILE__,
283
                __FUNCTION__,
284
                __LINE__
285
            );
286
            return false;
287
        }
288
289
        // set permissions
290
        $permissions = apply_filters('FHEE__EEH_Sideloader__sideload__permissions_applied', $this->_permissions, $this);
291
        chmod($path, $permissions);
292
293
        // that's it.  let's allow for actions after file uploaded.
294
        do_action('AHEE__EE_Sideloader__sideload_after', $this, $path);
295
296
        // unlink tempfile
297
        @unlink($temp_file);
298
        return true;
299
    }
300
301
    // deprecated
302
303
    /**
304
     * sets the _upload_from property to the location we should download the file from.
305
     *
306
     * @param string $upload_from The full path to the file we should sideload.
307
     * @return void
308
     * @deprecated since version 4.10.5.p
309
     */
310
    public function set_upload_from($upload_from)
311
    {
312
        EE_Error::doing_it_wrong(
313
            __CLASS__ . '::' . __FUNCTION__,
314
            __(
315
                'EEH_Sideloader::set_upload_from was renamed to EEH_Sideloader::set_download_from',
316
                'event_espresso'
317
            ),
318
            '4.10.5.p'
319
        );
320
        $this->set_download_from($upload_from);
321
    }
322
323
324
    /**
325
     * @since 4.1.0
326
     * @return string
327
     * @deprecated since version 4.10.5.p
328
     */
329
    public function get_upload_from()
330
    {
331
        EE_Error::doing_it_wrong(
332
            __CLASS__ . '::' . __FUNCTION__,
333
            __(
334
                'EEH_Sideloader::get_upload_from was renamed to EEH_Sideloader::get_download_from',
335
                'event_espresso'
336
            ),
337
            '4.10.5.p'
338
        );
339
        return $this->_download_from;
340
    }
341
} //end EEH_Sideloader class
342