addproperty.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * ownCloud - Addressbook
  4. *
  5. * @author Jakob Sack
  6. * @copyright 2011 Jakob Sack mail@jakobsack.de
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. // Check if we are a user
  23. OCP\JSON::checkLoggedIn();
  24. OCP\JSON::checkAppEnabled('contacts');
  25. OCP\JSON::callCheck();
  26. function bailOut($msg) {
  27. OCP\JSON::error(array('data' => array('message' => $msg)));
  28. OCP\Util::writeLog('contacts','ajax/addproperty.php: '.$msg, OCP\Util::DEBUG);
  29. exit();
  30. }
  31. $id = isset($_POST['id'])?$_POST['id']:null;
  32. $name = isset($_POST['name'])?$_POST['name']:null;
  33. $value = isset($_POST['value'])?$_POST['value']:null;
  34. $parameters = isset($_POST['parameters'])?$_POST['parameters']:array();
  35. $vcard = OC_Contacts_App::getContactVCard($id);
  36. if(!$name) {
  37. bailOut(OC_Contacts_App::$l10n->t('element name is not set.'));
  38. }
  39. if(!$id) {
  40. bailOut(OC_Contacts_App::$l10n->t('id is not set.'));
  41. }
  42. if(!$vcard) {
  43. bailOut(OC_Contacts_App::$l10n->t('Could not parse contact: ').$id);
  44. }
  45. if(!is_array($value)){
  46. $value = trim($value);
  47. if(!$value && in_array($name, array('TEL', 'EMAIL', 'ORG', 'BDAY', 'URL', 'NICKNAME', 'NOTE'))) {
  48. bailOut(OC_Contacts_App::$l10n->t('Cannot add empty property.'));
  49. }
  50. } elseif($name === 'ADR') { // only add if non-empty elements.
  51. $empty = true;
  52. foreach($value as $part) {
  53. if(trim($part) != '') {
  54. $empty = false;
  55. break;
  56. }
  57. }
  58. if($empty) {
  59. bailOut(OC_Contacts_App::$l10n->t('At least one of the address fields has to be filled out.'));
  60. }
  61. }
  62. // Prevent setting a duplicate entry
  63. $current = $vcard->select($name);
  64. foreach($current as $item) {
  65. $tmpvalue = (is_array($value)?implode(';', $value):$value);
  66. if($tmpvalue == $item->value) {
  67. bailOut(OC_Contacts_App::$l10n->t('Trying to add duplicate property: '.$name.': '.$tmpvalue));
  68. }
  69. }
  70. if(is_array($value)) {
  71. ksort($value); // NOTE: Important, otherwise the compound value will be set in the order the fields appear in the form!
  72. $value = array_map('strip_tags', $value);
  73. } else {
  74. $value = strip_tags($value);
  75. }
  76. /* preprocessing value */
  77. switch($name) {
  78. case 'BDAY':
  79. $date = New DateTime($value);
  80. $value = $date->format(DateTime::ATOM);
  81. case 'FN':
  82. if(!$value) {
  83. // create a method thats returns an alternative for FN.
  84. //$value = getOtherValue();
  85. }
  86. case 'N':
  87. case 'ORG':
  88. case 'NOTE':
  89. $value = str_replace('\n', ' \\n', $value);
  90. break;
  91. case 'NICKNAME':
  92. // TODO: Escape commas and semicolons.
  93. break;
  94. case 'EMAIL':
  95. $value = strtolower($value);
  96. break;
  97. case 'TEL':
  98. case 'ADR': // should I delete the property if empty or throw an error?
  99. break;
  100. }
  101. switch($name) {
  102. case 'NOTE':
  103. $vcard->setString('NOTE', $value);
  104. break;
  105. default:
  106. $property = $vcard->addProperty($name, $value); //, $parameters);
  107. break;
  108. }
  109. $line = count($vcard->children) - 1;
  110. // Apparently Sabre_VObject_Parameter doesn't do well with multiple values or I don't know how to do it. Tanghus.
  111. foreach ($parameters as $key=>$element) {
  112. if(is_array($element) && strtoupper($key) == 'TYPE') {
  113. // NOTE: Maybe this doesn't only apply for TYPE?
  114. // And it probably shouldn't be done here anyways :-/
  115. foreach($element as $e){
  116. if($e != '' && !is_null($e)){
  117. $vcard->children[$line]->parameters[] = new Sabre_VObject_Parameter($key,$e);
  118. }
  119. }
  120. } else {
  121. $vcard->children[$line]->parameters[] = new Sabre_VObject_Parameter($key,$element);
  122. }
  123. }
  124. $checksum = md5($vcard->children[$line]->serialize());
  125. if(!OC_Contacts_VCard::edit($id,$vcard)) {
  126. bailOut(OC_Contacts_App::$l10n->t('Error adding contact property: '.$name));
  127. }
  128. OCP\JSON::success(array('data' => array( 'checksum' => $checksum )));