2012-02-06 10:32:57 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud - Addressbook
|
|
|
|
*
|
2012-02-06 11:04:06 +04:00
|
|
|
* @author Thomas Tanghus
|
|
|
|
* @copyright 2012 Thomas Tanghus <thomas@tanghus.net>
|
2012-02-06 10:32:57 +04:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Init owncloud
|
|
|
|
require_once('../../../lib/base.php');
|
|
|
|
|
|
|
|
// Check if we are a user
|
|
|
|
OC_JSON::checkLoggedIn();
|
|
|
|
OC_JSON::checkAppEnabled('contacts');
|
|
|
|
|
|
|
|
function bailOut($msg) {
|
|
|
|
OC_JSON::error(array('data' => array('message' => $msg)));
|
|
|
|
OC_Log::write('contacts','ajax/saveproperty.php: '.$msg, OC_Log::DEBUG);
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
function debug($msg) {
|
|
|
|
OC_Log::write('contacts','ajax/saveproperty.php: '.$msg, OC_Log::DEBUG);
|
|
|
|
}
|
2012-03-27 00:26:06 +04:00
|
|
|
// foreach ($_POST as $key=>$element) {
|
|
|
|
// debug('_POST: '.$key.'=>'.print_r($element, true));
|
|
|
|
// }
|
2012-02-06 10:32:57 +04:00
|
|
|
|
|
|
|
$id = isset($_POST['id'])?$_POST['id']:null;
|
|
|
|
$name = isset($_POST['name'])?$_POST['name']:null;
|
|
|
|
$value = isset($_POST['value'])?$_POST['value']:null;
|
|
|
|
$parameters = isset($_POST['parameters'])?$_POST['parameters']:null;
|
|
|
|
$checksum = isset($_POST['checksum'])?$_POST['checksum']:null;
|
|
|
|
// if(!is_null($parameters)) {
|
|
|
|
// debug('parameters: '.count($parameters));
|
|
|
|
// foreach($parameters as $key=>$val ) {
|
|
|
|
// debug('parameter: '.$key.'=>'.implode('/',$val));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2012-03-07 19:39:56 +04:00
|
|
|
if(!$name) {
|
|
|
|
bailOut(OC_Contacts_App::$l10n->t('element name is not set.'));
|
2012-02-06 10:32:57 +04:00
|
|
|
}
|
|
|
|
if(!$id) {
|
2012-02-12 19:11:41 +04:00
|
|
|
bailOut(OC_Contacts_App::$l10n->t('id is not set.'));
|
2012-02-06 10:32:57 +04:00
|
|
|
}
|
|
|
|
if(!$checksum) {
|
2012-02-12 19:11:41 +04:00
|
|
|
bailOut(OC_Contacts_App::$l10n->t('checksum is not set.'));
|
2012-02-06 10:32:57 +04:00
|
|
|
}
|
2012-03-07 19:39:56 +04:00
|
|
|
if(is_array($value)){
|
|
|
|
$value = array_map('strip_tags', $value);
|
|
|
|
ksort($value); // NOTE: Important, otherwise the compound value will be set in the order the fields appear in the form!
|
2012-03-12 17:12:27 +04:00
|
|
|
//if($name == 'CATEGORIES') {
|
|
|
|
// $value = OC_Contacts_VCard::escapeDelimiters($value, ',');
|
|
|
|
//} else {
|
2012-03-07 19:39:56 +04:00
|
|
|
$value = OC_Contacts_VCard::escapeDelimiters($value, ';');
|
2012-03-12 17:12:27 +04:00
|
|
|
//}
|
2012-03-07 19:39:56 +04:00
|
|
|
} else {
|
|
|
|
$value = trim(strip_tags($value));
|
2012-02-06 10:32:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$vcard = OC_Contacts_App::getContactVCard( $id );
|
|
|
|
$line = OC_Contacts_App::getPropertyLineByChecksum($vcard, $checksum);
|
|
|
|
if(is_null($line)) {
|
2012-03-07 19:39:56 +04:00
|
|
|
bailOut(OC_Contacts_App::$l10n->t('Information about vCard is incorrect. Please reload the page: ').$checksum);
|
2012-02-06 10:32:57 +04:00
|
|
|
}
|
|
|
|
$element = $vcard->children[$line]->name;
|
|
|
|
|
|
|
|
if($element != $name) {
|
2012-02-12 19:11:41 +04:00
|
|
|
bailOut(OC_Contacts_App::$l10n->t('Something went FUBAR. ').$name.' != '.$element);
|
2012-02-06 10:32:57 +04:00
|
|
|
}
|
|
|
|
|
2012-03-26 23:46:18 +04:00
|
|
|
/* preprocessing value */
|
2012-02-06 10:32:57 +04:00
|
|
|
switch($element) {
|
|
|
|
case 'BDAY':
|
|
|
|
$date = New DateTime($value);
|
|
|
|
//$vcard->setDateTime('BDAY', $date, Sabre_VObject_Element_DateTime::DATE);
|
|
|
|
$value = $date->format(DateTime::ATOM);
|
2012-03-26 23:46:18 +04:00
|
|
|
break;
|
2012-02-06 10:32:57 +04:00
|
|
|
case 'FN':
|
|
|
|
if(!$value) {
|
|
|
|
// create a method thats returns an alternative for FN.
|
|
|
|
//$value = getOtherValue();
|
|
|
|
}
|
2012-03-26 23:46:18 +04:00
|
|
|
break;
|
|
|
|
case 'CATEGORIES':
|
|
|
|
/* multi autocomplete triggers an save with empty value */
|
|
|
|
if (!$value) {
|
|
|
|
$value = $vcard->getAsString('CATEGORIES');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'EMAIL':
|
|
|
|
$value = strtolower($value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-03-27 00:26:06 +04:00
|
|
|
if(!$value) {
|
|
|
|
bailOut(OC_Contacts_App::$l10n->t('Cannot save empty value.'));
|
|
|
|
}
|
|
|
|
|
2012-03-26 23:46:18 +04:00
|
|
|
/* setting value */
|
|
|
|
switch($element) {
|
|
|
|
case 'BDAY':
|
|
|
|
case 'FN':
|
2012-02-06 10:32:57 +04:00
|
|
|
case 'N':
|
|
|
|
case 'ORG':
|
2012-03-07 19:39:56 +04:00
|
|
|
case 'NOTE':
|
2012-02-06 10:32:57 +04:00
|
|
|
case 'NICKNAME':
|
2012-03-07 19:39:56 +04:00
|
|
|
case 'CATEGORIES':
|
2012-02-06 10:32:57 +04:00
|
|
|
debug('Setting string:'.$name.' '.$value);
|
|
|
|
$vcard->setString($name, $value);
|
|
|
|
break;
|
|
|
|
case 'EMAIL':
|
|
|
|
case 'TEL':
|
|
|
|
case 'ADR': // should I delete the property if empty or throw an error?
|
|
|
|
debug('Setting element: (EMAIL/TEL/ADR)'.$element);
|
|
|
|
if(!$value) {
|
|
|
|
unset($vcard->children[$line]); // Should never happen...
|
|
|
|
} else {
|
|
|
|
$vcard->children[$line]->setValue($value);
|
|
|
|
$vcard->children[$line]->parameters = array();
|
|
|
|
if(!is_null($parameters)) {
|
|
|
|
debug('Setting parameters: '.$parameters);
|
|
|
|
foreach($parameters as $key => $parameter) {
|
|
|
|
debug('Adding parameter: '.$key);
|
|
|
|
foreach($parameter as $val) {
|
|
|
|
debug('Adding parameter: '.$key.'=>'.$val);
|
|
|
|
$vcard->children[$line]->add(new Sabre_VObject_Parameter($key, strtoupper($val)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Do checksum and be happy
|
|
|
|
$checksum = md5($vcard->children[$line]->serialize());
|
|
|
|
debug('New checksum: '.$checksum);
|
|
|
|
|
2012-03-08 00:27:03 +04:00
|
|
|
if(!OC_Contacts_VCard::edit($id,$vcard)) {
|
2012-03-08 05:55:48 +04:00
|
|
|
bailOut(OC_Contacts_App::$l10n->t('Error updating contact property.'));
|
2012-02-06 10:32:57 +04:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
OC_JSON::success(array('data' => array( 'line' => $line, 'checksum' => $checksum, 'oldchecksum' => $_POST['checksum'] )));
|