public function testWriteFile() { $expected = '<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"><dict><key>string</key><string/><key>number</key><integer>0</integer><key>double</key><real>0</real></dict></plist> '; $plist = new CFPropertyList(); $dict = new CFDictionary(); $dict->add('string', new CFString('')); $dict->add('number', new CFNumber(0)); $dict->add('double', new CFNumber(0.0)); $plist->add($dict); $this->assertEquals($expected, $plist->toXML()); }
public function testWriteUid() { $plist = new CFPropertyList(); $dict = new CFDictionary(); $dict->add('test', new CFUid(1)); $plist->add($dict); $plist1 = new CFPropertyList(TEST_UID_XML_PLIST); $this->assertEquals($plist1->toXml(), $plist->toXml()); }
public function testWriteString() { $plist = new CFPropertyList(); $dict = new CFDictionary(); $names = new CFDictionary(); $names->add('given-name', new CFString('John')); $names->add('surname', new CFString('Dow')); $dict->add('names', $names); $pets = new CFArray(); $pets->add(new CFString('Jonny')); $pets->add(new CFString('Bello')); $dict->add('pets', $pets); $dict->add('age', new CFNumber(28)); $dict->add('birth-date', new CFDate(412035803)); $plist->add($dict); $content = $plist->toXML(); $plist->parse($content); }
function generateManifest($manifest_path, $parent, $catalog) { $plist = new CFPropertyList(); $plist->add($dict = new CFDictionary()); if ($catalog != '') { // Add manifest to production catalog by default $dict->add('catalogs', $array = new CFArray()); $array->add(new CFString($catalog)); } // Add parent manifest to included_manifests to achieve waterfall effect $dict->add('included_manifests', $array = new CFArray()); $array->add(new CFString($parent)); $tmp = explode('/', $manifest_path); $manifest = end($tmp); logToFile("Generating manifest '{$manifest}'..."); // Save the newly created plist $plist->saveXML($manifest_path); }
/** * Read a dictionary value, including contained objects * @param string $fname The filename of the file we're reading from * @param ressource $fd The file descriptor we're reading from * @param integer $length The number of contained objects * @return CFDictionary The dictionary value, including the objects * @throws IOException if read error occurs */ protected function readBinaryDict($fname, $fd, $length) { $dict = new CFDictionary(); // first: read keys if ($length != 0) { if (strlen($buff = fread($fd, $length * $this->objectRefSize)) != $length * $this->objectRefSize) { throw IOException::readError($fname); } $keys = unpack($this->objectRefSize == 1 ? "C*" : "n*", $buff); // second: read object refs if (strlen($buff = fread($fd, $length * $this->objectRefSize)) != $length * $this->objectRefSize) { throw IOException::readError($fname); } $objects = unpack($this->objectRefSize == 1 ? "C*" : "n*", $buff); // read real keys and objects for ($i = 0; $i < $length; ++$i) { $key = $this->readBinaryObjectAt($fname, $fd, $keys[$i + 1] + 1); $object = $this->readBinaryObjectAt($fname, $fd, $objects[$i + 1] + 1); $dict->add($key->getValue(), $object); } } return $dict; }
$identifier = $_GET["identifier"]; $hostname = $_GET["hostname"]; // Split the manifest path up to determine directory structure $directories = explode("/", $identifier, -1); $total = count($directories); $n = 0; $identifier_path = ""; while ($n < $total) { $identifier_path .= $directories[$n] . '/'; $n++; } // Check if manifest already exists for this machine if (file_exists('../manifests/' . $identifier_path . '/clients/' . $hostname)) { echo "Computer manifest already exists."; } else { echo "Computer manifest does not exist. Will create."; if (!is_dir('../manifests/' . $identifier_path . 'clients/')) { mkdir('../manifests/' . $identifier_path . 'clients/', 0755, true); } // Create the new manifest plist $plist = new CFPropertyList(); $plist->add($dict = new CFDictionary()); // Add manifest to production catalog by default $dict->add('catalogs', $array = new CFArray()); $array->add(new CFString('production')); // Add parent manifest to included_manifests to achieve waterfall effect $dict->add('included_manifests', $array = new CFArray()); $array->add(new CFString($identifier)); // Save the newly created plist $plist->saveXML('../manifests/' . $identifier_path . 'clients/' . $hostname); }
/** * Create CFType-structure from guessing the data-types. * {@link CFArray}, {@link CFDictionary}, {@link CFBoolean}, {@link CFNumber} and {@link CFString} can be created, {@link CFDate} and {@link CFData} cannot. * <br /><b>Note:</b>Distinguishing between {@link CFArray} and {@link CFDictionary} is done by examining the keys. * Keys must be strictly incrementing integers to evaluate to a {@link CFArray}. * Since PHP does not offer a function to test for associative arrays, * this test causes the input array to be walked twice and thus work rather slow on large collections. * If you work with large arrays and can live with all arrays evaluating to {@link CFDictionary}, * feel free to set the appropriate flag. * <br /><b>Note:</b> If $value is an instance of CFType it is simply returned. * <br /><b>Note:</b> If $value is neither a CFType, array, numeric, boolean nor string, it is omitted. * @param mixed $value Value to convert to CFType * @param boolean $autoDictionary if true {@link CFArray}-detection is bypassed and arrays will be returned as {@link CFDictionary}. * @return CFType CFType based on guessed type */ public static function guess($value, $autoDictionary = false) { switch (true) { case $value instanceof CFType: return $value; break; case is_array($value): // test if $value is simple or associative array if (!$autoDictionary) { $numericKeys = true; $previousKey = null; foreach ($value as $key => $v) { if (!is_numeric($key) || $previousKey !== null && $previousKey != $key - 1) { $numericKeys = false; break; } $previousKey = $key; } if ($numericKeys) { $t = new CFArray(); foreach ($value as $v) { $t->add(self::guess($v, $autoDictionary)); } return $t; } } $t = new CFDictionary(); foreach ($value as $k => $v) { $t->add($k, self::guess($v, $autoDictionary)); } return $t; break; case is_numeric($value): return new CFNumber($value); break; case is_bool($value): return new CFBoolean($value); break; case is_string($value): return new CFString($value); break; } }
/** * Create CFType-structure by guessing the data-types. * {@link CFArray}, {@link CFDictionary}, {@link CFBoolean}, {@link CFNumber} and {@link CFString} can be created, {@link CFDate} and {@link CFData} cannot. * <br /><b>Note:</b>Distinguishing between {@link CFArray} and {@link CFDictionary} is done by examining the keys. * Keys must be strictly incrementing integers to evaluate to a {@link CFArray}. * Since PHP does not offer a function to test for associative arrays, * this test causes the input array to be walked twice and thus work rather slow on large collections. * If you work with large arrays and can live with all arrays evaluating to {@link CFDictionary}, * feel free to set the appropriate flag. * <br /><b>Note:</b> If $value is an instance of CFType it is simply returned. * <br /><b>Note:</b> If $value is neither a CFType, array, numeric, boolean nor string, it is omitted. * @param mixed $value Value to convert to CFType * @param boolean $autoDictionary if true {@link CFArray}-detection is bypassed and arrays will be returned as {@link CFDictionary}. * @return CFType CFType based on guessed type * @uses isAssociativeArray() to check if an array only has numeric indexes */ public function toCFType($value) { switch (true) { case $value instanceof CFType: return $value; break; case is_object($value): // DateTime should be CFDate if (class_exists('DateTime') && $value instanceof DateTime) { return new CFDate($value->getTimestamp()); } // convert possible objects to arrays, arrays will be arrays if ($this->objectToArrayMethod && is_callable(array($value, $this->objectToArrayMethod))) { $value = call_user_func(array($value, $this->objectToArrayMethod)); } if (!is_array($value)) { if ($this->suppressExceptions) { return $this->defaultValue(); } throw new PListException('Could not determine CFType for object of type ' . get_class($value)); } /* break; omitted */ /* break; omitted */ case $value instanceof Iterator: case is_array($value): // test if $value is simple or associative array if (!$this->autoDictionary) { if (!$this->isAssociativeArray($value)) { $t = new CFArray(); foreach ($value as $v) { $t->add($this->toCFType($v)); } return $t; } } $t = new CFDictionary(); foreach ($value as $k => $v) { $t->add($k, $this->toCFType($v)); } return $t; break; case is_numeric($value): return new CFNumber($value); break; case is_bool($value): return new CFBoolean($value); break; case is_string($value): return new CFString($value); break; case is_null($value): return new CFString(); break; case is_resource($value): if ($this->suppressExceptions) { return $this->defaultValue(); } throw new PListException('Could not determine CFType for resource of type ' . get_resource_type($value)); break; default: if ($this->suppressExceptions) { return $this->defaultValue(); } throw new PListException('Could not determine CFType for ' . gettype($value)); break; } }
/** * Read a dictionary value, including contained objects * @param integer $length The number of contained objects * @return CFDictionary The dictionary value, including the objects * @throws IOException if read error occurs */ protected function readBinaryDict($length) { $dict = new CFDictionary(); // first: read keys if ($length != 0) { if (strlen($buff = substr($this->content, $this->pos, $length * $this->objectRefSize)) != $length * $this->objectRefSize) { throw IOException::readError(""); } $this->pos += $length * $this->objectRefSize; $keys = self::unpackWithSize($this->objectRefSize, $buff); // second: read object refs if (strlen($buff = substr($this->content, $this->pos, $length * $this->objectRefSize)) != $length * $this->objectRefSize) { throw IOException::readError(""); } $this->pos += $length * $this->objectRefSize; $objects = self::unpackWithSize($this->objectRefSize, $buff); // read real keys and objects for ($i = 0; $i < $length; ++$i) { $key = $this->readBinaryObjectAt($keys[$i + 1] + 1); $object = $this->readBinaryObjectAt($objects[$i + 1] + 1); $dict->add($key->getValue(), $object); } } return $dict; }
/** * Convert a DOMNode into a CFType. * @param DOMNode $node Node to import children of * @param CFDictionary|CFArray|CFPropertyList $parent * @return void */ protected function import(DOMNode $node, $parent) { // abort if there are no children if (!$node->childNodes->length) { return; } foreach ($node->childNodes as $n) { // skip if we can't handle the element if (!isset(self::$types[$n->nodeName])) { continue; } $class = self::$types[$n->nodeName]; $key = null; // find previous <key> if possible $ps = $n->previousSibling; while ($ps && $ps->nodeName == '#text' && $ps->previousSibling) { $ps = $ps->previousSibling; } // read <key> if possible if ($ps && $ps->nodeName == 'key') { $key = $ps->firstChild->nodeValue; } switch ($n->nodeName) { case 'date': $value = new $class(CFDate::dateValue($n->nodeValue)); break; case 'data': $value = new $class($n->nodeValue, true); break; case 'string': $value = new $class($n->nodeValue); break; case 'real': case 'integer': $value = new $class($n->nodeName == 'real' ? floatval($n->nodeValue) : intval($n->nodeValue)); break; case 'true': case 'false': $value = new $class($n->nodeName == 'true'); break; case 'array': case 'dict': $value = new $class(); $this->import($n, $value); break; } // Dictionaries need a key if ($parent instanceof CFDictionary) { $parent->add($key, $value); } else { $parent->add($value); } } }
protected function readBinaryDict($length) { $dict = new CFDictionary(); if ($length != 0) { if (strlen($buff = substr($this->content, $this->pos, $length * $this->objectRefSize)) != $length * $this->objectRefSize) { throw IOException::readError(""); } $this->pos += $length * $this->objectRefSize; $keys = unpack($this->objectRefSize == 1 ? "C*" : "n*", $buff); if (strlen($buff = substr($this->content, $this->pos, $length * $this->objectRefSize)) != $length * $this->objectRefSize) { throw IOException::readError(""); } $this->pos += $length * $this->objectRefSize; $objects = unpack($this->objectRefSize == 1 ? "C*" : "n*", $buff); for ($i = 0; $i < $length; ++$i) { $key = $this->readBinaryObjectAt($keys[$i + 1] + 1); $object = $this->readBinaryObjectAt($objects[$i + 1] + 1); $dict->add($key->getValue(), $object); } } return $dict; }
ini_set('display_errors', 'on'); /** * Require CFPropertyList */ require_once __DIR__ . '/../classes/CFPropertyList/CFPropertyList.php'; /* * create a new CFPropertyList instance without loading any content */ $plist = new CFPropertyList(); /* * Manuall Create the sample.xml.plist */ // the Root element of the PList is a Dictionary $plist->add($dict = new CFDictionary()); // <key>Year Of Birth</key><integer>1965</integer> $dict->add('Year Of Birth', new CFNumber(1965)); // <key>Date Of Graduation</key><date>2004-06-22T19:23:43Z</date> $dict->add('Date Of Graduation', new CFDate(gmmktime(19, 23, 43, 06, 22, 2004))); // <key>Pets Names</key><array/> $dict->add('Pets Names', new CFArray()); // <key>Picture</key><data>PEKBpYGlmYFCPA==</data> // to keep it simple we insert an already base64-encoded string $dict->add('Picture', new CFData('PEKBpYGlmYFCPA==', true)); // <key>City Of Birth</key><string>Springfield</string> $dict->add('City Of Birth', new CFString('Springfield')); // <key>Name</key><string>John Doe</string> $dict->add('Name', new CFString('John Doe')); // <key>Kids Names</key><array><string>John</string><string>Kyra</string></array> $dict->add('Kids Names', $array = new CFArray()); $array->add(new CFString('John')); $array->add(new CFString('Kyra'));
private function formatEntryAsPlist($entry) { $dict = new CFDictionary(); $dict->add('entryId', new CFNumber($entry['id'])); $dict->add('firstName', new CFString($entry['first_name'])); $dict->add('lastName', new CFString($entry['last_name'])); $dict->add('phone', new CFString($entry['phone'])); $dict->add('email', new CFString($entry['email'])); $dict->add('address', new CFString($entry['address'])); $dict->add('city', new CFString($entry['city'])); $dict->add('zip', new CFString($entry['zip'])); $dict->add('state', new CFString($entry['state'])); $dict->add('country', new CFString($entry['country'])); $dict->add('description', new CFString($entry['description'])); $dict->add('password', new CFString($entry['password'])); $dict->add('createdOn', new CFString($entry['created_on'])); $dict->add('modifiedOn', new CFString($entry['modified_on'])); return $dict; }
/** * Create CFType-structure by guessing the data-types. * {@link CFArray}, {@link CFDictionary}, {@link CFBoolean}, {@link CFNumber} and {@link CFString} can be created, {@link CFDate} and {@link CFData} cannot. * <br /><b>Note:</b>Distinguishing between {@link CFArray} and {@link CFDictionary} is done by examining the keys. * Keys must be strictly incrementing integers to evaluate to a {@link CFArray}. * Since PHP does not offer a function to test for associative arrays, * this test causes the input array to be walked twice and thus work rather slow on large collections. * If you work with large arrays and can live with all arrays evaluating to {@link CFDictionary}, * feel free to set the appropriate flag. * <br /><b>Note:</b> If $value is an instance of CFType it is simply returned. * <br /><b>Note:</b> If $value is neither a CFType, array, numeric, boolean nor string, it is omitted. * @param mixed $value Value to convert to CFType * @param boolean $autoDictionary if true {@link CFArray}-detection is bypassed and arrays will be returned as {@link CFDictionary}. * @return CFType CFType based on guessed type * @uses isAssociativeArray() to check if an array only has numeric indexes */ public function toCFType($value) { switch (true) { case $value instanceof CFType: return $value; break; case $value instanceof Iterator: case is_array($value): // test if $value is simple or associative array if (!$this->autoDictionary) { if (!$this->isAssociativeArray($value)) { $t = new CFArray(); foreach ($value as $v) { $t->add($this->toCFType($v)); } return $t; } } $t = new CFDictionary(); foreach ($value as $k => $v) { $t->add($k, $this->toCFType($v)); } return $t; break; case is_numeric($value): return new CFNumber($value); break; case is_bool($value): return new CFBoolean($value); break; case is_string($value): return new CFString($value); break; case is_null($value): return new CFString(); break; case is_object($value): if ($this->suppressExceptions) { return $this->defaultValue(); } throw new PListException('Could not determine CFType for object of type ' . get_class($value)); break; case is_resource($value): if ($this->suppressExceptions) { return $this->defaultValue(); } throw new PListException('Could not determine CFType for resource of type ' . get_resource_type($value)); break; default: if ($this->suppressExceptions) { return $this->defaultValue(); } throw new PListException('Could not determine CFType for ' . gettype($value)); break; } }