Completed
Branch master (c92e39)
by Michael
02:32
created

submit.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Module: SmartFAQ
5
 * Author: The SmartFactory <www.smartfactory.ca>
6
 * Licence: GNU
7
 */
8
9
include_once __DIR__ . '/header.php';
10
11
global $xoopsUser, $xoopsConfig, $xoopsModuleConfig, $xoopsModule;
12
13
// Creating the category handler object
14
$categoryHandler = sf_gethandler('category');
15
16
// Creating the FAQ handler object
17
$faqHandler = sf_gethandler('faq');
18
19
// Creating the answer handler object
20
$answerHandler = sf_gethandler('answer');
21
22
// Get the total number of categories
23
$totalCategories = count($categoryHandler->getCategories());
24
25
if ($totalCategories == 0) {
26
    redirect_header('index.php', 1, _AM_SF_NOCOLEXISTS);
27
}
28
29
// Find if the user is admin of the module
30
$isAdmin = sf_userIsAdmin();
31
// If the user is not admin AND we don't allow user submission, exit
32 View Code Duplication
if (!($isAdmin
33
      || (isset($xoopsModuleConfig['allowsubmit']) && $xoopsModuleConfig['allowsubmit'] == 1
34
          && (is_object($xoopsUser)
35
              || (isset($xoopsModuleConfig['anonpost'])
36
                  && $xoopsModuleConfig['anonpost'] == 1))))
37
) {
38
    redirect_header('index.php', 1, _NOPERM);
39
}
40
41
$op = 'form';
42
43
if (isset($_POST['post'])) {
44
    $op = 'post';
45
} elseif (isset($_POST['preview'])) {
46
    $op = 'preview';
47
}
48
49
switch ($op) {
50
    case 'preview':
51
52
        global $xoopsUser, $xoopsConfig, $xoopsModule, $xoopsModuleConfig, $xoopsDB;
53
54
        $faqObj      = $faqHandler->create();
55
        $answerObj   = $answerHandler->create();
56
        $categoryObj = $categoryHandler->get($_POST['categoryid']);
57
58 View Code Duplication
        if (!$xoopsUser) {
59
            if ($xoopsModuleConfig['anonpost'] == 1) {
60
                $uid = 0;
61
            } else {
62
                redirect_header('index.php', 3, _NOPERM);
63
            }
64
        } else {
65
            $uid = $xoopsUser->uid();
66
        }
67
68
        $notifypub = isset($_POST['notifypub']) ? $_POST['notifypub'] : 0;
69
70
        // Putting the values about the FAQ in the FAQ object
71
        $faqObj->setVar('categoryid', $_POST['categoryid']);
72
        $faqObj->setVar('uid', $uid);
73
        $faqObj->setVar('question', $_POST['question']);
74
        $faqObj->setVar('howdoi', $_POST['howdoi']);
75
        $faqObj->setVar('diduno', $_POST['diduno']);
76
        $faqObj->setVar('datesub', time());
77
78
        // Putting the values in the answer object
79
        $answerObj->setVar('status', _SF_AN_STATUS_APPROVED);
80
        $answerObj->setVar('faqid', $faqObj->faqid());
81
        $answerObj->setVar('answer', $_POST['answer']);
82
        $answerObj->setVar('uid', $uid);
83
84
        global $xoopsUser, $myts;
85
86
        $GLOBALS['xoopsOption']['template_main'] = 'smartfaq_submit.tpl';
87
        include_once XOOPS_ROOT_PATH . '/header.php';
88
        include_once __DIR__ . '/footer.php';
89
90
        $name = $xoopsUser ? ucwords($xoopsUser->getVar('uname')) : 'Anonymous';
0 ignored issues
show
The method getVar cannot be called on $xoopsUser (of type integer|double|string|array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
91
92
        $moduleName          =& $myts->displayTarea($xoopsModule->getVar('name'));
93
        $faq                 = $faqObj->toArray(null, $categoryObj, false);
94
        $faq['categoryPath'] = $categoryObj->getCategoryPath(true);
95
        $faq['answer']       = $answerObj->answer();
96
        $faq['who_when']     = $faqObj->getWhoAndWhen();
97
98
        $faq['comments'] = -1;
99
        $xoopsTpl->assign('faq', $faq);
100
        $xoopsTpl->assign('op', 'preview');
101
        $xoopsTpl->assign('whereInSection', $moduleName);
102
        $xoopsTpl->assign('lang_submit', _MD_SF_SUB_SNEWNAME);
103
104
        $xoopsTpl->assign('lang_intro_title', sprintf(_MD_SF_SUB_SNEWNAME, ucwords($xoopsModule->name())));
105
        $xoopsTpl->assign('lang_intro_text', _MD_SF_GOODDAY . "<b>$name</b>, " . _MD_SF_SUB_INTRO);
106
107
        include_once __DIR__ . '/include/submit.inc.php';
108
109
        include_once XOOPS_ROOT_PATH . '/footer.php';
110
111
        exit();
112
        break;
113
114
    case 'post':
115
116
        global $xoopsUser, $xoopsConfig, $xoopsModule, $xoopsModuleConfig, $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...
117
118
        $newFaqObj    = $faqHandler->create();
119
        $newAnswerObj = $answerHandler->create();
120
121 View Code Duplication
        if (!$xoopsUser) {
122
            if ($xoopsModuleConfig['anonpost'] == 1) {
123
                $uid = 0;
124
            } else {
125
                redirect_header('index.php', 3, _NOPERM);
126
            }
127
        } else {
128
            $uid = $xoopsUser->uid();
129
        }
130
131
        $notifypub = isset($_POST['notifypub']) ? $_POST['notifypub'] : 0;
132
133
        // Putting the values about the FAQ in the FAQ object
134
        $newFaqObj->setVar('categoryid', $_POST['categoryid']);
135
        $newFaqObj->setVar('uid', $uid);
136
        $newFaqObj->setVar('question', $_POST['question']);
137
        $newFaqObj->setVar('howdoi', $_POST['howdoi']);
138
        $newFaqObj->setVar('diduno', $_POST['diduno']);
139
        $newFaqObj->setVar('notifypub', $notifypub);
140
        //$newFaqObj->setVar('modulelink', $_POST['modulelink']);
141
        //$newFaqObj->setVar('contextpage', $_POST['contextpage']);
142
143
        // Setting the status of the FAQ
144
145
        // if user is admin, FAQ are automatically published
146
        $isAdmin = sf_userIsAdmin();
147
        if ($isAdmin) {
148
            $newFaqObj->setVar('status', _SF_STATUS_PUBLISHED);
149
        } elseif ($xoopsModuleConfig['autoapprove_submitted_faq'] == 1) {
150
            $newFaqObj->setVar('status', _SF_STATUS_PUBLISHED);
151
        } else {
152
            $newFaqObj->setVar('status', _SF_STATUS_SUBMITTED);
153
        }
154
155
        // Storing the FAQ object in the database
156
        if (!$newFaqObj->store()) {
157
            redirect_header('javascript:history.go(-1)', 2, _MD_SF_SUBMIT_ERROR);
158
        }
159
160
        // Putting the values in the answer object
161
        $newAnswerObj->setVar('status', _SF_AN_STATUS_APPROVED);
162
        $newAnswerObj->setVar('faqid', $newFaqObj->faqid());
163
        $newAnswerObj->setVar('answer', $_POST['answer']);
164
        $newAnswerObj->setVar('uid', $uid);
165
166
        //====================================================================================
167
        //TODO post Attachment
168
        $attachments_tmp = array();
169
        if (!empty($_POST['attachments_tmp'])) {
170
            $attachments_tmp = unserialize(base64_decode($_POST['attachments_tmp']));
171
            if (isset($_POST['delete_tmp']) && count($_POST['delete_tmp'])) {
172
                foreach ($_POST['delete_tmp'] as $key) {
173
                    unlink(XOOPS_ROOT_PATH . '/' . $xoopsModuleConfig['dir_attachments'] . '/' . $attachments_tmp[$key][0]);
174
                    unset($attachments_tmp[$key]);
175
                }
176
            }
177
        }
178
        if (count($attachments_tmp)) {
179
            foreach ($attachments_tmp as $key => $attach) {
180
                if (rename(XOOPS_CACHE_PATH . '/' . $attachments_tmp[$key][0], XOOPS_ROOT_PATH . '/' . $xoopsModuleConfig['dir_attachments'] . '/' . $attachments_tmp[$key][0])) {
181
                    $post_obj->setAttachment($attach[0], $attach[1], $attach[2]);
182
                }
183
            }
184
        }
185
        $error_upload = '';
186
187
        if (isset($_FILES['userfile']['name']) && $_FILES['userfile']['name'] != ''
188
            && $topicHandler->getPermission($forum_obj, $topic_status, 'attach')
189
        ) {
190
            require_once XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->getVar('dirname', 'n') . '/class/uploader.php';
191
            $maxfilesize = $forum_obj->getVar('attach_maxkb') * 1024;
192
            $uploaddir   = XOOPS_CACHE_PATH;
193
194
            $uploader = new sfUploader($uploaddir, $newAnswerObj->getVar('attach_ext'), (int)$maxfilesize, (int)$xoopsModuleConfig['max_img_width'], (int)$xoopsModuleConfig['max_img_height']);
195
196
            if ($_FILES['userfile']['error'] > 0) {
197
                switch ($_FILES['userfile']['error']) {
198
                    case 1:
199
                        $error_message[] = _MD_NEWBB_MAXUPLOADFILEINI;
200
                        break;
201
                    case 2:
202
                        $error_message[] = sprintf(_MD_NEWBB_MAXKB, $forum_obj->getVar('attach_maxkb'));
203
                        break;
204
                    default:
205
                        $error_message[] = _MD_NEWBB_UPLOAD_ERRNODEF;
206
                        break;
207
                }
208
            } else {
209
                $uploader->setCheckMediaTypeByExt();
210
211
                if ($uploader->fetchMedia($_POST['xoops_upload_file'][0])) {
212
                    $prefix = is_object($xoopsUser) ? (string)$xoopsUser->uid() . '_' : 'newbb_';
213
                    $uploader->setPrefix($prefix);
214
                    if (!$uploader->upload()) {
215
                        $error_message[] = $error_upload =& $uploader->getErrors();
216
                    } else {
217
                        if (is_file($uploader->getSavedDestination())) {
218
                            if (rename(XOOPS_CACHE_PATH . '/' . $uploader->getSavedFileName(), XOOPS_ROOT_PATH . '/' . $xoopsModuleConfig['dir_attachments'] . '/' . $uploader->getSavedFileName())) {
219
                                $post_obj->setAttachment($uploader->getSavedFileName(), $uploader->getMediaName(), $uploader->getMediaType());
220
                            }
221
                        }
222
                    }
223
                } else {
224
                    $error_message[] = $error_upload =& $uploader->getErrors();
225
                }
226
            }
227
        }
228
229
        //====================================================
230
231
        // Storing the answer object in the database
232
        if (!$newAnswerObj->store()) {
233
            redirect_header('javascript:history.go(-1)', 2, _MD_SF_SUBMIT_ERROR);
234
        }
235
236
        // Get the cateopry object related to that FAQ
237
        $categoryObj = $newFaqObj->category();
238
239
        // If autoapprove_submitted_faq
240
        if ($isAdmin) {
241
            // We do not not subscribe user to notification on publish since we publish it right away
242
243
            // Send notifications
244
            $newFaqObj->sendNotifications(array(_SF_NOT_FAQ_PUBLISHED));
245
246
            $redirect_msg = _MD_SF_SUBMIT_FROM_ADMIN;
247
        } elseif ($xoopsModuleConfig['autoapprove_submitted_faq'] == 1) {
248
            // We do not not subscribe user to notification on publish since we publish it right away
249
250
            // Send notifications
251
            $newFaqObj->sendNotifications(array(_SF_NOT_FAQ_PUBLISHED));
252
253
            $redirect_msg = _MD_SF_QNA_RECEIVED_AND_PUBLISHED;
254 View Code Duplication
        } else {
255
            // Subscribe the user to On Published notification, if requested
256
            if ($notifypub == 1) {
257
                include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
258
                $notificationHandler = xoops_getHandler('notification');
259
                $notificationHandler->subscribe('faq', $newFaqObj->faqid(), 'approved', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE);
260
            }
261
            // Send notifications
262
            $newFaqObj->sendNotifications(array(_SF_NOT_FAQ_SUBMITTED));
263
264
            $redirect_msg = _MD_SF_QNA_RECEIVED_NEED_APPROVAL;
265
        }
266
267
        redirect_header('index.php', 2, $redirect_msg);
268
        break;
269
270
    case 'form':
271
    default:
272
273
        global $xoopsUser, $myts;
274
275
        $faqObj      = $faqHandler->create();
276
        $answerObj   = $answerHandler->create();
277
        $categoryObj = $categoryHandler->create();
278
279
        $GLOBALS['xoopsOption']['template_main'] = 'smartfaq_submit.html';
280
        include_once XOOPS_ROOT_PATH . '/header.php';
281
        include_once __DIR__ . '/footer.php';
282
283
        $name       = $xoopsUser ? ucwords($xoopsUser->getVar('uname')) : 'Anonymous';
0 ignored issues
show
The method getVar cannot be called on $xoopsUser (of type integer|double|string|array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
284
        $notifypub  = 1;
285
        $moduleName =& $myts->displayTarea($xoopsModule->getVar('name'));
286
        $xoopsTpl->assign('whereInSection', $moduleName);
287
        $xoopsTpl->assign('lang_submit', _MD_SF_SUB_SNEWNAME);
288
289
        $xoopsTpl->assign('lang_intro_title', sprintf(_MD_SF_SUB_SNEWNAME, ucwords($xoopsModule->name())));
290
        $xoopsTpl->assign('lang_intro_text', _MD_SF_GOODDAY . "<b>$name</b>, " . _MD_SF_SUB_INTRO);
291
292
        include_once __DIR__ . '/include/submit.inc.php';
293
294
        include_once XOOPS_ROOT_PATH . '/footer.php';
295
        break;
296
}
297