vobject.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Bart Visscher
  6. * @copyright 2011 Bart Visscher bartv@thisnet.nl
  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. /**
  23. * This class provides a streamlined interface to the Sabre VObject classes
  24. */
  25. class OC_VObject{
  26. /** @var Sabre\VObject\Component */
  27. protected $vObject;
  28. /**
  29. * @return Sabre\VObject\Component
  30. */
  31. public function getVObject() {
  32. return $this->vObject;
  33. }
  34. /**
  35. * Parses the VObject
  36. * @param string $data VObject as string
  37. * @return Sabre\VObject\Reader|null
  38. */
  39. public static function parse($data) {
  40. try {
  41. Sabre\VObject\Property::$classMap['LAST-MODIFIED'] = 'Sabre\VObject\Property\DateTime';
  42. $vObject = Sabre\VObject\Reader::read($data);
  43. if ($vObject instanceof Sabre\VObject\Component) {
  44. $vObject = new OC_VObject($vObject);
  45. }
  46. return $vObject;
  47. } catch (Exception $e) {
  48. OC_Log::write('vobject', $e->getMessage(), OC_Log::ERROR);
  49. return null;
  50. }
  51. }
  52. /**
  53. * Escapes semicolons
  54. * @param array $value
  55. * @return string
  56. */
  57. public static function escapeSemicolons($value) {
  58. foreach($value as &$i ) {
  59. $i = implode("\\\\;", explode(';', $i));
  60. }
  61. return implode(';', $value);
  62. }
  63. /**
  64. * Creates an array out of a multivalue property
  65. * @param string $value
  66. * @return array
  67. */
  68. public static function unescapeSemicolons($value) {
  69. $array = explode(';', $value);
  70. for($i=0;$i<count($array);$i++) {
  71. if(substr($array[$i], -2, 2)=="\\\\") {
  72. if(isset($array[$i+1])) {
  73. $array[$i] = substr($array[$i], 0, count($array[$i])-2).';'.$array[$i+1];
  74. unset($array[$i+1]);
  75. }
  76. else{
  77. $array[$i] = substr($array[$i], 0, count($array[$i])-2).';';
  78. }
  79. $i = $i - 1;
  80. }
  81. }
  82. return $array;
  83. }
  84. /**
  85. * Constructor
  86. * @param Sabre\VObject\Component|string $vobject_or_name
  87. */
  88. public function __construct($vobject_or_name) {
  89. if (is_object($vobject_or_name)) {
  90. $this->vObject = $vobject_or_name;
  91. } else {
  92. $this->vObject = new Sabre\VObject\Component($vobject_or_name);
  93. }
  94. }
  95. /**
  96. * @todo Write documentation
  97. * @param \OC_VObject|\Sabre\VObject\Component $item
  98. * @param null $itemValue
  99. */
  100. public function add($item, $itemValue = null) {
  101. if ($item instanceof OC_VObject) {
  102. $item = $item->getVObject();
  103. }
  104. $this->vObject->add($item, $itemValue);
  105. }
  106. /**
  107. * Add property to vobject
  108. * @param object $name of property
  109. * @param object $value of property
  110. * @param array|object $parameters of property
  111. * @return Sabre\VObject\Property newly created
  112. */
  113. public function addProperty($name, $value, $parameters=array()) {
  114. if(is_array($value)) {
  115. $value = OC_VObject::escapeSemicolons($value);
  116. }
  117. $property = new Sabre\VObject\Property( $name, $value );
  118. foreach($parameters as $name => $value) {
  119. $property->parameters[] = new Sabre\VObject\Parameter($name, $value);
  120. }
  121. $this->vObject->add($property);
  122. return $property;
  123. }
  124. public function setUID() {
  125. $uid = substr(md5(rand().time()), 0, 10);
  126. $this->vObject->add('UID', $uid);
  127. }
  128. /**
  129. * @todo Write documentation
  130. * @param mixed $name
  131. * @param string $string
  132. */
  133. public function setString($name, $string) {
  134. if ($string != '') {
  135. $string = strtr($string, array("\r\n"=>"\n"));
  136. $this->vObject->__set($name, $string);
  137. }else{
  138. $this->vObject->__unset($name);
  139. }
  140. }
  141. /**
  142. * Sets or unsets the Date and Time for a property.
  143. * When $datetime is set to 'now', use the current time
  144. * When $datetime is null, unset the property
  145. *
  146. * @param string $name
  147. * @param DateTime $datetime
  148. * @param int $dateType
  149. * @return void
  150. */
  151. public function setDateTime($name, $datetime, $dateType=Sabre\VObject\Property\DateTime::LOCALTZ) {
  152. if ($datetime == 'now') {
  153. $datetime = new DateTime();
  154. }
  155. if ($datetime instanceof DateTime) {
  156. $datetime_element = new Sabre\VObject\Property\DateTime($name);
  157. $datetime_element->setDateTime($datetime, $dateType);
  158. $this->vObject->__set($name, $datetime_element);
  159. }else{
  160. $this->vObject->__unset($name);
  161. }
  162. }
  163. /**
  164. * @todo Write documentation
  165. * @param string $name
  166. * @return string
  167. */
  168. public function getAsString($name) {
  169. return $this->vObject->__isset($name) ?
  170. $this->vObject->__get($name)->value :
  171. '';
  172. }
  173. /**
  174. * @todo Write documentation
  175. * @param string $name
  176. * @return array
  177. */
  178. public function getAsArray($name) {
  179. $values = array();
  180. if ($this->vObject->__isset($name)) {
  181. $values = explode(',', $this->getAsString($name));
  182. $values = array_map('trim', $values);
  183. }
  184. return $values;
  185. }
  186. /**
  187. * @todo Write documentation
  188. * @param string $name
  189. * @return array|OC_VObject|\Sabre\VObject\Property
  190. */
  191. public function &__get($name) {
  192. if ($name == 'children') {
  193. return $this->vObject->children;
  194. }
  195. $return = $this->vObject->__get($name);
  196. if ($return instanceof Sabre\VObject\Component) {
  197. $return = new OC_VObject($return);
  198. }
  199. return $return;
  200. }
  201. /**
  202. * @todo Write documentation
  203. * @param string $name
  204. * @param string $value
  205. */
  206. public function __set($name, $value) {
  207. return $this->vObject->__set($name, $value);
  208. }
  209. /**
  210. * @todo Write documentation
  211. * @param string $name
  212. */
  213. public function __unset($name) {
  214. return $this->vObject->__unset($name);
  215. }
  216. /**
  217. * @todo Write documentation
  218. * @param string $name
  219. * @return bool
  220. */
  221. public function __isset($name) {
  222. return $this->vObject->__isset($name);
  223. }
  224. /**
  225. * @todo Write documentation
  226. * @param callable $function
  227. * @param array $arguments
  228. * @return mixed
  229. */
  230. public function __call($function, $arguments) {
  231. return call_user_func_array(array($this->vObject, $function), $arguments);
  232. }
  233. }