/api/save_comments.php

https://github.com/HelipengTony/oblog · PHP · 152 lines · 122 code · 10 blank · 20 comment · 30 complexity · c6318620efd8158cee3d932f4b8e11d0 MD5 · raw file

  1. <?php
  2. error_reporting(E_ALL ^ E_NOTICE);
  3. /*
  4. 提交/回复评论内容
  5. 请求 key 名为 ver、name、email、content、pid、reply
  6. ver 为 comment_ver
  7. name 为评论人
  8. email 为评论人邮箱
  9. content 为评论内容
  10. pid 为评论对应文章/页面
  11. 页面 pid 开头为 page-
  12. 文章 pid 为文件名
  13. reply 为要回复的当前文章对应评论
  14. */
  15. class post_back
  16. {
  17. function save($key)
  18. {
  19. return !empty($_POST[$key]) ? addslashes($_POST[$key]) : 0;
  20. }
  21. function infofy($string)
  22. {
  23. return $string = str_replace("\n", "", $string);
  24. }
  25. function contentfy($string)
  26. {
  27. return $string = str_replace("\n", " \n ", $string);
  28. }
  29. function valid_post($pid)
  30. {
  31. if (explode('-', $pid)[0] == 'page') { //页面评论
  32. $host = dirname(dirname(__FILE__)) . '/pages/'; //要读取的文件夹
  33. $file_path = $host . explode('-', $pid)[1] . '.md';
  34. if (file_exists($file_path)) {
  35. return 1;
  36. } else {
  37. return 0;
  38. }
  39. } else { //文章评论
  40. $host = dirname(dirname(__FILE__)) . '/posts/'; //要读取的文件夹
  41. $file_path = $host . $pid . '.md';
  42. if (file_exists($file_path)) {
  43. return 1;
  44. } else {
  45. return 0;
  46. }
  47. }
  48. }
  49. function valid_comm_post($pid)
  50. {
  51. $host = dirname(dirname(__FILE__)) . '/comments/'; //要读取的文件夹
  52. $file_path = $host . $pid . '.json';
  53. if (file_exists($file_path)) {
  54. return 1;
  55. } else {
  56. return 0;
  57. }
  58. }
  59. }
  60. $post = new post_back();
  61. /* 验证信息 */
  62. $ver = $post->save('ver');
  63. $name = $post->save('name');
  64. $email = $post->save('email');
  65. $content = $post->save('content');
  66. $pid = $_POST['pid'];
  67. $reply = $_POST['reply'];
  68. if ($reply !== NULL) { //回复评论
  69. $reply_status = 1;
  70. } else {
  71. $reply_status = 0;
  72. }
  73. $array = array();
  74. if (empty($ver) || empty($name) || empty($email) || empty($content) || empty($pid)) {
  75. $array['code'] = 101;
  76. $array['info'] = '信息不全';
  77. echo json_encode($array, JSON_UNESCAPED_UNICODE);
  78. } else if ($ver == 'comment_ver') {
  79. /* 结束验证信息 */
  80. if ($post->valid_post($pid)) { //判断文章/页面是否存在
  81. //处理评论内容
  82. if ($post->valid_comm_post($pid)) {
  83. $json_string = file_get_contents(dirname(dirname(__FILE__)) . '/comments/' . $pid . ".json"); // 从文件中读取数据到PHP变量
  84. $data = json_decode($json_string, true); // 把JSON字符串转成PHP数组
  85. $data_length = count($data);
  86. if ($reply_status) { //处理回复请求
  87. //回复的评论是否存在
  88. if ((int)$reply > ($data_length - 1) || (int)$reply < 0) {
  89. $array['code'] = 104;
  90. $array['info'] = '回复不存在';
  91. echo json_encode($array, JSON_UNESCAPED_UNICODE);
  92. die();
  93. }
  94. //获取回复对应评论回复数量
  95. if (!!$data[$reply]['reply']) {
  96. $reply_length = count($data[$reply]['reply']);
  97. } else {
  98. $reply_length = 0;
  99. }
  100. //增加在对应评论回复字段
  101. $data[$reply]['reply'][$reply_length]['id'] = $reply_length;
  102. $data[$reply]['reply'][$reply_length]['name'] = $name;
  103. $data[$reply]['reply'][$reply_length]['email'] = $email;
  104. $data[$reply]['reply'][$reply_length]['content'] = $content;
  105. $data[$reply]['reply'][$reply_length]['date'] = time();
  106. } else { //处理评论请求
  107. $data[$data_length]['id'] = $data_length;
  108. $data[$data_length]['name'] = $name;
  109. $data[$data_length]['date'] = time();
  110. $data[$data_length]['email'] = $email;
  111. $data[$data_length]['content'] = $content;
  112. }
  113. $json_strings = json_encode($data);
  114. file_put_contents(dirname(dirname(__FILE__)) . '/comments/' . $pid . ".json", $json_strings); //写入
  115. $array['code'] = 201;
  116. $array['info'] = '修改成功';
  117. echo json_encode($array, JSON_UNESCAPED_UNICODE);
  118. } else {
  119. $data = array();
  120. $data[0]['id'] = 0;
  121. $data[0]['name'] = $name;
  122. $data[0]['date'] = time();
  123. $data[0]['email'] = $email;
  124. $data[0]['content'] = $content;
  125. $json_string = json_encode($data);
  126. // 写入文件
  127. file_put_contents(dirname(dirname(__FILE__)) . '/comments/' . $pid . '.json', $json_string);
  128. $array['code'] = 202;
  129. $array['info'] = '创建成功';
  130. echo json_encode($array, JSON_UNESCAPED_UNICODE);
  131. }
  132. } else {
  133. $array['code'] = 102;
  134. $array['info'] = '文章不存在';
  135. echo json_encode($array, JSON_UNESCAPED_UNICODE);
  136. }
  137. } else {
  138. $array['code'] = 103;
  139. $array['info'] = '验证错误';
  140. echo json_encode($array, JSON_UNESCAPED_UNICODE);
  141. }
  142. /* 结束验证信息 */