diff options
author | Bart Visscher <bartv@thisnet.nl> | 2011-09-23 22:22:59 +0200 |
---|---|---|
committer | Bart Visscher <bartv@thisnet.nl> | 2011-09-25 22:19:28 +0200 |
commit | 17e631bc5e327514596ce8761fe7f93d414a8717 (patch) | |
tree | a2e6bed923a493988028adafaaebcab5b9bfbd39 /files | |
parent | dbddec9160338818009ec7020cec5a2b298aae7e (diff) | |
download | nextcloud-server-17e631bc5e327514596ce8761fe7f93d414a8717.tar.gz nextcloud-server-17e631bc5e327514596ce8761fe7f93d414a8717.zip |
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.
Diffstat (limited to 'files')
-rw-r--r-- | files/ajax/autocomplete.php | 11 | ||||
-rw-r--r-- | files/ajax/delete.php | 19 | ||||
-rw-r--r-- | files/ajax/list.php | 11 | ||||
-rw-r--r-- | files/ajax/move.php | 15 | ||||
-rw-r--r-- | files/ajax/newfolder.php | 15 | ||||
-rw-r--r-- | files/ajax/rename.php | 13 | ||||
-rw-r--r-- | files/ajax/upload.php | 20 |
7 files changed, 28 insertions, 76 deletions
diff --git a/files/ajax/autocomplete.php b/files/ajax/autocomplete.php index 183ee86c788..8d7a5b482bd 100644 --- a/files/ajax/autocomplete.php +++ b/files/ajax/autocomplete.php @@ -5,14 +5,7 @@ // Init owncloud 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(); // Get data $query = $_GET['term']; @@ -58,6 +51,6 @@ if(OC_Filesystem::file_exists($base) and OC_Filesystem::is_dir($base)){ } } } -echo json_encode($files); +OC_JSON::encodedPrint($files); ?> diff --git a/files/ajax/delete.php b/files/ajax/delete.php index 782db215dfc..b6bc859897c 100644 --- a/files/ajax/delete.php +++ b/files/ajax/delete.php @@ -3,14 +3,7 @@ // Init owncloud 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(); // Get data $dir = $_GET["dir"]; @@ -18,19 +11,19 @@ $files = isset($_GET["file"]) ? $_GET["file"] : $_GET["files"]; $files = explode(';', $files); $filesWithError = ''; -$status = 'success'; +$success = true; //Now delete foreach($files as $file) { if( !OC_Files::delete( $dir, $file )){ $filesWithError .= $file . "\n"; - $status = 'error'; + $success = false; } } -if($status == 'success') { - echo json_encode( array( "status" => $status, "data" => array( "dir" => $dir, "files" => $files ))); +if(success) { + OC_JSON::success(array("data" => array( "dir" => $dir, "files" => $files ))); } else { - echo json_encode( array( "status" => $status, "data" => array( "message" => "Could not delete:\n" . $filesWithError ))); + OC_JSON::error(array("data" => array( "message" => "Could not delete:\n" . $filesWithError ))); } ?> diff --git a/files/ajax/list.php b/files/ajax/list.php index 547bc91fb05..8a414827e1c 100644 --- a/files/ajax/list.php +++ b/files/ajax/list.php @@ -3,14 +3,7 @@ // Init owncloud 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(); // Load the files $dir = isset( $_GET['dir'] ) ? $_GET['dir'] : ''; @@ -45,6 +38,6 @@ $list = new OC_Template( "files", "part.list", "" ); $list->assign( "files", $files ); $data = array('files' => $list->fetchPage()); -echo json_encode( array( "status" => "success", "data" => $data)); +OC_JSON::success(array('data' => $data)); ?> diff --git a/files/ajax/move.php b/files/ajax/move.php index 4224cbce6d0..8a56a015486 100644 --- a/files/ajax/move.php +++ b/files/ajax/move.php @@ -3,14 +3,7 @@ // Init owncloud 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(); // Get data $dir = $_GET["dir"]; @@ -19,9 +12,9 @@ $target = $_GET["target"]; if(OC_Files::move($dir,$file,$target,$file)){ - echo json_encode( array( "status" => 'success', "data" => array( "dir" => $dir, "files" => $file ))); + OC_JSON::success(array("data" => array( "dir" => $dir, "files" => $file ))); }else{ - echo json_encode( array( "status" => 'error', "data" => array( "message" => "Could move $file" ))); + OC_JSON::error(array("data" => array( "message" => "Could move $file" ))); } -?>
\ No newline at end of file +?> diff --git a/files/ajax/newfolder.php b/files/ajax/newfolder.php index 8eb05280e73..43d87461fc7 100644 --- a/files/ajax/newfolder.php +++ b/files/ajax/newfolder.php @@ -3,27 +3,20 @@ // Init owncloud 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(); // Get the params $dir = isset( $_GET['dir'] ) ? $_GET['dir'] : ''; $foldername = isset( $_GET['foldername'] ) ? $_GET['foldername'] : ''; if($foldername == '') { - echo json_encode( array( "status" => "error", "data" => array( "message" => "Empty Foldername" ))); + OC_JSON::error(array("data" => array( "message" => "Empty Foldername" ))); exit(); } if(defined("DEBUG") && DEBUG) {error_log('try to create ' . $foldername . ' in ' . $dir);} if(OC_Files::newFile($dir, $foldername, 'dir')) { - echo json_encode( array( "status" => "success", "data" => array())); + OC_JSON::success(array("data" => array())); exit(); } -echo json_encode( array( "status" => "error", "data" => array( "message" => "Error when creating the folder" )));
\ No newline at end of file +OC_JSON::error(array("data" => array( "message" => "Error when creating the folder" ))); diff --git a/files/ajax/rename.php b/files/ajax/rename.php index 516077f6fda..87ffbc3ada0 100644 --- a/files/ajax/rename.php +++ b/files/ajax/rename.php @@ -3,14 +3,7 @@ // Init owncloud 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(); // Get data $dir = $_GET["dir"]; @@ -19,10 +12,10 @@ $newname = $_GET["newname"]; // Delete if( OC_Files::move( $dir, $file, $dir, $newname )) { - echo json_encode( array( "status" => "success", "data" => array( "dir" => $dir, "file" => $file, "newname" => $newname ))); + OC_JSON::success(array("data" => array( "dir" => $dir, "file" => $file, "newname" => $newname ))); } else{ - echo json_encode( array( "status" => "error", "data" => array( "message" => "Unable to rename file" ))); + OC_JSON::error(array("data" => array( "message" => "Unable to rename file" ))); } ?> diff --git a/files/ajax/upload.php b/files/ajax/upload.php index f005a8af226..041ec0c92e3 100644 --- a/files/ajax/upload.php +++ b/files/ajax/upload.php @@ -3,19 +3,13 @@ // Init owncloud require_once('../../lib/base.php'); -// We send json data -// header( "Content-Type: application/json" ); // Firefox and Konqueror tries to download application/json for me. --Arthur -header( "Content-Type: text/plain" ); +OC_JSON::setContentTypeHeader('text/plain'); -// 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(); if (!isset($_FILES['files'])) { - echo json_encode( array( "status" => "error", "data" => array( "message" => "No file was uploaded. Unknown error" ))); + OC_JSON::error(array("data" => array( "message" => "No file was uploaded. Unknown error" ))); exit(); } foreach ($_FILES['files']['error'] as $error) { @@ -28,7 +22,7 @@ foreach ($_FILES['files']['error'] as $error) { 4=>$l->t("No file was uploaded"), 6=>$l->t("Missing a temporary folder") ); - echo json_encode( array( "status" => "error", "data" => array( "message" => $errors[$error] ))); + OC_JSON::error(array("data" => array( "message" => $errors[$error] ))); exit(); } } @@ -43,7 +37,7 @@ foreach($files['size'] as $size){ $totalSize+=$size; } if($totalSize>OC_Filesystem::free_space('/')){ - echo json_encode( array( "status" => "error", "data" => array( "message" => "Not enough space available" ))); + OC_JSON::error(array("data" => array( "message" => "Not enough space available" ))); exit(); } @@ -56,12 +50,12 @@ if(strpos($dir,'..') === false){ $result[]=array( "status" => "success", 'mime'=>OC_Filesystem::getMimeType($target),'size'=>OC_Filesystem::filesize($target),'name'=>$files['name'][$i]); } } - echo json_encode($result); + OC_JSON::encodedPrint($result); exit(); }else{ $error='invalid dir'; } -echo json_encode(array( 'status' => 'error', 'data' => array('error' => $error, "file" => $fileName))); +OC_JSON::error(array('data' => array('error' => $error, "file" => $fileName))); ?> |