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 | * $Id: submit.php,v 1.16 2005/08/15 16:51:58 fx2024 Exp $ |
||
5 | * Module: SmartFAQ |
||
6 | * Author: The SmartFactory <www.smartfactory.ca> |
||
7 | * Licence: GNU |
||
8 | */ |
||
9 | |||
10 | include_once __DIR__ . '/header.php'; |
||
11 | |||
12 | Global $xoopsUser, $xoopsConfig, $xoopsModuleConfig, $xoopsModule; |
||
13 | |||
14 | // Creating the category handler object |
||
15 | $category_handler =& sf_gethandler('category'); |
||
16 | |||
17 | // Creating the FAQ handler object |
||
18 | $faq_handler =& sf_gethandler('faq'); |
||
19 | |||
20 | // Creating the answer handler object |
||
21 | $answer_handler =& sf_gethandler('answer'); |
||
22 | |||
23 | // Get the total number of categories |
||
24 | $totalCategories = count($category_handler->getCategories()); |
||
25 | |||
26 | if ($totalCategories == 0) { |
||
27 | redirect_header("index.php", 1, _AM_SF_NOCOLEXISTS); |
||
28 | exit(); |
||
29 | } |
||
30 | |||
31 | // Find if the user is admin of the module |
||
32 | $isAdmin = sf_userIsAdmin(); |
||
33 | // If the user is not admin AND we don't allow user submission, exit |
||
34 | View Code Duplication | if (!($isAdmin || (isset($xoopsModuleConfig['allowsubmit']) && $xoopsModuleConfig['allowsubmit'] == 1 && (is_object($xoopsUser) || (isset($xoopsModuleConfig['anonpost']) && $xoopsModuleConfig['anonpost'] == 1))))) { |
|
35 | redirect_header("index.php", 1, _NOPERM); |
||
36 | exit(); |
||
37 | } |
||
38 | |||
39 | $op = ''; |
||
40 | |||
41 | if (isset($_POST['post'])) { |
||
42 | $op = 'post'; |
||
43 | } elseif (isset($_POST['preview'])) { |
||
44 | $op = 'preview'; |
||
45 | } else { |
||
46 | $op = 'form'; |
||
47 | } |
||
48 | |||
49 | switch ($op) { |
||
50 | case 'preview': |
||
51 | |||
52 | Global $xoopsUser, $xoopsConfig, $xoopsModule, $xoopsModuleConfig, $xoopsDB; |
||
53 | |||
54 | $faqObj = $faq_handler->create(); |
||
55 | $answerObj = $answer_handler->create(); |
||
56 | $categoryObj = $category_handler->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 | exit(); |
||
64 | } |
||
65 | } else { |
||
66 | $uid = $xoopsUser->uid(); |
||
67 | } |
||
68 | |||
69 | $notifypub = isset($_POST['notifypub'])? $_POST['notifypub'] : 0; |
||
70 | |||
71 | // Putting the values about the FAQ in the FAQ object |
||
72 | $faqObj->setVar('categoryid', $_POST['categoryid']); |
||
73 | $faqObj->setVar('uid', $uid); |
||
74 | $faqObj->setVar('question', $_POST['question']); |
||
75 | $faqObj->setVar('howdoi', $_POST['howdoi']); |
||
76 | $faqObj->setVar('diduno', $_POST['diduno']); |
||
77 | $faqObj->setVar('datesub', time()); |
||
78 | |||
79 | // Putting the values in the answer object |
||
80 | $answerObj->setVar('status', _SF_AN_STATUS_APPROVED); |
||
81 | $answerObj->setVar('faqid', $faqObj->faqid()); |
||
82 | $answerObj->setVar('answer', $_POST['answer']); |
||
83 | $answerObj->setVar('uid', $uid); |
||
84 | |||
85 | global $xoopsUser, $myts; |
||
86 | |||
87 | $xoopsOption['template_main'] = 'smartfaq_submit.tpl'; |
||
88 | include_once(XOOPS_ROOT_PATH . "/header.php"); |
||
89 | include_once __DIR__ . '/footer.php'; |
||
90 | |||
91 | $name = ($xoopsUser)? (ucwords($xoopsUser->getVar("uname"))) : 'Anonymous'; |
||
92 | |||
93 | $moduleName = $myts->displayTarea($xoopsModule->getVar('name')); |
||
94 | $faq = $faqObj->toArray(null, $categoryObj, false); |
||
95 | $faq['categoryPath'] = $categoryObj->getCategoryPath(true); |
||
96 | $faq['answer'] = $answerObj->answer(); |
||
97 | $faq['who_when'] = $faqObj->getWhoAndWhen(); |
||
98 | |||
99 | $faq['comments'] = -1; |
||
100 | $xoopsTpl->assign('faq', $faq); |
||
101 | $xoopsTpl->assign('op', 'preview'); |
||
102 | $xoopsTpl->assign('whereInSection', $moduleName); |
||
103 | $xoopsTpl->assign('lang_submit', _MD_SF_SUB_SNEWNAME); |
||
104 | |||
105 | $xoopsTpl->assign('lang_intro_title', sprintf(_MD_SF_SUB_SNEWNAME, ucwords($xoopsModule->name()))); |
||
106 | $xoopsTpl->assign('lang_intro_text', _MD_SF_GOODDAY . "<b>$name</b>, " . _MD_SF_SUB_INTRO); |
||
107 | |||
108 | include_once 'include/submit.inc.php'; |
||
109 | |||
110 | include_once XOOPS_ROOT_PATH . '/footer.php'; |
||
111 | |||
112 | exit(); |
||
113 | break; |
||
114 | |||
115 | case 'post': |
||
116 | |||
117 | Global $xoopsUser, $xoopsConfig, $xoopsModule, $xoopsModuleConfig, $xoopsDB; |
||
0 ignored issues
–
show
|
|||
118 | |||
119 | $newFaqObj = $faq_handler->create(); |
||
120 | $newAnswerObj = $answer_handler->create(); |
||
121 | |||
122 | View Code Duplication | if (!$xoopsUser) { |
|
123 | if ($xoopsModuleConfig['anonpost'] == 1) { |
||
124 | $uid = 0; |
||
125 | } else { |
||
126 | redirect_header("index.php", 3, _NOPERM); |
||
127 | exit(); |
||
128 | } |
||
129 | } else { |
||
130 | $uid = $xoopsUser->uid(); |
||
131 | } |
||
132 | |||
133 | $notifypub = isset($_POST['notifypub'])? $_POST['notifypub'] : 0; |
||
134 | |||
135 | // Putting the values about the FAQ in the FAQ object |
||
136 | $newFaqObj->setVar('categoryid', $_POST['categoryid']); |
||
137 | $newFaqObj->setVar('uid', $uid); |
||
138 | $newFaqObj->setVar('question', $_POST['question']); |
||
139 | $newFaqObj->setVar('howdoi', $_POST['howdoi']); |
||
140 | $newFaqObj->setVar('diduno', $_POST['diduno']); |
||
141 | $newFaqObj->setVar('notifypub', $notifypub); |
||
142 | //$newFaqObj->setVar('modulelink', $_POST['modulelink']); |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
85% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. ![]() |
|||
143 | //$newFaqObj->setVar('contextpage', $_POST['contextpage']); |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
85% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. ![]() |
|||
144 | |||
145 | // Setting the status of the FAQ |
||
146 | |||
147 | // if user is admin, FAQ are automatically published |
||
148 | $isAdmin = sf_userIsAdmin(); |
||
149 | if ($isAdmin) { |
||
150 | $newFaqObj->setVar('status', _SF_STATUS_PUBLISHED); |
||
151 | } elseif ($xoopsModuleConfig['autoapprove_submitted_faq'] == 1) { |
||
152 | $newFaqObj->setVar('status', _SF_STATUS_PUBLISHED); |
||
153 | } else { |
||
154 | $newFaqObj->setVar('status', _SF_STATUS_SUBMITTED); |
||
155 | } |
||
156 | |||
157 | // Storing the FAQ object in the database |
||
158 | if ( !$newFaqObj->store() ) { |
||
159 | redirect_header("javascript:history.go(-1)", 2, _MD_SF_SUBMIT_ERROR); |
||
160 | exit(); |
||
161 | } |
||
162 | |||
163 | // Putting the values in the answer object |
||
164 | $newAnswerObj->setVar('status', _SF_AN_STATUS_APPROVED); |
||
165 | $newAnswerObj->setVar('faqid', $newFaqObj->faqid()); |
||
166 | $newAnswerObj->setVar('answer', $_POST['answer']); |
||
167 | $newAnswerObj->setVar('uid', $uid); |
||
168 | |||
169 | //==================================================================================== |
||
170 | //TODO post Attachment |
||
171 | $attachments_tmp = array(); |
||
172 | if (!empty($_POST["attachments_tmp"])) { |
||
173 | $attachments_tmp = unserialize(base64_decode($_POST["attachments_tmp"])); |
||
174 | if (isset($_POST["delete_tmp"]) && count($_POST["delete_tmp"])) { |
||
175 | foreach ($_POST["delete_tmp"] as $key) { |
||
176 | unlink(XOOPS_ROOT_PATH . "/" . $xoopsModuleConfig['dir_attachments'] . "/" . $attachments_tmp[$key][0]); |
||
177 | unset($attachments_tmp[$key]); |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | if (count($attachments_tmp)) { |
||
182 | foreach ($attachments_tmp as $key => $attach) { |
||
183 | if (rename(XOOPS_CACHE_PATH . "/" . $attachments_tmp[$key][0], XOOPS_ROOT_PATH . "/" . $xoopsModuleConfig['dir_attachments'] . "/" . $attachments_tmp[$key][0])) { |
||
184 | $post_obj->setAttachment($attach[0], $attach[1], $attach[2]); |
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | $error_upload = ''; |
||
189 | |||
190 | if (isset($_FILES['userfile']['name']) && $_FILES['userfile']['name'] != '' && $topic_handler->getPermission($forum_obj, $topic_status, 'attach') ) { |
||
191 | require_once XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->getVar("dirname", "n") . '/class/uploader.php'; |
||
192 | $maxfilesize = $forum_obj->getVar('attach_maxkb') * 1024; |
||
193 | $uploaddir = XOOPS_CACHE_PATH; |
||
194 | |||
195 | $uploader = new sf_uploader( |
||
196 | $uploaddir, |
||
197 | $newAnswerObj->getVar('attach_ext'), |
||
198 | intval($maxfilesize), |
||
199 | intval($xoopsModuleConfig['max_img_width']), |
||
200 | intval($xoopsModuleConfig['max_img_height']) |
||
201 | ); |
||
202 | |||
203 | if ($_FILES['userfile']['error'] > 0) { |
||
204 | switch ($_FILES['userfile']['error']) { |
||
205 | case 1: |
||
206 | $error_message[] = _MD_NEWBB_MAXUPLOADFILEINI; |
||
207 | break; |
||
208 | case 2: |
||
209 | $error_message[] = sprintf(_MD_NEWBB_MAXKB,$forum_obj->getVar('attach_maxkb')); |
||
210 | break; |
||
211 | default: |
||
212 | $error_message[] = _MD_NEWBB_UPLOAD_ERRNODEF; |
||
213 | break; |
||
214 | } |
||
215 | } else { |
||
216 | $uploader->setCheckMediaTypeByExt(); |
||
217 | |||
218 | if ( $uploader->fetchMedia( $_POST['xoops_upload_file'][0]) ) { |
||
219 | $prefix = is_object($xoopsUser) ? strval($xoopsUser->uid()) . '_' : 'newbb_'; |
||
220 | $uploader->setPrefix($prefix); |
||
221 | if ( !$uploader->upload() ) { |
||
222 | $error_message[] = $error_upload = $uploader->getErrors(); |
||
223 | } else { |
||
224 | if ( is_file( $uploader->getSavedDestination() )) { |
||
225 | if (rename(XOOPS_CACHE_PATH . "/" . $uploader->getSavedFileName(), XOOPS_ROOT_PATH . "/" . $xoopsModuleConfig['dir_attachments'] . "/" . $uploader->getSavedFileName())) { |
||
226 | $post_obj->setAttachment($uploader->getSavedFileName(), $uploader->getMediaName(), $uploader->getMediaType()); |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | } else { |
||
231 | $error_message[] = $error_upload = $uploader->getErrors(); |
||
232 | } |
||
233 | } |
||
234 | } |
||
235 | |||
236 | //==================================================== |
||
237 | |||
238 | // Storing the answer object in the database |
||
239 | if ( !$newAnswerObj->store() ) { |
||
240 | redirect_header("javascript:history.go(-1)", 2, _MD_SF_SUBMIT_ERROR); |
||
241 | exit(); |
||
242 | } |
||
243 | |||
244 | // Get the cateopry object related to that FAQ |
||
245 | $categoryObj =& $newFaqObj->category(); |
||
246 | |||
247 | // If autoapprove_submitted_faq |
||
248 | if ($isAdmin) { |
||
249 | // We do not not subscribe user to notification on publish since we publish it right away |
||
250 | |||
251 | // Send notifications |
||
252 | $newFaqObj->sendNotifications(array(_SF_NOT_FAQ_PUBLISHED)); |
||
253 | |||
254 | $redirect_msg = _MD_SF_SUBMIT_FROM_ADMIN; |
||
255 | } elseif ($xoopsModuleConfig['autoapprove_submitted_faq'] == 1) { |
||
256 | // We do not not subscribe user to notification on publish since we publish it right away |
||
257 | |||
258 | // Send notifications |
||
259 | $newFaqObj->sendNotifications(array(_SF_NOT_FAQ_PUBLISHED)); |
||
260 | |||
261 | $redirect_msg = _MD_SF_QNA_RECEIVED_AND_PUBLISHED; |
||
262 | View Code Duplication | } else { |
|
263 | // Subscribe the user to On Published notification, if requested |
||
264 | if ($notifypub == 1) { |
||
265 | include_once XOOPS_ROOT_PATH . '/include/notification_constants.php'; |
||
266 | $notification_handler = &xoops_gethandler('notification'); |
||
267 | $notification_handler->subscribe('faq', $newFaqObj->faqid(), 'approved', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE); |
||
268 | } |
||
269 | // Send notifications |
||
270 | $newFaqObj->sendNotifications(array(_SF_NOT_FAQ_SUBMITTED)); |
||
271 | |||
272 | $redirect_msg = _MD_SF_QNA_RECEIVED_NEED_APPROVAL; |
||
273 | } |
||
274 | |||
275 | redirect_header("index.php", 2, $redirect_msg); |
||
276 | |||
277 | exit(); |
||
278 | break; |
||
279 | |||
280 | case 'form': |
||
281 | default: |
||
282 | |||
283 | global $xoopsUser, $myts; |
||
284 | |||
285 | $faqObj = $faq_handler->create(); |
||
286 | $answerObj = $answer_handler->create(); |
||
287 | $categoryObj = $category_handler->create(); |
||
288 | |||
289 | $xoopsOption['template_main'] = 'smartfaq_submit.html'; |
||
290 | include_once(XOOPS_ROOT_PATH . "/header.php"); |
||
291 | include_once __DIR__ . '/footer.php'; |
||
292 | |||
293 | $name = ($xoopsUser)? (ucwords($xoopsUser->getVar("uname"))) : 'Anonymous'; |
||
0 ignored issues
–
show
|
|||
294 | $notifypub = 1; |
||
295 | $moduleName = $myts->displayTarea($xoopsModule->getVar('name')); |
||
296 | $xoopsTpl->assign('whereInSection', $moduleName); |
||
297 | $xoopsTpl->assign('lang_submit', _MD_SF_SUB_SNEWNAME); |
||
298 | |||
299 | $xoopsTpl->assign('lang_intro_title', sprintf(_MD_SF_SUB_SNEWNAME, ucwords($xoopsModule->name()))); |
||
300 | $xoopsTpl->assign('lang_intro_text', _MD_SF_GOODDAY . "<b>$name</b>, " . _MD_SF_SUB_INTRO); |
||
301 | |||
302 | include_once 'include/submit.inc.php'; |
||
303 | |||
304 | include_once XOOPS_ROOT_PATH . '/footer.php'; |
||
305 | break; |
||
306 | } |
||
307 |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state