PageRenderTime 61ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/comments.php

https://github.com/devimplode/guilddkp
PHP | 96 lines | 94 code | 2 blank | 0 comment | 23 complexity | df7d8a286c3acdeaf195258b2d71d401 MD5 | raw file
  1. <?php
  2. define('loadet', true);
  3. define('api', true);
  4. require_once(dirname(__FILE__).'/common.php');
  5. if($_POST)
  6. {
  7. if(($user->data['user_id'] != ANONYMOUS) && $user->check_auth('rank_add_comment'))
  8. {
  9. if($_POST['s']=='pc')
  10. {
  11. $comment_text=$db->sql_escape(htmlentities($in->get('m', ''),ENT_QUOTES,'UTF-8'));
  12. $comment_page=$db->sql_escape($in->get('p', ''));
  13. $comment_original_page=$db->sql_escape($in->get('op', ''));
  14. $comment_attach=$db->sql_escape($in->get('a', ''));
  15. $comment_respond=$db->sql_escape($in->get('r', 0));
  16. if($comment_text && $comment_text != '' && (str_replace(" ", "", $comment_text) != ''))
  17. {
  18. $sql = "INSERT INTO `".T_COMMENTS."` (user_id, user_name, comment_date, comment_text, comment_ranking, comment_page, comment_attach_id".(($comment_respond)?", comment_respond_to_id":"").") VALUES ('".$user->data['user_id']."', '".(($user->data['user_displayname']!='')?$user->data['user_displayname'] : $user->data['user_name'])."', '".time()."', '".$comment_text."', 0, '".$comment_page."', '".$comment_attach."'".(($comment_respond)?", '".$comment_respond."'":"").")";
  19. echo (($db->query($sql))? "Success":"Error");
  20. if($comment_page != 'comment')
  21. $cache->set('comment', 'last_id_'.$comment_page.'_'.$comment_attach, (int)$db->sql_lastid());
  22. else
  23. $cache->set('comment', 'last_id_'.$comment_original_page.'_'.$comment_attach, (int)$db->sql_lastid());
  24. }
  25. }
  26. elseif($_POST['s']=='v')
  27. {
  28. $comment_id = $in->get('i', 0);
  29. $comment_vote = $in->get('v', 0);
  30. $votes = unserialize($_COOKIE[$config->get('cookie_name').'_comment_votes']);
  31. if(isset($votes[$comment_id]))
  32. die("e.JUST_VOTED");
  33. $votes[$comment_id] = $comment_vote;
  34. $user->set_cookie('comment_votes', serialize($votes), 0);
  35. $db->query("UPDATE `".T_COMMENTS."` SET `comment_ranking` ".(($comment_vote)?'+':'-')." 1 WHERE comment_id = '".$comment_id."';");
  36. $query = $db->query("SELECT COUNT(`comment_ranking`) as ranking FROM ".T_COMMENTS." WHERE comment_id = '".$comment_id."';");
  37. $result = $db->fetch_record($query);
  38. die($result['ranking']);
  39. }
  40. }
  41. }
  42. else
  43. {
  44. if($user->check_auth('rank_read_comment'))
  45. {
  46. $last_id = $in->get('li', 0);
  47. $comment_page=$db->sql_escape($in->get('p', ''));
  48. $comment_attach=$db->sql_escape($in->get('a', ''));
  49. $comment_sort=$db->sql_escape($in->get('s', ''));
  50. $comment_sort=($comment_sort=='d')?'DESC':'ASC';
  51. $limit=$config->get($comment_page.'_limit');
  52. if(((($cache->get('comment', 'last_id_'.$comment_page.'_'.$comment_attach)) == $last_id) || (($cache->get('comment', 'last_id_comment_'.$comment_attach)) == $last_id)) && ($last_id != false))
  53. {
  54. $json = array('e'=>1);
  55. header('Content-Type: application/json; charset=utf8');
  56. print(json_encode($json));
  57. die();
  58. }
  59. $q_last_id = ($last_id) ? " AND c.comment_id > ".$last_id : "";
  60. $sql="SELECT c.*, u.user_name, u.user_displayname, u.user_icon, MD5(u.user_email) as emailHash FROM (".T_COMMENTS." c JOIN ".T_USER." u ON c.user_id = u.user_id) WHERE ( c.comment_page = '".$comment_page."' OR c.comment_page = 'comment') AND c.comment_attach_id = '".$comment_attach."'".$q_last_id." ORDER BY c.comment_date ".$comment_sort." LIMIT ".(($limit)?$limit:25).";";
  61. $comment_result = $db->query($sql);
  62. $comments_counter = 0;
  63. $comm=array();
  64. $answ=array();
  65. while($comments = $db->fetch_record($comment_result))
  66. {
  67. $last_id = ($comments['comment_id'] > $last_id)?$comments['comment_id']:$last_id;
  68. $tmp_comment = array(
  69. 'id' => $comments['comment_id'],
  70. 'u' => ($comments['user_displayname']!='')?$comments['user_displayname']:(($comments['user_name']) ? $comments['user_name'] : "Anonymous"),
  71. 'n' => ucfirst($comments['user_name']),
  72. 'i' => ($comments['user_icon'] != '')? $comments['user_icon']:"http://www.gravatar.com/avatar/".$comments['emailHash']."?d=identicon",
  73. 'm' => bbDeCode(nl2br($comments['comment_text'])),
  74. 'r' => $comments['comment_ranking'],
  75. 'D' => date('G:i', $comments['comment_date']),
  76. 'd' => date('G:i - d.m.', $comments['comment_date'])
  77. );
  78. if($comments['comment_respond_to_id']!='')
  79. {
  80. $tmp_comment['re']=$comments['comment_respond_to_id'];
  81. }
  82. $comm['data'][$tmp_comment['id']]=$tmp_comment;
  83. }
  84. $db->free_result($comment_result);
  85. $cache->set('comment', 'last_id_'.$comment_page.'_'.$comment_attach, (int)$last_id);
  86. if(array_key_exists('data', $comm))
  87. $json=array('li'=>$last_id, 'e'=>0,'d'=>$comm['data']);
  88. else
  89. $json = array('e'=>1);
  90. header('Content-Type: application/json; charset=utf8');
  91. print(json_encode($json));
  92. }
  93. }
  94. ?>