click_zan.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. if ($myUser == null) {
  21. die('<div class="text-danger">please <a href="'.HTML::url('/login').'">log in</a></div>');
  22. }
  23. $id = $_POST['id'];
  24. $delta = $_POST['delta'];
  25. $type = $_POST['type'];
  26. switch ($type) {
  27. case 'B':
  28. $table_name = 'blogs';
  29. break;
  30. case 'BC':
  31. $table_name = 'blogs_comments';
  32. break;
  33. case 'P':
  34. $table_name = 'problems';
  35. break;
  36. case 'C':
  37. $table_name = 'contests';
  38. break;
  39. }
  40. $cur = queryZanVal($id, $type, $myUser);
  41. if ($cur != $delta) {
  42. $row = DB::selectFirst("select zan from $table_name where id = $id");
  43. if ($row == null) {
  44. die('<div class="text-danger">failed</div>');
  45. }
  46. $cur += $delta;
  47. if ($cur == 0) {
  48. DB::query("delete from click_zans where username = '{$myUser['username']}' and type = '$type' and target_id = $id");
  49. } elseif ($cur != $delta) {
  50. DB::query("update click_zans set val = '$cur' where username = '{$myUser['username']}' and type = '$type' and target_id = $id");
  51. } else {
  52. DB::query("insert into click_zans (username, type, target_id, val) values ('{$myUser['username']}', '$type', $id, $cur)");
  53. }
  54. $cnt = $row['zan'] + $delta;
  55. DB::query("update $table_name set zan = $cnt where id = $id");
  56. } else {
  57. $row = DB::selectFirst("select zan from $table_name where id = $id");
  58. if ($row == null) {
  59. die('<div class="text-danger">failed</div>');
  60. }
  61. $cnt = $row['zan'];
  62. }
  63. ?>
  64. <?= getClickZanBlock($type, $id, $cnt, $cur) ?>