PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/rmcommon/tags/rmcommon-2.0.95/post_comment.php

http://bitcero-modules.googlecode.com/
PHP | 320 lines | 221 code | 73 blank | 26 comment | 47 complexity | 3cec4cc2267482d5dd51233eb1db363b MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // $Id: post_comment.php 511 2010-07-26 06:08:44Z i.bitcero $
  3. // --------------------------------------------------------------
  4. // Red México Common Utilities
  5. // A framework for Red México Modules
  6. // Author: Eduardo Cortés <i.bitcero@gmail.com>
  7. // Email: i.bitcero@gmail.com
  8. // License: GPL 2.0
  9. // --------------------------------------------------------------
  10. include '../../mainfile.php';
  11. $action = rmc_server_var($_REQUEST, 'action', '');
  12. /**
  13. * This file handle comments from Common Utilties
  14. */
  15. $rmc_config = RMFunctions::configs();
  16. if (!$rmc_config['enable_comments']){
  17. redirect_header(rmc_server_var($_REQUEST, 'comment_url', XOOPS_URL), 1, __('Sorry, comments has been disabled by administrator', 'rmcommon'));
  18. die();
  19. }
  20. if ($action=='save'){
  21. if (!$xoopsSecurity->checkReferer()){
  22. redirect_header(XOOPS_URL, 2, __('You are not allowed to do this action!', 'rmcommon'));
  23. die();
  24. }
  25. // Check if user is a Registered User
  26. if(!$xoopsUser){
  27. $name = rmc_server_var($_POST, 'comment_name', '');
  28. $email = rmc_server_var($_POST, 'comment_email', '');
  29. $url = rmc_server_var($_POST, 'comment_url', '');
  30. $xuid = 0;
  31. } else {
  32. $name = $xoopsUser->getVar('uname');
  33. $email = $xoopsUser->getVar('email');
  34. $url = $xoopsUser->getVar('url');
  35. $xuid = $xoopsUser->uid();
  36. }
  37. // Check uri
  38. $uri = urldecode(rmc_server_var($_POST, 'uri', ''));
  39. if (trim($uri)==''){
  40. header('loaction: '.XOOPS_URL);
  41. die();
  42. }
  43. if ($name=='' || $email==''){
  44. redirect_header($uri, 2, __('You must provide your name and email in order to can post comments','rmcommon'));
  45. die();
  46. }
  47. if (!$xoopsUser && !$rmc_config['anonymous_comments']){
  48. redirect_header($uri, 2, __('Sorry, you are not allowed to post comments!', 'rmcommon'));
  49. die();
  50. }
  51. // Check params
  52. $params = rmc_server_var($_POST, 'params', '');
  53. if (trim($params)==''){
  54. redirect_header($uri, 2, __('There are not params to save!','rmcommon'));
  55. die();
  56. }
  57. // Object type
  58. $type = rmc_server_var($_POST, 'type', '');
  59. if (trim($type)==''){
  60. redirect_header($uri, 2, __('Object type missing!','rmcommon'));
  61. die();
  62. }
  63. // Object name
  64. $object = strtolower(rmc_server_var($_POST, 'object', ''));
  65. if (trim($object)==''){
  66. redirect_header($uri, 2, __('Object name missing!','rmcommon'));
  67. die();
  68. }
  69. // Text
  70. $text = rmc_server_var($_POST, 'comment_text', '');
  71. if (trim($text)==''){
  72. redirect_header($uri, 2, __('You must write a message!','rmcommon'));
  73. die();
  74. }
  75. RMEvents::get()->run_event('rmcommon.comment.postdata', $uri);
  76. // Save comment user
  77. $db = Database::getInstance();
  78. if($xoopsUser){
  79. $sql = "SELECT id_user FROM ".$db->prefix("rmc_comusers")." WHERE xuid=".$xoopsUser->uid();
  80. } else {
  81. $sql = "SELECT id_user FROM ".$db->prefix("rmc_comusers")." WHERE email='$email'";
  82. }
  83. $result = $db->query($sql);
  84. list($uid) = $db->fetchRow($result);
  85. if ($uid<=0){
  86. $db->queryF("INSERT INTO ".$db->prefix("rmc_comusers")." (`xuid`,`name`,`email`,`url`) VALUES ('$xuid','$name','$email','$url')");
  87. $uid = $db->getInsertId();
  88. } else {
  89. $db->queryF("UPDATE ".$db->prefix("rmc_comusers")." SET `name`='$name',`email`='$email',`url`='$url' WHERE id_user='$uid'");
  90. }
  91. $comment = new RMComment();
  92. $comment->setVar('id_obj', $object);
  93. $comment->setVar('type', $type);
  94. $comment->setVar('parent', isset($parent) ? $parent : 0);
  95. $comment->setVar('params', $params);
  96. $comment->setVar('content', $text);
  97. $comment->setVar('user', $uid);
  98. $comment->setVar('ip', $_SERVER['REMOTE_ADDR']);
  99. $comment->setVar('posted', time());
  100. // Check if comment must be approved
  101. if ($xoopsUser && $rmc_config['approve_reg_coms']){
  102. $comment->setVar('status', 'approved');
  103. } elseif(!$xoopsUser && $rmc_config['approve_anon_coms']){
  104. $comment->setVar('status', 'approved');
  105. } elseif($xoopsUser && $xoopsUser->isAdmin()){
  106. $comment->setVar('status', 'approved');
  107. }
  108. if (!$comment->save()){
  109. redirect_header($uri, 1, __('Comment could not be posted!','rmcommon').'<br />'.$comment->errors());
  110. }
  111. if ($xoopsUser) $xoopsUser->incrementPost();
  112. RMEvents::get()->run_event('rmcommon.comment.saved', $comment, $uri);
  113. // Update comments number if object accepts this functionallity
  114. if (is_file(XOOPS_ROOT_PATH.'/modules/'.$object.'/class/'.$object.'controller.php')){
  115. include_once XOOPS_ROOT_PATH.'/modules/'.$object.'/class/'.$object.'controller.php';
  116. $class = ucfirst($object).'Controller';
  117. if(class_exists($class)){
  118. $controller = new $class();
  119. if (method_exists($controller, 'increment_comments_number')){
  120. $controller->increment_comments_number($comment);
  121. }
  122. }
  123. }
  124. redirect_header($uri.'#comment-'.$comment->id(), 1, __('Comment posted successfully!','rmcommon'));
  125. } elseif ($action=='edit') {
  126. if (rmc_server_var($_GET, 'ret', '')==''){
  127. redirect_header(XOOPS_URL, 2, __('Invalid operation','rmcommon'));
  128. die();
  129. }
  130. // Check if user is allowed to edit this comment
  131. if (!$xoopsUser){
  132. redirect_header(rmc_server_var($_REQUEST, 'comment_url', XOOPS_URL), 1, __('You are not allowed to edit this comment!', 'rmcommon'));
  133. die();
  134. }
  135. $id = rmc_server_var($_GET, 'id', 0);
  136. if ($id<=0){
  137. redirect_header(rmc_server_var($_REQUEST, 'ret', XOOPS_URL), 1, __('Please specify a comment', 'rmcommon'));
  138. die();
  139. }
  140. $comment = new RMComment($id);
  141. if ($comment->isNew()){
  142. redirect_header(rmc_server_var($_REQUEST, 'ret', XOOPS_URL), 1, __('Specified comment does not exist!', 'rmcommon'));
  143. die();
  144. }
  145. // Check if user is owner
  146. $editor = new RMCommentUser($comment->getVar('user'));
  147. if ($xoopsUser->uid()!=$editor->getVar('xuid') && !$xoopsUser->isAdmin($comment->getVar('id_obj'))){
  148. redirect_header(rmc_server_var($_REQUEST, 'ret', XOOPS_URL), 1, __('You are not allowed to edit this comment!', 'rmcommon'));
  149. die();
  150. }
  151. include '../../header.php';
  152. $cpath = XOOPS_ROOT_PATH.'/modules/'.$comment->getVar('id_obj').'/class/'.$comment->getVar('id_obj').'controller.php';
  153. if(is_file($cpath)){
  154. include $cpath;
  155. $class = ucfirst($comment->getVar('id_obj')).'Controller';
  156. $controller = new $class();
  157. }
  158. $form = new RMForm(__('Edit Comment', 'rmcommon'), 'editComment', 'post_comment.php');
  159. $form->addElement(new RMFormLabel(__('In reply to', 'rmcommon'), $controller ? $controller->get_item($comment->getVar('params'), $comment):''));
  160. $form->addElement(new RMFormLabel(__('Posted date','rmcommon'), formatTimestamp($comment->getVar('posted'), 'mysql')));
  161. $form->addElement(new RMFormLabel(__('Module','rmcommon'), $comment->getVar('id_obj')));
  162. if($xoopsUser->isAdmin()){
  163. $user = new RMCommentUser($comment->getVar('user'));
  164. $ele = new RMFormUser(__('Poster','rmcommon'), 'user', false, $user->getVar('xuid')>0 ? $user->getVar('xuid') : 0);
  165. $form->addElement($ele);
  166. }
  167. if($xoopsUser->isAdmin($comment->getVAr('id_obj'))){
  168. $ele = new RMFormRadio(__('Status','rmcommon'), 'status', 1, 0, 2);
  169. $ele->addOption(__('Approved', 'rmcommon'), 'approved', $comment->getVar('status')=='approved'?1:0);
  170. $ele->addOption(__('Unapproved', 'rmcommon'), 'waiting', $comment->getVar('status')=='waiting'?1:0);
  171. $form->addElement($ele);
  172. }
  173. $form->addElement(new RMFormTextArea(__('Content','rmcommon'), 'content', null, null, $comment->getVar('content','e'),'100%','150px'), true);
  174. $form->addElement(new RMFormHidden('action', 'saveedit'));
  175. $ele = new RMFormButtonGroup();
  176. $ele->addButton('sbt', __('Update Comment','rmcommon'), 'submit');
  177. $ele->addButton('cancel', __('Cancel','rmcommon'), 'button', 'onclick="history.go(-1);"');
  178. $form->addElement($ele);
  179. $form->addElement(new RMFormHidden('ret', rmc_server_var($_GET, 'ret', XOOPS_URL)));
  180. $form->addElement(new RMFormHidden('id', $id));
  181. // Event to modify or add new elements to comments form
  182. $form = RMEvents::get()->run_event('rmcommon.edit.comment.form', $form);
  183. $form->display();
  184. include '../../footer.php';
  185. } elseif($action=='saveedit'){
  186. $ret = rmc_server_var($_POST,'ret','');
  187. $id = rmc_server_var($_POST,'id',0);
  188. $page = rmc_server_var($_POST, 'page', 1);
  189. $filter = rmc_server_var($_POST, 'filter', '');
  190. $w = rmc_server_var($_POST, 'w', '1');
  191. if ($ret==''){
  192. redirect_header(XOOPS_URL, 1, __('Invalid Operation','rmcommon'));
  193. die();
  194. }
  195. // Check if user is allowed to edit this comment
  196. if (!$xoopsUser){
  197. redirect_header($ret, 1, __('You are not allowed to edit this comment!', 'rmcommon'));
  198. die();
  199. }
  200. if(!$xoopsSecurity->check()){
  201. redirect_header($ret, 1, __('You are not allowed to edit this comment!','rmcommon'));
  202. die();
  203. }
  204. if ($id<=0){
  205. redirect_header(XOOPS_URL, 1, __('Please specify a comment','rmcommon'));
  206. die();
  207. }
  208. $comment = new RMComment($id);
  209. if($comment->isNew()){
  210. redirect_header(XOOPS_URL, 1, __('Specified comment does not exist!','rmcommon'));
  211. die();
  212. }
  213. $status = $xoopsUser->isAdmin($comment->getVar('id_obj')) ? rmc_server_var($_POST, 'status', $comment->getVar('status')) : $comment->getVar('status');
  214. $status = $status=='approved'?$status:'unapproved';
  215. $user = $xoopsUser->isAdmin($comment->getVar('id_obj')) ? rmc_server_var($_POST, 'user', $xoopsUser->getVar('uid')) : $xoopsUser->getVar('uid');
  216. $content = rmc_server_var($_POST, 'content', '');
  217. if ($content==''){
  218. redirect_header('post_comment.php?id='.$id.'&ret='.urlencode($ret).'&action=edit', 2, __('You must provide a text for comment!','rmcommon'));
  219. die();
  220. }
  221. // save basic info in comment object
  222. $comment->setVar('content', $content);
  223. $comment->setVar('status', $status);
  224. // Modify, if neccessary, the user
  225. $cuser = new RMCommentUser($comment->getVar('user'));
  226. if ($cuser->getVar('xuid')!=$user){
  227. if ($user==0){
  228. $cuser->setVar('xuid', 0);
  229. $cuser->save();
  230. } else {
  231. $xuser = new XoopsUser($user);
  232. $cuser = new RMCommentUser($xuser->getVar('email'));
  233. $cuser->setVar('name', $xuser->getVar('uname'));
  234. $cuser->setVar('email', $xuser->getVar('email'));
  235. $cuser->setVar('xuid', $user);
  236. $cuser->setVar('url', $xuser->getVar('url'));
  237. $cuser->save();
  238. }
  239. $comment->setVar('user', $cuser->id());
  240. }
  241. RMEvents::get()->run_event('rmcommon.comment.saved', $comment, $ret);
  242. if ($comment->save()){
  243. redirect_header($ret.'#comment-'.$comment->id(), 2, __('Comment updated successfully!','rmcommon'));
  244. } else {
  245. redirect_header($ret.'#comment-'.$comment->id(), 2, __('Errros ocurrs while trying to update comment!', 'rmcommon'));
  246. }
  247. }