1 <?php
4 class Device extends plugin
5 {
7 public $objectclasses = array('top','device');
8 public $attributes = array("cn","serialNumber","seeAlso","owner","ou","o","ipHostNumber",
9 "l","description","manager","deviceUUID","deviceStatus","macAddress","deviceType");
11 public $dynClasses = array(
12 'ieee802Device' => array('macAddress'),
13 'registeredDevice' => array('deviceStatus','deviceUUID','manager','deviceType'),
14 'ipHost' => array('ipHostNumber'));
16 public $cn = "";
17 public $serialNumber = "";
18 public $seeAlso = "";
19 public $owner = "";
20 public $ou = "";
21 public $o = "";
22 public $l = "";
23 public $description = "";
24 public $manager = "";
25 public $deviceUUID = "";
26 public $deviceStatus = "";
27 public $deviceType = "";
28 public $ipHostNumber = "";
29 public $macAddress = "";
31 public $base = "";
32 public $orig_dn ="";
34 private $resolvedNamesCache = array();
37 /*! \brief Constructs the device object and keep some
38 * initial values.
39 */
40 function __construct(&$config, $dn)
41 {
42 plugin::plugin($config, $dn);
43 $this->base = $this->config->current['BASE'];
44 $this->orig_dn = $this->dn;
45 }
48 /*! \brief Generate a fake uuid, it is not a real uuid.
49 * @return String A fake uuid.
50 */
51 function genFakeUuid()
52 {
53 $strfmt = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
54 for($i=0;$i<strlen($strfmt);$i++)
55 if($strfmt[$i] == 'x') $strfmt[$i] = dechex(rand(0,15));
56 return($strfmt);
57 }
60 /*! \brief Generate the plugins HTML content.
61 * @return String HTML content
62 */
63 function execute()
64 {
65 plugin::execute();
67 if($this->dialog instanceOf singleUserSelect) return($this->dialog->execute());
69 $smarty = get_smarty();
71 // Assign ACL settings
72 $plInfo = $this->plInfo();
73 foreach($plInfo['plProvidedAcls'] as $name => $desc){
74 $smarty->assign("{$name}ACL", $this->getacl($name));
75 }
77 // Assign just user names instead of the complete dn.
78 $smarty->assign("owner_name", $this->getUserName($this->owner));
79 $smarty->assign("manager_name", $this->getUserName($this->manager));
81 foreach($this->attributes as $attr){
82 $smarty->assign($attr, $this->$attr);
83 }
84 return($smarty->fetch(get_template_path('goto/Device/Device.tpl', TRUE)));
85 }
88 /*! \brief Detect an object's name by querying the ldap
89 * for the object's cn.
90 * @param String The object's dn to query for.
91 */
92 function getUserName($dn)
93 {
94 // First asked the cache if we've already queried this name.
95 if(isset($this->resolvedNamesCache[$dn])) {
96 return($this->resolvedNamesCache[$dn]);
97 }
99 // Try to detect the object's name via ldap search.
100 $ldap = $this->config->get_ldap_link();
101 $this->resolvedNamesCache[$dn] = "";
102 if(!empty($dn)){
103 $ldap->cat($dn, array('cn'));
104 if($ldap->count()){
105 $attrs = $ldap->fetch();
106 $this->resolvedNamesCache[$dn] = $attrs['cn'][0];
107 return( $attrs['cn'][0]);
108 }else{
109 $this->resolvedNamesCache[$dn] = "("._("unknown")."!): ".$dn;
110 }
111 }
112 return($this->resolvedNamesCache[$dn]);
113 }
116 /*! \brief Detects and stores relevant values which where
117 * currently transmitted via $_GET/$_POST.
118 */
119 function save_object()
120 {
121 plugin::save_object();
123 // Change owner requested, initiate a user selection dialog.
124 if(isset($_POST['editOwner'])){
125 $this->currentUserSelect = "owner";
126 $this->dialog = new singleUserSelect($this->config, get_userinfo());
127 }
129 // Change manager requested, initiate a user selection dialog.
130 if(isset($_POST['editManager'])){
131 $this->currentUserSelect = "manager";
132 $this->dialog = new singleUserSelect($this->config, get_userinfo());
133 }
135 // The user selection dialog has send that it has finished its job.
136 // Store the posted user-dn as manager or owner.
137 if($this->dialog && $this->dialog instanceOf singleUserSelect && count($this->dialog->detectPostActions())){
138 $users = $this->dialog->detectPostActions();
139 if(isset($users['action']) && $users['action'] == 'userSelected' && isset($users['targets']) && count($users['targets'])){
140 $headpage = $this->dialog->getHeadpage();
141 $dn = $users['targets'][0];
142 $attr = $this->currentUserSelect;
143 $this->$attr = $dn;
144 $this->dialog = NULL;
145 }
146 }
148 // User selection canceled
149 if(isset($_POST['add_users_cancel'])){
150 $this->dialog = NULL;
151 }
153 // Regenerate a new uuid
154 if(isset($_POST['reloadUUID'])) $this->deviceUUID = $this->genFakeUuid();
155 }
158 /*! \brief Save the modified object back to the ldap.
159 */
160 function save()
161 {
162 plugin::save();
164 // Append and remove dynmic object classes
165 foreach($this->dynClasses as $oc => $attrs){
166 $this->attrs['objectClass'] = array_remove_entries(array($oc), $this->attrs['objectClass']);
167 foreach($attrs as $attr){
168 if(isset($this->attrs[$attr]) && !empty($this->attrs[$attr])){
169 $this->attrs['objectClass'][] = $oc;
170 break;
171 }
172 }
173 }
175 $this->cleanup();
176 $ldap=$this->config->get_ldap_link();
177 $ldap->cd($this->config->current['BASE']);
178 $ldap->cd($this->dn);
180 if($this->initially_was_account){
181 $ldap->modify($this->attrs);
182 }else{
183 $ldap->add($this->attrs);
184 }
185 echo $ldap->get_error();
186 }
189 static function plInfo()
190 {
191 return (array(
192 "plShortName" => _("Device"),
193 "plDescription" => _("Device"),
194 "plSelfModify" => FALSE,
195 "plDepends" => array(),
196 "plPriority" => 1,
197 "plSection" => array("administration"),
198 "plCategory" => array(
199 "Device" => array(
200 "description" => _("Device"),
201 "objectClass" => "Device")),
202 "plProvidedAcls" => array(
203 "cn" => _("Name"),
204 "serialNumber" => _("Serial number"),
205 "seeAlso" => _("See also"),
206 "owner" => _("Owner"),
207 "ou" => _("Organizational unit"),
208 "o" => _("Organization"),
209 "l" => _("Location"),
210 "description" => _("Description"),
211 "manager" => _("Manager"),
212 "deviceUUID" => _("Uuid"),
213 "deviceStatus" => _("Stauts"),
214 "macAddress" => _("MAC address"),
215 "ipHostNumber" => _("IP address")
216 )
217 )
218 );
219 }
220 }
222 ?>