diff options
author | Callum Macrae <callum@lynxphp.com> | 2011-08-19 10:45:03 +0100 |
---|---|---|
committer | Igor Wiedler <igor@wiedler.ch> | 2012-03-31 02:09:13 +0200 |
commit | dce38f44de04bd7a1f91f8e57f6d266bd5e1af86 (patch) | |
tree | 8c12244b1c2e052e03c06675848bff5d2297859a | |
parent | 94172b54dd09b28e19b4b12933b3f96e498d264a (diff) | |
download | forums-dce38f44de04bd7a1f91f8e57f6d266bd5e1af86.tar forums-dce38f44de04bd7a1f91f8e57f6d266bd5e1af86.tar.gz forums-dce38f44de04bd7a1f91f8e57f6d266bd5e1af86.tar.bz2 forums-dce38f44de04bd7a1f91f8e57f6d266bd5e1af86.tar.xz forums-dce38f44de04bd7a1f91f8e57f6d266bd5e1af86.zip |
[ticket/10328] Added a JSON class.
The JSON class adds a consistent way to send JSON to the client, making
it perfect for AJAX (jQuery automatically parses it).
PHPBB3-10328
-rw-r--r-- | phpBB/common.php | 1 | ||||
-rw-r--r-- | phpBB/includes/acp/acp_forums.php | 3 | ||||
-rw-r--r-- | phpBB/includes/functions.php | 6 | ||||
-rw-r--r-- | phpBB/includes/json.php | 59 | ||||
-rw-r--r-- | phpBB/styles/script.js | 3 |
5 files changed, 63 insertions, 9 deletions
diff --git a/phpBB/common.php b/phpBB/common.php index 129f7e4881..2c90ccf76c 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -74,6 +74,7 @@ if (!empty($load_extensions) && function_exists('dl')) // Include files require($phpbb_root_path . 'includes/class_loader.' . $phpEx); +require($phpbb_root_path . 'includes/json.' . $phpEx); require($phpbb_root_path . 'includes/session.' . $phpEx); require($phpbb_root_path . 'includes/auth.' . $phpEx); diff --git a/phpBB/includes/acp/acp_forums.php b/phpBB/includes/acp/acp_forums.php index cb410e361a..6c05b1e108 100644 --- a/phpBB/includes/acp/acp_forums.php +++ b/phpBB/includes/acp/acp_forums.php @@ -258,8 +258,7 @@ class acp_forums if ($request->is_ajax()) { - echo json_encode(array('success' => ($move_forum_name !== false))); - exit; + JSON::send(array('success' => ($move_forum_name !== false))); } break; diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index b126fcc709..b9ca30279f 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -2713,7 +2713,7 @@ function confirm_box($check, $title = '', $hidden = '', $html_body = 'confirm_bo if ($request->is_ajax()) { $u_action .= '&confirm_uid=' . $user->data['user_id'] . '&sess=' . $user->session_id . '&sid=' . $user->session_id; - echo json_encode(array( + JSON::send(array( 'MESSAGE_TITLE' => (!isset($user->lang[$title])) ? $user->lang['CONFIRM'] : $user->lang[$title], 'MESSAGE_TEXT' => (!isset($user->lang[$title . '_CONFIRM'])) ? $title : $user->lang[$title . '_CONFIRM'], @@ -2721,7 +2721,6 @@ function confirm_box($check, $title = '', $hidden = '', $html_body = 'confirm_bo 'S_CONFIRM_ACTION' => str_replace('&', '&', $u_action), //inefficient, rewrite whole function 'S_HIDDEN_FIELDS' => $hidden . $s_hidden_fields )); - exit; } if (defined('IN_ADMIN') && isset($user->data['session_admin']) && $user->data['session_admin']) @@ -3951,14 +3950,13 @@ function msg_handler($errno, $msg_text, $errfile, $errline) { global $refresh_data; - echo json_encode(array( + JSON::send(array( 'MESSAGE_TITLE' => $msg_title, 'MESSAGE_TEXT' => $msg_text, 'S_USER_WARNING' => ($errno == E_USER_WARNING) ? true : false, 'S_USER_NOTICE' => ($errno == E_USER_NOTICE) ? true : false, 'REFRESH_DATA' => (!empty($refresh_data)) ? $refresh_data : null )); - exit; } // We do not want the cron script to be called on error messages diff --git a/phpBB/includes/json.php b/phpBB/includes/json.php new file mode 100644 index 0000000000..04472080d9 --- /dev/null +++ b/phpBB/includes/json.php @@ -0,0 +1,59 @@ +<?php +/** +* +* @package phpBB3 +* @version $Id$ +* @copyright (c) 2005 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* JSON class +* @package phpBB3 +*/ +class JSON +{ + private static $data = array(); + + /** + * Send the data to the client and exit the script. + * + * @param array $data Any additional data to send. + * @param bool $exit Will exit the script if true. + */ + public static function send($data = false, $exit = true) + { + if ($data) + { + self::add($data); + } + + header('Content-type: application/json'); + echo json_encode(self::$data); + + if ($exit) + { + garbage_collection(); + exit_handler(); + } + } + + /** + * Saves some data to be written when JSON::send() is called. + * + * @param array $data Data to save to be sent. + */ + public static function add($data) + { + self::$data = array_merge(self::$data, $data); + } +} diff --git a/phpBB/styles/script.js b/phpBB/styles/script.js index 54248cc31c..3ed12bfee8 100644 --- a/phpBB/styles/script.js +++ b/phpBB/styles/script.js @@ -216,8 +216,6 @@ phpbb.ajaxify = function(options, refresh, callback) { function return_handler(res) { - res = JSON.parse(res); - if (typeof res.S_CONFIRM_ACTION === 'undefined') { /** @@ -243,7 +241,6 @@ phpbb.ajaxify = function(options, refresh, callback) { path = res.S_CONFIRM_ACTION; phpbb.loading_alert(); $.post(path, data + '&confirm=' + res.YES_VALUE, function(res) { - res = JSON.parse(res); var alert = phpbb.alert(res.MESSAGE_TITLE, res.MESSAGE_TEXT); callback = phpbb.ajax_callbacks[callback]; if (typeof callback === 'function') |