click_zan.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. function validateZan() {
  3. if (!validateUInt($_POST['id'])) {
  4. return false;
  5. }
  6. if (!validateInt($_POST['delta'])) {
  7. return false;
  8. }
  9. if ($_POST['delta'] != 1 && $_POST['delta'] != -1) {
  10. return false;
  11. }
  12. if ($_POST['type'] != 'B' && $_POST['type'] != 'BC' && $_POST['type'] != 'P' && $_POST['type'] != 'C') {
  13. return false;
  14. }
  15. return true;
  16. }
  17. if (!validateZan()) {
  18. die('<div class="text-danger">failed</div>');
  19. }
  20. crsf_defend();
  21. if ($myUser == null) {
  22. die('<div class="text-danger">please <a href="'.HTML::url('/login').'">log in</a></div>');
  23. }
  24. $id = $_POST['id'];
  25. $delta = $_POST['delta'];
  26. $type = $_POST['type'];
  27. switch ($type) {
  28. case 'B':
  29. $table_name = 'blogs';
  30. break;
  31. case 'BC':
  32. $table_name = 'blogs_comments';
  33. break;
  34. case 'P':
  35. $table_name = 'problems';
  36. break;
  37. case 'C':
  38. $table_name = 'contests';
  39. break;
  40. }
  41. $cur = queryZanVal($id, $type, $myUser);
  42. if ($cur != $delta) {
  43. $row = DB::selectFirst("select zan from $table_name where id = $id");
  44. if ($row == null) {
  45. die('<div class="text-danger">failed</div>');
  46. }
  47. $cur += $delta;
  48. if ($cur == 0) {
  49. DB::query("delete from click_zans where username = '{$myUser['username']}' and type = '$type' and target_id = $id");
  50. } elseif ($cur != $delta) {
  51. DB::query("update click_zans set val = '$cur' where username = '{$myUser['username']}' and type = '$type' and target_id = $id");
  52. } else {
  53. DB::query("insert into click_zans (username, type, target_id, val) values ('{$myUser['username']}', '$type', $id, $cur)");
  54. }
  55. $cnt = $row['zan'] + $delta;
  56. DB::query("update $table_name set zan = $cnt where id = $id");
  57. } else {
  58. $row = DB::selectFirst("select zan from $table_name where id = $id");
  59. if ($row == null) {
  60. die('<div class="text-danger">failed</div>');
  61. }
  62. $cnt = $row['zan'];
  63. }
  64. ?>
  65. <?= getClickZanBlock($type, $id, $cnt, $cur) ?>