Completed
Push — master ( 2f8aec...b781ca )
by Richard
16s
created

File   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 78
dl 0
loc 188
rs 10
c 0
b 0
f 0
wmc 29

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilePath() 0 3 1
A __construct() 0 18 3
A displayFlash() 0 3 1
A datesub() 0 3 1
A getNameFromFilename() 0 7 1
B storeUpload() 0 34 8
A checkUpload() 0 22 4
A getFileLink() 0 3 1
A getItemLink() 0 3 1
A getFileUrl() 0 3 1
A notLoaded() 0 3 1
A store() 0 19 5
A updateCounter() 0 4 1
1
<?php
2
3
namespace XoopsModules\Publisher;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
use RuntimeException;
16
use Xoops\Core\Kernel\XoopsObject;
17
use XoopsLocale;
18
use XoopsMediaUploader;
19
use XoopsModules\Publisher;
20
21
/**
22
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
23
 * @license         GNU GPL V2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
24
 * @package         Publisher
25
 * @since           1.0
26
 * @author          trabis <[email protected]>
27
 * @author          The SmartFactory <www.smartfactory.ca>
28
 * @version         $Id$
29
 */
30
require_once \dirname(__DIR__) . '/include/common.php';
31
32
// File status
33
\define('_PUBLISHER_STATUS_FILE_NOTSET', -1);
34
\define('_PUBLISHER_STATUS_FILE_ACTIVE', 1);
35
\define('_PUBLISHER_STATUS_FILE_INACTIVE', 2);
36
37
/**
38
 * Class File
39
 * @package XoopsModules\Publisher
40
 */
41
class File extends XoopsObject
42
{
43
    /**
44
     * @var Helper
45
     * @access public
46
     */
47
    public $helper = null;
48
49
    /**
50
     * @param null|int $id
51
     */
52
    public function __construct($id = null)
53
    {
54
        $this->helper = Helper::getInstance();
55
        $this->initVar('fileid', \XOBJ_DTYPE_INT, 0, false);
56
        $this->initVar('itemid', \XOBJ_DTYPE_INT, null, true);
57
        $this->initVar('name', \XOBJ_DTYPE_TXTBOX, null, true, 255);
58
        $this->initVar('description', \XOBJ_DTYPE_TXTBOX, null, false, 255);
59
        $this->initVar('filename', \XOBJ_DTYPE_TXTBOX, null, true, 255);
60
        $this->initVar('mimetype', \XOBJ_DTYPE_TXTBOX, null, true, 64);
61
        $this->initVar('uid', \XOBJ_DTYPE_INT, 0, false);
62
        $this->initVar('datesub', \XOBJ_DTYPE_INT, null, false);
63
        $this->initVar('status', \XOBJ_DTYPE_INT, 1, false);
64
        $this->initVar('notifypub', \XOBJ_DTYPE_INT, 0, false);
65
        $this->initVar('counter', \XOBJ_DTYPE_INT, null, false);
66
        if (isset($id)) {
67
            $file = $this->helper->getFileHandler()->get($id);
68
            foreach ($file->vars as $k => $v) {
69
                $this->assignVar($k, $v['value']);
70
            }
71
        }
72
    }
73
74
    /**
75
     * @param string $post_field
76
     * @param array  $allowed_mimetypes
77
     * @param array  $errors
78
     *
79
     * @return bool
80
     */
81
    public function checkUpload($post_field, $allowed_mimetypes, &$errors): ?bool
82
    {
83
        $errors = [];
84
        if (!$this->helper->getMimetypeHandler()->checkMimeTypes($post_field)) {
0 ignored issues
show
Bug introduced by
The method checkMimeTypes() does not exist on XoopsModules\Publisher\MimetypeHandler. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        if (!$this->helper->getMimetypeHandler()->/** @scrutinizer ignore-call */ checkMimeTypes($post_field)) {
Loading history...
85
            $errors[] = _CO_PUBLISHER_MESSAGE_WRONG_MIMETYPE;
86
87
            return false;
88
        }
89
        if (empty($allowed_mimetypes)) {
90
            $allowed_mimetypes = $this->helper->getMimetypeHandler()->getArrayByType();
0 ignored issues
show
Bug introduced by
The method getArrayByType() does not exist on XoopsModules\Publisher\MimetypeHandler. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
            $allowed_mimetypes = $this->helper->getMimetypeHandler()->/** @scrutinizer ignore-call */ getArrayByType();
Loading history...
91
        }
92
        $maxfilesize = $this->helper->getConfig('maximum_filesize');
93
        $maxfilewidth = $this->helper->getConfig('maximum_image_width');
94
        $maxfileheight = $this->helper->getConfig('maximum_image_height');
95
        $uploader = new XoopsMediaUploader(Publisher\Utils::getUploadDir(), $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight);
96
        if ($uploader->fetchMedia($post_field)) {
97
            return true;
98
        }
99
100
        $errors = \array_merge($errors, $uploader->getErrors(false));
101
102
        return false;
103
    }
104
105
    /**
106
     * @param string $post_field
107
     * @param array  $allowed_mimetypes
108
     * @param array  $errors
109
     *
110
     * @return bool
111
     */
112
    public function storeUpload($post_field, $allowed_mimetypes, &$errors): ?bool
113
    {
114
        $itemid = $this->getVar('itemid');
115
        if (empty($allowed_mimetypes)) {
116
            $allowed_mimetypes = $this->helper->getMimetypeHandler()->getArrayByType();
117
        }
118
        $maxfilesize = $this->helper->getConfig('maximum_filesize');
119
        $maxfilewidth = $this->helper->getConfig('maximum_image_width');
120
        $maxfileheight = $this->helper->getConfig('maximum_image_height');
121
        if (!\is_dir(Publisher\Utils::getUploadDir())) {
122
            if (!\mkdir($concurrentDirectory = Publisher\Utils::getUploadDir(), 0757) && !\is_dir($concurrentDirectory)) {
123
                throw new RuntimeException(\sprintf('Directory "%s" was not created', $concurrentDirectory));
124
            }
125
        }
126
        $uploader = new XoopsMediaUploader(Publisher\Utils::getUploadDir() . '/', $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight);
127
        if ($uploader->fetchMedia($post_field)) {
128
            $uploader->setTargetFileName($itemid . '_' . $uploader->getMediaName());
129
            if ($uploader->upload()) {
130
                $this->setVar('filename', $uploader->getSavedFileName());
131
                if ('' == $this->getVar('name')) {
132
                    $this->setVar('name', $this->getNameFromFilename());
133
                }
134
                $this->setVar('mimetype', $uploader->getMediaType());
135
136
                return true;
137
            }
138
139
            $errors = \array_merge($errors, $uploader->getErrors(false));
140
141
            return false;
142
        }
143
        $errors = \array_merge($errors, $uploader->getErrors(false));
144
145
        return false;
146
    }
147
148
    /**
149
     * @param null|array $allowed_mimetypes
150
     * @param bool       $force
151
     * @param bool       $doupload
152
     */
153
    public function store($allowed_mimetypes = null, $force = true, $doupload = true): bool
154
    {
155
        if ($this->isNew()) {
156
            $errors = [];
157
            if ($doupload) {
158
                $ret = $this->storeUpload('item_upload_file', $allowed_mimetypes, $errors);
159
            } else {
160
                $ret = true;
161
            }
162
            if (!$ret) {
163
                foreach ($errors as $error) {
164
                    $this->setErrors($error);
165
                }
166
167
                return false;
168
            }
169
        }
170
171
        return $this->helper->getFileHandler()->insert($this, $force);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->helper->ge...->insert($this, $force) could return the type integer which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
172
    }
173
174
    /**
175
     * @param string $dateFormat
176
     * @param string $format
177
     */
178
    public function datesub($dateFormat = 's', $format = 'S'): string
179
    {
180
        return XoopsLocale::formatTimestamp($this->getVar('datesub', $format), $dateFormat);
181
    }
182
183
    public function notLoaded(): bool
184
    {
185
        return (0 == $this->getVar('itemid'));
186
    }
187
188
    public function getFileUrl(): string
189
    {
190
        return Publisher\Utils::getUploadDir(false) . $this->getVar('filename');
191
    }
192
193
    public function getFilePath(): string
194
    {
195
        return Publisher\Utils::getUploadDir() . $this->getVar('filename');
196
    }
197
198
    public function getFileLink(): string
199
    {
200
        return "<a href='" . \PUBLISHER_URL . '/visit.php?fileid=' . $this->getVar('fileid') . "'>" . $this->getVar('name') . '</a>';
201
    }
202
203
    public function getItemLink(): string
204
    {
205
        return "<a href='" . \PUBLISHER_URL . '/item.php?itemid=' . $this->getVar('itemid') . "'>" . $this->getVar('name') . '</a>';
206
    }
207
208
    /**
209
     * Update Counter
210
     */
211
    public function updateCounter(): void
212
    {
213
        $this->setVar('counter', $this->getVar('counter') + 1);
214
        $this->store();
215
    }
216
217
    public function displayFlash(): string
218
    {
219
        return Publisher\Utils::displayFlash($this->getFileUrl());
220
    }
221
222
    public function getNameFromFilename(): string
223
    {
224
        $ret = $this->getVar('filename');
225
        $sep_pos = \mb_strpos($ret, '_');
0 ignored issues
show
Bug introduced by
It seems like $ret can also be of type string[]; however, parameter $haystack of mb_strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

225
        $sep_pos = \mb_strpos(/** @scrutinizer ignore-type */ $ret, '_');
Loading history...
226
        $ret = \mb_substr($ret, $sep_pos + 1, \mb_strlen($ret) - $sep_pos);
0 ignored issues
show
Bug introduced by
It seems like $ret can also be of type string[]; however, parameter $str of mb_substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

226
        $ret = \mb_substr(/** @scrutinizer ignore-type */ $ret, $sep_pos + 1, \mb_strlen($ret) - $sep_pos);
Loading history...
Bug introduced by
It seems like $ret can also be of type string[]; however, parameter $str of mb_strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

226
        $ret = \mb_substr($ret, $sep_pos + 1, \mb_strlen(/** @scrutinizer ignore-type */ $ret) - $sep_pos);
Loading history...
227
228
        return $ret;
229
    }
230
}
231