\OCPJSON

This class provides convenient functions to generate and send JSON data. Useful for Ajax calls

Summary

Methods
Properties
Constants
encodedPrint()
checkLoggedIn()
callCheck()
success()
error()
setContentTypeHeader()
checkAppEnabled()
checkAdminUser()
encode()
checkUserExists()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

encodedPrint()

encodedPrint(array  $data, boolean  $setContentType = true) 

Encode and print $data in JSON format

Parameters

array $data

The data to use

boolean $setContentType

the optional content type

checkLoggedIn()

checkLoggedIn() 

Check if the user is logged in, send json error msg if not.

This method checks if a user is logged in. If not, a json error response will be return and the method will exit from execution of the script. The returned json will be in the format:

{"status":"error","data":{"message":"Authentication error."}}

Add this call to the start of all ajax method files that requires an authenticated user.

callCheck()

callCheck() 

Check an ajax get/post call if the request token is valid.

This method checks for a valid variable 'requesttoken' in $_GET, $_POST and $_SERVER. If a valid token is not found, a json error response will be return and the method will exit from execution of the script. The returned json will be in the format:

{"status":"error","data":{"message":"Token expired. Please reload page."}}

Add this call to the start of all ajax method files that creates, updates or deletes anything. In cases where you e.g. use an ajax call to load a dialog containing a submittable form, you will need to add the requesttoken first as a parameter to the ajax call, then assign it to the template and finally add a hidden input field also named 'requesttoken' containing the value.

success()

success(array  $data = array()) : string

Send json success msg

Return a json success message with optional extra data.

Parameters

array $data

The data to use

Returns

string —

json formatted string.

error()

error(array  $data = array()) : string

Send json error msg

Return a json error message with optional extra data for error message or app specific data.

Example use:

$id = [some value]
OCP\JSON::error(array('data':array('message':'An error happened', 'id': $id)));

Will return the json formatted string:

{"status":"error","data":{"message":"An error happened", "id":[some value]}}

Parameters

array $data

The data to use

Returns

string —

json formatted error string.

setContentTypeHeader()

setContentTypeHeader(string  $type = 'application/json') 

Set Content-Type header to jsonrequest

Parameters

string $type

The content type header

checkAppEnabled()

checkAppEnabled(string  $app) 

Check if the App is enabled and send JSON error message instead

This method checks if a specific app is enabled. If not, a json error response will be return and the method will exit from execution of the script. The returned json will be in the format:

{"status":"error","data":{"message":"Application is not enabled."}}

Add this call to the start of all ajax method files that requires a specific app to be enabled.

Parameters

string $app

The app to check

checkAdminUser()

checkAdminUser() 

Check if the user is a admin, send json error msg if not

This method checks if the current user has admin rights. If not, a json error response will be return and the method will exit from execution of the script. The returned json will be in the format:

{"status":"error","data":{"message":"Authentication error."}}

Add this call to the start of all ajax method files that requires administrative rights.

encode()

encode(array  $data) : string

Encode JSON

Parameters

array $data

Returns

string

checkUserExists()

checkUserExists(string  $user) 

Check is a given user exists - send json error msg if not

Parameters

string $user