Passed
Push — master ( 2744eb...81ba93 )
by Michael
02:46
created

submit.php (5 issues)

1
<?php
2
3
/**
4
 * Module: SmartFAQ
5
 * Author: The SmartFactory <www.smartfactory.ca>
6
 * Licence: GNU
7
 */
8
9
use XoopsModules\Smartfaq;
10
use XoopsModules\Smartfaq\Constants;
11
12
require_once __DIR__ . '/header.php';
13
14
global $xoopsUser, $xoopsConfig, $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
15
/** @var Smartfaq\Helper $helper */
16
$helper = Smartfaq\Helper::getInstance();
17
18
// Creating the category handler object
19
/** @var \XoopsModules\Smartfaq\CategoryHandler $categoryHandler */
20
$categoryHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Category');
21
22
// Creating the FAQ handler object
23
/** @var \XoopsModules\Smartfaq\FaqHandler $faqHandler */
24
$faqHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Faq');
25
26
// Creating the answer handler object
27
/** @var \XoopsModules\Smartfaq\AnswerHandler $answerHandler */
28
$answerHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Answer');
29
30
// Get the total number of categories
31
$totalCategories = count($categoryHandler->getCategories());
32
33
if (0 == $totalCategories) {
34
    redirect_header('index.php', 1, _AM_SF_NOCOLEXISTS);
35
}
36
37
// Find if the user is admin of the module
38
$isAdmin = Smartfaq\Utility::userIsAdmin();
39
// If the user is not admin AND we don't allow user submission, exit
40
if (!($isAdmin
41
      ||  (null !== ($helper->getConfig('allowsubmit')) && 1 == $helper->getConfig('allowsubmit')
42
          && (is_object($xoopsUser)
43
              ||  (null !== ($helper->getConfig('anonpost'))
44
                  && 1 == $helper->getConfig('anonpost')))))) {
45
    redirect_header('index.php', 1, _NOPERM);
46
}
47
48
$op = 'form';
49
50
if (isset($_POST['post'])) {
51
    $op = 'post';
52
} elseif (isset($_POST['preview'])) {
53
    $op = 'preview';
54
}
55
56
switch ($op) {
57
    case 'preview':
58
59
        global $xoopsUser, $xoopsConfig, $xoopsModule,  $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
60
61
        $faqObj      = $faqHandler->create();
62
        $answerObj   = $answerHandler->create();
63
        $categoryObj = $categoryHandler->get($_POST['categoryid']);
64
65
        if (!$xoopsUser) {
66
            if (1 == $helper->getConfig('anonpost')) {
67
                $uid = 0;
68
            } else {
69
                redirect_header('index.php', 3, _NOPERM);
70
            }
71
        } else {
72
            $uid = $xoopsUser->uid();
73
        }
74
75
        $notifypub = \Xmf\Request::getInt('notifypub', 0, POST);
0 ignored issues
show
The type Xmf\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
The constant POST was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
76
77
        // Putting the values about the FAQ in the FAQ object
78
        $faqObj->setVar('categoryid', $_POST['categoryid']);
79
        $faqObj->setVar('uid', $uid);
80
        $faqObj->setVar('question', $_POST['question']);
81
        $faqObj->setVar('howdoi', $_POST['howdoi']);
82
        $faqObj->setVar('diduno', $_POST['diduno']);
83
        $faqObj->setVar('datesub', time());
84
85
        // Putting the values in the answer object
86
        $answerObj->setVar('status', Constants::SF_AN_STATUS_APPROVED);
87
        $answerObj->setVar('faqid', $faqObj->faqid());
88
        $answerObj->setVar('answer', $_POST['answer']);
89
        $answerObj->setVar('uid', $uid);
90
91
        global $xoopsUser, $myts;
92
93
        $GLOBALS['xoopsOption']['template_main'] = 'smartfaq_submit.tpl';
94
        require_once XOOPS_ROOT_PATH . '/header.php';
95
        require_once __DIR__ . '/footer.php';
96
97
        $name = $xoopsUser ? ucwords($xoopsUser->getVar('uname')) : 'Anonymous';
98
99
        $moduleName          =& $myts->displayTarea($xoopsModule->getVar('name'));
100
        $faq                 = $faqObj->toArray(null, $categoryObj, false);
101
        $faq['categoryPath'] = $categoryObj->getCategoryPath(true);
102
        $faq['answer']       = $answerObj->answer();
103
        $faq['who_when']     = $faqObj->getWhoAndWhen();
104
105
        $faq['comments'] = -1;
106
        $xoopsTpl->assign('faq', $faq);
107
        $xoopsTpl->assign('op', 'preview');
108
        $xoopsTpl->assign('whereInSection', $moduleName);
109
        $xoopsTpl->assign('lang_submit', _MD_SF_SUB_SNEWNAME);
110
111
        $xoopsTpl->assign('lang_intro_title', sprintf(_MD_SF_SUB_SNEWNAME, ucwords($xoopsModule->name())));
112
        $xoopsTpl->assign('lang_intro_text', _MD_SF_GOODDAY . "<b>$name</b>, " . _MD_SF_SUB_INTRO);
113
114
        require_once __DIR__ . '/include/submit.inc.php';
115
116
        require_once XOOPS_ROOT_PATH . '/footer.php';
117
118
        exit();
119
        break;
120
121
    case 'post':
122
123
        global $xoopsUser, $xoopsConfig, $xoopsModule,  $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
124
125
        $newFaqObj    = $faqHandler->create();
126
        $newAnswerObj = $answerHandler->create();
127
128
        if (!$xoopsUser) {
129
            if (1 == $helper->getConfig('anonpost')) {
130
                $uid = 0;
131
            } else {
132
                redirect_header('index.php', 3, _NOPERM);
133
            }
134
        } else {
135
            $uid = $xoopsUser->uid();
136
        }
137
138
        $notifypub = \Xmf\Request::getInt('notifypub', 0, POST);
139
140
        // Putting the values about the FAQ in the FAQ object
141
        $newFaqObj->setVar('categoryid', $_POST['categoryid']);
142
        $newFaqObj->setVar('uid', $uid);
143
        $newFaqObj->setVar('question', $_POST['question']);
144
        $newFaqObj->setVar('howdoi', $_POST['howdoi']);
145
        $newFaqObj->setVar('diduno', $_POST['diduno']);
146
        $newFaqObj->setVar('notifypub', $notifypub);
147
        //$newFaqObj->setVar('modulelink', $_POST['modulelink']);
148
        //$newFaqObj->setVar('contextpage', $_POST['contextpage']);
149
150
        // Setting the status of the FAQ
151
152
        // if user is admin, FAQ are automatically published
153
        $isAdmin = Smartfaq\Utility::userIsAdmin();
154
        if ($isAdmin) {
155
            $newFaqObj->setVar('status', Constants::SF_STATUS_PUBLISHED);
156
        } elseif (1 == $helper->getConfig('autoapprove_submitted_faq')) {
157
            $newFaqObj->setVar('status', Constants::SF_STATUS_PUBLISHED);
158
        } else {
159
            $newFaqObj->setVar('status', Constants::SF_STATUS_SUBMITTED);
160
        }
161
162
        // Storing the FAQ object in the database
163
        if (!$newFaqObj->store()) {
164
            redirect_header('javascript:history.go(-1)', 2, _MD_SF_SUBMIT_ERROR);
165
        }
166
167
        // Putting the values in the answer object
168
        $newAnswerObj->setVar('status', Constants::SF_AN_STATUS_APPROVED);
169
        $newAnswerObj->setVar('faqid', $newFaqObj->faqid());
170
        $newAnswerObj->setVar('answer', $_POST['answer']);
171
        $newAnswerObj->setVar('uid', $uid);
172
173
        //====================================================================================
174
        //TODO post Attachment
175
        $attachments_tmp = [];
176
        if (!empty($_POST['attachments_tmp'])) {
177
            $attachments_tmp = unserialize(base64_decode($_POST['attachments_tmp']));
178
            if (isset($_POST['delete_tmp']) && count($_POST['delete_tmp'])) {
179
                foreach ($_POST['delete_tmp'] as $key) {
180
                    unlink(XOOPS_ROOT_PATH . '/' . $helper->getConfig('dir_attachments') . '/' . $attachments_tmp[$key][0]);
181
                    unset($attachments_tmp[$key]);
182
                }
183
            }
184
        }
185
        if (count($attachments_tmp)) {
186
            foreach ($attachments_tmp as $key => $attach) {
187
                if (rename(XOOPS_CACHE_PATH . '/' . $attachments_tmp[$key][0], XOOPS_ROOT_PATH . '/' . $helper->getConfig('dir_attachments') . '/' . $attachments_tmp[$key][0])) {
188
                    $post_obj->setAttachment($attach[0], $attach[1], $attach[2]);
189
                }
190
            }
191
        }
192
        $error_upload = '';
193
194
        if (isset($_FILES['userfile']['name']) && '' != $_FILES['userfile']['name']
195
            && $topicHandler->getPermission($forum_obj, $topic_status, 'attach')) {
196
            require_once XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->getVar('dirname', 'n') . '/class/uploader.php';
197
            $maxfilesize = $forum_obj->getVar('attach_maxkb') * 1024;
198
            $uploaddir   = XOOPS_CACHE_PATH;
199
200
            $uploader = new Smartfaq\Uploader($uploaddir, $newAnswerObj->getVar('attach_ext'), (int)$maxfilesize, (int)$helper->getConfig('max_img_width'), (int)$helper->getConfig('max_img_height'));
201
202
            if ($_FILES['userfile']['error'] > 0) {
203
                switch ($_FILES['userfile']['error']) {
204
                    case 1:
205
                        $error_message[] = _MD_NEWBB_MAXUPLOADFILEINI;
206
                        break;
207
                    case 2:
208
                        $error_message[] = sprintf(_MD_NEWBB_MAXKB, $forum_obj->getVar('attach_maxkb'));
209
                        break;
210
                    default:
211
                        $error_message[] = _MD_NEWBB_UPLOAD_ERRNODEF;
212
                        break;
213
                }
214
            } else {
215
                $uploader->setCheckMediaTypeByExt();
216
217
                if ($uploader->fetchMedia($_POST['xoops_upload_file'][0])) {
218
                    $prefix = is_object($xoopsUser) ? (string)$xoopsUser->uid() . '_' : 'newbb_';
219
                    $uploader->setPrefix($prefix);
220
                    if (!$uploader->upload()) {
221
                        $error_message[] = $error_upload =& $uploader->getErrors();
222
                    } else {
223
                        if (is_file($uploader->getSavedDestination())) {
224
                            if (rename(XOOPS_CACHE_PATH . '/' . $uploader->getSavedFileName(), XOOPS_ROOT_PATH . '/' . $helper->getConfig('dir_attachments') . '/' . $uploader->getSavedFileName())) {
225
                                $post_obj->setAttachment($uploader->getSavedFileName(), $uploader->getMediaName(), $uploader->getMediaType());
226
                            }
227
                        }
228
                    }
229
                } else {
230
                    $error_message[] = $error_upload =& $uploader->getErrors();
231
                }
232
            }
233
        }
234
235
        //====================================================
236
237
        // Storing the answer object in the database
238
        if (!$newAnswerObj->store()) {
239
            redirect_header('javascript:history.go(-1)', 2, _MD_SF_SUBMIT_ERROR);
240
        }
241
242
        // Get the cateopry object related to that FAQ
243
        $categoryObj = $newFaqObj->category();
244
245
        // If autoapprove_submitted_faq
246
        if ($isAdmin) {
247
            // We do not not subscribe user to notification on publish since we publish it right away
248
249
            // Send notifications
250
            $newFaqObj->sendNotifications([Constants::SF_NOT_FAQ_PUBLISHED]);
251
252
            $redirect_msg = _MD_SF_SUBMIT_FROM_ADMIN;
253
        } elseif (1 == $helper->getConfig('autoapprove_submitted_faq')) {
254
            // We do not not subscribe user to notification on publish since we publish it right away
255
256
            // Send notifications
257
            $newFaqObj->sendNotifications([Constants::SF_NOT_FAQ_PUBLISHED]);
258
259
            $redirect_msg = _MD_SF_QNA_RECEIVED_AND_PUBLISHED;
260
        } else {
261
            // Subscribe the user to On Published notification, if requested
262
            if (1 == $notifypub) {
263
                require_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
264
                $notificationHandler = xoops_getHandler('notification');
265
                $notificationHandler->subscribe('faq', $newFaqObj->faqid(), 'approved', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE);
266
            }
267
            // Send notifications
268
            $newFaqObj->sendNotifications([Constants::SF_NOT_FAQ_SUBMITTED]);
269
270
            $redirect_msg = _MD_SF_QNA_RECEIVED_NEED_APPROVAL;
271
        }
272
273
        redirect_header('index.php', 2, $redirect_msg);
274
        break;
275
276
    case 'form':
277
    default:
278
279
        global $xoopsUser, $myts;
280
281
        $faqObj      = $faqHandler->create();
282
        $answerObj   = $answerHandler->create();
283
        $categoryObj = $categoryHandler->create();
284
285
        $GLOBALS['xoopsOption']['template_main'] = 'smartfaq_submit.tpl';
286
        require_once XOOPS_ROOT_PATH . '/header.php';
287
        require_once __DIR__ . '/footer.php';
288
289
        $name       = $xoopsUser ? ucwords($xoopsUser->getVar('uname')) : 'Anonymous';
290
        $notifypub  = 1;
291
        $moduleName =& $myts->displayTarea($xoopsModule->getVar('name'));
292
        $xoopsTpl->assign('whereInSection', $moduleName);
293
        $xoopsTpl->assign('lang_submit', _MD_SF_SUB_SNEWNAME);
294
295
        $xoopsTpl->assign('lang_intro_title', sprintf(_MD_SF_SUB_SNEWNAME, ucwords($xoopsModule->name())));
296
        $xoopsTpl->assign('lang_intro_text', _MD_SF_GOODDAY . "<b>$name</b>, " . _MD_SF_SUB_INTRO);
297
298
        require_once __DIR__ . '/include/submit.inc.php';
299
300
        require_once XOOPS_ROOT_PATH . '/footer.php';
301
        break;
302
}
303