PageRenderTime 61ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/main/php/peer/ldap/util/LDIFWriter.class.php

https://github.com/treuter/xp-framework
PHP | 84 lines | 22 code | 5 blank | 57 comment | 3 complexity | 4e319e2c1b64fd0b4e19d3959c7ad62d MD5 | raw file
  1. <?php
  2. /* This class is part of the XP framework
  3. *
  4. * $Id$
  5. */
  6. /**
  7. * Write LDAP entries to LDIF format
  8. *
  9. * Example:
  10. * <code>
  11. * uses(
  12. * 'io.File',
  13. * 'peer.ldap.LDAPClient',
  14. * 'peer.ldap.util.LDIFWriter',
  15. * 'peer.URL'
  16. * );
  17. *
  18. * // {{{ main
  19. * $dsn= new URL('ldap://my.ldap.host:389/uid=friebe,ou=People,o=XP,c=DE');
  20. * $dn= substr($dsn->getPath(), 1);
  21. *
  22. * $l= new LDAPClient($dsn->getHost(), $dsn->getPort(389));
  23. * $writer= new LDIFWriter(new File('php://stdout'));
  24. *
  25. * try {
  26. * $l->connect();
  27. * $l->bind($dsn->getUser(NULL), $dsn->getPassword(NULL));
  28. *
  29. * $writer->initialize();
  30. * $writer->write($l->read(new LDAPEntry($dn)));
  31. * } catch(XPException $e) {
  32. * fputs(STDERR, $e->getStackTrace());
  33. * }
  34. *
  35. * $l->close();
  36. * $writer->finish();
  37. * // }}}
  38. * </code>
  39. *
  40. * @purpose LDIF Writer
  41. */
  42. class LDIFWriter extends Object {
  43. public
  44. $stream = NULL;
  45. /**
  46. * Constructor
  47. *
  48. * @param io.Stream stream
  49. */
  50. public function __construct($stream) {
  51. $this->stream= $stream;
  52. }
  53. /**
  54. * Initialize this writer
  55. *
  56. * @param string mode default STREAM_MODE_WRITE
  57. * @return bool success
  58. */
  59. public function initialize($mode= STREAM_MODE_WRITE) {
  60. return $this->stream->open($mode);
  61. }
  62. /**
  63. * Write an entry
  64. *
  65. * @param peer.ldap.LDAPEntry entry
  66. * @throws lang.IllegalArgumentException in case the parameter is not an LDAPEntry object
  67. */
  68. public function write(LDAPEntry $entry) {
  69. $this->stream->write(sprintf("dn: %s\n", $entry->getDN()));
  70. foreach (array_keys($entry->attributes) as $key) {
  71. if ('dn' == $key) continue;
  72. for ($i= 0, $s= sizeof($entry->attributes[$key]); $i < $s; $i++) {
  73. $this->stream->write(sprintf("%s: %s\n", $key, $entry->attributes[$key][$i]));
  74. }
  75. }
  76. $this->stream->write("\n");
  77. }
  78. }
  79. ?>