]> source.dussan.org Git - nextcloud-server.git/commitdiff
Use OC_JSON for json responses
authorBart Visscher <bartv@thisnet.nl>
Fri, 23 Sep 2011 20:22:59 +0000 (22:22 +0200)
committerBart Visscher <bartv@thisnet.nl>
Sun, 25 Sep 2011 20:19:28 +0000 (22:19 +0200)
Create OC_JSON class, for single point of creating json responses.
No real logic change, this just cleans up the code a bit.

54 files changed:
apps/bookmarks/ajax/addBookmark.php
apps/bookmarks/ajax/delBookmark.php
apps/bookmarks/ajax/editBookmark.php
apps/bookmarks/ajax/getMeta.php
apps/bookmarks/ajax/recordClick.php
apps/bookmarks/ajax/updateList.php
apps/calendar/ajax/createcalendar.php
apps/calendar/ajax/deletecalendar.php
apps/calendar/ajax/deleteevent.php
apps/calendar/ajax/editevent.php
apps/calendar/ajax/newevent.php
apps/calendar/ajax/settimezone.php
apps/calendar/ajax/updatecalendar.php
apps/calendar/templates/part.getcal.php
apps/contacts/ajax/addcard.php
apps/contacts/ajax/addproperty.php
apps/contacts/ajax/deletebook.php
apps/contacts/ajax/deletecard.php
apps/contacts/ajax/deleteproperty.php
apps/contacts/ajax/getdetails.php
apps/contacts/ajax/setproperty.php
apps/contacts/ajax/showaddcard.php
apps/contacts/ajax/showaddproperty.php
apps/contacts/ajax/showsetproperty.php
apps/files_sharing/ajax/getitem.php
apps/files_sharing/ajax/userautocomplete.php
apps/media/ajax/api.php
apps/media/ajax/autoupdate.php
apps/media/tomahawk.php
core/ajax/grouplist.php
core/ajax/translations.php
core/ajax/userlist.php
core/ajax/validateuser.php
files/ajax/autocomplete.php
files/ajax/delete.php
files/ajax/list.php
files/ajax/move.php
files/ajax/newfolder.php
files/ajax/rename.php
files/ajax/upload.php
lib/json.php [new file with mode: 0644]
search/ajax/search.php
settings/ajax/changepassword.php
settings/ajax/creategroup.php
settings/ajax/createuser.php
settings/ajax/disableapp.php
settings/ajax/enableapp.php
settings/ajax/openid.php
settings/ajax/removegroup.php
settings/ajax/removeuser.php
settings/ajax/setlanguage.php
settings/ajax/setquota.php
settings/ajax/togglegroups.php
settings/templates/apps.php

index 7cf5baa4a6e55bded3a3d58d6ec4c49c22b035f3..9b0beb388a0875f8773fa6841bae8f9801b487eb 100644 (file)
@@ -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));
 }
 
index bf1611fe5c1fff4d6dda7ce6155fd53bfd8d3251..afe60f7d1bf7f919fbcec7bf9120d31059859a8d 100644 (file)
@@ -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()));
index 1bd2fc08bcad8c3d5cc296ecbb8a47dc04f14bbc..5125f9ce8989b2e55a62bf7fd96ec462c3a81b0a 100644 (file)
@@ -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' ){
index e9fe0d684dcc8fdbc27b553eb911b4a78c351b2e..4583ef204b477922298ac2d7a90a8c6d96e381fa 100644 (file)
@@ -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));
index 116daea8bbb593267025e5caa611f07a065dc3d8..f5f7c20c6a005118ec034b1e6dd7f850a3709013 100644 (file)
@@ -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
index e9051a8dbfa32ffd5ee5dac725afdb47954207b5..de3480d6c3a5b6c3399d66aa7b9781171deba570 100644 (file)
@@ -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));
index 64c6513f53f9a8a62ac73a8831a8e77f0d5e5d5a..7d80333b258a3cf426d08f4231b192124ec062e5 100644 (file)
@@ -10,14 +10,8 @@ require_once('../../../lib/base.php');
 
 $l10n = new OC_L10N('calendar');
 
-// 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" => $l->t("Authentication error") )));
-       exit();
-}
+OC_JSON::checkLoggedIn();
 
 $userid = OC_User::getUser();
 $calendarid = OC_Calendar_Calendar::addCalendar($userid, $_POST['name'], $_POST['description'], 'VEVENT,VTODO,VJOURNAL', null, 0, $_POST['color']);
@@ -25,4 +19,4 @@ OC_Calendar_Calendar::setCalendarActive($calendarid, 1);
 $calendar = OC_Calendar_Calendar::findCalendar($calendarid);
 $tmpl = new OC_Template('calendar', 'part.choosecalendar.rowfields');
 $tmpl->assign('calendar', $calendar);
-echo json_encode( array( "status" => "error", "data" => $tmpl->fetchPage().'' ));
+OC_JSON::success(array('data' => $tmpl->fetchPage()));
index 71129f2c040d5dc805c8c199047e61b3453a7921..30607b92e6fdaa585c708bbf0a93a7358bf9b92d 100644 (file)
@@ -16,13 +16,13 @@ if(!OC_USER::isLoggedIn()) {
 $cal = $_POST["calendarid"];
 $calendar = OC_Calendar_Calendar::findCalendar($cal);
 if($calendar["userid"] != OC_User::getUser()){
-       echo json_encode(array('status'=>'error','error'=>'permission_denied'));
+       OC_JSON::error(array('error'=>'permission_denied'));
        exit;
 }
 $del = OC_Calendar_Calendar::deleteCalendar($cal);
 if($del == true){
-       echo json_encode(array('status' => 'success'));
+       OC_JSON::success();
 }else{
-       echo json_encode(array('status'=>'error', 'error'=>'dberror'));
+       OC_JSON::error(array('error'=>'dberror'));
 }
 ?> 
index 08a0e1a1e2698637ec5ffcd4519398728011e0b4..a6750267bd218a0fcdfee864c3c9b9e2d5972321 100644 (file)
@@ -17,14 +17,14 @@ $id = $_POST['id'];
 $data = OC_Calendar_Object::find($id);
 if (!$data)
 {
-       echo json_encode(array('status'=>'error'));
+       OC_JSON::error();
        exit;
 }
 $calendar = OC_Calendar_Calendar::findCalendar($data['calendarid']);
 if($calendar['userid'] != OC_User::getUser()){
-       echo json_encode(array('status'=>'error'));
+       OC_JSON::error();
        exit;
 }
 $result = OC_Calendar_Object::delete($id);
-echo json_encode(array('status' => 'success'));
+OC_JSON::success();
 ?> 
index 5659e7e3c1cd27552bd3974c171a9c0cf8130bac..7187e05d56f6b078be87fa9ac623141cafa325f7 100644 (file)
@@ -17,8 +17,7 @@ if(!OC_USER::isLoggedIn()) {
 $errarr = OC_Calendar_Object::validateRequest($_POST);
 if($errarr){
        //show validate errors
-       $errarr['status'] = 'error';
-       echo json_encode($errarr);
+       OC_JSON::error($errarr);
        exit;
 }else{
        $id = $_POST['id'];
@@ -26,12 +25,12 @@ if($errarr){
        $data = OC_Calendar_Object::find($id);
        if (!$data)
        {
-               echo json_encode(array('status'=>'error'));
+               OC_JSON::error();
                exit;
        }
        $calendar = OC_Calendar_Calendar::findCalendar($data['calendarid']);
        if($calendar['userid'] != OC_User::getUser()){
-               echo json_encode(array('status'=>'error'));
+               OC_JSON::error();
                exit;
        }
        $vcalendar = Sabre_VObject_Reader::read($data['calendardata']);
@@ -40,6 +39,6 @@ if($errarr){
        if ($data['calendarid'] != $cal) {
                OC_Calendar_Object::moveToCalendar($id, $cal);
        }
-       echo json_encode(array('status' => 'success'));
+       OC_JSON::success();
 }
-?> 
+?>
index f3cca1cee4669b8a6039c6c52a97e757914b1df6..9ac3b0aaff6fd1e45b9e4a1657781fa1c917bd2a 100644 (file)
@@ -17,13 +17,12 @@ if(!OC_USER::isLoggedIn()) {
 $errarr = OC_Calendar_Object::validateRequest($_POST);
 if($errarr){
        //show validate errors
-       $errarr['status'] = 'error';
-       echo json_encode($errarr);
+       OC_JSON::error($errarr);
        exit;
 }else{
        $cal = $_POST['calendar'];
        $vcalendar = OC_Calendar_Object::createVCalendarFromRequest($_POST);
        $result = OC_Calendar_Object::add($cal, $vcalendar->serialize());
-       echo json_encode(array('status'=>'success'));
+       OC_JSON::success();
 }
 ?>
index a07b56ee9ae722a2eeaa8556cb24718ee578c9c1..2b82bc8e4bca9fd57d790447be607497c9a99354 100644 (file)
@@ -11,22 +11,16 @@ require_once('../../../lib/base.php');
 
 $l=new OC_L10N('calendar');
 
-// 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" => $l->t("Authentication error") )));
-       exit();
-}
+OC_JSON::checkLoggedIn();
 
 // Get data
 if( isset( $_POST['timezone'] ) ){
        $timezone=$_POST['timezone'];
        OC_Preferences::setValue( OC_User::getUser(), 'calendar', 'timezone', $timezone );
-       echo json_encode( array( "status" => "success", "data" => array( "message" => $l->t("Timezone changed") )));
+       OC_JSON::success(array('data' => array( 'message' => $l->t('Timezone changed') )));
 }else{
-       echo json_encode( array( "status" => "error", "data" => array( "message" => $l->t("Invalid request") )));
+       OC_JSON::error(array('data' => array( 'message' => $l->t('Invalid request') )));
 }
 
 ?>
index efb0b99bad76b08204a3d0ab4318309e77210098..d53515d0deb707562fb70666badbf6a5f4427b5d 100644 (file)
@@ -10,14 +10,8 @@ require_once('../../../lib/base.php');
 
 $l10n = new OC_L10N('calendar');
 
-// 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" => $l->t("Authentication error") )));
-       exit();
-}
+OC_JSON::checkLoggedIn();
 
 $calendarid = $_POST['id'];
 OC_Calendar_Calendar::editCalendar($calendarid, $_POST['name'], $_POST['description'], null, null, null, $_POST['color']);
@@ -25,4 +19,4 @@ OC_Calendar_Calendar::setCalendarActive($calendarid, $_POST['active']);
 $calendar = OC_Calendar_Calendar::findCalendar($calendarid);
 $tmpl = new OC_Template('calendar', 'part.choosecalendar.rowfields');
 $tmpl->assign('calendar', $calendar);
-echo json_encode( array( "status" => "success", "data" => $tmpl->fetchPage() ));
+OC_JSON::success(array('data' => $tmpl->fetchPage()));
index 35d08c85b3a0636b9a832fe2bf8e96a57d5a2c79..900a43b3df2620302c401c5045824fc74d8e4a07 100644 (file)
@@ -53,5 +53,5 @@ foreach($events as $event)
                $return_events[$year][$month][$day][$hour] = array(1 => $return_event);
        }
 }
-echo json_encode($return_events);
+OC_JSON::encodedPrint($return_events);
 ?>
index 6005d74d14428c5f042f214b504867a5072d6251..cfae3327f5694019e001fe656d24e7aa72707bb6 100644 (file)
@@ -27,14 +27,11 @@ $aid = $_POST['id'];
 $l10n = new OC_L10N('contacts');
 
 // Check if we are a user
-if( !OC_User::isLoggedIn()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('You need to log in.'))));
-       exit();
-}
+OC_JSON::checkLoggedIn();
 
 $addressbook = OC_Contacts_Addressbook::find( $aid );
 if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your addressbook.')))); // Same here (as with the contact error). Could this error be improved?
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your addressbook.')))); // Same here (as with the contact error). Could this error be improved?
        exit();
 }
 
@@ -51,4 +48,4 @@ $tmpl->assign('details',$details);
 $tmpl->assign('id',$id);
 $page = $tmpl->fetchPage();
 
-echo json_encode( array( 'status' => 'success', 'data' => array( 'id' => $id, 'page' => $page )));
+OC_JSON::success(array('data' => array( 'id' => $id, 'page' => $page )));
index a311bba6e26bdb5c38c38bac451fd6fc107b3c1b..5a37f77f858ddfe890d886f527813e01f5562e3a 100644 (file)
@@ -27,27 +27,24 @@ $id = $_POST['id'];
 $l10n = new OC_L10N('contacts');
 
 // Check if we are a user
-if( !OC_User::isLoggedIn()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('You need to log in.'))));
-       exit();
-}
+OC_JSON::checkLoggedIn();
 
 $card = OC_Contacts_VCard::find( $id );
 if( $card === false ){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Contact could not be found.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('Contact could not be found.'))));
        exit();
 }
 
 $addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
 if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your contact.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
        exit();
 }
 
 $vcard = OC_Contacts_VCard::parse($card['carddata']);
 // Check if the card is valid
 if(is_null($vcard)){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('vCard could not be read.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('vCard could not be read.'))));
        exit();
 }
 
@@ -75,4 +72,4 @@ $tmpl = new OC_Template('contacts','part.property');
 $tmpl->assign('property',OC_Contacts_VCard::structureProperty($property,$line));
 $page = $tmpl->fetchPage();
 
-echo json_encode( array( 'status' => 'success', 'data' => array( 'page' => $page )));
+OC_JSON::success(array('data' => array( 'page' => $page )));
index 38322f5c10936b63a636953148c7fa03293be119..13be33eb5a9ef114ceb838dba8651b8a631fcb18 100644 (file)
@@ -28,16 +28,13 @@ $id = $_GET['id'];
 $l10n = new OC_L10N('contacts');
 
 // Check if we are a user
-if( !OC_User::isLoggedIn()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('You need to log in.'))));
-       exit();
-}
+OC_JSON::checkLoggedIn();
 
 $addressbook = OC_Contacts_Addressbook::find( $id );
 if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your contact.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
        exit();
 }
 
 OC_Contacts_Addressbook::delete($id);
-echo json_encode( array( 'status' => 'success', 'data' => array( 'id' => $id )));
+OC_JSON::success(array('data' => array( 'id' => $id )));
index 7dde7d30a52f60d36ee2da9d644b70cf36158e79..c69638320ed3519e8636aae751ef29adcba57d64 100644 (file)
@@ -28,23 +28,19 @@ $id = $_GET['id'];
 $l10n = new OC_L10N('contacts');
 
 // Check if we are a user
-if( !OC_User::isLoggedIn()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('You need to log in.'))));
-       exit();
-}
-
+OC_JSON::checkLoggedIn();
 
 $card = OC_Contacts_VCard::find( $id );
 if( $card === false ){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Contact could not be found.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('Contact could not be found.'))));
        exit();
 }
 
 $addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
 if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your contact.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
        exit();
 }
 
 OC_Contacts_VCard::delete($id);
-echo json_encode( array( 'status' => 'success', 'data' => array( 'id' => $id )));
+OC_JSON::success(array('data' => array( 'id' => $id )));
index 07cea0b53e187902ffcd928145c3b65464c0beee..40b765cf845e934abcb3c3dd6250e44b5f0a577c 100644 (file)
@@ -30,28 +30,24 @@ $checksum = $_GET['checksum'];
 $l10n = new OC_L10N('contacts');
 
 // Check if we are a user
-if( !OC_User::isLoggedIn()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('You need to log in.'))));
-       exit();
-}
-
+OC_JSON::checkLoggedIn();
 
 $card = OC_Contacts_VCard::find( $id );
 if( $card === false ){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Contact could not be found.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('Contact could not be found.'))));
        exit();
 }
 
 $addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
 if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your contact.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
        exit();
 }
 
 $vcard = OC_Contacts_VCard::parse($card['carddata']);
 // Check if the card is valid
 if(is_null($vcard)){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('vCard could not be read.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('vCard could not be read.'))));
        exit();
 }
 
@@ -62,11 +58,11 @@ for($i=0;$i<count($vcard->children);$i++){
        }
 }
 if(is_null($line)){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Information about vCard is incorrect. Please reload the page.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('Information about vCard is incorrect. Please reload the page.'))));
        exit();
 }
 
 unset($vcard->children[$line]);
 
 OC_Contacts_VCard::edit($id,$vcard->serialize());
-echo json_encode( array( 'status' => 'success', 'data' => array( 'id' => $id )));
+OC_JSON::success(array('data' => array( 'id' => $id )));
index ab1f1d66f5ec75c3dc11e775b2b6db222fa140cd..47d88a771e61075440834953c426dfee4a861468 100644 (file)
@@ -28,28 +28,25 @@ $id = $_GET['id'];
 $l10n = new OC_L10N('contacts');
 
 // Check if we are a user
-if( !OC_User::isLoggedIn()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('You need to log in.'))));
-       exit();
-}
+OC_JSON::checkLoggedIn();
 
 
 $card = OC_Contacts_VCard::find( $id );
 if( $card === false ){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Contact could not be found.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('Contact could not be found.'))));
        exit();
 }
 
 $addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
 if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your contact.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
        exit();
 }
 
 $vcard = OC_Contacts_VCard::parse($card['carddata']);
 // Check if the card is valid
 if(is_null($vcard)){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('vCard could not be read.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('vCard could not be read.'))));
        exit();
 }
 
@@ -59,4 +56,4 @@ $tmpl->assign('details',$details);
 $tmpl->assign('id',$id);
 $page = $tmpl->fetchPage();
 
-echo json_encode( array( 'status' => 'success', 'data' => array( 'id' => $id, 'page' => $page )));
+OC_JSON::success(array('data' => array( 'id' => $id, 'page' => $page )));
index 9178a6aac9f6115172d216295bf770547ab1bce2..b4fc2162d90e4979ea1d0f1467ab3f2c551ef3a9 100644 (file)
@@ -28,27 +28,24 @@ $checksum = $_POST['checksum'];
 $l10n = new OC_L10N('contacts');
 
 // Check if we are a user
-if( !OC_User::isLoggedIn()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('You need to log in.'))));
-       exit();
-}
+OC_JSON::checkLoggedIn();
 
 $card = OC_Contacts_VCard::find( $id );
 if( $card === false ){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Contact could not be found.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('Contact could not be found.'))));
        exit();
 }
 
 $addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
 if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your contact.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
        exit();
 }
 
 $vcard = OC_Contacts_VCard::parse($card['carddata']);
 // Check if the card is valid
 if(is_null($vcard)){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('vCard could not be read.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('vCard could not be read.'))));
        exit();
 }
 
@@ -59,7 +56,7 @@ for($i=0;$i<count($vcard->children);$i++){
        }
 }
 if(is_null($line)){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Information about vCard is incorrect. Please reload the page.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('Information about vCard is incorrect. Please reload the page.'))));
        exit();
 }
 
@@ -100,4 +97,4 @@ $tmpl = new OC_Template('contacts','part.property');
 $tmpl->assign('property',OC_Contacts_VCard::structureProperty($vcard->children[$line],$line));
 $page = $tmpl->fetchPage();
 
-echo json_encode( array( 'status' => 'success', 'data' => array( 'page' => $page, 'line' => $line, 'oldchecksum' => $_POST['checksum'] )));
+OC_JSON::success(array('data' => array( 'page' => $page, 'line' => $line, 'oldchecksum' => $_POST['checksum'] )));
index 89c78fcdf54ab0dc872ffa2fd27d85bc9436be3f..58567392d7c19f9d12c7fbbc2f6fba0bdb4434ac 100644 (file)
@@ -26,14 +26,11 @@ require_once('../../../lib/base.php');
 $l10n = new OC_L10N('contacts');
 
 // Check if we are a user
-if( !OC_User::isLoggedIn()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('You need to log in.'))));
-       exit();
-}
+OC_JSON::checkLoggedIn();
 
 $addressbooks = OC_Contacts_Addressbook::all(OC_USER::getUser());
 $tmpl = new OC_Template('contacts','part.addcardform');
 $tmpl->assign('addressbooks',$addressbooks);
 $page = $tmpl->fetchPage();
 
-echo json_encode( array( 'status' => 'success', 'data' => array( 'page' => $page )));
+OC_JSON::success(array('data' => array( 'page' => $page )));
index 718478d42bf2741958e6002ee5c4a2dfee33ddef..0d01b37d8efacffa811d3f318d807c36755b8241 100644 (file)
@@ -27,20 +27,17 @@ $id = $_GET['id'];
 $l10n = new OC_L10N('contacts');
 
 // Check if we are a user
-if( !OC_User::isLoggedIn()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('You need to log in.'))));
-       exit();
-}
+OC_JSON::checkLoggedIn();
 
 $card = OC_Contacts_VCard::find( $id );
 if( $card === false ){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Contact could not be found.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('Contact could not be found.'))));
        exit();
 }
 
 $addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
 if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your contact.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
        exit();
 }
 
@@ -48,4 +45,4 @@ $tmpl = new OC_Template('contacts','part.addpropertyform');
 $tmpl->assign('id',$id);
 $page = $tmpl->fetchPage();
 
-echo json_encode( array( 'status' => 'success', 'data' => array( 'page' => $page )));
+OC_JSON::success(array('data' => array( 'page' => $page )));
index 77b90ea8cc4f4a59450f140d5491bf9c6d4da369..0b30a8e68eceb38de3c3b210eebf963b0f408492 100644 (file)
@@ -28,27 +28,24 @@ $checksum = $_GET['checksum'];
 $l10n = new OC_L10N('contacts');
 
 // Check if we are a user
-if( !OC_User::isLoggedIn()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('You need to log in.'))));
-       exit();
-}
+OC_JSON::checkLoggedIn();
 
 $card = OC_Contacts_VCard::find( $id );
 if( $card === false ){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Contact could not be found.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('Contact could not be found.'))));
        exit();
 }
 
 $addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
 if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your contact.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
        exit();
 }
 
 $vcard = OC_Contacts_VCard::parse($card['carddata']);
 // Check if the card is valid
 if(is_null($vcard)){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('vCard could not be read.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('vCard could not be read.'))));
        exit();
 }
 
@@ -59,7 +56,7 @@ for($i=0;$i<count($vcard->children);$i++){
        }
 }
 if(is_null($line)){
-       echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Information about vCard is incorrect. Please reload the page.'))));
+       OC_JSON::error(array('data' => array( 'message' => $l10n->t('Information about vCard is incorrect. Please reload the page.'))));
        exit();
 }
 
@@ -70,4 +67,4 @@ $tmpl->assign('checksum',$checksum);
 $tmpl->assign('property',OC_Contacts_VCard::structureProperty($vcard->children[$line]));
 $page = $tmpl->fetchPage();
 
-echo json_encode( array( 'status' => 'success', 'data' => array( 'page' => $page )));
+OC_JSON::success(array('data' => array( 'page' => $page )));
index 249af6cfa31b803c29808cea1bf8399936c4e5c7..e7bda0f6144fefcb82c71660636c55fdc6323087 100644 (file)
@@ -30,7 +30,7 @@ while ($source != "" && $source != "/" && $source != "." && $source != $userDire
        $source = dirname($source);
 }
 if (!empty($users)) {
-       echo json_encode($users);
+       OC_JSON::encodedPrint($users);
 }
 
-?>
\ No newline at end of file
+?>
index 6da7afb659c957591e47e8a7e1d3006f8d51e623..a3158cf72d6d283423ef92c2e0656980d5defde3 100644 (file)
@@ -3,10 +3,8 @@ $RUNTIME_NOAPPS = true;
 
 require_once('../../../lib/base.php');
 
-if (!OC_User::isLoggedIn()) {
-       echo json_encode(array("status" => "error", "data" => array("message" => "Authentication error")));
-       exit();
-}
+OC_JSON::checkLoggedIn();
+
 $users = array();
 $ocusers = OC_User::getUsers();
 $self = OC_User::getUser();
@@ -23,6 +21,6 @@ foreach ($groups as $group) {
        $users[] = "<option value='".$group."'>".$group."</option>";
 }
 $users[] = "</optgroup>";
-echo json_encode($users);
+OC_JSON::encodedPrint($users);
 
 ?>
index c5909e4c78b30264d531e55f9e5d854124df4e9e..84ee63344637e7959411d46c04949ffbfb041b56 100644 (file)
@@ -67,7 +67,7 @@ if($arguments['action']){
                        $data['artists']=OC_MEDIA_COLLECTION::getArtists();
                        $data['albums']=OC_MEDIA_COLLECTION::getAlbums();
                        $data['songs']=OC_MEDIA_COLLECTION::getSongs();
-                       echo json_encode($data);
+                       OC_JSON::encodedPrint($data);
                        break;
                case 'scan':
                        OC_DB::beginTransaction();
@@ -81,13 +81,13 @@ if($arguments['action']){
                        echo (OC_MEDIA_SCANNER::scanFile($arguments['path']))?'true':'false';
                        break;
                case 'get_artists':
-                       echo json_encode(OC_MEDIA_COLLECTION::getArtists($arguments['search']));
+                       OC_JSON::encodedPrint(OC_MEDIA_COLLECTION::getArtists($arguments['search']));
                        break;
                case 'get_albums':
-                       echo json_encode(OC_MEDIA_COLLECTION::getAlbums($arguments['artist'],$arguments['search']));
+                       OC_JSON::encodedPrint(OC_MEDIA_COLLECTION::getAlbums($arguments['artist'],$arguments['search']));
                        break;
                case 'get_songs':
-                       echo json_encode(OC_MEDIA_COLLECTION::getSongs($arguments['artist'],$arguments['album'],$arguments['search']));
+                       OC_JSON::encodedPrint(OC_MEDIA_COLLECTION::getSongs($arguments['artist'],$arguments['album'],$arguments['search']));
                        break;
                case 'get_path_info':
                        if(OC_Filesystem::file_exists($arguments['path'])){
@@ -100,7 +100,7 @@ if($arguments['action']){
                                        $song=OC_MEDIA_COLLECTION::getSong($songId);
                                        $song['artist']=OC_MEDIA_COLLECTION::getArtistName($song['song_artist']);
                                        $song['album']=OC_MEDIA_COLLECTION::getAlbumName($song['song_album']);
-                                       echo json_encode($song);
+                                       OC_JSON::encodedPrint($song);
                                }
                        }
                        break;
@@ -129,7 +129,7 @@ if($arguments['action']){
                        OC_Filesystem::readfile($arguments['path']);
                        exit;
                case 'find_music':
-                       echo json_encode(findMusic());
+                       OC_JSON::encodedPrint(findMusic());
                        exit;
        }
 }
index ac3d0650b4bc73d47209c0d26c987dcb70214214..ad103d1c39be197863c77a246b845edcb8ff30f5 100644 (file)
@@ -35,5 +35,5 @@ if(defined("DEBUG") && DEBUG) {error_log((integer)$autoUpdate);}
 
 OC_Preferences::setValue(OC_User::getUser(),'media','autoupdate',(integer)$autoUpdate);
 
-echo json_encode( array( "status" => "success", "data" => $autoUpdate));
-?>
\ No newline at end of file
+OC_JSON::success(array('data' => $autoUpdate));
+?>
index bf0c2c2a7563b13189c04b8c9e7d73974d82fd3c..1db982a35048e9a459140468af079b8750d5d4f0 100644 (file)
@@ -77,5 +77,5 @@ foreach($songs as $song) {
                'score' => (float)1.0
        );
 }
-echo json_encode($results);
-?>
\ No newline at end of file
+OC_JSON::encodedPrint($results);
+?>
index d0d10f7a84e01dd65f1607b5a5cd610bb6e3bd40..cc15102bbc343ece229cfe21bde953fc9638a325 100644 (file)
@@ -44,8 +44,6 @@ foreach( OC_Group::getGroups() as $i ){
         $groups[] = array( "groupname" => $i );
 }
 
-// We send json data
-header( "Content-Type: application/jsonrequest" );
-echo json_encode($groups);
+OC_JSON::encodedPrint($groups);
 
 ?>
index adaf7dcb75800e5d604c4a7f66eef0bf253f3c54..2e436f8d84eb01fa316e5d50bde5c369bbc04520 100644 (file)
@@ -26,9 +26,7 @@ require_once('../../lib/base.php');
 
 $app = $_POST["app"];
 
-// We send json data
-header( "Content-Type: application/jsonrequest" );
 $l = new OC_L10N( $app );
 
-echo json_encode( array( 'status' => 'success', 'data' => $l->getTranslations()));
+OC_JSON::success(array('data' => $l->getTranslations()));
 ?>
index 0485f514550a3aaa9cb686eef0214ecdb8e8e725..c8168eaf460869eca114f43a218eb2720157b020 100644 (file)
@@ -43,8 +43,6 @@ foreach( OC_User::getUsers() as $i ){
                $users[] = array( "username" => $i, "groups" => join( ", ", OC_Group::getUserGroups( $i ) ));
 }
 
-// We send json data
-header( "Content-Type: application/jsonrequest" );
-echo json_encode($users);
+OC_JSON::encodedPrint($users);
 
 ?>
index 032948fc331102fac4e7519b227db16bb4b390c1..258bd50fcad01bd14838e507ecee2856b5418a28 100644 (file)
@@ -30,11 +30,10 @@ if(!isset($_SERVER['PHP_AUTH_USER'])){
         echo 'Valid credentials must be supplied';
         exit();
 } else {
-       header("Content-Type: application/jsonrequest");
         if(OC_User::checkPassword($_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"])){
-               echo json_encode(array("username" => $_SERVER["PHP_AUTH_USER"], "user_valid" => "true"));
+               OC_JSON::encodedPrint(array("username" => $_SERVER["PHP_AUTH_USER"], "user_valid" => "true"));
        } else {
-               echo json_encode(array("username" => $_SERVER["PHP_AUTH_USER"], "user_valid" => "false"));
+               OC_JSON::encodedPrint(array("username" => $_SERVER["PHP_AUTH_USER"], "user_valid" => "false"));
        }
 }
 
index 183ee86c7889d119c5fc1c586de95f673c6112ef..8d7a5b482bd490903705b1661c586d4339f2ab9b 100644 (file)
@@ -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);
 
 ?>
index 782db215dfc2ef74eefd0ec32203efacbb357c4c..b6bc859897c7cbb5dd04065c818d5c13c526cbc1 100644 (file)
@@ -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 )));
 }
 
 ?>
index 547bc91fb052ab1f1ca8e5bdc99c6ef36baa9bf6..8a414827e1c8c43db3dee18a0005ea89e87c17c9 100644 (file)
@@ -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));
 
 ?>
index 4224cbce6d0b4f9d282b98cd9d8ae0de99581ed0..8a56a0154868c763334331a60429099c845f0a14 100644 (file)
@@ -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
+?>
index 8eb05280e73f435f7cbc37b4251c274a227f6b51..43d87461fc72fa1026a6abfe1170ecdcb7ddf599 100644 (file)
@@ -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" )));
index 516077f6fda08b3d6799b9cead6e07515316b5ff..87ffbc3ada0784ee9a88819bb173e1ffa8f5dda8 100644 (file)
@@ -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" )));
 }
 
 ?>
index f005a8af2263c108bf4eb92a3ae7bb0c2d5967fd..041ec0c92e354284fb12d55d123eb94225b81094 100644 (file)
@@ -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)));
 
 ?>
diff --git a/lib/json.php b/lib/json.php
new file mode 100644 (file)
index 0000000..47d5e82
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+/**
+ * Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+class OC_JSON{
+       static protected $send_content_type_header = false;
+       /**
+        * set Content-Type header to jsonrequest
+        */
+       public static function setContentTypeHeader($type='application/jsonrequest'){
+               if (!self::$send_content_type_header){
+                       // We send json data
+                       header( 'Content-Type: '.$type );
+                       self::$send_content_type_header = true;
+               }
+       }
+
+       /**
+       * Check if the user is logged in, send json error msg if not
+       */
+       public static function checkLoggedIn(){
+               if( !OC_User::isLoggedIn()){
+                       $l = new OC_L10N('core');
+                       self::error(array( 'data' => array( 'message' => $l->t('Authentication error') )));
+                       exit();
+               }
+       }
+
+       /**
+       * Check if the user is a admin, send json error msg if not
+       */
+       public static function checkAdminUser(){
+               self::checkLoggedIn();
+               if( !OC_Group::inGroup( OC_User::getUser(), 'admin' )){
+                       $l = new OC_L10N('core');
+                       self::error(array( 'data' => array( 'message' => $l->t('Authentication error') )));
+                       exit();
+               }
+       }
+
+       /**
+       * Send json error msg
+       */
+       public static function error($data = array()){
+               $data['status'] = 'error';
+               self::encodedPrint($data);
+       }
+
+       /**
+       * Send json success msg
+       */
+       public static function success($data = array()){
+               $data['status'] = 'success';
+               self::encodedPrint($data);
+       }
+
+       /**
+       * Encode and print $data in json format
+       */
+       public static function encodedPrint($data){
+               self::setContentTypeHeader();
+               echo json_encode($data);
+       }
+}
index 9472f97e18980637a3f45e5d9e8da4d1bcaeb940..326724d60c4f4787d55a49425c033c50bb9d9ca5 100644 (file)
 require_once('../../lib/base.php');
 
 // Check if we are a user
-OC_Util::checkLoggedIn();
+OC_JSON::checkLoggedIn();
 
 $query=(isset($_GET['query']))?$_GET['query']:'';
 if($query){
        $result=OC_Search::search($query);
-       echo json_encode($result);
+       OC_JSON::encodedPrint($result);
 }else{
        echo 'false';
 }
index 98218b9f89ea2adebe35efc13a7d3ae2f5ead315..860ea987871af323373ebff7a73e245e58655db9 100644 (file)
@@ -3,25 +3,23 @@
 // Init owncloud
 require_once('../../lib/base.php');
 
-// We send json data
-header( "Content-Type: application/jsonrequest" );
-
 $username = isset($_POST["username"]) ? $_POST["username"] : OC_User::getUser();
 $password = $_POST["password"];
 $oldPassword=isset($_POST["oldpassword"])?$_POST["oldpassword"]:'';
 
 // Check if we are a user
-if( !OC_User::isLoggedIn() || (!OC_Group::inGroup( OC_User::getUser(), 'admin' ) && ($username!=OC_User::getUser() || !OC_User::checkPassword($username,$oldPassword)))) {
-       echo json_encode( array( "status" => "error", "data" => array( "message" => "Authentication error" )));
+OC_JSON::checkLoggedIn();
+if( (!OC_Group::inGroup( OC_User::getUser(), 'admin' ) && ($username!=OC_User::getUser() || !OC_User::checkPassword($username,$oldPassword)))) {
+       OC_JSON::error( array( "data" => array( "message" => "Authentication error" )));
        exit();
 }
 
 // Return Success story
 if( OC_User::setPassword( $username, $password )){
-       echo json_encode( array( "status" => "success", "data" => array( "username" => $username )));
+       OC_JSON::success(array("data" => array( "username" => $username )));
 }
 else{
-       echo json_encode( array( "status" => "error", "data" => array( "message" => "Unable to change password" )));
+       OC_JSON::error(array("data" => array( "message" => "Unable to change password" )));
 }
 
 ?>
index 2631937b14dbec9c86c6876435d6f08eee581bc8..57d82e7bd94599f16002385cf1de2f8bcc5f82b0 100644 (file)
@@ -3,12 +3,9 @@
 // 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() || !OC_Group::inGroup( OC_User::getUser(), 'admin' )){
-       echo json_encode( array( "status" => "error", "data" => array( "message" => "Authentication error" )));
+       OC_JSON::error(array("data" => array( "message" => "Authentication error" )));
        exit();
 }
 
@@ -16,16 +13,16 @@ $groupname = $_POST["groupname"];
 
 // Does the group exist?
 if( in_array( $groupname, OC_Group::getGroups())){
-       echo json_encode( array( "status" => "error", "data" => array( "message" => "Group already exists" )));
+       OC_JSON::error(array("data" => array( "message" => "Group already exists" )));
        exit();
 }
 
 // Return Success story
 if( OC_Group::createGroup( $groupname )){
-       echo json_encode( array( "status" => "success", "data" => array( "groupname" => $groupname )));
+       OC_JSON::success(array("data" => array( "groupname" => $groupname )));
 }
 else{
-       echo json_encode( array( "status" => "error", "data" => array( "message" => "Unable to add group" )));
+       OC_JSON::error(array("data" => array( "message" => "Unable to add group" )));
 }
 
 ?>
index de52f90d4f3c730d321c322935406a42205d82b8..1ed53efcf062f1c5bc6b3a3b420e81a22d8311b6 100644 (file)
@@ -3,12 +3,9 @@
 // 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() || !OC_Group::inGroup( OC_User::getUser(), 'admin' )){
-       echo json_encode( array( "status" => "error", "data" => array( "message" => "Authentication error" )));
+       OC_JSON::error(array("data" => array( "message" => "Authentication error" )));
        exit();
 }
 
@@ -21,7 +18,7 @@ $password = $_POST["password"];
 
 // Does the group exist?
 if( in_array( $username, OC_User::getUsers())){
-       echo json_encode( array( "status" => "error", "data" => array( "message" => "User already exists" )));
+       OC_JSON::error(array("data" => array( "message" => "User already exists" )));
        exit();
 }
 
@@ -33,10 +30,10 @@ if( OC_User::createUser( $username, $password )){
                }
                OC_Group::addToGroup( $username, $i );
        }
-       echo json_encode( array( "status" => "success", "data" => array( "username" => $username, "groups" => implode( ", ", OC_Group::getUserGroups( $username )))));
+       OC_JSON::success(array("data" => array( "username" => $username, "groups" => implode( ", ", OC_Group::getUserGroups( $username )))));
 }
 else{
-       echo json_encode( array( "status" => "error", "data" => array( "message" => "Unable to add user" )));
+       OC_JSON::error(array("data" => array( "message" => "Unable to add user" )));
 }
 
 ?>
index 0cf66a553f8b715a4a59fd727be195a77ae79b13..12f6b32a4f396ac6ca734b7ec7434d56277d092c 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 // Init owncloud
 require_once('../../lib/base.php');
-header( "Content-Type: application/jsonrequest" );
+OC_JSON::setContentTypeHeader();
 
 OC_App::disable($_POST['appid']);
 
index eb1bfc54a0449c9f40e17ac7f18d32afb91d2a1e..8be80cd2ececdb91a2743df713b08c1c97cc6d33 100644 (file)
@@ -2,7 +2,7 @@
 
 // Init owncloud
 require_once('../../lib/base.php');
-header( "Content-Type: application/jsonrequest" );
+OC_JSON::setContentTypeHeader();
 
 OC_App::enable($_POST['appid']);
 
index 021fe35d8ec23e24637cc020a1d1808365af62d8..4226ae740f01e452aae4a96adc1a3edf949274e3 100644 (file)
@@ -5,22 +5,15 @@ require_once('../../lib/base.php');
 
 $l=new OC_L10N('settings');
 
-// 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" => $l->t("Authentication error") )));
-       exit();
-}
+OC_JSON::checkLoggedIn();
 
 // Get data
 if( isset( $_POST['identity'] ) ){
        $identity=$_POST['identity'];
        OC_Preferences::setValue(OC_User::getUser(),'user_openid','identity',$identity);
-       echo json_encode( array( "status" => "success", "data" => array( "message" => $l->t("OpenID Changed") )));
+       OC_JSON::success(array("data" => array( "message" => $l->t("OpenID Changed") )));
 }else{
-       echo json_encode( array( "status" => "error", "data" => array( "message" => $l->t("Invalid request") )));
+       OC_JSON::error(array("data" => array( "message" => $l->t("Invalid request") )));
 }
 
 ?>
index bf80da741c702d4a3a4f0e69dc91aa564561e356..4d3647818949f28d323aeef433082a8918465b89 100644 (file)
@@ -3,23 +3,16 @@
 // 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() || !OC_Group::inGroup( OC_User::getUser(), 'admin' )){
-       echo json_encode( array( "status" => "error", "data" => array( "message" => "Authentication error" )));
-       exit();
-}
+OC_JSON::checkAdminUser();
 
 $name = $_POST["groupname"];
 
 // Return Success story
 if( OC_Group::deleteGroup( $name )){
-       echo json_encode( array( "status" => "success", "data" => array( "groupname" => $name )));
+       OC_JSON::success(array("data" => array( "groupname" => $name )));
 }
 else{
-       echo json_encode( array( "status" => "error", "data" => array( "message" => "Unable to delete group" )));
+       OC_JSON::error(array("data" => array( "message" => "Unable to delete group" )));
 }
 
 ?>
index 0a94884cb96f4b192c59c64fd94cea128dd4cf82..2c288997a1f2467e8db132d6d1855fe2da264e3f 100644 (file)
@@ -3,23 +3,16 @@
 // 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() || !OC_Group::inGroup( OC_User::getUser(), 'admin' )){
-       echo json_encode( array( "status" => "error", "data" => array( "message" => "Authentication error" )));
-       exit();
-}
+OC_JSON::checkAdminUser();
 
 $username = $_POST["username"];
 
 // Return Success story
 if( OC_User::deleteUser( $username )){
-       echo json_encode( array( "status" => "success", "data" => array( "username" => $username )));
+       OC_JSON::success(array("data" => array( "username" => $username )));
 }
 else{
-       echo json_encode( array( "status" => "error", "data" => array( "message" => "Unable to delete user" )));
+       OC_JSON::error(array("data" => array( "message" => "Unable to delete user" )));
 }
 
 ?>
index a5ba3d81ba37a9086cfba8f59b8e75002ffd429e..dc1128de2e53ce975b40df15495030bff1c219ef 100644 (file)
@@ -5,22 +5,15 @@ require_once('../../lib/base.php');
 
 $l=new OC_L10N('settings');
 
-// 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" => $l->t("Authentication error") )));
-       exit();
-}
+OC_JSON::checkLoggedIn();
 
 // Get data
 if( isset( $_POST['lang'] ) ){
        $lang=$_POST['lang'];
        OC_Preferences::setValue( OC_User::getUser(), 'core', 'lang', $lang );
-       echo json_encode( array( "status" => "success", "data" => array( "message" => $l->t("Language changed") )));
+       OC_JSON::success(array("data" => array( "message" => $l->t("Language changed") )));
 }else{
-       echo json_encode( array( "status" => "error", "data" => array( "message" => $l->t("Invalid request") )));
+       OC_JSON::error(array("data" => array( "message" => $l->t("Invalid request") )));
 }
 
 ?>
index 244a85e3d9c39187720962865b29ce67f00109a3..edbf5b74516d4f4010746f561e7bab2b348dc3ba 100644 (file)
@@ -3,20 +3,13 @@
 // 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() || !OC_Group::inGroup( OC_User::getUser(), 'admin' )){
-       echo json_encode( array( "status" => "error", "data" => array( "message" => "Authentication error" )));
-       exit();
-}
+OC_JSON::checkAdminUser();
 
 $username = $_POST["username"];
 $quota= OC_Helper::computerFileSize($_POST["quota"]);
 
 // Return Success story
 OC_Preferences::setValue($username,'files','quota',$quota);
-echo json_encode( array( "status" => "success", "data" => array( "username" => $username ,'quota'=>$quota)));
+OC_JSON::success(array("data" => array( "username" => $username ,'quota'=>$quota)));
 
 ?>
index 3210252af02878839dd0725ccb4660945d1c5f3d..3ee3239dd89ddcc42adb97712d0250dd5f3b2a2a 100644 (file)
@@ -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() || !OC_Group::inGroup( OC_User::getUser(), 'admin' )){
-       echo json_encode( array( "status" => "error", "data" => array( "message" => "Authentication error" )));
-       exit();
-}
+OC_JSON::checkAdminUser();
 
 $success = true;
 $error = "add user to";
@@ -39,10 +32,10 @@ else{
 
 // Return Success story
 if( $success ){
-       echo json_encode( array( "status" => "success", "data" => array( "username" => $username, "action" => $action, "groupname" => $group )));
+       OC_JSON::success(array("data" => array( "username" => $username, "action" => $action, "groupname" => $group )));
 }
 else{
-       echo json_encode( array( "status" => "error", "data" => array( "message" => "Unable to $error group $group" )));
+       OC_JSON::error(array("data" => array( "message" => "Unable to $error group $group" )));
 }
 
 ?>
index d8f3fb5e4f8996fd39e94d469363a0e235a5982b..ba2385d794f68c8ebdafbb3490532bbec173d427 100644 (file)
@@ -12,7 +12,7 @@
        <li <?php if($app['active']) echo 'class="active"'?> data-id="<?php echo $app['id'] ?>">
                <?php  echo $app['name'] ?>
                <span class="hidden">
-                       <?php echo json_encode($app) ?>
+                       <?php OC_JSON::encodedPrint($app) ?>
                </span>
        </li>
        <?php endforeach;?>