From ceb4b25166c0b62760348c3c16baa5da1fcfbd42 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Fri, 23 Sep 2011 22:22:59 +0200 Subject: [PATCH] Use OC_JSON for json responses Create OC_JSON class, for single point of creating json responses. No real logic change, this just cleans up the code a bit. --- ajax/addBookmark.php | 10 ++-------- ajax/delBookmark.php | 10 ++-------- ajax/editBookmark.php | 8 +------- ajax/getMeta.php | 10 ++-------- ajax/recordClick.php | 6 +----- ajax/updateList.php | 10 ++-------- 6 files changed, 10 insertions(+), 44 deletions(-) diff --git a/ajax/addBookmark.php b/ajax/addBookmark.php index 7cf5baa4..9b0beb38 100644 --- a/ajax/addBookmark.php +++ b/ajax/addBookmark.php @@ -26,14 +26,8 @@ $RUNTIME_NOSETUPFS=true; require_once('../../../lib/base.php'); -// We send json data -header( "Content-Type: application/jsonrequest" ); - // Check if we are a user -if( !OC_User::isLoggedIn()){ - echo json_encode( array( "status" => "error", "data" => array( "message" => "Authentication error" ))); - exit(); -} +OC_JSON::checkLoggedIn(); $CONFIG_DBTYPE = OC_Config::getValue( "dbtype", "sqlite" ); if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){ @@ -76,6 +70,6 @@ if($b_id !== false) { $query->execute($params); } - echo json_encode( array( 'status' => 'success', 'data' => $b_id)); + OC_JSON::success(array('data' => $b_id)); } diff --git a/ajax/delBookmark.php b/ajax/delBookmark.php index bf1611fe..afe60f7d 100644 --- a/ajax/delBookmark.php +++ b/ajax/delBookmark.php @@ -26,14 +26,8 @@ $RUNTIME_NOSETUPFS=true; require_once('../../../lib/base.php'); -// We send json data -header( "Content-Type: application/jsonrequest" ); - // Check if we are a user -if( !OC_User::isLoggedIn()){ - echo json_encode( array( "status" => "error", "data" => array( "message" => "Authentication error" ))); - exit(); -} +OC_JSON::checkLoggedIn(); $params=array( htmlspecialchars_decode($_GET["url"]), @@ -64,4 +58,4 @@ $query = OC_DB::prepare(" $result = $query->execute(); // var_dump($params); -echo json_encode( array( "status" => "success", "data" => array())); +OC_JSON::success(array('data' => array())); diff --git a/ajax/editBookmark.php b/ajax/editBookmark.php index 1bd2fc08..5125f9ce 100644 --- a/ajax/editBookmark.php +++ b/ajax/editBookmark.php @@ -26,14 +26,8 @@ $RUNTIME_NOSETUPFS=true; require_once('../../../lib/base.php'); -// We send json data -header( 'Content-Type: application/jsonrequest' ); - // Check if we are a user -if( !OC_User::isLoggedIn()){ - echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => 'Authentication error' ))); - exit(); -} +OC_JSON::checkLoggedIn(); $CONFIG_DBTYPE = OC_Config::getValue( "dbtype", "sqlite" ); if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){ diff --git a/ajax/getMeta.php b/ajax/getMeta.php index e9fe0d68..4583ef20 100644 --- a/ajax/getMeta.php +++ b/ajax/getMeta.php @@ -26,14 +26,8 @@ $RUNTIME_NOSETUPFS=true; require_once('../../../lib/base.php'); -// We send json data -header( 'Content-Type: application/jsonrequest' ); - // Check if we are a user -if( !OC_User::isLoggedIn()){ - echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => 'Authentication error' ))); - exit(); -} +OC_JSON::checkLoggedIn(); // $metadata = array(); @@ -41,4 +35,4 @@ require '../bookmarksHelper.php'; $metadata = getURLMetadata(htmlspecialchars_decode($_GET["url"])); -echo json_encode( array( 'status' => 'success', 'data' => $metadata)); +OC_JSON::success(array('data' => $metadata)); diff --git a/ajax/recordClick.php b/ajax/recordClick.php index 116daea8..f5f7c20c 100644 --- a/ajax/recordClick.php +++ b/ajax/recordClick.php @@ -27,11 +27,7 @@ $RUNTIME_NOSETUPFS=true; require_once('../../../lib/base.php'); // Check if we are a user -if( !OC_User::isLoggedIn()){ - header( "Content-Type: application/jsonrequest" ); - echo json_encode( array( "status" => "error", "data" => array( "message" => "Authentication error" ))); - exit(); -} +OC_JSON::checkLoggedIn(); $query = OC_DB::prepare(" UPDATE *PREFIX*bookmarks diff --git a/ajax/updateList.php b/ajax/updateList.php index e9051a8d..de3480d6 100644 --- a/ajax/updateList.php +++ b/ajax/updateList.php @@ -26,14 +26,8 @@ $RUNTIME_NOSETUPFS=true; require_once('../../../lib/base.php'); -// We send json data -header( 'Content-Type: application/jsonrequest' ); - // Check if we are a user -if( !OC_User::isLoggedIn()){ - echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => 'Authentication error' ))); - exit(); -} +OC_JSON::checkLoggedIn(); $params=array(OC_User::getUser()); $CONFIG_DBTYPE = OC_Config::getValue( 'dbtype', 'sqlite' ); @@ -85,4 +79,4 @@ $query = OC_DB::prepare(' $bookmarks = $query->execute($params)->fetchAll(); -echo json_encode( array( 'status' => 'success', 'data' => $bookmarks)); +OC_JSON::success(array('data' => $bookmarks));