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