Property.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. namespace Sabre\VObject;
  3. /**
  4. * Property
  5. *
  6. * A property is always in a KEY:VALUE structure, and may optionally contain
  7. * parameters.
  8. *
  9. * @copyright Copyright (C) 2007-2014 fruux GmbH. All rights reserved.
  10. * @author Evert Pot (http://evertpot.com/)
  11. * @license http://sabre.io/license/ Modified BSD License
  12. */
  13. abstract class Property extends Node {
  14. /**
  15. * Property name.
  16. *
  17. * This will contain a string such as DTSTART, SUMMARY, FN.
  18. *
  19. * @var string
  20. */
  21. public $name;
  22. /**
  23. * Property group.
  24. *
  25. * This is only used in vcards
  26. *
  27. * @var string
  28. */
  29. public $group;
  30. /**
  31. * List of parameters
  32. *
  33. * @var array
  34. */
  35. public $parameters = array();
  36. /**
  37. * Current value
  38. *
  39. * @var mixed
  40. */
  41. protected $value;
  42. /**
  43. * In case this is a multi-value property. This string will be used as a
  44. * delimiter.
  45. *
  46. * @var string|null
  47. */
  48. public $delimiter = ';';
  49. /**
  50. * Creates the generic property.
  51. *
  52. * Parameters must be specified in key=>value syntax.
  53. *
  54. * @param Component $root The root document
  55. * @param string $name
  56. * @param string|array|null $value
  57. * @param array $parameters List of parameters
  58. * @param string $group The vcard property group
  59. * @return void
  60. */
  61. public function __construct(Component $root, $name, $value = null, array $parameters = array(), $group = null) {
  62. $this->name = $name;
  63. $this->group = $group;
  64. $this->root = $root;
  65. foreach($parameters as $k=>$v) {
  66. $this->add($k, $v);
  67. }
  68. if (!is_null($value)) {
  69. $this->setValue($value);
  70. }
  71. }
  72. /**
  73. * Updates the current value.
  74. *
  75. * This may be either a single, or multiple strings in an array.
  76. *
  77. * @param string|array $value
  78. * @return void
  79. */
  80. public function setValue($value) {
  81. $this->value = $value;
  82. }
  83. /**
  84. * Returns the current value.
  85. *
  86. * This method will always return a singular value. If this was a
  87. * multi-value object, some decision will be made first on how to represent
  88. * it as a string.
  89. *
  90. * To get the correct multi-value version, use getParts.
  91. *
  92. * @return string
  93. */
  94. public function getValue() {
  95. if (is_array($this->value)) {
  96. if (count($this->value)==0) {
  97. return null;
  98. } elseif (count($this->value)===1) {
  99. return $this->value[0];
  100. } else {
  101. return $this->getRawMimeDirValue($this->value);
  102. }
  103. } else {
  104. return $this->value;
  105. }
  106. }
  107. /**
  108. * Sets a multi-valued property.
  109. *
  110. * @param array $parts
  111. * @return void
  112. */
  113. public function setParts(array $parts) {
  114. $this->value = $parts;
  115. }
  116. /**
  117. * Returns a multi-valued property.
  118. *
  119. * This method always returns an array, if there was only a single value,
  120. * it will still be wrapped in an array.
  121. *
  122. * @return array
  123. */
  124. public function getParts() {
  125. if (is_null($this->value)) {
  126. return array();
  127. } elseif (is_array($this->value)) {
  128. return $this->value;
  129. } else {
  130. return array($this->value);
  131. }
  132. }
  133. /**
  134. * Adds a new parameter, and returns the new item.
  135. *
  136. * If a parameter with same name already existed, the values will be
  137. * combined.
  138. * If nameless parameter is added, we try to guess it's name.
  139. *
  140. * @param string $name
  141. * @param string|null|array $value
  142. * @return Node
  143. */
  144. public function add($name, $value = null) {
  145. $noName = false;
  146. if ($name === null) {
  147. $name = Parameter::guessParameterNameByValue($value);
  148. $noName = true;
  149. }
  150. if (isset($this->parameters[strtoupper($name)])) {
  151. $this->parameters[strtoupper($name)]->addValue($value);
  152. }
  153. else {
  154. $param = new Parameter($this->root, $name, $value);
  155. $param->noName = $noName;
  156. $this->parameters[$param->name] = $param;
  157. }
  158. }
  159. /**
  160. * Returns an iterable list of children
  161. *
  162. * @return array
  163. */
  164. public function parameters() {
  165. return $this->parameters;
  166. }
  167. /**
  168. * Returns the type of value.
  169. *
  170. * This corresponds to the VALUE= parameter. Every property also has a
  171. * 'default' valueType.
  172. *
  173. * @return string
  174. */
  175. abstract public function getValueType();
  176. /**
  177. * Sets a raw value coming from a mimedir (iCalendar/vCard) file.
  178. *
  179. * This has been 'unfolded', so only 1 line will be passed. Unescaping is
  180. * not yet done, but parameters are not included.
  181. *
  182. * @param string $val
  183. * @return void
  184. */
  185. abstract public function setRawMimeDirValue($val);
  186. /**
  187. * Returns a raw mime-dir representation of the value.
  188. *
  189. * @return string
  190. */
  191. abstract public function getRawMimeDirValue();
  192. /**
  193. * Turns the object back into a serialized blob.
  194. *
  195. * @return string
  196. */
  197. public function serialize() {
  198. $str = $this->name;
  199. if ($this->group) $str = $this->group . '.' . $this->name;
  200. foreach($this->parameters as $param) {
  201. $str.=';' . $param->serialize();
  202. }
  203. $str.=':' . $this->getRawMimeDirValue();
  204. $out = '';
  205. while(strlen($str)>0) {
  206. if (strlen($str)>75) {
  207. $out.= mb_strcut($str,0,75,'utf-8') . "\r\n";
  208. $str = ' ' . mb_strcut($str,75,strlen($str),'utf-8');
  209. } else {
  210. $out.=$str . "\r\n";
  211. $str='';
  212. break;
  213. }
  214. }
  215. return $out;
  216. }
  217. /**
  218. * Returns the value, in the format it should be encoded for json.
  219. *
  220. * This method must always return an array.
  221. *
  222. * @return array
  223. */
  224. public function getJsonValue() {
  225. return $this->getParts();
  226. }
  227. /**
  228. * Sets the json value, as it would appear in a jCard or jCal object.
  229. *
  230. * The value must always be an array.
  231. *
  232. * @param array $value
  233. * @return void
  234. */
  235. public function setJsonValue(array $value) {
  236. if (count($value)===1) {
  237. $this->setValue(reset($value));
  238. } else {
  239. $this->setValue($value);
  240. }
  241. }
  242. /**
  243. * This method returns an array, with the representation as it should be
  244. * encoded in json. This is used to create jCard or jCal documents.
  245. *
  246. * @return array
  247. */
  248. public function jsonSerialize() {
  249. $parameters = array();
  250. foreach($this->parameters as $parameter) {
  251. if ($parameter->name === 'VALUE') {
  252. continue;
  253. }
  254. $parameters[strtolower($parameter->name)] = $parameter->jsonSerialize();
  255. }
  256. // In jCard, we need to encode the property-group as a separate 'group'
  257. // parameter.
  258. if ($this->group) {
  259. $parameters['group'] = $this->group;
  260. }
  261. return array_merge(
  262. array(
  263. strtolower($this->name),
  264. (object)$parameters,
  265. strtolower($this->getValueType()),
  266. ),
  267. $this->getJsonValue()
  268. );
  269. }
  270. /**
  271. * Called when this object is being cast to a string.
  272. *
  273. * If the property only had a single value, you will get just that. In the
  274. * case the property had multiple values, the contents will be escaped and
  275. * combined with ,.
  276. *
  277. * @return string
  278. */
  279. public function __toString() {
  280. return (string)$this->getValue();
  281. }
  282. /* ArrayAccess interface {{{ */
  283. /**
  284. * Checks if an array element exists
  285. *
  286. * @param mixed $name
  287. * @return bool
  288. */
  289. public function offsetExists($name) {
  290. if (is_int($name)) return parent::offsetExists($name);
  291. $name = strtoupper($name);
  292. foreach($this->parameters as $parameter) {
  293. if ($parameter->name == $name) return true;
  294. }
  295. return false;
  296. }
  297. /**
  298. * Returns a parameter.
  299. *
  300. * If the parameter does not exist, null is returned.
  301. *
  302. * @param string $name
  303. * @return Node
  304. */
  305. public function offsetGet($name) {
  306. if (is_int($name)) return parent::offsetGet($name);
  307. $name = strtoupper($name);
  308. if (!isset($this->parameters[$name])) {
  309. return null;
  310. }
  311. return $this->parameters[$name];
  312. }
  313. /**
  314. * Creates a new parameter
  315. *
  316. * @param string $name
  317. * @param mixed $value
  318. * @return void
  319. */
  320. public function offsetSet($name, $value) {
  321. if (is_int($name)) {
  322. parent::offsetSet($name, $value);
  323. // @codeCoverageIgnoreStart
  324. // This will never be reached, because an exception is always
  325. // thrown.
  326. return;
  327. // @codeCoverageIgnoreEnd
  328. }
  329. $param = new Parameter($this->root, $name, $value);
  330. $this->parameters[$param->name] = $param;
  331. }
  332. /**
  333. * Removes one or more parameters with the specified name
  334. *
  335. * @param string $name
  336. * @return void
  337. */
  338. public function offsetUnset($name) {
  339. if (is_int($name)) {
  340. parent::offsetUnset($name);
  341. // @codeCoverageIgnoreStart
  342. // This will never be reached, because an exception is always
  343. // thrown.
  344. return;
  345. // @codeCoverageIgnoreEnd
  346. }
  347. unset($this->parameters[strtoupper($name)]);
  348. }
  349. /* }}} */
  350. /**
  351. * This method is automatically called when the object is cloned.
  352. * Specifically, this will ensure all child elements are also cloned.
  353. *
  354. * @return void
  355. */
  356. public function __clone() {
  357. foreach($this->parameters as $key=>$child) {
  358. $this->parameters[$key] = clone $child;
  359. $this->parameters[$key]->parent = $this;
  360. }
  361. }
  362. /**
  363. * Validates the node for correctness.
  364. *
  365. * The following options are supported:
  366. * - Node::REPAIR - If something is broken, and automatic repair may
  367. * be attempted.
  368. *
  369. * An array is returned with warnings.
  370. *
  371. * Every item in the array has the following properties:
  372. * * level - (number between 1 and 3 with severity information)
  373. * * message - (human readable message)
  374. * * node - (reference to the offending node)
  375. *
  376. * @param int $options
  377. * @return array
  378. */
  379. public function validate($options = 0) {
  380. $warnings = array();
  381. // Checking if our value is UTF-8
  382. if (!StringUtil::isUTF8($this->getRawMimeDirValue())) {
  383. $oldValue = $this->getRawMimeDirValue();
  384. $level = 3;
  385. if ($options & self::REPAIR) {
  386. $newValue = StringUtil::convertToUTF8($oldValue);
  387. if (true || StringUtil::isUTF8($newValue)) {
  388. $this->setRawMimeDirValue($newValue);
  389. $level = 1;
  390. }
  391. }
  392. if (preg_match('%([\x00-\x08\x0B-\x0C\x0E-\x1F\x7F])%', $oldValue, $matches)) {
  393. $message = 'Property contained a control character (0x' . bin2hex($matches[1]) . ')';
  394. } else {
  395. $message = 'Property is not valid UTF-8! ' . $oldValue;
  396. }
  397. $warnings[] = array(
  398. 'level' => $level,
  399. 'message' => $message,
  400. 'node' => $this,
  401. );
  402. }
  403. // Checking if the propertyname does not contain any invalid bytes.
  404. if (!preg_match('/^([A-Z0-9-]+)$/', $this->name)) {
  405. $warnings[] = array(
  406. 'level' => 1,
  407. 'message' => 'The propertyname: ' . $this->name . ' contains invalid characters. Only A-Z, 0-9 and - are allowed',
  408. 'node' => $this,
  409. );
  410. if ($options & self::REPAIR) {
  411. // Uppercasing and converting underscores to dashes.
  412. $this->name = strtoupper(
  413. str_replace('_', '-', $this->name)
  414. );
  415. // Removing every other invalid character
  416. $this->name = preg_replace('/([^A-Z0-9-])/u', '', $this->name);
  417. }
  418. }
  419. // Validating inner parameters
  420. foreach($this->parameters as $param) {
  421. $warnings = array_merge($warnings, $param->validate($options));
  422. }
  423. return $warnings;
  424. }
  425. }