Text.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. namespace Sabre\VObject\Property;
  3. use
  4. Sabre\VObject\Property,
  5. Sabre\VObject\Component,
  6. Sabre\VObject\Parser\MimeDir,
  7. Sabre\VObject\Document;
  8. /**
  9. * Text property
  10. *
  11. * This object represents TEXT values.
  12. *
  13. * @copyright Copyright (C) 2007-2014 fruux GmbH. All rights reserved.
  14. * @author Evert Pot (http://evertpot.com/)
  15. * @license http://sabre.io/license/ Modified BSD License
  16. */
  17. class Text extends Property {
  18. /**
  19. * In case this is a multi-value property. This string will be used as a
  20. * delimiter.
  21. *
  22. * @var string
  23. */
  24. public $delimiter = ',';
  25. /**
  26. * List of properties that are considered 'structured'.
  27. *
  28. * @var array
  29. */
  30. protected $structuredValues = array(
  31. // vCard
  32. 'N',
  33. 'ADR',
  34. 'ORG',
  35. 'GENDER',
  36. // iCalendar
  37. 'REQUEST-STATUS',
  38. );
  39. /**
  40. * Some text components have a minimum number of components.
  41. *
  42. * N must for instance be represented as 5 components, separated by ;, even
  43. * if the last few components are unused.
  44. *
  45. * @var array
  46. */
  47. protected $minimumPropertyValues = array(
  48. 'N' => 5,
  49. 'ADR' => 7,
  50. );
  51. /**
  52. * Creates the property.
  53. *
  54. * You can specify the parameters either in key=>value syntax, in which case
  55. * parameters will automatically be created, or you can just pass a list of
  56. * Parameter objects.
  57. *
  58. * @param Component $root The root document
  59. * @param string $name
  60. * @param string|array|null $value
  61. * @param array $parameters List of parameters
  62. * @param string $group The vcard property group
  63. * @return void
  64. */
  65. public function __construct(Component $root, $name, $value = null, array $parameters = array(), $group = null) {
  66. // There's two types of multi-valued text properties:
  67. // 1. multivalue properties.
  68. // 2. structured value properties
  69. //
  70. // The former is always separated by a comma, the latter by semi-colon.
  71. if (in_array($name, $this->structuredValues)) {
  72. $this->delimiter = ';';
  73. }
  74. parent::__construct($root, $name, $value, $parameters, $group);
  75. }
  76. /**
  77. * Sets a raw value coming from a mimedir (iCalendar/vCard) file.
  78. *
  79. * This has been 'unfolded', so only 1 line will be passed. Unescaping is
  80. * not yet done, but parameters are not included.
  81. *
  82. * @param string $val
  83. * @return void
  84. */
  85. public function setRawMimeDirValue($val) {
  86. $this->setValue(MimeDir::unescapeValue($val, $this->delimiter));
  87. }
  88. /**
  89. * Sets the value as a quoted-printable encoded string.
  90. *
  91. * @param string $val
  92. * @return void
  93. */
  94. public function setQuotedPrintableValue($val) {
  95. $val = quoted_printable_decode($val);
  96. // Quoted printable only appears in vCard 2.1, and the only character
  97. // that may be escaped there is ;. So we are simply splitting on just
  98. // that.
  99. //
  100. // We also don't have to unescape \\, so all we need to look for is a ;
  101. // that's not preceeded with a \.
  102. $regex = '# (?<!\\\\) ; #x';
  103. $matches = preg_split($regex, $val);
  104. $this->setValue($matches);
  105. }
  106. /**
  107. * Returns a raw mime-dir representation of the value.
  108. *
  109. * @return string
  110. */
  111. public function getRawMimeDirValue() {
  112. $val = $this->getParts();
  113. if (isset($this->minimumPropertyValues[$this->name])) {
  114. $val = array_pad($val, $this->minimumPropertyValues[$this->name], '');
  115. }
  116. foreach($val as &$item) {
  117. if (!is_array($item)) {
  118. $item = array($item);
  119. }
  120. foreach($item as &$subItem) {
  121. $subItem = strtr(
  122. $subItem,
  123. array(
  124. '\\' => '\\\\',
  125. ';' => '\;',
  126. ',' => '\,',
  127. "\n" => '\n',
  128. "\r" => "",
  129. )
  130. );
  131. }
  132. $item = implode(',', $item);
  133. }
  134. return implode($this->delimiter, $val);
  135. }
  136. /**
  137. * Returns the value, in the format it should be encoded for json.
  138. *
  139. * This method must always return an array.
  140. *
  141. * @return array
  142. */
  143. public function getJsonValue() {
  144. // Structured text values should always be returned as a single
  145. // array-item. Multi-value text should be returned as multiple items in
  146. // the top-array.
  147. if (in_array($this->name, $this->structuredValues)) {
  148. return array($this->getParts());
  149. } else {
  150. return $this->getParts();
  151. }
  152. }
  153. /**
  154. * Returns the type of value.
  155. *
  156. * This corresponds to the VALUE= parameter. Every property also has a
  157. * 'default' valueType.
  158. *
  159. * @return string
  160. */
  161. public function getValueType() {
  162. return "TEXT";
  163. }
  164. /**
  165. * Turns the object back into a serialized blob.
  166. *
  167. * @return string
  168. */
  169. public function serialize() {
  170. // We need to kick in a special type of encoding, if it's a 2.1 vcard.
  171. if ($this->root->getDocumentType() !== Document::VCARD21) {
  172. return parent::serialize();
  173. }
  174. $val = $this->getParts();
  175. if (isset($this->minimumPropertyValues[$this->name])) {
  176. $val = array_pad($val, $this->minimumPropertyValues[$this->name], '');
  177. }
  178. // Imploding multiple parts into a single value, and splitting the
  179. // values with ;.
  180. if (count($val)>1) {
  181. foreach($val as $k=>$v) {
  182. $val[$k] = str_replace(';','\;', $v);
  183. }
  184. $val = implode(';', $val);
  185. } else {
  186. $val = $val[0];
  187. }
  188. $str = $this->name;
  189. if ($this->group) $str = $this->group . '.' . $this->name;
  190. foreach($this->parameters as $param) {
  191. if ($param->getValue() === 'QUOTED-PRINTABLE') {
  192. continue;
  193. }
  194. $str.=';' . $param->serialize();
  195. }
  196. // If the resulting value contains a \n, we must encode it as
  197. // quoted-printable.
  198. if (strpos($val,"\n") !== false) {
  199. $str.=';ENCODING=QUOTED-PRINTABLE:';
  200. $lastLine=$str;
  201. $out = null;
  202. // The PHP built-in quoted-printable-encode does not correctly
  203. // encode newlines for us. Specifically, the \r\n sequence must in
  204. // vcards be encoded as =0D=OA and we must insert soft-newlines
  205. // every 75 bytes.
  206. for($ii=0;$ii<strlen($val);$ii++) {
  207. $ord = ord($val[$ii]);
  208. // These characters are encoded as themselves.
  209. if ($ord >= 32 && $ord <=126) {
  210. $lastLine.=$val[$ii];
  211. } else {
  212. $lastLine.='=' . strtoupper(bin2hex($val[$ii]));
  213. }
  214. if (strlen($lastLine)>=75) {
  215. // Soft line break
  216. $out.=$lastLine. "=\r\n ";
  217. $lastLine = null;
  218. }
  219. }
  220. if (!is_null($lastLine)) $out.= $lastLine . "\r\n";
  221. return $out;
  222. } else {
  223. $str.=':' . $val;
  224. $out = '';
  225. while(strlen($str)>0) {
  226. if (strlen($str)>75) {
  227. $out.= mb_strcut($str,0,75,'utf-8') . "\r\n";
  228. $str = ' ' . mb_strcut($str,75,strlen($str),'utf-8');
  229. } else {
  230. $out.=$str . "\r\n";
  231. $str='';
  232. break;
  233. }
  234. }
  235. return $out;
  236. }
  237. }
  238. /**
  239. * Validates the node for correctness.
  240. *
  241. * The following options are supported:
  242. * - Node::REPAIR - If something is broken, and automatic repair may
  243. * be attempted.
  244. *
  245. * An array is returned with warnings.
  246. *
  247. * Every item in the array has the following properties:
  248. * * level - (number between 1 and 3 with severity information)
  249. * * message - (human readable message)
  250. * * node - (reference to the offending node)
  251. *
  252. * @param int $options
  253. * @return array
  254. */
  255. public function validate($options = 0) {
  256. $warnings = parent::validate($options);
  257. if (isset($this->minimumPropertyValues[$this->name])) {
  258. $minimum = $this->minimumPropertyValues[$this->name];
  259. $parts = $this->getParts();
  260. if (count($parts) < $minimum) {
  261. $warnings[] = array(
  262. 'level' => 1,
  263. 'message' => 'This property must have at least ' . $minimum . ' components. It only has ' . count($parts),
  264. 'node' => $this,
  265. );
  266. if ($options & self::REPAIR) {
  267. $parts = array_pad($parts, $minimum, '');
  268. $this->setParts($parts);
  269. }
  270. }
  271. }
  272. return $warnings;
  273. }
  274. }