Upload   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 161
rs 10
c 2
b 1
f 0
wmc 17
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setFilter() 0 4 1
B uploadFile() 0 25 5
A _controlConsistency() 0 18 4
A _moveFile() 0 21 3
A createFolder() 0 14 3
A _sanitizeName() 0 7 1
1
<?php
2
3
/**
4
 * File Uploads
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Files
10
 * @author    Daniel Schalla <[email protected]>
11
 * @copyright 2013 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\files;
17
18
/**
19
 * File Uploads
20
 *
21
 * @category  Core
22
 * @package   Files
23
 * @author    Daniel Schalla <[email protected]>
24
 * @copyright 2013 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
class Upload
30
{
31
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
32
     * @var String Defines after which ruleset the validation class controls
33
     */
34
35
    private $_filter = "";
36
37
    /**
38
     * Sets the filter for the validation class
39
     *
40
     * @param string $filter FilterSet (See Validate)
41
     *
42
     * @return void
43
     */
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
44
45
    public function setFilter($filter)
46
    {
47
        $this->_filter = $filter;
48
    }
49
50
    /**
51
     * Uploads a file into the storage folder
52
     *
53
     * @param array  $file       Array of the file which should be uploaded,
54
     *                           using the $_FILES Array
55
     * @param string $plugin     Plugin Name
56
     * @param string $customName Rename File to $name
57
     *                           Class
58
     *
59
     * @return array
60
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
61
62
    public function uploadFile($file, $plugin, $customName = "")
63
    {
64
65
        $validate = new Validate($file);
66
67
        if (!empty($this->_filter) && !$validate->check($this->_filter)) {
68
            return false;
69
        }
70
71
        //Determine if the File Array is consistent
72
        if (!$this->_controlConsistency($file)) {
73
            return false;
74
        }
75
76
        $customName = $this->_sanitizeName($customName);
77
78
        //Try to upload the file in our destination folder
79
        if ($this->_moveFile($file, $plugin, $customName)) {
80
            $return = true;
81
        } else {
82
            $return = false;
83
        }
84
85
        return $return;
86
    }
87
88
    /**
89
     * Controls if our $file Array consists all data we require
90
     *
91
     * @param array $file The child of the $_FILES
92
     *
93
     * @return boolean
94
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
95
96
    private function _controlConsistency($file)
97
    {
98
        $consistent = 1;
99
100
        if ($file['error'] != 0) {
101
            $consistent = 0;
102
        }
103
104
        if (empty($file['tmp_name'])) {
105
            $consistent = 0;
106
        }
107
108
        if ($file['size'] == 0) {
109
            $consistent = 0;
110
        }
111
112
        return (boolean)$consistent;
113
    }
114
115
    /**
116
     * Move the uploaded file
117
     *
118
     * @param array  $file       File Array
119
     * @param string $plugin     Plugin Name
120
     * @param string $customName Customname for the final file
121
     *
122
     * @return string
123
     */
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
124
125
    private function _moveFile($file, $plugin, $customName)
126
    {
127
128
        $path = "csphere/storage/uploads/" . $plugin . "/";
129
130
        $this->createFolder($path);
131
132
        if ($customName != '') {
133
            $filename = $customName;
134
        } else {
135
            $filename = $file['name'];
136
        }
137
138
        if (move_uploaded_file($file['tmp_name'], $path . $filename)) {
139
            $filePath = $path . $filename;
140
        } else {
141
            $filePath = "";
142
        }
143
144
        return $filePath;
145
    }
146
147
    /**
148
     * Create a destination Folder
149
     *
150
     * @param string $path Filepath of the Folder
151
     *
152
     * @throws \ErrorException
153
     *
154
     * @return boolean
155
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
156
157
    public function createFolder($path)
158
    {
159
160
        if (!is_dir($path)) {
161
            $res = mkdir($path);
162
            if (!$res) {
163
                throw new \ErrorException("Couldn't create folder: " . $path);
164
            }
165
        } else {
166
            $res = false;
167
        }
168
169
        return $res;
170
    }
171
172
173
    /**
174
     * Filters the name string
175
     * TODO Filter for invalid characters, only allow [1-9A-Za-z]
176
     *
177
     * @param String $name the name to filter
178
     *
179
     * @return String the string where spaces where replaces through -
180
     **/
181
    private function _sanitizeName($name)
182
    {
183
184
        $name = str_replace(" ", "-", $name);
185
186
        return $name;
187
    }
188
189
}
190