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