Advertisement
Guest User

tinyboard post.php json support

a guest
Jul 25th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. ### inc/functions.php:
  2.  
  3. // the user is not currently logged in as a moderator
  4. $mod = false;
  5.  
  6. +// Setting to true will cause error messages to be given as JSON.
  7. +$wantjson = false;
  8. +
  9. register_shutdown_function('fatal_error_handler');
  10.  
  11. ...
  12.  
  13. function displayBan($ban) {
  14. - global $config;
  15. + global $config, $wantjson;
  16.  
  17. $ban['ip'] = $_SERVER['REMOTE_ADDR'];
  18.  
  19. $banhtml = Element('page.html', array(
  20. 'title' => 'Banned!',
  21. 'config' => $config,
  22. 'body' => Element('banned.html', array(
  23. 'config' => $config,
  24. 'ban' => $ban
  25. ))));
  26.  
  27. + if ($wantjson) {
  28. + header('Content-Type: application/json');
  29. + die(json_encode(array('error' => 'ban', 'banhtml' => $banhtml)));
  30. + }
  31. +
  32. die($banhtml);
  33. }
  34.  
  35. ### inc/display.php:
  36.  
  37. function error($message, $priority = true) {
  38. - global $board, $mod, $config;
  39. + global $board, $mod, $config, $wantjson;
  40.  
  41. if ($config['syslog'] && $priority !== false) {
  42.  
  43. ...
  44.  
  45. if (defined('STDIN')) {
  46. // Running from CLI
  47. die('Error: ' . $message . "\n");
  48. }
  49.  
  50. + if ($wantjson) {
  51. + header('Content-Type: application/json');
  52. + die(json_encode(array('error' => 'message', 'message' => _($message))));
  53. + }
  54. +
  55. die(Element('page.html', array(
  56.  
  57. ### post.php:
  58.  
  59. } elseif (isset($_POST['post'])) {
  60.  
  61. + if (isset($_POST['wantjson']) && $_POST['wantjson'])
  62. + $wantjson = true;
  63.  
  64. if (!isset($_POST['body'], $_POST['board']))
  65. error($config['error']['bot']);
  66.  
  67. ...
  68.  
  69. $root = $post['mod'] ? $config['root'] . $config['file_mod'] . '?/' : $config['root'];
  70.  
  71. - if ($config['always_noko'] || $noko) {
  72. + if ($wantjson || $config['always_noko'] || $noko) {
  73. $redirect = $root . $board['dir'] . $config['dir']['res'] .
  74.  
  75. ...
  76.  
  77. rebuildThemes('post');
  78.  
  79. + if ($wantjson) {
  80. + $response = array();
  81. + $response['status'] = 'success';
  82. + $response['postid'] = intval($id);
  83. + $response['threadid'] = $post['op'] ? null : intval($post['thread']);
  84. + $response['board'] = $board['uri'];
  85. + $response['url'] = $redirect;
  86. +
  87. + header('Content-Type: application/json');
  88. + die(json_encode($response));
  89. + }
  90.  
  91. header('Location: ' . $redirect, true, $config['redirect_http']);
  92. } else {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement