Code

Do not post names
[gosa.git] / gosa-plugins / goto / admin / devices / class_deviceGeneric.inc
1 <?php
3 class deviceGeneric extends plugin
4 {
5     public $dn            = "";
6     public $cn            = "";
7     public $orig_cn       = "";
8     public $orig_base     = "";
9     public $orig_dn       = "";
10     public $description   = "";
11     public $vendor        = "";
12     public $devID        = "";
13     public $serial        = "";
14     public $base          = ""; 
15     public $types;
16     public $type;
17     public $baseSelector;
19     public $attributes      = array("cn","description","devID","serial","vendor", "type");
20     public $objectclasses   = array("top","gotoDevice");
22     public $CopyPasteVars   = array("orig_cn","description","vendor","devID","serial","base");
24     public function deviceGeneric(&$config,$dn = NULL)
25     {
26         plugin::plugin($config,$dn);
28         $this->is_account = TRUE;
30         $this->types= array("camera" => _("Digital camera"),
31                 "disc" => _("USB stick"),
32                 "cdrom" => _("CD/DVD drive"),);
33         asort($this->types);
35         /* Set class values */
36         if(isset($this->attrs['gotoHotplugDevice'][0])){
37             $tmp = preg_split("/\|/",$this->attrs['gotoHotplugDevice'][0]);
38             $this->cn         = $this->attrs['cn'][0];
39             $this->description= $tmp[0];
40             $this->devID     = $tmp[1];
41             $this->serial     = $tmp[2];
42             $this->vendor     = $tmp[3];
43             if (isset($tmp[4])){
44                 $this->type= $tmp[4];
45             }
46         }     
48         $this->orig_cn = $this->cn;
50         /* Set Base */
51         if ($this->dn == "new"){
52             if(session::is_set('CurrentMainBase')){
53                 $this->base = session::get('CurrentMainBase');
54             }else{
55                 $ui= get_userinfo();
56                 $this->base= dn2base($ui->dn);
57             }
58         } else {
59             $this->base =preg_replace ("/^[^,]+,".preg_quote(get_ou("deviceGeneric", "deviceRDN"), '/')."/i","",$this->dn);
60         }
61         $this->orig_base = $this->base;
62         $this->orig_dn   = $this->dn;
64         /* Instanciate base selector */
65         $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
66         $this->baseSelector->setSubmitButton(false);
67         $this->baseSelector->setHeight(300);
68         $this->baseSelector->update(true);
69     }
72     public function execute()
73     {
74         plugin::execute();
75         $smarty = get_smarty();
77         $smarty->assign("base", $this->baseSelector->render());
78         foreach($this->attributes as $attr){
79             $smarty->assign($attr,set_post($this->$attr));
80         }
82         $tmp = $this->plInfo();
83         foreach($tmp['plProvidedAcls'] as $attr => $desc){
84             $smarty->assign($attr."ACL", $this->getacl($attr));
85         }
87         $smarty->assign("type",set_post($this->type));
88         $smarty->assign ("types", set_post($this->types));
89         return($smarty->fetch(get_template_path("deviceGeneric.tpl",TRUE,dirname(__FILE__))));
90     }
93     public function check()
94     {
95         $message = plugin::check();
97         if($this->cn == "" ||(preg_match("/[^a-z0-9]/i",$this->cn))){
98             $message[]= msgPool::invalid(_("Name"),$this->cn,"/^[a-z0-9]*$/i");
99         }
100         if(preg_match("/[^a-z0-9!\"?.,;:-_\(\) ]/i",$this->description)){
101             $message[]= msgPool::invalid(_("Description"),$this->cn,"/^[a-z0-9!\"?.,;:-_\(\) ]*$/i");
102         }
104         /* Skip serial check if vendor and product id are given */
105         if(preg_match("/^\s+$/i",$this->devID)){
106             $message[]= msgPool::invalid(_("iSerial"),"","01234");
107         }elseif(empty($this->devID) && $this->devID!="0"){
108             $message[]= msgPool::required(_("iSerial"));
109         }
110         if(empty($this->serial) || !$this->is_2byteHex($this->serial)){
111             $message[]= msgPool::invalid(_("Product-ID"),"","","0x1234");
112         }
113         if(empty($this->vendor) || !$this->is_2byteHex($this->vendor)){
114             $message[]= msgPool::invalid(_("Vendor-ID"),"","","0x1234");
115         }
117         // Check if a wrong base was supplied
118         if(!$this->baseSelector->checkLastBaseUpdate()){
119             $message[]= msgPool::check_base();
120         }
122         /* Check if entry already exists */ 
123         if($this->cn != $this->orig_cn || $this->dn == "new"){
124             $ldap = $this->config->get_ldap_link();
125             $ldap->search("(&(objectClass=gotoDevice)(cn=".$this->cn."))",array("cn"));
126             if($ldap->count()){
127                 $message[]= msgPool::duplicated(_("Name"));
128             }
129         }
131         /* Check if we are allowed to create or move this object
132          */
133         if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
134             $message[] = msgPool::permCreate();
135         }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
136             $message[] = msgPool::permMove();
137         }
139         return($message);
140     }
143     public function save_object()
144     {
145         if(isset($_POST['deviceGeneric_posted'])){
146             plugin::save_object();
148             /* Refresh base */
149             if ($this->acl_is_moveable($this->base)){
150                 if (!$this->baseSelector->update()) {
151                     msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
152                 }
153                 if ($this->base != $this->baseSelector->getBase()) {
154                     $this->base= $this->baseSelector->getBase();
155                     $this->is_modified= TRUE;
156                 }
157             }
159         }
160     }
163     public function remove_from_parent()
164     {
165         plugin::remove_from_parent();
166         $ldap = $this->config->get_ldap_link();
167         $ldap->cd($this->config->current['BASE']);
169         $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDeviceDN=".$this->dn."))",array("cn","gotoHotplugDeviceDN"));
170         $skip = FALSE;
171         $obj = array();
172         while($attrs = $ldap->fetch()){
173             $obj[$ldap->getDN()]= $attrs['cn'][0];
174             $skip =TRUE;
175         }
176         if($skip){
177             msg_dialog::display(_("Error"), msgPool::stillInUse(_("Device"), $obj), INFO_DIALOG);
178         }else{
179             $ldap->rmdir_recursive($this->dn);
180         }
181     }
184     public function save()
185     {
186         plugin::save();
188         /* Unset non ldap attributes 
189          */
190         foreach(array("devID","serial","vendor", "type", "description") as $name){
191             if(isset($this->attrs[$name])){
192                 unset($this->attrs[$name]);
193             }
194         }
196         $this->attrs['gotoHotplugDevice'] = "";
197         foreach(array("description","devID","serial","vendor", "type") as $post){
198             $this->attrs['gotoHotplugDevice'] .= $this->$post."|"; 
199         }
200         $this->attrs['gotoHotplugDevice'] = preg_replace("/\|$/","",$this->attrs['gotoHotplugDevice']);
202         $ldap = $this->config->get_ldap_link();
203         $ldap->cd($this->config->current['BASE']);
204         $ldap->cat($this->dn);
205         if($ldap->count()){
206             $ldap->cd($this->dn);
207             $ldap->modify($this->attrs);
208         }else{
209             $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$this->dn));
210             $ldap->cd($this->dn);
211             $ldap->add($this->attrs);
212         }
213         if (!$ldap->success()){
214             msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
215         }
216     }
219     /* check if given str in like this 0xffff*/
220     function is_2byteHex($str)
221     {
222         return !strlen($str) || preg_match("/^(0x|x|)[a-f0-9]{4}$/i",$str);
223     }
226     function PrepareForCopyPaste($source)
227     {
228         plugin::PrepareForCopyPaste($source);
229         $source_o = new deviceGeneric($this->config,$source['dn']);
230         foreach($this->CopyPasteVars as $post){
231             $this->$post = $source_o->$post;
232         }
233     }
236     /* Return a dialog with all fields that must be changed,
237        if we want to copy this entry */
238     function getCopyDialog()
239     {
240         $str = "";
241         $smarty = get_smarty();
242         $smarty->assign("cn",  set_post($this->cn));
243         $str = $smarty->fetch(get_template_path("paste_deviceGeneric.tpl",TRUE,dirname(__FILE__)));
245         $ret = array();
246         $ret['string'] = $str;
247         $ret['status'] = "";
248         return($ret);
249     }
252     /* Save all */
253     function saveCopyDialog()
254     {
255         $attrs = array("cn");
256         foreach($attrs as $attr){
257             if(isset($_POST[$attr])){
258                 $this->$attr = get_post($attr);
259             }
260         }
261     }
265     /* Return plugin informations for acl handling  */
266     public static function plInfo()
267     {
268         return (array(
269                     "plShortName"   => _("Generic"),
270                     "plDescription" => _("Device generic")." ("._("Hot-plug").")",
271                     "plSelfModify"  => FALSE,
272                     "plDepends"     => array(),
273                     "plPriority"    => 0,
274                     "plSection"     => array("administration"),
275                     "plRequirements"=> array(
276                         'ldapSchema' => array('gotoDevice' => '>=2.7'),
277                         'onFailureDisablePlugin' => array(get_class(), 'deviceManagement')
278                         ),
279                     "plCategory"    => array("devices" => array("description"  => _("Devices"),
280                             "objectClass"  => "gotoDevice")),
281                     "plProperties" =>
282                     array(
283                         array(
284                             "name"          => "deviceRDN",
285                             "type"          => "rdn",
286                             "default"       => "ou=devices,",
287                             "description"   => _("The 'deviceRDN' statement defines the location where new devices will be created. The default is 'ou=devices,'."),
288                             "check"         => "gosaProperty::isRdn",
289                             "migrate"       => "migrate_deviceRDN",
290                             "group"         => "plugin",
291                             "mandatory"     => FALSE)),
293             "plProvidedAcls"=> array(
294                     "cn"            => _("Name"),
295                     "base"          => _("Base"),
296                     "description"   => _("Description"),
297                     "type"          => _("Type"),
298                     "serial"        => _("Serial"),
299                     "vendor"        => _("Vendor"), 
300                     "devID"        => _("Device ID"))
301                 ));
303     }
306 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
307 ?>