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. $arrayCount = count($array);
  71. for($i = 0; $i < $arrayCount; $i++) {
  72. if(substr($array[$i], -2, 2)=="\\\\") {
  73. if(isset($array[$i+1])) {
  74. $array[$i] = substr($array[$i], 0, count($array[$i])-2).';'.$array[$i+1];
  75. unset($array[$i+1]);
  76. }
  77. else{
  78. $array[$i] = substr($array[$i], 0, count($array[$i])-2).';';
  79. }
  80. $i = $i - 1;
  81. }
  82. }
  83. return $array;
  84. }
  85. /**
  86. * Constructor
  87. * @param Sabre\VObject\Component|string $vobject_or_name
  88. */
  89. public function __construct($vobject_or_name) {
  90. if (is_object($vobject_or_name)) {
  91. $this->vObject = $vobject_or_name;
  92. } else {
  93. $this->vObject = new Sabre\VObject\Component($vobject_or_name);
  94. }
  95. }
  96. /**
  97. * @todo Write documentation
  98. * @param \OC_VObject|\Sabre\VObject\Component $item
  99. * @param null $itemValue
  100. */
  101. public function add($item, $itemValue = null) {
  102. if ($item instanceof OC_VObject) {
  103. $item = $item->getVObject();
  104. }
  105. $this->vObject->add($item, $itemValue);
  106. }
  107. /**
  108. * Add property to vobject
  109. * @param object $name of property
  110. * @param object $value of property
  111. * @param array|object $parameters of property
  112. * @return Sabre\VObject\Property newly created
  113. */
  114. public function addProperty($name, $value, $parameters=array()) {
  115. if(is_array($value)) {
  116. $value = OC_VObject::escapeSemicolons($value);
  117. }
  118. $property = new Sabre\VObject\Property( $name, $value );
  119. foreach($parameters as $name => $value) {
  120. $property->parameters[] = new Sabre\VObject\Parameter($name, $value);
  121. }
  122. $this->vObject->add($property);
  123. return $property;
  124. }
  125. public function setUID() {
  126. $uid = substr(md5(rand().time()), 0, 10);
  127. $this->vObject->add('UID', $uid);
  128. }
  129. /**
  130. * @todo Write documentation
  131. * @param mixed $name
  132. * @param string $string
  133. */
  134. public function setString($name, $string) {
  135. if ($string != '') {
  136. $string = strtr($string, array("\r\n"=>"\n"));
  137. $this->vObject->__set($name, $string);
  138. }else{
  139. $this->vObject->__unset($name);
  140. }
  141. }
  142. /**
  143. * Sets or unsets the Date and Time for a property.
  144. * When $datetime is set to 'now', use the current time
  145. * When $datetime is null, unset the property
  146. *
  147. * @param string $name
  148. * @param DateTime $datetime
  149. * @param int $dateType
  150. * @return void
  151. */
  152. public function setDateTime($name, $datetime, $dateType=Sabre\VObject\Property\DateTime::LOCALTZ) {
  153. if ($datetime == 'now') {
  154. $datetime = new DateTime();
  155. }
  156. if ($datetime instanceof DateTime) {
  157. $datetime_element = new Sabre\VObject\Property\DateTime($name);
  158. $datetime_element->setDateTime($datetime, $dateType);
  159. $this->vObject->__set($name, $datetime_element);
  160. }else{
  161. $this->vObject->__unset($name);
  162. }
  163. }
  164. /**
  165. * @todo Write documentation
  166. * @param string $name
  167. * @return string
  168. */
  169. public function getAsString($name) {
  170. return $this->vObject->__isset($name) ?
  171. $this->vObject->__get($name)->value :
  172. '';
  173. }
  174. /**
  175. * @todo Write documentation
  176. * @param string $name
  177. * @return array
  178. */
  179. public function getAsArray($name) {
  180. $values = array();
  181. if ($this->vObject->__isset($name)) {
  182. $values = explode(',', $this->getAsString($name));
  183. $values = array_map('trim', $values);
  184. }
  185. return $values;
  186. }
  187. /**
  188. * @todo Write documentation
  189. * @param string $name
  190. * @return array|OC_VObject|\Sabre\VObject\Property
  191. */
  192. public function &__get($name) {
  193. if ($name == 'children') {
  194. return $this->vObject->children;
  195. }
  196. $return = $this->vObject->__get($name);
  197. if ($return instanceof Sabre\VObject\Component) {
  198. $return = new OC_VObject($return);
  199. }
  200. return $return;
  201. }
  202. /**
  203. * @todo Write documentation
  204. * @param string $name
  205. * @param string $value
  206. */
  207. public function __set($name, $value) {
  208. return $this->vObject->__set($name, $value);
  209. }
  210. /**
  211. * @todo Write documentation
  212. * @param string $name
  213. */
  214. public function __unset($name) {
  215. return $this->vObject->__unset($name);
  216. }
  217. /**
  218. * @todo Write documentation
  219. * @param string $name
  220. * @return bool
  221. */
  222. public function __isset($name) {
  223. return $this->vObject->__isset($name);
  224. }
  225. /**
  226. * @todo Write documentation
  227. * @param callable $function
  228. * @param array $arguments
  229. * @return mixed
  230. */
  231. public function __call($function, $arguments) {
  232. return call_user_func_array(array($this->vObject, $function), $arguments);
  233. }
  234. }