xref: /dokuwiki/lib/plugins/authad/adLDAP/classes/adLDAPComputers.php (revision ccfec5606dae81e6524f6cfcdaca09d4cde7f111)
1*76ce1169SAndreas Gohr<?php
2*76ce1169SAndreas Gohr/**
3*76ce1169SAndreas Gohr * PHP LDAP CLASS FOR MANIPULATING ACTIVE DIRECTORY
4*76ce1169SAndreas Gohr * Version 4.0.4
5*76ce1169SAndreas Gohr *
6*76ce1169SAndreas Gohr * PHP Version 5 with SSL and LDAP support
7*76ce1169SAndreas Gohr *
8*76ce1169SAndreas Gohr * Written by Scott Barnett, Richard Hyland
9*76ce1169SAndreas Gohr *   email: scott@wiggumworld.com, adldap@richardhyland.com
10*76ce1169SAndreas Gohr *   http://adldap.sourceforge.net/
11*76ce1169SAndreas Gohr *
12*76ce1169SAndreas Gohr * Copyright (c) 2006-2012 Scott Barnett, Richard Hyland
13*76ce1169SAndreas Gohr *
14*76ce1169SAndreas Gohr * We'd appreciate any improvements or additions to be submitted back
15*76ce1169SAndreas Gohr * to benefit the entire community :)
16*76ce1169SAndreas Gohr *
17*76ce1169SAndreas Gohr * This library is free software; you can redistribute it and/or
18*76ce1169SAndreas Gohr * modify it under the terms of the GNU Lesser General Public
19*76ce1169SAndreas Gohr * License as published by the Free Software Foundation; either
20*76ce1169SAndreas Gohr * version 2.1 of the License.
21*76ce1169SAndreas Gohr *
22*76ce1169SAndreas Gohr * This library is distributed in the hope that it will be useful,
23*76ce1169SAndreas Gohr * but WITHOUT ANY WARRANTY; without even the implied warranty of
24*76ce1169SAndreas Gohr * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25*76ce1169SAndreas Gohr * Lesser General Public License for more details.
26*76ce1169SAndreas Gohr *
27*76ce1169SAndreas Gohr * @category ToolsAndUtilities
28*76ce1169SAndreas Gohr * @package adLDAP
29*76ce1169SAndreas Gohr * @subpackage Computers
30*76ce1169SAndreas Gohr * @author Scott Barnett, Richard Hyland
31*76ce1169SAndreas Gohr * @copyright (c) 2006-2012 Scott Barnett, Richard Hyland
32*76ce1169SAndreas Gohr * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html LGPLv2.1
33*76ce1169SAndreas Gohr * @revision $Revision: 97 $
34*76ce1169SAndreas Gohr * @version 4.0.4
35*76ce1169SAndreas Gohr * @link http://adldap.sourceforge.net/
36*76ce1169SAndreas Gohr */
37*76ce1169SAndreas Gohrrequire_once(dirname(__FILE__) . '/../adLDAP.php');
38*76ce1169SAndreas Gohrrequire_once(dirname(__FILE__) . '/../collections/adLDAPComputerCollection.php');
39*76ce1169SAndreas Gohr
40*76ce1169SAndreas Gohr/**
41*76ce1169SAndreas Gohr* COMPUTER MANAGEMENT FUNCTIONS
42*76ce1169SAndreas Gohr*/
43*76ce1169SAndreas Gohrclass adLDAPComputers {
44*76ce1169SAndreas Gohr
45*76ce1169SAndreas Gohr    /**
46*76ce1169SAndreas Gohr    * The current adLDAP connection via dependency injection
47*76ce1169SAndreas Gohr    *
48*76ce1169SAndreas Gohr    * @var adLDAP
49*76ce1169SAndreas Gohr    */
50*76ce1169SAndreas Gohr    protected $adldap;
51*76ce1169SAndreas Gohr
52*76ce1169SAndreas Gohr    public function __construct(adLDAP $adldap) {
53*76ce1169SAndreas Gohr        $this->adldap = $adldap;
54*76ce1169SAndreas Gohr    }
55*76ce1169SAndreas Gohr
56*76ce1169SAndreas Gohr    /**
57*76ce1169SAndreas Gohr    * Get information about a specific computer. Returned in a raw array format from AD
58*76ce1169SAndreas Gohr    *
59*76ce1169SAndreas Gohr    * @param string $computerName The name of the computer
60*76ce1169SAndreas Gohr    * @param array $fields Attributes to return
61*76ce1169SAndreas Gohr    * @return array
62*76ce1169SAndreas Gohr    */
63*76ce1169SAndreas Gohr    public function info($computerName, $fields = NULL)
64*76ce1169SAndreas Gohr    {
65*76ce1169SAndreas Gohr        if ($computerName === NULL) { return false; }
66*76ce1169SAndreas Gohr        if (!$this->adldap->getLdapBind()) { return false; }
67*76ce1169SAndreas Gohr
68*76ce1169SAndreas Gohr        $filter = "(&(objectClass=computer)(cn=" . $computerName . "))";
69*76ce1169SAndreas Gohr        if ($fields === NULL) {
70*76ce1169SAndreas Gohr            $fields = array("memberof","cn","displayname","dnshostname","distinguishedname","objectcategory","operatingsystem","operatingsystemservicepack","operatingsystemversion");
71*76ce1169SAndreas Gohr        }
72*76ce1169SAndreas Gohr        $sr = ldap_search($this->adldap->getLdapConnection(), $this->adldap->getBaseDn(), $filter, $fields);
73*76ce1169SAndreas Gohr        $entries = ldap_get_entries($this->adldap->getLdapConnection(), $sr);
74*76ce1169SAndreas Gohr
75*76ce1169SAndreas Gohr        return $entries;
76*76ce1169SAndreas Gohr    }
77*76ce1169SAndreas Gohr
78*76ce1169SAndreas Gohr    /**
79*76ce1169SAndreas Gohr    * Find information about the computers. Returned in a raw array format from AD
80*76ce1169SAndreas Gohr    *
81*76ce1169SAndreas Gohr    * @param string $computerName The name of the computer
82*76ce1169SAndreas Gohr    * @param array $fields Array of parameters to query
83*76ce1169SAndreas Gohr    * @return mixed
84*76ce1169SAndreas Gohr    */
85*76ce1169SAndreas Gohr    public function infoCollection($computerName, $fields = NULL)
86*76ce1169SAndreas Gohr    {
87*76ce1169SAndreas Gohr        if ($computerName === NULL) { return false; }
88*76ce1169SAndreas Gohr        if (!$this->adldap->getLdapBind()) { return false; }
89*76ce1169SAndreas Gohr
90*76ce1169SAndreas Gohr        $info = $this->info($computerName, $fields);
91*76ce1169SAndreas Gohr
92*76ce1169SAndreas Gohr        if ($info !== false) {
93*76ce1169SAndreas Gohr            $collection = new adLDAPComputerCollection($info, $this->adldap);
94*76ce1169SAndreas Gohr            return $collection;
95*76ce1169SAndreas Gohr        }
96*76ce1169SAndreas Gohr        return false;
97*76ce1169SAndreas Gohr    }
98*76ce1169SAndreas Gohr
99*76ce1169SAndreas Gohr    /**
100*76ce1169SAndreas Gohr    * Check if a computer is in a group
101*76ce1169SAndreas Gohr    *
102*76ce1169SAndreas Gohr    * @param string $computerName The name of the computer
103*76ce1169SAndreas Gohr    * @param string $group The group to check
104*76ce1169SAndreas Gohr    * @param bool $recursive Whether to check recursively
105*76ce1169SAndreas Gohr    * @return array
106*76ce1169SAndreas Gohr    */
107*76ce1169SAndreas Gohr    public function inGroup($computerName, $group, $recursive = NULL)
108*76ce1169SAndreas Gohr    {
109*76ce1169SAndreas Gohr        if ($computerName === NULL) { return false; }
110*76ce1169SAndreas Gohr        if ($group === NULL) { return false; }
111*76ce1169SAndreas Gohr        if (!$this->adldap->getLdapBind()) { return false; }
112*76ce1169SAndreas Gohr        if ($recursive === NULL) { $recursive = $this->adldap->getRecursiveGroups(); } // use the default option if they haven't set it
113*76ce1169SAndreas Gohr
114*76ce1169SAndreas Gohr        //get a list of the groups
115*76ce1169SAndreas Gohr        $groups = $this->groups($computerName, array("memberof"), $recursive);
116*76ce1169SAndreas Gohr
117*76ce1169SAndreas Gohr        //return true if the specified group is in the group list
118*76ce1169SAndreas Gohr        if (in_array($group, $groups)){
119*76ce1169SAndreas Gohr            return true;
120*76ce1169SAndreas Gohr        }
121*76ce1169SAndreas Gohr
122*76ce1169SAndreas Gohr        return false;
123*76ce1169SAndreas Gohr    }
124*76ce1169SAndreas Gohr
125*76ce1169SAndreas Gohr    /**
126*76ce1169SAndreas Gohr    * Get the groups a computer is in
127*76ce1169SAndreas Gohr    *
128*76ce1169SAndreas Gohr    * @param string $computerName The name of the computer
129*76ce1169SAndreas Gohr    * @param bool $recursive Whether to check recursively
130*76ce1169SAndreas Gohr    * @return array
131*76ce1169SAndreas Gohr    */
132*76ce1169SAndreas Gohr    public function groups($computerName, $recursive = NULL)
133*76ce1169SAndreas Gohr    {
134*76ce1169SAndreas Gohr        if ($computerName === NULL) { return false; }
135*76ce1169SAndreas Gohr        if ($recursive === NULL) { $recursive = $this->adldap->getRecursiveGroups(); } //use the default option if they haven't set it
136*76ce1169SAndreas Gohr        if (!$this->adldap->getLdapBind()){ return false; }
137*76ce1169SAndreas Gohr
138*76ce1169SAndreas Gohr        //search the directory for their information
139*76ce1169SAndreas Gohr        $info = @$this->info($computerName, array("memberof", "primarygroupid"));
140*76ce1169SAndreas Gohr        $groups = $this->adldap->utilities()->niceNames($info[0]["memberof"]); //presuming the entry returned is our guy (unique usernames)
141*76ce1169SAndreas Gohr
142*76ce1169SAndreas Gohr        if ($recursive === true) {
143*76ce1169SAndreas Gohr            foreach ($groups as $id => $groupName){
144*76ce1169SAndreas Gohr              $extraGroups = $this->adldap->group()->recursiveGroups($groupName);
145*76ce1169SAndreas Gohr              $groups = array_merge($groups, $extraGroups);
146*76ce1169SAndreas Gohr            }
147*76ce1169SAndreas Gohr        }
148*76ce1169SAndreas Gohr
149*76ce1169SAndreas Gohr        return $groups;
150*76ce1169SAndreas Gohr    }
151*76ce1169SAndreas Gohr
152*76ce1169SAndreas Gohr}
153*76ce1169SAndreas Gohr?>