Create OC_JSON class, for single point of creating json responses.
No real logic change, this just cleans up the code a bit.
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' ){
$query->execute($params);
}
- echo json_encode( array( 'status' => 'success', 'data' => $b_id));
+ OC_JSON::success(array('data' => $b_id));
}
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"]),
$result = $query->execute();
// var_dump($params);
-echo json_encode( array( "status" => "success", "data" => array()));
+OC_JSON::success(array('data' => array()));
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' ){
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();
$metadata = getURLMetadata(htmlspecialchars_decode($_GET["url"]));
-echo json_encode( array( 'status' => 'success', 'data' => $metadata));
+OC_JSON::success(array('data' => $metadata));
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
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' );
$bookmarks = $query->execute($params)->fetchAll();
-echo json_encode( array( 'status' => 'success', 'data' => $bookmarks));
+OC_JSON::success(array('data' => $bookmarks));
$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']);
$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()));
$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'));
}
?>
$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();
?>
$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'];
$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']);
if ($data['calendarid'] != $cal) {
OC_Calendar_Object::moveToCalendar($id, $cal);
}
- echo json_encode(array('status' => 'success'));
+ OC_JSON::success();
}
-?>
+?>
$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();
}
?>
$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') )));
}
?>
$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']);
$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()));
$return_events[$year][$month][$day][$hour] = array(1 => $return_event);
}
}
-echo json_encode($return_events);
+OC_JSON::encodedPrint($return_events);
?>
$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();
}
$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 )));
$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();
}
$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 )));
$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 )));
$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 )));
$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();
}
}
}
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 )));
$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();
}
$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 )));
$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();
}
}
}
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();
}
$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'] )));
$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 )));
$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();
}
$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 )));
$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();
}
}
}
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();
}
$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 )));
$source = dirname($source);
}
if (!empty($users)) {
- echo json_encode($users);
+ OC_JSON::encodedPrint($users);
}
-?>
\ No newline at end of file
+?>
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();
$users[] = "<option value='".$group."'>".$group."</option>";
}
$users[] = "</optgroup>";
-echo json_encode($users);
+OC_JSON::encodedPrint($users);
?>
$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();
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'])){
$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;
OC_Filesystem::readfile($arguments['path']);
exit;
case 'find_music':
- echo json_encode(findMusic());
+ OC_JSON::encodedPrint(findMusic());
exit;
}
}
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));
+?>
'score' => (float)1.0
);
}
-echo json_encode($results);
-?>
\ No newline at end of file
+OC_JSON::encodedPrint($results);
+?>
$groups[] = array( "groupname" => $i );
}
-// We send json data
-header( "Content-Type: application/jsonrequest" );
-echo json_encode($groups);
+OC_JSON::encodedPrint($groups);
?>
$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()));
?>
$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);
?>
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"));
}
}
// 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'];
}
}
}
-echo json_encode($files);
+OC_JSON::encodedPrint($files);
?>
// 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"];
$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 )));
}
?>
// 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'] : '';
$list->assign( "files", $files );
$data = array('files' => $list->fetchPage());
-echo json_encode( array( "status" => "success", "data" => $data));
+OC_JSON::success(array('data' => $data));
?>
// 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"];
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
+?>
// 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" )));
// 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"];
// 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" )));
}
?>
// 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) {
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();
}
}
$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();
}
$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)));
?>
--- /dev/null
+<?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);
+ }
+}
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';
}
// 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" )));
}
?>
// 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();
}
// 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" )));
}
?>
// 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();
}
// 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();
}
}
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" )));
}
?>
<?php
// Init owncloud
require_once('../../lib/base.php');
-header( "Content-Type: application/jsonrequest" );
+OC_JSON::setContentTypeHeader();
OC_App::disable($_POST['appid']);
// Init owncloud
require_once('../../lib/base.php');
-header( "Content-Type: application/jsonrequest" );
+OC_JSON::setContentTypeHeader();
OC_App::enable($_POST['appid']);
$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") )));
}
?>
// 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" )));
}
?>
// 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" )));
}
?>
$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") )));
}
?>
// 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)));
?>
// 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";
// 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" )));
}
?>
<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;?>