Passed
Branch master (7e303a)
by Michael
02:15
created

File::getItemLink()   A

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\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
/**
16
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
17
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
18
 * @package         Publisher
19
 * @since           1.0
20
 * @author          trabis <[email protected]>
21
 * @author          The SmartFactory <www.smartfactory.ca>
22
 */
23
24
use XoopsModules\Publisher;
25
26
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
27
28
require_once dirname(__DIR__) . '/include/common.php';
29
30
// File status
31
//define("_PUBLISHER_STATUS_FILE_NOTSET", -1);
32
//define("_PUBLISHER_STATUS_FILE_ACTIVE", 1);
33
//define("_PUBLISHER_STATUS_FILE_INACTIVE", 2);
34
35
/**
36
 * Class File
37
 */
38
class File extends \XoopsObject
39
{
40
    /**
41
     * @var Helper
42
     */
43
    public $helper;
44
45
    /**
46
     * @param null|int $id
47
     */
48
    public function __construct($id = null)
49
    {
50
        /** @var Publisher\Helper $this->helper */
51
        $this->helper = Publisher\Helper::getInstance();
52
        $this->db     = \XoopsDatabaseFactory::getDatabaseConnection();
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
53
        $this->initVar('fileid', XOBJ_DTYPE_INT, 0, false);
54
        $this->initVar('itemid', XOBJ_DTYPE_INT, null, true);
55
        $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 255);
56
        $this->initVar('description', XOBJ_DTYPE_TXTBOX, null, false, 255);
57
        $this->initVar('filename', XOBJ_DTYPE_TXTBOX, null, true, 255);
58
        $this->initVar('mimetype', XOBJ_DTYPE_TXTBOX, null, true, 64);
59
        $this->initVar('uid', XOBJ_DTYPE_INT, 0, false);
60
        $this->initVar('datesub', XOBJ_DTYPE_INT, null, false);
61
        $this->initVar('status', XOBJ_DTYPE_INT, 1, false);
62
        $this->initVar('notifypub', XOBJ_DTYPE_INT, 0, false);
63
        $this->initVar('counter', XOBJ_DTYPE_INT, null, false);
64
        if (null !== $id) {
65
            $file = $this->helper->getHandler('File')->get($id);
66
            foreach ($file->vars as $k => $v) {
67
                $this->assignVar($k, $v['value']);
68
            }
69
        }
70
    }
71
72
    /**
73
     * @param string $method
74
     * @param array  $args
75
     *
76
     * @return mixed
77
     */
78
    public function __call($method, $args)
79
    {
80
        $arg = isset($args[0]) ? $args[0] : null;
81
82
        return $this->getVar($method, $arg);
83
    }
84
85
    /**
86
     * @param string $postField
87
     * @param array  $allowedMimetypes
88
     * @param array  $errors
89
     *
90
     * @return bool
91
     */
92
    public function checkUpload($postField, $allowedMimetypes, &$errors)
93
    {
94
        $errors = [];
95
        if (!$this->helper->getHandler('Mimetype')->checkMimeTypes($postField)) {
0 ignored issues
show
Bug introduced by
The method checkMimeTypes() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

95
        if (!$this->helper->getHandler('Mimetype')->/** @scrutinizer ignore-call */ checkMimeTypes($postField)) {
Loading history...
96
            $errors[] = _CO_PUBLISHER_MESSAGE_WRONG_MIMETYPE;
97
98
            return false;
99
        }
100
        if (0 === count($allowedMimetypes)) {
101
            $allowedMimetypes = $this->helper->getHandler('Mimetype')->getArrayByType();
0 ignored issues
show
Bug introduced by
The method getArrayByType() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

101
            $allowedMimetypes = $this->helper->getHandler('Mimetype')->/** @scrutinizer ignore-call */ getArrayByType();
Loading history...
102
        }
103
        $maxfilesize   = $this->helper->getConfig('maximum_filesize');
104
        $maxfilewidth  = $this->helper->getConfig('maximum_image_width');
105
        $maxfileheight = $this->helper->getConfig('maximum_image_height');
106
        xoops_load('XoopsMediaUploader');
107
        $uploader = new \XoopsMediaUploader(Publisher\Utility::getUploadDir(), $allowedMimetypes, $maxfilesize, $maxfilewidth, $maxfileheight);
108
        if ($uploader->fetchMedia($postField)) {
109
            return true;
110
        }
111
        $errors = array_merge($errors, $uploader->getErrors(false));
0 ignored issues
show
Bug introduced by
It seems like $uploader->getErrors(false) can also be of type string; however, parameter $array2 of array_merge() does only seem to accept null|array, 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

111
        $errors = array_merge($errors, /** @scrutinizer ignore-type */ $uploader->getErrors(false));
Loading history...
112
113
        return false;
114
    }
115
116
    /**
117
     * @param string $postField
118
     * @param array  $allowedMimetypes
119
     * @param array  $errors
120
     *
121
     * @return bool
122
     */
123
    public function storeUpload($postField, $allowedMimetypes, &$errors)
124
    {
125
        $itemid = $this->getVar('itemid');
126
        if (0 === count($allowedMimetypes)) {
127
            $allowedMimetypes = $this->helper->getHandler('Mimetype')->getArrayByType();
128
        }
129
        $maxfilesize   = $this->helper->getConfig('maximum_filesize');
130
        $maxfilewidth  = $this->helper->getConfig('maximum_image_width');
131
        $maxfileheight = $this->helper->getConfig('maximum_image_height');
132
        if (!is_dir(Publisher\Utility::getUploadDir())) {
133
            if (!mkdir($concurrentDirectory = Publisher\Utility::getUploadDir(), 0757) && !is_dir($concurrentDirectory)) {
134
                throw new \RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
135
            }
136
        }
137
        xoops_load('XoopsMediaUploader');
138
        $uploader = new \XoopsMediaUploader(Publisher\Utility::getUploadDir() . '/', $allowedMimetypes, $maxfilesize, $maxfilewidth, $maxfileheight);
139
        if ($uploader->fetchMedia($postField)) {
140
            $uploader->setTargetFileName($itemid . '_' . $uploader->getMediaName());
141
            if ($uploader->upload()) {
142
                $this->setVar('filename', $uploader->getSavedFileName());
143
                if ('' == $this->getVar('name')) {
144
                    $this->setVar('name', $this->getNameFromFilename());
145
                }
146
                $this->setVar('mimetype', $uploader->getMediaType());
147
148
                return true;
149
            }
150
            $errors = array_merge($errors, $uploader->getErrors(false));
0 ignored issues
show
Bug introduced by
It seems like $uploader->getErrors(false) can also be of type string; however, parameter $array2 of array_merge() does only seem to accept null|array, 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

150
            $errors = array_merge($errors, /** @scrutinizer ignore-type */ $uploader->getErrors(false));
Loading history...
151
152
            return false;
153
        }
154
        $errors = array_merge($errors, $uploader->getErrors(false));
155
156
        return false;
157
    }
158
159
    /**
160
     * @param null|array $allowedMimetypes
161
     * @param bool       $force
162
     * @param bool       $doupload
163
     *
164
     * @return bool
165
     */
166
    public function store($allowedMimetypes = null, $force = true, $doupload = true)
167
    {
168
        if ($this->isNew()) {
169
            $errors = [];
170
            $ret    = true;
171
            if ($doupload) {
172
                $ret = $this->storeUpload('item_upload_file', $allowedMimetypes, $errors);
173
            }
174
            if (!$ret) {
175
                foreach ($errors as $error) {
176
                    $this->setErrors($error);
177
                }
178
179
                return false;
180
            }
181
        }
182
183
        return $this->helper->getHandler('File')->insert($this, $force);
0 ignored issues
show
Unused Code introduced by
The call to XoopsObjectHandler::insert() has too many arguments starting with $force. ( Ignorable by Annotation )

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

183
        return $this->helper->getHandler('File')->/** @scrutinizer ignore-call */ insert($this, $force);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
184
    }
185
186
    /**
187
     * @param string $dateFormat
188
     * @param string $format
189
     *
190
     * @return string
191
     */
192
    public function getDatesub($dateFormat = 's', $format = 'S')
193
    {
194
        //mb        xoops_load('XoopsLocal');
195
        //mb        return XoopsLocal::formatTimestamp($this->getVar('datesub', $format), $dateFormat);
196
        return formatTimestamp($this->getVar('datesub', $format), $dateFormat);
197
    }
198
199
    /**
200
     * @return bool
201
     */
202
    public function notLoaded()
203
    {
204
        return (0 == $this->getVar('itemid'));
205
    }
206
207
    /**
208
     * @return string
209
     */
210
    public function getFileUrl()
211
    {
212
        return Publisher\Utility::getUploadDir(false) . $this->filename();
0 ignored issues
show
Bug introduced by
The method filename() does not exist on XoopsModules\Publisher\File. 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

212
        return Publisher\Utility::getUploadDir(false) . $this->/** @scrutinizer ignore-call */ filename();
Loading history...
213
    }
214
215
    /**
216
     * @return string
217
     */
218
    public function getFilePath()
219
    {
220
        return Publisher\Utility::getUploadDir() . $this->filename();
221
    }
222
223
    /**
224
     * @return string
225
     */
226
    public function getFileLink()
227
    {
228
        return "<a href='" . PUBLISHER_URL . '/visit.php?fileid=' . $this->fileid() . "'>" . $this->name() . '</a>';
0 ignored issues
show
Bug introduced by
The method fileid() does not exist on XoopsModules\Publisher\File. 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

228
        return "<a href='" . PUBLISHER_URL . '/visit.php?fileid=' . $this->/** @scrutinizer ignore-call */ fileid() . "'>" . $this->name() . '</a>';
Loading history...
Bug introduced by
The method name() does not exist on XoopsModules\Publisher\File. 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

228
        return "<a href='" . PUBLISHER_URL . '/visit.php?fileid=' . $this->fileid() . "'>" . $this->/** @scrutinizer ignore-call */ name() . '</a>';
Loading history...
Bug introduced by
The constant XoopsModules\Publisher\PUBLISHER_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
229
    }
230
231
    /**
232
     * @return string
233
     */
234
    public function getItemLink()
235
    {
236
        return "<a href='" . PUBLISHER_URL . '/item.php?itemid=' . $this->itemid() . "'>" . $this->name() . '</a>';
0 ignored issues
show
Bug introduced by
The method itemid() does not exist on XoopsModules\Publisher\File. 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

236
        return "<a href='" . PUBLISHER_URL . '/item.php?itemid=' . $this->/** @scrutinizer ignore-call */ itemid() . "'>" . $this->name() . '</a>';
Loading history...
Bug introduced by
The constant XoopsModules\Publisher\PUBLISHER_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
237
    }
238
239
    /**
240
     * Update Counter
241
     */
242
    public function updateCounter()
243
    {
244
        $this->setVar('counter', $this->counter() + 1);
0 ignored issues
show
Bug introduced by
The method counter() does not exist on XoopsModules\Publisher\File. 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

244
        $this->setVar('counter', $this->/** @scrutinizer ignore-call */ counter() + 1);
Loading history...
245
        $this->store();
246
    }
247
248
    /**
249
     * @return string
250
     */
251
    public function displayFlash()
252
    {
253
        if (!defined('MYTEXTSANITIZER_EXTENDED_MEDIA')) {
254
            require_once PUBLISHER_ROOT_PATH . '/include/media.textsanitizer.php';
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Publisher\PUBLISHER_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
255
        }
256
        $mediaTs = MyTextSanitizerExtension::getInstance();
0 ignored issues
show
Bug introduced by
The type XoopsModules\Publisher\MyTextSanitizerExtension was not found. Did you mean MyTextSanitizerExtension? If so, make sure to prefix the type with \.
Loading history...
257
258
        return $mediaTs->displayFlash($this->getFileUrl());
259
    }
260
261
    /**
262
     * @return string
263
     */
264
    public function getNameFromFilename()
265
    {
266
        $ret    = $this->filename();
267
        $sepPos = mb_strpos($ret, '_');
0 ignored issues
show
Bug introduced by
It seems like $ret can also be of type array and array; 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

267
        $sepPos = mb_strpos(/** @scrutinizer ignore-type */ $ret, '_');
Loading history...
268
        $ret    = mb_substr($ret, $sepPos + 1);
0 ignored issues
show
Bug introduced by
It seems like $ret can also be of type array and array; 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

268
        $ret    = mb_substr(/** @scrutinizer ignore-type */ $ret, $sepPos + 1);
Loading history...
269
270
        return $ret;
271
    }
272
273
    /**
274
     * @return \XoopsModules\Publisher\Form\FileForm
275
     */
276
    public function getForm()
277
    {
278
        //        require_once $GLOBALS['xoops']->path('modules/' . PUBLISHER_DIRNAME . '/class/form/file.php');
279
        $form = new Publisher\Form\FileForm($this);
280
281
        return $form;
282
    }
283
}
284