Source for file oid_format.php

Documentation is available at oid_format.php

  1. <?php
  2. /**
  3. * phpsnmp - a PHP SNMP library
  4. *
  5. * Copyright (C) 2004 David Eder <david@eder,us>
  6. *
  7. * Based on snmp - a Python SNMP library
  8. * Copyright (C) 2003 Unicity Pty Ltd <libsnmp@unicity.com.au>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * @author David Eder <david@eder.us>
  25. * @copyright 2004 David Eder
  26. * @package phpSNMP
  27. * @subpackage mib_compiler
  28. * @version .7
  29. */
  30.  
  31. /**
  32. */
  33.  
  34. define('OID_NUMERIC', 1);
  35. define('OID_TEXT', 0);
  36. function oid_format($oid, $format=OID_TEXT)
  37. {
  38. static $nodes = NULL;
  39.  
  40. if(is_null($oid) || (is_array($oid) && count($oid) == 0) || (!is_array($oid) && strlen($oid) == 0)) return $oid;
  41.  
  42. if(is_null($nodes))
  43. $nodes = unserialize(file_get_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'oid_format.data'));
  44.  
  45. $ret_type = 'string';
  46. if(is_array($oid))
  47. {
  48. $oid = join('.', $oid);
  49. $ret_type = 'array';
  50. }
  51. elseif($oid{0} != '.')
  52. $oid = '1.3.6.1.2.1.' . $oid;
  53.  
  54. while($oid{0} == '.') $oid = substr($oid, 1);
  55.  
  56. $oid = explode('.', $oid);
  57.  
  58. $parent = '';
  59. for($i = 0; $i < count($oid); $i++)
  60. {
  61. if(!isset($nodes[$parent]))
  62. {
  63. if($ret_type == 'array') return $oid;
  64. return '.' . join('.', $oid);
  65. }
  66. $rec = $nodes[$parent];
  67. if(is_numeric($oid[$i]))
  68. {
  69. if(!isset($rec[$oid[$i]]))
  70. {
  71. if($ret_type == 'array') return $oid;
  72. return '.' . join('.', $oid);
  73. }
  74. $parent = $rec[$oid[$i]];
  75. if($format == OID_TEXT) $oid[$i] = $parent;
  76. }
  77. else
  78. {
  79. $z = array_search($oid[$i], $rec);
  80. if($z === false)
  81. {
  82. if($ret_type == 'array') return $oid;
  83. return '.' . join('.', $oid);
  84. }
  85. $parent = $rec[$z];
  86. if($format == OID_NUMERIC) $oid[$i] = $z;
  87. }
  88. }
  89. if($ret_type == 'array') return $oid;
  90. return '.' . join('.', $oid);
  91. }
  92. ?>

Documentation generated on Mon, 14 Nov 2005 17:54:53 -0700 by phpDocumentor 1.3.0RC3