PHP plist generator


/ Published in: PHP
Save to your folder(s)

Convert a php array to an apple plist xml or text format


Copy this code and paste it in your HTML
  1. <?PHP
  2.  
  3. /**
  4.  * PropertyList class
  5.  * Implements writing Apple Property List (.plist) XML and text files from an array.
  6.  *
  7.  * @author Jesus A. Alvarez <[email protected]>
  8.  */
  9.  
  10. function plist_encode_text ($obj) {
  11. $plist = new PropertyList($obj);
  12. return $plist->text();
  13. }
  14.  
  15. function plist_encode_xml ($obj) {
  16. $plist = new PropertyList($obj);
  17. return $plist->xml();
  18. }
  19.  
  20. class PropertyList
  21. {
  22. private $obj, $xml, $text;
  23.  
  24. public function __construct ($obj) {
  25. $this->obj = $obj;
  26. }
  27.  
  28. private static function is_assoc ($array) {
  29. return (is_array($array) && 0 !== count(array_diff_key($array, array_keys(array_keys($array)))));
  30. }
  31.  
  32. public function xml () {
  33. if (isset($this->xml)) return $this->xml;
  34. $x = new XMLWriter();
  35. $x->openMemory();
  36. $x->setIndent(TRUE);
  37. $x->startDocument('1.0', 'UTF-8');
  38. $x->writeDTD('plist', '-//Apple//DTD PLIST 1.0//EN', 'http://www.apple.com/DTDs/PropertyList-1.0.dtd');
  39. $x->startElement('plist');
  40. $x->writeAttribute('version', '1.0');
  41. $this->xmlWriteValue($x, $this->obj);
  42. $x->endElement(); // plist
  43. $x->endDocument();
  44. $this->xml = $x->outputMemory();
  45. return $this->xml;
  46. }
  47.  
  48. public function text() {
  49. if (isset($this->text)) return $this->text;
  50. $text = '';
  51. $this->textWriteValue($text, $this->obj);
  52. $this->text = $text;
  53. return $this->text;
  54. }
  55.  
  56. private function xmlWriteDict(XMLWriter $x, &$dict) {
  57. $x->startElement('dict');
  58. foreach($dict as $k => &$v) {
  59. $x->writeElement('key', $k);
  60. $this->xmlWriteValue($x, $v);
  61. }
  62. $x->endElement(); // dict
  63. }
  64.  
  65. private function xmlWriteArray(XMLWriter $x, &$arr) {
  66. $x->startElement('array');
  67. foreach($arr as &$v)
  68. $this->xmlWriteValue($x, $v);
  69. $x->endElement(); // array
  70. }
  71.  
  72. private function xmlWriteValue(XMLWriter $x, &$v) {
  73. if (is_int($v) || is_long($v))
  74. $x->writeElement('integer', $v);
  75. elseif (is_float($v) || is_real($v) || is_double($v))
  76. $x->writeElement('real', $v);
  77. elseif (is_string($v))
  78. $x->writeElement('string', $v);
  79. elseif (is_bool($v))
  80. $x->writeElement($v?'true':'false');
  81. elseif (PropertyList::is_assoc($v))
  82. $this->xmlWriteDict($x, $v);
  83. elseif (is_array($v))
  84. $this->xmlWriteArray($x, $v);
  85. elseif (is_a($v, 'PlistData'))
  86. $x->writeElement('data', $v->base64EncodedData());
  87. elseif (is_a($v, 'PlistDate'))
  88. $x->writeElement('date', $v->encodedDate());
  89. else {
  90. trigger_error("Unsupported data type in plist ($v)", E_USER_WARNING);
  91. $x->writeElement('string', $v);
  92. }
  93. }
  94.  
  95. private function textWriteValue(&$text, &$v, $indentLevel = 0) {
  96. if (is_int($v) || is_long($v))
  97. $text .= sprintf("%d", $v);
  98. elseif (is_float($v) || is_real($v) || is_double($v))
  99. $text .= sprintf("%g", $v);
  100. elseif (is_string($v))
  101. $this->textWriteString($text, $v);
  102. elseif (is_bool($v))
  103. $text .= $v?'YES':'NO';
  104. elseif (PropertyList::is_assoc($v))
  105. $this->textWriteDict($text, $v, $indentLevel);
  106. elseif (is_array($v))
  107. $this->textWriteArray($text, $v, $indentLevel);
  108. elseif (is_a($v, 'PlistData'))
  109. $text .= '<' . $v->hexEncodedData() . '>';
  110. elseif (is_a($v, 'PlistDate'))
  111. $text .= '"' . $v->ISO8601Date() . '"';
  112. else {
  113. trigger_error("Unsupported data type in plist ($v)", E_USER_WARNING);
  114. $this->textWriteString($text, $v);
  115. }
  116. }
  117.  
  118. private function textWriteString(&$text, &$str) {
  119. $oldlocale = setlocale(LC_CTYPE, "0");
  120. if (ctype_alnum($str)) $text .= $str;
  121. else $text .= '"' . $this->textEncodeString($str) . '"';
  122. setlocale(LC_CTYPE, $oldlocale);
  123. }
  124.  
  125. private function textEncodeString(&$str) {
  126. $newstr = '';
  127. $i = 0;
  128. $len = strlen($str);
  129. while($i < $len) {
  130. $ch = ord(substr($str, $i, 1));
  131. if ($ch == 0x22 || $ch == 0x5C) {
  132. // escape double quote, backslash
  133. $newstr .= '\\' . chr($ch);
  134. $i++;
  135. } else if ($ch >= 0x07 && $ch <= 0x0D ){
  136. // control characters with escape sequences
  137. $newstr .= '\\' . substr('abtnvfr', $ch - 7, 1);
  138. $i++;
  139. } else if ($ch < 32) {
  140. // other non-printable characters escaped as unicode
  141. $newstr .= sprintf('\U%04x', $ch);
  142. $i++;
  143. } else if ($ch < 128) {
  144. // ascii printable
  145. $newstr .= chr($ch);
  146. $i++;
  147. } else if ($ch == 192 || $ch == 193) {
  148. // invalid encoding of ASCII characters
  149. $i++;
  150. } else if (($ch & 0xC0) == 0x80){
  151. // part of a lost multibyte sequence, skip
  152. $i++;
  153. } else if (($ch & 0xE0) == 0xC0) {
  154. // U+0080 - U+07FF (2 bytes)
  155. $u = (($ch & 0x1F) << 6) | (ord(substr($str, $i+1, 1)) & 0x3F);
  156. $newstr .= sprintf('\U%04x', $u);
  157. $i += 2;
  158. } else if (($ch & 0xF0) == 0xE0) {
  159. // U+0800 - U+FFFF (3 bytes)
  160. $u = (($ch & 0x0F) << 12) | ((ord(substr($str, $i+1, 1)) & 0x3F) << 6) | (ord(substr($str, $i+2, 1)) & 0x3F);
  161. $newstr .= sprintf('\U%04x', $u);
  162. $i += 3;
  163. } else if (($ch & 0xF8) == 0xF0) {
  164. // U+10000 - U+3FFFF (4 bytes)
  165. $u = (($ch & 0x07) << 18) | ((ord(substr($str, $i+1, 1)) & 0x3F) << 12) | ((ord(substr($str, $i+2, 1)) & 0x3F) << 6) | (ord(substr($str, $i+3, 1)) & 0x3F);
  166. $newstr .= sprintf('\U%04x', $u);
  167. $i += 4;
  168. } else {
  169. // 5 and 6 byte sequences are not valid UTF-8
  170. $i++;
  171. }
  172. }
  173. return $newstr;
  174. }
  175.  
  176. private function textWriteDict(&$text, &$dict, $indentLevel) {
  177. if (count($dict) == 0) {
  178. $text .= '{}';
  179. return;
  180. }
  181. $text .= "{\n";
  182. $indent = '';
  183. $indentLevel++;
  184. while(strlen($indent) < $indentLevel) $indent .= "\t";
  185. foreach($dict as $k => &$v) {
  186. $text .= $indent;
  187. $this->textWriteValue($text, $k);
  188. $text .= ' = ';
  189. $this->textWriteValue($text, $v, $indentLevel);
  190. $text .= ";\n";
  191. }
  192. $text .= substr($indent, 0, -1) . '}';
  193. }
  194.  
  195. private function textWriteArray(&$text, &$arr, $indentLevel) {
  196. if (count($arr) == 0) {
  197. $text .= '()';
  198. return;
  199. }
  200. $text .= "(\n";
  201. $indent = '';
  202. $indentLevel++;
  203. while(strlen($indent) < $indentLevel) $indent .= "\t";
  204. foreach($arr as &$v) {
  205. $text .= $indent;
  206. $this->textWriteValue($text, $v, $indentLevel);
  207. $text .= ",\n";
  208. }
  209. $text .= substr($indent, 0, -1) . ')';
  210. }
  211. }
  212.  
  213. class PlistData
  214. {
  215. private $data;
  216.  
  217. public function __construct($str) {
  218. $this->data = $str;
  219. }
  220.  
  221. public function base64EncodedData () {
  222. return base64_encode($this->data);
  223. }
  224.  
  225. public function hexEncodedData () {
  226. $len = strlen($this->data);
  227. $hexstr = '';
  228. for($i = 0; $i < $len; $i += 4)
  229. $hexstr .= bin2hex(substr($this->data, $i, 4)) . ' ';
  230. return substr($hexstr, 0, -1);
  231. }
  232. }
  233.  
  234. class PlistDate
  235. {
  236. private $dateval;
  237.  
  238. public function __construct($init = NULL) {
  239. if (is_int($init))
  240. $this->dateval = $init;
  241. elseif (is_string($init))
  242. $this->dateval = strtotime($init);
  243. elseif ($init == NULL)
  244. $this->dateval = time();
  245. }
  246.  
  247. public function ISO8601Date() {
  248. return gmdate('Y-m-d\TH:i:s\Z', $this->dateval);
  249. }
  250. }
  251. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.