PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/class/bbs/SaveEdit.class.php

https://github.com/hylinux/ltebbs
PHP | 292 lines | 170 code | 58 blank | 64 comment | 46 complexity | ca4c9dbcc4dbd7ad2d6899a230d9d316 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/SaveEdit.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: SaveEdit.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. include_once CLASS_PATH.'bbs/TopicUtil.class.php';
  21. include_once FUNCTION_PATH.'getCurrentDate.fun.php';
  22. //include the language file
  23. if ( file_exists(LANG_PATH.SYSTEM_LANG.'/SaveEdit.lang.php') ) {
  24. include_once LANG_PATH.SYSTEM_LANG.'/SaveEdit.lang.php';
  25. }
  26. class SaveEdit extends BaseAction {
  27. /**
  28. * 数据库的连接
  29. */
  30. public $db;
  31. /**
  32. * 构造函数
  33. * @param: NULL
  34. * @return: NULL
  35. * @access: public
  36. */
  37. public function __construct() {
  38. $this->db = $this->getDB();
  39. }
  40. /**
  41. * 保存编辑后的帖子
  42. * @param: NULL
  43. * @return: NULL
  44. * @access: public
  45. */
  46. public function run() {
  47. //收集变量
  48. $topic = $this->getParameterFromPOST('topic');
  49. $id = $this->getParameterFromPOST('id');
  50. $title = $this->getParameterFromPOST('title');
  51. $content = $this->getParameterFromPOST('content');
  52. $express = $this->getParameterFromPOST('express');
  53. $delattach = $this->getParameterFromPOST('delattach');
  54. if ( !$title || strlen($title)<=0 ) {
  55. $this->AlertAndBack(SE_TITLE_IS_EMPTY);
  56. return;
  57. }
  58. /*
  59. if ( strlen($title) > 140 ) {
  60. $this->AlertAndBack(SE_TITLE_TOO_LONG);
  61. return;
  62. }*/
  63. if ( !$content || strlen($content) <= 0 ) {
  64. $this->AlertAndBack(SE_CONTENT_IS_EMPTY);
  65. return;
  66. }
  67. //做出基本的判断/*{{{*/
  68. if ( !$id ) {
  69. $this->AlertandBack(SE_NO_TOPIC_ID);
  70. return;
  71. }
  72. //找出这个帖子所在的版块的id, 作者。
  73. $layout_id = 0;
  74. $author = '';
  75. if ( $topic ) {
  76. //如果是主题
  77. $sql = 'select layout_id, author from bbs_subject where id=?';
  78. $sth = $this->db->prepare($sql);
  79. $res = $this->db->execute($sth, array($id));
  80. $rows = $res->FetchRow();
  81. $layout_id = $rows['layout_id'];
  82. $author = $rows['author'];
  83. } else {
  84. $sql = 'select layout_id, author from bbs_reply where id=?';
  85. $sth = $this->db->prepare($sql);
  86. $res = $this->db->execute($sth, array($id));
  87. $rows = $res->FetchRow();
  88. $layout_id = $rows['layout_id'];
  89. $author = $rows['author'];
  90. }
  91. if ( !$layout_id ) {
  92. //保存的帖子根本不存在。
  93. $this->AlertAndBlack(SE_TOPIC_IS_NOT_EXISTS);
  94. return;
  95. }
  96. //判断用户是否可以编辑
  97. if ( $topic ) {
  98. //如果等于1,则为主题
  99. //如果存在,则判断用户是否有权利修改
  100. $sql = 'select author, layout_id from bbs_subject where id=?';
  101. $sth = $this->db->Prepare($sql);
  102. $res = $this->db->Execute($sth, array($id));
  103. $rows = $res->FetchRow();
  104. $user_name = $rows['author'];
  105. $bbs_id = $rows['layout_id'];
  106. $user_can_be_edit = 0;
  107. if ( strtolower($_SESSION['user']['name']) == strtolower($user_name) ) {
  108. $user_can_be_edit = 1;
  109. } else if ( strtolower($_SESSION['user']['name']) != strtolower($user_name) ) {
  110. //判断用户是否是这个版块的版主。
  111. $dep = UserUtil::getUserDep($db, $_SESSION['user']['name']);
  112. if ( $dep == 1 || $dep == 2 ) {
  113. $user_can_be_edit = 1;
  114. } else if ( $dep == 3 ) {
  115. $user_can_be_edit = UserUtil::isThisLayoutAdmin($db, $id, $_SESSION['user']['name']);
  116. }
  117. }
  118. if ( !$user_can_be_edit ) {
  119. $this->AlertAndBack(SE_YOU_HAVE_NO_PRIVIATE);
  120. return;
  121. }
  122. } else {
  123. //$topic 为其他值,那么就是回帖,而不是主题
  124. $sql = 'select author, subject_id, layout_id from bbs_reply where id=?';
  125. $sth = $this->db->Prepare($sql);
  126. $res = $this->db->Execute($sth, array($id));
  127. $rows = $res->FetchRow();
  128. if ( !$rows['author'] ) {
  129. $this->AlertAndBack(SE_TOPIC_ID_IS_NOT_EXISTS);
  130. return;
  131. }
  132. //如果存在,
  133. //则判断用户是否有权限
  134. $user_name = $rows['author'];
  135. $subject_id = $rows['subject_id'];
  136. $bbs_id = $rows['layout_id'];
  137. $user_can_be_edit = 0;
  138. if ( strtolower($_SESSION['user']['name']) == strtolower($user_name) ) {
  139. $user_can_be_edit = 1;
  140. } else if ( strtolower($_SESSION['user']['name']) != strtolower($user_name) ) {
  141. //判断用户是否是这个版块的版主。
  142. $dep = UserUtil::getUserDep($db, $_SESSION['user']['name']);
  143. if ( $dep == 1 || $dep == 2 ) {
  144. $user_can_be_edit = 1;
  145. } else if ( $dep == 3 ) {
  146. $user_can_be_edit = UserUtil::isThisLayoutAdmin($db, $subject_id, $_SESSION['user']['name']);
  147. }
  148. }
  149. if ( !$user_can_be_edit ) {
  150. $this->AlertAndBack(SE_YOU_HAVE_NO_PRIVIATE);
  151. return;
  152. }
  153. }
  154. /*}}}*/
  155. //判断做完了,则可以开始进行更新了。
  156. //求现在的时间
  157. $now = getNoFormateCurrentDate();
  158. if ( $topic ) {
  159. $user_name = $_SESSION['user']['name'];
  160. $sql = 'update bbs_subject set title=?, content=?, express=?, is_edit=1, '.
  161. ' edit_user=?, edit_time=? where id=?';
  162. $sth = $this->db->prepare($sql);
  163. $this->db->execute($sth,
  164. array($title, $content, $express, $user_name, $now, $id));
  165. if ( $this->db->ErrorNo() ) {
  166. $this->AlertAndBack($this->db->ErrorMsg());
  167. return;
  168. }
  169. if ( $delattach ) {
  170. //删除这个附件
  171. $sql = 'select file_type from bbs_subject_attach where subject_id=?';
  172. $sth = $this->db->prepare($sql);
  173. $res = $this->db->execute($sth, array($id));
  174. $rows = $res->FetchRow();
  175. $file_type = $rows['file_type'];
  176. $del_sql = 'delete from bbs_subject_attach where subject_id=?';
  177. $sth = $this->db->prepare($del_sql);
  178. $this->db->execute($sth, array($id));
  179. //删除文件。
  180. $filename = ROOT_PATH.'upload/attach/'.$id.$file_type;
  181. unlink($filename);
  182. }
  183. } else {
  184. $user_name = $_SESSION['user']['name'];
  185. $sql = 'update bbs_reply set title=?, content=?, express=?, is_edit=1, '.
  186. ' edit_user=?, edit_time=? where id=?';
  187. $sth = $this->db->prepare($sql);
  188. $this->db->execute($sth,
  189. array($title, $content, $express, $user_name, $now, $id));
  190. if ( $this->db->ErrorNo() ) {
  191. $this->AlertAndBack($this->db->ErrorMsg());
  192. return;
  193. }
  194. if ( $delattach ) {
  195. //删除这个附件
  196. $sql = 'select file_type from bbs_reply_attach where reply_id=?';
  197. $sth = $this->db->prepare($sql);
  198. $res = $this->db->execute($sth, array($id));
  199. $rows = $res->FetchRow();
  200. $file_type = $rows['file_type'];
  201. $del_sql = 'delete from bbs_reply_attach where reply_id=?';
  202. $sth = $this->db->prepare($del_sql);
  203. $this->db->execute($sth, array($id));
  204. //删除文件。
  205. $filename = ROOT_PATH.'upload/attach/reply/'.$id.$file_type;
  206. unlink($filename);
  207. }
  208. }
  209. //编辑成功后,返回当时的页面
  210. if ( $topic ) {
  211. //如果是主页
  212. //则返回第一页
  213. $this->TipsAndForward(
  214. SE_SAVE_EDIT_SUCCESS,
  215. 'index.php?module=bbs&action=viewtopic&id='.$id);
  216. return;
  217. } else {
  218. //不是主题
  219. //则是回复
  220. //求这个回帖的位置所在的位置
  221. $sql = 'select subject_id from bbs_reply where id=?';
  222. $sth = $this->db->prepare($sql);
  223. $res = $this->db->Execute($sth, array($id));
  224. $rows = $res->FetchRow();
  225. $sort_number = TopicUtil::getSortNumber($this->db, $rows['subject_id'], $id);
  226. $page = ceil ( $sort_number / 10 );
  227. //这里还有很多的工作需要做
  228. $this->TipsAndForward(
  229. SE_SAVE_EDIT_SUCCESS,
  230. 'index.php?module=bbs&action=viewtopic&id='.$rows['subject_id'].'&page='.$page.
  231. '#topic'.$sort_number);
  232. }
  233. }
  234. }