vobject.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. * @returns Sabre_VObject_Component
  30. */
  31. public function getVObject(){
  32. return $this->vobject;
  33. }
  34. /**
  35. * @brief Parses the VObject
  36. * @param string VObject as string
  37. * @returns Sabre_VObject or 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. * @brief Escapes semicolons
  54. * @param string $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. * @brief 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. * Constuctor
  86. * @param Sabre_VObject_Component or string
  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. public function add($item, $itemValue = null){
  96. if ($item instanceof OC_VObject){
  97. $item = $item->getVObject();
  98. }
  99. $this->vobject->add($item, $itemValue);
  100. }
  101. /**
  102. * @brief Add property to vobject
  103. * @param object $name of property
  104. * @param object $value of property
  105. * @param object $parameters of property
  106. * @returns Sabre_VObject_Property newly created
  107. */
  108. public function addProperty($name, $value, $parameters=array()){
  109. if(is_array($value)){
  110. $value = OC_VObject::escapeSemicolons($value);
  111. }
  112. $property = new Sabre_VObject_Property( $name, $value );
  113. foreach($parameters as $name => $value){
  114. $property->parameters[] = new Sabre_VObject_Parameter($name, $value);
  115. }
  116. $this->vobject->add($property);
  117. return $property;
  118. }
  119. public function setUID(){
  120. $uid = substr(md5(rand().time()),0,10);
  121. $this->vobject->add('UID',$uid);
  122. }
  123. public function setString($name, $string){
  124. if ($string != ''){
  125. $string = strtr($string, array("\r\n"=>"\n"));
  126. $this->vobject->__set($name, $string);
  127. }else{
  128. $this->vobject->__unset($name);
  129. }
  130. }
  131. /**
  132. * Sets or unsets the Date and Time for a property.
  133. * When $datetime is set to 'now', use the current time
  134. * When $datetime is null, unset the property
  135. *
  136. * @param string property name
  137. * @param DateTime $datetime
  138. * @param int $dateType
  139. * @return void
  140. */
  141. public function setDateTime($name, $datetime, $dateType=Sabre_VObject_Property_DateTime::LOCALTZ){
  142. if ($datetime == 'now'){
  143. $datetime = new DateTime();
  144. }
  145. if ($datetime instanceof DateTime){
  146. $datetime_element = new Sabre_VObject_Property_DateTime($name);
  147. $datetime_element->setDateTime($datetime, $dateType);
  148. $this->vobject->__set($name, $datetime_element);
  149. }else{
  150. $this->vobject->__unset($name);
  151. }
  152. }
  153. public function getAsString($name){
  154. return $this->vobject->__isset($name) ?
  155. $this->vobject->__get($name)->value :
  156. '';
  157. }
  158. public function getAsArray($name){
  159. $values = array();
  160. if ($this->vobject->__isset($name)){
  161. $values = explode(',', $this->getAsString($name));
  162. $values = array_map('trim', $values);
  163. }
  164. return $values;
  165. }
  166. public function &__get($name){
  167. if ($name == 'children'){
  168. return $this->vobject->children;
  169. }
  170. $return = $this->vobject->__get($name);
  171. if ($return instanceof Sabre_VObject_Component){
  172. $return = new OC_VObject($return);
  173. }
  174. return $return;
  175. }
  176. public function __set($name, $value){
  177. return $this->vobject->__set($name, $value);
  178. }
  179. public function __unset($name){
  180. return $this->vobject->__unset($name);
  181. }
  182. public function __isset($name){
  183. return $this->vobject->__isset($name);
  184. }
  185. public function __call($function,$arguments){
  186. return call_user_func_array(array($this->vobject, $function), $arguments);
  187. }
  188. }