Passed
Pull Request — master (#3)
by Michael
05:20
created

imagemanager.php (1 issue)

Labels
Severity
1
<?php
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright      {@link https://xoops.org/ XOOPS Project}
14
 * @license        {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package
16
 * @since
17
 * @author         XOOPS Development Team
18
 */
19
20
use XoopsModules\Pedigree;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Pedigree. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
21
22
23
require_once dirname(dirname(__DIR__)) . '/mainfile.php';
24
if (!isset($_GET['target']) && !isset($_POST['target'])) {
25
    exit();
26
}
27
$op = 'list';
28
if (isset($_GET['op']) && 'upload' === $_GET['op']) {
29
    $op = 'upload';
30
}
31
if (isset($_POST)) {
32
    foreach ($_POST as $k => $v) {
33
        ${$k} = $v;
34
    }
35
}
36
if (!$xoopsUser) {
37
    $group = [XOOPS_GROUP_ANONYMOUS];
38
} else {
39
    $group = $xoopsUser->getGroups();
40
}
41
if ('list' === $op) {
42
    require_once XOOPS_ROOT_PATH . '/class/template.php';
43
    $xoopsTpl = new \XoopsTpl();
44
    $xoopsTpl->assign('lang_imgmanager', _IMGMANAGER);
45
    $xoopsTpl->assign('sitename', $xoopsConfig['sitename']);
46
    $target = htmlspecialchars($_GET['target'], ENT_QUOTES);
47
    $xoopsTpl->assign('target', $target);
48
    $imgcatHandler = xoops_getHandler('imagecategory');
49
    $catlist       = $imgcatHandler->getList($group, 'imgcat_read', 1);
50
    $catcount      = count($catlist);
51
    $xoopsTpl->assign('lang_align', _ALIGN);
52
    $xoopsTpl->assign('lang_add', _ADD);
53
    $xoopsTpl->assign('lang_close', _CLOSE);
54
    if ($catcount > 0) {
55
        $xoopsTpl->assign('lang_go', _GO);
56
        $catshow = \Xmf\Request::getInt('cat_id', 0, 'GET');
57
        $catshow = (!empty($catshow) && array_key_exists($catshow, $catlist)) ? $catshow : 0;
58
        $xoopsTpl->assign('show_cat', $catshow);
59
        if ($catshow > 0) {
60
            $xoopsTpl->assign('lang_addimage', _ADDIMAGE);
61
        }
62
        $catlist     = ['0' => '--'] + $catlist;
63
        $cat_options = '';
64
        foreach ($catlist as $c_id => $c_name) {
65
            $sel = '';
66
            if ($c_id == $catshow) {
67
                $sel = ' selected';
68
            }
69
            $cat_options .= '<option value="' . $c_id . '"' . $sel . '>' . $c_name . '</option>';
70
        }
71
        $xoopsTpl->assign('cat_options', $cat_options);
72
        if ($catshow > 0) {
73
            $imageHandler = xoops_getHandler('image');
74
            $criteria     = new \CriteriaCompo(new \Criteria('imgcat_id', $catshow));
75
            $criteria->add(new \Criteria('image_display', 1));
76
            $total = $imageHandler->getCount($criteria);
77
            if ($total > 0) {
78
                $imgcatHandler = xoops_getHandler('imagecategory');
79
                $imgcat        = $imgcatHandler->get($catshow);
80
                $xoopsTpl->assign('image_total', $total);
81
                $xoopsTpl->assign('lang_image', _IMAGE);
82
                $xoopsTpl->assign('lang_imagename', _IMAGENAME);
83
                $xoopsTpl->assign('lang_imagemime', _IMAGEMIME);
84
                $start = \Xmf\Request::getInt('start', 0, 'GET');
85
                $criteria->setLimit(10);
86
                $criteria->setStart($start);
87
                $storetype = $imgcat->getVar('imgcat_storetype');
88
                if ('db' === $storetype) {
89
                    $images = $imageHandler->getObjects($criteria, false, true);
90
                } else {
91
                    $images = $imageHandler->getObjects($criteria, false, false);
92
                }
93
                $imgcount = count($images);
94
                $max      = ($imgcount > 10) ? 10 : $imgcount;
95
96
                for ($i = 0; $i < $max; ++$i) {
97
                    if ('db' === $storetype) {
98
                        $lcode = '[img align=left id=' . $images[$i]->getVar('image_id') . ']' . $images[$i]->getVar('image_nicename') . '[/img]';
99
                        $code  = '[img id=' . $images[$i]->getVar('image_id') . ']' . $images[$i]->getVar('image_nicename') . '[/img]';
100
                        $rcode = '[img align=right id=' . $images[$i]->getVar('image_id') . ']' . $images[$i]->getVar('image_nicename') . '[/img]';
101
                        $src   = XOOPS_URL . '/image.php?id=' . $images[$i]->getVar('image_id');
102
                    } else {
103
                        $lcode = '[img align=left]' . XOOPS_UPLOAD_URL . '/' . $images[$i]->getVar('image_name') . '[/img]';
104
                        $code  = '[img]' . XOOPS_UPLOAD_URL . '/' . $images[$i]->getVar('image_name') . '[/img]';
105
                        $rcode = '[img align=right]' . XOOPS_UPLOAD_URL . '/' . $images[$i]->getVar('image_name') . '[/img]';
106
                        $src   = XOOPS_UPLOAD_URL . '/' . $images[$i]->getVar('image_name');
107
                    }
108
                    $xoopsTpl->append('images', [
109
                        'id'       => $images[$i]->getVar('image_id'),
110
                        'nicename' => $images[$i]->getVar('image_nicename'),
111
                        'mimetype' => $images[$i]->getVar('image_mimetype'),
112
                        'src'      => $src,
113
                        'lxcode'   => $lcode,
114
                        'xcode'    => $code,
115
                        'rxcode'   => $rcode
116
                    ]);
117
                }
118
                if ($total > 10) {
119
                    require_once XOOPS_ROOT_PATH . '/class/pagenav.php';
120
                    $nav = new \XoopsPageNav($total, 10, $start, 'start', 'target=' . $target . '&amp;cat_id=' . $catshow);
121
                    $xoopsTpl->assign('pagenav', $nav->renderNav());
122
                }
123
            } else {
124
                $xoopsTpl->assign('image_total', 0);
125
            }
126
        }
127
        $xoopsTpl->assign('xsize', 600);
128
        $xoopsTpl->assign('ysize', 400);
129
    } else {
130
        $xoopsTpl->assign('xsize', 400);
131
        $xoopsTpl->assign('ysize', 180);
132
    }
133
    $xoopsTpl->display('db:system_imagemanager.tpl');
134
    exit();
135
}
136
137
if ('upload' === $op) {
138
    $imgcatHandler = xoops_getHandler('imagecategory');
139
    $imgcat_id     = \Xmf\Request::getInt('imgcat_id', 0, 'GET');
140
    $imgcat        = $imgcatHandler->get($imgcat_id);
141
    $error         = false;
142
    if (!is_object($imgcat)) {
143
        $error = true;
144
    } else {
145
        $imgcatpermHandler = xoops_getHandler('groupperm');
146
        if ($xoopsUser) {
147
            if (!$imgcatpermHandler->checkRight('imgcat_write', $imgcat_id, $xoopsUser->getGroups())) {
148
                $error = true;
149
            }
150
        } else {
151
            if (!$imgcatpermHandler->checkRight('imgcat_write', $imgcat_id, XOOPS_GROUP_ANONYMOUS)) {
152
                $error = true;
153
            }
154
        }
155
    }
156
    if (false !== $error) {
157
        xoops_header(false);
158
        echo '</head><body><div style="text-align:center;"><input value="' . _BACK . '" type="button" onclick="javascript:history.go(-1);"></div>';
159
        xoops_footer();
160
        exit();
161
    }
162
    require_once XOOPS_ROOT_PATH . '/class/template.php';
163
    $xoopsTpl = new \XoopsTpl();
164
    $xoopsTpl->assign('show_cat', $imgcat_id);
165
    $xoopsTpl->assign('lang_imgmanager', _IMGMANAGER);
166
    $xoopsTpl->assign('sitename', $xoopsConfig['sitename']);
167
    $xoopsTpl->assign('target', htmlspecialchars($_GET['target'], ENT_QUOTES));
168
    require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
169
    $form = new \XoopsThemeForm('', 'image_form', 'imagemanager.php');
170
    $form->setExtra('enctype="multipart/form-data"');
171
    $form->addElement(new \XoopsFormText(_IMAGENAME, 'image_nicename', 20, 255), true);
172
    $form->addElement(new \XoopsFormLabel(_IMAGECAT, $imgcat->getVar('imgcat_name')));
173
    $form->addElement(new \XoopsFormFile(_IMAGEFILE, 'image_file', $imgcat->getVar('imgcat_maxsize')), true);
174
    $form->addElement(new \XoopsFormLabel(_IMGMAXSIZE, $imgcat->getVar('imgcat_maxsize')));
175
    $form->addElement(new \XoopsFormLabel(_IMGMAXWIDTH, $imgcat->getVar('imgcat_maxwidth')));
176
    $form->addElement(new \XoopsFormLabel(_IMGMAXHEIGHT, $imgcat->getVar('imgcat_maxheight')));
177
    $form->addElement(new \XoopsFormHidden('imgcat_id', $imgcat_id));
178
    $form->addElement(new \XoopsFormHidden('op', 'doupload'));
179
    $form->addElement(new \XoopsFormHidden('target', $target));
180
    $form->addElement(new \XoopsFormButton('', 'img_button', _SUBMIT, 'submit'));
181
    $form->assign($xoopsTpl);
182
    $xoopsTpl->assign('lang_close', _CLOSE);
183
    $xoopsTpl->display('db:system_imagemanager2.tpl');
184
    exit();
185
}
186
187
if ('doupload' === $op) {
188
    require_once XOOPS_ROOT_PATH . '/class/uploader.php';
189
    $imgcatHandler = xoops_getHandler('imagecategory');
190
    $imgcat        = $imgcatHandler->get((int)$imgcat_id);
191
    $error         = false;
192
    if (!is_object($imgcat)) {
193
        $error = true;
194
    } else {
195
        $imgcatpermHandler = xoops_getHandler('groupperm');
196
        if ($xoopsUser) {
197
            if (!$imgcatpermHandler->checkRight('imgcat_write', $imgcat_id, $xoopsUser->getGroups())) {
198
                $error = true;
199
            }
200
        } else {
201
            if (!$imgcatpermHandler->checkRight('imgcat_write', $imgcat_id, XOOPS_GROUP_ANONYMOUS)) {
202
                $error = true;
203
            }
204
        }
205
    }
206
    if (false !== $error) {
207
        xoops_header(false);
208
        echo '</head><body><div style="text-align:center;"><input value="' . _BACK . '" type="button" onclick="javascript:history.go(-1);"></div>';
209
        xoops_footer();
210
        exit();
211
    }
212
    $uploader = new \XoopsMediaUploader(XOOPS_UPLOAD_PATH, ['image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png'], $imgcat->getVar('imgcat_maxsize'), $imgcat->getVar('imgcat_maxwidth'), $imgcat->getVar('imgcat_maxheight'));
213
    $uploader->setPrefix('img');
214
    if ($uploader->fetchMedia($_POST['xoops_upload_file'][0])) {
215
        if (!$uploader->upload()) {
216
            $err = $uploader->getErrors();
217
        } else {
218
            $imageHandler = xoops_getHandler('image');
219
            $image        = $imageHandler->create();
220
            $image->setVar('image_name', $uploader->getSavedFileName());
221
            $image->setVar('image_nicename', $image_nicename);
222
            $image->setVar('image_mimetype', $uploader->getMediaType());
223
            $image->setVar('image_created', time());
224
            $image->setVar('image_display', 1);
225
            $image->setVar('image_weight', 0);
226
            $image->setVar('imgcat_id', $imgcat_id);
227
            if ('db' === $imgcat->getVar('imgcat_storetype')) {
228
                $fp      = @fopen($uploader->getSavedDestination(), 'rb');
229
                $fbinary = @fread($fp, filesize($uploader->getSavedDestination()));
230
                @fclose($fp);
231
                $image->setVar('image_body', addslashes($fbinary));
232
                @unlink($uploader->getSavedDestination());
233
            }
234
            if (!$imageHandler->insert($image)) {
235
                $err = sprintf(_FAILSAVEIMG, $image->getVar('image_nicename'));
236
            }
237
        }
238
    } else {
239
        $err = _FAILFETCHIMG;
240
    }
241
    if (isset($err)) {
242
        xoops_header(false);
243
        xoops_error($err);
244
        echo '</head><body><div style="text-align:center;"><input value="' . _BACK . '" type="button" onclick="javascript:history.go(-1);"></div>';
245
        xoops_footer();
246
        exit();
247
    }
248
    header('location: imagemanager.php?cat_id=' . $imgcat_id . '&target=' . $target);
249
}
250