index.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. class index {
  4. protected $commentid, $modules, $siteid, $format;
  5. function __construct() {
  6. pc_base::load_app_func('global');
  7. pc_base::load_sys_class('format', '', 0);
  8. $this->commentid = isset($_GET['commentid']) && trim(urldecode($_GET['commentid'])) ? trim(urldecode($_GET['commentid'])) : $this->_show_msg(L('illegal_parameters'));
  9. if(!preg_match("/^[a-z0-9_\-]+$/i",$this->commentid)) $this->_show_msg(L('illegal_parameters'));
  10. $this->format = isset($_GET['format']) ? $_GET['format'] : '';
  11. list($this->modules, $this->contentid, $this->siteid) = decode_commentid($this->commentid);
  12. define('SITEID', $this->siteid);
  13. $this->callback = isset($_GET['callback']) ? safe_replace($_GET['callback']) : '';
  14. }
  15. public function init() {
  16. $hot = isset($_GET['hot']) && intval($_GET['hot']) ? intval($_GET['hot']) : 0;
  17. pc_base::load_sys_class('form');
  18. $commentid =& $this->commentid;
  19. $modules =& $this->modules;
  20. $contentid =& $this->contentid;
  21. $siteid =& $this->siteid;
  22. $username = param::get_cookie('_username',L('phpcms_friends'));
  23. $userid = param::get_cookie('_userid');
  24. $comment_setting_db = pc_base::load_model('comment_setting_model');
  25. $setting = $comment_setting_db->get_one(array('siteid'=>$this->siteid));
  26. //SEO
  27. $SEO = seo($siteid, '', $title);
  28. //通过API接口调用数据的标题、URL地址
  29. if (!$data = get_comment_api($commentid)) {
  30. $this->_show_msg(L('illegal_parameters'));
  31. } else {
  32. $title = $data['title'];
  33. $url = $data['url'];
  34. if (isset($data['allow_comment']) && empty($data['allow_comment'])) {
  35. showmessage(L('canot_allow_comment'));
  36. }
  37. unset($data);
  38. }
  39. if (isset($_GET['iframe'])) {
  40. if (strpos($url,APP_PATH) === 0) {
  41. $domain = APP_PATH;
  42. } else {
  43. $urls = parse_url($url);
  44. $domain = $urls['scheme'].'://'.$urls['host'].(isset($urls['port']) && !empty($urls['port']) ? ":".$urls['port'] : '').'/';
  45. }
  46. include template('comment', 'show_list');
  47. } else {
  48. include template('comment', 'list');
  49. }
  50. }
  51. public function post() {
  52. $comment = pc_base::load_app_class('comment');
  53. $id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : '';
  54. $SITE = siteinfo($this->siteid);
  55. $username = param::get_cookie('_username',$SITE['name'].L('phpcms_friends'));
  56. $userid = param::get_cookie('_userid');
  57. $comment_setting_db = pc_base::load_model('comment_setting_model');
  58. $setting = $comment_setting_db->get_one(array('siteid'=>$this->siteid));
  59. if (!empty($setting)) {
  60. //是否允许游客
  61. if (!$setting['guest']) {
  62. if (!$username || !$userid) {
  63. $this->_show_msg(L('landing_users_to_comment'), HTTP_REFERER);
  64. }
  65. }
  66. if ($setting['code']) {
  67. $session_storage = 'session_'.pc_base::load_config('system','session_storage');
  68. pc_base::load_sys_class($session_storage);
  69. session_start();
  70. $code = isset($_POST['code']) && trim($_POST['code']) ? strtolower(trim($_POST['code'])) : $this->_show_msg(L('please_enter_code'), HTTP_REFERER);
  71. if ($code != $_SESSION['code']) {
  72. $this->_show_msg(L('code_error'), HTTP_REFERER);
  73. }
  74. }
  75. }
  76. //通过API接口调用数据的标题、URL地址
  77. if (!$data = get_comment_api($this->commentid)) {
  78. $this->_show_msg(L('illegal_parameters'));
  79. } else {
  80. $title = $data['title'];
  81. $url = $data['url'];
  82. unset($data);
  83. }
  84. if (strpos($url,APP_PATH) === 0) {
  85. $domain = APP_PATH;
  86. } else {
  87. $urls = parse_url($url);
  88. $domain = $urls['scheme'].'://'.$urls['host'].(isset($urls['port']) && !empty($urls['port']) ? ":".$urls['port'] : '').'/';
  89. }
  90. $content = isset($_POST['content']) && trim($_POST['content']) ? trim($_POST['content']) : $this->_show_msg(L('please_enter_content'), HTTP_REFERER);
  91. $direction = isset($_POST['direction']) && intval($_POST['direction']) ? intval($_POST['direction']) : '';
  92. $data = array('userid'=>$userid, 'username'=>$username, 'content'=>$content, 'direction'=>$direction);
  93. $comment->add($this->commentid, $this->siteid, $data, $id, $title, $url);
  94. $this->_show_msg($comment->get_error()."<iframe width='0' id='top_src' height='0' src='$domain/js.html?200'></iframe>", (in_array($comment->msg_code, array(0,7)) ? HTTP_REFERER : ''), (in_array($comment->msg_code, array(0,7)) ? 1 : 0));
  95. }
  96. public function support() {
  97. $id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : $this->_show_msg(L('illegal_parameters'), HTTP_REFERER);
  98. unset($_GET);
  99. if (param::get_cookie('comment_'.$id)) {
  100. $this->_show_msg(L('dragonforce'), HTTP_REFERER);
  101. }
  102. $comment = pc_base::load_app_class('comment');
  103. if ($comment->support($this->commentid, $id)) {
  104. param::set_cookie('comment_'.$id, $id, SYS_TIME+3600);
  105. }
  106. $this->_show_msg($comment->get_error(), ($comment->msg_code == 0 ? HTTP_REFERER : ''), ($comment->msg_code == 0 ? 1 : 0));
  107. }
  108. public function ajax() {
  109. $commentid =& $this->commentid;
  110. $siteid =& $this->siteid;
  111. $num = isset($_GET['num']) && intval($_GET['num']) ? intval($_GET['num']) : 20;
  112. $direction = isset($_GET['direction']) && intval($_GET['direction']) ? intval($_GET['direction']) : 0;
  113. $pc_tag = pc_base::load_app_class('comment_tag');
  114. $comment = array();
  115. if ($comment = $pc_tag->get_comment(array('commentid'=>$commentid))) {
  116. $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
  117. $offset = ($page-1)*$num;
  118. $data = array('commentid'=>$commentid, 'site'=>$siteid, 'limit'=>$offset.','.$num, 'direction'=>$direction);
  119. $comment['data'] = $pc_tag->lists($data);
  120. pc_base::load_sys_class('format', '', 0);
  121. foreach ($comment['data'] as $k=>$v) {
  122. $comment['data'][$k]['format_time'] = format::date($v['creat_at'], 1);
  123. }
  124. switch ($direction) {
  125. case '1'://正
  126. $total = $comment['square'];
  127. break;
  128. case '2'://反
  129. $total = $comment['anti'];
  130. break;
  131. case '3'://中
  132. $total = $comment['neutral'];
  133. break;
  134. default:
  135. $total = $comment['total'];
  136. break;
  137. }
  138. $comment['pages'] = pages($total, $page, $num, 'javascript:comment_next_page({$page})');
  139. if (pc_base::load_config('system', 'charset') == 'gbk') {
  140. $comment = array_iconv($comment, 'gbk', 'utf-8');
  141. }
  142. echo json_encode($comment);
  143. } else {
  144. exit('0');
  145. }
  146. }
  147. //提示信息处理
  148. protected function _show_msg($msg, $url = '', $status = 0) {
  149. switch ($this->format) {
  150. case 'json':
  151. $msg = pc_base::load_config('system', 'charset') == 'gbk' ? iconv('gbk', 'utf-8', $msg) : $msg;
  152. echo json_encode(array('msg'=>$msg, 'status'=>$status));
  153. exit;
  154. break;
  155. case 'jsonp':
  156. $msg = pc_base::load_config('system', 'charset') == 'gbk' ? iconv('gbk', 'utf-8', $msg) : $msg;
  157. echo strip_tags($this->callback).'('.json_encode(array('msg'=>$msg, 'status'=>$status)).')';
  158. exit;
  159. break;
  160. default:
  161. showmessage($msg, $url);
  162. break;
  163. }
  164. }
  165. }