Advertisement
K_Wall_24

sendmessage.php

Jan 31st, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. if(is_readable('dynmap_config.json')) {
  5. $config = json_decode(file_get_contents('dynmap_config.json'), true);
  6. $msginterval = $config['webchat-interval'];
  7. }
  8. else if(is_readable('dynmap_config.php')) {
  9. $lines = file('dynmap_config.php');
  10. array_shift($lines);
  11. array_pop($lines);
  12. $config = json_decode(implode(' ',$lines), true);
  13. $msginterval = $config['webchat-interval'];
  14. }
  15. else {
  16. $msginterval = 2000;
  17. }
  18.  
  19. if(isset($_SESSION['lastchat']))
  20. $lastchat = $_SESSION['lastchat'];
  21. else
  22. $lastchat = 0;
  23.  
  24. if($_SERVER['REQUEST_METHOD'] == 'POST' && $lastchat < time())
  25. {
  26. $micro = microtime(true);
  27. $timestamp = round($micro*1000.0);
  28.  
  29. $data = json_decode(trim(file_get_contents('php://input')));
  30. $data->timestamp = $timestamp;
  31. $data->ip = $_SERVER['REMOTE_ADDR'];
  32. if(isset($_SESSION['userid'])) {
  33. $uid = $_SESSION['userid'];
  34. if(strcmp($uid, '-guest-')) {
  35. $data->userid = $uid;
  36. }
  37. }
  38. if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  39. $data->ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  40. if(is_readable('dynmap_webchat.json')) {
  41. $old_messages = json_decode(file_get_contents('dynmap_webchat.json'), true);
  42. }
  43. if(!empty($old_messages))
  44. {
  45. foreach($old_messages as $message)
  46. {
  47. if(($timestamp - $config['updaterate'] - 10000) < $message['timestamp'])
  48. $new_messages[] = $message;
  49. }
  50. }
  51. $new_messages[] = $data;
  52. file_put_contents('dynmap_webchat.json', json_encode($new_messages));
  53. $_SESSION['lastchat'] = time()+$msginterval;
  54. echo "{ \"error\" : \"none\" }";
  55. }
  56. elseif($_SERVER['REQUEST_METHOD'] == 'POST' && $lastchat > time())
  57. {
  58. header('HTTP/1.1 403 Forbidden');
  59. }
  60. else {
  61. echo "{ \"error\" : \"none\" }";
  62. }
  63. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement