XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
comment_delete.php
1<?php
13
14if (!defined('XOOPS_ROOT_PATH') || !is_object($xoopsModule)) {
15 exit();
16}
17
18include_once XOOPS_ROOT_PATH.'/include/comment_constants.php';
19
20$op = 'delete';
21
22if (!empty($_POST)) {
23 $com_mode = isset($_POST['com_mode']) ? htmlspecialchars(trim($_POST['com_mode']), ENT_QUOTES) : 'flat';
24 $com_order = isset($_POST['com_order']) ? (int)$_POST['com_order'] : XOOPS_COMMENT_OLD1ST;
25 $com_id = isset($_POST['com_id']) ? (int)$_POST['com_id'] : 0;
26 $op = $_POST['op'] ?? 'delete';
27} else {
28 $com_mode = isset($_GET['com_mode']) ? htmlspecialchars(trim($_GET['com_mode']), ENT_QUOTES) : 'flat';
29 $com_order = isset($_GET['com_order']) ? (int)$_GET['com_order'] : XOOPS_COMMENT_OLD1ST;
30 $com_id = isset($_GET['com_id']) ? (int)$_GET['com_id'] : 0;
31}
32
33
34if ('system' == $xoopsModule->getVar('dirname')) {
35 $comment_handler =& xoops_gethandler('comment');
36 $comment =& $comment_handler->get($com_id);
37 $module_handler =& xoops_gethandler('module');
38 $module =& $module_handler->get($comment->getVar('com_modid'));
39 $comment_config = $module->getInfo('comments');
40 $com_modid = $module->getVar('mid');
41 $redirect_page = XOOPS_URL.'/modules/system/admin.php?fct=comments&amp;com_modid='.$com_modid.'&amp;com_itemid';
42 $moddir = $module->getVar('dirname');
43 unset($comment);
44} else {
45 if (XOOPS_COMMENT_APPROVENONE == $xoopsModuleConfig['com_rule']) {
46 exit();
47 }
48 $comment_config = $xoopsModule->getInfo('comments');
49 $com_modid = $xoopsModule->getVar('mid');
50 $redirect_page = $comment_config['pageName'].'?';
51 $comment_confirm_extra = [];
52 if (isset($comment_config['extraParams']) && is_array($comment_config['extraParams'])) {
53 foreach ($comment_config['extraParams'] as $extra_param) {
54 if (isset(${$extra_param})) {
55 $redirect_page .= $extra_param.'='.${$extra_param}.'&amp;';
56
57 // for the confirmation page
58 $comment_confirm_extra [$extra_param] = ${$extra_param};
59 } elseif (isset($_GET[$extra_param])) {
60 $redirect_page .= $extra_param.'='.$_GET[$extra_param].'&amp;';
61
62 // for the confirmation page
63 $comment_confirm_extra [$extra_param] = $_GET[$extra_param];
64 }
65 }
66 }
67 $redirect_page .= $comment_config['itemName'];
68 $moddir = $xoopsModule->getVar('dirname');
69}
70
71$accesserror = false;
72
73if (!is_object($xoopsUser)) {
74 $accesserror = true;
75} else if (!$xoopsUser->isAdmin($com_modid)) {
76 $sysperm_handler =& xoops_gethandler('groupperm');
77 if (!$sysperm_handler->checkRight('system_admin', LEGACY_SYSTEM_COMMENT, $xoopsUser->getGroups())) {
78 $accesserror = true;
79 }
80}
81
82if (false !== $accesserror) {
83 $ref = xoops_getenv('HTTP_REFERER');
84 if ('' !== $ref) {
85 redirect_header($ref, 2, _NOPERM);
86 } else {
87 redirect_header($redirect_page.'?'.$comment_config['itemName'].'='.(int)$com_itemid, 2, _NOPERM);
88 }
89 exit();
90}
91
92$t_root =& XCube_Root::getSingleton();
93
94$t_root->mLanguageManager->loadPageTypeMessageCatalog('comment');
95
96switch ($op) {
97case 'delete_one':
98 $comment_handler = xoops_gethandler('comment');
99 $comment =& $comment_handler->get($com_id);
100 if (!$comment_handler->delete($comment)) {
101 include XOOPS_ROOT_PATH.'/header.php';
102 xoops_error(_CM_COMDELETENG.' (ID: '.$comment->getVar('com_id').')');
103 include XOOPS_ROOT_PATH.'/footer.php';
104 exit();
105 }
106
107 $com_itemid = $comment->getVar('com_itemid');
108
109 // execute updateStat callback function if set
110 if (isset($comment_config['callback']['update']) && '' !== trim($comment_config['callback']['update'])) {
111 $skip = false;
112 if (!function_exists($comment_config['callback']['update'])) {
113 if (isset($comment_config['callbackFile'])) {
114 $callbackfile = trim($comment_config['callbackFile']);
115 if ('' != $callbackfile && file_exists(XOOPS_ROOT_PATH . '/modules/' . $moddir . '/' . $callbackfile)) {
116 include_once XOOPS_ROOT_PATH.'/modules/'.$moddir.'/'.$callbackfile;
117 }
118 if (!function_exists($comment_config['callback']['update'])) {
119 $skip = true;
120 }
121 } else {
122 $skip = true;
123 }
124 }
125 if (!$skip) {
126 $criteria = new CriteriaCompo(new Criteria('com_modid', $com_modid));
127 $criteria->add(new Criteria('com_itemid', $com_itemid));
128 $criteria->add(new Criteria('com_status', XOOPS_COMMENT_ACTIVE));
129 $comment_count = $comment_handler->getCount($criteria);
130 $comment_config['callback']['update']($com_itemid, $comment_count);
131 }
132 }
133
134 // update user posts if its not an anonymous post
135 if (0 !== $comment->getVar('com_uid')) {
136 $member_handler =& xoops_gethandler('member');
137 $com_poster =& $member_handler->getUser($comment->getVar('com_uid'));
138 if (is_object($com_poster)) {
139 $member_handler->updateUserByField($com_poster, 'posts', $com_poster->getVar('posts') - 1);
140 }
141 }
142
143 // get all comments posted later within the same thread
144 $thread_comments =& $comment_handler->getThread($comment->getVar('com_rootid'), $com_id);
145
146 include_once XOOPS_ROOT_PATH.'/class/tree.php';
147 $xot = new XoopsObjectTree($thread_comments, 'com_id', 'com_pid', 'com_rootid');
148
149 $child_comments =& $xot->getFirstChild($com_id);
150
151 // now set new parent ID for direct child comments
152 $new_pid = $comment->getVar('com_pid');
153 $errs = [];
154 foreach (array_keys($child_comments) as $i) {
155 $child_comments[$i]->setVar('com_pid', $new_pid);
156 // if the deleted comment is a root comment, need to change root id to own id
157 if (false !== $comment->isRoot()) {
158 $new_rootid = $child_comments[$i]->getVar('com_id');
159 $child_comments[$i]->setVar('com_rootid', $child_comments[$i]->getVar('com_id'));
160 if (!$comment_handler->insert($child_comments[$i])) {
161 $errs[] = 'Could not change comment parent ID from <b>'.$com_id.'</b> to <b>'.$new_pid.'</b>. (ID: '.$new_rootid.')';
162 } else {
163 // need to change root id for all its child comments as well
164 $c_child_comments =& $xot->getAllChild($new_rootid);
165 $cc_count = is_countable($c_child_comments) ? count($c_child_comments) : 0;
166 foreach (array_keys($c_child_comments) as $j) {
167 $c_child_comments[$j]->setVar('com_rootid', $new_rootid);
168 if (!$comment_handler->insert($c_child_comments[$j])) {
169 $errs[] = 'Could not change comment root ID from <b>'.$com_id.'</b> to <b>'.$new_rootid.'</b>.';
170 }
171 }
172 }
173 } else {
174 if (!$comment_handler->insert($child_comments[$i])) {
175 $errs[] = 'Could not change comment parent ID from <b>'.$com_id.'</b> to <b>'.$new_pid.'</b>.';
176 }
177 }
178 }
179 if (count($errs) > 0) {
180 include XOOPS_ROOT_PATH.'/header.php';
181 xoops_error($errs);
182 include XOOPS_ROOT_PATH.'/footer.php';
183 exit();
184 }
185 redirect_header($redirect_page.'='.$com_itemid.'&amp;com_order='.$com_order.'&amp;com_mode='.$com_mode, 1, _CM_COMDELETED);
186 break;
187
188case 'delete_all':
189 $comment_handler = xoops_gethandler('comment');
190 $comment =& $comment_handler->get($com_id);
191 $com_rootid = $comment->getVar('com_rootid');
192
193 // get all comments posted later within the same thread
194 $thread_comments =& $comment_handler->getThread($com_rootid, $com_id);
195
196 // construct a comment tree
197 include_once XOOPS_ROOT_PATH.'/class/tree.php';
198 $xot = new XoopsObjectTree($thread_comments, 'com_id', 'com_pid', 'com_rootid');
199 $child_comments =& $xot->getAllChild($com_id);
200 // add itself here
201 $child_comments[$com_id] =& $comment;
202 $msgs = [];
203 $deleted_num = [];
204 $member_handler =& xoops_gethandler('member');
205 foreach (array_keys($child_comments) as $i) {
206 if (!$comment_handler->delete($child_comments[$i])) {
207 $msgs[] = _CM_COMDELETENG.' (ID: '.$child_comments[$i]->getVar('com_id').')';
208 } else {
209 $msgs[] = _CM_COMDELETED.' (ID: '.$child_comments[$i]->getVar('com_id').')';
210 // store poster ID and deleted post number into array for later use
211 $poster_id = $child_comments[$i]->getVar('com_uid');
212 if ($poster_id > 0) {
213 $deleted_num[$poster_id] = !isset($deleted_num[$poster_id]) ? 1 : ($deleted_num[$poster_id] + 1);
214 }
215 }
216 }
217 foreach ($deleted_num as $user_id => $post_num) {
218 // update user posts
219 $com_poster = $member_handler->getUser($user_id);
220 if (is_object($com_poster)) {
221 $member_handler->updateUserByField($com_poster, 'posts', $com_poster->getVar('posts') - $post_num);
222 }
223 }
224
225 $com_itemid = $comment->getVar('com_itemid');
226
227 // execute updateStat callback function if set
228 if (isset($comment_config['callback']['update']) && '' !== trim($comment_config['callback']['update'])) {
229 $skip = false;
230 if (!function_exists($comment_config['callback']['update'])) {
231 if (isset($comment_config['callbackFile'])) {
232 $callbackfile = trim($comment_config['callbackFile']);
233 if ('' !== $callbackfile && file_exists(XOOPS_ROOT_PATH . '/modules/' . $moddir . '/' . $callbackfile)) {
234 include_once XOOPS_ROOT_PATH.'/modules/'.$moddir.'/'.$callbackfile;
235 }
236 if (!function_exists($comment_config['callback']['update'])) {
237 $skip = true;
238 }
239 } else {
240 $skip = true;
241 }
242 }
243 if (!$skip) {
244 $criteria = new CriteriaCompo(new Criteria('com_modid', $com_modid));
245 $criteria->add(new Criteria('com_itemid', $com_itemid));
246 $criteria->add(new Criteria('com_status', XOOPS_COMMENT_ACTIVE));
247 $comment_count = $comment_handler->getCount($criteria);
248 $comment_config['callback']['update']($com_itemid, $comment_count);
249 }
250 }
251
252 include XOOPS_ROOT_PATH.'/header.php';
253
254 xoops_result($msgs);
255 echo '<br><a href="'.$redirect_page.'='.$com_itemid.'&amp;com_order='.$com_order.'&amp;com_mode='.$com_mode.'">'._BACK.'</a>';
256 include XOOPS_ROOT_PATH.'/footer.php';
257 break;
258
259case 'delete':
260default:
261 include XOOPS_ROOT_PATH.'/header.php';
262 $comment_confirm = ['com_id' => $com_id, 'com_mode' => $com_mode, 'com_order' => $com_order, 'op' => [_CM_DELETEONE => 'delete_one', _CM_DELETEALL => 'delete_all']];
263 if (!empty($comment_confirm_extra) && is_array($comment_confirm_extra)) {
264 $comment_confirm = $comment_confirm + $comment_confirm_extra;
265 }
266 xoops_confirm($comment_confirm, 'comment_delete.php', _CM_DELETESELECT);
267 include XOOPS_ROOT_PATH.'/footer.php';
268 break;
269}