FileList::getSuffix()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Xoopstube;
4
5
/**
6
 * Module: XoopsTube
7
 *
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 *
12
 * PHP version 5
13
 *
14
 * @category        Module
15
 * @package         Xoopstube
16
 * @author          XOOPS Development Team
17
 * @copyright       2001-2016 XOOPS Project (https://xoops.org)
18
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
19
 * @link            https://xoops.org/
20
 * @since           1.0.6
21
 */
22
23
use XoopsModules\Xoopstube;
24
25
/**
26
 * Class FileList
27
 * @package XoopsModules\Xoopstube
28
 */
29
class FileList
30
{
31
    public $filelist = [];
32
    public $value;
33
    public $selected;
34
    public $path = 'uploads';
35
    public $size;
36
    public $emptySelect;
37
    public $type;
38
    public $prefix;
39
    public $suffix;
40
    public $noSelection;
41
42
    /**
43
     * fileList::construct()
44
     *
45
     * @param string $path
46
     * @param null   $value
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
47
     * @param string $selected
48
     * @param int    $size
49
     *
50
     * @internal param int $emptySelect
51
     * @internal param int $type
52
     * @internal param string $prefix
53
     * @internal param string $suffix
54
     */
55
    public function __construct($path = 'uploads', $value = null, $selected = '', $size = 1)
56
    {
57
        $this->value     = $value;
58
        $this->selected = $selected;
59
        $this->size      = (int)$size;
60
61
        $pathToCheck = XOOPS_ROOT_PATH . "/{$path}";
62
        if (!\is_dir($pathToCheck) && !\mkdir($pathToCheck, 0777) && !\is_dir($pathToCheck)) {
63
            /** @var \XoopsLogger $logger */
64
            $logger = \XoopsLogger::getInstance();
65
            $logger->handleError(\E_USER_WARNING, $pathToCheck . \_AM_XOOPSTUBE_DOESNOTEXIST, __FILE__, __LINE__);
66
67
            return false;
68
        }
69
        $this->path = $path;
70
71
        return true;
72
    }
73
74
    /**
75
     * SpotList::setNoSelection()
76
     *
77
     * @param int $value
78
     */
79
    public function setEmptySelect($value = 0)
80
    {
81
        $this->emptySelect = (1 !== (int)$value) ? 0 : 1;
82
    }
83
84
    /**
85
     * @param int $value
86
     */
87
    public function setNoSelection($value = 0)
88
    {
89
        $this->noSelection = (1 !== (int)$value) ? 0 : 1;
90
    }
91
92
    /**
93
     * @param string $value
94
     */
95
    public function setPrefix($value = '')
96
    {
97
        $this->prefix = '' !== ((string)$value) ? (string)$value : '';
98
    }
99
100
    /**
101
     * @param string $value
102
     */
103
    public function setSuffix($value = '')
104
    {
105
        $this->suffix = '' !== ((string)$value) ? (string)$value : '';
106
    }
107
108
    /**
109
     * @param string $value
110
     */
111
    public function setListType($value = 'images')
112
    {
113
        $this->type = mb_strtolower($value);
114
    }
115
116
    /**
117
     * SpotList::showSelection()
118
     *
119
     * @return string
120
     */
121
    public function &showSelection()
122
    {
123
        $ret = "<select size='" . $this->size() . "' name='$this->value()'>";
124
        if ($this->emptySelect) {
125
            $ret .= "<option value='" . $this->value() . "'>----------------------</option>";
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->value() targeting XoopsModules\Xoopstube\FileList::value() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
126
        }
127
        foreach ($this->filelist as $content) {
128
            $optSelected = '';
129
130
            if ($content[0] == $this->isSelected()) {
131
                $optSelected = "selected='selected'";
132
            }
133
            $ret .= "<option value='" . $content . "' $optSelected>" . $content . '</option>';
134
        }
135
        $ret .= '</select>';
136
137
        return $ret;
138
    }
139
140
    /**
141
     * SpotList::getListTypeAsArray()
142
     *
143
     * @return array
144
     */
145
    public function &getListTypeAsArray()
146
    {
147
        $filelist = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $filelist is dead and can be removed.
Loading history...
148
        switch (\trim($this->type)) {
149
            case 'images':
150
                $types = '[.gif|.jpg|.png]';
151
                if ($this->noSelection) {
152
                    $this->filelist[0] = \_AM_XOOPSTUBE_NOIMAGE;
153
                }
154
                break;
155
            case 'media':
156
                $types = '[.aac|.flv|.mp3|.mp4|.swf]';
157
                if ($this->noSelection) {
158
                    $this->filelist[0] = \_AM_XOOPSTUBE_NOVIDEO;
159
                }
160
                break;
161
            case 'html':
162
                $types = '[.htm|.tpl|.html|.xhtml|.php|.php3|.phtml|.txt]';
163
                if ($this->noSelection) {
164
                    $this->filelist[0] = \_AM_XOOPSTUBE_NOSELECT;
165
                }
166
                break;
167
            default:
168
                $types = '';
169
                if ($this->noSelection) {
170
                    $this->filelist[0] = \_AM_XOOPSTUBE_NOFILESELECT;
171
                }
172
                break;
173
        }
174
175
        if ('/' === mb_substr($this->path, -1)) {
176
            $this->path = mb_substr($this->path, 0, -1);
177
        }
178
179
        $_full_path = XOOPS_ROOT_PATH . "/{$this->path}";
180
181
        if (\is_dir($_full_path) && $handle = \opendir($_full_path)) {
182
            while (false !== ($file = \readdir($handle))) {
183
                if (!\preg_match('/^[.]{1,2}$/', $file) && \preg_match("/$types$/i", $file) && \is_file($_full_path . '/' . $file)) {
184
                    if ('blank.gif' === mb_strtolower($file)) {
185
                        continue;
186
                    }
187
                    $file                  = $this->prefix . $file;
188
                    $this->filelist[$file] = $file;
189
                }
190
            }
191
            \closedir($handle);
192
            \asort($this->filelist);
193
            \reset($this->filelist);
194
        }
195
196
        return $this->filelist;
197
    }
198
199
    /**
200
     * @return null
201
     */
202
    public function value()
203
    {
204
        return $this->value;
205
    }
206
207
    public function isSelected()
208
    {
209
        return $this->selected;
210
    }
211
212
    /**
213
     * @return string
214
     */
215
    public function paths()
216
    {
217
        return $this->path;
218
    }
219
220
    /**
221
     * @return int
222
     */
223
    public function size()
224
    {
225
        return $this->size;
226
    }
227
228
    public function isEmptySelect()
229
    {
230
        return $this->emptySelect;
231
    }
232
233
    public function getType()
234
    {
235
        return $this->type;
236
    }
237
238
    public function getPrefix()
239
    {
240
        return $this->prefix;
241
    }
242
243
    public function getSuffix()
244
    {
245
        return $this->suffix;
246
    }
247
}
248