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