PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/ww.plugins/comments/frontend/show-comments.php

http://kv-webme.googlecode.com/
PHP | 176 lines | 137 code | 5 blank | 34 comment | 25 complexity | 4807ba76c144c95a6e5e79fdb74190b8 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, BSD-3-Clause, BSD-2-Clause, Apache-2.0, MIT, LGPL-2.1
  1. <?php
  2. /**
  3. * Displays validated comments
  4. *
  5. * PHP Version 5
  6. *
  7. * @category CommentsPlugin
  8. * @package WebworksWebme
  9. * @subpackage CommentsPlugin
  10. * @author Belinda Hamilton <bhamilton@webworks.ie>
  11. * @author Kae Verens <kae@kvsites.ie>
  12. * @license GPL Version 2
  13. * @link www.kvweb.me
  14. **/
  15. require_once SCRIPTBASE.'ww.incs/recaptcha.php';
  16. /**
  17. * The main display function
  18. *
  19. * @param Object $page Page Info
  20. *
  21. * @return $html The comments and an add comment form
  22. **/
  23. function Comments_displayComments($page) {
  24. if (!$GLOBALS['access_allowed']) {
  25. return '';
  26. }
  27. // { order of display
  28. $commentboxfirst=isset($page->vars['comments_show_box_at_top'])
  29. && $page->vars['comments_show_box_at_top'];
  30. // }
  31. // { get list of existing comments
  32. $hideComments=isset($page->vars['hide_comments'])
  33. && $page->vars['hide_comments'];
  34. if ($hideComments) {
  35. if (count(@$_SESSION['comment_ids'])) {
  36. $query='select * from comments where objectid='.$page->id.' and id in ('
  37. .join(', ', $_SESSION['comment_ids']).')';
  38. }
  39. else {
  40. $query = '';
  41. }
  42. }
  43. else {
  44. if (count(@$_SESSION['comment_ids'])) {
  45. $query='select * from comments where objectid='.$page->id
  46. .' and (isvalid=1 or id in ('.join(', ', $_SESSION['comment_ids']).'))';
  47. }
  48. else {
  49. $query = 'select * from comments where objectid='.$page->id
  50. .' and isvalid=1';
  51. }
  52. }
  53. if ($query) {
  54. $sql=$query.' order by cdate '.($commentboxfirst?'desc':'asc');
  55. $md5=md5($sql);
  56. $comments=Core_cacheLoad('comments', $md5);
  57. if ($comments===false) {
  58. $comments=dbAll($sql);
  59. Core_cacheSave('comments', $md5, $comments);
  60. }
  61. }
  62. // }
  63. $clist='';
  64. if (count($comments)) {
  65. $clist = '<div id="start-comments" class="comments-list"><a name="comments"></a>'
  66. .'<strong>Comments</strong>';
  67. foreach ($comments as $comment) {
  68. $id = $comment['id'];
  69. $datetime = $comment['cdate'];
  70. $allowedToEdit=Core_isAdmin() || (
  71. (isset($_SESSION['comment_ids'])&&is_array($_SESSION['comment_ids']))
  72. && in_array($id, $_SESSION['comment_ids'], false)
  73. );
  74. $clist.= '<div class="comment-wrapper';
  75. if ($allowedToEdit) {
  76. $clist.= ' comment-editable" '
  77. .'cdate="'.$datetime.'" comment="'
  78. .htmlspecialchars($comment['comment']).'"';
  79. }
  80. else {
  81. $clist.= '" ';
  82. }
  83. $clist.='id="comment-wrapper-'.$comment['id'].'"'
  84. .'><a name="comments-'.$id.'"></a>'
  85. .'<div class="comment-info" id="comment-info-'.$id.'">Posted by ';
  86. if (!empty($comment['site'])) {
  87. $clist.= '<a href="'.$comment['site'].'" target=_blank>'
  88. .htmlspecialchars($comment['name']).'</a>';
  89. }
  90. else {
  91. $clist.= htmlspecialchars($comment['name']);
  92. }
  93. $clist.= ' on '.Core_dateM2H($datetime).'</div>'
  94. .'<div id="comment-'.$id.'" class="comments-comment">'
  95. .htmlspecialchars($comment['comment'])
  96. .'</div></div>';
  97. }
  98. $clist.='</div>';
  99. }
  100. else {
  101. $clist.= '';
  102. }
  103. // { get comment box HTML
  104. $allowComments=Core_cacheLoad('comments', 'allow-'.$page->id, -1);
  105. if ($allowComments===-1) {
  106. $allowComments=dbOne(
  107. 'select value from page_vars where name="allow_comments" and page_id='
  108. .$page->id,
  109. 'value'
  110. );
  111. Core_cacheSave('comments', 'allow-'.$page->id, $allowComments);
  112. }
  113. $cbhtml=$allowComments=='on'?Comments_showCommentForm($page->id):'';
  114. if ($allowComments=='on') {
  115. WW_addScript('comments/frontend/comments-frontend.js');
  116. $cbhtml.='<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/'
  117. .'jquery.validate.min.js"></script>';
  118. }
  119. WW_addCSS('/ww.plugins/comments/frontend/comments.css');
  120. // }
  121. return $commentboxfirst?$cbhtml.$clist:$clist.$cbhtml;
  122. }
  123. /**
  124. * Shows the add comment form
  125. *
  126. * @param int $pageID The page that the comment is to be displayed on
  127. *
  128. * @return $display The form
  129. *
  130. **/
  131. function Comments_showCommentForm($pageID) {
  132. if (isset($_SESSION['userdata'])) {
  133. $userID =$_SESSION['userdata']['id'];
  134. $user=dbRow('select name, email from user_accounts where id = '.$userID);
  135. }
  136. $noCaptchas=(int)dbOne(
  137. 'select value from site_vars where name = "comments_no_captchas"',
  138. 'value'
  139. );
  140. $display= '<form id="comment-form" class="comments-form" method="post"
  141. action="javascript:comments_check_captcha();">';
  142. $display.= '<strong>Add Comment</strong>';
  143. $display.= '<input type="hidden" name="page" id="comments-page-id"
  144. value="'.$pageID.'" />';
  145. $display.='<table class="comments-form-table"><tr class="comments-name">'
  146. .'<th>Name</th><td><input id="comments-name-input" name="name" ';
  147. if (isset($user)) {
  148. $display.= ' value="'.htmlspecialchars($user['name']).'"';
  149. }
  150. $display.= ' /></td></tr>';
  151. $display.= '<tr class="comments-email"><th>Email</th>';
  152. $display.= '<td><input id="comments-email-input" name="email"';
  153. if (isset($user)) {
  154. $display.= ' value="'.htmlspecialchars($user['email']).'"';
  155. }
  156. $display.= ' /></td></tr>'
  157. .'<tr class="comments-url"><th>Website</th>'
  158. .'<td><input id="site" name="comments-site-input" /></td></tr>'
  159. .'<tr class="comments-comment"><th>Comment</th><td>'
  160. .'<textarea id="comments-comment-input" name="comment"></textarea></td>'
  161. .'</tr>';
  162. if (!$noCaptchas) {
  163. $display.='<tr><td colspan="2"><div id="captcha" class="comments_captcha">'
  164. .Recaptcha_getHTML()
  165. .'</div></td></tr>';
  166. }
  167. $display.='<tr class="comments-submit-comment"><th>&nbsp;</th><td>'
  168. .'<input type="submit" id="submit" value="Submit Comment" /></td></tr>'
  169. .'</table></form><script defer="defer">comments_noCaptchas='.$noCaptchas.';</script>';
  170. return $display;
  171. }