2012-02-06 07:32:57 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud - Addressbook
|
|
|
|
*
|
2012-02-06 08:04:06 +01:00
|
|
|
* @author Thomas Tanghus
|
|
|
|
* @copyright 2012 Thomas Tanghus <thomas@tanghus.net>
|
2012-02-06 07:32:57 +01: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/>.
|
|
|
|
*
|
|
|
|
*/
|
2012-08-09 11:35:14 +02:00
|
|
|
|
|
|
|
require_once __DIR__.'/../loghandler.php';
|
|
|
|
|
2012-06-13 17:35:42 +02:00
|
|
|
// Check if we are a user
|
|
|
|
OCP\JSON::checkLoggedIn();
|
|
|
|
OCP\JSON::checkAppEnabled('contacts');
|
|
|
|
OCP\JSON::callCheck();
|
2012-02-06 07:32:57 +01: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;
|
|
|
|
|
2012-03-07 16:39:56 +01:00
|
|
|
if(!$name) {
|
|
|
|
bailOut(OC_Contacts_App::$l10n->t('element name is not set.'));
|
2012-02-06 07:32:57 +01:00
|
|
|
}
|
|
|
|
if(!$id) {
|
2012-02-12 16:11:41 +01:00
|
|
|
bailOut(OC_Contacts_App::$l10n->t('id is not set.'));
|
2012-02-06 07:32:57 +01:00
|
|
|
}
|
|
|
|
if(!$checksum) {
|
2012-02-12 16:11:41 +01:00
|
|
|
bailOut(OC_Contacts_App::$l10n->t('checksum is not set.'));
|
2012-02-06 07:32:57 +01:00
|
|
|
}
|
2012-07-19 23:48:17 +02:00
|
|
|
if(is_array($value)) {
|
2012-03-07 16:39:56 +01:00
|
|
|
$value = array_map('strip_tags', $value);
|
2012-08-02 05:02:36 +02:00
|
|
|
// NOTE: Important, otherwise the compound value will be
|
2012-07-19 23:48:17 +02:00
|
|
|
// set in the order the fields appear in the form!
|
2012-08-02 05:02:36 +02:00
|
|
|
ksort($value);
|
2012-03-12 14:12:27 +01:00
|
|
|
//if($name == 'CATEGORIES') {
|
|
|
|
// $value = OC_Contacts_VCard::escapeDelimiters($value, ',');
|
|
|
|
//} else {
|
2012-03-07 16:39:56 +01:00
|
|
|
$value = OC_Contacts_VCard::escapeDelimiters($value, ';');
|
2012-03-12 14:12:27 +01:00
|
|
|
//}
|
2012-03-07 16:39:56 +01:00
|
|
|
} else {
|
|
|
|
$value = trim(strip_tags($value));
|
2012-02-06 07:32:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$vcard = OC_Contacts_App::getContactVCard( $id );
|
|
|
|
$line = OC_Contacts_App::getPropertyLineByChecksum($vcard, $checksum);
|
|
|
|
if(is_null($line)) {
|
2012-08-02 05:02:36 +02:00
|
|
|
bailOut(OC_Contacts_App::$l10n->t(
|
|
|
|
'Information about vCard is incorrect. Please reload the page: ').$checksum
|
|
|
|
);
|
2012-02-06 07:32:57 +01:00
|
|
|
}
|
|
|
|
$element = $vcard->children[$line]->name;
|
|
|
|
|
|
|
|
if($element != $name) {
|
2012-08-02 05:02:36 +02:00
|
|
|
bailOut(OC_Contacts_App::$l10n->t(
|
|
|
|
'Something went FUBAR. ').$name.' != '.$element
|
|
|
|
);
|
2012-02-06 07:32:57 +01:00
|
|
|
}
|
|
|
|
|
2012-03-26 21:46:18 +02:00
|
|
|
/* preprocessing value */
|
2012-02-06 07:32:57 +01:00
|
|
|
switch($element) {
|
|
|
|
case 'BDAY':
|
|
|
|
$date = New DateTime($value);
|
2012-07-16 23:36:11 +02:00
|
|
|
$value = $date->format('Y-m-d');
|
2012-03-26 21:46:18 +02:00
|
|
|
break;
|
2012-02-06 07:32:57 +01:00
|
|
|
case 'FN':
|
|
|
|
if(!$value) {
|
|
|
|
// create a method thats returns an alternative for FN.
|
|
|
|
//$value = getOtherValue();
|
|
|
|
}
|
2012-03-26 21:46:18 +02:00
|
|
|
break;
|
2012-05-29 16:42:54 +02:00
|
|
|
case 'NOTE':
|
|
|
|
$value = str_replace('\n', '\\n', $value);
|
2012-05-29 16:46:54 +02:00
|
|
|
break;
|
2012-03-26 21:46:18 +02:00
|
|
|
case 'EMAIL':
|
|
|
|
$value = strtolower($value);
|
|
|
|
break;
|
2012-08-22 18:50:50 +02:00
|
|
|
case 'IMPP':
|
|
|
|
if(is_null($parameters) || !isset($parameters['X-SERVICE-TYPE'])) {
|
|
|
|
bailOut(OC_Contacts_App::$l10n->t('Missing IM parameter.'));
|
|
|
|
}
|
|
|
|
$impp = OC_Contacts_App::getIMOptions($parameters['X-SERVICE-TYPE']);
|
|
|
|
if(is_null($impp)) {
|
|
|
|
bailOut(OC_Contacts_App::$l10n->t('Unknown IM: '.$parameters['X-SERVICE-TYPE']));
|
|
|
|
}
|
|
|
|
$value = $impp['protocol'] . ':' . $value;
|
|
|
|
break;
|
2012-03-26 21:46:18 +02:00
|
|
|
}
|
|
|
|
|
2012-03-26 22:26:06 +02:00
|
|
|
if(!$value) {
|
2012-04-07 16:03:11 +02:00
|
|
|
unset($vcard->children[$line]);
|
|
|
|
$checksum = '';
|
|
|
|
} else {
|
|
|
|
/* setting value */
|
|
|
|
switch($element) {
|
2012-07-16 23:36:11 +02:00
|
|
|
case 'BDAY':
|
2012-08-02 05:02:36 +02:00
|
|
|
// I don't use setDateTime() because that formats it as YYYYMMDD instead
|
|
|
|
// of YYYY-MM-DD which is what the RFC recommends.
|
2012-07-16 23:36:11 +02:00
|
|
|
$vcard->children[$line]->setValue($value);
|
|
|
|
$vcard->children[$line]->parameters = array();
|
2012-08-02 05:02:36 +02:00
|
|
|
$vcard->children[$line]->add(
|
2012-10-17 14:32:18 +02:00
|
|
|
new Sabre\VObject\Parameter('VALUE', 'DATE')
|
2012-08-02 05:02:36 +02:00
|
|
|
);
|
2012-07-16 23:36:11 +02:00
|
|
|
debug('Setting value:'.$name.' '.$vcard->children[$line]);
|
|
|
|
break;
|
2012-04-07 16:03:11 +02:00
|
|
|
case 'CATEGORIES':
|
|
|
|
debug('Setting string:'.$name.' '.$value);
|
|
|
|
$vcard->children[$line]->setValue($value);
|
|
|
|
break;
|
|
|
|
case 'EMAIL':
|
|
|
|
case 'TEL':
|
2012-08-22 18:50:50 +02:00
|
|
|
case 'ADR':
|
|
|
|
case 'IMPP':
|
2012-04-07 16:03:11 +02:00
|
|
|
debug('Setting element: (EMAIL/TEL/ADR)'.$element);
|
2012-02-06 07:32:57 +01:00
|
|
|
$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);
|
2012-08-22 18:50:50 +02:00
|
|
|
if(is_array($parameter)) {
|
|
|
|
foreach($parameter as $val) {
|
2012-08-23 01:18:59 +02:00
|
|
|
if(trim($val)) {
|
|
|
|
debug('Adding parameter: '.$key.'=>'.$val);
|
2012-10-17 14:32:18 +02:00
|
|
|
$vcard->children[$line]->add(new Sabre\VObject\Parameter(
|
2012-08-23 01:18:59 +02:00
|
|
|
$key,
|
|
|
|
strtoupper(strip_tags($val)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(trim($parameter)) {
|
2012-10-17 14:32:18 +02:00
|
|
|
$vcard->children[$line]->add(new Sabre\VObject\Parameter(
|
2012-08-22 18:50:50 +02:00
|
|
|
$key,
|
2012-08-23 01:18:59 +02:00
|
|
|
strtoupper(strip_tags($parameter)))
|
2012-08-22 18:50:50 +02:00
|
|
|
);
|
|
|
|
}
|
2012-02-06 07:32:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-07 16:03:11 +02:00
|
|
|
break;
|
2012-06-06 15:28:39 +02:00
|
|
|
default:
|
|
|
|
$vcard->setString($name, $value);
|
|
|
|
break;
|
2012-04-07 16:03:11 +02:00
|
|
|
}
|
|
|
|
// Do checksum and be happy
|
|
|
|
$checksum = md5($vcard->children[$line]->serialize());
|
2012-02-06 07:32:57 +01:00
|
|
|
}
|
2012-04-07 16:03:11 +02:00
|
|
|
//debug('New checksum: '.$checksum);
|
2012-02-06 07:32:57 +01:00
|
|
|
|
2012-08-23 22:02:38 +02:00
|
|
|
try {
|
|
|
|
OC_Contacts_VCard::edit($id, $vcard);
|
|
|
|
} catch(Exception $e) {
|
|
|
|
bailOut($e->getMessage());
|
2012-02-06 07:32:57 +01:00
|
|
|
}
|
|
|
|
|
2012-08-02 05:02:36 +02:00
|
|
|
OCP\JSON::success(array('data' => array(
|
|
|
|
'line' => $line,
|
|
|
|
'checksum' => $checksum,
|
2012-08-09 23:16:07 +02:00
|
|
|
'oldchecksum' => $_POST['checksum'],
|
|
|
|
'lastmodified' => OC_Contacts_App::lastModified($vcard)->format('U'),
|
|
|
|
)));
|