markdown格式wiki文档

Index.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. namespace app\controller;
  3. // use \Firebase\JWT\JWT;
  4. use app\Request;
  5. use app\View;
  6. use app\Session;
  7. use app\model\User;
  8. use app\utils\RRException;
  9. use app\utils\ParsedownExtension;
  10. class Index extends View
  11. {
  12. public function auth()
  13. {
  14. if (!Session::get('userid')) {
  15. echo "您未登录请登录";
  16. header("Location: /login", TRUE, 301);
  17. exit();
  18. }
  19. }
  20. public function index()
  21. {
  22. $this->auth();
  23. $request = Request::getInstance();
  24. $doc = $request->params['doc'];
  25. $doc = ltrim($doc, '/');
  26. if (substr($doc, -1) === '/') {
  27. $doc = $doc . "index.md";
  28. }
  29. $doc = empty($doc) ? 'index.md' : $doc;
  30. if (pathinfo($doc, PATHINFO_EXTENSION) != 'md') {
  31. $doc = $doc . ".md";
  32. }
  33. $filename = DATA_PATH . $doc;
  34. $text = "";
  35. $toc = "";
  36. $content = "";
  37. if ($is_file_exists = file_exists($filename)) {
  38. $parsedownExtension = new ParsedownExtension();
  39. $parsedownExtension->setTocEnabled(true);
  40. $text = file_get_contents($filename);
  41. $res = $parsedownExtension->text($text);
  42. $toc = $res['toc'];
  43. $content = $res['content'];
  44. }
  45. $this->assign('doc', $doc);
  46. $this->assign('is_file_exists', $is_file_exists);
  47. $this->assign('text', $text);
  48. $this->assign('toc', $toc);
  49. $this->assign('content', $content);
  50. $this->fetch();
  51. }
  52. public function editor()
  53. {
  54. $doc = $GLOBALS['doc'];
  55. var_dump($doc);
  56. exit;
  57. $this->fetch();
  58. }
  59. public function save()
  60. {
  61. $this->auth();
  62. // 获取payload json数据,转换成数组形式
  63. $postData = file_get_contents('php://input');
  64. $requests = !empty($postData) ? json_decode($postData, true) : [];
  65. $doc = $requests['doc'];
  66. $content = $requests['content'];
  67. $pathinfo = pathinfo($doc);
  68. $dir_name = DATA_PATH . $pathinfo['dirname'];
  69. if (!is_dir($dir_name) && !mkdir($dir_name, 0744, true)) {
  70. die(json_encode(['code' => 2, 'msg' => '目录创建失败']));
  71. }
  72. $filename = DATA_PATH . $doc;
  73. $res = file_put_contents($filename, $content);
  74. if ($res === false) {
  75. echo json_encode(['code' => 2, 'msg' => '保存失败']);
  76. } else {
  77. echo json_encode(['code' => 0, 'msg' => '保存成功, 字数:' . $res]);
  78. }
  79. die();
  80. }
  81. // private function validate ()
  82. // {
  83. // $jwt = isset($_SERVER['HTTP_TOKEN']) ? $_SERVER['HTTP_TOKEN'] : '';
  84. // if (empty($jwt)) {
  85. // throw new RRException("You do not have permission to access.", 401);
  86. // }
  87. // try {
  88. // JWT::$leeway = 60;
  89. // $decoded = JWT::decode($jwt, KEY, ['HS256']);
  90. // $arr = (array)$decoded;
  91. // if ($arr['exp'] < time()) {
  92. // throw new RRException("认证信息已过期, 请重新登录.", 401);
  93. // }
  94. // } catch(\Exception $e) {
  95. // throw new RRException($e->getMessage(), 401);
  96. // }
  97. // return true;
  98. // }
  99. public function login()
  100. {
  101. if (Session::get('userid')) {
  102. echo "您已登录请登录";
  103. header("Location: /index", TRUE, 301);
  104. exit();
  105. }
  106. $request = Request::getInstance();
  107. if ($request->isPost()) {
  108. $username = strip_tags(htmlentities($request->params['username']));
  109. $password = strip_tags(htmlentities($request->params['password']));
  110. if (!$username || !$password) {
  111. throw new RRException("用户名或密码不能为空!", 1);
  112. }
  113. $user = new User();
  114. $info = $user->getInfoByUsername($username);
  115. if (!$info) {
  116. throw new RRException("用户名或密码错误!", 1);
  117. }
  118. if (md5($password) !== $info['password']) {
  119. throw new RRException("用户名或密码错误!", 1);
  120. }
  121. // // 用户名和密码正确,则签发tokon
  122. // $nowtime = time();
  123. // $jwtInfo = [
  124. // 'iss' => 'huwhois@163.com', //签发者
  125. // 'iat' => $nowtime, //签发时间
  126. // 'nbf' => $nowtime + 10, //在什么时间之后该jwt才可用
  127. // 'exp' => $nowtime + 64800, //过期时间-18h
  128. // 'data' => [
  129. // 'userid' => $user['id'],
  130. // 'username' => $username
  131. // ]
  132. // ];
  133. // $token = JWT::encode($jwtInfo, KEY);
  134. // echo json_encode(['code'=>0, 'msg'=>'success', 'token'=>$token]);
  135. // 存 session
  136. Session::set('userid', $info['id']);
  137. Session::set('username', $info['username']);
  138. echo json_encode(['code' => 0, 'msg' => 'success']);
  139. die();
  140. } else {
  141. $this->fetch();
  142. }
  143. }
  144. public function logout()
  145. {
  146. Session::destroy();
  147. echo "登出成功.....";
  148. header("Location: /login", TRUE, 301);
  149. exit();
  150. }
  151. public function password()
  152. {
  153. $request = Request::getInstance();
  154. if ($request->isPost()) {
  155. $userid = Session::get('userid');
  156. if (!$userid) {
  157. throw new RRException('用户未登录, 请登录');
  158. }
  159. $user = new User();
  160. // 修改密码
  161. $oldpassword = isset($request->params['oldpassword']) ? (string) $request->params['oldpassword'] : '';
  162. $password = isset($request->params['newpassword']) ? (string) $request->params['newpassword'] : '';
  163. $repassword = isset($request->params['repassword']) ? (string) $request->params['repassword'] : '';
  164. if (!$password) {
  165. throw new RRException("密码不能为空", 1);
  166. }
  167. if ($password != $repassword) {
  168. throw new RRException("两次密码不一致", 1);
  169. }
  170. $userinfo = $user->getInfoById($userid);
  171. if (md5($oldpassword) != $userinfo['password']) {
  172. throw new RRException("原密码不正确", 1);
  173. }
  174. try {
  175. $user->updateById([
  176. 'id' => $userid,
  177. 'password' => md5($password)
  178. ]);
  179. } catch (\Exception $e) {
  180. throw new RRException($e->getMessage(), 1);
  181. }
  182. Session::destroy();
  183. echo json_encode(['code' => 0, 'msg' => "保存成功"]);
  184. die();
  185. }
  186. }
  187. public function userlist()
  188. {
  189. $list = (new User())->dataList();
  190. $this->assign('list', $list);
  191. $this->fetch();
  192. }
  193. public function userdelete()
  194. {
  195. $request = Request::getInstance();
  196. if ($request->isPost()) {
  197. $id = isset($request->params['userid']) ? (int) $request->params['userid'] : 0;
  198. if (!$id) {
  199. throw new RRException('userid 不能为空', 1);
  200. }
  201. $res = (new User())->deleteById($id);
  202. if ($res) {
  203. throw new RRException('操作成功', 0);
  204. } else {
  205. throw new RRException('操作失败', 1);
  206. }
  207. }
  208. }
  209. public function userinfo()
  210. {
  211. $request = Request::getInstance();
  212. $userid = isset($request->params['userid']) ? (int) $request->params['userid'] : 0;
  213. if ($userid !== 0) {
  214. $data = (new User())->getInfoById($userid);
  215. unset($data['password']);
  216. echo json_encode(['code' => 0, 'info' => $data]);
  217. } else {
  218. throw new RRException("userid 不能为空", 1);
  219. }
  220. die();
  221. }
  222. public function usersave()
  223. {
  224. $request = Request::getInstance();
  225. if ($request->isPost()) {
  226. $id = isset($request->params['userid']) ? (int) $request->params['userid'] : 0;
  227. $user = new User();
  228. if ($id === 0) {
  229. // 新增
  230. $username = isset($request->params['username']) ? (string) $request->params['username'] : '';
  231. if (!$username) {
  232. throw new RRException("用户名不能为空", 1);
  233. }
  234. $password = isset($request->params['password']) ? (string) $request->params['password'] : '';
  235. $repassword = isset($request->params['repassword']) ? (string) $request->params['repassword'] : '';
  236. if (!$password) {
  237. throw new RRException("密码不能为空", 1);
  238. }
  239. if ($password != $repassword) {
  240. throw new RRException("两次密码不一致", 1);
  241. }
  242. try {
  243. $user->save([
  244. 'username' => $username,
  245. 'password' => md5($password),
  246. 'create_time' => time()
  247. ]);
  248. } catch (\Exception $e) {
  249. throw new RRException($e->getMessage(), 1);
  250. }
  251. echo json_encode(['code' => 0, 'msg' => "保存成功"]);
  252. die();
  253. } else {
  254. // 修改密码
  255. $password = isset($request->params['password']) ? (string) $request->params['password'] : '';
  256. $repassword = isset($request->params['repassword']) ? (string) $request->params['repassword'] : '';
  257. if (!$password) {
  258. throw new RRException("密码不能为空", 1);
  259. }
  260. if ($password != $repassword) {
  261. throw new RRException("两次密码不一致", 1);
  262. }
  263. // $userinfo = $user->getInfoById($id);
  264. // $newpassword = md5($password);
  265. // if ($newpassword != $userinfo['password']) {
  266. // throw new RRException("原密码不正确", 1);
  267. // }
  268. try {
  269. $user->updateById([
  270. 'id' => $id,
  271. 'password' => md5($password)
  272. ]);
  273. } catch (\Exception $e) {
  274. throw new RRException($e->getMessage(), 1);
  275. }
  276. echo json_encode(['code' => 0, 'msg' => "保存成功, 请重新登录."]);
  277. die();
  278. }
  279. }
  280. }
  281. }