Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
form_storage
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 3
132
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
 upload
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 form_upload
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
90
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\files\types;
15
16use bantu\IniGetWrapper\IniGetWrapper;
17use phpbb\files\factory;
18use phpbb\files\filespec;
19use phpbb\language\language;
20use phpbb\plupload\plupload;
21use phpbb\request\request;
22
23class form_storage extends base
24{
25    /** @var factory Files factory */
26    protected $factory;
27
28    /** @var plupload */
29    protected $plupload;
30
31    /** @var request */
32    protected $request;
33
34    /**
35     * Construct a form upload type
36     *
37     * @param factory            $factory    Files factory
38     * @param language            $language    Language class
39     * @param IniGetWrapper        $php_ini    ini_get() wrapper
40     * @param plupload            $plupload    Plupload
41     * @param request            $request    Request object
42     */
43    public function __construct(factory $factory, language $language, IniGetWrapper $php_ini, plupload $plupload, request $request)
44    {
45        $this->factory = $factory;
46        $this->language = $language;
47        $this->php_ini = $php_ini;
48        $this->plupload = $plupload;
49        $this->request = $request;
50    }
51
52    /**
53     * {@inheritdoc}
54     */
55    public function upload()
56    {
57        $args = func_get_args();
58        return $this->form_upload($args[0]);
59    }
60
61    /**
62     * Form upload method
63     * Upload file from users harddisk
64     *
65     * @param string $form_name Form name assigned to the file input field (if it is an array, the key has to be specified)
66     *
67     * @return filespec $file Object "filespec" is returned, all further operations can be done with this object
68     */
69    protected function form_upload($form_name)
70    {
71        $upload = $this->request->file($form_name);
72        unset($upload['local_mode']);
73
74        $result = $this->plupload->handle_upload($form_name);
75        if (is_array($result))
76        {
77            $upload = array_merge($upload, $result);
78        }
79
80        /** @var filespec $file */
81        $file = $this->factory->get('filespec_storage')
82            ->set_upload_ary($upload)
83            ->set_upload_namespace($this->upload);
84
85        if ($file->init_error())
86        {
87            $file->error[] = '';
88            return $file;
89        }
90
91        // Error array filled?
92        if (isset($upload['error']))
93        {
94            $error = $this->upload->assign_internal_error($upload['error']);
95
96            if ($error !== false)
97            {
98                $file->error[] = $error;
99                return $file;
100            }
101        }
102
103        // Check if empty file got uploaded (not catched by is_uploaded_file)
104        if (isset($upload['size']) && $upload['size'] == 0)
105        {
106            $file->error[] = $this->language->lang($this->upload->error_prefix . 'EMPTY_FILEUPLOAD');
107            return $file;
108        }
109
110        // PHP Upload file size check
111        $file = $this->check_upload_size($file);
112        if (count($file->error))
113        {
114            return $file;
115        }
116
117        // Not correctly uploaded
118        if (!$file->is_uploaded())
119        {
120            $file->error[] = $this->language->lang($this->upload->error_prefix . 'NOT_UPLOADED');
121            return $file;
122        }
123
124        $this->upload->common_checks($file);
125
126        return $file;
127    }
128}