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", "DeviceRDN"), '/')."/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 }
103 return($smarty->fetch(get_template_path('goto/Device/Device.tpl', TRUE)));
104 }
107 /*! \brief Validate the user input.
108 */
109 function check()
110 {
111 $message = plugin::check();
113 // If one of the registered user attributes is set,
114 // the others have to be set too.
115 $str = $this->manager.$this->deviceUUID.$this->deviceStatus;
116 if(!empty($str)){
117 foreach($this->dynClasses['registeredDevice'] as $attr){
118 if(empty($this->$attr)){
119 $message[] = "Please set all values for 'registered device' or leave all blank! {$attr}";
120 break;
121 }
122 }
123 }
125 // Check ip-address
126 if (!empty($this->ipHostNumber) && !tests::is_ip($this->ipHostNumber)){
127 $message[]= msgPool::invalid(_("IP address"), "", "", "192.168.1.10");
128 }
130 // Check if there a cn given
131 if (empty($this->cn)){
132 $message[]= msgPool::required(_("Name"));
133 }
135 // Check mac-address
136 if (!empty($this->macAddress) && !tests::is_mac($this->macAddress)){
137 $message[]= msgPool::invalid(_("MAC address"), "", "", "00:0C:7F:31:33:F1");
138 }
140 return($message);
141 }
144 /*! \brief Detect an object's name by querying the ldap
145 * for the object's cn.
146 * @param String The object's dn to query for.
147 */
148 function getUserName($dn)
149 {
150 // First asked the cache if we've already queried this name.
151 if(isset($this->resolvedNamesCache[$dn])) {
152 return($this->resolvedNamesCache[$dn]);
153 }
155 // Try to detect the object's name via ldap search.
156 $ldap = $this->config->get_ldap_link();
157 $this->resolvedNamesCache[$dn] = "";
158 if(!empty($dn)){
159 $ldap->cat($dn, array('cn'));
160 if($ldap->count()){
161 $attrs = $ldap->fetch();
162 $this->resolvedNamesCache[$dn] = $attrs['cn'][0];
163 return( $attrs['cn'][0]);
164 }else{
165 $this->resolvedNamesCache[$dn] = "("._("unknown")."!): ".$dn;
166 }
167 }
168 return($this->resolvedNamesCache[$dn]);
169 }
172 /*! \brief Detects and stores relevant values which where
173 * currently transmitted via $_GET/$_POST.
174 */
175 function save_object()
176 {
177 plugin::save_object();
179 // Change owner requested, initiate a user selection dialog.
180 if(isset($_POST['editOwner'])){
181 $this->currentUserSelect = "owner";
182 $this->dialog = new singleUserSelect($this->config, get_userinfo());
183 }
185 // Change manager requested, initiate a user selection dialog.
186 if(isset($_POST['editManager'])){
187 $this->currentUserSelect = "manager";
188 $this->dialog = new singleUserSelect($this->config, get_userinfo());
189 }
191 // Remove owner initiated
192 if(isset($_POST['removeManager'])) $this->manager = "";
193 if(isset($_POST['removeOwner'])) $this->owner = "";
195 // The user selection dialog has send that it has finished its job.
196 // Store the posted user-dn as manager or owner.
197 if($this->dialog && $this->dialog instanceOf singleUserSelect && count($this->dialog->detectPostActions())){
198 $users = $this->dialog->detectPostActions();
199 if(isset($users['action']) && $users['action'] == 'userSelected' && isset($users['targets']) && count($users['targets'])){
200 $headpage = $this->dialog->getHeadpage();
201 $dn = $users['targets'][0];
202 $attr = $this->currentUserSelect;
203 $this->$attr = $dn;
204 $this->dialog = NULL;
205 }
206 }
208 // User selection canceled
209 if(isset($_POST['add_users_cancel'])){
210 $this->dialog = NULL;
211 }
213 // Regenerate a new uuid
214 if(isset($_POST['reloadUUID'])) $this->deviceUUID = $this->genFakeUuid();
216 // Update the base
217 if ($this->acl_is_moveable($this->base)){
218 if (!$this->baseSelector->update()) {
219 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
220 }
221 if ($this->base != $this->baseSelector->getBase()) {
222 $this->base= $this->baseSelector->getBase();
223 $this->is_modified= TRUE;
224 }
225 }
227 }
230 /*! \brief Save the modified object back to the ldap.
231 */
232 function save()
233 {
234 plugin::save();
236 // Append and remove dynmic object classes
237 foreach($this->dynClasses as $oc => $attrs){
238 $this->attrs['objectClass'] = array_remove_entries(array($oc), $this->attrs['objectClass']);
239 foreach($attrs as $attr){
240 if(isset($this->attrs[$attr]) && !empty($this->attrs[$attr])){
241 $this->attrs['objectClass'][] = $oc;
242 break;
243 }
244 }
245 }
247 $this->cleanup();
248 $ldap=$this->config->get_ldap_link();
249 $ldap->cd($this->config->current['BASE']);
250 $ldap->create_missing_trees(preg_replace("/^[^,]*+,/","",$this->dn));
251 $ldap->cd($this->dn);
253 // Perform action modify/create
254 if($this->initially_was_account){
255 $ldap->modify($this->attrs);
256 new log("modify","Device/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
257 if (!$ldap->success()){
258 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
259 }else{
260 $this->handle_post_events("modify");
261 }
262 }else{
263 $ldap->add($this->attrs);
264 new log("create","Device/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
265 if (!$ldap->success()){
266 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_ADD, get_class()));
267 }else{
268 $this->handle_post_events("add");
269 }
270 }
271 }
274 static function plInfo()
275 {
276 return (array(
277 "plShortName" => _("Device"),
278 "plDescription" => _("Device"),
279 "plSelfModify" => FALSE,
280 "plDepends" => array(),
281 "plPriority" => 1,
282 "plSection" => array("administration"),
283 "plCategory" =>
284 array(
285 "Device" => array(
286 "description" => _("Device"),
287 "objectClass" => "Device")
288 ),
289 "plProvidedAcls" =>
290 array(
291 "base" => _("Base"),
292 "cn" => _("Name"),
293 "serialNumber" => _("Serial number"),
294 "seeAlso" => _("See also"),
295 "owner" => _("Owner"),
296 "ou" => _("Organizational Unit"),
297 "o" => _("Organization"),
298 "l" => _("Location"),
299 "description" => _("Description"),
300 "manager" => _("Manager"),
301 "deviceUUID" => _("UUID"),
302 "deviceStatus" => _("Status"),
303 "deviceType" => _("Type"),
304 "macAddress" => _("MAC address"),
305 "ipHostNumber" => _("IP address")
306 ),
307 "plProperties" =>
308 array(
309 array(
310 "name" => "DeviceRDN",
311 "type" => "rdn",
312 "default" => "ou=devices,ou=systems,",
313 "description" => _("RDN for device storage."),
314 "check" => "gosaProperty::isRdn",
315 "migrate" => "migrate_deviceRDN",
316 "group" => "plugin",
317 "mandatory" => FALSE
318 ),
320 )
321 )
322 );
323 }
324 }
326 ?>