XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
comment_delete.php
Go to the documentation of this file.
1 <?php
20 if (!defined('XOOPS_ROOT_PATH') || !is_object($xoopsModule)) {
21  die('Restricted access');
22 }
23 
24 include_once $GLOBALS['xoops']->path('include/comment_constants.php');
25 
26 $op = 'delete';
27 if (!empty($_POST)) {
28  extract($_POST);
29  $com_mode = isset($com_mode) ? htmlspecialchars(trim($com_mode), ENT_QUOTES) : 'flat';
31  $com_id = isset($com_id) ? intval($com_id) : 0;
32 } else {
33  $com_mode = isset($_GET['com_mode']) ? htmlspecialchars(trim($_GET['com_mode']), ENT_QUOTES) : 'flat';
34  $com_order = isset($_GET['com_order']) ? intval($_GET['com_order']) : XOOPS_COMMENT_OLD1ST;
35  $com_id = isset($_GET['com_id']) ? intval($_GET['com_id']) : 0;
36 
37 }
38 
39 if ('system' == $xoopsModule->getVar('dirname')) {
40  $comment_handler =& xoops_gethandler('comment');
42  $module_handler =& xoops_gethandler('module');
43  $module =& $module_handler->get($comment->getVar('com_modid'));
44  $comment_config = $module->getInfo('comments');
45  $com_modid = $module->getVar('mid');
46  $redirect_page = XOOPS_URL . '/modules/system/admin.php?fct=comments&amp;com_modid=' . $com_modid . '&amp;com_itemid';
47  $moddir = $module->getVar('dirname');
48  unset($comment);
49 } else {
50  if (XOOPS_COMMENT_APPROVENONE == $xoopsModuleConfig['com_rule']) {
51  exit();
52  }
53  $comment_config = $xoopsModule->getInfo('comments');
54  $com_modid = $xoopsModule->getVar('mid');
55  $redirect_page = $comment_config['pageName'] . '?';
57  if (isset($comment_config['extraParams']) && is_array($comment_config['extraParams'])) {
58  foreach ($comment_config['extraParams'] as $extra_param) {
59  if (isset(${$extra_param})) {
60  $redirect_page .= $extra_param . '=' . ${$extra_param} . '&amp;';
61 
62  // for the confirmation page
63  $comment_confirm_extra[$extra_param] = ${$extra_param};
64  } elseif (isset($_GET[$extra_param])) {
65  $redirect_page .= $extra_param . '=' . $_GET[$extra_param] . '&amp;';
66 
67  // for the confirmation page
68  $comment_confirm_extra[$extra_param] = $_GET[$extra_param];
69  }
70  }
71  }
72  $redirect_page .= $comment_config['itemName'];
73  $moddir = $xoopsModule->getVar('dirname');
74 }
75 
76 $accesserror = false;
77 if (!is_object($xoopsUser)) {
78  $accesserror = true;
79 } else {
80  if (!$xoopsUser->isAdmin($com_modid)) {
81  include_once $GLOBALS['xoops']->path('modules/system/constants.php');
82  $sysperm_handler =& xoops_gethandler('groupperm');
83  if (!$sysperm_handler->checkRight('system_admin', XOOPS_SYSTEM_COMMENT, $xoopsUser->getGroups())) {
84  $accesserror = true;
85  }
86  }
87 }
88 
89 if (false != $accesserror) {
90  $ref = xoops_getenv('HTTP_REFERER');
91  if ($ref != '') {
92  redirect_header($ref, 2, _NOPERM);
93  } else {
94  redirect_header($redirect_page . '?' . $comment_config['itemName'] . '=' . intval($com_itemid), 2, _NOPERM);
95  }
96  exit();
97 }
98 
99 xoops_loadLanguage('comment');
100 switch ($op) {
101  case 'delete_one':
102  $comment_handler =& xoops_gethandler('comment');
104  if (!$comment_handler->delete($comment)) {
105  include_once $GLOBALS['xoops']->path('header.php');
106  xoops_error(_CM_COMDELETENG . ' (ID: ' . $comment->getVar('com_id') . ')');
107  include_once $GLOBALS['xoops']->path('footer');
108  exit();
109  }
110 
111  $com_itemid = $comment->getVar('com_itemid');
112  // execute updateStat callback function if set
113  if (isset($comment_config['callback']['update']) && trim($comment_config['callback']['update']) != '') {
114  $skip = false;
115  if (!function_exists($comment_config['callback']['update'])) {
116  if (isset($comment_config['callbackFile'])) {
117  $callbackfile = trim($comment_config['callbackFile']);
118  if ($callbackfile != '' && file_exists($file = $GLOBALS['xoops']->path('modules/' . $moddir . '/' . $callbackfile))) {
119  include_once $file;
120  }
121  if (!function_exists($comment_config['callback']['update'])) {
122  $skip = true;
123  }
124  } else {
125  $skip = true;
126  }
127  }
128  if (! $skip) {
129  $criteria = new CriteriaCompo(new Criteria('com_modid', $com_modid));
130  $criteria->add(new Criteria('com_itemid', $com_itemid));
131  $criteria->add(new Criteria('com_status', XOOPS_COMMENT_ACTIVE));
132  $comment_count = $comment_handler->getCount($criteria);
133  $comment_config['callback']['update']($com_itemid, $comment_count);
134  }
135  }
136 
137  // update user posts if its not an anonymous post
138  if ($comment->getVar('com_uid') != 0) {
139  $member_handler =& xoops_gethandler('member');
140  $com_poster =& $member_handler->getUser($comment->getVar('com_uid'));
141  if (is_object($com_poster)) {
142  $member_handler->updateUserByField($com_poster, 'posts', $com_poster->getVar('posts') - 1);
143  }
144  }
145 
146  // get all comments posted later within the same thread
147  $thread_comments =& $comment_handler->getThread($comment->getVar('com_rootid'), $com_id);
148  include_once $GLOBALS['xoops']->path('class/tree.php');
149  $xot = new XoopsObjectTree($thread_comments, 'com_id', 'com_pid', 'com_rootid');
150  $child_comments =& $xot->getFirstChild($com_id);
151  // now set new parent ID for direct child comments
152  $new_pid = $comment->getVar('com_pid');
153  $errs = array();
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 <strong>' . $com_id . '</strong> to <strong>' . $new_pid . '</strong>. (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 = count($c_child_comments);
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 <strong>' . $com_id . '</strong> to <strong>' . $new_rootid . '</strong>.';
170  }
171  }
172  }
173  } else {
174  if (!$comment_handler->insert($child_comments[$i])) {
175  $errs[] = 'Could not change comment parent ID from <strong>' . $com_id . '</strong> to <strong>' . $new_pid . '</strong>.';
176  }
177  }
178  }
179  if (count($errs) > 0) {
180  include_once $GLOBALS['xoops']->path('header.php');
181  xoops_error($errs);
182  include_once $GLOBALS['xoops']->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 
188  case 'delete_all':
189  $comment_handler = xoops_gethandler('comment');
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 $GLOBALS['xoops']->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 = array();
203  $deleted_num = array();
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  // execute updateStat callback function if set
227  if (isset($comment_config['callback']['update']) && trim($comment_config['callback']['update']) != '') {
228  $skip = false;
229  if (!function_exists($comment_config['callback']['update'])) {
230  if (isset($comment_config['callbackFile'])) {
231  $callbackfile = trim($comment_config['callbackFile']);
232  if ($callbackfile != '' && file_exists($file = $GLOBALS['xoops']->path('modules/' . $moddir . '/' . $callbackfile))) {
233  include_once $file;
234  }
235  if (!function_exists($comment_config['callback']['update'])) {
236  $skip = true;
237  }
238  } else {
239  $skip = true;
240  }
241  }
242  if (!$skip) {
243  $criteria = new CriteriaCompo(new Criteria('com_modid', $com_modid));
244  $criteria->add(new Criteria('com_itemid', $com_itemid));
245  $criteria->add(new Criteria('com_status', XOOPS_COMMENT_ACTIVE));
246  $comment_count = $comment_handler->getCount($criteria);
247  $comment_config['callback']['update']($com_itemid, $comment_count);
248  }
249  }
250  include_once $GLOBALS['xoops']->path('header.php');
251  xoops_result($msgs);
252  echo '<br /><a href="' . $redirect_page . '=' . $com_itemid . '&amp;com_order=' . $com_order . '&amp;com_mode=' . $com_mode . '">' . _BACK . '</a>';
253  include_once $GLOBALS['xoops']->path('footer.php');
254  break;
255 
256  case 'delete':
257  default:
258  include_once $GLOBALS['xoops']->path('header.php');
259  $comment_confirm = array(
260  'com_id' => $com_id ,
261  'com_mode' => $com_mode ,
262  'com_order' => $com_order ,
263  'op' => array(
264  _CM_DELETEONE => 'delete_one' ,
265  _CM_DELETEALL => 'delete_all'));
266  if (!empty($comment_confirm_extra) && is_array($comment_confirm_extra)) {
267  $comment_confirm = $comment_confirm + $comment_confirm_extra;
268  }
269  xoops_confirm($comment_confirm, 'comment_delete.php', _CM_DELETESELECT);
270  include_once $GLOBALS['xoops']->path('footer.php');
271  break;
272 }
273 
274 ?>