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->orig_dn = $this->dn;
45 // Initialize the object base
46 if ($this->dn == "new"){
47 $ui= get_userinfo();
48 $this->base= dn2base(session::global_is_set("CurrentMainBase")?"cn=dummy,".session::global_get("CurrentMainBase"):$ui->dn);
49 } else {
50 $this->base= preg_replace ("/^[^,]+,".preg_quote(get_ou("Device", "DeviceRDN"), '/')."/i", "", $this->dn);
51 }
53 // Prepare the base selector
54 $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
55 $this->baseSelector->setSubmitButton(false);
56 $this->baseSelector->setHeight(300);
57 $this->baseSelector->update(true);
59 $this->orig_deviceUUID = $this->deviceUUID;
60 }
63 /*! \brief Generate a fake uuid, it is not a real uuid.
64 * @return String A fake uuid.
65 */
66 function genFakeUuid()
67 {
68 $strfmt = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
69 for($i=0;$i<strlen($strfmt);$i++)
70 if($strfmt[$i] == 'x') $strfmt[$i] = dechex(rand(0,15));
71 return($strfmt);
72 }
75 /*! \brief Generate the plugins HTML content.
76 * @return String HTML content
77 */
78 function execute()
79 {
80 plugin::execute();
82 if($this->dialog instanceOf singleUserSelect) return($this->dialog->execute());
84 $smarty = get_smarty();
86 // Assign ACL settings
87 $plInfo = $this->plInfo();
88 foreach($plInfo['plProvidedAcls'] as $name => $desc){
89 $smarty->assign("{$name}ACL", $this->getacl($name));
90 }
92 // Assign just user names instead of the complete dn.
93 $smarty->assign("owner_name", $this->getUserName($this->owner));
94 $smarty->assign("manager_name", $this->getUserName($this->manager));
95 $smarty->assign("base", $this->baseSelector->render());
97 // Assign attribute values
98 foreach($this->attributes as $attr){
99 $smarty->assign($attr, set_post($this->$attr));
100 }
101 return($smarty->fetch(get_template_path('goto/Device/Device.tpl', TRUE)));
102 }
105 /*! \brief Validate the user input.
106 */
107 function check()
108 {
109 $message = plugin::check();
111 // If one of the registered user attributes is set,
112 // the others have to be set too.
113 $str = $this->manager.$this->deviceUUID.$this->deviceStatus;
114 if(!empty($str)){
115 foreach($this->dynClasses['registeredDevice'] as $attr){
116 if(empty($this->$attr)){
117 $message[] = "Please set all values for 'registered device' or leave all blank! {$attr}";
118 break;
119 }
120 }
121 }
123 // Check ip-address
124 if (!empty($this->ipHostNumber) && !tests::is_ip($this->ipHostNumber)){
125 $message[]= msgPool::invalid(_("IP address"), "", "", "192.168.1.10");
126 }
128 // Check if there a cn given
129 if (empty($this->cn)){
130 $message[]= msgPool::required(_("Name"));
131 }
133 // Check mac-address
134 if (!empty($this->macAddress) && !tests::is_mac($this->macAddress)){
135 $message[]= msgPool::invalid(_("MAC address"), "", "", "00:0C:7F:31:33:F1");
136 }
138 return($message);
139 }
142 /*! \brief Detect an object's name by querying the ldap
143 * for the object's cn.
144 * @param String The object's dn to query for.
145 */
146 function getUserName($dn)
147 {
148 // First asked the cache if we've already queried this name.
149 if(isset($this->resolvedNamesCache[$dn])) {
150 return($this->resolvedNamesCache[$dn]);
151 }
153 // Try to detect the object's name via ldap search.
154 $ldap = $this->config->get_ldap_link();
155 $this->resolvedNamesCache[$dn] = "";
156 if(!empty($dn)){
157 $ldap->cat($dn, array('cn'));
158 if($ldap->count()){
159 $attrs = $ldap->fetch();
160 $this->resolvedNamesCache[$dn] = $attrs['cn'][0];
161 return( $attrs['cn'][0]);
162 }else{
163 $this->resolvedNamesCache[$dn] = "("._("unknown")."!): ".$dn;
164 }
165 }
166 return($this->resolvedNamesCache[$dn]);
167 }
170 /*! \brief Detects and stores relevant values which where
171 * currently transmitted via $_GET/$_POST.
172 */
173 function save_object()
174 {
175 plugin::save_object();
177 // Change owner requested, initiate a user selection dialog.
178 if(isset($_POST['editOwner'])){
179 $this->currentUserSelect = "owner";
180 $this->dialog = new singleUserSelect($this->config, get_userinfo());
181 }
183 // Change manager requested, initiate a user selection dialog.
184 if(isset($_POST['editManager'])){
185 $this->currentUserSelect = "manager";
186 $this->dialog = new singleUserSelect($this->config, get_userinfo());
187 }
189 // Remove owner initiated
190 if(isset($_POST['removeManager'])) $this->manager = "";
191 if(isset($_POST['removeOwner'])) $this->owner = "";
193 // The user selection dialog has send that it has finished its job.
194 // Store the posted user-dn as manager or owner.
195 if($this->dialog && $this->dialog instanceOf singleUserSelect && count($this->dialog->detectPostActions())){
196 $users = $this->dialog->detectPostActions();
197 if(isset($users['action']) && $users['action'] == 'userSelected' && isset($users['targets']) && count($users['targets'])){
198 $headpage = $this->dialog->getHeadpage();
199 $dn = $users['targets'][0];
200 $attr = $this->currentUserSelect;
201 $this->$attr = $dn;
202 $this->dialog = NULL;
203 }
204 }
206 // User selection canceled
207 if(isset($_POST['add_users_cancel'])){
208 $this->dialog = NULL;
209 }
211 // Regenerate a new uuid
212 if(isset($_POST['reloadUUID'])) $this->deviceUUID = $this->genFakeUuid();
214 // Update the base
215 if ($this->acl_is_moveable($this->base)){
216 if (!$this->baseSelector->update()) {
217 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
218 }
219 if ($this->base != $this->baseSelector->getBase()) {
220 $this->base= $this->baseSelector->getBase();
221 $this->is_modified= TRUE;
222 }
223 }
225 }
228 /*! \brief Save the modified object back to the ldap.
229 */
230 function save()
231 {
232 plugin::save();
234 // Append and remove dynmic object classes
235 foreach($this->dynClasses as $oc => $attrs){
236 $this->attrs['objectClass'] = array_remove_entries(array($oc), $this->attrs['objectClass']);
237 foreach($attrs as $attr){
238 if(isset($this->attrs[$attr]) && !empty($this->attrs[$attr])){
239 $this->attrs['objectClass'][] = $oc;
240 break;
241 }
242 }
243 }
245 $this->cleanup();
246 $ldap=$this->config->get_ldap_link();
247 $ldap->cd($this->config->current['BASE']);
248 $ldap->create_missing_trees(preg_replace("/^[^,]*+,/","",$this->dn));
249 $ldap->cd($this->dn);
251 // Perform action modify/create
252 if($this->initially_was_account){
253 $ldap->modify($this->attrs);
254 new log("modify","Device/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
255 if (!$ldap->success()){
256 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
257 }else{
258 $this->handle_post_events("modify");
259 }
260 }else{
261 $ldap->add($this->attrs);
262 new log("create","Device/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
263 if (!$ldap->success()){
264 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_ADD, get_class()));
265 }else{
266 $this->handle_post_events("add");
267 }
268 }
269 }
272 static function plInfo()
273 {
274 return (array(
275 "plShortName" => _("Device"),
276 "plDescription" => _("Device"),
277 "plSelfModify" => FALSE,
278 "plDepends" => array(),
279 "plPriority" => 1,
280 "plSection" => array("administration"),
281 "plCategory" =>
282 array(
283 "Device" => array(
284 "description" => _("Device"),
285 "objectClass" => "Device")
286 ),
287 "plProvidedAcls" =>
288 array(
289 "base" => _("Base"),
290 "cn" => _("Name"),
291 "serialNumber" => _("Serial number"),
292 "seeAlso" => _("See also"),
293 "owner" => _("Owner"),
294 "ou" => _("Organizational unit"),
295 "o" => _("Organization"),
296 "l" => _("Location"),
297 "description" => _("Description"),
298 "manager" => _("Manager"),
299 "deviceUUID" => _("Uuid"),
300 "deviceStatus" => _("Status"),
301 "deviceType" => _("Type"),
302 "macAddress" => _("MAC address"),
303 "ipHostNumber" => _("IP address")
304 ),
305 "plProperties" =>
306 array(
307 array(
308 "name" => "DeviceRDN",
309 "type" => "rdn",
310 "default" => "ou=devices,ou=systems,",
311 "description" => _("The 'deviceRDN' statement defines the location where new devices will be created. The default is 'ou=device,ou=systems,'."),
312 "check" => "gosaProperty::isRdn",
313 "migrate" => "migrate_deviceRDN",
314 "group" => "plugin",
315 "mandatory" => FALSE
316 ),
318 )
319 )
320 );
321 }
322 }
324 ?>