PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/class/bbs/SaveReply.class.php

https://github.com/hylinux/ltebbs
PHP | 359 lines | 200 code | 84 blank | 75 comment | 47 complexity | 281255d629e80e7b4de0914d4b785550 MD5 | raw file
  1. <?php
  2. //vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldcolumn=1 foldmethod=marker:
  3. /**
  4. * 项目: 5anet(BBS)
  5. * 文件: class/bbs/SaveReply.class.php
  6. *
  7. * 保存新的回复
  8. *
  9. * PHP Version 5
  10. *
  11. * @package: class.bbs
  12. * @author: Mike.G Chinese Name: 黄叶 <hylinux@gmail.com>
  13. * @license: http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  14. * @copyright: http://www.5anet.com
  15. * @version: $Id: SaveReply.class.php,v 1.1.1.1 2006-08-28 13:09:20 ghw Exp $
  16. * @date: $Date: 2006-08-28 13:09:20 $
  17. */
  18. include_once CLASS_PATH.'main/BaseAction.class.php';
  19. include_once CLASS_PATH.'bbs/LayoutUtil.class.php';
  20. //包含需要用到的函数
  21. include_once FUNCTION_PATH.'getIp.fun.php';
  22. include_once FUNCTION_PATH.'getCurrentDate.fun.php';
  23. //include the language file
  24. if ( file_exists(LANG_PATH.SYSTEM_LANG.'/SaveReply.lang.php') ) {
  25. include_once LANG_PATH.SYSTEM_LANG.'/SaveReply.lang.php';
  26. }
  27. class SaveReply extends BaseAction {
  28. /**
  29. * 数据库的连接
  30. */
  31. public $db;
  32. /**
  33. * 每页显示的记录数
  34. */
  35. private $pre_page = 10;
  36. /**
  37. * 构造函数
  38. * @param: NULL
  39. * @return: NULL
  40. * @access: public
  41. */
  42. public function __construct() {
  43. $this->db = $this->getDB();
  44. }
  45. /**
  46. * 保存新回复
  47. * @param: NULL
  48. * @return: NULL
  49. * @access: public
  50. */
  51. public function run() {
  52. //取得主题的id
  53. $topic_id = $this->getParameterFromPOST('id');
  54. //验证帖子的是否存在
  55. if ( !TopicUtil::isExists($this->db, $topic_id) ) {
  56. $this->AlertAndBack(TOPIC_IS_NOT_EXISTS);
  57. return;
  58. }
  59. //验证帖子的状态
  60. $status = TopicUtil::getTopicStatus($this->db, $topic_id);
  61. //注意状态为3,则为帖子被锁定,不能回复了。
  62. if ( $status == 2 ) {
  63. $this->AlertAndBack(TOPIC_HAD_BE_CLOSED);
  64. return;
  65. } else if ( $status == 3 ) {
  66. $this->AlertAndBack(TOPIC_HAD_BE_LOCK);
  67. return;
  68. }
  69. //取得版块的id
  70. $bbs_id = TopicUtil::getLayoutId($this->db, $topic_id);
  71. if ( !$bbs_id ) {
  72. $this->forward('index.php');
  73. }
  74. //验证论坛是否存在
  75. if ( !LayoutUtil::isExists($this->db, $bbs_id)) {
  76. //论坛不存在,则转向首页
  77. $this->forward('index.php');
  78. }
  79. //更新用户在本版的信息
  80. LayoutUtil::updateOnlineUser($this->db, $bbs_id);
  81. $bbs_status = LayoutUtil::getLayoutStatus($this->db, $bbs_id);
  82. if ( $bbs_status == 1 && !isset($_SESSION['user']) ) {
  83. $this->AlertAndForward(SNT_NEED_LOGIN, 'index.php?module=user&action=showlogin');
  84. return;
  85. } else if ( $bbs_status == 2 ) {
  86. $this->AlertAndForward(SNT_LAYOUT_WAS_CLOSED, 'index.php');
  87. return;
  88. } else if ( $bbs_status == 3 ) {
  89. //等于三不允许发帖
  90. $this->AlertAndBack(SNT_NOW_ALLOW_NEW_TOPIC);
  91. return;
  92. } else if ( LayoutUtil::isClosedByParent($this->db, $bbs_id) ) {
  93. $this->AlertAndForward(SNT_LAYOUT_WAS_CLOSED, 'index.php');
  94. return;
  95. }
  96. //取得各种参数
  97. //帖子的表情
  98. $express = $this->getParameterFromPost('express');
  99. //上传的帖子标题
  100. $title = $this->getParameterFromPost('title');
  101. //上传的内容
  102. $content = $this->getParameterFromPost('content');
  103. //记录在Session里
  104. $_SESSION['temp_title'] = $title;
  105. $_SESSION['temp_content'] = $content;
  106. $_SESSION['temp_express'] = $express;
  107. //看文件是否有文件上传
  108. if ( $_FILES['attach']['tmp_name'] ) {
  109. //用户有上传文件
  110. if ( $_FILES['attach']['type'] != 'image/gif'
  111. && $_FILES['attach']['type'] != 'image/jpeg'
  112. && $_FILES['attach']['type'] != 'image/jpg'
  113. && $_FILES['attach']['type'] != 'image/pjpeg'
  114. && $_FILES['attach']['type'] != 'image/png' ) {
  115. $this->AlertandBack(ST_PHONE_FILE_LIMIT);
  116. }
  117. //判断上传的文件大小是否合乎要求
  118. if ( $_FILES['attach']['size'] > 1048576 ) {
  119. $this->AlertAndBack(ST_PHONE_FILE_SIZE_LIMIT);
  120. return;
  121. }
  122. }
  123. //回复标题可以为空
  124. //如果标题为空,则自动生成一个标题
  125. if ( !$title || strlen($title)<=0 ) {
  126. $sql = 'select title from bbs_subject where id=?';
  127. $sth = $this->db->Prepare($sql);
  128. $res = $this->db->Execute($sth, array($topic_id));
  129. $rows = $res->FetchRow();
  130. $title = "Re:".$rows['title'];
  131. }
  132. /*
  133. if ( strlen($title) > 143 ) {
  134. $this->AlertAndBack(ST_TITLE_TOO_LONG);
  135. return;
  136. }*/
  137. if ( !$content || strlen($content) <= 0 ) {
  138. $this->AlertAndBack(ST_CONTENT_IS_EMPTY);
  139. return;
  140. }
  141. //插入新回复
  142. $ip_temp = getIp();
  143. $ip = $ip_temp['ip'];
  144. $user_name = $_SESSION['user']['name'];
  145. $now = time();
  146. $sql = 'insert into bbs_reply ( layout_id, title, author, content, post_ip, '.
  147. 'post_date, express, subject_id ) values (?, ?, ?, ?, ?, ?, ?, ?) ';
  148. $sth = $this->{'db'}->Prepare($sql);
  149. $this->{'db'}->Execute($sth, array(
  150. $bbs_id, $title, $user_name, $content, $ip, $now, $express, $topic_id));
  151. if ( $this->{'db'}->ErrorNo() ) {
  152. $this->AlertAndBack($this->{'db'}->ErrorMsg());
  153. return;
  154. }
  155. //得到最后的id
  156. $insert_id = $this->{'db'}->Insert_id();
  157. if ( $_FILES['attach']['tmp_name'] ) {
  158. //取得文件的大小
  159. list($image_width, $image_height, $image_type, $image_attr )
  160. = getimagesize($_FILES['attach']['tmp_name']);
  161. //判断文件的类型
  162. switch ( $image_type ) {
  163. case 1:
  164. $image_left_type = '.gif';
  165. break;
  166. case 2:
  167. $image_left_type = '.jpg';
  168. break;
  169. case 3:
  170. $image_left_type = '.png';
  171. break;
  172. }
  173. //存储的文件名
  174. $file_name = ROOT_PATH.'upload/attach/reply/'.$insert_id.$image_left_type;
  175. if ( !move_uploaded_file($_FILES['attach']['tmp_name'], $file_name ) ) {
  176. $sql = 'delete from bbs_reply where id=?';
  177. $sth = $this->{'db'}->Prepare($sql);
  178. $this->{'db'}->Execute($sth, array($insert_id));
  179. $this->AlertAndBack(ST_UPLOAD_ERROR);
  180. return;
  181. } else {
  182. $sql = 'insert into bbs_reply_attach (reply_id, file_type) '.
  183. ' values (?, ?)';
  184. $sth = $this->{'db'}->Prepare($sql);
  185. $this->{'db'}->Execute($sth, array(
  186. $insert_id, $image_left_type));
  187. }
  188. }
  189. unset($_SESSION['temp_title']);
  190. unset($_SESSION['temp_content']);
  191. unset($_SESSION['temp_express']);
  192. //发送短信,通知各个用户有回复了你的帖子
  193. //发送邮件,通知各个用户有回复了你的帖子
  194. $mail_user = array();
  195. $message_user = array();
  196. $sql = 'select a.author, b.user_email, b.id from bbs_subject a join '.
  197. ' base_user_info b on a.author = b.user_name '.
  198. ' join user_setting c on b.id=c.user_id where a.id=? and c.user_whether_receive_email=1';
  199. $sth = $this->db->Prepare($sql);
  200. $res = $this->db->Execute($sth, array($topic_id));
  201. $rows = $res->FetchRow();
  202. if ( $rows['id'] ) {
  203. $mail_user[] = $rows['user_email'];
  204. }
  205. $sql = 'select distinct a.author, b.user_email, b.id from bbs_reply a join base_user_info b '.
  206. ' on a.author = b.user_name join user_setting c on b.id=c.user_id '.
  207. ' where a.subject_id=? and c.user_whether_receive_email=1';
  208. $sth = $this->db->Prepare($sql);
  209. $res = $this->db->Execute($sth, array($topic_id));
  210. while ( $rows = $res->FetchRow() ) {
  211. if ( $rows['id'] ) {
  212. $mail_user[] = $rows['user_email'];
  213. }
  214. }
  215. $mail_user = array_unique($mail_user);
  216. //计算发送短信的用户数组
  217. $sql = 'select a.author, b.id from bbs_subject a join '.
  218. ' base_user_info b on a.author = b.user_name '.
  219. ' join user_setting c on b.id=c.user_id where a.id=? and c.receive_system_message=1';
  220. $sth = $this->db->Prepare($sql);
  221. $res = $this->db->Execute($sth, array($topic_id));
  222. $rows = $res->FetchRow();
  223. if ( $rows['id'] ) {
  224. $message_user[] = $rows['id'];
  225. }
  226. $sql = 'select distinct a.author, b.id from bbs_reply a join base_user_info b '.
  227. ' on a.author = b.user_name join user_setting c on b.id=c.user_id '.
  228. ' where a.subject_id=? and c.receive_system_message=1';
  229. $sth = $this->db->Prepare($sql);
  230. $res = $this->db->Execute($sth, array($topic_id));
  231. while ( $rows = $res->FetchRow() ) {
  232. if ( $rows['id'] ) {
  233. $message_user[] = $rows['id'];
  234. }
  235. }
  236. $message_user = array_unique($message_user);
  237. //开始发送邮件
  238. $to_address = implode(',', $mail_user);
  239. $mail_content = ST_MAIL_CONTENT."\n\n";
  240. $mail_content .= ROOT_URL.'index.php?module=bbs&action=viewtopic&id='.$topic_id."\n\n";
  241. $headers = "To:".$to_address."\r\n";
  242. $headers .= "From:".WEBSITE_EMAIL."\r\n";
  243. //发送邮件:
  244. @mail($to_address, ST_MAIL_SUBJECT, $mail_content, $headers);
  245. //发送短消息
  246. //发件人
  247. $sender = '0';
  248. $message_content = ST_MESSAGE_CONTENT."\n";
  249. $now = getNoFormateCurrentDate();
  250. $message_content .= "[url=".
  251. 'index.php?module=bbs&action=viewtopic&id='.$topic_id."][color=red]".ST_CLICK_HERE."[/color]".
  252. "[/url]";
  253. $sql = 'insert into message_inbox ( user_id, send_user_id, title, receive_time, content ) '.
  254. ' values ( ?, ?, ?, ?, ?) ';
  255. $sth = $this->db->Prepare($sql);
  256. foreach ( $message_user as $user ) {
  257. $this->db->Execute($sth, array(
  258. $user, $sender, ST_MAIL_SUBJECT, $now, $message_content));
  259. }
  260. $now = time();
  261. //更新主题的最后更新时间
  262. $update_sql = 'update bbs_subject set last_access_date=?,reply_number=reply_number+1 where id=?';
  263. $sth = $this->db->Prepare($update_sql);
  264. $this->db->Execute($sth, array($now, $topic_id));
  265. //求这个回帖的位置所在的位置
  266. $sort_number = TopicUtil::getSortNumber($this->db, $topic_id, $insert_id);
  267. $page = ceil ( $sort_number / $this->pre_page );
  268. //这里还有很多的工作需要做
  269. $this->TipsAndForward(
  270. ST_SAVE_REPLY_SUCCESS,
  271. 'index.php?module=bbs&action=viewtopic&id='.$topic_id.'&page='.$page.
  272. '#topic'.$sort_number);
  273. }
  274. }
  275. ?>