IDNA.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // {{{ license
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  4. //
  5. // +----------------------------------------------------------------------+
  6. // | This library is free software; you can redistribute it and/or modify |
  7. // | it under the terms of the GNU Lesser General Public License as |
  8. // | published by the Free Software Foundation; either version 2.1 of the |
  9. // | License, or (at your option) any later version. |
  10. // | |
  11. // | This library is distributed in the hope that it will be useful, but |
  12. // | WITHOUT ANY WARRANTY; without even the implied warranty of |
  13. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
  14. // | Lesser General Public License for more details. |
  15. // | |
  16. // | You should have received a copy of the GNU Lesser General Public |
  17. // | License along with this library; if not, write to the Free Software |
  18. // | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
  19. // | USA. |
  20. // +----------------------------------------------------------------------+
  21. //
  22. // }}}
  23. /**
  24. * Encode/decode Internationalized Domain Names.
  25. * Factory class to get correct implementation either for php4 or php5.
  26. *
  27. * @author Markus Nix <mnix@docuverse.de>
  28. * @author Matthias Sommerfeld <mso@phlylabs.de>
  29. * @package Net
  30. * @version $Id: IDNA.php 284681 2009-07-24 04:24:27Z clockwerx $
  31. */
  32. class Net_IDNA
  33. {
  34. // {{{ factory
  35. /**
  36. * Attempts to return a concrete IDNA instance for either php4 or php5.
  37. *
  38. * @param array $params Set of paramaters
  39. * @return object IDNA The newly created concrete Log instance, or an
  40. * false on an error.
  41. * @access public
  42. */
  43. function getInstance($params = array())
  44. {
  45. $version = explode( '.', phpversion() );
  46. $handler = ((int)$version[0] > 4) ? 'php5' : 'php4';
  47. $class = 'Net_IDNA_' . $handler;
  48. $classfile = 'Net/IDNA/' . $handler . '.php';
  49. /*
  50. * Attempt to include our version of the named class, but don't treat
  51. * a failure as fatal. The caller may have already included their own
  52. * version of the named class.
  53. */
  54. @include_once $classfile;
  55. /* If the class exists, return a new instance of it. */
  56. if (class_exists($class)) {
  57. return new $class($params);
  58. }
  59. return false;
  60. }
  61. // }}}
  62. // {{{ singleton
  63. /**
  64. * Attempts to return a concrete IDNA instance for either php4 or php5,
  65. * only creating a new instance if no IDNA instance with the same
  66. * parameters currently exists.
  67. *
  68. * @param array $params Set of paramaters
  69. * @return object IDNA The newly created concrete Log instance, or an
  70. * false on an error.
  71. * @access public
  72. */
  73. function singleton($params = array())
  74. {
  75. static $instances;
  76. if (!isset($instances)) {
  77. $instances = array();
  78. }
  79. $signature = serialize($params);
  80. if (!isset($instances[$signature])) {
  81. $instances[$signature] = Net_IDNA::getInstance($params);
  82. }
  83. return $instances[$signature];
  84. }
  85. // }}}
  86. }
  87. ?>