home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / database / ldap.class.inc next >
Text File  |  2004-03-08  |  9KB  |  403 lines

  1. <?php
  2. /*
  3.  * LDAP Abstraction class
  4.  *
  5.  * by Markus Schabel, markus.schabel@tgm.ac.at
  6.  * and by Michael Borko, michal.borko@tgm.ac.at
  7.  */
  8.  
  9. class ldap { 
  10.  
  11.   /* public: connection parameters */
  12.   var $Host     = "";
  13.   var $User     = "";
  14.   var $Password = "";
  15.   var $BaseDN    = "";
  16.   var $PeopleDN    = "";
  17.   var $GroupsDN    = "";
  18.  
  19.   /* public: result array and current row number */
  20.   var $Entry   = array();
  21.   var $NumEntry = 0;
  22.   var $Values = array();
  23.   var $NumValues = 0;
  24.  
  25.   /* public: configuration options */
  26.   var $Debug    = 0;     ## Set to 1 for debugging messages.
  27.   var $Halt_On_Error = "no";
  28.  
  29.   /* public: current error number and error text */
  30.   var $Errno    = 0;
  31.   var $Error    = "";
  32.  
  33.   /* private: link and query handles */
  34.   var $Link_ID  = 0;
  35.   var $Bind_ID  = 0;
  36.   var $Search_ID = 0;
  37.   var $Modify_ID = 0;
  38.   var $Create_ID = 0;
  39.   var $Delete_ID = 0;
  40.  
  41.   var $Auto_Free = 0;
  42.  
  43.   /* public: constructor */
  44.   function ldap($search = "") {
  45.       global $GO_CONFIG;
  46.       $this->Host = $GO_CONFIG->auth_db_host;
  47.       $this->User = $GO_CONFIG->auth_db_user;
  48.         $this->Password = $GO_CONFIG->auth_db_pass;
  49.       $this->BaseDN = $GO_CONFIG->auth_db_ldap_basedn;
  50.       $this->PeopleDN = $GO_CONFIG->auth_db_ldap_peopledn;
  51.       $this->GroupsDN = $GO_CONFIG->auth_db_ldap_groupsdn;
  52.       $this->search($search);
  53.   }
  54.  
  55.   /* public: some trivial reporting */
  56.   function link_id() {
  57.     return $this->Link_ID;
  58.   }
  59.  
  60.   function search_id() {
  61.     return $this->Search_ID;
  62.   }
  63.  
  64.   /* public: connection management */
  65.   function connect($Host = "") {
  66.     /* Handle defaults */
  67.     if ("" == $Host)
  68.       $Host     = $this->Host;
  69.  
  70.     /* establish connection, select database */
  71.     if ( 0 == $this->Link_ID ) {
  72.       $this->Link_ID=ldap_connect($Host);
  73.       if (!$this->Link_ID) {
  74.         $this->halt("pconnect($Host) failed.");
  75.         return 0;
  76.       }
  77.       @ldap_set_option($this->Link_ID,LDAP_OPT_PROTOCOL_VERSION,3);
  78.     }
  79.  
  80.     return $this->Link_ID;
  81.   }
  82.  
  83.   function disconnect() {
  84.     if ( $this->Link_ID ) {
  85.       @ldap_close( $this->Link_ID );
  86.     }
  87.   }
  88.  
  89.   function bind($User = "", $Password = "" ) {
  90.     /* Handle defaults */
  91.     if ("" == $User)
  92.       $User     = $this->User;
  93.     if ("" == $Password)
  94.       $Password = $this->Password;
  95.  
  96.     if ( $this->Link_ID ) {
  97.       if ( $User != "" ) {
  98.         $this->Bind_ID = @ldap_bind( $this->Link_ID, $User, $Password );
  99.     if (!$this->Bind_ID) {
  100.       return 0;
  101.     }
  102.       }
  103.     }
  104.  
  105.     return $this->Bind_ID;
  106.   }
  107.  
  108.   function unbind() {
  109.     if ( $this->Bind_ID ) {
  110.       ldap_unbind( $this->Link_ID );
  111.       $this->Bind_ID = 0;
  112.     }
  113.   }
  114.  
  115.   /* public: discard the query result */
  116.   function free() {
  117.       @ldap_free_result($this->Search_ID);
  118.       $this->Search_ID = 0;
  119.       $this->NumEntry = 0;
  120.       $this->NumValues = 0;
  121.   }
  122.  
  123.   /* public: perform a query */
  124.   function search($Search_String,$Base_DN="") {
  125.     if ( $Base_DN == "" )
  126.       $Base_DN = $this->BaseDN;
  127.  
  128.     /* No empty queries, please, since PHP4 chokes on them. */
  129.     if ($Search_String == "")
  130.       return 0;
  131.  
  132.     if (!$this->connect()) {
  133.       return 0;
  134.     };
  135.  
  136.     # New query, discard previous result.
  137.     if ($this->Search_ID) {
  138.       $this->free();
  139.     }
  140.  
  141.     if ($this->Debug)
  142.       printf("Debug: search = %s<br>\n", $Search_String);
  143.  
  144.     $this->Search_ID = @ldap_search( $this->Link_ID, $Base_DN, $Search_String );
  145.     $this->NumEntry = 0;
  146.     $this->Errno = ldap_errno( $this->Link_ID );
  147.     $this->Error = ldap_error( $this->Link_ID );
  148.     if (!$this->Search_ID) {
  149.       $this->halt("Invalid LDAP Filter: ".$Search_String);
  150.     }
  151.  
  152.     # Will return nada if it fails. That's fine.
  153.     return $this->Search_ID;
  154.   }
  155.  
  156.   /* public: modifying entries */
  157.   function modify( $ToModify, $DN, $Host="", $User="", $Password="") {
  158.  
  159.     if ( $Host == "" ) {
  160.       $Host = $this->Host;
  161.       $newconnect = false;
  162.     }
  163.     elseif ( $Host != $this->Host ) {
  164.       $this->disconnect();
  165.       $this->connect($Host);
  166.       $newconnect = true;
  167.     }
  168.     if ( $User == "" || $Password == "" ) {
  169.       $User = $this->User;
  170.       $Password = $this->Password;
  171.       $this->bind();
  172.       $newbind = false;
  173.     }
  174.     elseif ( $User != $this->User ) {
  175.       $this->bind($User, $Password);
  176.       $newbind = true;
  177.     }
  178.  
  179.     if ( $ToModify == "" )
  180.       return false;
  181.  
  182.     if ( $DN == "" )
  183.       return false;
  184.  
  185.     if ( $this->Link_ID ) {
  186.       $this->Modify_ID = ldap_modify( $this->connect($Host), $DN, $ToModify );
  187.       $not_modified = false;
  188.       if ( !$this->Modify_ID )
  189.           $not_modified = true;
  190.     }
  191.     if ( $newconnect ) {
  192.       $this->unbind();
  193.       $this->disconnect();
  194.       $newbind = false;
  195.     }
  196.     if ( $newbind ) {
  197.       $this->unbind();
  198.     }
  199.     if ( $not_modified )
  200.       return false;
  201.     return true;
  202.   }
  203.  
  204.   /* public: create entries */
  205.   function create( $ToCreate, $DN, $Host="", $User="", $Password="") {
  206.  
  207.     if ( $Host == "" ) {
  208.       $Host = $this->Host;
  209.       $newconnect = false;
  210.     }
  211.     elseif ( $Host != $this->Host ) {
  212.       $this->disconnect();
  213.       $this->connect($Host);
  214.       $newconnect = true;
  215.     }
  216.     if ( $User == "" || $Password == "" ) {
  217.       $User = $this->User;
  218.       $Password = $this->Password;
  219.       $this->bind();
  220.       $newbind = false;
  221.     }
  222.     elseif ( $User != $this->User ) {
  223.       $this->bind($User, $Password);
  224.       $newbind = true;
  225.     }
  226.  
  227.     if ( $ToCreate == "" )
  228.       return false;
  229.  
  230.     if ( $DN == "" )
  231.       return false;
  232.  
  233.     if ( $this->Link_ID ) {
  234.       $this->Create_ID = ldap_add( $this->connect($Host), $DN, $ToCreate );
  235.       $not_created = false;
  236.       if ( !$this->Create_ID )
  237.           $not_created = true;
  238.     }
  239.     if ( $newconnect ) {
  240.       $this->unbind();
  241.       $this->disconnect();
  242.       $newbind = false;
  243.     }
  244.     if ( $newbind ) {
  245.       $this->unbind();
  246.     }
  247.     if ( $not_created )
  248.       return false;
  249.     return true;
  250.   }
  251.  
  252.   /* public: delete entries */
  253.   function del( $DN, $Host="", $User="", $Password="") {
  254.  
  255.     if ( $Host == "" ) {
  256.       $Host = $this->Host;
  257.       $newconnect = false;
  258.     }
  259.     elseif ( $Host != $this->Host ) {
  260.       $this->disconnect();
  261.       $this->connect($Host);
  262.       $newconnect = true;
  263.     }
  264.     if ( $User == "" || $Password == "" ) {
  265.       $User = $this->User;
  266.       $Password = $this->Password;
  267.       $this->bind();
  268.       $newbind = false;
  269.     }
  270.     elseif ( $User != $this->User ) {
  271.       $this->bind($User, $Password);
  272.       $newbind = true;
  273.     }
  274.  
  275.     if ( $DN == "" )
  276.       return false;
  277.  
  278.     if ( $this->Link_ID ) {
  279.       $this->Delete_ID = ldap_delete( $this->connect($Host), $DN );
  280.       $not_deleted = false;
  281.       if ( !$this->Delete_ID )
  282.           $not_deleted = true;
  283.     }
  284.     if ( $newconnect ) {
  285.       $this->unbind();
  286.       $this->disconnect();
  287.       $newbind = false;
  288.     }
  289.     if ( $newbind ) {
  290.       $this->unbind();
  291.     }
  292.     if ( $not_deleted )
  293.       return false;
  294.     return true;
  295.   }
  296.  
  297.   function sort($Filter) {
  298.     if ( $this->Search_ID ) {
  299.       ldap_sort( $this->Link_ID, $this->Search_ID, $Filter );
  300.     }
  301.   }
  302.  
  303.   /* public: walk result set */
  304.   function next_entry() {
  305.     if ( $this->NumEntry == 0 ) {
  306.       $this->Entry = @ldap_first_entry( $this->Link_ID, $this->Search_ID );
  307.     } else {
  308.       $this->Entry = @ldap_next_entry( $this->Link_ID, $this->Entry );
  309.     }
  310.     $this->NumEntry++;
  311.  
  312.     $this->Errno  = ldap_errno( $this->Link_ID );
  313.     $this->Error  = ldap_error( $this->Link_ID );
  314.  
  315.     return $this->Entry;
  316.   }
  317.  
  318.   function get_entries() {
  319.     return @ldap_get_entries( $this->Link_ID, $this->Search_ID );
  320.   }
  321.  
  322.   function num_entries() {
  323.     return @ldap_count_entries( $this->Link_ID, $this->Search_ID );
  324.   }
  325.  
  326.   function dn() {
  327.     if ( $this->Entry )
  328.     {
  329.       return @ldap_get_dn( $this->Link_ID, $this->Entry );
  330.     }
  331.   }
  332.  
  333.   function f($Name) {
  334.     return @ldap_get_values( $this->Link_ID, $this->Entry, $Name );
  335.   }
  336.  
  337.   function get_values( $attr )
  338.   {
  339.     if ( $this->Entry )
  340.     {
  341.       return @ldap_get_values( $this->Link_ID, $this->Entry, $attr );
  342.     }
  343.   }
  344.  
  345.   function first_value( $attr )
  346.   {
  347.     if ( $this->Entry )
  348.     {
  349.       $NumValues = 0;
  350.       $Values = @ldap_get_values( $this->Link_ID, $this->Entry, $attr );
  351.       return $Values[$NumValues];
  352.     } else {
  353.       return false;
  354.     }
  355.   }
  356.  
  357.   function next_value()
  358.   {
  359.     $NumValues++;
  360.     if ($NumValues<count($Values))
  361.     {
  362.       return $Values[$NumValues];
  363.     }else
  364.     {
  365.       return false;
  366.     }
  367.   }
  368.  
  369.   function in_values( $attr, $val )
  370.   {
  371.     $found = false;
  372.     $value = $this->first_value( $attr );
  373.     do {
  374.       if ($value == $val)
  375.       {
  376.         $found = true;
  377.       }
  378.     } while ( (!$found) && ($value = $this->next_value()) );
  379.     return $found;
  380.   }
  381.  
  382.   /* private: error handling */
  383.   function halt($msg) {
  384.     $this->Error = @ldap_error($this->Link_ID);
  385.     $this->Errno = @ldap_errno($this->Link_ID);
  386.     if ($this->Halt_On_Error == "no")
  387.       return;
  388.  
  389.     $this->haltmsg($msg);
  390.  
  391.     if ($this->Halt_On_Error != "report")
  392.       die("Session halted.");
  393.   }
  394.  
  395.   function haltmsg($msg) {
  396.     printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
  397.     printf("<b>LDAP Error</b>: %s (%s)<br>\n",
  398.       $this->Errno,
  399.       $this->Error);
  400.   }
  401. }
  402. ?>
  403.